function add_invoice_item(item) {

    if (count == 1) {
        spositems = {};
    }
    if (item == null) {
        return;
    }

    var item_id = Settings.item_addition == 1 ? item.item_id : item.id;

    if (spositems[item_id]) {
        spositems[item_id].row.qty = parseFloat(spositems[item_id].row.qty) + 1;
    } else {
        spositems[item_id] = item;
    }

    store('spositems', JSON.stringify(spositems));
    loadItems();
    return true;
}







//************TRJ010 - ALEXANDER ROCA 15-03-2019********************
window.onload = function submitSaleTPV() {

    $('#submit-sale').click(function () {


        var codigCMP = $("#custom_field_1_val").val();
        var montoPagar = $("#amount_val").val();

        if (codigCMP <= 0) {

            if (!document.getElementById("custom_field_1")) {
                //alert("si existe");
            } else {
                window.stop();
                //alert("no existe");
                bootbox.alert("Debe ingresar el codigo CMP");
                document.getElementById('submit-sale').disabled = false;
            }


        }

        if (montoPagar <= 0) {
            window.stop();
            //alert("no existe");
            bootbox.alert("Debe ingresar un valor de pago");
            document.getElementById('submit-sale').disabled = false;
        }
        if($('#document_type').val() == 0){
            window.stop();
            //alert("no existe");
            bootbox.alert("Debe seleccionar el tipo de documento.");
            document.getElementById('submit-sale').disabled = false;
        }
    });

    $('#submit-sale_caja').click(function () {

        /*document.getElementById("pos-sale-form").innerHTML='<img src="http://localhost:8000/sistemaTPV2.2.0/themes/default/assets/images/loader.gif" style="z-index:1000px;" />';
        */
        var codigCMP = $("#custom_field_1_val").val();
        var montoPagar = $("#amount_val").val();

        if (codigCMP <= 0) {

            if (!document.getElementById("custom_field_1")) {
                //alert("si existe");
            } else {
                window.stop();
                //alert("no existe");
                bootbox.alert("Debe ingresar el codigo CMP");
                document.getElementById('submit-sale_caja').disabled = false;
            }


        }


        if (montoPagar <= 0) {
            window.stop();
            //alert("no existe");
            bootbox.alert("Debe ingresar un valor de pago");
            document.getElementById('submit-sale_caja').disabled = false;
        }




    });


}
//*****************TRJ010 - ALEXANDER ROCA 15-03-2019***************

