
<?php $__env->startSection('content'); ?>
    <?php
    Fun::lang();
    $arr_wh = [];
    $arr_code = [];

    $from_date = @$_GET['from_date'] ? $_GET['from_date'] : \Carbon\Carbon::now()->startOfMonth()->toDateString();
    $to_date = @$_GET['to_date'] ? $_GET['to_date'] : \Carbon\Carbon::now()->EndOfMonth()->toDateString();
    $from_date = date('Y-m-d',strtotime($from_date));
    $to_date = date('Y-m-d',strtotime($to_date));
    $pur_warehouse = DB::table('nso007_purchases')
        ->where('type', 'product')
        ->where(function ($query) use ($from_date, $to_date) {
            if ($from_date && $to_date) {
                $query->whereBetween('nso007_purchases.purchase_date', [date('Y-m-d', strtotime($from_date)) . ' 00:00:00', date('Y-m-d', strtotime($to_date)) . ' 23:59:59']);
            }
        })
        ->groupby('warehouse_id')
        ->pluck('warehouse_id');
    $pur_warehouse = DB::table('nso007_warehouse')->whereIn('id', $pur_warehouse)->pluck('warehouse_name', 'id');


    $tran_warehouse = DB::table('nso007_transfer_stock')
        ->where('type', 'item')
        ->where(function ($query) use ($from_date, $to_date) {
            if ($from_date && $to_date) {
                $query->whereBetween('nso007_transfer_stock.date', [$from_date, $to_date]);
            }
        })
        ->groupby('to_warehouse_id')
        ->pluck('to_warehouse_id');
    $tran_warehouse = DB::table('nso007_warehouse')->whereIn('id', $tran_warehouse)->pluck('warehouse_name', 'id');

    $pur_code = DB::table('nso007_purchase_details')
        ->join('nso007_purchases', 'nso007_purchases.id', 'nso007_purchase_details.purchase_id')
        ->where(function ($query) use ($from_date, $to_date) {
            if ($from_date && $to_date) {
                $query->whereBetween('nso007_purchases.purchase_date', [$from_date . ' 00:00:00', $to_date . ' 23:59:59']);
            }
        })
        ->where('nso007_purchases.type', 'product')
        ->groupBy('nso007_purchase_details.barcode')
        ->pluck('nso007_purchase_details.barcode');
    $tran_code = DB::table('nso007_transfer_stock_detail')
        ->join('nso007_transfer_stock', 'nso007_transfer_stock.id', 'nso007_transfer_stock_detail.transfer_id')
        ->where(function ($query) use ($from_date, $to_date) {
            if ($from_date && $to_date) {
                $query->whereBetween('nso007_transfer_stock.date', [$from_date, $to_date]);
            }
        })
        ->where('nso007_transfer_stock.type', 'item')
        ->groupBy('nso007_transfer_stock_detail.barcode')
        ->pluck('nso007_transfer_stock_detail.barcode');
    foreach ($pur_warehouse as $key => $row) {
        $arr_wh[$key] = $row;
    }
    foreach ($tran_warehouse as $key => $row) {
        $arr_wh[$key] = $row;
    }
    foreach ($pur_code as $key => $row) {
        $arr_code[$key] = $row;
    }
    foreach ($tran_code as $key => $row) {
        $arr_code[$key] = $row;
    }
    $arr_total_item = [];
    ?>
    <div class="page">
        <div class=container-fluid>
            <div class="top-content">
                <div class="row">
                    <div class="col-sm-12 col-xs-12 form-group">
                        <div class="title-add">
                            <div class="pull-left">
                                <h3><?php echo e(__('administrator.stock_in_item_by_branch_report')); ?></h3>
                            </div>
                            <div class="pull-right">
                                <?php echo $__env->make('administrator.report-stock.sub-menu', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="top-content">
                <div class="row">
                    <div class="col-sm-12 col-xs-12 form-group padding-0">
                        <div>
                            <?php echo Form::open(['method'=>'get']); ?>

                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3">
                                    <?php echo e(__('administrator.from_date')); ?>

                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    <?php echo Form::text('from_date', date('d-m-Y',strtotime($from_date)), array('class' => 'form-control date_picker', 'placeholder' =>  __('administrator.from_date'))); ?>

                                </div>
                            </div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3">
                                    <?php echo e(__('administrator.to_date')); ?>

                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    <?php echo Form::text('to_date', date('d-m-Y',strtotime($to_date)), array('class' => 'form-control date_picker', 'placeholder' =>  __('administrator.to_date'))); ?>

                                </div>
                            </div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3">
                                    <button class="button_info"><?php echo e(__('administrator.search')); ?></button>
                                </div>
                            </div>
                            <?php echo Form::close(); ?>


                            <div class="col-sm-offset-6 col-sm-6">
                                <button class=" button_success pull-right print_button" type="button"
                                        data-text="<?php echo e(__('administrator.stock_in_item_by_branch_report')); ?>"><i
                                            class="fa fa-print"></i> <?php echo e(__('administrator.print')); ?></button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="wrapper-table">
                <table id="print_table_report" class="table table-striped table-hover" width="100%">
                    <thead>
                    <tr>
                        <th rowspan="2">#</th>
                        <th rowspan="2"><?php echo e(__('administrator.barcode')); ?></th>
                        <th rowspan="2"><?php echo e(__('administrator.item_name')); ?></th>
                        <th colspan="<?php echo e(count($arr_wh)); ?>"><?php echo e(__('administrator.warehouse')); ?></th>
                    </tr>
                    <tr>
                        <?php $__currentLoopData = $arr_wh; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <?php
                                $wh = findWarehouse($key);
                                $branch = findBranch($wh->branch_id);
                                $branch = $branch?'( '.$branch->name.' )':''
                            ?>
                            <th><?php echo e($row); ?><br><?php echo e($branch); ?></th>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tr>
                    </thead>
                    <tbody class="tbody">
                    <?php $__currentLoopData = $arr_code; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key_code=>$row_code): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php
                            $product = findProductUnitByCode($row_code,'product');
                            if($product){
                            $product = getProductById($product->product_id);
                            }

                        ?>
                        <tr>
                            <td><?php echo e($key_code+1); ?></td>
                            <td><?php echo e($row_code); ?></td>
                            <td><?php echo e($product?$product->product_name:''); ?></td>
                            <?php $__currentLoopData = $arr_wh; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key_wh=>$row_wh): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php

                                    $purchase_wh_qty = DB::table('nso007_purchases')->where('type', 'product')
                                     ->where(function ($query) use ($from_date, $to_date) {
                                            if($from_date&&$to_date){
                                                $query->whereBetween('nso007_purchases.purchase_date', [$from_date.' 00:00:00',$to_date.' 23:59:59'] );
                                            }
                                    })
                                    ->where('warehouse_id',$key_wh)
                                    ->pluck('id');
                                    $purchase_wh_qty = DB::table('nso007_purchase_details')->selectRaw('SUM(qty) as in_qty')->where('barcode',$row_code)->whereIn('purchase_id',$purchase_wh_qty)->first();

                                    $tran_wh_qty = DB::table('nso007_transfer_stock')
                                    ->where('type', 'item')
                                     ->where(function ($query) use ($from_date, $to_date) {
                                            if($from_date&&$to_date){
                                                $query->whereBetween('nso007_transfer_stock.date', [$from_date,$to_date] );
                                            }
                                    })
                                    ->where('to_warehouse_id',$key_wh)
                                    ->pluck('id');

                                    $tran_wh_qty = DB::table('nso007_transfer_stock_detail')->selectRaw('SUM(qty) as in_qty')->where('barcode',$row_code)->whereIn('transfer_id',$tran_wh_qty)->first();
                                    $arr_total_item[$key_wh] = (@$arr_total_item[$key_wh]?$arr_total_item[$key_wh]:0)+$purchase_wh_qty->in_qty+$tran_wh_qty->in_qty;
                                ?>
                                <td class="data_warehouse" data-id="<?php echo e($key_wh); ?>"
                                    data-qty="<?php echo e($purchase_wh_qty->in_qty+$tran_wh_qty->in_qty); ?>"><?php echo e($purchase_wh_qty->in_qty+$tran_wh_qty->in_qty); ?></td>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                    </tbody>
                    <tfoot>
                    <tr>
                        <th colspan="3" class="text-right"><?php echo e(__('administrator.total')); ?></th>
                        <?php if($arr_total_item): ?>
                            <?php $__currentLoopData = $arr_wh; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <th class="total_item<?php echo e($key); ?>"><?php echo e($arr_total_item[$key]); ?></th>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        <?php endif; ?>
                    </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
    <script type="text/javascript">
        function getTable() {
            $('#print_table_report').DataTable({
                "oLanguage": {
                    "sSearch": "<?php echo e(__('administrator.search')); ?>:",
                    "sLengthMenu": "<?php echo e(__('administrator.display')); ?> _MENU_ <?php echo e(__('administrator.record_per_page')); ?>",
                    "sZeroRecords": "<?php echo e(__('administrator.Nothing_foundsorry')); ?>",
                    "sInfo": "<?php echo e(__('administrator.showing')); ?> _START_ <?php echo e(__('administrator.to')); ?> _END_ <?php echo e(__('administrator.of')); ?> _TOTAL_ <?php echo e(__('administrator.record')); ?>",
                    "sInfoEmpty": "<?php echo e(__('administrator.showing')); ?> 0 <?php echo e(__('administrator.to')); ?> 0 <?php echo e(__('administrator.of')); ?> 0 <?php echo e(__('administrator.record')); ?>",
                    "sInfoFiltered": "(<?php echo e(__('administrator.filter_form')); ?> _MAX_ <?php echo e(__('administrator.total_records')); ?>)",
                    "oPaginate": {
                        "sFirst": "<?php echo e(__('administrator.first')); ?>",
                        "sLast": "<?php echo e(__('administrator.last')); ?>",
                        "sNext": "<?php echo e(__('administrator.next')); ?>",
                        "sPrevious": "<?php echo e(__('administrator.previous')); ?>"
                    },
                },
                ordering: true,
                lengthMenu: [[20, 30, 50, 100, 200, 500, 1000], [20, 30, 50, 100, 200, 500, 1000]],
                "fnDrawCallback": function () {
                    let table = $('#print_table_report tr');
                    let total_item = [];
                    $.each(table, function (ii, vv) {
                        $.each($(this).find('.data_warehouse'), function (jj, uu) {
                            total_item[$(this).data('id')] = (total_item[$(this).data('id')] ? total_item[$(this).data('id')] : 0) + parseFloat($(this).data('qty'));
                        });
                    });
                    <?php $__currentLoopData = $arr_wh; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    $('tfoot tr ').find('.total_item<?php echo e($key); ?>').html(total_item['<?php echo e($key); ?>']);
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                }
            });
        }

        $(document).ready(function () {
            getTable();

        });
    </script>
<?php $__env->stopSection(); ?>




<?php echo $__env->make(branchMenu(), \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/nsowos/public_html/shr-brand/resources/views/administrator/report-stock/new-stock-report/stock-in-item-by-branch.blade.php ENDPATH**/ ?>