 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>'+$("#category").val()+'<input class="form-control " name="category[]" type="hidden" value="'+$("#category").val()+'" ></td>';
			
			saleDetails +='<td>'+$("#gross_weight").val()+'<input class="form-control " name="gross_weight[]" type="hidden" value="'+$("#gross_weight").val()+'" ></td>';
			saleDetails +='<td>'+$("#wastage_percentage").val()+'<input class="form-control " name="wastage_percentage[]" type="hidden" value="'+$("#wastage_percentage").val()+'" ></td>';
			saleDetails +='<td>'+$("#wastage").val()+'<input class="form-control " name="wastage[]" type="hidden" value="'+$("#wastage").val()+'" ></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>'+$("#making_charge").val()+'<input class="form-control " name="making_charge[]" type="hidden" value="'+$("#making_charge").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('');
			 $("#gross_weight").val('0.00');
			  $("#wastage").val('0.00');
			   $("#macking_charge").val('0.00');
            $("#gst").val('');
            $(".eqty-input").val('');
            $("#amt").val('');
            $("#gst_amount").val('');
            $("#cgst").val('');
            $("#sgst").val('');
            $("#netamt").val('');
			$("#wastage_percentage").val('0.00');
            //$('.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('');
		
		$("#gross_weight").val('0.00');
        $("#wastage_percentage").val('0.00');
        $("#wastage").val('0.00');
		$("#macking_charge").val('0.00');

    }

    

});

$('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();
	
	//alert(CID);
	
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/fetchCustomerAddress.php?cid='+CID,
            dataType: "json",
            type: 'GET',        
            success: function(data) {
               
               $("#caddress").val(data[0].caddress);
			    $("#saleGSTNO").val(data[0].saleGSTNO);
               
               
            },
            error: function() {
                console.log('An error has occurred');
            },
            
         });
        }
    } else {
    
            alert("Please Select Customer...!");
            $("#caddress").val('');
    }
});

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

    var category = $('#category').val(); 
    if(category) {
    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/fetchSaleDetail.php?pid='+str+'&cid='+category,
        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('');

    }
});

$("#gross_weight").on("keyup",function() {
									   
									  // alert("Hi");
									  var gross=$(this).val();
									  var waste=$("#wastage").val();
									  var pgst    = $("#gst").val();
    var prate   = $("#prate").val();
	var mc   = $("#making_charge").val();
									  
									  var  ntwt=parseFloat(gross)+parseFloat(waste);
									  
									  $("#eqty").val(ntwt);
									  
									  var temp_amt1 = parseFloat(ntwt)*parseFloat(prate);
									   var temp_amt = parseFloat(mc)+parseFloat(temp_amt1);

    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);
									  
									   
									   });

$("#wastage").on("keyup",function() {
									   
									var gross=$("#gross_weight").val();
									  var waste=$("#wastage").val();
									  var pgst    = $("#gst").val();
    var prate   = $("#prate").val();
	var mc   = $("#making_charge").val();
	
	 var wastage_percentage1=parseFloat(waste)/parseFloat(gross);
	 var wastage_percentage2=parseFloat(wastage_percentage1*100).toFixed(2);
	 $("#wastage_percentage").val(wastage_percentage2)
									  
									  var  ntwt=parseFloat(gross)+parseFloat(waste);
									  
									  $("#eqty").val(ntwt);
									  
									  var temp_amt1 = parseFloat(ntwt)*parseFloat(prate);
									   var temp_amt = parseFloat(mc)+parseFloat(temp_amt1);
									   //alert(temp_amt);

    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);
									  
									   
									   });



$("#wastage_percentage").on("keyup",function() {
									   
									var gross=$("#gross_weight").val();
									  var wastage_percentage=$("#wastage_percentage").val();
									  var pgst    = $("#gst").val();
    var prate   = $("#prate").val();
	var mc   = $("#making_charge").val();
	
	var wastage1=parseFloat(gross)*parseFloat(wastage_percentage);
	var waste=parseFloat(wastage1/100).toFixed(2);
	
									  $("#wastage").val(waste)
									  var  ntwt=parseFloat(gross)+parseFloat(waste);
									  
									  $("#eqty").val(ntwt);
									  
									  var temp_amt1 = parseFloat(ntwt)*parseFloat(prate);
									   var temp_amt = parseFloat(mc)+parseFloat(temp_amt1);

    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);
									  
									   
									   });

$("#making_charge").on("keyup",function() {
									   
									var gross=$("#gross_weight").val();
									  var waste=$("#wastage").val();
									  var pgst    = $("#gst").val();
    var prate   = $("#prate").val();
	var mc   = $("#making_charge").val();
									  
									  var  ntwt=parseFloat(gross)+parseFloat(waste);
									  
									  $("#eqty").val(ntwt);
									  
									  var temp_amt1 = parseFloat(ntwt)*parseFloat(prate);
									   var temp_amt = parseFloat(mc)+parseFloat(temp_amt1);

    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);
									  
									   
									   });

/*$(".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('');

    }
    
});

})();
/*
$(".product-select-box").on("change",function() {

    console.log((this));

    $(this).find("#test3").val("1");

});

function fetchData(str) {

   
    if(str) {
        $("#prate").val('');
        $("#gst").val('');
        $(".eqty-input").val('');
        $("#amt").val('');
        $("#gst_amt").val('');
        $("#cgst").val('');
        $("#sgst").val('');
        $("#netamt").val('');    

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

}

function addProduct() {


    var index = $('#saleDetailsAdd tbody tr:last').attr("id");
    $("#saleDetailsAdd tbody").append("<tr ></tr>");
     if(index) {
        index = $('#saleDetailsAdd tbody tr:last').length;
        index  =parseInt(index)+parseInt(1);
        $('#saleDetailsAdd tbody tr:last').attr("id",index);
    }
    
    if(typeof index == 'undefined'){
        // your code here.
        index = 1;
        index  =parseInt(index);
        $('#saleDetailsAdd tbody tr:last').attr("id",index);
    }

    $("#saleDetailsAdd tbody tr:last").append('<td>(Like Product Name)</td><td>'+$(".eqty-input").val()+'</td><td>'+$("#prate").val()+'</td><td>'+$("#gst").val()+'</td><td>'+$("#amt").val()+'</td><td>'+$("#cgst").val()+' + '+$("#sgst").val()+'</td><td>'+$("#netamt").val()+'</td><td><input type="button" class="btn btn-default removeProductDetails" value="-"></td>');

        $("#prate").val('');
        $("#gst").val('');
        $(".eqty-input").val('');
        $("#amt").val('');
        $("#gst_amt").val('');
        $("#cgst").val('');
        $("#sgst").val('');
        $("#netamt").val('');
    
    
}


*/

//var sql_product ="";

    //$('#saleAdd tbody tr:last').after('<tr id="'+index+'"><td>1</td><td><input class="form-control" type="text"></td><td><select class="form-control ><option value="">Select the Product</option>"'+sql_product+'"</select></td><td><input class="form-control" type="text"></td><td><input class="form-control" type="text"></td><td><input class="form-control" type="text"></td><td><input class="form-control" type="text"></td><td><input class="form-control" type="text"></td><td><input class="form-control" type="text"></td><td><input class="form-control" type="text"></td><td><input class="form-control" type="text"></td><td><input type="button" onclick="removeProduct('+index+')" class="btn btn-default" value="-"></td></tr>');