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