/* start cargar items en inputs keyup*/
function loadItems() {
    if (count == 1) {
        spositems = {};

    }

    if (get('spositems')) {
        total = 0;
        var tax_ICBPER = 0;
        count = 1;
        an = 1;
        product_tax = 0;
        invoice_tax = 0;
        product_discount = 0;
        order_discount = 0;
        total_discount = 0;

        $("#posTable tbody").empty();
        var time = ((new Date).getTime()) / 1000;

        $("#order_span").empty(); $("#bill_span").empty();
        var pos_head1 = '<span style="text-align:center;"><h3>' + Settings.site_name + '</h3>'
        //var pos_head2 = '</h4><h5>'+$('#select2-chosen-1').text()+'</h5></span>';
        var pos_head2 = '<h5>' + date(Settings.dateformat + ' ' + Settings.timeformat, time) + '</h5></span>'; //'</h4><h5>'+$('#select2-chosen-1').text()+'</h5></span>';
        $("#order_span").prepend(pos_head1 + '<h4>Orden</h4><h5>Por: ' + username + '</h5>' + pos_head2);
        $("#bill_span").prepend(pos_head1 + '<h4>Factura</h4><h5>Por: ' + username + '</h5>' + pos_head2);
        $("#order-table").empty(); $("#bill-table").empty();


        spositems = JSON.parse(get('spositems'));

        $.each(spositems, function () {
            tax_ICBPER = 0;
            var item = this;
            var item_id = Settings.item_addition == 1 ? item.item_id : item.id;
            spositems[item_id] = item;
            //************************TRJ014 - ALEXANDER ROCA - 03/04/2019****************
            var product_id = item.row.productID, item_type = item.row.type, item_tax_method = parseFloat(item.row.tax_method), combo_items = item.combo_items, item_qty = item.row.qty, item_aqty = parseFloat(item.row.quantity), item_type = item.row.type, item_ds = item.row.discount, item_code = item.row.code, item_name = item.row.name.replace(/"/g, "&#034;").replace(/'/g, "&#039;"), item_tax_ICBPER = item.tax_ICBPER;
            //************************TRJ014 - ALEXANDER ROCA - 03/04/2019****************
            var item_currency = item.row.currency;
            //***********************************TRJ024 - ALEXANDER ROCA - 01/04/2019**********************
            item_name = item_name.replace("<", "&lt;");
            item_name = item_name.replace(">", "&gt;");
            //***********************************TRJ024 - ALEXANDER ROCA - 01/04/2019**********************
            var unit_price = parseFloat(item.row.real_unit_price);


            if (item.row.edit != 1) {
                if (unit_price > 0 && item_currency != Settings.currency_prefix) {
                    if (item_currency == "PEN") {
                        unit_price = unit_price / $("#exchange").val();
                        spositems[item_id].row.real_unit_price = unit_price;
                    }
                    if (item_currency == "USD") {
                        unit_price = unit_price * $("#exchange").val();
                        spositems[item_id].row.real_unit_price = unit_price;
                    }
                }
                spositems[item_id].row.edit = 1;
            }

            // var unit_price = parseFloat(item.row.real_unit_price);
            var net_price = unit_price;

            var ds = item_ds ? item_ds : '0';
            var item_discount = formatDecimal(ds);
            if (ds.indexOf("%") !== -1) {
                var pds = ds.split("%");
                if (!isNaN(pds[0])) {
                    item_discount = formatDecimal(parseFloat(((net_price) * parseFloat(pds[0])) / 100));
                }
            }
            product_discount += formatDecimal(item_discount * item_qty);
            net_price = formatDecimal(net_price - item_discount);

            var pr_tax = parseInt(Settings.default_tax_rate), pr_tax_val = 0;
            if (pr_tax !== null && pr_tax != 0) {
                if (item_tax_method == 0) { //Incluido
                    pr_tax_val = formatDecimal((net_price * parseFloat(pr_tax)) / (100 + parseFloat(pr_tax)));
                    net_price -= pr_tax_val;
                    tax = lang.inclusive;
                } else if (item_tax_method == 1) { //Excluido
                    pr_tax_val = formatDecimal((net_price * parseFloat(pr_tax)) / 100);
                    tax = lang.exclusive;
                } else if (item_tax_method == 2) { //Exonerado
                    pr_tax_val = 0;
                    tax = lang.exonerated;
                }
            }
            product_tax += formatDecimal(pr_tax_val * item_qty);

            //alert(item_id+" - "+item_name+" - "+item.indice);
            var row_no = (new Date).getTime();


            /*123*/
            /*   console.log(localStorage.getItem("spositems"))*/


            /*console.log(item["row"]["code"]);*/

            var newTr = $('<tr id="' + row_no + '" class="' + item_id + '" data-item-id="' + item_id + '"></tr>');

            tr_html = '<td><input name="product_id[]" type="hidden" class="rid" value="' + product_id + '"><button type="button" class="btn bg-purple btn-block btn-xs edit" id="' + row_no + '" data-item="' + item_id + '"><span class="sname" id="name_' + row_no + '">' + item_name + ' (' + item_code + ')</span></button></td>';
            tr_html += '<input name="name[]" type="hidden" class="rid" value="' + item_name + '">';
            tr_html += '<input name="code[]" id="testtt" type="hidden" class="rid" value="' + item_code + '">';
            // <input class="rprice" name="net_price[]" type="hidden" id="price_' + row_no + '" value="' + formatDecimal(item_price) + '">
            tr_html += '<td class="text-right"><input class="realuprice" name="real_unit_price[]" type="hidden" value="' + item.row.real_unit_price + '"><input class="rdiscount" name="product_discount[]" type="hidden" id="discount_' + row_no + '" value="' + ds + '"><span class="text-right sprice" id="sprice_' + row_no + '">' + formatMoney(parseFloat(net_price) + parseFloat(pr_tax_val)) + '</span></td>';
            tr_html += '<td><input class="form-control input-qty kb-pad text-center rquantity" name="quantity[]" type="text" value="' + formatDecimal(item_qty) + '" data-id="' + row_no + '" data-item="' + item_id + '" id="quantity_' + row_no + '" onClick="this.select();"></td>';
            /*input cantidad*/
            var subTotal_ICBPER = 0;
            var check_ICBPER = '', input_text_ICBPER = '"0"';

            if (plastic_bags > 0) {
                ocultar_ICBPER = '';

                if (item_tax_ICBPER == 0) {
                    check_ICBPER = '';
                    input_text_ICBPER = '"0"';
                    subTotal_ICBPER = 0;
                } else {
                    check_ICBPER = 'checked';
                    input_text_ICBPER = '"1"';
                    subTotal_ICBPER = (plastic_bags * Math.round(parseFloat(item_qty)));
                }

            } else {
                ocultar_ICBPER = ' style="display:none;" ';
            }


            tr_html += '<td class="text-center checkboxICBPER" ' + ocultar_ICBPER + '><input value="yes" class="" name="tax_ICBPER[]" type="checkbox" data-id="' + row_no + '" data-item="' + item_id + '" id="tax_ICBPER_' + row_no + '" onClick="javascript:chececkTaxBP();" ' + check_ICBPER + '>';
            tr_html += '<input value=' + input_text_ICBPER + ' class="" name="value_ICBPER[]" type="hidden" id="value_ICBPER_' + row_no + '"></td>';
            tr_html += '<td class="text-right"><span class="text-right ssubtotal" id="subtotal_' + row_no + '">' + formatMoney(((parseFloat(net_price) + parseFloat(pr_tax_val)) * parseFloat(item_qty)) + subTotal_ICBPER) + '</span></td>';




        /*button delete*/    tr_html += '<td class="text-center"><i class="fa fa-trash-o tip pointer posdel" id="' + row_no + '" title="Remove"></i></td>';
            newTr.html(tr_html);
            newTr.prependTo("#posTable");



            var isChecked = document.getElementById('tax_ICBPER_' + row_no).checked;

            if (isChecked) {
                tax_ICBPER += plastic_bags * Math.round(parseFloat(item_qty));
            }

            //total += formatDecimal((parseFloat(net_price) + parseFloat(pr_tax_val)) * parseFloat(item_qty));
            total += formatDecimal(((parseFloat(net_price) + parseFloat(pr_tax_val)) * parseFloat(item_qty)) + tax_ICBPER);
            count += parseFloat(item_qty);
            an++;
            $('#list-table-div').scrollTop(0);
            if (item_type == 'standard' && item_qty > item_aqty) {
                $('#' + row_no).addClass('danger');
                $('#' + row_no).find('.edit').removeClass('bg-purple').addClass('btn-warning');
            }
            else if (item_type == 'combo') {
                if (combo_items === false) {
                    $('#' + row_no).addClass('danger');
                } else {
                    $.each(combo_items, function () {
                        if (parseFloat(this.quantity) < (parseFloat(this.qty) * item_qty)) {
                            $('#' + row_no).addClass('danger');
                            $('#' + row_no).find('.edit').removeClass('bg-purple').addClass('btn-warning');
                        }
                    });
                }
            }

            var bprTr = '<tr class="row_' + item_id + '" data-item-id="' + item_id + '"><td colspan="2">#' + (an - 1) + ' ' + item_name + ' (' + item_code + ')</td></tr>';
            bprTr += '<tr class="row_' + item_id + '" data-item-id="' + item_id + '"><td>(' + item_qty + ' x ' + formatMoney(parseFloat(net_price) + parseFloat(pr_tax_val)) + ')</td><td style="text-align:right;">' + formatMoney(((parseFloat(net_price) + parseFloat(pr_tax_val)) * parseFloat(item_qty))) + '</td></tr>';
            var oprTr = '<tr class="row_' + item_id + '" data-item-id="' + item_id + '"><td>#' + (an - 1) + ' ' + item_name + ' (' + item_code + ')</td><td>' + item_qty + '</td></tr>';
            $("#order-table").append(oprTr);
            $("#bill-table").append(bprTr);

        });



        var ds = get('spos_discount') ? get('spos_discount') :
            ($('#discount_val').val() ? $('#discount_val').val() : '0');
        order_discount = parseFloat(ds);
        if (ds.indexOf("%") !== -1) {
            var pds = ds.split("%");
            order_discount = parseFloat((total * parseFloat(pds[0])) / 100);
        }

        var g_total = total - parseFloat(order_discount);// + parseFloat(order_tax);
        grand_total = formatMoney((parseFloat(g_total)));//+parseFloat(tax_ICBPER)));
        $("#ds_con").text('(' + formatMoney(product_discount) + ') ' + formatMoney(order_discount));
        // $("#ts_con").text(formatMoney(order_tax));
        $("#total-payable").text(grand_total);
        $("#total").text(formatMoney(total));

        $("#monto_total_val").val(total);
        $("#descuento_val").val(order_discount);

        $("#count").text((an - 1) + ' (' + formatMoney(count - 1) + ')');
        bill_data += printLine(lang.rounding + ': ' + formatMoney(rounding)) + "\n";

        var bill_totals = '';
        bill_totals += '<tr><td>' + lang.total + '</td><td style="text-align:right;">' + formatMoney(total) + '</td></tr>';
        bill_totals += '<tr><td>' + lang.total_items + '</td><td style="text-align:right;">' + (an - 1) + ' (' + (parseFloat(count) - 1) + ')</td></tr>';
        if (order_discount > 0) {
            bill_totals += '<tr><td>' + lang.order_discount + '</td><td style="text-align:right;">' + formatMoney(order_discount) + '</td></tr>';
        }
        if (order_tax != 0) {
            bill_totals += '<tr><td>' + lang.order_tax + '</td><td style="text-align:right;">' + formatMoney(order_tax) + '</td></tr>';
        }
        bill_totals += '<tr><td>' + lang.grand_total + '</td><td style="text-align:right;">' + formatMoney(g_total) + '</td></tr>';
        if (Settings.rounding != 0) {
            round_total = roundNumber(g_total, parseInt(Settings.rounding));
            var rounding = formatDecimal(round_total - g_total);
            bill_totals += '<tr><td>' + lang.rounding + '</td><td style="text-align:right;">' + formatMoney(rounding) + '</td></tr>';
            bill_totals += '<tr><td>' + lang.total_payable + '</td><td style="text-align:right;">' + formatMoney(round_total) + '</td></tr>';
        }
        $('#bill-total-table').empty();
        $('#bill-total-table').append(bill_totals);
        // }

        if (Settings.display_kb == 1) { display_keyboards(); }
        $('#add_item').focus();
    }


}
/*end cargar items en inputs keyup*/



$(document).ready(function () {
    $('#spos_customer').select2({
        language: {
            inputTooShort: function (args) {
                var remainingChars = args.minimum - args.input.length;
                var message = 'Por favor, ingrese ' + remainingChars + ' o más caracteres';
                return message;
            },
            searching: function () {
                return 'Buscando…';
            },
        },
        ajax: {
            url: function (params) {
                var searchTerm = params.term.trim().replace(/\s+/g, '-'); // Reemplazar espacios por guiones
                return 'pos/get_customers/'+searchTerm;
            },
            dataType: 'json',
            type: 'GET',
            delay: 250,
            processResults: function (data) {
                // alert('La función se está ejecutando.');
                var modifiedData = data.map(function (customer) {
                    return {
                        id: customer.id,
                        text: customer.name,
                        is_retainer: customer.is_retainer,
                        retainer_percentage: customer.retainer_percentage
                    };
                });
                // var modifiedData = data.map(function (customer) {
                //     return {
                //         id: customer.id,
                //         text: customer.name,
                //         is_retainer: $('#spos_customer option[value="' + customer.id + '"]').data('isretainer'),
                //         retainer_percentage: $('#spos_customer option[value="' + customer.id + '"]').data('retainerpercentage')
                //     };
                // });

                // var listHTML = '';
                // $.each(data, function(index, customer) {
                //     listHTML += '<option value="' + customer.id + '" data-isretainer="' + customer.is_retainer + '" data-retainerpercentage="' + customer.retainer_percentage + '"></option>';
                // });
                // $('#spos_customer').html(listHTML);
                // console.log(listHTML);

                //console.log(modifiedData);
                return { results: modifiedData };
            },
            cache: true
        },
        minimumInputLength: 1,
        placeholder: 'Buscar cliente por nombre, RUC o DNI',
        escapeMarkup: function (markup) { return markup; }
    });

    $('#cerrarBotonModal').click(function(){
        $('#document_type').val(0);
    });
    $('#equisBotonModal').click(function(){
        $('#document_type').val(0);
    });
    /* =============================
    Edit Item Modal
    ============================= */

    $("#posTable").on("click", '.edit', function () {
        var row = $(this).closest('tr');
        var id = row.attr('id');
        var item_id = row.attr('data-item-id');
        var item = spositems[item_id];
        //var unit_price = parseFloat(item.row.real_unit_price);
        console.log(item)
        var unit_price = formatDecimal(row.find('.realuprice').val());
        var net_price = unit_price;
        var ds = item.row.discount ? item.row.discount : '0';
        item_discount = formatDecimal(parseFloat(ds));
        if (ds.indexOf("%") !== -1) {
            var pds = ds.split("%");
            if (!isNaN(pds[0])) {
                item_discount = formatDecimal((net_price * parseFloat(pds[0])) / 100);
            }
        }
        net_price -= item_discount;
        var pr_tax = parseFloat(Settings.default_tax_rate), pr_tax_val = 0, tax = '';
        if (pr_tax !== null && pr_tax != 0) {
            if (parseFloat(item.row.tax_method) == 0) {
                pr_tax_val = formatDecimal((net_price * parseFloat(pr_tax)) / (100 + parseFloat(pr_tax)));
                net_price -= pr_tax_val;
                tax = lang.inclusive;
            } else if (parseFloat(item.row.tax_method) == 1) {
                pr_tax_val = formatDecimal((net_price * parseFloat(pr_tax)) / 100);
                tax = lang.exclusive;
            } else if (parseFloat(item.row.tax_method) == 2) {
                pr_tax_val = 0;
                tax = lang.exonerated;
            }
        }
        $('#proModalLabel').text(item.label);
        $('#net_price').text(formatMoney(net_price));
        $('#pro_tax').text(formatMoney(pr_tax_val));
        $('#pro_tax_method').text('(' + tax + ')');
        $('#row_id').val(row_id);
        $('#item_id').val(item_id);
        $('#nPrice').val(unit_price);
        $('#nQuantity').val(item.row.qty);
        $('#nDiscount').val(ds);
        $('#proModal').modal({ backdrop: 'static' });
    });

    $(document).on('change', '#nPrice, #nDiscount', function () {
        var item_id = $('#item_id').val();
        var unit_price = parseFloat($('#nPrice').val());
        var net_price = unit_price;
        var item = spositems[item_id];
        var ds = $('#nDiscount').val() ? $('#nDiscount').val() : '0';
        item_discount = formatDecimal(parseFloat(ds));
        if (ds.indexOf("%") !== -1) {
            var pds = ds.split("%");
            if (!isNaN(pds[0])) {
                item_discount = formatDecimal((unit_price * parseFloat(pds[0])) / 100);
            }
        }
        net_price -= item_discount;
        var pr_tax = parseFloat(Settings.default_tax_rate), pr_tax_val = 0;
        if (pr_tax !== null && pr_tax != 0) {
            if (parseFloat(item.row.tax_method) == 0) {
                pr_tax_val = formatDecimal((net_price * parseFloat(pr_tax)) / (100 + parseFloat(pr_tax)));
                net_price -= pr_tax_val;
                tax = lang.inclusive;
            } else if (parseFloat(item.row.tax_method) == 1) {
                pr_tax_val = formatDecimal((net_price * parseFloat(pr_tax)) / 100);
                tax = lang.exclusive;
            } else if (parseFloat(item.row.tax_method) == 2) {
                pr_tax_val = 0;
                tax = lang.exonerated;
            }
        }

        $('#net_price').text(formatMoney(net_price));
        $('#pro_tax').text(formatMoney(pr_tax_val));
    });

    /* =============================
    Edit Item Method
    ============================= */

    $(document).on('click', '#editItem', function () {
        var item_id = $('#item_id').val();

        var price = parseFloat($('#nPrice').val());
        if (!is_valid_discount($('#nDiscount').val()) || !is_numeric($('#nPrice').val()) || !is_numeric($('#nQuantity').val())) {
            bootbox.alert(lang.unexpected_value);
            return false;
        }
        spositems[item_id].row.qty = parseFloat($('#nQuantity').val()),
            spositems[item_id].row.real_unit_price = price,
            spositems[item_id].row.discount = $('#nDiscount').val() ? $('#nDiscount').val() : '0',
            spositems[item_id].row.edit = 1;
        localStorage.setItem('spositems', JSON.stringify(spositems));
        $('#proModal').modal('hide');

        loadItems();
        return;
    });

    /* =============================
    Row quantity change
    ============================= */
    $(document).on("change", '.rquantity', function () {
        var row = $(this).closest('tr');
        if (!is_numeric($(this).val()) || $(this).val() == 0) {
            loadItems();
            bootbox.alert(lang.unexpected_value);
            return false;
        }
        var new_qty = parseFloat($(this).val()),
            item_id = row.attr('data-item-id');
        spositems[item_id].row.qty = new_qty;
        localStorage.setItem('spositems', JSON.stringify(spositems));
        loadItems();
    });

    $('#reset').click(function (e) {
        if (count <= 1) {
            return false;
        }
        bootbox.confirm(lang.r_u_sure, function (result) {
            if (result) {
                if (get('spositems')) {
                    remove('spositems');
                }
                // if (get('spos_tax')) {
                //     remove('spos_tax');
                // }
                if (get('spos_discount')) {
                    remove('spos_discount');
                }
                if (get('spos_customer')) {
                    remove('spos_customer');
                }

                if (get('spos_note')) {
                    remove('spos_note');
                    $('#spos_note').val("");
                    $('#note').val("");
                }
                if (get('custom_field_1')) {
                    remove('custom_field_1');
                    $('#custom_field_1').val("");
                    $('#custom_field_1_val').val("");
                }
                if (get('custom_field_2')) {
                    remove('custom_field_2');
                    $('#custom_field_2').val("");
                    $('#custom_field_2_val').val("");
                }

                window.location.href = base_url + "pos";
            }
        });
    });

    $(document).ready(function () {
        $('#print_order').click(function () {
            if (count <= 1) {
                bootbox.alert(lang.please_add_product);
                return false;
            } else {
                Popup($('#order_tbl').html());
            }
        });
        $('#print_bill').click(function () {
            if (count <= 1) {
                bootbox.alert(lang.please_add_product);
                return false;
            } else {
                Popup($('#bill_tbl').html());
            }
        });
    });
    // }

    $("#updateDiscount").click(function () {
        var ds = $('#get_ds').val() ? $('#get_ds').val() : '0';
        var apply_to = $('input[name=apply_to]:checked').val();
        if (ds.length != 0) {
            if (apply_to == 'order') {
                $('#discount_val').val(ds);
                store('spos_discount', ds);

                if (ds.indexOf("%") !== -1) {
                    var pds = ds.split("%");

                    order_discount = (total * parseFloat(pds[0])) / 100;
                    // order_tax = calTax();
                    // var g_total = total + order_tax - order_discount;
                    var g_total = total - order_discount;
                    grand_total = parseFloat(g_total);
                    $("#ds_con").text('(' + formatMoney(product_discount) + ') ' + formatMoney(order_discount));
                    $("#total-payable").text(formatMoney(grand_total));

                    $("#monto_total_val").val(total);
                    $("#descuento_val").val(order_discount);

                } else {

                    order_discount = ds;
                    // order_tax = calTax();
                    // var g_total = (total + order_tax) - parseFloat(order_discount);
                    var g_total = (total) - parseFloat(order_discount);
                    grand_total = parseFloat(g_total);
                    $("#ds_con").text('(' + formatMoney(product_discount) + ') ' + formatMoney(order_discount));
                    $("#total-payable").text(formatMoney(grand_total));

                    $("#monto_total_val").val(total);
                    $("#descuento_val").val(order_discount);

                }
            } else if (apply_to == 'products') {
                var items = {};
                spositems = JSON.parse(get('spositems'));
                $.each(spositems, function () {
                    this.row.discount = ds;
                });
                store('spositems', JSON.stringify(spositems));
                loadItems();
            }
            $('#dsModal').modal('hide');
        }
    });

    $("#add_discount").click(function () {
        var dval = $('#discount_val').val();
        $('#get_dfs').val(dval);
        $('#dsModal').modal({ backdrop: 'static' });
        return false;
    });

    /*get product*/
    $(document).on('click', '.product', function (e) {
        code = $(this).val();

        $.ajax({
            type: "get",
            url: base_url + 'pos/get_product/' + code,
            dataType: "json",
            success: function (data) {


                if (data !== null) {
                    /*TRJ128-AMADOR START*/


                    add_invoice_item(data)
                    sumarCantidadItem(code);
                    /*END TRJ128*/

                } else {
                    bootbox.alert(lang.no_match_found);
                }
            }
        });
    });


    function removeItemsSpositems(item_id) {

        delete spositems[item_id];
        /*row.remove();*/
        if (spositems.hasOwnProperty(item_id)) { } else {
            localStorage.setItem('spositems', JSON.stringify(spositems));
            loadItems();
        }
    }



    $(document).on('click', '.category', function () {
        var cid = $(this).attr('id');
        if (cat_id != cid) {
            cat_id = cid;
            $.ajax({
                type: "get",
                url: base_url + 'pos/ajaxproducts',
                data: { category_id: cat_id, tcp: 1 },
                dataType: "json",
                success: function (data) {
                    p_page = 'n';
                    $('[data-toggle="control-sidebar"]').click();
                    //ocat_id = cat_id;
                    tcp = data.tcp;
                    $('.items').html(data.products);
                    $('.category').removeClass('active');
                    $('#' + cat_id).addClass('active');
                    nav_pointer();
                }
            });
        }
        return false;
    });



    $('#category-' + cat_id).addClass('active');

    $('#next').click(function () {
        if (p_page == 'n') { p_page = 0; }
        p_page += pro_limit;
        if (tcp >= pro_limit && p_page < tcp) {
            $.ajax({
                type: "get",
                url: base_url + 'pos/ajaxproducts',
                data: { category_id: cat_id, per_page: p_page },
                dataType: "html",
                success: function (data) {
                    $('.items').html(data);
                    nav_pointer();
                }
            });
        } else {
            p_page -= pro_limit;
        }
    });

    $('#previous').click(function () {
        if (p_page == 'n') { p_page = 0; }
        if (p_page != 0) {
            p_page -= pro_limit;
            if (p_page == 0) { p_page = 'n'; }
            $.ajax({
                type: "get",
                url: base_url + 'pos/ajaxproducts',
                data: { category_id: cat_id, per_page: p_page },
                dataType: "html",
                success: function (data) {
                    $('.items').html(data);
                    nav_pointer();
                }
            });
        }
    });

    $("#add_item").autocomplete({
        source: base_url + 'pos/suggestions',
        minLength: 1,
        autoFocus: false,
        delay: 200,

        response: function (event, ui) {
            if ($(this).val().length >= 16 && ui.content[0].id == 0) {
                bootbox.alert(lang.no_match_found, function () {
                    $('#add_item').focus();
                });
                $(this).val('');
            }
            else if (ui.content.length == 1 && ui.content[0].id != 0) {
                ui.item = ui.content[0];
                $(this).data('ui-autocomplete')._trigger('select', 'autocompleteselect', ui);
                $(this).autocomplete('close');
            }
            else if (ui.content.length == 1 && ui.content[0].id == 0) {
                bootbox.alert(lang.no_match_found, function () {
                    $('#add_item').focus();
                });
                $(this).val('');
            }


        },
        select: function (event, ui) {
            event.preventDefault();

            if (ui.item != null) { /*keyup item*/

                add_invoice_item(ui.item);
                $(this).val('');

                for (var dt in ui.content) {
                    var cdee = ui.content[dt]["row"];

                }

                if (cdee) {
                    sumarCantidadItem(cdee.code);

                } else {
                    sumarCantidadItem(ui.item.row.code);
                }

            } else {
                bootbox.alert(lang.no_match_found);

            }
        }
    });


    /*TRJ128 -Amador ccasani*/
    function sumarCantidadItem(code) {
        arraycode = [];
        var repetidos = {};
        for (var prop in spositems) {
            arraycode.push(spositems[prop]["row"]["code"])
        }

        arraycode.forEach(function (numero) {
            repetidos[numero] = (repetidos[numero] || 0) + 1;

        });

        if (repetidos[code] > 1) {
            //console.log("invalido")
            first_code = Object.keys(spositems).sort().pop();

            removeItemsSpositems(first_code);

            var input = $(".rquantity");
            a = input.attr("data-item");

            var item_id_pos = [];

            for (var p in spositems) {

                if (code === spositems[p]["row"]["code"]) {

                    spositems[spositems[p]["item_id"]].row.qty = parseFloat(spositems[spositems[p]["item_id"]].row.qty) + 1;
                    store('spositems', JSON.stringify(spositems));
                    loadItems();

                }

            }

        } else {
            console.log("valido")
        }
    }




    $('#add_item').bind('keypress', function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            $(this).autocomplete("search");
        }
    });

    $('#add_item').focus();
    $('#gccard_no').inputmask("9999 9999 9999 9999");
    $('#gift_card_no').inputmask("9999 9999 9999 9999");
    $('#genNo').click(function () {
        var no = generateCardNo();
        $(this).parent().parent('.input-group').children('input').val(no);
        return false;
    });

    $("#btn_search_product").click(function () {

        var negocio = $("#negocio").val();
        var orden;
        var columnas;

        switch (negocio) {
            case "0":
                orden = 6;
                columnas = [null, null, null, null, null, null, { "bVisible": false }, { "bSortable": false, "bSearchable": false }];
                break;
            case "1":
                orden = 8;
                columnas = [null, null, null, null, null, null, null, null, { "bVisible": false }, { "bSortable": false, "bSearchable": false }];
                break;
        }


        var ddt = $('#searchPresults').dataTable({
            "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, lang.all]],
            //"aaSorting": [[ 1, "asc" ]],
            "aaSorting": [[orden, "desc"]],
            "iDisplayLength": 10,
            'bProcessing': true, 'bServerSide': true, 'retrieve': true, 'bDestroy': true, "bFilter": false,
            'sAjaxSource': base_url + 'products/get_products_search',
            'fnServerData': function (sSource, aoData, fnCallback) {
                aoData.push({
                    "name": $("#___token").val(),
                    "value": $("#___hash").val()
                });
                aoData.push(
                    { "name": "product_search", "value": $("#product_search").val() },
                    { "name": "prin_activ_search", "value": $("#prin_activ_search").val() },
                    { "name": "acc_farm_search", "value": $("#acc_farm_search").val() },
                );

                $.ajax({ 'dataType': 'json', 'type': 'POST', 'url': sSource, 'data': aoData, 'success': fnCallback });
            },
            "aoColumns": columnas

        }).on('click', 'tbody tr', function () {

            code = $(this).find("input").val();
            $.ajax({
                type: "get",
                url: base_url + 'pos/get_product/' + code,
                dataType: "json",
                success: function (data) {
                    if (data !== null) {
                        //add_invoice_item(data);
                        if (count == 1) {
                            spositems = {};
                        }
                        if (data == null) {
                            return;
                        }

                        var item_id = Settings.item_addition == 1 ? data.item_id : data.id;
                        if (spositems[item_id]) {
                            spositems[item_id].row.qty = 1;
                        } else {
                            spositems[item_id] = data;
                        }

                        store('spositems', JSON.stringify(spositems));
                        loadItems();
                        return true;

                    } else {
                        bootbox.alert(lang.no_match_found);
                    }
                }
            });
            $('#searchProducts').modal('hide');
        });
    });

    $("#searchProducts").on("hidden.bs.modal", function () {
        $("#product_search, #prin_activ_search, #acc_farm_search").val("");
    });

    $(document).on('click', '#sellGiftCard', function (e) {
        if (count == 1) {
            spositems = {};
        }
        $('#gcModal').modal({ backdrop: 'static' });
    });

    $(document).on('click', '#addGiftCard', function (e) {
        var mid = (new Date).getTime(),
            gccode = $('#gccard_no').val(),
            gcname = $('#gcname').val(),
            gcvalue = $('#gcvalue').val(),
            gcprice = parseFloat($('#gcprice').val());
        if (gccode == '' || gcvalue == '' || gcprice == '' || gcvalue == 0 || gcprice == 0) {
            $('#gcerror').text(lang.file_required_fields);
            $('.gcerror-con').show();
            return false;
        }
        var gc_data = new Array();
        gc_data[0] = gccode;
        gc_data[1] = gcvalue;

        $.ajax({
            type: 'get',
            url: base_url + 'gift_cards/sell_gift_card',
            dataType: "json",
            data: { gcdata: gc_data },
            success: function (data) {
                if (data.result === 'success') {
                    spositems[mid] = { "id": mid, "item_id": mid, "label": gcname + ' (' + gccode + ')', "row": { "id": mid, "code": gccode, "name": gcname, "quantity": 1, "price": gcprice, "real_unit_price": gcprice, "tax": 0, "qty": 1, "type": "manual", "discount": "0" } };
                    store('spositems', JSON.stringify(spositems));
                    loadItems();
                    $('#gcModal').modal('hide');
                    $('#gccard_no').val('');
                    $('#gcvalue').val('');
                    $('#gcprice').val('');
                } else {
                    $('#gcerror').text(data.message);
                    $('.gcerror-con').show();
                }
            }
        });
    });




    var pwacc = false;
    $(document).on('click', '.posdel', function () {/*item delete*/
        var row = $(this).closest('tr');

        var item_id = row.attr('data-item-id');


        if (protect_delete == 1) {
            var boxd = bootbox.dialog({
                title: lang.enter_pin_code,
                closeButton: true,
                message: '<input id="pos_pin" name="pos_pin" type="password" placeholder="Pin Code" class="form-control kb-pad" autocomplete="off"> ',
                buttons: {
                    danger: {
                        label: lang.close,
                        className: "btn-default pull-left",
                        callback: function () { }
                    },
                    success: {
                        label: "<i class='fa fa-tick'></i> " + lang.delete,
                        className: "btn-warning verify_pin",
                        callback: function () {
                            var pos_pin = md5($('#pos_pin').val());
                            if (pos_pin == Settings.pin_code) {
                                delete spositems[item_id];
                                row.remove();
                                if (spositems.hasOwnProperty(item_id)) { } else {
                                    localStorage.setItem('spositems', JSON.stringify(spositems));
                                    loadItems();
                                }
                            } else {
                                bootbox.alert(lang.wrong_pin);
                            }
                        }
                    }
                }
            });
            boxd.on("shown.bs.modal", function () {
                if (Settings.display_kb == 1) { display_keyboards(); }
                $("#pos_pin").focus().keypress(function (e) {
                    if (e.keyCode == 13) {
                        e.preventDefault();
                        $('.verify_pin').trigger('click');
                        return false;
                    }
                });
            });
        } else {
            delete spositems[item_id];
            row.remove();
            if (spositems.hasOwnProperty(item_id)) { } else {
                localStorage.setItem('spositems', JSON.stringify(spositems));
                loadItems();
            }
        }
        return false;
    });











    $('#suspend').click(function () {
        if (count <= 1) {
            bootbox.alert(lang.please_add_product);
            return false;
        } else {
            $('#susModal').modal({ backdrop: 'static' });
        }
    });

    $('#suspend_sale').click(function () {
        ref = $('#reference_note').val();
        if (!ref || ref == '') {
            bootbox.alert(lang.type_reference_note);
            return false;
        } else {
            suspend = $('<span></span>');
            if (sid !== 0) {
                suspend.html('<input type="hidden" name="delete_id" value="' + sid + '" /><input type="hidden" name="suspend" value="yes" /><input type="hidden" name="suspend_note" value="' + ref + '" />');
            } else {
                suspend.html('<input type="hidden" name="suspend" value="yes" /><input type="hidden" name="suspend_note" value="' + ref + '" />');
            }
            suspend.appendTo("#hidesuspend");
            $('#pos-sale-form').submit();
        }
    });

    $('#payment').click(function () {



        if (count <= 1) {
            bootbox.alert(lang.please_add_product);
            return false;
        } else {

            //***********************TRJ033 - ALEXANDER ROCA - 06/04/2019***************************
            var valorClase = document.getElementsByClassName('sprice');
            var sensorCero = 0;
            //window.stop();
            //alert("no existe");
            //bootbox.alert("hola "+valorClase[i].innerHTML);

            for (var i = 0; i < valorClase.length; i++) {

                if (valorClase[i].innerHTML <= 0) {
                    sensorCero = 1;
                    //bootbox.alert("todos los productos deben tener un precio mayor a 0");
                    break;
                }

            }

            if (sensorCero == 0) {

                if (sid) {
                    suspend = $('<span></span>');
                    suspend.html('<input type="hidden" name="delete_id" value="' + sid + '" />');
                    suspend.appendTo("#hidesuspend");
                }

                gtotal = formatDecimal(total - order_discount + order_tax);
                if (Settings.rounding != 0) {
                    round_total = formatDecimal(roundNumber(gtotal, parseInt(Settings.rounding)));
                    var rounding = formatDecimal(round_total - gtotal);
                    $('#twt').text(formatMoney(round_total) + ' (' + formatMoney(rounding) + ')');
                    $('#quick-payable').text(formatMoney(round_total));
                } else {
                    $('#twt').text(formatMoney(gtotal));
                    $('#quick-payable').text(formatMoney(gtotal));
                }
                $('#item_count').text((an - 1) + ' (' + (count - 1) + ')');
                $("#order_quantity").val(count - 1);
                $("#order_items").val(an - 1);
                $('#balance').text('0.00');
                $('#payModal').modal({ backdrop: 'static' });


            } else {
                bootbox.alert("Todos los productos deben tener un precio unitario mayor a 0");
            }
            //***********************TRJ033 - ALEXANDER ROCA - 06/04/2019***************************

        }
    });






    $('#canal_id').change(function () {
        $("#canal_id_val").val($(this).val());
    });
    $('#payModal').on('shown.bs.modal', function (e) {
        $('#amount').focus();
    });
    $('#payModal').on('hidden.bs.modal', function (e) {
        $('#amount').val('').change();
    });

    $('#amount').change(function (e) {
        var total_paying = $(".amount").val();
        $('#total_paying').text(formatMoney(total_paying));
        if (Settings.rounding != 0) {
            $('#balance').text(formatMoney(total_paying - round_total));
            $('#balance_val').val(formatDecimal(total_paying - round_total));
            total_paid = total_paying;
            grand_total = round_total;
        } else {
            $('#balance').text(formatMoney(total_paying - gtotal));
            $('#balance_val').val(formatDecimal(total_paying - gtotal));
            total_paid = total_paying;
            grand_total = gtotal;
        }
    });

    $('#add-customer').click(function () {
        

        $('#customerModal').modal({ backdrop: 'static' });

        $("#idclienteedit").val("");
        $("#name").val("");
        $("#email").val("");
        $("#phone").val("");
        $("#cf1").val("");
        $("#cf2").val("");
        $("#cdireccion").val("");
        $('#document-type').val("").trigger('change.select2');
        $("#customer_type").val(0).trigger('change.select2');
        $("#input_hidden_document_type").val("");
        $("#input_hidden_person_type").val("");
        $("#name").removeAttr("readOnly");
        $("#cf1").removeAttr("readOnly");
        $("#cf2").removeAttr("readOnly");
        $("#div-document_type *").prop("disabled", false);
        $("#div-customer_type *").prop("disabled", false);

        $("#person-natural").prop("checked", true);
        $('#nombre-persona').text('Nombre *');
        $('#div-cf2').show();
        $("#cModalLabel").text("Nuevo Cliente");

        if ($('#person-natural').is(':checked')) {
            $('#document-type option[value="6"]').prop('disabled', true);

        }



    });




    $('#edit-customer').click(function () {
        code = $("#spos_customer").val();
        console.log(code)
        $.ajax({
            type: "get",
            url: base_url + 'customers/get_customers_byid/' + code,
            dataType: "json",
            success: function (data) {
                // console.log(data.names);
                // console.log(data.person_type+" "+data.document_type_id);
                /**********************TRJ073 - KENY PONTE 23/08/2019*****************/

                /*TRJ124-AMADOR*/
                $("#idclienteedit").val(data.id);
                $("#accion-cliente").val(2);
                /*    document.getElementById("document_type_edit").value=data.document_type_id;*/
                /*END*/

                $("#name").val(data.name);
                $("#email").val(data.email);
                $("#phone").val(data.phone);
                $("#cf1").val(data.cf1);
                $("#cf2").val(data.cf2);
                $("#cdireccion").val(data.direccion);
                $('#document-type').val(data.document_type_id).trigger('change.select2');
                $("#customer_type").val(data.customers_type_id).trigger('change.select2');
                $("#input_hidden_document_type").val(data.document_type_id);
                $("#input_hidden_person_type").val(data.person_type);

                $("#name").attr("readOnly", true);
                $("#cf1").attr("readOnly", true);
                $("#cf2").attr("readOnly", true);
                $("#div-document_type *").prop("disabled", true);
                $("#div-customer_type *").prop("disabled", true);



                if (data.person_type == 1) {
                    // $("#person3").val(data.person_type);
                    $("#person-natural").prop("checked", true)
                    $('#nombre-persona').text('Nombre *');
                    $('#nombre-direccion').text('Dirección *');
                }
                else if (data.person_type == 2) {
                    // $("#person4").val(data.person_type);
                    $("#person-juridica").prop("checked", true)
                    $("#cf1").val(data.cf2);
                    $('#div-cf2').hide();
                    $('#nombre-direccion').text('Dirección *');

                }


                var titleNamevalue = $("#cf1").val();
                $("#cModalLabel").text("Cliente - " + titleNamevalue);

                // var newlangforbutton = "Editar Cliente";
                // var button = document.getElementById("add_customer");
                // button.innerHTML = newlangforbutton;



                // $("#document_type2").val(data.document_type_id);
                /**********************TRJ073 - KENY PONTE 23/08/2019*****************/

            }
        });
        //
        if (protect_delete == 1) {
            var boxd = bootbox.dialog({
                title: lang.enter_pin_code,
                closeButton: true,
                message: '<input id="pos_pin" name="pos_pin" type="password" placeholder="Pin Code" class="form-control kb-pad" autocomplete="off"> ',
                buttons: {
                    danger: {
                        label: lang.close,
                        className: "btn-default pull-left",
                        callback: function () { }
                    },
                    success: {
                        label: "<i class='fa fa-tick'></i> " + lang.edit,
                        className: "btn-warning verify_pin",
                        callback: function () {
                            var pos_pin = md5($('#pos_pin').val());
                            if (pos_pin == Settings.pin_code) {

                                $('#customerModal').modal({ backdrop: 'static' });

                            } else {
                                bootbox.alert(lang.wrong_pin);
                            }
                        }
                    }
                }
            });
            boxd.on("shown.bs.modal", function () {
                if (Settings.display_kb == 1) { display_keyboards(); }
                $("#pos_pin").focus().keypress(function (e) {
                    if (e.keyCode == 13) {
                        e.preventDefault();
                        $('.verify_pin').trigger('click');
                        return false;
                    }
                });
            });
        } else {
            //
            $('#customerModal').modal({ backdrop: 'static' });
        }
    });

    $('#search-customer').click(function () {
        $('#searchProducts').modal({ backdrop: 'static' });
    });






    $('#product_search,#prin_activ_search,#acc_farm_search').bind('keypress', function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            $("#btn_search_product").trigger("click");
            // $(this).autocomplete("search");
        }
    });

    $('#payModal').on('change', '#document_type', function () {
        var p_val = $(this).val();
        $('#document_type_val').val(p_val);
        console.log('pieros');
        showHideRetainer();

    });

    $('#payModal').on('change', '#custom_field_2', function () { //tec_sales
        var p_val = $(this).val();
        $('#custom_field_2_val').val(p_val);
    });

    if (custom_field_2 = get('custom_field_2')) {
        $('#custom_field_2_val').val(custom_field_2);
        $('#custom_field_2').val(custom_field_2);
    }





    $('#payModal').on('change', '#custom_field_1', function () { //tec_sales
        var p_val = $(this).val();
        $('#custom_field_1_val').val(p_val);
        store('custom_field_1', p_val);
    });

    if (custom_field_1 = get('custom_field_1')) {
        $('#custom_field_1').val(custom_field_1);
        $('#custom_field_1_val').val(custom_field_1);
    }

    $('#payModal').on('change', '#paid_by', function () {
        var p_val = $(this).val();
        $('#paid_by_val').val(p_val);
        var gtotal = formatDecimal(total - order_discount + order_tax);
        if (Settings.rounding != 0) {
            var rounded_total = formatMoney(roundNumber(gtotal, parseInt(Settings.rounding)));
        } else {
            var rounded_total = formatMoney(gtotal);
        }
        $('#rpaidby').val(p_val);
        if (p_val == 'gift_card') {
            $('.gc').slideDown();
            $('.ngc').slideUp('fast');
            setTimeout(function () { $('#gift_card_no').focus(); }, 10);
            $('#amount').attr('readonly', true);
        } else {
            $('.ngc').slideDown();
            $('.gc').slideUp('fast');
            $('#gc_details').html('');
            $('#amount').attr('readonly', false);
        }
        if (p_val == 'cash' || p_val == 'other') {
            $('.pcash').slideDown();
            $('.pcheque').slideUp('fast');
            $('.pcc').slideUp('fast');
            $('#amount').val(rounded_total);
            setTimeout(function () { $('#amount').focus(); }, 10);
        } else if (p_val == 'CC' || p_val == 'stripe') {
            $('.pcc').slideDown();
            $('.pcheque').slideUp('fast');
            $('.pcash').slideUp('fast');
            $('#amount').val(rounded_total);
            setTimeout(function () { $('#amount').focus(); }, 10);//sin sistema de tarjetas
            // setTimeout(function(){ $('#swipe').val('').focus(); }, 10);
        } else if (p_val == 'Cheque') {
            $('.pcheque').slideDown();
            $('.pcc').slideUp('fast');
            $('.pcash').slideUp('fast');
            $('#amount').val(rounded_total);
            setTimeout(function () { $('#cheque_no').focus(); }, 10);
        } else {
            $('.pcheque').hide();
            $('.pcc').hide();
            $('.pcash').hide();
        }
    });

    $(document).on('change', '.gift_card_no', function () {
        var cn = $(this).val() ? $(this).val() : '';
        if (cn != '') {
            $.ajax({
                type: "get", async: false,
                url: base_url + "pos/validate_gift_card/" + cn,
                dataType: "json",
                success: function (data) {
                    if (data === false || data.balance < 0) {
                        $('#gift_card_no').parent('.form-group').addClass('has-error');
                        bootbox.alert(lang.incorrect_gift_card);
                    } else {
                        $('#gc_details').html(lang.card_no + ': ' + data.card_no + '<br>' + lang.value + ': ' + data.value + ' - ' + lang.balance + ': ' + data.balance);
                        $('#gift_card_no').parent('.form-group').removeClass('has-error');
                        var paying = (gtotal > data.balance) ? data.balance : gtotal;
                        $('#amount_val').val(paying);
                        $('#amount').val(paying);
                    }
                }
            });
        }
        return false;
    });

    $(document).on('click', '.quick-cash', function () {
        var $quick_cash = $(this);
        var amt = $quick_cash.contents().filter(function () {
            return this.nodeType == 3;
        }).text();
        var th = Settings.thousands_sep == 0 ? '' : Settings.thousands_sep;
        var $pi = $('#amount');
        amt = formatDecimal(amt.split(th).join("")) * 1 + $pi.val() * 1;
        $pi.val(formatDecimal(amt)).change().focus();
        var note_count = $quick_cash.find('span');
        if (note_count.length == 0) {
            $quick_cash.append('<span class="badge">1</span>');
        } else {
            note_count.text(parseInt(note_count.text()) + 1);
        }
    });

    $(document).on('click', '#clear-cash-notes', function () {
        $('.quick-cash').find('.badge').remove();
        $('#amount').val('').change().focus();
    });

    $('#payModal').on('change', '#amount', function (e) {
        $('#amount_val').val($(this).val()).focus();//sin sistema de tarjeta (focus)
    });
    $('#payModal').on('blur', '#amount', function (e) {
        $('#amount_val').val($(this).val());
    });
    $('#payModal').on('select2-close', '#paid_by', function (e) {
        $('#paid_by_val').val($(this).val());
    });

    $('#payModal').on('change', '#pcc_no', function (e) {
        $('#cc_no_val').val($(this).val());
    });
    $('#payModal').on('change', '#pcc_holder', function (e) {
        $('#cc_holder_val').val($(this).val());
    });
    $('#payModal').on('change', '#gift_card_no', function (e) {
        $('#paying_gift_card_no_val').val($(this).val());
    });
    $('#payModal').on('change', '#pcc_month', function (e) {
        $('#cc_month_val').val($(this).val());
    });
    $('#payModal').on('change', '#pcc_year', function (e) {
        $('#cc_year_val').val($(this).val());
    });
    $('#payModal').on('change', '#pcc_type', function (e) {
        $('#cc_type_val').val($(this).val());
    });
    $('#payModal').on('change', '#pcc_cvv2', function (e) {
        $('#cc_cvv2_val').val($(this).val());
    });
    $('#payModal').on('change', '#cheque_no', function (e) {
        $('#cheque_no_val').val($(this).val());
    });
    $('#payModal').on('change', '#payment_note', function (e) {
        $('#payment_note_val').val($(this).val());
    });
    $('#payModal').on('change', '#note', function (e) {
        var n = $(this).val();
        store('spos_note', n);
        $('#spos_note').val(n);
    });
    if (spos_note = get('spos_note')) {
        $('#note').val(spos_note);
        $('#spos_note').val(spos_note);
    }
    $('#spos_customer').change(function (e) {
        store('spos_customer', $(this).val());
    });
    if (spos_customer = get('spos_customer')) {
        $('#spos_customer').select2('val', spos_customer);
    }

    $(".modal").each(function (index) {
        $(this).on('show.bs.modal', function (e) {
            var open = $(this).attr('data-easein');
            $('.modal-dialog').velocity('transition.' + open);
        });
    });

    $('[data-toggle="ajax"]').click(function (event) {
        event.preventDefault();
        var href = $(this).attr('href');
        $.get(href, function (data) {
            $("#posModal").html(data);
            $("#posModal").modal({ backdrop: 'static' });
            return false;
        });
    });

    var pay_sale = true;
    var pay_caja = true;

    if (typeof $('#submit-sale').val() === 'undefined' || $('#submit-sale').val() === null) {
        pay_sale = false;
    }

    if (typeof $('#submit-sale_caja').val() === 'undefined' || $('#submit-sale_caja').val() === null) {
        pay_caja = false;
    }

    if (pay_sale) {
        $('#submit-sale').click(function () {
            var boton = document.getElementById('submit-sale');
            boton.disabled = true;
            if (pay_caja) {
                var boton_caja = document.getElementById('submit-sale_caja');
                boton_caja.disabled = true;
            }
            $('#total_items').val(an - 1);
            $('#total_quantity').val(count - 1);
            $('#submit_type').val("1");
            $('#submit').click();
        });
    }

    if (pay_caja) {
        $('#submit-sale_caja').click(function () {
            if (pay_sale) {
                var boton = document.getElementById('submit-sale');
                boton.disabled = true;
            }
            $('#submit-sale_caja').attr("disabled");//desabilita boton
            $('#total_items').val(an - 1);
            $('#total_quantity').val(count - 1);
            $('#submit_type').val("2");
            $('#submit').click();
        });
    }

    $('#suspend_sale').click(function () {
        if ($('#reference_note').val()) {
            $('#hold_ref').val($('#reference_note').val());
            $('#total_items').val(an - 1);
            $('#total_quantity').val(count - 1);
            $('#submit').click();
        }
        return false;
    });

    $("#customer-form").on("submit", function (e) {
        var numeroruc = $("#cf2").val();
        var url = 'customers/add';
        var idCliente = $('#idclienteedit').val();

        console.log("idCliente:", idCliente); // Agrega esta línea para depurar

        if (idCliente != "") {
            url = 'customers/edit/' + idCliente;

        }

        if (numeroruc.substring(0, 2) == "20") {
            mensaje_alerta('El RUC ingresado pertenece a una persona jurídica.');

            return false;
        }

        console.log(base_url + url)
        e.preventDefault();
        $.ajax({
            type: "post",
            url: base_url + url,
            data: $(this).serialize(),
            dataType: "json",
            success: function (res) {
                console.log("Success response:", res); // Agrega esta línea para depurar
                if (res.status == 'success') {
                    console.log("Repuesta",res);

                    if (idCliente=="") {
                        $('#spos_customer').append($("<option></option>").attr("value", res.id).text(res.val));
                    }


                    $('#spos_customer').select2('val', res.id);
                    $('#customerModal').modal('hide');
                    /*TRJ124-amador*/
                    document.getElementById("customer_type").value = 0;
                    document.getElementById("cdireccion").value = '';

                    $('#alert').html();
                    $('#alert').hide();

                    /*end*/
                } else {
                    $('#alert').html(res.msg);
                    $('#alert').show();
                }
            },
            error: function () {
                console.error("AJAX Error"); // Agrega esta línea para depurar
                bootbox.alert(lang.customer_request_failed);
                return false;
            }
        });


        return false;
    });


    // $("#customer-edit-form").on("submit", function (e) {
    //     e.preventDefault();
    //     $.ajax({
    //         type: "put",
    //         url: base_url + 'customers/edit/' + $("#spos_customer").val(),
    //         data: $(this).serialize(),
    //         dataType: "json",
    //         success: function (res) {
    //             if (res.status == 'success') {

    //                 $('#customerModal').modal('hide');

    //             } else {

    //                 $('#c-alert2').html(res.msg);
    //                 $('#c-alert2').show();
    //             }
    //         },
    //         error: function () {
    //             bootbox.alert(lang.customer_request_failed);
    //             return false;
    //         }
    //     });
    //     return false;
    // });

    $('#customerModal').on('hidden.bs.modal', function (e) {
        $('#c-alert').hide();
        $('#cname').val('');
        $('#cemail').val('');
        $('#cphone').val('');
        $('#cf1').val('');
        $('#cf2').val('');
    });

});




