
var hrURL =localStorage['serviceURLHR'];
var editIcon = "images/edit.png";
var editIconpen = "images/pen.png";
var removeIcon = "images/delete.png";

$(window).load(function(){
	hash = window.location.href.split('#')[1]	;
	$('*[data-page="'+hash+'"]').addClass('menu-selected');
	
	switch(hash){

		case "EmployeePayStructure" : EmployeePayStructure();break;		
		
	}
	
	
});


$('.save-salary-structure').live('click',saveSalarystructure);



var itemCount=0;
var k=1;
var pageLimit = 25;

//////////////////////////////////////////////////////////////////////// Salary Management ////////////////////////////////////////////////////////////////
function EmployeePayStructure(pageno,e){
	
	
	go_to_empstruct();
	
	$('#light-pagination').hide();
	$('#payroll_search_btn').attr("onclick", 'search_emp_pay_structure()');	
	$('#tablelist').html('');
	$(".memsrch").css("display", "none");
	$(".paysalsearch").css("display", "block");
	$(".calsrch").css("display", "none");
	$('#search_month,#search_year').css("display", "none");
	$('.employee-tab-dwn').css("display", "none");
	if(!pageno)
	pageno =1;
	var totbasicpay = 0;
	var totdeduction = 0;
	var totbenefit = 0;
	var totcontribution = 0;
	var totpayable = 0;

	var searchString = $('.pdtSearchForm').serialize(); 
	//alert(searchString);
	if(pageno == 1 ){
			k=1; 
		 }else{
			k= ((pageno-1) * pageLimit)+1;
		 }
		 var tot = 0;
	
	$('.header').show('blind',500);
	removeCurrentPage();
	
	$('#tablelist').append('<table width="100%" border="0" cellspacing="0" style="display:none" cellpadding="0" class="list-employees">'+
		' <tr>'+
			'<td width="8%" height="17"><translate>Employee Id</translate></td>'+
			'<td width="20%"><translate>Employee Name</translate></td>'+
			'<td width="12%"><translate>Branch</translate></td>'+
			'<td width="13%"><translate>Designation</translate></td>'+
			
			'<td width="11%"><translate>Earnings</translate></td>'+
			'<td width="16%"><translate>Deductions</translate></td>'+
			'<td width="11%"><translate>Net Pay</translate></td>'+
		'</tr>'+
	'</table>');
	
	$.ajax({
		url:hrURL+"managePayrollSalary.php",
		type:"post",
		data:"act=EmployeePayStructure",
		success:function(result){
    		$('.list-employees').append(result);
			$('.expand_parent_row').click(function () {
				var currentDetails = $(this).next('.row_to_expand');
				$('.row_to_expand').not(currentDetails).hide();
				currentDetails.toggle();
			});
			$('.list-employees').show('blind',500);
			$('#light-pagination').pagination('updateItems',itemCount);
  		}
	});
	
}

function load_emp_branch(branch_id)
{
	$.ajax({
		url:hrURL+"managePayrollSalary.php",
		type:"post",
		data:"act=LoadEmpBranch&branch_id="+branch_id,
		success:function(result){
			$('#imdatesupervisor').html(result);
  		}
	});
}
var pageno;
function search_emp_pay_structure()
{
	$('.pdtSearchForm').append('<input type="hidden" id="act" name="act" value="EmployeePayStructure"/>');
	$('#tablelist').html('');
	if(!pageno)
	pageno =1;

	if(pageno == 1 ){
			k=1; 
		 }else{
			k= ((pageno-1) * pageLimit)+1;
		 }
		 var tot = 0;
	
	$('.header').show('blind',500);
	//removeCurrentPage();
	$('#tablelist').append('<table width="100%" border="0" cellspacing="0" style="display:none" cellpadding="0" class="list-employees">'+
		' <tr>'+
			'<td width="8%" height="17"><translate>Employee Id</translate></td>'+
			'<td width="20%"><translate>Employee Name</translate></td>'+
			'<td width="12%"><translate>Branch</translate></td>'+
			'<td width="13%"><translate>Designation</translate></td>'+
			
			'<td width="11%"><translate>Earnings</translate></td>'+
			'<td width="16%"><translate>Deductions</translate></td>'+
			'<td width="11%"><translate>Net Pay</translate></td>'+
		'</tr>'+
	'</table>');
	//alert($('.pdtSearchForm').serialize());
	$.ajax({
		url:hrURL+"managePayrollSalary.php",
		type:"post",
		data:$('.pdtSearchForm').serialize(),
		success:function(result){
    		$('.list-employees').append(result);
			$('.expand_parent_row').click(function () {
				var currentDetails = $(this).next('.row_to_expand');
				$('.row_to_expand').not(currentDetails).hide();
				currentDetails.toggle();
			});
			$('.list-employees').show('blind',500);
			$('#light-pagination').pagination('updateItems',itemCount);
  		}
	});
}

