fix invoice amount not tally when service charge get refunded

This commit is contained in:
Jia Sheng
2025-03-08 15:41:15 +08:00
parent 1b1b9da51e
commit d3ac49c516
2 changed files with 47 additions and 5 deletions
@@ -0,0 +1,27 @@
<?php
namespace App\Classes\Modules\Bookings\Services;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\Booking;
class CalculatesBookingRefundServiceCharge
{
public function execute(Booking $booking, int $type, ?string $payment_reference = null): float
{
$refundAmounts = $booking->transactions()->payments()->complete()->get()->map(function ($payment) use ($type) {
return $this->calculateRefundAmount($payment, $type);
});
$totalRefundAmount = $refundAmounts->sum();
return $totalRefundAmount;
}
public function calculateRefundAmount($payment): float
{
$refundTransactions = $payment->transactions()->refunds()->whereIn('status', [ApprovalStatus::APPROVED]);
return $refundTransactions->sum('service_charge');
}
}
@@ -21,6 +21,7 @@
<?php
use App\Classes\Modules\Bookings\Services\CalculatesBookingRefundAmount;
use App\Classes\Modules\Bookings\Services\CalculatesBookingRefundServiceCharge;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
@@ -30,12 +31,13 @@
->where('status', ApprovalStatus::COMPLETED)
->get()
->sum(function ($transaction) {
return round($transaction->amount, 2);
return $transaction->amount;
});
$refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->calculateRefundAmount($booking, $booking->fix_currency_id);
$refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->execute($booking, 1);
$refundedServiceCharge = (App()->make(CalculatesBookingRefundServiceCharge::class))->execute($booking, 1);
$totalPayment = $paymentSum - $refundedAmount;
$totalPayment = $paymentSum - $refundedAmount - $refundedServiceCharge;
?>
@foreach ($po_order_transaction->transactionDetails as $key => $transaction_detail)
@@ -78,13 +80,26 @@
// if has payment, calculate and deduct the refund
if ($totalPayment) {
$refundedServiceCharge = $subtotal + $serviceCharge - $totalPayment;
$serviceCharge = $serviceCharge - $refundedServiceCharge - $voucherDiscount;
$serviceCharge = $serviceCharge - $voucherDiscount;
}
?>
<td class="right">{{ number_format($serviceCharge, 2) }}</td>
</tr>
@if($totalPayment && $refundedServiceCharge)
<tr class="billingcharges">
<td colspan="4"></td>
<td class="right">Refunded Service Charges</td>
<?php
$serviceCharge = $transaction->service_charge;
// if has payment, calculate and deduct the refund
if ($totalPayment) {
$serviceCharge = $serviceCharge - $refundedServiceCharge;
}
?>
<td class="right">-{{ number_format($refundedServiceCharge, 2) }}</td>
</tr>
@endif
@if($voucher_redemption)
<tr class="voucher">
<td colspan="4"></td>