<?php
/*
	[Office 515158] (C) 2009-2012 天生创想 Inc.
	$Id: mod_registration 1209087 2012-01-08 08:58:28Z baiwei.jiang $
*/
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
empty($do) && $do = 'list';
$_check['ischeck']='  ui-tab-trigger-item-current';
if ($do == 'list') {
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE.DB_JT."attendance_config  WHERE user like',".$_USER->id.",'  and type='2'");
	if($num>0){
		$sql="SELECT sdate,edate FROM ".DB_TABLEPRE.DB_JT."attendance_config  WHERE user like',".$_USER->id.",'  and type='2' order by id desc";
	}else{
		$sql="SELECT sdate,edate FROM ".DB_TABLEPRE.DB_JT."attendance_config  WHERE  type='1' order by id desc";
	}
	$config = $db->fetch_one_array($sql);
	$csdate=explode(':',$config['sdate']);
	$cedate=explode(':',$config['edate']);
	$date=get_date('H',PHP_TIME)*60+get_date('i',PHP_TIME);
	//是否跨时区
	if($csdate[0]<$cedate[0]){
		$type=1;
	}
	if($csdate[0]=='00'){
		$csdate[0]='24';
	}
	if($cedate[0]=='00'){
		$cedate[0]='24';
	}
	$_sdate=$csdate[0]*60+$csdate[1];
	$_edate=$cedate[0]*60+$cedate[1];
	if($type==1){
		$sql="SELECT * FROM ".DB_TABLEPRE.DB_JT."attendance  WHERE  uid='".$_USER->id."' and date='".get_date('Y-m-d',PHP_TIME)."' order by id desc";
		$row = $db->fetch_one_array($sql);
	}else{
		if(get_date('H',PHP_TIME)>$csdate[0]){
			$sql="SELECT * FROM ".DB_TABLEPRE.DB_JT."attendance  WHERE  uid='".$_USER->id."' and date='".get_date('Y-m-d',PHP_TIME)."' order by id desc";
			$row = $db->fetch_one_array($sql);
		}else{
			$testdate=get_date('Y-m',PHP_TIME).'-'.(get_date('d',PHP_TIME)-1);
			$sql="SELECT * FROM ".DB_TABLEPRE.DB_JT."attendance  WHERE  uid='".$_USER->id."' and (date='".$testdate."' or date='".get_date('Y-m-d',PHP_TIME)."')  order by id desc";
			$nums = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE.DB_JT."attendance  WHERE  uid='".$_USER->id."' and (date='".$testdate."' or date='".get_date('Y-m-d',PHP_TIME)."')  order by id desc");
			$row = $db->fetch_one_array($sql);
			if($date>($_edate+120) && $row['edate']!='' && $nums<2){
				$row['id']='';
				$row['edate']='';
			}
			
		}
		
	}
	//列表数据
	$page = max(1, getGP('page','G','int'));
	$pagesize = $_CONFIG->config_data('pagenum');
	$offset = ($page - 1) * $pagesize;
	$url = 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'';
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE.DB_JT."attendance  WHERE  uid=".$_USER->id."  AND DATE_SUB(CURDATE(),INTERVAL 1 MONTH)<=date(date) order by id desc");
    $sql = "SELECT * FROM ".DB_TABLEPRE.DB_JT."attendance  WHERE  uid=".$_USER->id."  AND DATE_SUB(CURDATE(),INTERVAL 1 MONTH)<=date(date) order by id desc LIMIT $offset, $pagesize";
	$results = $db->fetch_all($sql);
	include_once('template/registrationlist.php');
}elseif ($do == 'add') {
	//获取考勤数据
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE.DB_JT."attendance_config  WHERE user like',".$_USER->id.",'  and type='2'");
	if($num>0){
		$sql="SELECT sdate,edate FROM ".DB_TABLEPRE.DB_JT."attendance_config  WHERE user like',".$_USER->id.",'  and type='2' order by id desc";
	}else{
		$sql="SELECT sdate,edate FROM ".DB_TABLEPRE.DB_JT."attendance_config  WHERE  type='1' order by id desc";
	}
	$config = $db->fetch_one_array($sql);
	$csdate=explode(':',$config['sdate']);
	$cedate=explode(':',$config['edate']);
	$date=get_date('H',PHP_TIME)*60+get_date('i',PHP_TIME);
	if($csdate[0]=='00'){
		$csdate[0]='24';
	}
	if($cedate[0]=='00'){
		$cedate[0]='24';
	}
	$_sdate=$csdate[0]*60+$csdate[1];
	$_edate=$cedate[0]*60+$cedate[1];
	//退早
	if($csdate[0]<$cedate[0]){
		$latedate=$date-$_sdate;
		$ledate=$_edate-$date;
	}else{
		if($date>$_edate){
			$latedate=$date-$_sdate;
		}else{
			$latedate=($date+1440)-$_sdate;
		}
		////////////
		if($date>$_sdate){
			$ledate=($_edate+1440)-$date;
		}else{
			$ledate=$_edate-$date;
		}
	}
	
	//获取打卡数据
	$attendanceid=getGP('attendanceid','P','int');
	if(!$attendanceid){
		$atte_add = array();
		$atte_add['name']=get_realname($_USER->id);
		$atte_add['uid']=$_USER->id;
		$atte_add['sdate']=get_date('Y-m-d H:i:s',PHP_TIME);
		if($latedate>0){
			$atte_add['latedate']=$latedate;
		}else{
			$atte_add['latedate']=0;
		}
		$atte_add['xy']=getGP('gpsxy','P');
		$atte_add['addr']=getGP('gpsaddress','P');
		$atte_add['note']=getGP('note','P');
		$atte_add['date']=get_date('Y-m-d',PHP_TIME);
		//写入数据 
		insert_db(DB_JT.'attendance',$atte_add);
	}else{
		$sql="SELECT * FROM ".DB_TABLEPRE.DB_JT."attendance  WHERE  id=".$attendanceid."";
		$row = $db->fetch_one_array($sql);
		$atte_edit = array();
		$atte_edit['edate']=get_date('Y-m-d H:i:s',PHP_TIME);
		if($ledate>0){
			$atte_edit['ledate']=$ledate;
		}else{
			$atte_edit['ledate']=0;
		}
		$atte_edit['xy']=$row['xy'].'/'.getGP('gpsxy','P');
		$atte_edit['addr']=$row['addr'].'/'.getGP('gpsaddress','P');
		$atte_edit['note']=getGP('note','P');
		update_db(DB_JT.'attendance',$atte_edit, array('id' =>$attendanceid,'uid'=>$_USER->id));
	}
	show_msg('考勤数据提交成功！', 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'');
	exit;
}elseif($do=='charts'){
		include_once('template/registrationcharts.php');
}
function attendance_date($date){
	if($date>=60){
		$_date=sprintf("%.2f",($date/60));
		$_date=explode('.',$_date);
		return $_date[0].'小时'.$_date[1].'分';
	}else{
		return $date.'分';
	}
}
?>