From 28c12f440257e0a94d741730f91118f942b02140 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Tue, 14 Oct 2025 13:34:18 +0800 Subject: [PATCH] E-Invoice - API, Sales Invoice Report - added more checking and validation --- .../ListBookingsSalesInvoiceLogic.php | 43 +++++++++++++++++++ .../Rules/CanListBookingsSalesInvoice.php | 40 +++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 app/Classes/Modules/Bookings/Standards/Rules/CanListBookingsSalesInvoice.php diff --git a/app/Classes/Modules/Bookings/ControllersLogic/ListBookingsSalesInvoiceLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/ListBookingsSalesInvoiceLogic.php index a2134295..ddaacd85 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/ListBookingsSalesInvoiceLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/ListBookingsSalesInvoiceLogic.php @@ -43,6 +43,49 @@ class ListBookingsSalesInvoiceLogic extends AbstractControllerLogic public function logic(Request $request) : JsonResponse { // $this->canListBookings->passes(); //cief todo: 90 - apipub + $filters = $request->input('filters'); + + // Decode JSON if sent as string + if (is_string($filters)) { + $filters = json_decode($filters, true); + } + + if (!is_array($filters)) { + return response()->json([ + 'success' => false, + 'message' => 'Invalid filters format. Expected JSON object.' + ], 400); + } + + // Only these filters are allowed + $allowedKeys = ['per_page', 'status', 'order_by', 'date_range', 'date_range_no_data']; + + $extra = array_diff(array_keys($filters), $allowedKeys); + if (!empty($extra)) { + return response()->json([ + 'success' => false, + 'message' => 'Invalid filter(s) provided: ' . implode(', ', $extra), + ], 400); + } + + // Required filters that must always exist + $alwaysRequired = ['per_page', 'status']; + + $missing = array_diff($alwaysRequired, array_keys($filters)); + if (!empty($missing)) { + return response()->json([ + 'success' => false, + 'message' => 'Missing required filter(s): ' . implode(', ', $missing), + ], 400); + } + + // Required at least one of: date_range or date_range_no_data + if (!isset($filters['date_range']) && !isset($filters['date_range_no_data'])) { + return response()->json([ + 'success' => false, + 'message' => 'Either "date_range" or "date_range_no_data" filter is required.', + ], 400); + } $query = $this->listsBookings->execute($this->listsBookings->deserializeFilters($request->input('filters'))); diff --git a/app/Classes/Modules/Bookings/Standards/Rules/CanListBookingsSalesInvoice.php b/app/Classes/Modules/Bookings/Standards/Rules/CanListBookingsSalesInvoice.php new file mode 100644 index 00000000..b2f409da --- /dev/null +++ b/app/Classes/Modules/Bookings/Standards/Rules/CanListBookingsSalesInvoice.php @@ -0,0 +1,40 @@ +