<?php
$thisDir=realpath(dirname(__FILE__)."/../");
require_once($thisDir."/webini.inc.php");
extract($_POST);
$result='';
if($action==='applyFanCang'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from goods 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("applyfancang.html","sold");
		}
	}
	echo $result;
}else if($action==='saveApplyFanCang'){
	if(!isset($objId) || !is_numeric($objId) || !isset($aAmount) || !is_numeric($aAmount)){
		$result="参数错误";
	}else if(!isset($aStorage) || !is_numeric($aStorage)){
		$result="请选择返回到的仓库";
	}else{
		$storage=getStorage($aStorage);
		$aAmount=round(floatval($aAmount),2);
		if($storage==false){
			$result="请选择返回到的仓库，然后再保存";
		}else if($aAmount<=0){
			$result="返仓数量不能为0";
		}else{
			$sql="select * from goods 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($aAmount>round(floatval($row['amount']),2)){
					$result="返仓数量不能大于在售商品的剩余数量";
				}else{
					$sql="select * from stock where comId={$_SESSION['comId']} and id={$aStorage} and barcode='{$row['barcode']}' limit 1";
					$stock=$db->myQuery($sql,false);
					if($stock===false || count($stock)==0){
						$result="指定的仓库没有此产品的库存";
					}else if($aUnit!==$stock[0]['unit']){
						$result="与库存商品的数量单位不一致";
					}else{
						$sql="insert into fancang(goodsId,comId,class,branch,stock,storage,status,amount,unit,addUser,ip,memo) values({$row['id']},{$_SESSION['comId']},{$row['class']},{$_SESSION['branch']},{$row['id']},{$aStorage},0,{$aAmount},'{$aUnit}','{$_SESSION['userNick']}','{$cg['ip']}','{$aMemo}');";
						$sql.="update goods set amount=amount-{$aAmount} where id={$objId};";
						$result=$db->execSql($sql,false)===true?"保存成功":"保存失败";
					}
				}
			}
		}
	}
	echo $result;
}else if($action==='showApplyFanCang'){
	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","sold");
		}
	}
	echo $result;
}else if($action==='recallApplyFanCang'){
	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];
			if(intval($row['status'])!==0){
				$result="已审核的返仓申请不能撤销返仓";
			}else{
				$sql="update fancang set status=3 where id={$objId};";
				$sql.="udpate goods set amount=amount+{$row['amount']} where comId={$_SESSION['comId']} and id={$row['id']};";
				$result=$db->execSql($sql,false)===false?"保存失败":"保存成功";
			}
		}
	}
	echo $result;
}else if($action==='getGoodsFormAddPromotion'){
	if(isNull($v)){
		$result="参数错误";
	}else{
		$sql="select * from stock where barcode='{$v}' and comId={$_SESSION['comId']} and status=1 limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="没有找到对应的条码";
		}else if(intval($row[0]['status'])==0){
			$result="指定的产品不能销售";
		}else{
			$sql="select * from goods where comId={$_SESSION['comId']} and stock={$row[0]['id']}";
			$goods=$db->myQuery($sql,false);
			if($goods===false || count($goods)==0){
				$result="没有店铺销售此产品";
			}else{
				foreach($goods as $key=>$v){
					$branch=getBranch($v['branch']);
					$result.="<input type=\"checkbox\" name=\"aBranch\" value=\"{$v['id']}\">{$branch}&nbsp;";
				}
				$result.="<script type=\"text/javascript\">$('#price').html('{$row[0]['retail']}元/{$row[0]['unit']}');$('#unit').html('元/{$row[0]['unit']}');</script>";
			}
		}
	}
	echo $result;
}else if($action==='saveAddPromotion'){
	$aBranch=preg_replace($zz_trim,"",preg_replace($zz_douhao,",",$aBranch));
	if(isNull($aBranch) || isNull($aBarcode) || !preg_match($zz_shuzhi,$aPromotion) || floatval($aPromotion)==0 || !preg_match($zz_dateTime,$aStart) || !preg_match($zz_dateTime,$aEnd)){
		$result="参数错误";
	}else if(strtotime($aStart)>strtotime($aEnd)){
		$result="活动开始日期不能大于结束时间";
	}else{
		$sql="select * from stock where barcode='{$aBarcode}' and comId={$_SESSION['comId']} and status=1 limit 1";
		$product=$db->myQuery($sql,false);
		if($product===false || count($product)==0){
			$result="没有找到对应的库存产品条码";
		}else if(intval($product[0]['status'])==0){
			$result="指定的产品不能销售";
		}else{
			$sql="select * from goods where comId={$_SESSION['comId']} and stock={$product[0]['id']}";
			$goods=$db->myQuery($sql,false);
			if($goods===false || count($goods)==0){
				$result="没有店铺销售此产品";
			}else{
				$aMember=$aMember==='1'?1:0;
				$aBranch=",{$aBranch},";
				$sql="select * from promotion where goodsId=:goodsId and comId={$_SESSION['comId']}";
				$pdo=$db->conn->prepare($sql);
				$update='';
				$status=(strtotime($aStart)<=time() && strtotime($aEnd)>=time())?1:0;
				foreach($goods as $key=>$v){
					if(stripos($aBranch,",{$v['branch']},")!==false){
						$pdo->bindValue(":goodsId",$v['id']);
						$pdo->execute();
						$row=$pdo->fetchAll();
						if(count($row)==0){
							$update.="insert into promotion(comId,goodsId,branch,stock,status,member,start,end,addUser,ip) values({$_SESSION['comId']},{$v['id']},{$v['branch']},{$v['stock']},{$status},{$aMember},'{$aStart}','{$aEnd}','{$_SESSION['userNick']}','{$cg['ip']}');";
						}
					}
				}
				$aBranch=preg_replace($zz_trim,'',$aBranch);
				$update.="update goods set promotion={$aPromotion} where id in({$aBranch}) and comId={$_SESSION['comId']};";
				if(!isNull($update)){
					$result=$db->execSql($update,false)?"保存成功":"保存失败";
				}else{
					$result="保存成功";
				}
			}
		}
	}
	echo $result;
}else if($action==='setPromotionStop' || $action==='setPromotionStart'){
	if(!is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from promotion 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($action==='setPromotionStop'){
				if(intval($row['status'])===0){
					$result="已处于停止状态，不需要手动停止";
				}else{
					$sql="update promotion set status=0 where id={$objId}";
					$result=$db->execSql($sql,false)===true?'成功停止优惠活动':'停止优惠活动失败';
				}
			}else{
				if(intval($row['status'])===1 || strtotime($row['start'])>time() || strtotime($row['end'])<time()){
					$result="优惠活动已经过期，不能执行启用操作";
				}else{
					$sql="update promotion set status=1 where id={$objId}";
					$result=$db->execSql($sql,false)===true?'成功启用优惠活动':'启用优惠活动失败';
				}
			}
		}
	}
	echo $result;
}else if($action==='stopGoodsForManage' || $action==='startGoodsForManage'){
	if(!isset($objId) && !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from goods where comId={$_SESSION['comId']} and id={$objId}";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="指定的商品不存在";
		}else{
			if($action==='stopGoodsForManage'){
				$sql="update goods set status=0 where id={$objId}";
			}else{
				$sql="update goods set status=1 where id={$objId}";
			}
			$result=$db->execSql($sql,false)===true?"操作成功":"操作失败";
		}
	}
	echo $result;
}else if($action==='checkMemberFormExecSold'){
	if(isNull($v)){
		$result="会员账号为空";
		$sale=10;
	}else{
		$sql="select * from member where comId={$_SESSION['comId']} and account='{$v}' limit 1";
		$member=$db->myQuery($sql,false);
		if($member===false || count($member)==0){
			$result="账号不存在";
			$sale=10;
		}else{
			$sql="select * from grade where comId={$_SESSION['comId']} and id={$member[0]['grade']} limit 1";
			$grade=$db->myQuery($sql,false);
			if($grade===false || count($grade)==0){
				$result="会员等级不存在";
				$sale=10;
			}else{
				$result="会员等级：{$grade[0]['name']},享受{$grade[0]['sale']}折优惠";
				$sale=$grade[0]['sale'];
			}
		}
	}
	echo $result;
	echo"<script type=\"text/javascript\">$('#aSale').val({$sale});$('#member').val('{$v}');$('#goodsCode').focus();</script>";
}else if($action==='checkBarcodeForExecSold'){
	if(isNull($v)){
		$result="条码不能为空";
	}else{
		$sql="select * from goods where comId={$_SESSION['comId']} and barcode='{$v}' and branch={$_SESSION['branch']} 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];
			$sql="select * from stock where comId={$_SESSION['comId']} and id={$row['stock']} limit 1";
			$goods=$db->myQuery($sql,false);
			if($goods===false || count($goods)==0){
				$result='不存在此产品';
			}else{
				$goods=$goods[0];
				$price=round(floatval($row['retail']),2);
				$sql="select * from promotion where comId={$_SESSION['comId']} and goodsId={$row['id']} limit 1";
				$promotion=$db->myQuery($sql,false);
				if(is_array($promotion) && count($promotion)){
					if(!(strtotime($promotion[0]['start'])<=time() && strtotime($promotion[0]['end'])>=time()) && intval($promotion[0]['status'])==1){
						$sql="update promotion set status=0 where goodId={$row['id']}";
						$db->execSql($sql,false);
					}else{
						$price=round(floatval($row['promotion']),2);
					}
				}
				$result="<script type=\"text/javascript\">$('#lb_1').html('条码正确');$('#goodsName').val('{$goods['name']}');$('#brand').val('{$goods['brand']}');$('#lb_2').html('{$row['unit']}');$('#price').val({$price});$('#lb_3').html('元/{$row['unit']}');$('#soldSum').focus();</script>";
			}
		}
	}
	echo $result;
}else if($action==='applyChaiHe'){
	if(!isset($objId) || !is_numeric($objId)){
		$result="参数错误";
	}else{
		$sql="select * from goods 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","sold");
		}
	}
	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 goods 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);
						$promotion=$retail=round(floatval($row['retail'])/$cAmount,2);
						$sql="update goods set amount={$amount},unit='{$cUnit}',price={$price},retail={$retail},promotion={$promotion} 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'])*$cAmount,2);
							$promotion=$retail=round(floatval($row['retail'])*$cAmount,2);
							$sql="update goods set amount={$amount},unit='{$cUnit}',price={$price},retail={$retail},promotion={$promotion} where id={$objId};";
							$result=$db->execSql($sql,false)===true?"合并成功":"合并失败";
						}
					}
				}
			}
		}
	}
	echo $result;
}
?>