<?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('final_mon_in');
	$montime_out = $postdata->get('final_mon_out');
	
	
	
	
	
	$monhours = abs(strtotime($montime_in) - strtotime($montime_out)) / 3600;
		
    $postdata->set('final_mon', $monhours);
	

	
	//tuesday
	
	$tuestime_in = $postdata->get('final_tues_in');
	$tuestime_out = $postdata->get('final_tues_out');
	
	$tueshours = abs(strtotime($tuestime_in) - strtotime($tuestime_out)) / 3600;
		
    $postdata->set('final_tues', $tueshours);
	
	
	//wednesday 
	
	$wedtime_in = $postdata->get('final_wed_in');
	$wedtime_out = $postdata->get('final_wed_out');
	
	$wedhours = abs(strtotime($wedtime_in) - strtotime($wedtime_out)) / 3600;
		
    $postdata->set('final_wed', $wedhours);
	
	
	
	
	
	//thursday
	
	$thurstime_in = $postdata->get('final_thurs_in');
	$thurstime_out = $postdata->get('final_thurs_out');
	
	$thurshours = abs(strtotime($thurstime_in) - strtotime($thurstime_out)) / 3600;
		
    $postdata->set('final_thurs', $thurshours);
	
	
	//Friday
	
	$fritime_in = $postdata->get('final_fri_in');
	$fritime_out = $postdata->get('final_fri_out');
	
	$frihours = abs(strtotime($fritime_in) - strtotime($fritime_out)) / 3600;
		
    $postdata->set('final_fri', $frihours);
	
	
	
	//sat
	  
	$sattime_in = $postdata->get('final_sat_in');
	$sattime_out = $postdata->get('final_sat_out');
	
	$sathours = abs(strtotime($sattime_in) - strtotime($sattime_out)) / 3600;
		
    $postdata->set('final_sat', $sathours);
	
	
	
	//sunday
	
	$suntime_in = $postdata->get('final_sun_in');
	$suntime_out = $postdata->get('final_sun_out');
	
	$sunhours = abs(strtotime($suntime_in) - strtotime($suntime_out)) / 3600;
		
    $postdata->set('final_sun', $sunhours);
	
	
	//Monday 2 till cut over dates
	
	$mon2time_in = $postdata->get('final_mon2_in');
	$mon2time_out = $postdata->get('final_mon2_out');
	
	$mon2hours = abs(strtotime($mon2time_in) - strtotime($mon2time_out)) / 3600;
		
    $postdata->set('final_mon2', $mon2hours);
	
	
	if ($mon2time_out > '08:00:00'){
		
		$xcrud->set_exception('mon2time_out','Cant extend hours after 8a.m!','success'); 
	}
	

	
	
	
	
	
	
	 $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('final_total', $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('fnormal', $totalnormalhours);
	 $postdata->set('ftime', $totaltimeandhalf);
	 $postdata->set('fdouble', $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('final_total', $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('fnormal', $totalnormalhours);
	 $postdata->set('ftime', $totaltimeandhalf);
	 $postdata->set('fdouble', $totaldouble);
	 //$postdata->set('holiday', $holiday);
	

      
       
		
//	if ($totalhours > $maxhours){
    
  //  $xcrud->set_exception('total_hrs','Hours Exceeded!','error'); 
	//	}
		
}
	
	
//cem totals for manual manupulation
	
	//monday 
	
	$cmontime_in = $postdata->get('cmon_in');
	$cmontime_out = $postdata->get('cmon_out');
	
	
	
	
	
	$cmonhours = abs(strtotime($cmontime_in) - strtotime($cmontime_out)) / 3600;
		
    $postdata->set('cem_mon', $cmonhours);
	

	
	//tuesday
	
	$ctuestime_in = $postdata->get('ctues_in');
	$ctuestime_out = $postdata->get('ctues_out');
	
	$ctueshours = abs(strtotime($ctuestime_in) - strtotime($ctuestime_out)) / 3600;
		
    $postdata->set('cem_tues', $ctueshours);
	
	
	//wednesday 
	
	$cwedtime_in = $postdata->get('cwed_in');
	$cwedtime_out = $postdata->get('cwed_out');
	
	$cwedhours = abs(strtotime($cwedtime_in) - strtotime($cwedtime_out)) / 3600;
		
    $postdata->set('cem_wed', $cwedhours);
	
	
	
	
	
	//thursday
	
	$cthurstime_in = $postdata->get('cthurs_in');
	$cthurstime_out = $postdata->get('cthurs_out');
	
	$cthurshours = abs(strtotime($cthurstime_in) - strtotime($cthurstime_out)) / 3600;
		
    $postdata->set('cem_thurs', $cthurshours);
	
	
	//Friday
	
	$cfritime_in = $postdata->get('cfri_in');
	$cfritime_out = $postdata->get('cfri_out');
	
	$cfrihours = abs(strtotime($cfritime_in) - strtotime($cfritime_out)) / 3600;
		
    $postdata->set('cem_fri', $cfrihours);
	
	
	
	//sat
	  
	$csattime_in = $postdata->get('csat_in');
	$csattime_out = $postdata->get('csat_out');
	
	$csathours = abs(strtotime($csattime_in) - strtotime($csattime_out)) / 3600;
		
    $postdata->set('cem_sat', $csathours);
	
	
	
	//sunday
	
	$csuntime_in = $postdata->get('csun_in');
	$csuntime_out = $postdata->get('csun_out');
	
	$csunhours = abs(strtotime($csuntime_in) - strtotime($csuntime_out)) / 3600;
		
    $postdata->set('cem_sun', $csunhours);
	
	
	//Monday 2 till cut over dates
	
	$cmon2time_in = $postdata->get('cmon2_in');
	$cmon2time_out = $postdata->get('cmon2_out');
	
	$cmon2hours = abs(strtotime($cmon2time_in) - strtotime($cmon2time_out)) / 3600;
		
    $postdata->set('cem_mon2', $cmon2hours);
	
	
	if ($cmon2time_out > '08:00:00'){
		
		$xcrud->set_exception('cmon2time_out','Cant extend hours after 8a.m!','success'); 
	}
	

	
	
	
	
	
	
	 $cdb = Xcrud_db::get_instance();
	    $cquery = "SELECT contract,
		              max_hours_to_work
					  
		          FROM employeemaster
				  				  
				   WHERE employeeid = '".$employeeid."'";
        
        $cdb->query($cquery);
        $coutm = $cdb->result();
        foreach($coutm as $coutputm){
			$ccontract = $coutputm['contract'];
            $cmaxhours = $coutputm['max_hours_to_work'];
			
        } 
	
	
	
	$nine = '9';
	
	
		
if ($ccontract == $nine ) {
	
	if ($cmonhours <= '9')  {
		
		$cmonnormal = $cmonhours;
		$cmontimeandhalfhours = '0';
		$cmondouble = '0';
		
     	}
	
		if (($cmonhours > '9') &&($cmonhours <= '13')) {
		
		$cmontimeandhalfhours = ($cmonhours - '9');
		$cmonnormal = '9';
		$cmondouble = '0';
		}
	
	
		if ($cmonhours >'13')  {
	  
	    $cmonnormal = '9';
		$cmontimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($cmonhours- $cmonnormal );
	  	   
	    
	    $cmondouble = $ctotalovertimehours - $cmontimeandhalfhours;
	  
	  
		}
	
	 if ($ctueshours <= '9')  {
		
		$ctuesnormal = $ctueshours;
		$ctuestimeandhalfhours = '0';
		$ctuesdouble = '0';
		
     	}
	
		if (($ctueshours > '9') &&($ctueshours <= '13')) {
		
		$ctuestimeandhalfhours = ($ctueshours - '9');
		$ctuesnormal = '9';
		$ctuesdouble = '0';
		}
	
	
		if ($ctueshours >'13')  {
	  
	    $ctuesnormal = '9';
		$ctuestimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($ctueshours - $ctuesnormal );
	  	   
	    
	    $ctuesdouble = $ctotalovertimehours - $ctuestimeandhalfhours;
	  
	  
		}
		 if ($cwedhours <= '9')  {
		
		$cwednormal = $cwedhours;
		$cwedtimeandhalfhours = '0';
		$cweddouble = '0';
		
     	}
	
		if (($cwedhours > '9') &&($cwedhours<= '13')) {
		
		$cwedtimeandhalfhours = ($cwedhours- '9');
		$cwednormal = '9';
		$weddouble = '0';
		}
	
	
		if ($cwedhours>'13')  {
	  
	    $cwednormal = '9';
		$cwedtimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($cwedhours - $cwednormal );
	  	   
	    
	    $cweddouble = $ctotalovertimehours - $cwedtimeandhalfhours;
	  
	  
		}
		
		if ($cthurshours <= '9')  {
		
		$cthursnormal = $cthurshours;
		$cthurstimeandhalfhours = '0';
		$cthursdouble = '0';
		
     	}
	
		if (($cthurshours > '9') &&($cthurshours <= '13')) {
		
		$cthurstimeandhalfhours = ($cthurshours - '9');
		$cthursnormal = '9';
		$cthursdouble = '0';
		}
	
	
		if ($cthurshours >'13')  {
	  
	    $cthursnormal = '9';
		$cthurstimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($cthurshours - $cthursnormal );
	  	   
	    
	    $cthursdouble = $ctotalovertimehours - $cthurstimeandhalfhours;
	  
	  
		}
		
		
	 if ($cfrihours <= '9')  {
		
		$cfrinormal = $cfrihours ;
		$cfritimeandhalfhours = '0';
		$cfridouble = '0';
		
     	}
	
		if (($cfrihours  > '9') &&($cfrihours  <= '13')) {
		
		$cfritimeandhalfhours = ($cfrihours  - '9');
		$cfrinormal = '9';
		$cfridouble = '0';
		}
	
	
		if ($cfrihours  >'13')  {
	  
	    $cfrinormal = '9';
		$cfritimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($cfrihours - $cfrinormal );
	  	   
	    
	    $cfridouble = $ctotalovertimehours - $cfritimeandhalfhours;
	  
	  
		}    
		if ($csathours  <= '9')  {
		
		$csatnormal = $csathours ;
		$csattimeandhalfhours = '0';
		$csatdouble = '0';
		
     	}
	
		if (($csathours > '9') &&($csathours  <= '13')) {
		
		$csattimeandhalfhours = ($csathours  - '9');
		$csatnormal = '9';
		$csatdouble = '0';
		}
	
	
		if ($csathours  >'13')  {
	  
	    $csatnormal = '9';
		$csattimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($csathours  - $csatnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		if ($csunhours <= '9')  {
		
		$csunnormal = $csunhours;
		$csuntimeandhalfhours = '0';
		$csundouble = '0';
		
     	}
	
		if (($csunhours > '9') &&($csunhours <= '13')) {
		
		$csuntimeandhalfhours = ($csunhours - '9');
		$csunnormal = '9';
		$csundouble = '0';
		}
	
	
		if ($sunhours >'13')  {
	  
	    $csunnormal = '9';
		$csuntimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($csunhours - $csunnormal );
	  	   
	    
	    $csundouble = $ctotalovertimehours - $csuntimeandhalfhours;
	  
	  
		}    
		if ($cmon2hours <= '9')  {
		
		$cmonnormal2 = $cmon2hours;
		$cmontimeandhalfhours2 = '0';
		$cmondouble2 = '0';
		
     	}
	
		if (($cmon2hours > '9') &&($cmon2hours <= '13')) {
		
		$cmontimeandhalfhours2 = ($cmon2hours - '9');
		$cmonnormal2 = '9';
		$cmondouble2 = '0';
		}
	
	
		if ($cmon2hours >'13')  {
	  
	    $cmonnormal2 = '9';
		$cmontimeandhalfhours2 = '4';
	  
		$ctotalovertimehours = ($cmon2hours - $cmonnormal2 );
	  	   
	    
	    $cmondouble2 = $ctotalovertimehours - $cmontimeandhalfhours2;
	  
	  
		}
   
		 
	
        $ctotalhours = $cmonhours+$ctueshours+$cwedhours+$cthurshours+$cfrihours+$csathours+$csunhours+$cmon2hours;
		$postdata->set('cem_total', $ctotalhours);
	
		$ctotalnormalhours = $cmonnormal +$ctuesnormal+$cwednormal+$cthursnormal+$cfrinormal+$csatnormal+$csunnormal+$cmonnormal2;
	$ctotaltimeandhalf = $cmontimeandhalfhours + $ctuestimeandhalfhours +$cwedtimeandhalfhours+$cthurstimeandhalfhours+$cfritimeandhalfhours+$csattimeandhalfhours+$csuntimeandhalfhours+$cmontimeandhalfhours2 ;
	
	$ctotaldouble = $cmondouble+$ctuesdouble+$cweddouble+$cthursdouble+$cfridouble+$csatdouble+$csundouble+$cmondouble2;
	
	$postdata->set('cnormal', $ctotalnormalhours);
	 $postdata->set('ctime', $ctotaltimeandhalf);
	 $postdata->set('cdouble', $ctotaldouble);
	 //$postdata->set('holiday', $holiday);
	
	
	//if ($ctotalhours > $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 ($cmonhours <= '8')  {
		
		$cmonnormal = $cmonhours;
		$cmontimeandhalfhours = '0';
		$cmondouble = '0';
		
     	}
	
		if (($cmonhours > '8') &&($cmonhours <= '12')) {
		
		$cmontimeandhalfhours = ($cmonhours - '8');
		$cmonnormal = '8';
		$cmondouble = '0';
		}
	
	
		if ($cmonhours >'12')  {
	  
	    $cmonnormal = '8';
		$cmontimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($cmonhours- $cmonnormal );
	  	   
	    
	    $cmondouble = $ctotalovertimehours - $cmontimeandhalfhours;
	  
	  
		}
	
	 if ($ctueshours <= '8')  {
		
		$ctuesnormal = $ctueshours;
		$ctuestimeandhalfhours = '0';
		$ctuesdouble = '0';
		
     	}
	
		if (($ctueshours > '8') &&($ctueshours <= '12')) {
		
		$ctuestimeandhalfhours = ($ctueshours - '8');
		$ctuesnormal = '8';
		$ctuesdouble = '0';
		}
	
	
		if ($ctueshours >'12')  {
	  
	    $ctuesnormal = '8';
		$ctuestimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($ctueshours - $ctuesnormal );
	  	   
	    
	    $ctuesdouble = $ctotalovertimehours - $ctuestimeandhalfhours;
	  
	  
		}
		 if ($cwedhours <= '8')  {
		
		$cwednormal = $cwedhours;
		$cwedtimeandhalfhours = '0';
		$cweddouble = '0';
		
     	}
	
		if (($cwedhours > '8') &&($cwedhours<= '12')) {
		
		$cwedtimeandhalfhours = ($cwedhours- '8');
		$cwednormal = '8';
		$weddouble = '0';
		}
	
	
		if ($cwedhours>'12')  {
	  
	    $cwednormal = '8';
		$cwedtimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($cwedhours - $cwednormal );
	  	   
	    
	    $cweddouble = $ctotalovertimehours - $cwedtimeandhalfhours;
	  
	  
		}
		
		if ($cthurshours <= '8')  {
		
		$cthursnormal = $cthurshours;
		$cthurstimeandhalfhours = '0';
		$cthursdouble = '0';
		
     	}
	
		if (($cthurshours > '8') &&($cthurshours <= '12')) {
		
		$cthurstimeandhalfhours = ($cthurshours - '8');
		$cthursnormal = '8';
		$cthursdouble = '0';
		}
	
	
		if ($cthurshours >'12')  {
	  
	    $cthursnormal = '8';
		$cthurstimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($cthurshours - $cthursnormal );
	  	   
	    
	    $cthursdouble = $ctotalovertimehours - $cthurstimeandhalfhours;
	  
	  
		}
		
		
	 if ($cfrihours <= '8')  {
		
		$cfrinormal = $cfrihours ;
		$cfritimeandhalfhours = '0';
		$cfridouble = '0';
		
     	}
	
		if (($cfrihours  > '8') &&($cfrihours  <= '12')) {
		
		$cfritimeandhalfhours = ($cfrihours  - '8');
		$cfrinormal = '8';
		$cfridouble = '0';
		}
	
	
		if ($cfrihours  >'12')  {
	  
	    $cfrinormal = '8';
		$cfritimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($cfrihours - $cfrinormal );
	  	   
	    
	    $cfridouble = $ctotalovertimehours - $cfritimeandhalfhours;
	  
	  
		}    
		if ($csathours  <= '8')  {
		
		$csatnormal = $csathours ;
		$csattimeandhalfhours = '0';
		$csatdouble = '0';
		
     	}
	
		if (($csathours > '8') &&($csathours  <= '12')) {
		
		$csattimeandhalfhours = ($csathours  - '8');
		$csatnormal = '8';
		$csatdouble = '0';
		}
	
	
		if ($csathours  >'12')  {
	  
	    $csatnormal = '8';
		$csattimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($csathours  - $csatnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		if ($csunhours <= '8')  {
		
		$csunnormal = $csunhours;
		$csuntimeandhalfhours = '0';
		$csundouble = '0';
		
     	}
	
		if (($csunhours > '8') &&($csunhours <= '12')) {
		
		$csuntimeandhalfhours = ($csunhours - '8');
		$csunnormal = '8';
		$csundouble = '0';
		}
	
	
		if ($sunhours >'12')  {
	  
	    $csunnormal = '8';
		$csuntimeandhalfhours = '4';
	  
		$ctotalovertimehours = ($csunhours - $csunnormal );
	  	   
	    
	    $csundouble = $ctotalovertimehours - $csuntimeandhalfhours;
	  
	  
		}    
		if ($cmon2hours <= '8')  {
		
		$cmonnormal2 = $cmon2hours;
		$cmontimeandhalfhours2 = '0';
		$cmondouble2 = '0';
		
     	}
	
		if (($cmon2hours > '8') &&($cmon2hours <= '12')) {
		
		$cmontimeandhalfhours2 = ($cmon2hours - '8');
		$cmonnormal2 = '8';
		$cmondouble2 = '0';
		}
	
	
		if ($cmon2hours >'12')  {
	  
	    $cmonnormal2 = '8';
		$cmontimeandhalfhours2 = '4';
	  
		$ctotalovertimehours = ($cmon2hours - $cmonnormal2 );
	  	   
	    
	    $cmondouble2 = $ctotalovertimehours - $cmontimeandhalfhours2;
	  
	  
		}
   
		 
	
        $ctotalhours = $cmonhours+$ctueshours+$cwedhours+$cthurshours+$cfrihours+$csathours+$csunhours+$cmon2hours;
		$postdata->set('cem_total', $ctotalhours);
	
		$ctotalnormalhours = $cmonnormal +$ctuesnormal+$cwednormal+$cthursnormal+$cfrinormal+$csatnormal+$csunnormal+$cmonnormal2;
	$ctotaltimeandhalf = $cmontimeandhalfhours + $ctuestimeandhalfhours +$cwedtimeandhalfhours+$cthurstimeandhalfhours+$cfritimeandhalfhours+$csattimeandhalfhours+$csuntimeandhalfhours+$cmontimeandhalfhours2 ;
	
	$ctotaldouble = $cmondouble+$ctuesdouble+$cweddouble+$cthursdouble+$cfridouble+$csatdouble+$csundouble+$cmondouble2;
	
	$postdata->set('cnormal', $ctotalnormalhours);
	 $postdata->set('ctime', $ctotaltimeandhalf);
	 $postdata->set('cdouble', $ctotaldouble);
	

      
}
	
	
	
	//variance
	
	$rmon = $postdata->get('rostered_mon');
	$rtues = $postdata->get('rostered_tues');
	$rwed = $postdata->get('rostered_wed');
	$rthurs = $postdata->get('rostered_thurs');
	$rfri = $postdata->get('rostered_fri');
	$rsat = $postdata->get('rostered_sat');
	$rsun = $postdata->get('rostered_sun');
	$rmon2 = $postdata->get('rostered_mon2');
	
$mv = $cmonhours - $rmon;
	
	$postdata->set('variance_mon', $mv);
	
	
	$tv = $ctueshours - $rtues;
	
	$postdata->set('variance_tues', $tv);
	
	$wv = $cwedhours - $rwed;
	
	$postdata->set('variance_wed', $wv);
	
	
	$thv = $cthurshours - $rthurs;
	
	$postdata->set('variance_thurs', $thv);
	
	$fv = $cfrihours - $rfri;
	
	$postdata->set('variance_fri', $fv);
	
	$sv = $csathours - $rsat;
	
	$postdata->set('variance_sat', $sv);
	
	$sunv = $csunhours - $rsun;
	
	$postdata->set('variance_sun', $sunv);
	
	$m2v = $cmon2hours - $rmon2;
	
	$postdata->set('variance_mon2', $mv2);
	
	
	
	
	}

 	 

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';
}
	
