|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Domains\Customers; |
|
4
|
|
|
|
|
5
|
|
|
use App\CustomerContactPhones; |
|
6
|
|
|
use App\CustomerContacts; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
use Illuminate\Support\Facades\Log; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class SetCustomerContacts extends GetCustomerDetails |
|
12
|
|
|
{ |
|
13
|
6 |
|
public function createNewContact($request, $custID) |
|
14
|
|
|
{ |
|
15
|
6 |
|
if($request->shared) |
|
16
|
|
|
{ |
|
17
|
2 |
|
$parent = $this->getParentID($custID); |
|
18
|
2 |
|
if($parent) |
|
19
|
|
|
{ |
|
20
|
2 |
|
$custID = $parent; |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
6 |
|
$contID = $this->createContact($custID, $request->name, $request->email, $request->shared); |
|
25
|
6 |
|
$this->processNewPhoneNumbers($contID, $request->customer_contact_phones); |
|
26
|
|
|
|
|
27
|
6 |
|
return true; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
6 |
|
public function updateExistingContact($request, $contID) |
|
31
|
|
|
{ |
|
32
|
6 |
|
$custID = $request->cust_id; |
|
33
|
6 |
|
if($request->shared) |
|
34
|
|
|
{ |
|
35
|
2 |
|
$parent = $this->getParentID($custID); |
|
36
|
2 |
|
if($parent) |
|
37
|
|
|
{ |
|
38
|
2 |
|
$custID = $parent; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
6 |
|
$this->updateContact($custID, $contID, $request->name, $request->email, $request->shared); |
|
43
|
6 |
|
$this->processExistingNumbers($contID, $request->customer_contact_phones); |
|
44
|
|
|
|
|
45
|
6 |
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
4 |
|
public function deleteContact($contID) |
|
49
|
|
|
{ |
|
50
|
4 |
|
CustomerContacts::find($contID)->delete(); |
|
51
|
4 |
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
6 |
|
protected function createContact($custID, $name, $email, $shared) |
|
55
|
|
|
{ |
|
56
|
6 |
|
$newCont = CustomerContacts::create([ |
|
57
|
6 |
|
'cust_id' => $custID, |
|
58
|
6 |
|
'name' => $name, |
|
59
|
6 |
|
'email' => $email, |
|
60
|
6 |
|
'shared' => $shared, |
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
6 |
|
return $newCont->cont_id; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
6 |
|
public function updateContact($custID, $contID, $name, $email, $shared) |
|
67
|
|
|
{ |
|
68
|
6 |
|
CustomerContacts::find($contID)->update([ |
|
69
|
6 |
|
'cust_id' => $custID, |
|
70
|
6 |
|
'name' => $name, |
|
71
|
6 |
|
'email' => $email, |
|
72
|
6 |
|
'shared' => $shared, |
|
73
|
|
|
]); |
|
74
|
|
|
|
|
75
|
6 |
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
12 |
|
protected function processNewPhoneNumbers($contID, $phoneData) |
|
79
|
|
|
{ |
|
80
|
12 |
|
foreach($phoneData as $number) |
|
81
|
|
|
{ |
|
82
|
12 |
|
$number = (object) $number; |
|
83
|
12 |
|
if($number->readable != null) |
|
84
|
|
|
{ |
|
85
|
12 |
|
CustomerContactPhones::create([ |
|
86
|
12 |
|
'cont_id' => $contID, |
|
87
|
12 |
|
'phone_type_id' => $number->phone_type_id, |
|
88
|
12 |
|
'phone_number' => $this->cleanPhoneNumber($number->readable), |
|
89
|
12 |
|
'extension' => $number->extension, |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
12 |
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
protected function processExistingNumbers($contID, $phoneData) |
|
96
|
|
|
{ |
|
97
|
6 |
|
$curentData = CustomerContactPhones::where('cont_id', $contID)->get()->keyBy('id'); |
|
98
|
6 |
|
$newData = []; |
|
99
|
|
|
|
|
100
|
6 |
|
foreach($phoneData as $number) |
|
101
|
|
|
{ |
|
102
|
6 |
|
$number = (object) $number; |
|
103
|
6 |
|
if(isset($number->id) && $number->readable != null) |
|
104
|
|
|
{ |
|
105
|
4 |
|
$this->updateNumber($number->id, $number->phone_type_id, $this->cleanPhoneNumber($number->readable), $number->extension); |
|
106
|
4 |
|
$curentData->forget($number->id); |
|
107
|
|
|
} |
|
108
|
|
|
else |
|
109
|
|
|
{ |
|
110
|
6 |
|
$newData[] = $number; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
6 |
|
$this->processNewPhoneNumbers($contID, $newData); |
|
115
|
6 |
|
$this->deletePhoneNumbers($curentData); |
|
116
|
|
|
|
|
117
|
6 |
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
4 |
|
protected function updateNumber($numID, $typeID, $number, $ext) |
|
121
|
|
|
{ |
|
122
|
4 |
|
CustomerContactPhones::find($numID)->update([ |
|
123
|
4 |
|
'phone_type_id' => $typeID, |
|
124
|
4 |
|
'phone_number' => $number, |
|
125
|
4 |
|
'extension' => $ext, |
|
126
|
|
|
]); |
|
127
|
|
|
|
|
128
|
4 |
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
6 |
|
protected function deletePhoneNumbers($delArray) |
|
132
|
|
|
{ |
|
133
|
6 |
|
foreach($delArray as $del) |
|
134
|
|
|
{ |
|
135
|
4 |
|
CustomerContactPhones::find($del->id)->delete(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
6 |
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
12 |
|
protected function cleanPhoneNumber($number) |
|
142
|
|
|
{ |
|
143
|
12 |
|
return preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '$1$2$3', $number); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|