Passed
Branch dev5 (c43caf)
by Ron
07:00
created

CustomerController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 26.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 152
ccs 12
cts 45
cp 0.2667
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toggleFav() 0 23 3
A checkID() 0 14 2
A index() 0 13 2
A __construct() 0 3 1
A search() 0 54 2
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Customers;
6
use App\SystemTypes;
7
use App\CustomerFavs;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Support\Facades\Auth;
11
use App\Http\Controllers\Controller;
12
use Illuminate\Support\Facades\Route;
13
use App\Http\Resources\CustomerCollection;
14
15
class CustomerController extends Controller
16
{
17 6
    public function __construct()
18
    {
19 6
        $this->middleware('auth');
20 6
    }
21
22
    //  Landing page to search for customer
23
    public function index()
24
    {
25
        $sysTypes = SystemTypes::all();
26
27
        $sysArr = [];
28
        foreach($sysTypes as $sys)
29
        {
30
            $sysArr[] = $sys->name;
1 ignored issue
show
Bug introduced by
The property name does not seem to exist on App\SystemTypes. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
31
        }
32
33
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
34
        return view('customer.index', [
35
            'sysTypes' => $sysArr
36
        ]);
37
    }
38
39
    //  Search for the customer based on their ID - For new file link form
40
    // public function searchID($id)
41
    // {
42
    //     $id = urldecode($id);
43
    //     if($id === 'NULL')
44
    //     {
45
    //         $id = '';
46
    //     }
47
48
    //     //  Determine if a customer number/name has already been entered
49
    //     if(!empty($id))
50
    //     {
51
    //         $split = explode(' ', $id);
52
    //         if(isset($split[1]) && $split[1] === '-')
53
    //         {
54
    //             $id = $split[0];
55
    //         }
56
    //     }
57
58
    //     $res = Customers::where('cust_id', 'like', '%'.$id.'%')
59
    //         ->orWhere('name', 'like', '%'.$id.'%')
60
    //         ->where('active', 1)
61
    //         ->orderBy('name')
62
    //         ->get();
63
64
    //     Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
65
    //     Log::debug('ID Entered - '.$id.' Resolution - '.$res);
66
    //     return response()->json($res);
67
    // }
68
69
    //  Search for a customer
70 6
    public function search(Request $request)
71
    {
72
        // $customers = Customers::
73
        //         with('CustomerSystems.SystemTypes')
74
        //         ->get();
75
76
        // $custList = [];
77
        // foreach($customers as $cust)
78
        // {
79
        //     $sysArr = '';
80
        //     foreach($cust->CustomerSystems as $sys)
81
        //     {
82
        //         $sysArr .= $sys->SystemTypes->name.'<br />';
83
        //     }
84
85
        //     $custList[] = [
86
        //         'cust_id' => $cust->cust_id,
87
        //         'name' => $cust->name,
88
        //         'dba'  => $cust->dba_name,
89
        //         'city' => $cust->city.', '.$cust->state,
90
        //         'url'  => route('customer.details', [$cust->cust_id, urlencode($cust->name)]),
91
        //         'sys'  => $sysArr
92
        //     ];
93
94
95
        // }
96
97
        // Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
98
        // Log::debug('Customer Data - ', $custList);
99
        // return response()->json($custList);
100
101
102
        // $searchResults = new CustomerCollection(
103
        //     Customers::where('cust_id', $request->search)
104
        //         ->orWhere('name', $request->search)
105
        //         ->orWhere('dba_name', $request->search)
106
        //         ->get()
107
        //     );
108
109 6
        if($request->search)
110
        {
111 4
            $searchResults = new CustomerCollection(
112 4
            Customers::where('cust_id', 'like', '%'.$request->search.'%')
113 4
                ->orWhere('name', 'like', '%'.$request->search.'%')
114 4
                ->orWhere('dba_name', 'like', '%'.$request->search.'%')
115 4
                ->get()
116
            );
117
        }
118
        else
119
        {
120 2
            $searchResults = new CustomerCollection(Customers::all());
121
        }
122
123 6
        return $searchResults;
124
    }
125
126
    //  Check to see if a customer ID already exists
127
    public function checkID($id)
128
    {
129
        $cust = Customers::find($id);
130
131
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
132
        if($cust === null)
133
        {
134
            Log::debug('Customer ID - '.$id.' is available to use');
135
            return response()->json(['dup' => false]);
136
        }
137
138
139
        Log::debug('Customer ID is in use by - '.$cust->name);
1 ignored issue
show
Bug introduced by
The property name does not seem to exist on App\Customers. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
140
        return response()->json(['dup' => true, 'url' => route('customer.details', [$cust->cust_id, urlencode($cust->name)])]);
1 ignored issue
show
Bug introduced by
The property cust_id does not seem to exist on App\Customers. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
141
    }
142
143
    //  Toggle whether or not the customer is listed as a user favorite
144
    public function toggleFav($action, $id)
145
    {
146
        switch($action)
147
        {
148
            case 'add':
149
                CustomerFavs::create([
150
                    'user_id' => Auth::user()->user_id,
151
                    'cust_id' => $id
152
                ]);
153
                break;
154
            case 'remove':
155
                $custFav = CustomerFavs::where('user_id', Auth::user()->user_id)->where('cust_id', $id)->first();
156
                $custFav->delete();
157
                break;
158
        }
159
160
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
161
        Log::debug('Customer Bookmark Updated.', [
162
            'user_id' => Auth::user()->user_id,
163
            'cust_id' => $id,
164
            'action'  => $action
165
        ]);
166
        return response()->json(['success' => true]);
167
    }
168
}
169