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\Http\Controllers\Controller; |
10
|
|
|
use App\Models\UserCustomerBookmark; |
11
|
|
|
use App\Http\Requests\Customers\CustomerRequest; |
12
|
|
|
use App\Models\PhoneNumberType; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
use Illuminate\Support\Facades\Log; |
15
|
|
|
use Illuminate\Support\Facades\Auth; |
16
|
|
|
|
17
|
|
|
class CustomerController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Customer search page |
21
|
|
|
*/ |
22
|
|
|
public function index() |
23
|
|
|
{ |
24
|
|
|
return Inertia::render('Customer/index', [ |
25
|
|
|
'can_create' => Auth::user()->can('create', Customer::class), |
26
|
|
|
'equip_types' => EquipmentType::orderBy('cat_id')->get()->pluck('name')->values(), |
27
|
|
|
]); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Form to create a new customer |
32
|
|
|
*/ |
33
|
|
|
public function create() |
34
|
|
|
{ |
35
|
|
|
$this->authorize('create', Customer::class); |
36
|
|
|
return Inertia::render('Customer/create'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Save a newly created customer |
41
|
|
|
*/ |
42
|
|
|
public function store(CustomerRequest $request) |
43
|
|
|
{ |
44
|
|
|
$cust = $request->toArray(); |
45
|
|
|
$cust['slug'] = Str::slug($request->name); |
46
|
|
|
$newCust = Customer::create($cust); |
47
|
|
|
|
48
|
|
|
Log::channel('cust')->info('New Customer - '.$request->name.' created by '.Auth::user()->full_name); |
|
|
|
|
49
|
|
|
|
50
|
|
|
return redirect(route('customers.show',$newCust->slug))->with(['message' => 'New Customer Created', 'type' => 'success']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Show the customer details |
55
|
|
|
*/ |
56
|
|
|
public function show($id) |
57
|
|
|
{ |
58
|
|
|
// Check if we are passing the customer slugged name, or customer ID number |
59
|
|
|
if(is_numeric($id)) |
60
|
|
|
{ |
61
|
|
|
// To keep things uniform, redirect to a link that has the customers name rather than the ID |
62
|
|
|
$customer = Customer::findOrFail($id); |
63
|
|
|
return redirect(route('customers.show', $customer->slug)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$customer = Customer::where('slug', $id) |
67
|
|
|
->orWhere('cust_id', $id) |
68
|
|
|
->with('Parent') |
69
|
|
|
->with('CustomerEquipment.CustomerEquipmentData') |
70
|
|
|
// ->with('ParentEquipment.CustomerEquipmentData') |
71
|
|
|
->with('CustomerContact.CustomerContactPhone.PhoneNumberType') |
72
|
|
|
// ->with('ParentContact.CustomerContactPhone.PhoneNumberType') |
73
|
|
|
->with('CustomerNote') |
74
|
|
|
->firstOrFail(); |
75
|
|
|
$isFav = UserCustomerBookmark::where('user_id', Auth::user()->user_id) |
|
|
|
|
76
|
|
|
->where('cust_id', $customer->cust_id) |
77
|
|
|
->count(); |
78
|
|
|
|
79
|
|
|
return Inertia::render('Customer/details', [ |
80
|
|
|
'details' => $customer, |
81
|
|
|
'phone_types' => PhoneNumberType::all(), |
82
|
|
|
'user_functions' => [ |
83
|
|
|
'fav' => $isFav, // Customer is bookmarked by the user |
84
|
|
|
'edit' => Auth::user()->can('update', $customer), // User is allowed to edit the customers basic details |
85
|
|
|
], |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* TODO - Go away??? |
91
|
|
|
*/ |
92
|
|
|
// public function edit($id) |
93
|
|
|
// { |
94
|
|
|
// // |
95
|
|
|
// } |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Update the customers basic information |
99
|
|
|
*/ |
100
|
|
|
public function update(CustomerRequest $request, $id) |
101
|
|
|
{ |
102
|
|
|
$cust = $request->toArray(); |
103
|
|
|
$cust['slug'] = Str::slug($request->name); |
104
|
|
|
Customer::find($id)->update($cust); |
105
|
|
|
|
106
|
|
|
return redirect(route('customers.show', $cust['slug']))->with(['message' => 'Customer Details Updated', 'type' => 'success']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Deactivate the customer |
111
|
|
|
*/ |
112
|
|
|
// public function destroy($id) |
113
|
|
|
// { |
114
|
|
|
// // |
115
|
|
|
// } |
116
|
|
|
} |
117
|
|
|
|