Merge branch 'main' of bitbucket.org:jubilian/nhance_partner_be
This commit is contained in:
commit
1c6a7539c8
@ -157,10 +157,61 @@ class InvoiceController extends ResourceController
|
|||||||
try {
|
try {
|
||||||
$input = $this->request->getJSON(true);
|
$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;
|
$posIdRaw = $input['pos_id'] ?? 0;
|
||||||
$posId = is_numeric($posIdRaw) ? (int)$posIdRaw : 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
|
// Basic invoice data
|
||||||
$invoiceData = [
|
$invoiceData = [
|
||||||
'invoice_amount' => $input['invoice_amount'] ?? 0,
|
'invoice_amount' => $input['invoice_amount'] ?? 0,
|
||||||
@ -169,15 +220,12 @@ class InvoiceController extends ResourceController
|
|||||||
'pos_id' => $posId,
|
'pos_id' => $posId,
|
||||||
'till_date' => $input['till_date'] ? date('Y-m-d', strtotime($input['till_date'])) : null,
|
'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,
|
'invoice_date' => $input['invoice_date'] ? date('Y-m-d', strtotime($input['invoice_date'])) : null,
|
||||||
'payout_status' => 1,
|
'payout_status' => $payoutStatus,
|
||||||
'is_active' => 1,
|
'is_active' => 1,
|
||||||
'updated_at' => date('Y-m-d H:i:s'),
|
'updated_at' => date('Y-m-d H:i:s'),
|
||||||
'updated_by' => $input['updated_by'] ?? 0
|
'updated_by' => $input['updated_by'] ?? 0
|
||||||
];
|
];
|
||||||
|
|
||||||
$invoiceId = $input['id'] ?? null;
|
|
||||||
$isCreate = empty($invoiceId);
|
|
||||||
|
|
||||||
if (!$invoiceId) {
|
if (!$invoiceId) {
|
||||||
|
|
||||||
// Generate invoice number only for CREATE
|
// Generate invoice number only for CREATE
|
||||||
@ -189,6 +237,7 @@ class InvoiceController extends ResourceController
|
|||||||
$invoiceId = $this->InvoiceModel->insert($invoiceData);
|
$invoiceId = $this->InvoiceModel->insert($invoiceData);
|
||||||
|
|
||||||
if ($invoiceId === false) {
|
if ($invoiceId === false) {
|
||||||
|
$this->db->transRollback();
|
||||||
log_message('error', json_encode($this->InvoiceModel->errors()));
|
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);
|
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);
|
$this->InvoiceItemModel->insert($itemData);
|
||||||
|
|
||||||
//update pos_id to policy table
|
//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 — validations already passed above, just insert
|
||||||
|
|
||||||
/*
|
|
||||||
* UTR save (Tempa)
|
|
||||||
*/
|
|
||||||
if ($isCreate && !empty($input['utrs']) && is_array($input['utrs'])) {
|
if ($isCreate && !empty($input['utrs']) && is_array($input['utrs'])) {
|
||||||
|
|
||||||
$seenUtrs = [];
|
|
||||||
|
|
||||||
foreach ($input['utrs'] as $utrRow) {
|
foreach ($input['utrs'] as $utrRow) {
|
||||||
|
|
||||||
$utrNo = trim((string)($utrRow['utr_no'] ?? ''));
|
$utrNo = trim((string)($utrRow['utr_no'] ?? ''));
|
||||||
|
|
||||||
|
|
||||||
if ($utrNo === '') continue;
|
if ($utrNo === '') continue;
|
||||||
|
|
||||||
|
$utrDate = !empty($utrRow['utr_date'])
|
||||||
|
? date('Y-m-d', strtotime($utrRow['utr_date']))
|
||||||
|
: null;
|
||||||
|
|
||||||
// if (in_array($utrNo, $seenUtrs, true)) {
|
$amount = isset($utrRow['amount']) ? (float) $utrRow['amount'] : 0;
|
||||||
// $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([
|
$result_utr = $this->InvoiceUtrModel->insert([
|
||||||
'invoice_id' => $invoiceId,
|
'invoice_id' => $invoiceId,
|
||||||
'utr_no' => $utrNo,
|
'utr_no' => $utrNo,
|
||||||
'utr_date' => null,
|
'utr_date' => $utrDate,
|
||||||
'amount' => 0,
|
'amount' => $amount,
|
||||||
'is_active' => 1,
|
'is_active' => 1,
|
||||||
'created_at' => date('Y-m-d H:i:s'),
|
'created_at' => date('Y-m-d H:i:s'),
|
||||||
'created_by' => $input['created_by'] ?? 0
|
'created_by' => $input['created_by'] ?? 0,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// if (!$result_utr) {
|
if ($result_utr === false) {
|
||||||
// print_r($this->InvoiceUtrModel->errors());
|
$this->db->transRollback();
|
||||||
// die;
|
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_invoice pi', 'pi.id = pii.invoice_id AND pi.is_active = 1', 'left')
|
||||||
->join('partner_agent pa', 'pa.id = pe.agent_id', 'left')
|
->join('partner_agent pa', 'pa.id = pe.agent_id', 'left')
|
||||||
->where('partner_policy.is_active', 1)
|
->where('partner_policy.is_active', 1)
|
||||||
->where('partner_policy.commission_amount >= 0')
|
->where('partner_policy.commission_amount > 0')
|
||||||
// ->where('partner_policy.is_data_accuracy_checked', 1)
|
->where('partner_policy.is_data_accuracy_checked', 1)
|
||||||
->where('partner_policy.manager_id', $input['manager_id']);
|
->where('partner_policy.manager_id', $input['manager_id']);
|
||||||
|
|
||||||
// Apply agent_id filter (supports multiple agents)
|
// Apply agent_id filter (supports multiple agents)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user