|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Traits\FileTrait; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
|
|
8
|
|
|
use App\Models\Customer; |
|
9
|
|
|
use App\Models\CustomerFile; |
|
10
|
|
|
use App\Models\CustomerFileType; |
|
11
|
|
|
|
|
12
|
|
|
use App\Http\Controllers\Controller; |
|
13
|
|
|
use App\Http\Requests\Customers\CustomerFileRequest; |
|
14
|
|
|
|
|
15
|
|
|
use App\Events\Customers\Files\CustomerFileAddedEvent; |
|
16
|
|
|
use App\Events\Customers\Files\CustomerFileDeletedEvent; |
|
17
|
|
|
use App\Events\Customers\Files\CustomerFileForceDeletedEvent; |
|
18
|
|
|
use App\Events\Customers\Files\CustomerFileRestoredEvent; |
|
19
|
|
|
use App\Events\Customers\Files\CustomerFileUpdatedEvent; |
|
20
|
|
|
|
|
21
|
|
|
class CustomerFileController extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
use FileTrait; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Store a file for a specific customer |
|
27
|
|
|
*/ |
|
28
|
|
|
public function store(CustomerFileRequest $request) |
|
29
|
|
|
{ |
|
30
|
|
|
// Make sure that the file information was stored in the users session |
|
31
|
|
|
$this->checkForFile(); |
|
32
|
|
|
$fileData = $request->session()->pull('new-file-upload'); |
|
33
|
|
|
|
|
34
|
|
|
$cust = Customer::findOrFail($request->cust_id); |
|
35
|
|
|
$cust_id = $cust->cust_id; |
|
36
|
|
|
|
|
37
|
|
|
// If the equipment is shared, it must be assigned to the parent site |
|
38
|
|
|
if($request->shared && $cust->parent_id > 0) |
|
39
|
|
|
{ |
|
40
|
|
|
$cust_id = $cust->parent_id; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$newFile = CustomerFile::create([ |
|
44
|
|
|
'file_id' => $fileData[0]->file_id, |
|
45
|
|
|
'file_type_id' => CustomerFileType::where('description', $request->type)->first()->file_type_id, |
|
46
|
|
|
'cust_id' => $cust_id, |
|
47
|
|
|
'user_id' => $request->user()->user_id, |
|
48
|
|
|
'shared' => $request->shared, |
|
49
|
|
|
'name' => $request->name, |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
event(new CustomerFileAddedEvent(Customer::find($request->cust_id), $newFile)); |
|
|
|
|
|
|
53
|
|
|
return back()->with([ |
|
54
|
|
|
'message' => 'File Added', |
|
55
|
|
|
'type' => 'success', |
|
56
|
|
|
]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Update the details for a customer uploaded file - not the file itself |
|
61
|
|
|
*/ |
|
62
|
|
|
public function update(CustomerFileRequest $request, $id) |
|
63
|
|
|
{ |
|
64
|
|
|
$cust = Customer::findOrFail($request->cust_id); |
|
65
|
|
|
$cust_id = $cust->cust_id; |
|
66
|
|
|
|
|
67
|
|
|
// If the equipment is shared, it must be assigned to the parent site |
|
68
|
|
|
if($request->shared && $cust->parent_id > 0) |
|
69
|
|
|
{ |
|
70
|
|
|
$cust_id = $cust->parent_id; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$fileData = CustomerFile::find($id); |
|
74
|
|
|
$fileData->update([ |
|
75
|
|
|
'file_type_id' => CustomerFileType::where('description', $request->type)->first()->file_type_id, |
|
76
|
|
|
'cust_id' => $cust_id, |
|
77
|
|
|
'shared' => $request->shared, |
|
78
|
|
|
'name' => $request->name, |
|
79
|
|
|
]); |
|
80
|
|
|
|
|
81
|
|
|
event(new CustomerFileUpdatedEvent($cust, $fileData)); |
|
|
|
|
|
|
82
|
|
|
return back()->with([ |
|
83
|
|
|
'message' => 'File Information Updated', |
|
84
|
|
|
'type' => 'success' |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Delete a customer file |
|
90
|
|
|
*/ |
|
91
|
|
|
public function destroy($id) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->authorize('delete', CustomerFile::class); |
|
94
|
|
|
$file = CustomerFile::find($id); |
|
95
|
|
|
$file->delete(); |
|
96
|
|
|
|
|
97
|
|
|
event(new CustomerFileDeletedEvent(Customer::find($file->cust_id), $file)); |
|
|
|
|
|
|
98
|
|
|
return back()->with([ |
|
99
|
|
|
'message' => 'File Deleted', |
|
100
|
|
|
'type' => 'danger', |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/* |
|
105
|
|
|
* Restore a deleted file |
|
106
|
|
|
*/ |
|
107
|
|
|
public function restore($id) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->authorize('restore', CustomerFile::class); |
|
110
|
|
|
$file = CustomerFile::withTrashed()->where('cust_file_id', $id)->first(); |
|
111
|
|
|
$file->restore(); |
|
112
|
|
|
|
|
113
|
|
|
event(new CustomerFileRestoredEvent(Customer::find($file->cust_id), $file)); |
|
|
|
|
|
|
114
|
|
|
return back()->with(['message' => 'Customer File restored', 'type' => 'success']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/* |
|
118
|
|
|
* Permanently delete a file |
|
119
|
|
|
*/ |
|
120
|
|
|
public function forceDelete($id) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->authorize('forceDelete', CustomerFile::class); |
|
123
|
|
|
|
|
124
|
|
|
$file = CustomerFile::withTrashed()->where('cust_file_id', $id)->first(); |
|
125
|
|
|
$file->forceDelete(); |
|
126
|
|
|
$this->deleteFile($file->file_id); |
|
127
|
|
|
|
|
128
|
|
|
event(new CustomerFileForceDeletedEvent(Customer::find($file->cust_id), $file)); |
|
|
|
|
|
|
129
|
|
|
return back()->with(['message' => 'File permanently deleted', 'type' => 'danger']); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|