function display_keyboards() {

    $('.kb-text').keyboard({
        autoAccept: true,
        alwaysOpen: false,
        openOn: 'focus',
        usePreview: false,
        // layout: 'qwerty',
        layout: 'custom',
        display: {
            'bksp': "\u2190",
            'accept': 'return',
            'default': 'ABC',
            'meta1': '123',
            'meta2': '#+='
        },
        customLayout: {
            'default': [
                'q w e r t y u i o p {bksp}',
                'a s d f g h j k l {enter}',
                '{s} z x c v b n m , . {s}',
                '{meta1} {space} {cancel} {accept}'
            ],
            'shift': [
                'Q W E R T Y U I O P {bksp}',
                'A S D F G H J K L {enter}',
                '{s} Z X C V B N M / ? {s}',
                '{meta1} {space} {meta1} {accept}'
            ],
            'meta1': [
                '1 2 3 4 5 6 7 8 9 0 {bksp}',
                '- / : ; ( ) \u20ac & @ {enter}',
                '{meta2} . , ? ! \' " {meta2}',
                '{default} {space} {default} {accept}'
            ],
            'meta2': [
                '[ ] { } # % ^ * + = {bksp}',
                '_ \\ | &lt; &gt; $ \u00a3 \u00a5 {enter}',
                '{meta1} ~ . , ? ! \' " {meta1}',
                '{default} {space} {default} {accept}'
            ]
        }
    });

    $('.kb-pad').keyboard({
        restrictInput: true,
        preventPaste: true,
        autoAccept: true,
        alwaysOpen: false,
        openOn: 'click',
        usePreview: false,
        layout: 'costom',
        display: {
            'b': '\u2190:Backspace'
        },
        customLayout: {
            'default': [
                '1 2 3 {b}',
                '4 5 6 . {clear}',
                '7 8 9 0 %',
                '{accept} {cancel}'
            ]
        }
    });

}




