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
|
|
|
use App\CustomerContacts; |
10
|
|
|
use App\CustomerContactPhones; |
11
|
|
|
|
12
|
|
|
use App\Http\Requests\CustomerEditContactRequest; |
13
|
|
|
use App\Http\Requests\CustomerNewContactRequest; |
14
|
|
|
|
15
|
|
|
class SetCustomerContacts |
16
|
|
|
{ |
17
|
|
|
protected $custID; |
18
|
|
|
|
19
|
12 |
|
public function __construct($custID) |
20
|
|
|
{ |
21
|
12 |
|
$this->custID = $custID; |
22
|
12 |
|
} |
23
|
|
|
|
24
|
4 |
|
public function createContact(CustomerNewContactRequest $request) |
25
|
|
|
{ |
26
|
|
|
// |
27
|
4 |
|
if($request->shared) |
28
|
|
|
{ |
29
|
|
|
$this->checkBelongToParent($request->cust_id); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Update the primary information |
33
|
4 |
|
$contData = CustomerContacts::create([ |
34
|
4 |
|
'cust_id' => $this->custID, |
35
|
4 |
|
'shared' => $request->shared, |
36
|
4 |
|
'name' => $request->name, |
37
|
4 |
|
'email' => $request->email, |
38
|
|
|
]); |
39
|
|
|
|
40
|
4 |
|
$this->processPhoneNumbers($contData->cont_id, $request->customer_contact_phones); |
41
|
|
|
|
42
|
4 |
|
Log::info('Contact created for customer ID '.$this->custID.' by '.Auth::user()->full_name.'. New Data - ', array($contData)); |
43
|
4 |
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
6 |
|
public function updateContact(CustomerEditContactRequest $request) |
47
|
|
|
{ |
48
|
|
|
// verify that the contact id is valid |
49
|
6 |
|
$contData = CustomerContacts::where('cont_id', $request->cont_id)->where('cust_id', $this->custID)->first(); |
50
|
6 |
|
if(!$contData) |
51
|
|
|
{ |
52
|
6 |
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if($request->shared) |
56
|
|
|
{ |
57
|
|
|
$this->checkBelongToParent($request->cust_id); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Update the primary information |
61
|
|
|
$contData->update([ |
62
|
|
|
'cust_id' => $this->custID, |
63
|
|
|
'shared' => $request->shared, |
64
|
|
|
'name' => $request->name, |
65
|
|
|
'email' => $request->email, |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$this->processPhoneNumbers($request->cont_id, $request->customer_contact_phones); |
69
|
|
|
|
70
|
|
|
Log::info('Contact information updated for customer ID '.$this->custID.' by '.Auth::user()->full_name.'. New Data - ', array($contData)); |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Remove a specific contact |
75
|
2 |
|
public function deleteContact($contID) |
76
|
|
|
{ |
77
|
2 |
|
CustomerContacts::find($contID)->delete(); |
78
|
2 |
|
Log::notice('Customer Contact ID '.$contID.' deleted by '.Auth::user()->full_name); |
79
|
2 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Determine if the contact should be attached to the parent site |
83
|
|
|
protected function checkBelongToParent($custID) |
84
|
|
|
{ |
85
|
|
|
$custData = Customers::find($custID); |
86
|
|
|
if($custData->parent_id) |
87
|
|
|
{ |
88
|
|
|
$this->custID = $custData->parentID; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Cycle through phone numbers and enter or edit them in the database |
93
|
4 |
|
protected function processPhoneNumbers($contID, $phoneData) |
94
|
|
|
{ |
95
|
4 |
|
$curData = CustomerContactPhones::where('cont_id', $contID)->get(); |
96
|
|
|
|
97
|
4 |
|
if($phoneData) |
98
|
|
|
{ |
99
|
4 |
|
foreach($phoneData as $phone) |
100
|
|
|
{ |
101
|
4 |
|
if(isset($phone['id']) && !empty($phone['readable'])) |
102
|
|
|
{ |
103
|
|
|
CustomerContactPhones::find($phone['id'])->update([ |
104
|
|
|
'phone_type_id' => $phone['phone_type_id'], |
105
|
|
|
'phone_number' => $this->cleanPhoneNumber($phone['readable']), |
106
|
|
|
'extension' => $phone['extension'], |
107
|
|
|
]); |
108
|
|
|
$curData = $curData->filter(function($item) use ($phone) { |
109
|
|
|
return $item->id != $phone['id']; |
110
|
|
|
}); |
111
|
|
|
} |
112
|
4 |
|
else if(!empty($phone['readable'])) |
113
|
|
|
{ |
114
|
|
|
CustomerContactPhones::create([ |
115
|
|
|
'cont_id' => $contID, |
116
|
|
|
'phone_type_id' => $phone['phone_type_id'], |
117
|
|
|
'phone_number' => $this->cleanPhoneNumber($phone['readable']), |
118
|
|
|
'extension' => $phone['extension'], |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Remove any numbers that were deleted |
125
|
4 |
|
foreach($curData as $phone) |
126
|
|
|
{ |
127
|
|
|
CustomerContactPhones::find($phone->id)->delete(); |
128
|
|
|
} |
129
|
4 |
|
} |
130
|
|
|
|
131
|
|
|
// Re-format the number to be strictly 10 digits |
132
|
|
|
protected function cleanPhoneNumber($number) |
133
|
|
|
{ |
134
|
|
|
return preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '$1$2$3', $number); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|