1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use App\Customers; |
6
|
|
|
use App\CustomerNotes; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use App\Http\Controllers\Controller; |
11
|
|
|
use Illuminate\Support\Facades\Route; |
12
|
|
|
|
13
|
|
|
class CustomerNotesController extends Controller |
14
|
|
|
{ |
15
|
|
|
|
16
|
26 |
|
public function __construct() |
17
|
|
|
{ |
18
|
26 |
|
$this->middleware('auth'); |
19
|
26 |
|
} |
20
|
|
|
|
21
|
|
|
// Store a new customer note |
22
|
6 |
|
public function store(Request $request) |
23
|
|
|
{ |
24
|
6 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
25
|
|
|
|
26
|
6 |
|
$request->validate([ |
27
|
6 |
|
'cust_id' => 'required|numeric', |
28
|
|
|
'title' => 'required', |
29
|
|
|
'note' => 'required' |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
// Determine if the note should go to the customer, or its parent |
33
|
4 |
|
$details = Customers::find($request->cust_id); |
34
|
4 |
|
if ($details->parent_id && $request->shared == 'true') { |
35
|
2 |
|
$request->cust_id = $details->parent_id; |
36
|
|
|
} |
37
|
|
|
|
38
|
4 |
|
$noteID = CustomerNotes::create([ |
39
|
4 |
|
'cust_id' => $request->cust_id, |
40
|
4 |
|
'user_id' => Auth::user()->user_id, |
41
|
4 |
|
'urgent' => $request->urgent, |
42
|
4 |
|
'shared' => $request->shared == 'true' ? 1 : 0, |
43
|
4 |
|
'subject' => $request->title, |
44
|
4 |
|
'description' => $request->note |
45
|
|
|
]); |
46
|
|
|
|
47
|
4 |
|
Log::info('New Customer Note Created for Customer ID - '.$request->custID.' by '.Auth::user()->full_name.'. New Note ID - '.$noteID->note_id); |
48
|
4 |
|
return response()->json(['success' => true]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Get the customer notes |
52
|
4 |
|
public function show($id) |
53
|
|
|
{ |
54
|
4 |
|
$notes = CustomerNotes::where('cust_id', $id)->orderBy('urgent', 'desc')->get(); |
55
|
|
|
|
56
|
|
|
// Determine if there is a parent site with shared notes |
57
|
4 |
|
$parent = Customers::find($id)->parent_id; |
58
|
4 |
|
if ($parent) { |
59
|
2 |
|
$parentList = CustomerNotes::where('cust_id', $parent)->where('shared', 1)->orderBy('urgent', 'desc')->get(); |
60
|
|
|
|
61
|
2 |
|
$notes = $notes->merge($parentList); |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name); |
65
|
4 |
|
return $notes; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Update a customer note |
69
|
6 |
|
public function update(Request $request, $id) |
70
|
|
|
{ |
71
|
6 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
72
|
|
|
|
73
|
6 |
|
$request->validate([ |
74
|
6 |
|
'cust_id' => 'required', |
75
|
|
|
'title' => 'required', |
76
|
|
|
'note' => 'required' |
77
|
|
|
]); |
78
|
|
|
|
79
|
4 |
|
$details = Customers::find($request->cust_id); |
80
|
4 |
|
if ($details->parent_id && $request->shared == 'true') { |
81
|
2 |
|
$request->cust_id = $details->parent_id; |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
CustomerNotes::find($id)->update([ |
85
|
4 |
|
'cust_id' => $request->cust_id, |
86
|
4 |
|
'shared' => $request->shared == 'true' ? 1 : 0, |
87
|
4 |
|
'urgent' => $request->urgent, |
88
|
4 |
|
'subject' => $request->title, |
89
|
4 |
|
'description' => $request->note |
90
|
|
|
]); |
91
|
|
|
|
92
|
4 |
|
Log::info('Customer Note ID - '.$id.' updated by '.Auth::user()->full_name); |
93
|
4 |
|
return response()->json(['success' => true]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Delete a customer note |
97
|
2 |
|
public function destroy($id) |
98
|
|
|
{ |
99
|
2 |
|
CustomerNotes::find($id)->delete(); |
100
|
|
|
|
101
|
2 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name); |
102
|
2 |
|
Log::notice('Customer Note ID - '.$id.' deleted by '.Auth::user()->full_name); |
103
|
2 |
|
return response()->json(['success' => true]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|