Passed
Push — dev6 ( 1762ad...6b002f )
by Ron
08:16
created

CustomerController::forceDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use Inertia\Inertia;
6
7
use App\Models\Customer;
8
use App\Models\CustomerNote;
9
use App\Models\CustomerFile;
10
use App\Models\EquipmentType;
11
use App\Models\CustomerContact;
12
use App\Models\PhoneNumberType;
13
use App\Models\CustomerFileType;
14
use App\Models\CustomerEquipment;
15
use App\Http\Controllers\Controller;
16
use App\Models\UserCustomerBookmark;
17
use App\Http\Requests\Customers\CustomerRequest;
18
19
use Illuminate\Support\Str;
20
use Illuminate\Http\Request;
21
use Illuminate\Support\Facades\Log;
22
use Illuminate\Support\Facades\Auth;
23
24
class CustomerController extends Controller
25
{
26
    /**
27
     *  Customer search page
28
     */
29 1
    public function index()
30
    {
31 1
        return Inertia::render('Customer/index', [
32 1
            'can_create'  => Auth::user()->can('create', Customer::class),
33 1
            'equip_types' => EquipmentType::orderBy('cat_id')->get()->pluck('name')->values(),
34
        ]);
35
    }
36
37
    /**
38
     *  Form to create a new customer
39
     */
40 2
    public function create()
41
    {
42 2
        $this->authorize('create', Customer::class);
43 1
        return Inertia::render('Customer/create');
44
    }
45
46
    /**
47
     *  Save a newly created customer
48
     */
49 1
    public function store(CustomerRequest $request)
50
    {
51 1
        $cust         = $request->toArray();
52 1
        $cust['slug'] = Str::slug($request->name);
53 1
        $newCust      = Customer::create($cust);
54
55 1
        Log::channel('cust')->info('New Customer - '.$request->name.' created by '.Auth::user()->full_name);
1 ignored issue
show
Bug introduced by
Accessing full_name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
56
57 1
        return redirect(route('customers.show',$newCust->slug))->with(['message' => 'New Customer Created', 'type' => 'success']);
58
    }
59
60
    /**
61
     *  Show the customer details
62
     */
63 3
    public function show($id)
64
    {
65
        //  Check if we are passing the customer slugged name, or customer ID number
66 3
        if(is_numeric($id))
67
        {
68
            //  To keep things uniform, redirect to a link that has the customers name rather than the ID
69 1
            $customer = Customer::findOrFail($id);
70 1
            return redirect(route('customers.show', $customer->slug));
71
        }
72
73 2
        $customer = Customer::where('slug', $id)
74 2
                        ->orWhere('cust_id', $id)
75 2
                        ->with('Parent')
76 2
                        ->with('CustomerEquipment.CustomerEquipmentData')
77 2
                        ->with('ParentEquipment.CustomerEquipmentData')
78 2
                        ->with('CustomerContact.CustomerContactPhone.PhoneNumberType')
79 2
                        ->with('ParentContact.CustomerContactPhone.PhoneNumberType')
80 2
                        ->with('CustomerNote')
81 2
                        ->with('ParentNote')
82 2
                        ->with('CustomerFile.FileUpload')
83 2
                        ->with('ParentFile.FileUpload')
84 2
                        ->firstOrFail();
85 1
        $isFav    = UserCustomerBookmark::where('user_id', Auth::user()->user_id)
1 ignored issue
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
86 1
                        ->where('cust_id', $customer->cust_id)
87 1
                        ->count();
88
89 1
        return Inertia::render('Customer/details', [
90 1
            'details'        => $customer,
91 1
            'phone_types'    => PhoneNumberType::all(),
92 1
            'file_types'     => CustomerFileType::all(),
93
            'user_functions' => [
94 1
                'fav'        => $isFav,                                                  //  Customer is bookmarked by the user
95 1
                'edit'       => Auth::user()->can('update', $customer),                  //  User is allowed to edit the customers basic details
96 1
                'manage'     => Auth::user()->can('manage', $customer),                  //  User can recover deleted items
97 1
                'deactivate' => Auth::user()->can('delete', $customer),                  //  User can deactivate the customer profile
98
                'equipment'  => [
99 1
                    'create' => Auth::user()->can('create', CustomerEquipment::class),   //  If user can add equipment
100 1
                    'update' => Auth::user()->can('update', CustomerEquipment::class),   //  If user can edit equipment
101 1
                    'delete' => Auth::user()->can('delete', CustomerEquipment::class),   //  If user can delete eqipment
102
                ],
103
                'contacts'   => [
104 1
                    'create' => Auth::user()->can('create', CustomerContact::class),     //  If user can add contact
105 1
                    'update' => Auth::user()->can('update', CustomerContact::class),     //  If user can edit contact
106 1
                    'delete' => Auth::user()->can('delete', CustomerContact::class),     //  If user can delete contact
107
                ],
108
                'notes'      => [
109 1
                    'create' => Auth::user()->can('create', CustomerNote::class),        //  If user can add note
110 1
                    'update' => Auth::user()->can('update', CustomerNote::class),        //  If user can edit note
111 1
                    'delete' => Auth::user()->can('delete', CustomerNote::class),        //  If user can delete note
112
                ],
113
                'files'     => [
114 1
                    'create' => Auth::user()->can('create', CustomerFile::class),        //  If user can add file
115 1
                    'update' => Auth::user()->can('update', CustomerFile::class),        //  If user can edit file
116 1
                    'delete' => Auth::user()->can('delete', CustomerFile::class),        //  If user can delete file
117
                ],
118
            ],
119
        ]);
120
    }
121
122
    /**
123
     *  Update the customers basic information
124
     */
125 1
    public function update(CustomerRequest $request, $id)
126
    {
127 1
        $cust         = $request->toArray();
128 1
        $cust['slug'] = Str::slug($request->name);
129 1
        Customer::find($id)->update($cust);
130
131 1
        return redirect(route('customers.show', $cust['slug']))->with(['message' => 'Customer Details Updated', 'type' => 'success']);
132
    }
133
134
    /**
135
     *  Deactivate the customer
136
     */
137 2
    public function destroy($id)
138
    {
139 2
        $cust = Customer::findOrFail($id);
140
141 2
        $this->authorize('delete', $cust);
142 1
        $cust->delete();
143
144 1
        Log::channel('cust')->alert('Customer ID '.$id.' has been deactivated 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...
145 1
        return redirect(route('customers.index'))->with(['message' => 'Customer '.$cust->name.' deactivated', 'type' => 'danger']);
146
    }
147
148
    /*
149
    *   Restore a customer who was deactivated
150
    */
151 2
    public function restore($id)
152
    {
153 2
        $this->authorize('manage', Customer::class);
154
155 1
        $cust = Customer::withTrashed()->where('cust_id', $id)->firstOrFail();
156 1
        $cust->restore();
157
158 1
        Log::channel('cust')->info('Customer '.$cust->name.' has been restored 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...
159
160 1
        return redirect()->back()->with(['message' => 'Customers Restored', 'type' => 'success']);
161
    }
162
163
    /*
164
    *   Completely Delete a customer and all associated informatin/files
165
    */
166 2
    public function forceDelete($id)
167
    {
168 2
        $this->authorize('manage', Customer::class);
169
170 1
        $cust = Customer::withTrashed()->where('cust_id', $id)->firstOrFail();
171
172 1
        Log::channel('cust')->notice('Customer '.$cust->name.' 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...
173 1
        $cust->forceDelete();
174
175 1
        return response()->noContent();
176
    }
177
}
178