  <?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'];

$sqlcheck = "Select * from payrun_details where id ='" . $calendarid."'";
	
$resultcheck = DB_query($sqlcheck);
	$myrowcheck = DB_fetch_array($resultcheck);
	
	

	


$sql4 =  "SELECT 
							  SUM(th) AS totalhours,
							 SUM(nt) AS totalnormalhours,
							  SUM(timehalf) AS totaltimeandhalfhours,
							   SUM(dt) AS totaldoublehours,
							   SUM(public_holiday_hours) AS publicholidayhours,
				                SUM(meal) AS totalmeal,
								 SUM(stay_away) AS totalstayaway,
								  SUM(premium) AS totalpremium,
								   SUM(leave_hours) AS totalleavehours,
								   SUM(IF(leave_type = 'Sick Leave', leave_days, 0)) AS sickleavedays,
								  SUM(IF(leave_type = 'Annual Leave', leave_days, 0)) AS annualleavedays,
								  SUM(IF(leave_type = 'Bereavement Leave', leave_days, 0)) AS bleavedays,
								  SUM(IF(leave_type = 'Family Care Leave', leave_days, 0)) AS fleavedays,
								  SUM(IF(leave_type = 'Paternity Leave', leave_days, 0)) AS pleavedays,
								   SUM(IF(leave_type = 'Maternity Leave', leave_days, 0)) AS mleavedays,
								  SUM(IF(leave_type = 'Sick Leave', leave_hours, 0)) AS sickleavehours,
								  SUM(IF(leave_type = 'Annual Leave', leave_hours, 0)) AS annualleavehours,
								  SUM(IF(leave_type = 'Bereavement Leave', leave_hours, 0)) AS bleavehours,
								  SUM(IF(leave_type = 'Family Care Leave', leave_hours, 0)) AS fleavehours,
								  SUM(IF(leave_type = 'Paternity Leave', leave_hours, 0)) AS pleavehours,
								   SUM(IF(leave_type = 'Maternity Leave', leave_hours, 0)) AS mleavehours,
								
								  employeeid,
								  department,
								  remarks
								  
						 
			FROM manual_timesheet  
			where id = '" . $calendarid."'
			AND employeeid >= '13827'
		    Group by employeeid
			Order by employeeid 
			";
	$ErrMsg = _('This customer could not be added    because');
			$result4 = DB_query($sql4,$ErrMsg);
		
		$leaveallowances= 0;
			$sickleave =0;
			$annualleave =0;
			$blleave =0;
			$maternityleave= 0;
			$paternityleave =0;
			$familycareleave =0;
			
		while ($myrow4 = DB_fetch_array($result4)) {
		
		
			$totalhours =  $myrow4['totalhours'];
			
			$totalnormalhours = $myrow4['totalnormalhours'];
	
	
	
	
	$totaltimeandhalf = $myrow4['totaltimeandhalfhours'];
	
	$totaldouble = $myrow4['totaldoublehours'];
	
			$meal = $myrow4['totalmeal'];
			$premiumamount1= $myrow4['totalpremium'];
			$totalstayaway = $myrow4['totalstayaway'];
			//for each employee do payrol calculations below and save in payrun_details table. insert for new records on same calender id and update for updating or recalculating tallies
		
			
		//get all premium and add to above premium
			
		
			//get the rates. usa embassy get rates from client table
			
			
			$sql41 =  "SELECT fnpfmember,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, ytd_gross,
			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);
			$fnpfmember = $myrow41['fnpfmember'];
				$salarytype = $myrow41['salary_type'];
				$paycycle = $myrow41['paycyle'];
				$completed_pay_period = $myrow41['completed_pay_period'];
			
			//if ($myrow4['special_wage_rate'] > 0 ){
				
				
				//$baserate = $myrow4['special_wage_rate'];
				
			//}else{
				
			$baserate = $myrow41['base_wage_rate'];	
			//}
			
			//$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 
			
			
			
		  $totalnormalamount = $totalnormalhours * $baserate;
			
			$totaltimeandhalfamount =$totaltimeandhalf * (1.5 * $baserate);
			
			$totaldoubleamount = $totaldouble * (2 * $baserate);
			
			$totalpublicholidayamount = $myrow4['publicholidayhours'] * $baserate ;
			
			$thisbonusamount = $totaltimeandhalfamount + $totaldoubleamount ;
			
			
			
				
				$Lallowances= $myrow4['totalleavehours'] * $baserate;
				
				$leaveallowances =$Lallowances;
			
		//	if ($myrow4['leave_type'] == 'Sick Leave'){
					
					$sickleaveleft = $myrow41['sick_leave_available'] - $myrow4['sickleavedays'];
					$ytdsick = $myrow41['ytd_sick_leave'] + $myrow4['sickleavedays'];
					
					$thissickleave = $myrow4['sickleavehours'] * $baserate;
					$sickleave =  $thissickleave;
					
					
					 $sql38s = "UPDATE employeemaster SET sick_leave_available='".$sickleaveleft."',
						 									 ytd_sick_leave='".$ytdsick."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38s = DB_query($sql38s,$ErrMsg);
					
				
		//if ($myrow4['leave_type'] == 'Annual Leave'){
					
					$aleaveleft = $myrow41['annual_leave_available'] - $myrow4['annualleavedays'];
					$ytda = $myrow41['ytd_annual_leave'] + $myrow4['annualleavedays'];
					
						$thisannualleave = $myrow4['annualleavehours'] * $baserate;
					$annualleave =$thisannualleave ;
						
						 $sql38a = "UPDATE employeemaster SET annual_leave_available='".$aleaveleft."',
						 								 ytd_annual_leave='".$ytda."'
						WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

		$ErrMsg = _('The customer could not be updated because');
			$result38a = DB_query($sql38a,$ErrMsg);
					
				
	
				//if ($myrow4['leave_type'] == 'Bereavement Leave'){
					
					$bleaveleft = $myrow41['bereavement_leave_available'] - $myrow4['bleavedays'];
					$ytdb= $myrow41['ytd_bereavement_leave'] + $myrow4['bleavedays'];
					
					
						 $thisblleave = $myrow4['bleavehours'] * $baserate;
					$blleave = $thisblleave ;
					
					 $sql38b = "UPDATE employeemaster SET bereavement_leave_available='".$bleaveleft."',
						 									 ytd_bereavement_leave='".$ytdb."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

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

				//if ($myrow4['leave_type'] == 'Family Care Leave'){
					
					$fleaveleft = $myrow41['familycare_leave_available'] - $myrow4['fleavedays'];
					$ytdf = $myrow41['ytd_familycare_leave'] + $myrow4['fleavedays'];
					
					
					 $thisfleave = $myrow4['fleavehours'] * $baserate;
					$familycareleave = $thisfleave ;
					$sql38fam = "UPDATE employeemaster SET familycare_leave_available='".$fleaveleft."',
						 									 ytd_familycare_leave='".$ytdf."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38f = DB_query($sql38fam,$ErrMsg);
					
					
				
				
				
				//	if ($myrow4['leave_type'] == 'Paternity Leave'){
					
					$paternityleaveleft = $myrow41['paternity_leave_available'] - $myrow4['pleavedays'];
					$ytdpaternity = $myrow41['ytd_paternity_leave'] + $myrow4['pleavedays'];
					
					
						 $thispleave =$myrow4['pleavehours'] * $baserate;
					$paternityleave = $thispleave ;
						
						$sql38p = "UPDATE employeemaster SET paternity_leave_available='".$paternityleaveleft."',
						 									 ytd_paternity_leave='".$ytdpaternity."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38p = DB_query($sql38p,$ErrMsg);
						
				
				
					//if ($myrow4['leave_type'] == 'Maternity Leave'){
					
					$maternityleaveleft = $myrow41['maternity_leave_available'] - $myrow4['mleavedays'];
					$ytdmaternity = $myrow41['ytd_maternity_leave'] + $myrow4['mleavedays'];
					
					
						 $thismleave = $myrow4['mleavehours'] * $baserate;
					$maternityleave=  $thismleave ;
						
						$sql38m = "UPDATE employeemaster SET maternity_leave_available='".$maternityleaveleft."',
						 									 ytd_maternity_leave='".$ytdmaternity."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38m = DB_query($sql38m,$ErrMsg);
					
				
			
			
			
		
				$sqlpremium =  "SELECT   SUM(amount) AS totalpremium
				
						 
			FROM premium_management   
			where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultpremium = DB_query($sqlpremium,$ErrMsg);
		
			
			
		$myrowpremium = DB_fetch_array($resultpremium);
			
			
		//get allowance
			//check in payrol_adjustments_details fnpf related
			//get allowance
			//check in payrol_adjustments_details fnpf related
				$sql411 =  "SELECT sum(amount) as totalamount,sum(premium) as premiumtotal,sum(meal) as mealtotal
				   
						 
			FROM payrol_adjustments_details  
			where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 6 
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result411 = DB_query($sql411,$ErrMsg);
		
			//$manualallowances= 0;
			//$mealonsix = 0;
			//	$premiumonsix = 0;
			$myrowallowances = DB_fetch_array($result411);
				
				$allowances= $myrowallowances['totalamount'];
				$m= $myrowallowances['mealtotal'];
					$p= $myrowallowances['premiumtotal'];
				$manualallowances = $allowances;
				$mealonsix =  $m;
				$premiumonsix =$p;
			
			
	$premiumamount = $premiumamount1 + $myrowpremium['totalpremium'] + $premiumonsix;
		
			$sql4117 =  "SELECT sum(amount) as totalamount,sum(premium) as premiumtotal,sum(meal) as mealtotal
				   
						 
			FROM payrol_adjustments_details  
			where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 16 
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4117 = DB_query($sql4117,$ErrMsg);
		
			//$manualallowances= 0;
			//$mealonsix = 0;
			//	$premiumonsix = 0;
			$myrowallowances7 = DB_fetch_array($result4117);
				
				$allowances= $myrowallowances7['totalamount'];
				$m= $myrowallowances7['mealtotal'];
					$p= $myrowallowances7['premiumtotal'];
				$manualallowances7 = $allowances7;
				$mealonsix7 =  $m;
				$premiumonsix7 =$p;
			
			
	$premiumamount = $premiumamount + $premiumonsix7;
		
			$manualallowances = $manualallowances + $manualallowances7;
			
			
			//absent
			//check incase  entries there
		
			
			
			$sql4119 =  "SELECT type,days,gross
				   
						 
			FROM absent  where employeeid = '" . $myrow4['employeeid']."'
			and id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			
			$result4119 = DB_query($sql4119,$ErrMsg);
		
			
			$leaveallowances2= 0;
			$sickleave2 =0;
			$annualleave2 =0;
			$blleave2 =0;
			$maternityleave2= 0;
			$paternityleave2 =0;
			$familycareleave2 =0;
			
			
		
			while ($m = DB_fetch_array($result4119)){
				
				$Lallowances= $m['gross'];
				
				$leaveallowances2 = $leaveallowances2 + $Lallowances;
				
				if ($m['type'] == 'Sick Leave'){
					
					$sickleaveleft = $myrow41['sick_leave_available'] - $m['days'];
					$ytdsick = $myrow41['ytd_sick_leave'] + $m['days'];
					
					$thissickleave = $m['gross'];
					$sickleave2 = $sickleave2 + $thissickleave;
					
					
					 $sql38s = "UPDATE employeemaster SET sick_leave_available='".$sickleaveleft."',
						 									 ytd_sick_leave='".$ytdsick."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

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

			  

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

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38b = DB_query($sql38b,$ErrMsg);
					
				}
				
				if ($m['type'] == 'Family Care Leave'){
					
					$fleaveleft = $myrow41['familycare_leave_available'] - $m['hours'];
					$ytdf = $myrow41['ytd_familycare_leave'] + $m['hours'];
					
					
					 $thisfleave = $m['gross'];
					$familycareleave2 = $familycareleave2 + $thisfleave ;
					
				}
				
				
					if ($m['type'] == 'Paternity Leave'){
					
					$paternityleaveleft = $myrow41['paternity_leave_available'] - $m['hours'];
					$ytdpaternity = $myrow41['ytd_paternity_leave'] + $m['hours'];
					
					
						 $thispleave = $m['gross'];
					$paternityleave2 = $paternityleave2 + $thispleave ;
						
				}
				
					if ($m['type'] == 'Maternity Leave'){
					
					$maternityleaveleft = $myrow41['maternity_leave_available'] - $m['hours'];
					$ytdmaternity = $myrow41['ytd_maternity_leave'] + $m['hours'];
					
					
						 $thismleave = $m['gross'];
					$maternityleave2= $maternityleave2 + $thismleave ;
					
				}
				
				
				
				
				
			}
				
			$leaveallowances = $leaveallowances + $leaveallowances2;
				
			$sickleave =  $sickleave + $sickleave2 ;
			$annualleave = $annualleave + $annualleave2;
			$blleave = $blleave + $blleave2 ;
			$maternityleave = $maternityleave + $maternityleave2;
			$paternityleave = $paternityleave + $paternityleave2 ;
			$familycareleave =  $familycareleave + $familycareleave2;
						
			
			$thisbonusamount = $thisbonusamount+ $manualallowances + $leaveallowances  + $premiumamount;
			//get manual deductions
				$sql42 =  "SELECT amount
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details >= 8 AND type_details <= 15
			  
			
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result42 = DB_query($sql42,$ErrMsg);
		
			$manualdeductions = 0;
			while ($myrowdeductions = DB_fetch_array($result42)){
				
				$deductions = $myrowdeductions['amount'];
				
				$manualdeductions = $manualdeductions + $deductions;
			}
			
			
			
			
	
               
			
			
					//get fixed deductions for ongoing
			
			
				
				
			
			
			
			
			
			
		
			
			//non fnpf allowances
			
			$sql4112 =  "SELECT sum(amount) as totalamount,sum(meal) as mealtotal
				   
						 
			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);
		
		
		$nonfnpfmyrow = DB_fetch_array($result4112);
				
				$allowances= $nonfnpfmyrow['totalamount'];
				$mealvalue = $nonfnpfmyrow['mealtotal'];
				//$mealvalue = $allowances1;
				$nonfnpfmanualallowances = $allowances;
			
			
			$mealtotalvalue = $meal + $mealvalue + $mealonsix + $mealonsix7;
			
			//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'];
			
			
	
			
			
			
				
			
			//first check 
			$totalgrosspay = $totalnormalamount +$thisbonusamount +$totalpublicholidayamount;
			$gpfnpf = $totalgrosspay * $fnpfemployee;
			$grosspayafterfnpf = $totalgrosspay - $gpfnpf ;
			
			    $grossamount50 = $grosspayafterfnpf * 0.5;
			   $grossamount75 = $grosspayafterfnpfy * 0.75;
			
				if ($fnpfmember == '1'){
			
			$fnpfemployee_amount= $totalgrosspay * $fnpfemployee;
			$fnpfemployer_amount= $totalgrosspay * $fnpfemployer;
		}else{
			$fnpfemployee_amount= 0;
			$fnpfemployer_amount= 0;
			
		}
			
				
			//first get fixed deduction balance
				
		
			
			
			//fixed deduction 
			
					//make an entry in extra table
					//get the payid from above
				$sqlpayid =  "SELECT payid,fixed_deductions
			
						 
			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'];
						$newpayid = $lastid + 1;
			
				$sql43 =  "SELECT id,name,total_amount_to_deduct,deduction_amount,total_paid_till_date,balance_left,ongoing
				   
						 
			FROM fixeddeductions   where employeeid = '" . $myrow4['employeeid']."'
			AND status = 'pending'
			AND deduction_amount > '0.00'
			
			
			
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result43 = DB_query($sql43,$ErrMsg);
		
			$fixeddeductions = 0;
			//$fixeddedcutions75rule =0;
			while ($myrowfixeddeductions = DB_fetch_array($result43)){
		    
				
		
			
				
				 if ($fixeddeduction <= $grossamount50){
				
				    if ( $myrowfixeddeductions['ongoing'] == '0' ){
				
				     	if ( $myrowfixeddeductions['balance_left'] >= 0){
						
						
					      	if ($myrowfixeddeductions['balance_left'] >= $myrowfixeddeductions['deduction_amount']  ){
							
							$fdeductions = $myrowfixeddeductions['deduction_amount'];
							
						}
					    	if ($myrowfixeddeductions['balance_left'] < $myrowfixeddeductions['deduction_amount']  ){
							
							$fdeductions = $myrowfixeddeductions['balance_left'];
							
						}
						
				
						
						
			
				
				
			
						
						//insert in extra table
					if ($fdeductions > '0.00' && $fdeductions <= $grossamount50)	{
						
						$totalpaidtilldate = $myrowfixeddeductions['total_paid_till_date']+$myrowfixeddeductions['deduction_amount'];
						$balance = $myrowfixeddeductions['balance_left'] - $fdeductions;
						
						$sql279 = "INSERT INTO payrun_details_extra (
							payid,
							type,
							fixeddeduction_id,
							name,
							ref_no,
							
							
							amount)
				VALUES ('" . $newpayid."',
						'Fixed Deductions',
						'" . $myrowfixeddeductions['id']."',
						'" . $myrowfixeddeductions['name']."',
						'" . $myrowfixeddeductions['ref_no']."',
							'" . $fdeductions."'
						)";

			$ErrMsg = _('Fixed deduction extra table error');
			$result279 = DB_query($sql279,$ErrMsg);
				
					
						
						//update fixed deduction
	
	 $sql399 = "UPDATE fixeddeductions SET total_paid_till_date= '".$totalpaidtilldate."', 
												  balance_left= '".$balance."'
						WHERE id= '" . $myrowfixeddeductions['id'] . "'";

			  

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

						
						$fixeddeductions = $fixeddeductions + $fdeductions;	
					}
							
					}//balance >=0
				
				
				
				
			
			
					
					
				
				} 
					 else {
				
			
				//if ongoing
				
				$fdeductions = $myrowfixeddeductions['deduction_amount'];
				
				if ($fdeductions > '0.00' && $fdeductions <= $grossamount50)	{
				
				
			$totalpaidtilldate = $myrowfixeddeductions['total_paid_till_date']+$myrowfixeddeductions['deduction_amount'];
				
				
					//insert in extra table
						
						$sql279 = "INSERT INTO payrun_details_extra (
							payid,
							type,
							fixeddeduction_id,
							name,
							ref_no,
							
							
							amount)
				VALUES ('" . $newpayid."',
						'Fixed Deductions',
						'" . $myrowfixeddeductions['id']."',
						'" . $myrowfixeddeductions['name']."',
						'" . $myrowfixeddeductions['ref_no']."',
							'" . $fdeductions."'
						)";

			$ErrMsg = _('Fixed deduction extra table error');
			$result279 = DB_query($sql279,$ErrMsg);
		
						//update fixed deduction
	
	 $sql399 = "UPDATE fixeddeductions SET total_paid_till_date= '".$totalpaidtilldate."'
	 
						WHERE id= '" . $myrowfixeddeductions['id'] . "'";

			  

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

				}
			
			
			
			//tax
			
			//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
			$ytdgross = $myrow41['ytd_gross'];
			
			
			$annualincome = ($totalnormalamount + $totalpublicholidayamount) * 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;
				
				         $thisincometaxtopay = round($annualincometax/52,2);
						$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;
					$thisincometaxtopay = round($annualincometax/52,2);
						$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;
						}
				
			}
			
			//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
	$incometax =  round($annualincometax/52,2);
	$totaldeductions = $incometax + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
		
			
			
	$sql = "INSERT INTO payrun_details (
							id,
							date_processed,
							department,
							employee_type,
							employeeid,
							normal_hours,
							normal_pay,
							timehalf_hours,
							timehalf_amount,
							double_hours,
							double_amount,
							public_holiday_hours,
							public_holiday_amount,
							premium,
							stay_away,
							leave_allowances,
							sick_leave,
							bl_leave,
							annual_leave,
							maternity_leave,
							paternity_leave,
							family_care_leave,
							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,
							employee_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 . "',
						
						'" . $myrow4['publicholidayhours']."',
						'" . $totalpublicholidayamount . "',
						'" . $premiumamount . "',
						'" . $totalstayaway. "',
						'" . $leaveallowances. "',
						'" . $sickleave. "',
						'" . $blleave . "',
						'" . $annualleave. "',
						'" . $maternityleave. "',
						'" . $paternityleave. "',
						'" . $familycareleave. "',
						'" . $manualallowances . "',
						'" . $nonfnpfmanualallowances . "',
						'" . $mealtotalvalue . "',
						'" . $thisbonusamount . "',
						'" . $totalgrosspay. "',
						'52',
						'" . $thispayperiod . "',
						'" . $annualincome . "',
						'" . $annualincomebonus . "',
						'" . $annualincometax . "',
						'" . $annualsrt . "',
						'" . $annualecal . "',
						'" . ($annualincometax + $annualsrt +$annualecal). "',
						'" . $incometax. "',
						'" . $thispaysrt . "',
						'" . $thispayecal . "',
						'" . ($incometax + $thispaysrt+$thispayecal)  . "',
						'" . $fnpfemployee_amount. "',
						'" . $fnpfemployer_amount . "',
						'" . $fixeddeductions . "',
						'" . $manualdeductions. "',
						'" . ($totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances + $totalstayaway - $incometax - $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 + $totalstayaway+ $nonfnpfmanualallowances  - $thisincometaxtopay - $thispaysrt -$thispayecal - $fnpfemployee_amount-$fixeddeductions-$manualdeductions;
			
			$ytd_gross = $myrow41['ytd_gross'] + $totalgrosspay;
			$ytdpay = $ytdpay + $netpay;
			$ytd_income_tax_withheld = $ytd_income_tax_withheld + round($annualincometax/52,2);
			$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;
		
	//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 
			
			//fnpf
		
			
	//insert in detail pay table
			
			//get sum of all premium total and save in parent table-payrun_details
		
		$sql39 = "UPDATE employeemaster SET ytd_gross = '".$ytd_gross."',
											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);
			
	$leaveallowances= 0;
			$sickleave =0;
			$annualleave =0;
			$blleave =0;
			$maternityleave= 0;
			$paternityleave =0;
			$familycareleave =0;
			
				$leaveallowances2= 0;
			$sickleave2 =0;
			$annualleave2 =0;
			$blleave2 =0;
			$maternityleave2= 0;
			$paternityleave2 =0;
			$familycareleave2 =0;
			
			
		}	//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 in adjustments
