Test Failed
Branch dev5 (bb1748)
by Ron
08:17
created

CustomerAdminController::enableCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Customers;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Log;
8
use App\Http\Controllers\Controller;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Route;
11
use App\CustomerFileTypes;
12
use App\HTTP\Resources\CustomerFileTypesCollection;
13
14
class CustomerAdminController extends Controller
15
{
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
        $this->middleware(function ($request, $next) {
20
            $this->authorize('hasAccess', 'Manage Customers');
21
            return $next($request);
22
        });
23
    }
24
25
    //  Form to change a Cusomter's ID
26
    public function modifyIdIndex()
27
    {
28
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
29
        return view('admin.customerID');
30
    }
31
32
    //  Submit the form to update a customer's ID
33
    public function updateID(Request $request)
34
    {
35
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data - ', $request->toArray());
36
37
        $request->validate([
38
            'original_id' => 'required|numeric|exists:customers,cust_id',
39
            'cust_id'     => 'nullable|numeric|unique:customers,cust_id',
40
        ]);
41
42
        $data = Customers::find($request->original_id)->update([
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
43
            'cust_id' => $request->cust_id,
44
        ]);
45
46
        // Log::notice('Customer ID Updated for '.$data->name.'. Old Customer ID - '.$request->original_id.' New Customer ID - '.$request->cust_id);
47
        return response()->json(['success' => true]);
48
    }
49
50
    //  Form to view what kind of file types can be assigned to customers
51
    public function fileTypes()
52
    {
53
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
54
        return view('admin.customerFileTypes');
55
    }
56
57
    //  Get the types of files that can be assigned to a customer file
58
    public function getFileTypes()
59
    {
60
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
61
62
        $types = new CustomerFileTypesCollection(CustomerFileTypes::all());
63
64
        Log::debug('Customer File Types gathered - ', array($types));
65
        return $types;
66
    }
67
68
    //  Submit a new file type name
69
    public function submitNewFileType(Request $request)
70
    {
71
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name . '. Submitted Data - ', $request->toArray());
72
        $request->validate([
73
            'name' => 'required',
74
        ]);
75
76
        CustomerFileTypes::create([
77
            'description' => $request->name,
78
        ]);
79
80
        Log::notice('New Customer File Type ' . $request->name . ' created by '. Auth::user()->full_name);
81
        return response()->json(['success' => true]);
82
    }
83
84
    //  Update the name of a file type
85
    public function setFileTypes(Request $request)
86
    {
87
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name . '. Submitted Data - ', $request->toArray());
88
        $request->validate([
89
            'name' => 'required',
90
            'id'   => 'required|exists:customer_file_types,file_type_id',
91
        ]);
92
93
        CustomerFileTypes::find($request->id)->update([
94
            'description' => $request->name,
95
        ]);
96
97
        Log::notice('Customer File Type ID '.$request->id.' name updated to '.$request->name.' by '.Auth::user()->full_name);
98
        return response()->json(['success' => true]);
99
    }
100
101
    //  Try to delete a file type
102
    public function delFileType($id)
103
    {
104
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
105
106
        try {
107
            //  Try to delete file type from database - will throw error if foreign key is in use
108
            CustomerFileTypes::find($id)->delete();
109
        } catch (\Illuminate\Database\QueryException $e) {
110
            //  Unable to remove file type  from the database
111
            Log::warning('Attempt to delete file type ID '.$id.' by User '.Auth::user()->full_name.' failed.  Reason - ' . $e);
112
            return response()->json(['success' => false, 'reason' => 'In Use']);
113
        }
114
115
        Log::notice('Customer File Type ID '.$id.' deleted by '.Auth::user()->full_name);
116
        return response()->json(['success' => true]);
117
    }
118
119
    //  Show all disabled customers
120
    public function showDisabled()
121
    {
122
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
123
124
        $custList = Customers::select('cust_id', 'name', 'deleted_at')
125
            ->onlyTrashed()
126
            ->get()
127
            ->makeVisible('deleted_at');
128
129
        Log::debug('Deactivated Customer data gathered - ', array($custList));
130
        return view('admin.customerDisabledList', ['custList' => $custList]);
131
    }
132
133
    //  Re-enable a customer
134
    public function enableCustomer($id)
135
    {
136
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
137
138
        Customers::withTrashed()->where('cust_id', $id)->restore();
139
140
        Log::notice('Customer ID '.$id.' re-enabled by '.Auth::user()->full_name);
141
        return response()->json(['success' => true]);
142
    }
143
}
144