1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Domains\Customers; |
4
|
|
|
|
5
|
|
|
use App\CustomerContacts; |
6
|
|
|
use Illuminate\Support\Facades\Log; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
|
9
|
|
|
use App\Customers; |
10
|
|
|
use App\Http\Resources\PhoneNumberTypesCollection; |
11
|
|
|
use App\PhoneNumberTypes; |
12
|
|
|
|
13
|
|
|
class GetCustomerContacts |
14
|
|
|
{ |
15
|
|
|
protected $custID; |
16
|
|
|
|
17
|
10 |
|
public function __construct($custID = null) |
18
|
|
|
{ |
19
|
10 |
|
$this->custID = $custID; |
20
|
10 |
|
} |
21
|
|
|
|
22
|
6 |
|
public function execute() |
23
|
|
|
{ |
24
|
6 |
|
$hasParent = Customers::findOrFail($this->custID)->parent_id; |
25
|
6 |
|
$localContacts = $this->getLocalContacts(); |
26
|
|
|
|
27
|
6 |
|
if($hasParent) |
28
|
|
|
{ |
29
|
2 |
|
$parentContacts = $this->getParentContacts($hasParent); |
30
|
2 |
|
return $localContacts->merge($parentContacts); |
31
|
|
|
} |
32
|
4 |
|
return $localContacts; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Pull a specific contact |
36
|
2 |
|
public function getOneContact($contID) |
37
|
|
|
{ |
38
|
2 |
|
$contact = CustomerContacts::where('cont_id', $contID)->with('CustomerContactPhones')->first(); |
39
|
2 |
|
$contactName = explode(' ', $contact->name); |
40
|
2 |
|
$contactDetails = collect((object) [ |
41
|
2 |
|
'firstName' => $contactName[0], |
42
|
2 |
|
'lastName' => isset($contactName[1]) ? $contactName[1] : '', |
43
|
2 |
|
'email' => $contact->eamil, // FIXME: is this broken??? |
44
|
2 |
|
'additional' => '', |
45
|
2 |
|
'prefix' => '', |
46
|
2 |
|
'suffix' => '', |
47
|
2 |
|
'cust_id' => $contact->cust_id, |
48
|
2 |
|
'numbers' => $contact->CustomerContactPhones, |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
// return $contact; |
52
|
2 |
|
return $contactDetails; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Get the type of phone numbers that can be assigned to a customer |
56
|
2 |
|
public function getPhoneNumberTypes($collection = false) |
57
|
|
|
{ |
58
|
2 |
|
$numberTypes = PhoneNumberTypes::all(); |
59
|
2 |
|
Log::debug('Phone number type list gathered. Gathered data - ', array($numberTypes)); |
60
|
2 |
|
if($collection) |
61
|
|
|
{ |
62
|
|
|
return new PhoneNumberTypesCollection($numberTypes); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return $numberTypes; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Retrieve any contacts attached to the customer |
69
|
6 |
|
protected function getLocalContacts() |
70
|
|
|
{ |
71
|
6 |
|
$contacts = CustomerContacts::where('cust_id', $this->custID) |
72
|
6 |
|
->with('CustomerContactPhones') |
73
|
6 |
|
->get(); |
74
|
|
|
|
75
|
6 |
|
Log::debug('Customer Contacts Query completed for customer ID '.$this->custID.'. Results - ', array($contacts)); |
76
|
6 |
|
return $contacts; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Retrieve any equipment attached to the parent customer |
80
|
2 |
|
protected function getParentContacts($parentID) |
81
|
|
|
{ |
82
|
2 |
|
$parentList = CustomerContacts::where('cust_id', $parentID) |
83
|
2 |
|
->where('shared', 1) |
84
|
2 |
|
->with('CustomerContactPhones') |
85
|
2 |
|
->get(); |
86
|
|
|
|
87
|
2 |
|
Log::debug('Customer Parent Contacts Query completed for Customer ID '.$this->custID.'. Results - ', array($parentList)); |
88
|
2 |
|
return $parentList; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|