diff --git a/app/Controllers/InvoiceController.php b/app/Controllers/InvoiceController.php index 2262a25..9ead9c0 100644 --- a/app/Controllers/InvoiceController.php +++ b/app/Controllers/InvoiceController.php @@ -197,6 +197,7 @@ class InvoiceController extends ResourceController ]; $invoiceId = $input['id'] ?? null; + $isCreate = empty($invoiceId); if (!$invoiceId) { @@ -252,6 +253,70 @@ class InvoiceController extends ResourceController } } + + + /* + * UTR save (Tempa) + */ + if ($isCreate && !empty($input['utrs']) && is_array($input['utrs'])) { + + $seenUtrs = []; + + foreach ($input['utrs'] as $utrRow) { + + $utrNo = trim((string)($utrRow['utr_no'] ?? '')); + + + if ($utrNo === '') continue; + + + // if (in_array($utrNo, $seenUtrs, true)) { + // $this->db->transRollback(); + // return $this->respond([ + // 'status' => 'failed', + // 'code' => 400, + // 'message'=> "Duplicate UTR in request: {$utrNo}" + // ], 400); + // } + + $seenUtrs[] = $utrNo; + + // $exists = $this->InvoiceUtrModel->where('utr_no', $utrNo)->where('is_active', 1)->first(); + + // if ($exists) { + // $this->db->transRollback(); + // return $this->respond([ + // 'status' => 'failed', + // 'code' => 400, + // 'message'=> "UTR already exists: {$utrNo}" + // ], 400); + // } + + // ✅ Format values + // $utrDate = !empty($utrRow['utr_date']) + // ? date('Y-m-d', strtotime($utrRow['utr_date'])) + // : null; + + // $amount = isset($utrRow['amount']) + // ? (float)$utrRow['amount'] + // : 0; + + $result_utr = $this->InvoiceUtrModel->insert([ + 'invoice_id' => $invoiceId, + 'utr_no' => $utrNo, + 'utr_date' => null, + 'amount' => 0, + 'is_active' => 1, + 'created_at' => date('Y-m-d H:i:s'), + 'created_by' => $input['created_by'] ?? 0 + ]); + + // if (!$result_utr) { + // print_r($this->InvoiceUtrModel->errors()); + // die; + // } + } + } if ($this->db->transStatus() === false) { $this->db->transRollback(); @@ -340,7 +405,13 @@ class InvoiceController extends ResourceController // Build the main query $query = $this->PolicyModel - ->select('partner_policy.policy_number as policy_no, partner_policy.id as policy_id, partner_policy.issued_date, partner_policy.commission_amount, partner_policy.insured_name as customer_name, pe.agent_id ,pa.name as agent_name,pa.agent_code, partner_policy.premium_amount, pi.id as invoice_id, pi.invoice_no') + ->select('partner_policy.policy_number as policy_no, partner_policy.id as policy_id, partner_policy.issued_date, partner_policy.commission_amount, partner_policy.insured_name as customer_name, pe.agent_id ,pa.name as agent_name,pa.agent_code, partner_policy.premium_amount, pi.id as invoice_id, pi.invoice_no, + COALESCE(( + SELECT GROUP_CONCAT(piu.utr_no ORDER BY piu.id SEPARATOR ", ") + FROM partner_invoice_utr piu + WHERE piu.invoice_id = pi.id + AND piu.is_active = 1 + ), "") as utr_no', false) ->join( 'partner_enquiry pe', 'pe.id = partner_policy.enquiry_id AND pe.broker_id = ' . (int)$input['broker_id'],