<?php
/* $Id: index.php 6941 2014-10-26 23:18:08Z daintree $*/
error_reporting(E_ALL & ~E_NOTICE);
$PageSecurity=0;

include('includes/session.inc');
$Title=_('HRMS-Process Pay');
include('includes/newheader.php');

?>

		
  					
	<script src="angular/angular.min.js"></script>  
        
 
     	   <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.10.16/b-1.5.1/b-flash-1.5.1/b-html5-1.5.1/b-print-1.5.1/datatables.min.css"/>
 <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.5.1/css/buttons.dataTables.min.css"/>
 <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css"/>
 
   <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
	<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.flash.min.js"></script>
	<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.print.min.js"></script>
     
		

   <h3 class="m-subheader__title ">Employee Total Tallies Report-All Locations <p><a href="timesheet.php" class="btn btn-danger" role="button">Return to Timesheet</a></p></h3>
  
 									
									<?php
									
		
  if (isset($_GET['id'])) {
		$_SESSION['id'] = stripslashes($_GET['id']);
	}
	$calendarid = $_SESSION['id'];

$sql4 =  "SELECT 
	              timesheetsuvadetails.*,
				   			 employeemaster.tin,
							  employeemaster.fnpf,
							  employeemaster.first_name,
							  employeemaster.last_name,
							  employeemaster.job_role,
							  employeemaster.department,
							  employeemaster.contract,
							  timesheetsuva.patrol_location,
						      timesheetsuva.internal_location,
							  SUM(IF( mon_gap = 0, timesheetsuvadetails.mon_total, 0)) AS montotal,
							  SUM(IF( mon_gap = 1, timesheetsuvadetails.mon_total, 0)) AS montotalgap,
							  SUM(IF( tues_gap = 0, timesheetsuvadetails.tues_total, 0)) AS tuestotal,
							  SUM(IF(tues_gap = 1, timesheetsuvadetails.tues_total, 0)) AS tuestotalgap,
							  SUM(IF( wed_gap = 0, timesheetsuvadetails.wed_total, 0)) AS wedtotal,
							  SUM(IF(wed_gap = 1, timesheetsuvadetails.wed_total, 0)) AS wedtotalgap,
							  SUM(IF( thurs_gap = 0, timesheetsuvadetails.thurs_total, 0)) AS thurstotal,
							  SUM(IF(thurs_gap = 1, timesheetsuvadetails.thurs_total, 0)) AS thurstotalgap,
							  SUM(IF( fri_gap = 0, timesheetsuvadetails.fri_total, 0)) AS fritotal,
							  SUM(IF(fri_gap = 1, timesheetsuvadetails.fri_total, 0)) AS fritotalgap,
							  SUM(IF( sat_gap = 0, timesheetsuvadetails.sat_total, 0)) AS sattotal,
							  SUM(IF(sat_gap = 1, timesheetsuvadetails.sat_total, 0)) AS sattotalgap,
							  SUM(IF( sun_gap = 0, timesheetsuvadetails.sun_total, 0)) AS suntotal,
							  SUM(IF(sun_gap = 1, timesheetsuvadetails.sun_total, 0)) AS suntotalgap,
							 
				             SUM(timesheetsuvadetails.total_hrs) as totalhours
				   
						 
			FROM0 timesheetsuvadetails  INNER JOIN  employeemaster on timesheetsuvadetails.employeeid = employeemaster.employeeid
			INNER JOIN  timesheetsuva on timesheetsuvadetails.timesuvaid = timesheetsuva.timesuvaid 
			where timesheetsuvadetails.calendar_id = '" . $calendarid."'
			Group by employeeid
			Order by employeeid 
			";
	$ErrMsg = _('This customer could not be added because');
			$result4 = DB_query($sql4,$ErrMsg);
		
			
			
		while ($myrow4 = DB_fetch_array($result4)) {
		
			
			//do maths
			
			//first check if employee is on 9 or 8
			
			
			
			
			$contract = $myrow4['contract'];
			
		
			
			$nine =  9;
			if ($contract == $nine ) {
	
	
	
	if ( $myrow4['montotal'] <= '9')  {
		
		$monnormal = $myrow4['montotal'] + $myrow4['montotalgap'];
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($myrow4['montotal'] > '9') &&($myrow4['montotal'] <= '13')) {
		
		$montimeandhalfhours = ($myrow4['montotal'] - '9');
		$monnormal = '9'  + $myrow4['montotalgap'];
		$mondouble = '0';
			
		}
	
	
		if ( $myrow4['montotal'] >'13')  {
	  
	    $monnormal = '9'+ $myrow4['montotalgap'];
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['montotal']- '9');
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
	
	 if ( $myrow4['tuestotal'] <= '9')  {
		
		$tuesnormal = $myrow4['tuestotal']+ $myrow4['tuestotalgap'];
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($myrow4['tuestotal'] > '9') &&($myrow4['tuestotal'] <= '13')) {
		
		$tuestimeandhalfhours = ($myrow4['tuestotal'] - '9');
		$tuesnormal = '9'+ $myrow4['tuestotalgap'];
		$tuesdouble = '0';
		}
	
	
		if ($myrow4['tuestotal'] >'13')  {
	  
	    $tuesnormal = '9'+ $myrow4['tuestotalgap'];
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['tuestotal'] - '9' );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		 if ( $myrow4['wedtotal'] <= '9')  {
		
		$wednormal = $myrow4['wedtotal']+ $myrow4['wedtotalgap'];
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($myrow4['wedtotal'] > '9') &&($myrow4['wedtotal']<= '13')) {
		
		$wedtimeandhalfhours = ($myrow4['wedtotal']- '9');
		$wednormal = '9'+ $myrow4['wedtotalgap'];
		$weddouble = '0';
		}
	
	
		if ( $myrow4['wedtotal']>'13')  {
	  
	    $wednormal = '9'+ $myrow4['wedtotalgap'];
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['wedtotal'] - '9' );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		
		if ( $myrow4['thurstotal'] <= '9')  {
		
		$thursnormal = $myrow4['thurstotal']+ $myrow4['thurstotalgap'];
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($myrow4['thurstotal'] > '9') &&($myrow4['thurstotal'] <= '13')) {
		
		$thurstimeandhalfhours = ($myrow4['thurstotal'] - '9');
		$thursnormal = '9'+ $myrow4['thurstotalgap'];
		$thursdouble = '0';
		}
	
	
		if ( $myrow4['thurstotal'] >'13')  {
	  
	    $thursnormal = '9'+ $myrow4['thurstotalgap'];
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['thurstotal'] - '9' );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
		
	 if ($myrow4['fritotal'] <= '9')  {
		
		$frinormal = $myrow4['fritotal'] + $myrow4['fritotalgap'];
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($myrow4['fritotal']  > '9') &&($myrow4['fritotal']  <= '13')) {
		
		$fritimeandhalfhours = ($myrow4['fritotal']  - '9');
		$frinormal = '9'+ $myrow4['fritotalgap'];
		$fridouble = '0';
		}
	
	
		if ( $myrow4['fritotal']  >'13')  {
	  
	    $frinormal = '9'+ $myrow4['fritotalgap'];
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['fritotal'] - '9' );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
		if ($myrow4['sattotal']  <= '9')  {
		
		$satnormal = $myrow4['sattotal']+ $myrow4['sattotalgap'] ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (( $myrow4['sattotal'] > '9') &&($myrow4['sattotal']  <= '13')) {
		
		$sattimeandhalfhours = ($myrow4['sattotal'] - '9');
		$satnormal = '9'+ $myrow4['sattotalgap'];
		$satdouble = '0';
		}
	
	
		if ( $myrow4['sattotal']  >'13')  {
	  
	    $satnormal = '9'+ $myrow4['sattotalgap'];
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['sattotal']  - '9' );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		if ( $myrow4['suntotal'] <= '9')  {
		
		$sunnormal = $myrow4['suntotal']+ $myrow4['suntotalgap'];
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($myrow4['suntotal'] > '9') &&($myrow4['suntotal'] <= '13')) {
		
		$suntimeandhalfhours = ($myrow4['suntotal'] - '9');
		$sunnormal = '9'+ $myrow4['suntotalgap'];
		$sundouble = '0';
		}
	
	
		if ( $myrow4['suntotal'] >'13')  {
	  
	    $sunnormal = '9'+ $myrow4['suntotalgap'];
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['suntotal'] - '9' );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		} 
	


	

 $totalhours = $myrow4['montotal']+$myrow4['tuestotal']+$myrow4['wedtotal']+$myrow4['thurstotal']+$myrow4['fritotal']+$myrow4['sattotal']+$myrow4['suntotal'];
	
	
		
	$totalnormalhours = $monnormal +$tuesnormal+$wednormal+$thursnormal+$frinormal+$satnormal+$sunnormal;
	
	
	
	
	$totaltimeandhalf = $montimeandhalfhours + $tuestimeandhalfhours +$wedtimeandhalfhours+$thurstimeandhalfhours+$fritimeandhalfhours+$sattimeandhalfhours+$suntimeandhalfhours ;
	
	$totaldouble = $mondouble+$tuesdouble+$weddouble+$thursdouble+$fridouble+$satdouble+$sundouble;
	

	
	

	
	
}
	//calcualate payroll timesheet data if the contract for staffs is 9 hours a day and 5 days
elseif ($contract !== $nine ) {

if ( $myrow4['montotal'] <= '8')  {
		
		$monnormal = $myrow4['montotal'] + $myrow4['montotalgap'];
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($myrow4['montotal'] > '8') &&($myrow4['montotal'] <= '12')) {
		
		$montimeandhalfhours = ($myrow4['montotal'] - '8');
		$monnormal = '8'  + $myrow4['montotalgap'];
		$mondouble = '0';
		}
	
	
		if ( $myrow4['montotal'] >'12')  {
	  
	    $monnormal = '8'+ $myrow4['montotalgap'];
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['montotal']- '8');
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
	
	 if ( $myrow4['tuestotal'] <= '8')  {
		
		$tuesnormal = $myrow4['tuestotal']+ $myrow4['tuestotalgap'];
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($myrow4['tuestotal'] > '8') &&($myrow4['tuestotal'] <= '12')) {
		
		$tuestimeandhalfhours = ($myrow4['tuestotal'] - '8');
		$tuesnormal = '8'+ $myrow4['tuestotalgap'];
		$tuesdouble = '0';
		}
	
	
		if ($myrow4['tuestotal'] >'12')  {
	  
	    $tuesnormal = '8'+ $myrow4['tuestotalgap'];
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['tuestotal'] - '8' );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		 if ( $myrow4['wedtotal'] <= '8')  {
		
		$wednormal = $myrow4['wedtotal']+ $myrow4['wedtotalgap'];
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($myrow4['wedtotal'] > '8') &&($myrow4['wedtotal']<= '12')) {
		
		$wedtimeandhalfhours = ($myrow4['wedtotal']- '8');
		$wednormal = '8'+ $myrow4['wedtotalgap'];
		$weddouble = '0';
		}
	
	
		if ( $myrow4['wedtotal']>'12')  {
	  
	    $wednormal = '8'+ $myrow4['wedtotalgap'];
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['wedtotal'] - '8' );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		
		if ( $myrow4['thurstotal'] <= '8')  {
		
		$thursnormal = $myrow4['thurstotal']+ $myrow4['thurstotalgap'];
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($myrow4['thurstotal'] > '8') &&($myrow4['thurstotal'] <= '12')) {
		
		$thurstimeandhalfhours = ($myrow4['thurstotal'] - '8');
		$thursnormal = '8'+ $myrow4['thurstotalgap'];
		$thursdouble = '0';
		}
	
	
		if ( $myrow4['thurstotal'] >'12')  {
	  
	    $thursnormal = '8'+ $myrow4['thurstotalgap'];
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['thurstotal'] - '8' );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
		
	 if ($myrow4['fritotal'] <= '8')  {
		
		$frinormal = $myrow4['fritotal'] + $myrow4['fritotalgap'];
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($myrow4['fritotal']  > '8') &&($myrow4['fritotal']  <= '12')) {
		
		$fritimeandhalfhours = ($myrow4['fritotal']  - '8');
		$frinormal = '8'+ $myrow4['fritotalgap'];
		$fridouble = '0';
		}
	
	
		if ( $myrow4['fritotal']  >'12')  {
	  
	    $frinormal = '8'+ $myrow4['fritotalgap'];
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['fritotal'] - '8' );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
		if ($myrow4['sattotal']  <= '8')  {
		
		$satnormal = $myrow4['sattotal']+ $myrow4['sattotalgap'] ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (( $myrow4['sattotal'] > '8') &&($myrow4['sattotal']  <= '12')) {
		
		$sattimeandhalfhours = ($myrow4['sattotal'] - '8');
		$satnormal = '8'+ $myrow4['sattotalgap'];
		$satdouble = '0';
		}
	
	
		if ( $myrow4['sattotal']  >'12')  {
	  
	    $satnormal = '8'+ $myrow4['sattotalgap'];
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['sattotal']  - '8' );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		if ( $myrow4['suntotal'] <= '8')  {
		
		$sunnormal = $myrow4['suntotal']+ $myrow4['suntotalgap'];
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($myrow4['suntotal'] > '8') &&($myrow4['suntotal'] <= '12')) {
		
		$suntimeandhalfhours = ($myrow4['suntotal'] - '8');
		$sunnormal = '8'+ $myrow4['suntotalgap'];
		$sundouble = '0';
		}
	
	
		if ( $myrow4['suntotal'] >'12')  {
	  
	    $sunnormal = '8'+ $myrow4['suntotalgap'];
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['suntotal'] - '8' );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		} 

 $totalhours = $myrow4['montotal']+$myrow4['tuestotal']+$myrow4['wedtotal']+$myrow4['thurstotal']+$myrow4['fritotal']+$myrow4['sattotal']+$myrow4['suntotal'];
	

	
		
	$totalnormalhours = $monnormal +$tuesnormal+$wednormal+$thursnormal+$frinormal+$satnormal+$sunnormal;
	
	
	
	
	$totaltimeandhalf = $montimeandhalfhours + $tuestimeandhalfhours +$wedtimeandhalfhours+$thurstimeandhalfhours+$fritimeandhalfhours+$sattimeandhalfhours+$suntimeandhalfhours ;
	
	$totaldouble = $mondouble+$tuesdouble+$weddouble+$thursdouble+$fridouble+$satdouble+$sundouble;
	

	

      
       

		
}
		
			$monmealallowance= '0';
			$tuesmealallowance= '0';
			$wedmealallowance= '0';
			$thursmealallowance= '0';
			$frimealallowance= '0';
			$satmealallowance= '0';
			$sunmealallowance= '0';
			
			if (($montimeandhalfhours >= '2') AND ($mondouble>='2')){
				
				$monmealallowance= '1';
				
			}
			if (($tuestimeandhalfhours >= '2') AND ($tuesdouble>='2')){
				
				$tuesmealallowance= '1';
				
			}
			if (($wedtimeandhalfhours >= '2') AND ($weddouble>='2')){
				
				$wedmealallowance= '1';
				
			}
			if (($thurstimeandhalfhours >= '2') AND ($thursdouble>='2')){
				
				$thursmealallowance= '1';
				
			}
			if (($fritimeandhalfhours >= '2') AND ($fridouble>='2')){
				
				$frimealallowance= '1';
				
			}
			if (($sattimeandhalfhours >= '2') AND (satdouble>='2')){
				
				$satmealallowance= '1';
				
			}
			if (($suntimeandhalfhours >= '2') AND ($sundouble>='2')){
				
				$sunmealallowance= '1';
				
			}
			
			$totalmealunit = $monmealallowance+$tuesmealallowance+$wedmealallowance+$thursmealallowance+$frimealallowance+$satmealallowance+$sunmealallowance;
			
			$unitprice = '6.55';
			
			$mealtotalvalue = $unitprice * $totalmealunit;
			
			//for each employee do payrol calculations below and save in payrun_details table. insert for new records on same calender id and update for updating or recalculating tallies
			
			//get client details and rostering locations
			$sql5 =  "SELECT debtorsmaster.name, guardforceclient.patrollocation,guardforceclient.base_wage,guardforceclient.premium_rate,guardforceclient.debtorno from guardforceclient 
		    INNER JOIN debtorsmaster on guardforceclient.debtorno = debtorsmaster.debtorno 
			where clientid = '" . $myrow41['patrol_location']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql5,$ErrMsg);
			$myrow5 = DB_fetch_array($result5);
			$debtorno = $myrow5['debtorno'];
			$clientname =$myrow5['name'];
			$locationname =$myrow5['patrollocation'];
			$baseus_rate= $myrow5['base_wage'];
				$premium_rate= $myrow5['premium_rate'];
			//internal location
					$sql6 =  "SELECT location from internal_location
		
			where id = '" . $myrow41['internal_location']."'
			";
	$ErrMsg = _('This customer could not be added because');
			$result6 = DB_query($sql6,$ErrMsg);
			$myrow6 = DB_fetch_array($result6);
			
			$internallocation =$myrow6['location'];
			
			//technician
				$sql7 =  "SELECT projectdetails.project_name from projectdetails
		
			where projectid = '" . $myrow41['jobno']."'
			";
	$ErrMsg = _('This customer could not be added because');
			$result7 = DB_query($sql7,$ErrMsg);
			$myrow7 = DB_fetch_array($result7);
			
			$project =$myrow7['project_name'];
			
		
			
			
			//get the rates. usa embassy get rates from client table
			
			
			$sql41 =  "SELECT salary_type,paycycle,base_wage_rate,completed_pay_period,ytd_income_tax_withheld,ytd_srt_tax_withheld,ytd_ecal_tax_withheld,non_resident,payment_method,bank_name,account_name,account_number
				
						 
			FROM employeemaster   where employeeid = '" . $myrow4['employeeid']."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result41 = DB_query($sql41,$ErrMsg);
		
			
			
		$myrow41 = DB_fetch_array($result41);
				$salarytype = $myrow41['salary_type'];
				$paycycle = $myrow41['paycyle'];
				$completed_pay_period = $myrow41['completed_pay_period'];
			$baserate = $myrow41['base_wage_rate'];
			$ytd_income_tax_withheld = $myrow41['ytd_income_tax_withheld'];
			$ytd_srt_tax_withheld = $myrow41['ytd_srt-tax_withheld'];
			
				$ytd_ecal_tax_withheld = $myrow41['ytd_ecal_tax_withheld'];
				$nonresident = $myrow41['non_resident'];
			//do calculations for all hours and locations except usa locations. deduct usa hours from total ones and 
			//then multiply the  us hours by the us rates and add to the total then show the total normal pay.
			
			//detect where they working 
			
			if ($debtorno == 'US-EM'){
				
				$baserate = $baseus_rate;
				
						
										
			}
		
			//calculation logic for payrol
			
	
//start 	//process wage type staffs
			
			$totalnormalamount = $totalnormalhours * ($baserate + $premium_rate) ;
			
			$totaltimeandhalfamount =$totaltimeandhalf * (1.5 * $baserate);
			
			$totaldoubleamount = $totaldouble * (2 * $baserate);
			
			
			$bonusamount = $totaltimeandhalfamount + $totaldoubleamount;
			
		
			
		//get allowance
			//check in payrol_adjustments_details fnpf related
				$sql411 =  "SELECT amount
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 6
			OR  type_details = 16
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result411 = DB_query($sql411,$ErrMsg);
		
			$manualallowances= 0;
			while ($myrowallowances = DB_fetch_array($result411)){
				
				$allowances= $myrowallowances['amount'];
				
				$manualallowances = $manualallowances + $allowances;
			}
			
		
		
			
			$sql411 =  "SELECT gross
				   
						 
			FROM absent  where employeeid = '" . $myrow4['employeeid']."'
			and id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result411 = DB_query($sql411,$ErrMsg);
		
			$leaveallowances= 0;
			while ($myrowleaveallowances = DB_fetch_array($result411)){
				
				$allowances1= $myrowleaveallowances['amount'];
				
				$leaveallowances = $leaveallowances + $allowances1;
			}
				$bonusamount = $bonusamount + $manualallowances + $leaveallowances;
			//get manual deductions
				$sql42 =  "SELECT amount
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details between 8 and 15
			
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result42 = DB_query($sql42,$ErrMsg);
		
			$manualdeductions = 0;
			while ($myrowdeductions = DB_fetch_array($result41)){
				
				$deductions = $myrowdeductions['amount'];
				
				$manualdeductions = $manualdeductions + $deductions;
			}
			//get fixed deductions
				$sql43 =  "SELECT deduction_amount
				   
						 
			FROM fixeddeductions   where employeeid = '" . $myrow4['employeeid']."'
			AND status = 'pending'
			
			
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result43 = DB_query($sql43,$ErrMsg);
		
			$fixeddeductions = 0;
			while ($myrowfixeddeductions = DB_fetch_array($result43)){
				
				$fdeductions = $myrowfixeddeductions['deduction_amount'];
				
				$fixeddeductions = $fixeddeductions + $fdeductions;
			}
			
			
			
			
			
			//now lets calculate tax 
			$totalgrosspay = $totalnormalamount +$bonusamount;
			
			    $grossamount50 = $totalgrosspay * 0.5;
			
			//non fnpf allowances
			
			$sql4112 =  "SELECT amount
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 5
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4112 = DB_query($sql4112,$ErrMsg);
		
			$nonfnpfmanualallowances= 0;
			while ($nonfnpfmyrow = DB_fetch_array($result4112)){
				
				$allowances= $nonfnpfmyrow['amount'];
				
				$nonfnpfmanualallowances = $nonfnpfmanualallowances + $allowances;
			}
			
			
			//fnpf deductions
		//get fnpf rate
			
			$sql44 =  "SELECT employee_rate,employer_rate
				   
						 
			FROM fnpf_rate   
			
			
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result44 = DB_query($sql44,$ErrMsg);
		
			
		$myrowfnpf = DB_fetch_array($result44);
		
			$fnpfemployee = $myrowfnpf['employee_rate'];
			$fnpfemployer = $myrowfnpf['employer_rate'];
			
			$fnpfemployee_amount= $totalgrosspay * $fnpfemployee;
			$fnpfemployer_amount= $totalgrosspay * $fnpfemployer;
			//first get ytd tax withheld to determine how much suppose to be withheld till date
			
			//get yearly income tax and srt based on tax bracket
			
			$annualincome = $totalnormalamount * 52;
			
			$annualincomebonus = $annualincome + $bonusamount;
			
			if ($annualincomebonus >= 0 AND $annualincomebonus <= 30000){
				
				
				$annualincometax = 0;
				$annualsrt= 0;
				$thispaysrt = 0;
				$thisytd_srt = 0;
			
				
			}
			
			if ($annualincomebonus >= 30001 AND $annualincomebonus <= 50000){
				
				
				
					$taxableamount = $annualincomebonus - 30000;
			//	$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= ($taxableamount * 0.18);
					
				//$annualsrt = 3900+ ($taxableamount * 0.14);
					
					$thispayperiod = $completed_pay_period + 1;
				
				$ytd_srt_tax_withheld_current= 0;
					//$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
			//	$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current=0;
			//	$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
			
				
				
				
			}
			
				if ($annualincomebonus >= 50001 AND $annualincomebonus <= 270000){
				
				
				
				
					$taxableamount = $annualincomebonus - 50000;
			//	$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 3600+ ($taxableamount * 0.20);
					
				//$annualsrt = 3900+ ($taxableamount * 0.14);
					
					$thispayperiod = $completed_pay_period + 1;
				
				$ytd_srt_tax_withheld_current= 0;
					//$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
			//	$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current=0;
			//	$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
			
						
			}
				if ($annualincomebonus >= 270001 AND $annualincomebonus <= 300000){
				
				
				$taxableamount = $annualincomebonus - 270000;
				
				
				
				$annualincometax= 47600+($taxableamount * 0.20);
					
				$annualsrt = $taxableamount * 0.13;
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamount * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
				
			}
			if ($annualincomebonus >= 300001 AND $annualincomebonus <= 350000){
				
				
			$taxableamount = $annualincomebonus - 300000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 53600+($taxableamount * 0.20);
					
				$annualsrt = 3900+ ($taxableamount * 0.14);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
				
				
			}
			
			
			if ($annualincomebonus >= 350001 AND $annualincomebonus <= 400000){
			
			$taxableamount = $annualincomebonus - 350000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 63600+($taxableamount * 0.20);
					
				$annualsrt = 10900+ ($taxableamount * 0.14);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
				
			}
			if ($annualincomebonus >= 400001 AND $annualincomebonus <= 450000){
				
			$taxableamount = $annualincomebonus - 400000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 73600+($taxableamount * 0.20);
					
				$annualsrt = 18400+ ($taxableamount * 0.16);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
				
			}
			if ($annualincomebonus >= 450001 AND $annualincomebonus <= 500000){
				$taxableamount = $annualincomebonus - 450000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 83600+($taxableamount * 0.20);
					
				$annualsrt = 26400+ ($taxableamount * 0.17);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
				
			}
				if ($annualincomebonus >= 500001 AND $annualincomebonus <= 1000000){
				$taxableamount = $annualincomebonus - 500000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 93600+($taxableamount * 0.20);
					
				$annualsrt = 34900+ ($taxableamount * 0.18);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
				
			}
			if ($annualincomebonus >= 1000001){
				
					$taxableamount = $annualincomebonus - 500000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 193600+($taxableamount * 0.20);
					
				$annualsrt = 124900+ ($taxableamount * 0.19);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
				
			}
			//nonresident
			if ($nonresident == '1'){
				
				if ($annualincomebonus >= 0 AND $annualincomebonus <= 30000){
				
				
			
					//$taxableamount = $annualincomebonus - 30000;
			//	$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= ($annualincomebonus * 0.20);
					
				//$annualsrt = 3900+ ($taxableamount * 0.14);
					
					$thispayperiod = $completed_pay_period + 1;
				
				$ytd_srt_tax_withheld_current= 0;
					//$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
			//	$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current=0;
			//	$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
			
			
				
			}
			
			if ($annualincomebonus >= 30001 AND $annualincomebonus <= 50000){
				
				
				
					$taxableamount = $annualincomebonus - 30000;
			//	$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 6000+($taxableamount * 0.18);
					
				//$annualsrt = 3900+ ($taxableamount * 0.14);
					
					$thispayperiod = $completed_pay_period + 1;
				
				$ytd_srt_tax_withheld_current= 0;
					//$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
			//	$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current=0;
			//	$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
			
				
				
				
			}
			
				if ($annualincomebonus >= 50001 AND $annualincomebonus <= 270000){
				
				
				
				
					$taxableamount = $annualincomebonus - 50000;
			//	$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 10000+ ($taxableamount * 0.20);
					
				//$annualsrt = 3900+ ($taxableamount * 0.14);
					
					$thispayperiod = $completed_pay_period + 1;
				
				$ytd_srt_tax_withheld_current= 0;
					//$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
			//	$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current=0;
			//	$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
			
						
			}
				if ($annualincomebonus >= 270001 AND $annualincomebonus <= 300000){
				
				
				$taxableamount = $annualincomebonus - 270000;
				
				
				
				$annualincometax= 54000+($taxableamount * 0.20);
					
				$annualsrt = $taxableamount * 0.13;
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamount * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
				
			}
			if ($annualincomebonus >= 300001 AND $annualincomebonus <= 350000){
				
				
			$taxableamount = $annualincomebonus - 300000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 60000+($taxableamount * 0.20);
					
				$annualsrt = 3900+ ($taxableamount * 0.14);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
				
				
			}
			
			
			if ($annualincomebonus >= 350001 AND $annualincomebonus <= 400000){
			
			$taxableamount = $annualincomebonus - 350000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 70000+($taxableamount * 0.20);
					
				$annualsrt = 10900+ ($taxableamount * 0.14);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
				
			}
			if ($annualincomebonus >= 400001 AND $annualincomebonus <= 450000){
				
			$taxableamount = $annualincomebonus - 400000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 80000+($taxableamount * 0.20);
					
				$annualsrt = 18400+ ($taxableamount * 0.16);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
				
			}
			if ($annualincomebonus >= 450001 AND $annualincomebonus <= 500000){
				$taxableamount = $annualincomebonus - 450000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 90000+($taxableamount * 0.20);
					
				$annualsrt = 26400+ ($taxableamount * 0.17);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
				
			}
				if ($annualincomebonus >= 500001 AND $annualincomebonus <= 1000000){
				$taxableamount = $annualincomebonus - 500000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 10000+($taxableamount * 0.20);
					
				$annualsrt = 34900+ ($taxableamount * 0.18);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
				
			}
			if ($annualincomebonus >= 1000001){
				
					$taxableamount = $annualincomebonus - 500000;
				$taxableamountecal =  $annualincomebonus - 270000;
				
				
				$annualincometax= 20000+($taxableamount * 0.20);
					
				$annualsrt = 124900+ ($taxableamount * 0.19);
					
					$thispayperiod = $completed_pay_period + 1;
				
					$ytd_srt_tax_withheld_current =    $annualsrt / 52 * $thispayperiod;			
						
						$thispaysrt = $ytd_srt_tax_withheld_current -$ytd_srt_tax_withheld;
					
				$annualecal = $taxableamountecal * 0.10;
				$ytd_ecal_tax_withheld_current =    $annualecal / 52 * $thispayperiod;	
				$thispayecal = $ytd_ecal_tax_withheld_current -$ytd_ecal_tax_withheld;
			
  
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt - $thispayecal-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
				
			}
				
			}
			//current ytd_income tax
			
			
			
			//save in payrun-details table
			
				$sql45 =  "SELECT id
				   
						 
			FROM payrun_details   where id = '" . $calendarid."'
						
			";
	$ErrMsg = _('This customer could not be added because');
			$result45 = DB_query($sql45,$ErrMsg);
		
		
	$myrowid = DB_fetch_array($result45);
			$userid = $_SESSION['UserID'];
			
			$timenow = date('Y-m-d H:i:s');

if (!isset($myrowid['id'])){
	
	//insert the database
	
	$sql = "INSERT INTO payrun_details (
							id,
							date_processed,
							employee_type,
							employeeid,
							department,
							normal_pay,
							timehalf_amount,
							double_amount,
							leave_allowances,
							other_allowances_fnpf,
							other_allowances_nonfnpf,
							meal_allowance,
							total_bonus,
							totalgross_amount,
							paycycle,
							completed_cycle,
							annual_income,
							annual_income_with_bonus,
							annual_income_tax,
							annual_srt_tax,
							annual_ecal_tax,
							annual_total_tax,
							income_tax_to_withhold,
							srt_tax_to_withhold,
							ecal_tax_to_withhold,
							total_tax_to_withhold,
							fnpf,
							fnpf_employer,
							fixed_deductions,
							other_deductions,
							net_pay,
							method_of_payment,
							bank_name,
							bank_account_name,
							bank_account_number,
							entered_by)
				VALUES ('" . $calendarid."',
						'" . $timenow ."',
						'Timesheet',
						'" . $myrow4['employeeid'] ."',
						'" . $myrow4['department'] ."',
						'" . $totalnormalamount."',
						'" . $totaltimeandhalfamount ."',
						'" . $totaldoubleamount . "',
						'" . $leaveallowances. "',
						'" . $manualallowances . "',
						'" . $nonfnpfmanualallowances . "',
						'" . $mealtotalvalue . "',
						'" . $bonusamount . "',
						'" . $totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances + $leaveallowances. "',
						'52',
						'" . $thispayperiod . "',
						'" . $annualincome . "',
						'" . $annualincomebonus . "',
						'" . $annualincometax . "',
						'" . $annualsrt . "',
						'" . $annualecal . "',
						'" . $annualincometax + $annualsrt +$annualecal. "',
						'" . $thisincometaxtopay. "',
						'" . $thispaysrt . "',
						'" . $thispayecal . "',
						'" . $thisincometaxtopay +$thispaysrt+$thispayecal  . "',
						'" . $fnpfemployee. "',
						'" . $fnpfemployer . "',
						'" . $fixeddeductions . "',
						'" . $manualdeductions. "',
						'" . $totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances - $thisincometaxtopay - $thispaysrt -$thispayecal - $fnpfemployee-$fixeddeductions-$manualdeductions. "',
						'".$myrow41['payment_method']."',
						'".$myrow41['bank_name']."',
						'".$myrow41['account_name']."',
						'".$myrow41['account_number']."',
						'".$userid."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
	
	
	
}

if (isset($myrowid['id'])){
	
//update the database
	
	  $sql = "UPDATE payrun_details SET	
			  									date_processed='" . $timenow . "',
												employee_type='Timesheet',
												employeeid='" . $myrow4['employeeid'] . "',
												department='" . $myrow4['department'] ."',
												normal_pay='" . $totalnormalamount . "',
												timehalf_amount='" . $totaltimeandhalfamount ."',
												double_amount='" . $totaldoubleamount . "',
												leave_allowances='" . $leaveallowances . "',
												other_allowances_fnpf='" . $manualallowances . "',
												other_allowances_nonfnpf='" . $nonfnpfmanualallowances . "',
												meal_allowance='" . $mealtotalvalue . "',
							total_bonus='" . $bonusamount . "',
							totalgross_amount='" .$totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances + $leaveallowances. "',
							paycycle='52',
							completed_cycle='" . $thispayperiod . "',
							annual_income='" . $annualincome . "',
							annual_income_with_bonus='" . $annualincomebonus . "',
							annual_income_tax='" . $annualincometax . "',
							annual_srt_tax='" . $annualsrt. "',
							annual_ecal_tax='" . $annualecal . "',
							annual_total_tax='" . $annualincometax + $annualsrt +$annualecal. "',
							income_tax_to_withhold='" . $thisincometaxtopay. "',
							srt_tax_to_withhold='" . $thispaysrt . "',
							ecal_tax_to_withhold='" . $thispayecal. "',
							total_tax_to_withhold='" . $thisincometaxtopay +$thispaysrt+$thispayecal  . "',
							fnpf='" . $fnpfemployee. "',
							fnpf_employer='" . $fnpfemployer. "',
							fixed_deductions='" . $fixeddeductions . "',
							other_deductions='" . $manualdeductions. "',
							net_pay='" . $totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances - $thisincometaxtopay - $thispaysrt -$thispayecal - $fnpfemployee-$fixeddeductions-$manualdeductions. "',
							method_of_payment='".$myrow41['payment_method']."',
							bank_name='".$myrow41['bank_name']."',
							bank_account_name='".$myrow41['account_name']."',
							bank_account_number='".$myrow41['account_number']."',
							entered_by='".$userid."'
						WHERE  id = '".$myrowid['id']."' ";
	
}	
		
			
			
			
		}//end of pay loop

