<?php $__env->startSection('content'); ?>
    <?php Fun::lang()?>

    <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.report_stock_item')); ?></h3>
                            </div>
                            <div class="pull-right">
                                <?php if(myBranch()->main): ?>
                                    <?php echo $__env->make('administrator.report-stock.sub-menu', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
                                <?php endif; ?>
                            </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>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3">
                                    <?php echo e(__('administrator.warehouse')); ?>

                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    <?php echo Form::select('warehouse', $warehouses, null, array('class' => 'form-control select2', 'placeholder' =>  __('administrator.please_select_warehouse'))); ?>

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

                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    <?php echo Form::select('product_id', [], null, array('class' => 'form-control select2 search_product', 'placeholder' =>  __('administrator.please_select'))); ?>

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

                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    <?php echo Form::select('qty_alert', ['qty_alert'=>__('administrator.qty_alert')], null, array('class' => 'form-control select2', 'placeholder' =>  __('administrator.please_select'))); ?>

                                </div>
                            </div>
                            <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.report_stock')); ?>"><i class="fa fa-print"></i> <?php echo e(__('administrator.print')); ?></button>
                                <a class="button_info pull-right save_to_excel"><i class="fa fa-file-excel-o" aria-hidden="true"></i> Save to excel</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="wrapper-table">
                <table id="print_table" class="table table-striped table-hover  ">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th><?php echo e(__('administrator.warehouse')); ?></th>
                        <th><?php echo e(__('administrator.barcode')); ?></th>
                        <th><?php echo e(__('administrator.item_name')); ?></th>
                        <th><?php echo e(__('administrator.import_qty')); ?></th>
                        <th><?php echo e(__('administrator.transfers_stock')); ?></th>
                        <th><?php echo e(__('administrator.request_stock')); ?></th>
                        <th><?php echo e(__('administrator.export_qty')); ?></th>
                        <th><?php echo e(__('administrator.production')); ?></th>
                        <th><?php echo e(__('administrator.adjustment')); ?></th>
                        <th><?php echo e(__('administrator.sale_return')); ?></th>
                        <th><?php echo e(__('administrator.balance_qty')); ?></th>
                        <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('Stock Total Cost')): ?>
                            <th><?php echo e(__('administrator.total_cost')); ?></th>
                        <?php endif; ?>
                    </tr>
                    </thead>
                    <tbody class="tbody">
                    </tbody>
                    <tr class="tfoot">
                        <th></th>
                        <th></th>
                        <th></th>
                        <th class="text-right"><?php echo e(__('administrator.total')); ?></th>
                        <th>0</th>
                        <th>0</th>
                        <th>0</th>
                        <th>0</th>
                        <th>0</th>
                        <th>0</th>
                        <th>0</th>
                        <th>0</th>
                        <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('Stock Total Cost')): ?>
                            <th>0</th>
                        <?php endif; ?>
                    </tr>
                </table>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
    <script type="text/javascript">
        function getTable(warehouse = '', product_id = '', qty_alert = '') {
            $('#print_table').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: false,
                serverSide: true,
                lengthMenu: tbLeng,
                ajax: {
                    url: '<?php echo e(route('report.stock-item')); ?>',
                    data: {
                        warehouse,
                        product_id,
                        qty_alert,
                    },
                },
                dom: 'lBfrtip',
                buttons: [
                    {
                        extend: 'excelHtml5',
                        className: 'btn-save-excel',
                        title:'Report-Stock-Item',
                        text: 'Save as Excel',
                        footer: true,
                        header: true,

                    },
                ],
                columns:
                    [
                        {data: 'DT_RowIndex', 'searchable': false},
                        {data: 'warehouse', 'searchable': true},
                        {data: 'barcode', 'name': 'nso007_stocks.barcode'},
                        {data: 'product_name', 'name': 'nso007_products.product_name'},
                        {data: 'in_qty'},
                        {data: 'transfer_stock'},
                        {data: 'request_stock'},
                        {data: 'out_qty'},
                        {data: 'production'},
                        {data: 'adjustment'},
                        {data: 'sale_return'},
                        {data: 'stock_qty'},
                            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('Stock Total Cost')): ?>
                        {
                            data: 'total_cost'
                        },
                        <?php endif; ?>
                    ],
                "fnDrawCallback": function () {
                    let tr = $('.tbody tr');
                    let $total_in = 0,
                        $total_ex = 0,
                        $total_production = 0,
                        $total_return = 0,
                        $total_stock = 0,
                        $total_tran_in = 0,
                        $total_tran_out = 0,
                        $total_request_to = 0,
                        $total_request_from = 0,
                        $total_add = 0,
                        $total_minus = 0,
                        $total_cost_riel = 0,
                        $total_cost_usd = 0;
                    let findFRow = function (index, value) {
                        $('#print_table').find(`tr.tfoot:first-child th:nth-child(${index})`).html(value);
                    }
                    let getBRow = function (this_, index, selector = '') {
                        let qty = 0;
                        if (selector) {
                            qty = this_.find(`td:nth-child(${index}) ${selector}`).data('qty')
                        } else {
                            qty = this_.find(`td:nth-child(${index})`).text()
                        }
                        return toFloat(qty ? qty : 0)
                    }

                    $.each(tr, function (ii, vv) {
                        let t = $(this)
                        $total_in += getBRow(t, 5)
                        $total_tran_in += getBRow(t, 6, '.text-success')
                        $total_tran_out += getBRow(t, 6, '.text-danger')
                        $total_request_to += getBRow(t, 7, '.text-success')
                        $total_request_from += getBRow(t, 7, '.text-danger')
                        $total_ex += getBRow(t, 8)
                        $total_production += getBRow(t, 9)
                        $total_add += getBRow(t, 10, '.text-success')
                        $total_minus += getBRow(t, 10, '.text-danger')
                        $total_return += getBRow(t, 11)
                        $total_stock += getBRow(t, 12)

                        $total_cost_riel += toFloat(t.find('.cost_riel').text())
                        $total_cost_usd += toFloat(t.find('.cost_usd').text())
                    });

                    findFRow(5, $total_in)
                    findFRow(6, `<span class="text-success">ចូល: </span>${$total_tran_in} | <span class="text-danger">ចេញ: </span>${$total_tran_out}`)
                    findFRow(7, `<span class="text-success">បានស្នើសុំ: </span> ${$total_request_to} | <span class="text-danger">បានចេញ: </span>${$total_request_from}`)
                    findFRow(8, $total_ex)
                    findFRow(9, $total_production)
                    findFRow(10, `<span class="text-success">ថែម: </span>${$total_add} | <span class="text-danger">ថយ </span>${$total_minus}`)
                    findFRow(11, $total_return)
                    findFRow(12, $total_stock)
                    findFRow(13, `${$total_cost_riel.toFixed(2)} ៛<br>${$total_cost_usd.toFixed(2)} $`)
                },
            });
        }

        $(document).ready(function () {
            getTable();
            $('.select2').on('change', function () {
                $('#print_table').DataTable().destroy();
                let warehouse = $('select[name=warehouse]').val();
                let product_id = $('select[name=product_id]').val();
                let qty_alert = $('select[name=qty_alert]').val();
                getTable(warehouse, product_id, qty_alert);
            });
            $('#print_table').on('click', '.show-stock-in', function () {
                let url = $(this).data('url');
                getHtmlFormShow(url, '1200px');
            });
        });
    </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/stock-item.blade.php ENDPATH**/ ?>