<?php 
class leaveClassAction extends Action{
	
	
	public function publicbeforesave($table, $arr, $id)
	{
		$msg 	= '';
		$start 	= $arr['stime'];
		$end 	= $arr['etime'];
		$db		= m($table);
		$sdf 	= $db->rows("`uid`='".$arr['uid']."' and ((`stime`<='$start' and `etime`>='$start') or (`stime`<='$end' and `etime`>='$end') or (`stime`>='$start' and `etime`<='$end')) and `kind`='请假' and `id`<>'$id' ");
		if($sdf > 0){
			$msg = '该时间段已申请过了';
		}
		return array('msg'=>$msg);
	}
	
	public function publicbeforesave121221($table, $arr, $id)
	{
		$msg 	= '';
		$start 	= $arr['stime'];
		$end 	= $arr['etime'];
		$total 	= (int)$arr['totals'];
		$cdate	= c('date', true);
		$db		= m($table);
		for($i=0; $i<$total; $i++){
			if($i>0)$start = $cdate->adddate($start, 'H', 1);
			$sdf = $db->rows("`uid`='".$arr['uid']."' and `stime`<='$start' and `etime`>='$start' and `kind`='请假' and `id`<>'$id' ");
			if($sdf > 0){
				$msg = '该时间段已申请过了';
				break;
			}
		}
		return array('msg'=>$msg);
	}
	
	public function totalAjax()
	{
		$start	= $this->rock->post('stime');
		$end	= $this->rock->post('etime');
		
		$sj	= $this->getjgsj($start, $end);
		
		echo json_encode(array($sj, ''));
	}
	
	private function getjgsj($start, $end)
	{
		$sj	= 0;
		$datec	= c('date', true);
		$jg		= $datec->datediff('d', $start, $end);

		
		$ends	= explode(' ', $end);
		$endts	= $ends[1];
		for($i=0; $i<=$jg; $i++){
			$time	= $datec->adddate($start, 'd', $i);
			$tiems	= explode(' ', $time);
			$dt		= $tiems[0];
			
			//不是工作日
			$ixxiuxi	= $this->db->rows($this->T('kq_xiu'), "`date`='$dt'");
			
			if($ixxiuxi>0)continue;

			if($i==0){
				$st1	= $time;
			}else{
				$st1	= ''.$dt.' 09:00:00';
			}
			if($i<$jg){
				$st2	= ''.$dt.' 18:00:00';
			}else{
				$st2	= ''.$dt.' '.$endts.'';
			}
			
			$xb1	= strtotime(''.$dt.' 12:00:00');	//早上下班
			$sb1	= strtotime(''.$dt.' 13:00:00');	//下午上班

			$xb2	= strtotime(''.$dt.' 09:00:00');	//早上上班	
			$sb2	= strtotime(''.$dt.' 18:00:00');	//下午下班
				
			$time1 	= strtotime($st1);
			$time2 	= strtotime($st2);
			
			if($time1< $xb2)$time1=$xb2;
			if($time2> $sb2)$time2=$sb2;
			
			$te_sj	= ceil(($time2 - $time1)/3600);
			
			$sj		= $te_sj + $sj;
	
			if($time1<=$xb1 && $time2>= $sb1)$sj--;
		}
		return $sj;	
	}
	
	public function publicstoreAfter($table, $rows)
	{
		$opentype	= $this->post('opentype');
		$arr		= array();
		//可用年假	= 总假期 - 已申请的年假
		if($opentype == '0'){
			$db = m($table);
			$month	= date('Y-m');
			$to1	= (int)$db->getmou("sum(totals)", "`kind`='请假' and `uid`='$this->adminid' and `stime` like '".$month."%' and `status` in(0,1)");
			$to2	= (int)$db->getmou("sum(totals)", "`kind` like '增加%' and `uid`='$this->adminid' and `status`=1");
			$to3	= 0;
			$arr['totalstring'] = '本月请假<font color=red>'.$to1.'</font>小时，可用假期<font color=green>'.$to3.'</font>小时';
		}
		return $arr;
	}
}