From c7eb344dcbd3bede8a67ae4ebe7524ad79297691 Mon Sep 17 00:00:00 2001 From: Gowtham M Date: Sat, 4 Apr 2026 17:01:40 +0530 Subject: [PATCH] GWM : getAgentUnusedCommissionList --- app/Controllers/InvoiceController.php | 69 +++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/app/Controllers/InvoiceController.php b/app/Controllers/InvoiceController.php index 5665eb5..c83b795 100644 --- a/app/Controllers/InvoiceController.php +++ b/app/Controllers/InvoiceController.php @@ -38,8 +38,36 @@ class InvoiceController extends ResourceController public function invoiceList() { try { + $fromDateRaw = $this->request->getGet('from_date'); + $toDateRaw = $this->request->getGet('to_date'); - $data = $this->InvoiceModel + $fromParsed = $this->parseOptionalDmYDate($fromDateRaw); + $toParsed = $this->parseOptionalDmYDate($toDateRaw); + + if ($fromParsed['error'] !== null) { + return $this->respond([ + 'status' => 'failed', + 'code' => 400, + 'data' => $fromParsed['error'], + ], 400); + } + if ($toParsed['error'] !== null) { + return $this->respond([ + 'status' => 'failed', + 'code' => 400, + 'data' => $toParsed['error'], + ], 400); + } + + if ($fromParsed['ymd'] !== null && $toParsed['ymd'] !== null && $fromParsed['ymd'] > $toParsed['ymd']) { + return $this->respond([ + 'status' => 'failed', + 'code' => 400, + 'data' => 'from_date must be on or before to_date', + ], 400); + } + + $query = $this->InvoiceModel ->select('partner_invoice.*, PB.name as broker_name, pos.name as pos_name, @@ -78,9 +106,16 @@ class InvoiceController extends ResourceController false) ->join('partner_brokers PB', 'PB.id = partner_invoice.broker_id', 'left') ->join('partner_pos pos', 'pos.id = partner_invoice.pos_id', 'left') - ->where('partner_invoice.is_active', 1) - ->orderBy('partner_invoice.id', 'DESC') - ->findAll(); + ->where('partner_invoice.is_active', 1); + + if ($fromParsed['ymd'] !== null) { + $query->where('DATE(partner_invoice.invoice_date) >=', $fromParsed['ymd']); + } + if ($toParsed['ymd'] !== null) { + $query->where('DATE(partner_invoice.invoice_date) <=', $toParsed['ymd']); + } + + $data = $query->orderBy('partner_invoice.id', 'DESC')->findAll(); return $this->respond(['status' => 'success', 'code' => 200,'data' => $data ], 200); @@ -89,6 +124,32 @@ class InvoiceController extends ResourceController } } + /** + * Optional invoice list date filter: format d-m-Y (e.g. 10-03-2026). Empty / null = no filter. + * + * @return array{ymd: ?string, error: ?string} + */ + private function parseOptionalDmYDate($value): array + { + if ($value === null) { + return ['ymd' => null, 'error' => null]; + } + $v = trim((string) $value); + if ($v === '') { + return ['ymd' => null, 'error' => null]; + } + + $dt = \DateTime::createFromFormat('d-m-Y', $v); + if ($dt === false || $dt->format('d-m-Y') !== $v) { + return [ + 'ymd' => null, + 'error' => 'Invalid date format; use d-m-Y (e.g. 10-03-2026)', + ]; + } + + return ['ymd' => $dt->format('Y-m-d'), 'error' => null]; + } + public function findInvoiceWithItems() { try {