@extends('layouts.jform')
@section('content')
    <?php
    $total_due_riel = 0;
    $total_due_usd = 0;
    $total_mat_riel = 0;
    $total_mat_usd = 0;
    $total_return = 0;
    $production = \App\Model\production\ProductionRequestModel::find($payment->invoice_id);
    $symbol = $payment->currency->symbol;

    ?>
    <div class="print_this_form">
    <table>
        <tr>
            <td>{{__('administrator.tailor')}}</td>
            <td> : {{$payment->tailor->name??''}}</td>
        </tr>
        <tr>
            <td>{{__('administrator.invoice_number')}}</td>
            <td> : {{$payment->production->invoice_number??''}}</td>
        </tr>
    </table>
    <hr>
    <h3>{{__('administrator.tailor_due')}}</h3>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>#</th>
            <th>{{__('administrator.date')}}</th>
            <th>{{__('administrator.amount')}}</th>
            <th>{{__('administrator.note')}}</th>
        </tr>
        </thead>
        <tbody>
        @foreach($tailor_due as $key=>$row)
            <?php
                if($row->currency_id===1){
                    $total_due_riel += $row->paid_amount;
                }
                if($row->currency_id===2){
                    $total_due_usd += $row->paid_amount;
                }
            $pCur = findCurrency($row->currency_id)
            ?>
            <tr>
                <td>{{$key+1}}</td>
                <td>{{date('d-m-Y',strtotime($row->payment_date))}}</td>
                <td>{{$pCur->symbol??''}}{{$row->paid_amount}}</td>
                <td>{{$row->note}}</td>
            </tr>
        @endforeach
        </tbody>
        <tfoot>
        <tr>
            <th colspan="2" class="text-right">{{__('administrator.total')}}</th>
            <th>{!! '៛'.$total_due_riel.'<br>$'.$total_due_usd !!}</th>
            <th></th>
        </tr>
        </tfoot>
    </table>
    <hr>
    <h3>{{__('administrator.material')}}</h3>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>#</th>
            <th>{{__('administrator.date')}}</th>
            <th>{{__('administrator.invoice_number')}}</th>
            <th>{{__('administrator.material')}}</th>
            <th>{{__('administrator.amount')}}</th>
        </tr>
        </thead>
        <tbody>
        @foreach($material_due as $key=>$row)
            <?php
                if($row->currency_id===1){
                    $total_mat_riel += $row->account_receivable;
                }
                if($row->currency_id===2){
                    $total_mat_usd += $row->account_receivable;
                }
            $inv_cur = findCurrency($row->currency_id);
            $html = '';
            foreach ($row->detail as $kmat=>$mat){
                $unit = findUnit($mat->unit_id);
                $html.= ($kmat+1).'. '.$mat->product->product_name.' ('.$mat->qty.($unit->unit??'').' * '.$inv_cur->symbol.$mat->price.' = '.$inv_cur->symbol.($mat->qty*$mat->price).' )<br>';
            }
            ?>
            <tr>
                <td>{{$key+1}}</td>
                <td>{{date('d-m-Y',strtotime($row->date))}}</td>
                <td>{{$row->invoice_number}}</td>
                <td>{!! $html !!}</td>
                <td>{{$inv_cur->symbol}}{{$row->account_receivable}}</td>
            </tr>
        @endforeach
        </tbody>
        <tfoot>
        <tr>
            <th colspan="4" class="text-right">{{__('administrator.total')}}</th>
            <th>{!! '៛'.$total_mat_riel.'<br>$'.$total_mat_usd !!}</th>
        </tr>
        </tfoot>
    </table>
    <hr>
    <h3>{{__('administrator.return_item')}}</h3>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>#</th>
            <th>{{__('administrator.date')}}</th>
            <th>{{__('administrator.invoice_number')}}</th>
            <th>{{__('administrator.qty')}}</th>
        </tr>
        </thead>
        <tbody>
        @foreach($return_qty as $key=>$row)
            <?php
            $qty = \App\Model\production\ProductionDoneDetailModel::where('production_done_id', $row->id)->sum('qty');
            $total_return += $qty;
            ?>
            <tr>
                <td>{{$key+1}}</td>
                <td>{{date('d-m-Y',strtotime($row->date))}}</td>
                <td>{{$row->invoice_number}}</td>
                <td>{{$qty}}</td>
            </tr>
        @endforeach
        </tbody>
        <tfoot>
        <tr>
            <th colspan="3" class="text-right">{{__('administrator.total_qty')}}</th>
            <th> : {{$total_return}}</th>
        </tr>
        <tr>
            <th colspan="3" class="text-right">{{__('administrator.sewing_price')}}</th>
            <th> : {{$production->sewing_price}}</th>
        </tr>
        <tr>
            <th colspan="3" class="text-right">{{__('administrator.total_amount')}}</th>
            <?php
            $return_amount = $total_return*$production->sewing_price
            ?>
            <th> : {{$symbol}}{{$return_amount}}</th>
        </tr>
        </tfoot>
    </table>
    <hr>
    <h3>ខោបាត់</h3>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th></th>
            <th>{{__('administrator.qty')}}</th>
            <th>{{__('administrator.price')}}</th>
            <th>{{__('administrator.total')}}</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>បាត់ខោ</td>
            <td>{{$payment->lost_qty}}</td>
            <td>{{$symbol.$payment->lost_price}}</td>
            <td>{{$symbol}}{{$payment->lost_qty*$payment->lost_price}}</td>
        </tr>
        </tbody>
    </table>
    <hr>
    <h3>{{__('administrator.grand_total')}}</h3>
    <br>
    <table class="">
        <tr>
            <td>{{__('administrator.total')}} {{__('administrator.return')}}  </td>
            <td> : {{$symbol}}{{$return_amount}}</td>
        </tr>
        @if($total_due_riel)
        <tr>
            <td>{{__('administrator.total')}} {{__('administrator.tailor_due')}}</td>
            <td> : ៛{{$total_due_riel}}</td>
        </tr>
        @endif
        @if($total_due_usd)
        <tr>
            <td>{{__('administrator.total')}} {{__('administrator.tailor_due')}} </td>
            <td> : ${{$total_due_usd}}</td>
        </tr>
        @endif
        @if($total_mat_riel)
        <tr>
            <td>{{__('administrator.total')}} {{__('administrator.material_price')}} </td>
            <td> : ៛{{$total_mat_riel}}</td>
        </tr>
        @endif
        @if($total_mat_usd)
        <tr>
            <td>{{__('administrator.total')}} {{__('administrator.material_price')}} </td>
            <td> : ${{$total_mat_usd}}</td>
        </tr>
        @endif

        <tr>
            <td>បាត់ខោ</td>
            <td> : {{$symbol}}{{$payment->lost_qty*$payment->lost_price}}</td>
        </tr>
        <tr>
            <td>{{__('administrator.paid')}}</td>
            <td>
                @php
                        $pCur = findCurrency($payment->paid_currency);
                @endphp
                : {{$pCur->symbol??''}}{{$payment->paid_amount}}
            </td>
        </tr>


    </table>
    </div>
@endsection
