1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use App\Http\Requests\Customers\CustomerContactRequest; |
7
|
|
|
use App\Models\CustomerContact; |
8
|
|
|
use App\Models\CustomerContactPhone; |
9
|
|
|
use App\Models\PhoneNumberType; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
|
12
|
|
|
class CustomerContactsController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Create a new contact |
16
|
|
|
*/ |
17
|
|
|
public function store(CustomerContactRequest $request) |
18
|
|
|
{ |
19
|
|
|
// Create the contact |
20
|
|
|
$newContact = CustomerContact::create($request->only(['cust_id', 'name', 'email', 'shared', 'title', 'note'])); |
21
|
|
|
|
22
|
|
|
// Input the contacts phone numbers |
23
|
|
|
foreach($request->phones as $phone) |
24
|
|
|
{ |
25
|
|
|
if(isset($phone['number'])) |
26
|
|
|
{ |
27
|
|
|
CustomerContactPhone::create([ |
28
|
|
|
'cont_id' => $newContact->cont_id, |
29
|
|
|
'phone_type_id' => PhoneNumberType::where('description', $phone['type'])->first()->phone_type_id, |
30
|
|
|
'phone_number' => $this->cleanPhoneNumber($phone['number']), |
31
|
|
|
'extension' => $phone['extension'], |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return back()->with(['message' => 'Contact Created', 'type' => 'success']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Ajax call to get the contacts for a customer |
41
|
|
|
*/ |
42
|
|
|
public function show($id) |
43
|
|
|
{ |
44
|
|
|
return CustomerContact::where('cust_id', $id)->with('CustomerContactPhone.PhoneNumberType')->get(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Update an existing contact |
49
|
|
|
*/ |
50
|
|
|
public function update(CustomerContactRequest $request, $id) |
51
|
|
|
{ |
52
|
|
|
CustomerContact::find($id)->update($request->only(['cust_id', 'name', 'email', 'shared', 'title', 'note'])); |
53
|
|
|
|
54
|
|
|
$updatedNumbers = []; |
55
|
|
|
foreach($request->phones as $phone) |
56
|
|
|
{ |
57
|
|
|
// If the number is an existing number, update it |
58
|
|
|
if(isset($phone['id'])) |
59
|
|
|
{ |
60
|
|
|
CustomerContactPhone::find($phone['id'])->update([ |
61
|
|
|
'phone_type_id' => PhoneNumberType::where('description', $phone['phone_number_type']['description'])->first()->phone_type_id, |
62
|
|
|
'phone_number' => $this->cleanPhoneNumber($phone['phone_number']), |
63
|
|
|
'extension' => $phone['extension'], |
64
|
|
|
]); |
65
|
|
|
$updatedNumbers[] = $phone['id']; |
66
|
|
|
} |
67
|
|
|
// Otherwise enter a new number |
68
|
|
|
else |
69
|
|
|
{ |
70
|
|
|
$new = CustomerContactPhone::create([ |
71
|
|
|
'cont_id' => $id, |
72
|
|
|
'phone_type_id' =>PhoneNumberType::where('description', $phone['phone_number_type']['description'])->first()->phone_type_id, |
73
|
|
|
'phone_number' => $this->cleanPhoneNumber($phone['phone_number']), |
74
|
|
|
'extension' => $phone['extension'], |
75
|
|
|
]); |
76
|
|
|
$updatedNumbers[] = $new->id; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$oldContacts = CustomerContactPhone::where('cont_id', $id)->whereNotIn('id', $updatedNumbers)->get(); |
81
|
|
|
foreach($oldContacts as $cont) |
82
|
|
|
{ |
83
|
|
|
$cont->delete(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return back()->with(['message' => 'Contact Updated', 'type' => 'success']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Delete a contact |
91
|
|
|
*/ |
92
|
|
|
public function destroy($id) |
93
|
|
|
{ |
94
|
|
|
CustomerContact::findOrFail($id)->delete(); |
95
|
|
|
return response()->noContent(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/* |
99
|
|
|
* Clean the phone number to be digits only |
100
|
|
|
*/ |
101
|
|
|
protected function cleanPhoneNumber($number) |
102
|
|
|
{ |
103
|
|
|
return preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '$1$2$3', $number); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|