//insert in database before checking if update is needed.


	//process salary type is fixed


//this is for case for none timesheet staffs

	
				$taxableamount = $annualincomebonus - 30001;
				
				
				
				$annualincometax= $taxableamount * 0.18;
				$annualsrt = 0;
				$thispaysrt = 0;
				$thisytd_srt = 0;
				$thispayperiod = $completed_pay_period + 1;
				
				
				switch ($paycycle) {
						//weekly
    case "2":
       
			$ytd_income_tax_withheld_current =    $annualincometax / 52 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
						
						
        break;
						
						//fortnightly
    case "3":
        $ytd_income_tax_withheld_current =    $annualincometax / 26 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
						
						//monthly
    case "4":
          $ytd_income_tax_withheld_current =    $annualincometax / 26 * $thispayperiod;			
						
						$thisincometaxtopay = $ytd_income_tax_withheld_current -$ytd_income_tax_withheld;
						$comparetax = $thisincometaxtopay + $thispaysrt+$fnpfemployee + $manualdeductions + $fixeddeductions;
						
						if ($comparetax > $grossamount50 ){
							
							$thisincometaxtopay = $grossamount50 - $thispaysrt-$fnpfemployee - $manualdeductions - $fixeddeductions;
						}
        break;
    
}



		
	
echo '</div>';
  
 	echo '</div >';


