<?php


function get_Mtotal($postdata,$m){
	
	$acc =$postdata->get('account_number');
	//$mrate =$postdata->get('monthly_rate');
	
	$id =$postdata->get('id');
	
	$db222 = Xcrud_db::get_instance();
	    $query = "SELECT SUM(monitoring_premises.monthly_rate) as accounttotal
					  
		          FROM monitoring_premises 
				  				  
				   WHERE account_number ='".$acc."'";
        
        $db222->query($query);
        $outm222 = $db222->result();
	
	$vepamount = $outm222['accounttotal'];
	//$vepround = round($vepamount,2);
	$vat = $vepamount * 1.09;
	
//	$vip = $vepround + $vat;
	
	//$vipround = round($vip,2);
	
		$db223 = Xcrud_db::get_instance();
	    $query2 = "UPDATE  monitoring_master_data set vep_amount = '".$vepamount."',
		                                            vat ='".$vat."'
				  				  
				   WHERE account_number ='".$acc."'";
        
        $db223->query($query2);
        $outm223 = $db223->result();
	

}
function monitoring_billing($postdata,$ebusiness){
	
	//get id from table
		 $db0 = Xcrud_db::get_instance();
	    $query0 = "SELECT id
					  
		          FROM  monitoring_billing
				  ORDER BY id DESC LIMIT 1
				  				  
				   ";
        
        $db0->query($query0);
        $outm0 = $db0->result();
        foreach($outm0 as $outputm0){
		
			if ($outputm0['id'] == '0'){
				
				$id = '1';
			}
			else {
				
				$id = $outputm0['id'] + 1;
				
			}
			
			
        } 
	
	//$id =$postdata->get('id');
	
	
	
	
	$timenow = date('Y-m-d H:i:s');
	 $db = Xcrud_db::get_instance();
	    $query = "SELECT mon_id,vep_amount,vat_amount,vip_amount
					  
		          FROM  monitoring_customers
				  				  
				   ";
        
        $db->query($query);
        $outm = $db->result();
        foreach($outm as $outputm){
		
			$db2 = Xcrud_db::get_instance();
			 $query1 = "INSERT into monitoring_billing_details set  id = '".$id."',
			 									 mon_id = '".$outputm['mon_id']."',
					  							 vep_amount= '".$outputm['vep_amount']."',
												 vat = '".$outputm['vat_amount']."',
												vip_amount = '".$outputm['vip_amount']."'
												
		          
				  				  
				   ";
        
        $db2->query($query1);
			
			
        } 

	
	
}
function add_locstock($postdata,$ebusiness){
	
	$catid =$postdata->get('categoryid');
	$name =$postdata->get('description');
	
	$barcode =$postdata->get('barcode');
	$cost=$postdata->get('materialcost');
	$price =$postdata->get('price');
	$stockid = $postdata->get('stockid');
	if (empty($name)){
	$ebusiness->set_exception('description','Cant be blank mate!'); 
		
	}
	else{
	
	
	$timenow = date('Y-m-d H:i:s');
	 $db = Xcrud_db::get_instance();
	    $query = "SELECT loccode 
					  
		          FROM locations
				  				  
				   ";
        
        $db->query($query);
        $outm = $db->result();
        foreach($outm as $outputm){
		
			$db2 = Xcrud_db::get_instance();
			 $query1 = "INSERT into locstock set  loccode = '".$outputm['loccode']."',
			 									 stockid = '".$stockid."',
					  							 quantity = '0',
												 reorderlevel = '0',
												 bin = '0',
												 lastupdate= '".$timenow."'
		          
				  				  
				   ";
        
        $db2->query($query1);
			
			
        } 

	
	}
}
function fixed_deductions($postdata,$ebusiness){
	$totalamount = $postdata->get('total_amount_to_deduct');
	$paycycle= $postdata->get('pay_cycle');
	
	$deductionamount= $postdata->get('deduction_amount');

	
		
		$estimate = $totalamount / $deductionamount;
	
		$postdata->set('estimate_cycle', $estimate);
		
	 
}
function get_PeriodDetails($postdata,$ebusiness){
	$start = $postdata->get('start_date');
	$end = $postdata->get('end_date');
	
	
	$month = date("M",strtotime($end));
	$year = date("Y",strtotime($end));
	
$fullperiod = $month.'-'.$year;
	
	$postdata->set('period', $fullperiod);

	
	
}