$sql411check =  "SELECT employeeid
				   
						 
			FROM payrol_adjustments_details 
			WHERE id = '" . $calendarid."'
			AND employeeid >= '13827'
			group by employeeid
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result411check = DB_query($sql411check,$ErrMsg);
		
			//$manualallowances= 0;
			//$mealonsix = 0;
			//	$premiumonsix = 0;
			while ($myrowallowancescheck = DB_fetch_array($result411check)){
			
				
				///check with timesheet. process when record not there. if isset then dont process since above is processed.
				
				
$sql4check  =  "SELECT 
							  SUM(th) AS totalhours,
							 SUM(nt) AS totalnormalhours,
							  SUM(timehalf) AS totaltimeandhalfhours,
							   SUM(dt) AS totaldoublehours,
							     SUM(public_holiday_hours) AS publicholidayhours,
				                SUM(meal) AS totalmeal,
								 SUM(stay_away) AS totalstayaway,
								  SUM(premium) AS totalpremium,
								  employeeid,
								  department,
								  remarks
								  
						 
			FROM manual_timesheet  
			where id = '" . $calendarid."'
		    AND  employeeid = '" . $myrowallowancescheck['employeeid']."'
			
			";
	$ErrMsg = _('This customer could not be added    because');
			$result4check  = DB_query($sql4check ,$ErrMsg);
		
			
			
		$myrow4check  = DB_fetch_array($result4check);
				
				if (!isset($myrow4check['employeeid'])){
					
					
					echo $myrowallowancescheck['employeeid'];
					echo "<br>";
					
					
					$sqlpremium1 =  "SELECT   SUM(amount) AS totalpremium
				
						 
			FROM premium_management   
			where employeeid = '" . $myrowallowancescheck['employeeid']."'
			AND id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultpremium1 = DB_query($sqlpremium1,$ErrMsg);
		
			
			
		$myrowpremium1 = DB_fetch_array($resultpremium1);
					//get allowance
			//check in payrol_adjustments_details fnpf related
			//get allowance
			//check in payrol_adjustments_details fnpf related
				$sql4111 =  "SELECT sum(amount) as totalamount,sum(premium) as premiumtotal,sum(meal) as mealtotal
				   
						 
			FROM payrol_adjustments_details  
			where employeeid = '" . $myrowallowancescheck['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = '6'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4111 = DB_query($sql4111,$ErrMsg);
		
			//$manualallowances= 0;
			//$mealonsix = 0;
			//	$premiumonsix = 0;
			$myrowallowances1 = DB_fetch_array($result4111);
				
				$allowances1= $myrowallowances1['totalamount'];
				$m1= $myrowallowances1['mealtotal'];
					$p1= $myrowallowances1['premiumtotal'];
				$manualallowances1 = $allowances1;
				$mealonsix1 =  $m1;
				$premiumonsix1 =$p1;
			
			
	$premiumamountadj = $myrowpremium1['totalpremium'] + $premiumonsix1;
		//underpayment wages
			$sql41171 =  "SELECT sum(amount) as totalamount,sum(premium) as premiumtotal,sum(meal) as mealtotal
				   
						 
			FROM payrol_adjustments_details  
			where employeeid = '" . $myrowallowancescheck['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 16 
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result41171 = DB_query($sql41171,$ErrMsg);
		
			//$manualallowances= 0;
			//$mealonsix = 0;
			//	$premiumonsix = 0;
			$myrowallowances71 = DB_fetch_array($result41171);
				
				$allowances1= $myrowallowances71['totalamount'];
				$m2= $myrowallowances71['mealtotal'];
					$p2= $myrowallowances71['premiumtotal'];
				$manualallowances71 = $allowances71;
				$mealonsix71 =  $m2;
				$premiumonsix71 =$p2;
			
			
	$premiumamountadj = $premiumamountadj + $premiumonsix71;
		
			$manualallowances1 = $manualallowances1 + $manualallowances71;
			
			
			//absent
			
			$sql41195 =  "SELECT type,gross
				   
						 
			FROM absent  where employeeid = '" . $myrowallowancescheck['employeeid']."'
			and id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			
			$result41195 = DB_query($sql41195,$ErrMsg);
		
			
					$sql415 =  "SELECT fnpfmember,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, ytd_gross,
			total_paid_till_date,fnpf_deducted_till_date,fnpf_employers_contribution_till_date
				
						 
			FROM employeemaster   where employeeid = '" . $myrowallowancescheck['employeeid']."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result415 = DB_query($sql415,$ErrMsg);
		
			
			
		$myrow415 = DB_fetch_array($result415);
			$fnpfmember5 = $myrow415['fnpfmember'];
				$salarytype5 = $myrow415['salary_type'];
				$paycycle5 = $myrow415['paycyle'];
				$completed_pay_period5 = $myrow415['completed_pay_period'];
			
			//if ($myrow4['special_wage_rate'] > 0 ){
				
				
				//$baserate = $myrow4['special_wage_rate'];
				
			//}else{
				
			$baserate5 = $myrow415['base_wage_rate'];	
			//}
			
			//$baserate = $myrow41['base_wage_rate'];
			$ytd_income_tax_withheld5 = $myrow415['ytd_income_tax_withheld'];
			$ytd_srt_tax_withheld5 = $myrow415['ytd_srt-tax_withheld'];
			$departmentemployee5= $myrow415['department'];
				$ytd_ecal_tax_withheld5 = $myrow415['ytd_ecal_tax_withheld'];
				$nonresident5 = $myrow415['non_resident'];
			$bonuspaidtilldate5 = $myrow415['total_bonuspaid_to_date'];
				$ytdpay5 = $myrow41['total_paid_till_date'];
			$ytdemployeecontribution5 = $myrow415['fnpf_deducted_till_date'];
			$ytdemployercontribution5 = $myrow415['fnpf_employers_contribution_till_date'];
					
					
					
					
			
			
			$leaveallowances= 0;
			$sickleave =0;
			$annualleave=0;
			$blleave =0;
			$maternityleave= 0;
			$paternityleave =-0;
			$familycareleave =0;
			while ($m5 = DB_fetch_array($result4119)){
				
				$Lallowances5= $m5['gross'];
				
				$leaveallowances = $leaveallowances + $Lallowances5;
				
				if ($m['type'] == 'Sick Leave'){
					
					$sickleaveleft = $myrow415['sick_leave_available'] - $m['days'];
					$ytdsick = $myrow415['ytd_sick_leave'] + $m['days'];
					
					$thissickleave = $m['gross'];
					$sickleave = $sickleave + $thissickleave;
					
					
					 $sql38s1 = "UPDATE employeemaster SET sick_leave_available='".$sickleaveleft."',
						 									 ytd_sick_leave='".$ytdsick."'
				WHERE employeeid= '" . $myrowallowancescheck['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38s1 = DB_query($sql38s1,$ErrMsg);
					
				}
					if ($m['type'] == 'Annual Leave'){
					
					$aleaveleft = $myrow415['annual_leave_available'] - $m['days'];
					$ytda = $myrow415['ytd_annual_leave'] + $m['days'];
					
						$thisannualleave = $m['gross'];
					$annualleave = $annualleave  + $thisannualleave ;
						
						 $sql38a1 = "UPDATE employeemaster SET annual_leave_available='".$aleaveleft."',
						 								 ytd_annual_leave='".$ytda."'
						WHERE employeeid= '" .$myrowallowancescheck['employeeid']."'";

			  

		$ErrMsg = _('The customer could not be updated because');
			$result38a1 = DB_query($sql38a1,$ErrMsg);
					
				}
	
				if ($m['type'] == 'Bereavement Leave'){
					
					$bleaveleft = $myrow415['bereavement_leave_available'] - $m['days'];
					$ytdb= $myrow415['ytd_bereavement_leave'] + $m['days'];
					
					
						 $thisblleave = $m['gross'];
					$blleave = $blleave + $thisblleave ;
					
					 $sql38b1 = "UPDATE employeemaster SET bereavement_leave_available='".$bleaveleft."',
						 									 ytd_bereavement_leave='".$ytdb."'
				WHERE employeeid= '" . $myrowallowancescheck['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38b1 = DB_query($sql38b1,$ErrMsg);
					
				}
				if ($m5['type'] == 'Family Care Leave'){
					
					$fleaveleft = $myrow415['familycare_leave_available'] - $m5['hours'];
					$ytdf = $myrow415['ytd_familycare_leave'] + $m['hours'];
					
					
					 $thisfleave = $m5['gross'];
					$familycareleave = $familycareleave + $thisfleave ;
					
				}
				
				
					if ($m5['type'] == 'Paternity Leave'){
					
					$paternityleaveleft = $myrow415['paternity_leave_available'] - $m5['hours'];
					$ytdpaternity = $myrow415['ytd_paternity_leave'] + $m5['hours'];
					
					
						 $thispleave = $m5['gross'];
					$paternityleave = $paternityleave + $thispleave ;
						
				}
				
					if ($m5['type'] == 'Maternity Leave'){
					
					$maternityleaveleft = $myrow415['maternity_leave_available'] - $m5['hours'];
					$ytdmaternity = $myrow415['ytd_maternity_leave'] + $m5['hours'];
					
					
						 $thismleave = $m5['gross'];
					$maternityleave= $maternityleave + $thismleave ;
					
				}
				
				
				
				
				
			}
				
			
			$thisbonusamountadj = $leaveallowances +$premiumamountadj;
			//get manual deductions
				$sql42 =  "SELECT amount
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $myrowallowancescheck['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details >= 8 AND type_details <= 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;
			}
			
			
			
               
			
			
			
			
		
			
			//non fnpf allowances
			
			$sql4112 =  "SELECT sum(amount) as totalamount,sum(meal) as mealtotal
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $myrowallowancescheck['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 5
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4112 = DB_query($sql4112,$ErrMsg);
		
		
		$nonfnpfmyrow1 = DB_fetch_array($result4112);
				
				$allowances= $nonfnpfmyrow1['totalamount'];
				$allowances1= $nonfnpfmyrow1['mealmeal'];
				$mealvalue = $allowances1;
				$nonfnpfmanualallowances = $allowances;
			
			
			$mealtotalvalueadj = $mealvalue + $mealonsix1 + $mealonsix71;
			
			//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'];
			
			
	
					//get emaployeedetails
					
					//$myrowallowancescheck['employeeid']
					
				
			
				
			
						//now lets calculate tax 
			$totalgrosspayadj = $manualallowances1 + $thisbonusamountadj;
			
			    $grossamount50 = $totalgrosspayadj * 0.5;
					
					
					//first check 
			//$totalgrosspayadj = $totalnormalamount +$thisbonusamount;
			$gpfnpf = $totalgrosspayadj * $fnpfemployee;
			$grosspayafterfnpf = $totalgrosspayadj - $gpfnpf ;
			
			    $grossamount50 = $grosspayafterfnpf * 0.5;
			   $grossamount75 = $grosspayafterfnpfy * 0.75;
			
				if ($fnpfmember == '1'){
			
			$fnpfemployee_amount= $totalgrosspayadj * $fnpfemployee;
			$fnpfemployer_amount= $totalgrosspayadj * $fnpfemployer;
		}else{
			$fnpfemployee_amount= 0;
			$fnpfemployer_amount= 0;
			
		}			
					
					
					
					//make an entry in extra table
					//get the payid from above
				$sqlpayid =  "SELECT payid,fixed_deductions
			
						 
			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'];
						$newpayid = $lastid + 1;
			
			
			$sql43 =  "SELECT id,name,total_amount_to_deduct,deduction_amount,total_paid_till_date,balance_left,ongoing
				   
						 
			FROM fixeddeductions   where employeeid = '" . $myrowallowancescheck['employeeid']."'
			AND status = 'pending'
			
			
			AND deduction_amount > '0.00'
			
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result43 = DB_query($sql43,$ErrMsg);
		
		$fixeddeductions = 0;
			//$fixeddedcutions75rule =0;
			while ($myrowfixeddeductions = DB_fetch_array($result43)){
		    
			
			
				
				 if ($fixeddeduction <= $grossamount50){
				
				    if ( $myrowfixeddeductions['ongoing'] == '0' ){
				
				     	if ( $myrowfixeddeductions['balance_left'] >= 0){
						
						
					      	if ($myrowfixeddeductions['balance_left'] >= $myrowfixeddeductions['deduction_amount']  ){
							
							$fdeductions = $myrowfixeddeductions['deduction_amount'];
							
						}
					    	if ($myrowfixeddeductions['balance_left'] < $myrowfixeddeductions['deduction_amount']  ){
							
							$fdeductions = $myrowfixeddeductions['balance_left'];
							
						}
						
				
						
						
			
				
				
			
						
						//insert in extra table
					if ($fdeductions > '0.00' && $fdeductions <= $grossamount50)	{
						
						$totalpaidtilldate = $myrowfixeddeductions['total_paid_till_date']+$myrowfixeddeductions['deduction_amount'];
						$balance = $myrowfixeddeductions['balance_left'] - $fdeductions;
						
						$sql279 = "INSERT INTO payrun_details_extra (
							payid,
							type,
							fixeddeduction_id,
							name,
							ref_no,
							
							
							amount)
				VALUES ('" . $newpayid."',
						'Fixed Deductions',
						'" . $myrowfixeddeductions['id']."',
						'" . $myrowfixeddeductions['name']."',
						'" . $myrowfixeddeductions['ref_no']."',
							'" . $fdeductions."'
						)";

			$ErrMsg = _('Fixed deduction extra table error');
			$result279 = DB_query($sql279,$ErrMsg);
				
					
						
						//update fixed deduction
	
	 $sql399 = "UPDATE fixeddeductions SET total_paid_till_date= '".$totalpaidtilldate."', 
												  balance_left= '".$balance."'
						WHERE id= '" . $myrowfixeddeductions['id'] . "'";

			  

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

						
						$fixeddeductions = $fixeddeductions + $fdeductions;	
					}
							
					}//balance >=0
				
				
				
				
			
			
					
					
				
				} 
					 else {
				
			
				//if ongoing
				
				$fdeductions = $myrowfixeddeductions['deduction_amount'];
				
				if ($fdeductions > '0.00' && $fdeductions <= $grossamount50)	{
				
				
			$totalpaidtilldate = $myrowfixeddeductions['total_paid_till_date']+$myrowfixeddeductions['deduction_amount'];
				
				
					//insert in extra table
						
						$sql279 = "INSERT INTO payrun_details_extra (
							payid,
							type,
							fixeddeduction_id,
							name,
							ref_no,
							
							
							amount)
				VALUES ('" . $newpayid."',
						'Fixed Deductions',
						'" . $myrowfixeddeductions['id']."',
						'" . $myrowfixeddeductions['name']."',
						'" . $myrowfixeddeductions['ref_no']."',
							'" . $fdeductions."'
						)";

			$ErrMsg = _('Fixed deduction extra table error');
			$result279 = DB_query($sql279,$ErrMsg);
		
						//update fixed deduction
	
	 $sql399 = "UPDATE fixeddeductions SET total_paid_till_date= '".$totalpaidtilldate."'
	 
						WHERE id= '" . $myrowfixeddeductions['id'] . "'";

			  

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

				}
					
					
					
					//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
			
				$ytdgross = $myrow415['ytd_gross']	;
					
			$annualincome = $ytdgross + $manualallowances1 ;
			
			$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;
						}
				
			}
			
			//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

					
					$incometax =  round($annualincometax/52,2);
	$totaldeductions = $incometax + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
		
	$sql = "INSERT INTO payrun_details (
							id,
							date_processed,
							department,
							employee_type,
							employeeid,
							normal_hours,
							normal_pay,
							timehalf_hours,
							timehalf_amount,
							double_hours,
							double_amount,
							premium,
							stay_away,
							leave_allowances,
							sick_leave,
							bl_leave,
							annual_leave,
							maternity_leave,
							paternity_leave,
							family_care_leave,
							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,
							employee_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',
						'" . $myrowallowancescheck['employeeid'] ."',
						'0',
						'0',
						'0',
						'0',
						'0',
						'0',
						'" . $premiumamountadj . "',
						'0',
						'" . $leaveallowances. "',
						'" . $sickleave. "',
						'" . $blleave . "',
						'" . $annualleave. "',
						'" . $maternityleave. "',
						'" . $paternityleave. "',
						'" . $familycareleave. "',
						'" . $manualallowances1 . "',
						'" . $nonfnpfmanualallowances . "',
						'" . $mealtotalvalueadj . "',
						'" . $thisbonusamountadj . "',
						'" . $totalgrosspayadj. "',
						'52',
						'" . $thispayperiod . "',
						'" . $annualincome . "',
						'" . $annualincomebonus . "',
						'" . $annualincometax . "',
						'" . $annualsrt . "',
						'" . $annualecal . "',
						'" . ($annualincometax + $annualsrt +$annualecal). "',
						'" . round($annualincometax/52,2). "',
						'" . $thispaysrt . "',
						'" . $thispayecal . "',
						'" . ($incometax + $thispaysrt+$thispayecal)  . "',
						'" . $fnpfemployee_amount. "',
						'" . $fnpfemployer_amount . "',
						'" . $fixeddeductions . "',
						'" . $manualdeductions. "',
						'" . ($totalgrosspayadj + $mealtotalvalueadj + $nonfnpfmanualallowances - $incometax - $thispaysrt -$thispayecal - $fnpfemployee_amount-$fixeddeductions-$manualdeductions). "',
						'".$myrow415['payment_method']."',
						'".$myrow415['bank_name']."',
						'".$myrow415['account_name']."',
						'".$myrow415['account_number']."',
						'".$userid."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			
			$netpay = $totalgrosspayadj + $mealtotalvalueadj + $nonfnpfmanualallowances - $incometax - $thispaysrt -$thispayecal - $fnpfemployee_amount-$fixeddeductions-$manualdeductions;
			
			$ytd_gross = $myrow415['ytd_gross'] + $totalgrosspayadj;
			$ytdpay = $ytdpay + $netpay;
			$ytd_income_tax_withheld = $ytd_income_tax_withheld + round($annualincometax/52,2);
			$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;
		
	//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 
			
			//fnpf
		
			
	//insert in detail pay table
			
			//get sum of all premium total and save in parent table-payrun_details
		
		$sql39 = "UPDATE employeemaster SET ytd_gross = '".$ytd_gross."',
											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= '" . $myrowallowancescheck['employeeid'] . "'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result39 = DB_query($sql39,$ErrMsg);
			
		
				$leaveallowances= 0;
			$sickleave =0;
			$annualleave =0;
			$blleave =0;
			$maternityleave= 0;
			$paternityleave =0;
			$familycareleave =0;
			
				
					
					
				}
				
				
				//check with absent see if entry is there so doess not duplicate in payrun
		
				
				
			}



		//find staffs who was only in leave management and not in others
		
		$sql411check1 =  "SELECT employeeid
				   
						 
			FROM absent 
			WHERE id = '" . $calendarid."'
			AND employeeid >= '13827'
			group by employeeid
			";
	$ErrMsg = _('This customer could not be added because');
			$result411check1 = DB_query($sql411check1,$ErrMsg);
		
			//$manualallowances= 0;
			//$mealonsix = 0;
			//	$premiumonsix = 0;
			while ($myrowallowancescheck1 = DB_fetch_array($result411check1)){
			
				
				///check with timesheet. process when record not there. if isset then dont process since above is processed.
				
				
$sql4check1  =  "SELECT 
							  SUM(th) AS totalhours,
							 SUM(nt) AS totalnormalhours,
							  SUM(timehalf) AS totaltimeandhalfhours,
							   SUM(dt) AS totaldoublehours,
				                SUM(meal) AS totalmeal,
								 SUM(stay_away) AS totalstayaway,
								  SUM(premium) AS totalpremium,
								  employeeid,
								  department,
								  remarks
								  
						 
			FROM manual_timesheet  
			where id = '" . $calendarid."'
		    AND  employeeid = '" . $myrowallowancescheck1['employeeid']."'
			
			";
	$ErrMsg = _('This customer could not be added    because');
			$result4check1  = DB_query($sql4check1 ,$ErrMsg);
		
			
			
		$myrow4check1  = DB_fetch_array($result4check1);
				
						//check in adjustments see any entries there
				$sql4check2  =  "SELECT 
							 
								  employeeid
								  
						 
			FROM payrol_adjustments_details  
			where id = '" . $calendarid."'
		    AND  employeeid = '" . $myrowallowancescheck1['employeeid']."'
			
			";
	$ErrMsg = _('This customer could not be added    because');
			$result4check2  = DB_query($sql4check2 ,$ErrMsg);
		
			
			
		$myrow4check2  = DB_fetch_array($result4check2);
				
				
				
				if (!isset($myrow4check1['employeeid']) && !isset($myrow4check2['employeeid'])){
					
					
					echo $myrowallowancescheck1['employeeid'];
					echo "<br>";
					
					
					$sqlpremiumleave =  "SELECT   SUM(amount) AS totalpremium
				
						 
			FROM premium_management   
			where employeeid = '" . $myrowallowancescheck1['employeeid']."'
			AND id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultpremiumleave = DB_query($sqlpremiumleave,$ErrMsg);
		
			
			
		$myrowpremiumleave = DB_fetch_array($resultpremium);
					//get allowance
			//check in payrol_adjustments_details fnpf related
			//get allowance
			//check in payrol_adjustments_details fnpf related
				$sql411leave =  "SELECT sum(amount) as totalamount,sum(premium) as premiumtotal,sum(meal) as mealtotal
				   
						 
			FROM payrol_adjustments_details  
			where employeeid = '" . $myrowallowancescheck1['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 6 
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result411leave = DB_query($sql411leave,$ErrMsg);
		
			//$manualallowances= 0;
			//$mealonsix = 0;
			//	$premiumonsix = 0;
			$myrowallowancesleave = DB_fetch_array($result411leave);
				
				$allowancesleave= $myrowallowancesleave['totalamount'];
				$mleave= $myrowallowancesleave['mealtotal'];
					$pleave= $myrowallowancesleave['premiumtotal'];
				$manualallowancesleave = $allowancesleave;
				$mealonsixleave =  $mleave;
				$premiumonsixleave =$pleave;
			
			
	$premiumamountadjleave = $myrowpremiumleave['totalpremium'] + $premiumonsixleave;
		
			$sql4117leave =  "SELECT sum(amount) as totalamount,sum(premium) as premiumtotal,sum(meal) as mealtotal
				   
						 
			FROM payrol_adjustments_details  
			where employeeid = '" . $myrowallowancescheck1['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 16 
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4117leave = DB_query($sql4117leave,$ErrMsg);
		
			//$manualallowances= 0;
			//$mealonsix = 0;
			//	$premiumonsix = 0;
			$myrowallowances7leave = DB_fetch_array($result4117leave);
				
				$allowances7leave= $myrowallowances7leave['totalamount'];
				$mleave1= $myrowallowances7leave['mealtotal'];
					$pleave1= $myrowallowances7leave['premiumtotal'];
				$manualallowances7leave= $allowances7leave;
				$mealonsix7leave =  $mleave1;
				$premiumonsix7leave =$pleave1;
			
			
	$premiumamountadjleave = $premiumamountadjleave + $premiumonsix7leave;
		
			$manualallowancesleave = $manualallowancesleave + $manualallowances7leave;
			
			
		
		
			//get emaployeedetails
					
					//$myrowallowancescheck['employeeid']
					
				
			$sql416 =  "SELECT fnpfmember,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,ytd_gross
			total_paid_till_date,fnpf_deducted_till_date,fnpf_employers_contribution_till_date
				
						 
			FROM employeemaster   where employeeid = '" . $myrowallowancescheck1['employeeid']."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result416 = DB_query($sql416,$ErrMsg);
		
			
			
		$myrow416 = DB_fetch_array($result416);
			$fnpfmember = $myrow416['fnpfmember'];
				$salarytype = $myrow416['salary_type'];
				$paycycle = $myrow416['paycyle'];
				$completed_pay_period = $myrow416['completed_pay_period'];
			$baserate = $myrow416['base_wage_rate'];
			$ytd_income_tax_withheld = $myrow416['ytd_income_tax_withheld'];
			$ytd_srt_tax_withheld = $myrow416['ytd_srt-tax_withheld'];
			$departmentemployee= $myrow416['department'];
				$ytd_ecal_tax_withheld = $myrow416['ytd_ecal_tax_withheld'];
				$nonresident = $myrow416['non_resident'];
			$bonuspaidtilldate = $myrow416['total_bonuspaid_to_date'];
				$ytdpay = $myrow41['total_paid_till_date'];
			$ytdemployeecontribution = $myrow416['fnpf_deducted_till_date'];
			$ytdemployercontribution = $myrow416['fnpf_employers_contribution_till_date'];	
					
			
			
						//absent
			
			$sql4119leave =  "SELECT type,gross
				   
						 
			FROM absent  where employeeid = '" . $myrowallowancescheck1['employeeid']."'
			and id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			
			$result4119leave = DB_query($sql4119leave,$ErrMsg);
					
					
					
					
					
			
			
			$leaveallowancesabsent= 0;
			$sickleaveabsent =0;
			$annualleaveabsent =0;

			$blleaveabsent =0;
			$maternityleaveabsent= 0;
			$paternityleaveabsent =-0;
			$familycareleaveabsent =0;
			while ($m = DB_fetch_array($result4119leave)){
				
				$Lallowances= $m['gross'];
				
				$leaveallowancesabsent = $leaveallowancesabsent + $Lallowances;
				
			if ($m['type'] == 'Sick Leave'){
					
					$sickleaveleft = $myrow416['sick_leave_available'] - $m['days'];
					$ytdsick = $myrow416['ytd_sick_leave'] + $m['days'];
					
					$thissickleave = $m['gross'];
					$sickleaveabsent = $sickleaveabsent + $thissickleave;
					
					
					 $sql38s1 = "UPDATE employeemaster SET sick_leave_available='".$sickleaveleft."',
						 									 ytd_sick_leave='".$ytdsick."'
				WHERE employeeid= '" . $myrowallowancescheck1['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38s1 = DB_query($sql38s1,$ErrMsg);
					
				}
					if ($m['type'] == 'Annual Leave'){
					
					$aleaveleft = $myrow416['annual_leave_available'] - $m['days'];
					$ytda = $myrow416['ytd_annual_leave'] + $m['days'];
					
						$thisannualleave = $m['gross'];
					$annualleaveabsent = $annualleaveabsent  + $thisannualleave ;
						
						 $sql38a1 = "UPDATE employeemaster SET annual_leave_available='".$aleaveleft."',
						 								 ytd_annual_leave='".$ytda."'
						WHERE employeeid= '" .$myrowallowancescheck1['employeeid']."'";

			  

		$ErrMsg = _('The customer could not be updated because');
			$result38a1 = DB_query($sql38a1,$ErrMsg);
					
				}
	
				if ($m['type'] == 'Bereavement Leave'){
					
					$bleaveleft = $myrow416['bereavement_leave_available'] - $m['days'];
					$ytdb= $myrow416['ytd_bereavement_leave'] + $m['days'];
					
					
						 $thisblleave = $m['gross'];
					$blleaveabsent = $blleaveabsent + $thisblleave ;
					
					 $sql38b1 = "UPDATE employeemaster SET bereavement_leave_available='".$bleaveleft."',
						 									 ytd_bereavement_leave='".$ytdb."'
				WHERE employeeid= '" . $myrowallowancescheck1['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38b1 = DB_query($sql38b1,$ErrMsg);
					
				}
				
				if ($m['type'] == 'Family Care Leave'){
					
					$fleaveleft = $myrow416['familycare_leave_available'] - $m['hours'];
					$ytdf = $myrow416['ytd_familycare_leave'] + $m['hours'];
					
					
					 $thisfleave = $m['gross'];
					$familycareleaveabsent = $familycareleaveabsent + $thisfleave ;
					
				}
				
				
					if ($m['type'] == 'Paternity Leave'){
					
					$paternityleaveleft = $myrow416['paternity_leave_available'] - $m['hours'];
					$ytdpaternity = $myrow416['ytd_paternity_leave'] + $m['hours'];
					
					
						 $thispleave = $m['gross'];
					$paternityleaveabsent = $paternityleaveabsent + $thispleave ;
						
				}
				
					if ($m['type'] == 'Maternity Leave'){
					
					$maternityleaveleft = $myrow416['maternity_leave_available'] - $m['hours'];
					$ytdmaternity = $myrow416['ytd_maternity_leave'] + $m['hours'];
					
					
						 $thismleave = $m['gross'];
					$maternityleaveabsent= $maternityleaveabsent + $thismleave ;
					
				}
				
				
				
				
				
			}
				
			
			$thisbonusamountadj1 = $leaveallowancesabsent +$premiumamountadjleave;
			//get manual deductions
				$sql42 =  "SELECT amount
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $myrowallowancescheck1['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details >= 8 AND type_details <= 15
			  
			 
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result42 = DB_query($sql42,$ErrMsg);
		
			$manualdeductions = 0;
			while ($myrowdeductions = DB_fetch_array($result42)){
				
				$deductions = $myrowdeductions['amount'];
				
				$manualdeductions = $manualdeductions + $deductions;
			}
			
			
		
               
			
		
			//non fnpf allowances
			
			$sql4112 =  "SELECT sum(amount) as totalamount,sum(meal) as mealtotal
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $myrowallowancescheck1['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 5
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4112 = DB_query($sql4112,$ErrMsg);
		
		
		$nonfnpfmyrow2 = DB_fetch_array($result4112);
				
				$allowancesleave= $nonfnpfmyrow2['totalamount'];
				$allowances1leave= $nonfnpfmyrow2['mealmeal'];
				$mealvalueleave = $allowances1leave;
				$nonfnpfmanualallowancesleave = $allowancesleave;
			
			
			$mealtotalvalueadjleave = $mealvalueleave + $mealonsixleave + $mealonsix7leave;
			
			//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'];
					
					
						//first check 
			$totalgrosspayadj = $thisbonusamountadj1;
			$gpfnpf = $totalgrosspayadj * $fnpfemployee;
			$grosspayafterfnpf = $totalgrosspayadj - $gpfnpf ;
			
			    $grossamount50 = $grosspayafterfnpf * 0.5;
			   $grossamount75 = $grosspayafterfnpfy * 0.75;
			
				if ($fnpfmember == '1'){
			
			$fnpfemployee_amount= $totalgrosspayadj * $fnpfemployee;
			$fnpfemployer_amount= $totalgrosspayadj * $fnpfemployer;
		}else{
			$fnpfemployee_amount= 0;
			$fnpfemployer_amount= 0;
			
		}			
					
			
				//make an entry in extra table
					//get the payid from above
				$sqlpayid =  "SELECT payid,fixed_deductions
			
						 
			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'];
						$newpayid = $lastid + 1;
			
			//get fixed deductions
			
			
				$sql43 =  "SELECT id,name,total_amount_to_deduct,deduction_amount,total_paid_till_date,balance_left,ongoing
				   
						 
			FROM fixeddeductions   where employeeid = '" . $myrowallowancescheck1['employeeid']."'
			
			AND status = 'pending'
			
			AND deduction_amount > '0.00'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result43 = DB_query($sql43,$ErrMsg);
			$fixeddeductions = 0;
			//$fixeddedcutions75rule =0;
		while ($myrowfixeddeductions = DB_fetch_array($result43)){
		    
				
			 
			
				
				 if ($fixeddeduction <= $grossamount50){
				
				    if ( $myrowfixeddeductions['ongoing'] == '0' ){
				
				     	if ( $myrowfixeddeductions['balance_left'] >= 0){
						
						
					      	if ($myrowfixeddeductions['balance_left'] >= $myrowfixeddeductions['deduction_amount']  ){
							
							$fdeductions = $myrowfixeddeductions['deduction_amount'];
							
						}
					    	if ($myrowfixeddeductions['balance_left'] < $myrowfixeddeductions['deduction_amount']  ){
							
							$fdeductions = $myrowfixeddeductions['balance_left'];
							
						}
						
				
						
						
			
				
				
			
						
						//insert in extra table
					if ($fdeductions > '0.00' && $fdeductions <= $grossamount50)	{
						
						$totalpaidtilldate = $myrowfixeddeductions['total_paid_till_date']+$myrowfixeddeductions['deduction_amount'];
						$balance = $myrowfixeddeductions['balance_left'] - $fdeductions;
						
						$sql279 = "INSERT INTO payrun_details_extra (
							payid,
							type,
							fixeddeduction_id,
							name,
							ref_no,
							
							
							amount)
				VALUES ('" . $newpayid."',
						'Fixed Deductions',
						'" . $myrowfixeddeductions['id']."',
						'" . $myrowfixeddeductions['name']."',
						'" . $myrowfixeddeductions['ref_no']."',
							'" . $fdeductions."'
						)";

			$ErrMsg = _('Fixed deduction extra table error');
			$result279 = DB_query($sql279,$ErrMsg);
				
					
						
						//update fixed deduction
	
	 $sql399 = "UPDATE fixeddeductions SET total_paid_till_date= '".$totalpaidtilldate."', 
												  balance_left= '".$balance."'
						WHERE id= '" . $myrowfixeddeductions['id'] . "'";

			  

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

						
						$fixeddeductions = $fixeddeductions + $fdeductions;	
					}
							
					}//balance >=0
				
				
				
				
			
			
					
					
				
				} 
					 else {
				
			
				//if ongoing
				
				$fdeductions = $myrowfixeddeductions['deduction_amount'];
				
				if ($fdeductions > '0.00' && $fdeductions <= $grossamount50)	{
				
				
			$totalpaidtilldate = $myrowfixeddeductions['total_paid_till_date']+$myrowfixeddeductions['deduction_amount'];
				
				
					//insert in extra table
						
						$sql279 = "INSERT INTO payrun_details_extra (
							payid,
							type,
							fixeddeduction_id,
							name,
							ref_no,
							
							
							amount)
				VALUES ('" . $newpayid."',
						'Fixed Deductions',
						'" . $myrowfixeddeductions['id']."',
						'" . $myrowfixeddeductions['name']."',
						'" . $myrowfixeddeductions['ref_no']."',
							'" . $fdeductions."'
						)";

			$ErrMsg = _('Fixed deduction extra table error');
			$result279 = DB_query($sql279,$ErrMsg);
		
						//update fixed deduction
	
	 $sql399 = "UPDATE fixeddeductions SET total_paid_till_date= '".$totalpaidtilldate."'
	 
						WHERE id= '" . $myrowfixeddeductions['id'] . "'";

			  

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

				}
	
					
					
					
					
					
					
					
					
					
					//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
			$ytdgross = $myrow416['ytd_gross'];
			$annualincome = $ytdgross;
			
			$bonuspaidtilldate = $bonuspaidtilldate  + $manualallowancesleave + $thisbonusamountadj1;
			
			$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;
						}
				
			}
			
			//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
	$incometax =  round($annualincometax/52,2);
	$sql = "INSERT INTO payrun_details (
							id,
							date_processed,
							department,
							employee_type,
							employeeid,
							normal_hours,
							normal_pay,
							timehalf_hours,
							timehalf_amount,
							double_hours,
							double_amount,
							premium,
							stay_away,
							leave_allowances,
							sick_leave,
							bl_leave,
							annual_leave,
							maternity_leave,
							paternity_leave,
							family_care_leave,
							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,
							employee_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',
						'" . $myrowallowancescheck1['employeeid'] ."',
						'0',
						'0',
						'0',
						'0',
						'0',
						'0',
						'" . $premiumamountadjleave . "',
						'0',
						'" . $leaveallowancesabsent. "',
						'" . $sickleaveabsent. "',
						'" . $blleaveabsent . "',
						'" . $annualleaveabsent. "',
						'" . $maternityleaveabsent. "',
						'" . $paternityleaveabsent. "',
						'" . $familycareleaveabsent. "',
						'" . $manualallowancesleaveabsent . "',
						'" . $nonfnpfmanualallowancesleave . "',
						'" . $mealtotalvalueadjleave . "',
						'" . $thisbonusamountadj1 . "',
						'" . $totalgrosspayadj. "',
						'52',
						'" . $thispayperiod . "',
						'" . $annualincome . "',
						'" . $annualincomebonus . "',
						'" . $annualincometax . "',
						'" . $annualsrt . "',
						'" . $annualecal . "',
						'" . ($annualincometax + $annualsrt +$annualecal). "',
						'" . round($annualincometax/52,2). "',
						'" . $thispaysrt . "',
						'" . $thispayecal . "',
						'" . ($incometax + $thispaysrt+$thispayecal)  . "',
						'" . $fnpfemployee_amount. "',
						'" . $fnpfemployer_amount . "',
						'" . $fixeddeductions . "',
						'" . $manualdeductions. "',
						'" . ($totalgrosspayadj + $mealtotalvalueadjleave + $nonfnpfmanualallowancesleave - $incometax - $thispaysrt -$thispayecal - $fnpfemployee_amount-$fixeddeductions-$manualdeductions). "',
						'".$myrow416['payment_method']."',
						'".$myrow416['bank_name']."',
						'".$myrow416['account_name']."',
						'".$myrow416['account_number']."',
						'".$userid."'
						)";

			$ErrMsg = _('This customer could not be added because');
			$result = DB_query($sql,$ErrMsg);
			
			$netpay = $totalgrosspayadj + $mealtotalvalueadjleave + $nonfnpfmanualallowancesleave - $incometax - $thispaysrt -$thispayecal - $fnpfemployee_amount-$fixeddeductions-$manualdeductions;
			
			$ytd_gross = $myrow416['ytd_gross'] + $totalgrosspayadj;
			$ytdpay = $ytdpay + $netpay;
			$ytd_income_tax_withheld = $ytd_income_tax_withheld + round($annualincometax/52,2);
			$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;
		
	//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 
			
			//fnpf
		
			
	//insert in detail pay table
			
			//get sum of all premium total and save in parent table-payrun_details
		
		$sql39 = "UPDATE employeemaster SET ytd_gross = '".$ytd_gross."',
											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= '" . $myrowallowancescheck1['employeeid'] . "'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result39 = DB_query($sql39,$ErrMsg);
			
			
						$leaveallowancesabsent= 0;
			$sickleaveabsent =0;
			$annualleaveabsent =0;

			$blleaveabsent =0;
			$maternityleaveabsent= 0;
			$paternityleaveabsent =-0;
			$familycareleaveabsent =0;
					
				}
				
				
				
				
				
			}
		
		
		

		

