1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use App\Events\CustomerDetailsUpdated; |
6
|
|
|
use App\Events\NewCustomerCreated; |
7
|
|
|
use Inertia\Inertia; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use App\Http\Controllers\Controller; |
10
|
|
|
use App\Http\Requests\Customers\EditCustomerRequest; |
11
|
|
|
use App\Http\Requests\Customers\NewCustomerRequest; |
12
|
|
|
use App\Models\Customer; |
13
|
|
|
use App\Models\CustomerFileType; |
14
|
|
|
use App\Models\EquipmentType; |
15
|
|
|
use App\Models\PhoneNumberType; |
16
|
|
|
use App\Models\UserCustomerBookmark; |
17
|
|
|
use Illuminate\Support\Facades\Auth; |
18
|
|
|
use Illuminate\Support\Str; |
19
|
|
|
|
20
|
|
|
class CustomerController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Search page for finding a customer |
24
|
|
|
*/ |
25
|
|
|
public function index(Request $request) |
26
|
|
|
{ |
27
|
|
|
return Inertia::render('Customers/Index', [ |
28
|
|
|
'create' => $request->user()->can('create', Customer::class), |
29
|
1 |
|
'equip_types' => EquipmentType::orderBy('cat_id')->get()->pluck('name')->values(), |
30
|
|
|
]); |
31
|
1 |
|
} |
32
|
1 |
|
|
33
|
1 |
|
/** |
34
|
|
|
* Show the form for creating a new Customer |
35
|
|
|
*/ |
36
|
|
|
public function create() |
37
|
|
|
{ |
38
|
|
|
$this->authorize('create', Customer::class); |
39
|
|
|
return Inertia::render('Customers/Create'); |
40
|
2 |
|
} |
41
|
|
|
|
42
|
2 |
|
/** |
43
|
1 |
|
* Create a new Customer |
44
|
|
|
*/ |
45
|
|
|
public function store(NewCustomerRequest $request) |
46
|
|
|
{ |
47
|
|
|
$cust = $request->toArray(); |
48
|
|
|
$cust['slug'] = Str::slug($request->name); |
49
|
1 |
|
$newCust = Customer::create($cust); |
50
|
|
|
|
51
|
1 |
|
event(new NewCustomerCreated($newCust)); |
52
|
1 |
|
return redirect(route('customers.show',$newCust->slug))->with(['message' => 'New Customer Created', 'type' => 'success']); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
/** |
56
|
|
|
* Display the Customers Information |
57
|
1 |
|
*/ |
58
|
|
|
public function show($id) |
59
|
|
|
{ |
60
|
|
|
// Check if we are passing the customer slugged name, or customer ID number |
61
|
|
|
if(is_numeric($id)) |
62
|
|
|
{ |
63
|
3 |
|
// To keep things uniform, redirect to a link that has the customers name rather than the ID |
64
|
|
|
$customer = Customer::findOrFail($id); |
65
|
|
|
return redirect(route('customers.show', $customer->slug)); |
66
|
3 |
|
} |
67
|
|
|
|
68
|
|
|
// Pull the customers information |
69
|
1 |
|
$customer = Customer::where('slug', $id) |
70
|
1 |
|
->orWhere('cust_id', $id) |
71
|
|
|
->with('Parent') |
72
|
|
|
->with('CustomerEquipment.CustomerEquipmentData') |
73
|
2 |
|
->with('ParentEquipment.CustomerEquipmentData') |
74
|
2 |
|
->with('CustomerContact.CustomerContactPhone.PhoneNumberType') |
75
|
2 |
|
->with('ParentContact.CustomerContactPhone.PhoneNumberType') |
76
|
2 |
|
->with('CustomerNote') |
77
|
2 |
|
->with('ParentNote') |
78
|
2 |
|
->with('CustomerFile.FileUpload') |
79
|
2 |
|
->with('ParentFile.FileUpload') |
80
|
2 |
|
->firstOrFail(); |
81
|
2 |
|
// Determine if the customer is bookmarked by the user |
82
|
2 |
|
$isFav = UserCustomerBookmark::where('user_id', Auth::user()->user_id) |
|
|
|
|
83
|
2 |
|
->where('cust_id', $customer->cust_id) |
84
|
2 |
|
->count(); |
85
|
1 |
|
|
86
|
1 |
|
return Inertia::render('Customers/Show', [ |
87
|
1 |
|
'details' => $customer, |
88
|
|
|
// 'phone_types' => PhoneNumberType::all(), |
89
|
1 |
|
// 'file_types' => CustomerFileType::all(), |
90
|
1 |
|
// User Permissions for customers |
91
|
1 |
|
'user_data' => [ |
92
|
1 |
|
'fav' => $isFav, // Customer is bookmarked by the user |
93
|
|
|
'edit' => Auth::user()->can('update', $customer), // User is allowed to edit the customers basic details |
94
|
1 |
|
'manage' => Auth::user()->can('manage', $customer), // User can recover deleted items |
95
|
1 |
|
'deactivate' => Auth::user()->can('delete', $customer), // User can deactivate the customer profile |
96
|
1 |
|
// 'equipment' => [ |
97
|
1 |
|
// 'create' => Auth::user()->can('create', CustomerEquipment::class), // If user can add equipment |
98
|
|
|
// 'update' => Auth::user()->can('update', CustomerEquipment::class), // If user can edit equipment |
99
|
1 |
|
// 'delete' => Auth::user()->can('delete', CustomerEquipment::class), // If user can delete eqipment |
100
|
1 |
|
// ], |
101
|
1 |
|
// 'contacts' => [ |
102
|
|
|
// 'create' => Auth::user()->can('create', CustomerContact::class), // If user can add contact |
103
|
|
|
// 'update' => Auth::user()->can('update', CustomerContact::class), // If user can edit contact |
104
|
1 |
|
// 'delete' => Auth::user()->can('delete', CustomerContact::class), // If user can delete contact |
105
|
1 |
|
// ], |
106
|
1 |
|
// 'notes' => [ |
107
|
|
|
// 'create' => Auth::user()->can('create', CustomerNote::class), // If user can add note |
108
|
|
|
// 'update' => Auth::user()->can('update', CustomerNote::class), // If user can edit note |
109
|
1 |
|
// 'delete' => Auth::user()->can('delete', CustomerNote::class), // If user can delete note |
110
|
1 |
|
// ], |
111
|
1 |
|
// 'files' => [ |
112
|
|
|
// 'create' => Auth::user()->can('create', CustomerFile::class), // If user can add file |
113
|
|
|
// 'update' => Auth::user()->can('update', CustomerFile::class), // If user can edit file |
114
|
1 |
|
// 'delete' => Auth::user()->can('delete', CustomerFile::class), // If user can delete file |
115
|
1 |
|
// ], |
116
|
1 |
|
], |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Update the customers basic details |
122
|
|
|
*/ |
123
|
|
|
public function update(EditCustomerRequest $request, $id) |
124
|
|
|
{ |
125
|
1 |
|
$cust = $request->toArray(); |
126
|
|
|
$cust['slug'] = Str::slug($request->name); |
127
|
1 |
|
|
128
|
1 |
|
$data = Customer::findOrFail($id); |
129
|
1 |
|
$data->update($cust); |
130
|
|
|
event(new CustomerDetailsUpdated($data, $id)); |
131
|
1 |
|
|
132
|
|
|
return redirect(route('customers.show', $cust['slug']))->with(['message' => 'Customer Details Updated', 'type' => 'success']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Remove the specified resource from storage. |
137
|
2 |
|
* |
138
|
|
|
* @param int $id |
139
|
2 |
|
* @return \Illuminate\Http\Response |
140
|
|
|
*/ |
141
|
2 |
|
public function destroy($id) |
|
|
|
|
142
|
1 |
|
{ |
143
|
|
|
// |
144
|
1 |
|
} |
145
|
|
|
} |
146
|
|
|
|