function nav_pointer() {
    var pp = p_page == 'n' ? 0 : p_page;
    (pp == 0) ? $('#previous').attr('disabled', true) : $('#previous').attr('disabled', false);
    ((pp + pro_limit) > tcp) ? $('#next').attr('disabled', true) : $('#next').attr('disabled', false);
}

function Popup(data) {
    var mywindow = window.open('', 'spos_print', 'height=500,width=300');
    mywindow.document.write('<!DOCTYPE html><html><head><title>Print</title>');
    mywindow.document.write('<link rel="stylesheet" href="' + assets + 'bootstrap/css/bootstrap.min.css" type="text/css" />');
    mywindow.document.write('<style>a {color: #333;} #totaltbl td, #totaltbl th { vertical-align: middle; }</style>');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');
    mywindow.print();
    mywindow.close();
    return true;
}

$(document).ready(function ($) {
    window.setTimeout(function () {
        $('.alerts').slideUp();
    }, 15000);
    $('.alerts').on('click', function (e) {
        $(this).slideUp();
    });
    $('html').perfectScrollbar();
    $('.navbar .menu').perfectScrollbar({ suppressScrollX: true });
    $('.control-sidebar').perfectScrollbar({ suppressScrollX: true });
    $('#list-table-div').perfectScrollbar({ suppressScrollX: true });
    $('.items').perfectScrollbar({ suppressScrollX: true });
});

