fix payment issues
Tests / PHPUnit (push) Successful in 1m17s

This commit is contained in:
root
2026-07-18 19:12:13 -04:00
parent 4aac9472af
commit 068e739408
8 changed files with 1204 additions and 40 deletions
+37 -16
View File
@@ -15,6 +15,7 @@ use App\Models\ClassSectionModel;
use App\Models\ParentModel;
use App\Models\PaymentModel;
use App\Models\CalendarModel;
use App\Libraries\FinancialStatus;
use App\Services\EmailService;
use Config\Database;
use App\Controllers\View\InvoiceController;
@@ -1175,7 +1176,7 @@ class EventController extends ResourceController
(float)($invoice['balance'] ?? 0.0)
) ?? 0);
} elseif (!$isPaid && $paymentId > 0) {
$this->paymentModel->delete($paymentId);
$this->voidPayment($paymentId, 'Event charge marked unpaid.');
$paymentId = 0;
}
@@ -1258,20 +1259,13 @@ class EventController extends ResourceController
return null;
}
$monthSemester = $semester ?: $this->semester;
$newPaid = (float)$invoicePaid + $amount;
$newBalance = (float)$invoiceBalance - $amount;
$paymentStatus = ($newBalance <= 0.00001)
? 'Paid'
: (($newPaid > 0) ? 'Partially Paid' : 'Unpaid');
$exclude = ['void', 'voided', 'refunded', 'failed', 'chargeback', 'declined', 'reversed', 'canceled', 'cancelled'];
$priorCount = $this->paymentModel
$newBalance = max(0.0, round((float)$invoiceBalance - $amount, 2));
$row = $this->paymentModel->db->table('payments')
->select('COALESCE(MAX(installment_seq), 0) + 1 AS next_seq', false)
->where('invoice_id', $invoiceId)
->where('paid_amount >', 0)
->whereNotIn('status', $exclude)
->countAllResults();
$installmentSeq = $priorCount + 1;
->get()
->getRowArray();
$installmentSeq = (int)($row['next_seq'] ?? 1);
$data = [
'parent_id' => $parentId,
@@ -1280,11 +1274,11 @@ class EventController extends ResourceController
'paid_amount' => $amount,
'balance' => $newBalance,
'number_of_installments' => $installmentSeq,
'installment_seq' => $installmentSeq,
'payment_method' => 'cash',
'payment_date' => utc_now(),
'school_year' => $schoolYear,
'semester' => $monthSemester,
'status' => $paymentStatus,
'status' => FinancialStatus::PAYMENT_RECORDED,
'transaction_id' => $this->paymentModel->generateNewTransactionId(),
'updated_by' => session()->get('user_id'),
];
@@ -1297,6 +1291,33 @@ class EventController extends ResourceController
return (int)$this->paymentModel->getInsertID();
}
private function voidPayment(int $paymentId, string $reason): bool
{
if ($paymentId <= 0) {
return false;
}
$data = [
'status' => FinancialStatus::PAYMENT_VOIDED,
'updated_by' => session()->get('user_id'),
];
if ($this->paymentModel->db->fieldExists('is_void', 'payments')) {
$data['is_void'] = 1;
}
if ($this->paymentModel->db->fieldExists('voided_at', 'payments')) {
$data['voided_at'] = utc_now();
}
if ($this->paymentModel->db->fieldExists('voided_by', 'payments')) {
$data['voided_by'] = session()->get('user_id');
}
if ($this->paymentModel->db->fieldExists('void_reason', 'payments')) {
$data['void_reason'] = $reason;
}
return (bool) $this->paymentModel->update($paymentId, $data);
}
private function normalizeExternalName(?string $value): string
{
$raw = trim((string)$value);