function check_guards($postdata,$rostering_timesheet){
	
	$rosterid = $postdata->get('rosterid');
	
	 $db2 = Xcrud_db::get_instance();
	    $query = "SELECT rostering.required_guards,rosteringtimesheet.count(*) as totalrecords
					  
		          FROM rosteringtimesheet INNER JOIN 
				  rostering on rosteringtimsheet.rosterid = rostering.rosterid
				  			  				  
				   WHERE  rostering.rosterid = '".$rosterid."'";
        
        $db2->query($query);
        $outm2 = $db2->result();
        foreach($outm2 as $outputm2){
		
			$requireguards = $outputm2['required_guards'];
           			$currentguards =  $outputm2['totalrecords'];
        } 
	
	
	
	
}
function calculate_update($postdata,$rostering_timesheet,$xcrud,$ebusiness){
	$rstimeid = $postdata->get('rstimeid');
	$rosterid = $postdata->get('rosterid');
	$scheduleid = $postdata->get('rstimeid');
	
	$employeeid = $postdata->get('employeeid');
	
	
		//$moncheck = $postdata->get('overlap_mon');
	
	

	
//monday 
	
	$montime_in = $postdata->get('montime_in');
	$montime_out = $postdata->get('montime_out');
	
	
	
	
	
	$monhours = abs(strtotime($montime_in) - strtotime($montime_out)) / 3600;
		
    $postdata->set('mon_hrs', $monhours);
	
	
	
	$montime_in1 = $postdata->get('montime_in1');
	$montime_out1 = $postdata->get('montime_out1');
	


	
	$monhours2 = abs(strtotime($montime_in1) - strtotime($montime_out1)) / 3600;	
		
	
		 $mondaytotal = $monhours +$monhours2;
		
    $postdata->set('mon_hrs2', $monhours2);
	
	
	
	   $postdata->set('mon_total', $mondaytotal);
	
	

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

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

	$frihours2 = abs(strtotime($fritime_in1) - strtotime($fritime_out1)) / 3600;
		
    $postdata->set('fri_hrs2', $frihours2);
	
	$fridaytotal = $frihours +$frihours2;
	
	   $postdata->set('fri_total', $fridaytotal);
	
	
	//sat
	  
	$sattime_in = $postdata->get('sattime_in');
	$sattime_out = $postdata->get('sattime_out');
	
	$sathours = abs(strtotime($sattime_in) - strtotime($sattime_out)) / 3600;
		
    $postdata->set('sat_hrs', $sathours);
	
	$sattime_in1 = $postdata->get('sattime_in1');
	$sattime_out1 = $postdata->get('sattime_out2');
	
	
		
	$sathours5 = abs(strtotime($sattime_in1) - strtotime($sattime_out1)) / 3600;
		
	//if ($sathours5 > '20') {
		
		//$sathours5 = '0';
	//}
	
	
		
    $postdata->set('sat_hrs2', $sathours5);
	
	
	$sattotal = $sathours + $sathours5;

	
	   $postdata->set('sat_total', $sattotal);
	
	
	//sunday
	
	$suntime_in = $postdata->get('sunttime_in');
	$suntime_out = $postdata->get('sunttime_out');
	
	$sunhours = abs(strtotime($suntime_in) - strtotime($suntime_out)) / 3600;
		
    $postdata->set('sun_hrs', $sunhours);
	
	$suntime_in1 = $postdata->get('suntime_in1');
	$suntime_out1 = $postdata->get('suntime_out1');
	
	$sunhours2 = abs(strtotime($suntime_in1) - strtotime($suntime_out1)) / 3600;
		
    $postdata->set('sun_hrs2', $sunhours2);
	
	$suntotal = $sunhours +$sunhours2;
	
	   $postdata->set('sun_total', $suntotal);
	
	
	//Monday 2 till cut over dates
	
	$mon2time_in = $postdata->get('mon2time_in');
	$mon2time_out = $postdata->get('mon2time_out');
	
	$mon2hours = abs(strtotime($mon2time_in) - strtotime($mon2time_out)) / 3600;
		
    $postdata->set('mon2_total', $mon2hours);
	
	
	if ($mon2time_out > '08:00:00'){
		
		$rostering_timesheet->set_exception('mon2time_out','Cant extend hours after 8a.m!','success'); 
	}
	
	
	
	$shifttweleve = $tuestime_in - $montime_out1 ;
	$shifttwelevetues = $wedtime_in - $tuestime_out1 ;
	$shifttwelevewed = $thurstime_in - $wedtime_out1 ;
	$shifttwelevethurs = $fritime_in - $thurstime_out1 ;
	$shifttwelevefri = $sattime_in - $fritime_out1 ;
	$shifttwelevesat = $suntime_in - $sattime_out1 ;
	$shifttwelevesun = $mon2time_in - $suntime_out1 ;
	
	
	//quality check on shift 2

	
	//$cenvertedTime = date('H:i:s',strtotime('+8 hour',strtotime($montime_out)));
	
	
	
		//if ($montime_in1 < $cenvertedTime ){
			
			
			
	//$rostering_timesheet->set_exception('montime_in1',$montime_in1,'success'); 
	//$xcrud->set_exception('montime_in1','8 Hours Rest is required between shifts','success'); 
	
//}
	
	

	
	
		
	//check if public holiday has to be paid in any particular day in this date range
	
	
	//check if the employee is added to timesheet twice at the same roster
	
	$db222 = Xcrud_db::get_instance();
	    $query = "SELECT employeeid
					  
		          FROM rosteringtimesheet
				  				  
				   WHERE rosterid = '".$rosterid."' AND employeeid = '".$employeeid."'";
        
        $db222->query($query);
        $outm222 = $db222->result();
        foreach($outm222 as $outputm222){
			$employeealreadyrostered = $outputm222['employeeid'];
			
		}
	
       //if ($employeeid == $employeealreadyrostered ) {
			
			
		//$xcrud->set_exception('employeeid','This Staff has already been entered in this location. Cant roster this person again since it doesnt make sense'); 
			
		//}
          
			
			
           			
         
	
	
	// get mon-mon2 dates
	
	 $db2 = Xcrud_db::get_instance();
	    $query = "SELECT start_date, end_date,patrol_location
					  
		          FROM rostering
				  				  
				   WHERE rosterid = '".$rosterid."'";
        
        $db2->query($query);
        $outm2 = $db2->result();
        foreach($outm2 as $outputm2){
			$start = $outputm2['start_date'];
			$end = $outputm2['end_date'];
			
           			
        } 
	
	//set deafult values for dates for each day
	
	//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);
	
	
	
	//we know the dates now for all days so we going to check day and time to see if anyone has already been rostered at this time
	
	//check for mondays if a employee has already occupied on a time 

		
	
	
	
	//mon1
	
	
	//eight hour gap check mon
	
	//$db500 = Xcrud_db::get_instance();
	  //  $query500 = "SELECT montime_out
		      //    FROM rosteringtimesheet
				//   WHERE  employeeid = '".$employeeid."'
				//   AND mon_date = '".$mondate."'
				//   ";
        
    //    $db500->query($query);
     //   $outm500 = $db500->result();
     //   foreach($outm500 as $outputm500){
			
	//		$moneighthoursgap = date('H:i:s',strtotime('+8 hour',strtotime($outm500['montime_out'])));   

	//	}
	
	//if ($montime_in <=  $moneighthoursgap) {
			
			
	//	$xcrud->set_exception('employeeid','This Staff has not meet the 8 hour gap yet'.$moneighthoursgap); 
			
	//	}
	
	//quality check on monday shift 2
	
	
	
	
 

	 $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'];
			
        } 
	
	//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:
	



	
	
	
	
$nine = '9';
	
	
	
	//check if employees already added in the timesheet
	
	 $db1 = Xcrud_db::get_instance();
	    $query = "SELECT employeeid
					  
		          FROM rosteringtimesheet
				  				  
				   WHERE rosterid = '".$rosterid."'";
        
        $db1->query($query);
        $outm1 = $db1->result();
        foreach($outm1 as $outputm1){
			$employeedata = $outputm1['employeeid'];
           			
        } 
	
	//if ($employeedata === $employeeid){
    
     //   $xcrud->set_exception('employeeid','Employee Already Scheduled!'); 
		
	//	}
	

	
	

	
	
