<?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.income')); ?></h3>
                            </div>
                            <div class="pull-right">
                                <?php echo $__env->make('report.income.sub-menu-income', \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 paddin-0">
                        <div>
                            <form class="form_data">
                                <div class="col-sm-3 col-md-3">
                                    <div class="col-sm-3 col-md-3" style="margin-top: 5px">
                                        <?php echo e(__('administrator.from_date')); ?>

                                    </div>
                                    <div class="form-group col-sm-9 col-md-9">
                                        <?php echo Form::text('from_date',null, array('placeholder' => __('administrator.from_date'),'class' => 'form-control date_picker','autocomplete' => 'off','')); ?>

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

                                    </div>
                                    <div class="form-group col-sm-9 col-md-9">
                                        <?php echo Form::text('to_date',  null, array('placeholder' => __('administrator.to_date'),'class' => 'form-control date_picker','autocomplete' => 'off','')); ?>

                                    </div>
                                </div>
                                <div class="col-sm-3 col-md-3">
                                    <div class="col-sm-3 col-md-3" style="margin-top: 5px">
                                        <?php echo e(__('administrator.branch')); ?>

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

                                    </div>
                                </div>
                                <div class="col-sm-3 col-md-3">
                                    <div class="col-sm-3 col-md-3" style="margin-top: 5px">
                                        <?php echo e(__('administrator.seller')); ?>

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

                                    </div>
                                </div>
                            </form>
                            <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_sale_detail')); ?>"><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" width="100%" class="table table-striped table-hover ">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th><?php echo e(__('administrator.sale_date')); ?></th>
                        <th><?php echo e(__('administrator.seller')); ?></th>
                        <th><?php echo e(__('administrator.branch')); ?></th>
                        <th><?php echo e(__('administrator.invoice_number')); ?></th>
                        <th><?php echo e(__('administrator.grand_total')); ?></th>
                        <th><?php echo e(__('administrator.total_cost')); ?></th>
                        <th><?php echo e(__('administrator.delivery_fee')); ?></th>
                        <th><?php echo e(__('administrator.profit')); ?></th>
                    </tr>
                    </thead>
                    <tbody class="tbody"></tbody>
                    <tfoot class="tfoot">
                    <tr>
                        <th class="text-right" colspan="5"><?php echo e(__('administrator.total')); ?></th>
                        <th class="green f_grand_total"></th>
                        <th class="green f_total_cost"></th>
                        <th class="green f_delivery_fee"></th>
                        <th class="green f_total_profit"></th>
                    </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('script'); ?>

    <script type="text/javascript">
        $(document).ready(function () {
            dataTableFilter();
            $('.form_data input,.form_data select').on('change', function () {
                dataTableFilter();
            });
        });

        function dataTableFilter() {
            $('#print_table').DataTable().destroy();
            $('#print_table').DataTable({
                ordering: false,
                serverSide: true,
                lengthMenu: tbLeng,
                ajax: {
                    url: "<?php echo e(route('report-income-by-sale')); ?>?" + $('.form_data').serialize(),
                },
                columns:
                    [
                        {data: 'DT_RowIndex', 'searchable': false},
                        {data: 'sale_date', 'searchable': false},
                        {data: 'user_id', 'searchable': false},
                        {data: 'branch_id', 'searchable': false},
                        {data: 'invoice_number', 'searchable': false},
                        {data: 'grand_total', 'searchable': false},
                        {data: 'total_cost', 'searchable': false},
                        {data: 'delivery_fee', 'searchable': false},
                        {data: 'profit', 'searchable': false},
                    ],
                "fnDrawCallback": function () {
                    let tr = $('.tbody .row_data');
                    let total_cost_riel = 0;
                    let total_cost_usd = 0;

                    let total_profit_riel = 0;
                    let total_profit_usd = 0;

                    let deliver_fee_riel = 0;
                    let deliver_fee_usd = 0;

                    let grand_total_riel = 0;
                    let grand_total_usd = 0;

                    $.each(tr, function (ii, vv) {
                        let row = $(this);
                        let grand_total = toFloat(row.data('grand_total'));
                        let total_cost = toFloat(row.data('total_cost'));
                        let delivery_fee = toFloat(row.data('delivery_fee'));
                        let total_profit = toFloat(row.data('total_profit'));
                        let currency_id = toFloat(row.data('currency_id'));


                        if (currency_id === 1) {
                            total_cost_riel += total_cost;
                            total_profit_riel += total_profit;
                            deliver_fee_riel += delivery_fee;
                            grand_total_riel += grand_total;
                        }
                        if (currency_id === 2) {
                            total_cost_usd += total_cost;
                            total_profit_usd += total_profit;
                            deliver_fee_usd += delivery_fee;
                            grand_total_usd += grand_total;
                        }
                        console.log(delivery_fee)
                    });
                    let tc_r = total_cost_riel ? ('៛' + addComma(total_cost_riel.toFixed(2))) : '';
                    let tc_u = total_cost_usd ? ('$' + addComma(total_cost_usd.toFixed(2))) : '';

                    let tp_r = total_profit_riel ? ('៛' + addComma(total_profit_riel.toFixed(2))) : '';
                    let tp_u = total_profit_usd ? ('$' + addComma(total_profit_usd.toFixed(2))) : '';

                    let td_r = deliver_fee_riel ? ('៛' + addComma(deliver_fee_riel.toFixed(2))) : '';
                    let td_u = deliver_fee_usd ? ('$' + addComma(deliver_fee_usd.toFixed(2))) : '';

                    let tg_r = grand_total_riel ? ('៛' + addComma(grand_total_riel.toFixed(2))) : '';
                    let tg_u = grand_total_usd ? ('$' + addComma(grand_total_usd.toFixed(2))) : '';


                    $('.f_total_cost').html(tc_r+(tc_r&&tc_u?'<br>':'')+tc_u);
                    $('.f_grand_total').html(tg_r+(tg_r&&tg_u?'<br>':'')+tg_u);
                    $('.f_delivery_fee').html(td_r+(td_r&&td_u?'<br>':'')+td_u);
                    $('.f_total_profit').html(tp_r+(tp_r&&tp_u?'<br>':'')+tp_u);

                },
            });
        }

        $(document).on('click', '#search', function () {
            dataTableFilter();
        });
    </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/report/sale-report/report-income.blade.php ENDPATH**/ ?>