@@ -16,6 +16,7 @@ use App\Models\ParentModel;
|
||||
use App\Models\PaymentModel;
|
||||
use App\Models\CalendarModel;
|
||||
use App\Libraries\FinancialStatus;
|
||||
use App\Libraries\InvoiceLedgerService;
|
||||
use App\Services\EmailService;
|
||||
use Config\Database;
|
||||
use App\Controllers\View\InvoiceController;
|
||||
@@ -43,6 +44,7 @@ class EventController extends ResourceController
|
||||
protected $semester;
|
||||
protected $categories;
|
||||
protected $enrollmentModel;
|
||||
protected $invoiceLedgerService;
|
||||
private ?bool $eventChargesHasCreatedBy = null;
|
||||
private ?bool $eventChargesHasWaiverSigned = null;
|
||||
|
||||
@@ -62,6 +64,7 @@ class EventController extends ResourceController
|
||||
$this->enrollmentModel = new EnrollmentModel();
|
||||
$this->parentModel = new ParentModel();
|
||||
$this->emailService = new EmailService();
|
||||
$this->invoiceLedgerService = new InvoiceLedgerService();
|
||||
|
||||
$this->schoolYear = $this->currentSchoolYearName();
|
||||
$this->semester = $this->configModel->getConfig('semester');
|
||||
@@ -1050,77 +1053,11 @@ class EventController extends ResourceController
|
||||
return;
|
||||
}
|
||||
|
||||
$invoice = $this->invoiceModel->find($invoiceId);
|
||||
if (!$invoice) {
|
||||
return;
|
||||
}
|
||||
|
||||
$db = \Config\Database::connect();
|
||||
$exclude = ['void', 'voided', 'refunded', 'failed', 'chargeback', 'declined', 'reversed', 'canceled', 'cancelled'];
|
||||
|
||||
$paidSum = 0.0;
|
||||
try {
|
||||
$qb = $db->table('payments')
|
||||
->select('COALESCE(SUM(paid_amount),0) AS tot')
|
||||
->where('invoice_id', $invoiceId)
|
||||
->where('paid_amount >', 0);
|
||||
|
||||
if ($db->fieldExists('status', 'payments')) {
|
||||
$qb->groupStart()
|
||||
->whereNotIn('status', $exclude)
|
||||
->orWhere('status IS NULL', null, false)
|
||||
->groupEnd();
|
||||
}
|
||||
if ($db->fieldExists('is_void', 'payments')) {
|
||||
$qb->groupStart()
|
||||
->where('is_void', 0)
|
||||
->orWhere('is_void IS NULL', null, false)
|
||||
->groupEnd();
|
||||
}
|
||||
|
||||
$row = $qb->get()->getRowArray();
|
||||
$paidSum = (float)($row['tot'] ?? 0.0);
|
||||
$this->invoiceLedgerService->recalculateInvoice($invoiceId);
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Failed to sum payments for invoice ' . $invoiceId . ': ' . $e->getMessage());
|
||||
log_message('error', 'Failed to recalculate invoice ledger for invoice ' . $invoiceId . ': ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$discountSum = 0.0;
|
||||
try {
|
||||
$row = $db->table('discount_usages')
|
||||
->select('COALESCE(SUM(discount_amount),0) AS tot')
|
||||
->where('invoice_id', $invoiceId)
|
||||
->get()
|
||||
->getRowArray();
|
||||
$discountSum = (float)($row['tot'] ?? 0.0);
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Failed to sum discounts for invoice ' . $invoiceId . ': ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$refundSum = 0.0;
|
||||
try {
|
||||
$row = $db->table('refunds')
|
||||
->select('COALESCE(SUM(refund_paid_amount),0) AS tot')
|
||||
->where('invoice_id', $invoiceId)
|
||||
->whereIn('status', ['Partial', 'Paid'])
|
||||
->get()
|
||||
->getRowArray();
|
||||
$refundSum = (float)($row['tot'] ?? 0.0);
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Failed to sum refunds for invoice ' . $invoiceId . ': ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$total = (float)($invoice['total_amount'] ?? 0.0);
|
||||
$newBalance = round($total - $discountSum - $refundSum - $paidSum, 2);
|
||||
$newStatus = ($newBalance <= 0.00001)
|
||||
? 'Paid'
|
||||
: (($paidSum > 0) ? 'Partially Paid' : 'Unpaid');
|
||||
|
||||
$this->invoiceModel->update($invoiceId, [
|
||||
'paid_amount' => $paidSum,
|
||||
'balance' => $newBalance,
|
||||
'status' => $newStatus,
|
||||
'updated_at' => utc_now(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function applyEventPaymentStatus(int $chargeId, bool $isPaid): ?array
|
||||
@@ -1171,9 +1108,7 @@ class EventController extends ResourceController
|
||||
$eventAmount,
|
||||
$paymentSchoolYear,
|
||||
$paymentSemester,
|
||||
(float)($invoice['total_amount'] ?? 0.0),
|
||||
(float)($invoice['paid_amount'] ?? 0.0),
|
||||
(float)($invoice['balance'] ?? 0.0)
|
||||
(float)($invoice['total_amount'] ?? 0.0)
|
||||
) ?? 0);
|
||||
} elseif (!$isPaid && $paymentId > 0) {
|
||||
$this->voidPayment($paymentId, 'Event charge marked unpaid.');
|
||||
@@ -1250,16 +1185,13 @@ class EventController extends ResourceController
|
||||
float $amount,
|
||||
string $schoolYear,
|
||||
?string $semester,
|
||||
float $invoiceTotal = 0.0,
|
||||
float $invoicePaid = 0.0,
|
||||
float $invoiceBalance = 0.0
|
||||
float $invoiceTotal = 0.0
|
||||
): ?int
|
||||
{
|
||||
if ($amount <= 0 || $invoiceId <= 0 || $parentId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$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)
|
||||
@@ -1272,7 +1204,7 @@ class EventController extends ResourceController
|
||||
'invoice_id' => $invoiceId,
|
||||
'total_amount' => $invoiceTotal > 0 ? $invoiceTotal : $amount,
|
||||
'paid_amount' => $amount,
|
||||
'balance' => $newBalance,
|
||||
'balance' => null,
|
||||
'number_of_installments' => $installmentSeq,
|
||||
'installment_seq' => $installmentSeq,
|
||||
'payment_method' => 'cash',
|
||||
@@ -1347,13 +1279,9 @@ class EventController extends ResourceController
|
||||
->findAll();
|
||||
|
||||
foreach ($invoices as $invoice) {
|
||||
$status = strtolower(trim($invoice['status'] ?? ''));
|
||||
$balance = (float)($invoice['balance'] ?? 0.0);
|
||||
|
||||
if ($balance <= 0.00001 && $status !== 'paid') {
|
||||
$this->invoiceModel->update($invoice['id'], ['status' => 'Paid']);
|
||||
} elseif ($status === 'paid' && $balance > 0) {
|
||||
$this->invoiceModel->update($invoice['id'], ['status' => 'Unpaid']);
|
||||
$invoiceId = (int)($invoice['id'] ?? 0);
|
||||
if ($invoiceId > 0) {
|
||||
$this->invoiceLedgerService->recalculateInvoice($invoiceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user