if ($contract == $nine ) {
	
	
	
	if ( $mondaytotal <= '9')  {
		
		$monnormal = $mondaytotal;
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($mondaytotal > '9') &&($mondaytotal <= '13')) {
		
		$montimeandhalfhours = ($mondaytotal - '9');
		$monnormal = '9';
		$mondouble = '0';
		}
	
	
		if ( $mondaytotal >'13')  {
	  
	    $monnormal = '9';
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($mondaytotal- $monnormal );
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
	
	 if ( $tuesdaytotal <= '9')  {
		
		$tuesnormal = $tuesdaytotal;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdaytotal > '9') &&($tuesdaytotal <= '13')) {
		
		$tuestimeandhalfhours = ($tuesdaytotal - '9');
		$tuesnormal = '9';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdaytotal >'13')  {
	  
	    $tuesnormal = '9';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdaytotal - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		 if ( $wednesdaytotal <= '9')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '9') &&($wednesdaytotal<= '13')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '9');
		$wednormal = '9';
		$weddouble = '0';
		}
	
	
		if ( $wednesdaytotal>'13')  {
	  
	    $wednormal = '9';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		
		if ( $thursdaytotal <= '9')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '9') &&($thursdaytotal <= '13')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '9');
		$thursnormal = '9';
		$thursdouble = '0';
		}
	
	
		if ( $thursdaytotal >'13')  {
	  
	    $thursnormal = '9';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
		
	 if ($fridaytotal <= '9')  {
		
		$frinormal = $fridaytotal ;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($fridaytotal  > '9') &&($fridaytotal  <= '13')) {
		
		$fritimeandhalfhours = ($fridaytotal  - '9');
		$frinormal = '9';
		$fridouble = '0';
		}
	
	
		if ( $fridaytotal  >'13')  {
	  
	    $frinormal = '9';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($fridaytotal - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
		if ($sattotal  <= '9')  {
		
		$satnormal = $sattotal ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (( $sattotal > '9') &&($sattotal  <= '13')) {
		
		$sattimeandhalfhours = ($sattotal - '9');
		$satnormal = '9';
		$satdouble = '0';
		}
	
	
		if ( $sattotal  >'13')  {
	  
	    $satnormal = '9';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($sattotal  - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		if ( $suntotal <= '9')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '9') &&($suntotal <= '13')) {
		
		$suntimeandhalfhours = ($suntotal - '9');
		$sunnormal = '9';
		$sundouble = '0';
		}
	
	
		if ( $suntotal >'13')  {
	  
	    $sunnormal = '9';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		}    
		if ( $mon2hours <= '9')  {
		
		$monnormal2 = $mon2hours;
		$montimeandhalfhours2 = '0';
		$mondouble2 = '0';
		
     	}
	
		if (($mon2hours > '9') &&($mon2hours <= '13')) {
		
		$montimeandhalfhours2 = ($mon2hours - '9');
		$monnormal2 = '9';
		$mondouble2 = '0';
		}
	
	
		if ($mon2hours >'13')  {
	  
	    $monnormal2 = '9';
		$montimeandhalfhours2 = '4';
	  
		$totalovertimehours = ($mon2hours - $monnormal2 );
	  	   
	    
	    $mondouble2 = $totalovertimehours - $montimeandhalfhours2;
	  
	  
		}
   
		 
	
       
	 //$postdata->set('holiday', $holiday);
	
//this is for 12 hour shift adjustment to allow the daily hours be the same but the calulations on tallies changes.
	
if (isset($montime_out1) && isset($tuestime_in) && $shifttweleve == '-23'  ){
	
//this is one shift that comes under monday	  
	
        $mondaytotal = $monhours +$monhours2+$tueshours;
	
	
		if ($mondaytotal <= '9')  {
		
		$monnormal = $mondaytotal;
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($mondaytotal > '9') &&($mondaytotal <= '13')) {
		
		$montimeandhalfhours = ($mondaytotal - '9');
		$monnormal = '9';
		$mondouble = '0';
		}
	
	
		if ($mondaytotal >'13')  {
	  
	    $monnormal = '9';
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($mondaytotal- $monnormal );
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
		
	 $tuesdaytotal = $tuesdaytotal - $tueshours;
	if ($tuesdaytotal <= '9')  {
		
		$tuesnormal = $tuesdaytotal;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdaytotal > '9') &&($tuesdaytotal <= '13')) {
		
		$tuestimeandhalfhours = ($tuesdaytotal - '9');
		$tuesnormal = '9';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdaytotal >'13')  {
	  
	    $tuesnormal = '9';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdaytotal - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		
}
	
	//when the previous day  is not 12 hour shift
 
if (isset($tuestime_out1) && isset($wedtime_in) && $shifttwelevetues == '-23' && $shifttweleve !== '-23'  ){
	
	
	$tuesdaytotal = $tueshours + $tueshours2+$wedhours;
	
	if ($tuesdaytotal <= '9')  {
		
		$tuesnormal = $tuesdaytotal;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdaytotal > '9') &&($tuesdaytotal <= '13')) {
		
		$tuestimeandhalfhours = ($tuesdaytotal - '9');
		$tuesnormal = '9';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdaytotal >'13')  {
	  
	    $tuesnormal = '9';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdaytotal - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
	$wednesdaytotal= $wednesdaytotal - $wedhours;
	if ($wednesdaytotal <= '9')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '9') &&($wednesdaytotal<= '13')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '9');
		$wednormal = '9';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal>'13')  {
	  
	    $wednormal = '9';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
	
	
}

 if (isset($wedtime_out1) && isset($thurstime_in) && $shifttwelevewed == '-23' && $shifttwelevetues !== '-23' ){
	 $wednesdaytotal = $wedhours + $wedhours2+$thurshours;
	 if ($wednesdaytotal <= '9')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '9') &&($wednesdaytotal <= '13')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '9');
		$wednormal = '9';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal >'13')  {
	  
	    $wednormal = '9';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		$thursdaytotal = $thursdaytotal -$thurshours;
		if ($thursdaytotal <= '9')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '9') &&($thursdaytotal <= '13')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '9');
		$thursnormal = '9';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'13')  {
	  
	    $thursnormal = '9';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
	
	
}
  
	
	if (isset($thurstime_out1) && isset($fritime_in) && $shifttwelevethurs == '-23' && $shifttwelevewed !== '-23'  ){
	
	 $thursdaytotal = $thurshours + $thurshours2+$frihours;
	if ($thursdaytotal <= '9')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '9') &&($thursdaytotal <= '13')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '9');
		$thursnormal = '9';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'13')  {
	  
	    $thursnormal = '9';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		$fridaytotal = $fridaytotal - $frihours;
		
	 if ($fridaytotal <= '9')  {
		
		$frinormal = $fridaytotal ;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($fridaytotal  > '9') &&($fridaytotal  <= '13')) {
		
		$fritimeandhalfhours = ($fridaytotal  - '9');
		$frinormal = '9';
		$fridouble = '0';
		}
	
	
		if ($fridaytotal  >'13')  {
	  
	    $frinormal = '9';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($fridaytotal - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
	
}

	if (isset($fritime_out1) && isset($sattime_in) && $shifttwelevefri == '-23' && $shifttwelevethurs !== '-23' ){
	
	$fridaytotal = $frihours +  $frihours2+$sathours;
	
	if ($fridaytotal  <= '9')  {
		
		$frinormal  = $fridaytotal  ;
		$fritimeandhalfhours  = '0';
		$fridouble  = '0';
		
     	}
	
		if (($fridaytotal   > '9') &&($fridaytotal   <= '13')) {
		
		$fritimeandhalfhours  = ($fridaytotal   - '9');
		$frinormal  = '9';
		$fridouble  = '0';
		}
	
	
		if ($fridaytotal   >'13')  {
	  
	    $frinormal  = '9';
		$fritimeandhalfhours  = '4';
	  
		$totalovertimehours  = ($fridaytotal  - $frinormal  );
	  	   
	    
	    $fridouble  = $totalovertimehours  - $fritimeandhalfhours ;
	  
	  
		}    
	$sattotal = $sattotal - $sathours;
if ($sattotal  <= '9')  {
		
		$satnormal = $sattotal ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (($sattotal > '9') &&($sattotal  <= '13')) {
		
		$sattimeandhalfhours = ($sattotal - '9');
		$satnormal = '9';
		$satdouble = '0';
		}
	
	
		if ($sattotal  >'13')  {
	  
	    $satnormal = '9';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($sattotal  - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}  
		
		
		
		
}
	
if (isset($sattime_out1) && isset($suntime_in) && $shifttwelevesat == '-23' && $shifttwelevefri !== '-23' ){
	
	
	$sattotal = $sathours + $sathours5+$sunhours;
	
	if ($sattotal   <= '9')  {
		
		$satnormal  = $sattotal  ;
		$sattimeandhalfhours  = '0';
		$satdouble  = '0';
		
     	}
	
		if (($sattotal  > '9') &&($sattotal   <= '13')) {
		
		$sattimeandhalfhours  = ($sattotal  - '9');
		$satnormal  = '9';
		$satdouble  = '0';
		}
	
	
		if ($sattotal   >'13')  {
	  
	    $satnormal  = '9';
		$sattimeandhalfhours  = '4';
	  
		$totalovertimehours  = ($sattotal   - $satnormal  );
	  	   
	    
	    $satdouble  = $totalovertimehours  - $sattimeandhalfhours ;
	  
	  
		}  
	
		$suntotal = $suntotal -$sunhours;
		if ($suntotal <= '9')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '9') &&($suntotal <= '13')) {
		
		$suntimeandhalfhours = ($suntotal - '9');
		$sunnormal = '9';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'13')  {
	  
	    $sunnormal = '9';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		} 
	
}	
	if (isset($suntime_out1) && isset($mon2time_in) && $shifttwelevesun == '-23' && $shifttwelevesat !== '-23' ){
	
	
	$suntotal = $sunhours + $sunhours2+$mon2hours;
	
	if ($suntotal <= '9')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '9') &&($suntotal <= '13')) {
		
		$suntimeandhalfhours = ($suntotal - '9');
		$sunnormal = '9';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'13')  {
	  
	    $sunnormal = '9';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		} 
		
		$mon2hours = '0';
		$monnormal2 = '0';
		$montimeandhalfhours2 = '0';
		$mondouble2 = '0';
	
}	
	
	
	//when previous day is 12 hour shift also then dont add the shift hour totals since its already been added to previous day
	
	if (isset($tuestime_out1) && isset($wedtime_in) && $shifttwelevetues == '-23' && $shifttweleve == '-23'  ){
	
	
	$tuesdaytotal = $tueshours2+$wedhours;
	
	if ($tuesdaytotal <= '9')  {
		
		$tuesnormal = $tuesdaytotal;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdaytotal > '9') &&($tuesdaytotal <= '13')) {
		
		$tuestimeandhalfhours = ($tuesdaytotal - '9');
		$tuesnormal = '9';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdaytotal >'13')  {
	  
	    $tuesnormal = '9';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdaytotal - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
	$wednesdaytotal= $wednesdaytotal - $wedhours;
	if ($wednesdaytotal <= '9')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '9') &&($wednesdaytotal<= '13')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '9');
		$wednormal = '9';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal>'13')  {
	  
	    $wednormal = '9';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
	
	
}

 if (isset($wedtime_out1) && isset($thurstime_in) && $shifttwelevewed == '-23' && $shifttwelevetues == '-23' ){
	 $wednesdaytotal = $wedhours2+$thurshours;
	 if ($wednesdaytotal <= '9')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '9') &&($wednesdaytotal <= '13')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '9');
		$wednormal = '9';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal >'13')  {
	  
	    $wednormal = '9';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		$thursdaytotal = $thursdaytotal -$thurshours;
		if ($thursdaytotal <= '9')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '9') &&($thursdaytotal <= '13')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '9');
		$thursnormal = '9';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'13')  {
	  
	    $thursnormal = '9';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
	
	
}
  
	
	if (isset($thurstime_out1) && isset($fritime_in) && $shifttwelevethurs == '-23' && $shifttwelevewed == '-23'  ){
	
	 $thursdaytotal = $thurshours2+$frihours;
	if ($thursdaytotal <= '9')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '9') &&($thursdaytotal <= '13')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '9');
		$thursnormal = '9';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'13')  {
	  
	    $thursnormal = '9';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		$fridaytotal = $fridaytotal - $frihours;
		
	 if ($fridaytotal <= '9')  {
		
		$frinormal = $fridaytotal ;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($fridaytotal  > '9') &&($fridaytotal  <= '13')) {
		
		$fritimeandhalfhours = ($fridaytotal  - '9');
		$frinormal = '9';
		$fridouble = '0';
		}
	
	
		if ($fridaytotal  >'13')  {
	  
	    $frinormal = '9';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($fridaytotal - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
	
}

	if (isset($fritime_out1) && isset($sattime_in) && $shifttwelevefri == '-23' && $shifttwelevethurs == '-23' ){
	
	$fridaytotal =  $frihours2+$sathours;
	
	if ($fridaytotal  <= '9')  {
		
		$frinormal  = $fridaytotal  ;
		$fritimeandhalfhours  = '0';
		$fridouble  = '0';
		
     	}
	
		if (($fridaytotal   > '9') &&($fridaytotal   <= '13')) {
		
		$fritimeandhalfhours  = ($fridaytotal   - '9');
		$frinormal  = '9';
		$fridouble  = '0';
		}
	
	
		if ($fridaytotal   >'13')  {
	  
	    $frinormal  = '9';
		$fritimeandhalfhours  = '4';
	  
		$totalovertimehours  = ($fridaytotal  - $frinormal  );
	  	   
	    
	    $fridouble  = $totalovertimehours  - $fritimeandhalfhours ;
	  
	  
		}    
	$sattotal = $sattotal - $sathours;
if ($sattotal  <= '9')  {
		
		$satnormal = $sattotal ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (($sattotal > '9') &&($sattotal  <= '13')) {
		
		$sattimeandhalfhours = ($sattotal - '9');
		$satnormal = '9';
		$satdouble = '0';
		}
	
	
		if ($sattotal  >'13')  {
	  
	    $satnormal = '9';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($sattotal  - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}  
		
		
		
		
}
	
if (isset($sattime_out1) && isset($suntime_in) && $shifttwelevesat == '-23' && $shifttwelevefri == '-23' ){
	
	
	$sattotal = $sathours5+$sunhours;
	
	if ($sattotal   <= '9')  {
		
		$satnormal  = $sattotal  ;
		$sattimeandhalfhours  = '0';
		$satdouble  = '0';
		
     	}
	
		if (($sattotal  > '9') &&($sattotal   <= '13')) {
		
		$sattimeandhalfhours  = ($sattotal  - '9');
		$satnormal  = '9';
		$satdouble  = '0';
		}
	
	
		if ($sattotal   >'13')  {
	  
	    $satnormal  = '9';
		$sattimeandhalfhours  = '4';
	  
		$totalovertimehours  = ($sattotal   - $satnormal  );
	  	   
	    
	    $satdouble  = $totalovertimehours  - $sattimeandhalfhours ;
	  
	  
		}  
	
		$suntotal = $suntotal -$sunhours;
		if ($suntotal <= '9')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '9') &&($suntotal <= '13')) {
		
		$suntimeandhalfhours = ($suntotal - '9');
		$sunnormal = '9';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'13')  {
	  
	    $sunnormal = '9';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		} 
	
	//$sunhours = '0';
	
}	
	if (isset($suntime_out1) && isset($mon2time_in) && $shifttwelevesun == '-23' && $shifttwelevesat == '-23' ){
	
	
	$suntotal = $sunhours2+$mon2hours;
	
	if ($suntotal <= '9')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '9') &&($suntotal <= '13')) {
		
		$suntimeandhalfhours = ($suntotal - '9');
		$sunnormal = '9';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'13')  {
	  
	    $sunnormal = '9';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		} 
	
		$mon2hours = '0';
		$monnormal2 = '0';
		$montimeandhalfhours2 = '0';
		$mondouble2 = '0';
}	
	
	if (isset($mon2time_in) && $shifttwelevesun == '-23'){
	
	$mon2hours = '0';
	$monnormal2 = '0';
		$montimeandhalfhours2 = '0';
		$mondouble2 = '0';
		
		
	
}
	

	

 $totalhours = $mondaytotal+$tuesdaytotal+$wednesdaytotal+$thursdaytotal+$fridaytotal+$sattotal+$suntotal+$mon2hours;
	
		$postdata->set('total_hrs', $totalhours);
	
		$totalnormalhours = $monnormal +$tuesnormal+$wednormal+$thursnormal+$frinormal+$satnormal+$sunnormal+$monnormal2;
	$totaltimeandhalf = $montimeandhalfhours + $tuestimeandhalfhours +$wedtimeandhalfhours+$thurstimeandhalfhours+$fritimeandhalfhours+$sattimeandhalfhours+$suntimeandhalfhours+$montimeandhalfhours2 ;
	
	$totaldouble = $mondouble+$tuesdouble+$weddouble+$thursdouble+$fridouble+$satdouble+$sundouble+$mondouble2;
	
	$postdata->set('normal_hours', $totalnormalhours);
	 $postdata->set('time_half_hours', $totaltimeandhalf);
	 $postdata->set('double_hours', $totaldouble);	
	
	
				
	if ($totalhours > $maxhours){
    
    $xcrud->set_exception('total_hrs','Hours Exceeded!','error'); 
		}
	
	
}
	//calcualate payroll timesheet data if the contract for staffs is 9 hours a day and 5 days
