(function () {
    $("#vc_party_id").focus();
	disable_enable_background(false);
}());


/*payment voucher */
var voucher_cnt = 1;
	function get_group_data(id)
	{
		if(id > 0)
		{
			var path = base_url+"voucherctrl/get_group_data/"+id;  

			$("#loading").show();
			$.ajax({
				type:'GET',
				url:path,
				dataType:'json',
				success:function(resp)
				{	
					var data = "";
					data += '<select class="form-control" id="vc_party_id" name="vc_party_id" onchange="get_party_balance_status();">';
					data += '<option value="0">PLEASE SELECT</option>'
					console.log(resp);
					$.each(resp, function(index, value){
						if (id == 5) 
						{
                        	data += '<option value="'+value['client_id']+'">'+value['client_name']+' - '+value['client_mobile1'] +'</option>';
						}
						else if (id == 6) 
						{
                        	data += '<option value="'+value['acc_id']+'">'+value['acc_name']+' - '+value['acc_mobile']+'</option>';
						}
						else if(id == 7)
						{
                        	data += '<option value="'+value['km_id']+'">'+value['km_name']+' - '+value['km_mob']+'</option>';
						}else{
							data += '<option value="'+value['acc_id']+'">'+value['acc_name']+' - '+value['acc_mobile']+'</option>';	
						}
                    });
					data += '</select>';

					$("#party_id").html(data);
					$("#vc_party_id").flexselect();

					$("#vc_payment_status").val("");
					$("#loading").hide();
				},
				error:function(err){
					console.log(err);
				}   
			});
		}
		else
		{
			$("#vc_payment_status").val(0);
		}
	}
    
    function get_party_balance_status()
	{
		var party_id = parseFloat($("#vc_party_id").val());
		var grp_id 	 = parseFloat($("#vc_grp_id").val());//vc_grp_id=5 is for client and vc_grp_id=6 is for suplier vc_grp_id=7 for karigar
		var vc_type  = parseFloat($("#vc_voucher_type").val()); //value=1 is for PAYMENT and value=2 is for RECEIPT
		var billno  = parseFloat($("#vc_search_billno").val());
		// alert(billno);

		console.log(grp_id);


		if((party_id > 0) && (grp_id > 0)) 
		{
			var path = base_url+"voucherctrl/get_party_balance_status/"+party_id+"/"+grp_id;  

			$("#loading").show();
			$.ajax({
				type:'GET',
				url:path,
				dataType:'json',
				success:function(resp)
				{
					console.log(resp);
					// console.log(vc_type);
					// console.log(grp_id);
					$("#vc_payment_status").val(resp['status']);
					
					$("#vc_mob_no").val(resp['acc_mob_no']);

					if (vc_type == 1 && grp_id == 7) //payment
					{
						get_party_bill_details();
					}
					else if (vc_type == 2 && grp_id == 5) //receipt
					{
						get_party_bill_details();
					}
					else if (vc_type == 1 && grp_id == 6) //payment
					{
						get_party_bill_details();
					}
					
					$("#loading").hide();
				},
				error:function(err){
					console.log(err);
				}   
			});
		}
		else if(billno > 0)
		{
			// var grp_id = 5;
			// var vc_type = 2;

			var path = base_url+"voucherctrl/get_party_balance_status_with_billno/"+billno;  

			$("#loading").show();
			$.ajax({
				type:'GET',
				url:path,
				dataType:'json',
				success:function(resp)
				{
					console.log(resp);
					
					$("#vc_payment_status").val(resp['status']);
					
					$("#vc_mob_no").val(resp['acc_mob_no']);

					$("#vc_party_id").append('<option value="'+resp['client_id']+'">'+resp['acc_name']+'-'+resp['acc_mob_no']+'</option>');
					// if (vc_type == 1 && grp_id == 7) 
					// {
					// 	get_party_bill_details();
					// }
					// else if (vc_type == 2 && grp_id == 5) 
					// {
						get_party_bill_details();
					// }
					// else if (vc_type == 1 && grp_id == 6) 
					// {
					// 	get_party_bill_details();
					// }
					
					$("#loading").hide();
				},
				error:function(err){
					console.log(err);
				}   
			});
		}
		else
		{
			$("#vc_payment_status").val(0);
		}
	}

	function get_party_bill_details()
	{
		var grp_id 	= parseFloat($("#vc_grp_id").val());
		var party_id = parseFloat($("#vc_party_id").val());
		var billno  = parseFloat($("#vc_search_billno").val());
		// console.log(grp_id);
		// console.log(party_id);

		var vc_payment_status = $("#vc_payment_status").val();

		if(vc_payment_status != "0 TO RECEIVE")
		{
			vc_payment_bal = "-1";
		}
		else
		{
			vc_payment_bal = vc_payment_status.indexOf("PAY");
		}
	
		if(party_id > 0)
		{
			// console.log("xyz");

			var path = base_url+"voucherctrl/get_party_order_details/"+party_id+"/"+grp_id;  

			$("#loading").show();
			$.ajax({
				type:'POST',
				url:path,
				dataType:'json',
				success:function(resp)
				{
					console.log(resp);
					var data = '';
					if(resp.length != 0)
					{   

						if (grp_id == 5) 
						{
							data += '<table class="table">';

							data += '<tr>';
							data += '<th>Bill No</th>';
							data += '<th>Bill Date</th>';
							data += '<th>Final Total</th>';
							// data += '<th>Total Disc</th>';
							// data += '<th>Advance</th>';
							data += '<th>Allocated Amt</th>';
							data += '<th>Allocated Round off Amt</th>';
							data += '<th>Adjust Amt</th>';
							data += '<th>Round off Amt</th>';
							data += '<th>Current Bal Amt</th>';
							data += '</tr>';

							$.each(resp, function(index, value)
							{
								var current_bal = parseFloat(value['sale_total_amt']) - parseFloat(value['sale_allocated_amt']) - parseFloat(value['sale_round_off']);
								if (isNaN(current_bal)) current_bal = 0;
								current_bal = current_bal.toFixed(0);

								data += '<tr>';
								data += '<td>';
								data += '<input type="hidden" class="form-control" name="sale_id[]" id="sale_id_'+voucher_cnt+'" value="'+value['sale_id']+'"/>';
								
								data += '<input type="hidden" class="form-control" name="vt_id[]" value="0"/>';

								data += '<input type="text" class="form-control" name="sale_entry_no[]" id="sale_entry_no_'+voucher_cnt+'" value="'+value['sale_entry_no']+'" readonly/></td>';
								data += '<td><input type="text" class="form-control" name="sale_entry_date[]" id="sale_entry_date_'+voucher_cnt+'" value="'+value['sale_entry_date']+'" readonly/></td>';

								data += '<td><input type="text" class="form-control" name="sale_total_amt[]" id="sale_total_amt_'+voucher_cnt+'" value="'+value['sale_total_amt']+'" readonly/></td>';
								
								// data += '<td><input type="text" class="form-control" name="om_tot_disc[]" id="om_tot_disc_'+voucher_cnt+'" value="'+value['om_tot_disc']+'" readonly/></td>';

								// data += '<td><input type="text" class="form-control" name="om_advance[]" id="om_advance_'+voucher_cnt+'" value="'+value['om_advance']+'" readonly/></td>';
								
								data += '<td><input type="text" class="form-control" name="sale_allocated_amt[]" id="sale_allocated_amt_'+voucher_cnt+'" value="'+value['sale_allocated_amt']+'" readonly/></td>';

								data += '<td><input type="text" class="form-control" name="sale_round_off[]" id="sale_round_off_'+voucher_cnt+'" value="'+value['sale_round_off']+'" readonly/></td>';

								data += '<td>';
								data += '<input type="text" class="form-control" name="sale_adjust_amt[]" id="sale_adjust_amt_'+voucher_cnt+'" value=""/>';
								data += '</td>';

								data += '<td>';
								data += '<input type="text" class="form-control" name="sale_adjust_round_off[]" id="sale_adjust_round_off_'+voucher_cnt+'" value=""/>';
								data += '</td>';

								data += '<td><input type="text" class="form-control" id="sale_current_bal_amt_'+voucher_cnt+'" value="'+current_bal+'" readonly/></td>';
								data += '</td>';

								data += '</tr>';

								voucher_cnt++;
							});

							data += '</table>';
						}
						else if (grp_id == 6) 
						{ 
							data += '<table class="table">';

							data += '<tr>';
							data += '<th>Entry No</th>';
							data += '<th>Entry Date</th>';
							data += '<th>Bill No</th>';
							data += '<th>Bill Date</th>';
							data += '<th>Final Total</th>';
							data += '<th>Allocated Amt</th>';
							data += '<th>Allocated Round off Amt</th>';
							data += '<th>Adjust Amt</th>';
							data += '<th>Round off Amt</th>';
							data += '<th>Current Bal Amt</th>';
							data += '</tr>';

							$.each(resp, function(index, value)
							{
								var current_bal = parseFloat(value['per_total_amt']) -  parseFloat(value['per_allocated_amt']);
								if (isNaN(current_bal)) current_bal = 0;
								current_bal = current_bal.toFixed(2);

								data += '<tr>';
								data += '<td>';
								data += '<input type="hidden" class="form-control" name="per_id[]" id="per_id_'+voucher_cnt+'" value="'+value['per_id']+'"/>';
								
								data += '<input type="hidden" class="form-control" name="vt_id[]" value="0"/>';

								data += '<input type="text" class="form-control" name="per_entry_no[]" id="per_entry_no_'+voucher_cnt+'" value="'+value['per_entry_no']+'" readonly/></td>';
								data += '<td><input type="text" class="form-control" name="per_entry_date[]" id="per_entry_date_'+voucher_cnt+'" value="'+value['per_entry_date']+'" readonly/></td>';

								data += '<td><input type="text" class="form-control" name="per_bill_no[]" id="per_bill_no_'+voucher_cnt+'" value="'+value['per_bill_no']+'" readonly/></td>';
								data += '<td><input type="text" class="form-control" name="per_bill_date[]" id="per_bill_date_'+voucher_cnt+'" value="'+value['per_bill_date']+'" readonly/></td>';


								data += '<td><input type="text" class="form-control" name="per_total_amt[]" id="per_total_amt_'+voucher_cnt+'" value="'+value['per_total_amt']+'" readonly/></td>';
								
								data += '<td><input type="text" class="form-control" name="per_allocated_amt[]" id="per_allocated_amt_'+voucher_cnt+'" value="'+value['per_allocated_amt']+'" readonly/></td>';

								data += '<td><input type="text" class="form-control" name="per_round_off[]" id="per_round_off_'+voucher_cnt+'" value="'+value['per_round_off']+'" readonly/></td>';

								data += '<td>';
								data += '<input type="text" class="form-control" name="per_adjust_amt[]" id="per_adjust_amt_'+voucher_cnt+'" value=""/>';
								data += '</td>';

								data += '<td>';
								data += '<input type="text" class="form-control" name="per_adjust_round_off[]" id="per_adjust_round_off_'+voucher_cnt+'" value=""/>';
								data += '</td>';

								data += '<td><input type="text" class="form-control" id="per_current_bal_amt_'+voucher_cnt+'" value="'+current_bal+'" readonly/></td>';
								data += '</td>';

								data += '</tr>';

								voucher_cnt++;
							});

							data += '</table>';
						}
						else 
						{


							data += '<table class="table">';
							data += '<tr>';
							data += '<th>Bill No</th>';
							data += '<th>Bill Date</th>';
							data += '<th>Final Total</th>';
							data += '<th>Allocated Amt</th>';
							data += '<th>Allocated Round off Amt</th>';
							data += '<th>Adjust Amt</th>';
							data += '<th>Round off Amt</th>';
							data += '<th>Current Bal Amt</th>';
							data += '</tr>';
						
							$.each(resp, function(index, value)
							{
								var current_bal = parseFloat(value['hm_amount']) - parseFloat(value['hm_allocated_amt']) - parseFloat(value['hm_round_off']);
								if (isNaN(current_bal)) current_bal = 0;
								current_bal = current_bal.toFixed(2);

								data += '<tr>'; 
								data += '<td>';
								data += '<input type="hidden" class="form-control" name="hm_id[]" id="hm_id_'+voucher_cnt+'" value="'+value['hm_id']+'"/>';
								data += '<input type="hidden" class="form-control" name="vt_id[]" value="0"/>';

								data += '<input type="text" class="form-control" name="vt_bill_no[]" id="vt_bill_no_'+voucher_cnt+'" value="'+value['hm_entry_no']+'" readonly/></td>';
								data += '<td><input type="text" class="form-control" name="vt_bill_date[]" id="vt_bill_date_'+voucher_cnt+'" value="'+value['hm_entry_date']+'" readonly/></td>';

								
								
								data += '<td><input type="text" class="form-control" name="hm_total[]" id="hm_total_'+voucher_cnt+'" value="'+value['hm_amount']+'" readonly/></td>';
								
								data += '<td><input type="text" class="form-control" name="hm_allocated_amt[]" id="hm_allocated_amt_'+voucher_cnt+'" value="'+value['hm_allocated_amt']+'" readonly/></td>';

								data += '<td><input type="text" class="form-control" name="hm_round_off[]" id="hm_round_off_'+voucher_cnt+'" value="'+value['hm_round_off']+'" readonly/></td>';


								data += '<td>';
								data += '<input type="text" class="form-control" name="hm_adjust_amt[]" id="hm_adjust_amt_'+voucher_cnt+'" value=""/>';
								data += '</td>';

								data += '<td>';
								data += '<input type="text" class="form-control" name="hm_adjust_round_off[]" id="hm_adjust_round_off_'+voucher_cnt+'" value=""/>';
								data += '</td>';

								data += '<td><input type="text" class="form-control" id="hm_current_bal_amt_'+voucher_cnt+'" value="'+current_bal+'" readonly/></td>';
								data += '</td>';

								data += '</tr>';

								voucher_cnt++;
							});
						

							data += '</table>';
						}
					}
					else
					{
						console.log("No Data");
					} 
				
					$("#order_details").append(data);
					$("#loading").hide();
				},
				error:function(resp) {
					console.log(resp);
				}   
			});
		}
		else if(billno > 0)
		{			
			var path = base_url+"voucherctrl/get_party_order_details_with_billno/"+billno;  

			$("#loading").show();
			$.ajax({
				type:'GET',
				url:path,
				dataType:'json',
				success:function(resp)
				{
					console.log(resp);
					var data = '';
					if(resp.length != 0)
					{   
							data += '<table class="table">';

							data += '<tr>';
							data += '<th>Bill No</th>';
							data += '<th>Bill Date</th>';
							data += '<th>Final Total</th>';
							data += '<th>Advance</th>';
							data += '<th>Allocated Amt</th>';
							data += '<th>Allocated Round off Amt</th>';
							data += '<th>Adjust Amt</th>';
							data += '<th>Round off Amt</th>';
							data += '<th>Current Bal Amt</th>';
							data += '</tr>';

							
							$.each(resp, function(index, value)
							{
								var current_bal = parseFloat(value['sale_total_amt']) - parseFloat(value['om_advance']) - parseFloat(value['sale_allocated_amt']) - parseFloat(value['sale_round_off']);
								if (isNaN(current_bal)) current_bal = 0;
								current_bal = current_bal.toFixed(0);

								data += '<tr>';
								data += '<td>';
								data += '<input type="hidden" class="form-control" name="sale_id[]" id="sale_id_'+voucher_cnt+'" value="'+value['sale_id']+'"/>';
								
								data += '<input type="hidden" class="form-control" name="vt_id[]" value="0"/>';

								data += '<input type="text" class="form-control" name="sale_entry_no[]" id="sale_entry_no_'+voucher_cnt+'" value="'+value['sale_entry_no']+'" readonly/></td>';
								data += '<td><input type="text" class="form-control" name="sale_entry_date[]" id="sale_entry_date_'+voucher_cnt+'" value="'+value['sale_entry_date']+'" readonly/></td>';

								data += '<td><input type="text" class="form-control" name="sale_total_amt[]" id="sale_total_amt_'+voucher_cnt+'" value="'+value['sale_total_amt']+'" readonly/></td>';
								
								// data += '<td><input type="text" class="form-control" name="om_advance[]" id="om_advance_'+voucher_cnt+'" value="'+value['om_advance']+'" readonly/></td>';
								
								data += '<td><input type="text" class="form-control" name="sale_allocated_amt[]" id="sale_allocated_amt_'+voucher_cnt+'" value="'+value['sale_allocated_amt']+'" readonly/></td>';

								data += '<td><input type="text" class="form-control" name="sale_round_off[]" id="sale_round_off_'+voucher_cnt+'" value="'+value['sale_round_off']+'" readonly/></td>';

								data += '<td>';
								data += '<input type="text" class="form-control" name="sale_adjust_amt[]" id="sale_adjust_amt_'+voucher_cnt+'" value=""/>';
								data += '</td>';

								data += '<td>';
								data += '<input type="text" class="form-control" name="sale_adjust_round_off[]" id="sale_adjust_round_off_'+voucher_cnt+'" value=""/>';
								data += '</td>';

								data += '<td><input type="text" class="form-control" id="sale_current_bal_amt_'+voucher_cnt+'" value="'+current_bal+'" readonly/></td>';
								data += '</td>';

								data += '</tr>';

								voucher_cnt++;
							});

							data += '</table>';
						
					}
					else
					{
						console.log("No Data");
					}

					$("#order_details").html(data);
					$("#loading").hide();
				},
				error:function(err){
					console.log(err);
				}   
			});
		}
	}
	
	
	function add_voucher(id)
	{

		var check = 1;

		var acc_id = $("#vc_cash_acc_id").val();

		if($("#vc_entry_date").val() == "")
		{
			$("#vc_entry_date").css({'background-color':'yellow'});
			check = 0;
		}
		else
		{
			$("#vc_entry_date").css({'background-color':'#fff'});
		}

		if($('#vc_amount').val() < 0 || $('#vc_amount').val() == '' || isNaN(($('#vc_amount')).val()))
		{
			$("#vc_amount").css({'background-color':'yellow'});
			check = 0;
		}
		else
		{
			$("#vc_amount").css({'background-color':'#fff'});
		}

		if($('#vc_party_id').val() == 0 || $('#vc_party_id').val() == '')
		{
			$("#vc_party_id_flexselect").css({'background-color':'yellow'});
			check = 0;
		}
		else
		{
			$("#vc_party_id_flexselect").css({'background-color':'#fff'});
		}


		var grp_id=$('#vc_grp_id').val();
		// alert(grp_id);

		if(grp_id==5)
		{

		for (var i = 1; i < voucher_cnt; i++) 
		{
			if(($('#sale_adjust_amt_'+i).val() < 0 || $('#sale_adjust_amt_'+i).val() == '' || isNaN(($('#sale_adjust_amt_'+i)).val())))
			{
				// $('#sm_adjust_amt_'+i).css({'background-color':'yellow'});
				// check = 0;
			}
			else
			{
				var sale_adjust_amt = parseFloat($('#sale_adjust_amt_'+i).val());
				if(isNaN(sale_adjust_amt)) sale_adjust_amt = 0;

				var sale_adjust_round_off = parseFloat($('#sale_adjust_round_off_'+i).val());
				if(isNaN(sale_adjust_round_off)) sale_adjust_round_off = 0;


				var total_adjust_amt = parseFloat(sale_adjust_amt) + parseFloat(sale_adjust_round_off);
				if(isNaN(total_adjust_amt)) total_adjust_amt = 0; 

				var sale_current_bal_amt = parseFloat($('#sale_current_bal_amt_'+i).val());
				if(isNaN(sale_current_bal_amt)) sale_current_bal_amt = 0;
				
				if(total_adjust_amt > sale_current_bal_amt)
				{
					$('#sale_adjust_amt_'+i).css({'background-color':'yellow'});
					$('#sale_adjust_round_off_'+i).css({'background-color':'yellow'});
					check = 2;
				}
				else
				{
					$('#sale_adjust_amt_'+i).css({'background-color':'#fff'});
					$('#sale_adjust_round_off_'+i).css({'background-color':'#fff'});
				}
				
			}
		}

		}
		else if(grp_id==6)
		{
			for (var i = 1; i < voucher_cnt; i++) 
			{
				if(($('#per_adjust_amt_'+i).val() < 0 || $('#per_adjust_amt_'+i).val() == '' || isNaN(($('#per_adjust_amt_'+i)).val())))
				{
					// $('#sm_adjust_amt_'+i).css({'background-color':'yellow'});
					// check = 0;
				}
				else
				{
					var per_adjust_amt = parseFloat($('#per_adjust_amt_'+i).val());
					if(isNaN(sale_adjust_amt)) per_adjust_amt = 0;

					var per_adjust_round_off = parseFloat($('#per_adjust_round_off_'+i).val());
					if(isNaN(per_adjust_round_off)) per_adjust_round_off = 0;


					var total_adjust_amt = parseFloat(per_adjust_amt) + parseFloat(per_adjust_round_off);
					if(isNaN(total_adjust_amt)) total_adjust_amt = 0;

					var per_current_bal_amt = parseFloat($('#per_current_bal_amt_'+i).val());
					if(isNaN(per_current_bal_amt)) per_current_bal_amt = 0;

					
					if(total_adjust_amt > per_current_bal_amt)
					{
						$('#per_adjust_amt_'+i).css({'background-color':'yellow'});
						$('#per_adjust_round_off_'+i).css({'background-color':'yellow'});
						check = 2;
					}
					else
					{
						$('#per_adjust_amt_'+i).css({'background-color':'#fff'});
						$('#per_adjust_round_off_'+i).css({'background-color':'#fff'});
					}
					
				}
			}

		}
		else if(grp_id==7)
		{
		
			for (var i = 1; i < voucher_cnt; i++) 
			{
				if(($('#hm_adjust_amt_'+i).val() < 0 || $('#hm_adjust_amt_'+i).val() == '' || isNaN(($('#hm_adjust_amt_'+i)).val())))
				{
					// $('#sm_adjust_amt_'+i).css({'background-color':'yellow'});
					// check = 0;
				}
				else
				{
					var hm_adjust_amt = parseFloat($('#hm_adjust_amt_'+i).val());
					if(isNaN(hm_adjust_amt)) hm_adjust_amt = 0;

					var hm_adjust_round_off = parseFloat($('#hm_adjust_round_off_'+i).val());
					if(isNaN(hm_adjust_round_off)) hm_adjust_round_off = 0;


					var total_adjust_amt = parseFloat(hm_adjust_amt) + parseFloat(hm_adjust_round_off);
					if(isNaN(total_adjust_amt)) total_adjust_amt = 0;

					var hm_current_bal_amt = parseFloat($('#hm_current_bal_amt_'+i).val());
					if(isNaN(hm_current_bal_amt)) hm_current_bal_amt = 0;

					if(total_adjust_amt > hm_current_bal_amt)
					{
						$('#hm_adjust_amt_'+i).css({'background-color':'yellow'});
						$('#hm_adjust_round_off_'+i).css({'background-color':'yellow'});
						check = 2;
					}
					else
					{
						$('#hm_adjust_amt_'+i).css({'background-color':'#fff'});
						$('#hm_adjust_round_off_'+i).css({'background-color':'#fff'});
					}
					
				}
			} 
		
		} 
		

		if(check == 0)
		{
			var msg = 'Oh snap ! You forgot to enter some information';
			display_alert('err',msg);

			$("body, html").animate({'scrollTop':0},1000);
		}
		else if(check == 2)
		{
			var msg = 'Adjust Amt should not be greater than current balance';
			display_alert('err',msg);
			$("body, html").animate({'scrollTop':0},1000);
		}
		else
		{     
			var check_validation 		   = 1;
			var check_validation_round 	   = 1;
			var total_adjust_amt 		   = 0;
			var total_adjust_round_off_amt = 0;

			var vc_amount = parseFloat($('#vc_amount').val());
			if(isNaN(vc_amount)) vc_amount = 0;

			var vc_round_off = parseFloat($('#vc_round_off').val());
			if(isNaN(vc_round_off)) vc_round_off = 0;

			if(grp_id==5)
			{
				for (var i = 1; i < voucher_cnt; i++) 
				{
					var sale_adjust_amt = parseFloat($('#sale_adjust_amt_'+i).val());
					if(isNaN(sale_adjust_amt)) sale_adjust_amt = 0;

					var sale_adjust_round_off = parseFloat($('#sale_adjust_round_off_'+i).val());
					if(isNaN(sale_adjust_round_off)) sale_adjust_round_off = 0;

					// console.log(sale_adjust_round_off);

					total_adjust_amt = parseFloat(total_adjust_amt) + parseFloat(sale_adjust_amt);
					total_adjust_round_off_amt = parseFloat(total_adjust_round_off_amt) + parseFloat(sale_adjust_round_off);
				}
			}

			if(grp_id==6)
			{
				for (var i = 1; i < voucher_cnt; i++) 
				{
					var per_adjust_amt = parseFloat($('#per_adjust_amt_'+i).val());
					if(isNaN(per_adjust_amt)) per_adjust_amt = 0;

					var per_adjust_round_off = parseFloat($('#per_adjust_round_off_'+i).val());
					if(isNaN(per_adjust_round_off)) per_adjust_round_off = 0;

					// console.log(sale_adjust_round_off);

					total_adjust_amt = parseFloat(total_adjust_amt) + parseFloat(per_adjust_amt);
					total_adjust_round_off_amt = parseFloat(total_adjust_round_off_amt) + parseFloat(per_adjust_round_off);
				}
			}

			if(grp_id==7)
			{ 
				for (var i = 1; i < voucher_cnt; i++)  
				{
					trans_id = $('#vt_id_'+i).val();
					if(trans_id>0){
						var hm_adjust_amt = parseFloat($('#vt_adjust_amt_'+i).val());
						if(isNaN(hm_adjust_amt)) hm_adjust_amt = 0; 

						var hm_adjust_round_off = parseFloat($('#vt_adjust_round_off_'+i).val());
						if(isNaN(hm_adjust_round_off)) hm_adjust_round_off = 0;

					}else{
						var hm_adjust_amt = parseFloat($('#hm_adjust_amt_'+i).val());
						if(isNaN(hm_adjust_amt)) hm_adjust_amt = 0; 

						var hm_adjust_round_off = parseFloat($('#hm_adjust_round_off_'+i).val());
						if(isNaN(hm_adjust_round_off)) hm_adjust_round_off = 0;
					}
					// console.log(sale_adjust_round_off);

					total_adjust_amt = parseFloat(total_adjust_amt) + parseFloat(hm_adjust_amt);
					total_adjust_round_off_amt = parseFloat(total_adjust_round_off_amt) + parseFloat(hm_adjust_round_off);
				}
			}
			
			// console.log(total_adjust_amt);

			if((total_adjust_amt != 0) || (total_adjust_round_off_amt != 0))
			{
				if((vc_amount < total_adjust_amt))
				{
					check_validation = 0;
				}
				else
				{
					check_validation = 1;
				}

				if (vc_round_off > 0) 
				{
					if((vc_round_off != total_adjust_round_off_amt))
					{
						check_validation_round = 0;
					}
					else
					{
						check_validation_round = 1;
					}
				}
				else
				{
					check_validation_round = 1; 
				}
			}
			else
			{
				check_validation = 1;
			}

			if(check_validation == 0)
			{
				var msg = 'Voucher Amt and adjust amt are not matching';
				display_alert('err',msg);

				$("body, html").animate({'scrollTop':0},1000);
			}
			else if(check_validation_round == 0)
			{
				var msg = 'Voucher round off Amt and adjust round off amt are not matching';
				display_alert('err',msg);

				$("body, html").animate({'scrollTop':0},1000);
			}
			else if((check_validation == 1) && (check_validation_round == 1))
			{
				var path = base_url+"voucherctrl/voucher_insert/"+id;   
				var form_data = $("#voucher_form").serialize();
					

				$("#loading").show();
				$("#sbt_btn").prop('disabled', true);
				$.ajax({
					type:'POST',
					url:path,
					data:form_data,
					success:function(resp)
					{
						console.log(resp);
						if(resp == 1)
						{
							if(id == 0)
							{
								var msg = 'Successfully added!';
								display_alert('succ',msg);
								$("html, body").animate({'scrollTop':0},1000);
								setInterval(function(){window.location.reload(); },1000);
							}
							else
							{
								var msg = 'Successfully updated!';
								display_alert('succ',msg);  
								$("html, body").animate({'scrollTop':0},1000);
								setInterval(function(){window.location.reload(); },1000);
							}
						}
						else if (resp == 2) 
						{
							var msg = 'Voucher Entery No Already Exist';
							display_alert('warn',msg);
							$("html, body").animate({'scrollTop':0},1000);
						}
						else
						{
							var msg = 'Unknown Error ! Please try again letter';
							display_alert('warn',msg);
							$("html, body").animate({'scrollTop':0},1000);
							$("#sbt_btn").prop('disabled', false);
						}
						$("#loading").hide();
					},
					error:function(resp){
						console.log(resp);
						$("#sbt_btn").prop('disabled', false);
					}   
				});
			}
			else
			{
				var msg = 'Unknown Error ! Please try again letter';
				display_alert('warn',msg);
				$("html, body").animate({'scrollTop':0},1000);
				$("#sbt_btn").prop('disabled', false);
			}
		}
	}

	function update_receipt_voucher(id)
	{
		var check = 1;
		var acc_id = $("#vc_cash_acc_id").val();

		if($("#vc_entry_no").val() == "")
		{
			$("#vc_entry_no").css({'background-color':'yellow'});
			check = 0;
		}
		else
		{
			$("#vc_entry_no").css({'background-color':'#FFF'});
		}

		if($("#vc_entry_date").val() == "")
		{
			$("#vc_entry_date").css({'background-color':'yellow'});
			check = 0;
		}
		else
		{
			$("#vc_entry_date").css({'background-color':'#FFF'});
		}

		if($("#vc_party_id").val() == 0)
		{
			$("#vc_party_id").css({'background-color':'yellow'});
			check = 0;
		}
		else
		{
			$("#vc_party_id").css({'background-color':'#FFF'});
		}

		if($('#vc_amount').val() < 0 || $('#vc_amount').val() == '' || isNaN(($('#vc_amount')).val()))
		{
			$("#vc_amount").css({'background-color':'yellow'});
			check = 0;
		}
		else
		{
			$("#vc_amount").css({'background-color':'#FFF'});
		}

		for (var i = 1; i < voucher_cnt; i++) 
		{
			if($('#sale_adjust_amt_'+i).val() < 0 || $('#sale_adjust_amt_'+i).val() == '' || isNaN(($('#sale_adjust_amt_'+i)).val()))
			{
				// $('#sm_adjust_amt_'+i).css({'background-color':'yellow'});
				// check = 0;
			}
			else
			{
				var sale_adjust_amt = parseFloat($('#sale_adjust_amt_'+i).val());
				if(isNaN(sale_adjust_amt)) sale_adjust_amt = 0;

				var total_adjust_amt = parseFloat(sale_adjust_amt);
				if(isNaN(total_adjust_amt)) total_adjust_amt = 0;

				var sale_current_bal_amt = parseFloat($('#sale_current_bal_amt_'+i).val());
				if(isNaN(sale_current_bal_amt)) sale_current_bal_amt = 0;

				if(total_adjust_amt > sale_current_bal_amt)
				{
					$('#sale_adjust_amt_'+i).css({'background-color':'yellow'});
					check = 2;
				}
				else
				{
					$('#sale_adjust_amt_'+i).css({'background-color':'#fff'});
				}
			}
		}

		if(check == 0)
		{
			var msg = 'Oh snap ! You forgot to enter some information';
			display_alert('err',msg);

			$("body, html").animate({'scrollTop':0},1000);
		}
		else if(check == 2)
		{
			var msg = 'Adjust Amt should not be greater than current balance';
			display_alert('err',msg);
			$("body, html").animate({'scrollTop':0},1000);
		}
		else
		{     
			var check_validation 		   = 1;
			var total_adjust_amt 		   = 0;

			var vc_amount = parseFloat($('#vc_amount').val());
			if(isNaN(vc_amount)) vc_amount = 0;

			for (var i = 1; i < voucher_cnt; i++) 
			{
				var sale_adjust_amt = parseFloat($('#sale_adjust_amt_'+i).val());
				if(isNaN(sale_adjust_amt)) sale_adjust_amt = 0;

				total_adjust_amt = parseFloat(total_adjust_amt) + parseFloat(sale_adjust_amt);
			}

			
			if((total_adjust_amt != 0))
			{
				if((vc_amount != total_adjust_amt))
				{
					check_validation = 0;
				}
				else
				{
					check_validation = 1;
				}
			}
			else
			{
				check_validation = 1;
			}

			if(check_validation == 0)
			{
				var msg = 'Voucher Amt and adjust amt are not matching';
				display_alert('err',msg);

				$("body, html").animate({'scrollTop':0},1000);
			}
			else if(check_validation == 1)
			{
				var path = base_url+"voucherctrl/receipt_voucher_update/"+id;   
				var form_data = $("#receipt_edit_form").serialize();

				$("#loading").show();
				$("#sbt_btn").prop('disabled', true);
				$.ajax({
					type:'POST',
					url:path,
					data:form_data,
					success:function(resp)
					{
						console.log(resp);
						if(resp == 1)
						{
							if(id == 0)
							{
								var msg = 'Successfully added!';
								display_alert('succ',msg);
								$("html, body").animate({'scrollTop':0},1000);
								setInterval(function(){window.location.reload(); },1000);
							}
							else
							{
								var msg = 'Successfully updated!';
								display_alert('succ',msg);  
								$("html, body").animate({'scrollTop':0},1000);
								setInterval(function(){window.location.reload(); },1000);
							}
						}
						else if (resp == 2) 
						{
							var msg = 'Voucher Entery No Already Exist';
							display_alert('warn',msg);
							$("html, body").animate({'scrollTop':0},1000);
						}
						else
						{
							var msg = 'Unknown Error ! Please try again letter';
							display_alert('warn',msg);
							$("html, body").animate({'scrollTop':0},1000);
							$("#sbt_btn").prop('disabled', false);
						}
						$("#loading").hide();
					},
					error:function(resp){
						console.log(resp);
						$("#sbt_btn").prop('disabled', false);
					}   
				});
			}
			else
			{
				var msg = 'Unknown Error ! Please try again letter';
				display_alert('warn',msg);
				$("html, body").animate({'scrollTop':0},1000);
				$("#sbt_btn").prop('disabled', false);
			}
		}
	}

