<?php $__env->startSection('title','Checking Purchase Requisition | Invoice'); ?>

<?php $__env->startSection('content'); ?>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.4/jquery-confirm.min.css">

    <!-- Content Header (Page header) -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Purchase Requisition View</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="#">Home</a></li>
                        <li class="breadcrumb-item active">Requisition</li>
                    </ol>
                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    
                </div>
                <div class="col-sm-6 text-right">
                    <a href="<?php echo e(route('purchase_requisitions_single_excel',$requisition->id)); ?>" class="btn btn-info"><i class="fa fa-file-excel"></i> Excel</a>
                    <a onclick="window.open('<?php echo e(route('inventory.purchase-requisition.single-print',$requisition->id)); ?>','_parent')" class="btn bg-success"> <i class="fa fa-print"></i> Print</a>
                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>

    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <!-- Main content -->
                    <div class="invoice p-3 mb-3">
                        <div class="row">
                            <div class="col-12">
                                <h4>
                                    <i class="fas fa-globe"></i> Requisition No : <?php echo e($requisition->requisition_id); ?> , Requisition Invoice : <?php echo e($requisition->invoice_no); ?><br>
                                    <input style="margin: 5px;" class="btn btn-success btn-sm float-left" type="button" onclick='selectAll()' value="Select All"/>
                                    <input style="margin: 5px;" class="btn btn-success btn-sm float-left" type="button" onclick='UnSelectAll()' value="Unselect All"/>
                                    
                                    <small class="float-right" style="margin-top: -30px"> Date: <label class="label-success" style="padding: 5px 15px"> <?php echo e(\Carbon\Carbon::parse($requisition->created_at)->format('D-d-M-Y')); ?></label> </small>
                                </h4>
                                <br>
                            </div>
                            <!-- /.col -->
                        </div>
                        <!-- Table row -->
                        <div class="row">
                            <div class="col-12 table-responsive">
                                <table class="table table-striped table-bordered text-center">
                                    <thead>
                                        <tr class="bg-info">
                                            <th> #SL</th>
                                            <th> Approval</th>
                                            
                                            <th>Group</th>
                                            <th  width="13%">Item Name
                                                
                                            </th>
                                            <th>Item Code</th>
                                            <th> Unit </th>

                                            <th width="10%">Approve Qty</th>
                                            <th> Req. Qty</th>
                                            <th> Approved Qty</th>
                                            
                                            <th> Cur Stock</th>
                                            <th> 2 Monts Consum.</th>
                                            <th> Last Purchase</th>
                                            <th>Remarks</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>


                                    <?php $i=0; ?>
                                    <?php if($requisition->count()>0): ?>
                                        <?php echo e(Form::open(['route'=>['inventory.requisition-item.checking-approval',$requisition->id],'method'=>'post'])); ?>


                                        <?php $__currentLoopData = $requisition->requisition_products; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $product): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <?php if($product->disabled == 0): ?>
                                            <?php
                                            // $last_purchase = App\GoodsinProduct::where('goodsin_id','!=', 0)->where(get_where())->where('product_id',$product->product_id)->orderBy('id','desc')->first();
                                            $last_purchase = App\GoodsinProduct::find($product->last_goodsin_id);
                                            
                                            $approved_total_qty = $product->calculate_approve_qty($requisition,$product);
                                            $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');

                                            ?>
                                            <tr id="tr-<?php echo e($product->id); ?>">
                                                <input type="hidden" name="requisition_qty[]" value="<?php echo e($product->qty); ?>" class="form-control form-control-sm" />
                                                <td><?php echo e(++$i); ?></td>
                                                <td><input type="checkbox" name="requisition_product_id[]" value="<?php echo e($product->id); ?>" class="form-control example" <?php if($product->status == 1): ?> checked <?php endif; ?>>
                                                </td>
                                                <td><?php echo e($product->product->group ? $product->product->group->name : " N/A "); ?></td>
                                                <td><?php echo e($product->product ? $product->product->name  : " N/A "); ?></td>
                                                <td><?php echo e($product->product ? $product->product->item_code : " N/A "); ?></td>
                                                <td><?php echo e($product->product->unit ? $product->product->unit->name : " N/A "); ?></td>

                                                <td>
                                                    <input type="text" name="qty[]" value="" class="form-control text-right" />
                                                </td>
                                                <td>
                                                    <input type="text" name="req_qty[<?php echo e($product->id); ?>]" value="<?php echo e($product->qty); ?>" class="form-control text-right" />
                                                    
                                                </td>
                                                <td><?php echo e($approved_total_qty); ?></td>
                                                
                                                    
                                                
                                                <td> <?php echo e($curr_stock); ?> </td>
                                                <td> <?php echo e($two_months_consume); ?> </td>
                                                <td>
                                                    <?php $new_goods_in = false; ?>
                                                    <?php if(isset($last_purchase->id)): ?>
                                                    <?php if(optional($last_purchase->goodsin)->bill_date != null): ?>
                                                    Price: <?php echo e($last_purchase->price); ?> <br/>Date: <?php echo e(date('d-m-Y',strtotime(optional($last_purchase->goodsin)->bill_date))); ?> 
                                                    <?php endif; ?>
                                                    <?php endif; ?>
                                                </td>
                                                <td><?php echo e($product->note); ?></td>
                                                <td>
                                                    
                                                    <button onclick="Delete('<?php echo e($product->id); ?>')" title="Delete" type="button" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> </button><br>
                                                    <select id="item_id_<?php echo e($product->id); ?>" onchange="changeItem('<?php echo e($product->id); ?>')" class="form-control select2">
                                                        <option >Change Item</option>
                                                        <?php $__currentLoopData = $items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                            <option value="<?php echo e($item->id); ?>"><?php echo e($item->name); ?></option>
                                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                    </select>
                                                </td>

                                            </tr>
                                            <?php endif; ?>

                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                            <!-- /.col -->
                        </div>
                        <!-- /.row -->

                        <div class="row">
                            <div class="col-lg-4">
                                <label for="sent_for">Sent For: <span style="color: red"> *</span></label>
                                <select id="sent_for" name="sent_for" class="form-control">
                                    <option  value="1">Quotation</option>
                                    
                                    
                                </select>
                            </div>
                            
                            <div class="col-lg-12">
                                <br>
                                <?php echo e(Form::textarea('checking_note',null,['class'=>'form-control','rows'=>5,'cols'=>5,'placeholder'=>'Approval Note....'])); ?>

                            </div>
                            <div class="col-lg-12">
                                <br>

                                <?php if($requisition->new_requisition->isEmpty()): ?>
                                <?php echo e(Form::submit('Approved',['class'=>'btn btn-info'])); ?>

                               <?php endif; ?>
                            </div>


                        </div>

                    </div>
                    <!-- /.invoice -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->

