<?php
 /*
	[Office 515158] (C) 2009-2014 天生创想 Inc.
	$Id: workflow.php 1209087 2013-11-08 08:58:28Z baiwei.jiang $
*/
define('IN_ADMIN',True);
require_once('../include/common.php');
require_once('../include/function_workclass.php');
get_login($_USER->id);
//获取流程数据
global $db;
$query = $db->query("SELECT * FROM ".DB_TABLEPRE.DB_JT."workclass_personnel where flowdatetype=1  and pertype=0 order by perid asc");
//and flowdate<'".get_date('Y-m-d H:i:s',PHP_TIME)."'
	while ($row = $db->fetch_array($query)) {
		//获取当前流程数据
		//$flow1 = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE.DB_JT."workclass_flow WHERE fid='".$row['flowid']."'");
		$workid=$row['workid'];
		echo $row['perid'].'9898';
		$perid=$row['perid'];
		$flowid=$row['flowid'];
		$w = $db->fetch_one_array("SELECT tplid FROM ".DB_TABLEPRE.DB_JT."workclass WHERE id='".$workid."'");
		$tplid=$w['tplid'];
		$typeid=$row['typeid'];
		$sql = "SELECT a.perid,a.name,a.uid,a.pertype,a.appkey,a.appkey1,a.countersign,a.entrust,
	        b.flowname,b.flownum,b.fid,b.flowkey,b.flowkey4,b.flowkey5,b.flowkey6 
			FROM ".DB_TABLEPRE.DB_JT."workclass_personnel a,
			".DB_TABLEPRE.DB_JT."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";
			$flow1 = $db->fetch_one_array($sql);
			//检测条件流
			//$flowkey5=work_whereflow($flow1['flowkey5'],$workid);
			echo $workid;
			exit;
			//获取有效步骤
			$nextflow = array();
			if($flowkey5!=0){
				$nextflow[]=work_nextflow($tplid,$typeid,$workid,$flow1['flownum']);
			}
		//获取下一步流程数据
		//$flow2 = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE.DB_JT."workclass_flow  WHERE flownum >'".$flow1['flownum']."' and tplid='".$flow1['tplid']."' order by flownum asc");
		echo 'fdsa';
		exit;
		//制定审批状态
		$pertype=2;
		$pertypelog=2;
		if($flow2['flowuser']!=''){
			$pertype=1;
			$pertypelog=1;
		}
		if($flow1['flowkey']=='2'){
			$pertype=5;
			$pertypelog=1;
		}
		//处理当前流程
		$personnel1 = array(
				'pertype' =>$pertype,
				'approvaldate' =>get_date('Y-m-d H:i:s',PHP_TIME),
				'lnstructions' =>'此步骤己超时,由系统自动处理!'
				);
		update_db(DB_JT.'workclass_personnel',$personnel1, array('perid' => $row['perid']));
		if($row['appkey']==1){//多人审批
			$per_log = array(
					'pertype' =>$pertypelog,
					'approvaldate' =>get_date('Y-m-d H:i:s',PHP_TIME),
					'lnstructions' =>'此步骤己超时,由系统自动处理!'
					);
			update_db(DB_JT.'workclass_personnel_log',$per_log, array('perid' => $row['perid']));
		}
		$db->query("UPDATE ".DB_TABLEPRE.DB_JT."workclass_personnel set 
				           userend=CONCAT_WS('',userend,'".get_realname($row['uid']).",') 
						   WHERE perid=".$row['perid']." and workid = '".$row['workid']."' ");
		flowuserdel($row['perid'],$row['workid']);
		//创建下一步流程[当默认审批人员为空时不创建]
		if($flow2['flowuser']!=''){
			if($flow2['flowdatetype']==1){
				$flowdate=get_date('Y-m-d H:i:s',PHP_TIME+$flow2['flowdate']*60);
			}
			if($flow2['flowkey2']==1){//多人审批
				$personnel2 = array(
					'name' => $flow2['flowuser'],
					'uid' =>get_realid($flow2['flowuser']),
					'pertype' =>0,
					'workid' =>$row['workid'],
					'flowid' => $flow2['fid'],
					'appkey' => $flow2['flowkey2'],
					'appkey1' => $flow2['flowkey3'],
					'typeid' => $row['typeid'],
					'flowdatetype' => $flow2['flowdatetype'],
					'flowdate' => $flowdate,
					'flowkey8' => $flow2['flowkey8'],
					'usernum' => ','.$flow2['flowuser'].','
					);
				insert_db(DB_JT.'workclass_personnel',$personnel2);
				$pid=$db->insert_id();
					$staff=explode(',',$flow2['flowuser']);
					$staffid=explode(',',get_realid($flow2['flowuser']));
					for($i=0;$i<sizeof($staffid);$i++){
						$personnel_log = array(
							'name' => $staff[$i],
							'uid' =>$staffid[$i],
							'pertype' =>0,
							'perid' =>$pid,
							'workid' =>$row['workid'],
							'typeid' =>$row['typeid']
							);
						insert_db(DB_JT.'workclass_personnel_log',$personnel_log);
					}
			}else{
				$flowuser=explode(',',$flow2['flowuser']);
				$personnel2 = array(
					'name' => $flowuser[0],
					'uid' =>get_userid($flowuser[0]),
					'pertype' =>0,
					'workid' =>$row['workid'],
					'flowid' => $flow2['fid'],
					'appkey' => $flow2['flowkey2'],
					'appkey1' => $flow2['flowkey3'],
					'typeid' => $row['typeid'],
					'flowdatetype' => $flow2['flowdatetype'],
					'flowdate' => $flowdate,
					'flowkey8' => $flow2['flowkey8'],
					'usernum' => ','.$flow2['flowuser'].','
					);
				insert_db(DB_JT.'workclass_personnel',$personnel2);
			}
		}
	}
echo $pid;
//过滤审批人员
function flowuserdel($perid=0,$workid=0){
	global $db;
	global $_USER;
	$sql = "SELECT usernum FROM ".DB_TABLEPRE.DB_JT."workclass_personnel
	        WHERE perid=".$perid." and workid = '".$workid."'";
	$row = $db->fetch_one_array($sql);
	$usernum=str_replace(get_realname($_USER->id).',','',$row['usernum']);
	$db->query("UPDATE ".DB_TABLEPRE.DB_JT."workclass_personnel set usernum='".$usernum."'
	            WHERE perid=".$perid." and workid = '".$workid."' ");
}
?>