Passed
Push — dev6 ( 318218...c800c6 )
by Ron
17:51
created

CustomerController::forceDelete()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 31
rs 9.7998
ccs 0
cts 0
cp 0
cc 4
nc 5
nop 1
crap 20
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use Inertia\Inertia;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
10
use App\Traits\FileTrait;
11
use App\Http\Controllers\Controller;
12
use App\Events\Customers\Admin\CustomerRestoredEvent;
13
use App\Events\Customers\Admin\CustomerForceDeletedEvent;
14
15
use App\Events\Customers\NewCustomerCreated;
16
use App\Events\Customers\CustomerDetailsUpdated;
17
use App\Events\Customers\CustomerDeactivatedEvent;
18
19
use App\Models\Customer;
20
use App\Models\EquipmentType;
21
use App\Models\PhoneNumberType;
22
use App\Models\CustomerFileType;
23
use App\Models\UserCustomerBookmark;
24
25
use App\Http\Requests\Customers\NewCustomerRequest;
26
use App\Http\Requests\Customers\EditCustomerRequest;
27
use App\Models\CustomerFile;
28
29 1
class CustomerController extends Controller
30
{
31 1
    use FileTrait;
1 ignored issue
show
Bug introduced by
The trait App\Traits\FileTrait requires the property $username which is not provided by App\Http\Controllers\Customers\CustomerController.
Loading history...
32 1
33 1
    /**
34
     * Search page for finding a customer
35
     */
36
    public function index(Request $request)
37
    {
38
        return Inertia::render('Customers/Index', [
39
            'create'      => $request->user()->can('create', Customer::class),
40 2
            'equip_types' => EquipmentType::orderBy('cat_id')->get()->pluck('name')->values(),
41
        ]);
42 2
    }
43 1
44
    /**
45
     * Show the form for creating a new Customer
46
     */
47
    public function create()
48
    {
49 1
        $this->authorize('create', Customer::class);
50
        return Inertia::render('Customers/Create');
51 1
    }
52 1
53 1
    /**
54
     * Create a new Customer
55 1
     */
56
    public function store(NewCustomerRequest $request)
57 1
    {
58
        $cust         = $request->toArray();
59
        $cust['slug'] = Str::slug($request->name);
60
        $newCust      = Customer::create($cust);
61
62
        event(new NewCustomerCreated($newCust));
63 3
        return redirect(route('customers.show',$newCust->slug))->with(['message' => 'New Customer Created', 'type' => 'success']);
64
    }
65
66 3
    /**
67
     * Display the Customers Information
68
     */
69 1
    public function show($id)
70 1
    {
71
        //  Check if we are passing the customer slugged name, or customer ID number
72
        if(is_numeric($id))
73 2
        {
74 2
            //  To keep things uniform, redirect to a link that has the customers name rather than the ID
75 2
            $customer = Customer::findOrFail($id);
76 2
            return redirect(route('customers.show', $customer->slug));
77 2
        }
78 2
79 2
        //  Pull the customers information
80 2
        $customer = Customer::where('slug', $id)
81 2
                        ->orWhere('cust_id', $id)
82 2
                        ->with('Parent')
83 2
                        ->with('CustomerEquipment.CustomerEquipmentData')
84 2
                        ->with('ParentEquipment.CustomerEquipmentData')
85 1
                        ->with('CustomerContact.CustomerContactPhone.PhoneNumberType')
86 1
                        ->with('ParentContact.CustomerContactPhone.PhoneNumberType')
87 1
                        ->with('CustomerNote')
88
                        ->with('ParentNote')
89 1
                        ->with('CustomerFile.FileUpload')
90 1
                        ->with('ParentFile.FileUpload')
91 1
                        ->firstOrFail();
92 1
        //  Determine if the customer is bookmarked by the user
93
        $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...
94 1
                        ->where('cust_id', $customer->cust_id)
95 1
                        ->count();
96 1
97 1
        return Inertia::render('Customers/Show', [
98
            'details'        => $customer,
99 1
            'phone_types'    => PhoneNumberType::all(),
100 1
            'file_types'     => CustomerFileType::all(),
101 1
            //  User Permissions for customers
102
            'user_data' => [
103
                'fav'        => $isFav,                                                  //  Customer is bookmarked by the user
104 1
                'edit'       => Auth::user()->can('update', $customer),                  //  User is allowed to edit the customers basic details
105 1
                'manage'     => Auth::user()->can('manage', $customer),                  //  User can recover deleted items
106 1
                'deactivate' => Auth::user()->can('delete', $customer),                  //  User can deactivate the customer profile
107
                'equipment'  => [
108
                    'create' => Auth::user()->can('create', CustomerEquipment::class),   //  If user can add equipment
109 1
                    'update' => Auth::user()->can('update', CustomerEquipment::class),   //  If user can edit equipment
110 1
                    'delete' => Auth::user()->can('delete', CustomerEquipment::class),   //  If user can delete equipment
111 1
                ],
112
                'contacts'   => [
113
                    'create' => Auth::user()->can('create', CustomerContact::class),     //  If user can add contact
114 1
                    'update' => Auth::user()->can('update', CustomerContact::class),     //  If user can edit contact
115 1
                    'delete' => Auth::user()->can('delete', CustomerContact::class),     //  If user can delete contact
116 1
                ],
117
                'notes'      => [
118
                    'create' => Auth::user()->can('create', CustomerNote::class),        //  If user can add note
119
                    'update' => Auth::user()->can('update', CustomerNote::class),        //  If user can edit note
120
                    'delete' => Auth::user()->can('delete', CustomerNote::class),        //  If user can delete note
121
                ],
122
                'files'     => [
123
                    'create' => Auth::user()->can('create', CustomerFile::class),        //  If user can add file
124
                    'update' => Auth::user()->can('update', CustomerFile::class),        //  If user can edit file
125 1
                    'delete' => Auth::user()->can('delete', CustomerFile::class),        //  If user can delete file
126
                ],
127 1
            ],
128 1
        ]);
129 1
    }
130
131 1
    /**
132
     * Update the customers basic details
133
     */
134
    public function update(EditCustomerRequest $request, $id)
135
    {
136
        $cust         = $request->toArray();
137 2
        $cust['slug'] = Str::slug($request->name);
138
139 2
        $data = Customer::findOrFail($id);
140
        $data->update($cust);
141 2
        event(new CustomerDetailsUpdated($data, $id));
142 1
143
        return redirect(route('customers.show', $cust['slug']))->with(['message' => 'Customer Details Updated', 'type' => 'success']);
144 1
    }
145 1
146
    /**
147
     * Soft Delete a Customer from the database
148
     */
149
    public function destroy($id)
150
    {
151 2
        $cust = Customer::findOrFail($id);
152
        $this->authorize('delete', $cust);
153 2
        $cust->delete();
154
155 1
        event(new CustomerDeactivatedEvent($cust));
156 1
        return redirect(route('customers.index'))->with(['message' => 'Customer '.$cust->name.' deactivated', 'type' => 'danger']);
157
    }
158 1
159
    /**
160 1
     * Restore a soft deleted customer
161
     */
162
    public function restore(Request $idArr)
163
    {
164
        $this->authorize('restore', Customer::class);
165
166 2
        //  Customer ID's are in an array
167
        foreach($idArr->list as $cust)
168 2
        {
169
            $cust = Customer::onlyTrashed()->where('cust_id', $cust['cust_id'])->firstOrFail();
170 1
            $cust->restore();
171
172 1
            event(new CustomerRestoredEvent($cust));
173 1
        }
174
175 1
        return back()->with([
176
            'message' => 'Customers Restored',
177
            'type'    => 'success',
178
        ]);
179
    }
180
181
    /**
182
     * Permanently Delete a customer and all associated files and information
183
     */
184
    public function forceDelete(Request $id)
185
    {
186
        $this->authorize('forceDelete', Customer::class);
187
188
        //  Customer ID's are in an array
189
        foreach($id->list as $cust)
190
        {
191
            $cust     = Customer::onlyTrashed()->where('cust_id', $cust['cust_id'])->firstOrFail();
192
            $fileList = [];
193
194
            //  Get all of the files that are attached to the customer
195
            $files = CustomerFile::where('cust_id', $cust->cust_id)->get();
196
            foreach($files as $file)
197
            {
198
                $fileList[] = $file->file_id;
199
            }
200
201
            $cust->forceDelete();
202
203
            //  Delete the files from the Storage System
204
            foreach($fileList as $file)
205
            {
206
                $this->deleteFile($file);
207
            }
208
209
            event(new CustomerForceDeletedEvent($cust));
210
        }
211
212
        return back()->with([
213
            'message' => 'Customers Deleted',
214
            'type'    => 'danger',
215
        ]);
216
    }
217
}
218