<?php
$thisDir=realpath(dirname(__FILE__)."/..");
require_once("{$thisDir}/webini.inc.php");
extract($_POST);
if($action==='searchForAddPur'){
	$result="";
	if(isNull($v)){
		$result="参数错误";
	}else{
		$sql="select * from stock where comId='{$_SESSION['comId']}' and barcode='{$v}'";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="没有对应的库存信息";
		}else{
			$row=$row[0];
			rc_require("debris_1.html","purchase");
		}
	}
	echo $result;
}else if($action=='checkUserForAddPur' || $action=='checkPurUserForAuditPur'){
	if(!isset($v) || isNull($v)){
		$result="请输入采购申请的账号";
	}else if(!preg_match($zz_userNick,$v)){
		$result="账号格式错误";
	}else{
		$sql="select * from user where comId={$_SESSION['comId']} and userNick='{$v}' limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="输入的账号不存在";
		}else{
			$result="账号正确";
		}
	}
	echo $result;
}else if($action==='savePurUserForAuditPur'){
	if(isNull($purUser) || isNull($noteNo) || isNull(preg_replace($zz_douhao,'',$idList))){
		$result="参数错误";
	}else{
		$idList=preg_replace("/(^\,)|(\,$)/",'',preg_replace($zz_douhao,',',$idList));
		$sql="select * from user where comId={$_SESSION['comId']} and userNick='{$purUser}' limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="输入的账号不存在";
		}else{
			$where=" where comId={$_SESSION['comId']} and status=1 and id in({$idList})";
			$sql="update purchase set status=2 {$where};";
			$sql.="insert into purnote(comId,purId,noteNo,status,purUser,addUser,ip,memo) values({$_SESSION['comId']},',{$idList},','{$noteNo}',0,'{$purUser}','{$_SESSION['userNick']}','{$cg['ip']}','{$memo}');";
			$result=$db->execSql($sql,false)?"success":"生成采购单失败，请重试";
			$result="success";
		}
	}
	echo $result;
}else if($action=='saveAddPur'){
	if(!preg_match($zz_date,$aNeedDate) || !is_numeric($aClass) || !is_numeric($aStorage) || isNull($aName) || isNull($aBrand) || !preg_match($zz_shuzhi,$aAmount) || floatval($aAmount)==0 || !preg_match($zz_userNick,$aApplyUser)){
		$result="必填数据填写不完整";
	}else{
		$sql="select * from user where comId={$_SESSION['comId']} and userNick='{$aApplyUser}' limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="输入的采购申请人账号不存在";
		}else{
			$sql="select * from purchase where comId={$_SESSION['comId']} and storage={$aStorage} and name='{$aName}' and barcode='{$aBarcode}' and brand='{$aBrand}' and colour='{$aColour}' limit 0,1";
			$row=$db->myQuery($sql,false);
			if($row===false || count($row)==0){
				$sql="insert into purchase(comId,storage,status,applyUser,needDate,class,name,barcode,vender,brand,colour,wrap,spe,unit,amount,addUser,ip) values({$_SESSION['comId']},{$aStorage},0,'{$aApplyUser}','{$aNeedDate}',{$aClass},'{$aName}','{$aBarcode}','{$aVender}','{$aBrand}','{$aColour}','{$aWrap}','{$aSpe}','{$aUnit}',{$aAmount},'{$_SESSION['userNick']}','{$cg['ip']}');";
				$result=$db->execSql($sql,false);
				$result=$result?"成功提交采购申请":"提交采购申请失败";
			}else{
				$result="相同的采购单已经存在,本次取消";
			}
		}
	}
	echo $result;
}else if($action=='auditPur'){
	if(!isset($v) || !is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="select * from purchase where comId={$_SESSION['comId']} and status=0 and id={$v} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="没有找到指定的采购单,或此采购单已经被审核";
		}else{
			$result="";
			$row=$row[0];
			rc_require("audit.html","purchase");
		}
	}
	echo $result;
}else if($action=='saveAuditPur'){
	if(!isset($purId) || !is_numeric($purId) || !isset($audit) || !is_numeric($audit)){
		$result="参数错误";
	}else{
		$sql="select * from purchase where comId={$_SESSION['comId']} and status=0 and id={$purId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="没有找到指定的采购单,或此采购单已经被审核";
		}else{
			$sql="update purchase set status={$audit},auditUser='{$_SESSION['userNick']}' where id={$row[0]['id']}";
			$result=$db->execSql($sql,false);
			$result=$result?"审核成功":"审核失败";
		}
	}
	echo $result;
}else if($action==='sfForAuditPur'){
	if(!isset($objId) || !is_numeric($objId) || !isset($status) || !is_numeric($status)){
		$result="参数错误";
	}else{
		$status=intval($status);
		if($status!==3 && $status!==4){
			$resutl="状态错误";
		}else{
			$sql="update purchase set status={$status}  where id={$objId} and comId={$_SESSION['comId']}";
			$result=$db->execSql($sql,false)===false?"保存失败":"保存成功";
		}
	}
	echo $result;
}else if($action==='sfForAuditPurNot'){
	if(!preg_match($zz_shuzhi,$objId) || !is_numeric($status)){
		$resutl="参数错误";
	}else{
		$status=intval($status)==1?1:2;
		$sql="update purnote set status={$status},buyTime=now() where comId={$_SESSION['comId']} and id={$objId};";
		$result=$db->execSql($sql,false)===true?"保存成功":"保存失败";
	}
	echo $result;
}
?>