@extends('report.index')

@section('title')
PR Report
@endsection

@section('report_title')
PR Report
@endsection

@section('by')
Who prepared this
@endsection

@section('content')
<style type="text/css">
  .invoice {
    border-right: 1px solid #2e2d2d;
    border-bottom: 1px solid #2e2d2d;
    padding: 3px;
  }
  .th{
      border-bottom: 1px solid #2e2d2d;border-right: 1px solid #2e2d2d;
  }
</style>
   @php
        $chunckNumber = 20;
        if(request('per_page_item') > 0){
            $chunckNumber = request('per_page_item');
        }
    @endphp
  @foreach($requisition->requisition_products ->chunk($chunckNumber) as $product)
  
  <section class="sheet padding-10mm">
    <div id="logo" style="float:right;">
      <img src="{{ asset('logo.png') }}" alt="Well Group" height="40" style="border-style: none;">
    </div>
    <div style="margin-left: 10%;float: center !important">
      <center>
        <span style="font-size: 30px"><strong>{{ unit_name() ?? app_info()->company_name }}</strong></span>
        <br>
        <span style="font-size: 24px"><strong>Purchase Requisition</strong></span>
        {{-- <h1 style="margin-left: 10%">{{ unit_name() ?? app_info()->company_name }}</h1>
        <h2>Purchase Requistion Form</h2> --}}
        
        <div class="print-none" style="font-weight: normal !important; font-size: 18px !important">
                @if($loop->first)
                    <!--<form action="" method="get">-->
                    <!--    <select name="gap" onchange="this.form.submit()">-->
                    <!--        @foreach(gaps() as $key => $gap)-->
                    <!--            <option value="{{ $key }}" @if(request('gap') == $key) selected @endif >{{ $gap }}</option>-->
                    <!--        @endforeach-->
                    <!--    </select>-->
                    <!--    <select name="per_page_item" onchange="this.form.submit()">-->
                    <!--        @foreach(perPageItem() as $key => $item_number)-->
                    <!--            <option value="{{ $key }}" @if(request('per_page_item') == $key) selected @endif>{{ $item_number }}</option>-->
                    <!--        @endforeach-->
                    <!--    </select>-->
                    <!--</form>-->
                @endif

            </div>  
      </center>
    </div>
    <br>
    <div style="margin-bottom: 5px;font-size: 16px;float: left">
      <strong>
          Requisition No : {{ $requisition->requisition_id }}
      </strong>
    </div>
    <div style="margin-bottom: 5px;font-size: 16px;float: right">
      <strong>
          Requisition Date : {{ dateFormat($requisition->created_at) }}
      </strong>
    </div>
