<?php
class kaoqinClassAction extends Action
{
	public function getsetAjax()
	{
		$rows	= m('kq_set')->getone('1=1 order by id desc');
		echo json_encode($rows);
	}
	
	public function getxiuAjax()
	{
		$month	= $this->rock->get('y1').'-'.$this->rock->get('m1');
		$rows	= m('kq_xiu')->getall("`date` like '{$month}%' order by id desc");
		echo json_encode($rows);
	}
	public function setallxiuAjax()
	{
		$s	= $this->rock->post('s');
		$a	= explode(',', $s);
		$db = m('kq_xiu');
		foreach($a as $day){
			if($db->rows("`date`='$day'")==0){
				$db->record("`date`='$day'");
			}
		}
		$this->getxiuAjax();
	}
	public function changexiuAjax()
	{
		$day = $this->rock->get('day');
		$lx  = $this->rock->get('lx');
		$db = m('kq_xiu');
		if($lx == '0')$db->record("`date`='$day'");
		if($lx == '1')$db->delete("`date`='$day'");
	}
	
	//导入
	public function importAjax()
	{
		$import	= c('PHPExcelReader', true);
		$cdt	= c('date', true);
		
		$rows	= $import->reader($_FILES['file']['tmp_name']);
		$db		= m('kq_dkjl');
		$oi		= 0;
		foreach($rows as $k=>$rs){
			$arr	= array(
				'name'		=> $rs['A'],
				'finge'		=> $rs['B'],
				'checktime'	=> substr($rs['C'],1,-1)
			);
			if($cdt->isdate($arr['checktime'])){
				$db->insert($arr);
				$oi++;
			}
		}
		$this->backmsg('', '成功导入'.$oi.'条');
	}
	
	public function kaoqinfxafter($table, $rows)
	{
		$s = "'0'";
		$s1 = '0';
		foreach($rows as $k=>$rs){
			$s.=",'".$rs['dt']."'";
			$s1.=",".$rs['uid']."";
		}
		$rowss	= m('kq_xiu')->getall("`date` in($s)");
		foreach($rowss as $k=>$rs)$ssraa[$rs['date']] = true;
		
		foreach($rows as $k=>$rs){
			$isxx= 0;
			if(isset($ssraa[$rs['dt']]))$isxx = 1;
			$rows[$k]['week'] = $this->rock->cnweek($rs['dt']);
			$rows[$k]['isxx'] = $isxx;
		}
		return array('rows' => $rows);
	}
	
	//考勤分析
	public function kaoqinfx11212($table, $rows)
	{
		$s = '';
		$s1 = '0';
		foreach($rows as $k=>$rs){
			$s.=",'".$rs['dt']."'";
			$s1.=",'".$rs['uid']."'";
		}
		if($s != ''){
			$s = substr($s, 1);
			$ssras	= array();
			$ssraa	= array();
			$ssra	= $this->db->getall("SELECT ztname,time,state,dt FROM `[Q]kq_anay` where `dt` in($s) and `uid` in($s1) order by dt,sort");
			foreach($ssra as $k=>$rs)$ssras[$rs['dt']][] = $rs;
			
			$rowss	= m('kq_xiu')->getall("`date` in($s)");
			foreach($rowss as $k=>$rs)$ssraa[$rs['date']] = true;
			
			foreach($rows as $k=>$rs){
				$str = '';
				$dt	 = $rs['dt'];
				$amsb= '';
				$pmxb= '';
				$isxx= 0;
				if(isset($ssraa[$dt]))$isxx = 1;
				if(isset($ssras[$dt])){
					foreach($ssras[$dt] as $k1=>$rs){
						$ztname = $rs['ztname'];
						$str.=''.$ztname.'：';
						$s11 = '';
						
						$s11.=''.$rs['state'].'';
						if(!$this->rock->isempt($rs['time']))$s11.='('.$rs['time'].')';
						if($ztname == '上班'){
							$amsb = $s11;
						}
						if($ztname == '下班'){
							$pmxb = $s11;
						}
						if($rs['state']!='正常')$s11='<font color=red>'.$s11.'</font>';
						$str.=''.$s11.'<br>';

					}
				}
				//$rows[$k]['amsb'] = $amsb;
				//$rows[$k]['pmxb'] = $pmxb;
				//$rows[$k]['week'] = $this->rock->cnweek($dt);
				$rows[$k]['isxx'] = $isxx;
			}
		}
		return array('rows' => $rows);
	}
	
	//考勤统计
	public function totalafter($table, $rows)
	{
		$uid = '0';
		foreach($rows as $k=>$rs){
			$uid.=','.$rs['id'];
		}
		$month	= $this->rock->post('month', date('Y-m'));
		$pdxx	= (int)$this->rock->post('pdxx','0');
		
		$db 	= m('kq_info');
		$qirow	= $db->getarr("`uid` in($uid) and `status`=1 and `kind`='请假' and `stime` like '$month%' group by `uid`", "sum(`totals`)as stotal", 'uid');
		$jbrow	= $db->getarr("`uid` in($uid) and `status`=1 and `kind`='加班' and `stime` like '$month%' group by `uid`", "sum(`totals`)as stotal", 'uid');
		
		//休息日
		$xius	= "'0'";
		if($pdxx == 1){
			$rowss	= m('kq_xiu')->getall("`date` like '$month%'");
			foreach($rowss as $k=>$rs)$xius.=",'".$rs['date']."'";
		}
		
		$ssras	= array();
		$ssra	= $this->db->getall("SELECT state,uid,count(1) as totols FROM `[Q]kq_anay` where `dt` like '$month%' and `dt` not in($xius) and `uid` in($uid) group by state,uid");
		foreach($ssra as $k=>$rs){
			$ssras[$rs['uid']][] = $rs;
		}

		foreach($rows as $k=>$rs){
			$qj = '';
			$jb = '';
			$s1 = '';
			$uid= $rs['id'];
			if( isset($qirow[$uid]) )$qj = $qirow[$uid]['stotal'];
			if( isset($jbrow[$uid]) )$jb = $jbrow[$uid]['stotal'];
			
			$rows[$k]['leavesj'] = $qj;
			$rows[$k]['jiabansj'] = $jb;
			
			if(isset($ssras[$uid])){
				foreach($ssras[$uid] as $k1=>$rs1){
					$s1.=''.$rs1['state'].':'.$rs1['totols'].'次; ';
				}
			}
			$rows[$k]['kaoqinzt'] = $s1;
		}
		return array('rows' => $rows);
	}
	
	
	//获取上下班时间
	public function getsxbAjax()
	{
		$rows	= array();
		$db		= m('kq_set');
		$arrs 	= $db->getall("`type`=0 and `mid`=0 order by `sort`", "`id`,`name`");
		foreach($arrs as $k=>$rs){
			$rows[] = array('name'=>$rs['name'],'time'=>'');
			
			//对应值
			$arrss 	= $db->getall("`type`=".$rs['id']." order by `sort`", "`id`,`name`,`stime`");
			foreach($arrss as $k1=>$rs1){
				$rows[] = array('name'=>'&nbsp; &nbsp; '.$rs1['name'],'time'=>$rs1['stime']);
			}	
		}
		echo json_encode(array(
			'rows' => $rows
		));
	}
}