<?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 == '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 == 'weekly_salary_list'){
	$weekly_sal_list  = $db->fetchQuery("SELECT emp.e_fname as e_fname,brch.branch_name as branch_name ,wk.amount as amount,MONTHNAME(STR_TO_DATE(paid_month, '%m')) as MONTHNAME,paid_year,add_date FROM `weekly_payroll_salary_pay_details` as wk join `employee` as emp on wk.emp_id = emp.e_id join `branches` as brch on brch.branch_id = emp.branchid  ");
	echo '{"weeklysalary":'.json_encode($weekly_sal_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 == 'insert_weekly_salary'){
	$today_dt = date('Y-m-d');

for($j=0;$j<count($_POST['branch']);$j++){
		$months = $_POST['mnth'][0];
		$years = $_POST['years'][0];
		$branch_id = $_POST['branch'][$j];
		$member_id = $_POST['memberid'][$j];
		$amount_val = $_POST['amount_value'][$j];
		
		
		$db->ExecuteSQL("INSERT INTO weekly_payroll_salary_pay_details (emp_id,brnch_id,paid_month,paid_year,amount,add_date) VALUES (".$member_id.",".$branch_id.",".$months.",".$years.",".$amount_val.",'".$today_dt."')");
		
	}



}



?>