invoice no : GWM

This commit is contained in:
Gowtham M 2025-12-19 09:41:41 +05:30
parent c75e855836
commit e18c55e38d

View File

@ -99,7 +99,6 @@ class InvoiceController extends ResourceController
// Basic invoice data // Basic invoice data
$invoiceData = [ $invoiceData = [
'invoice_no' => $input['invoice_no'] ?? '',
'invoice_amount' => $input['invoice_amount'] ?? 0, 'invoice_amount' => $input['invoice_amount'] ?? 0,
'broker_id' => $input['broker_id'] ?? 0, 'broker_id' => $input['broker_id'] ?? 0,
'pos_id' => $input['pos_id'] ?? 0, 'pos_id' => $input['pos_id'] ?? 0,
@ -115,6 +114,9 @@ class InvoiceController extends ResourceController
if (!$invoiceId) { if (!$invoiceId) {
// Generate invoice number only for CREATE
$invoiceData['invoice_no'] = $this->generateInvoiceNo();
$invoiceData['created_at'] = date('Y-m-d H:i:s'); $invoiceData['created_at'] = date('Y-m-d H:i:s');
$invoiceData['created_by'] = $input['created_by'] ?? 0; $invoiceData['created_by'] = $input['created_by'] ?? 0;
@ -123,6 +125,10 @@ class InvoiceController extends ResourceController
if (!$invoiceId) { throw new DataException("Failed to create invoice"); } if (!$invoiceId) { throw new DataException("Failed to create invoice"); }
} else { } else {
// Invoice number should NOT change on update
unset($invoiceData['invoice_no']);
$this->InvoiceModel->update($invoiceId, $invoiceData); $this->InvoiceModel->update($invoiceId, $invoiceData);
} }
@ -175,6 +181,30 @@ class InvoiceController extends ResourceController
} }
} }
private function generateInvoiceNo()
{
$year = date('Y');
$prefix = "INV/$year/";
// Get last invoice of current year
$lastInvoice = $this->InvoiceModel
->select('invoice_no')
->like('invoice_no', $prefix, 'after')
->orderBy('id', 'DESC')
->first();
if ($lastInvoice && isset($lastInvoice['invoice_no'])) {
// Extract numeric part
$lastNumber = intval(substr($lastInvoice['invoice_no'], -5));
$nextNumber = $lastNumber + 1;
} else {
$nextNumber = 1;
}
return $prefix . str_pad($nextNumber, 5, '0', STR_PAD_LEFT);
}
public function deleteInvoice() public function deleteInvoice()
{ {
try { try {