|
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
|
2 |
|
{ |
|
20
|
|
|
public function index() |
|
21
|
2 |
|
{ |
|
22
|
2 |
|
return view('customers.index', [ |
|
23
|
|
|
'equipList' => (new GetEquipment)->getEquipmentArray(), |
|
24
|
|
|
]); |
|
25
|
|
|
} |
|
26
|
2 |
|
|
|
27
|
|
|
public function search(CustomerSearchRequest $request) |
|
28
|
2 |
|
{ |
|
29
|
|
|
return (new CustomerSearch)->search($request); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function toggleFav($custID) |
|
33
|
|
|
{ |
|
34
|
|
|
$result = (new SetUserFavorites)->toggleCustomerFavorite($custID, Auth::user()->user_id); |
|
35
|
|
|
return response()->json(['success' => true, 'favorite' => $result]); |
|
36
|
4 |
|
} |
|
37
|
|
|
|
|
38
|
4 |
|
public function details($id) |
|
39
|
4 |
|
{ |
|
40
|
|
|
$custObj = new CustomerSearch; |
|
41
|
2 |
|
$details = $custObj->searchID($id); |
|
42
|
|
|
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
|
|
|
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
|
|
|
public function checkID($id) |
|
56
|
|
|
{ |
|
57
|
|
|
$cust = (new CustomerSearch)->searchID($id); |
|
58
|
|
|
if($cust === null) |
|
59
|
|
|
{ |
|
60
|
|
|
return response()->json(['duplicate' => false]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return response()->json(['duplicate' => true, 'name' => $cust->name, 'url' => route('customer.details', [$cust->cust_id, urlencode($cust->name)])]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function store(NewCustomerRequest $request) |
|
67
|
|
|
{ |
|
68
|
|
|
$newID = (new SetCustomerDetails)->createCustomer($request); |
|
69
|
|
|
Log::info('New Customer ID '.$newID.' created. Details - ', $request->toArray()); |
|
70
|
|
|
return response()->json(['success' => true, 'cust_id' => $newID]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function update(NewCustomerRequest $request, $id) |
|
74
|
|
|
{ |
|
75
|
|
|
(new SetCustomerDetails)->updateCustomer($id, $request); |
|
76
|
|
|
Log::info('Customer ID '.$id.' updated by '.Auth::user()->full_name.'. Details - ', $request->toArray()); |
|
77
|
|
|
return response()->json(['success' => true]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function linkParent(LinkParentRequest $request) |
|
81
|
|
|
{ |
|
82
|
|
|
(new SetCustomerDetails)->linkParent($request); |
|
83
|
|
|
return response()->json(['success' => true]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function delete($custID) |
|
87
|
|
|
{ |
|
88
|
|
|
(new SetCustomerDetails)->deactivateCustomer($custID); |
|
89
|
|
|
Log::notice('Customer ID '.$custID.' deactivated by '.Auth::user()->full_name); |
|
90
|
|
|
return response()->json(['success' => true]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|