<?php
function checkpublicholiday($postdata,$xcrud,$techtimesheetdetails){
	
	
	//get monday to next Monday hours and date
	
	$id = $postdata->get('techtimeid');
			
	$db97 = Xcrud_db::get_instance();
			
	    $query = "SELECT mon_hrs,
						 mondate,
						 tues_hrs,
						 tuesdate,
						 wed_hrs,
						 weddate,
						 thurs_hrs,
						 thursdate,
						 fri_hrs,
						 fridate,
						 sat_hrs,
						 satdate,
						 sun_hrs,
						 sundate,
						 mon_hrs_2,
						 mondate2
					  
		          FROM techniciantimesheetdetails
				  				  
				   WHERE techtimedetailid = '". $id."'
				   ";
        
        $db97->query($query);
        $out1 = $db97->result();
        foreach($out1 as $output1){
			$mondayhours=$output1['mon_hrs'];
			$mondaydate=$output1['mondate'];
			
			$tuesdayhours=$output1['tues_hrs'];
			$tuesdaydate=$output1['tuesdate'];
			
			$wednesdayhours=$output1['wed_hrs'];
			$wednesdaydate=$output1['weddate'];
			
			$thursdayhours=$output1['thurs_hrs'];
			$thursdaydate=$output1['thursdate'];
			
			$fridayhours=$output1['fri_hrs'];
			$fridaydate=$output1['fridate'];
			
			$saturdayhours=$output1['sat_hrs'];
			$saturdaydate=$output1['satdate'];
			
			$sundayhours=$output1['sun_hrs'];
			$sundaydate=$output1['sundate'];
			
			$mondayhours2=$output1['mon_hrs_2'];
			$mondaydate2=$output1['mondate2'];
			
			
		}
	
	
	//check if monday is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $mondaydate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $mondaydate) {
	
		$monnormal = '0' ;
		$montimeandhalf = '0';
		$mondouble = $mondayhours;
		
	}
	elseif ($holiday = '') {
	    if ($mondayhours <= '8')  {
		
		$monnormal = $mondayhours;
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($mondayhours > '8') &&($mondayhours <= '12')) {
		
		$montimeandhalfhours = ($mondayhours - '8');
		$monnormal = '8';
		$mondouble = '0';
		}
	
	
		if ($mondayhours >'12')  {
	  
	    $monnormal = '8';
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($mondayhours - $monnormal );
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
		
		
	}
	
		//check if tuesday is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $tuesdaydate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $tuesdaydate) {
	
		$tuesnormal = '0' ;
		$tuestimeandhalf = '0';
		$tuesdouble = $tuesdayhours;
		
	}
	elseif ($holiday = '') {
	    if ($tuesdayhours <= '8')  {
		
		$tuesnormal = $tuesdayhours;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdayhours > '8') &&($tuesdayhours <= '12')) {
		
		$tuestimeandhalfhours = ($tuesdayhours - '8');
		$tuesnormal = '8';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdayhours >'12')  {
	  
	    $tuesnormal = '8';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdayhours - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		
		
	}
	
		//check if wednesday is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $wednesdaydate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $wednesdaydate) {
	
		$wednormal = '0' ;
		$wedtimeandhalf = '0';
		$weddouble = $wednesdayhours;
		
	}
	elseif ($holiday = '') {
	    if ($wednesdayhours <= '8')  {
		
		$wednormal = $wednesdayhours;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdayhours > '8') &&($wednesdayhours <= '12')) {
		
		$wedtimeandhalfhours = ($wednesdayhours - '8');
		$wednormal = '8';
		$weddouble = '0';
		}
	
	
		if ($wednesdayhours >'12')  {
	  
	    $wednormal = '8';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdayhours - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		
		
	}
	
	//check if thursday is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $thursdaydate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $thursdaydate) {
	
		$thursnormal = '0' ;
		$thurstimeandhalf = '0';
		$thursdouble = $thursdayhours;
		
	}
	elseif ($holiday = '') {
	    if ($thursdayhours <= '8')  {
		
		$thursnormal = $thursdayhours;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdayhours> '8') &&($thursdayhours <= '12')) {
		
		$thurstimeandhalfhours = ($thursdayhours - '8');
		$thursnormal = '8';
		$thursdouble = '0';
		}
	
	
		if ($thursdayhours >'12')  {
	  
	    $thursnormal = '8';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdayhours - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
		
	}
		//check if Friday is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $fridaydate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $fridaydate) {
	
		$frinormal = '0' ;
		$fritimeandhalf = '0';
		$fridouble = $fridayhours;
		
	}
	elseif ($holiday = '') {
	    if ($fridayhours <= '8')  {
		
		$frinormal = $fridayhours;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($fridayhours > '8') &&($fridayhours <= '12')) {
		
		$fritimeandhalfhours = ($fridayhours- '8');
		$frinormal = '8';
		$fridouble = '0';
		}
	
	
		if ($fridayhours >'12')  {
	  
	    $frinormal = '8';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($fridayhours - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
		
		
	}
		//check if Sat is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $saturdaydate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $saturdaydate) {
	
		$satnormal = '0' ;
		$sattimeandhalf = '0';
		$satdouble = $satdayhours;
		
	}
	elseif ($holiday = '') {
	    if ($saturdayhours <= '8')  {
		
		$satnormal = $saturdayhours;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (($saturdayhours > '8') &&($saturdayhours <= '12')) {
		
		$sattimeandhalfhours = ($saturdayhours - '8');
		$satnormal = '8';
		$satdouble = '0';
		}
	
	
		if ($saturdayhours >'12')  {
	  
	    $satnormal = '8';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($saturdayhours - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		
		
	}
	
		//check if Sun is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $sundaydate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $sundaydate) {
	
		$sunnormal = '0' ;
		$suntimeandhalf = '0';
		$sundouble = $sundayhours;
		
	}
	elseif ($holiday = '') {
	    if ($sundayhours <= '8')  {
		
		$sunnormal = $sundayhours;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($sundayhours > '8') &&($sundayhours <= '12')) {
		
		$suntimeandhalfhours = ($sundayhours - '8');
		$sunnormal = '8';
		$sundouble = '0';
		}
	
	
		if ($sundayhours >'12')  {
	  
	    $sunnormal = '8';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($sundayhours - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		}    
		
		
	}
	
		//check if Mon 2 is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $mondaydate2."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $mondaydate2) {
	
		$monnormal2 = '0' ;
		$montimeandhalf2 = '0';
		$mondouble2 = $mondayhours2;
		
	}
	elseif ($holiday = '') {
	    if ($mondayhours2 <= '8')  {
		
		$monnormal2 = $mondayhours2;
		$montimeandhalfhours2 = '0';
		$mondouble2 = '0';
		
     	}
	
		if (($mondayhours2 > '8') &&($mondayhours2 <= '12')) {
		
		$montimeandhalfhours2 = ($mondayhours2 - '8');
		$monnormal2 = '8';
		$mondouble2 = '0';
		}
	
	
		if ($mondayhours2 >'12')  {
	  
	    $monnormal2 = '8';
		$montimeandhalfhours2 = '4';
	  
		$totalovertimehours = ($mondayhours2 - $monnormal2 );
	  	   
	    
	    $mondouble2 = $totalovertimehours - $montimeandhalfhours2;
	  
	  
		}    
		
		
	}
	
	$totalnormalhours = $monnormal +$tuesnormal+$wednormal+$thursnormal+$frinormal+$satnormal+$sunnormal+$monnormal2;
	$totaltimeandhalf = $montimeandhalfhours + $tuestimeandhalfhours +$wedtimeandhalfhours+$thurstimeandhalfhours+$fritimeandhalfhours+$sattimeandhalfhours+$suntimeandhalfhours+$montimeandhalfhours2 ;
	
	$totaldouble = $mondouble+$tuesdouble+$weddouble+$thursdouble+$fridouble+$satdouble+$sundouble+$mondouble2;
	
	$postdata->set('normal_hours', $totalnormalhours);
	 $postdata->set('time_half_hours', $totaltimeandhalf);
	 $postdata->set('double_hours', $totaldouble);
 
	
	
}

function getdata($postdata,$xcrud){

	$jobno= $postdata->get('job_no');
   
$db41 = Xcrud_db::get_instance();
	    $query = "SELECT 
		             customer,
					 account_number,
					 department
		               
					  
		          FROM projectdetails
				    
				   WHERE projectid = '" . $jobno . "'";
        
        $db41->query($query);
        $out41 = $db41->result();
        foreach($out41 as $output41){
			
			$customer=$output41['customer'];
			$account=$output41['account_number'];
			$depart=$output41['department'];
			$postdata->set('account_number', $account);
			$postdata->set('department', $depart);
			$db42= Xcrud_db::get_instance();
			
	     $query = "SELECT 
		             name
		               
					  
		          FROM debtorsmaster
				    
				   WHERE debtorno = '" . $customer . "'";
        
        $db42->query($query);
        $out42 = $db42->result();
        foreach($out42 as $output42){
			
			$customername=$output42['name'];
			
			$postdata->set('client', $customername);	
            
        }
		
        } 
$userid = $_SESSION['UserID'];
	
$postdata->set('last_modifiedby', $userid);
	
}

function highlightcell($xcrud,$techtimesheetdetails){
	
	//monday cell

$db15 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM techniciantimesheetdetails
				  INNER JOIN shifts
				  ON  techniciantimesheetdetails.mon_shift = shifts.shiftid";
				  	 	   
        
        $db15->query($query);
        $out8 = $db15->result();
        foreach($out8 as $output8){
            $mon_shift = $output8['shiftid'];
			$mon_hrs = $output8['hours'];
			$mon_color = $output8['color'];


$techtimesheetdetails->highlight('mon_shift','=',$mon_shift,$mon_color);
$techtimesheetdetails->highlight('mon_hrs','=',$mon_hrs,$mon_color);


		} 

 
//tuesday cell

$db16 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM techniciantimesheetdetails
				  INNER JOIN shifts
				  ON  techniciantimesheetdetails.tues_shift = shifts.shiftid";
				  	 	   
        
        $db16->query($query);
        $out9 = $db16->result();
        foreach($out9 as $output9){
            $tues_shift = $output9['shiftid'];
			$tues_hrs = $output9['hours'];
			$tues_color = $output9['color'];


$techtimesheetdetails->highlight('tues_shift','=',$tues_shift,$tues_color);
$techtimesheetdetails->highlight('tues_hrs','=',$tues_hrs,$tues_color);


		} 

//wed cell

$db17 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM techniciantimesheetdetails
				  INNER JOIN shifts
				  ON  techniciantimesheetdetails.wed_shift = shifts.shiftid";
				  	 	   
        
        $db17->query($query);
        $out10 = $db17->result();
        foreach($out10 as $output10){
            $wed_shift = $output10['shiftid'];
			$wed_hrs = $output10['hours'];
			$wed_color = $output10['color'];


$techtimesheetdetails->highlight('wed_shift','=',$wed_shift,$wed_color);
$techtimesheetdetails->highlight('wed_hrs','=',$wed_hrs,$wed_color);


		} 

//thurs cell

$db18 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM techniciantimesheetdetails
				  INNER JOIN shifts
				  ON  techniciantimesheetdetails.thurs_shift = shifts.shiftid";
				  	 	   
        
        $db18->query($query);
        $out11 = $db18->result();
        foreach($out11 as $output11){
            $thurs_shift = $output11['shiftid'];
			$thurs_hrs = $output11['hours'];
			$thurs_color = $output11['color'];


$techtimesheetdetails->highlight('thurs_shift','=',$thurs_shift,$thurs_color);
$techtimesheetdetails->highlight('thurs_hrs','=',$thurs_hrs,$thurs_color);


		} 


//friday cell

$db19 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM techniciantimesheetdetails
				  INNER JOIN shifts
				  ON  techniciantimesheetdetails.fri_shift = shifts.shiftid";
				  	 	   
        
        $db19->query($query);
        $out12 = $db19->result();
        foreach($out12 as $output12){
            $fri_shift = $output12['shiftid'];
			$fri_hrs = $output12['hours'];
			$fri_color = $output12['color'];


$techtimesheetdetails->highlight('fri_shift','=',$fri_shift,$fri_color);
$techtimesheetdetails->highlight('fri_hrs','=',$fri_hrs,$fri_color);


		} 



//Sat cell

$db20 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM techniciantimesheetdetails
				  INNER JOIN shifts
				  ON  techniciantimesheetdetails.sat_shift = shifts.shiftid";
				  	 	   
        
        $db20->query($query);
        $out13 = $db20->result();
        foreach($out13 as $output13){
            $sat_shift = $output13['shiftid'];
			$sat_hrs = $output13['hours'];
			$sat_color = $output13['color'];


$techtimesheetdetails->highlight('sat_shift','=',$sat_shift,$sat_color);
$techtimesheetdetails->highlight('sat_hrs','=',$sat_hrs,$sat_color);


		} 

//Sun cell

$db21 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM techniciantimesheetdetails
				  INNER JOIN shifts
				  ON  techniciantimesheetdetails.sun_shift = shifts.shiftid";
				  	 	   
        
        $db21->query($query);
        $out14 = $db21->result();
        foreach($out14 as $output14){
            $sun_shift = $output14['shiftid'];
			$sun_hrs = $output14['hours'];
			$sun_color = $output14['color'];


$techtimesheetdetails->highlight('sun_shift','=',$sun_shift,$sun_color);
$techtimesheetdetails->highlight('sun_hrs','=',$sun_hrs,$sun_color);


		} 
//monday 2 cell

$db89 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM techniciantimesheetdetails
				  INNER JOIN shifts
				  ON  techniciantimesheetdetails.mon_shift_2 = shifts.shiftid";
				  	 	   
        
        $db89->query($query);
        $out89 = $db89->result();
        foreach($out89 as $output89){
            $mon_shift_2 = $output89['shiftid'];
			$mon_hrs_2 = $output89['hours'];
			$mon_color_2 = $output89['color'];


$techtimesheetdetails->highlight('mon_shift_2','=',$mon_shift_2,$mon_color_2);
$techtimesheetdetails->highlight('mon_hrs_2','=',$mon_hrs_2,$mon_color_2);


		} 


}


function calculate_totalhoursperweek($postdata,$techtimesheetdetails,$xcrud){


$employeeid = $postdata->get('employeeid');
	
	
	
	

	
//monday 
	
	$montime_in = $postdata->get('montime_in');
	$montime_out = $postdata->get('montime_out');
	
	
	
	
	
	$monhours = abs(strtotime($montime_in) - strtotime($montime_out)) / 3600;
		
    $postdata->set('mon_hrs', $monhours);
	
	$montime_in1 = $postdata->get('montime_in1');
	$montime_out1 = $postdata->get('montime_out1');
	
	//tuesday
	
	$tuestime_in = $postdata->get('tuestime_in');
	$tuestime_out = $postdata->get('tuestime_out');
	
	$tueshours = abs(strtotime($tuestime_in) - strtotime($tuestime_out)) / 3600;
		
    $postdata->set('tues_hrs', $tueshours);
	
	$tuestime_in1 = $postdata->get('tuestime_in1');
	$tuestime_out1 = $postdata->get('tuestime_out1');
	
	
	
	
	$tueshours2 = abs(strtotime($tuestime_in1) - strtotime($tuestime_out1)) / 3600;
		
    $postdata->set('tues_hrs2', $tueshours2);
	
	$tuesdaytotal = $tueshours +$tueshours2;
	
	   $postdata->set('tues_total', $tuesdaytotal);
	
	//wednesday 
	
	$wedtime_in = $postdata->get('wedtime_in');
	$wedtime_out = $postdata->get('wedtime_out');
	
	$wedhours = abs(strtotime($wedtime_in) - strtotime($wedtime_out)) / 3600;
		
    $postdata->set('wed_hrs', $wedhours);
	
	$wedtime_in1 = $postdata->get('wedtime_in1');
	$wedtime_out1 = $postdata->get('wedtime_out1');
	
	
	$shift2passed8hoursrest2 = $wedtime_out + '08:00:00';
	
	
	
	$wedhours2 = abs(strtotime($wedtime_in1) - strtotime($wedtime_out1)) / 3600;
		
    $postdata->set('wed_hrs2', $wedhours2);
	
	$wednesdaytotal= $wedhours +$wedhours2;
	
	   $postdata->set('wed_total', $wednesdaytotal);
	
	//thursday
	
	$thurstime_in = $postdata->get('thurstime_in');
	$thurstime_out = $postdata->get('thurstime_out');
	
	$thurshours = abs(strtotime($thurstime_in) - strtotime($thurstime_out)) / 3600;
		
    $postdata->set('thurs_hrs', $thurshours);
	
	$thurstime_in1 = $postdata->get('thurstime_in1');
	$thurstime_out1 = $postdata->get('thurstime_out1');
	

	$shift2passed8hoursrest3 = $thurstime_out + '08:00:00';
	
	
	
	$thurshours2 = abs(strtotime($thurstime_in1) - strtotime($thurstime_out1)) / 3600;
		
    $postdata->set('thurs_hrs2', $thurshours2);
	
	$thursdaytotal = $thurshours +$thurshours2;
	
	   $postdata->set('thurs_total', $thursdaytotal);
	
	//Friday
	
	$fritime_in = $postdata->get('fritime_in');
	$fritime_out = $postdata->get('fritime_out');
	
	$frihours = abs(strtotime($fritime_in) - strtotime($fritime_out)) / 3600;
		
    $postdata->set('fri_hrs', $frihours);
	
	$fritime_in1 = $postdata->get('fritime_in1');
	$fritime_out1 = $postdata->get('fritime_out1');
	
	
	
		
	$shift2passed8hoursrest4 = $fritime_out + '08:00:00';
	

	$frihours2 = abs(strtotime($fritime_in1) - strtotime($fritime_out1)) / 3600;
		
    $postdata->set('fri_hrs2', $frihours2);
	
	$fridaytotal = $frihours +$frihours2;
	
	   $postdata->set('fri_total', $fridaytotal);
	
	
	//sat
	  
	$sattime_in = $postdata->get('sattime_in');
	$sattime_out = $postdata->get('sattime_out');
	
	$sathours = abs(strtotime($sattime_in) - strtotime($sattime_out)) / 3600;
		
    $postdata->set('sat_hrs', $sathours);
	
	$sattime_in1 = $postdata->get('sattime_in1');
	$sattime_out1 = $postdata->get('sattime_out2');
	
	
		
	$sathours5 = abs(strtotime($sattime_in1) - strtotime($sattime_out1)) / 3600;
		
	//if ($sathours5 > '20') {
		
		//$sathours5 = '0';
	//}
	
	
		
    $postdata->set('sat_hrs2', $sathours5);
	
	
	$sattotal = $sathours + $sathours5;
	
	   $postdata->set('sat_total', $sattotal);
	
	
	//sunday
	
	$suntime_in = $postdata->get('sunttime_in');
	$suntime_out = $postdata->get('sunttime_out');
	
	$sunhours = abs(strtotime($suntime_in) - strtotime($suntime_out)) / 3600;
		
    $postdata->set('sun_hrs', $sunhours);
	
	$suntime_in1 = $postdata->get('suntime_in1');
	$suntime_out1 = $postdata->get('suntime_out1');
	
	$sunhours2 = abs(strtotime($suntime_in1) - strtotime($suntime_out1)) / 3600;
		
    $postdata->set('sun_hrs2', $sunhours2);
	
	$suntotal = $sunhours +$sunhours2;
	
	   $postdata->set('sun_total', $suntotal);
	
	
	//Monday 2 till cut over dates
	
	$mon2time_in = $postdata->get('mon2time_in');
	$mon2time_out = $postdata->get('mon2time_out');
	
	$mon2hours = abs(strtotime($mon2time_in) - strtotime($mon2time_out)) / 3600;
		
    $postdata->set('mon2_total', $mon2hours);
	
	
	if ($mon2time_out > '08:00:00'){
		
		$techtimesheetdetails->set_exception('mon2time_out','Cant extend hours after 8a.m!','success'); 
	}
	
	$monhours2 = abs(strtotime($montime_in1) - strtotime($montime_out1)) / 3600;
		
    $postdata->set('mon_hrs2', $monhours2);
	
	$mondaytotal = $monhours +$monhours2;
	
	   $postdata->set('mon_total', $mondaytotal);
	
	
	//mon
		
	$mondate =  date('Y-m-d ',strtotime($start));
	 $postdata->set('mon_date', $mondate);
	
	//tues
	$tuesdate =  date('Y-m-d ',strtotime('+24 hour',strtotime($start)));
	
	 $postdata->set('tues_date', $tuesdate);
	
	//wed
	$weddate =  date('Y-m-d ',strtotime('+48 hour',strtotime($start)));
	
	 $postdata->set('wed_date', $weddate);
	
		//thurs
	$thursdate =  date('Y-m-d ',strtotime('+72 hour',strtotime($start)));
	
	 $postdata->set('thurs_date', $thursdate);
	
		//fri
	$fridate =  date('Y-m-d ',strtotime('+96 hour',strtotime($start)));
	
	 $postdata->set('fri_date', $fridate);
		//sat
	$satdate =  date('Y-m-d ',strtotime('+120 hour',strtotime($start)));
	
	 $postdata->set('sat_date', $satdate);
		//sun
	$sundate =  date('Y-m-d ',strtotime('+144 hour',strtotime($start)));
	
	 $postdata->set('sun_date', $sundate);
		//mon 2
	$mon2date =  date('Y-m-d ',strtotime('+168 hour',strtotime($start)));
	
	 $postdata->set('mon2_date', $mon2date);
	
	
	
	 $db = Xcrud_db::get_instance();
	    $query = "SELECT contract,
		              max_hours_to_work
					  
		          FROM employeemaster
				  				  
				   WHERE employeeid = '".$employeeid."'";
        
        $db->query($query);
        $outm = $db->result();
        foreach($outm as $outputm){
			$contract = $outputm['contract'];
            $maxhours = $outputm['max_hours_to_work'];
			
        } 
	
	
	
	$nine = '9';
	
	
		
if ($contract == $nine ) {
	
	if ($mondaytotal <= '9')  {
		
		$monnormal = $mondaytotal;
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($mondaytotal > '9') &&($mondaytotal <= '13')) {
		
		$montimeandhalfhours = ($mondaytotal - '9');
		$monnormal = '9';
		$mondouble = '0';
		}
	
	
		if ($mondaytotal >'13')  {
	  
	    $monnormal = '9';
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($mondaytotal- $monnormal );
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
	
	 if ($tuesdaytotal <= '9')  {
		
		$tuesnormal = $tuesdaytotal;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdaytotal > '9') &&($tuesdaytotal <= '13')) {
		
		$tuestimeandhalfhours = ($tuesdayhours - '9');
		$tuesnormal = '9';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdaytotal >'13')  {
	  
	    $tuesnormal = '9';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdaytotal - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		 if ($wednesdaytotal <= '9')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '9') &&($wednesdaytotal<= '13')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '9');
		$wednormal = '9';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal>'13')  {
	  
	    $wednormal = '9';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		
		if ($thursdaytotal <= '9')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '9') &&($thursdaytotal <= '13')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '9');
		$thursnormal = '9';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'13')  {
	  
	    $thursnormal = '9';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
		
	 if ($fridaytotal <= '9')  {
		
		$frinormal = $fridaytotal ;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($fridaytotal  > '9') &&($fridaytotal  <= '13')) {
		
		$fritimeandhalfhours = ($fridaytotal  - '9');
		$frinormal = '9';
		$fridouble = '0';
		}
	
	
		if ($fridaytotal  >'13')  {
	  
	    $frinormal = '9';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($fridaytotal - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
		if ($sattotal  <= '9')  {
		
		$satnormal = $sattotal ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (($sattotal > '9') &&($sattotal  <= '13')) {
		
		$sattimeandhalfhours = ($saturdayhours - '9');
		$satnormal = '9';
		$satdouble = '0';
		}
	
	
		if ($sattotal  >'13')  {
	  
	    $satnormal = '9';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($sattotal  - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		if ($suntotal <= '9')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '9') &&($suntotal <= '13')) {
		
		$suntimeandhalfhours = ($suntotal - '9');
		$sunnormal = '9';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'13')  {
	  
	    $sunnormal = '9';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		}    
		if ($mon2hours <= '9')  {
		
		$monnormal2 = $mon2hours;
		$montimeandhalfhours2 = '0';
		$mondouble2 = '0';
		
     	}
	
		if (($mon2hours > '9') &&($mon2hours <= '13')) {
		
		$montimeandhalfhours2 = ($mon2hours - '9');
		$monnormal2 = '9';
		$mondouble2 = '0';
		}
	
	
		if ($mon2hours >'13')  {
	  
	    $monnormal2 = '9';
		$montimeandhalfhours2 = '4';
	  
		$totalovertimehours = ($mon2hours - $monnormal2 );
	  	   
	    
	    $mondouble2 = $totalovertimehours - $montimeandhalfhours2;
	  
	  
		}
   
		 
	
        $totalhours = $mondaytotal+$tuesdaytotal+$wednesdaytotal+$thursdaytotal+$fridaytotal+$sattotal+$suntotal+$mon2hours;
	
		$postdata->set('total_hrs', $totalhours);
	
		$totalnormalhours = $monnormal +$tuesnormal+$wednormal+$thursnormal+$frinormal+$satnormal+$sunnormal+$monnormal2;
	$totaltimeandhalf = $montimeandhalfhours + $tuestimeandhalfhours +$wedtimeandhalfhours+$thurstimeandhalfhours+$fritimeandhalfhours+$sattimeandhalfhours+$suntimeandhalfhours+$montimeandhalfhours2 ;
	
	$totaldouble = $mondouble+$tuesdouble+$weddouble+$thursdouble+$fridouble+$satdouble+$sundouble+$mondouble2;
	
	$postdata->set('normal_hours', $totalnormalhours);
	 $postdata->set('time_half_hours', $totaltimeandhalf);
	 $postdata->set('double_hours', $totaldouble);
	 //$postdata->set('holiday', $holiday);
	
	
	if ($totalhours > $maxhours){
    
    $techtimesheetdetails->set_exception('total_hrs','Hours Exceeded!','error'); 
		}
 
	 

 
      

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

if ($mondaytotal <= '8')  {
		
		$monnormal = $mondaytotal;
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($mondaytotal > '8') &&($mondaytotal <= '12')) {
		
		$montimeandhalfhours = ($mondaytotal - '8');
		$monnormal = '8';
		$mondouble = '0';
		}
	
	
		if ($mondaytotal >'12')  {
	  
	    $monnormal = '8';
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($mondaytotal- $monnormal );
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
	
	 if ($tuesdaytotal <= '8')  {
		
		$tuesnormal = $tuesdaytotal;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdaytotal > '8') &&($tuesdaytotal <= '12')) {
		
		$tuestimeandhalfhours = ($tuesdayhours - '8');
		$tuesnormal = '8';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdaytotal >'12')  {
	  
	    $tuesnormal = '8';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdaytotal - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		 if ($wednesdaytotal <= '8')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '8') &&($wednesdaytotal<= '12')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '8');
		$wednormal = '8';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal>'12')  {
	  
	    $wednormal = '8';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		
		if ($thursdaytotal <= '8')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '8') &&($thursdaytotal <= '12')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '8');
		$thursnormal = '8';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'12')  {
	  
	    $thursnormal = '8';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
		
	 if ($fridaytotal <= '8')  {
		
		$frinormal = $fridaytotal ;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($fridaytotal  > '8') &&($fridaytotal  <= '12')) {
		
		$fritimeandhalfhours = ($fridaytotal  - '8');
		$frinormal = '8';
		$fridouble = '0';
		}
	
	
		if ($fridaytotal  >'12')  {
	  
	    $frinormal = '8';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($fridaytotal - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
		if ($sattotal  <= '8')  {
		
		$satnormal = $sattotal ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (($sattotal > '8') &&($sattotal  <= '12')) {
		
		$sattimeandhalfhours = ($saturdayhours - '8');
		$satnormal = '8';
		$satdouble = '0';
		}
	
	
		if ($sattotal  >'12')  {
	  
	    $satnormal = '8';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($sattotal  - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		if ($suntotal <= '8')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '8') &&($suntotal <= '12')) {
		
		$suntimeandhalfhours = ($suntotal - '8');
		$sunnormal = '8';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'12')  {
	  
	    $sunnormal = '8';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		}    
		if ($mon2hours <= '8')  {
		
		$monnormal2 = $mon2hours;
		$montimeandhalfhours2 = '0';
		$mondouble2 = '0';
		
     	}
	
		if (($mon2hours > '8') &&($mon2hours <= '12')) {
		
		$montimeandhalfhours2 = ($mon2hours - '8');
		$monnormal2 = '8';
		$mondouble2 = '0';
		}
	
	
		if ($mon2hours >'12')  {
	  
	    $monnormal2 = '8';
		$montimeandhalfhours2 = '4';
	  
		$totalovertimehours = ($mon2hours - $monnormal2 );
	  	   
	    
	    $mondouble2 = $totalovertimehours - $montimeandhalfhours2;
	  
	  
		}


$totalhours = $mondaytotal+$tuesdaytotal+$wednesdaytotal+$thursdaytotal+$fridaytotal+$sattotal+$suntotal+$mon2hours;
	
		$postdata->set('total_hrs', $totalhours);
	
		$totalnormalhours = $monnormal +$tuesnormal+$wednormal+$thursnormal+$frinormal+$satnormal+$sunnormal+$monnormal2;
	$totaltimeandhalf = $montimeandhalfhours + $tuestimeandhalfhours +$wedtimeandhalfhours+$thurstimeandhalfhours+$fritimeandhalfhours+$sattimeandhalfhours+$suntimeandhalfhours+$montimeandhalfhours2 ;
	
	$totaldouble = $mondouble+$tuesdouble+$weddouble+$thursdouble+$fridouble+$satdouble+$sundouble+$mondouble2;
	
	$postdata->set('normal_hours', $totalnormalhours);
	 $postdata->set('time_half_hours', $totaltimeandhalf);
	 $postdata->set('double_hours', $totaldouble);
	 //$postdata->set('holiday', $holiday);
	

      
       
		
	if ($totalhours > $maxhours){
    
    $techtimesheetdetails->set_exception('total_hrs','Hours Exceeded!','error'); 
		}
		
}

		
	
	}

	
	
	

 	 
