<?php
function department($xcrud){

	$accnum1 = $postdata->get('patrol_location');
   
$db41 = Xcrud_db::get_instance();
	    $query = "SELECT 
		             department
		               
					  
		          FROM guardforceclient
				    
				   WHERE clientid = '" . $accnum1 . "'";
        
        $db41->query($query);
        $out41 = $db41->result();
        foreach($out41 as $output41){
			
			$depart=$output41['department'];
			
			
            
        } 

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

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

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


		} 

	$db151 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.mon_shift2 = shifts.shiftid";
				  	 	   
        
        $db151->query($query);
        $out81 = $db151->result();
        foreach($out81 as $output81){
            $mon_shift1 = $output81['shiftid'];
			$mon_hrs1 = $output81['hours'];
			$mon_color1 = $output81['color'];


$rostering_timesheet->highlight('mon_shift2','=',$mon_shift1,$mon_color1);
//$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 timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.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);


		} 
	
	
$db161 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.tues_shift2 = shifts.shiftid";
				  	 	   
        
        $db161->query($query);
        $out91 = $db161->result();
        foreach($out91 as $output91){
            $tues_shift2 = $output91['shiftid'];
			$tues_hrs2 = $output91['hours'];
			$tues_color2 = $output91['color'];


$rostering_timesheet->highlight('tues_shift2','=',$tues_shift2,$tues_color2);
//$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 timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.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);


		} 

	$db171 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.wed_shift2 = shifts.shiftid";
				  	 	   
        
        $db171->query($query);
        $out101 = $db171->result();
        foreach($out101 as $output101){
            $wed_shift2 = $output101['shiftid'];
			$wed_hrs2 = $output101['hours'];
			$wed_color2 = $output101['color'];


$rostering_timesheet->highlight('wed_shift2','=',$wed_shift2,$wed_color2);
//$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 timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.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);


		} 

	$db181 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.thurs_shift2 = shifts.shiftid";
				  	 	   
        
        $db181->query($query);
        $out111 = $db181->result();
        foreach($out111 as $output111){
            $thurs_shift2 = $output111['shiftid'];
			$thurs_hrs2 = $output111['hours'];
			$thurs_color2 = $output111['color'];


$rostering_timesheet->highlight('thurs_shift2','=',$thurs_shift2,$thurs_color2);
//$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 timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.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);


		} 

$db191 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.fri_shift = shifts.shiftid";
				  	 	   
        
        $db191->query($query);
        $out121 = $db191->result();
        foreach($out121 as $output121){
            $fri_shift2 = $output121['shiftid'];
			$fri_hrs2 = $output121['hours'];
			$fri_color2 = $output121['color'];


$rostering_timesheet->highlight('fri_shift2','=',$fri_shift2,$fri_color2);
//$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 timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.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);


		} 

	$db201 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.sat_shift2 = shifts.shiftid";
				  	 	   
        
        $db201->query($query);
        $out131 = $db201->result();
        foreach($out131 as $output131){
            $sat_shift2 = $output131['shiftid'];
			$sat_hrs2 = $output131['hours'];
			$sat_color2 = $output131['color'];


$rostering_timesheet->highlight('sat_shift2','=',$sat_shift2,$sat_color2);
//$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 timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.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);


		} 

	$db211 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.sun_shift2 = shifts.shiftid";
				  	 	   
        
        $db211->query($query);
        $out141 = $db211->result();
        foreach($out141 as $output141){
            $sun_shift2 = $output141['shiftid'];
			$sun_hrs2 = $output141['hours'];
			$sun_color2 = $output141['color'];


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


		} 

	
	
	
	//mon2
	//Sun cell

