|
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\CustomersCollection; |
|
14
|
|
|
use App\Http\Resources\SystemTypesCollection; |
|
15
|
|
|
|
|
16
|
|
|
class CustomerController extends Controller |
|
17
|
|
|
{ |
|
18
|
22 |
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
22 |
|
$this->middleware('auth'); |
|
21
|
22 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
// Landing page to search for customer |
|
24
|
2 |
|
public function index() |
|
25
|
|
|
{ |
|
26
|
2 |
|
$systems = new SystemTypesCollection(SystemTypes::orderBy('cat_id', 'ASC')->orderBy('name', 'ASC')->get()); |
|
27
|
|
|
|
|
28
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
29
|
2 |
|
return view('customer.index', [ |
|
30
|
2 |
|
'sysTypes' => $systems |
|
31
|
|
|
]); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Search for a customer |
|
35
|
16 |
|
public function search(Request $request) |
|
36
|
|
|
{ |
|
37
|
16 |
|
$request->validate([ |
|
38
|
16 |
|
'sortField' => 'required', |
|
39
|
|
|
'sortType' => 'required', |
|
40
|
|
|
'perPage' => 'required' |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
16 |
|
Log::debug('Request Data - ', $request->toArray()); |
|
44
|
|
|
|
|
45
|
16 |
|
if(isset($request->name) || isset($request->city) || isset($request->system)) |
|
46
|
|
|
{ |
|
47
|
10 |
|
$searchResults = new CustomersCollection( |
|
48
|
10 |
|
Customers::orderBy($request->sortField, $request->sortType) |
|
49
|
|
|
// Search the name, dba name, and cust id columns |
|
50
|
|
|
->where(function($query) use ($request) |
|
51
|
|
|
{ |
|
52
|
10 |
|
$query->where('name', 'like', '%' . $request->name . '%') |
|
53
|
10 |
|
->orWhere('cust_id', 'like', '%' . $request->name . '%') |
|
54
|
10 |
|
->orWhere('dba_name', 'like', '%' . $request->name . '%'); |
|
55
|
10 |
|
}) |
|
56
|
|
|
// Search the city column |
|
57
|
10 |
|
->where('city', 'like', '%' . $request->city . '%') |
|
58
|
|
|
// Include the customers systems |
|
59
|
10 |
|
->with('CustomerSystems.SystemTypes') |
|
60
|
|
|
// If the system field is present - search for system type |
|
61
|
|
|
->when($request->system, function($query) use ($request) |
|
62
|
|
|
{ |
|
63
|
|
|
$query->whereHas('CustomerSystems.SystemTypes', function($query) use ($request) |
|
64
|
|
|
{ |
|
65
|
|
|
$query->where('sys_id', $request->system); |
|
66
|
|
|
}); |
|
67
|
10 |
|
}) |
|
68
|
10 |
|
->paginate($request->perPage) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
else |
|
72
|
|
|
{ |
|
73
|
6 |
|
$searchResults = new CustomersCollection(Customers::orderBy($request->sortField, $request->sortType)->with('CustomerSystems.SystemTypes')->paginate($request->perPage)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
16 |
|
return $searchResults; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
// Check to see if a customer ID already exists |
|
92
|
|
|
public function checkID($id) |
|
93
|
|
|
{ |
|
94
|
|
|
$cust = Customers::find($id); |
|
95
|
|
|
|
|
96
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
97
|
|
|
if($cust === null) |
|
98
|
|
|
{ |
|
99
|
|
|
Log::debug('Customer ID - '.$id.' is available to use'); |
|
100
|
|
|
return response()->json(['dup' => false]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
Log::debug('Customer ID is in use by - '.$cust->name); |
|
105
|
|
|
return response()->json(['dup' => true, 'url' => route('customer.details', [$cust->cust_id, urlencode($cust->name)])]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Toggle whether or not the customer is listed as a user favorite |
|
109
|
|
|
public function toggleFav($action, $id) |
|
110
|
|
|
{ |
|
111
|
|
|
switch($action) |
|
112
|
|
|
{ |
|
113
|
|
|
case 'add': |
|
114
|
|
|
CustomerFavs::create([ |
|
115
|
|
|
'user_id' => Auth::user()->user_id, |
|
116
|
|
|
'cust_id' => $id |
|
117
|
|
|
]); |
|
118
|
|
|
break; |
|
119
|
|
|
case 'remove': |
|
120
|
|
|
$custFav = CustomerFavs::where('user_id', Auth::user()->user_id)->where('cust_id', $id)->first(); |
|
121
|
|
|
$custFav->delete(); |
|
122
|
|
|
break; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
126
|
|
|
Log::debug('Customer Bookmark Updated.', [ |
|
127
|
|
|
'user_id' => Auth::user()->user_id, |
|
128
|
|
|
'cust_id' => $id, |
|
129
|
|
|
'action' => $action |
|
130
|
|
|
]); |
|
131
|
|
|
return response()->json(['success' => true]); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|