  <?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>
     
		


  
 									
									<?php

		
  if (isset($_GET['status'])) {
		$_SESSION['status'] = stripslashes($_GET['status']);
	}
	$status = $_SESSION['status'];

if ($status == "locked" || $status == "pending" || $status == "processed"  ){
	$message = "Status is not approved or already processed or locked. ";
	echo "<script type='text/javascript'>alert('$message');</script>";
echo "<script type='text/javascript'>javascript:history.go(-1)</script>"; 
	exit();
	
}
	else {								
		
  if (isset($_GET['id'])) {
		$_SESSION['id'] = stripslashes($_GET['id']);
	}
	$calendarid = $_SESSION['id'];





$sql4 =  "SELECT 
	              timesheetsuvadetails.*,
				   			 
							  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
				   
						 
			FROM timesheetsuvadetails  
			where 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
			
			$sql77 =  "SELECT contract
				
						 
			FROM payrun_employee_contract   where employeeid = '" . $myrow4['employeeid']."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result77 = DB_query($sql77,$ErrMsg);
		
			
			
		$myrow77 = DB_fetch_array($result77);
			
			
			$contract = $myrow77['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;
	  
	  
		} 
	
if ( $myrow4['mon2total'] <= '9')  {
		
		$mon2normal = $myrow4['mon2total']+ $myrow4['mon2totalgap'];
		$mon2timeandhalfhours = '0';
		$mon2double = '0';
		
     	}
	
		if (($myrow4['mon2total'] > '9') &&($myrow4['mon2total'] <= '13')) {
		
		$mon2timeandhalfhours = ($myrow4['mon2total'] - '9');
		$mon2normal = '9'+ $myrow4['mon2totalgap'];
		$mon2double = '0';
		}
	
	
		if ( $myrow4['mon2total'] >'13')  {
	  
	    $mon2normal = '9'+ $myrow4['mon2totalgap'];
		$mon2timeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['mon2total'] - '9' );
	  	   
	    
	    $mon2double = $totalovertimehours - $mon2timeandhalfhours;
	  
	  
		} 

	

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

	
	

	
	
}
	//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;
	  
	  
		} 
	
		if ( $myrow4['mon2total'] <= '8')  {
		
		$mon2normal = $myrow4['mon2total']+ $myrow4['mon2totalgap'];
		$mon2timeandhalfhours = '0';
		$mon2double = '0';
		
     	}
	
		if (($myrow4['mon2total'] > '8') &&($myrow4['mon2total'] <= '12')) {
		
		$mon2timeandhalfhours = ($myrow4['mon2total'] - '8');
		$mon2normal = '8'+ $myrow4['mon2totalgap'];
		$mon2double = '0';
		}
	
	
		if ( $myrow4['mon2total'] >'12')  {
	  
	    $mon2normal = '8'+ $myrow4['mon2totalgap'];
		$mon2timeandhalfhours = '4';
	  
		$totalovertimehours = ($myrow4['mon2total'] - '8' );
	  	   
	    
	    $mon2double = $totalovertimehours - $mon2timeandhalfhours;
	  
	  
		} 

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

	

      
       

		
}
		
			$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';
				
			}
			if (($mon2timeandhalfhours >= '2') AND ($mon2double>='2')){
				
				$mon2mealallowance= '1';
				
			}
			$totalmealunit = $monmealallowance+$tuesmealallowance+$wedmealallowance+$thursmealallowance+$frimealallowance+$satmealallowance+$sunmealallowance+$mon2mealallowance;
			
			$unitprice = '7.20';
			
			$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
			
			$sql05 =  "SELECT * from timesheetsuva
		    
			where timesuvaid = '" . $myrow4['timesuvaid']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result05 = DB_query($sql05,$ErrMsg);
			$myrow05 = DB_fetch_array($result05);
			
			//get client details and rostering locations
			$sql5 =  "SELECT base_wage,premium, premium_rate from guardforceclient 
		   
			where clientid = '" . $myrow05['patrol_location']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql5,$ErrMsg);
			$myrow5 = DB_fetch_array($result5);
	
			$baseus_rate= $myrow5['base_wage'];
				$premium_rate= $myrow5['premium_rate'];
			//internal location
					$sql6 =  "SELECT location from internal_location
		
			where id = '" . $myrow05['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 = '" . $myrow05['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 department,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,sick_leave_available,annual_leave_available,bereavement_leave_available,familycare_leave_available,paternity_leave_available,maternity_leave_available,ytd_sick_leave,ytd_annual_leave,ytd_bereavement_leave,ytd_familycare_leave,ytd_paternity_leave,ytd_maternity_leave,total_bonuspaid_to_date,
			total_paid_till_date,fnpf_deducted_till_date,fnpf_employers_contribution_till_date
				
						 
			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'];
			$departmentemployee= $myrow41['department'];
				$ytd_ecal_tax_withheld = $myrow41['ytd_ecal_tax_withheld'];
				$nonresident = $myrow41['non_resident'];
			$bonuspaidtilldate = $myrow41['total_bonuspaid_to_date'];
				$ytdpay = $myrow41['total_paid_till_date'];
			$ytdemployeecontribution = $myrow41['fnpf_deducted_till_date'];
			$ytdemployercontribution = $myrow41['fnpf_employers_contribution_till_date'];
			//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 ($myrow05['patrol_location'] >= 30 AND $myrow05['patrol_location'] <=54){
				
				$baserate = $baseus_rate;
				
						
										
			}
			
		
			//calculation logic for payrol
			
			//first check if the location they worked in a day where there was any premium rate. open location they worked and hours if the premium rate is 
	$sqlcheck =  "SELECT timesheetsuvadetails.total_hrs,timesheetsuvadetails.timesuvaid,timesheetsuva.patrol_location
			
						 
			FROM timesheetsuvadetails INNER JOIN timesheetsuva on 
			timesheetsuvadetails.timesuvaid = timesheetsuva.timesuvaid
			
			
			where employeeid = '" . $myrow4['employeeid']."' AND calendar_id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultcheck = DB_query($sqlcheck,$ErrMsg);
		
			
			$totalnormalamount = 0;
			$x = 0;
			$k =$totalnormalhours;

			
			while (($myrowcheck = DB_fetch_array($resultcheck))) {	
		  
				//get rates
				
				//get client details and rostering locations
			$sql5 =  "SELECT base_wage,premium_rate,premium from guardforceclient 
		   
			where clientid = '" . $myrowcheck['patrol_location']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql5,$ErrMsg);
			$myrow5 = DB_fetch_array($result5);
	
			$baseus_rate= $myrow5['base_wage'];
				$premium_rate= $myrow5['premium_rate'];
				
				
		       if ($myrowcheck['patrol_location'] >= 30 AND $myrowcheck['patrol_location'] <=54){
				
				$baserate = $baseus_rate; 
																	
			  }
				
				$yy= $k - $x;
			
				if (($yy >= $myrowcheck['total_hrs']  ) ) {
						
					 
					
						  $normalamount = $myrowcheck['total_hrs'] * ($baserate + $premium_rate);
				
						  $totalnormalamount = $totalnormalamount + $normalamount;
				
						  $x = $x + $myrowcheck['total_hrs'];
					 
						 
						
			  	  }
					 
				 		if (($yy < $myrowcheck['total_hrs']  ) ) {
						
					 
					
						  $normalamount = $yy * ($baserate + $premium_rate);
				
						  $totalnormalamount = $totalnormalamount + $normalamount;
				
						 // $x = $x + $myrowcheck['total_hrs'];
					 
						
			  	  }
				
		
	       }
		//	$totalnormalamount = $totalnormalhours * $baserate ;
			
			$totaltimeandhalfamount =$totaltimeandhalf * (1.5 * $baserate);
			
			$totaldoubleamount = $totaldouble * (2 * $baserate);
			
			
			$thisbonusamount = $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;
			}
			
		
		
			
			$sql4119 =  "SELECT gross
				   
						 
			FROM absent  where employeeid = '" . $myrow4['employeeid']."'
			and id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			
			$result4119 = DB_query($sql4119,$ErrMsg);
		
			$leaveallowances= 0;
			while ($m = DB_fetch_array($result4119)){
				
				$Lallowances= $m['gross'];
				
				$leaveallowances = $leaveallowances + $Lallowances;
				
				
				
				
				
				
				
			}
				
			
			$thisbonusamount = $thisbonusamount+ $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 total_amount_to_deduct,deduction_amount,total_paid_till_date
				   
						 
			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;
				
				
			$totalpaidtilldate = $myrowfixeddeductions['total_paid_till_date']+$myrowfixeddeductions['deduction_amount'];
				
				$balanceleft = $myrowfixeddeductions['total_amount_to_deduct'] - $totalpaidtilldate;
			
            $sql399 = "UPDATE fixeddeductions SET total_paid_till_date= '".$totalpaidtilldate."', 
												  balance_left= '".$balanceleft."'
						WHERE employeeid= '" . $myrow4['employeeid'] . "'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result399 = DB_query($sql399,$ErrMsg);

			
			
				
				
				
			}
			
			
			
			
			
			//now lets calculate tax 
			$totalgrosspay = $totalnormalamount +$thisbonusamount;
			
			    $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;
			
			$bonuspaidtilldate = $bonuspaidtilldate + $thisbonusamount;
			
			$annualincomebonus = $annualincome + $bonuspaidtilldate;
			
			if ($annualincomebonus >= 0 AND $annualincomebonus <= 30000){
				
					$thispayperiod = $completed_pay_period + 1;
				$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
			
				
			
			$userid = $_SESSION['UserID'];
			
			$timenow = date('Y-m-d H:i:s');

		
	if ($annualecal = 0) {
		
		$annualecal = 0.00;
	}
	
	//insert the database
	
	$sql = "INSERT INTO payrun_details (
							id,
							date_processed,
							department,
							employee_type,
							employeeid,
							normal_hours,
							normal_pay,
							timehalf_hours,
							timehalf_amount,
							double_hours,
							double_amount,
							leave_allowances,
							other_allowances_fnpf,
							other_allowances_nonfnpf,
							meal_allowance,
							total_bonus,
							totalgross_amount,
							pay_cycle,
							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 ."',
						'" . $departmentemployee ."',
						'Timesheet',
						'" . $myrow4['employeeid'] ."',
						'" . $totalnormalhours ."',
						'" . $totalnormalamount."',
						'" . $totaltimeandhalf ."',
						'" . $totaltimeandhalfamount ."',
						'" . $totaldouble."',
						'" . $totaldoubleamount . "',
						'" . $leaveallowances. "',
						'" . $manualallowances . "',
						'" . $nonfnpfmanualallowances . "',
						'" . $mealtotalvalue . "',
						'" . $thisbonusamount . "',
						'" . ($totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances ). "',
						'52',
						'" . $thispayperiod . "',
						'" . $annualincome . "',
						'" . $annualincomebonus . "',
						'" . $annualincometax . "',
						'" . $annualsrt . "',
						'" . $annualecal . "',
						'" . ($annualincometax + $annualsrt +$annualecal). "',
						'" . $thisincometaxtopay. "',
						'" . $thispaysrt . "',
						'" . $thispayecal . "',
						'" . ($thisincometaxtopay +$thispaysrt+$thispayecal)  . "',
						'" . $fnpfemployee_amount. "',
						'" . $fnpfemployer_amount . "',
						'" . $fixeddeductions . "',
						'" . $manualdeductions. "',
						'" . ($totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances - $thisincometaxtopay - $thispaysrt -$thispayecal - $fnpfemployee_amount-$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);
			
			$netpay = $totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances - $thisincometaxtopay - $thispaysrt -$thispayecal - $fnpfemployee_amount-$fixeddeductions-$manualdeductions;
			
			$ytdpay = $ytdpay + $netpay;
			$ytd_income_tax_withheld = $ytd_income_tax_withheld + $thisincometaxtopay;
			$ytd_srt_tax_withheld = $ytd_srt_tax_withheld +$thispaysrt;
			$ytd_ecal_tax_withheld = $ytd_ecal_tax_withheld + $thispayecal;
			$ytdemployeecontribution = $ytdemployeecontribution + $fnpfemployee_amount;
			$ytdemployercontribution = $ytdemployercontribution + $fnpfemployer_amount;
	
			$sql39 = "UPDATE employeemaster SET completed_pay_period= '".$thispayperiod."',
												total_bonuspaid_to_date= '".$bonuspaidtilldate."',
												total_paid_till_date = '".$ytdpay."',
												ytd_income_tax_withheld = '".$ytd_income_tax_withheld."',
												ytd_srt_tax_withheld = '".$ytd_srt_tax_withheld."',
												ytd_ecal_tax_withheld = '".$ytd_ecal_tax_withheld."',
												fnpf_deducted_till_date = '".$ytdemployeecontribution."',
												fnpf_employers_contribution_till_date = '".$ytdemployercontribution."',
												fnpf_total_till_date = '".($ytdemployercontribution + $ytdemployeecontribution)."'
												
												   
						WHERE employeeid= '" . $myrow4['employeeid'] . "'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result39 = DB_query($sql39,$ErrMsg);
			
	//get the payid from above
				$sqlpayid =  "SELECT payid
			
						 
			FROM payrun_details  ORDER BY payid DESC LIMIT 1;
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultpayid = DB_query($sqlpayid,$ErrMsg);
			$myrowpayid = DB_fetch_array($resultpayid);
			
			$lastid = $myrowpayid['payid'];

			//get defailt department
			$sql41 =  "SELECT department
				
						 
			FROM employeemaster   where employeeid = '" . $myrow4['employeeid']."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result41 = DB_query($sql41,$ErrMsg);
			$row = DB_fetch_array($result41);
			
	//get second table output done
		//first check if the location they worked in a day where there was any premium rate. open location they worked and hours if the premium rate is 
	$sqlcheck =  "SELECT timesheetsuvadetails.mon_total,timesheetsuvadetails.timesuvaid,timesheetsuva.timesuvaid,timesheetsuva.patrol_location,timesheetsuva.department
	                   
			
						 
			FROM timesheetsuvadetails INNER JOIN timesheetsuva on 
			timesheetsuvadetails.timesuvaid = timesheetsuva.timesuvaid
			
			
			where employeeid = '" . $myrow4['employeeid']."' AND calendar_id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultcheck = DB_query($sqlcheck,$ErrMsg);
		
			
			$monnormalamount = 0;
			$x = 0;
			$k =$monnormal;

			
			while (($myrowcheck = DB_fetch_array($resultcheck))) {	
		  
				//get rates
				
				//get client details and rostering locations
			$sql5 =  "SELECT department.departmentname,guardforceclient.patrollocation,guardforceclient.base_wage,guardforceclient.premium_rate from guardforceclient 
			
			INNER JOIN department on 
			guardforceclient.department = department.departmentid
		   
			where clientid = '" . $myrowcheck['patrol_location']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql5,$ErrMsg);
			$myrow5 = DB_fetch_array($result5);
	
			$baseus_rate= $myrow5['base_wage'];
				
				$premium_rate= $myrow5['premium_rate'];
				
			
				
			if ($myrowcheck['mon_total'] > 0){	
		       if ($myrowcheck['patrol_location'] >= 30 AND $myrowcheck['patrol_location'] <=54){
				
				$baserate = $baseus_rate; 
																	
			  }
				
				$yy= $k - $x;
			
				if (($yy >= $myrowcheck['mon_total']  ) ) {
						
					 
					
						  $normalamount = $myrowcheck['mon_total'] * ($baserate + $premium_rate);
				
						  $monnormalamount = $monnormalamount + $normalamount;
				
						  $x = $x + $myrowcheck['mon_total'];
					
					 $description = $myrow5['patrollocation'] ;
					
						if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['mon_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
					
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-Mon',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $x ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'" . $premiumhours."',
						'" . $premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
						 
						
			  	  }
					 
				 		if (($yy < $myrowcheck['mon_total']  ) ) {
						
					 
					
						  $normalamount = $yy * ($baserate + $premium_rate);
				
						  $monnormalamount = $monnormalamount+ $normalamount;
						if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['mon_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
					// $x = $x + $myrowcheck['mon_total'];
					 
						$description = $myrow5['patrollocation'] ;
							
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-Mon',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $yy ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'" . $premiumhours."',
						'" . $premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);  
			  	  } 
				
			
			}
				
				
				
		
	       }
		//	$totalnormalamount = $totalnormalhours * $baserate ;
			
			$montimeandhalfamount =$montimeandhalfhours * (1.5 * $baserate);
			
			$mondoubleamount = $mondouble * (2 * $baserate);
			
			
			if ($montimeandhalfamount > 0){
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-Mon',
						'Time and half-Mon',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $montimeandhalfhours."',
						'" . (1.5 * $baserate)."',
						'0',
						'" . ($montimeandhalfhours *(1.5 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			
			}
			if ($mondoubleamount > 0){
			
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-Mon',
						'Double-Mon',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $mondouble."',
						'" . (2* $baserate)."',
						'0',
						'" . ($mondouble *(2 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
			
		//tues
			
			$sqlcheck =  "SELECT timesheetsuvadetails.tues_total,timesheetsuvadetails.timesuvaid,timesheetsuva.timesuvaid,timesheetsuva.patrol_location,timesheetsuva.department
	                   
			
						 
			FROM timesheetsuvadetails INNER JOIN timesheetsuva on 
			timesheetsuvadetails.timesuvaid = timesheetsuva.timesuvaid
			
			
			where employeeid = '" . $myrow4['employeeid']."' AND calendar_id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultcheck = DB_query($sqlcheck,$ErrMsg);
		
			
			$tuesnormalamount = 0;
			$x = 0;
			$k =$tuesnormal;

			
			while (($myrowcheck = DB_fetch_array($resultcheck))) {	
		  
				//get rates
				
				//get client details and rostering locations
			$sql5 =  "SELECT department.departmentname,guardforceclient.patrollocation,guardforceclient.base_wage,guardforceclient.premium_rate from guardforceclient 
			
			INNER JOIN department on 
			guardforceclient.department = department.departmentid
		   
			where clientid = '" . $myrowcheck['patrol_location']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql5,$ErrMsg);
			$myrow5 = DB_fetch_array($result5);
	
			$baseus_rate= $myrow5['base_wage'];
				
				$premium_rate= $myrow5['premium_rate'];
				
			if ($myrowcheck['tues_total']>0) {	
		       if ($myrowcheck['patrol_location'] >= 30 AND $myrowcheck['patrol_location'] <=54){
				
				$baserate = $baseus_rate; 
																	
			  }
				
				$yy= $k - $x;
			
				if (($yy >= $myrowcheck['tues_total']  ) ) {
						
					 
					
						  $normalamount = $myrowcheck['tues_total'] * ($baserate + $premium_rate);
				
						  $tuesnormalamount = $tuesnormalamount + $normalamount;
						if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['tues_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
					
						  $x = $x + $myrowcheck['tues_total'];
					 $description = $myrow5['patrollocation'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-Tues',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $x ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'" . $premiumhours."',
						'" . $premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
						 
						
			  	  }
					 
				 		if (($yy < $myrowcheck['tues_total']  ) ) {
						
					 
					
						  $normalamount = $yy * ($baserate + $premium_rate);
				
						  $tuesnormalamount = $tuesnormalamount+ $normalamount;
				
					// $x = $x + $myrowcheck['mon_total'];
							if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['tues_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
						$description = $myrow5['patrollocation'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-Tues',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $yy ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'" . $premiumhours."',
						'" . $premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);  
			  	  } 
				
			
			}
				
				
				
		
	       }
		//	$totalnormalamount = $totalnormalhours * $baserate ;
			
			$tuestimeandhalfamount =$tuestimeandhalfhours * (1.5 * $baserate);
			
			$tuesdoubleamount = $tuesdouble * (2 * $baserate);
			
			
			if ($tuestimeandhalfamount > 0){
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-Mon',
						'time and half-Mon',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $tuestimeandhalfhours."',
						'" . (1.5 * $baserate)."',
						'0',
						'" . ($tuestimeandhalfhours *(1.5 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			
			}
			if ($tuesdoubleamount > 0){
			
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-Mon',
						'Double-Mon',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $tuesdouble."',
						'" . (2* $baserate)."',
						'0',
						'" . ($tuesdouble *(2 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
			  
		
			
			$sqlcheck =  "SELECT timesheetsuvadetails.wed_total,timesheetsuvadetails.timesuvaid,timesheetsuva.timesuvaid,timesheetsuva.patrol_location,timesheetsuva.department
	                   
			
						 
			FROM timesheetsuvadetails INNER JOIN timesheetsuva on 
			timesheetsuvadetails.timesuvaid = timesheetsuva.timesuvaid
			
			
			where employeeid = '" . $myrow4['employeeid']."' AND calendar_id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultcheck = DB_query($sqlcheck,$ErrMsg);
		
			
			$wednormalamount = 0;
			$x = 0;
			$k =$wednormal;

			
			while (($myrowcheck = DB_fetch_array($resultcheck))) {	
		  
				//get rates
				
				//get client details and rostering locations
			$sql5 =  "SELECT department.departmentname,guardforceclient.patrollocation,guardforceclient.base_wage,guardforceclient.premium_rate from guardforceclient 
			
			INNER JOIN department on 
			guardforceclient.department = department.departmentid
		   
			where clientid = '" . $myrowcheck['patrol_location']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql5,$ErrMsg);
			$myrow5 = DB_fetch_array($result5);
	
			$baseus_rate= $myrow5['base_wage'];
				
				$premium_rate= $myrow5['premium_rate'];
				
			if ($myrowcheck['wed_total'] > 0){	
		       if ($myrowcheck['patrol_location'] >= 30 AND $myrowcheck['patrol_location'] <=54){
				
				$baserate = $baseus_rate; 
																	
			  }
				
				$yy= $k - $x;
			
				if (($yy >= $myrowcheck['wed_total']  ) ) {
						
					 
					
						  $normalamount = $myrowcheck['wed_total'] * ($baserate + $premium_rate);
				
						  $wednormalamount = $wednormalamount + $normalamount;
					if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['wed_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
						  $x = $x + $myrowcheck['wed_total'];
					 $description = $myrow5['patrollocation'] ;
					
					
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-Wed',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $x ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'" . $premiumhours."',
						'" . $premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
						 
						
			  	  }
					 
				 		if (($yy < $myrowcheck['wed_total']  ) ) {
						
					 
					
						  $normalamount = $yy * ($baserate + $premium_rate);
				
						  $wednormalamount = $wednormalamount+ $normalamount;
						if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['wed_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
					// $x = $x + $myrowcheck['mon_total'];
					 
						$description = $myrow5['patrollocation'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-Wed',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $yy ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'" . $premiumhours."',
						'" . $premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);  
			  	  } 
				
			
			}
				
				
				
		
	       }
		//	$totalnormalamount = $totalnormalhours * $baserate ;
			
			$wedtimeandhalfamount =$wedtimeandhalfhours * (1.5 * $baserate);
			
			$weddoubleamount = $weddouble * (2 * $baserate);
			
			
			if ($wedtimeandhalfamount > 0){
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-Wed',
						'Time and half-Wed',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $wedtimeandhalfhours."',
						'" . (1.5 * $baserate)."',
						'0',
						'" . ($wedtimeandhalfhours *(1.5 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			
			}
			if ($weddoubleamount > 0){
			
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-Wed',
						'Double-Wed',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $weddouble."',
						'" . (2* $baserate)."',
						'0',
						'" . ($weddouble *(2 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
			
			
			//thuirs
					
			$sqlcheck =  "SELECT timesheetsuvadetails.thurs_total,timesheetsuvadetails.timesuvaid,timesheetsuva.timesuvaid,timesheetsuva.patrol_location,timesheetsuva.department
	                   
			
						 
			FROM timesheetsuvadetails INNER JOIN timesheetsuva on 
			timesheetsuvadetails.timesuvaid = timesheetsuva.timesuvaid
			
			
			where employeeid = '" . $myrow4['employeeid']."' AND calendar_id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultcheck = DB_query($sqlcheck,$ErrMsg);
		
			
			$thursnormalamount = 0;
			$x = 0;
			$k =$thursnormal;

			
			while (($myrowcheck = DB_fetch_array($resultcheck))) {	
		  
				//get rates
				
				//get client details and rostering locations
			$sql5 =  "SELECT department.departmentname,guardforceclient.patrollocation,guardforceclient.base_wage,guardforceclient.premium_rate from guardforceclient 
			
			INNER JOIN department on 
			guardforceclient.department = department.departmentid
		   
			where clientid = '" . $myrowcheck['patrol_location']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql5,$ErrMsg);
			$myrow5 = DB_fetch_array($result5);
	
			$baseus_rate= $myrow5['base_wage'];
				
				$premium_rate= $myrow5['premium_rate'];
				
			if ($myrowcheck['thurs_total'] > 0){	
		       if ($myrowcheck['patrol_location'] >= 30 AND $myrowcheck['patrol_location'] <=54){
				
				$baserate = $baseus_rate; 
																	
			  }
				
				$yy= $k - $x;
			
				if (($yy >= $myrowcheck['thurs_total']  ) ) {
						
					 
					
						  $normalamount = $myrowcheck['thurs_total'] * ($baserate + $premium_rate);
				
						  $thursnormalamount = $thursnormalamount + $normalamount;
					if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['thurs_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
					
					
					
						  $x = $x + $myrowcheck['thurs_total'];
					 $description = $myrow5['patrollocation'] ;
					
					
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-thurs',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $x ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'".$premiumhours."',
						'".$premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
						 
						
			  	  }
					 
				 		if (($yy < $myrowcheck['thurs_total']  ) ) {
						
					 
					
						  $normalamount = $yy * ($baserate + $premium_rate);
				
						  $thursnormalamount = $thursnormalamount+ $normalamount;
					if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['thurs_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
					// $x = $x + $myrowcheck['mon_total'];
					 
						$description = $myrow5['patrollocation'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-thurs',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $yy ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'".$premiumhours."',
						'".$premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);  
			  	  } 
				
			
				
			}
				
				
		
	       }
		//	$totalnormalamount = $totalnormalhours * $baserate ;
			
			$thurstimeandhalfamount =$thurstimeandhalfhours * (1.5 * $baserate);
			
			$thursdoubleamount = $thursdouble * (2 * $baserate);
			
			
			if ($thurstimeandhalfamount > 0){
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-thurs',
						'Time and half-thurs',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $thurstimeandhalfhours."',
						'" . (1.5 * $baserate)."',
						'0',
						'" . ($thurstimeandhalfhours *(1.5 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			
			}
			if ($thursdoubleamount > 0){
			
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-thurs',
						'Double-thurs',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $thursdouble."',
						'" . (2* $baserate)."',
						'0',
						'" . ($thursdouble *(2 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
			
			//fri
					
			$sqlcheck =  "SELECT timesheetsuvadetails.fri_total,timesheetsuvadetails.timesuvaid,timesheetsuva.timesuvaid,timesheetsuva.patrol_location,timesheetsuva.department
	                   
			
						 
			FROM timesheetsuvadetails INNER JOIN timesheetsuva on 
			timesheetsuvadetails.timesuvaid = timesheetsuva.timesuvaid
			
			
			where employeeid = '" . $myrow4['employeeid']."' AND calendar_id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultcheck = DB_query($sqlcheck,$ErrMsg);
		
			
			$frinormalamount = 0;
			$x = 0;
			$k =$frinormal;

			
			while (($myrowcheck = DB_fetch_array($resultcheck))) {	
		  
				//get rates
				
				//get client details and rostering locations
			$sql5 =  "SELECT department.departmentname,guardforceclient.patrollocation,guardforceclient.base_wage,guardforceclient.premium_rate from guardforceclient 
			
			INNER JOIN department on 
			guardforceclient.department = department.departmentid
		   
			where clientid = '" . $myrowcheck['patrol_location']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql5,$ErrMsg);
			$myrow5 = DB_fetch_array($result5);
	
			$baseus_rate= $myrow5['base_wage'];
				
				$premium_rate= $myrow5['premium_rate'];
				
			if ($myrowcheck['fri_total'] > 0){	
		       if ($myrowcheck['patrol_location'] >= 30 AND $myrowcheck['patrol_location'] <=54){
				
				$baserate = $baseus_rate; 
																	
			  }
				
				$yy= $k - $x;
			
				if (($yy >= $myrowcheck['fri_total']  ) ) {
						
					 
					
						  $normalamount = $myrowcheck['fri_total'] * ($baserate + $premium_rate);
				
						  $frinormalamount = $frinormalamount + $normalamount;
					if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['fri_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
						  $x = $x + $myrowcheck['fri_total'];
					 $description = $myrow5['patrollocation'] ;
					
					
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-fri',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $x ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'".$premiumhours."',
						'".$premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
						 
						
			  	  }
					 
				 		if (($yy < $myrowcheck['fri_total']  ) ) {
						
					 
					
						  $normalamount = $yy * ($baserate + $premium_rate);
				
						  $frinormalamount = $frinormalamount+ $normalamount;
					if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['fri_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
					// $x = $x + $myrowcheck['mon_total'];
					 
						$description = $myrow5['patrollocation'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-fri',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $yy ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'".$premiumhours."',
						'".$premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);  
			  	  } 
				
			
			}
				
				
				
		
	       }
		//	$totalnormalamount = $totalnormalhours * $baserate ;
			
			$fritimeandhalfamount =$fritimeandhalfhours * (1.5 * $baserate);
			
			$fridoubleamount = $fridouble * (2 * $baserate);
			
			
			if ($fritimeandhalfamount > 0){
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-fri',
						'Time and half-fri',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $fritimeandhalfhours."',
						'" . (1.5 * $baserate)."',
						'0',
						'" . ($fritimeandhalfhours *(1.5 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			
			}
			if ($fridoubleamount > 0){
			
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-fri',
						'Double-fri',
						'" . $myrowcheck['timesuvaid'] ."',
						'" . fridouble."',
						'" . (2* $baserate)."',
						'0',
						'" . ($fridouble *(1.5 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
			
			//sat
					
			$sqlcheck =  "SELECT timesheetsuvadetails.sat_total,timesheetsuvadetails.timesuvaid,timesheetsuva.timesuvaid,timesheetsuva.patrol_location,timesheetsuva.department
	                   
			
						 
			FROM timesheetsuvadetails INNER JOIN timesheetsuva on 
			timesheetsuvadetails.timesuvaid = timesheetsuva.timesuvaid
			
			
			where employeeid = '" . $myrow4['employeeid']."' AND calendar_id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultcheck = DB_query($sqlcheck,$ErrMsg);
		
			
			$satnormalamount = 0;
			$x = 0;
			$k =$satnormal;

			
			while (($myrowcheck = DB_fetch_array($resultcheck))) {	
		  
				//get rates
				
				//get client details and rostering locations
			$sql5 =  "SELECT department.departmentname,guardforceclient.patrollocation,guardforceclient.base_wage,guardforceclient.premium_rate from guardforceclient 
			
			INNER JOIN department on 
			guardforceclient.department = department.departmentid
		   
			where clientid = '" . $myrowcheck['patrol_location']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql5,$ErrMsg);
			$myrow5 = DB_fetch_array($result5);
	
			$baseus_rate= $myrow5['base_wage'];
				
				$premium_rate= $myrow5['premium_rate'];
				
			if ($myrowcheck['sat_total'] > 0){	
		       if ($myrowcheck['patrol_location'] >= 30 AND $myrowcheck['patrol_location'] <=54){
				
				$baserate = $baseus_rate; 
																	
			  }
				
				$yy= $k - $x;
			
				if (($yy >= $myrowcheck['sat_total']  ) ) {
						
					 
					
						  $normalamount = $myrowcheck['sat_total'] * ($baserate + $premium_rate);
				
						  $satnormalamount = $satnormalamount + $normalamount;
						if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['sat_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
						  $x = $x + $myrowcheck['sat_total'];
					 $description = $myrow5['patrollocation'] ;
					
					
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-sat',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $x ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'".$premiumhours."',
						'".$premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
						 
						
			  	  }
					 
				 		if (($yy < $myrowcheck['sat_total']  ) ) {
						
					 
					
						  $normalamount = $yy * ($baserate + $premium_rate);
				
						  $satnormalamount = $satnormalamount+ $normalamount;
						if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['sat_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
					// $x = $x + $myrowcheck['mon_total'];
					 
						$description = $myrow5['patrollocation'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-sat',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $yy ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'".$premiumhours."',
						'".$premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);  
			  	  } 
				
			
			}
				
				
				
		
	       }
		//	$totalnormalamount = $totalnormalhours * $baserate ;
			
			$sattimeandhalfamount =$sattimeandhalfhours * (1.5 * $baserate);
			
			$satdoubleamount = $satdouble * (2 * $baserate);
			
			
			if ($sattimeandhalfamount > 0){
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-sat',
						'Time and half-sat',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $sattimeandhalfhours."',
						'" . (1.5 * $baserate)."',
						'0',
						'" . ($sattimeandhalfhours *(1.5 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			
			}
			if ($satdoubleamount > 0){
			
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-sat',
						'Double-sat',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $satdouble."',
						'" . (2* $baserate)."',
						'0',
						'" . ($satdouble *(2 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
			
			
			//sun
					
			$sqlcheck =  "SELECT timesheetsuvadetails.sun_total,timesheetsuvadetails.timesuvaid,timesheetsuva.timesuvaid,timesheetsuva.patrol_location,timesheetsuva.department
	                   
			
						 
			FROM timesheetsuvadetails INNER JOIN timesheetsuva on 
			timesheetsuvadetails.timesuvaid = timesheetsuva.timesuvaid
			
			
			where employeeid = '" . $myrow4['employeeid']."' AND calendar_id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultcheck = DB_query($sqlcheck,$ErrMsg);
		
			
			$sunnormalamount = 0;
			$x = 0;
			$k =$sunnormal;

			
			while (($myrowcheck = DB_fetch_array($resultcheck))) {	
		  
				//get rates
				
				//get client details and rostering locations
			$sql5 =  "SELECT department.departmentname,guardforceclient.patrollocation,guardforceclient.base_wage,guardforceclient.premium_rate from guardforceclient 
			
			INNER JOIN department on 
			guardforceclient.department = department.departmentid
		   
			where clientid = '" . $myrowcheck['patrol_location']."'
			";
	        $ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql5,$ErrMsg);
			$myrow5 = DB_fetch_array($result5);
	
			$baseus_rate= $myrow5['base_wage'];
				
				$premium_rate= $myrow5['premium_rate'];
				
			if ($myrowcheck['sun_total'] > 0){	
		       if ($myrowcheck['patrol_location'] >= 30 AND $myrowcheck['patrol_location'] <=54){
				
				$baserate = $baseus_rate; 
																	
			  }
				
				$yy= $k - $x;
			
				if (($yy >= $myrowcheck['sun_total']  ) ) {
						
					 
					
						  $normalamount = $myrowcheck['sun_total'] * ($baserate + $premium_rate);
				
						  $thursnormalamount = $thursnormalamount + $normalamount;
						if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['sun_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
						  $x = $x + $myrowcheck['sun_total'];
					 $description = $myrow5['patrollocation'] ;
					
					
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-sun',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $x ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'".$premiumhours."',
						'".$premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
						 
						
			  	  }
					 
				 		if (($yy < $myrowcheck['sun_total']  ) ) {
						
					 
					
						  $normalamount = $yy * ($baserate + $premium_rate);
				
						  $sunnormalamount = $sunnormalamount+ $normalamount;
					if ($premium_rate > 0){
					
					$premiumhours = $myrowcheck['sun_total'];
							$premiumamount = $premiumhours * $premium_rate;
				}
					// $x = $x + $myrowcheck['mon_total'];
					 
						$description = $myrow5['patrollocation'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							premium_hours,
							premium_amount,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Normal Wage-sun',
						'" . $description."',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $yy ."',
						'" . $baserate."',
						'" . $premium_rate."',
						'".$premiumhours."',
						'".$premiumamount."',
						'" . $normalamount ."',
						 '".$myrowcheck['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);  
			  	  } 
				
			
				
			}
				
				
		
	       }
		//	$totalnormalamount = $totalnormalhours * $baserate ;
			
			$suntimeandhalfamount =$suntimeandhalfhours * (1.5 * $baserate);
			
			$sundoubleamount = $sundouble * (2 * $baserate);
			
			
			if ($suntimeandhalfamount > 0){
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-sun',
						'Time and half-sun',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $suntimeandhalfhours."',
						'" . (1.5 * $baserate)."',
						'0',
						'" . ($suntimeandhalfhours *(1.5 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			
			}
			if ($sundoubleamount > 0){
			
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							timesheet_id,
							hours,
							rate,
							premium_rate,
							amount,
							department)
				VALUES ('" . $lastid."',
						'Overtime-sun',
						'Double-sun',
						'" . $myrowcheck['timesuvaid'] ."',
					   '" . $sundouble."',
						'" . (2* $baserate)."',
						'0',
						'" . ($sundouble *(2 * $baserate)) ."',
						 '".$row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
			
			
			
		//get allowance
			//check in payrol_adjustments_details fnpf related
				$sql411 =  "SELECT payrol_adjustments_details.amount,payrol_adjustments_details.comments,payrol_adjustments_type_details.type_detail,
				payrol_adjustments_details.department
				   
				   
						 
			FROM payrol_adjustments_details  
			
			INNER JOIN payrol_adjustments_type_details on 
			payrol_adjustments_details.type_details = payrol_adjustments_type_details.id
			where payrol_adjustments_details.employeeid = '" . $myrow4['employeeid']."'
			AND payrol_adjustments_details.id = '" . $calendarid."'
			AND payrol_adjustments_details.type_details = 6
			OR  payrol_adjustments_details.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;
				
				
				$description = $myrowallowances['type_detail']. '- Comments-:' .$myrowallowances['comments'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Manual Allowances',
						'" . $description."',
							'" . $allowances."',
							'" . $myrowallowances['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				
				
			}
			
		
		//non fnpf allowances
			
			$sql4112 =  "SELECT  payrol_adjustments_details.amount,payrol_adjustments_details.comments,payrol_adjustments_type_details.type_detail,
			payrol_adjustments_details.department
				   
						 
			FROM payrol_adjustments_details   
			INNER JOIN payrol_adjustments_type_details on 
			payrol_adjustments_details.type_details = payrol_adjustments_type_details.id
			 where payrol_adjustments_details.employeeid = '" . $myrow4['employeeid']."'
			AND payrol_adjustments_details.id = '" . $calendarid."'
			AND payrol_adjustments_details.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  ;
				
				
					$description = $myrowallowances['type_detail']. '- Comments-:' .$myrowallowances['comments'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Allowances-Non FNPF',
						'" . $description."',
							'" . $allowances."',
							'" . $nonfnpfmyrow['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				
				
				
			}
				
			
			$sql4119 =  "SELECT type,rate, hours,gross,dates,comments,department
				   
						 
			FROM absent  where employeeid = '" . $myrow4['employeeid']."'
			and id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			
			$result4119 = DB_query($sql4119,$ErrMsg);
		
			$leaveallowances= 0;
			while ($m = DB_fetch_array($result4119)){
				
				$Lallowances= $m['gross'];
				
				$leaveallowances = $leaveallowances + $Lallowances;
				
					$sickdata = 'Dates:-'.$m['dates'].'-Comments:'.$m['comments'];
				
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							hours,
							rate,
						
							amount,department)
				VALUES ('" . $lastid."',
						'" . $m['type']."',
						'" . $sickdata."',
						'" . $m['hours']."',
						'" .$m['rate'] ."',
						'" . $m['gross']."',
						'" . $m['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				if ($m['type'] == 'Sick Leave'){
					
					$sickleaveleft = $myrow41['sick_leave_available'] - $m['hours'];
					$ytdsick = $myrow41['ytd_sick_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE emp		$manualallowances= 0;
			while ($myrowallowances = DB_fetch_array($result411)){
				
				$allowances= $myrowallowances['amount'];
				
				$manualallowances = $manualallowances + $allowances;
				
				
				$description = $myrowallowances['type_detail']. '- Comments-:' .$myrowallowances['comments'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Manual Allowances',
						'" . $description."',
							'" . $allowances."',
							'" . $myrowallowances['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				
				
			}
			
		
		//non fnpf allowances
			
			$sql4112 =  "SELECT  payrol_adjustments_details.amount,payrol_adjustments_details.comments,payrol_adjustments_type_details.type_detail,
			payrol_adjustments_details.department
				   
						 
			FROM payrol_adjustments_details   
			INNER JOIN payrol_adjustments_type_details on 
			payrol_adjustments_details.type_details = payrol_adjustments_type_details.id
			 where payrol_adjustments_details.employeeid = '" . $myrow4['employeeid']."'
			AND payrol_adjustments_details.id = '" . $calendarid."'
			AND payrol_adjustments_details.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  ;
				
				
					$description = $myrowallowances['type_detail']. '- Comments-:' .$myrowallowances['comments'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Allowances-Non FNPF',
						'" . $description."',
							'" . $allowances."',
							'" . $nonfnpfmyrow['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				
				
				
			}
				
			
			$sql4119 =  "SELECT type,rate, hours,gross,dates,comments,department
				   
						 
			FROM absent  where employeeid = '" . $myrow4['employeeid']."'
			and id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			
			$result4119 = DB_query($sql4119,$ErrMsg);
		
			$leaveallowances= 0;
			while ($m = DB_fetch_array($result4119)){
				
				$Lallowances= $m['gross'];
				
				$leaveallowances = $leaveallowances + $Lallowances;
				
					$sickdata = 'Dates:-'.$m['dates'].'-Comments:'.$m['comments'];
				
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							hours,
							rate,
						
							amount,department)
				VALUES ('" . $lastid."',
						'" . $m['type']."',
						'" . $sickdata."',
						'" . $m['hours']."',
						'" .$m['rate'] ."',
						'" . $m['gross']."',
						'" . $m['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				if ($m['type'] == 'Sick Leave'){
					
					$sickleaveleft = $myrow41['sick_leave_available'] - $m['hours'];
					$ytdsick = $myrow41['ytd_sick_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET sick_leave_available='".$sickleaveleft."',
						 									 ytd_sick_leave='".$ytdsick."'
						WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
					if ($m['type'] == 'Annual Leave'){
					
					$aleaveleft = $myrow41['annual_leave_available'] - $m['hours'];
					$ytda = $myrow41['ytd_annual_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET annual_leave_available='".$aleaveleft."',
						 									 ytd_annual_leave='".$ytda."'
						WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
	
				if ($m['type'] == 'Bereavement Leave'){
					
					$bleaveleft = $myrow41['bereavement_leave_available'] - $m['hours'];
					$ytdb= $myrow41['ytd_bereavement_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET bereavement_leave_available='".$bleaveleft."',
						 									 ytd_bereavement_leave='".$ytdb."'
						WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
				
				if ($m['type'] == 'Family Care Leave'){
					
					$fleaveleft = $myrow41['familycare_leave_available'] - $m['hours'];
					$ytdf = $myrow41['ytd_familycare_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET familycare_leave_available='".$fleaveleft."',
						 									 ytd_familycare_leave='".$ytdf."'
						WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
				
				
					if ($m['type'] == 'Paternity Leave'){
					
					$paternityleaveleft = $myrow41['paternity_leave_available'] - $m['hours'];
					$ytdpaternity = $myrow41['ytd_paternity_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET paternity_available='".$paternityleaveleft."',
						 									 ytd_paternity_leave='".$ytdpaternity."'
						WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
				
					if ($m['type'] == 'Maternity Leave'){
					
					$maternityleaveleft = $myrow41['maternity_leave_available'] - $m['hours'];
					$ytdmaternity = $myrow41['ytd_maternity_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET maternity_available='".$maternityleaveleft."',
						 									 ytd_maternity_leave='".$ytdmaternity."'
						WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
				
				
			}
			
				
			//meal allowance
				if ($monmealallowance > 0) {
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Meal Allowance',
						'Monday',
							'" . ($monmealallowance * 6)."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
				if ($tuesmealallowance > 0) {
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Meal Allowance',
						'Tuesday',
							'" . ($tuesmealallowance * 6)."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
				if ($wedmealallowance > 0) {
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Meal Allowance',
						'Wednesday',
							'" . ($wedmealallowance * 6)."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
				if ($thursmealallowance > 0) {
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Meal Allowance',
						'Thursday',
							'" . ($thursmealallowance * 6)."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
				if ($frimealallowance > 0) {
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Meal Allowance',
						'Friday',
							'" . ($frimealallowance * 6)."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
				if ($satmealallowance > 0) {
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Meal Allowance',
						'Saturday',
							'" . ($satmealallowance * 6)."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
				if ($sunmealallowance > 0) {
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Meal Allowance',
						'Sunday',
							'" . ($sunmealallowance * 6)."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
			
			
			//get manual deductions
				$sql42 =  "SELECT  payrol_adjustments_details.amount,payrol_adjustments_details.comments,payrol_adjustments_type_details.type_detail,
				payrol_adjustments_details.department
				   
						 
			FROM payrol_adjustments_details   
			INNER JOIN payrol_adjustments_type_details on 
			payrol_adjustments_details.type_details = payrol_adjustments_type_details.id
			
			where payrol_adjustments_details.employeeid = '" . $myrow4['employeeid']."'
			AND payrol_adjustments_details.id = '" . $calendarid."'
			AND payrol_adjustments_details.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;
				
				
					$description = $myrowallowances['type_detail']. '- Comments-:' .$myrowallowances['comments'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department])
				VALUES ('" . $lastid."',
						'Manual Deductions',
						'" . $description."',
							'" . -$deductions."','" . $myrowdeductions['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				
			}
			//get fixed deductions
				$sql43 =  "SELECT payrol_adjustments_type_details.type_detail,  fixeddeductions.name,fixeddeductions.deduction_amount, fixeddeductions.total_amount_to_deduct,fixeddeductions.total_paid_till_date,fixeddeductions.department
				            
				   						 
			FROM fixeddeductions
			INNER JOIN payrol_adjustments_type_details on 
			fixeddeductions.type = payrol_adjustments_type_details.id
			where fixeddeductions.employeeid = '" . $myrow4['employeeid']."'
			AND fixeddeductions.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;
				
				
				$description = $myrowfixeddeductions['name'].'- Balance-:' .$balanceleft.'- Paid till date-:' .$myrowfixeddeductions['total_paid_till_date'] ;
					$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							amount,department)
				VALUES ('" . $lastid."',
						'Fixed Deductions',
						'" . $description."',
							'" . -$fdeductions."',
							'" . $myrowfixeddeductions['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				
				
			}
			
			//
			
			
			//fnpf
			
			
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'FNPF',
						'employee Contribution',
							'" . -$fnpfemployee_amount."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
			//tax
			if ($thisincometaxtopay > 0) {
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Tax',
						'Income tax',
							'" . -$thisincometaxtopay."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
			if ($thispaysrt > 0) {
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Tax',
						'SRT Tax',
							'" . -$thispaysrt."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
             
			if ($thispayecal > 0) {
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Tax',
						'ECAL Tax',
							'" . -$thispayecal."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
			}
	//insert in detail pay table
			
			//get sum of all premium total and save in parent table-payrun_details
			
						//echo ' <div class="row"> ';
$sql4 =  "SELECT 
				   SUM(payrun_details_extra.premium_amount) as premium,
				    SUM(payrun_details_extra.premium_hours) as premiumhours
				
						 
			FROM payrun_details_extra WHERE payrun_details_extra.payid = '" . $lastid."' 
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql4,$ErrMsg);
	$myrow41 = DB_fetch_array($result5);
			
			$sql4111 =  "SELECT 
				   normal_pay
				
						 
			FROM payrun_details WHERE payid = '" . $lastid."' 
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result5 = DB_query($sql4111,$ErrMsg);
	$myrow4111 = DB_fetch_array($result5);
			$normalwithoutpremium= $myrow4111['normal_pay'] - $myrow41['premium'];
			//now update parent table
			
		$sql4 =  "UPDATE payrun_details SET premium='" . $myrow41['premium'] . "',premium_hours='" . $myrow41['premiumhours'] . "',normal_pay_without_premium='" . $normalwithoutpremium . "'  WHERE payid ='" . $lastid . "'";
				
			
	$ErrMsg = _('This customer could not be added because');
			$result51 = DB_query($sql4,$ErrMsg);	
			
			//$message = $myrow05['patrol_location'];
			
		}//end of pay loop end of all pay for each person who was in a timesheet

//now find staffs who were not in timesheet but got sick whole week





$sql55 =  "SELECT employeeid,type,rate, hours,gross
				   
						 
			FROM absent  where id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			
			$result55= DB_query($sql55,$ErrMsg);
		
			$leaveallowances= 0;
			while ($m = DB_fetch_array($resul55)){
				
				
				
$sql56 =  "SELECT employeeid
				   
						 
			FROM payrun_details where  employeeid = '" . $m['employeeid']."'
			
			";
	
			
			$result56= DB_query($sql56,$ErrMsg);
		
	

				
		
			if (DB_num_rows($result56) !== '1'){
				
					
				
					
			$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,sick_leave_available,annual_leave_available,bereavement_leave_available,familycare_leave_available,paternity_leave_available,maternity_leave_available,ytd_sick_leave,ytd_annual_leave,ytd_bereavement_leave,ytd_familycare_leave,ytd_paternity_leave,ytd_maternity_leave,total_bonuspaid_to_date,
			total_paid_till_date,fnpf_deducted_till_date,fnpf_employers_contribution_till_date
				
						 
			FROM employeemaster   where employeeid = '" . $m['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'];
			$bonuspaidtilldate = $myrow41['total_bonuspaid_to_date'];
				$ytdpay = $myrow41['total_paid_till_date'];
			$ytdemployeecontribution = $myrow41['fnpf_deducted_till_date'];
			$ytdemployercontribution = $myrow41['fnpf_employers_contribution_till_date'];
				
				
				
				
		//get allowance
			//check in payrol_adjustments_details fnpf related
				$sql411 =  "SELECT amount
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $m['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;
			}
			
		
		
				
			
			$thisbonusamount = $m['gross']+$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 total_amount_to_deduct,deduction_amount,total_paid_till_date
				   
						 
			FROM fixeddeductions   where employeeid = '" .$m['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;
				
				
			$totalpaidtilldate = $myrowfixeddeductions['total_paid_till_date']+$myrowfixeddeductions['deduction_amount'];
				
				$balanceleft = $myrowfixeddeductions['total_amount_to_deduct'] - $totalpaidtilldate;
			
            $sql399 = "UPDATE fixeddeductions SET total_paid_till_date= '".$totalpaidtilldate."', 
												  balance_left= '".$balanceleft."'
						WHERE employeeid= '" . $m['employeeid'] . "'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result399 = DB_query($sql399,$ErrMsg);

			
			
				
				
				
			}
			
			
			
			
			
			//now lets calculate tax 
			$totalgrosspay =$thisbonusamount;
			
			    $grossamount50 = $totalgrosspay * 0.5;
			
			//non fnpf allowances
			
			$sql4112 =  "SELECT amount
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $m['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;
			
			$bonuspaidtilldate = $bonuspaidtilldate + $thisbonusamount;
			
			$annualincomebonus = $bonuspaidtilldate;
			
			if ($annualincomebonus >= 0 AND $annualincomebonus <= 30000){
				
					$thispayperiod = $completed_pay_period + 1;
				$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
			
				
			
			$userid = $_SESSION['UserID'];
			
			$timenow = date('Y-m-d H:i:s');

		
	
	
	//insert the database
	
	$sql = "INSERT INTO payrun_details (
							id,
							date_processed,
							employee_type,
							employeeid,
							normal_pay,
							
							timehalf_amount,
							double_amount,
							leave_allowances,
							other_allowances_fnpf,
							other_allowances_nonfnpf,
							meal_allowance,
							total_bonus,
							totalgross_amount,
							pay_cycle,
							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',
						'" . $m['employeeid']."',
						
						'0',
						'0',
						'0',
						'" . $leaveallowances. "',
						'" . $manualallowances . "',
						'" . $nonfnpfmanualallowances . "',
						'" . $mealtotalvalue . "',
						'" . $thisbonusamount . "',
						'" . ($totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances ). "',
						'52',
						'" . $thispayperiod . "',
						'0',
						'" . $annualincomebonus . "',
						'" . $annualincometax . "',
						'" . $annualsrt . "',
						'" . $annualecal . "',
						'" . ($annualincometax + $annualsrt +$annualecal). "',
						'" . $thisincometaxtopay. "',
						'" . $thispaysrt . "',
						'" . $thispayecal . "',
						'" . ($thisincometaxtopay +$thispaysrt+$thispayecal)  . "',
						'" . $fnpfemployee_amount. "',
						'" . $fnpfemployer_amount . "',
						'" . $fixeddeductions . "',
						'" . $manualdeductions. "',
						'" . ($totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances - $thisincometaxtopay - $thispaysrt -$thispayecal - $fnpfemployee_amount-$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);
			
			$netpay = $totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances - $thisincometaxtopay - $thispaysrt -$thispayecal - $fnpfemployee_amount-$fixeddeductions-$manualdeductions;
			
			$ytdpay = $ytdpay + $netpay;
			$ytd_income_tax_withheld = $ytd_income_tax_withheld + $thisincometaxtopay;
			$ytd_srt_tax_withheld = $ytd_srt_tax_withheld +$thispaysrt;
			$ytd_ecal_tax_withheld = $ytd_ecal_tax_withheld + $thispayecal;
			$ytdemployeecontribution = $ytdemployeecontribution + $fnpfemployee_amount;
			$ytdemployercontribution = $ytdemployercontribution + $fnpfemployer_amount;
	
			$sql39 = "UPDATE employeemaster SET completed_pay_period= '".$thispayperiod."',
												total_bonuspaid_to_date= '".$bonuspaidtilldate."',
												total_paid_till_date = '".$ytdpay."',
												ytd_income_tax_withheld = '".$ytd_income_tax_withheld."',
												ytd_srt_tax_withheld = '".$ytd_srt_tax_withheld."',
												ytd_ecal_tax_withheld = '".$ytd_ecal_tax_withheld."',
												fnpf_deducted_till_date = '".$ytdemployeecontribution."',
												fnpf_employers_contribution_till_date = '".$ytdemployercontribution."',
												fnpf_total_till_date = '".($ytdemployercontribution + $ytdemployeecontribution)."'
												
												   
						WHERE employeeid= '" . $m['employeeid'] . "'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result39 = DB_query($sql39,$ErrMsg);
				
				
				
				
				//now second table for sick leave
				
				
				
				
				
				
				
				
				
				//get the payid from above
				$sqlpayid =  "SELECT payid
			
						 
			FROM payrun_details  ORDER BY payid DESC LIMIT 1;
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultpayid = DB_query($sqlpayid,$ErrMsg);
			$myrowpayid = DB_fetch_array($resultpayid);
			
			$lastid1 = $myrowpayid['payid'];

	//get second table output done
		//first check if the location they worked in a day where there was any premium rate. open location they worked and hours if the premium rate is 
	
			
		//get allowance
			//check in payrol_adjustments_details fnpf related
				$sql411 =  "SELECT payrol_adjustments_details.amount,payrol_adjustments_details.comments,payrol_adjustments_details.department,payrol_adjustments_type_details.type_detail
				   
				   
						 
			FROM payrol_adjustments_details  
			
			INNER JOIN payrol_adjustments_type_details on 
			payrol_adjustments_details.type_details = payrol_adjustments_type_details.id
			where payrol_adjustments_details.employeeid = '" . $m['employeeid']."'
			AND payrol_adjustments_details.id = '" . $calendarid."'
			AND payrol_adjustments_details.type_details = 6
			OR  payrol_adjustments_details.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;
				
				
				$description = $myrowallowances['type_detail']. '- Comments-:' .$myrowallowances['comments'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							amount,
							department)
				VALUES ('" . $lastid1."',
						'Manual Allowances',
						'" . $description."',
							'" . $allowances."',
							'" . $myrowallowances['department']."',
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				
				
			}
			
		
		
			
			$sql4119 =  "SELECT type,rate, hours,gross,dates,comments department
				   
						 
			FROM absent  where employeeid = '" .$m['employeeid']."'
			and id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			
			$result4119 = DB_query($sql4119,$ErrMsg);
		
			$leaveallowances= 0;
			while ($m = DB_fetch_array($result4119)){
				
				$Lallowances= $m['gross'];
				
				$leaveallowances = $leaveallowances + $Lallowances;
				
					$sickdata = 'Dates:-'.$m['dates'].'-Comments:'.$m['comments'];
				
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							hours,
							rate,						
							amount,
							department)
				VALUES ('" . $lastid1."',
						'" . $m['type']."',
						'" . $sickdata."',
						'" . $m['hours']."',
						'" .$m['rate'] ."',
						'" . $m['gross']."',
						'" . $m['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				if ($m['type'] == 'Sick Leave'){
					
					$sickleaveleft = $myrow41['sick_leave_available'] - $m['hours'];
					$ytdsick = $myrow41['ytd_sick_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET sick_leave_available='".$sickleaveleft."',
						 									 ytd_sick_leave='".$ytdsick."'
						WHERE employeeid= '" . $m['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
					if ($m['type'] == 'Annual Leave'){
					
					$aleaveleft = $myrow41['annual_leave_available'] - $m['hours'];
					$ytda = $myrow41['ytd_annual_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET annual_leave_available='".$aleaveleft."',
						 									 ytd_annual_leave='".$ytda."'
						WHERE employeeid= '" . $m['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
	
				if ($m['type'] == 'Bereavement Leave'){
					
					$bleaveleft = $myrow41['bereavement_leave_available'] - $m['hours'];
					$ytdb= $myrow41['ytd_bereavement_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET bereavement_leave_available='".$bleaveleft."',
						 									 ytd_bereavement_leave='".$ytdb."'
						WHERE employeeid= '" . $m['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
				
				if ($m['type'] == 'Family Care Leave'){
					
					$fleaveleft = $myrow41['familycare_leave_available'] - $m['hours'];
					$ytdf = $myrow41['ytd_familycare_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET familycare_leave_available='".$fleaveleft."',
						 									 ytd_familycare_leave='".$ytdf."'
						WHERE employeeid= '" . $m['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
				
				
					if ($m['type'] == 'Paternity Leave'){
					
					$paternityleaveleft = $myrow41['paternity_leave_available'] - $m['hours'];
					$ytdpaternity = $myrow41['ytd_paternity_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET paternity_available='".$paternityleaveleft."',
						 									 ytd_paternity_leave='".$ytdpaternity."'
						WHERE employeeid= '" . $m['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
				
					if ($m['type'] == 'Maternity Leave'){
					
					$maternityleaveleft = $myrow41['maternity_leave_available'] - $m['hours'];
					$ytdmaternity = $myrow41['ytd_maternity_leave'] + $m['hours'];
					
					
						 $sql38 = "UPDATE employeemaster SET maternity_available='".$maternityleaveleft."',
						 									 ytd_maternity_leave='".$ytdmaternity."'
						WHERE employeeid= '" . $m['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);
					
				}
				
				
			}
				
			
		
			//get manual deductions
				$sql42 =  "SELECT  payrol_adjustments_details.amount,payrol_adjustments_details.comments,payrol_adjustments_details.department,payrol_adjustments_type_details.type_detail
				   
						 
			FROM payrol_adjustments_details   
			INNER JOIN payrol_adjustments_type_details on 
			payrol_adjustments_details.type_details = payrol_adjustments_type_details.id
			
			where payrol_adjustments_details.employeeid = '" . $m['employeeid']."'
			AND payrol_adjustments_details.id = '" . $calendarid."'
			AND payrol_adjustments_details.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;
				
				
					$description = $myrowallowances['type_detail']. '- Comments-:' .$myrowallowances['comments'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,
							department)
				VALUES ('" . $lastid."',
						'Manual Deductions',
						'" . $description."',
							'" . -$deductions."',
							'" . $myrowdeductions['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				
			}
			//get fixed deductions
				$sql43 =  "SELECT payrol_adjustments_type_details.type_detail,  fixeddeductions.name,fixeddeductions.deduction_amount, fixeddeductions.total_amount_to_deduct,fixeddeductions.total_paid_till_date,fixeddeductions.department
				            
				   						 
			FROM fixeddeductions
			INNER JOIN payrol_adjustments_type_details on 
			fixeddeductions.type = payrol_adjustments_type_details.id
			where fixeddeductions.employeeid = '" . $m['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;
				
				
				$description = $myrowfixeddeductions['name'].'- Paid till date-:' .$myrowfixeddeductions['total_paid_till_date'].'- Balance-:' .$balanceleft;
					$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,
							department)
				VALUES ('" . $lastid."',
						'Fixed Deductions',
						'" . $description."',
							'" . -$fdeductions."',
							'" . $myrowfixeddeductions['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				
				
			}
			
			
			//non fnpf allowances
			
			$sql4112 =  "SELECT  payrol_adjustments_details.amount,payrol_adjustments_details.comments,payrol_adjustments_type_details.type_detail,
			payrol_adjustments_details.department
				   
						 
			FROM payrol_adjustments_details   
			INNER JOIN payrol_adjustments_type_details on 
			payrol_adjustments_details.type_details = payrol_adjustments_type_details.id
			 where payrol_adjustments_details.employeeid = '" . $m['employeeid']."'
			AND payrol_adjustments_details.id = '" . $calendarid."'
			AND payrol_adjustments_details.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  ;
				
				
					$description = $myrowallowances['type_detail']. '- Comments-:' .$myrowallowances['comments'] ;
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,
							department)
				VALUES ('" . $lastid."',
						'Allowances-Non FNPF',
						'" . $description."',
							'" . $allowances."'
							'" . $nonfnpfmyrow['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
				
				
				
			}
				
				//fnpf
			//get defailt department
			$sql41 =  "SELECT department
				
						 
			FROM employeemaster   where employeeid = '" . $myrow4['employeeid']."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result41 = DB_query($sql41,$ErrMsg);
			$row = DB_fetch_array($result41);
			
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'FNPF',
						'employee Contribution',
							'" . -$fnpfemployee_amount."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
				
			//tax
			if ($thisincometaxtopay > 0) {
			$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Tax',
						'Income tax',
							'" . -$thisincometaxtopay."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
			
			if ($thispaysrt > 0) {
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Tax',
						'SRT Tax',
							'" . -$thispaysrt."','" . $row['department']."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			}
             
			if ($thispayecal > 0) {
				$sql = "INSERT INTO payrun_details_extra (
							payid,
							type,
							description,
							
							amount,department)
				VALUES ('" . $lastid."',
						'Tax',
						'ECAL Tax',
							'" . -$thispayecal."','" . $row['department']."'
						)";

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

			}


//change status to wrap everything
				
	 $sql36 = "UPDATE timesheetsuvadetails SET locked='1'
						WHERE calendar_id= '" . $calendarid . "'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result36 = DB_query($sql36,$ErrMsg);

 $sql37 = "UPDATE timesheetsuva SET status='locked', locked= '1'
						WHERE id= '" . $calendarid . "'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result37 = DB_query($sql37,$ErrMsg);

 $sql38 = "UPDATE calendar_management SET status='locked'
						WHERE id= '" . $calendarid . "'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38 = DB_query($sql38,$ErrMsg);




//process salary type is fixed


//this is for case for none timesheet staffs

	//success message and redirect to a page

$message = "SUCCESS! This pay period has been processed. Please check the entries.";
	echo "<script type='text/javascript'>alert('$message');</script>";
echo "<script type='text/javascript'>javascript:history.go(-1)</script>"; 
	exit();
}
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;
}
	
?>