elseif ($contract !== $nine ) {

if ($mondaytotal <= '8')  {
		
		$monnormal = $mondaytotal;
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($mondaytotal > '8') &&($mondaytotal <= '12')) {
		
		$montimeandhalfhours = ($mondaytotal - '8');
		$monnormal = '8';
		$mondouble = '0';
		}
	
	
		if ($mondaytotal >'12')  {
	  
	    $monnormal = '8';
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($mondaytotal- $monnormal );
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
	
	 if ($tuesdaytotal <= '8')  {
		
		$tuesnormal = $tuesdaytotal;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdaytotal > '8') &&($tuesdaytotal <= '12')) {
		
		$tuestimeandhalfhours = ($tuesdaytotal - '8');
		$tuesnormal = '8';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdaytotal >'12')  {
	  
	    $tuesnormal = '8';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdaytotal - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		 if ($wednesdaytotal <= '8')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '8') &&($wednesdaytotal<= '12')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '8');
		$wednormal = '8';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal>'12')  {
	  
	    $wednormal = '8';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		
		if ($thursdaytotal <= '8')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '8') &&($thursdaytotal <= '12')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '8');
		$thursnormal = '8';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'12')  {
	  
	    $thursnormal = '8';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
		
	 if ($fridaytotal <= '8')  {
		
		$frinormal = $fridaytotal ;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($fridaytotal  > '8') &&($fridaytotal  <= '12')) {
		
		$fritimeandhalfhours = ($fridaytotal  - '8');
		$frinormal = '8';
		$fridouble = '0';
		}
	
	
		if ($fridaytotal  >'12')  {
	  
	    $frinormal = '8';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($fridaytotal - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
		if ($sattotal  <= '8')  {
		
		$satnormal = $sattotal ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (($sattotal > '8') &&($sattotal  <= '12')) {
		
		$sattimeandhalfhours = ($sattotal - '8');
		$satnormal = '8';
		$satdouble = '0';
		}
	
	
		if ($sattotal  >'12')  {
	  
	    $satnormal = '8';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($sattotal  - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}    
		if ($suntotal <= '8')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '8') &&($suntotal <= '12')) {
		
		$suntimeandhalfhours = ($suntotal - '8');
		$sunnormal = '8';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'12')  {
	  
	    $sunnormal = '8';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		}    
		if ($mon2hours <= '8')  {
		
		$monnormal2 = $mon2hours;
		$montimeandhalfhours2 = '0';
		$mondouble2 = '0';
		
     	}
	
		if (($mon2hours > '8') &&($mon2hours <= '12')) {
		
		$montimeandhalfhours2 = ($mon2hours - '8');
		$monnormal2 = '8';
		$mondouble2 = '0';
		}
	
	
		if ($mon2hours >'12')  {
	  
	    $monnormal2 = '8';
		$montimeandhalfhours2 = '4';
	  
		$totalovertimehours = ($mon2hours - $monnormal2 );
	  	   
	    
	    $mondouble2 = $totalovertimehours - $montimeandhalfhours2;
	  
	  
		}
if (isset($montime_out1) && isset($tuestime_in) && $shifttweleve == '-23'  ){
	
//this is one shift that comes under monday	  
	
        $mondaytotal = $monhours +$monhours2+$tueshours;
	
	
		if ($mondaytotal <= '8')  {
		
		$monnormal = $mondaytotal;
		$montimeandhalfhours = '0';
		$mondouble = '0';
		
     	}
	
		if (($mondaytotal > '8') &&($mondaytotal <= '12')) {
		
		$montimeandhalfhours = ($mondaytotal - '8');
		$monnormal = '8';
		$mondouble = '0';
		}
	
	
		if ($mondaytotal >'12')  {
	  
	    $monnormal = '8';
		$montimeandhalfhours = '4';
	  
		$totalovertimehours = ($mondaytotal- $monnormal );
	  	   
	    
	    $mondouble = $totalovertimehours - $montimeandhalfhours;
	  
	  
		}
		
	 $tuesdaytotal = $tuesdaytotal - $tueshours;
	if ($tuesdaytotal <= '8')  {
		
		$tuesnormal = $tuesdaytotal;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdaytotal > '8') &&($tuesdaytotal <= '12')) {
		
		$tuestimeandhalfhours = ($tuesdaytotal - '8');
		$tuesnormal = '8';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdaytotal >'12')  {
	  
	    $tuesnormal = '8';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdaytotal - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
		
}
 //when the previous day  is not 12 hour shift
 
if (isset($tuestime_out1) && isset($wedtime_in) && $shifttwelevetues == '-23' && $shifttweleve !== '-23'  ){
	
	
	$tuesdaytotal = $tueshours + $tueshours2+$wedhours;
	
	if ($tuesdaytotal <= '8')  {
		
		$tuesnormal = $tuesdaytotal;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdaytotal > '8') &&($tuesdaytotal <= '12')) {
		
		$tuestimeandhalfhours = ($tuesdaytotal - '8');
		$tuesnormal = '8';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdaytotal >'12')  {
	  
	    $tuesnormal = '8';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdaytotal - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
	$wednesdaytotal= $wednesdaytotal - $wedhours;
	if ($wednesdaytotal <= '8')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '8') &&($wednesdaytotal<= '12')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '8');
		$wednormal = '8';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal>'12')  {
	  
	    $wednormal = '8';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
	
	
}

 if (isset($wedtime_out1) && isset($thurstime_in) && $shifttwelevewed == '-23' && $shifttwelevetues !== '-23' ){
	 $wednesdaytotal = $wedhours + $wedhours2+$thurshours;
	 if ($wednesdaytotal <= '8')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '8') &&($wednesdaytotal <= '12')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '8');
		$wednormal = '8';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal >'12')  {
	  
	    $wednormal = '8';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		$thursdaytotal = $thursdaytotal -$thurshours;
		if ($thursdaytotal <= '8')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '8') &&($thursdaytotal <= '12')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '8');
		$thursnormal = '8';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'12')  {
	  
	    $thursnormal = '8';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
	
	
}
  
	
	if (isset($thurstime_out1) && isset($fritime_in) && $shifttwelevethurs == '-23' && $shifttwelevewed !== '-23'  ){
	
	 $thursdaytotal = $thurshours + $thurshours2+$frihours;
	if ($thursdaytotal <= '8')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '8') &&($thursdaytotal <= '12')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '8');
		$thursnormal = '8';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'12')  {
	  
	    $thursnormal = '8';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		$fridaytotal = $fridaytotal - $frihours;
		
	 if ($fridaytotal <= '8')  {
		
		$frinormal = $fridaytotal ;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($fridaytotal  > '8') &&($fridaytotal  <= '12')) {
		
		$fritimeandhalfhours = ($fridaytotal  - '8');
		$frinormal = '8';
		$fridouble = '0';
		}
	
	
		if ($fridaytotal  >'12')  {
	  
	    $frinormal = '8';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($fridaytotal - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
	
}

	if (isset($fritime_out1) && isset($sattime_in) && $shifttwelevefri == '-23' && $shifttwelevethurs !== '-23' ){
	
	$fridaytotal = $frihours +  $frihours2+$sathours;
	
	if ($fridaytotal  <= '8')  {
		
		$frinormal  = $fridaytotal  ;
		$fritimeandhalfhours  = '0';
		$fridouble  = '0';
		
     	}
	
		if (($fridaytotal   > '8') &&($fridaytotal   <= '12')) {
		
		$fritimeandhalfhours  = ($fridaytotal   - '8');
		$frinormal  = '8';
		$fridouble  = '0';
		}
	
	
		if ($fridaytotal   >'12')  {
	  
	    $frinormal  = '8';
		$fritimeandhalfhours  = '4';
	  
		$totalovertimehours  = ($fridaytotal  - $frinormal  );
	  	   
	    
	    $fridouble  = $totalovertimehours  - $fritimeandhalfhours ;
	  
	  
		}    
	$sattotal = $sattotal - $sathours;
if ($sattotal  <= '8')  {
		
		$satnormal = $sattotal ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (($sattotal > '8') &&($sattotal  <= '12')) {
		
		$sattimeandhalfhours = ($sattotal - '8');
		$satnormal = '8';
		$satdouble = '0';
		}
	
	
		if ($sattotal  >'12')  {
	  
	    $satnormal = '8';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($sattotal  - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}  
		
		
		
		
}
	
if (isset($sattime_out1) && isset($suntime_in) && $shifttwelevesat == '-23' && $shifttwelevefri !== '-23' ){
	
	
	$sattotal = $sathours + $sathours5+$sunhours;
	
	if ($sattotal   <= '8')  {
		
		$satnormal  = $sattotal  ;
		$sattimeandhalfhours  = '0';
		$satdouble  = '0';
		
     	}
	
		if (($sattotal  > '8') &&($sattotal   <= '12')) {
		
		$sattimeandhalfhours  = ($sattotal  - '8');
		$satnormal  = '8';
		$satdouble  = '0';
		}
	
	
		if ($sattotal   >'12')  {
	  
	    $satnormal  = '8';
		$sattimeandhalfhours  = '4';
	  
		$totalovertimehours  = ($sattotal   - $satnormal  );
	  	   
	    
	    $satdouble  = $totalovertimehours  - $sattimeandhalfhours ;
	  
	  
		}  
	
		$suntotal = $suntotal -$sunhours;
		if ($suntotal <= '8')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '8') &&($suntotal <= '12')) {
		
		$suntimeandhalfhours = ($suntotal - '8');
		$sunnormal = '8';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'12')  {
	  
	    $sunnormal = '8';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		} 
	
}	
	if (isset($suntime_out1) && isset($mon2time_in) && $shifttwelevesun == '-23' && $shifttwelevesat !== '-23' ){
	
	
	$suntotal = $sunhours + $sunhours2+$mon2hours;
	
	if ($suntotal <= '8')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '8') &&($suntotal <= '12')) {
		
		$suntimeandhalfhours = ($suntotal - '8');
		$sunnormal = '8';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'12')  {
	  
	    $sunnormal = '8';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		} 
	
		
		
}	
	
	
	//when previous day is 12 hour shift also then dont add the shift hour totals since its already been added to previous day
	
	if (isset($tuestime_out1) && isset($wedtime_in) && $shifttwelevetues == '-23' && $shifttweleve == '-23'  ){
	
	
	$tuesdaytotal = $tueshours2+$wedhours;
	
	if ($tuesdaytotal <= '8')  {
		
		$tuesnormal = $tuesdaytotal;
		$tuestimeandhalfhours = '0';
		$tuesdouble = '0';
		
     	}
	
		if (($tuesdaytotal > '8') &&($tuesdaytotal <= '12')) {
		
		$tuestimeandhalfhours = ($tuesdaytotal - '8');
		$tuesnormal = '8';
		$tuesdouble = '0';
		}
	
	
		if ($tuesdaytotal >'12')  {
	  
	    $tuesnormal = '8';
		$tuestimeandhalfhours = '4';
	  
		$totalovertimehours = ($tuesdaytotal - $tuesnormal );
	  	   
	    
	    $tuesdouble = $totalovertimehours - $tuestimeandhalfhours;
	  
	  
		}
	$wednesdaytotal= $wednesdaytotal - $wedhours;
	if ($wednesdaytotal <= '8')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '8') &&($wednesdaytotal<= '12')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '8');
		$wednormal = '8';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal>'12')  {
	  
	    $wednormal = '8';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
	
	
}

 if (isset($wedtime_out1) && isset($thurstime_in) && $shifttwelevewed == '-23' && $shifttwelevetues == '-23' ){
	 $wednesdaytotal = $wedhours2+$thurshours;
	 if ($wednesdaytotal <= '8')  {
		
		$wednormal = $wednesdaytotal;
		$wedtimeandhalfhours = '0';
		$weddouble = '0';
		
     	}
	
		if (($wednesdaytotal > '8') &&($wednesdaytotal <= '12')) {
		
		$wedtimeandhalfhours = ($wednesdaytotal- '8');
		$wednormal = '8';
		$weddouble = '0';
		}
	
	
		if ($wednesdaytotal >'12')  {
	  
	    $wednormal = '8';
		$wedtimeandhalfhours = '4';
	  
		$totalovertimehours = ($wednesdaytotal - $wednormal );
	  	   
	    
	    $weddouble = $totalovertimehours - $wedtimeandhalfhours;
	  
	  
		}
		$thursdaytotal = $thursdaytotal -$thurshours;
		if ($thursdaytotal <= '8')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '8') &&($thursdaytotal <= '12')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '8');
		$thursnormal = '8';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'12')  {
	  
	    $thursnormal = '8';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		
	
	
}
  
	
	if (isset($thurstime_out1) && isset($fritime_in) && $shifttwelevethurs == '-23' && $shifttwelevewed == '-23'  ){
	
	 $thursdaytotal = $thurshours2+$frihours;
	if ($thursdaytotal <= '8')  {
		
		$thursnormal = $thursdaytotal;
		$thurstimeandhalfhours = '0';
		$thursdouble = '0';
		
     	}
	
		if (($thursdaytotal > '8') &&($thursdaytotal <= '12')) {
		
		$thurstimeandhalfhours = ($thursdaytotal - '8');
		$thursnormal = '8';
		$thursdouble = '0';
		}
	
	
		if ($thursdaytotal >'12')  {
	  
	    $thursnormal = '8';
		$thurstimeandhalfhours = '4';
	  
		$totalovertimehours = ($thursdaytotal - $thursnormal );
	  	   
	    
	    $thursdouble = $totalovertimehours - $thurstimeandhalfhours;
	  
	  
		}
		$fridaytotal = $fridaytotal - $frihours;
		
	 if ($fridaytotal <= '8')  {
		
		$frinormal = $fridaytotal ;
		$fritimeandhalfhours = '0';
		$fridouble = '0';
		
     	}
	
		if (($fridaytotal  > '8') &&($fridaytotal  <= '12')) {
		
		$fritimeandhalfhours = ($fridaytotal  - '8');
		$frinormal = '8';
		$fridouble = '0';
		}
	
	
		if ($fridaytotal  >'12')  {
	  
	    $frinormal = '8';
		$fritimeandhalfhours = '4';
	  
		$totalovertimehours = ($fridaytotal - $frinormal );
	  	   
	    
	    $fridouble = $totalovertimehours - $fritimeandhalfhours;
	  
	  
		}    
	
}

	if (isset($fritime_out1) && isset($sattime_in) && $shifttwelevefri == '-23' && $shifttwelevethurs == '-23' ){
	
	$fridaytotal =  $frihours2+$sathours;
	
	if ($fridaytotal  <= '8')  {
		
		$frinormal  = $fridaytotal  ;
		$fritimeandhalfhours  = '0';
		$fridouble  = '0';
		
     	}
	
		if (($fridaytotal   > '8') &&($fridaytotal   <= '12')) {
		
		$fritimeandhalfhours  = ($fridaytotal   - '8');
		$frinormal  = '8';
		$fridouble  = '0';
		}
	
	
		if ($fridaytotal   >'12')  {
	  
	    $frinormal  = '8';
		$fritimeandhalfhours  = '4';
	  
		$totalovertimehours  = ($fridaytotal  - $frinormal  );
	  	   
	    
	    $fridouble  = $totalovertimehours  - $fritimeandhalfhours ;
	  
	  
		}    
	$sattotal = $sattotal - $sathours;
if ($sattotal  <= '8')  {
		
		$satnormal = $sattotal ;
		$sattimeandhalfhours = '0';
		$satdouble = '0';
		
     	}
	
		if (($sattotal > '8') &&($sattotal  <= '12')) {
		
		$sattimeandhalfhours = ($sattotal - '8');
		$satnormal = '8';
		$satdouble = '0';
		}
	
	
		if ($sattotal  >'12')  {
	  
	    $satnormal = '8';
		$sattimeandhalfhours = '4';
	  
		$totalovertimehours = ($sattotal  - $satnormal );
	  	   
	    
	    $satdouble = $totalovertimehours - $sattimeandhalfhours;
	  
	  
		}  
		
		
		
		
}
	
