From d3ac49c5168ca4149fe2a8e9e4cefce810ad34f2 Mon Sep 17 00:00:00 2001 From: Jia Sheng Date: Sat, 8 Mar 2025 15:41:15 +0800 Subject: [PATCH 1/3] fix invoice amount not tally when service charge get refunded --- .../CalculatesBookingRefundServiceCharge.php | 27 +++++++++++++++++++ .../pages/pdfs/purchase_order_table.blade.php | 25 +++++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 app/Classes/Modules/Bookings/Services/CalculatesBookingRefundServiceCharge.php 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..f9a1d222 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 @@ 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; } ?> {{ number_format($serviceCharge, 2) }} + @if($totalPayment && $refundedServiceCharge) + + + Refunded Service Charges + service_charge; + // if has payment, calculate and deduct the refund + if ($totalPayment) { + $serviceCharge = $serviceCharge - $refundedServiceCharge; + } + ?> + -{{ number_format($refundedServiceCharge, 2) }} + + @endif @if($voucher_redemption) From 57999faf76088252865ba7b33fdf29a0fa36d68c Mon Sep 17 00:00:00 2001 From: Jia Sheng Date: Sun, 9 Mar 2025 11:23:36 +0800 Subject: [PATCH 2/3] fix for multiple payments and discount order --- .../pages/pdfs/purchase_order_table.blade.php | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/resources/views/pages/pdfs/purchase_order_table.blade.php b/resources/views/pages/pdfs/purchase_order_table.blade.php index f9a1d222..8c558b04 100644 --- a/resources/views/pages/pdfs/purchase_order_table.blade.php +++ b/resources/views/pages/pdfs/purchase_order_table.blade.php @@ -31,9 +31,20 @@ ->where('status', ApprovalStatus::COMPLETED) ->get() ->sum(function ($transaction) { - return $transaction->amount; + return round($transaction->amount, 2); }); + $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(); + $refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->execute($booking, 1); $refundedServiceCharge = (App()->make(CalculatesBookingRefundServiceCharge::class))->execute($booking, 1); @@ -42,7 +53,7 @@ @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); @@ -76,12 +87,13 @@ Service Charges service_charge; - - // if has payment, calculate and deduct the refund - if ($totalPayment) { - $serviceCharge = $serviceCharge - $voucherDiscount; - } + $serviceCharge = $booking->transactions() + ->where('type', TransactionType::PAYMENT) + ->where('status', ApprovalStatus::COMPLETED) + ->get() + ->sum(function ($transaction) { + return $transaction->service_charge; + }); ?> {{ number_format($serviceCharge, 2) }} @@ -90,8 +102,6 @@ Refunded Service Charges service_charge; - // if has payment, calculate and deduct the refund if ($totalPayment) { $serviceCharge = $serviceCharge - $refundedServiceCharge; @@ -134,6 +144,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 From e31f708edd9be02bfdc32dfa604e30e0cf0d35ae Mon Sep 17 00:00:00 2001 From: edmondlang Date: Tue, 11 Mar 2025 16:32:14 +0800 Subject: [PATCH 3/3] fix Proforma Invoice --- .../pages/pdfs/purchase_order_table.blade.php | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/resources/views/pages/pdfs/purchase_order_table.blade.php b/resources/views/pages/pdfs/purchase_order_table.blade.php index 8c558b04..542f1144 100644 --- a/resources/views/pages/pdfs/purchase_order_table.blade.php +++ b/resources/views/pages/pdfs/purchase_order_table.blade.php @@ -34,21 +34,25 @@ return round($transaction->amount, 2); }); - $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 = 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(); - $refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->execute($booking, 1); - $refundedServiceCharge = (App()->make(CalculatesBookingRefundServiceCharge::class))->execute($booking, 1); + $refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->execute($booking, 1); + $refundedServiceCharge = (App()->make(CalculatesBookingRefundServiceCharge::class))->execute($booking, 1); - $totalPayment = $paymentSum - $refundedAmount - $refundedServiceCharge; + $totalPayment = $paymentSum - $refundedAmount - $refundedServiceCharge; + } ?> @foreach ($po_order_transaction->transactionDetails as $key => $transaction_detail) @@ -87,13 +91,18 @@ Service Charges transactions() - ->where('type', TransactionType::PAYMENT) - ->where('status', ApprovalStatus::COMPLETED) - ->get() - ->sum(function ($transaction) { - return $transaction->service_charge; - }); + 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) }}