<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title><?php echo (ENVIRONMENT !== 'production')?ENVIRONMENT.'-':'*'; ?>POS Module | Stock Manager Advance</title>
        <link rel="stylesheet" href="<?php echo asset_url(); ?>pos/css/perfect-scrollbar.min.css" />
        <script src="<?php echo asset_url(); ?>pos/js/moment.min.js"></script>
        <link rel="stylesheet" href="<?php echo asset_url(); ?>pos/css/bootstrap.min.css">
        <link rel="stylesheet" href="<?php echo asset_url(); ?>pos/plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="<?php echo asset_url(); ?>pos/css/style.css" type="text/css"/>
        <link rel="stylesheet" href="<?php echo asset_url(); ?>css/bar.1.3.css" type="text/css"/>
        <link rel="stylesheet" href="<?php echo asset_url(); ?>pos/css/jquery-ui.css" type="text/css"/>
        <link rel="stylesheet" href="<?php echo asset_url(); ?>pos/plugins/select2/select2.css">
        <!--<link rel="stylesheet" href="<?php echo asset_url(); ?>pos/plugins/iCheck/skins/all.css">-->
        <!--<link rel="stylesheet" href="<?php echo asset_url(); ?>pos/plugins/gritter/css/jquery.gritter.css" type="text/css">-->
        <link rel="stylesheet" href="<?php echo asset_url(); ?>pos/css/print_pos.css" type="text/css" media="print"/>
        <?php $this->load->view("bar/in_file_css") ?>
        <style>
             .srh{
                 float:right;
             }
        </style>
    </head>
    <body>
    <noscript>
    <div class="global-site-notice noscript">
      <div class="notice-inner">
        <p><strong>JavaScript seems to be disabled in your browser.</strong><br>
          You must have JavaScript enabled in
          your browser to utilize the functionality of this website. </p>
      </div>
    </div>
    </noscript>
    <!-- start -->
    <?php $this->load->view("bar/navigation") ?>
    <div class=" panel-scroll" style="height:auto">
      <div class="tabbable tabs-top">
        <div class="tab-content">
          <div id="Sales" class="tab-pane active">
            <div class="table-responsive"> 
              <!-- panel one -->
              <?php $this->load->view("bar/panel_main") ?>
              <!-- end panel one --> 
            </div>
          </div>
            <!-- content here -->
            <?php // $this->load->view("bar/add_product") ?>
          
          <input id="selected_batch" type="hidden" value=""/>
          <!--add customer model-->
          <?php $this->load->view("bar/add_customer") ?>
            <div class="hover_bkgr_fricc">
                <span class="helper"></span>
                <div>
                    <div class="popupCloseButton">&times;</div>
                        <form role="form" class="form-horizontal" id="select_price_form" action="#" autocomplete="off" style="text-align:left">
                              
                        </form>
                </div>
            </div>
          <!--end-->
        </div>
      </div>
    </div>
    <!-- end -->
    <input type="hidden" id="fucking_done" value="0">
    <!-- end pop upbox--> 
    <script type="text/javascript" src="<?php echo asset_url(); ?>plugins/jQuery-lib/2.0.3/jquery.min.js"></script> 
    <script type="text/javascript" src="<?php echo asset_url(); ?>plugins/select2/select2.min.js"></script> 
    <script type="text/javascript" src="<?php echo asset_url(); ?>plugins/bootstrap/js/bootstrap.min.js"></script> 
    <script type="text/javascript" src="<?php echo asset_url(); ?>pos/js/bootbox.min.js"></script> 
    <script type="text/javascript" src="<?php echo asset_url(); ?>pos/js/perfect-scrollbar.min.js"></script> 
    <script type="text/javascript" src="<?php echo asset_url(); ?>pos/js/jquery-ui.min.js"></script>
    <script type="text/javascript" src="<?php echo asset_url(); ?>pos/js/accounting.min.js"></script> 
    <script type="text/javascript" src="<?php echo asset_url(); ?>pos/js/jquery.sendkeys.min.js"></script>
    <script type="text/javascript" src="<?php echo asset_url(); ?>pos/js/bililiteRange.js"></script>
    <script type="text/javascript">
    function add_customer() {
        var fields = $("#create_customer_form").serialize();
        $.post("<?php echo base_url();?>customers/save_customer", fields).done(function(data) {
            var obj = jQuery.parseJSON(data);
            if (obj.status == 0) {
                $('div#error').html('<div class="alert alert-block alert-danger fade in"><button type="button" class="close" data-dismiss="alert">×</button><h4 class="alert-heading"><i class="fa fa-times-circle"></i> Error!</h4>' + obj.validation + '</div>');
            } else {
                $('div#error').html('<div class="alert alert-block alert-success fade in"><button type="button" class="close" data-dismiss="alert">×</button><h4 class="alert-heading"><i class="fa fa-times-circle"></i> Saved Successfully !</h4></div>');
                var data = {
                    id: obj.id,
                    text: $('#cus_name').val() + "/" + $('#cus_phone').val()
                };
                var newOption = new Option(data.text, data.id, true, true);
                $('#poscustomer').append(newOption).trigger('change');
                setTimeout(function() {
                    $('#add_customer_model').modal('hide');
                    $('#add_item').focus();
                }, 2000);
            }
        });
    }
    $('#product-list, #category-list, #subcategory-list, #brands-list').perfectScrollbar({
        suppressScrollX: true
    });
    var jsonarray = <?php print_r($product_list) ?> ;
    var base_url = '<?php echo base_url(); ?>';
    $('#modal-loading').show(); </script>
    <script type = "text/javascript" src = "<?php echo asset_url();?>js/bar.ajax.5.6.js" > </script>
    <script>
    /*function get_cus_tot_credit() {
        return false;
        var cus_id = $('#poscustomer').val();
        if (cus_id != 1) {alert();
            $.post("<?php echo base_url();?>sales/get_cus_tot_credit/" + cus_id).done(function(data) {
                var obj = jQuery.parseJSON(data);
                //$('#totals_cus_credit').html(obj.totals_cus_credit);
                if (obj.previus_total_sale) {
                    $('#previus_total_sale').val(obj.previus_total_sale);
                    $('#customer_outstanding').html(obj.previus_total_sale);
                }
                $('#cus_id').val(obj.cus_id);
                $('#cus_name').val(obj.cus_name);
                $('#cus_phone').val(obj.cus_phone);
                $('#cus_card').val(obj.cus_card);
                $('#add_customer').val('Update');
                $('#cus_add_type').val('E');
            });
        } else {
            $('#customer_outstanding').html('0.00');
        }
    }*/
    
    function validate_cash() {
        $(":focus").each(function() {
            focused = this.id;
        });
        var for_fld = "#" + focused;
        var for_val = $(for_fld).val();
        if (for_val > 9999) {
            $(for_fld).val(0);
            displayNotice("page", " Invalid quantity :  " + for_val);
            $('#add_item').focus();
        }
    }
    
    function cal_amount_btn_click(val) {
        var cash_en_val = 0;
        var cash_en_tot = parseFloat($('#cash_en_tot').val());
        var cash_en_val = parseFloat(val);
        $('#cash_en').val(cash_en_val + cash_en_tot);
        grand_total_cal();
        $('#cash_en_tot').val(cash_en_val + cash_en_tot);
    }
    
    function clear_amount() {
        $(":focus").each(function() {
            focused = this.id;
        });
        var focused_fld = "#" + focused;
        $(focused_fld).val(0);
        grand_total_cal();
        $("#qty_en_tot").val('');
        $("#dis_en_tot").val('');
    }
    
    function cal_btn_click(val) {
        var focused = '';
        $(":focus").each(function() {
            focused = this.id;
        });
        var e = jQuery.Event("keydown");
        e.key = val;
        $('#' + focused).trigger(e);
        grand_total_cal();
    }
    
    function form_submit() {
        var price_type_name = $("input[name='price_type']:checked").attr("lable");
        $('.sale_type').text("Sale:" + price_type_name);
        var cus_id = $('#poscustomer').val();
        if(cus_id == 1 && $("#print_del").prop("checked")){
            bootbox.alert("Select customer for delivery!");
            setTimeout(function(){
                $('body > div.bootbox.modal.fade.bootbox-alert.in > div > div.modal-footer > button').focus().on('click',function(){
                    setTimeout(function(){
                        $('#poscustomer').focus();
                    });
                });
            },200);
            return false;
        }
        if (cus_id == 1 && price_type_name == "credit") {
            bootbox.alert("Select customer for credit bills!");
            return false;
        } else if (cus_id == 1) {
            <?php
            if ($this->session->userdata['ss_user_id'] == 1) {
                ?>
                if (price_type_name == "cash") {
                    if ($('#cash_en').val() == "") {
                        bootbox.alert("Please select customer for half payments ! - 1");
                        return false;
                    } else if (parseFloat($('#cash_en').val()) < parseFloat($('#gtotal').text())) {
                        bootbox.alert("Please select customer for half payments ! - 2");
                        return false;
                    } else {
                        // alert("passed through");    
                    }
                    //return false;
                } <?php
            } else {
                ?>
                if (price_type_name == "cash") {
                    if ($('#cash_en').val() == "") {
                        bootbox.alert("Please select customer for half payments ! - 3");
                        return false;
                    } else if (parseFloat($('#cash_en').val()) < parseFloat($('#gtotal').text())) {
                        bootbox.alert("Please select customer for half payments ! - 4");
                        return false;
                    } else {
                        // alert("passed through");    
                    }
                    //return false;
                } <?php
            } ?>
        }
        
        if (price_type_name == "wholesale") {
            if (parseFloat($('#cash_en').val()) < parseFloat($('#gtotal').text())) {
                bootbox.alert("Please select paying amount!");
                $('#cash_en').focus();
                return false;
            }
        }
        if (price_type_name == "credit" || price_type_name == "wholesale") {
            $('.carrier_details').css('display', 'block');
        }
        // else
        // if(price_type_name == "wholesale"){
        //     $('.carrier_details').css('display','block');
        // }
        var print_stn = ($("#print_stn").prop("checked") == true) ? 1 : 0;
        var invoice_type = $("input[name='invoice_type']:checked").val();
        if (!invoice_type) {
            alert("please select SALE TYPE");
            $('#submit_form').removeAttr('disabled').removeClass('disabled');;
            return false;
        }
        var show_list = false;
        var got_stn1 = false;
        var stn1 = "";
        var show_list2 = false;
        var got_stn2 = false;
        var stn2 = "";
        var show_list3 = false;
        var got_stn3 = false;
        var stn3 = "";
        $("input[class='deliveryList']:checked").each(function() {
            //sList += "(" + $(this).val() + "-" + (this.checked ? "checked" : "not checked") + ")";
            //var c = $(this).val();
            var c = $(this).attr("row_id");
            var product_name = $(this).attr("pd_name");
            var product_qty = $('#quantity_' + c).val(); //$(this).attr("pd_qty");
            console.log($('#quantity_' + c).val());
            add_to_deli_list(c, product_name, product_qty);
            if (c) {
                show_list = true;
            }
        });
        if (show_list) {
            if (print_stn) {
                $('#del_list').css('display', 'block');
                $('#del_list_div').css('display', 'block');
            }
            if (!got_stn1) {
                get_stn(4, "#stn1");
                got_stn1 = true;
            }
        }
        $("input[class='deliveryList2']:checked").each(function() {
            var c = $(this).attr("row_id");
            var product_name = $(this).attr("pd_name");
            var product_qty = $('#quantity_' + c).val(); //$(this).attr("pd_qty");
            //console.log($('#quantity_'+c).val());
            add_to_deli_list2(c, product_name, product_qty);
            if (c) {
                show_list2 = true;
            }
        });
        if (show_list2) {
            if (print_stn) {
                $('#del_list2').css('display', 'block');
                $('#del_list2_div').css('display', 'block');
            }
            if (!got_stn2) {
                get_stn(5, "#stn2");
                got_stn2 = true;
            }
        }
        $("input[class='deliveryList3']:checked").each(function() {
            var c = $(this).attr("row_id");
            var product_name = $(this).attr("pd_name");
            var product_qty = $('#quantity_' + c).val(); //$(this).attr("pd_qty");
            //console.log($('#quantity_'+c).val());
            add_to_deli_list3(c, product_name, product_qty);
            if (c) {
                show_list3 = true;
            }
        });
        if (show_list3) {
            if (print_stn) {
                $('#del_list3').css('display', 'block');
                $('#del_list3_div').css('display', 'block');
            }
            if (!got_stn3) {
                get_stn(6, "#stn3");
                got_stn3 = true;
            }
        }
        $('#modal-loading').show();
        /*---- add to bill ----*/
        $('.bill_date').text('Date: ' + $('#sale_datetime').val());
        /*-- end --*/
        var fields = $("#pos-sale-form").serialize(); <?php
        if ($this->session->userdata('ss_user_id') == 9999) {
            ?> console.log("_running on dev");
            $("#soTable > tbody").empty();
            $('.bill_no').text('Bill No:' + obj.sale_ref);
            $('#kot_bill_no').text('Bill No:' + obj.sale_ref);
            $('#fucking_done').val(1);
            if ($("#is_phone_bill").prop("checked") == true) {
                site.settings.printable = false;
            }
            if (site.settings.printable === true) {
                $("#view_bill_modal").modal().on("shown.bs.modal", function() {
                    console.log('attempting to print...');
                    console.log('printable status :' + site.settings.printable);
                    if (site.settings.printable === true) {
                        //$('#modal-loading').hide();
                        //window.print();
                        console.log('doc printed');
                        site.settings.printable = false;
                        console.log('print status changed to:' + site.settings.printable);
                        //$("#view_bill_modal").modal('hide');
                    } else {
                        console.log('not printed');
                    }
                }).on("hidden.bs.modal", function() {
                    form_reset();
                });
            } else {
                form_reset();
            } <?php
        } else {
            ?> $.post("<?php echo base_url();?>pos/pos_submit", fields).done(function(data) {
                var obj = jQuery.parseJSON(data);
                if (obj.error == 1) {
                    $('#modal-loading').hide();
                    $('.alert-success').hide();
                    $('.alert-danger').show();
                    $(".errortxt").text(obj.disMsg);
                    window.scrollTo(500, 0);
                }
                if (obj.error == 0) {
                    if ($('#grand_total').val() < 10000) {
                        save_hw(fields);
                    }
                    window.scrollTo(500, 0);
                    $("#soTable > tbody").empty();
                    //$('#bill_no').text('Bill No:' + obj.sale_ref);
                    $('.bill_no').text('Bill No:' + obj.sale_ref);
                    $('#kot_bill_no').text('Bill No:' + obj.sale_ref);
                    $('#fucking_done').val(1);
                    if ($("#is_phone_bill").prop("checked") == true) {
                        site.settings.printable = false;
                    }
                    if (site.settings.printable === true) {
                        //window.open(URL, obj.sale_ref, "","");
                        var mth = 0;
                        if (mth == 1) {
                            $("#view_bill_modal").modal().on("shown.bs.modal", function() {
                                console.log('attempting to print...');
                                console.log('printable status :' + site.settings.printable);
                                if (site.settings.printable === true) {
                                    $('#modal-loading').hide();
                                    window.print();
                                    console.log('doc printed');
                                    site.settings.printable = false;
                                    console.log('print status changed to:' + site.settings.printable);
                                    $("#view_bill_modal").modal('hide');
                                } else {
                                    console.log('not printed');
                                }
                            }).on("hidden.bs.modal", function() {
                                form_reset();
                            });
                        } else {
                            window.open('<?php echo base_url() ?>pos/sale_details_stn?sale_id=' + obj.sale_id + '&stn=' + print_stn, 'pos_print', 'toolbar=0,status=0,width=700,height=700, left=10, top=10,scrollbars=yes');
                            form_reset();
                        }
                    } else {
                        form_reset();
                    }
                }
            }); <?php
        } ?>
    }
    
    function get_stn(warehouse_id, id) {
        $.ajax({
            type: "post",
            async: false,
            url: "<?php echo base_url('common/get_store_note'); ?>",
            data: {
                warehouse_id: warehouse_id
            },
            //dataType: "html",
            success: function(data) {
                var obj = jQuery.parseJSON(data);
                var stn_no = obj.stn_no;
                var stn_code = obj.stn_code;
                $(id).text(stn_code);
                switch (id) {
                    case "#stn1":
                        $('#stnn1').val(stn_no);
                        $('#stnn1_code').val(stn_code);
                        $('#stnn1_wh').val(warehouse_id); //console.log($('#stnn1').val());
                        break;
                    case "#stn2":
                        $('#stnn2').val(stn_no);
                        $('#stnn2_code').val(stn_code);
                        $('#stnn2_wh').val(warehouse_id); //console.log($('#stnn2').val());
                        break;
                    case "#stn3":
                        $('#stnn3').val(stn_no);
                        $('#stnn3_code').val(stn_code);
                        $('#stnn3_wh').val(warehouse_id); //console.log($('#stnn3').val());
                        break;
                    default:
                        break;
                }
                //return stn_code;
            },
            error: function() {
                //alert('Error occured while getting data from server.');
                return false;
            }
        });
    }
    
    /*function getQty(product_id, c) {
        $.ajax({
            type: "post",
            async: false,
            url: "<?php echo base_url('bar/warehouse_qty'); ?>",
            data: {
                product_id: product_id
            },
            //dataType: "html",
            success: function(data) {
                var obj = jQuery.parseJSON(data);
                var maxq = obj.quantity;
                setTimeout(function() {
                    $('#maxq_' + c).text(maxq);
                }, 1000);
            },
            error: function() {
                alert('Error occured while getting data from server.');
            }
        });
    }*/
    
    function form_submit_for_kot(result) {
        $('#cash_en').val(0);
        $('#cash_en_tot').val(0);
        $('#cash_balance').text('');
        $('#modal-loading').show();
        /*---- add to bill ----*/
        $('.bill_date').text('Date: ' + $('#sale_datetime').val());
        /*-- end --*/
        var fields = $("#pos-sale-form").serialize();
        $.post("<?php echo base_url();?>pos/pos_submit", fields).done(function(data) {
            var obj = jQuery.parseJSON(data);
            if (obj.error == 1) {
                $('.alert-success').hide();
                $('.alert-danger').show();
                $(".errortxt").text(obj.disMsg);
                window.scrollTo(500, 0);
            }
            if (obj.error == 0) {
                window.scrollTo(500, 0);
                $("#soTable > tbody").empty();
                $('.bill_no').text('Bill No:' + obj.sale_ref);
                $('#fucking_done').val(1);
                form_reset();
            }
        });
    }
    
    function save_hw(formData) {
        <?php
        if ($this->session->userdata('ss_user_id') != 1) {
            ?>
            var url = "hw/pos/pos_submit";
            try {
                $.post("<?php echo base_url();?>" + url + "", formData).done(function(data) {
                    var obj = jQuery.parseJSON(data);
                    console.log(obj);
                    if (obj.error == 1) {
                        console.log("step one: failed");
                    }
                    if (obj.error == 0) {
                        console.log("step one: successfully saved");
                    }
                });
            } catch (e) {} <?php
        } ?>
    }
    
    function form_reset() {
        var done = $('#fucking_done').val();
        if (done == 1) {
            if (!$('#sale_id').val()) form_clear();
            else form_locate();
        }
    }
    
    function form_clear() {
        $('#pos-sale-form').trigger("reset");
        location.reload();
    }
    
    function form_locate() {
        <?php
        $location = 'bar';
        if ($order_place == 'rest') $location = 'pos'; 
        ?> 
        window.location.href = '<?php echo base_url($location); ?>';
    }
    $('select#category').on('change', function() {
        var v = $(this).val();
        $.ajax({
            type: "get",
            async: false,
            url: "<?php echo base_url('products/get_sub_category_by_id'); ?>",
            data: {
                category_id: v
            },
            dataType: "html",
            success: function(data) {
                if (data != "") {
                    $('#subcat_data').empty();
                    $('#subcat_data').html(data);
                    $("#subcategory").select2({
                        allowClear: true
                    });
                } else {
                    $('#subcat_data').empty();
                    var default_data = '<select name="subcategory" id="subcategory" class="form-control search-select" data-placeholder="Select Category to load Subcategories"></select>';
                    $('#subcat_data').html(default_data);
                    $("#subcategory").select2({
                        allowClear: true
                    });
                    displayNotice("Product Info", "No Subcategory found for the select category.");
                }
            },
            error: function() {
                alert('Error occured while getting data from server.');
            }
        });
    });
    $('#save_product').on('click', (function(event) {
        /*console.log(event);	alert();*/
        if (event.target.id == "save_product") event.preventDefault();
        add_product();
    }));
    
    /*jQuery(document).ready(function() {
        hide_all_div();
    });*/
    
    function click_sales_view_btn(sale_id) {
        var $modal = $('#ajax-modal');
        $('body').modalmanager('loading');
        setTimeout(function() {
            $modal.load('<?php echo base_url("sales/sale_details?sale_id="); ?>' + sale_id, '', function() {
                $modal.modal();
                $(".search-select").select2({
                    placeholder: "Select a State",
                    allowClear: true
                });
            });
        }, 1000);
    }
    
    function fbs_click(id) {
        u = location.href;
        t = document.title;
        window.open('<?php echo base_url() ?>bar/print_reciept?sale_id=' + id, 'sharer', 'toolbar=0,status=0,width=626,height=700, left=10, top=10,scrollbars=yes');
        return false;
    }
    
    function fbs_click_pos(id) {
        complete_sale(id);
        u = location.href;
        t = document.title;
        window.open('<?php echo base_url() ?>sales/sale_details_pos?sale_id=' + id, 'sharer', 'toolbar=0,status=0,width=626,height=700, left=10, top=10,scrollbars=yes');
        return false;
        /*console.log(mywindow);*/
    }
    
    function fbs_click_pos_no_c(id) {
        u = location.href;
        t = document.title;
        window.open('<?php echo base_url() ?>sales/sale_details_pos?sale_id=' + id, 'sharer', 'toolbar=0,status=0,width=626,height=700, left=10, top=10,scrollbars=yes');
        return false;
    }
    
    function edit_sale(id) {
        window.location = '<?php echo base_url() ?>bar/0/' + id;
        return false;
    }
    /*function resetfun() {
    	this.location.reload(true);
    }*/
    function complete_sale(id) {
        jQuery.ajax({
            type: "POST",
            url: "<?php echo base_url().'bar/complete_sale'?>",
            data: {
                sale_id: id,
            },
            cache: false,
            success: function(response) {
                displayNotice('page', 'Sale Completed !!');
                //loadDelivery();
                //loadDineIn();
                //loadTakeaway();
            }
        });
    }
    
    function set_as_paid(sid) {
        var sale_pymnt_date_time = $('#sale_datetime').val();
        var paid_by = $('#paying_by_' + sid).val();
        var given_amount = $('#c_pay_amount_' + sid).val();
        jQuery.ajax({
            type: "POST",
            url: "<?php echo base_url().'bar/set_as_paid'?>",
            data: {
                sale_id: sid,
                paid_by: paid_by,
                sale_pymnt_date_time: sale_pymnt_date_time,
                given_amount: given_amount
            },
            cache: false,
            success: function(response) {
                displayNotice('page', 'Payment Succeed!!');
            }
        });
    }
    
    function delete_invoice(sid) {
        var group_id = $('#group_id').val();
        /*var confm =	window.confirm("Delete This Invoice ?");*/
        if (group_id != 3) {
            bootbox.confirm('Delete Invoice ' + sid + '?', function(e) {
                if (e) {
                    jQuery.ajax({
                        type: "POST",
                        url: "<?php echo base_url().'sales/sales_delete?sale_id='?>" + sid,
                        cache: false,
                        success: function(response) {
                            displayNotice('page', 'Successfully Deleted!!');
                        }
                    });
                }
            });
        }
    }
    
    function delete_payments(sid) {
        var group_id = $('#group_id').val();
        /*var confm =	window.confirm("Delete Payments ?");*/
        if (group_id != 3) {
            bootbox.confirm('Delete Invoice Payments of Invoice ID: ' + sid + '?', function(e) {
                if (e) {
                    jQuery.ajax({
                        type: "POST",
                        url: "<?php echo base_url().'sales/sale_pymnts_delete?sale_id='?>" + sid + "&in_type=sale",
                        cache: false,
                        success: function(response) {
                            displayNotice('page', 'Successfully Deleted!!');
                        }
                    });
                }
            });
        }
    }
    $('button').on('mousedown', function(event) {
        event.preventDefault();
    });
    //-------------------------------- 2019-10-05 sachith ernaga new implpimentation
    /*function hide_all_div() {
        $(".dinmic").hide();
    }
    
    function show_all_div() {
        $(".dinmic").show();
    }
    
    function show_brand_list(id) {
        if ($("#cat_" + id).css('display') == "none") {
            $(".dinmic").hide();
            $("#cat_" + id).show();
        } else {
            $(".dinmic").hide();
            $("#cat_" + id).hide();
        }
    }
    
    function refresh_products(cat_id, brand_id, $sub_cat_id) {
        show_brand_list(cat_id);
        load_sub_cat_list(cat_id, brand_id);
        //load_product_list(cat_id,brand_id,$sub_cat_id);		
    }
    
    function load_product_list(cat_id, brand_id, $sub_cat_id) {
        $.post("<?php echo base_url();?>bar/load_product_by_cat_bran_sub_cat_id?cat_id=" + cat_id).done(function(data) {
            var obj = jQuery.parseJSON(data);
            $("#list_product_set_1").html(obj.li_pro);
        });
    }
    
    function load_product_with_subcategory(cat_id, brand_id, $sub_cat_id) {
        var price_type = $("input[name='price_type']:checked").val();
        if (!price_type) {
            alert("please select price type");
            return false;
        }
        $("#list_sub_category_set_1").hide();
        $.post("<?php echo base_url();?>bar/load_product_by_cat_bran_sub_cat_id?cat_id=" + cat_id + '&brand_id=' + brand_id + '&sub_cat_id=' + $sub_cat_id + '&price_type=' + price_type).done(function(data) {
            var obj = jQuery.parseJSON(data);
            $("#list_product_set_1").html(obj.li_pro);
        });
    }*/
    
    function load_sub_cat_list(cat_id, brand_id) {
        $.post("<?php echo base_url();?>bar/load_sub_cat_by_cat_brand_id?cat_id=" + cat_id + '&brand_id=' + brand_id).done(function(data) {
            var obj = jQuery.parseJSON(data);
            $("#list_sub_category_set_1").html(obj.li_sub_cat);
            $("#list_sub_category_set_1").show();
        });
        $("#list_product_set_1").html('');
        //$(window).scrollTop($('a#list_sub_category_set_1').position().top);
    }
    
    function search_products() {
        return true;
      var input, filter, table, tr, td, i, txtValue;
      input = document.getElementById("srh_pro_name");
      filter = input.value.toUpperCase();
      table = document.getElementById("products_table");
      tr = table.getElementsByTagName("tr");
      for (i = 0; i < tr.length; i++) {
        td = tr[i].getElementsByTagName("td")[0];
        if (td) {
          txtValue = td.textContent || td.innerText;
          if (txtValue.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
          } else {
            tr[i].style.display = "none";
          }
        }       
      }
    }
    function search_products_ajax() {
      var input, filter, table, tr, td, i, txtValue;
      input = document.getElementById("srh_pro_name");
      filter = input.value.toUpperCase();
      table = document.getElementById("products_table");
      tr = table.getElementsByTagName("tr");
      
      $.ajax({
          url:"<?php echo base_url();?>bar/get_prices",
          method:"POST",
          dataType:"JSON",
          data:{
              filter:filter
          },
          success:function(data){
              if(data.success){
                  $('#products_table > tbody').html(data.data);
              }else{
                  $('#products_table > tbody').html("<tr><td>No data</td></tr>");
              }
          },
          error:function(data){
              console.log(data);
          }
      });
    }
    
    $(document).ready(function(){
      var tr = '<tr class="header"><th style="width:40%;">Name</th><th style="width:20%;">Cash</th><th style="width:20%;">Credit</th><th style="width:20%;">Wholesale</th></tr>';
        $.each(jsonarray,function(key,value){
            var name = "<td>"+value.product_name+"</td>";
            var cash = "<td>"+value.product_price+"</td>";
            var credit = "<td>"+value.retail_credit+"</td>";
            var whlsle = "<td>"+value.retail_wholesale+"</td>";
            tr+= "<tr>"+name+cash+credit+whlsle+"</tr>";
        });
        $('#products_table').html(tr);
        var environment = localStorage.getItem("environment");
        if(environment == null){
            localStorage.setItem("environment", "<?php echo ENVIRONMENT; ?>");
        }else{
            if(environment !== '<?php echo ENVIRONMENT; ?>'){
                alert("WARNING!! Environment has been changed.");
                localStorage.setItem("environment", "<?php echo ENVIRONMENT; ?>");
            }
        }
        //
    });
    function search_p(pc){
        $('#add_item').val('');
        var e = jQuery.Event("keydown");
        e.key = pc;
        $('#add_item').trigger(e);
        
        $('#srh_pro_name').val("");
        search_products_ajax();
    }
    $('#return_sale_id').on("keypress", function(e) {
        if (e.key === "Enter") {
    		window.open($(this).attr("href")+$(this).val(), "popupWindow", "width=600,height=600,scrollbars=yes");
    		$(this).val('');
    	}
    });
    /*$(document).on('click',function(e){
        console.trace('click on document');
        console.log(e.target);
    });
    $(document).on('keypress',function(e){
        console.trace('keypress on document');
        console.log(e.target);
    });*/
    </script>
     
     
    </body>
</html>