<?php
function displaydepartment($postdata,$xcrud){
	
	$aflid = $postdata->get('patrol_location');
   
$db40 = Xcrud_db::get_instance();
	    $query = "SELECT 
		             department
		               
					  
		          FROM aflpatrollocations
				    
				   WHERE aflid = '" . $aflid . "'";
        
        $db40->query($query);
        $out40 = $db40->result();
        foreach($out40 as $output40){
			
			$id=$output40['department'];
			
			$db41 = Xcrud_db::get_instance();
	    $query = "SELECT 
		             departmentname
		               
					  
		          FROM department
				    
				   WHERE departmentid = '" . $id . "'";
        
        $db41->query($query);
        $out41 = $db41->result();
        foreach($out41 as $output41){
			$department=$output41['departmentname'];
			
			
			
            
        } 
			
            
        } 

$postdata->set('department', $department);
	
}


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

$db15 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM aflrosteringtimesheet
				  INNER JOIN shifts
				  ON  aflrosteringtimesheet.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'];


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


		} 

 
//tuesday cell

$db16 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM aflrosteringtimesheet
				  INNER JOIN shifts
				  ON  aflrosteringtimesheet.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'];


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


		} 

//wed cell

$db17 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM aflrosteringtimesheet
				  INNER JOIN shifts
				  ON  aflrosteringtimesheet.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'];


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


		} 

//thurs cell

$db18 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM aflrosteringtimesheet
				  INNER JOIN shifts
				  ON  aflrosteringtimesheet.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'];


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


		} 


//friday cell

$db19 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM aflrosteringtimesheet
				  INNER JOIN shifts
				  ON  aflrosteringtimesheet.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'];


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


		} 



//Sat cell

$db20 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM aflrosteringtimesheet
				  INNER JOIN shifts
				  ON  aflrosteringtimesheet.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'];


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


		} 

//Sun cell

$db21 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM aflrosteringtimesheet
				  INNER JOIN shifts
				  ON  aflrosteringtimesheet.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'];


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


		} 
//monday 2 cell

$db89 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM aflrosteringtimesheet
				  INNER JOIN shifts
				  ON  aflrosteringtimesheet.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'];


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


		} 


}

