|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use App\Http\Requests\Customers\CustomerNoteRequest; |
|
7
|
|
|
|
|
8
|
|
|
use App\Events\Customers\Notes\CustomerNoteAddedEvent; |
|
9
|
|
|
use App\Events\Customers\Notes\CustomerNoteDeletedEvent; |
|
10
|
|
|
use App\Events\Customers\Notes\CustomerNoteUpdatedEvent; |
|
11
|
|
|
use App\Events\Customers\Notes\CustomerNoteRestoredEvent; |
|
12
|
|
|
use App\Events\Customers\Notes\CustomerNoteForceDeletedEvent; |
|
13
|
|
|
|
|
14
|
|
|
use App\Models\Customer; |
|
15
|
|
|
use App\Models\CustomerNote; |
|
16
|
|
|
|
|
17
|
2 |
|
class CustomerNoteController extends Controller |
|
18
|
|
|
{ |
|
19
|
2 |
|
/** |
|
20
|
2 |
|
* Store a new customer note |
|
21
|
|
|
*/ |
|
22
|
|
|
public function store(CustomerNoteRequest $request) |
|
23
|
2 |
|
{ |
|
24
|
|
|
$cust = Customer::findOrFail($request->cust_id); |
|
25
|
1 |
|
$cust_id = $cust->cust_id; |
|
26
|
|
|
|
|
27
|
|
|
// If the equipment is shared, it must be assigned to the parent site |
|
28
|
2 |
|
if($request->shared && $cust->parent_id > 0) |
|
29
|
2 |
|
{ |
|
30
|
2 |
|
$cust_id = $cust->parent_id; |
|
31
|
2 |
|
} |
|
32
|
2 |
|
|
|
33
|
2 |
|
$note = CustomerNote::create([ |
|
34
|
2 |
|
'cust_id' => $cust_id, |
|
35
|
|
|
'created_by' => $request->user()->user_id, |
|
36
|
|
|
'urgent' => $request->urgent, |
|
37
|
2 |
|
'shared' => $request->shared, |
|
38
|
2 |
|
'subject' => $request->subject, |
|
39
|
|
|
'details' => $request->details, |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
|
|
event(new CustomerNoteAddedEvent($cust, $note)); |
|
43
|
|
|
return back()->with([ |
|
44
|
2 |
|
'message' => 'Note Created', |
|
45
|
|
|
'type' => 'success', |
|
46
|
2 |
|
]); |
|
47
|
|
|
} |
|
48
|
2 |
|
|
|
49
|
2 |
|
/** |
|
50
|
|
|
* Update an existing customer note |
|
51
|
1 |
|
*/ |
|
52
|
2 |
|
public function update(CustomerNoteRequest $request, $id) |
|
53
|
2 |
|
{ |
|
54
|
2 |
|
$cust = Customer::findOrFail($request->cust_id); |
|
55
|
|
|
$cust_id = $cust->cust_id; |
|
56
|
|
|
|
|
57
|
|
|
// If the equipment is shared, it must be assigned to the parent site |
|
58
|
|
|
if($request->shared && $cust->parent_id > 0) |
|
59
|
|
|
{ |
|
60
|
2 |
|
$cust_id = $cust->parent_id; |
|
61
|
|
|
} |
|
62
|
2 |
|
|
|
63
|
2 |
|
$note = CustomerNote::find($id); |
|
64
|
|
|
$note->update([ |
|
65
|
|
|
'cust_id' => $cust_id, |
|
66
|
2 |
|
'updated_by' => $request->user()->user_id, |
|
67
|
|
|
'urgent' => $request->urgent, |
|
68
|
1 |
|
'shared' => $request->shared, |
|
69
|
|
|
'subject' => $request->subject, |
|
70
|
|
|
'details' => $request->details, |
|
71
|
2 |
|
]); |
|
72
|
2 |
|
|
|
73
|
2 |
|
event(new CustomerNoteUpdatedEvent($cust, $note)); |
|
|
|
|
|
|
74
|
2 |
|
return back()->with([ |
|
75
|
2 |
|
'message' => 'Note Updated', |
|
76
|
2 |
|
'type' => 'success', |
|
77
|
2 |
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
/** |
|
81
|
2 |
|
* Delete a customer note |
|
82
|
|
|
*/ |
|
83
|
|
|
public function destroy($id) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->authorize('delete', CustomerNote::class); |
|
86
|
|
|
$note = CustomerNote::find($id); |
|
87
|
2 |
|
$note->delete(); |
|
88
|
|
|
|
|
89
|
2 |
|
event(new CustomerNoteDeletedEvent(Customer::find($note->cust_id), $note)); |
|
|
|
|
|
|
90
|
|
|
return back()->with([ |
|
91
|
1 |
|
'message' => 'Note Deleted', |
|
92
|
|
|
'type' => 'danger', |
|
93
|
1 |
|
]); |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/* |
|
97
|
|
|
* Restore a deleted note |
|
98
|
|
|
*/ |
|
99
|
|
|
public function restore($id) |
|
100
|
2 |
|
{ |
|
101
|
|
|
$this->authorize('restore', CustomerNote::class); |
|
102
|
2 |
|
$note = CustomerNote::withTrashed()->where('note_id', $id)->first(); |
|
103
|
1 |
|
$note->restore(); |
|
104
|
1 |
|
|
|
105
|
|
|
event(new CustomerNoteRestoredEvent(Customer::find($note->cust_id), $note)); |
|
|
|
|
|
|
106
|
1 |
|
return redirect()->back()->with(['message' => 'Customer Note restored', 'type' => 'success']); |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/* |
|
110
|
|
|
* Permanently delete a note |
|
111
|
|
|
*/ |
|
112
|
|
|
public function forceDelete($id) |
|
113
|
2 |
|
{ |
|
114
|
|
|
$this->authorize('forceDelete', CustomerNote::class); |
|
115
|
2 |
|
|
|
116
|
|
|
$note = CustomerNote::withTrashed()->where('note_id', $id)->first(); |
|
117
|
1 |
|
$note->forceDelete(); |
|
118
|
|
|
|
|
119
|
1 |
|
event(new CustomerNoteForceDeletedEvent(Customer::find($note->cust_id), $note)); |
|
|
|
|
|
|
120
|
1 |
|
return redirect()->back()->with(['message' => 'Note permanently deleted', 'type' => 'danger']); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|