@php
// 1. Calculate Totals Upfront
$totalRoll = 0;
$totalWeight = 0;
$totalLength = 0;
$totalArea = 0;
$totalAmount = 0;
$currency = strtoupper($ET_HEADER->first()->CURRENCY ?? 'JPY');
$amountMultiplier = (in_array($currency, ['JPY', 'IDR'])) ? 100 : 1;
$backendTotalAmount = (float) ($ET_HEADER->first()->TOTALAMOUNT ?? 0) * $amountMultiplier;
$backendDiscountAmt = (float) ($ET_HEADER->first()->DISCOUNTAMT ?? 0) * $amountMultiplier;
$backendTransCostAmt = (float) ($ET_HEADER->first()->TRANSCOSTAMT ?? 0) * $amountMultiplier;
$backendNetAmount = (float) ($ET_HEADER->first()->NETAMOUNT ?? 0) * $amountMultiplier;
$backendVatAmount = (float) ($ET_HEADER->first()->VATAMOUNT ?? 0) * $amountMultiplier;
$backendGrandTotal = (float) ($ET_HEADER->first()->GRANDTOTAL ?? 0) * $amountMultiplier;
$vatRate = (float) ($ET_HEADER->first()->VATRATE ?? 0);
$vatRateUnit = $ET_HEADER->first()->VATRATEUNIT ?? '%';
if ($backendVatAmount == 0) {
$calculatedAmount = $backendNetAmount;
} else {
$calculatedAmount = round($backendNetAmount * 11 / 12);
}
if (!function_exists('numberToWords')) {
function numberToWords($number, $currency = 'JPY') {
if ($number == 0) return '(' . strtoupper($currency) . ') Zero';
// For USD and other decimal currencies, handle cents
$hasCents = !in_array(strtoupper($currency), ['JPY', 'IDR']);
$integerPart = (int) $number;
$decimalPart = 0;
if ($hasCents) {
$decimalPart = round(($number - $integerPart) * 100);
}
$ones = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
$tens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
$teens = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
$units = [['', 'Thousand', 'Million', 'Billion', 'Trillion'], ['', 'Thousand', 'Million', 'Milliard', 'Billion']];
$convertNumber = function($num) use ($ones, $tens, $teens, $units, &$convertNumber) {
if ($num == 0) return 'Zero';
if ($num < 0) return 'Minus ' . $convertNumber(abs($num));
$words = '';
if ($num < 10) $words = $ones[$num];
elseif ($num < 20) $words = $teens[$num - 10];
elseif ($num < 100) { $words = $tens[(int) ($num / 10)]; if ($num % 10) $words .= ' ' . $ones[$num % 10]; }
elseif ($num < 1000) { $words = $ones[(int) ($num / 100)] . ' Hundred'; if ($num % 100) $words .= ' ' . $convertNumber($num % 100); }
else {
$unit_index = 0; $unit_words = '';
while ($num > 0) {
$chunk = $num % 1000;
if ($chunk) {
$chunk_words = $convertNumber($chunk);
if ($unit_index > 0) $chunk_words .= ' ' . $units[0][$unit_index];
$unit_words = $chunk_words . ($unit_words ? ' ' . $unit_words : '');
}
$num = (int) ($num / 1000);
$unit_index++;
}
$words = $unit_words;
}
return trim($words);
};
$result = $convertNumber($integerPart);
if ($hasCents && $decimalPart > 0) {
$result .= ' and ' . $convertNumber($decimalPart) . ' Cents';
}
return '(' . strtoupper($currency) . ') ' . trim($result);
}
}
// Get majority SALESUOM for BX1 label
$majorityUom = isset($ET_ITEM) && $ET_ITEM->isNotEmpty()
? strtoupper($ET_ITEM->pluck('SALESUOM')->countBy()->sortDesc()->keys()->first() ?? 'BX1')
: 'BX1';
$firstItemSalesUom = $majorityUom === 'BAL' ? 'BALE' : $majorityUom;
// Loop to calculate totals only
$totalQtyBx1 = 0;
$totalPcs = 0;
foreach ($ET_ITEM as $item) {
$totalQtyBx1 += (float) ($item->ORDERQTY ?? 0);
$totalWeight += (float) ($item->WEIGHT ?? 0);
$totalPcs += (float) ($item->AREAPERPIECE ?? 0);
$totalAmount += (float) ($item->NETAMOUNT ?? 0) * $amountMultiplier;
}
// --- PAGINATION LOGIC ---
$chunks = \App\Helpers\DocumentPagination_Helper::paginate($ET_ITEM, 'sales_contract');
@endphp
@foreach ($chunks as $pageIndex => $chunk)
@if ($pageIndex > 0)
@endif
@if ($pageIndex == 0)
|
Consignee
{{ $ET_HEADER->first()->CONSIGNEENAME1 ?? '' }}
@if ($ET_HEADER->first()->CONSIGNEENAME2 ?? null)
{{ $ET_HEADER->first()->CONSIGNEENAME2 }}
@endif
@if ($ET_HEADER->first()->CONSIGNEESTREET ?? null)
{{ $ET_HEADER->first()->CONSIGNEESTREET ?? '' }}
@endif
@if ($ET_HEADER->first()->CONSIGNEESTRSUPPL1 ?? null)
{{ $ET_HEADER->first()->CONSIGNEESTRSUPPL1 ?? '' }}
@endif
@if ( $ET_HEADER->first()->CONSIGNEESTRSUPPL2 ?? null)
{{ $ET_HEADER->first()->CONSIGNEESTRSUPPL2 ?? '' }}
@endif
@if ($ET_HEADER->first()->CONSIGNEESTRSUPPL3 ?? null)
{{ $ET_HEADER->first()->CONSIGNEESTRSUPPL3 ?? '' }}
@endif
@if ($ET_HEADER->first()->CONSIGNEECITY ?? null)
{{ $ET_HEADER->first()->CONSIGNEECITY ?? '' }}
@endif
{{ $ET_HEADER->first()->CONSIGNEECOUNTRY ?? '' }}
|
| SO/PI No |
: |
{{ ltrim($ET_HEADER->first()->SALESDOCUMENT ?? '', '0') }} |
Date
{{ $ET_HEADER->first()->DOCUMENTDATE
? \Carbon\Carbon::parse($ET_HEADER->first()->DOCUMENTDATE)->format('d-m-Y')
: '' }}
|
| Our Ref |
: |
{{ $ET_HEADER->first()->OURREFERENCE ?? '' }} |
| No PO |
: |
{{ $ET_HEADER->first()->CUSTOMERPO ?? '' }} |
| End User |
: |
{{ $ET_HEADER->first()->ENDUSER ?? '' }} |
| Payment Term |
: |
{{ $ET_HEADER->first()->ZTERMTXT ?? '' }} |
| Price Condition |
: |
{{ $ET_HEADER->first()->PRICECOND ?? '' }} |
|
|
Buyer / Notify Party ( if other than consignee )
{{ $ET_HEADER->first()->BUYERNAME1 ?? '' }}
@if ($ET_HEADER->first()->BUYERNAME2 ?? null)
{{ $ET_HEADER->first()->BUYERNAME2 }}
@endif
@if ($ET_HEADER->first()->BUYERSTREET ?? null)
{{ $ET_HEADER->first()->BUYERSTREET ?? '' }}
@endif
@if ($ET_HEADER->first()->BUYERSTRSUPPL1 ?? null)
{{ $ET_HEADER->first()->BUYERSTRSUPPL1 ?? '' }}
@endif
@if ( $ET_HEADER->first()->BUYERSTRSUPPL2 ?? null)
{{ $ET_HEADER->first()->BUYERSTRSUPPL2 ?? '' }}
@endif
@if ($ET_HEADER->first()->BUYERSTRSUPPL3 ?? null)
{{ $ET_HEADER->first()->BUYERSTRSUPPL3 ?? '' }}
@endif
@if ($ET_HEADER->first()->BUYERCITY ?? null)
{{ $ET_HEADER->first()->BUYERCITY ?? '' }}
@endif
{{ $ET_HEADER->first()->BUYERCOUNTRY ?? '' }}
|
| Loading Port |
: |
{{ $ET_HEADER->first()->LOADPORT ?? '' }} |
| Destination |
: |
{{ $ET_HEADER->first()->DESTINATION ?? '' }} |
| Shipment by |
: |
{{ $ET_HEADER->first()->SHIPMENTBY ?? '' }} |
| Packing |
: |
{{ $ET_HEADER->first()->PACKING ?? '' }} |
| Shipment Date |
: |
{{ $ET_HEADER->first()->SHIPMENTDATE ?? '' }} |
|
@else
@endif
| Gsm |
Width (m) |
Length |
Qty {{ $firstItemSalesUom }} |
Tot. Weight (kg) |
PCS / {{ $firstItemSalesUom }} |
Total PCS |
Price /
Unit {{ $ET_HEADER->first()->CURRENCY ?? 'JPY' }} / {{ $firstItemSalesUom }} |
Amount (
{{ $ET_HEADER->first()->CURRENCY ?? 'JPY' }} )
|
@foreach ($chunk as $item)
|
{{ $item->DESCRIPTION ?? '' }}
@if($item->CUSTOMERMATERIAL ?? null)
// {{ $item->CUSTOMERMATERIAL }}
@endif
@if($item->MATERIAL ?? null)
{{ $item->MATERIAL }}
@endif
|
| {{ $item->GSM ?? '' }} |
{{ number_format((float) ($item->WIDTHPERROLL ?? 0), 3, ',', '.') }} |
{{ number_format((float) ($item->LENGTHPERROLL ?? 0), 3, ',', '.') }} |
{{ $item->ORDERQTY ?? '' }} |
{{ number_format((float) ($item->WEIGHT ?? 0), 2, ',', '.') }} |
{{ number_format((float) ($item->LENGTHPERPIECE ?? 0), 0, ',', '.') }} |
{{ number_format((float) ($item->AREAPERPIECE ?? 0), 0, ',', '.') }} |
{{ number_format((float) ($item->PRICECONDITIONRATE ?? 0), 2, ',', '.') }}
/ {{ $item->PRICECONDITIONBASEQTY ?? '1' }} |
{{ number_format((float) ($item->NETAMOUNT ?? 0) * $amountMultiplier, 2, ',', '.') }}
|
@endforeach
@if ($loop->last)
| TOTAL AMOUNT |
{{ $totalQtyBx1 }} |
{{ number_format($totalWeight, 2, ',', '.') }} |
|
{{ number_format($totalPcs, 0, ',', '.') }} |
|
{{ number_format($backendTotalAmount, 2, ',', '.') }} |
| Discount |
{{ number_format($backendDiscountAmt, 2, ',', '.') }} |
| Transportation
Cost
|
{{ number_format($backendTransCostAmt, 2, ',', '.') }} |
| TOTAL AMOUNT -
Discount +
Trans. Cost |
{{ number_format($backendNetAmount, 2, ',', '.') }}
|
@if(!isset($is_no_amount_no_vat_rate_n_rate_unit) || !$is_no_amount_no_vat_rate_n_rate_unit)
| Amount |
{{ number_format($calculatedAmount, 2, ',', '.') }}
|
@endif
|
@if(isset($is_no_amount_no_vat_rate_n_rate_unit) && $is_no_amount_no_vat_rate_n_rate_unit)
VAT
@else
VAT {{ number_format($vatRate, 0) }}{{ $vatRateUnit }}
@endif
|
{{ number_format($backendVatAmount, 2, ',', '.') }}
|
| TOTAL |
{{ number_format($backendGrandTotal, 2, ',', '.') }}
|
|
{{ numberToWords($backendGrandTotal, $currency) }}
|
@endif
@if ($loop->last)
@endif
@endforeach