<?php Fun::lang()?>
<?php $total = 0 ?>
<?php $total_discount = 0 ?>
<?php $g_total = 0 ?>
<div class="print_this_form">
    <style>
        .tb_info td{
            padding-top: 7px;
        }
    </style>
    <table width="100%">
        <tr>
            <td style="text-align: left">
                @php
                    $br = myBranch();
                @endphp
                @if($br->logo)
                    <img src="{{asset($br->logo??LogoImage())}} " style="height: 100px;margin: 5px">
                @else
                    <h4>{{$br->name}}</h4>
                @endif
            </td>
            <td style="text-align: right;width: 30%">
                <h2 class="text-green pull-right">{{__('administrator.stock_in')}}</h2><br>
                <table class="table table-striped pull-right">
                    <tr>
                        <td class=""><b>{{__('administrator.invoice')}}:</b></td>
                        <td>{{$purchases->invoice_number}}</td>
                    </tr>
                    <tr>
                        <td class=""><b>{{__('administrator.stock_in_date')}}:</b></td>
                        <td>{{date('d-m-Y',strtotime($purchases->purchase_date))}}</td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <hr>
    <table width="100%" class="tb_info">
        <tr>
            <td>
                <table class="text-left">
                    <tr>
                        <td colspan="2"><h4><u>{{__('administrator.suppliers')}}</u></h4></td>
                    </tr>
                    <tr>
                        <td>{{__('administrator.from')}}</td>
                        <td> : {{findSupplier($purchases->supplier_id)->supplier_name}}</td>
                    </tr>
                    <tr>
                        <td>{{__('administrator.supplier_phone')}}</td>
                        <td> : {{findSupplier($purchases->supplier_id)->supplier_phone}}</td>
                    </tr>
                    @if(findSupplierType($purchases->supplier_id))
                        <td>{{__('administrator.supplier_type')}}</td>
                        <td> : {{findSupplierType($purchases->supplier_id)->type_name}}</td>
                    @endif
                </table>
            </td>
            <td>
                <table class="text-left pull-right">
                    <tr>
                        <td colspan="2"><h4><u>{{__('administrator.ship_to')}}</u></h4></td>
                    </tr>
                    <tr>
                        <td>{{__('administrator.warehouse')}}</td>
                        <td> : {{ findWarehouse($purchases->warehouse_id)->warehouse_name }}</td>
                    </tr>
                    <tr>
                        <td>{{__('administrator.address')}}</td>
                        <td> : {{ findWarehouse($purchases->warehouse_id)->location_name }}</td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <hr>
    <table class="table table-striped table-hover ">
        <thead>
        <tr>
            <th>#</th>
            <th>{{__('administrator.product_code')}}</th>
            <th>{{__('administrator.item_name')}}</th>
            <th>{{__('administrator.qty')}}</th>
            @can('Show Purchase Item Price')
                <th>{{__('administrator.cost')}}</th>
                <th>{{__('administrator.discount')}} (%)</th>
                <th>{{__('administrator.total')}}</th>
            @endcan
        </tr>
        </thead>
        <tbody>
        @php

            $g_total= 0;
            $total = 0;
            $currency = findCurrency($purchases->currency_id);
            $currency = $currency?$currency->symbol:'';
        @endphp
        @foreach($purchase_details as $key => $value)

            <?php
            $check_pro = false;
            if ($purchases->type == 'material') {
                $unit = findUnit($value->unit_id);
                $barcode = getProductById($value->product_id)->barcode;
                if($barcode){
                    $check_pro = true;
                }
            } else {
                if ($value->type == 'fabric') {

                    $product = getProductById($value->product_id);
                    $unit = findUnit($product->unit_id??'');
                    $sub_unit = findUnit($product->child_unit??0);
                    if($unit){
                        $check_pro = true;
                    }
                } else {
                    $unit = findProductUnitByCode($value->barcode, $value->type);
                    if($unit){
                        $check_pro = true;
                    }
                    $unit = findUnit($unit->unit_id??'');
                }
                $barcode = $value->barcode;
            }
            $amount = $value->qty * $value->cost;
            $total = $amount - ($value->dis * $amount / 100);
            $g_total += $total;
            // $total_discount += ($currency == '$' ? ($amount * $value->dis / 100) : ($amount * $value->dis / 100));
            $color = findColor($value->color_code);
            $size = findSize($value->size_code);

            ?>
                <tr>
                    <td>{{$key+1}}</td>
                    <td>{{$barcode}}</td>
                    <td>{{getProductById($value->product_id)->product_name??''}}
                        @if($purchases->type !== 'material')
                            @if($purchases->type=='product')
                                ({{($size->size??'').','.($color->name??'')}})
                            @else
                                ({{$value->qty_per_unit.' '.($sub_unit->unit??'')}})
                            @endif
                        @endif
                    </td>
                    <td>{{$value->qty.' '.($unit?$unit->unit:'')}}</td>
                    @can('Show Purchase Item Price')
                        <td>{{$currency.$value->cost }}</td>
                        <td>{{number_format($value->dis)}}%</td>
                        <td>{{$currency.$total}}</td>
                    @endcan
                </tr>
        @endforeach
        @php
            $paid = $purchases->paid;
        @endphp
        </tbody>
        @can('Show Purchase Item Price')
            <tfoot class="label-right">
            <tr>
                <td colspan="6" class="text-right">{{__('administrator.discount')}} :</td>
                <td class="">{{number_format($purchases->discount)}}%</td>
            </tr>
            <tr>
                <td colspan="6" class="text-right"><b>{{__('administrator.grand_total')}} :</b></td>
                <td class=""> {{$currency.number_format(($g_total),2)}}</td>
            </tr>
            @if($purchases->paid)
                <tr>
                    <td colspan="6" class="text-right"><b>{{__('administrator.paid')}}:</b></td>
                    <td> {{$currency.number_format($purchases->paid,2)}}</td>
                </tr>
            @endif
            @if($paid-$g_total>0)
                <tr>
                    <td colspan="6" class="text-right"><b>{{__('administrator.change_amount')}} :</b></td>
                    <td>{{$currency.number_format(($paid-$g_total),2)}}</td>
                </tr>
            @elseif($paid-$g_total)
                <tr>
                    <td colspan="6" class="text-right"><b>{{__('administrator.owed_amount')}} :</b></td>
                    <td>{{$currency.number_format(($g_total-$paid),2)}}</td>
                </tr>
            @endif
            </tfoot>
        @endcan
    </table>
</div>