$db22 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.mon_shift_2 = shifts.shiftid";
				  	 	   
        
        $db22->query($query);
        $out22 = $db22->result();
        foreach($out22 as $output22){
            $mon_shift2 = $output22['shiftid'];
			$mon_hrs2 = $output22['hours'];
			$mon_color2 = $output22['color'];


$rostering_timesheet->highlight('mon_shift_2','=',$mon_shift2,$mon_color2);
//$rostering_timesheet->highlight('mon_hrs_2','=',$mon_hrs2,$mon_color2);


		} 
	
	
	$db221 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM timesheetsuvadetails
				  INNER JOIN shifts
				  ON  timesheetsuvadetails.mon_shift_2_2 = shifts.shiftid";
				  	 	   
        
        $db221->query($query);
        $out221 = $db221->result();
        foreach($out221 as $output221){
            $mon_shift22 = $output221['shiftid'];
			$mon_hrs22 = $output221['hours'];
			$mon_color22 = $output221['color'];


$rostering_timesheet->highlight('mon_shift_2_2','=',$mon_shift22,$mon_color22);
//$rostering_timesheet->highlight('mon_hrs_2','=',$mon_hrs2,$mon_color2);


		} 
	
}

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 timesheetsuvadetails";
				  	 
				   
        
        $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){
	
	$rosterid = $postdata->get('techtimeid');
	$scheduleid = $postdata->get('id');
	
	$employeeid = $postdata->get('employeeid');
	$monhours1 = $postdata->get('montotal_manual');
	$tueshours1 = $postdata->get('tuestotal_manual');
	$wedhours1 = $postdata->get('wedtotal_manual');
	$thurshours1 = $postdata->get('thurstotal_manual');
	$frihours1 = $postdata->get('fritotal_manual');
	$sathours1 = $postdata->get('sattotal_manual');
	$sunhours1 = $postdata->get('suntotal_manual');
	$mon2hours1 = $postdata->get('mon2total_manual');
	
	
$totalhours1 = $monhours1 + $tueshours1+$wedhours1+$thurshours1+$frihours1+$sathours1+$sunhours1+$mon2hours1;
	
	$postdata->set('totalhours_manual', $totalhours1);

	//get calender id
	
	
	$dbcalendar = Xcrud_db::get_instance();
	$querycalendar = "SELECT id
					  
		          FROM techniciantimesheet
				  				  
				   WHERE techtimeid= '".$rosterid."' ";
        
        $dbcalendar->query($querycalendar);
        $outmcalendar = $dbcalendar->result();
        foreach($outmcalendar as $outputmcalendar){
			$calenderid = $outputmcalendar['id'];
			
		}
	
	 $postdata->set('calendar_id', $calenderid);

	

	
//monday 
	
	$montime_in = $postdata->get('montime_in');
	$montime_out = $postdata->get('montime_out');
	
	
	
	if (isset($montime_in)){
	
	$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');
	if (isset($tuestime_in)){
	$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');
	if (isset($wedtime_in)){
	$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');
	if (isset($thurstime_in)){
	$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');
	if (isset($fritime_in)){
	$frihours = abs(strtotime($fritime_in) - strtotime($fritime_out)) / 3600;
		
    $postdata->set('fri_total', $frihours);
	
	}
	  
	$sattime_in = $postdata->get('sattime_in');
	$sattime_out = $postdata->get('sattime_out');
	if (isset($sattime_in)){
	$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');
	if (isset($suntime_in)){
	$sunhours = abs(strtotime($suntime_in) - strtotime($suntime_out)) / 3600;
		
    $postdata->set('sun_total', $sunhours);
	
	}
	//monday 
	
	$mon2time_in = $postdata->get('mon2time_in');
	$mon2time_out = $postdata->get('mon2time_out');
	
	
	
	
	if (isset($mon2time_in)){
	$mon2hours = abs(strtotime($mon2time_in) - strtotime($mon2time_out)) / 3600;
		
    $postdata->set('mon2_total', $mon2hours);
	}
	
	
	// get mon-mon2 dates
	
	 $db2 = Xcrud_db::get_instance();
	    $query = "SELECT calendar_management.start_date, calendar_management.end_date,timesheetsuva.required_guards
					  
		          FROM calendar_management INNER JOIN 
				  timesheetsuva on calendar_management.id = timesheetsuva.id
				  			  				  
				   WHERE  timesheetsuva.timesuvaid = '".$rosterid."'";
        
        $db2->query($query);
        $outm2 = $db2->result();
        foreach($outm2 as $outputm2){
			$start = $outputm2['start_date'];
			$end = $outputm2['end_date'];
			$requireguards = $outputm2['required_guards'];
           			
        } 
	
	
	
	
 

	 $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'];
			
        } 
	
	//calculate payroll timesheet data for 8hours and 6 days contract staffs
	
	//check if continuation field on shift two was ticked so that the system knows its one shift in 24 hr clocking system:
	//if ($totalhours > $maxhours){
    
 //   $xcrud->set_exception('total_hrs','Hours Exceeded!','error'); 
	//	}
	
	

$totalhours = $monhours + $tueshours+$wedhours+$thurshours+$frihours+$sathours+$sunhours+$mon2hours;
	
	$postdata->set('total_hrs', $totalhours);


	

	

	
	

	

	

	
	

	
}

	

 	 

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