@@ -195,6 +195,8 @@ class PaymentController extends ResourceController
|
||||
'amount' => $this->request->getPost('amount'),
|
||||
'payment_method' => $this->request->getPost('payment_method'),
|
||||
'reference' => $this->request->getPost('reference'),
|
||||
'school_year' => $this->schoolYear,
|
||||
'update_by' => $this->loggedInUserId(),
|
||||
'created_at' => utc_now(),
|
||||
];
|
||||
|
||||
@@ -945,7 +947,7 @@ class PaymentController extends ResourceController
|
||||
try {
|
||||
// Lock invoice & get context (also ensure totals are up-to-date before validation)
|
||||
$row = $this->db->query(
|
||||
'SELECT id, parent_id, total_amount, school_year FROM invoices WHERE id = ? FOR UPDATE',
|
||||
'SELECT id, parent_id, invoice_number, total_amount, school_year FROM invoices WHERE id = ? FOR UPDATE',
|
||||
[$invoiceId]
|
||||
)->getRowArray();
|
||||
|
||||
@@ -1020,6 +1022,20 @@ class PaymentController extends ResourceController
|
||||
return redirect()->back()->with('error', 'Failed to record payment.');
|
||||
}
|
||||
|
||||
if (!$this->recordManualPayment(
|
||||
(string)($row['invoice_number'] ?? $invoiceId),
|
||||
$amount,
|
||||
$paymentMethod,
|
||||
$transactionId,
|
||||
$checkFile,
|
||||
$invYear,
|
||||
$paymentDate
|
||||
)) {
|
||||
$this->db->transRollback();
|
||||
log_message('error', '[manualPayUpdate] Failed to record manual payment audit row: ' . json_encode($this->manualPaymentModel->errors()));
|
||||
return redirect()->back()->with('error', 'Payment was not recorded because the manual payment audit row could not be saved.');
|
||||
}
|
||||
|
||||
// Ensure invoice totals/balance reflect discounts and this payment
|
||||
$ledger = $this->invoiceLedgerService->recalculateInvoice($invoiceId);
|
||||
|
||||
@@ -1086,6 +1102,35 @@ class PaymentController extends ResourceController
|
||||
}
|
||||
|
||||
|
||||
private function recordManualPayment(
|
||||
string $invoiceNumber,
|
||||
float $amount,
|
||||
string $paymentMethod,
|
||||
string $reference,
|
||||
?string $proofPath,
|
||||
string $schoolYear,
|
||||
string $createdAt
|
||||
): bool {
|
||||
return (bool) $this->manualPaymentModel->insert([
|
||||
'invoice_number' => $invoiceNumber,
|
||||
'amount' => $amount,
|
||||
'payment_method' => strtolower($paymentMethod),
|
||||
'reference' => $reference,
|
||||
'proof_path' => $proofPath,
|
||||
'school_year' => $schoolYear,
|
||||
'update_by' => $this->loggedInUserId(),
|
||||
'created_at' => $createdAt,
|
||||
]);
|
||||
}
|
||||
|
||||
private function loggedInUserId(): ?int
|
||||
{
|
||||
$userId = (int) (session()->get('user_id') ?? 0);
|
||||
|
||||
return $userId > 0 ? $userId : null;
|
||||
}
|
||||
|
||||
|
||||
public function manualPayEdit()
|
||||
{
|
||||
$paymentId = (int) $this->request->getPost('payment_id');
|
||||
|
||||
Reference in New Issue
Block a user