<?php

include '../connect/cms-config.php' ;
include 'function.php' ;
include 'session.php' ;

function getNewSalary($slip_id){
	global $mysqli;
	$query_salary = $mysqli->query("SELECT * FROM salary_slip a JOIN setting_slip_tax b ON (a.slip_id = b.slip_id) WHERE a.slip_id = '$slip_id' LIMIT 1");
	$new_salary = [];
	while($salary_result = mysqli_fetch_assoc($query_salary)){
		$new_salary[] = $salary_result;
	}
	
	return $new_salary;
}

function calculatePCB($new_salary){
	global $mysqli;
	$boolean_pcb = true;
	$type = 'pcb';
	$salary = $new_salary[0]['basic_salary'] + $new_salary[0]['commission'] + $new_salary[0]['allowance'] - $new_salary[0]['deduction'];

	$marital_status = $new_salary[0]['marital_status'];
	$num_children = $new_salary[0]['num_children'];
	$spouse_working = $new_salary[0]['spouse_working'];
	if($new_salary[0]['tax_status_id'] == 1){
		if($marital_status == 'Single' || $marital_status == 'Divorced' || $marital_status == 'Widow'){
			$category = 1;
			$category2 = 'B';
		}else if($marital_status == 'Married'){
			
			if($num_children == '' || $num_children == 0){
				$category2 = 'K';
			}else{
				$category2 = 'KA'.$num_children;
			}
			
			if($spouse_working == 'Yes'){
				$category = 3;
			}else{
				$category = 2;
			}
			
		}else{
			$boolean_pcb = false;
		}
	}else{
		$boolean_pcb = false;
	}
	if($boolean_pcb){
		$pcb = calculateTax($type, $salary, $category, $category2);	
	}else{
		$pcb = 0;
	}

	return $pcb;
}

function calculateEPF($new_salary){
	global $mysqli;
	$age = $new_salary[0]['age'];
	$country = '';
	$country_id = $new_salary[0]['country_id'];
	$query_country = $mysqli->query("SELECT country_desc FROM master_country WHERE country_id = '$country_id' LIMIT 1");
	$country = mysqli_fetch_assoc($query_country)['country_desc'];

	if($country == 'MALAYSIA'){
		$citizen = 'yes';
	}else{
		$citizen = 'yes';
	}

	$type = 'epf';
	$salary = $new_salary[0]['basic_salary'] + $new_salary[0]['commission'] + $new_salary[0]['allowance'] - $new_salary[0]['deduction'] ;
	if($new_salary[0]['staff_epf_rate'] == '11'){
		$epf_array = calculateTaxEPF($type, $salary, $age, $citizen);
	}else if($new_salary[0]['staff_epf_rate'] == '9'){
		$epf_array = calculateTaxEPF($type, $salary, $age, $citizen);
	}else if($new_salary[0]['staff_epf_rate'] == '0'){
		$epf_array = array(
				'employer_epf' => 0,
				'staff_epf' => 0,
		);
	}
	
	return $epf_array;
}

function calculateSOCSO($new_salary){
	global $mysqli;
	$socso_category_id = $new_salary[0]['socso_category_id'];
	$type = 'socso';
	$salary = $new_salary[0]['basic_salary'] + $new_salary[0]['commission'] + $new_salary[0]['allowance'] - $new_salary[0]['deduction'] ;
	if($socso_category_id == 1){
		$socso_array = calculateTaxSOCSO($type, $salary, $socso_category_id);
	}else if($socso_category_id == 2){
		$socso_array = calculateTaxSOCSO($type, $salary, $socso_category_id);
	}else{
		$socso_array = array(
				'employer_socso' => 0,
				'staff_socso' => 0,
		);
	}
	
	return $socso_array;
}

function calculateEIS($new_salary){
	global $mysqli;
	$eis_status = $new_salary[0]['staff_eis_status'];
	$type = 'eis';
	$salary = $new_salary[0]['basic_salary'] + $new_salary[0]['commission'] + $new_salary[0]['allowance'] - $new_salary[0]['deduction'] ;
	if($eis_status == 1){
		$eis_array = calculateTaxEIS($type, $salary);
	}else{
		$eis_array = array(
				'employer_eis' => 0,
				'staff_eis' => 0,
		);
	}
	
	return $eis_array;
}

