1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use App\Customers; |
6
|
|
|
use App\CustomerFavs; |
7
|
|
|
use App\PhoneNumberTypes; |
8
|
|
|
use App\CustomerFileTypes; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Support\Facades\Log; |
11
|
|
|
use Illuminate\Support\Facades\Auth; |
12
|
|
|
use App\Http\Controllers\Controller; |
13
|
|
|
use Illuminate\Support\Facades\Route; |
14
|
|
|
use App\Http\Resources\PhoneNumberTypesCollection; |
15
|
|
|
use App\Http\Resources\CustomerFileTypesCollection; |
16
|
|
|
|
17
|
|
|
class CustomerDetailsController extends Controller |
18
|
|
|
{ |
19
|
44 |
|
public function __construct() |
20
|
|
|
{ |
21
|
44 |
|
$this->middleware('auth'); |
22
|
44 |
|
} |
23
|
|
|
|
24
|
|
|
// New Customer Form |
25
|
4 |
View Code Duplication |
public function create() |
|
|
|
|
26
|
|
|
{ |
27
|
4 |
|
$this->authorize('hasAccess', 'Add Customer'); |
28
|
|
|
|
29
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
|
|
|
|
30
|
2 |
|
return view('customer.newCustomer'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Submit the new customer form |
34
|
10 |
|
public function store(Request $request) |
35
|
|
|
{ |
36
|
10 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
|
|
|
|
37
|
10 |
|
$this->authorize('hasAccess', 'Add Customer'); |
38
|
|
|
|
39
|
8 |
|
$request->validate([ |
|
|
|
|
40
|
8 |
|
'cust_id' => 'nullable|numeric|unique:customers,cust_id', |
41
|
|
|
'parent_id' => 'nullable|numeric|exists:customers,cust_id', |
42
|
|
|
'name' => 'required', |
43
|
|
|
'dba_name' => 'nullable', |
44
|
|
|
'address' => 'required', |
45
|
|
|
'city' => 'required', |
46
|
|
|
'zip' => 'required|numeric' |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
// Remove any forward slash (/) from the Customer name field |
50
|
6 |
|
$request->merge(['name' => str_replace('/', '-', $request->name)]); |
51
|
|
|
|
52
|
|
|
// Check if the parent ID noted, has a parent of its own |
53
|
6 |
|
if($request->parent_id) |
54
|
|
|
{ |
55
|
4 |
|
$parentsParent = Customers::find($request->parent_id); |
56
|
|
|
|
57
|
4 |
|
if($parentsParent->parent_id) |
58
|
|
|
{ |
59
|
2 |
|
$request->parent_id = $parentsParent->parent_id; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
$custData = Customers::create([ |
|
|
|
|
64
|
6 |
|
'cust_id' => $request->cust_id, |
65
|
6 |
|
'parent_id' => $request->parent_id, |
66
|
6 |
|
'name' => $request->name, |
67
|
6 |
|
'dba_name' => $request->dba_name, |
68
|
6 |
|
'address' => $request->address, |
69
|
6 |
|
'city' => $request->city, |
70
|
6 |
|
'state' => $request->selectedState, |
71
|
6 |
|
'zip' => $request->zip, |
72
|
|
|
]); |
73
|
|
|
|
74
|
6 |
|
Log::notice('New Customer ID-'.$request->custID.' created by '.Auth::user()->full_name); |
|
|
|
|
75
|
6 |
|
return response()->json(['success' => true, 'cust_id' => $custData->cust_id]); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Show the customer details |
79
|
4 |
|
public function details($id, $name) |
80
|
|
|
{ |
81
|
4 |
|
$custDetails = Customers::find($id); |
82
|
|
|
|
83
|
4 |
|
if($custDetails === null) |
84
|
|
|
{ |
85
|
2 |
|
Log::info('User - '.Auth::user()->user_id.' visited invalid customer ID - '.$id.' - '.$name); |
|
|
|
|
86
|
2 |
|
return view('customer.customerNotFound'); |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
$custFav = CustomerFavs::where('user_id', Auth::user()->user_id)->where('cust_id', $custDetails->cust_id)->first(); |
|
|
|
|
90
|
2 |
|
$numTypes = new PhoneNumberTypesCollection(PhoneNumberTypes::all()); |
91
|
2 |
|
$fileTypes = new CustomerFileTypesCollection(CustomerFileTypes::all()); |
92
|
2 |
|
$parent = $custDetails->parent_id ? Customers::find($custDetails->parent_id)->name : null; |
93
|
|
|
|
94
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
|
|
|
|
95
|
2 |
|
return view('customer.details', [ |
96
|
2 |
|
'cust_id' => $custDetails->cust_id, |
97
|
2 |
|
'details' => $custDetails->toJson(), |
98
|
2 |
|
'isFav' => empty($custFav) ? 'false' : 'true', |
99
|
2 |
|
'numberTypes' => $numTypes, |
100
|
2 |
|
'fileTypes' => $fileTypes, |
101
|
2 |
|
'parent' => $parent, |
102
|
2 |
|
'linked' => $custDetails->child_count || $parent ? 'true' : 'false', |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Update the customer details |
107
|
6 |
|
public function update(Request $request, $id) |
108
|
|
|
{ |
109
|
6 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
|
|
|
|
110
|
|
|
|
111
|
6 |
|
$request->validate([ |
|
|
|
|
112
|
6 |
|
'name' => 'required', |
113
|
|
|
'dba_name' => 'nullable', |
114
|
|
|
'address' => 'required', |
115
|
|
|
'city' => 'required', |
116
|
|
|
'state' => 'required', |
117
|
|
|
'zip' => 'required|numeric' |
118
|
|
|
]); |
119
|
|
|
|
120
|
2 |
|
Customers::find($id)->update([ |
121
|
2 |
|
'name' => $request->name, |
122
|
2 |
|
'dba_name' => $request->dba_name, |
123
|
2 |
|
'address' => $request->address, |
124
|
2 |
|
'city' => $request->city, |
125
|
2 |
|
'state' => $request->state, |
126
|
2 |
|
'zip' => $request->zip |
127
|
|
|
]); |
128
|
|
|
|
129
|
2 |
|
Log::info('Customer Details Updated for Customer ID-'.$id.' by User ID-'.Auth::user()->user_id); |
|
|
|
|
130
|
2 |
|
return response()->json([ |
|
|
|
|
131
|
2 |
|
'success' => true |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Link a site to a parent site |
136
|
4 |
|
public function linkParent(Request $request) |
137
|
|
|
{ |
138
|
4 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
|
|
|
|
139
|
|
|
|
140
|
4 |
|
$request->validate([ |
|
|
|
|
141
|
4 |
|
'parent_id' => 'required|numeric|exists:customers,cust_id', |
142
|
|
|
'cust_id' => 'required|numeric|exists:customers,cust_id' |
143
|
|
|
]); |
144
|
|
|
|
145
|
4 |
|
$parentsParent = Customers::find($request->parent_id); |
146
|
|
|
|
147
|
4 |
|
if($parentsParent->parent_id) |
148
|
|
|
{ |
149
|
2 |
|
$request->parent_id = $parentsParent->parent_id; |
150
|
|
|
} |
151
|
|
|
|
152
|
4 |
|
Customers::find($request->cust_id)->update([ |
153
|
4 |
|
'parent_id' => $request->parent_id, |
154
|
|
|
]); |
155
|
|
|
|
156
|
4 |
|
Log::info('Customer ID '.$request->cust_id.' was linked to parent ID '.$request->parent_id.' by '.Auth::user()->full_name); |
|
|
|
|
157
|
4 |
|
return response()->json(['success' => true]); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
2 |
|
public function removeParent($id) |
162
|
|
|
{ |
163
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
|
|
|
|
164
|
2 |
|
Customers::find($id)->update(['parent_id' => null]); |
165
|
2 |
|
Log::info('Parent Customer ID was removed for Customer ID '.$id.' by '.Auth::user()->full_name); |
|
|
|
|
166
|
2 |
|
} |
167
|
|
|
|
168
|
|
|
// Deactivate a customer - note this will not remove it from the database, but make it inaccessable |
169
|
4 |
|
public function destroy($id) |
170
|
|
|
{ |
171
|
4 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
|
|
|
|
172
|
4 |
|
$this->authorize('hasAccess', 'Deactivate Customer'); |
173
|
|
|
|
174
|
|
|
// Remove the customer from any users favorites |
175
|
2 |
|
CustomerFavs::where('cust_id', $id)->delete(); |
176
|
|
|
|
177
|
|
|
// Disable the tip |
178
|
2 |
|
Customers::destroy($id); |
179
|
|
|
|
180
|
2 |
|
Log::notice('User - '.Auth::user()->full_name.' has deactivated Customer ID '.$id); |
|
|
|
|
181
|
2 |
|
} |
182
|
|
|
} |
183
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.