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

    <div class="page-pos_clothess">
        <div class="preview">
            <div class="col-sm-12 col-md-12 ">
                <div class="" style="">
                    <h3 class="pull-left"><b><?php echo e(__('administrator.edit_sale')); ?></b></h3>
                </div>
            </div>
            <div class="col-sm-5" style="margin-top: 5px">
                <div class="col-sm-5">
                    <div class="form-group">
                        <?php echo Form::text('barcode', null, array('placeholder' => __('administrator.barcode') ,'class' => 'form-control typeahead', "autocomplete" => "on")); ?>

                    </div>
                </div>
                <div class="col-sm-5">
                    <div class="form-group">
                        <?php echo Form::text('search', null, array('placeholder' => __('administrator.search') ,'class' => 'form-control search')); ?>

                    </div>
                </div>
                <div class="col-sm-2">
                    <div class="form-group">
                        <button type="button" class="button_info btn_search_pos"><?php echo e(__('administrator.search')); ?></button>
                    </div>
                </div>
                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                    <div class="switch-field">
                    <?php
                    $count_member_type = 0;
                    foreach (getMemberType() as $id => $item) {
                        echo "
                         <input type='radio' id='radio-$id' name='member_type' value='$id' " . ($count_member_type === 0 ? 'checked' : '') . " />
                         <label for='radio-$id'>$item</label>
                        ";
                        $count_member_type++;
                    }
                    ?>
                    </div>
                </div>
                <div class="product-item-list">
                    <div class="item-list">
                        <div class="row my-product-list">

                        </div>
                    </div>
                </div>
            </div>
            <?php
                $currency = findCurrency($sale->currency_id);
                $symbol = $currency?$currency->symbol:'';
            ?>
            <?php echo Form::model($sale, ['method' => 'PATCH','route' => ['all-sale-history.update', $sale->id],'enctype'=>'multipart/form-data','id'=>'submit_post','class'=>'jform']); ?>

            <div class="col-sm-7">
                <div class="check-bill">
                    <div class="row">
                        <div class="check-bill-table">
                            <div class="product-view-table">
                                <div class="parent-box-pos">
                                    <table class="table table-hover item-list-tb ">
                                        <thead class="text-center">
                                        <tr>
                                            <th>#</th>
                                            <th><?php echo e(__('administrator.item_name')); ?></th>
                                            <th><?php echo e(__('administrator.qty')); ?> <span class="red">*</span></th>
                                            <th><?php echo e(__('administrator.price')); ?><span class="red">*</span></th>
                                            <th><?php echo e(__('administrator.total')); ?></th>
                                            <th width="6%"><?php echo e(__('administrator.action')); ?></th>
                                        </tr>

                                        </thead>
                                        <tbody id="append_product">
                                        <?php $__currentLoopData = getSaleDetail($sale->id); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <?php
                                                $product_unit = findProductUnitByCode($row->barcode,'product');
                                                $product = getProductById($row->product_id);
                                                $stock = \App\Model\stock_inventory\StockModel::where(['warehouse_id'=>$sale->warehouse_id,'barcode'=>$row->barcode,'type'=>'product'])->first();

                                                $amount = $row->sale_price*$row->qty;

                                            $size = findSize($row->size_code);
                                            $color = findColor($row->color_code);
                                            ?>
                                            <tr data-symbol="<?php echo e($symbol); ?>">
                                                <td>
                                                    <input type="hidden" class="product_unit"
                                                           value="<?php echo e($product_unit->id??''); ?>" name="product_unit[]">
                                                    <input type="hidden" class="sale_detail??''" value="<?php echo e($row->id??''); ?>"
                                                           name="sale_detail[]">
                                                    <?php echo e($key+1); ?></td>
                                                <td><?php echo e($row->barcode); ?><br><?php echo e($product->product_name??''); ?>

                                                    (<?php echo e($size?$size->size:''); ?>,<?php echo e($color?$color->name:''); ?>)
                                                </td>
                                                <td>
                                                    <input type="number" value="<?php echo e($row->qty); ?>"
                                                           data-old_qty="<?php echo e($row->qty); ?>"
                                                           class="form-control input-sm qty number-only" name="qty[]"
                                                           required/>
                                                    <input type="hidden" value="<?php echo e($stock->stock_qty??0); ?>" class="stock"
                                                           name="stock[]"/>
                                                </td>
                                                <td><input type="text" value="<?php echo e($row->sale_price); ?>"
                                                           class="form-control input-sm retail_price number-only "
                                                           required name="retail_price[]"/></td>
                                                <td class="text-center"><span
                                                        class="total_item"><?php echo e($amount); ?> <?php echo e($symbol); ?></span><input
                                                        type="hidden" class="amount-hidden" name="amount_hidden"
                                                        value="<?php echo e($amount); ?>"></td>
                                                <td class="text-center"><span class="fa fa-times text-danger fa-2x  cursor remove_row"></span>
                                                </td>
                                            </tr>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="blog-payment">
                        <div class="col-sm-7">
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-6">
                                        <label class="required"><?php echo e(__('administrator.customer')); ?> </label>
                                        <?php
                                            $customer = null;
                                            if($sale->customer_id){
                                                $customer = \App\Model\people\CustomerModel::where('id',$sale->customer_id)->pluck('name','id');
                                            }
                                        ?>
                                        <?php echo Form::select('customer',$customer?$customer:[],null , ['class' => 'form-control input-sm search_customer']); ?>

                                    </div>
                                    <div class="col-sm-6">
                                        <label class="required"><?php echo e(__('administrator.cashier')); ?></label>
                                        <input type="text" name="cashier_name" class="form-control input-sm" readonly=""
                                               value="<?php echo e(findUser($sale->user_id)->name); ?>">
                                    </div>
                                </div>
                            </div>
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-6">
                                        <label><?php echo e(__('administrator.invoice_number')); ?></label>
                                        <input type="text" class=" form-control input-sm"
                                               value="<?php echo e($sale->invoice_number); ?>" readonly="">
                                    </div>
                                    <div class="col-sm-6">
                                        <label class="required"><?php echo e(__('administrator.date')); ?></label>
                                        <input type="text" value="<?php echo e(date('d-m-Y',strtotime($sale->sale_date))); ?>"
                                               class="date_picker form-control input-sm" name="date" readonly>
                                    </div>
                                </div>
                            </div>

                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-4">
                                        <label><?php echo e(__('administrator.total')); ?> ( <?php echo e(__('administrator.'.$currency->code)); ?>

                                            )</label>
                                        <input type="text" class="sub_total form-control input-sm"
                                               value="<?php echo e(0,2); ?> ៛"
                                               readonly="">
                                    </div>
                                    <div class="col-sm-4">
                                        <label><?php echo e(__('administrator.grand_total')); ?>

                                            ( <?php echo e(__('administrator.'.$currency->code)); ?> )</label>
                                        <input type="text" class="g_total form-control input-sm" value="<?php echo e(0); ?> ៛"
                                               readonly="">
                                        <input type="hidden" class="g_total_hidden" name="g_total_hidden"
                                               value="0">
                                    </div>
                                    <div class="col-sm-4">
                                        <label><?php echo e(__('administrator.paid')); ?> ( <?php echo e(__('administrator.'.$currency->code)); ?>

                                            )</label>
                                        <input type="text"
                                               class="form-control input-sm number-only border bg-transparent paid"
                                               name="paid" value="<?php echo e($sale->paid_amount); ?>" placeholder="0.00"
                                               readonly>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <div class="col-sm-5">
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-6">
                                        <label><?php echo e(__('administrator.discount')); ?> (%)</label>
                                        <?php echo Form::text('discount',$sale->discount, array('placeholder' =>__('administrator.discount'),'class' => 'form-control input-sm number-only total-discount')); ?>

                                    </div>
                                    <div class="col-sm-6">
                                        <label><?php echo e(__('administrator.payment_method')); ?></label>
                                        <?php echo Form::select('payment_method_id', getPaymentMethod() , null , ['placeholder'=>__('administrator.please_select'),'class' => 'form-control']); ?>

                                    </div>
                                </div>
                            </div>
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-6">
                                        <label><?php echo e(__('administrator.reference')); ?></label>
                                        <input type="file" name="reference_file" class="form-control">
                                    </div>
                                    <div class="col-sm-6 ">
                                        <label><?php echo e(__('administrator.delivery_fee_by')); ?></label>
                                        <?php echo Form::select('delivery_fee_by', getFeeBy() , null , ['placeholder'=>__('administrator.please_select'),'class' => 'form-control']); ?>

                                    </div>
                                </div>
                            </div>
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-6">
                                        <label><?php echo e(__('administrator.additional_charge')); ?>

                                            ( <?php echo e(__('administrator.'.$currency->code)); ?> )</label>
                                        <?php echo Form::text('additional_charge', $sale->additional_charge , ['placeholder'=>__('administrator.additional_charge'),'class' => 'form-control']); ?>

                                    </div>
                                    <div class="col-sm-6">
                                        <label><?php echo e(__('administrator.refund_amount')); ?>

                                            ( <?php echo e(__('administrator.'.$currency->code)); ?> )</label>
                                        <?php echo Form::text('refund_amount',$sale->refund_amount, array('placeholder' =>__('administrator.refund_amount'),'class' => 'form-control input-sm number-only refund_amount')); ?>

                                    </div>
                                </div>
                            </div>
                            <div class="list-box display-none">
                                <div class="list">
                                    <div class="col-sm-12">
                                        <label><?php echo e(__('administrator.delivery_fee')); ?>

                                            ( <?php echo e(__('administrator.'.$currency->code)); ?> )</label>
                                        <?php echo Form::text('delivery_fee', null , ['placeholder'=>__('administrator.delivery_fee'),'class' => 'form-control']); ?>

                                    </div>
                                </div>
                            </div>
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-12">
                                        <textarea name="refund_reason" rows="2"
                                                  placeholder="<?php echo e(__('administrator.refund_reason')); ?>"
                                                  class="form-control no-resize"></textarea>
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-12">
                                <div class="button_payment">
                                    <button type="submit"
                                            class="button_info button_lg pull-right"><?php echo e(__('administrator.submit')); ?></button>
                                    <button type="button"
                                            class="button_danger btn_clear button_lg"><?php echo e(__('administrator.remove')); ?></button>
                                    <button type="button" class="button_warning button_lg"
                                            onclick="window.location.replace('<?php echo e(route('all-sale-history.index')); ?>')"><?php echo e(__('administrator.back')); ?></button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php echo Form::close(); ?>

            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
    <script type="text/javascript">
        let tb = $('#append_product');
        $(document).ready(function () {
            get_total_all();
            search_product();
            $('input[name=search]').keypress(function (e) {
                // e.preventDefault();
                var key = e.which;
                if (key === 13)  // the enter key code
                {
                    search_product();
                }
            });
            $('.btn_search_pos').on('click', function () {
                search_product();
            });
            $(document).on('click', 'a.page-link', function (e) {
                e.preventDefault();
                let url = $(this).attr('href');
                search_product(url);
            });
            $(document).on('input', 'input[name=member_type]', function (e) {
                tb.html('');
                get_total_all();
                search_product();
            });
        });

        $(".typeahead").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "<?php echo e(Route('pos-scan-code')); ?>",
                    dataType: "json",
                    method: "get",
                    data: {
                        warehouse: '<?php echo e($sale->warehouse_id); ?>',
                        barcode: request.term,
                    },
                    success: function (rows) {
                        let check = 0;
                        let table = $('.product_unit');
                        let id = rows.data.id;
                        let name = rows.data.name;
                        let stock = rows.data.stock;
                        let size = rows.data.size;
                        let color = rows.data.color;
                        let code = rows.data.code;
                        let price = rows.data.price;
                        let symbol = rows.data.symbol;
                        let count = $('#append_product tr').length;
                        let $html = `
                        <tr data-symbol="` + symbol + `">
                            <td>
                            <input type="hidden" class="product_unit" value="` + id + `" name="product_unit[]">
                            <input type="hidden" class="sale_detail" value="0" name="sale_detail[]">
                            <input type="hidden" class="currency_id"  name="currency_id[]">
                            ` + (count + 1) + `</td>
                            <td>${code}<br>${name} ( ${size},${color} )</td>
                            <td>
                                <input type="number" value="1" class="form-control input-sm qty number-only" name="qty[]" required/>
                                <input type="hidden" value="` + stock + `" class="stock" name="stock[]"/>
                            </td>
                            <td><input type="text"  value="` + price + `"  class="form-control input-sm retail_price number-only " required name="retail_price[]" /></td>
                            <td class="text-center"><span class="total_item">` + (price) + symbol + `</span><input type="hidden" class="amount-hidden" name="amount_hidden" value="` + (price) + `"></td>
                            <td class="text-center"><span class="fa fa-times text-danger fa-2x  cursor remove_row"></span></td>
                        </tr>
                    `;

                        if (table.length > 0) {
                            $.each(table, function (i, v) {
                                if (parseFloat(v.value) === parseFloat(id)) {
                                    let qty = $(this).closest('tr').find('.qty').val();
                                    let old_qty = $(this).closest('tr').find('.qty').data('old_qty');
                                    old_qty = old_qty ? parseFloat(old_qty) : 0;
                                    stock = stock + old_qty;
                                    let add_mor = parseFloat(qty) + 1;

                                    if (parseFloat(add_mor) > parseFloat(stock)) {
                                        add_mor = stock;
                                        $.alert('Out of Stock');
                                    }
                                    $(this).closest('tr').find('.qty').val(add_mor);
                                    gen_item_total($(this));
                                    check++;
                                }
                            });
                            if (check === 0 && id) {
                                $('#append_product').append($html);
                            }
                        } else {
                            if (id) {
                                $('#append_product').append($html);
                            }
                        }
                        get_total_all();
                        $(".typeahead").val('');
                        $(".typeahead").focus();
                    }
                });
            },
            minLength: 3,
        });

        function search_product(url = "<?php echo e(route('pos.create')); ?>") {
            let barcode = $('.search').val();
            let branch_id = '<?php echo e($sale->branch_id); ?>';
            branch_id = branch_id?branch_id:$('select[name=branch_id]').val();
            let member_type = $('input[name=member_type]:checked').val();
            $.ajax({
                url: url,
                method: "get",
                data: {
                    barcode,
                    branch_id,
                    member_type,
                },
                success: function (response) {
                    $('.my-product-list').html(response.html);
                }
            });
        }

        $(document).on('click', '.listings', function () {
            let id = $(this).data('id');
            let check = 0;
            let table = $('.product_unit');
            let name = $(this).data('name');
            let stock = $(this).data('stock');
            let size = $(this).data('size');
            let color = $(this).data('color');
            let code = $(this).data('code');
            let price = $(this).data('price');
            let symbol = $(this).data('symbol');
            let count = $('#append_product tr').length;
            let $html = `
                        <tr data-symbol="` + symbol + `">
                            <td>
                            <input type="hidden" class="product_unit" value="` + id + `" name="product_unit[]">
                            <input type="hidden" class="sale_detail" value="0" name="sale_detail[]">
                            ` + (count + 1) + `</td>
                            <td>${code}<br>${name} ( ${size},${color} )</td>
                            <td>
                                <input type="number" value="1" class="form-control input-sm qty number-only" name="qty[]" required/>
                                <input type="hidden" value="` + stock + `" class="stock" name="stock[]"/>
                            </td>
                            <td><input type="text"  value="` + price + `"  class="form-control input-sm retail_price number-only " required name="retail_price[]" /></td>
                            <td class="text-center"><span class="total_item">` + (price) + symbol + `</span><input type="hidden" class="amount-hidden" name="amount_hidden" value="` + (price) + `"></td>
                            <td class="text-center"><span class="fa fa-times text-danger fa-2x  cursor remove_row"></span></td>
                        </tr>

                    `;
            if (table.length > 0) {
                $.each(table, function (i, v) {
                    if (parseFloat(v.value) === parseFloat(id)) {
                        let qty = $(this).closest('tr').find('.qty').val();
                        let old_qty = $(this).closest('tr').find('.qty').data('old_qty');
                        old_qty = old_qty ? parseFloat(old_qty) : 0;
                        qty = qty ? parseFloat(qty) : 0;
                        stock = stock + old_qty;
                        let add_mor = parseFloat(qty) + 1;
                        if (parseFloat(add_mor) > parseFloat(stock)) {
                            add_mor = stock;
                            $.alert('Out of Stock');
                        }
                        $(this).closest('tr').find('.qty').val(add_mor);
                        gen_item_total($(this));
                        check++;
                    }
                });
                if (check === 0 && id) {
                    $('#append_product').append($html);
                }
            } else {
                if (id) {
                    $('#append_product').append($html);
                }
            }
            get_total_all();
        });

        $(document).on('click', '.remove_row', function () {
            $(this).closest('tr').remove();
            get_total_all();
        });

        $(document).on('keyup', '.number-only', function () {
            gen_item_total($(this));
        });

        $(document).on('keyup', 'input[name=additional_charge]', function () {
            get_total_all();
        });
        $(document).on('keyup', 'input[name=delivery_fee]', function () {
            get_total_all();
        });
        $(document).on('change', 'input[name=delivery_fee_by]', function () {
            get_total_all();
        });

        function gen_item_total(this_) {
            let qty = this_.closest('tr').find('.qty').val();
            let price = this_.closest('tr').find('.retail_price').val();
            let disc = this_.closest('tr').find('.disc').val();
            let stock = this_.closest('tr').find('.stock').val();
            let old_qty = this_.closest('tr').find('.qty').data('old_qty');
            old_qty = old_qty ? parseFloat(old_qty) : 0;
            stock = stock ? parseFloat(stock) : 0;
            stock = stock + old_qty;
            console.log(stock);
            if (parseFloat(qty) > parseFloat(stock)) {
                qty = stock;
                this_.closest('tr').find('.qty').val(qty);
                $.alert('Out of Stock');
            }
            disc = disc ? disc : 0;
            let symbol = this_.closest('tr').data('symbol');
            let stotal = qty * price;
            let total = stotal - (stotal * disc / 100);
            this_.closest('tr').find('.subtotal_item').text(stotal + '' + symbol);
            this_.closest('tr').find('.total_item').text(total + '' + symbol);
            this_.closest('tr').find('.amount-hidden').val(total);
            get_total_all();
        }

        function get_total_all() {
            let total = 0;
            let discount = parseFloat($('input[name=discount]').val());
            let delivery_fee = ($('input[name=delivery_fee]').val());
            delivery_fee = delivery_fee ? parseFloat(delivery_fee) : 0;
            let delivery_fee_by = ($('select[name=delivery_fee_by]').val());
            delivery_fee_by = delivery_fee_by ? parseFloat(delivery_fee_by) : 0;
            discount = discount ? discount : 0;
            $('.amount-hidden').each(function () {
                let value_total = parseFloat($(this).val());
                total += parseFloat(value_total);
            });
            let g_total = total - (total * discount / 100);
            if (delivery_fee_by === 2) {
                g_total += delivery_fee;
            }
            $('.g_total').val(parseFloat(g_total).toFixed(2));
            $('.g_total_hidden').val(parseFloat(g_total));
            $('.sub_total').val(parseFloat(total).toFixed(2));
        }

        $('.total-discount').on('keyup', function () {
            get_total_all();
        });
        $(document).on('click', '.pos_clothes_remove_session ', function () {
            $.confirm({
                title: 'Are you sure ?',
                theme: 'modern',
                buttons: {
                    cancel: {
                        btnClass: 'button_warning',
                        text: 'Cancel'
                    },
                    ok: {
                        btnClass: 'button_primary',
                        text: 'OK',
                        action: function () {
                            $('#append_product').html('');
                            get_total_all();
                        }
                    }
                }
            });
        });
    </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/pos/all-sale-list/edit.blade.php ENDPATH**/ ?>