1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use App\Customers; |
6
|
|
|
use App\PhoneNumberTypes; |
7
|
|
|
use App\CustomerContacts; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use App\CustomerContactPhones; |
10
|
|
|
use JeroenDesloovere\VCard\VCard; |
11
|
|
|
use Illuminate\Support\Facades\Log; |
12
|
|
|
use Illuminate\Support\Facades\Auth; |
13
|
|
|
use App\Http\Controllers\Controller; |
14
|
|
|
use Illuminate\Support\Facades\Route; |
15
|
|
|
|
16
|
|
|
class CustomerContactsController extends Controller |
17
|
|
|
{ |
18
|
18 |
|
public function __construct() |
19
|
|
|
{ |
20
|
18 |
|
$this->middleware('auth'); |
21
|
18 |
|
} |
22
|
|
|
|
23
|
|
|
// Store a new customer contact |
24
|
2 |
|
public function store(Request $request) |
25
|
|
|
{ |
26
|
2 |
|
$request->validate([ |
27
|
2 |
|
'cust_id' => 'required', |
28
|
|
|
'name' => 'required' |
29
|
|
|
]); |
30
|
|
|
|
31
|
|
|
// Determine if the contact should be assigned under this site or the parent site |
32
|
2 |
|
$details = Customers::find($request->cust_id); |
33
|
2 |
|
if($details->parent_id && $request->shared == 'true') |
34
|
|
|
{ |
35
|
|
|
$request->cust_id = $details->parent_id; |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
$cont = CustomerContacts::create([ |
39
|
2 |
|
'cust_id' => $request->cust_id, |
40
|
2 |
|
'name' => $request->name, |
41
|
2 |
|
'email' => !empty($request->email) ? $request->email : null, |
42
|
2 |
|
'shared' => $request->shared == 'true' ? 1 : 0, |
43
|
2 |
|
])->cont_id; |
44
|
|
|
|
45
|
2 |
|
foreach($request->numbers['type'] as $key => $num) |
46
|
|
|
{ |
47
|
2 |
|
if(!empty($request->numbers['number'][$key])) |
48
|
|
|
{ |
49
|
2 |
|
CustomerContactPhones::create([ |
50
|
2 |
|
'cont_id' => $cont, |
51
|
2 |
|
'phone_type_id' => $request->numbers['type'][$key], |
52
|
2 |
|
'phone_number' => PhoneNumberTypes::cleanPhoneNumber($request->numbers['number'][$key]), |
53
|
2 |
|
'extension' => isset($request->numbers['ext'][$key]) ? $request->numbers['ext'][$key] : null |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
59
|
2 |
|
Log::debug('Submitted Data - ', $request->toArray()); |
60
|
2 |
|
Log::info('New Customer Contact Created for Cust ID - '.$request->cust_id.'. Contact ID-'.$cont); |
61
|
2 |
|
return response()->json(['success' => true]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Get the contacts for a customer |
65
|
2 |
|
public function show($id) |
66
|
|
|
{ |
67
|
2 |
|
$contacts = CustomerContacts::where('cust_id', $id) |
68
|
2 |
|
->with('CustomerContactPhones') |
69
|
2 |
|
->get(); |
70
|
|
|
|
71
|
|
|
// Determine if there is a parent site with shared contacts |
72
|
2 |
|
$parent = Customers::find($id)->parent_id; |
73
|
2 |
|
if ($parent) |
74
|
|
|
{ |
75
|
|
|
$parentList = CustomerContacts::where('cust_id', $parent) |
76
|
|
|
->where('shared', 1) |
77
|
|
|
->with('CustomerContactPhones') |
78
|
|
|
->get(); |
79
|
|
|
|
80
|
|
|
$contacts = $contacts->merge($parentList); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
84
|
2 |
|
Log::debug('Fetched Data - ', $contacts->toArray()); |
85
|
2 |
|
return $contacts; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Edit function will actually download the contact information in V-Card format |
89
|
|
|
public function edit($id) |
90
|
|
|
{ |
91
|
|
|
$contact = CustomerContacts::find($id); |
92
|
|
|
$numbers = CustomerContactPhones::where('cont_id', $id)->get(); |
93
|
|
|
$custData = Customers::find($contact->cust_id); |
94
|
|
|
|
95
|
|
|
$contactName = explode(' ', $contact->name); |
96
|
|
|
$firstName = $contactName[0]; |
97
|
|
|
$lastName = isset($contactName[1]) ? $contactName[1] : ''; |
98
|
|
|
$additional = ''; |
99
|
|
|
$prefix = ''; |
100
|
|
|
$suffix = ''; |
101
|
|
|
|
102
|
|
|
$vcard = new VCard(); |
103
|
|
|
$vcard->addName($lastName, $firstName, $additional, $prefix, $suffix); |
104
|
|
|
$vcard->addCompany($custData->name); |
105
|
|
|
$vcard->addEmail($contact->email); |
106
|
|
|
$vcard->addAddress(null, null, $custData->address, $custData->city, $custData->state, $custData->zip, null); |
107
|
|
|
|
108
|
|
|
if(!empty($numbers)) |
109
|
|
|
{ |
110
|
|
|
foreach($numbers as $phone) |
111
|
|
|
{ |
112
|
|
|
$vcard->addPhoneNumber($phone->phone_number, $phone->description); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
117
|
|
|
Log::info('Customer Contact Downloaded - Contact ID-'.$id); |
118
|
|
|
return $vcard->download(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Update an existing Customer Contact |
122
|
2 |
|
public function update(Request $request, $id) |
123
|
|
|
{ |
124
|
2 |
|
$request->validate([ |
125
|
2 |
|
'cust_id' => 'required', |
126
|
|
|
'name' => 'required' |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
// Update the primary contact information |
130
|
2 |
|
$details = Customers::find($request->cust_id); |
131
|
2 |
|
if ($details->parent_id && $request->shared == 'true') { |
132
|
|
|
$request->cust_id = $details->parent_id; |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
CustomerContacts::find($id)->update([ |
136
|
2 |
|
'cust_id' => $request->cust_id, |
137
|
2 |
|
'name' => $request->name, |
138
|
2 |
|
'email' => isset($request->email) ? $request->email : null, |
139
|
2 |
|
'shared' => $request->shared == 'true' ? 1 : 0, |
140
|
|
|
]); |
141
|
|
|
|
142
|
2 |
|
$contID = $id; |
143
|
|
|
|
144
|
|
|
// Clear all contact phone numbers and re-enter them |
145
|
2 |
|
CustomerContactPhones::where('cont_id', $id)->delete(); |
146
|
2 |
|
foreach($request->numbers['type'] as $key => $num) |
147
|
|
|
{ |
148
|
2 |
|
if(!empty($request->numbers['number'][$key])) |
149
|
|
|
{ |
150
|
2 |
|
CustomerContactPhones::create([ |
151
|
2 |
|
'cont_id' => $contID, |
152
|
2 |
|
'phone_type_id' => $request->numbers['type'][$key], |
153
|
2 |
|
'phone_number' => PhoneNumberTypes::cleanPhoneNumber($request->numbers['number'][$key]), |
154
|
2 |
|
'extension' => isset($request->numbers['ext'][$key]) ? $request->numbers['ext'][$key] : null |
155
|
|
|
]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
160
|
2 |
|
Log::debug('Submitted Data - ', $request->toArray()); |
161
|
2 |
|
Log::info('Customer Contact Updated for Cust ID - '.$request->cust_id.'. Contact ID-'.$contID); |
162
|
2 |
|
return response()->json(['success' => true]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// Delete an existing contact |
166
|
2 |
|
public function destroy($id) |
167
|
|
|
{ |
168
|
2 |
|
$cont = CustomerContacts::find($id); |
169
|
|
|
|
170
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
171
|
2 |
|
Log::info('Customer Contact deleted for Customer ID-'.$cont->cust_id.' by User ID-'.Auth::user()->user_id.'. Deleted Contact ID-'.$id); |
172
|
|
|
|
173
|
2 |
|
$cont->delete(); |
174
|
|
|
|
175
|
2 |
|
return response()->json(['success' => true]); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|