<?php 
class kaoqinClassAction extends Action{
	
	//考勤分析
	public function fenxiAjax()
	{
		$sdt	= $this->rock->post('startdt');
		$edt	= $this->rock->post('enddt');
		$uid	= $this->rock->post('uid');
		
		
		
		$date	= $this->date;
		$dt		= c('date', true);
		if($sdt == '')$sdt = $dt->adddate($date,'d', -3);
		if($edt == '')$edt = $dt->adddate($date,'d', -1);
		
		if(strtotime($edt)>time())$edt = $date;
		
		$jg		= $dt->datediff('d', $sdt, $edt);
		$uwhere	= 'workdate is not null and finge is not null';
		if($uid != '')$uwhere.=" and id in($uid)";
		$urows	= m('admin')->getall($uwhere, 'id,workdate,quitdt,finge');
		
		$dkjl	= m('kq_dkjl')->getall("`checktime` >='$sdt 00:00:00' and `checktime`<='$edt 23:59:59'  order by `checktime`");
		$dkrs	= array();
		foreach($dkjl as $k=>$rs){
			$sjl	= $rs['checktime'];
			$dtss = explode(' ', $sjl);
			$dkrs[$rs['finge']][] = array($dtss[0], $dtss[1], $sjl, strtotime($sjl));
		}
		
		$set	= $this->readkaoset();
		$db1 	= m('kq_anay');
		
		for($i=0; $i<=$jg; $i++){
			if($i>0)$sdt = $dt->adddate($sdt,'d', 1);
			
			foreach($urows as $k=>$urs){
				//入职离职判断
				
				$dtdk	= $this->getdk($dkrs, $urs['finge'], $sdt); //当天打卡记录
				$len	= count($dtdk);
				$uid	= $urs['id'];
				$zid   	= '0';
				
				//添加到分析表
				foreach($set as $k1=>$cog){
					$ztname = $cog['name'];
					$sarr = array(
						'ztname'  => $ztname,
						'sort'  => $k1,
						'time'	=> '',
						'state'	=> '',
						'dt'	=> $sdt,
						'uid'	=> $uid,
						'optname'=> $this->adminname,
						'optdt'	=> $this->now
					);
					$time = '';
					$state = '未打卡';
					
					//判断对应状态并且有打卡记录
					if($len>0){
						foreach($cog['rows'] as $k2=>$cog2){
							$jlsj	= $dtdk[0];
							if($cog2['qtype']==1)$jlsj	= $dtdk[$len-1];
							
							$st1	= strtotime($sdt.' '.$cog2['stime']);
							$et1	= strtotime($sdt.' '.$cog2['etime']);
							
							if($jlsj[3] >= $st1 && $jlsj[3]<=$et1){
								$time = $jlsj[1];
								$state = $cog2['name'];
								break;
							}
						}
					}
					$sarr['time'] 	= $time;
					$sarr['state'] 	= $state;
					
					$where 	= "`dt`='$sdt' and `uid`='$uid' and `ztname`='$ztname'";
					$sid	= (int)$db1->getmou('id', $where);
					if($sid == 0)$where='';
					$db1->record($sarr, $where);
					if($sid == 0)$sid = $this->db->insert_id();
					$zid.=','.$sid.'';
				}
				$db1->delete("`id` not in($zid) and `dt`='$sdt' and `uid`='$uid'");
			}
		}
		echo 'success';
	}
	
	private function getdk($arr, $finge, $dt)
	{
		$a	= array();
		if(isset($arr[$finge])){
			foreach($arr[$finge] as $dtss){
				if($dtss[0] == $dt)$a[] = $dtss;
			}
		}
		return $a;
	}
	//读取考勤设置
	private function readkaoset()
	{
		$db 	= m('kq_set');
		$rows	= $db->getall("`type`=0 and `mid`=0 order by `sort`", '`id`,`receid`');
		$mid 	= 0;
		$a		= array();
		foreach($rows as $k=>$rs){
			$receid = $rs['receid'];
			if($this->rock->isempt($receid))$mid = $rs['id'];
		}
		$rows	= $db->getall("`type`='$mid' order by `sort`", '`id`,`name`,`stime`');
		foreach($rows as $k=>$rs){
			$rs['state'] = '';
			$rs['time'] = '';
			$rs['rows'] = $db->getall("`mid`='".$rs['id']."' order by `sort`", '`id`,`name`,`stime`,`etime`,`qtype`');
			$a[] = $rs;
		}
		return $a;
	}
	
	
	
	
	/**
		日报分析
	*/
	public function dailyfxAjax()
	{
		$nmonth	= date('Y-m');
		$uid	= (int)$this->request('uid', '0');
		$month	= $this->request('month', $nmonth);
		m('daily')->dailyfenxi($uid, $month, true);
		echo 'success';
	}
	

}