debug invoice pdf

This commit is contained in:
Omair Saleh
2023-08-10 16:27:56 +08:00
parent 34399aa34d
commit f5121d35da
3 changed files with 110 additions and 63 deletions
@@ -69,6 +69,7 @@
<br>
<br>
<table class="line-table" style="overflow: wrap" autosize="1">
<!-- Table Header -->
<thead>
<tr>
<th width="5%">No</th>
@@ -81,34 +82,37 @@
</thead>
<tbody>
@php
$subtotal = 0;
$voucher_redemption = isset($voucher_redemption) ? $voucher_redemption : null;
$voucherDiscount = $voucher_redemption ? $voucher_redemption->value * -1 : 0;
$currencyRate = $transaction->currency_rate;
$subtotal = calculateSubtotal($po_order_transaction, $transaction->currency_rate);
$voucherDiscount = getVoucherDiscount($voucher_redemption);
$displayedSubtotal = 0;
@endphp
@foreach ($po_order_transaction->transactionDetails as $key => $transaction_detail)
@php
$exactUnitPrice = bcdiv($transaction_detail->price, $transaction->currency_rate, 5);
$itemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 5);
$displayedItemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 2);
$displayedSubtotal = bcadd($displayedSubtotal, $displayedItemTotal, 2);
@endphp
<tr>
<td width="5%" class="center top">{{ $key + 1 }}</td>
<td class="stock-code top" width="10%">{{ $transaction_detail->product_code }}</td>
<td class="description">{{ $transaction_detail->product_name }}</td>
<td width="10%" class="center top">{{ $transaction_detail->quantity }}</td>
<td width="15%" class="center top">
@php
$unitPrice = $transaction_detail->price / $currencyRate;
@endphp
{{ number_format($unitPrice, 2) }}
{{ number_format($exactUnitPrice, 2) }}
</td>
<td width="20%" class="right top">
@php
$itemTotal = $unitPrice * $transaction_detail->quantity;
$subtotal += $itemTotal;
@endphp
{{ number_format($itemTotal, 2) }}
</td>
</tr>
@endforeach
</tbody>
<tfoot>
@php
$voucherDiscount = getVoucherDiscount($voucher_redemption);
$subtotalWithDiscount = bcsub($subtotal, $voucherDiscount, 5); // Use bcsub to subtract
@endphp
<tr class="subtotal">
<td colspan="4"></td>
<td class="right middle">Subtotal</td>
@@ -117,27 +121,17 @@
<tr class="billingcharges">
<td colspan="4"></td>
<td class="right">Service Charges</td>
<td class="right">
{{ number_format($transaction->service_charge, 2) }}
</td>
<td class="right">{{ number_format($transaction->service_charge, 2) }}</td>
</tr>
@if($voucher_redemption)
<tr class="voucher">
<td colspan="4"></td>
<td class="right middle">Voucher ({{ $voucher_redemption->voucher->code }})</td>
<td class="right middle">-{{ $voucherDiscount }}</td>
<td class="right middle">-{{ number_format($voucherDiscount, 2) }}</td>
</tr>
@endif
<tr class="billingcharges">
<td colspan="4"></td>
<td class="right">Adjustment</td>
<td class="right">
@php
$adjustment = $transaction->amount - $subtotal;
@endphp
{{ number_format($adjustment, 2) }}
</td>
</tr>
@if($transaction->tax > 0)
<tr class="billingcharges">
<td colspan="4"></td>
@@ -145,13 +139,21 @@
<td class="right">{{ number_format($transaction->tax, 2) }}</td>
</tr>
@endif
@php
$displayedTotal = bcadd(bcadd(bcadd($displayedSubtotal, $transaction->service_charge, 2), $transaction->tax, 2), $voucherDiscount, 2);
$expectedTotal = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5);
$discrepancy = bcsub($displayedTotal, $expectedTotal, 5);
$total = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); // Keep precision
@endphp
<tr>
<td colspan="4"></td>
<td class="right middle">Adjustment</td>
<td class="right middle">{{ roundUp($discrepancy,2) }}</td>
</tr>
<tr>
<td colspan="4"></td>
<td class="right middle">Total</td>
<td class="total right middle">
@php
$total = $subtotal + $transaction->service_charge + $transaction->tax + $voucherDiscount;
@endphp
{{ number_format($total, 2) }}
</td>
</tr>
@@ -165,4 +167,28 @@
</tr>
</table>
</htmlpagefooter>
@php
function calculateSubtotal($po_order_transaction, $currencyRate) {
$subtotal = "0";
foreach ($po_order_transaction->transactionDetails as $transaction_detail) {
$unitPrice = bcdiv((string)$transaction_detail->price, (string)$currencyRate, 5);
$itemTotal = bcmul($unitPrice, (string)$transaction_detail->quantity, 5);
$subtotal = bcadd($subtotal, $itemTotal, 5);
}
return $subtotal;
}
function getVoucherDiscount($voucher_redemption) {
return $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0";
}
function roundUp($number, $decimals) {
$factor = pow(10, $decimals);
if ($number > 0) {
return ceil($number * $factor) / $factor;
} else {
return floor($number * $factor) / $factor;
}
}
@endphp
@endsection
@@ -134,11 +134,6 @@
$expectedTotal = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5);
$discrepancy = bcsub($displayedTotal, $expectedTotal, 5);
$total = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); // Keep precision
// $displayedTotal =
// ((float) number_format($subtotal, 2)) +
// ((float) number_format($transaction->service_charge, 2)) +
// ((float) number_format($voucherDiscount, 2)) +
// ((float) number_format($transaction->tax, 2));
@endphp
<tr>
<td colspan="4"></td>
@@ -77,6 +77,7 @@
<br>
<table class="line-table" style="overflow: wrap" autosize="1">
<!-- Table Header -->
<thead>
<tr>
<th width="5%">No</th>
@@ -89,34 +90,37 @@
</thead>
<tbody>
@php
$subtotal = 0;
$voucher_redemption = isset($voucher_redemption) ? $voucher_redemption : null;
$voucherDiscount = $voucher_redemption ? $voucher_redemption->value * -1 : 0;
$currencyRate = $transaction->currency_rate;
$subtotal = calculateSubtotal($po_order_transaction, $transaction->currency_rate);
$voucherDiscount = getVoucherDiscount($voucher_redemption);
$displayedSubtotal = 0;
@endphp
@foreach ($po_order_transaction->transactionDetails as $key => $transaction_detail)
@php
$exactUnitPrice = bcdiv($transaction_detail->price, $transaction->currency_rate, 5);
$itemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 5);
$displayedItemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 2);
$displayedSubtotal = bcadd($displayedSubtotal, $displayedItemTotal, 2);
@endphp
<tr>
<td width="5%" class="center top">{{ $key + 1 }}</td>
<td class="stock-code top" width="10%">{{ $transaction_detail->product_code }}</td>
<td class="description">{{ $transaction_detail->product_name }}</td>
<td width="10%" class="center top">{{ $transaction_detail->quantity }}</td>
<td width="15%" class="center top">
@php
$unitPrice = $transaction_detail->price / $currencyRate;
@endphp
{{ number_format($unitPrice, 2) }}
{{ number_format($exactUnitPrice, 2) }}
</td>
<td width="20%" class="right top">
@php
$itemTotal = $unitPrice * $transaction_detail->quantity;
$subtotal += $itemTotal;
@endphp
{{ number_format($itemTotal, 2) }}
</td>
</tr>
@endforeach
</tbody>
<tfoot>
@php
$voucherDiscount = getVoucherDiscount($voucher_redemption);
$subtotalWithDiscount = bcsub($subtotal, $voucherDiscount, 5); // Use bcsub to subtract
@endphp
<tr class="subtotal">
<td colspan="4"></td>
<td class="right middle">Subtotal</td>
@@ -125,27 +129,17 @@
<tr class="billingcharges">
<td colspan="4"></td>
<td class="right">Service Charges</td>
<td class="right">
{{ number_format($transaction->service_charge, 2) }}
</td>
<td class="right">{{ number_format($transaction->service_charge, 2) }}</td>
</tr>
@if($voucher_redemption)
<tr class="voucher">
<td colspan="4"></td>
<td class="right middle">Voucher ({{ $voucher_redemption->voucher->code }})</td>
<td class="right middle">-{{ $voucherDiscount }}</td>
<td class="right middle">-{{ number_format($voucherDiscount, 2) }}</td>
</tr>
@endif
<tr class="billingcharges">
<td colspan="4"></td>
<td class="right">Adjustment</td>
<td class="right">
@php
$adjustment = $transaction->amount - $subtotal;
@endphp
{{ number_format($adjustment, 2) }}
</td>
</tr>
@if($transaction->tax > 0)
<tr class="billingcharges">
<td colspan="4"></td>
@@ -153,13 +147,21 @@
<td class="right">{{ number_format($transaction->tax, 2) }}</td>
</tr>
@endif
@php
$displayedTotal = bcadd(bcadd(bcadd($displayedSubtotal, $transaction->service_charge, 2), $transaction->tax, 2), $voucherDiscount, 2);
$expectedTotal = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5);
$discrepancy = bcsub($displayedTotal, $expectedTotal, 5);
$total = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); // Keep precision
@endphp
<tr>
<td colspan="4"></td>
<td class="right middle">Adjustment</td>
<td class="right middle">{{ roundUp($discrepancy,2) }}</td>
</tr>
<tr>
<td colspan="4"></td>
<td class="right middle">Total</td>
<td class="total right middle">
@php
$total = $subtotal + $transaction->service_charge + $transaction->tax + $voucherDiscount;
@endphp
{{ number_format($total, 2) }}
</td>
</tr>
@@ -173,4 +175,28 @@
</tr>
</table>
</htmlpagefooter>
@php
function calculateSubtotal($po_order_transaction, $currencyRate) {
$subtotal = "0";
foreach ($po_order_transaction->transactionDetails as $transaction_detail) {
$unitPrice = bcdiv((string)$transaction_detail->price, (string)$currencyRate, 5);
$itemTotal = bcmul($unitPrice, (string)$transaction_detail->quantity, 5);
$subtotal = bcadd($subtotal, $itemTotal, 5);
}
return $subtotal;
}
function getVoucherDiscount($voucher_redemption) {
return $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0";
}
function roundUp($number, $decimals) {
$factor = pow(10, $decimals);
if ($number > 0) {
return ceil($number * $factor) / $factor;
} else {
return floor($number * $factor) / $factor;
}
}
@endphp
@endsection