<?php 

	if($type == 'pcb'){

		/*
				pcb_csv_1.csv  =  (PCB Schedule 2018.pdf – Category B -> KA10)
				pcb_csv_2.csv  =  (PCB Schedule 2018.pdf – Category KA11 -> KA20)
		*/

		$tax = $pcb = 0;

		if($category2 == 'B' || $category2 == 'K' || $category2 == 'KA1' || $category2 == 'KA2' || $category2 == 'KA3' || $category2 == 'KA4' || $category2 == 'KA5' || $category2 == 'KA6' || $category2 == 'KA7' || $category2 == 'KA8' || $category2 == 'KA9' || $category2 == 'KA10'){
			
			$csvToRead = fopen('../csv/pcb_csv_1.csv', 'r');

			// read CSV file using comma as delimiter
		    while (! feof($csvToRead)) {
		        $csvArray[] = fgetcsv($csvToRead, 1000, ',');
		    }

		    fclose($csvToRead);

		    //print('<pre>'.print_r($csvArray, true).'</pre>'); //print out array

		    $salary = round($salary);

		    if($salary > 86085){
				$extra = $salary - 86085;
				$salary = 86085;
			}else{
				$extra = 0;
			}

			if($salary >= 3141){
				if(count($csvArray) > 0){
		    		
		    		switch($category){
		    			case 1:
			    			foreach($csvArray[0] as $k => $v){
			    				if($v == $category2){
			    					$key = $k;
			    					break;
			    				}
			    			}
		    			break;
		    			case 2:
		    			
		    				foreach($csvArray[0] as $k => $v){
		    					if($k < 15){
		    						if($v == $category2){
			    						$key = $k;
			    						break;
			    					}
		    					}
			    			}
		    			break;
		    			case 3:
		    				foreach($csvArray[0] as $k => $v){
			    				if($k >= 15){
		    						if($v == $category2){
			    						$key = $k;
			    						break;
			    					}
		    					}
			    			}
		    			break;
		    		}
			    		

		    		if($key != ''){
		    			foreach($csvArray as $k => $v){
		    				if( $k >= 2 ){
		    					$salary_from = $v[0];
		    					$salary_to = $v[2];
		    					
		    					if($salary_from <= $salary &&  $salary <= $salary_to  ){
		    						$pcb = $v[$key];
		    						break;
		    					}
		    				}
		    			}
		    		} 	
			    }
			}
			
		   
		    if($extra != 0){
		    	$extra_fee = $extra * 28 / 100;
		    }else{
		    	$extra_fee = 0;
		    }

		    $tax = $pcb + $extra_fee;

		}else if($category2 == 'KA11' || $category2 == 'KA12' || $category2 == 'KA13' || $category2 == 'KA14' || $category2 == 'KA15' || $category2 == 'KA16' || $category2 == 'KA17' || $category2 == 'KA18' || $category2 == 'KA19' || $category2 == 'KA20'){
			
			$csvToRead = fopen('../csv/pcb_csv_2.csv', 'r');

			// read CSV file using comma as delimiter
		    while (! feof($csvToRead)) {
		        $csvArray[] = fgetcsv($csvToRead, 1000, ',');
		    }

		    fclose($csvToRead);

		    //print('<pre>'.print_r($csvArray, true).'</pre>'); //print out array

		    if($salary > 87755){
				$extra = $salary - 87755  ;
				$salary = 87755;
			}else{
				$extra = 0;
			}

			if($salary >= 4976){
				if(count($csvArray) > 0){
		    		switch($category){
		    			case 2:
		    				foreach($csvArray[0] as $k => $v){
		    					if($k < 13){
		    						if($v == $category2){
			    						$key = $k;
			    						break;
			    					}
		    					}
			    			}
		    			break;
		    			case 3:
		    				foreach($csvArray[0] as $k => $v){
			    				if($k >= 13){
		    						if($v == $category2){
			    						$key = $k;
			    						break;
			    					}
		    					}
			    			}
		    			break;
		    		}
			    		

		    		if($key != ''){
		    			foreach($csvArray as $k => $v){
		    				if( $k >= 2 ){
		    					$salary_from = $v[0];
		    					$salary_to = $v[2];
		    					
		    					if($salary_from <= $salary &&  $salary <= $salary_to  ){
		    						$pcb = $v[$key];
		    						break;
		    					}
		    				}
		    			}
		    		}
			    	
			    }
			}
		    

		    if($extra != 0){
		    	$extra_fee = $extra * 28 / 100;
		    }else{
		    	$extra_fee = 0;
		    }
		    $tax = $pcb + $extra_fee;
		}


	}else if($type == 'epf'){

		/*
				epf_csv_1.csv  =  (EPF Schedule July 2022.pdf – Part A)
				epf_csv_2.csv  =  (EPF Schedule July 2022.pdf – Part B)
				epf_csv_3.csv  =  (EPF Schedule July 2022.pdf – Part C)
				epf_csv_4.csv  =  (EPF Schedule July 2022.pdf – Part D)
				epf_csv_5.csv  =  (EPF Schedule July 2022.pdf – Part E)
		*/

		if($citizen == 'yes'){
			if($age < 60){
				$csvToRead = fopen('../csv/epf_csv_1.csv', 'r');
				$staff_epf_rate = 11;
				$employer_epf_rate = 12;
			}else{
				$csvToRead = fopen('../csv/epf_csv_5.csv', 'r');
			}	
		}else{
			if($age < 60){
				$csvToRead = fopen('../csv/epf_csv_1.csv', 'r');
				$staff_epf_rate = 11;
				$employer_epf_rate = 12;
				//$csvToRead = fopen('../csv/epf_csv_2.csv', 'r');
			}else{
				$csvToRead = fopen('../csv/epf_csv_5.csv', 'r');
				//$csvToRead = fopen('../csv/epf_csv_4.csv', 'r');
			}
		}
				
		// read CSV file using comma as delimiter
	    while (! feof($csvToRead)) {
	        $csvArray[] = fgetcsv($csvToRead, 1000, ',');
	    }
	    fclose($csvToRead);

	    //print('<pre>'.print_r($csvArray, true).'</pre>'); //print out array
	   
	    $employer_epf = 0;
		$staff_epf = 0;

		$reverse = array_reverse($csvArray);
		$max = 20000;
		foreach($reverse as $k => $v){
			if($v[3]!=''){
				$max = (float)str_replace(',','',$v[3]);
				break;
			}
		}
		
		if($salary > $max){
			$employer_epf = roundNearestRinggit($salary * $employer_epf_rate / 100);
			$staff_epf = roundNearestRinggit($salary * $staff_epf_rate / 100);
		}else{
			foreach($csvArray as $key => $value){
		    	if($value[1] != 'RM'){
		    		$salary_from = (float)str_replace(',','',$value[1]);
		    		$salary_to = (float)str_replace(',','',$value[3]);
		    		if($salary_from <= $salary &&  $salary <= $salary_to  ){
		    			$employer_epf = (float)str_replace(',','',$value[4]);
						$staff_epf = (float)str_replace(',','',$value[5]);
						break;
					}
		    	}
		    }
		}

		$tax = array(
				'employer_epf' => $employer_epf,
				'staff_epf' => $staff_epf,
		);
	    
	}else if($type == 'socso'){

		/*
			socso_csv.csv = ( SOCSO Schedule ACT 4.pdf )
		*/

		$csvToRead = fopen('../csv/socso_csv.csv', 'r');
		// read CSV file using comma as delimiter
	    while (! feof($csvToRead)) {
	        $csvArray[] = fgetcsv($csvToRead, 1000, ',');
	    }
	    fclose($csvToRead);

	   	// print('<pre>'.print_r($csvArray, true).'</pre>'); //print out array
	    $employer_socso = $staff_socso = 0;
	 
	    $reverse = array_reverse($csvArray);
		$max = 5000;
		foreach($reverse as $k => $v){
			if($v[0]!=''){
				$max = $v[0];
				break;
			}
		}

		if($salary > $max){
			foreach($reverse as $k => $v){
				if($v[0] == $max){
					if($category == 1){
		    			$employer_socso = $v[2];
						$staff_socso = $v[3];
						break;
		    		}else if($category == 2){
		    			$employer_socso = $v[4];
		    			break;
		    		}
				}
			}
		}else{
			foreach($csvArray as $key => $value){
		    	if($key != 0){
		    		$salary_from = $value[0];
			    	$salary_to = $value[1];
			    	if($salary_from < $salary &&  $salary <= $salary_to  ){
			    		if($category == 1){
			    			$employer_socso = $value[2];
							$staff_socso = $value[3];
							break;
			    		}else if($category == 2){
			    			$employer_socso = $value[4];
			    			break;
			    		}
			    	}
		    	}
		    }
		}
	
		$tax = array(
				'employer_socso' => $employer_socso,
				'staff_socso' => $staff_socso,
		);

		
	    
	}else if($type == 'eis'){

		/*
			eis_csv.csv = ( EIS Schedule ACT 800.pdf )
		*/

		$csvToRead = fopen('../csv/eis_csv.csv', 'r');

		// read CSV file using comma as delimiter
	    while (! feof($csvToRead)) {
	        $csvArray[] = fgetcsv($csvToRead, 1000, ',');
	    }

	    fclose($csvToRead);

	    //print('<pre>'.print_r($csvArray, true).'</pre>'); //print out array

	    $employer_eis = $staff_eis = 0;
	 
	    $reverse = array_reverse($csvArray);
		$max = 5000;
		foreach($reverse as $k => $v){
			if($v[0]!=''){
				$max = $v[0];
				break;
			}
		}

		if($salary > $max){
			foreach($reverse as $k => $v){
				if($v[0] == $max){
	    			$employer_eis = $v[2];
					$staff_eis = $v[3];
					break;
				}
			}
		}else{
			foreach($csvArray as $key => $value){
		    	if($key != 0){
		    		$salary_from = $value[0];
			    	$salary_to = $value[1];
			    	if($salary_from < $salary &&  $salary <= $salary_to  ){
		    			$employer_eis = $value[2];
						$staff_eis = $value[3];
						break;
			    	}
		    	}
		    }
		}
	
		$tax = array(
				'employer_eis' => $employer_eis,
				'staff_eis' => $staff_eis,
		);
		
	}

	
	




?>