echo '<div class="row"> <div class="col-sm-12" >';

//echo '<iframe src="Reporttimesheetsuva-GFS.php" height="400" width="100%">
  // alternative content for browsers which do not support iframe.
//</iframe>';		?>
										
						</div>

						<!--end:: Widgets/Stats-->

						<!--Begin::Section-->
						

		<!-- end::Quick Sidebar -->

		<!-- begin::Scroll Top -->
		<div id="m_scroll_top" class="m-scroll-top">
			<i class="la la-arrow-up"></i>
		</div>

		<!-- end::Scroll Top -->

		<!-- begin::Quick Nav -->
		

		<!-- begin::Quick Nav -->
<script src="includes/vendors/owl.carousel/dist/owl.carousel.js" type="text/javascript"></script>

		<!--end::Page Scripts -->
	</body>

	<!-- end::Body -->
</html>
 
      
<?php



/*The module link codes are hard coded in a switch statement below to determine the options to show for each tab */
include('includes/MainMenuLinksArray.php');

if (isset($SupplierLogin) AND $SupplierLogin==1){
	echo '<table class="table_index">
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/SupplierTenders.php?TenderType=1">' . _('View or Amend outstanding offers') . '</a></p>
			</td>
			</tr>
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/SupplierTenders.php?TenderType=2">' . _('Create a new offer') . '</a></p>
			</td>
			</tr>
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/SupplierTenders.php?TenderType=3">' . _('View any open tenders without an offer') . '</a></p>
			</td>
			</tr>
		</table>';
	include('includes/footer.inc');
	exit;
} elseif (isset($CustomerLogin) AND $CustomerLogin==1){
	echo '<table class="table_index">
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/CustomerInquiry.php?CustomerID=' . $_SESSION['CustomerID'] . '">' . _('Account Status') . '</a></p>
			</td>
			</tr>
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/SelectOrderItems.php?NewOrder=Yes">' . _('Place An Order') . '</a></p>
			</td>
			</tr>
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/SelectCompletedOrder.php?SelectedCustomer=' . $_SESSION['CustomerID'] . '">' . _('Order Status') . '</a></p>
			</td>
			</tr>
		</table>';

	//include('includes/footer.inc');
	exit;
}

