1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Domains\Customers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Log; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
|
8
|
|
|
use App\Customers; |
9
|
|
|
|
10
|
|
|
use App\Http\Requests\CustomerCreateRequest; |
11
|
|
|
use App\Http\Requests\CustomerDetailsUpdateRequest; |
12
|
|
|
use App\Http\Requests\CustomerParentSetRequest; |
13
|
|
|
|
14
|
|
|
class SetCustomerDetails |
15
|
|
|
{ |
16
|
6 |
|
public function createNewCustomer(CustomerCreateRequest $request) |
17
|
|
|
{ |
18
|
6 |
|
$request->name = $this->cleanCustomerName($request->name); |
|
|
|
|
19
|
6 |
|
if($request->parent_id != null) |
20
|
|
|
{ |
21
|
4 |
|
$request->parent_id = $this->verifyParentID($request->parent_id); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
6 |
|
$custData = Customers::create([ |
25
|
6 |
|
'cust_id' => $request->cust_id, |
26
|
6 |
|
'parent_id' => $request->parent_id, |
27
|
6 |
|
'name' => $request->name, |
28
|
6 |
|
'dba_name' => $request->dba_name, |
29
|
6 |
|
'address' => $request->address, |
30
|
6 |
|
'city' => $request->city, |
31
|
6 |
|
'state' => $request->selectedState, |
32
|
6 |
|
'zip' => $request->zip, |
33
|
|
|
]); |
34
|
|
|
|
35
|
6 |
|
Log::notice('New Customer ID-'.$request->custID.' created by '.Auth::user()->full_name.'. New Customer Data - ', array($custData)); |
36
|
6 |
|
return $custData->cust_id; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Update customer details |
40
|
2 |
|
public function updateCustomerDetails(CustomerDetailsUpdateRequest $request, $custID) |
41
|
|
|
{ |
42
|
2 |
|
Customers::find($custID)->update([ |
43
|
2 |
|
'name' => $request->name, |
44
|
2 |
|
'dba_name' => $request->dba_name, |
45
|
2 |
|
'address' => $request->address, |
46
|
2 |
|
'city' => $request->city, |
47
|
2 |
|
'state' => $request->state, |
48
|
2 |
|
'zip' => $request->zip |
49
|
|
|
]); |
50
|
|
|
|
51
|
2 |
|
Log::info('Customer Details Updated for Customer ID-'.$custID.' by '.Auth::user()->full_name.'. Details - ', array($request)); |
52
|
2 |
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Add a parent ID to the customer site |
56
|
4 |
|
public function setParentCustomerID(CustomerParentSetRequest $request) |
57
|
|
|
{ |
58
|
|
|
// Determine if the parent assigned already has a parent |
59
|
4 |
|
$parentsParent = Customers::find($request->parent_id); |
60
|
4 |
|
if($parentsParent->parent_id) |
61
|
|
|
{ |
62
|
2 |
|
$request->parent_id = $parentsParent->parent_id; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
Customers::find($request->cust_id)->update([ |
66
|
4 |
|
'parent_id' => $request->parent_id, |
67
|
|
|
]); |
68
|
|
|
|
69
|
4 |
|
Log::info('Customer ID '.$request->cust_id.' was linked to parent ID '.$request->parent_id.' by '.Auth::user()->full_name); |
70
|
4 |
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Remove the parent ID from a customer site |
74
|
2 |
|
public function removeParentCustomerID($custID) |
75
|
|
|
{ |
76
|
2 |
|
Customers::find($custID)->update(['parent_id' => null]); |
77
|
|
|
|
78
|
2 |
|
Log::info('Parent Customer ID was removed for Customer ID '.$custID.' by '.Auth::user()->full_name); |
79
|
2 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Remove special characters from the customers name |
83
|
6 |
|
protected function cleanCustomerName($name) |
84
|
|
|
{ |
85
|
6 |
|
return str_replace('/', '-', $name); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Check to see if a customer ID has a Parent ID |
89
|
4 |
|
protected function verifyParentID($id) |
90
|
|
|
{ |
91
|
4 |
|
$parent = Customers::find($id); |
92
|
|
|
|
93
|
4 |
|
return $parent->parent_id != null ? $parent->parent_id : $id; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|