From f241ddf20b9048ab9004f9bcbb384d1f955a48e6 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Wed, 8 Oct 2025 14:21:08 +0800 Subject: [PATCH] E-Invoice - Fix issue feedback by PM on 20251006 on refund cases --- .../CreateInvoiceDocumentProcessor.php | 3 + .../elements/PaymentHistoryComponent.vue | 4 +- .../views/pages/pdfs/deliver_order.blade.php | 6 +- .../views/pages/pdfs/purchase_order.blade.php | 6 +- .../pdfs/purchase_order_table_v2.blade.php | 202 ++++++++++++++++++ 5 files changed, 217 insertions(+), 4 deletions(-) create mode 100644 resources/views/pages/pdfs/purchase_order_table_v2.blade.php diff --git a/app/Classes/Modules/Transactions/Processors/CreateInvoiceDocumentProcessor.php b/app/Classes/Modules/Transactions/Processors/CreateInvoiceDocumentProcessor.php index b063ac93..7dcda460 100644 --- a/app/Classes/Modules/Transactions/Processors/CreateInvoiceDocumentProcessor.php +++ b/app/Classes/Modules/Transactions/Processors/CreateInvoiceDocumentProcessor.php @@ -57,6 +57,7 @@ class CreateInvoiceDocumentProcessor $eInvoiceStartDate = Carbon::parse(env('E_INVOICE_START_DATE', '2025-07-01 00:00:00')); $autoCountInvoiceId = ''; $autoCountEInvoiceValidationLink = 'CIEF'; + $eInvoiceStartedForBooking = false; if ($booking) { $bookingCreatedDate = Carbon::parse($booking->created_at); @@ -69,6 +70,7 @@ class CreateInvoiceDocumentProcessor } $documentDate = $lastPaymentTransaction->created_at; + $eInvoiceStartedForBooking = true; } if($document_type === DocumentType::EINVOICE){ @@ -140,6 +142,7 @@ class CreateInvoiceDocumentProcessor 'booking' => $booking, 'autocountId' => $autoCountInvoiceId, 'autocountEInvoiceValidationLink' => $autoCountEInvoiceValidationLink, + 'e_invoice_started_for_booking' => $eInvoiceStartedForBooking, ]); if($purchaseOrder && $purchaseOrder->booking->service_id === 4) { diff --git a/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue b/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue index dadbbdd0..3d582f0d 100644 --- a/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue +++ b/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue @@ -87,7 +87,7 @@
Payment Amount
- {{item.original_currency.short_code}} {{(Math.round((item.original_amount - item.refunded_amount + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}} + {{item.original_currency.short_code}} {{(item.original_amount).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
@@ -161,7 +161,7 @@
Recipient Gets
-
{{item.original_currency.short_code}} {{(item.original_amount).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
+
{{item.original_currency.short_code}} {{(Math.round((item.original_amount - item.refunded_amount + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
diff --git a/resources/views/pages/pdfs/deliver_order.blade.php b/resources/views/pages/pdfs/deliver_order.blade.php index f06e4174..5faa5819 100644 --- a/resources/views/pages/pdfs/deliver_order.blade.php +++ b/resources/views/pages/pdfs/deliver_order.blade.php @@ -69,7 +69,11 @@

- @include('pages.pdfs.purchase_order_table') + @if($e_invoice_started_for_booking) + @include('pages.pdfs.purchase_order_table_v2') + @else + @include('pages.pdfs.purchase_order_table') + @endif diff --git a/resources/views/pages/pdfs/purchase_order.blade.php b/resources/views/pages/pdfs/purchase_order.blade.php index 1f6d72d9..1a484332 100644 --- a/resources/views/pages/pdfs/purchase_order.blade.php +++ b/resources/views/pages/pdfs/purchase_order.blade.php @@ -76,7 +76,11 @@
- @include('pages.pdfs.purchase_order_table') + @if($e_invoice_started_for_booking) + @include('pages.pdfs.purchase_order_table_v2') + @else + @include('pages.pdfs.purchase_order_table') + @endif
diff --git a/resources/views/pages/pdfs/purchase_order_table_v2.blade.php b/resources/views/pages/pdfs/purchase_order_table_v2.blade.php new file mode 100644 index 00000000..1f349082 --- /dev/null +++ b/resources/views/pages/pdfs/purchase_order_table_v2.blade.php @@ -0,0 +1,202 @@ +
+ + + + + + + + + + + + + @php + $subtotal = "0"; + $voucherDiscount = $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0"; + $displayedSubtotal = 0; + $currency_id = $transaction->owner->fix_currency_id; + @endphp + + owner ?? $booking; + $paymentSum = $booking->transactions() + ->where('type', TransactionType::PAYMENT) + ->where('status', ApprovalStatus::COMPLETED) + ->get() + ->sum(function ($transaction) { + return round($transaction->amount, 2); + }); + + $totalPayment = 0; + $average_currency_rate = $transaction->currency_rate; + + $paymentSumRefund = 0; + 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); + + // $totalPayment = $paymentSum - $refundedAmount - $refundedServiceCharge; + + $totalPayment = $paymentSum; + } + else{ + $paymentSumRefund = $booking->transactions() + ->where('type', TransactionType::PAYMENT) + ->where('status', ApprovalStatus::REFUNDED) + ->get() + ->sum(function ($transaction) { + return round($transaction->amount, 2); + }); + } + ?> + + @if (!empty($po_order_transaction) && $po_order_transaction->transactionDetails) + @foreach ($po_order_transaction->transactionDetails as $key => $transaction_detail) + @php + $exactUnitPrice = ($currency_id) === 1 ? $transaction_detail->price : bcdiv($transaction_detail->price, $average_currency_rate, 7); + $displayUnitPrice = round($exactUnitPrice, 2); + + @endphp + + @continue($transaction_detail->product_name === 'Discount' && $displayUnitPrice < 0) + + @php + $itemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 5); + $displayedItemTotal = round(bcmul($displayUnitPrice, $transaction_detail->quantity, 7), 2); + $displayedSubtotal = bcadd($displayedSubtotal, $displayedItemTotal, 2); + $subtotal = bcadd($subtotal, $itemTotal, 5); + @endphp + + + + + + + + + @endforeach + @else + @if ($paymentSumRefund) + + + + + + + + + @endif + @endif + + + @php + $subtotalWithDiscount = bcsub($subtotal, $voucherDiscount, 5); + 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; + }); + } + if ($paymentSumRefund && $displayedSubtotal === 0){ + $subtotal = bcsub($paymentSumRefund, $serviceCharge, 5); + $displayedSubtotal = bcsub($paymentSumRefund, $serviceCharge, 2); + } + @endphp + + + + + + + + + + + + @if($voucher_redemption) + + + + + + @endif + + @if($transaction->tax > 0) + + + + + + @endif + @php + $displayedSubtotal = is_numeric($displayedSubtotal) ? sprintf('%F', $displayedSubtotal) : '0'; + $serviceCharge = is_numeric($serviceCharge) ? sprintf('%F', $serviceCharge) : '0'; + $tax = is_numeric($transaction->tax) ? sprintf('%F', $transaction->tax) : '0'; + $voucherDiscount = is_numeric($voucherDiscount) ? sprintf('%F', $voucherDiscount) : '0'; + + $displayedTotal = bcadd( + bcadd( + bcadd($displayedSubtotal, $serviceCharge, 5), + $tax, + 5 + ), + $voucherDiscount, + 5 + ); + + $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; + } + + if($paymentSumRefund){ + $total = $paymentSumRefund; + } + @endphp + + + + + + + + + + + +
NoStock CodeDescriptionQuantityUnit Price (RM)Total Amount
(RM)
{{ $key + 1 }}{{ $transaction_detail->product_code }}{{ $transaction_detail->product_name }}{{ $transaction_detail->quantity }} + {{ number_format($displayUnitPrice, 2) }} + + {{ number_format($displayedItemTotal, 2) }} +
CANCEL FULL ORDER
Subtotal{{ number_format($displayedSubtotal, 2) }}
Service Charges{{ number_format($serviceCharge, 2) }}
Voucher ({{ $voucher_redemption->voucher->code }}){{ number_format($voucherDiscount, 2) }}
Tax{{ number_format($transaction->tax, 2) }}
Adjustment{{number_format($discrepancy, 5)}}
Total + {{ number_format($total, 2) }} +