1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use Inertia\Inertia; |
6
|
|
|
|
7
|
|
|
use App\Models\Customer; |
8
|
|
|
use App\Models\EquipmentType; |
9
|
|
|
use App\Models\PhoneNumberType; |
10
|
|
|
use App\Models\CustomerFileType; |
11
|
|
|
use App\Http\Controllers\Controller; |
12
|
|
|
use App\Models\UserCustomerBookmark; |
13
|
|
|
use App\Http\Requests\Customers\CustomerRequest; |
14
|
|
|
|
15
|
|
|
use Illuminate\Support\Str; |
16
|
|
|
use Illuminate\Support\Facades\Log; |
17
|
|
|
use Illuminate\Support\Facades\Auth; |
18
|
|
|
|
19
|
|
|
class CustomerController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Customer search page |
23
|
|
|
*/ |
24
|
|
|
public function index() |
25
|
|
|
{ |
26
|
|
|
return Inertia::render('Customer/index', [ |
27
|
|
|
'can_create' => Auth::user()->can('create', Customer::class), |
28
|
|
|
'equip_types' => EquipmentType::orderBy('cat_id')->get()->pluck('name')->values(), |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Form to create a new customer |
34
|
|
|
*/ |
35
|
|
|
public function create() |
36
|
|
|
{ |
37
|
|
|
$this->authorize('create', Customer::class); |
38
|
|
|
return Inertia::render('Customer/create'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Save a newly created customer |
43
|
|
|
*/ |
44
|
|
|
public function store(CustomerRequest $request) |
45
|
|
|
{ |
46
|
|
|
$cust = $request->toArray(); |
47
|
|
|
$cust['slug'] = Str::slug($request->name); |
48
|
|
|
$newCust = Customer::create($cust); |
49
|
|
|
|
50
|
|
|
Log::channel('cust')->info('New Customer - '.$request->name.' created by '.Auth::user()->full_name); |
|
|
|
|
51
|
|
|
|
52
|
|
|
return redirect(route('customers.show',$newCust->slug))->with(['message' => 'New Customer Created', 'type' => 'success']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Show the customer details |
57
|
|
|
*/ |
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
|
|
|
// 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
|
|
|
} |
67
|
|
|
|
68
|
|
|
$customer = Customer::where('slug', $id) |
69
|
|
|
->orWhere('cust_id', $id) |
70
|
|
|
->with('Parent') |
71
|
|
|
->with('CustomerEquipment.CustomerEquipmentData') |
72
|
|
|
->with('ParentEquipment.CustomerEquipmentData') |
73
|
|
|
->with('CustomerContact.CustomerContactPhone.PhoneNumberType') |
74
|
|
|
// ->with('ParentContact.CustomerContactPhone.PhoneNumberType') |
75
|
|
|
->with('CustomerNote') |
76
|
|
|
// ->with('ParentNote') |
77
|
|
|
->with('CustomerFile.FileUpload') |
78
|
|
|
// ->with('ParentFile') |
79
|
|
|
->firstOrFail(); |
80
|
|
|
$isFav = UserCustomerBookmark::where('user_id', Auth::user()->user_id) |
|
|
|
|
81
|
|
|
->where('cust_id', $customer->cust_id) |
82
|
|
|
->count(); |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
// return $customer; |
86
|
|
|
|
87
|
|
|
return Inertia::render('Customer/details', [ |
88
|
|
|
'details' => $customer, |
89
|
|
|
'phone_types' => PhoneNumberType::all(), |
90
|
|
|
'file_types' => CustomerFileType::all(), |
91
|
|
|
'user_functions' => [ |
92
|
|
|
'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
|
|
|
'manage' => Auth::user()->can('manage', $customer), // User can recover deleted items |
95
|
|
|
'deactivate' => Auth::user()->can('delete', $customer), // User can deactivate the customer profile |
96
|
|
|
], |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Update the customers basic information |
102
|
|
|
*/ |
103
|
|
|
public function update(CustomerRequest $request, $id) |
104
|
|
|
{ |
105
|
|
|
$cust = $request->toArray(); |
106
|
|
|
$cust['slug'] = Str::slug($request->name); |
107
|
|
|
Customer::find($id)->update($cust); |
108
|
|
|
|
109
|
|
|
return redirect(route('customers.show', $cust['slug']))->with(['message' => 'Customer Details Updated', 'type' => 'success']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Deactivate the customer |
114
|
|
|
*/ |
115
|
|
|
public function destroy($id) |
116
|
|
|
{ |
117
|
|
|
$cust = Customer::findOrFail($id); |
118
|
|
|
|
119
|
|
|
$this->authorize('delete', $cust); |
120
|
|
|
$cust->delete(); |
121
|
|
|
|
122
|
|
|
Log::channel('cust')->alert('Customer ID '.$id.' has been deactivated by '.Auth::user()->username); |
|
|
|
|
123
|
|
|
return redirect(route('customers.index'))->with(['message' => 'Customer '.$cust->name.' deactivated', 'type' => 'danger']); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|