<?php $__env->stopSection(); ?>

<?php $__env->startSection('style'); ?>
    <!-- DataTables -->

    <link rel="stylesheet" href="<?php echo e(asset('plugins/datatables/dataTables.bootstrap4.css')); ?>">
    <link href='https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css' rel='stylesheet' type='text/css'>
    <link href='https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.4/jquery-confirm.min.css' rel='stylesheet' type='text/css'>
    
<?php $__env->stopSection(); ?>

<?php $__env->startSection('plugin'); ?>
    <!-- DataTables -->
    <script src="<?php echo e(asset('plugins/datatables/jquery.dataTables.js')); ?>"></script>
    <script src="<?php echo e(asset('plugins/datatables/dataTables.bootstrap4.js')); ?>"></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js'></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.4/jquery-confirm.min.js"></script>

    <!-- page script -->
    <script type="text/javascript">
        $('.select2').select2();

        function changeItem(requisition_product_id){

            var item_id = $('#item_id_'+requisition_product_id).val();
            console.log(item_id)
            if(item_id > 0){
                window.open('<?php echo e(url('Inventory/change-item-for-requisition')); ?>/'+requisition_product_id+'&'+item_id,'_parent');
            }
           
        }


        function toggleSupplier(){
            var sent_for = $('#sent_for').val();
            if(sent_for == 1){
                $('#suppliers').hide();
            }else{
                $('#suppliers').show();
            }
        }

        function Delete(id) {
            $.confirm({
                title: 'Confirm!',
                content: '<hr><strong class="text-danger">Are you sure to delete ?</strong><hr>',
                buttons: {
                    confirm: function () {
                        $.ajax({
                          url: "<?php echo e(url('Inventory/delete-individual-requisition')); ?>/"+id,
                          type: 'GET',
                          dataType: 'json',
                          success:function(response) {
                            if(response.success){
                              $('#tr-'+id).fadeOut();
                            }else{
                              $.alert({
                                title:"Whoops!",
                                content:"<hr><strong class='text-danger'>This product has PO, It can not be deleted!</strong><hr>",
                                type:"red"
                              });
                            }
                          }
                        });
                    },
                    cancel: function () {

                    }
                }
            });   
          }

    </script>
    <!-- Script -->
    <script src='https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js'></script>




    <script type="text/javascript">
        function selectAll(){
            var items=document.getElementsByClassName("example");
            for(var i=0; i<items.length; i++){
                if(items[i].type=='checkbox')
                    items[i].checked=true;
            }
        }
        
        
        function UnSelectAll(){
            var items=document.getElementsByClassName("example");
            for(var i=0; i<items.length; i++){
                if(items[i].type=='checkbox')
                    items[i].checked=false;
            }
        }			
     </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.fixed', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/welluugs/purchase.wellgrouperp.com/resources/views/inventory/requisition/invoice-requisition.blade.php ENDPATH**/ ?>