1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use App\Customers; |
6
|
|
|
use App\CustomerSystems; |
7
|
|
|
use App\SystemDataFields; |
8
|
|
|
use App\SystemCategories; |
9
|
|
|
use App\CustomerSystemData; |
10
|
|
|
use Illuminate\Http\Request; |
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
|
|
|
use App\Http\Resources\SystemCategoriesCollection as CategoriesCollection; |
16
|
|
|
|
17
|
|
|
class CustomerSystemsController extends Controller |
18
|
|
|
{ |
19
|
28 |
|
public function __construct() |
20
|
|
|
{ |
21
|
28 |
|
$this->middleware('auth'); |
22
|
28 |
|
} |
23
|
|
|
|
24
|
|
|
// Get the possible system types that can be assigned to the customer |
25
|
2 |
|
public function index() |
26
|
|
|
{ |
27
|
2 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name); |
28
|
2 |
|
$sysList = new CategoriesCollection(SystemCategories::with('SystemTypes')->with('SystemTypes.SystemDataFields.SystemDataFieldTypes')->get()); |
29
|
|
|
|
30
|
2 |
|
return $sysList; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Store a new system for the customer |
34
|
4 |
|
public function store(Request $request) |
35
|
|
|
{ |
36
|
4 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
37
|
|
|
|
38
|
4 |
|
$request->validate([ |
39
|
4 |
|
'cust_id' => 'required', |
40
|
|
|
'system' => 'required' |
41
|
|
|
// TODO: validate system is unique to customer (write a test for it) |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
// Determine if the system is supposed to be added for the parent, or this site |
45
|
4 |
|
$details = Customers::find($request->cust_id); |
46
|
4 |
|
if($details->parent_id && $request->shared == 1) |
47
|
|
|
{ |
48
|
2 |
|
$request->cust_id = $details->parent_id; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Insert the system into the DB |
52
|
4 |
|
$sys = CustomerSystems::create([ |
53
|
4 |
|
'cust_id' => $request->cust_id, |
54
|
4 |
|
'sys_id' => $request->system, |
55
|
4 |
|
'shared' => $request->shared, |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
// Get the data fields for the new system |
59
|
4 |
|
$fields = SystemDataFields::where('sys_id', $request->system)->get(); |
60
|
|
|
|
61
|
|
|
// Enter each of the data fields into the DB |
62
|
4 |
|
foreach($fields as $field) |
63
|
|
|
{ |
64
|
|
|
$data = 'field_'.$field->field_id; |
65
|
|
|
CustomerSystemData::create([ |
66
|
|
|
'cust_sys_id' => $sys->cust_sys_id, |
67
|
|
|
'field_id' => $field->field_id, |
68
|
|
|
'value' => isset($request->$data) ? $request->$data : null |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
Log::info('New Customer System Added by '.Auth::user()->user_id.' - Customer ID - '.$request->cust_id.' System ID - '.$request->system); |
73
|
4 |
|
return response()->json(['success' => true]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Get the list of systems attached to the customer |
77
|
6 |
|
public function show($id) |
78
|
|
|
{ |
79
|
6 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name); |
80
|
|
|
|
81
|
6 |
|
$sysList = CustomerSystems::where('cust_id', $id) |
82
|
6 |
|
->with('SystemTypes') |
83
|
6 |
|
->with('SystemDataFields') |
84
|
6 |
|
->with('SystemDataFields.SystemDataFieldTypes') |
85
|
6 |
|
->orderBy('cust_sys_id', 'DESC') |
86
|
6 |
|
->get(); |
87
|
|
|
|
88
|
|
|
// determine if there is a parent site with shared systems |
89
|
6 |
|
$parent = Customers::findOrFail($id)->parent_id; |
90
|
4 |
|
if($parent != null) |
91
|
|
|
{ |
92
|
2 |
|
$parentList = CustomerSystems::where('cust_id', $parent) |
93
|
2 |
|
->where('shared', 1) |
94
|
2 |
|
->with('SystemTypes') |
95
|
2 |
|
->with('SystemDataFields') |
96
|
2 |
|
->with('SystemDataFields.SystemDataFieldTypes') |
97
|
2 |
|
->get(); |
98
|
|
|
|
99
|
2 |
|
$sysList = $sysList->merge($parentList); |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
return $sysList; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Update the customers system data |
106
|
4 |
|
public function update(Request $request, $id) |
107
|
|
|
{ |
108
|
4 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
109
|
|
|
|
110
|
4 |
|
$request->validate([ |
111
|
4 |
|
'cust_id' => 'required', |
112
|
|
|
'system' => 'required', |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
// Verify the system type and customer ID match |
116
|
4 |
|
$valid = CustomerSystems::where('cust_id', $request->cust_id)->where('cust_sys_id', $id)->first(); |
117
|
|
|
|
118
|
4 |
|
if(!$valid) |
119
|
|
|
{ |
120
|
2 |
|
return abort(400); |
121
|
|
|
} |
122
|
|
|
|
123
|
2 |
|
$fields = SystemDataFields::where('sys_id', $request->system)->get(); |
124
|
|
|
|
125
|
2 |
|
foreach($fields as $data) |
126
|
|
|
{ |
127
|
|
|
$fieldName = 'field_' . $data->field_id; |
128
|
|
|
if(isset($request->$fieldName)) |
129
|
|
|
{ |
130
|
|
|
Log::debug($request->$fieldName); |
131
|
|
|
} |
132
|
|
|
Log::debug($fieldName); |
133
|
|
|
CustomerSystemData::where('cust_sys_id', $id)->where('field_id', $data->field_id)->update([ |
134
|
|
|
'value' => isset($request->$fieldName) ? $request->$fieldName : null |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
Log::info('Customer System Updated. Cust ID-'.$request->custID.' System ID-'.$request->sysstem.' by '.Auth::user()->full_name); |
139
|
2 |
|
return response()->json(['success' => true]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Delete a system attached to a customer |
143
|
2 |
|
public function destroy($id) |
144
|
|
|
{ |
145
|
2 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name); |
146
|
|
|
|
147
|
|
|
// return response('deleted '.$id); |
148
|
2 |
|
$system = CustomerSystems::find($id); |
149
|
|
|
|
150
|
2 |
|
Log::notice('Customer System Deleted for Customer ID-'.$system->cust_id.' by '.Auth::user()->full_name.'. System ID-'.$id); |
151
|
|
|
|
152
|
2 |
|
$system->delete(); |
153
|
|
|
|
154
|
2 |
|
return response()->json(['success' => true]); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|