<script>
    function enabletaxes(val){
        if(val==='yes'){
            enabtax=true;
            $("#divtaxes").slideDown();
            updatetotal();
        } else {
            enabtax=false;
            $("#divtaxes").slideUp();
            updatetotal();
        }
    }


    function single_discount_pass(id){
        $("#discount_by_product"+id).on('click',function() {
            if($(this).attr("readonly")){
                $("#invoicepass").modal("show");
            }
        });
        
        $("#discount_by_product"+id).dblclick(function() {
            $("#invoicepass").modal("show");
        });
        
        $("#perc_discount_product"+id).on('click',function() {
            if($(this).attr("readonly")){
                $("#invoicepass").modal("show");
            }
        });
        
        $("#perc_discount_product"+id).dblclick(function() {
            $("#invoicepass").modal("show");
        });
    }
    
    
    function perc_discount_product(discount, idproducts){
    //todo    
    console.log('perc_discount_product');
        var inputname="#perc_discount_product"+idproducts;
        discount= $(inputname).val();

       // console.log(discount);

        var orignal_unitcost = parseFloat($('#orignal_product_price'+idproducts).val());
        var qty =  parseFloat($('#qty'+idproducts).val());
        
        /////////////////////////
        var additional_cost = parseFloat($('#addition_by_product' + idproducts).val());
        additional_cost= additional_cost * qty;
        if(additional_cost > 0){
            var temp = '#unitcost' + idproducts + ' .combat';
            orignal_unitcost = orignal_unitcost + additional_cost;
            $(temp).html(orignal_unitcost.toFixed(2));            
          //  original_row_sum();
        }
        ///////////////////////////////
        
        
        
        if(discount === ""){
            $('#unitcost'+idproducts).text(orignal_unitcost.toFixed(2));
            var temp='#discount_by_product'+idproducts;
            $(temp).val('');
            product_row_sum(); 
        }else{
            discount = (parseFloat(discount)*orignal_unitcost)/100; 
            var temp='#discount_by_product'+idproducts;
            $(temp).val(Math.round(discount));
        }
        
        //discount_by_product(discount, idproducts, qty);
        
    }
    
    function discount_by_product(discount, idproducts, qty=0){
        console.log('discount_by_product');
        var inputname="#discount_by_product"+idproducts;
        discount= $(inputname).val();

        var orignal_unitcost = parseFloat($('#orignal_product_price'+idproducts).val());
        
        /////////////////////////
        var additional_cost = parseFloat($('#addition_by_product' + idproducts).val());
        additional_cost= additional_cost * qty;
        //console.log('addition '+additional_cost);
        if(additional_cost > 0){
            var temp = '#unitcost' + idproducts + ' .combat';
            orignal_unitcost = orignal_unitcost + additional_cost;
            $(temp).html(orignal_unitcost.toFixed(2));            
           // original_row_sum();
        }
        ///////////////////////////////
        
        
       // console.log(discount);
       // console.log(orignal_unitcost);
        if(discount === ""){
            $('#unitcost'+idproducts).text(orignal_unitcost.toFixed(2));
            product_row_sum(); 
            var temp='#perc_discount_product'+idproducts;
            $(temp).val('');
        }else{
            discount = parseFloat(discount);
            var discount_rate = orignal_unitcost - discount;
            $('#unitcost'+idproducts).text(discount_rate.toFixed(2));
            product_row_sum();
            
            var perc_discount = (100*discount)/orignal_unitcost;
            var temp='#perc_discount_product'+idproducts;
            $(temp).val(Math.round(perc_discount));
            
        }
        //calcdiscount_perc();
        updatetotal();
    }

    
    
    function updateothercharges(){
        $("#txtdiscount").val(0);
        $("#discount_in_percent").val(0)
        calcdiscount_perc();
    }
    
    function calcdiscount_perc(){
        //console.log('calcdiscount_perc');
        if($("#txtothercharges").val()==""){$("#txtothercharges").val(0);}
        var result=0;
        if($("#discount_in_percent").val() !== "" && $("#discount_in_percent").val()>0){
            result = (parseInt($("#txtsubtotal").val()) + parseInt($("#txtothercharges").val())) * parseInt($("#discount_in_percent").val());
            result=result/100;
        }
        
        $("#txtdiscount").val(Math.round(result));
        //updatetotal();
        //$("#paid").val($("#grandtotal").val());
        //$("#balanceamount").val(parseFloat($("#grandtotal").val()) - parseFloat($("#paid").val()));
        updatetotal();
    }
    
    
    function percent_discount_calculation(){
        // console.log('calcdiscount_perc');
        var discount_in_percent = parseFloat($('#discount_in_percent').val());
        var sub_total_rs = parseFloat($('#txtsubtotal').val());
        var new_rs = ((discount_in_percent / 100) * sub_total_rs);
        $('#txtdiscount').val(Math.round(new_rs));
//        updatetotal();
//        $('#paid').val($('#txtgross').val());
        updatetotal();
    }
    
    function rupee_discount_calculation(){
         console.log('calcdiscount_perc');
        var discount_in_rupee = parseFloat($('#txtdiscount').val());
        var sub_total_rs = parseFloat($('#txtsubtotal').val());
        
        var new_percent = (discount_in_rupee / sub_total_rs) * 100;
        
        $('#discount_in_percent').val(Math.round(new_percent));
        percent_discount_calculation();
        
    }

    function updatepaying(){
        
        $("#paid").val(parseFloat($("#cashpaid").val()) + parseFloat($("#cardpaid").val()));
        //updatetotal();
    }
    
    function updatepayingcard(mixedcash=false){
        if(parseFloat($("#cc_charge").val()) > 0 && mixedcash==false){
          //  $("#divcccharge").show();
            $("#txtcc_charge").val("0");
         //   console.log('grandtotal : '+ parseFloat($("#grandtotal").val()) + " cc_charge : " + $("#cc_charge").val());
            var ccc= (parseFloat($("#cardpaid").val()) * parseFloat($("#cc_charge").val()))/100;
            ccc = Math.round(ccc);
            
            $("#txtcc_charge").val(ccc);
            $("#cardpaid").val(parseFloat($("#cardpaid").val()) + ccc);
        }
        
        $("#paid").val(parseFloat($("#cashpaid").val()) + parseFloat($("#cardpaid").val()));
        
       // updatetotal();
    }
    
    
     function updatecccharge(){
         //console.log('here');
            if(parseFloat($("#cc_charge").val()) > 0){
            
             //    $("#divcccharge").show();
                $("#txtcc_charge").val("0");
             
                var ccc= (parseFloat($("#paid").val()) * parseFloat($("#cc_charge").val()))/100;
                ccc = Math.round(ccc);
                
                $("#txtcc_charge").val(ccc);

            }  
            $("#paid").val(parseFloat($("#paid").val()) + ccc);
    }
    
    function validateVouchers(){
        
        if($('#voucherno').val() === "" || $('#voucherno').val() === "0"){
            swal({
                title: 'Empty Field',
                text: 'Please provide voucher number!',
                type: 'warning',
                confirmButtonText: 'OK!'
            });
            return false;
        } else{
            var voucherno = 'C'+$('#voucherno').val();
            
            $.ajax({
                type: 'POST',
                url: 'Voucher_controller/validateVoucher',
                data: {
                    voucherno: voucherno
                },
                success: function(response) {
                    var data = $.parseJSON(response);
                    if(data.length > 0){
                        var html = '<table class="table">';
                            html += '<thead>';
                                html += '<tr><th colspan="3">Customer Details</th></tr>';
                            html += '</thead>';
                            html += '<tbody>';
                                html += '<tr><td>Customer Name</td><td>'+data[0]['customer_name']+'</td></tr>';
                                html += '<tr><td>Customer Email</td><td>'+data[0]['customer_email']+'</td></tr>';
                                html += '<tr><td>Customer Phone</td><td>'+data[0]['customer_cell']+'</td></tr>';
                            html += '</tbody>';
                            html += '<thead>';
                                html += '<tr><th colspan="3">Voucher Details</th></tr>';
                            html += '</thead>';
                            html += '<tbody>';
                                html += '<tr><td>Type</td><td>'+data[0]['type'].toUpperCase()+'</td></tr>';
                                html += '<tr><td>Generated on</td><td>'+data[0]['voucher_date']+'</td></tr>';
                                html += '<tr><td>Expire on</td><td>'+data[0]['valid_until']+'</td></tr>';
                                html += '<tr><td>Voucher #</td><td>'+data[0]['voucher_number']+'</td></tr>';
                                
                                if(data[0]['type'] === 'amount'){
                                    html += '<tr><td>Total Amount <?php echo CURRENCY_CODE;?>.</td><td>'+data[0]['amount']+'</td></tr>';
                                    html += '<tr><td>Remaining Amount <?php echo CURRENCY_CODE;?>.</td><td>'+data[0]['remaining_amount']+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-size: 11px;">(This amount will be apply to the invoice)</span></td></tr>';
                                }
                                if(data[0]['type'] === 'service'){
                                    html += '<tr><td>Services: </td><td>'+data[0]['service_names'].replace(/\|/g, '<div style="border-bottom: 1px solid #eee;"></div>')+'</td></tr>';
                                }
                                
                                html += '<tr><td></td><td></td></tr>';
                            html += '</tbody>';
                        html += '</table>';
                        
                        $('#voucherType').val(data[0]['type']);
                        $('#voucherAmount').val(data[0]['amount']);
                        $('#voucherRemainingAmount').val(data[0]['remaining_amount']);
                        $('#voucherRemainingServices').val(data[0]['remaining_service_ids']);
                        $('#voucherHtml').html(html);
                        $('#voucherModal').modal({
                            backdrop:'static',
                            keyboard:false,
                            show:true
                        });
                        
                    } else{
                        swal({
                            title: 'Not Found / Expired',
                            text: 'The provided voucher number '+voucherno+' is not found or expired.',
                            type: 'error',
                            confirmButtonText: 'OK!'
                        });
                    }
                }
            });
        }
        
    }
    
    function applyVoucher(){
        
        var type = $('#voucherType').val();
        
        if(type === 'amount'){
            var remaining = parseInt($('#voucherRemainingAmount').val());
            var grandtotal = parseInt($('#grandtotal').val());
            if(remaining >= grandtotal){
                $('#paid').val(grandtotal);
                $('#voucherRemainingAmount').val(parseInt(remaining) - parseInt(grandtotal));
            }
            if(remaining < grandtotal){
                $('#paid').val(remaining);
                $('#voucherRemainingAmount').val('0');
            }
            updatetotal();
            $('#voucherModal').modal('hide');
        }
        if(type === 'service'){
            
            var sids = [];
            var remainingServiceIds = $('#voucherRemainingServices').val().split('|');
            
            $('input[name=service_ids]').each(function () {
                sids.push($(this).val());
            });
            
            var diff_ids = arrayDifference(remainingServiceIds, sids);
            var remainingservices = "";
            
            $.each(diff_ids, function( index, value ) {
                remainingservices += value+"|";
            });
            
            remainingservices = remainingservices.slice(0, -1);
            
            $('#voucherRemainingServices').val(remainingservices);
            
            var remaining = parseInt($('#voucherRemainingAmount').val());
            var grandtotal = parseInt($('#grandtotal').val());
            if(remaining >= grandtotal){
                $('#paid').val(grandtotal);
                $('#voucherRemainingAmount').val(parseInt(remaining) - parseInt(grandtotal));
            }
            if(remaining < grandtotal){
                $('#paid').val(remaining);
                $('#voucherRemainingAmount').val('0');
            }
            updatetotal();
            $('#voucherModal').modal('hide');
        }
        
    }
    
    function updateVoucher(){
        
        var remaining = $('#voucherRemainingAmount').val();
        var remaining_services = $('#voucherRemainingServices').val();
        var voucherno = 'C'+$('#voucherno').val();
        
        $.ajax({
            type: 'POST',
            url: 'Voucher_controller/updateRemainingAmount',
            data: {
                voucherno: voucherno,
                remaining: remaining,
                remaining_services: remaining_services
            },
            success: function(response) {
                if(response === 'success'){
                    $('button:contains("Verify")').hide();
                    if(parseInt(remaining) > 0){
                        var vmodal = $('#voucherModal');
                        var html = '<center><h5>Voucher # '+voucherno+' has remaining amount. Please print new slip for it.</h5>';
                        html += '<a onclick="hideVoucherModal();" href="<?php echo base_url(); ?>viewvoucher/'+$('#voucherno').val()+'" target="_blank"><i class="fa fa-print"></i> Print Preview</a></center>';
                        $('#voucherModalFooter').hide();
                        $('#voucherHtml').html(html);
                        vmodal.modal({
                            backdrop:'static',
                            keyboard:false,
                            show:true
                        });
                    }
                    return true;
                } else{
                    return false;
                }
            }
        });
        
    }
    
    function hideVoucherModal(){
        $('#voucherModal').modal('hide');
    }
    
    function opencc(){
        if($("#ccp").is(":hidden")){
            $("#ccp").slideDown();
        }
        if($("#voucherp").is(":visible")){
            $("#voucherp").slideUp();
        }
        if($("#balance").is(":visible")){
            $("#balance").slideUp();
        }
        if($("#ccp").is(":visible")){
            $("#checkp").slideUp();
        }
        if($("#payingcash").is(":visible")){
            $("#payingcash").slideUp();
        }
        if($("#payingcard").is(":visible")){
            $("#payingcard").slideUp();
        }

        if( parseFloat($("#cc_charge").val()) > 0){
            $("#divcccharge").show();
            $("#txtcc_charge").val("0");
//           
//            var ccc= (parseFloat($("#grandtotal").val()) * parseFloat($("#cc_charge").val()))/100;
//            $("#txtcc_charge").val(Math.round(ccc));
            
        }
        
        
        $("#cctip").show();
        $("#mode").html('Card');
        $("#input_payment_mode").val('Card');
        $("#paid").removeAttr("disabled");
        $("#cashpaid").val("0");
        $("#cardpaid").val("0");
        $("#paid").val("0");
        updatetotal();
    }
    function opendc(){
        if($("#ccp").is(":hidden")){
            $("#ccp").slideDown();
        }
        if($("#voucherp").is(":visible")){
            $("#voucherp").slideUp();
        }
        if($("#balance").is(":visible")){
            $("#balance").slideUp();
        }
        if($("#ccp").is(":visible")){
            $("#checkp").slideUp();
        }
        if($("#payingcash").is(":visible")){
            $("#payingcash").slideUp();
        }
        if($("#payingcard").is(":visible")){
            $("#payingcard").slideUp();
        }

        $("#divcccharge").hide();
        $("#txtcc_charge").val("0");
        
        
        $("#cctip").show();
        $("#mode").html('Card');
        $("#input_payment_mode").val('Card');
        $("#paid").removeAttr("disabled");
        $("#cashpaid").val("0");
        $("#cardpaid").val("0");
        $("#paid").val("0");
        updatetotal();
    }
    function openvoucher(mode){
        if($("#voucherp").is(":hidden")){
            $("#voucherp").slideDown();
            $('#paid').val('0');
        }
        if($("#checkp").is(":visible")){
            $("#checkp").slideUp();
        }
        if($("#balance").is(":visible")){
            $("#balance").slideUp();
        }
        if($("#ccp").is(":visible")){
            $("#ccp").slideUp();
        }
        if($("#payingcash").is(":visible")){
            $("#payingcash").slideUp();
        }
        if($("#payingcard").is(":visible")){
            $("#payingcard").slideUp();
        }
        
        $("#divcccharge").hide();
        $("#txtcc_charge").val("0");
        
        
        $("#cctip").hide();
        $("#mode").html(mode);
        $("#input_payment_mode").val(mode);
        $("#paid").removeAttr("disabled");
        $("#cashpaid").val("0");
        $("#cardpaid").val("0");
        $("#paid").val("0");
        updatetotal();
    }
    
    function openbank(mode){
        if($("#checkp").is(":hidden")){
            $("#checkp").slideDown();
        } 
        if($("#voucherp").is(":visible")){
            $("#voucherp").slideUp();
        }
        if($("#balance").is(":visible")){
            $("#balance").slideUp();
        }
        if($("#ccp").is(":visible")){
            $("#ccp").slideUp();
        }
        if($("#payingcash").is(":visible")){
            $("#payingcash").slideUp();
        }
        if($("#payingcard").is(":visible")){
            $("#payingcard").slideUp();
        }
        
        $("#divcccharge").hide();
        $("#txtcc_charge").val("0");
        
        
        $("#cctip").show();
        $("#mode").html(mode);
        $("#input_payment_mode").val(mode);
        $("#paid").removeAttr("disabled");
        $("#cashpaid").val("0");
        $("#cardpaid").val("0");
        $("#paid").val("0");
        updatetotal();
    }
    
    function openonline(mode){
        if($("#checkp").is(":hidden")){
            $("#checkp").slideDown();
        } 
        if($("#voucherp").is(":visible")){
            $("#voucherp").slideUp();
        }
        if($("#balance").is(":visible")){
            $("#balance").slideUp();
        }
        if($("#ccp").is(":visible")){
            $("#ccp").slideUp();
        }
        if($("#payingcash").is(":visible")){
            $("#payingcash").slideUp();
        }
        if($("#payingcard").is(":visible")){
            $("#payingcard").slideUp();
        }
        
        $("#divcccharge").hide();
        $("#txtcc_charge").val("0");
        
        
        $("#cctip").show();
        $("#mode").html(mode);
        $("#input_payment_mode").val(mode);
        $("#paid").removeAttr("disabled");
        $("#cashpaid").val("0");
        $("#cardpaid").val("0");
        $("#paid").val("0");
        updatetotal();
    }
    
    function opencash(){
        
        if($("#ccp").is(":visible")){
            $("#ccp").slideUp();
        }
        if($("#voucherp").is(":visible")){
            $("#voucherp").slideUp();
        }
        if($("#checkp").is(":visible")){
            $("#checkp").slideUp();
        }
        if($("#balance").is(":visible")){
            $("#balance").slideUp();
        }
        if($("#payingcash").is(":visible")){
            $("#payingcash").slideUp();
        }
        if($("#payingcard").is(":visible")){
            $("#payingcard").slideUp();
        }
        
        $("#divcccharge").hide();
        $("#txtcc_charge").val("0");
       
       
        $("#cctip").hide();
        $("#mode").html('Cash');
        $("#input_payment_mode").val('Cash');
        $("#paid").removeAttr("disabled");
        $("#cashpaid").val("0");
        $("#cardpaid").val("0");
        $("#paid").val("0");
        updatetotal();
    }
    
    function openmixed(){
        
        if($("#payingcash").is(":hidden")){
            $("#payingcash").slideDown();
        }
        if($("#payingcard").is(":hidden")){
            $("#payingcard").slideDown();
        }
        if($("#ccp").is(":visible")){
            $("#ccp").slideUp();
        }
        if($("#voucherp").is(":visible")){
            $("#voucherp").slideUp();
        }
        if($("#checkp").is(":visible")){
            $("#checkp").slideUp();
        }
        if($("#balance").is(":visible")){
            $("#balance").slideUp();
        }
        
        if(parseFloat($("#cc_charge").val()) > 0){
            $("#divcccharge").show();
            $("#txtcc_charge").val("0");
//            console.log('grandtotal : '+ parseFloat($("#grandtotal").val()) + " cc_charge : " + $("#cc_charge").val());
//            var ccc= (parseFloat($("#cardpaid").val()) * parseInt($("#cc_charge").val()))/100;
//            $("#txtcc_charge").val(Math.round(ccc));
            
        }
        
        $("#cctip").show();
        $("#mode").html('Mixed');
        $("#input_payment_mode").val('Mixed');
        $("#cashpaid").val("0");
        $("#cardpaid").val("0");
        $("#paid").val("0");
        $("#paid").attr("disabled","disabled");
        updatetotal();
        
    }
    
    function openbalance(){
        
        if($("#voucherp").is(":visible")){
            $("#voucherp").slideUp();
        }
        if($("#ccp").is(":visible")){
            $("#ccp").slideUp();
        }
        if($("#checkp").is(":visible")){
            $("#checkp").slideUp();
        }        
        if($("#balance").is(":hidden")){
            $("#balance").slideDown();
        }
        if($("#payingcash").is(":visible")){
            $("#payingcash").slideUp();
        }
        if($("#payingcard").is(":visible")){
            $("#payingcard").slideUp();
        }
        
        $("#divcccharge").hide();
        $("#txtcc_charge").val("0");
        
        
        $("#cctip").hide();
        $("#mode").html('Balance');
        $("#input_payment_mode").val('Balance');
        $("#paid").removeAttr("disabled");
        updatetotal();
    }
    

    function product_row_sum(){
        
        var theTotal = 0;
        var additionalTotal=0;
        var orgTotal = 0;
        
        $("table td:nth-child(11) .combat").each(function () {
            
            var val = $(this).text();//.replace(" ", "").replace(",-", "");
            theTotal += parseFloat(val);
        });

        
        
        $('table tr').each(function(row, tr) {
            
            if(row>0){
                var org = $(tr).find('td:eq(10) input[name=orignal_product_price]').val();
                var addition = $(tr).find('td:eq(9) input[name=addition_by_product]').val();
                var qty = $(tr).find('td:eq(5)').text();
                //added product tax
                
                
                orgTotal += parseFloat(org) + (parseFloat(addition) * parseFloat(qty));
                var tax_perc = $(tr).find('td:eq(11)').val();
                var taxedTotal = orgTotal*tax_perc/100;
                
            }
        });
        
        $("#txt_org_subtotal").val(orgTotal);
        $('#txtsubtotal').val(theTotal.toFixed(2));
        
        //added product tax
        
    }
    
    
    function original_row_sum(){
        var theTotal = 0; var additionalTotal = 0;
        $("table td:nth-child(11) input[name=orignal_product_price]").each(function () {
          //  console.log($(this).val());
            var val = $(this).val();//.replace(" ", "").replace(",-", "");
            theTotal += parseFloat(val);
        });
        $("table td:nth-child(10) .addition_by_product").each(function () {
          //  console.log('addition ' + $(this).val());
            var val = $(this).val();//.replace(" ", "").replace(",-", "");
            additionalTotal += parseFloat(val);
            
        });
        
        //console.log(Math.round(theTotal + additionalTotal));
        $('#txt_org_subtotal').val(Math.round(theTotal + additionalTotal));
    }

    function usecustomeradvance(){
        var customeradvance = parseFloat($("#customeradvance").val());
        var grandtotal = parseFloat($("#grandtotal").val());
        var remainingadvance =0;
        
        if(customeradvance >= grandtotal){
            $("#txtadvance").val(grandtotal);
            remainingadvance = customeradvance-grandtotal;
            $("#remainingretained").val(remainingadvance);
            $("#retainedamountused").val(grandtotal);
            
            updatetotal();
            
            $("#btncustomeradvance").css('display','none');
            $("#txtcustomeradvance").html("Customer's Remaining Available Amount: " + remainingadvance);
            $("#retainedused").val('Yes');
            
        } else {
            $("#txtadvance").val(customeradvance);
            $("#remainingretained").val('0');
            $("#retainedamountused").val(customeradvance);
            
            updatetotal();
            
            $("#btncustomeradvance").css('display','none');
            $("#txtcustomeradvance").html("Customer's Remaining Available Amount: " + remainingadvance);
            $("#retainedused").val('Yes');
        }
        
        
    }

    function loyaltydiscount(lmode){       
        
        var loyaltyotp='No';
        $.ajax({
            url:'<?php echo base_url();?>sms_controller/checkloyaltyotp',            
            dataType:"text",
            method:"POST",
            async:false,
            success:function(response){
                if(response=='Yes'){

                    loyaltyotp='Yes';

                } else {

                    loyaltyotp='No';

                }
            }
        });

            if(loyaltyotp == 'Yes'){ 
                ///Ask OTP
                var customercell=$("#customercell").val();
                
                ///send sms
                $.ajax({
                    url:'<?php echo base_url();?>sms_controller/send_otp_sms',
                    data:{
                        'msg': 'Your 4 digit OTP is ',
                        'customercell': customercell,
                        'using': 'LoyaltyOTP',
                        'customer_id': $("#customerid").val()
                    },
                    dataType:"text",
                    method:"POST",
                    async:false,
                    success:function(response){
                        //console.log(response)
                    }
                });

                swal({
                        title: "OTP",
                        text: "Please Enter The 4 Digit OTP Sent To "+customercell,
                        type: "input",
                        showCancelButton: true,
                        confirmButtonColor: '#ff5b5b !important',
                        cancelButtonColor: '#d33',
                        cancelButtonText: 'Cancel',
                        confirmButtonText: 'Submit'
                    }, function (inputValue) {
                        if (inputValue === false ){ return false;}
                        if (inputValue === "") {
                            swal.showInputError("You need to write something!");
                            return false;
                        }                        
                        
                        $.ajax({
                            url:'<?php echo base_url();?>sms_controller/check_customer_otp',
                            data:{                                
                                'input': inputValue,
                                'customer_id': $("#customerid").val()                                
                            },
                            dataType:"text",
                            method:"POST",
                            async:false,
                            success:function(response){
                                console.log(response)
                                if(response == 'verified'){
                                    console.log(lmode);
                                    if (lmode == 'PercDiscount') {
                                        loyaltydiscount_perc();
                                    } else if (lmode == 'RupeeDiscount') {
                                        loyaltydiscount_rupee();
                                    }                
                                } else {
                                    toastr.error('Invalid OTP Entered');
                                    return false;
                                }                                
                            }
                        }); 
                });
            } else { 
                console.log(lmode);
                if (lmode == 'PercDiscount') {
                    loyaltydiscount_perc();
                } else if (lmode == 'RupeeDiscount') {
                    loyaltydiscount_rupee();
                }
            }        
    }

    function loyaltydiscount_rupee(){
        var loyaltyrate=parseFloat($("#loyaltyrate").val());
        var loyaltydiscount=0;
        var loyaltypoints= parseFloat($("#loyaltypoints").val());
        loyaltydiscount=loyaltypoints*loyaltyrate;
        var loyaltyused=0;
        if(loyaltydiscount > parseFloat($("#txtsubtotal").val())){
            loyaltydiscount=parseFloat($("#txtsubtotal").val());           
        } 
        loyaltyused=loyaltydiscount/loyaltyrate;
        
        if(loyaltypoints>=1){
            if($("#btnloyalty").hasClass('btn-success')){
                $("#txtdiscount").val(loyaltydiscount);
                
                lu = parseFloat($("#txtdiscount").val())/loyaltyrate;
                $("#loyaltyused").val(loyaltyused);
                
                var parcentage = (parseFloat($("#txtdiscount").val()) / parseFloat($('#txtsubtotal').val())) * 100;
                 $('#txtdiscountperc').val(parcentage.toFixed(2));
                 
                $("#discount_remarks").val('Loyalty Points used for discount - ' + $("#discount_remarks").val());
                $("#ddiscount_remarks").val($("#discount_remarks").val());
                $("#txtloyalty").html('Loyalty');

                //$("#txtdiscount").attr('readonly','readonly');
                $("#btnloyalty").html('Remove Loyalty');
                $("#btnloyalty").removeClass('btn-success');
                $("#btnloyalty").addClass('btn-danger');


            } else {
                $("#loyaltyused").val('0');
                $("#txtdiscount").val('0');
                $("#txtdiscountperc").val('0');
                $("#txtdiscount").attr('readonly','readonly');
                var str=$("#discount_remarks").val();
                $("#discount_remarks").val(str.replace('Loyalty Points used for discount - ', ''));
                $("#ddiscount_remarks").val($("#discount_remarks").val());
                $("#txtloyalty").html('');

                $("#btnloyalty").html('Redeem Loyalty');
                $("#btnloyalty").removeClass('btn-danger');
                $("#btnloyalty").addClass('btn-success');

            }
            updatetotal();
        }
    }
    
    function loyaltydiscount_perc(){
        
        var loyaltydiscount=0;
        var loyaltypoints= parseFloat($("#loyaltypoints").val());
        
        var loyaltyrate= 0;
        var loyaltyused=0;
        
        if(loyaltypoints >= 50 && loyaltypoints < 70){
            loyaltyrate=5;
            loyaltyused=50;
        } else if (loyaltypoints >= 70 && loyaltypoints < 170){
            loyaltyrate=7;
            loyaltyused=70;
        } else if (loyaltypoints >= 170 && loyaltypoints < 250){
            loyaltyrate=10;
            loyaltyused=170;
        } else if (loyaltypoints >= 250 && loyaltypoints < 350){
            loyaltyrate=16;
            loyaltyused=250;
        } else if (loyaltypoints >= 350 && loyaltypoints < 500){
            loyaltyrate=20;
            loyaltyused=350;
        } else if (loyaltypoints >= 500){
            loyaltyrate=20;
            loyaltyused=500;
        }
        
        loyaltydiscount=(parseFloat($("#txtsubtotal").val())*loyaltyrate)/100;
        
        if($("#btnloyalty").hasClass('btn-success')){
            $("#txtdiscount").val(loyaltydiscount);
            $('#txtdiscountperc').val(loyaltyrate);
            $("#loyaltyused").val(loyaltyused);
            
            $("#discount_remarks").val('Loyalty Points used for discount - ' + $("#discount_remarks").val());
            $("#ddiscount_remarks").val($("#discount_remarks").val());
            $("#txtloyalty").html('Loyalty');
            $("#txtdiscount").attr('readonly','readonly');
            $("#btnloyalty").html('Remove Loyalty');
            $("#btnloyalty").removeClass('btn-success');
            $("#btnloyalty").addClass('btn-danger');
            
        } else {
            $("#loyaltyused").val('0');
            $("#txtdiscount").val('0');
            $("#txtdiscountperc").val('0');
            $("#txtdiscount").attr('readonly','readonly');
            
            var str=$("#discount_remarks").val();
            $("#discount_remarks").val(str.replace('Loyalty Points used for discount - ', ''));
            $("#ddiscount_remarks").val($("#discount_remarks").val());
            $("#txtloyalty").html('');

            $("#btnloyalty").html('Redeem Loyalty');
            $("#btnloyalty").removeClass('btn-danger');
            $("#btnloyalty").addClass('btn-success');
        }
        updatetotal();
    }
    
    
    function recalculate_all(){
        console.log('recalculate_all');
        var orgSubTotal =0;
        var SubTotal =0;
        var totalbeforediscounts=0;
        
        var Tax=0;
        var totalDiscount=0;
        var totalAddition=0;
        
         $('#ordertbl tr').each(function(row, tr) {
            if(row>0){
                var org = $(tr).find('td:eq(11) input[name=orignal_product_price]').val();
                var addition = $(tr).find('td:eq(10) input[name=addition_by_product]').val();
                totalAddition += parseFloat(addition);
                var discount = $(tr).find('td:eq(8) input[name=discount_by_product]').val();
                totalDiscount += parseFloat(discount);
                
                
                var qty = $(tr).find('td:eq(5)').text();
                
                var orgTotal = parseFloat(org) * parseFloat(qty);
                totalbeforediscounts += orgTotal;
                orgTotal = orgTotal + parseFloat(addition) - parseFloat(discount);
                
                var tax_perc = parseFloat($(tr).find('td:eq(12) .product_tax_perc').html());
                var thisTax = orgTotal*tax_perc/100;
                var taxedTotal = Math.round((orgTotal + thisTax)*100)/100;
                
                
//                console.log('org ' + org);
//                console.log('addition ' + addition);
//                console.log('discount ' + discount);
//                console.log('qty ' + qty);
//                console.log('orgTotal ' + orgTotal);
//                console.log('tax_perc '+ tax_perc);
//                console.log('Tax '+ thisTax);
//                console.log('taxedTotal ' + taxedTotal);
                //added product tax
//                var disc_perc = discount * org /100;
//                $(tr).find('td:eq(9) .perc_discount_product').text(Math.round(disc_perc *100)/100);
                
                $(tr).find('td:eq(11) .combat').text(Math.round(orgTotal *100)/100);                
                $(tr).find('td:eq(13) .product_tax').text(Math.round(thisTax *100)/100);
                $(tr).find('td:eq(14) .product_total').text(Math.round(taxedTotal *100)/100);          
                orgSubTotal += orgTotal;
                SubTotal += taxedTotal;
                Tax += thisTax;
            }
        });
        
        
        //$sum += $product['price']*$product['qty'];
        
        $("#txtorgsubtotal").val(Math.round(totalbeforediscounts *100)/100);
        
        $("#txt_org_subtotal").val(Math.round(orgSubTotal *100)/100);
        $('#txtsubtotal').val(Math.round(SubTotal *100)/100);
        $('#txttaxtotal').val(Math.round(Tax *100)/100);
        
        var totalPerc = Math.round(Tax*100/orgSubTotal *100)/100;        
        $("#producttax").html(Math.round(totalPerc *100)/100);
        //$("#txtgross").val(SubTotal);
        
        
        var totalDiscountPerc= Math.round(totalDiscount*100/SubTotal *100)/100;
        
        $("#total_discount_perc").val(Math.round(totalDiscountPerc *100)/100);
        $("#total_discount").val(Math.round(totalDiscount *100)/100);
        $("#total_additional_charges").val(Math.round(totalAddition *100)/100);
        
        
        updatetotal();
    }
    
</script>