function search_emp_pay_salary()
{
	$('.pdtSearchForm').append('<input type="hidden" id="act" name="act" value="EmployeePaySalary"/>');
	$('#tablelist').html('');
	$(".memsrch").css("display", "none");
	$(".paysalsearch").css("display", "block");
	$(".calsrch").css("display", "none");
	$("#search_month").css("display", "block");
	$("#search_year").css("display", "block");
	$('.header').show('blind',500);
	removeCurrentPage();
	
	$('#tablelist').append('<table width="100%" border="0" cellspacing="0" style="display:none" cellpadding="0" class="list-employees">'+
		'<tr>'+
			'<td width="20%"><translate>Employee Name</translate></td>'+
			'<td width="12%"><translate>Branch</translate></td>'+
			'<td width="11%"><translate>Basic Pay</translate></td>'+
			'<td width="11%"><translate>Earnings</translate></td>'+
			'<td width="16%"><translate>Deductions</translate></td>'+
			'<td width="11%"><translate>Loss of Pay</translate></td>'+
			'<td width="11%"><translate>Net Pay</translate></td>'+
			'<td width="11%"><translate>Pay Slip</translate></td>'+
			'<td width="11%"><translate>Status</translate></td>'+
			'<td width="11%"><translate>Pay Type</translate></td>'+
		'</tr>'+
	'</table>');
	//alert($('.pdtSearchForm').serialize());
	$.ajax({
		url:hrURL+"managePayrollSalary.php",
		type:"post",
		data:$('.pdtSearchForm').serialize(),
		success:function(result){
    		$('.list-employees').append(result);
			$('.expand_parent_row').click(function () {
				var currentDetails = $(this).next('.row_to_expand');
				$('.row_to_expand').not(currentDetails).hide();
				currentDetails.toggle();
			});
			$('.list-employees').show('blind',500);
			$('#light-pagination').pagination('updateItems',itemCount);
  		}
	});
}


/////////////////////////////////////ADDED BY JITHIN ////////////////////////////////////////

function saveSalarystructure(){
	$this = $(this);
	employeeid = $this.attr('data-id');
	
	dataString = $('#salarystructure'+employeeid).serialize();
	dataString += '&empid='+employeeid;

	//dataString =  new FormData('.salary-structure-'+employeeid)[0];
	$.post(hrURL+"update-salary-structure.php?action=update",dataString,function(){
			alert("Salary Package updated.");		
			EmployeePayStructure();
	});
}


$('.earning-deduction').live('keyup',function(e){
	$this = $(this);
	 eid=$this.attr('data-eid');
	 updateChild($this);
	maxamt = $this.parent().find('.maxamount').val();
	enteredAmt = $this.val();
	//console.log(maxamt +"---"+enteredAmt );
	if(parseInt(maxamt) < parseInt(enteredAmt)  && parseInt(maxamt)!= 0 ){
		alert("Maximum amount is "+maxamt);
		$this.val(maxamt);
		 updateChild($this);
	}else{
	
	 $('#employee-details-'+eid).one('mousemove',function(){								  
			$(this).find('.earning-deduction').each(function(){
				$this = $(this);
				 updateChild($this);											 
			});
	 });
	 
	}
	 e.preventDefault();
});


$('.reset-salary-structure').live('click',function(){
		console.log('sdf');										   
	r = confirm("Do you want to reset default salary structure?");	
	
	eid = $(this).attr('data-id');
	
	if(r==true){
		$.post(hrURL+"update-salary-structure.php?action=reset&eid="+eid,function(){
				//alert("Salary Structure updated.");		
				EmployeePayStructure();
		});	
	}
	
});


