<?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);	
            
        }
		
        } 


	
}

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){

//save daily dates for future reference in checking public holiday
	
	$techtimeid = $postdata->get('techtimeid');
	$db99 = Xcrud_db::get_instance();
	    $query = "SELECT start_date,
						 end_date
					  
		          FROM techniciantimesheet
				  				  
				   WHERE techtimeid = '" . $techtimeid  . "'";
        
        $db99->query($query);
        $out = $db99->result();
        foreach($out as $output){
			$startdate=$output['start_date'];
            $enddate = $output['end_date'];
			
			
		}
			
             $postdata->set('mondate', $startdate);
	
			$tuesdate = strtotime("+1 day", strtotime($startdate));
	        $tuesdate = date("Y-m-d", $tuesdate);
			$postdata->set('tuesdate', $tuesdate);
	
	
			$weddate = strtotime("+2 day", strtotime($startdate));
	$weddate = date("Y-m-d", $weddate);
	$postdata->set('weddate', $weddate);
	
			$thursdate = strtotime("+3 day", strtotime($startdate));
	$thursdate = date("Y-m-d", $thursdate);
	$postdata->set('thursdate', $thursdate);
	
			$fridate = strtotime("+4 day", strtotime($startdate));
	$fridate = date("Y-m-d", $fridate);
	$postdata->set('fridate', $fridate);
	
			$satdate = strtotime("+5 day", strtotime($startdate));
	$satdate = date("Y-m-d", $satdate);
	$postdata->set('satdate', $satdate);

		
	
			$sundate = strtotime("+6 day", strtotime($startdate));
	$sundate = date("Y-m-d", $sundate);
	$postdata->set('sundate', $sundate);
	
			$mondate2 = strtotime("+7 day", strtotime($startdate));
	$mondate2 = date("Y-m-d", $mondate2);
		$postdata->set('mondate2', $mondate2);

//check whether public holiday falls in one of this days.
	
	
	
//mon
 	
$m_shift = $postdata->get('mon_shift');
if (!isset($m_shift))
    {
         $m_shift = 'None';
		
    }
   
$db1 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		                 hours,
		                 color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $m_shift . "'";
        
        $db1->query($query);
        $out = $db1->result();
        foreach($out as $output){
			$mondayshift=$output['shiftid'];
            $mondayhours = $output['hours'];
			$moncolor = $output['color'];
        } 

$postdata->set('mon_hrs', $mondayhours);
	
	//check if monday is holiday
	//$db96 = Xcrud_db::get_instance();
			
	  //  $query = "SELECT date
		//			  
		    //      FROM publicholidaycalender
				  				  
			//	   WHERE date = '". $startdate."'
				///   ";
        
       // $db96->query($query);
       // $out2 = $db96->result();
       // foreach($out2 as $output2){
		//	$holiday=$output2['date'];
			
		//}
	if ($holiday = $startdate) {
	
		$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;
	  
	  
		}
		
		
	}
	
//$xcrud->pass_var('mon_color', $moncolor);


//$techtimesheetdetails->highlight('mon_shift','=',$mondayshift,$moncolor);
//$techtimesheetdetails->highlight('mon_hrs','=',$monhours,$moncolor);

//tuesday

$t_shift = $postdata->get('tues_shift');
if (!isset($t_shift))
    {
         $t_shift = 'None';
		
    }
   
$db2 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		                  hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $t_shift . "'";
        
        $db2->query($query);
        $out = $db2->result();
        foreach($out as $output){
			$tuesdayshift=$output['shiftid'];
            $tuesdayhours = $output['hours'];
			$tuescolor = $output['color'];
        } 

$postdata->set('tues_hrs', $tuesdayhours);
	
	//check if tuesday is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $tuesdate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $tuesdate) {
	
		$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;
	  
	  
		}
		
		
	}
	
	
//$xcrud->pass_var('tues_color', $tuescolor);
//$xcrud->highlight('tues_shift','=',$tuesdayshift,$tuescolor);
//$xcrud->highlight('tues_hrs','=',$tueshours ,$tuescolor);

//wednesday
$w_shift = $postdata->get('wed_shift');
	
		
   
   
$db3 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		               hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $w_shift . "'";
        
        $db3->query($query);
        $out = $db3->result();
        foreach($out as $output){
			$wednesdayshift=$output['shiftid'];
            $wednesdayhours = $output['hours'];
			$wedcolor = $output['color'];
        } 

$postdata->set('wed_hrs', $wednesdayhours);
		
			
	//check if wednesday is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $weddate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $weddate) {
	
		$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;
	  
	  
		}
		
		
	}
		
		
		
		
		
 //$xcrud->pass_var('wed_color', $wedcolor);

//$xcrud->highlight('wed_shift','=',$wednesdayshift,$wedcolor);
//$xcrud->highlight('wed_hrs','=',$wedhours ,$wedcolor);


//thurs
$th_shift = $postdata->get('thurs_shift');
   if (!isset($th_shift))
    {
         $th_shift = 'None';
		
    }
$db4 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		                 hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $th_shift . "'";
        
        $db4->query($query);
        $out = $db4->result();
        foreach($out as $output){
			$thursdayshift=$output['shiftid'];
            $thursdayhours = $output['hours'];
			$thurscolor = $output['color'];
        } 

$postdata->set('thurs_hrs', $thursdayhours);
		//check if thursday is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $thursdate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $thursdate) {
	
		$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;
	  
	  
		}
		
		
	}
			
		
		
		
