Completed
Push — dev5 ( 0ca5ae...abba83 )
by Ron
09:59
created

CustomerController::searchID()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 6
nop 1
dl 0
loc 25
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Customers;
6
use App\SystemTypes;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Log;
9
use App\Http\Controllers\Controller;
10
11
class CustomerController extends Controller
12
{
13
    public function __construct()
14
    {
15
        $this->middleware('auth');
16
    }
17
    
18
    //  Landing page to search for customer
19
    public function index()
20
    {
21
        $sysTypes = SystemTypes::all();
22
        
23
        $sysArr = [];
24
        foreach($sysTypes as $sys)
25
        {
26
            $sysArr[] = $sys->name;
27
        }
28
        
29
        return view('customer.index', [
30
            'sysTypes' => $sysArr
31
        ]);
32
    }
33
    
34
    //  Search for the customer based on their ID - For new file link form
35
    public function searchID($id)
36
    {
37
        $id = urldecode($id);
38
        if($id === 'NULL')
39
        {
40
            $id = '';
41
        }
42
        
43
        //  Determine if a customer number/name has already been entered
44
        if(!empty($id))
45
        {
46
            $split = explode(' ', $id);
47
            if(isset($split[1]) && $split[1] === '-')
48
            {
49
                $id = $split[0];
50
            }
51
        }
52
        
53
        $res = Customers::where('cust_id', 'like', '%'.$id.'%')
54
            ->orWhere('name', 'like', '%'.$id.'%')
55
            ->where('active', 1)
56
            ->orderBy('name')
57
            ->get();
58
        
59
        return response()->json($res);
60
    }
61
    
62
    //  Return a full JSON array of the available customers
63
    public function search()
64
    {
65
        $customers = Customers::
66
                with('CustomerSystems.SystemTypes')
67
                ->get();
68
        
69
        $custList = [];
70
        foreach($customers as $cust)
71
        {
72
            $sysArr = '';
73
            foreach($cust->CustomerSystems as $sys)
74
            {
75
                $sysArr .= $sys->SystemTypes->name.'<br />';
76
            }
77
            
78
            $custList[] = [
79
                'name' => $cust->name,
80
                'city' => $cust->city.', '.$cust->state,
81
                'url'  => route('customer.details', [$cust->cust_id, urlencode($cust->name)]),
82
                'sys'  => $sysArr
83
            ];            
84
        }
85
        
86
        return response()->json($custList);
87
    }
88
    
89
    //  Check to see if a customer ID already exists
90
    public function checkID($id)
91
    {
92
        $cust = Customers::find($id);
93
        
94
        if($cust === null)
95
        {
96
            return response()->json(['dup' => false]);
97
        }
98
        
99
        return response()->json(['dup' => true, 'url' => route('customer.details', [$cust->cust_id, urlencode($cust->name)])]);
100
    }
101
}
102