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