function posScreen() {
    var wh = $(window).height(),
        total_dh = $('#totaldiv').height(),
        buttons_dh = $('.botbuttons').height();
    var items_dh = wh - 120, list_table_dh = wh - 258 - total_dh - buttons_dh;
    $('#right-col').height(wh - 100);
    $('.items').height((items_dh > 400 ? items_dh : 400));
    $('#list-table-div').height(list_table_dh);
    $('html').perfectScrollbar('update');
    $('.items').perfectScrollbar('update');
    $('#list-table-div').perfectScrollbar('update');
}

function printLine(str) {
    var size = Settings.char_per_line;
    var len = str.length;
    var res = str.split(":");
    var newd = res[0];
    for (i = 1; i < (size - len); i++) {
        newd += " ";
    }
    newd += res[1];
    return newd;
}

$(window).bind("resize", posScreen);

function read_card() {

}

$.extend($.keyboard.keyaction, {
    enter: function (base) {
        base.accept();
    }
});

$(document).ready(function () {

    $('.swipe').keypress(function (e) {
        var TrackData = $(this).val() ? $(this).val() : '';
        if (TrackData != '') {
            if (e.keyCode == 13) {
                e.preventDefault();
                var p = new SwipeParserObj(TrackData);

                if (p.hasTrack1) {

                    var CardType = null;
                    var ccn1 = p.account.charAt(0);
                    if (ccn1 == 4)
                        CardType = 'Visa';
                    else if (ccn1 == 5)
                        CardType = 'MasterCard';
                    else if (ccn1 == 3)
                        CardType = 'Amex';
                    else if (ccn1 == 6)
                        CardType = 'Discover';
                    else
                        CardType = 'Visa';

                    $('#pcc_no').val(p.account).change();
                    $('#pcc_holder').val(p.account_name).change();
                    $('#pcc_month').val(p.exp_month).change();
                    $('#pcc_year').val(p.exp_year).change();
                    $('#pcc_cvv2').val('');
                    $('#pcc_type').select2('val', CardType);

                } else {
                    $('#pcc_no').val('').change();
                    $('#pcc_holder').val('').change();
                    $('#pcc_month').val('').change();
                    $('#pcc_year').val('').change();
                    $('#pcc_cvv2').val('').change();
                    $('#pcc_type').val('').change();
                }

                $('#pcc_cvv2').focus();
            }
        }

    }).blur(function (e) {
        $(this).val('');
    }).focus(function (e) {
        $(this).val('');
    });

    $(document).on('blur', '#pcc_no', function () {
        var cn = $(this).val();
        var ccn1 = cn.charAt(0);
        if (ccn1 == 4)
            CardType = 'Visa';
        else if (ccn1 == 5)
            CardType = 'MasterCard';
        else if (ccn1 == 3)
            CardType = 'Amex';
        else if (ccn1 == 6)
            CardType = 'Discover';
        else
            CardType = 'Visa';

        $('#pcc_type').select2('val', CardType);
    });

    $('.modal').on('hidden.bs.modal', function () {
        $(this).removeData('bs.modal');
    });
    $('#clearLS').click(function (event) {
        bootbox.confirm(lang.r_u_sure, function (result) {
            if (result == true) {
                localStorage.clear();
                location.reload();
            }
        });
        return false;
    });

    if (Settings.focus_add_item != '') {
        shortcut.add(Settings.focus_add_item, function () { $("#add_item").focus(); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }
    if (Settings.add_customer != '') {
        shortcut.add(Settings.add_customer, function () { $("#add-customer").trigger('click'); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }
    if (Settings.toggle_category_slider != '') {
        shortcut.add(Settings.toggle_category_slider, function () { $('[data-toggle="control-sidebar"]').trigger('click'); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }
    if (Settings.cancel_sale != '') {
        shortcut.add(Settings.cancel_sale, function () { $("#reset").click(); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }
    if (Settings.suspend_sale != '') {
        shortcut.add(Settings.suspend_sale, function () { $("#suspend").trigger('click'); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }
    if (Settings.print_order != '') {
        shortcut.add(Settings.print_order, function () { $("#print_order").click(); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }
    if (Settings.print_bill != '') {
        shortcut.add(Settings.print_bill, function () { $("#print_bill").click(); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }
    if (Settings.finalize_sale != '') {
        shortcut.add(Settings.finalize_sale, function () { $("#payment").trigger('click'); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }
    if (Settings.today_sale != '') {
        shortcut.add(Settings.today_sale, function () { $("#today_sale").click(); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }
    if (Settings.open_hold_bills != '') {
        shortcut.add(Settings.open_hold_bills, function () { $("#opened_bills").trigger('click'); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }
    if (Settings.close_register != '') {
        shortcut.add(Settings.close_register, function () { $("#close_register").click(); }, { 'type': 'keydown', 'propagate': false, 'target': document });
    }

});

$.ajaxSetup({ cache: false, headers: { "cache-control": "no-cache" } });

function chececkTaxBP() {

    $('body').on('click', '.checkboxICBPER', function () {
        var row = $(this).closest('tr');
        var item_id = row.attr('data-item-id');
        var producto = row.attr('id');

        var porId = document.getElementById('tax_ICBPER_' + producto).checked;

        if (porId) {
            spositems[item_id].tax_ICBPER = 1;
            alert("Se va realizar el cobro del impuesto a las bolsas por la cantidad de productos de este Item (cantidad redondeada, si la cantidad es menor a 0.5 el monto del impuesto a las bolsas de este item sera 0)");
        } else {
            spositems[item_id].tax_ICBPER = 0;
            //alert("desactivado");
        }

        spositems[item_id].edit = 1;
        store('spositems', JSON.stringify(spositems));
        loadItems();
        return true;



    });

}


// <!--********START TRJ143 - DANIEL PADILLA - 20/12/2022*****************-->
function checkIfIsRetainer() {
    var customer_selected = $("#spos_customer").find(':selected');
    //console.log('customer_selected.val() = ', customer_selected.val())
    if (!customer_selected.val()) return false

    var is_retainer = customer_selected.attr('data-isretainer')
    var retainer_percentage = customer_selected.attr('data-retainerpercentage')

    //console.log('is_retainer = ', is_retainer)
    //console.log('retainer_percentage = ', retainer_percentage)

    if ($('#document_type').val() == 2 && is_retainer == 1) { // Si documento es factura y es cliente retenedor
        $("#lbl_retainer_percentage").html(retainer_percentage + "%");
        $("#retainer_percentage").val(retainer_percentage);
        return {
            is_retainer: is_retainer,
            retainer_percentage: retainer_percentage
        }
    }
}

function showHideRetainer() {
    $("#customer-retainer").hide(600);
    //console.log($("#customer-retainer"));
    //console.log('arriba del check')
    $("#retainer_percentage").val("0.00");
    $("#lbl_retainer_percentage").html("-");
    var customer_retainer = checkIfIsRetainer();
    if (customer_retainer) {
        $("#customer-retainer").show(600);
    }
}
// <!--********END TRJ143 - DANIEL PADILLA - 20/12/2022*****************-->
