diff --git a/app/Http/Controllers/ClientsController.php b/app/Http/Controllers/ClientsController.php index 012deac..67ca0b4 100644 --- a/app/Http/Controllers/ClientsController.php +++ b/app/Http/Controllers/ClientsController.php @@ -832,7 +832,7 @@ class ClientsController extends Controller } public function update_notes(Request $request, $id) { - $note = ClientNote::findOrFail($id); + $note = Models\ClientNote::findOrFail($id); // Optional: Double-check the 2-day rule on the backend for security if ($note->created_at->diffInDays(now()) > 2) { @@ -859,7 +859,7 @@ class ClientsController extends Controller ]); // Find the record and update it - $shortcode = ShortCode::findOrFail($id); // Adjust 'ShortCode' to your actual model name + $shortcode = Models\ShortCode::findOrFail($id); // Adjust 'ShortCode' to your actual model name $shortcode->update($validated); // Return the JSON response for SweetAlert @@ -879,7 +879,7 @@ class ClientsController extends Controller ]); // Find the record and update it - $payment = ClientPayment::findOrFail($id); // Adjust 'ClientPayment' to your actual model name + $payment = Models\ClientPayment::findOrFail($id); // Adjust 'ClientPayment' to your actual model name $payment->update($validated); // Return the JSON response for SweetAlert @@ -889,6 +889,44 @@ class ClientsController extends Controller ]); } + public function destroy_note($id) + { + $note = Models\ClientNote::findOrFail($id); + + // Enforce the 2-day restriction on the backend for security + if ($note->created_at->diffInDays(now()) > 2) { + return response()->json(['message' => 'Cannot delete notes older than 2 days.'], 403); + } + + $note->delete(); + + return response()->json([ + 'success' => true, + 'message' => 'Note deleted successfully.' + ]); + } + public function destroy_short_code($id) + { + $shortcode = Models\ShortCode::findOrFail($id); // Adjust model name if needed + $shortcode->delete(); + + return response()->json([ + 'success' => true, + 'message' => 'Short code deleted successfully.' + ]); + } + + public function destroy_payment($id) + { + $payment = Models\ClientPayment::findOrFail($id); // Adjust model name if needed + $payment->delete(); + + return response()->json([ + 'success' => true, + 'message' => 'Payment record deleted successfully.' + ]); + } + diff --git a/public/assets/js/client-show-modal.js b/public/assets/js/client-show-modal.js index 8a3d4d9..9759834 100644 --- a/public/assets/js/client-show-modal.js +++ b/public/assets/js/client-show-modal.js @@ -386,16 +386,11 @@ document.addEventListener("DOMContentLoaded", function() { $(document).on('click', '.delete-note-btn', function() { let id = $(this).data('id'); if(confirm("Are you sure you want to delete this note? This action cannot be undone.")) { - // Send AJAX DELETE request using the meta tag for CSRF $.ajax({ - url: `/notes/${id}`, + url: base_url + `/client-notes/${id}`, type: 'DELETE', - headers: { - 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') - }, - success: function(res) { - location.reload(); - } + headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, + success: function(res) { location.reload(); } }); } }); @@ -405,7 +400,7 @@ document.addEventListener("DOMContentLoaded", function() { let id = $(this).data('id'); if(confirm("Are you sure you want to delete this short code?")) { $.ajax({ - url: `/shortcodes/${id}`, + url: base_url + `/shortcodes/${id}`, type: 'DELETE', headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, success: function(res) { location.reload(); } @@ -418,7 +413,7 @@ document.addEventListener("DOMContentLoaded", function() { let id = $(this).data('id'); if(confirm("Are you sure you want to delete this payment record?")) { $.ajax({ - url: `/payments/${id}`, + url: base_url + `/client-payments/${id}`, type: 'DELETE', headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, success: function(res) { location.reload(); } @@ -426,7 +421,9 @@ document.addEventListener("DOMContentLoaded", function() { } }); - // --- POPULATE NOTE EDIT MODAL --- + // --- POPULATE EDIT MODALS --- + + // Populate Note Edit Modal $(document).on('click', '.edit-note-btn', function() { let id = $(this).data('id'); let body = $(this).closest('.list-group-item').find('p').text(); @@ -434,13 +431,12 @@ document.addEventListener("DOMContentLoaded", function() { $('#edit_note_id').val(id); $('#edit_note_body').val(body); - $('#editNoteForm').attr('action', `/notes/${id}`); - - // Replaced vanilla JS with jQuery modal show + // Fixed URL + $('#editNoteForm').attr('action', base_url + `/client-notes/${id}`); $('#editNoteModal').modal('show'); }); - // --- POPULATE SHORT CODE EDIT MODAL --- + // Populate Short Code Edit Modal $(document).on('click', '.edit-shortcode-btn', function() { let id = $(this).data('id'); @@ -452,13 +448,12 @@ document.addEventListener("DOMContentLoaded", function() { $('#edit_sc_expiry').val($(this).data('expiry')); $('#edit_sc_remarks').val($(this).data('remarks')); - $('#editShortcodeForm').attr('action', `/shortcodes/${id}`); - - // Replaced vanilla JS with jQuery modal show + // Fixed URL + $('#editShortcodeForm').attr('action', base_url + `/shortcodes/${id}`); $('#editShortcodeModal').modal('show'); }); - // --- POPULATE PAYMENT EDIT MODAL --- + // Populate Payment Edit Modal $(document).on('click', '.edit-payment-btn', function() { let id = $(this).data('id'); @@ -468,9 +463,8 @@ document.addEventListener("DOMContentLoaded", function() { $('#edit_pay_date').val($(this).data('date')); $('#edit_pay_status').val($(this).data('status')); - $('#editPaymentForm').attr('action', `/payments/${id}`); - - // Replaced vanilla JS with jQuery modal show + // Fixed URL + $('#editPaymentForm').attr('action', base_url + `/client-payments/${id}`); $('#editPaymentModal').modal('show'); }); diff --git a/routes/web.php b/routes/web.php index 7e8a127..27e2b60 100644 --- a/routes/web.php +++ b/routes/web.php @@ -23,11 +23,23 @@ Route::middleware(['auth'])->group(function () { Route::get('/', [App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard'); Route::get('home', [App\Http\Controllers\DashboardController::class, 'index'])->name('home'); + #Clients + Route::put('/clients/notes/{id}', [App\Http\Controllers\ClientsController::class, 'update_notes']); + Route::post('/clients/shortcodes/{id}', [App\Http\Controllers\ClientsController::class, 'update_short_code']); + Route::put('/clients/payments/{id}', [App\Http\Controllers\ClientsController::class, 'update_payment']); - Route::put('/notes/{id}', [App\Http\Controllers\ClientsController::class, 'update_notes']); + // Notes + Route::put('/client-notes/{id}', [App\Http\Controllers\ClientsController::class, 'update_notes']); + Route::delete('/client-notes/{id}', [App\Http\Controllers\ClientsController::class, 'destroy_notes']); + + // Payments + Route::put('/client-payments/{id}', [App\Http\Controllers\ClientsController::class, 'update_payment']); + Route::delete('/client-payments/{id}', [App\Http\Controllers\ClientsController::class, 'destroy_payment']); + + // Shortcodes Route::put('/shortcodes/{id}', [App\Http\Controllers\ClientsController::class, 'update_short_code']); - Route::put('/payments/{id}', [App\Http\Controllers\ClientsController::class, 'update_payment']); + Route::delete('/shortcodes/{id}', [App\Http\Controllers\ClientsController::class, 'destroy_short_code']); Route::get('/clients/data', [App\Http\Controllers\ClientsController::class, 'fetchData'])->name('clients.data'); Route::get('/clients/export', [App\Http\Controllers\ClientsController::class, 'export'])->name('clients.export');