if (isset($_GET['Application'])){ /*This is sent by this page (to itself) when the user clicks on a tab */
	$_SESSION['Module'] = $_GET['Application'];
}








//include('includes/footer.inc');

function GetRptLinks($GroupID) {
/*
This function retrieves the reports given a certain group id as defined in /reports/admin/defaults.php
in the acssociative array $ReportGroups[]. It will fetch the reports belonging solely to the group
specified to create a list of links for insertion into a table to choose a report. Two table sections will
be generated, one for standard reports and the other for custom reports.
*/
	global $db, $RootPath, $ReportList;
	require_once('reportwriter/languages/en_US/reports.php');
	require_once('reportwriter/admin/defaults.php');
	$GroupID=$ReportList[$GroupID];
	$Title= array(_('Custom Reports'), _('Standard Reports and Forms'));

	$sql= "SELECT id,
				reporttype,
				defaultreport,
				groupname,
				reportname
			FROM reports
			ORDER BY groupname,
					reportname";
	$Result=DB_query($sql,'','',false,true);
	$ReportList = '';
	while ($Temp = DB_fetch_array($Result)) $ReportList[] = $Temp;

	$RptLinks = '';
	for ($Def=1; $Def>=0; $Def--) {
        $RptLinks .= '<li class="menu_group_headers">';
        $RptLinks .= '<b>' .  $Title[$Def] . '</b>';
        $RptLinks .= '</li>';
		$NoEntries = true;
		if ($ReportList) { // then there are reports to show, show by grouping
			foreach ($ReportList as $Report) {
				if ($Report['groupname']==$GroupID AND $Report['defaultreport']==$Def) {
                    $RptLinks .= '<li class="menu_group_item">';
					$RptLinks .= '<p>&bull; <a href="' . $RootPath . '/reportwriter/ReportMaker.php?action=go&amp;reportid=' . $Report['id'] . '">' . _($Report['reportname']) . '</a></p>';
					$RptLinks .= '</li>';
					$NoEntries = false;
				}
			}
			// now fetch the form groups that are a part of this group (List after reports)
			$NoForms = true;
			foreach ($ReportList as $Report) {
				$Group=explode(':',$Report['groupname']); // break into main group and form group array
				if ($NoForms AND $Group[0]==$GroupID AND $Report['reporttype']=='frm' AND $Report['defaultreport']==$Def) {
                    $RptLinks .= '<li class="menu_group_item">';
					$RptLinks .= '<img src="' . $RootPath . '/css/' . $_SESSION['Theme'] . '/images/folders.gif" width="16" height="13" alt="" />&nbsp;';
					$RptLinks .= '<p>&bull; <a href="' . $RootPath . '/reportwriter/FormMaker.php?id=' . $Report['groupname'] . '"></p>';
					$RptLinks .= $FormGroups[$Report['groupname']] . '</a>';
					$RptLinks .= '</li>';
					$NoForms = false;
					$NoEntries = false;
				}
			}
		}
		if ($NoEntries) $RptLinks .= '<li class="menu_group_item">' . _('There are no reports to show!') . '</li>';
	}
	return $RptLinks;
}
	
