<?php
include_once("../class/class.MySQL.php");
$db	=new MySQL();
session_start();

$action = $_GET['action'];


if($action == "update"):

	// collect all earnings and maximum amount to array $earnings /////////////////
	$earnings = array();
	$k=0;
	foreach($_POST['earningamount'] as $earning){
		$earnings[$k]['id'] 			= 	$_POST['earningid'][$k];
		$earnings[$k]['newamount'] 		= 	$_POST['earningamount'][$k];    // Submit amount textbox. may be same as calculated.
		$earnings[$k]['currentamount'] 	= 	$_POST['earning-maxamount-org'][$k]; // Actual calculated amount.
		$earnings[$k]['precentage'] 	= 	$_POST['earning-percentage'][$k]; // actual persentage
		$earnings[$k]['maxamount'] 		= 	$_POST['earning-maxamount'][$k]; // actual maximum amount.
		$k++;
	}
		
	//// Same as above collect all desuction and maximum amounts./////////////////
	$deductions = array();
	$j=0;
	foreach($_POST['deductionamount'] as $deduction){
		$deductions[$j]['id'] = 	$_POST['deductionid'][$j];
		$deductions[$j]['newamount'] = 	$_POST['deductionamount'][$j];
		$deductions[$j]['currentamount'] = 	$_POST['deduction-maxamount-org'][$j]; // Actual calculated amount.
		$deductions[$j]['precentage'] = 	$_POST['deduction-percentage'][$j]; // actual persentage
		$deductions[$j]['maxamount'] = 	$_POST['deduction-maxamount'][$j]; // actual maximum amount.
		$j++;
	}

	

///// Check employee's salary structure id ////
$where['employee_id'] = $_POST['empid'];
$structure = $db->fetchAll("salary_structure_employee",$where);
$structureid;
$copystatus; // for find collected from employee relation table or designation relational table.

	if(count($structure) == 0)://getStructure from designation structure//
		$designation['designation_id'] = $_POST['designationid'];
		$structure = $db->fetchAll("salary_designation",$designation);
		$structureid = $structure[0]['salary_structure_id'];
		$copystatus = 1;
	else:	
		$structureid = $structure[0]['salary_structure_id'];
		$copystatus = 2;
	endif;


if($copystatus == 1){ // collect from designation part, then......
		
		$str_where['structure_id'] = $structureid ;
		$structure = $db->fetchAll("salary_structure",$str_where," id ASC ");
		
		$lastStructureId = $db->fetchQuery("SELECT structure_id FROM salary_structure ORDER BY structure_id DESC LIMIT 1");
		$structureid =$lastStructureId[0]['structure_id']+1;
		
	foreach($structure as $structure){
		
	
		$data['earning_deduction_id'] 	= $structure['earning_deduction_id'];
		$data['percentage'] 			= $structure['percentage'];
		$data['amount'] 				= $structure['amount'];
		$data['parent_id'] 				= $structure['parent_id'];
		$data['structure_id'] 			= $structureid;
		
		$db->Insert($data,'salary_structure');
	}
	
	$employeeStructure['salary_structure_id'] = $structureid;
	$employeeStructure['employee_id'] = $where['employee_id'];
	
	$db->Insert($employeeStructure,'salary_structure_employee');
	
}else{
	
	
	
	

}

///update changes to structure.
/* check status of current amount and actual amount. */ 

// 1). Loop through earnings.
$setearnings = array();
foreach($earnings as $earning){
	$current_amount = $earning['newamount'];
	$actual_amount  = $earning['currentamount'];
	$actual_maxamount  = $earning['maxamount'];
	
	
	$condition['earning_deduction_id'] = $earning['id'];
	$condition['structure_id'] =$structureid;
	
	if($current_amount != $actual_amount ){ // if amount changed
	
	
	
		if($current_amount > $actual_amount){
		
			$set['percentage'] =  0;
			$set['amount'] =  $current_amount;
			
		}elseif($current_amount < $actual_amount){

			$set['amount'] =  $current_amount;

		}

		$db->Update("salary_structure",$set,$condition);
	}
}


// 2). Loop through deductions..
//print_r($deductions);
$setdeductions = array();
foreach($deductions as $deduction){
	$current_amount = $deduction['newamount'];
	$actual_amount  = $deduction['currentamount'];
	$actual_maxamount  = $deduction['maxamount'];

	
	$condition['earning_deduction_id'] = $deduction['id'];
	$condition['structure_id'] =$structureid;
	
	
	
	if($current_amount !=$actual_amount ){ // if amount changed
		if($current_amount > $actual_maxamount){
		
			$set['percentage'] =  0;
			$set['amount'] =  $current_amount;
			
		}elseif($current_amount < $actual_maxamount){
			
			$set['amount'] =  $current_amount;
		}
		$db->Update("salary_structure",$set,$condition);
	}
	
}

elseif($action == "reset"):

	$where['employee_id'] = $_GET['eid'];
	$getStructure = $db->fetchAll("salary_structure_employee",$where);
	//print_r($getStructure);
	if(count($getStructure) > 0 ){
	echo$cnd['structure_id'] = $getStructure[0]['salary_structure_id'];
		$db->Delete('salary_structure',$cnd);
		$db->Delete('salary_structure_employee',$where);
	}

endif;//// close update if Condition here.

?>