//technical timesheet
			$leaveallowancestech= 0;
			$sickleavetech =0;
			$annualleavetech =0;
			$blleavetech =0;
			$maternityleavetech= 0;
			$paternityleavetech =0;
			$familycareleavetech =0;
		

$sql4 =  "SELECT 
							  SUM(th) AS totalhours,
							 SUM(nt) AS totalnormalhours,
							  SUM(timehalf) AS totaltimeandhalfhours,
							   SUM(dt) AS totaldoublehours,
				                SUM(meal) AS totalmeal,
								 SUM(stay_away) AS totalstayaway,
								   SUM(call_out_allowances) AS calloutallowances,
								  SUM(premium) AS totalpremium, 
								  SUM(leave_hours) AS totalleavehours,
								   SUM(IF(leave_type = 'Sick Leave', leave_days, 0)) AS sickleavedays,
								  SUM(IF(leave_type = 'Annual Leave', leave_days, 0)) AS annualleavedays,
								  SUM(IF(leave_type = 'Bereavement Leave', leave_days, 0)) AS bleavedays,
								  SUM(IF(leave_type = 'Family Care Leave', leave_days, 0)) AS fleavedays,
								  SUM(IF(leave_type = 'Paternity Leave', leave_days, 0)) AS pleavedays,
								   SUM(IF(leave_type = 'Maternity Leave', leave_days, 0)) AS mleavedays,
								  SUM(IF(leave_type = 'Sick Leave', leave_hours, 0)) AS sickleavehours,
								  SUM(IF(leave_type = 'Annual Leave', leave_hours, 0)) AS annualleavehours,
								  SUM(IF(leave_type = 'Bereavement Leave', leave_hours, 0)) AS bleavehours,
								  SUM(IF(leave_type = 'Family Care Leave', leave_hours, 0)) AS fleavehours,
								  SUM(IF(leave_type = 'Paternity Leave', leave_hours, 0)) AS pleavehours,
								   SUM(IF(leave_type = 'Maternity Leave', leave_hours, 0)) AS mleavehours,
								  employeeid,
								  department,
								  labourcode,
								  glcode,
								  remarks
								  
						 
			FROM manual_tech_timesheet  
			where id = '" . $calendarid."'
			AND employeeid >= '13827'
		    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)) {
		
		
			$totalhours =  $myrow4['totalhours'];
			
			$totalnormalhours = $myrow4['totalnormalhours'];
	
	
	
	
	$totaltimeandhalf = $myrow4['totaltimeandhalfhours'];
	
	$totaldouble = $myrow4['totaldoublehours'];
	
			$mealtotalvalue = $myrow4['totalmeal'];
			$calloutamount= $myrow4['calloutallowances'];
			
			$publicholidayhours= $myrow4['totalpremium'];
			$totalstayaway = $myrow4['totalstayaway'];
			//for each employee do payrol calculations below and save in payrun_details table. insert for new records on same calender id and update for updating or recalculating tallies
		
			
		//get all premium and add to above premium
			
		
			//get the rates. usa embassy get rates from client table
			
			
			$sql41 =  "SELECT fnpfmember,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, ytd_gross,
			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);
			$fnpfmember = $myrow41['fnpfmember'];
				$salarytype = $myrow41['salary_type'];
				$paycycle = $myrow41['paycyle'];
				$completed_pay_period = $myrow41['completed_pay_period'];
			
			//if ($myrow4['special_wage_rate'] > 0 ){
				
				
				//$baserate = $myrow4['special_wage_rate'];
				
			//}else{
				
			$baserate = $myrow41['base_wage_rate'];	
			//}
			
			//$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 
			
			
			
		  $totalnormalamount = $totalnormalhours * $baserate;
			
			$totaltimeandhalfamount =$totaltimeandhalf * (1.5 * $baserate);
			
			$totaldoubleamount = $totaldouble * (2 * $baserate);
			$publicholidayamount = $publicholidayhours * $baserate;
			
			$thisbonusamount = $totaltimeandhalfamount + $totaldoubleamount ;
			
		
			 
			
			
			
			
				$sqlpremium =  "SELECT   SUM(amount) AS totalpremium
				
						 
			FROM premium_management   
			where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$resultpremium = DB_query($sqlpremium,$ErrMsg);
		
			
			
		$myrowpremium = DB_fetch_array($resultpremium);
			
			
		//get allowance
			//check in payrol_adjustments_details fnpf related
			//get allowance
			//check in payrol_adjustments_details fnpf related
				$sql411 =  "SELECT sum(amount) as totalamount,sum(premium) as premiumtotal,sum(meal) as mealtotal
				   
						 
			FROM payrol_adjustments_details  
			where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 6 
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result411 = DB_query($sql411,$ErrMsg);
		
			//$manualallowances= 0;
			//$mealonsix = 0;
			//	$premiumonsix = 0;
			$myrowallowances = DB_fetch_array($result411);
				
				$allowances= $myrowallowances['totalamount'];
				$m= $myrowallowances['mealtotal'];
					$p= $myrowallowances['premiumtotal'];
				$manualallowances = $allowances;
				$mealonsix =  $m;
				$premiumonsix =$p;
			
			
	$premiumamount = $premiumamount1 + $myrowpremium['totalpremium'] + $premiumonsix;
		
			$sql4117 =  "SELECT sum(amount) as totalamount,sum(premium) as premiumtotal,sum(meal) as mealtotal
				   
						 
			FROM payrol_adjustments_details  
			where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details = 16 
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result4117 = DB_query($sql4117,$ErrMsg);
		
			//$manualallowances= 0;
			//$mealonsix = 0;
			//	$premiumonsix = 0;
			$myrowallowances7 = DB_fetch_array($result4117);
				
				$allowances= $myrowallowances7['totalamount'];
				$m= $myrowallowances7['mealtotal'];
					$p= $myrowallowances7['premiumtotal'];
				$manualallowances7 = $allowances7;
				$mealonsix7 =  $m;
				$premiumonsix7 =$p;
			
			
	$premiumamount = $premiumamount + $premiumonsix7;
		
			$manualallowances = $manualallowances + $manualallowances7;
			
			
			//absent
			
					$Lallowances= $myrow4['totalleavehours'] * $baserate;
				
				$leaveallowancestech =$Lallowances;
			
		//	if ($myrow4['leave_type'] == 'Sick Leave'){
					
					$sickleaveleft = $myrow41['sick_leave_available'] - $myrow4['sickleavedays'];
					$ytdsick = $myrow41['ytd_sick_leave'] + $myrow4['sickleavedays'];
					
					$thissickleave = $myrow4['sickleavehours'] * $baserate;
					$sickleavetech =  $thissickleave;
					
					
					 $sql38s = "UPDATE employeemaster SET sick_leave_available='".$sickleaveleft."',
						 									 ytd_sick_leave='".$ytdsick."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38s = DB_query($sql38s,$ErrMsg);
					
				
		//if ($myrow4['leave_type'] == 'Annual Leave'){
					
					$aleaveleft = $myrow41['annual_leave_available'] - $myrow4['annualleavedays'];
					$ytda = $myrow41['ytd_annual_leave'] + $myrow4['annualleavedays'];
					
						$thisannualleave = $myrow4['annualleavehours'] * $baserate;
					$annualleavetech =$thisannualleave ;
						
						 $sql38a = "UPDATE employeemaster SET annual_leave_available='".$aleaveleft."',
						 								 ytd_annual_leave='".$ytda."'
						WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

		$ErrMsg = _('The customer could not be updated because');
			$result38a = DB_query($sql38a,$ErrMsg);
					
				
	
				//if ($myrow4['leave_type'] == 'Bereavement Leave'){
					
					$bleaveleft = $myrow41['bereavement_leave_available'] - $myrow4['bleavedays'];
					$ytdb= $myrow41['ytd_bereavement_leave'] + $myrow4['bleavedays'];
					
					
						 $thisblleave = $myrow4['bleavehours'] * $baserate;
					$blleavetech = $thisblleave ;
					
					 $sql38b = "UPDATE employeemaster SET bereavement_leave_available='".$bleaveleft."',
						 									 ytd_bereavement_leave='".$ytdb."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

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

				//if ($myrow4['leave_type'] == 'Family Care Leave'){
					
					$fleaveleft = $myrow41['familycare_leave_available'] - $myrow4['fleavedays'];
					$ytdf = $myrow41['ytd_familycare_leave'] + $myrow4['fleavedays'];
					
					
					 $thisfleave = $myrow4['fleavehours'] * $baserate;
					$familycareleavetech = $thisfleave ;
					$sql38fam = "UPDATE employeemaster SET familycare_leave_available='".$fleaveleft."',
						 									 ytd_familycare_leave='".$ytdf."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38f = DB_query($sql38fam,$ErrMsg);
					
					
				
				
				
				//	if ($myrow4['leave_type'] == 'Paternity Leave'){
					
					$paternityleaveleft = $myrow41['paternity_leave_available'] - $myrow4['pleavedays'];
					$ytdpaternity = $myrow41['ytd_paternity_leave'] + $myrow4['pleavedays'];
					
					
						 $thispleave =$myrow4['pleavehours'] * $baserate;
					$paternityleavetech = $thispleave ;
						
						$sql38p = "UPDATE employeemaster SET paternity_leave_available='".$paternityleaveleft."',
						 									 ytd_paternity_leave='".$ytdpaternity."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38p = DB_query($sql38p,$ErrMsg);
						
				
				
					//if ($myrow4['leave_type'] == 'Maternity Leave'){
					
					$maternityleaveleft = $myrow41['maternity_leave_available'] - $myrow4['mleavedays'];
					$ytdmaternity = $myrow41['ytd_maternity_leave'] + $myrow4['mleavedays'];
					
					
						 $thismleave = $myrow4['mleavehours'] * $baserate;
					$maternityleavetech=  $thismleave ;
						
						$sql38m = "UPDATE employeemaster SET maternity_leave_available='".$maternityleaveleft."',
						 									 ytd_maternity_leave='".$ytdmaternity."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38m = DB_query($sql38m,$ErrMsg);
					
				
		
				
			
			//check in leave management in case any entries there
			
			$sql4119 =  "SELECT type,gross
				   
						 
			FROM absent  where employeeid = '" . $myrow4['employeeid']."'
			and id = '" . $calendarid."'
			
			";
	$ErrMsg = _('This customer could not be added because');
			
			$result4119 = DB_query($sql4119,$ErrMsg);
			
			$leaveallowances2tech= 0;
			$sickleave2tech =0;
			$annualleave2tech =0;
			$blleave2tech =0;
			$maternityleave2tech= 0;
			$paternityleave2tech =0;
			$familycareleave2tech =0;
			
			
			
			
			while ($m = DB_fetch_array($result4119)){
				
				$Lallowances= $m['gross'];
				
				$leaveallowances2tech = $leaveallowances2tech + $Lallowances;
				
if ($m['type'] == 'Sick Leave'){
					
					$sickleaveleft = $myrow41['sick_leave_available'] - $m['days'];
					$ytdsick = $myrow41['ytd_sick_leave'] + $m['days'];
					
					$thissickleave = $m['gross'];
					$sickleave2tech = $sickleave2tech + $thissickleave;
					
					
					 $sql38s = "UPDATE employeemaster SET sick_leave_available='".$sickleaveleft."',
						 									 ytd_sick_leave='".$ytdsick."'
				WHERE employeeid= '" . $myrow4['employeeid']."'";

			  

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

			  

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

			  

			$ErrMsg = _('The customer could not be updated because');
			$result38b = DB_query($sql38b,$ErrMsg);
					
				}
				

				if ($m['type'] == 'Family Care Leave'){
					
					$fleaveleft = $myrow41['familycare_leave_available'] - $m['hours'];
					$ytdf = $myrow41['ytd_familycare_leave'] + $m['hours'];
					
					
					 $thisfleave = $m['gross'];
					$familycareleave2tech = $familycareleave2tech + $thisfleave ;
					
				}
				
				
					if ($m['type'] == 'Paternity Leave'){
					
					$paternityleaveleft = $myrow41['paternity_leave_available'] - $m['hours'];
					$ytdpaternity = $myrow41['ytd_paternity_leave'] + $m['hours'];
					
					
						 $thispleave = $m['gross'];
					$paternityleave2tech = $paternityleave2tech + $thispleave ;
						
				}
				
					if ($m['type'] == 'Maternity Leave'){
					
					$maternityleaveleft = $myrow41['maternity_leave_available'] - $m['hours'];
					$ytdmaternity = $myrow41['ytd_maternity_leave'] + $m['hours'];
					
					
						 $thismleave = $m['gross'];
					$maternityleave2tech= $maternityleave2tech + $thismleave ;
					
				}
				
				
				
				
				
			}
				
				$leaveallowancestech = $leaveallowancestech + $leaveallowances2tech;
				
			$sickleavetech =  $sickleavetech + $sickleave2tech ;
			$annualleavetech = $annualleavetech + $annualleave2tech;
			$blleavetech = $blleavetech + $blleave2tech ;
			$maternityleavetech = $maternityleavetech + $maternityleave2tech;
			$paternityleavetech = $paternityleavetech + $paternityleave2tech ;
			$familycareleavetech =  $familycareleavetech + $familycareleave2tech;
			
			
			
			$thisbonusamount = $thisbonusamount+ $manualallowances + $leaveallowancestech +$premiumamount;
			//get manual deductions
				$sql42 =  "SELECT amount
				   
						 
			FROM payrol_adjustments_details   where employeeid = '" . $myrow4['employeeid']."'
			AND id = '" . $calendarid."'
			AND type_details >= 8 AND type_details <= 15
			  
			
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result42 = DB_query($sql42,$ErrMsg);
		
			$manualdeductions = 0;
			while ($myrowdeductions = DB_fetch_array($result42)){
				
				$deductions = $myrowdeductions['amount'];
				
				$manualdeductions = $manualdeductions + $deductions;
			}
			
			
			
			
	
               
			
			
					//get fixed deductions for ongoing
			
			
				
				
			
			
			
			
			
			
		
			
			//non fnpf allowances
			
			$sql4112 =  "SELECT sum(amount) as totalamount,sum(meal) as mealtotal
				   
						 
			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);
		
		
		$nonfnpfmyrow = DB_fetch_array($result4112);
				
				$allowances= $nonfnpfmyrow['totalamount'];
				$allowances1= $nonfnpfmyrow['mealmeal'];
				$mealvalue = $allowances1;
				$nonfnpfmanualallowances = $allowances;
			
			
			$mealtotalvalue = $mealtotalvalue + $mealvalue + $mealonsix + $mealonsix7;
			
			//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'];
			
			
	
			
			
			
				
			
			//first check 
			$totalgrosspay = $totalnormalamount +$thisbonusamount +$publicholidayamount ;
			$gpfnpf = $totalgrosspay * $fnpfemployee;
			$grosspayafterfnpf = $totalgrosspay - $gpfnpf ;
			
			    $grossamount50 = $grosspayafterfnpf * 0.5;
			   $grossamount75 = $grosspayafterfnpfy * 0.75;
			
				if ($fnpfmember == '1'){
			
			$fnpfemployee_amount= $totalgrosspay * $fnpfemployee;
			$fnpfemployer_amount= $totalgrosspay * $fnpfemployer;
		}else{
			$fnpfemployee_amount= 0;
			$fnpfemployer_amount= 0;
			
		}
			
				//fixed deduction 
			
					//make an entry in extra table
					//get the payid from above
				$sqlpayid =  "SELECT payid,fixed_deductions
			
						 
			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'];
						$newpayid = $lastid + 1;
			
				$sql43 =  "SELECT id,name,total_amount_to_deduct,deduction_amount,total_paid_till_date,balance_left,ongoing
				   
						 
			FROM fixeddeductions   where employeeid = '" . $myrow4['employeeid']."'
			
			AND status = 'pending'
			
			AND deduction_amount > '0.00'
			
			";
	$ErrMsg = _('This customer could not be added because');
			$result43 = DB_query($sql43,$ErrMsg);
		
			$fixeddeductions = 0;
			//$fixeddedcutions75rule =0;
			while ($myrowfixeddeductions = DB_fetch_array($result43)){
		    
			
			
				
				 if ($fixeddeduction <= $grossamount50){
				
				    if ( $myrowfixeddeductions['ongoing'] == '0' ){
				
				     	if ( $myrowfixeddeductions['balance_left'] >= 0){
						
						
					      	if ($myrowfixeddeductions['balance_left'] >= $myrowfixeddeductions['deduction_amount']  ){
							
							$fdeductions = $myrowfixeddeductions['deduction_amount'];
							
						}
					    	if ($myrowfixeddeductions['balance_left'] < $myrowfixeddeductions['deduction_amount']  ){
							
							$fdeductions = $myrowfixeddeductions['balance_left'];
							
						}
						
				
						
						
			
				
				
			
						
						//insert in extra table
					if ($fdeductions > '0.00' && $fdeductions <= $grossamount50)	{
						
						$totalpaidtilldate = $myrowfixeddeductions['total_paid_till_date']+$myrowfixeddeductions['deduction_amount'];
						$balance = $myrowfixeddeductions['balance_left'] - $fdeductions;
						
						$sql279 = "INSERT INTO payrun_details_extra (
							payid,
							type,
							fixeddeduction_id,
							name,
							ref_no,
							
							
							amount)
				VALUES ('" . $newpayid."',
						'Fixed Deductions',
						'" . $myrowfixeddeductions['id']."',
						'" . $myrowfixeddeductions['name']."',
						'" . $myrowfixeddeductions['ref_no']."',
							'" . $fdeductions."'
						)";

			$ErrMsg = _('Fixed deduction extra table error');
			$result279 = DB_query($sql279,$ErrMsg);
				
					
						
						//update fixed deduction
	
	 $sql399 = "UPDATE fixeddeductions SET total_paid_till_date= '".$totalpaidtilldate."', 
												  balance_left= '".$balance."'
						WHERE id= '" . $myrowfixeddeductions['id'] . "'";

			  

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

						
						$fixeddeductions = $fixeddeductions + $fdeductions;	
					}
							
					}//balance >=0
				
				
				
				
			
			
					
					
				
				} 
					 else {
				
			
				//if ongoing
				
				$fdeductions = $myrowfixeddeductions['deduction_amount'];
				
				if ($fdeductions > '0.00' && $fdeductions <= $grossamount50)	{
				
				
			$totalpaidtilldate = $myrowfixeddeductions['total_paid_till_date']+$myrowfixeddeductions['deduction_amount'];
				
				
					//insert in extra table
						
						$sql279 = "INSERT INTO payrun_details_extra (
							payid,
							type,
							fixeddeduction_id,
							name,
							ref_no,
							
							
							amount)
				VALUES ('" . $newpayid."',
						'Fixed Deductions',
						'" . $myrowfixeddeductions['id']."',
						'" . $myrowfixeddeductions['name']."',
						'" . $myrowfixeddeductions['ref_no']."',
							'" . $fdeductions."'
						)";

			$ErrMsg = _('Fixed deduction extra table error');
			$result279 = DB_query($sql279,$ErrMsg);
		
						//update fixed deduction
	
	 $sql399 = "UPDATE fixeddeductions SET total_paid_till_date= '".$totalpaidtilldate."'
	 
						WHERE id= '" . $myrowfixeddeductions['id'] . "'";

			  

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

				}
			
			
			
			//tax
			
			//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 +$publicholidayamount)* 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;
				
				         $thisincometaxtopay = round($annualincometax/52,2);
						$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;
					$thisincometaxtopay = round($annualincometax/52,2);
						$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;
						}
				
			}
			
			//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
	$incometax =  round($annualincometax/52,2);
	$totaldeductions = $incometax + $thispaysrt + $thispayecal+$fnpfemployee + $manualdeductions + $fixeddeductions;
			
			
			 
	$sql = "INSERT INTO payrun_details (
							id,
							date_processed,
							department,
							employee_type,
							employeeid,
							labourcode,
							glcode,
							normal_hours,
							normal_pay,
							timehalf_hours,
							timehalf_amount,
							double_hours,
							double_amount,
							public_holiday_hours,
							public_holiday_amount,
							premium,
							stay_away,
							call_out_allowances,
							leave_allowances,
							sick_leave,
							bl_leave,
							annual_leave,
							maternity_leave,
							paternity_leave,
							family_care_leave,
							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,
							employee_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'] ."',
						'" . $myrow4['labourcode'] ."',
						'" . $myrow4['glcode'] ."',
						'" . $totalnormalhours ."',
						'" . $totalnormalamount."',
						'" . $totaltimeandhalf ."',
						'" . $totaltimeandhalfamount ."',
						'" . $totaldouble."',
						'" . $totaldoubleamount . "',
					
						'" . $publicholidayhours."',
						'" . $publicholidayamount . "',
						'" . $premiumamount . "',
						'" . $totalstayaway. "',
						'" . $calloutamount. "',
						'" . $leaveallowancestech. "',
						'" . $sickleavetech. "',
						'" . $blleavetech . "',
						'" . $annualleavetech. "',
						'" . $maternityleavetech. "',
						'" . $paternityleavetech. "',
						'" . $familycareleavetech. "',
						'" . $manualallowances . "',
						'" . $nonfnpfmanualallowances . "',
						'" . $mealtotalvalue . "',
						'" . $thisbonusamount . "',
						'" . $totalgrosspay. "',
						'52',
						'" . $thispayperiod . "',
						'" . $annualincome . "',
						'" . $annualincomebonus . "',
						'" . $annualincometax . "',
						'" . $annualsrt . "',
						'" . $annualecal . "',
						'" . ($annualincometax + $annualsrt +$annualecal). "',
						'" . $incometax. "',
						'" . $thispaysrt . "',
						'" . $thispayecal . "',
						'" . ($incometax + $thispaysrt+$thispayecal)  . "',
						'" . $fnpfemployee_amount. "',
						'" . $fnpfemployer_amount . "',
						'" . $fixeddeductions . "',
						'" . $manualdeductions. "',
						'" . ($totalgrosspay + $mealtotalvalue + $nonfnpfmanualallowances + $totalstayaway + $calloutamount - $incometax - $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 + $totalstayaway + $calloutamount + $nonfnpfmanualallowances   - $thisincometaxtopay - $thispaysrt -$thispayecal - $fnpfemployee_amount-$fixeddeductions-$manualdeductions;
			
			$ytd_gross = $myrow41['ytd_gross'] + $totalgrosspay;
			$ytdpay = $ytdpay + $netpay;
			$ytd_income_tax_withheld = $ytd_income_tax_withheld + round($annualincometax/52,2);
			$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;
		
	//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 
			
			//fnpf
		
			
	//insert in detail pay table
			
			//get sum of all premium total and save in parent table-payrun_details
		
		$sql39 = "UPDATE employeemaster SET ytd_gross = '".$ytd_gross."',
											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);
			
	
			
				$leaveallowancestech= 0;
			$sickleavetech =0;
			$annualleavetech =0;
			$blleavetech =0;
			$maternityleavetech= 0;
			$paternityleavetech =0;
			$familycareleavetech =0;
			
				$leaveallowances2tech= 0;
			$sickleave2tech =0;
			$annualleave2tech =0;
			$blleave2tech =0;
			$maternityleave2tech= 0;
			$paternityleave2tech =0;
			$familycareleave2tech =0;
			
			
		}	//end of pay loop end of all pay for each person who was in a timesheet
		
		
		//update status

 $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();
		
	}//payid end

		
		

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;
}
	
?>
here are no reports to show!') . '</li>';
	}
	return $RptLinks;
}
	
?>
