@extends('layouts.base_pdf')
@section('inner_content')
    <br>
    <htmlpageheader name="page-header">
        <br><br>
        <div class="separator"><strong><i>{{ $group->reference }}</i></strong></div>
    </htmlpageheader>
    <br>
    <table>
        <tr>
            <td class="title">
                <strong>Purchase Order</strong>
            </td>
            <td class="document-detail">
                PO#: {{ $group->reference }} <br>
                Date: {{ $group->created_at }}
            </td>
        </tr>
    </table>

    <br>

    <table class="buyer-seller">
        <tr>
            <td width="50%" class="top">
                <span class="buyer-seller-title">
                    Seller
                </span>
                <br>
                <span class="buyer-company">
                    {{ $supplier->reference }}
                </span>
            </td>
            <td width="50%" class="top">
                <span class="buyer-seller-title">
                    Buyer
                </span>
                <br>
                <span class="buyer-company">
                    CIEF Worldwide Sdn Bhd (1134596-M)
                </span>
                <div class="address">
                    No. 72-3, Jalan Jalil 1,<br>
                    The Earth Bukit Jalil,<br>
                    57000 Kuala Lumpur
                </div>
                <div class="contact-no">
                    Tel: 03-8082 1252
                </div>
            </td>
        </tr>
    </table>

    <br>

    <table class="line-table" style="overflow: wrap" autosize="1">
        <thead>
            <tr>
                <th width="5%">No</th>
                <th class="stock-code" width="10%">Stock Code</th>
                <th class="description">Description</th>
                <th width="10%">Quantity</th>
                <th width="15%">Unit Price (RM)</th>
                <th width="10%">Total Amount<br>(RM)</th>
            </tr>
        </thead>
        <tbody>
            @php
                $subtotal = 0;
            @endphp

            @foreach($group->transactions as $po_order_transaction)
                @foreach ($po_order_transaction->owner->owner->transactions()->where('type', \App\Classes\ValueObjects\Constants\TransactionType::PURCHASE_ORDER)->where('status', \App\Classes\ValueObjects\Constants\ApprovalStatus::APPROVED)->first()->transactionDetails as $key => $transaction_detail)
                    <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">
                            {{ number_format( (1/$group->currency_rate) * $transaction_detail->price, 2) }}
                        </td>
                        <td width="20%" class="right top">
                            {{  number_format((float)number_format( (1/$group->currency_rate) * $transaction_detail->price, 2,'.','')*$transaction_detail->quantity,2) }}

                            @php
                                $subtotal += number_format((float)number_format( (1/$group->currency_rate) * $transaction_detail->price, 2,'.','')*$transaction_detail->quantity,2,'.','');
                            @endphp
                        </td>
                    </tr>
                @endforeach
            @endforeach
        </tbody>
        <tfoot>
            <tr class="subtotal">
                <td colspan="4"></td>
                <td class="right middle">Subtotal</td>
                <td class="right middle">
                    {{ number_format($subtotal, 2) }}
                </td>
            </tr>
            <tr class="billingcharges">
                <td colspan="4"></td>
                <td class="right">Service Charges</td>
                <td class="right">
                    {{ number_format($group->service_charge, 2) }}
                </td>
            </tr>
            <tr class="billingcharges">
                <td colspan="4"></td>
                <td class="right">Adjustment</td>
                <td class="right">
                    {{ number_format((float)number_format( (1/$group->currency_rate) * $group->original_amount, 2,'.','') - (float)number_format($subtotal, 2,'.',''),2) }}
                </td>
            </tr>
            @if($group->tax > 0)
                <tr class="billingcharges">
                    <td colspan="4"></td>
                    <td class="right">Tax</td>
                    <td class="right">{{ number_format($group->tax, 2) }}</td>
                </tr>
            @endif
            <tr>
                <td colspan="4"></td>
                <td class="right middle">Total</td>
                <td class="total right middle">
                    {{ number_format( ((1/$group->currency_rate) * $group->original_amount) + $group->service_charge + $group->tax, 2) }}
                </td>
            </tr>
        </tfoot>
    </table>
    <htmlpagefooter name="page-footer">
        <table width="100%">
            <tr>
                <td style="text-align: right; ">This is generated by computer. No signature required.</td>
                <td style="text-align: right; ">Page {PAGENO} of {nbpg}</td>
            </tr>
        </table>
    </htmlpagefooter>
@endsection
