<?php
include_once("../class/class.MySQL.php");
$db	=new MySQL();
$action = $_GET['act'];
$max_sql = $db->fetchQuery("SELECT max(structure_id) as structure_id FROM `salary_structure` ");
$max_sal_structure_id = $max_sql[0]['structure_id'];
$max_sal_structure_id = $max_sal_structure_id+1;

if($action == 'add_new_earning_name'){
	$vers['name'] = $_GET['attribute'];
	$vers['type'] = '1';
	$db->Insert($vers,'earning_deduction');	
	$new_id	= $db->LastInsertID();
	echo $new_id;
}
if($action == 'add_new_deduction_name'){
	$vers['name'] = $_GET['attribute'];
	$vers['type'] = '2';
	$db->Insert($vers,'earning_deduction');	
	$new_id	= $db->LastInsertID();
	echo $new_id;
}

if($action == 'insertContribution'){
	$vers['name'] = $_GET['attribute'];
	$vers['type'] = '3';
	$db->Insert($vers,'earning_deduction');	
	$new_id	= $db->LastInsertID();
	echo $new_id;
}

if($action == 'add_new_employeecontribution_name'){
	$vers['name'] = $_GET['attribute'];
	$vers['type'] = '3';
	$db->Insert($vers,'earning_deduction');	
	$new_id	= $db->LastInsertID();
	echo $new_id;
}


if($action == 'list_designation_type'){
	/*$designations_list  = $db->fetchQuery("SELECT d.desig_id, d.desig_name FROM `designation` as d ");*/
	$designations_list  = $db->fetchQuery("SELECT d.desig_id, d.desig_name FROM designation d WHERE d.desig_name!='' AND d.desig_id NOT IN (SELECT sd.designation_id FROM salary_designation sd) ");
	echo '{"designations":'.json_encode($designations_list).'}';	 
}

if($action == 'list_employercontribution'){
	$employercontribution  = $db->fetchQuery("SELECT * FROM `earning_deduction` WHERE  type ='3'");
	echo '{"contribution":'.json_encode($employercontribution).'}';	  
}


if($action == 'list'){
	$earning_deduction_list  = $db->fetchQuery("SELECT id, type, name, CASE WHEN type = 1 THEN 'Earning' Else 'Deduction' END as type_text FROM `earning_deduction` ");
	echo '{"earningdeductions":'.json_encode($earning_deduction_list).'}';	 
}
if($action == "insert"){
	$vers['name'] = $_POST['deductionTitle'];
	$vers['type'] = $_POST['type'];
	$db->Insert($vers,'earning_deduction');	
	$vars['id']	= $db->LastInsertID();
	$getemp = $db->fetchQuery("SELECT count(*) as count, id, type, name, CASE WHEN type = 1 THEN 'Earning' Else 'Deduction' END as type_text FROM `earning_deduction` WHERE id = '".$vars['id']."' ");
	echo '{"earningdeductions":'.json_encode($getemp).'}';
}
if($action == "remove"){
	$did	= $_GET['did'];
	$db->ExecuteSQL("delete from earning_deduction where id = '$did'");
}
if($action == 'list_earning_type'){
	$earning_deduction_list  = $db->fetchQuery("SELECT id, name FROM `earning_deduction` WHERE type = '1'");
	echo '{"earningtype":'.json_encode($earning_deduction_list).'}';	 	
}
if($action == 'list_deduction_type'){
	$earning_deduction_list  = $db->fetchQuery("SELECT id, name FROM `earning_deduction` WHERE type = '2'");
	echo '{"deductionstype":'.json_encode($earning_deduction_list).'}';	 	
}
if($action == 'insert_salary_structure'){
	for($i=0;$i<count($_POST['earning']);$i++){
		$vers['earning_deduction_id'] = $_POST['earning'][$i];
		$vers['percentage'] = $_POST['percenatge_value'][$i];
		if($_POST['mul_earn_parent_value'][$i] != ''){
			$vers['parent_id'] = $_POST['mul_earn_parent_value'][$i];
		}else{
			$vers['parent_id'] = '0';
		}	
		$vers['amount'] = $_POST['amount_value'][$i];
		$vers['structure_id'] = $max_sal_structure_id;
		$db->Insert($vers,'salary_structure');
	}
	for($j=0;$j<count($_POST['deduction']);$j++){
		$vers['earning_deduction_id'] = $_POST['deduction'][$j];
		$vers['percentage'] = $_POST['deduction_percenatge_value'][$j];
		if($_POST['mul_dedc_parent_value'][$j] != ''){ 
			$vers['parent_id'] = $_POST['mul_dedc_parent_value'][$j];
		}else{
			$vers['parent_id'] = '0';
		}	
		$vers['amount'] = $_POST['deduction_amount_value'][$j];
		$vers['structure_id'] = $max_sal_structure_id;
		$db->Insert($vers,'salary_structure');
	}
	
	for($des=0;$des<count($_POST['designation']);$des++){
		$vers['salary_structure_id'] = $max_sal_structure_id;
		$vers['designation_id'] = $_POST['designation'][$des];	
		$db->ExecuteSQL("INSERT INTO salary_designation (salary_structure_id, designation_id) VALUES (".$max_sal_structure_id.",".$_POST['designation'][$des].")");
	}
}
if($action == 'list_salary_structure'){
	$designations_list  = $db->fetchQuery("SELECT sd.id, sd.designation_id,d.desig_name FROM designation as d, salary_designation as sd WHERE d.desig_id = sd.designation_id ");
	echo '{"designationlist":'.json_encode($designations_list).'}';	 
}

if($action == "remove_structure"){
	$did	= $_GET['did'];
	$db->ExecuteSQL("delete from salary_designation where id = '$did'");
}

if($action == "designation_structure"){
	$designation_id = $_GET['designation_id'];
	//echo "SELECT structure.*, ed1.name as parent_name FROM (SELECT ed.id as id1, ed.type, ed.name, sd.id as id, sd.salary_structure_id, sd.designation_id, ss.percentage, ss.amount, ss.earning_deduction_id,ss.parent_id FROM salary_designation AS sd, salary_structure AS ss, earning_deduction as ed WHERE ss.structure_id = sd.salary_structure_id AND ss.earning_deduction_id = ed.id AND sd.designation_id = '".$designation_id."') as structure JOIN earning_deduction as ed1 ON ed1.id =  structure.parent_id";
	
	$designationstructure  = $db->fetchQuery("SELECT ed.id as id1, ed.type, ed.name, sd.id as id, sd.salary_structure_id, sd.designation_id, ss.percentage, ss.amount, ss.earning_deduction_id,ss.parent_id 
	FROM salary_designation AS sd, salary_structure AS ss, earning_deduction as ed 
	WHERE ss.structure_id = sd.salary_structure_id AND ss.earning_deduction_id = ed.id AND sd.designation_id = '".$designation_id."' ORDER BY ss.id ASC");
	
	/*echo "<br />SQL is : "."SELECT ed.id as id1, ed.type, ed.name, sd.id as id, sd.salary_structure_id, sd.designation_id, ss.percentage, ss.amount, ss.earning_deduction_id,ss.parent_id 
	FROM salary_designation AS sd, salary_structure AS ss, earning_deduction as ed 
	WHERE ss.structure_id = sd.salary_structure_id AND ss.earning_deduction_id = ed.id AND sd.designation_id = '".$designation_id."' ORDER BY ss.id ASC";*/
	
	echo '{"designationstructure":'.json_encode($designationstructure).'}';
}


?>