?>
<script>



	jQuery(document).ready(function() {
    $("#projectreport").DataTable( {
	fnRowCallback: function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
      if ( aData[6] > 48 )
      {
        $('td', nRow).css('background-color', '#f2dede' );
      }
       else if ( aData[6] == 48 )
      {
        $('td', nRow).css('background-color', '	#ffd480' );
      }
    
      
      else
      {
        $('td', nRow).css('background-color', '#e6ffe6');
      }
    },
		
        dom: 'Bfrtip',
        buttons: [
           
		{
                extend: 'pdfHtml5',
                orientation: 'portrait',
			pageSize: 'A4',
			
			
                customize: function(doc) {
      doc.defaultStyle.fontSize = 12; //<-- set fontsize to 16 instead of 10 
					  
   }  
            },
			{
                extend: 'excelHtml5'},
			
			{
                extend: 'copyHtml5'}
			
			
		]
		
		
    } );
		
		$("#projectreport2").DataTable( {
			
			fnRowCallback: function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
      if ( aData[9] > 48 )
      {
        $('td', nRow).css('background-color', '#f2dede' );
      }
       else if ( aData[9] == 48 )
      {
        $('td', nRow).css('background-color', '	#ffd480' );
      }
    
      
      else
      {
        $('td', nRow).css('background-color', '#e6ffe6');
      }
    },
        dom: 'Bfrtip',
        buttons: [
           
		{
                extend: 'pdfHtml5',
                orientation: 'landscape',
			pageSize: 'A4',
			
			
                customize: function(doc) {
      doc.defaultStyle.fontSize = 12; //<-- set fontsize to 16 instead of 10 
					  
   }  
            },
			{
                extend: 'excelHtml5'},
			
			{
                extend: 'copyHtml5'}
			
			
		]
    } );
	
		$("#projectreport3").DataTable( {
			fnRowCallback: function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
      if ( aData[29] > 48 )
      {
        $('td', nRow).css('background-color', '#f2dede' );
      }
       else if ( aData[29] == 48 )
      {
        $('td', nRow).css('background-color', '	#ffd480' );
      }
    
      
      else
      {
        $('td', nRow).css('background-color', '#e6ffe6');
      }
    },
			
        dom: 'Bfrtip',
        buttons: [
           
		{
                extend: 'pdfHtml5',
                orientation: 'landscape',
			pageSize: 'A2',
			
			
                customize: function(doc) {
      doc.defaultStyle.fontSize = 12; //<-- set fontsize to 16 instead of 10 
					  
   }  
            },
			{
                extend: 'excelHtml5'},
			
			{
                extend: 'copyHtml5'}
			
			
		]
    } );	
	
} );
		


	
						
</script>