function calculateZAKAT($new_salary){
	global $mysqli;
	$zakat_rate = $new_salary[0]['staff_zakat_rate'];
	$salary = $new_salary[0]['basic_salary'] + $new_salary[0]['commission'] + $new_salary[0]['allowance'] - $new_salary[0]['deduction'] ;
	$zakat = calculateTaxZAKAT($salary, $zakat_rate);
	
	return $zakat;
}

if($_POST['save_edit_salary'] == 1){

	$slip_id 	    = escapeString( $_POST['slip_id'] ) ;
    $type 		    = escapeString( $_POST['type'] ) ;
	if($type 	   == 'remark'){
	    $remark     = escapeString( $_POST['change_input'] ) ;
		$update     = $mysqli->query("UPDATE salary_slip SET remark = '$remark' WHERE slip_id = '$slip_id'");

		$result['status'] = 'success';
	}else{
	    if($type 	== 'basic_salary'){
	    	$basic_salary = (float)$_POST['change_input'];
	    	$update = $mysqli->query("UPDATE salary_slip SET basic_salary = '$basic_salary' WHERE slip_id = '$slip_id'");
	    }else if($type == 'commission'){
			$commission = (float)$_POST['change_input'];
			$update = $mysqli->query("UPDATE salary_slip SET commission = '$commission' WHERE slip_id = '$slip_id'");
	    }else if($type == 'allowance'){
	    	$allowance = (float)$_POST['change_input'];
	    	$update = $mysqli->query("UPDATE salary_slip SET allowance = '$allowance' WHERE slip_id = '$slip_id'");
	    }else if($type == 'deduction'){
	    	$deduction = (float)$_POST['change_input'];
	    	$update = $mysqli->query("UPDATE salary_slip SET deduction = '$deduction' WHERE slip_id = '$slip_id'");
	    }
	    
	    if($update){
	    	$new_salary = getNewSalary($slip_id);
	    	$new_pcb_value = calculatePCB($new_salary);

	    	$epf_array = calculateEPF($new_salary);
	    	$new_staff_epf_value = $epf_array['staff_epf'];
	    	$new_employer_epf_value = $epf_array['employer_epf'];

	    	$socso_array = calculateSOCSO($new_salary);
	    	$new_staff_socso_value = $socso_array['staff_socso'];
	    	$new_employer_socso_value = $socso_array['employer_socso'];

	    	$eis_array = calculateEIS($new_salary);
	    	$new_staff_eis_value = $eis_array['staff_eis'];
	    	$new_employer_eis_value = $eis_array['employer_eis'];

	    	$new_zakat_value = calculateZAKAT($new_salary);

	    	$new_sub_total = $new_salary[0]['basic_salary'] + $new_salary[0]['commission'] + $new_salary[0]['allowance'] - $new_salary[0]['deduction'] ;
	    	$new_total = $new_salary[0]['basic_salary'] + $new_salary[0]['commission'] + $new_salary[0]['allowance'] - $new_salary[0]['deduction'] - $new_pcb_value - $new_staff_epf_value - $new_staff_socso_value - $new_staff_eis_value - $new_zakat_value;

	    	//update tax value in db
	    	$update = $mysqli->query("UPDATE salary_slip SET staff_epf = '$new_staff_epf_value', employer_epf = '$new_employer_epf_value', staff_socso = '$new_staff_socso_value', employer_socso = '$new_employer_socso_value', staff_eis = '$new_staff_eis_value', employer_eis = '$new_employer_eis_value', staff_zakat = '$new_zakat_value', staff_pcb = '$new_pcb_value', sub_total = '$new_sub_total', total = '$new_total' WHERE slip_id = '$slip_id'");

	    	if($update){
	    		$result['commission'] = $new_salary[0]['commission'];
		    	$result['allowance'] = $new_salary[0]['allowance'];
		    	$result['staff_epf'] = $new_staff_epf_value ;
		    	$result['staff_socso'] = $new_staff_socso_value ;
		    	$result['staff_eis'] = $new_staff_eis_value ;
		    	$result['em_epf'] = $new_employer_epf_value ;
		    	$result['em_socso'] = $new_employer_socso_value ;
		    	$result['em_eis'] = $new_employer_eis_value ;
		    	$result['staff_zakat'] = $new_zakat_value ;
		    	$result['staff_pcb'] = $new_pcb_value;
		    	$result['sub_total'] = $new_sub_total;
		    	$result['total'] = $new_total;
		    	$result['status'] = 'success';
	    	}else{
	    		$result['status'] = 'failed';
	    	}
	    	
	    }else{
	    	$result['status'] = 'failed';
	    }
	}

    echo json_encode($result);
    exit;
}




?>