diff --git a/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundServiceCharge.php b/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundServiceCharge.php new file mode 100644 index 00000000..25cd44c1 --- /dev/null +++ b/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundServiceCharge.php @@ -0,0 +1,27 @@ +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'); + } +} diff --git a/resources/views/pages/pdfs/purchase_order_table.blade.php b/resources/views/pages/pdfs/purchase_order_table.blade.php index 5f62205f..542f1144 100644 --- a/resources/views/pages/pdfs/purchase_order_table.blade.php +++ b/resources/views/pages/pdfs/purchase_order_table.blade.php @@ -21,6 +21,7 @@ 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 @@ Service Charges 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; + }); } ?> {{ number_format($serviceCharge, 2) }} - + @if($totalPayment && $refundedServiceCharge) + + + Refunded Service Charges + + -{{ number_format($refundedServiceCharge, 2) }} + + @endif @if($voucher_redemption) @@ -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