1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Domains\Customers; |
4
|
|
|
|
5
|
|
|
use App\Customers; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class CustomerSearch |
12
|
|
|
{ |
13
|
|
|
// Initialize the search request |
14
|
14 |
|
public function search($request) |
15
|
|
|
{ |
16
|
14 |
|
$searchData = $this->getSearchFields($request); |
17
|
14 |
|
$pagination = $this->getPagination($request); |
18
|
14 |
|
$sort = $this->getSortFields($request); |
19
|
|
|
|
20
|
14 |
|
if((array)$searchData) |
21
|
|
|
{ |
22
|
12 |
|
$results = $this->searchFor($searchData, $pagination, $sort, true); |
23
|
|
|
} |
24
|
|
|
else |
25
|
|
|
{ |
26
|
2 |
|
$results = $this->getAllCustomers($pagination, $sort, true); |
27
|
|
|
} |
28
|
|
|
|
29
|
14 |
|
return $results; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Search specifically for a customer ID |
33
|
6 |
|
public function searchID($id) |
34
|
|
|
{ |
35
|
6 |
|
return Customers::find($id); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// Separate the name, city, and equipment id fields from request data |
39
|
14 |
|
protected function getSearchFields($request) |
40
|
|
|
{ |
41
|
14 |
|
$searchData = []; |
42
|
14 |
|
if(isset($request->name)) |
43
|
|
|
{ |
44
|
6 |
|
$searchData['name'] = $request->name; // explode(' ', $request->name); |
45
|
|
|
} |
46
|
14 |
|
if(isset($request->city)) |
47
|
|
|
{ |
48
|
4 |
|
$searchData['city'] = $request->city; |
49
|
|
|
} |
50
|
14 |
|
if(isset($request->equipment)) |
51
|
|
|
{ |
52
|
2 |
|
$searchData['equipment'] = $request->equipment; |
53
|
|
|
} |
54
|
|
|
|
55
|
14 |
|
return (object) $searchData; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Separate pagination information from request data |
59
|
14 |
|
protected function getPagination($request) |
60
|
|
|
{ |
61
|
14 |
|
return (object) ['page' => $request->page, 'perPage' => $request->perPage]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Separate sort information from request data |
65
|
14 |
|
protected function getSortFields($request) |
66
|
|
|
{ |
67
|
14 |
|
return (object) ['sortField' => $request->sortField, 'sortType' => $request->sortType]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Perform a detailed search based on the name, city, and equipemnt type paramaters |
71
|
12 |
|
protected function searchFor($searchData, $pagination, $sort, $includeSystems = false) |
72
|
|
|
{ |
73
|
12 |
|
return Customers::orderBy($sort->sortField, $sort->sortType) |
74
|
|
|
// Search equipment field |
75
|
|
|
->when(isset($searchData->equipment), function($q) use ($searchData) |
76
|
|
|
{ |
77
|
|
|
$q->whereHas('ParentSystems.SystemTypes', function ($qry) use ($searchData) |
78
|
|
|
{ |
79
|
2 |
|
$qry->where('sys_id', $searchData->equipment); |
80
|
2 |
|
}) |
81
|
|
|
->orWhereHas('CustomerSystems.SystemTypes', function ($qry) use ($searchData) |
82
|
|
|
{ |
83
|
2 |
|
$qry->where('sys_id', $searchData->equipment); |
84
|
2 |
|
}); |
85
|
12 |
|
}) |
86
|
|
|
// Search city field |
87
|
|
|
->when(isset($searchData->city), function($q) use ($searchData) |
88
|
|
|
{ |
89
|
4 |
|
$q->where('city', 'like', '%'.$searchData->city.'%'); |
90
|
12 |
|
}) |
91
|
|
|
// Search name field |
92
|
|
|
->when(isset($searchData->name), function($q) use ($searchData) |
93
|
|
|
{ |
94
|
6 |
|
$q->where('name', 'like', '%'.$searchData->name.'%') |
95
|
6 |
|
->orWhere('cust_id', 'like', '%'.$searchData->name.'%') |
96
|
6 |
|
->orWhere('dba_name', 'like', '%'.$searchData->name.'%'); |
97
|
12 |
|
}) |
98
|
|
|
->when($includeSystems, function($q) |
99
|
|
|
{ |
100
|
12 |
|
$q->with('CustomerSystems.SystemTypes') |
101
|
12 |
|
->with('ParentSystems.SystemTypes'); |
102
|
12 |
|
}) |
103
|
12 |
|
->paginate($pagination->perPage); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// No search data present, return all customers |
107
|
2 |
|
protected function getAllCustomers($pagination, $sort, $includeSystems = false) |
108
|
|
|
{ |
109
|
2 |
|
return Customers::orderBy($sort->sortField, $sort->sortType) |
110
|
|
|
->when($includeSystems, function($q) |
111
|
|
|
{ |
112
|
2 |
|
$q->with('CustomerSystems.SystemTypes') |
113
|
2 |
|
->with('ParentSystems.SystemTypes'); |
114
|
2 |
|
}) |
115
|
2 |
|
->paginate($pagination->perPage); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|