function highlightcell55($primary, $xcrud){
$db12 = Xcrud_db::get_instance();
	    $query = "SELECT mon_shift,
						 mon_hrs,
		                 mon_color,
						 tues_shift,
					     tues_hrs,
		                 tues_color,
						 wed_shift,
						 wed_hrs,
						 wed_color,
						 thurs_shift,
						 thurs_hrs,
						 thurs_color,
						 fri_shift,
						 fri_hrs,
						 fri_color,
						 sat_shift,
						 sat_hrs,
						 sat_color,
						 sun_shift,
						 sun_hrs,
						 sun_color,
						 mon_hrs_2,
						 mon_color_2
					  
		          FROM aflrosteringtimesheet";
				  	 
				   
        
        $db12->query($query);
        $outl = $db12->result();
        foreach($outl as $outputl){
            $mon_shift = $outputl['mon_shift'];
			$mon_hrs = $outputl['mon_hrs'];
			$mon_color = $outputl['mon_color'];
			
			$tues_shift = $outputl['tues_shift'];
			$tues_hrs = $outputl['tues_hrs'];
			$tues_color = $outputl['tues_color'];
			
			$wed_shift = $outputl['wed_shift'];
			$wed_hrs = $outputl['wed_hrs'];
			$wed_color = $outputl['wed_color'];
			
			$thurs_shift = $outputl['thurs_shift'];
			$thurs_hrs = $outputl['thurs_hrs'];
			$thurs_color = $outputl['thurs_color'];
			
			$fri_shift = $outputl['fri_shift'];
			$fri_hrs = $outputl['fri_hrs'];
			$fri_color = $outputl['fri_color'];
			
			$sat_shift = $outputl['sat_shift'];
			$sat_hrs = $outputl['sat_hrs'];
			$sat_color = $outputl['sat_color'];
			
			$sun_shift = $outputl['sun_shift'];
			$sun_hrs = $outputl['sun_hrs'];
			$sun_color = $outputl['sun_color'];

		} 

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

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

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

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

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

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

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

	
}

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


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

	
//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_total', $monhours);
	

	
	//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_total', $tueshours);
	
	
	//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_total', $wedhours);
	
	
	
	
	
	//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_total', $thurshours);
	
	
	//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_total', $frihours);
	
	
	
	//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_total', $sathours);
	
	
	
	//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_total', $sunhours);
	
	
	//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'){
		
		$rostering_timesheet->set_exception('mon2time_out','Cant extend hours after 8a.m!','success'); 
	}
	

	
	
	
	//first check which this roster start and end date
	 $db2 = Xcrud_db::get_instance();
	    $query = "SELECT start_date, end_date,patrol_location
					  
		          FROM aflrostering
				  				  
				   WHERE aflrosterid = '".$rosterid."'";
        
        $db2->query($query);
        $outm2 = $db2->result();
        foreach($outm2 as $outputm2){
			$start = $outputm2['start_date'];
			$end = $outputm2['end_date'];
			
           			
        } 
	
	
	
	
	//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 ($monhours <= '9')  {
		
		$monnormal = $monhours;
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($monhours > '9') &&($monhours <= '13')) {
		
		$montimeandhalfhours = ($monhours - '9');
		$monnormal = '9';
		$mondouble = '0';
		}
	
	
		if ($monhours >'13')  {
	  
	    $monnormal = '9';
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($monhours- $monnormal );
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
	
	 if ($tueshours <= '9')  {
		
		$tuesnormal = $tueshours;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tueshours > '9') &&($tueshours <= '13')) {
		
		$tuestimeandhalfhours = ($tueshours - '9');
		$tuesnormal = '9';
		$tuesdouble = '0';
		}
	
	
		if ($tueshours >'13')  {
	  
	    $tuesnormal = '9';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tueshours - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		 if ($wedhours <= '9')  {
		
		$wednormal = $wedhours;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wedhours > '9') &&($wedhours<= '13')) {
		
		$wedtimeandhalfhours = ($wedhours- '9');
		$wednormal = '9';
		$weddouble = '0';
		}
	
	
		if ($wedhours>'13')  {
	  
	    $wednormal = '9';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wedhours - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		
		if ($thurshours <= '9')  {
		
		$thursnormal = $thurshours;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thurshours > '9') &&($thurshours <= '13')) {
		
		$thurstimeandhalfhours = ($thurshours - '9');
		$thursnormal = '9';
		$thursdouble = '0';
		}
	
	
		if ($thurshours >'13')  {
	  
	    $thursnormal = '9';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thurshours - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
		
	 if ($frihours <= '9')  {
		
		$frinormal = $frihours ;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($frihours  > '9') &&($frihours  <= '13')) {
		
		$fritimeandhalfhours = ($frihours  - '9');
		$frinormal = '9';
		$fridouble = '0';
		}
	
	
		if ($frihours  >'13')  {
	  
	    $frinormal = '9';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($frihours - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
		if ($sathours  <= '9')  {
		
		$satnormal = $sathours ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (($sathours > '9') &&($sathours  <= '13')) {
		
		$sattimeandhalfhours = ($sathours  - '9');
		$satnormal = '9';
		$satdouble = '0';
		}
	
	
		if ($sathours  >'13')  {
	  
	    $satnormal = '9';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($sathours  - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		if ($sunhours <= '9')  {
		
		$sunnormal = $sunhours;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($sunhours > '9') &&($sunhours <= '13')) {
		
		$suntimeandhalfhours = ($sunhours - '9');
		$sunnormal = '9';
		$sundouble = '0';
		}
	
	
		if ($sunhours >'13')  {
	  
	    $sunnormal = '9';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($sunhours - $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 = $monhours+$tueshours+$wedhours+$thurshours+$frihours+$sathours+$sunhours+$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){
    
    $rostering_timesheet->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 ($monhours <= '8')  {
		
		$monnormal = $monhours;
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($monhours > '8') &&($monhours <= '12')) {
		
		$montimeandhalfhours = ($monhours - '8');
		$monnormal = '8';
		$mondouble = '0';
		}
	
	
		if ($monhours >'12')  {
	  
	    $monnormal = '8';
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($monhours- $monnormal );
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
	
	 if ($tueshours <= '8')  {
		
		$tuesnormal = $tueshours;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tueshours > '8') &&($tueshours <= '12')) {
		
		$tuestimeandhalfhours = ($tueshours - '8');
		$tuesnormal = '8';
		$tuesdouble = '0';
		}
	
	
		if ($tueshours >'12')  {
	  
	    $tuesnormal = '8';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tueshours - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		 if ($wedhours <= '8')  {
		
		$wednormal = $wedhours;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wedhours > '8') &&($wedhours<= '12')) {
		
		$wedtimeandhalfhours = ($wedhours- '8');
		$wednormal = '8';
		$weddouble = '0';
		}
	
	
		if ($wedhours>'12')  {
	  
	    $wednormal = '8';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wedhours - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		
		if ($thurshours <= '8')  {
		
		$thursnormal = $thurshours;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thurshours > '8') &&($thurshours <= '12')) {
		
		$thurstimeandhalfhours = ($thurshours - '8');
		$thursnormal = '8';
		$thursdouble = '0';
		}
	
	
		if ($thurshours >'12')  {
	  
	    $thursnormal = '8';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thurshours - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
		
	 if ($frihours <= '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;
	  
	  
		}    
		if ($sathours  <= '8')  {
		
		$satnormal = $sathours ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (($sathours > '8') &&($sathours  <= '12')) {
		
		$sattimeandhalfhours = ($sathours - '8');
		$satnormal = '8';
		$satdouble = '0';
		}
	
	
		if ($sathours  >'12')  {
	  
	    $satnormal = '8';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($sathours  - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		if ($sunhours <= '8')  {
		
		$sunnormal = $sunhours;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($sunhours > '8') &&($sunhours <= '12')) {
		
		$suntimeandhalfhours = ($sunhours - '8');
		$sunnormal = '8';
		$sundouble = '0';
		}
	
	
		if ($sunhours >'12')  {
	  
	    $sunnormal = '8';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($sunhours - $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 = $monhours+$tueshours+$wedhours+$thurshours+$frihours+$sathours+$sunhours+$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){
    
    $rostering_timesheet->set_exception('total_hrs','Hours Exceeded!','error'); 
		}
		
}
	
	

	
		
	
	}

 	 

function calculate_totalhoursperweekg($value, $field, $priimary_key, $list, $xcrud)
{
   return '<div>' 
        
        . '<input type="text" name="'.$xcrud->fieldname_encode($fieldname).'" value="'.$value.'" class="xcrud-input" />'
        
        . '</div>';
}

//display hours automatically
function autocalculatehours($postdata, $primary,$xcrud){
	
	
$mondayshift = $postdata->get('mon_shift');
 
        $db = Xcrud_db::get_instance();
	    $query = "SELECT 
		              
		
		           hours FROM shifts WHERE shiftname = ".$mondayshift;
        
        $db->query($query);
        $outm = $db->result();
        foreach($outm as $outputm){
            $resulthours[] = $outputm['hours'];
        } 
		$postdata->set('mon_hrs',$resulthours[]);
		
}


//display colors for cell for the shift automatically


function after($postdata,$xcrud) {
    
  $monshift= $postdata->get('mon_shift');
   	if ($monshift = ('9am-12pm'))
    {
         $postdata->set('mon_hrs', 3.0);
		
    }
 

		
	 
}

function validate_maxhours($postdata, $primary, $xcrud){
    
	
$totalhrs = $postdata->get('total_hrs');

 $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 ($totalhrs > $maxhours)
    {
         $xcrud->set_exception($totalhrs, 'Employee max hours reached. Contact Manager', 'error');
		 $xcrud->highlight('total_hrs', '>', $maxhours, 'red');
    }
 	 

 
}



function publish_action($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
        $query = 'UPDATE base_fields SET `bool` = b\'1\' WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
    }
}
function unpublish_action($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
        $query = 'UPDATE base_fields SET `bool` = b\'0\' WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
    }
}

function exception_example($postdata, $primary, $xcrud)
{
    // get random field from $postdata
    $postdata_prepared = array_keys($postdata->to_array());
    shuffle($postdata_prepared);
    $random_field = array_shift($postdata_prepared);
    // set error message
    $xcrud->set_exception($random_field, 'This is a test error', 'error');
}

function test_column_callback($value, $fieldname, $primary, $row, $xcrud)
{
    return $value . ' - nice!';
}

function after_upload_example($field, $file_name, $file_path, $params, $xcrud)
{
    $ext = trim(strtolower(strrchr($file_name, '.')), '.');
    if ($ext != 'pdf' && $field == 'uploads.simple_upload')
    {
        unlink($file_path);
        $xcrud->set_exception('simple_upload', 'This is not PDF', 'error');
    }
}

function movetop($xcrud)
{
    if ($xcrud->get('primary') !== false)
    {
        $primary = (int)$xcrud->get('primary');
        $db = Xcrud_db::get_instance();
        $query = 'SELECT `departmentid` FROM `department` ORDER BY `departmentname`,`departmentid`';
        $db->query($query);
        $result = $db->result();
        $count = count($result);

        $sort = array();
        foreach ($result as $key => $item)
        {
            if ($item['departmentid'] == $primary && $key != 0)
            {
                array_splice($result, $key - 1, 0, array($item));
                unset($result[$key + 1]);
                break;
            }
        }

        foreach ($result as $key => $item)
        {
            $query = 'UPDATE `department` SET `departmentname` = ' . $key . ' WHERE departmentid = ' . $item['departmentid'];
            $db->query($query);
        }
    }
}
function movebottom($xcrud)
{
    if ($xcrud->get('primary') !== false)
    {
        $primary = (int)$xcrud->get('primary');
        $db = Xcrud_db::get_instance();
        $query = 'SELECT `departmentid` FROM `department` ORDER BY `departmentname`,`departmentid`';
        $db->query($query);
        $result = $db->result();
        $count = count($result);

        $sort = array();
        foreach ($result as $key => $item)
        {
            if ($item['departmentid'] == $primary && $key != $count - 1)
            {
                unset($result[$key]);
                array_splice($result, $key + 1, 0, array($item));
                break;
            }
        }

        foreach ($result as $key => $item)
        {
            $query = 'UPDATE `department` SET `departmentname` = ' . $key . ' WHERE departmentid = ' . $item['departmentid'];
            $db->query($query);
        }
    }
}

function show_description($value, $fieldname, $primary_key, $row, $xcrud)
{
    $result = '';
    if ($value == '1')
    {
        $result = '<i class="fa fa-check" />' . 'OK';
    }
    elseif ($value == '2')
    {
        $result = '<i class="fa fa-circle-o" />' . 'Pending';
    }
    return $result;
}

function custom_field($value, $fieldname, $primary_key, $row, $xcrud)
{
	$mondayshift = $postdata->get('mon_shift');
 
        $db = Xcrud_db::get_instance();
	    $query = "SELECT 
		              
		
		           hours FROM shifts WHERE shiftname = ".$mondayshift;
        
        $db->query($query);
        $outm = $db->result();
        foreach($outm as $outputm){
            $resulthours[] = $outputm['hours'];
        } 
	
    return '<input type="text" readonly class="xcrud-input" name="' . $xcrud->fieldname_encode($resulthours) . '" value="' . $value .
        '" />';
}
function unset_val($postdata)
{
    $postdata->del('Paid');
}

function format_phone($new_phone)
{
    $new_phone = preg_replace("/[^0-9]/", "", $new_phone);

    if (strlen($new_phone) == 7)
        return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $new_phone);
    elseif (strlen($new_phone) == 10)
        return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $new_phone);
    else
        return $new_phone;
}

function before_list_example($list, $xcrud)
{
    var_dump($list);
}

function after_update_test($pd, $pm, $xc)
{
    $xc->search = 0;
}

function after_upload_test($field, &$filename, $file_path, $upload_config, $this)
{
    $filename = 'bla-bla-bla';
}
	