function updateChild($this){

	deduction_earningid = $this.attr('data-id');
	amount = $this.val();
	
	$this.parent().parent().find('.earning-deduction-mth').text(parseInt(amount/12).toFixed(2));
	childrow = $this.parents('.table-bordered').find('.childrow-'+deduction_earningid);
	
		
		childrow.each(function(){
										  
		$child = $(this);
		
		var percentage =$child.find('.percentage').val();
		var maxamount = $child.find('.maxamount').val();
		calculateAmount=0;
		
		if(percentage!='0.00'){
			calculateAmount = parseFloat(amount)*(parseFloat(percentage)/100);
		}else{
			calculateAmount = maxamount;
		}
		
		if(maxamount < calculateAmount && maxamount!='0.00'){
			calculateAmount = maxamount;
		}
			
		
		
		
		if($child.find('.earning-deduction').val() == $child.find('.calculatedamount').val() ){	
			$child.find('.earning-deduction').val(parseInt(calculateAmount).toFixed(2));	
			$child.find('.earning-deduction-mth').text(parseInt(calculateAmount/12).toFixed(2));	
			$child.find('.calculatedamount').val(parseInt(calculateAmount).toFixed(2));	
		

		var ern_total_yrl=0;
		var ded_total_yrl=0;
		$this.parents('.table-bordered').find('.earning-amount').each(function(){
			ern_total_yrl  +=parseInt($(this).val());
		});

		$this.parents('.table-bordered').find('.deduction-amount').each(function(){
			ded_total_yrl  +=parseInt($(this).val());
		});
		
		$('.earning-mth-total').text(ern_total_yrl);
		$('.deduction-mth-total').text(ded_total_yrl);
		$('.earning-yrl-total').text(parseInt(ern_total_yrl/12).toFixed(2));
		$('.deduction-yrl-total').text(parseInt(ded_total_yrl/12).toFixed(2));
		$('.net-pay').text(parseInt(parseInt(ern_total_yrl)-parseInt(ded_total_yrl)).toFixed(2));
		
	}
		/*
			child_e_id = 	deduction_earningid;
			nextchild = $this.parents('.table-bordered').find('.childrow-'+child_e_id)
			nextchild.each(function(){
				updateChild( $(this).find('.earning-deduction'));
			});*/
		// first calculation done. 
		// then ?
	});
		
		
	$this.parent().parent().find('.earning-deduction-mth').text(parseInt(amount/12).toFixed(2));
	childrows = $this.parents('.table-bordered').find('.childrow-2');
	
		
		childrows.each(function(){
										  
		$child = $(this);
		
			
		var percentage =$child.find('.percentage').val();
		var maxamount = $child.find('.maxamount').val();
		
			
		var ern_total_yrl=0;
		var ded_total_yrl=0;
		$this.parents('.table-bordered').find('.earning-amount').each(function(){
			ern_total_yrl  +=parseInt($(this).val());
		});
		
		

		calculateAmount = parseFloat(ern_total_yrl)*(parseFloat(percentage)/100);
	
		
		if(calculateAmount > $child.find('.maxamount').val() && $child.find('.maxamount').val()!="0.00"){
			calculateAmount = $child.find('.maxamount').val();
		}
		
		
	if($child.find('.earning-deduction').val() == $child.find('.calculatedamount').val() ){	
		$child.find('.earning-deduction').val(parseInt(calculateAmount).toFixed(2));	
		$child.find('.earning-deduction-mth').text(parseInt(calculateAmount/12).toFixed(2));	
		$child.find('.calculatedamount').val(parseInt(calculateAmount).toFixed(2));	

		
		$this.parents('.table-bordered').find('.deduction-amount').each(function(){
			ded_total_yrl  +=parseInt($(this).val());
		});
		
		$('.earning-mth-total').text(ern_total_yrl);
		$('.deduction-mth-total').text(ded_total_yrl);
		$('.earning-yrl-total').text(parseInt(ern_total_yrl/12).toFixed(2));
		$('.deduction-yrl-total').text(parseInt(ded_total_yrl/12).toFixed(2));
		$('.net-pay').text(parseInt(parseInt(ern_total_yrl)-parseInt(ded_total_yrl)).toFixed(2));
	}
	
		
		
	});	
		
	
}