//$xcrud->pass_var('thurs_color', $thurscolor);

//$xcrud->highlight('thurs_shift','=',$thursdayshift,$thurscolor);
//$xcrud->highlight('thurs_hrs','=',$thurshours ,$thurscolor);


//fri
$frid_shift = $postdata->get('fri_shift');
   if (!isset($frid_shift))
    {
         $frid_shift = 'None';
		
    }
$db5 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		               hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $frid_shift . "'";
        
        $db5->query($query);
        $out = $db5->result();
        foreach($out as $output){
			$fridayshift=$output['shiftid'];
            $frihours = $output['hours'];
			$fricolor = $output['color'];
        } 

$postdata->set('fri_hrs', $frihours);
		
		//check if Friday is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $fridate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $fridate) {
	
		$frinormal = '0' ;
		$fritimeandhalf = '0';
		$fridouble = $frihours;
		
	}
	elseif ($holiday = '') {
	    if ($fridayhours <= '8')  {
		
		$frinormal = $frihours;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($frihours > '8') &&($frihours <= '12')) {
		
		$fritimeandhalfhours = ($frihours- '8');
		$frinormal = '8';
		$fridouble = '0';
		}
	
	
		if ($frihours >'12')  {
	  
	    $frinormal = '8';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($frihours - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
		
		
	}	
		
		

//$xcrud->pass_var('fri_color', $fricolor);
//$xcrud->highlight('fri_shift','=',$fridayshift,$fricolor);
//$xcrud->highlight('fri_hrs','=',$frihours ,$fricolor);



//sat


$saturday_shift = $postdata->get('sat_shift');

   if (!isset($saturday_shift))
    {
         $saturday_shift = 'None';
		
    }
$db6 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		               hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $saturday_shift . "'";
        
        $db6->query($query);
        $out = $db6->result();
        foreach($out as $output){
			$saturdayshift=$output['shiftid'];
            $saturdayhours = $output['hours'];
			$satcolor = $output['color'];
        } 

$postdata->set('sat_hrs', $saturdayhours);
		
		//check if Sat is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $satdate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $satdaydate) {
	
		$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;
	  
	  
		}    
		
		
	}
//$xcrud->pass_var('sat_color', $satcolor);


//$xcrud->highlight('sat_shift','=',$saturdayshift,$satcolor);
//$xcrud->highlight('sat_hrs','=',$sathours ,$satcolor);



//sun

$s_shift = $postdata->get('s_shift');
  if (!isset($s_shift))
    {
         $s_shift = 'None';
		
    }
$db7 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		              hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $s_shift . "'";
        
        $db7->query($query);
        $out = $db7->result();
        foreach($out as $output){
			$sundayshift=$output['shiftid'];
            $sundayhours = $output['hours'];
			$suncolor = $output['color'];
        } 

$postdata->set('sun_hrs', $sundayhours);
		
			//check if Sun is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $sundate."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday = $sundate) {
	
		$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;
	  
	  
		}    
		
		
	}
		

	//mon 2
 	
$m_shift_2 = $postdata->get('mon_shift_2');
if (!isset($m_shift_2))
    {
         $m_shift_2 = 'None';
		
    }
   
$db117 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		                 hours,
		                 color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $m_shift_2 . "'";
        
        $db117->query($query);
        $out117 = $db117->result();
        foreach($out117 as $output117){
			$mondayshift2=$output['shiftid'];
            $mondayhours2 = $output['hours'];
			$moncolor2 = $output['color'];
        } 

$postdata->set('mon_hrs_2', $mondayhours2);
		
//check if Mon 2 is holiday
	$db96 = Xcrud_db::get_instance();
			
	    $query = "SELECT date
					  
		          FROM publicholidaycalender
				  				  
				   WHERE date = '". $mondate2."'
				   ";
        
        $db96->query($query);
        $out2 = $db96->result();
        foreach($out2 as $output2){
			$holiday=$output2['date'];
			
		}
	if ($holiday == $mondate2) {
	
		$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;
	  
	  
		}    
		
		
	}
	
//$xcrud->pass_var('sun_color', $suncolor);
//$xcrud->highlight('sun_shift','=',$sundayshift,$suncolor);
//$xcrud->highlight('sun_hrs','=',$sunhours ,$suncolor);



//insert color infor silently into techniciantimesheetdetails table




	
//$mon = $postdata->get('mon_hrs');
//$tues = $postdata->get('tues_hrs');
//$wed = $postdata->get('wed_hrs');
//$thurs = $postdata->get('thurs_hrs');
//$fri = $postdata->get('fri_hrs');
//$sat = $postdata->get('sat_hrs');
//$sun = $postdata->get('sun_hrs');

$totalhours = $mondayhours+$tuesdayhours+$wednesdayhours+$thursdayhours+$fridayhours+$saturdayhours+$sundayhours+$mondayhours2;
		
		$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);
	

 $postdata->set('total_hrs', $totalhours);
	 
 $employeeid = $postdata->get('employeeid');
 
        $db = Xcrud_db::get_instance();
	    $query = "SELECT 
		              max_hours_to_work
					  
		          FROM employeemaster
				  				  
				   WHERE employeeid = ".$employeeid;
        
        $db->query($query);
        $outm = $db->result();
        foreach($outm as $outputm){
            $maxhours = $outputm['max_hours_to_work'];
			
        } 
		
		if ($totalhours > $maxhours){
    
        $xcrud->set_exception('total_hrs','Hours Exceeded!','error'); 
		}
		
	
	}

	
	
	

 	 
