|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Customers; |
|
6
|
|
|
use App\SystemTypes; |
|
7
|
|
|
use App\CustomerSystems; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use App\CustomerSystemFields; |
|
10
|
|
|
use App\SystemCustDataFields; |
|
11
|
|
|
use App\Http\Traits\SystemsTrait; |
|
12
|
|
|
use Illuminate\Support\Facades\Log; |
|
13
|
|
|
use Illuminate\Support\Facades\Auth; |
|
14
|
|
|
use App\Http\Controllers\Controller; |
|
15
|
|
|
|
|
16
|
|
|
class CustomerSystemsController extends Controller |
|
17
|
|
|
{ |
|
18
|
|
|
use SystemsTrait; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->middleware('auth'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
// Store a new system for the customer |
|
26
|
|
|
public function store(Request $request) |
|
27
|
|
|
{ |
|
28
|
|
|
$request->validate([ |
|
29
|
|
|
'custID' => 'required', |
|
30
|
|
|
'system' => 'required' |
|
31
|
|
|
]); |
|
32
|
|
|
|
|
33
|
|
|
// Insert the system into the DB |
|
34
|
|
|
$sys = CustomerSystems::create([ |
|
35
|
|
|
'cust_id' => $request->custID, |
|
36
|
|
|
'sys_id' => $request->system |
|
37
|
|
|
]); |
|
38
|
|
|
|
|
39
|
|
|
// Get the data fields for the new system |
|
40
|
|
|
$fields = SystemCustDataFields::where('sys_id', $request->system)->get(); |
|
41
|
|
|
|
|
42
|
|
|
// Enter each of the data fields into the DB |
|
43
|
|
|
foreach($fields as $field) |
|
44
|
|
|
{ |
|
45
|
|
|
CustomerSystemFields::create([ |
|
46
|
|
|
'cust_sys_id' => $sys->cust_sys_id, |
|
47
|
|
|
'field_id' => $field->field_id, |
|
48
|
|
|
'value' => isset($request->fieldData[$field->field_id]) ? $request->fieldData[$field->field_id] : null |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return response()->json(['success' => true]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Get the list of systems attached to the customer |
|
56
|
|
|
public function show($id) |
|
57
|
|
|
{ |
|
58
|
|
|
$sysList = CustomerSystems::where('cust_id', $id)->get(); |
|
59
|
|
|
|
|
60
|
|
|
$sysArr = []; |
|
61
|
|
|
foreach($sysList as $sys) |
|
62
|
|
|
{ |
|
63
|
|
|
// Pull all system data |
|
64
|
|
|
$sysName = SystemTypes::find($sys->sys_id)->name; |
|
65
|
|
|
$sysData = CustomerSystemFields::where('cust_sys_id', $sys->cust_sys_id) |
|
66
|
|
|
->leftJoin('system_cust_data_fields', 'customer_system_fields.field_id', '=', 'system_cust_data_fields.field_id') |
|
67
|
|
|
->leftJoin('system_cust_data_types', 'system_cust_data_fields.data_type_id', '=', 'system_cust_data_types.data_type_id') |
|
68
|
|
|
->orderBy('order', 'asc') |
|
69
|
|
|
->get(); |
|
70
|
|
|
|
|
71
|
|
|
// Sort the data attached to the system |
|
72
|
|
|
$dataArr = []; |
|
73
|
|
|
$dataArr[] = [ |
|
74
|
|
|
'name' => 'System Type', |
|
75
|
|
|
'value' => $sysName, |
|
76
|
|
|
'id' => 0 |
|
77
|
|
|
]; |
|
78
|
|
|
foreach($sysData as $data) |
|
79
|
|
|
{ |
|
80
|
|
|
$dataArr[] = [ |
|
81
|
|
|
'name' => $data->name, |
|
82
|
|
|
'value' => $data->value, |
|
83
|
|
|
'id' => $data->id |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Populate the system array |
|
88
|
|
|
$sysArr[] = [ |
|
89
|
|
|
'sys_id' => $sys->sys_id, |
|
90
|
|
|
'name' => $sysName, |
|
91
|
|
|
'cust_sys_id' => $sys->cust_sys_id, |
|
92
|
|
|
'data' => $dataArr |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return response()->json($sysArr); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Get the data fields attached to a system |
|
100
|
|
|
public function getDataFields($id) |
|
101
|
|
|
{ |
|
102
|
|
|
return response()->json($this->getFields($id)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Update the customers system data |
|
106
|
|
|
public function update(Request $request, $id) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$request->validate([ |
|
109
|
|
|
'custID' => 'required', |
|
110
|
|
|
'system' => 'required', |
|
111
|
|
|
'fieldData' => 'required' |
|
112
|
|
|
]); |
|
113
|
|
|
|
|
114
|
|
|
foreach($request->fieldData as $data) |
|
115
|
|
|
{ |
|
116
|
|
|
if($data['id'] != 0) |
|
117
|
|
|
{ |
|
118
|
|
|
CustomerSystemFields::find($data['id'])->update([ |
|
119
|
|
|
'value' => !empty($data['value']) ? $data['value'] : null |
|
120
|
|
|
]); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return response()->json(['success' => true]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Remove the specified resource from storage. |
|
129
|
|
|
* |
|
130
|
|
|
* @param int $id |
|
131
|
|
|
* @return \Illuminate\Http\Response |
|
132
|
|
|
*/ |
|
133
|
|
|
public function destroy($id) |
|
134
|
|
|
{ |
|
135
|
|
|
$system = CustomerSystems::find($id); |
|
136
|
|
|
|
|
137
|
|
|
Log::info('Customer System Deleted for Customer ID-'.$system->cust_id.' by User ID-'.Auth::user()->user_id.'. System ID-'.$id); |
|
138
|
|
|
|
|
139
|
|
|
$system->delete(); |
|
140
|
|
|
|
|
141
|
|
|
return response()->json(['success' => true]); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|