if (isset($sattime_out1) && isset($suntime_in) && $shifttwelevesat == '-23' && $shifttwelevefri == '-23' ){
	
	
	$sattotal = $sathours5+$sunhours;
	
	if ($sattotal   <= '8')  {
		
		$satnormal  = $sattotal  ;
		$sattimeandhalfhours  = '0';
		$satdouble  = '0';
		
     	}
	
		if (($sattotal  > '8') &&($sattotal   <= '12')) {
		
		$sattimeandhalfhours  = ($sattotal  - '8');
		$satnormal  = '8';
		$satdouble  = '0';
		}
	
	
		if ($sattotal   >'12')  {
	  
	    $satnormal  = '8';
		$sattimeandhalfhours  = '4';
	  
		$totalovertimehours  = ($sattotal   - $satnormal  );
	  	   
	    
	    $satdouble  = $totalovertimehours  - $sattimeandhalfhours ;
	  
	    
		}  
	     $sunhours = '0';
		$suntotal = $suntotal -$sunhours;
		if ($suntotal <= '8')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '8') &&($suntotal <= '12')) {
		
		$suntimeandhalfhours = ($suntotal - '8');
		$sunnormal = '8';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'12')  {
	  
	    $sunnormal = '8';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		} 
	
}	
	if (isset($suntime_out1) && isset($mon2time_in) && $shifttwelevesun == '-23' && $shifttwelevesat == '-23' ){
	
	
	$suntotal = $sunhours2+$mon2hours;
	

	if ($suntotal <= '8')  {
		
		$sunnormal = $suntotal;
		$suntimeandhalfhours = '0';
		$sundouble = '0';
		
     	}
	
		if (($suntotal > '8') &&($suntotal <= '12')) {
		
		$suntimeandhalfhours = ($suntotal - '8');
		$sunnormal = '8';
		$sundouble = '0';
		}
	
	
		if ($suntotal >'12')  {
	  
	    $sunnormal = '8';
		$suntimeandhalfhours = '4';
	  
		$totalovertimehours = ($suntotal - $sunnormal );
	  	   
	    
	    $sundouble = $totalovertimehours - $suntimeandhalfhours;
	  
	  
		} 
		//$xcrud->set_exception('total_hrs',$mon2hours,'error');
		 
	
}
	if ( $shifttwelevesun == '-23'){
	
	$mon2hours = '0';
	$monnormal2 = '0';
		$montimeandhalfhours2 = '0';
		$mondouble2 = '0';
		
	
		
	
}
	
