add refund logic and fix books inventory logic
This commit is contained in:
@@ -38,7 +38,7 @@ class RefundEligibilityService
|
||||
$completedPayoutCents = $this->completedPayoutsAffectAvailability($sourceType)
|
||||
? $this->calculateCompletedPayoutCents($sourceType, $sourceId, $excludeRefundId)
|
||||
: 0;
|
||||
$reservedAmountCents = $this->calculateReservedAmountCents($sourceType, $sourceId, $excludeRefundId);
|
||||
$reservedAmountCents = $this->calculateReservedAmountCents($sourceType, $sourceId, $excludeRefundId, $invoiceId);
|
||||
$availableAmountCents = max(0, $sourceCreditCents - $completedPayoutCents - $reservedAmountCents);
|
||||
|
||||
$reasons = [];
|
||||
@@ -164,7 +164,7 @@ class RefundEligibilityService
|
||||
{
|
||||
return match ($sourceType) {
|
||||
'invoice_overpayment' => $this->invoiceCreditCents($parentId, $invoiceId ?: $sourceId),
|
||||
'tuition_withdrawal' => $this->invoicePaidCents($parentId, $invoiceId ?: $sourceId),
|
||||
'tuition_withdrawal' => $this->invoiceCreditCents($parentId, $invoiceId ?: $sourceId),
|
||||
'payment_duplicate', 'payment_correction' => $this->paymentCreditCents($parentId, $invoiceId, $sourceId),
|
||||
'credit_memo', 'administrative_credit' => 0,
|
||||
default => 0,
|
||||
@@ -173,7 +173,7 @@ class RefundEligibilityService
|
||||
|
||||
protected function completedPayoutsAffectAvailability(string $sourceType): bool
|
||||
{
|
||||
return $sourceType !== 'invoice_overpayment';
|
||||
return ! in_array($sourceType, ['invoice_overpayment', 'tuition_withdrawal'], true);
|
||||
}
|
||||
|
||||
protected function invoicePaidCents(int $parentId, int $invoiceId): int
|
||||
@@ -286,13 +286,21 @@ class RefundEligibilityService
|
||||
return (int)round(((float)($row['total_paid'] ?? 0)) * 100);
|
||||
}
|
||||
|
||||
protected function calculateReservedAmountCents(string $sourceType, int $sourceId, ?int $excludeRefundId): int
|
||||
protected function calculateReservedAmountCents(string $sourceType, int $sourceId, ?int $excludeRefundId, ?int $invoiceId = null): int
|
||||
{
|
||||
$query = $this->refundModel
|
||||
->select('id, refund_amount, refund_paid_amount, approved_amount_cents')
|
||||
->where('source_type', $sourceType)
|
||||
->where('source_id', $sourceId)
|
||||
->whereIn('status', ['Approved', 'Partial', 'approved', 'partially_paid']);
|
||||
->select('id, refund_amount, requested_amount_cents, refund_paid_amount, approved_amount_cents');
|
||||
|
||||
if (in_array($sourceType, ['invoice_overpayment', 'tuition_withdrawal'], true)) {
|
||||
$invoiceSourceId = $invoiceId !== null && $invoiceId > 0 ? $invoiceId : $sourceId;
|
||||
$query->where('invoice_id', $invoiceSourceId)
|
||||
->whereIn('source_type', ['invoice_overpayment', 'tuition_withdrawal']);
|
||||
} else {
|
||||
$query->where('source_type', $sourceType)
|
||||
->where('source_id', $sourceId);
|
||||
}
|
||||
|
||||
$query->whereIn('status', ['Pending', 'requested', 'Approved', 'Partial', 'pending', 'approved', 'partially_paid']);
|
||||
|
||||
if ($excludeRefundId !== null && $excludeRefundId > 0) {
|
||||
$query->where('id !=', $excludeRefundId);
|
||||
@@ -302,7 +310,7 @@ class RefundEligibilityService
|
||||
foreach ($query->findAll() as $refund) {
|
||||
$approved = isset($refund['approved_amount_cents']) && $refund['approved_amount_cents'] !== null
|
||||
? (int)$refund['approved_amount_cents']
|
||||
: (int)round(((float)($refund['refund_amount'] ?? 0)) * 100);
|
||||
: ((int)($refund['requested_amount_cents'] ?? 0) ?: (int)round(((float)($refund['refund_amount'] ?? 0)) * 100));
|
||||
$paid = $this->getCompletedPayoutTotalCentsForRefund((int)($refund['id'] ?? 0));
|
||||
$reserved += max(0, $approved - $paid);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user