<?php
/*
	[Office 515158] (C) 2009-2014 天生创想 Inc.
	$Id: mod_app mobile $
*/
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
empty($do) && $do = 'list';
require('../include/function_apps.php');
$type=getGP('type','G');
$type_name='公文';	
if ($do == 'list') {
	if($type==1){
		$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."apps a,".DB_TABLEPRE."app_personnel b WHERE  a.id=b.appid and (b.pertype=0 or b.pertype=4) and b.name like '%".get_realname($_USER->id)."%' and a.type!=1 order by b.perid asc");
		$sql = "SELECT a.* FROM ".DB_TABLEPRE."apps a,".DB_TABLEPRE."app_personnel b WHERE  a.id=b.appid and (b.pertype=0 or b.pertype=4) and b.name like '%".get_realname($_USER->id)."%' and a.type!=1 order by b.perid asc LIMIT 100";
	}elseif($type==2){
		$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."apps a,".DB_TABLEPRE."app_personnel b WHERE  a.id=b.appid and b.pertype!=0 and b.name like '%".get_realname($_USER->id)."%' order by b.perid desc");
		$sql = "SELECT a.* FROM ".DB_TABLEPRE."apps a,".DB_TABLEPRE."app_personnel b WHERE  a.id=b.appid and b.pertype<>'0' and b.name like '%".get_realname($_USER->id)."%' order by b.perid desc LIMIT 100";
	}elseif($type==3){
		//$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."app a,".DB_TABLEPRE."app_template b WHERE 1 ".$wheresqltype." and a.tplid=b.tplid and b.tpladmin like '%".get_realname($_USER->id)."%' order by a.id desc");
		//$sql = "SELECT a.* FROM ".DB_TABLEPRE."app a,".DB_TABLEPRE."app_template b WHERE 1 ".$wheresqltype." and a.tplid=b.tplid and b.tpladmin like '%".get_realname($_USER->id)."%' order by a.id desc LIMIT $offset, $pagesize";
		
	}else{
		$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."apps WHERE uid='".$_USER->id."' order by id desc");
		$sql = "SELECT * FROM ".DB_TABLEPRE."apps WHERE uid='".$_USER->id."'  order by id desc LIMIT 100";
	}
	$result = $db->fetch_all($sql);
	include_once('template/apps.php');

}elseif ($do == 'views') {
	$id = getGP('id','G','int');
	$type = getGP('type','G','int');
	if($id!=''){
		$row = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."apps  WHERE id = '".$id."'");
	}
	include_once('template/apps_view.php');
}elseif ($do == 'personnel') {
	if(getGP('savetype','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');//状态
		$type=getGP('type','P');
		if(getGP('stafftype','P')!='1'){
			$staffid=getGP('staff','P');
			$staff=get_realname(getGP('staff','P'));
		}else{
			$staff=mob_select_post(getGP('staff','P','array'));
			$staffid=get_realid($staff);
		}

		if($pkey==3 && $_POST['views']!='3'){
			//获取流程信息
			$sql = "SELECT * FROM ".DB_TABLEPRE."apps  WHERE id = '".$appid."'";
			$row = $db->fetch_one_array($sql);
			//获取下一步流程
			$sql = "SELECT * FROM ".DB_TABLEPRE."app_flow  WHERE fid=".getGP('updatefid','P')."";
			$flow = $db->fetch_one_array($sql);
			include_once('template/apps_personnel_up.php');
			exit;
		}
		
		//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;
				}
			}
			//处理个别流程丢失的问题
			//if(($_POST['staffid']=='' || $_POST['staffid']=='0') && $pkeys!=5){
			//	show_msg('审批人员数据不正确,请检查填写的数据是否完整或刷新浏览器重新提交！', 'admin.php?ac=list&do=personnel&fileurl=app&appid='.$appid);
			//}
			$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('flowfrom','P')=='1'){//fromflowid
			$sql = "SELECT typeid,tplid FROM ".DB_TABLEPRE."app  WHERE id = '".$appid."'";
			$work = $db->fetch_one_array($sql);
			global $db;
			$query = $db->query("SELECT * FROM ".DB_TABLEPRE."app_from where tplid='".$work['tplid']."' and typeid='".$work['typeid']."' and flowid='".getGP('fromflowid','P')."' and formtype=2 and inputtype!='6' ORDER BY fromid Asc");
			while ($row = $db->fetch_array($query)) {
				if($row['inputtype1']=='4'){
					$inputvalues='';
					$inputvalue=getGP(''.$row["inputname"].'','P','array');
					foreach ($inputvalue as $arrsave) {
						$inputvalues.=$arrsave.',';
					}
					$inputvalue=substr($inputvalues, 0, -1);
				}else{
					$inputvalue=trim(getGP(''.$row["inputname"].'','P'));
				}
				$app_db = array(
						'inputname' => $row["inputname"],
						'content' => $inputvalue,
						'appid' => $appid,
						'tplid' => $work['tplid'],
						'fromid' => $row["fromid"],
						'typeid' => $work['typeid'],
						'type' => 2
					);
				insert_db('app_db',$app_db);
			
			}
			//写入数组
			global $db;
			$query = $db->query("SELECT * FROM ".DB_TABLEPRE."app_from where tplid='".$work['tplid']."' and typeid='".$work['typeid']."' and flowid='".getGP('fromflowid','P')."' and formtype=2 and inputtype='6'   ORDER BY fromid Asc");
			while ($row = $db->fetch_array($query)) {
					$inputvalue=getGP(''.$row["inputname"].'','P','array');
					$app_db = array(
					'inputname' => $row["inputname"],
					'content' => serialize($inputvalue),
					'appid' => $appid,
					'tplid' => $work['tplid'],
					'fromid' => $row["fromid"],
					'typeid' => $work['typeid'],
					'type' => 2
				);
				insert_db('app_db',$app_db);
			}
		}
		*/
		//通知审批人员
		if(getGP('sms_info_box_shownamemaster','P')!=''){
			$content1='您有一个公文流程需要审批,请点击进入公文进行审批!';
			SMS_ADD_POST($staff,$content1,0,0,$_USER->id);
		}
		
		//通知其它人员
		if(getGP('viewuser','P')!=''){
			$content3='您接收到一条来自公文的通知,请点击“<a href="admin.php?ac=list&do=view&fileurl=apps&appid='.$appid.'">查看</a>”公文!';
			SMS_ADD_POST(mob_select_post(getGP('viewuser','P')),$content3,0,0,$_USER->id);
		}
		$content4='审批流程“'.getGP('title','P').'“于“'.get_date('Y-m-d H:i:s',PHP_TIME).'“被'.get_realname($_USER->id).'审批完成,请点击“<a href="admin.php?ac=list&do=view&fileurl=apps&appid='.$appid.'">查看</a>”公文!';
		//通知前几步审批人员
		$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);
		
		$formlist='true';
		//show_msg('公文己成功审批！', 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'&type=1');
	}else{
		//获取公文信息
		$appid=getGP('appid','G','int');
		$type=getGP('type','G','int');
		$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) 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='".$row['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/apps_personnel.php');
}
?>