 var saleModule = (function () { 

$('.addProduct-sale').on('click',function(){

    var productID = $('.product-select-box').val();
    var checkCustomer = $('.customerName-select').val();
    if(checkCustomer) {  
    $.ajax({
        url: base_url+'fetchAjaxData/fetchSaleDetailProductName.php?pid='+productID,
        dataType: "json",
        type: 'GET',        
        success: function(data) {
         //console.log(data[0]); 
         
            var trlength = $('#saleDetailsAdd tbody tr:last').length;
            if(trlength == 0) {

                trlength = 1;

            } else {

                trlength = $('#saleDetailsAdd tbody tr:last').attr('id');
                
                trlength = parseInt(trlength)+parseInt(1);

            }

            var saleDetails = '<td class="product_name">'+data[0].productname+'<input class="form-control productid" name="productID[]"  value="'+productID+'" type="hidden"></td>'
            
            saleDetails +='<td>'+$(".eqty-input").val()+'<input class="form-control eqtyinput" name="eqtyinput[]" type="hidden" value="'+$(".eqty-input").val()+'" ></td>';
            
            saleDetails +='<td>'+$("#prate").val()+'<input class="form-control prateinput" name="prateinput[]" type="hidden" value="'+$("#prate").val()+'" ></td>';
            
            saleDetails +='<td>'+$("#gst").val()+'<input class="form-control gstinput" name="gstinput[]" type="hidden" value="'+$("#gst").val()+'" ></td>';

            saleDetails +='<td>'+$("#amt").val()+'<input class="form-control amtinput" name="amtinput[]" type="hidden" value="'+$("#amt").val()+'" ></td>';

            saleDetails +='<td>'+$("#cgst").val()+' + '+$("#sgst").val()+'<input class="form-control cgstinput" name="cgstinput[]" type="hidden" value="'+$("#cgst").val()+'" ><input class="form-control sgstinput" name="sgstinput[]" type="hidden" value="'+$("#sgst").val()+'" ></td>';

            saleDetails +='<td>'+$("#netamt").val()+'<input class="form-control salenetamt" name="salenetamt[]" type="hidden" value="'+$("#netamt").val()+'" ></td>';

            saleDetails +='<td><input type="button" class="btn btn-default removeProductDetails" id="removeProductDetails" value="-"></td>';

            $("#saleDetailsAdd tbody").append("<tr id='"+trlength+"'></tr>");
            $("#saleDetailsAdd tbody tr:last").append(saleDetails);

            $('.product-select-box').val('');
            //$('.product-select-box option[value="'+productID+'"],option:not([value])').remove();

            $('.product-select-box option[value="'+productID+'"],option:not([value])').attr("disabled","disabled");


            var saleTAmount =(parseFloat($(".saleT-Amount").html())+parseFloat($("#amt").val())).toFixed(2);;
            $(".saleT-Amount").html(saleTAmount+'<input class="form-control saleT-Amount" name="totalamt" type="hidden" value="'+saleTAmount+'">');
            //$(".saleT-Amount").val(saleTAmount);

            var saleTGST =(parseFloat($(".saleT-GST").html())+parseFloat($("#cgst").val())+parseFloat($('#sgst').val())).toFixed(2);
            $(".saleT-GST").html(saleTGST+'<input class="form-control saleT-GST" name="totalGSTamt" type="hidden" value="'+saleTGST+'">');
            //$(".saleT-GST").val(saleTGST);

            var saleTNetAmt =(parseFloat($(".saleT-NetAmt").html())+parseFloat($("#netamt").val())).toFixed(2);
            $(".saleT-NetAmt").html(saleTNetAmt+'<input class="form-control saleT-NetAmt" name="totalNetamt" type="hidden" value="'+saleTNetAmt+'">');
            

            $("#prate").val('');
            $("#gst").val('');
            $(".eqty-input").val('');
            $("#amt").val('');
            $("#gst_amount").val('');
            $("#cgst").val('');
            $("#sgst").val('');
            $("#netamt").val('');
            //$('.product-select-box option').attr('disabled',false);
            
            
        },
        error: function() {
            console.log('An error has occurred: Please check this file fetchSaleDetailProductName.php');
        }        
     }); 

    } else {
        alert("Please Select Customer & Remove the saledetails values....!");
        $('.product-select-box').val('');
        $("#prate").val('');
        $("#gst").val('');
        $(".eqty-input").val('');
        $("#amt").val('');
        $("#gst_amount").val('');
        $("#cgst").val('');
        $("#sgst").val('');
        $("#netamt").val('');

    }

    

});

$('body').on('click', 'input.removeProductDetails', function(){

    console.log("test:"+$(this).closest('tr').attr('id'));
    var removeTRID = $(this).closest('tr').attr('id');  
    $("#"+removeTRID).next().val();
    var salenetamt = $(this).closest('td').prev().find('.salenetamt').val();

    var sgstinput = $(this).closest('td').prev().prev().find('.sgstinput').val();

    var cgstinput = $(this).closest('td').prev().prev().find('.cgstinput').val();

    var amtinput = $(this).closest('td').prev().prev().prev().find('.amtinput').val();


    var saleTAmount =(parseFloat($(".saleT-Amount").html())-parseFloat(amtinput)).toFixed(2);
    $(".saleT-Amount").html(saleTAmount+'<input class="form-control saleT-Amount" name="totalamt" type="hidden" value="'+saleTAmount+'">');

    var saleTGST =(parseFloat($(".saleT-GST").html())-( parseFloat(sgstinput)+parseFloat(cgstinput)    ) ).toFixed(2);
    $(".saleT-GST").html(saleTGST+'<input class="form-control saleT-GST" name="totalGSTamt" type="hidden" value="'+saleTGST+'">');

    var saleTNetAmt =(parseFloat($(".saleT-NetAmt").html())-parseFloat(salenetamt)).toFixed(2);
    $(".saleT-NetAmt").html(saleTNetAmt+'<input class="form-control saleT-NetAmt" name="totalNetamt" type="hidden" value="'+saleTNetAmt+'">');


 
    

    //var gstinput = $(this).closest('td').prev().prev().prev().prev().find('.gstinput').val();

    //var prateinput = $(this).closest('td').prev().prev().prev().prev().prev().find('.prateinput').val();

    //var eqtyinput =$(this).closest('td').prev().prev().prev().prev().prev().prev().find('.eqtyinput').val();

    var productid = $(this).closest('td').prev().prev().prev().prev().prev().prev().prev().find('.productid').val();


    $('.product-select-box option[value="'+productid+'"],option:not([value])').removeAttr("disabled");

    $("#"+removeTRID).remove(); 

});

function removeProduct(current_id) {
    //$("#"+current_id+"_saledetails").remove();
    $("#"+current_id).remove();
}


$('.customerName-select').on('change',function(){

    var CID = $(this).val();
console.log(CID+"  ---          ---"+$("#saleDetailsAdd tbody tr").length);
    if(CID) {
        if($("#saleDetailsAdd tbody tr").length>0) {

            alert("You Changed Customer Name so removed sale details");
            $("#saleDetailsAdd tbody tr").remove();
            $(".saleT-Amount").html('0.000 <input class="form-control saleT-Amount" name="totalamt" type="hidden" value="0.000">');
            $(".saleT-GST").html('0.000<input class="form-control saleT-GST" name="totalGSTamt" type="hidden" value="0.000">');
            $(".saleT-NetAmt").html('0.000<input class="form-control saleT-NetAmt" name="totalNetamt" type="hidden" value="0.000">');
        } else {
        
          $.ajax({
            url: base_url+'fetchAjaxData/fetchSupplierAddress.php?cid='+CID,
            dataType: "json",
            type: 'GET',        
            success: function(data) {
               
               $("#caddress").val(data[0].caddress);
			    
               
               
            },
            error: function() {
                console.log('An error has occurred');
            },
            
         });
        }
    } else {
    
            alert("Please Select Customer...!");
            $("#caddress").val('');
    }
});

$('.product-select-box').on('change',function(){

    var CID = $('.customerName-select').val(); 
    if(CID) {
    var str = $(this).val();
    if(str) {
        $("#prate").val('');
        $("#gst").val('');
        $(".eqty-input").val('');
        $("#amt").val('');
        $("#gst_amount").val('');
        $("#cgst").val('');
        $("#sgst").val('');
        $("#netamt").val('');    

      $.ajax({
        url: base_url+'fetchAjaxData/fetchPurchaseDetailwo.php?pid='+str+'&cid='+CID,
        dataType: "json",
        type: 'GET',        
        success: function(data) {
           
           $("#prate").val(data[0].sale_rate);
           $("#gst").val(data[0].gst);
           
        },
        error: function() {
            console.log('An error has occurred');
        },
        
     });
    }
    else {
        console.log("please atlest one product");
    }

    } else {

        alert("Please Select Customer...!");
        $('.product-select-box').val('');

    }
});

$(".eqty-input").on("blur",function() {


     

       
    var eqty    = $(this).val();

    var productID = $(".product-select-box").val();
    if(productID>0){

    if(eqty>0) {
    var pgst    = $("#gst").val();
    var prate   = $("#prate").val();

    
    //var temp_amt = parseFloat(eqty)+parseFloat((parseInt(eqty)*parseInt(pgst))/100).toFixed(3);

    var temp_amt = parseFloat(eqty)*parseFloat(prate);

    var temp_gst =parseFloat((parseFloat(temp_amt)*parseFloat(pgst))/100).toFixed(2);

    var temp_cgst = parseFloat(temp_gst/2);

    var temp_netamount  = parseFloat(parseFloat(temp_amt)+parseFloat(temp_gst)).toFixed(2);

    $("#amt").val(temp_amt);
    $("#gst_amount").val(temp_gst);
    $("#cgst").val(temp_cgst);
    $("#sgst").val(temp_cgst);
    $("#netamt").val(temp_netamount);

    }
    else {
        alert("Please enter Vaild Number");
    }
    }
    else {
        alert("Please select product");
    }
    
});

/*$(".eqty-input").keypress(function (e) {
    if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        return false;
    }
});*/

$(".productrate").on("blur",function() {

    var prate    = $(this).val();
    if(prate>0) {
    var productID = $(".product-select-box").val();
    if(productID>0){
        var eqty    = $("#eqty").val(); 
    if(eqty>0) {

        
        var pgst='',temp_amt ='', temp_gst ='', temp_cgst ='',temp_netamount  ='';
        pgst    = $("#gst").val();
        //var prate   = $(this).val();
        var temp_amt = parseFloat(eqty)*parseFloat(prate);

        var temp_gst =parseFloat((parseFloat(temp_amt)*parseFloat(pgst))/100).toFixed(2);

        var temp_cgst = parseFloat(temp_gst/2);

        var temp_netamount  = parseFloat(parseFloat(temp_amt)+parseFloat(temp_gst)).toFixed(2);

        $("#amt").val(temp_amt);
        $("#gst_amount").val(temp_gst);
        $("#cgst").val(temp_cgst);
        $("#sgst").val(temp_cgst);
        $("#netamt").val(temp_netamount);

    }
    else {
        alert("Please enter Vaild Number");
    }
    }
    else {
        alert("Please select product");
    }
    }
    else {

        $("#amt").val('');
        $("#gst_amount").val('');
        $("#cgst").val('');
        $("#sgst").val('');
        $("#netamt").val('');

    }
    
});

})();
