|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
|
4
|
|
|
|
|
5
|
|
|
use Inertia\Inertia; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Support\Facades\Auth; |
|
9
|
|
|
|
|
10
|
|
|
use App\Traits\FileTrait; |
|
11
|
|
|
use App\Http\Controllers\Controller; |
|
12
|
|
|
use App\Events\Customers\Admin\CustomerRestoredEvent; |
|
13
|
|
|
use App\Events\Customers\Admin\CustomerForceDeletedEvent; |
|
14
|
|
|
|
|
15
|
34 |
|
use App\Events\Customers\NewCustomerCreated; |
|
16
|
|
|
use App\Events\Customers\CustomerDetailsUpdated; |
|
17
|
34 |
|
use App\Events\Customers\CustomerDeactivatedEvent; |
|
18
|
34 |
|
|
|
19
|
|
|
use App\Models\Customer; |
|
20
|
|
|
use App\Models\CustomerFile; |
|
21
|
2 |
|
use App\Models\EquipmentType; |
|
22
|
|
|
use App\Models\PhoneNumberType; |
|
23
|
2 |
|
use App\Models\CustomerFileType; |
|
24
|
2 |
|
use App\Models\UserCustomerBookmark; |
|
25
|
|
|
|
|
26
|
|
|
use App\Http\Requests\Customers\NewCustomerRequest; |
|
27
|
|
|
use App\Http\Requests\Customers\EditCustomerRequest; |
|
28
|
|
|
|
|
29
|
16 |
|
class CustomerController extends Controller |
|
30
|
|
|
{ |
|
31
|
16 |
|
use FileTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Search page for finding a customer |
|
35
|
4 |
|
*/ |
|
36
|
|
|
public function index(Request $request) |
|
37
|
4 |
|
{ |
|
38
|
|
|
return Inertia::render('Customers/Index', [ |
|
39
|
4 |
|
'create' => $request->user()->can('create', Customer::class), |
|
40
|
|
|
'equip_types' => EquipmentType::orderBy('cat_id')->get()->pluck('name')->values(), |
|
41
|
2 |
|
]); |
|
42
|
|
|
} |
|
43
|
2 |
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Show the form for creating a new Customer |
|
46
|
|
|
*/ |
|
47
|
4 |
|
public function create() |
|
48
|
|
|
{ |
|
49
|
4 |
|
$this->authorize('create', Customer::class); |
|
50
|
4 |
|
return Inertia::render('Customers/Create'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create a new Customer |
|
55
|
|
|
*/ |
|
56
|
|
|
public function store(NewCustomerRequest $request) |
|
57
|
|
|
{ |
|
58
|
|
|
$cust = $request->toArray(); |
|
59
|
|
|
$cust['slug'] = Str::slug($request->name); |
|
60
|
|
|
$newCust = Customer::create($cust); |
|
61
|
|
|
|
|
62
|
|
|
event(new NewCustomerCreated($newCust)); |
|
63
|
|
|
return redirect(route('customers.show',$newCust->slug))->with(['message' => 'New Customer Created', 'type' => 'success']); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Display the Customers Information |
|
68
|
|
|
*/ |
|
69
|
|
|
public function show($id) |
|
70
|
|
|
{ |
|
71
|
|
|
// Check if we are passing the customer slugged name, or customer ID number |
|
72
|
|
|
if(is_numeric($id)) |
|
73
|
|
|
{ |
|
74
|
|
|
// To keep things uniform, redirect to a link that has the customers name rather than the ID |
|
75
|
|
|
$customer = Customer::findOrFail($id); |
|
76
|
|
|
return redirect(route('customers.show', $customer->slug)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Pull the customers information |
|
80
|
|
|
$customer = Customer::where('slug', $id) |
|
81
|
|
|
->orWhere('cust_id', $id) |
|
82
|
|
|
->with('Parent') |
|
83
|
|
|
->with('CustomerEquipment.CustomerEquipmentData') |
|
84
|
|
|
->with('ParentEquipment.CustomerEquipmentData') |
|
85
|
|
|
->with('CustomerContact.CustomerContactPhone.PhoneNumberType') |
|
86
|
|
|
->with('ParentContact.CustomerContactPhone.PhoneNumberType') |
|
87
|
|
|
->with('CustomerNote') |
|
88
|
|
|
->with('ParentNote') |
|
89
|
|
|
->with('CustomerFile.FileUpload') |
|
90
|
|
|
->with('ParentFile.FileUpload') |
|
91
|
|
|
->firstOrFail(); |
|
92
|
|
|
// Determine if the customer is bookmarked by the user |
|
93
|
|
|
$isFav = UserCustomerBookmark::where('user_id', Auth::user()->user_id) |
|
94
|
|
|
->where('cust_id', $customer->cust_id) |
|
95
|
|
|
->count(); |
|
96
|
|
|
|
|
97
|
|
|
return Inertia::render('Customers/Show', [ |
|
98
|
|
|
'details' => $customer, |
|
99
|
|
|
'phone_types' => PhoneNumberType::all(), |
|
100
|
|
|
'file_types' => CustomerFileType::all(), |
|
101
|
|
|
// User Permissions for customers |
|
102
|
|
|
'user_data' => [ |
|
103
|
|
|
'fav' => $isFav, // Customer is bookmarked by the user |
|
104
|
|
|
'edit' => Auth::user()->can('update', $customer), // User is allowed to edit the customers basic details |
|
105
|
|
|
'manage' => Auth::user()->can('manage', $customer), // User can recover deleted items |
|
106
|
|
|
'deactivate' => Auth::user()->can('delete', $customer), // User can deactivate the customer profile |
|
107
|
|
|
'equipment' => [ |
|
108
|
|
|
'create' => Auth::user()->can('create', CustomerEquipment::class), // If user can add equipment |
|
109
|
|
|
'update' => Auth::user()->can('update', CustomerEquipment::class), // If user can edit equipment |
|
110
|
|
|
'delete' => Auth::user()->can('delete', CustomerEquipment::class), // If user can delete equipment |
|
111
|
|
|
], |
|
112
|
|
|
'contacts' => [ |
|
113
|
|
|
'create' => Auth::user()->can('create', CustomerContact::class), // If user can add contact |
|
114
|
|
|
'update' => Auth::user()->can('update', CustomerContact::class), // If user can edit contact |
|
115
|
|
|
'delete' => Auth::user()->can('delete', CustomerContact::class), // If user can delete contact |
|
116
|
|
|
], |
|
117
|
|
|
'notes' => [ |
|
118
|
|
|
'create' => Auth::user()->can('create', CustomerNote::class), // If user can add note |
|
119
|
|
|
'update' => Auth::user()->can('update', CustomerNote::class), // If user can edit note |
|
120
|
|
|
'delete' => Auth::user()->can('delete', CustomerNote::class), // If user can delete note |
|
121
|
|
|
], |
|
122
|
|
|
'files' => [ |
|
123
|
|
|
'create' => Auth::user()->can('create', CustomerFile::class), // If user can add file |
|
124
|
|
|
'update' => Auth::user()->can('update', CustomerFile::class), // If user can edit file |
|
125
|
|
|
'delete' => Auth::user()->can('delete', CustomerFile::class), // If user can delete file |
|
126
|
|
|
], |
|
127
|
|
|
], |
|
128
|
|
|
]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Update the customers basic details |
|
133
|
|
|
*/ |
|
134
|
|
|
public function update(EditCustomerRequest $request, $id) |
|
135
|
|
|
{ |
|
136
|
|
|
$cust = $request->toArray(); |
|
137
|
|
|
$cust['slug'] = Str::slug($request->name); |
|
138
|
|
|
|
|
139
|
|
|
$data = Customer::findOrFail($id); |
|
140
|
|
|
$data->update($cust); |
|
141
|
|
|
event(new CustomerDetailsUpdated($data, $id)); |
|
142
|
|
|
|
|
143
|
|
|
return redirect(route('customers.show', $cust['slug']))->with(['message' => 'Customer Details Updated', 'type' => 'success']); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Soft Delete a Customer from the database |
|
148
|
|
|
*/ |
|
149
|
|
|
public function destroy($id) |
|
150
|
|
|
{ |
|
151
|
|
|
$cust = Customer::findOrFail($id); |
|
152
|
|
|
$this->authorize('delete', $cust); |
|
153
|
|
|
$cust->delete(); |
|
154
|
|
|
|
|
155
|
|
|
event(new CustomerDeactivatedEvent($cust)); |
|
156
|
|
|
return redirect(route('customers.index'))->with(['message' => 'Customer '.$cust->name.' deactivated', 'type' => 'danger']); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Restore a soft deleted customer |
|
161
|
|
|
*/ |
|
162
|
|
|
public function restore(Request $idArr) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->authorize('restore', Customer::class); |
|
165
|
|
|
|
|
166
|
|
|
// Customer ID's are in an array |
|
167
|
|
|
foreach($idArr->list as $cust) |
|
168
|
|
|
{ |
|
169
|
|
|
$cust = Customer::onlyTrashed()->where('cust_id', $cust['cust_id'])->firstOrFail(); |
|
170
|
|
|
$cust->restore(); |
|
171
|
|
|
|
|
172
|
|
|
event(new CustomerRestoredEvent($cust)); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return back()->with([ |
|
176
|
|
|
'message' => 'Customers Restored', |
|
177
|
|
|
'type' => 'success', |
|
178
|
|
|
]); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Permanently Delete a customer and all associated files and information |
|
183
|
|
|
*/ |
|
184
|
|
|
public function forceDelete(Request $id) |
|
185
|
|
|
{ |
|
186
|
|
|
$this->authorize('forceDelete', Customer::class); |
|
187
|
|
|
|
|
188
|
|
|
// Customer ID's are in an array |
|
189
|
|
|
foreach($id->list as $cust) |
|
190
|
|
|
{ |
|
191
|
|
|
$cust = Customer::onlyTrashed()->where('cust_id', $cust['cust_id'])->firstOrFail(); |
|
192
|
|
|
$fileList = []; |
|
193
|
|
|
|
|
194
|
|
|
// Get all of the files that are attached to the customer |
|
195
|
|
|
$files = CustomerFile::where('cust_id', $cust->cust_id)->get(); |
|
196
|
|
|
foreach($files as $file) |
|
197
|
|
|
{ |
|
198
|
|
|
$fileList[] = $file->file_id; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$cust->forceDelete(); |
|
202
|
|
|
|
|
203
|
|
|
// Delete the files from the Storage System |
|
204
|
|
|
foreach($fileList as $file) |
|
205
|
|
|
{ |
|
206
|
|
|
$this->deleteFile($file); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
event(new CustomerForceDeletedEvent($cust)); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return back()->with([ |
|
213
|
|
|
'message' => 'Customers Deleted', |
|
214
|
|
|
'type' => 'danger', |
|
215
|
|
|
]); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|