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