Passed
Push — dev5 ( a24f56...eeca7a )
by Ron
08:23
created

SetCustomerEquipment::updateEquipment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 6
rs 10
1
<?php
2
3
namespace App\Domains\Customers;
4
5
use Illuminate\Support\Facades\Log;
6
use Illuminate\Support\Facades\Auth;
7
8
use App\Customers;
9
use App\CustomerSystems;
10
use App\CustomerSystemData;
11
12
use App\Http\Requests\CustomerNewEquipmentRequest;
13
use App\Http\Requests\CustomerEditEquipmentRequest;
14
15
class SetCustomerEquipment
16
{
17
    protected $custID;
18
19 4
    public function __construct($custID)
20
    {
21 4
        $this->custID = $custID;
22 4
    }
23
24
    //  Attach new equipment to the customer
25 4
    public function creatNewEquipment(CustomerNewEquipmentRequest $request)
26
    {
27 4
        $equipID = $this->validateEquipment($request->equip['sys_id']);
28 4
        if($equipID)
29
        {
30
            return false;
31
        }
32
33
        //  Determine if the equipment should go to the customer or their parent
34 4
        if($request->share)
35
        {
36 2
            $parent = Customers::find($this->custID)->parent_id;
37
38 2
            if($parent)
39
            {
40 2
                $this->custID = $parent;
41
            }
42
        }
43
44 4
        $newSys = CustomerSystems::create([
45 4
            'cust_id' => $this->custID,
46 4
            'sys_id' => $request->equip['sys_id'],
47 4
            'shared' => $request->share,
48
        ]);
49
50 4
        foreach($request->fields as $field)
51
        {
52 4
            CustomerSystemData::create([
53 4
                'cust_sys_id' => $newSys->cust_sys_id,
54 4
                'field_id' => $field['field_id'],
55 4
                'value' => isset($field['value']) ? $field['value'] : null,
56
            ]);
57
        }
58
59 4
        Log::notice('New equipment '.$request->equip['name'].' created for Customer ID '.$this->custID.' by '.Auth::user()->full_name);
60 4
        return true;
61
    }
62
63
    //  Update a customers exising equipment
64
    public function updateEquipment(CustomerEditEquipmentRequest $request)
65
    {
66
        //  Verify equipment is valid
67
        $equipID = $this->validateEquipment($request->equip['sys_id'])->cust_sys_id;
68
        if(!$equipID)
69
        {
70
            return false;
71
        }
72
        $this->updateEquipmentFields($equipID, $request->fields);
73
74
        Log::info('Customer Equipment Updated for Customer ID - '.$this->custID.' by '.Auth::user()->full_name.'.  Equipment Data - ', array($request->equip));
75
        return true;
76
    }
77
78
    //  Delete equipmetn that is assigned to customer
79
    public function deleteEquipment($equipID)
80
    {
81
        //  Get the data to make sure this is not shared
82
        $equip = CustomerSystems::find($equipID);
83
        if($equip->shared && $this->custID != $equip->cust_id)
84
        {
85
            return false;
86
        }
87
88
        $equip->delete();
89
90
        Log::notice('Customer Equipment ID '.$equipID.' deleted by '.Auth::user()->full_name);
91
        return true;
92
    }
93
94
    //  Verify the equipment belongs to the customer or parent customer
95 4
    protected function validateEquipment($sysID)
96
    {
97
        //  Check local customer first
98 4
        $valid = CustomerSystems::where('cust_id', $this->custID)->where('sys_id', $sysID)->first();
99
100 4
        if(!$valid)
101
        {
102 4
            $parent = Customers::find($this->custID)->parent_id;
103 4
            if($parent)
104
            {
105 2
                $valid = CustomerSystems::where('cust_id', $parent)->where('sys_id', $sysID)->first();
106
            }
107
        }
108
109 4
        return $valid;
110
    }
111
112
    //  Update the equipment data
113
    protected function updateEquipmentFields($equipID, $newData)
114
    {
115
        $curData = CustomerSystemData::where('cust_sys_id', $equipID)->get();
116
        $newData = collect($newData);
117
118
        foreach($curData as $data)
119
        {
120
            CustomerSystemData::find($data->id)->update([
121
                'value' => $newData->where('field_id', $data->field_id)->first()['value'],
122
            ]);
123
        }
124
    }
125
}
126