Test Failed
Push — dev6 ( d203b2...9db5ac )
by Ron
17:53 queued 02:15
created

CustomerNoteController::forceDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Models\CustomerNote;
6
use App\Http\Controllers\Controller;
7
use App\Http\Requests\Customers\CustomerNoteRequest;
8
use App\Models\Customer;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Log;
11
12
class CustomerNoteController extends Controller
13
{
14
    /**
15
     *  Store a new customer note
16
     */
17
    public function store(CustomerNoteRequest $request)
18
    {
19
        $cust    = Customer::findOrFail($request->cust_id);
20
        $cust_id = $cust->cust_id;
21
22
        //  If the equipment is shared, it must be assigned to the parent site
23
        if($request->shared && $cust->parent_id > 0)
24
        {
25
            $cust_id = $cust->parent_id;
26
        }
27
28
        CustomerNote::create([
29
            'cust_id'    => $cust_id,
30
            'created_by' => $request->user()->user_id,
31
            'urgent'     => $request->urgent,
32
            'shared'     => $request->shared,
33
            'subject'    => $request->subject,
34
            'details'    => $request->details,
35
        ]);
36
37
        Log::channel('cust')->info('New Customer Note created for Customer '.$cust->name.' 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...
38
        return response()->noContent();
39
    }
40
41
    /**
42
     *  Get all of the notes for a customer
43
     */
44
    public function show($id)
45
    {
46
        $cust = Customer::findOrFail($id);
47
48
        return CustomerNote::where('cust_id', $id)
49
                ->when($cust->parent_id, function($q) use ($cust)
50
                {
51
                    $q->orWhere('cust_id', $cust->parent_id)->where('shared', true);
52
                })
53
                ->orderBy('urgent', 'DESC')
54
                ->get();
55
    }
56
57
    /**
58
     *  Modify an existing note
59
     */
60
    public function update(CustomerNoteRequest $request, $id)
61
    {
62
        $cust    = Customer::findOrFail($request->cust_id);
63
        $cust_id = $cust->cust_id;
64
65
        //  If the equipment is shared, it must be assigned to the parent site
66
        if($request->shared && $cust->parent_id > 0)
67
        {
68
            $cust_id = $cust->parent_id;
69
        }
70
71
        CustomerNote::find($id)->update([
72
            'cust_id'    => $cust_id,
73
            'updated_by' => $request->user()->user_id,
74
            'urgent'     => $request->urgent,
75
            'shared'     => $request->shared,
76
            'subject'    => $request->subject,
77
            'details'    => $request->details,
78
        ]);
79
80
        Log::channel('cust')->info('Customer Note ID '.$id.' updated for Customer '.$cust->name.' 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...
81
        return response()->noContent();
82
    }
83
84
    /**
85
     *  Delete a customer Note
86
     */
87
    public function destroy($id)
88
    {
89
        $this->authorize('delete', CustomerNote::class);
90
91
        CustomerNote::find($id)->delete();
92
93
        Log::channel('cust')->notice('Customer Note ID '.$id.' 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...
94
        return response()->noContent();
95
    }
96
97
    /*
98
    *   Restore a deleted note
99
    */
100
    public function restore($id)
101
    {
102
        $this->authorize('restore', CustomerNote::class);
103
        $note = CustomerNote::withTrashed()->where('note_id', $id)->first();
104
        $note->restore();
105
106
        Log::channel('cust')->info('Customer Note '.$note->note_id.' was restored for Customer ID '.$note->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...
107
        return redirect()->back()->with(['message' => 'Customer Note restored', 'type' => 'success']);
108
    }
109
110
    /*
111
    *   Permanently delete a note
112
    */
113
    public function forceDelete($id)
114
    {
115
        $this->authorize('forceDelete', CustomerNote::class);
116
117
        $note = CustomerNote::withTrashed()->where('note_id', $id)->first();
118
119
        Log::channel('cust')->alert('Customer Note '.$note->subject.' has been permanently 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...
120
        $note->forceDelete();
121
122
        return redirect()->back()->with(['message' => 'Note permanently deleted', 'type' => 'danger']);
123
    }
124
}
125