<?php 

// default config setting
$boolean_ssl_lock = true ;

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

$today_date = date('Y-m-d');
// $today_date = '2023-04-01';

//get setting generate slip for current or previous month
$setting_month = 'current';
$query_setting = $mysqli->query("SELECT * FROM setting_salary_tax WHERE tax_type = 'slip_month_setting' LIMIT 1");
if(mysqli_num_rows($query_setting) > 0){
	$result_setting = mysqli_fetch_assoc($query_setting);
	$setting_month = $result_setting['tax_title'];
}
if($setting_month == 'current'){
	$month = date('Y-m-01', strtotime($today_date));
}else if($setting_month == 'previous'){
	$temp_month = date('Y-m-01', strtotime($today_date));
	$month = date('Y-m-01', strtotime($temp_month. ' - 1 months'));
}


$query_staff = $mysqli->query("SELECT * FROM staff WHERE deleted_at is NULL");
if(mysqli_num_rows($query_staff) > 0){

	//get all tax rate and store
	$tax_rate = [];
	$query_tax_rate = $mysqli->query("SELECT * FROM setting_salary_tax WHERE deleted_at is NULL");
	while($row_tax = mysqli_fetch_assoc($query_tax_rate)){
		$tax_rate[$row_tax['tax_id']] = $row_tax;
	}

	while($row = mysqli_fetch_assoc($query_staff)){
		$staff_id = $row['staff_id'];
		$staff_idno = $row['staff_idno'];
		$basic_salary = $row['staff_salary'];
		$epf_rate_id = $row['staff_epf_rate_id'];
		$socso_rate_id = $row['staff_socso_rate_id'];
		$eis_rate_id = $row['staff_eis_rate_id'];
		$zakat_rate_id = $row['staff_zakat_rate_id'];
		$tax_status_id = $row['tax_status_id']; //pcb category, resident, non-resident, no tax

		$staff_epf_rate = $tax_rate[$epf_rate_id]['employee_rate'];
		$employer_epf_rate = $tax_rate[$epf_rate_id]['employer_rate'];
		$staff_socso_rate = $tax_rate[$socso_rate_id]['employee_rate'];
		$employer_socso_rate = $tax_rate[$socso_rate_id]['employer_rate'];
		$staff_eis_rate = $tax_rate[$eis_rate_id]['employee_rate'];
		$employer_eis_rate = $tax_rate[$eis_rate_id]['employer_rate'];
		$staff_zakat_rate = $tax_rate[$zakat_rate_id]['employee_rate'];
		$employer_zakat_rate = $tax_rate[$zakat_rate_id]['employer_rate'];

		$socso_category_id = $row['socso_category_id'];
		$eis_status = $row['staff_eis_status'];

		$birthday = $row['staff_birthdate'];
		if($birthday == NULL || $birthday == '0000-00-00'){
			$age = 0;
		}else{
			$birthDate = date("Y",strtotime($birthday));
			$age = date("Y") - $birthDate;
		}
		$country_id = $row['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';
		}

		$staff_settings = $row['staff_settings'] ;
		if ( $staff_settings != '' ){
			$staff_settings = JsonEncodeDecode('decode', $staff_settings) ;
		}else{
			$staff_settings = [] ;
		}
		$marital_status = $staff_settings['marital_status'];
		$num_children = $staff_settings['no_children'];
		$spouse_working = $staff_settings['spouse_working'];


		//for kindlemind
		$query_commission_temp = $mysqli->query("SELECT * FROM staff_commission_temp WHERE staff_id='$staff_idno' AND month='$month' LIMIT 1");
		if(mysqli_num_rows($query_commission_temp) > 0){
			$commission = mysqli_fetch_assoc($query_commission_temp)['commission'];
		}else{
			$commission = 0;
		}

	
		$allowance = $row['staff_allowance_topup'] + $row['staff_allowance_work'] + $row['staff_allowance_food'];

	//calculate epf, socso, eis, pcb
		$staff_epf = 0;
		$staff_socso = 0;
		$staff_eis = 0;
		$staff_zakat = 0;
		$staff_pcb = 0;
		$employer_epf = 0;
		$employer_socso = 0;
		$employer_eis = 0;
		$employer_zakat = 0;
		$employer_pcb = 0;
		$deduction = 0;

		$gross_total = $basic_salary + $commission + $allowance;

	//calculate pcb tax

		$boolean_pcb = true;
		if($row['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('pcb', $gross_total, $category, $category2);	
		}else{
			$pcb = 0;
		}
		
		$staff_pcb = $pcb;

	//calculate pcb tax end



	//calculate epf tax

		if($staff_epf_rate == '11'){
			$epf_array = calculateTaxEPF('epf', $gross_total, $age, $citizen);
		}else if($staff_epf_rate == '9'){
			$epf_array = calculateTaxEPF('epf', $gross_total, $age, $citizen);
		}else if($staff_epf_rate == '0' || $staff_epf_rate == ''){
			$epf_array = [];
		}

		if($epf_array['staff_epf']!= ''){
			$staff_epf = $epf_array['staff_epf'];
		}
		if($epf_array['employer_epf']!= ''){
			$employer_epf = $epf_array['employer_epf'];
		}

	//calculate epf tax end


	//calculate socso tax

		if($socso_category_id == 1){
			$socso_array = calculateTaxSOCSO('socso', $gross_total, $socso_category_id);
		}else if($socso_category_id == 2){
			$socso_array = calculateTaxSOCSO('socso', $gross_total, $socso_category_id);
		}else{
			$socso_array = [];
		}

		if($socso_array['staff_socso']!= ''){
			$staff_socso = $socso_array['staff_socso'];
		}
		if($socso_array['employer_socso']!= ''){
			$employer_socso = $socso_array['employer_socso'];
		}

	//calculate socso tax end


	//calculate eis tax

		if($eis_status == 1){
			$eis_array = calculateTaxEIS('eis', $gross_total);
		}else{
			$eis_array = [];
		}

		if($eis_array['staff_eis']!= ''){
			$staff_eis = $eis_array['staff_eis'];
		}
		if($eis_array['employer_eis']!= ''){
			$employer_eis = $eis_array['employer_eis'];
		}

	//calculate eis tax end


	//calculate zakat fund

		if($staff_zakat_rate > 0){
			$staff_zakat = calculateTaxZAKAT($gross_total, $staff_zakat_rate);
		}

	//calculate zakat fund end

		$subtotal = $basic_salary + $commission + $allowance ;
		$total = $basic_salary + $commission + $allowance - $staff_epf - $staff_socso - $staff_eis - $staff_zakat - $staff_pcb ;


		$query_salary_slip = $mysqli->query("SELECT slip_id FROM salary_slip WHERE staff_id = '$staff_id' AND month = '$month' LIMIT 1");
		if(mysqli_num_rows($query_salary_slip) <= 0){
			$insert = $mysqli->query("INSERT INTO salary_slip ( staff_id, basic_salary, commission, allowance, staff_epf, staff_socso, staff_eis, staff_zakat, staff_pcb, employer_epf, employer_socso, employer_eis, employer_zakat, employer_pcb, deduction, deduction_reason, status, sub_total, total, month ) VALUES ( '$staff_id', '$basic_salary', '$commission', '$allowance', '$staff_epf', '$staff_socso', '$staff_eis', '$staff_zakat', '$staff_pcb', '$employer_epf', '$employer_socso', '$employer_eis', '$employer_zakat', '$employer_pcb', '$deduction', '', 'active', '$subtotal', '$total', '$month' )");

			if($insert){
				$slip_id = $mysqli->insert_id;
			
				$mysqli->query("INSERT INTO setting_slip_tax( slip_id, staff_epf_rate, employer_epf_rate, age, country_id, staff_socso_rate, employer_socso_rate, socso_category_id, staff_eis_rate, employer_eis_rate, staff_eis_status, staff_zakat_rate, employer_zakat_rate, tax_status_id, marital_status, spouse_working, num_children ) VALUES ( '$slip_id', '$staff_epf_rate', '$employer_epf_rate', '$age', '$country_id', '$staff_socso_rate', '$employer_socso_rate', '$socso_category_id', '$staff_eis_rate', '$employer_eis_rate', '$eis_status', '$staff_zakat_rate', '$employer_zakat_rate', '$tax_status_id', '$marital_status', '$spouse_working', '$num_children')");

			}
		}
		

	}
}


?>