<?php
$thisDir=realpath(dirname(__FILE__)."/../");
require_once("{$thisDir}/webini.inc.php");
extract($_POST);
$result='';
if($action==='saveAddManageStorage'){
	if(isNull($aName) || isNull($aAddress) || isNull($aTelephone)){
		$result="新仓库的信息输入不完整";
	}else{
		$sql="select * from storage where comId={$_SESSION['comId']} and name='{$aName}' limit 1";
		$row=$db->myQuery($sql,false);
		if(is_array($row) && count($row)){
			$result="相同名称的仓库已经存在，请使用其它名称";
		}else{
			$sql="insert into storage(comId,name,address,telephone,addUser,ip) values({$_SESSION['comId']},'{$aName}','{$aAddress}','{$aTelephone}','{$_SESSION['userNick']}','{$cg['ip']}')";
			$result=$db->execSql($sql,false);
			$result=$result===true?"保存成功":"保存失败";
		}
	}
	echo $result;
}else if($action==='stopManageStorage'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="select sum(amount) as a from stock where comId={$_SESSION['comId']} and storage={$v} limit 1";
		$row=$db->myQuery($sql,false);
		if(isNull($row[0]['a']) || intval($row[0]['a'])==0){
			$sql="update storage set status=0 where comId={$_SESSION['comId']} and id={$v}";
			$result=$db->execSql($sql,false);
			$result=$result===true?"成功停用":"停用失败";
		}else{
			$result="此仓库中还有库存物品，请先将库存物品全部出库";
		}
	}
	echo $result;
}else if($action==='startManageStorage'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="update storage set status=1 where comId={$_SESSION['comId']} and id={$v} limit 1";
		$result=$db->execSql($sql,false);
		$result=$result===true?"成功启用":"启用失败";
	}
	echo $result;
}else if($action==='editManageStorage'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="select * from storage where comId={$_SESSION['comId']} and id={$v} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="未找到指定的仓库";
		}else{
			$row=$row[0];
			rc_require("showeditstorage.html","storage");
		}
	}
	echo $result;
}else if($action==='saveEditManageStorage'){
	if(isNull($eName) || isNull($eAddress) || isNull($eTelephone)){
		$result="仓库的信息输入不完整";
	}else{
		$sql="select * from storage where comId={$_SESSION['comId']} and name='{$eName}' and Id<>{$objId}";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$sql="update storage set name='{$eName}',address='{$eAddress}',telephone='{$eTelephone}' where comId={$_SESSION['comId']} and id={$objId}";
			$result=$db->execSql($sql,false);
			$result=$result===true?"保存成功":"保存失败";
		}else{
			$result="相同名称的仓库已经存在，请使用其它名称";
		}
	}
	echo $result;
}else if($action==="saveIncreaseClassify"){
	if(isNull($v)){
		$result="没有输入分类名称";
	}else if(!preg_match($zz_shuzhi,$aLrl) || intval($aLrl)<=0){
		$result="请正确输入当前分类的建议利润率";
	}else if(!preg_match($zz_shuzhi,$aAlarm) || round(floatval($aAlarm),2)<=0){
		$result="请正确输入当前分类的仓库报警数量";
	}else{
		$sql="select * from classify where comId={$_SESSION['comId']} and name='{$v}' limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$sql="insert into classify(comId,name,lrl,alarm,addUser,ip) values({$_SESSION['comId']},'{$v}',{$aLrl},{$aAlarm},'{$_SESSION['userNick']}','{$cg['ip']}');";
			$result=$db->execSql($sql,false);
			$result=$result===true?"保存成功":"保存失败";
		}else{
			$result="已经存在相同的分类名称";
		}
	}
	echo $result;
}else if($action==='alterClassify'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="select * from classify where comId={$_SESSION['comId']}";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="未添加商品分类";
		}else{
			$row=$row[0];
			rc_require("editclassify.html","storage");
		}
	}
	echo $result;
}else if($action==='saveAlterClassify'){
	if(!is_numeric($objId)){
		$result="参数错误";
	}else if(isNull($v)){
		$result="请正确输入分类的名称";
	}else if(!preg_match($zz_shuzhi,$eLrl) || intval($eLrl)<=0){
		$result="请正确输入当前分类的建议利润率";
		$result="请正确输入当前分类的建议利润率";
	}else if(!preg_match($zz_shuzhi,$eAlarm) || round(floatval($eAlarm),2)<=0){
		$result="请正确输入当前分类的仓库报警数量";
	}else{
		$sql="select * from classify where comId={$_SESSION['comId']} and name='{$v}' and id<>{$objId}";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)===0){
			$sql="update classify set name='{$v}',lrl={$eLrl},alarm={$eAlarm} where id={$objId}";
			$result=$db->execSql($sql,false);
			$result=$result===true?"保存成功":"保存失败";
		}else{
			$result="相同的分类名称已经存在";
		}
	}
	echo $result;
}else if($action==='dropClassify'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="delete from classify where comId={$_SESSION['comId']} and id={$v}";
		$result=$db->execSql($sql,false);
		$result=$result===true?"删除成功":"删除失败";
	}
	echo $result;
}else if($action==='checkLrlForInStorage'){
	$result=false;
	if(is_numeric($v) && intval($v)>0){
		$class=getClassify($v);
		if($class===true){
			$result="<script type=\"text/javascript\">$('#aLrl').val({$class['lrl']});</script>";
		}
	}
	echo $result;
}else if($action==='saveInStorage'){
	if(isNull($aName) || isNull($aAsName) || isNull($aBarcode) || $aStorage==='-1' || $acs==='-1' || isNull($aBarcode) || isNull($aBrand) || !preg_match($zz_shuzhi,$aAmount) || !preg_match($zz_shuzhi,$aPrice) || !preg_match($zz_shuzhi,$aRetail)){
		$result="必填项有错误";
	}else{
		$aAmount=round(floatval($aAmount),2);
		$aPrice=round(floatval($aPrice),2);
		$aRetail=round(floatval($aRetail),2);
		//查询库存中是否已经存在新入库的产品
		$sql="select * from stock where comId={$_SESSION['comId']} and barcode='{$aBarcode}'";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$sql="insert into stock(comId,storage,class,name,asName,barcode,vender,brand,colour,wrap,spe,unit,amount,price,average,retail,addUser,ip) values({$_SESSION['comId']},{$aStorage},{$acs},'{$aName}','{$aAsName}','{$aBarcode}','{$aVender}','{$aBrand}','{$aColour}','{$aWrap}','{$aSpe}','{$aUnit}',{$aAmount},{$aPrice},{$aPrice},{$aRetail},'{$_SESSION['userNick']}','{$cg['ip']}')";
			$result=$db->execSql($sql,false);
			$id=$db->lastInsertId;
		}else if($aUnit!==$row[0]['unit']){
			$result="与库存中的数量单位不一致";
		}else{
			$menySum=round(floatval($row[0]['amount']),2)*round(floatval($row[0]['average']),2)+$aAmount*$aPrice;
			$sum=$row[0]['amount']+$aAmount;
			$average=round($menySum/$sum,2);
			$sql="update stock set amount=amount+{$aAmount},price={$aPrice},average={$average},retail={$aRetail},addUser='{$_SESSION['userNick']}',addTime=now(),ip='{$cg['ip']}' where id={$row[0]['id']}";
			$result=$db->execSql($sql,false);
			$id=$row[0]['id'];
		}
		if($result){
			$meny=$aAmount*$aPrice;
			$sql="insert into instorage(comId,stock,barcode,amount,price,unit,meny,buyUser,buyDate,addUser,ip) values({$_SESSION['comId']},{$id},'{$aBarcode}',{$aAmount},{$aPrice},'{$aUnit}',{$meny},'{$aBuyUser}','{$aBuyDate}','{$_SESSION['userNick']}','{$cg['ip']}')";
			$result=$db->execSql($sql,false);
			$result=$result===true?"入库成功，写入入库记录成功":"入库成功,但写入入库记录出错";
		}else{
			$result="入库失败,请重试";
		}
	}
	echo $result;
}else if($action==='editIn'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="select a.id as inId,a.amount as inAmount,a.unit as inUnit,a.buyUser,a.buyDate,b.* from instorage as a inner join stock as b on a.stock=b.id where b.comId={$_SESSION['comId']} and a.id={$v}";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的入库记录";
		}else{
			$result="";
			$row=$row[0];
			rc_require("editin.html","storage");
		}
	}
	echo $result;
}else if($action==='saveEditIn'){
	if(isNull($eName) || isNull($eAsName) || !is_numeric($objId) || isNull($eBarcode) || !is_numeric($ecs) || intval($ecs)<1 || !is_numeric($eStorage) || intval($eStorage)<1 || !is_numeric($eAmount) || !is_numeric($ePrice) || !is_numeric($eRetail) || !preg_match($zz_userNick,$eBuyUser) || !preg_match($zz_date,$eBuyDate)){
		$result="参数错误";
	}else{
		$eStorage=intval($eStorage);
		$ecs=intval($ecs);
		$eAmount=round(floatval($eAmount),2);
		$ePrice=round(floatval($ePrice),2);
		$eRetail=round(floatval($eRetail),2);
		$sql="select * from user where comId={$_SESSION['comId']} and userNick='{$eBuyUser}'";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="采购人账号不存在";
		}else{
			$sql="select * from instorage where comId={$_SESSION['comId']} and id={$objId}";
			$row=$db->myQuery($sql,false);
			if($row===false || count($row)==0){
				$result="不存在指定的入库记录";
			}else{
				$row=$row[0];
				$sql="select * from stock where comId={$_SESSION['comId']} and id={$row['stock']}";
				$stock=$db->myQuery($sql,false);
				if($stock===false || count($stock)==0){
					$result="库存中不存在指定的产品信息";
				}else{
					$oldPrice=round(floatval($stock[0]['price']),2);
					$average=round(($oldPrice+$ePrice)/2,2);
					$amount=$eAmount-round(floatval($row['amount']),2);
					$meny=$ePrice*$eAmount;
					$sql="update stock set storage={$eStorage},class={$ecs},name='{$eName}',asName='{$eAsName}',barcode='{$eBarcode}',vender='{$eVender}',brand='{$eBrand}',colour='{$eColour}',wrap='{$eWrap}',spe='{$eSpe}',unit='{$eUnit}',amount=amount+{$amount},price={$ePrice},average={$average},retail={$eRetail},addUser='{$_SESSION['userNick']}',ip='{$cg['ip']}' where id={$row['stock']};";
					$sql.="update instorage set amount={$eAmount},price={$ePrice},unit='{$eUnit}',meny={$meny},buyUser='{$eBuyUser}',buyDate='{$eBuyDate}',addUser='{$_SESSION['userNick']}',ip='{$cg['ip']}' where id={$objId};";
					$result=$db->execSql($sql,false)===true?"成功保存修改":"保存失败";
				}
				
			}
		}
	}
	echo $result;
}else if($action==='searchStockIn'){
	if(isNull($v)){
		$result="参数错误";
	}else{
		$sql="select * from purchase where comId={$_SESSION['comId']} and barcode='{$v}' limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$sql="select * from stock where comId={$_SESSION['comId']} and barcode='{$v}' limit 1";
			$row=$db->myQuery($sql,false);
			if($row===false || count($row)==0){
				$result="可以使用";
			}else{
				$row=$row[0];
				$row['purUser']='';
			}
		}else{
			$row=$row[0];
			$row['asName']=$row['name'];
			$row['price']=0.0;
			$row['aPrice']=0.0;
			$sql="select * from purnote where find_in_set({$row['id']},purId) and comId={$_SESSION['comId']} order by id desc limit 1";
			$purNote=$db->myQuery($sql,false);
			if($purNote===false || count($purNote)==0){
				$row['purUser']='';
			}else{
				$row['purUser']=$purNote[0]['purUser'];
			}
		}
		if(is_array($row) && count($row)){
			if(!isNull($aName) && $aName!==$row['name']){
				$result="已经存在相同条码的其它产品<script type=\"text/javascript\">$('#aBarcode').val('');</script>";
			}else if(isNull($aName) || $aName!==$row['name']){
				rc_require("debris_1.html","storage");
			}
		}
	}
	echo $result;
}else if($action==='searchBarcodeForEditIn'){
	if(isNull($eBarcode) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from stock where comId={$_SESSION['comId']} and barcode='{$eBarcode}' and id<>{$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="可以使用";
			rc_require("debris_3.html","storage");
		}else{
			$result="已经存在相同条码的其它产品<script type=\"text/javascript\">$('#eBarcode').val('');</script>";
		}
	}
	echo $result;
}else if($action==='editStock'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="select * from stock where comId={$_SESSION['comId']} and id={$v}";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="未找到指定的库存产品";
		}else{
			$row=$row[0];
			rc_require("editstock.html","storage");
		}
	}
	echo $result;
}else if($action==="saveEidtStock"){
	if(isNull($eName) || isNull($eAsName) || $eStorage==='-1' || $ecs==='-1' || isNull($eBrand) || !preg_match($zz_shuzhi,$eAmount) || !preg_match($zz_shuzhi,$ePrice) || !preg_match($zz_shuzhi,$eRetail)){
		$result="必填项有错误";
	}else{
		$eAmount=floatval($eAmount);
		$ePrice=floatval($ePrice);
		$eRetail=floatval($eRetail);
		//查询库存中是否已经存在新入库的产品
		$sql="select * from stock where comId={$_SESSION['comId']} and barcode='{$eBarcode}'";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的库存产品";
		}else{
			$average=$row[0]['price']==$ePrice?$row[0]['average']:$ePrice;
			$sql="update stock set storage={$eStorage},class={$ecs},name='{$eName}',asName='{$eAsName}',barcode='{$eBarcode}',vender='{$eVender}',brand='{$eBrand}',colour='{$eColour}',wrap='{$eWrap}',spe='{$eSpe}',unit='{$eUnit}',amount={$eAmount},price={$ePrice},retail={$eRetail},average={$average},addUser='{$_SESSION['userNick']}',addTime=now(),ip='{$cg['ip']}' where id={$row[0]['id']}";
			$result=$db->execSql($sql,false)===true?"保存成功":"保存失败";
		}
	}
	echo $result;
}else if($action==='applyOutStock'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="select * from stock where comId={$_SESSION['comId']} and id={$v}";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="没有找到指定的库存产品";
		}else{
			$row=$row[0];
			rc_require("applyout.html","storage");
		}
	}
	echo $result;
}else if($action==='saveApplyOutStock'){
	if(!preg_match($zz_shuzhi,$bName) || $bName==='-1' || !preg_match($zz_shuzhi,$outSum) || !is_numeric($objId) || !preg_match($zz_number,$bName)){
		$result="参数错误";
	}else{
		$outSum=floatval($outSum);
		$sql="select * from branch where comId={$_SESSION['comId']} and id={$bName} limit 1";
		$row=$db->myQuery($sql,false);
		if(is_array($row) && count($row)){
			if($outSum<=0){
				$result="出库数量不能为0";
			}else{
				$sql="select * from stock where comId={$_SESSION['comId']} and id={$objId} limit 1";
				$row=$db->myQuery($sql,false);
				if($row===false || count($row)==0){
					$result="没有找到指定的库存产品";
				}else{
					$row=$row[0];
					$amount=floatval($row['amount']);
					$sql="select * from goods where comId={$_SESSION['comId']} and barcode='{$row['barcode']}' limit 1";
					$goods=$db->myQuery($sql,false);
					if($amount==0){
						$result="此产品的库存为0,不能申请出库";
					}else if($amount<$outSum){
						$result="库存数量不足，请减少出库数量后再提交申请";
					}else if(is_array($goods) && count($goods)>0 && $row['unit']!==$goods[0]['unit']){
						$result="库存数量单位与在售商品的数量单位不一致，不能申请出库";
					}else{
						$amount=$amount-$outSum;
						$memo=str_replace(array("\r\n","\r","\n"),"<br />",$memo);
						$sql="update stock set amount={$amount} where id={$row['id']};";
						$sql.="insert into applyout(comId,stock,branch,applyUser,ip,amount,unit,memo) values({$_SESSION['comId']},{$row['id']},{$bName},'{$_SESSION['userNick']}','{$cg['ip']}',{$outSum},'{$row['unit']}','{$memo}');";
						$result=$db->execSql($sql,false);
						$result=$result==false?"提交申请失败，请重试":"已经成功提交申请，请等待管理人员的审核";
					}
				}
			}
		}else{
			$result="不存在指定的分店";
		}
	}
	echo $result;
}else if($action==='auditApplyOut'){
	$result="";
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="select * from applyout where comId={$_SESSION['comId']} and id={$v} and status=-1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的出库申请或出库申请已经被审核";
		}else{
			$row=$row[0];
			rc_require("debris_2.html","storage");
		}
	}
	echo $result;
}else if($action==='saveAuditApplyOut'){
	if(!is_numeric($objId) || ($v!=='1' && $v!=='0')){
		$result="参数错误";
	}else{
		$v=intval($v);
		$sql="select * from applyout where comId={$_SESSION['comId']} and id={$objId} and status=-1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的出库申请或出库申请已经被审核";
		}else{
			$sql="update applyout set status={$v},auditUser='{$_SESSION['userNick']}',memo='{$memo}' where id={$objId};";
			if($v===0){
				$sql.="update stock set amount=amount+{$row[0]['amount']} where id={$row[0]['stock']}";
			}
			$result=$db->execSql($sql,false);
			$result=$result===true?"审核结果已经成功保存":"保存审核失败";
		}
		
	}
	echo $result;
}else if($action==='auditOutMemo'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="select * from applyout where comId={$_SESSION['comId']} and id={$v}";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="出库申请不存在";
		}else{
			$result="";
			echo"<div style=\"text-align:left;\"><strong>审核人:</strong>",$row[0]['auditUser'],"<br /><strong>留言信息:</strong><br />",$row[0]['memo'],"</div>";
		}
	}
	echo $result;
}else if($action==='execOutStorage'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else{
		$sql="select * from applyout where comId={$_SESSION['comId']} and id={$v} and status=1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="出库申请不存在或未通过审核";
		}else{
			$row=$row[0];
			$result='';
			rc_require("outstock.html","storage");
		}
	}
	echo $result;
}else if($action==='saveOutStorage'){
	if(!preg_match($zz_number,$objId) || !preg_match($zz_userNick,$apply) || isNull($password)){
		$result="请输入产品出库接收人的账号和密码";
	}else if(!preg_match($zz_shuzhi,$outSum) || intval($outSum)<=0){
		$result="请正确输入实际出库的数量";
	}else if(!preg_match($zz_date,$outDate)){
		$result="请正确输入出库的日期";
	}else{
		$sql="select userNick,password,privatePass from user where userNick='{$apply}' and comId='{$_SESSION['comId']}'";
		$applyUser=$db->myQuery($sql,false);
		if($applyUser===false || count($applyUser)==0){
			$result="账号不存在";
		}else{
			$ps=md5($password.$applyUser[0]['privatePass']);
			if($ps!==$applyUser[0]['password']){
				$result="接收人的登录密码错误";
			}else{
				unset($applyUser,$ps);
				$sql="select * from applyout where comId={$_SESSION['comId']} and id={$objId} and status=1 and outed=0";
				$row=$db->myQuery($sql,false);
				if($row===false || count($row)==0){
					$result="出库申请单不存在,不是审核通过状态,或已经出库";
				}else{
					$row=$row[0];
					$sql="select * from stock where comId={$_SESSION['comId']} and id={$row['stock']}";
					$product=$db->myQuery($sql,false);
					if($product===false || count($product)==0){
						$result="仓库中不存在申请出库的产品";
					}else{
						$product=$product[0];
						$amount=floatval($product['amount']);
						if($amount<$outSum){
							$result="库存产品数量不足,请减少实际出库数量";
						}else{
							$sql="update applyout set outed=1 where id={$objId};";
							$sql.="insert into outstorage(comId,stock,applyOut,retail,amount,unit,outDate,apply,addUser,ip) values({$_SESSION['comId']},{$product['id']},{$row['id']},{$product['retail']},{$amount},'{$row['unit']}','{$outDate}','{$apply}','{$_SESSION['userNick']}','{$cg['ip']}');";
							$p="select * from goods where comId={$_SESSION['comId']} and branch=row['branch'] and barcode='{$product['barcode']}';";
							$exists=$db->myQuery($p,false);
							if($exists===false || count($exists)==0){
								$sql.="insert into goods(comId,stock,class,branch,name,asName,barcode,vender,brand,colour,wrap,spe,unit,amount,price,average,retail,promotion,addUser,ip) values({$_SESSION['comId']},{$row['stock']},{$product['class']},{$row['branch']},'{$product['name']}','{$product['asName']}','{$product['barcode']}','{$product['vender']}','{$product['brand']}','{$product['colour']}','{$product['wrap']}','{$product['spe']}','{$product['unit']}',{$product['amount']},{$product['price']},{$product['average']},{$product['retail']},{$product['retail']},'{$_SESSION['userNick']}','{$cg['ip']}');";
							}else{
								$sql.="update goods set amount=amount+$outSum where comId={$_SESSION['comId']} and id={$exists[0]['id']};";
								
							}
							if(floatval($row['amount'])!=$outSum){
								$sql.="update stock set amount=amount+{$row['amount']}-$outSum where id={$row['stock']};";
							}
							$result=$db->execSql($sql,false)===true?"产品已经成功出库":"产品出库失败,请重试";
						}
					}
				}
			}
		}
	}
	echo $result;
}else if($action==='editOut'){
	if(!is_numeric($v) || intval($v)<1){
		$result="参数错误";
	}else{
		$sql="select a.id as outId,a.stock,a.applyOut,a.amount,a.unit as outUnit,a.apply,a.retail,a.outDate,a.addUser,a.addTime,a.ip,b.id,b.comId,b.storage,b.class,b.name,b.asName,b.barcode,b.vender,b.brand,b.colour,b.wrap,b.spe,b.price from outstorage as a inner join stock as b on a.stock=b.id where b.comId={$_SESSION['comId']} and b.id=a.stock limit 0,1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的出库记录";
		}else{
			$row=$row[0];
			$result='';
			rc_require("editout.html","storage");
		}
	}
	echo $result;
}else if($action==='saveEditOut'){
	if(!preg_match($zz_shuzhi,$eAmount) || floatval($eAmount)<=0){
		$result="请输入出库数量";
	}else if(!preg_match($zz_shuzhi,$eRetail) || floatval($eRetail)<=0){
		$result="请正确输入零售价格";
	}else if(!preg_match($zz_date,$eOutDate)){
		$result="请正确输入出库日期";
	}else if(!preg_match($zz_userNick,$eApply)){
		$result="请正确输入接收人的登录账号";
	}else if(!is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from user where comId={$_SESSION['comId']} and userNick='{$eApply}' limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="输入的登录账号不存在";
		}else{
			$sql="select * from outstorage where comId={$_SESSION['comId']} and id={$objId} limit 1";
			$row=$db->myQuery($sql,false);
			if($row===false || count($row)==0){
				$result="不存在指定的出库记录";
			}else{
				$sql="select * from stock where comId={$_SESSION['comId']} and id={$row[0]['stock']} limit 1";
				$goods=$db->myQuery($sql,false);
				if($goods===false || count($goods)==0){
					$result="不存在指定的商品库存";
				}else if($goods[0]['unit']!==$eUnit){
					$result="与库存中的数量单位不一致";
				}else{
					$sql="update stock set amount=amount-({$eAmount}-{$row[0]['amount']}) where comId={$_SESSION['comId']} and id={$row[0]['stock']};";
					$sql.="update outstorage set retail={$eRetail},amount={$eAmount},unit='{$eUnit}',outDate='{$eOutDate}',apply='{$eApply}' where id={$objId}";
					$result=$db->execSql($sql,false);
					$result=$result===true?"修改成功":"修改失败,请重试";
				}
			}
		}
	}
	echo $result;
}else if($action==='applyTuiHuo'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from instorage where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的产品入库信息";
		}else{
			$result='';
			$row=$row[0];
			rc_require("tuihuo.html","storage");
		}
	}
	echo $result;
}else if($action==='saveApplyTuiHuo'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else if(!isset($aSum) || !preg_match($zz_shuzhi,$aSum) || floatval($aSum)==0){
		$result="退货数量不能为0";
	}else{
		$aSum=round(floatval($aSum),2);
		$sql="select * from instorage where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的产品入库信息";
		}else if($aSum>round(floatval($row[0]['amount']),2)){
			$result="申请退货数量不能大于本次采购数量";
		}else{
			$row=$row[0];
			$sql="select * from stock where comId={$_SESSION['comId']} and id={$row['stock']} limit 1";
			$goods=$db->myQuery($sql,false);
			if($goods===false){
				$result="仓库中没有此产品的信息，申请被取消！";
			}else{
				$goods=$goods[0];
				$price=round(floatval($row['price']),2);
				$aSum=round(floatval($aSum),2);
				$meny=$price*$aSum;
				$aMemo=str_replace(array("\r\n","\r","\n"),"<br />",$aMemo);
				$sql="insert into tuihuo(inId,comId,stock,status,class,barcode,amount,unit,price,meny,auditUser,addUser,ip,memo) values({$row['id']},{$_SESSION['comId']},{$row['stock']},0,{$goods['class']},'{$row['barcode']}',{$aSum},'{$row['unit']}',{$price},{$meny},'','{$_SESSION['userNick']}','{$cg['ip']}','{$aMemo}');";
				$sql.="update stock set amount=amount-{$aSum} where comId={$_SESSION['comId']} and id={$row['stock']};";
				$sql.="update instorage set amount=amount-{$aSum} where comId={$_SESSION['comId']} and id={$row['id']};";
				$result=$db->execSql($sql,false)===true?"成功保存":"保存失败";
			}
		}
	}
	echo $result;
}else if($action==='execAuditTuiHuo'){
	if(!isset($objId)  || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from tuihuo where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的退货申请";
		}else{
			$row=$row[0];
			rc_require("exectuihuo.html","storage");
		}
	}
	echo $result;
}else if($action==='saveAuditTuiHuo'){
	if(!isset($objId)  || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sjSum=(!preg_match($zz_shuzhi,$sjSum) || floatval($sjSum)<=0)?0:round(floatval($sjSum),2);
		if($sjSum===0){
			$result="请正确输入实际退货数量";
		}else{
			$sql="select * from tuihuo where comId={$_SESSION['comId']} and id={$objId} limit 1";
			$row=$db->myQuery($sql,false);
			if($row===false || count($row)==0){
				$result="不存在指定的退货申请";
			}else{
				$row=$row[0];
				$sql="select * from instorage where comId={$_SESSION['comId']} and id={$row['inId']} limit 1";
				$inRow=$db->myQuery($sql,false);
				if($inRow===false){
					$result="对应产品的入库记录不存在";
				}else{
					$inRow=$inRow[0];
					$inAmount=round(floatval($inRow['amount']),2);
					$price=round(floatval($row['price']),2);
					$amount=round(floatval($row['amount']),2);
					$meny=$price*$sjSum;
					if($sjSum>$inAmount){
						$result="实际退货数量不能大于本条入库数量";
					}else{
						$sql="select * from stock where comId={$_SESSION['comId']} and id={$row['stock']} limit 1";
						$goods=$db->myQuery($sql,false);
						if($goods===false){
							$result="仓库中不存在指定产品的信息";
						}else{
							$sql="update tuihuo set status=1,sjSum={$sjSum},meny={$meny},auditUser='{$_SESSION['userNick']}',auditTime='{$cg['dateTime']}' where id={$objId};";
							if($sjSum>$amount){
								$cha=$sjSum-$amount;
								$sql.="update instorage set amount=amount-{$cha},meny=meny-{$meny} where id={$row['inId']};";
								$sql.="update stock set amount=amount-{$cha} where id={$row['stock']};";
							}else if($sjSum<$amount){
								$char=$amount-$sjSum;
								$sql.="update instorage set amount=amount+{$cha},meny=meny-{$meny} where id={$row['inId']};";
								$sql.="update stock set amount=amount+{$cha} where id={$row['stock']};";
							}else{
								$sql.="update instorage set meny=meny-{$meny} where id={$row['inId']};";
							}
							$result=$db->execSql($sql,false)===true?"保存成功":"保存失败";
						}
					}
				}
			}
		}
	}
	echo $result;
}else if($action==='execFailedAuditTuiHuo'){
	if(!isset($objId)  || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from tuihuo where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的退货申请";
		}else{
			$row=$row[0];
			rc_require("failedtuihuo.html","storage");
		}
	}
	echo $result;
}else if($action==='saveExecFailedAuditTuiHuo'){
	if(!isset($objId)  || !is_numeric($objId)){
		$result="参数错误";
	}else if(!isset($because) || isNull($because)){
		$result="请输入退货失败的原因";
	}else{
		$sql="select * from tuihuo where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的退货申请";
		}else{
			$row=$row[0];
			$amount=round(floatval($row['amount']),2);
			$because=str_replace(array("\r\n","\r","\n"),"<br />",$because);
			$sql="update tuihuo set status=2,memo=concat_ws('<br />',memo,'{$because}') where comId={$_SESSION['comId']} and id={$objId};";
			$sql.="update instorage set amount=amount+{$amount} where comId={$_SESSION['comId']} and id={$row['inId']};";
			$sql.="update stock set amount=amount+{$amount} where comId={$_SESSION['comId']} and id={$row['stock']};";
			die($sql);
			$result=$db->execSql($sql,false)===true?"保存成功":"保存失败";
		}
	}
	echo $result;
}else if($action==='showMemoAuditTuiHuo'){
	if(!isset($objId)  || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from tuihuo where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="没有找到指定的退货申请";
		}else{
			$result=$row[0]['memo'];
		}
	}
	echo $result;
}else if($action==='showAuditFanCang'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from fancang where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的返仓申请";
		}else{
			$row=$row[0];
			rc_require("showfancang.html","storage");
		}
	}
	echo $result;
}else if($action==='auditFanCang'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from fancang where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的返仓申请";
		}else if(intval($row[0]['status'])!==0){
			$result="指定的返仓申请已被审核，不能重复审核！";
		}else{
			$row=$row[0];
			rc_require("auditfancang.html","storage");
		}
	}
	echo $result;
}else if($action==='saveAuditFanCang'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else if(!isset($audit) || !is_numeric($audit)){
		$result="请选中一选审核结果后再保存";
	}else{
		$sql="select * from fancang where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的返仓申请";
		}else if(intval($row[0]['status'])!==0){
			$result="指定的返仓申请已被审核，不能重复审核！";
		}else{
			$row=$row[0];
			$status=intval($audit)===1?1:2;
			$memo=str_ireplace(array("\r","\n","\r\n"),"<br />",$memo);
			$sql="update fancang set status={$status},auditUser='{$_SESSION['userNick']}',auditTime=now(),memo=concat_ws('<br />',memo,'{$memo}') where id={$objId};";
			if($status===2){
				$sql.="update goods set amount=amount+{$row['amount']} where id={$row['goodsId']};";
			}
			$result=$db->execSql($sql,false)===true?"保存成功":"保存失败";
		}
	}
	echo $result;
}else if($action==='jieshouAuditFanCang'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from fancang where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的返仓申请";
		}else if(intval($row[0]['status'])!==1){
			$result="指定的返仓申请未被审核通过，仓库不能接收！";
		}else{
			$row=$row[0];
			rc_require("jieshouauditfancang.html","storage");
		}
	}
	echo $result;
}else if($action==='savejsFanCang'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else if(!isset($aAmount) || !is_numeric($aAmount) || round(floatval($aAmount),2)<0){
		$result="实际接收数量错误";
	}else{
		$sql="select * from fancang where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的返仓申请";
		}else if(intval($row[0]['status'])!==1){
			$result="指定的返仓申请未被审核通过，仓库不能接收！";
		}else{
			$row=$row[0];
			$amount=round(floatval($row['amount']),2);
			if(round(floatval($aAmount),2)>$amount){
				$result="实际接收数量不能大于申请返回的数量";
			}else{
				$sql="update fancang set status=4 where id={$objId};";
				$sql.="update stock set amount=amount+{$row['amount']} where id={$row['stock']};";
				$result=$db->execSql($sql,false)===true?"保存成功":"保存失败";
			}
		}
	}
	echo $result;
}else if($action==='applyBaoFei'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from stock where comId={$_SESSION['comId']} and id={$objId} limit 1;";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的库存商品";
		}else{
			$row=$row[0];
			rc_require("applybaofei.html","storage");
		}
	}
	echo $result;
}else if($action==='saveApplyBaoFei'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else if(!isset($aAmount) || !is_numeric($aAmount)){
		$result="请输入申请报废的数量";
	}else if(!isset($aMemo) || isNull($aMemo)){
		$result="请输入申请报废的原因";
	}else{$sql="select * from stock where comId={$_SESSION['comId']} and id={$objId} limit 1;";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的库存商品";
		}else{
			$row=$row[0];
			$amount=round(floatval($row['amount']),2);
			$aAmount=round(floatval($aAmount),2);
			if($amount<$aAmount){
				$result="申请报废的数量不能大于库存数量";
			}else{
				$sql="update stock set amount=amount-{$aAmount} where id={$objId};";
				$sql.="insert into baofei(comId,stock,class,storage,status,name,barcode,brand,amount,unit,price,addUser,ip,memo) values('{$_SESSION['comId']}',{$row['id']},{$row['class']},{$row['storage']},0,'{$row['name']}','{$row['barcode']}','{$row['brand']}',{$aAmount},'{$row['unit']}',{$row['price']},'{$_SESSION['userNick']}','{$cg['ip']}','{$aMemo}');";
				$result=$db->execSql($sql,false)===true?"保存成功":"保存失败";
			}
		}
	}
	echo $result;
}else if($action==='showApplyBaoFei'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from baofei where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的商品库存";
		}else{
			$row=$row[0];
			rc_require("showbaofei.html","storage");
		}
	}
	echo $result;
}else if($action==='auditBaoFei'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from baofei where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的商品库存";
		}else{
			$row=$row[0];
			rc_require("auditbaofei.html","storage");
		}
	}
	echo $result;
}else if($action==="saveAuditBaoFei"){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else if(!isset($audit) || !is_numeric($audit)){
		$result="请选中一项审核结果后再保存";
	}else{
		$sql="select * from baofei where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的报废申请";
		}else if(intval($row[0]['status'])!==0){
			$result="指定的报废申请已被审核，不能重复审核！";
		}else{
			$row=$row[0];
			$status=intval($audit)===1?1:2;
			$memo=str_ireplace(array("\r","\n","\r\n"),"<br />",$memo);
			$sql="update baofei set status={$status},auditUser='{$_SESSION['userNick']}',auditTime=now(),memo=concat_ws('<br />',memo,'{$memo}') where id={$objId};";
			if($status===2){
				$sql.="update stock set amount=amount+{$row['amount']} where id={$row['stock']};";
			}
			$result=$db->execSql($sql,false)===true?"保存成功":"保存失败";
		}
	}
	echo $result;
}else if($action==='applyChaiHe'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from stock where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="指定的库存商品不存在";
		}else{
			$row=$row[0];
			rc_require("applychaihe.html","storage");
		}
	}
	echo $result;
}else if($action==='saveApplyChaiHe'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else if(!isset($operateType) || !is_numeric($operateType)){
		$result="非法操作";
	}else{
		$operateType=intval($operateType);
		if($operateType!==1 && $operateType!==2){
			$result="非法操作";
		}else{
			$sql="select * from stock where comId={$_SESSION['comId']} and id={$objId} limit 1";
			$row=$db->myQuery($sql,false);
			if($row===false || count($row)==0){
				$result="指定的库存商品不存在";
			}else{
				$row=$row[0];
				if($operateType===1){
					if(!isset($cAmount) || !is_numeric($cAmount) || round(floatval($cAmount),2)<=0 || !isset($cUnit)){
						$result="非法操作";
					}else{
						$cAmount=round(floatval($cAmount),2);
						$amount=round(floatval($row['amount']),2)*$cAmount;
						$price=round(floatval($row['price'])/$cAmount,2);
						$retail=round(floatval($row['retail'])/$cAmount,2);
						$sql="update stock set amount={$amount},unit='{$cUnit}',price={$price},retail={$retail} where id={$objId};";
						$result=$db->execSql($sql,false)===true?"拆分成功":"拆分失败";
					}
				}else{
					if(!isset($hAmount) || !is_numeric($hAmount) || round(floatval($hAmount),2)<=0 || !isset($hUnit)){
						$result="非法操作";
					}else{
						$cAmount=round(floatval($hAmount),2);
						if(round(floatval($row['amount']),2)%$hAmount>0){
							$result="合并后数量不为整数，不能合并！";
						}else{
							$amount=round(floatval($row['amount']),2)/$hAmount;
							$price=round(floatval($row['price'])*$hAmount,2);
							$retail=round(floatval($row['retail'])*$hAmount,2);
							$sql="update stock set amount={$amount},unit='{$cUnit}',price={$price},retail={$retail} where id={$objId};";
							$result=$db->execSql($sql,false)===true?"合并成功":"合并失败";
						}
					}
				}
			}
		}
	}
	echo $result;
}
?>