1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
|
7
|
|
|
use App\Domains\Equipment\GetEquipment; |
8
|
|
|
use App\Domains\Customers\CustomerSearch; |
9
|
|
|
use App\Domains\Customers\SetCustomerDetails; |
10
|
|
|
use App\Domains\User\GetUserStats; |
11
|
|
|
use App\Domains\User\SetUserFavorites; |
12
|
|
|
use App\Http\Requests\Customers\NewCustomerRequest; |
13
|
|
|
use App\Http\Requests\Customers\CustomerSearchRequest; |
14
|
|
|
use App\Http\Requests\Customers\LinkParentRequest; |
15
|
|
|
use Illuminate\Support\Facades\Auth; |
16
|
|
|
use Illuminate\Support\Facades\Log; |
17
|
|
|
|
18
|
|
|
class CustomerController extends Controller |
19
|
|
|
{ |
20
|
2 |
|
public function index() |
21
|
|
|
{ |
22
|
2 |
|
return view('customers.index', [ |
23
|
2 |
|
'equipList' => (new GetEquipment)->getEquipmentArray(), |
24
|
|
|
]); |
25
|
|
|
} |
26
|
|
|
|
27
|
2 |
|
public function search(CustomerSearchRequest $request) |
28
|
|
|
{ |
29
|
2 |
|
return (new CustomerSearch)->search($request); |
30
|
|
|
} |
31
|
|
|
|
32
|
4 |
|
public function toggleFav($custID) |
33
|
|
|
{ |
34
|
4 |
|
$result = (new SetUserFavorites)->toggleCustomerFavorite($custID, Auth::user()->user_id); |
35
|
4 |
|
return response()->json(['success' => true, 'favorite' => $result]); |
36
|
|
|
} |
37
|
|
|
|
38
|
4 |
|
public function details($id) |
39
|
|
|
{ |
40
|
4 |
|
$custObj = new CustomerSearch; |
41
|
4 |
|
$details = $custObj->searchID($id); |
42
|
4 |
|
if(!$details) |
43
|
|
|
{ |
44
|
2 |
|
abort(404, 'The customer you are looking for does not exist or cannot be found'); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
$isFav = (new GetUserStats(Auth::user()->user_id))->checkCustomerForFav($details->cust_id); |
48
|
2 |
|
return view('customers.details', [ |
49
|
2 |
|
'details' => $details, |
50
|
2 |
|
'parent' => $details->parent_id ? $custObj->searchID($details->parent_id) : null, |
51
|
2 |
|
'isFav' => $isFav ? true : false, |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
public function checkID($id) |
56
|
|
|
{ |
57
|
4 |
|
$cust = (new CustomerSearch)->searchID($id); |
58
|
4 |
|
if($cust === null) |
59
|
|
|
{ |
60
|
2 |
|
return response()->json(['duplicate' => false]); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
return response()->json(['duplicate' => true, 'name' => $cust->name, 'url' => route('customer.details', [$cust->cust_id, urlencode($cust->name)])]); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
public function store(NewCustomerRequest $request) |
67
|
|
|
{ |
68
|
2 |
|
$newID = (new SetCustomerDetails)->createCustomer($request); |
69
|
2 |
|
Log::info('New Customer ID '.$newID.' created. Details - ', $request->toArray()); |
70
|
2 |
|
return response()->json(['success' => true, 'cust_id' => $newID]); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function update(NewCustomerRequest $request, $id) |
74
|
|
|
{ |
75
|
2 |
|
(new SetCustomerDetails)->updateCustomer($id, $request); |
76
|
2 |
|
Log::info('Customer ID '.$id.' updated by '.Auth::user()->full_name.'. Details - ', $request->toArray()); |
77
|
2 |
|
return response()->json(['success' => true]); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function linkParent(LinkParentRequest $request) |
81
|
|
|
{ |
82
|
2 |
|
(new SetCustomerDetails)->linkParent($request); |
83
|
2 |
|
return response()->json(['success' => true]); |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
public function delete($custID) |
87
|
|
|
{ |
88
|
2 |
|
(new SetCustomerDetails)->deactivateCustomer($custID); |
89
|
2 |
|
Log::notice('Customer ID '.$custID.' deactivated by '.Auth::user()->full_name); |
90
|
2 |
|
return response()->json(['success' => true]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|