$totalhours = $mondaytotal+$tuesdaytotal+$wednesdaytotal+$thursdaytotal+$fridaytotal+$sattotal+$suntotal+$mon2hours;
	//$xcrud->set_exception('total_hrs',$fridaytotal,'error');
		$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){
    
    $xcrud->set_exception('total_hrs','Hours Exceeded!','error'); 
		}
		
}
	
	
//get all values inserted in another table to track pool guards
	
	 $dbpool = Xcrud_db::get_instance();
	    $querypool = "UPDATE  poolguards set 
		                     rostered =	'1' where start_date = '".$start."'
							 AND employeeid  =   '".$employeeid."'
		";
				
        
        $dbpool->query($querypool);

	
	
}

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

	$unitprice = $postdata->get('unit_price');
   $qty = $postdata->get('qty');
	$total= $unitprice * $qty;
	
	
	  $postdata->set('total', $total);
	
	//create entries in a poolguard table that will be used to roster guards from and keep a record of which one used and use one is not.
	
	
	
	
}


function poolguardcreation($xcrud){

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

	
	//create entries in a poolguard table that will be used to roster guards from and keep a record of which one used and use one is not.
	
	
	
	
}
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){
	
	

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

	
}

function calculate_totalhoursperweek($postdata,$rostering_timesheet,$xcrud,$ebusiness){
	
	$rstimeid = $postdata->get('rstimeid');
	$rosterid = $postdata->get('rosterid');
	$scheduleid = $postdata->get('rstimeid');
	
	$employeeid = $postdata->get('employeeid');
	
	
		//get calender id
	
	
	$dbcalendar = Xcrud_db::get_instance();
	$querycalendar = "SELECT id
					  
		          FROM rostering
				  				  
				   WHERE rosterid = '".$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');
	
	
	
	
	
	$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);
	
	
	  
	$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
	
	$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);
	
	
	
	// get mon-mon2 dates
	
	 $db2 = Xcrud_db::get_instance();
	    $query = "SELECT calendar_management.start_date, calendar_management.end_date,rostering.required_guards
					  
		          FROM calendar_management INNER JOIN 
				  rostering on calendar_management.id = rostering.id
				  			  				  
				   WHERE  rostering.rosterid = '".$rosterid."'";
        
        $db2->query($query);
        $outm2 = $db2->result();
        foreach($outm2 as $outputm2){
			$start = $outputm2['start_date'];
			$end = $outputm2['end_date'];
			$requireguards = $outputm2['required_guards'];
           			
        } 
	
	//set deafult values for dates for each day
	
	//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);
	
	
	
	//we know the dates now for all days so we going to check day and time to see if anyone has already been rostered at this time
	
	//check for mondays if a employee has already occupied on a time 
	//mon
	
	// $db3 = Xcrud_db::get_instance();
	   // $query = "SELECT employeeid
					  
		     //     FROM rosteringtimesheet
				  				  
				//   WHERE employeeid = '".$employeeid."'
				//   AND mon_date = '".$mondate."'
				//   AND montime_in BETWEEN '".$montime_in."'AND '".$montime_out."'
				  
				 //  ";
        
       // $db3->query($query);
       // $outm3 = $db3->result();
       // foreach($outm3 as $outputm3){
			
		//	$employecheckmon = $outputm3['employeeid'];
		
	
  
		///}
	// if (isset($employecheckmon)) {
			
			
	//	$xcrud->set_exception('employeeid','This Staff is already working on Monday at that time range at another location'); 
			
	//	} 
		
	
	
	
	//mon1
	
	
	//eight hour gap check mon
	
	//$db500 = Xcrud_db::get_instance();
	    //$query500 = "SELECT montime_out
		     //     FROM rosteringtimesheet
				//   WHERE  employeeid = '".$employeeid."'
				//   AND mon_date = '".$mondate."'
				//   ";
        
       // $db500->query($query);
       // $outm500 = $db500->result();
       // foreach($outm500 as $outputm500){
			
		//	$moneighthoursgap = date('H:i:s',strtotime('+8 hour',strtotime($outm500['montime_out'])));   

		//}
	
	//if ($montime_in <=  $moneighthoursgap) {
			
			
		//$xcrud->set_exception('employeeid','This Staff has not meet the 8 hour gap yet'.$moneighthoursgap); 
			
		//}
	
	//quality check on monday shift 2
	
	
	
	
 

	 $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 bulk_delete($xcrud)
{
	//print_r($xcrud.selected);
	$selected = $xcrud->get('selected');
	$table = $xcrud->get('table');
	$identifier = $xcrud->get('identifier');
	$cnt = count($selected); 
	
	$insertString = "";
	$count = 0;
	foreach($selected as $value){
		$count++;
		if($count == 1){	
			$insertString .= $value; 
		}else{
			$insertString .= "," . $value;
		}		
	}
	   
    $db = Xcrud_db::get_instance();
    $query = "DELETE from $table WHERE $identifier IN ($insertString)" ;
	//echo $query;
    $db->query($query);
   
    $xcrud->set_exception('Bulk Delete', 'You have Deleted ' . $cnt . ' items', 'error');
	//$xcrud->set_exception('You have deleted ' . $cnt . ' items', 'error');
}

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 `officeCode` FROM `offices` ORDER BY `ordering`,`officeCode`';
        $db->query($query);
        $result = $db->result();
        $count = count($result);

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

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

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

        foreach ($result as $key => $item)
        {
            $query = 'UPDATE `offices` SET `ordering` = ' . $key . ' WHERE officeCode = ' . $item['officeCode'];
            $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)
{
    return '<input type="text" readonly class="xcrud-input" name="' . $xcrud->fieldname_encode($fieldname) . '" 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';
}*/

