fix parent pages and teachers and admin
API CI/CD / Validate (composer + pint) (push) Successful in 3m8s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Test (PHPUnit) (push) Failing after 6m5s
API CI/CD / Security audit (push) Failing after 52s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
API CI/CD / Validate (composer + pint) (push) Successful in 3m8s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Test (PHPUnit) (push) Failing after 6m5s
API CI/CD / Security audit (push) Failing after 52s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
This commit is contained in:
@@ -35,8 +35,49 @@ class InvoicePaymentService
|
||||
}
|
||||
|
||||
$invoiceIds = array_column($invoices, 'id');
|
||||
$payments = [];
|
||||
$discounts = [];
|
||||
$refunds = [];
|
||||
if (! empty($invoiceIds)) {
|
||||
$paymentTotals = DB::table('payments')
|
||||
->selectRaw('invoice_id, COALESCE(SUM(paid_amount),0) as paid_amount')
|
||||
->whereIn('invoice_id', $invoiceIds)
|
||||
->where(function ($query) {
|
||||
$query->whereNotIn(DB::raw('LOWER(status)'), [
|
||||
'pending',
|
||||
'void',
|
||||
'voided',
|
||||
'cancelled',
|
||||
'canceled',
|
||||
'failed',
|
||||
'rejected',
|
||||
'refunded',
|
||||
'chargeback',
|
||||
'declined',
|
||||
'reversed',
|
||||
])->orWhereNull('status');
|
||||
})
|
||||
->groupBy('invoice_id')
|
||||
->get()
|
||||
->map(fn ($r) => (array) $r)
|
||||
->all();
|
||||
|
||||
foreach ($paymentTotals as $row) {
|
||||
$payments[$row['invoice_id']] = (float) ($row['paid_amount'] ?? 0);
|
||||
}
|
||||
|
||||
$discountResults = DB::table('discount_usages')
|
||||
->selectRaw('invoice_id, COALESCE(SUM(discount_amount),0) as discount_amount')
|
||||
->whereIn('invoice_id', $invoiceIds)
|
||||
->groupBy('invoice_id')
|
||||
->get()
|
||||
->map(fn ($r) => (array) $r)
|
||||
->all();
|
||||
|
||||
foreach ($discountResults as $row) {
|
||||
$discounts[$row['invoice_id']] = (float) ($row['discount_amount'] ?? 0);
|
||||
}
|
||||
|
||||
$refundResults = DB::table('refunds')
|
||||
->selectRaw('invoice_id, COALESCE(SUM(refund_paid_amount),0) as refund_paid_amount')
|
||||
->whereIn('invoice_id', $invoiceIds)
|
||||
@@ -63,9 +104,20 @@ class InvoicePaymentService
|
||||
}
|
||||
|
||||
foreach ($invoices as &$invoice) {
|
||||
$invoice['refund_amount'] = $refunds[$invoice['id']] ?? 0.0;
|
||||
$invoice['last_paid_amount'] = $lastPayments[$invoice['id']]['last_paid_amount'] ?? 0.0;
|
||||
$invoice['last_payment_date'] = $lastPayments[$invoice['id']]['last_payment_date'] ?? null;
|
||||
$invoiceId = (int) ($invoice['id'] ?? 0);
|
||||
$totalAmount = round((float) ($invoice['total_amount'] ?? 0), 2);
|
||||
$paidAmount = round((float) ($payments[$invoiceId] ?? $invoice['paid_amount'] ?? 0), 2);
|
||||
$discountAmount = round((float) ($discounts[$invoiceId] ?? $invoice['discount'] ?? 0), 2);
|
||||
$refundAmount = round((float) ($refunds[$invoiceId] ?? 0), 2);
|
||||
$balance = max(0.0, round($totalAmount - $paidAmount - $discountAmount - $refundAmount, 2));
|
||||
|
||||
$invoice['paid_amount'] = $paidAmount;
|
||||
$invoice['discount'] = $discountAmount;
|
||||
$invoice['refund_amount'] = $refundAmount;
|
||||
$invoice['balance'] = $balance;
|
||||
$invoice['status'] = $this->deriveInvoiceStatus($invoice['status'] ?? null, $paidAmount, $balance);
|
||||
$invoice['last_paid_amount'] = $lastPayments[$invoiceId]['last_paid_amount'] ?? 0.0;
|
||||
$invoice['last_payment_date'] = $lastPayments[$invoiceId]['last_payment_date'] ?? null;
|
||||
}
|
||||
unset($invoice);
|
||||
|
||||
@@ -77,4 +129,17 @@ class InvoicePaymentService
|
||||
'dueDate' => $this->config->getDueDate(),
|
||||
];
|
||||
}
|
||||
|
||||
private function deriveInvoiceStatus(?string $storedStatus, float $paidAmount, float $balance): string
|
||||
{
|
||||
if ($balance <= 0.00001) {
|
||||
return 'Paid';
|
||||
}
|
||||
|
||||
if ($paidAmount > 0) {
|
||||
return 'Partially Paid';
|
||||
}
|
||||
|
||||
return $storedStatus ?: 'Unpaid';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user