Test Failed
Push — dev6 ( 033ddc...06d260 )
by Ron
20:10
created

CustomerEquipmentController::show()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Models\Customer;
6
use App\Models\DataField;
7
use App\Models\CustomerEquipment;
8
use App\Http\Controllers\Controller;
9
use App\Models\CustomerEquipmentData;
10
use App\Http\Requests\Customers\CustomerEquipmentRequest;
11
12
use Illuminate\Support\Facades\Log;
13
use Illuminate\Support\Facades\Auth;
14
15
class CustomerEquipmentController extends Controller
16
{
17
    /**
18
     *  Store the new Customer Equipment
19
     */
20
    public function store(CustomerEquipmentRequest $request)
21
    {
22
        $cust = Customer::findOrFail($request->cust_id);
23
24
        //  If the equipment is shared, it must be assigned to the parent site
25
        if($request->shared)
26
        {
27
            if($cust->parent_id !== null)
28
            {
29
                $request->cust_id = $cust->parent_id;
30
            }
31
        }
32
33
        //  Input the equipment type
34
        $newEquip = CustomerEquipment::create($request->only(['cust_id', 'equip_id', 'shared']));
35
36
        //  Input the equipment data
37
        foreach($request->data as $field)
38
        {
39
            CustomerEquipmentData::create([
40
                'cust_equip_id' => $newEquip->cust_equip_id,
41
                'field_id'      => DataField::where('equip_id', $request->equip_id)->where('type_id', $field['type_id'])->first()->field_id,//  ???
42
                'value'         => isset($field['value']) ? $field['value'] : null,
43
            ]);
44
        }
45
46
        Log::channel('cust')->notice('New Equipment ID '.$request->equip_id.' has been added for Customer ID '.$request->cust_id.'('.$cust->name.') by '.$request->user()->username);
47
        return back()->with(['message' => 'Successfully Added Equipment', 'type' => 'success']);
48
    }
49
50
    /**
51
     *  Ajax call to get the list of customer equipment
52
     */
53
    public function show($id)
54
    {
55
        $customer  = Customer::find($id);
56
        $custEquip = CustomerEquipment::where('cust_id', $id)
57
                ->with('CustomerEquipmentData')
58
                ->get();
59
60
        if($customer->parent_id)
61
        {
62
            $custEquip = CustomerEquipment::where('cust_id', $customer->parent_id)
63
                            ->whereShared(true)
64
                            ->with('CustomerEquipmentData')
65
                            ->get()->merge($custEquip);
66
        }
67
68
        return $custEquip;
69
    }
70
71
    /**
72
     *  Update the customers equipment information
73
     */
74
    public function update(CustomerEquipmentRequest $request, $id)
75
    {
76
        $cust = Customer::findOrFail($request->cust_id);
77
78
        //  If the equipment is shared, it must be assigned to the parent site
79
        if($request->shared)
80
        {
81
            if($cust->parent_id !== null)
82
            {
83
                $request->cust_id = $cust->parent_id;
84
            }
85
        }
86
87
        CustomerEquipment::find($id)->update(['cust_id' => $request->cust_id, 'shared' => $request->shared]);
88
89
        foreach($request->data as $field)
90
        {
91
            CustomerEquipmentData::where('id', $field['id'])->where('cust_equip_id', $id)->update([
92
                'value' => $field['value'],
93
            ]);
94
        }
95
96
        Log::channel('cust')->notice('Customer Equipment ID '.$id.' has been updated for Customer ID '.$request->cust_id.'('.$cust->name.') by '.$request->user()->username);
97
        return back()->with(['message' => 'Successfully Updated Equipment', 'type' => 'success']);
98
    }
99
100
    /**
101
     *  Remove the selected equipment
102
     */
103
    public function destroy($id)
104
    {
105
        $equip = CustomerEquipment::find($id);
106
107
        Log::channel('cust')->notice('Equipment '.$equip->name.' for Customer ID '.$equip->cust_id.' was Soft Deleted by'.Auth::user()->username);
1 ignored issue
show
Bug introduced by
Accessing username on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
108
        $equip->delete();
109
        return response()->noContent();
110
    }
111
112
    /*
113
    *   Restore a piece of equipment that was deleted
114
    */
115
    public function restore($id)
116
    {
117
        CustomerEquipment::withTrashed()->where('cust_equip_id', $id)->restore();
118
        $equip = CustomerEquipment::find($id);
119
        Log::channel('cust')->info('Equipment '.$equip->equip_id.' was restored for Customer ID '.$equip->cust_id.' by '.Auth::user()->username);
1 ignored issue
show
Bug introduced by
Accessing username on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
120
121
        return redirect()->back();
122
    }
123
124
    /*
125
    *   Completely delete the equipment
126
    */
127
    public function forceDelete($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

127
    public function forceDelete(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        return 'force delete equipment';
130
    }
131
}
132