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