<?php
/*
	[Office 515158] (C) 2009-2014 天生创想 Inc.
	$Id: mod_workclass mobile $
*/
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
$typeid=getGP('typeid','G','int');
$tplid=getGP('tplid','G','int');
$workid=getGP('workid','P','int');
empty($do) && $do = 'list';
if ($do == 'list') {
	$tpl = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."workclass_template  WHERE tplid = '".$tplid."'  ");
	//更新表单数据
	mod_workflow_update();
	if($tpl['tplkey']!=4 && $tpl['tplkey']!=5){
		mod_workform_update();
		mod_workform_view_update();
	}
	//更新签批内容
	$perid=getGP('perid','P','int');
	$flowid=getGP('flowid','P','int');
	$content=getGP('content','P');
	$sql = "SELECT * FROM ".DB_TABLEPRE."workclass  WHERE id = '".$workid."'";
	$row = $db->fetch_one_array($sql);
	//获取当前流程
	$sql = "SELECT a.*,b.flowname,b.flownum,b.flowuser,b.flowkey4,b.flowkey,b.flowkey1,b.flowkey2,b.flowkey3 FROM ".DB_TABLEPRE."workclass_personnel a,".DB_TABLEPRE."workclass_flow b  WHERE a.flowid=b.fid and a.workid = '".$workid."' and (a.pertype=0 or a.pertype=4) and a.perid=".$perid." and a.flowid=".$flowid." order by a.perid desc";
	$per = $db->fetch_one_array($sql);
	if($per['flowkey']!='2'){
		//获取下一步流程
		$sql = "SELECT * FROM ".DB_TABLEPRE."workclass_flow  WHERE flownum >'".$per['flownum']."' and tplid='".$tplid."' and typeid='".$typeid."' order by flownum asc";
		$flow = $db->fetch_one_array($sql);
	}
	$perlnums = $db->result("SELECT COUNT(*) AS perlnums FROM ".DB_TABLEPRE."workclass_personnel_log where perid='".$per['perid']."' and pertype=0");
	if($per['appkey']==1 && $per['appkey1']==1){
		$wherestr=$perlnums<2 && $per['flowkey']!=2;
	}else{
		$wherestr=$per['flowkey']!=2;
	}
	include_once('template/workclass_personnel.php');

}elseif ($do == 'personnel') {
	if(getGP('view','P')!=''){
		$perid=getGP('perid','P');//当前审批ID
		$content=getGP('content','P');//批示
		$pkey=getGP('pkey','P');//状态
		$workid=getGP('workid','P');//流程ID
		$oldappkey=getGP('oldappkey','P');//状态
		$oldappkey1=getGP('oldappkey1','P');//状态
		if(getGP('stafftype','P')!='1'){
			if(getGP('updatefid','P')!=''){
				$staffid=getGP('staffid','P');
				$staff=trim(getGP('staff','P'));
			}else{
				$staffid=getGP('staff','P');
				$staff=get_realname(getGP('staff','P'));
			}
		}else{
			$staff=mob_select_post(getGP('staff','P','array'));
			$staffid=get_realid($staff);
		}
		//if($type['appkey']==1){
			$sql = "SELECT * FROM ".DB_TABLEPRE."workclass_flow  WHERE fid = '".getGP('oldappflow','P')."'";
			$flow = $db->fetch_one_array($sql);
			$perlnum = $db->result("SELECT COUNT(*) AS perlnum FROM ".DB_TABLEPRE."workclass_personnel_log where perid='".$perid."' and pertype=0");
			if($oldappkey==1){
				if($oldappkey1==1 && $perlnum>1 && $pkey!=2){
					$pkeys=4;
				}elseif($flow['flowkey']==2){
					$pkeys=5;
				}else{
					$pkeys=$pkey;
				}
			}else{
				$pkeys=$pkey;
				if($flow['flowkey']==2 && $pkey!=2){
					$pkeys=5;
				}
			}
			//处理个别流程丢失的问题
			//if(($_POST['staffid']=='' || $_POST['staffid']=='0') && $pkeys!=5){
			//	show_msg('审批人员数据不正确,请检查填写的数据是否完整或刷新浏览器重新提交！', 'admin.php?ac=list&do=personnel&fileurl=workclass&workid='.$workid);
			//}
			$newsql = "SELECT perid FROM ".DB_TABLEPRE."workclass_personnel  WHERE perid = '".$perid."' and (pertype='0' or pertype='4')";
			$newflow = $db->fetch_one_array($newsql);
			if(trim($newflow['perid'])!=''){
				$personnel1 = array(
					'pertype' =>$pkeys,
					'approvaldate' =>get_date('Y-m-d H:i:s',PHP_TIME),
					'lnstructions' =>$content
					);
				update_db('workclass_personnel',$personnel1, array('perid' => $perid));
				if($oldappkey==1){
					$per_log = array(
						'pertype' =>$pkey,
						'approvaldate' =>get_date('Y-m-d H:i:s',PHP_TIME),
						'lnstructions' =>$content
						);
					update_db('workclass_personnel_log',$per_log, array('perid' => $perid,'uid' => $_USER->id));
					if($pkey==2){
						$personnel1 = array(
						'pertype' =>$pkey,
						'approvaldate' =>get_date('Y-m-d H:i:s',PHP_TIME),
						'lnstructions' =>$content
						);
						update_db('workclass_personnel',$personnel1, array('perid' => $perid));
					}
				}
				//创建下一步审批人员
				if(getGP('flowdatetype','P')==1){
					$flowdate=get_date('Y-m-d H:i:s',PHP_TIME+getGP('flowdate','P')*60);
				}
				if($staffid!='' && $pkeys!=2 && $pkeys!=5){
					$personnel2 = array(
						'name' => $staff,
						'uid' =>$staffid,
						'pertype' =>0,
						'workid' =>$workid,
						'flowid' => getGP('flowid','P'),
						'appkey' => getGP('appkey','P'),
						'appkey1' => getGP('appkey1','P'),
						'typeid' => getGP('typeid','P'),
						'flowdatetype' => getGP('flowdatetype','P'),
						'flowdate' => $flowdate
						);
					insert_db('workclass_personnel',$personnel2);
					$pid=$db->insert_id();
					if(getGP('appkey','P')=='1'){
						$staffs=explode(',',$staff);
						$staffids=explode(',',$staffid);
						for($i=0;$i<sizeof($staffids);$i++){
							$personnel_log = array(
								'name' => $staffs[$i],
								'uid' =>$staffids[$i],
								'pertype' =>0,
								'perid' =>$pid,
								'workid' =>$workid,
								'typeid' =>getGP('typeid','P')
								);
							insert_db('workclass_personnel_log',$personnel_log);
						}
					}
				}
			
			//通知审批人员
			if(getGP('sms_info_box_shownamemaster','P')!=''){
				$content1='您有一个工作流程需要审批,请点击进入工作流进行审批!';
				SMS_ADD_POST($staff,$content1,0,0,$_USER->id);
			}
			$content4='审批流程“'.getGP('title','P').'“于“'.get_date('Y-m-d H:i:s',PHP_TIME).'“被'.get_realname($_USER->id).'审批完成!';
			//通知前几步审批人员
			$sqlu = "SELECT * FROM ".DB_TABLEPRE."workclass_personnel WHERE workid = '".$workid."' and perid <".$perid."";
			$results = $db->query($sqlu);
			while ($upfid = $db->fetch_array($results)) {	
				SMS_ADD_POST($upfid['name'],$content4,0,0,$_USER->id);
			}
		}
		$content=serialize($personnel1);
		$title='审批工作流';
		get_logadd($id,$content,$title,14,$_USER->id);
		header('Location: index.php?ac=workclass&fileurl=plugin&type=1&error=true');
		//show_msg(work_modid().'己成功审批！', 'admin.php?ac=list&fileurl='.$fileurl.'&type=1&modid='.$modid.'&tplid='.$tplid.'');
	}
}elseif ($do == 'up') {
//更新签批内容
	$perid=getGP('perid','P','int');
	$flowid=getGP('flowid','P','int');
	//$content=getGP('content','P');
	$sql = "SELECT * FROM ".DB_TABLEPRE."workclass  WHERE id = '".$workid."'";
	$row = $db->fetch_one_array($sql);
	//获取当前流程
	$sql = "SELECT a.*,b.flowname,b.flownum,b.flowuser,b.flowkey4,b.flowkey,b.flowkey1,b.flowkey2,b.flowkey3 FROM ".DB_TABLEPRE."workclass_personnel a,".DB_TABLEPRE."workclass_flow b  WHERE a.flowid=b.fid and a.workid = '".$workid."' and (a.pertype=0 or a.pertype=4) and a.perid=".$perid." and a.flowid=".$flowid." order by a.perid desc";
	$per = $db->fetch_one_array($sql);
	include_once('template/workclass_personnel_up.php');
}elseif ($do == 'upajax') {
	$sqlpers = "SELECT * FROM ".DB_TABLEPRE."workclass_personnel where workid='".getGP('workid','G')."' and flowid='".getGP('flowid','G')."'  order by perid asc";
	$pers = $db->fetch_one_array($sqlpers);
	if(getGP('flowid','G')!=''){
		echo '<input type="hidden" name="flowid" value="'.$pers['flowid'].'" />';
		echo '<input type="hidden" name="appkey" value="'.$pers['appkey'].'" />';
		echo '<input type="hidden" name="appkey1" value="'.$pers['appkey1'].'" />';
		echo '<input type="hidden" name="flowdatetype" value="'.$pers['flowdatetype'].'"/>';
		echo '<input type="hidden" name="flowdate" value="'.$pers['flowdate'].'" />';
		echo '<input type="hidden" name="staffid" value="'.$pers['uid'].'" />';
		echo '<input type="hidden" name="staff" value="'.$pers['name'].'" />';
	}
} 
?>