<?php
/*
	[Office 515158] (C) 2009-2015 天生创想 Inc.
	$Id: mod_appsflow mobile $
*/
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
if(getGP('tpltype','G')!=''){
	$tpltype=getGP('tpltype','G');
}else{
	$tpltype=getGP('tpltype','P');
}
if(getGP('tplid','G')!=''){
	$tplid=getGP('tplid','G');
}else{
	$tplid=getGP('tplid','P');
}
$appid=getGP('appid','P','int');
empty($do) && $do = 'list';
if ($do == 'list') {
	$tpl = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."app_type  WHERE tplid = '".$tplid."'  ");
	//更新表单数据
	mod_apps_update();
	mod_appsform_update();
	//更新签批内容
	$perid=getGP('perid','P','int');
	$flowid=getGP('flowid','P','int');
	$content=getGP('content','P');
	$sql = "SELECT * FROM ".DB_TABLEPRE."apps  WHERE id = '".$appid."'";
	$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."app_personnel a,".DB_TABLEPRE."app_flow b  WHERE a.flowid=b.fid and a.appid = '".$appid."' 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."app_flow  WHERE flownum >'".$per['flownum']."' and tplid='".$tplid."' order by flownum asc";
		$flow = $db->fetch_one_array($sql);
	}
	$perlnums = $db->result("SELECT COUNT(*) AS perlnums FROM ".DB_TABLEPRE."app_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/appspersonnel.php');

}elseif ($do == 'personnel') {
	if(getGP('view','P')!=''){
		$perid=getGP('perid','P');//当前审批ID
		$content=getGP('content','P');//批示
		$pkey=getGP('pkey','P');//状态
		$appid=getGP('appid','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."app_flow  WHERE fid = '".getGP('oldappflow','P')."'";
			$flow = $db->fetch_one_array($sql);
			$perlnum = $db->result("SELECT COUNT(*) AS perlnum FROM ".DB_TABLEPRE."app_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;
				}
			}

			$personnel1 = array(
				'pertype' =>$pkeys,
				'approvaldate' =>get_date('Y-m-d H:i:s',PHP_TIME),
				'lnstructions' =>$content
				);
			update_db('app_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('app_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('app_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,
					'appid' =>$appid,
					'flowid' => getGP('flowid','P'),
					'appkey' => getGP('appkey','P'),
					'appkey1' => getGP('appkey1','P'),
					'tplid' => getGP('tplid','P'),
					'flowdatetype' => getGP('flowdatetype','P'),
					'flowdate' => $flowdate
					);
				insert_db('app_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,
							'appid' =>$appid,
							'tplid' =>getGP('tplid','P')
							);
						insert_db('app_personnel_log',$personnel_log);
					}
				}
			}
		    if(getGP('reads','P')!=''){
				$uid=$_USER->id;
				$date=get_date('Y-m-d H:i:s',PHP_TIME);
				$read=mob_select_post(getGP('read','P','array'));
				$readid=get_realid($read);
				$readuid=explode(',',$readid);
				for($i=0;$i<sizeof($readuid);$i++){
					if($readuid[$i]!=''){
						$read = array();
						$read['appid']=$appid;
						$read['tplid']=$tplid;
						$read['tpltype']=$tpltype;
						$read['uid']=$uid;
						$read['date']=$date;
						$read['readuid']=$readuid[$i];
						insert_db('app_read',$read);
						//$pid=$db->insert_id();
					}
				}
				//通知阅读人员
				if(getGP('sms_info_box_shownamemaster','P')!=''){
					$content1='您有一个公文流程需要阅读,请点击进入公文阅读里进行查看!';
					SMS_ADD_POST($read,$content1,0,0,$_USER->id);
				}
			}
		//通知审批人员
		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."app_personnel WHERE appid = '".$appid."' 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=apps&fileurl=plugin&error=true&do=listkey');
	}
}elseif ($do == 'up') {
	//更新签批内容
	$perid=getGP('perid','P','int');
	$flowid=getGP('flowid','P','int');
	//$content=getGP('content','P');
	$sql = "SELECT * FROM ".DB_TABLEPRE."apps  WHERE id = '".$appid."'";
	$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."app_personnel a,".DB_TABLEPRE."app_flow b  WHERE a.flowid=b.fid and a.appid = '".$appid."' 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/appspersonnelup.php');
}elseif ($do == 'upajax') {
	$sqlpers = "SELECT * FROM ".DB_TABLEPRE."app_personnel where appid='".getGP('appid','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'].'" />';
	}
}elseif ($do == 'read') { 
	$tpl = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."app_type  WHERE tplid = '".$tplid."'  ");
	//更新表单数据
	mod_apps_update();
	mod_appsform_update();
	//更新签批内容
	$perid=getGP('perid','P','int');
	$flowid=getGP('flowid','P','int');
	$content=getGP('content','P');
	$sql = "SELECT * FROM ".DB_TABLEPRE."apps  WHERE id = '".$appid."'";
	$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."app_personnel a,".DB_TABLEPRE."app_flow b  WHERE a.flowid=b.fid and a.appid = '".$appid."' 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."app_flow  WHERE flownum >'".$per['flownum']."' and tplid='".$tplid."' order by flownum asc";
		$flow = $db->fetch_one_array($sql);
	}
	$perlnums = $db->result("SELECT COUNT(*) AS perlnums FROM ".DB_TABLEPRE."app_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/appspersonnelread.php');
}
?>