diff --git a/app/Controllers/InvoiceController.php b/app/Controllers/InvoiceController.php index 81526f0..dba0e5e 100644 --- a/app/Controllers/InvoiceController.php +++ b/app/Controllers/InvoiceController.php @@ -40,6 +40,14 @@ class InvoiceController extends ResourceController try { $fromDateRaw = $this->request->getGet('from_date'); $toDateRaw = $this->request->getGet('to_date'); + $agentNameRaw = $this->request->getGet('agent_name'); + $agentCodeRaw = $this->request->getGet('agent_code'); + $policyNumberRaw = $this->request->getGet('policy_no'); + + $agentName = trim((string) ($agentNameRaw ?? '')); + $agentCode = trim((string) ($agentCodeRaw ?? '')); + $policyNumber = trim((string) ($policyNumberRaw ?? '')); + $fromParsed = $this->parseOptionalDmYDate($fromDateRaw); $toParsed = $this->parseOptionalDmYDate($toDateRaw); @@ -122,6 +130,47 @@ class InvoiceController extends ResourceController $query->where('DATE(partner_invoice.invoice_date) <=', $toParsed['ymd']); } + if ($agentName !== '' || $agentCode !== '') { + $agentQuery = $this->db->table('partner_agent')->select('id'); + + if ($agentName !== '') { + $agentQuery->like('name', $agentName); + } + if ($agentCode !== '') { + $agentQuery->like('agent_code', $agentCode); + } + + $agentIds = array_column($agentQuery->get()->getResultArray(), 'id'); + + if (empty($agentIds)) { + return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200); + } + + $query->groupStart(); + foreach ($agentIds as $agentId) { + $query->orWhere( + 'JSON_SEARCH(partner_invoice.agent_id, "one", CAST(' . (int) $agentId . ' AS CHAR)) IS NOT NULL', + null, + false + ); + } + $query->groupEnd(); + } + + if ($policyNumber !== '') { + $policyLike = '%' . $this->db->escapeLikeString($policyNumber) . '%'; + $query->where( + 'EXISTS ( + SELECT 1 FROM partner_invoice_items pii + WHERE pii.invoice_id = partner_invoice.id + AND pii.is_active = 1 + AND pii.policy_no LIKE ' . $this->db->escape($policyLike) . ' + )', + null, + false + ); + } + $data = $query->orderBy('partner_invoice.id', 'DESC')->findAll(); return $this->respond(['status' => 'success', 'code' => 200,'data' => $data ], 200); diff --git a/app/Controllers/MasterController.php b/app/Controllers/MasterController.php index 073f587..6da1771 100644 --- a/app/Controllers/MasterController.php +++ b/app/Controllers/MasterController.php @@ -116,7 +116,7 @@ class MasterController extends ResourceController public function saveInsurer() { - $input = $this->request->getPost(); + $input = $this->request->getJSON(true); if (empty($input['name'])) { return $this->respond(['status' => 404, 'message' => 'Insurer name is required', 'data' => []]);