<table class="table" align="center" cellpadding="0" cellspacing="0" border="0" style="margin-top:20px;">

    <thead>
        <tr style="font-size: 12px;height: 20px; background-color: #e3e1dc;" class="text-center">
            <!--<td class="th" rowspan="2"> #SL</td>-->
            <td class="th" rowspan="2"> Group Name</td>
            <td class="th" width="6%" rowspan="2"> Code</td>
            <td class="th" width="20%" rowspan="2"> Item Name</td>
            <td class="th" rowspan="2" width="3%"> Unit</td>
            <td class="th" width="5%" rowspan="2"> Cur Stock</td>
            <td class="th" width="5%" rowspan="2"> 2 Monts Consum.</td>
            {{--<td class="th" rowspan="2"> 2 Months Consum.</td>--}}
            <td class="th"  width="7%"  rowspan="2"> Present Req. Qty</td>
            <td class="th" colspan="4"> Last Purchase </td>
            <td class="th"  width="17%" rowspan="2"> Remarks</td>
      </tr>
      <tr style="font-size: 12px;height: 20px; background-color: #e3e1dc;" class="text-center">
        <td class="th" width="5%" > Ord No</td>
        <td class="th" width="7%" > Date</td>
        <td class="th" width="5%" > Qty</td>
        <td class="th" width="5%" > Rate</td>
    </tr>
    </thead>
    <tbody style="font-size: 10px">
        @php $i=0; @endphp
        @if($requisition->count()>0)
            {{ Form::open(['route'=>['inventory.requisition-item.checking-approval',$requisition->id],'method'=>'post']) }}

            @foreach($product as $product)
            @if($product->disabled == 0)
            @php
                $last_purchase = App\GoodsinProduct::find($product->last_goodsin_id);
                // $last_purchase = App\Product::find($product->id);

                $curr_stock = App\GoodsinProduct::where(get_where())->where('product_id',$product->product_id)->sum('qty') - App\GoodsIssueProduct::where(get_where())->where('product_id',$product->product_id)->sum('qty');
                $date_today = date('Y-m-d H:i:s');
                $date_two_months = date("Y-m-d H:i:s", strtotime("-2 months"));
                $two_months_consume = App\GoodsinProduct::where(get_where())->where('product_id',$product->product_id)->where('created_at', '<=', $date_today)->where('created_at', '>=', $date_two_months)->sum('qty');
            @endphp
                <tr>
                    
                    <!--<td class="invoice" >{{ ++$i }}</td>-->
                    
                    {{-- <td class="invoice" > {{ Form::checkbox('product_id[]',$product->id,($product->user_id !=null ? true : false),['class'=>'form-control']) }} </td> --}}
                    {{-- <td class="invoice" >{{ $product->product->department ? $product->product->department->name : "N/A" }}</td> --}}
                    <td class="invoice" >{{ $product->product->group ? $product->product->group->name : " N/A " }}</td>
                    <td class="invoice text-center" >{{ $product->product ? $product->product->item_code : " N/A "  }}</td>
                    <td class="invoice text-left" >{{ $product->product ? $product->product->name  : " N/A " }}</td>


                    <td class="invoice text-center" >{{ $product->product->unit ? $product->product->unit->name : " N/A "  }}</td>
                    <td class="invoice text-right" > {{ number_format($curr_stock) }}</td>
                    <td class="invoice text-right" > {{ number_format($two_months_consume) }}</td>
                    {{--<td class="invoice" > N/A </td>--}}
                    <td class="invoice text-right" >{{ $product->qty > 0? number_format(floatval($product->qty),2) : '' }}</td>
                    <td class="invoice text-right" >
                      @if(isset($last_purchase->id))
                        {{ optional(optional($last_purchase->goodsin)->purchase)->purchase_no}} 
                        @else
                        {{-- {{ $product->product ? $product->product->last_purchase_order_no  : " N/A " }} --}}
                      @endif
                      {{--{{ $product->product ? $product->product->last_purchase_order_no  : " N/A " }}--}}
                    </td>
                    <td class="invoice text-center" >
                      @php $new_goods_in = false; @endphp
                      @if(isset($last_purchase->id))
                        @if (optional($last_purchase->goodsin)->bill_date != null)
                          @php $new_goods_in = true; @endphp
                          {{ date('d-m-Y',strtotime(optional($last_purchase->goodsin)->bill_date)) }}
                        
                        @endif
                        {{-- @else
                        {{ $product->product ? $product->product->last_purchase_date  : " N/A " }} --}}
                      @endif
                      
                    </td>
                    <td class="invoice text-right" >
                      @if(isset($last_purchase->id) && $new_goods_in)
                        {{ $last_purchase->qty > 0? number_format(floatval($last_purchase->qty),2) : ''}} 
                      @endif
                    </td>
                    <td class="invoice text-right" >
                       @if(isset($last_purchase->id) && $new_goods_in)
                      {{ $last_purchase->price }}
                      {{-- @else
                      {{ $product->product ? $product->product->last_purchase_price  : " N/A " }} --}}
                      @endif
                      
                    </td>
                    <td class="invoice text-center" >
                        {{-- @if(isset($last_purchase->id) && $new_goods_in)
                      
                       @else
                      Last Price = {{ $product->product ? $product->product->last_purchase_price  : " N/A " }}
                       @endif --}}
                       <br>
                       @php $new_goods_in = false; @endphp
                      @if(isset($last_purchase->id))
                        @if (optional($last_purchase->goodsin)->bill_date != null)
                          @php $new_goods_in = true; @endphp
                         
                        
                        @endif
                        @else
                        {{-- Date = {{ date('d-m-Y',strtotime( $product->product ? $product->product->last_purchase_date  : " N/A ")) }} --}}
                        {{-- {{ date('d-m-Y',strtotime(optional($last_purchase->goodsin)->bill_date)) }} --}}
                      @endif
                       
                     </td>
                    {{-- <td class="invoice" >{{ $product->note }}</td> --}}
                </tr>
            @endif
            @endforeach
        @endif
    </tbody>
</table>

<div style="font-size: 12px">
  <strong>Note: </strong> <i>{!! $requisition->note !!}</i>

<div class="signature signature-gap">
            <table class=" table"  align="center" cellpadding="0" cellspacing="0" style="width: 100%;border:none; float:left;">
                <tr>
                    <td style="border-top: 1px solid black;width: 20%;text-align: center;font-size: 13px; margin-top:30px;" ><br>ENTRY BY</td>
                    <td style="width: 20%;border:none;"></td>
                    <td style="border-top: 1px solid black;width: 20%;text-align: center;font-size: 13px; margin-top:30px;" ><br> STORE MANAGER</td>
                    <td style="width: 20%;border:none;"></td>
                    <td style="border-top: 1px solid black;width: 20%;text-align: center;font-size: 13px"><br>APPROVE BY</td>
                    {{-- <td style="width: 12%;border:none;"></td>
                    <td style="border-top: 1px solid black;width: 15%;text-align: center;font-size: 13px"><br><span class="strong">ED(Tech)/ Director/ MD</span></td> --}}
                </tr>
            </table>
        </div>
<br><br><br>
@if($loop->last)
                <center><a id="print" class="btn-print print-none" onclick="window.print()" >Print</a></center>
            @endif
</section>
<div class="page_break"></div>
@endforeach

@endsection