FIX_CreateInvoice

This commit is contained in:
sanjeev.p 2026-04-03 17:24:25 +05:30
parent 8316178230
commit 6f9c084499

View File

@ -157,10 +157,61 @@ class InvoiceController extends ResourceController
try {
$input = $this->request->getJSON(true);
// Normalize pos_id (client may send null/0/non-numeric like "w")
// Normalize pos_id
$posIdRaw = $input['pos_id'] ?? 0;
$posId = is_numeric($posIdRaw) ? (int)$posIdRaw : 0;
$payoutStatus = isset($input['payout_status']) ? (int) $input['payout_status'] : 1;
$invoiceId = $input['id'] ?? null;
$isCreate = empty($invoiceId);
// ─────────────────────────────────────────────────────────────
// PRE-SAVE VALIDATION: UTR checks (only on CREATE)
// Run BEFORE any insert/update so nothing is written on failure
// ─────────────────────────────────────────────────────────────
if ($isCreate && !empty($input['utrs']) && is_array($input['utrs'])) {
$seenUtrs = [];
foreach ($input['utrs'] as $utrRow) {
$utrNo = trim((string)($utrRow['utr_no'] ?? ''));
if ($utrNo === '') {
continue;
}
// 1. Duplicate within the same request
if (in_array($utrNo, $seenUtrs, true)) {
$this->db->transRollback();
return $this->respond([
'status' => 'failed',
'code' => 400,
'message' => "Duplicate UTR found: \"{$utrNo}\" appears more than once in your submission. Please remove the duplicate and try again.",
], 400);
}
$seenUtrs[] = $utrNo;
// 2. Already exists in DB (used in another payout)
$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 \"{$utrNo}\" is already linked to another payout. Please verify the UTR number or contact support if you believe this is an error.",
], 400);
}
}
}
// ─────────────────────────────────────────────────────────────
// Basic invoice data
$invoiceData = [
'invoice_amount' => $input['invoice_amount'] ?? 0,
@ -169,15 +220,12 @@ class InvoiceController extends ResourceController
'pos_id' => $posId,
'till_date' => $input['till_date'] ? date('Y-m-d', strtotime($input['till_date'])) : null,
'invoice_date' => $input['invoice_date'] ? date('Y-m-d', strtotime($input['invoice_date'])) : null,
'payout_status' => 1,
'payout_status' => $payoutStatus,
'is_active' => 1,
'updated_at' => date('Y-m-d H:i:s'),
'updated_by' => $input['updated_by'] ?? 0
];
$invoiceId = $input['id'] ?? null;
$isCreate = empty($invoiceId);
if (!$invoiceId) {
// Generate invoice number only for CREATE
@ -189,6 +237,7 @@ class InvoiceController extends ResourceController
$invoiceId = $this->InvoiceModel->insert($invoiceData);
if ($invoiceId === false) {
$this->db->transRollback();
log_message('error', json_encode($this->InvoiceModel->errors()));
return $this->respond(['status' => 'failed', 'message' => 'Failed to create invoice', 'error' => json_encode($this->InvoiceModel->errors())], 500);
}
@ -227,73 +276,43 @@ class InvoiceController extends ResourceController
$this->InvoiceItemModel->insert($itemData);
//update pos_id to policy table
$this->PolicyModel->update($item['policy_id'], ['pos_id' => $input['pos_id'] ]);
$this->PolicyModel->update($item['policy_id'], ['pos_id' => $posId ]);
}
}
}
/*
* UTR save (Tempa)
*/
// UTR save — validations already passed above, just insert
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);
// }
$utrDate = !empty($utrRow['utr_date'])
? date('Y-m-d', strtotime($utrRow['utr_date']))
: null;
$seenUtrs[] = $utrNo;
$amount = isset($utrRow['amount']) ? (float) $utrRow['amount'] : 0;
// $exists = $this->InvoiceUtrModel->where('utr_no', $utrNo)->where('is_active', 1)->first();
$result_utr = $this->InvoiceUtrModel->insert([
'invoice_id' => $invoiceId,
'utr_no' => $utrNo,
'utr_date' => $utrDate,
'amount' => $amount,
'is_active' => 1,
'created_at' => date('Y-m-d H:i:s'),
'created_by' => $input['created_by'] ?? 0,
]);
// 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 ($result_utr === false) {
$this->db->transRollback();
return $this->respond([
'status' => 'failed',
'code' => 500,
'message' => 'Could not save UTR details. Please try again.',
], 500);
}
}
}
@ -402,8 +421,8 @@ class InvoiceController extends ResourceController
->join('partner_invoice pi', 'pi.id = pii.invoice_id AND pi.is_active = 1', 'left')
->join('partner_agent pa', 'pa.id = pe.agent_id', 'left')
->where('partner_policy.is_active', 1)
->where('partner_policy.commission_amount >= 0')
// ->where('partner_policy.is_data_accuracy_checked', 1)
->where('partner_policy.commission_amount > 0')
->where('partner_policy.is_data_accuracy_checked', 1)
->where('partner_policy.manager_id', $input['manager_id']);
// Apply agent_id filter (supports multiple agents)