Merge branch 'vapor/production' of gitlab.com:CIEFWorldwideSdnBhd/exchange-2.0 into vapor/staging

This commit is contained in:
edmondlang
2025-03-11 16:32:31 +08:00
2 changed files with 77 additions and 10 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;
@@ -33,14 +34,30 @@
return round($transaction->amount, 2);
});
$refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->calculateRefundAmount($booking, $booking->fix_currency_id);
$totalPayment = 0;
$average_currency_rate = $transaction->currency_rate;
if ($paymentSum){
$average_currency_rate = $booking->transactions()
->where('type', TransactionType::PAYMENT)
->where('status', ApprovalStatus::COMPLETED)
->get()
->sum(function ($transaction) {
return $transaction->currency_rate;
}) / $booking->transactions()
->where('type', TransactionType::PAYMENT)
->where('status', ApprovalStatus::COMPLETED)
->count();
$totalPayment = $paymentSum - $refundedAmount;
$refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->execute($booking, 1);
$refundedServiceCharge = (App()->make(CalculatesBookingRefundServiceCharge::class))->execute($booking, 1);
$totalPayment = $paymentSum - $refundedAmount - $refundedServiceCharge;
}
?>
@foreach ($po_order_transaction->transactionDetails as $key => $transaction_detail)
@php
$exactUnitPrice = ($currency_id) === 1 ? $transaction_detail->price : bcdiv($transaction_detail->price, $transaction->currency_rate, 7);
$exactUnitPrice = ($currency_id) === 1 ? $transaction_detail->price : bcdiv($transaction_detail->price, $average_currency_rate, 7);
$displayUnitPrice = round($exactUnitPrice, 2);
$itemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 5);
$displayedItemTotal = round(bcmul($displayUnitPrice, $transaction_detail->quantity, 7), 2);
@@ -74,17 +91,34 @@
<td colspan="4"></td>
<td class="right">Service Charges</td>
<?php
$serviceCharge = $transaction->service_charge;
// if has payment, calculate and deduct the refund
if ($totalPayment) {
$refundedServiceCharge = $subtotal + $serviceCharge - $totalPayment;
$serviceCharge = $serviceCharge - $refundedServiceCharge - $voucherDiscount;
if (!$totalPayment) {
$serviceCharge = $transaction->service_charge;
}
else {
$serviceCharge = $booking->transactions()
->where('type', TransactionType::PAYMENT)
->where('status', ApprovalStatus::COMPLETED)
->get()
->sum(function ($transaction) {
return $transaction->service_charge;
});
}
?>
<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
// 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>
@@ -119,6 +153,12 @@
$expectedTotal = bcadd(bcadd(bcadd($subtotal, $serviceCharge, 5), $transaction->tax, 5), $voucherDiscount, 5);
$discrepancy = bcsub($expectedTotal, $displayedTotal, 5);
$total = bcadd(bcadd(bcadd($subtotal, $serviceCharge, 5), $transaction->tax, 5), $voucherDiscount, 5);
if ($totalPayment) {
$expectedTotal = $totalPayment;
$discrepancy = bcsub($expectedTotal, $displayedTotal, 5);
$total = $totalPayment;
}
@endphp
<tr>
<td colspan="4"></td>