1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
|
7
|
|
|
use App\Models\Customer; |
8
|
|
|
use App\Models\PhoneNumberType; |
9
|
|
|
use App\Models\CustomerContact; |
10
|
|
|
use App\Models\CustomerContactPhone; |
11
|
|
|
|
12
|
|
|
use App\Http\Requests\Customers\CustomerContactsRequest; |
13
|
|
|
use App\Events\Customers\Contacts\CustomerContactAddedEvent; |
14
|
|
|
use App\Events\Customers\Contacts\CustomerContactDeletedEvent; |
15
|
|
|
use App\Events\Customers\Contacts\CustomerContactForceDeletedEvent; |
16
|
|
|
use App\Events\Customers\Contacts\CustomerContactRestoredEvent; |
17
|
|
|
use App\Events\Customers\Contacts\CustomerContactUpdatedEvent; |
18
|
|
|
|
19
|
2 |
|
class CustomerContactsController extends Controller |
20
|
|
|
{ |
21
|
2 |
|
/** |
22
|
2 |
|
* Store a newly created customer contact |
23
|
|
|
*/ |
24
|
|
|
public function store(CustomerContactsRequest $request) |
25
|
2 |
|
{ |
26
|
|
|
$cust = Customer::findOrFail($request->cust_id); |
27
|
1 |
|
$cust_id = $cust->cust_id; |
28
|
|
|
|
29
|
|
|
// If the equipment is shared, it must be assigned to the parent site |
30
|
|
|
if($request->shared && $cust->parent_id > 0) |
31
|
2 |
|
{ |
32
|
2 |
|
$cust_id = $cust->parent_id; |
33
|
2 |
|
} |
34
|
2 |
|
|
35
|
2 |
|
// Create the contact |
36
|
2 |
|
$newContact = CustomerContact::create([ |
37
|
2 |
|
'cust_id' => $cust_id, |
38
|
|
|
'name' => $request->name, |
39
|
|
|
'email' => $request->email, |
40
|
|
|
'shared' => $request->shared, |
41
|
2 |
|
'title' => $request->title, |
42
|
|
|
'note' => $request->note, |
43
|
2 |
|
]); |
44
|
|
|
|
45
|
2 |
|
// Input the contacts phone numbers |
46
|
2 |
|
foreach($request->phones as $phone) |
47
|
2 |
|
{ |
48
|
2 |
|
if(isset($phone['number'])) |
49
|
2 |
|
{ |
50
|
|
|
CustomerContactPhone::create([ |
51
|
|
|
'cont_id' => $newContact->cont_id, |
52
|
|
|
'phone_type_id' => PhoneNumberType::where('description', $phone['type'])->first()->phone_type_id, |
53
|
|
|
'phone_number' => $this->cleanPhoneNumber($phone['number']), |
54
|
2 |
|
'extension' => $phone['extension'], |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
event(new CustomerContactAddedEvent($cust, $newContact)); |
60
|
2 |
|
return back()->with(['message' => 'New Contact Created', 'type' => 'success']); |
61
|
|
|
} |
62
|
2 |
|
|
63
|
|
|
/** |
64
|
2 |
|
* Update an existing contact |
65
|
2 |
|
*/ |
66
|
2 |
|
public function update(CustomerContactsRequest $request, $id) |
67
|
|
|
{ |
68
|
1 |
|
$cust = Customer::findOrFail($request->cust_id); |
69
|
2 |
|
$cust_id = $cust->cust_id; |
70
|
2 |
|
|
71
|
|
|
// If the equipment is shared, it must be assigned to the parent site |
72
|
|
|
if($request->shared && $cust->parent_id > 0) |
73
|
|
|
{ |
74
|
|
|
$cust_id = $cust->parent_id; |
75
|
|
|
} |
76
|
2 |
|
|
77
|
|
|
$contact = CustomerContact::find($id); |
78
|
2 |
|
$contact->update([ |
79
|
2 |
|
'cust_id' => $cust_id, |
80
|
|
|
'name' => $request->name, |
81
|
|
|
'email' => $request->email, |
82
|
2 |
|
'shared' => $request->shared, |
83
|
|
|
'title' => $request->title, |
84
|
1 |
|
'note' => $request->note, |
85
|
|
|
]); |
86
|
|
|
|
87
|
2 |
|
$updatedNumbers = []; |
88
|
2 |
|
foreach($request->phones as $phone) |
89
|
2 |
|
{ |
90
|
2 |
|
// If the number is an existing number, update it |
91
|
2 |
|
if(isset($phone['id'])) |
92
|
2 |
|
{ |
93
|
2 |
|
CustomerContactPhone::find($phone['id'])->update([ |
94
|
|
|
'phone_type_id' => PhoneNumberType::where('description', $phone['phone_number_type']['description'])->first()->phone_type_id, |
95
|
|
|
'phone_number' => $this->cleanPhoneNumber($phone['phone_number']), |
96
|
2 |
|
'extension' => $phone['extension'], |
97
|
2 |
|
]); |
98
|
|
|
$updatedNumbers[] = $phone['id']; |
99
|
|
|
} |
100
|
2 |
|
// Otherwise enter a new number |
101
|
|
|
else |
102
|
2 |
|
{ |
103
|
2 |
|
$new = CustomerContactPhone::create([ |
104
|
2 |
|
'cont_id' => $id, |
105
|
2 |
|
'phone_type_id' =>PhoneNumberType::where('description', $phone['phone_number_type']['description'])->first()->phone_type_id, |
106
|
|
|
'phone_number' => $this->cleanPhoneNumber($phone['phone_number']), |
107
|
2 |
|
'extension' => $phone['extension'], |
108
|
|
|
]); |
109
|
|
|
$updatedNumbers[] = $new->id; |
110
|
|
|
} |
111
|
|
|
} |
112
|
2 |
|
|
113
|
2 |
|
$oldContacts = CustomerContactPhone::where('cont_id', $id)->whereNotIn('id', $updatedNumbers)->get(); |
114
|
2 |
|
foreach($oldContacts as $cont) |
115
|
2 |
|
{ |
116
|
2 |
|
$cont->delete(); |
117
|
|
|
} |
118
|
2 |
|
|
119
|
|
|
event(new CustomerContactUpdatedEvent($cust, $contact)); |
120
|
|
|
return back()->with(['message' => 'Contact Updated', 'type' => 'success']); |
121
|
|
|
} |
122
|
2 |
|
|
123
|
2 |
|
/** |
124
|
|
|
* Soft Delete a Customer Contact |
125
|
2 |
|
*/ |
126
|
|
|
public function destroy($id) |
127
|
|
|
{ |
128
|
2 |
|
$this->authorize('delete', CustomerContact::class); |
129
|
|
|
$cont = CustomerContact::find($id); |
130
|
|
|
$cont->delete(); |
131
|
|
|
|
132
|
|
|
event(new CustomerContactDeletedEvent(Customer::find($cont->cust_id), $cont)); |
|
|
|
|
133
|
|
|
return back()->with(['message' => 'Contact deleted', 'type' => 'danger']); |
134
|
2 |
|
} |
135
|
|
|
|
136
|
2 |
|
|
137
|
|
|
/** |
138
|
1 |
|
* Restore a contact that was soft deleted |
139
|
1 |
|
*/ |
140
|
|
|
public function restore($id) |
141
|
|
|
{ |
142
|
|
|
$this->authorize('restore', CustomerContact::class); |
143
|
2 |
|
$cont = CustomerContact::withTrashed()->where('cont_id', $id)->first(); |
144
|
|
|
$cont->restore(); |
145
|
2 |
|
|
146
|
1 |
|
event(new CustomerContactRestoredEvent(Customer::find($cont->cust_id), $cont)); |
|
|
|
|
147
|
1 |
|
return back()->with(['message' => 'Contact '.$cont->name.' restored', 'type' => 'success']); |
148
|
|
|
} |
149
|
1 |
|
|
150
|
1 |
|
/** |
151
|
|
|
* Permanently delete a contact |
152
|
|
|
*/ |
153
|
2 |
|
public function forceDelete($id) |
154
|
|
|
{ |
155
|
2 |
|
$cont = CustomerContact::withTrashed()->where('cont_id', $id)->first(); |
156
|
|
|
$this->authorize('forceDelete', $cont); |
157
|
1 |
|
$cont->forceDelete(); |
158
|
|
|
|
159
|
1 |
|
event(new CustomerContactForceDeletedEvent(Customer::find($cont->cust_id), $cont)); |
|
|
|
|
160
|
1 |
|
return back()->with(['message' => 'Contact permanently deleted', 'type' => 'danger']); |
161
|
|
|
} |
162
|
1 |
|
|
163
|
|
|
|
164
|
|
|
|
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/* |
168
|
4 |
|
* Clean the phone number to be digits only |
169
|
|
|
*/ |
170
|
4 |
|
protected function cleanPhoneNumber($number) |
171
|
|
|
{ |
172
|
|
|
return preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '$1$2$3', $number); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|