Passed
Push — dev5 ( d3ea14...eaf296 )
by Ron
08:06
created

CustomerSystemsController::update()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.4704

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
nc 4
nop 2
dl 0
loc 34
ccs 11
cts 18
cp 0.6111
crap 6.4704
rs 9.3554
c 1
b 0
f 0
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 22
    public function __construct()
20
    {
21 22
        $this->middleware('auth');
22 22
    }
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 2
    public function store(Request $request)
34
    {
35 2
        $request->validate([
36 2
            '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 2
        $details = Customers::find($request->cust_id);
43 2
        if($details->parent_id && $request->shared)
44
        {
45
            $request->cust_id = $details->parent_id;
1 ignored issue
show
Bug introduced by
The property cust_id does not seem to exist on Illuminate\Http\Request.
Loading history...
46
        }
47
48
        //  Insert the system into the DB
49 2
        $sys = CustomerSystems::create([
50 2
            'cust_id' => $request->cust_id,
51 2
            'sys_id'  => $request->system,
52 2
            'shared'  => $request->shared,
53
        ]);
54
55
        //  Get the data fields for the new system
56 2
        $fields = SystemDataFields::where('sys_id', $request->system)->get();
57
58
        //  Enter each of the data fields into the DB
59 2
        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 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
70 2
        Log::info('New Customer System Added - Customer ID-'.$request->cust_id.' System ID-'.$request->system);
71 2
        Log::debug('Submitted System Data', $request->toArray());
72 2
        return response()->json(['success' => true]);
73
    }
74
75
    //  Get the list of systems attached to the customer
76 4
    public function show($id)
77
    {
78 4
        $sysList = CustomerSystems::where('cust_id', $id)
79 4
                    ->with('SystemTypes')
80 4
                    ->with('SystemDataFields')
81 4
                    ->with('SystemDataFields.SystemDataFieldTypes')
82 4
                    ->get();
83
84
        //  determine if there is a parent site with shared systems
85 4
        $parent = Customers::findOrFail($id)->parent_id;
86 2
        if($parent != null)
87
        {
88
            $parentList = CustomerSystems::where('cust_id', $parent)
89
                                ->where('shared', 1)
90
                                ->with('SystemTypes')
91
                                ->with('SystemDataFields')
92
                                ->with('SystemDataFields.SystemDataFieldTypes')
93
                                ->get();
94
95
            $sysList = $sysList->merge($parentList);
96
        }
97
98 2
        return $sysList;
99
    }
100
101
    // Update the customers system data
102 2
    public function update(Request $request, $id)
103
    {
104 2
        $request->validate([
105 2
            'cust_id'    => 'required',
106
            'system'    => 'required',
107
        ]);
108
109
        //  Verify the system type and customer ID match
110 2
        $valid = CustomerSystems::where('cust_id', $request->cust_id)->where('cust_sys_id', $id)->first();
111
112 2
        if(!$valid)
113
        {
114
            return abort(400);
115
        }
116
117 2
        $fields = SystemDataFields::where('sys_id', $request->system)->get();
118
119 2
        foreach($fields as $data)
120
        {
121
            $fieldName = 'field_' . $data->field_id;
122
            if(isset($request->$fieldName))
123
            {
124
                Log::debug($request->$fieldName);
125
            }
126
            Log::debug($fieldName);
127
            CustomerSystemData::where('cust_sys_id', $id)->where('field_id', $data->field_id)->update([
128
                'value'       => isset($request->$fieldName) ? $request->$fieldName : null
129
            ]);
130
        }
131
132 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
133 2
        Log::notice('Customer System Updated.  Cust ID-'.$request->custID.' System ID-'.$request->sysstem.' User ID-'.Auth::user()->user_id);
134 2
        Log::debug('Submitted Data - ID:'.$id.' - ', $request->toArray());
135 2
        return response()->json(['success' => true]);
136
    }
137
138
    //  Delete a system attached to a customer
139 2
    public function destroy($id)
140
    {
141
        // return response('deleted '.$id);
142 2
        $system = CustomerSystems::find($id);
143
144 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
145 2
        Log::notice('Customer System Deleted for Customer ID-'.$system->cust_id.' by User ID-'.Auth::user()->user_id.'. System ID-'.$id);
146 2
        Log::debug('System Data', $system->toArray());
147
148 2
        $system->delete();
149
150 2
        return response()->json(['success' => true]);
151
    }
152
}
153