<?php
require("webini.inc.php");
if(isset($_SESSION['userNick']) && !checkLogined()){
	die("请登录后访问");
}
extract($_REQUEST);
rc_require("header.html");
if(!isset($mod) || !isset($action)){
	die("Access Denied");
}
$page=(!isset($page) || !preg_match($zz_shuzhi,$page) || intval($page)<1)?1:intval($page);
if($mod==='sys'){//系统参数配置
	if($action=='showSys'){
	  $row=$db->myQuery("select * from company order by id limit 0,1",false);
	  if(count($row)){
		$row=$row[0];
	  }else{
		$name='未定义';
		$address=$telephone='';
		$id=0;
	  }
	  rc_require("show.html",'sys');
	}else if($action==='refreshCache'){
		rc_require("refreshcache.html","sys");
	}else{
		echo "参数错误";
	}
}else if($mod=='office'){//部门管理
	if($action=='showOffice'){
		$sql="select * from office where comId={$_SESSION['comId']} order by comId";
		$row=$db->page($sql,10,$page);
		rc_require("show.html",'office');
	}else if($action=='addOffice'){
		$company=md::findCompany($_SESSION['comId']);
		rc_require("addoffice.html",'office');
	}
}else if($mod==="personnel"){//人事管理
	if($action=='searchStation'){
		$sql="select * from station where comId={$_SESSION['comId']}";
		$row=$db->myQuery($sql,false);
		rc_require("showstation.html","personnel");
	}else if($action=='searchUser'){
		$sNick=(!isset($sNick) || !preg_match($zz_userNick,$sNick))?'':$sNick;
		$sName=(!isset($sName) || isNull($sName) || mb_strlen($sName,'utf-8')>20)?'':$sName;
		$sOffice=(!isset($sOffice) || !is_numeric($sOffice))?-1:(int)$sOffice;
		$sSex=(!isset($sSex) || !is_numeric($sSex))?0:(int)$sSex;
		$where="where comId={$_SESSION['comId']}";
		$where.=(!isset($sNick) || isNull($sNick))?'':" and userNick='{$sNick}'";
		$where.=(!isset($sName) || isNull($sName))?'':" and name='{$sName}'";
		$where.=(!isset($sBranch) || !is_numeric($sBranch) || intval($sBranch)===-1)?"":" and branch={$sBranch}";
		$where.=(!isset($sOffice) || !is_numeric($sOffice) || intval($sOffice)===-1)?'':" and officeId={$sOffice}";
		$where.=(!isset($sSex) || !is_numeric($sSex) || intval($sSex)===0)?'':" and sex={$sSex}";
		$sql="select * from user {$where} order by id desc";
		$row=$db->page($sql,15,$page);
		rc_require("showuser.html","personnel");
	}else if($action=='displayPower'){
		if(isset($search) && $search=='yes'){
			$where=" where comId={$_SESSION['comId']}";
			$where.=(isset($sNick) && preg_match($zz_userNick,$sNick))?" and userNick='{$sNick}'":'';
			$where.=(isset($sName) && !isNull($sName) && mb_strlen($sName,"utf-8")<20)?" and name='{$sName}'":'';
			$where.=(isset($sOffice) && is_numeric($sOffice) && intval($sOffice)!=0)?" and officeId={$sOffice}":'';
			$sql="select a.id as aId,a.operateObj,a.operate,b.* from power as a inner join (select * from user {$where}) as b on a.userNick=b.userNick";
		}else{
			$sql="select a.id as aId,a.operateObj,a.operate,b.* from power as a inner join user as b on a.userNick=b.userNick";
		}
		$row=$db->page($sql,5,$page);
		rc_require("showpower.html","personnel");
	}else if($action==='tongzhi'){
		rc_require("tongzhi.html","personnel");
	}else if($action==='serachTongZhi'){
		$where=" where comId={$_SESSION['comId']}";
		$where.=(isset($sCaption) && !isNull($sCaption))?" and caption='{$sCaption}'":'';
		$where.=(isset($sStatus) && is_numeric($sStatus) && intval($sStatus)!==-1)?" and status={$sStatus}":'';
		$sql="select * from tongzhi {$where} order by id desc";
		$row=$db->myQuery($sql,false);
		rc_require("searchtongzhi.html","personnel");
	}
}else if($mod==="member"){
	if($action=='searchGrade'){
		$where=" where comId={$_SESSION['comId']}";
		$sql="select * from grade {$where} order by id desc";
		$row=$db->page($sql,10,$page);
		rc_require("grade.html","member");
	}else if($action=='searchMember'){
		$where=" where comId={$_SESSION['comId']}";
		$where.=(isset($sAccount) && preg_match($zz_userNick,$sAccount))?" and account='{$sAccount}'":'';
		$where.=(isset($sSex) && is_numeric($sSex))?" and sex={$sSex}":'';
		$where.=(isset($sTelephone) && !isNull($sTelephone))?" and telephone='{$sTelephone}'":'';
		$where.=(isset($sName) && !isNull($sName))?" and name='{$sName}'":'';
		$where.=(isset($sBranch) && is_numeric($sBranch))?" and branch={$sBranch}":'';
		$sql="select sum(meny) as yue from member {$where}";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0 || isNull($row[0]['yue'])){
			$yue=0;
		}else{
			$yue=$row[0]['yue'];
		}
		$sql="select * from member {$where} order by id desc limit 1";
		$row=$db->page($sql,10,$page);
		rc_require("searchmember.html","member");
	}else if($action==='addMember'){
		rc_require("addmember.html","member");
	}
}else if($mod==='purchase'){//采购管理
	if($action=='searchPur'){
		$where="where comId={$_SESSION['comId']}";
		$where.=(isset($sStorage) && intval($sStorage)>0)?" and storage={$sStorage}":'';
		if(isset($isPrint) && intval($isPrint)==1){
			$where.=" and status=1";
		}else{
			$where.=(isset($sStatus) && is_numeric($sStatus))?" and status={$sStatus}":'';
		}
		$where.=(isset($sName) && !isNull($sName))?" and name='{$sName}'":'';
		$where.=(isset($sClass) && intval($sClass)!==-1)?" and class={$sClass}":'';
		$sql="select * from purchase {$where}";
		$row=$db->page($sql,10,$page);
		if(isset($isPrint) && intval($isPrint)==1){
			rc_require("printpurchase.html","purchase");
		}else{
			rc_require("searchpurchase.html","purchase");
		}
	}else if($action=='searchPurNote'){
		$where=" where comId={$_SESSION['comId']}";
		$where.=(isset($purNo) && !isNull($purNo))?" and noteNO='{$purNo}'":'';
		$where.=(isset($sStatus) && is_numeric($sStatus))?" and status={$sStatus}":'';
		$where.=(isset($start) && preg_match($zz_date,$start))?" and addTime>='{$start}'":'';
		$where.=(isset($end) && preg_match($zz_date,$end))?" and addTime<='{$end}'":'';
		$sql="select * from purnote {$where} order by id desc";
		if(isset($isPrint) && intval($isPrint)==1){
			if(!isset($objId) || isNull($objId)){
				echo"参数不足";
			}else{
				$row=$db->myQuery($sql,false);
				if(is_array($row) && count($row)){
					$row[0]['purId']=preg_replace("/(^\,*)|(\,*)$/","",$row[0]['purId']);
					$sql="select a.id as noteId,a.purId,a.noteNo,a.status,a.purUser,a.buyTime,a.addUser,a.addTime,a.memo,b.id,b.comId,b.storage,b.applyUser,b.needDate,b.class,b.name,b.barcode,b.brand,b.colour,b.wrap,b.spe,b.unit,b.amount from purnote as a inner join purchase as b where a.id={$objId} and b.id in({$row[0]['purId']}) and b.comId={$_SESSION['comId']}";
					$row=$db->myQuery($sql,false);
				}
				rc_require("printpurchase.html","purchase");
			}
		}else{
			$row=$db->page($sql,10,$page);
			rc_require("searchpurnote.html","purchase");
		}
	}else if($action=='addPur'){
		rc_require("addpurchase.html","purchase");
	}
}else if($mod==='storage'){//仓库管理
	if($action=='showStorage'){
		$sql="select * from storage where comId={$_SESSION['comId']}";
		$row=$db->page($sql,10,$page);
		rc_require("showstorage.html","storage");
	}else if($action=="classify"){
		$sql="select * from classify where comId={$_SESSION['comId']}";
		$row=$db->page($sql,10,$page);
		rc_require("classify.html","storage");
	}else if($action=='inStorage'){
		rc_require("instorage.html","storage");
	}else if($action=='searchApplyOut'){
		$where=" where a.comId='{$_SESSION['comId']}'";
		$where.=(isset($sBranch) && intval($sBranch)>-1)?" and a.branch={$sBranch}":'';
		$where.=(isset($sUser) && preg_match($zz_userNick,$sUser))?" and a.applyUser='{$sUser}'":'';
		$where.=(isset($sStatus) && intval($sStatus)>-1)?" and a.status={$sStatus}":'';
		$sql="select a.id as applyId,a.comId,a.stock,a.branch,a.status,a.outed,a.amount as outAmount,a.unit as applyUnit,a.auditUser,a.applyUser as userNick,a.applyTime,a.memo,b.id,b.storage,b.class,b.name,b.asName,b.brand,b.colour,b.amount,b.unit,b.price,b.retail from applyout as a inner join stock as b on a.stock=b.id {$where} order by applyId desc";
		$row=$db->page($sql,10,$page);
		rc_require("applyoutlist.html","storage");
	}else if($action=='searchIn'){
		$where=" where b.comId={$_SESSION['comId']}";
		$where.=(!isset($sStorage) || $sStorage==='-1')?"":" and b.storage={$sStorage}";
		$where.=(!isset($sClass) || $sClass==='-1')?"":" and b.class={$sClass}";
		$where.=(!isset($sProduct) || isNull($sProduct))?"":" and b.name='{$sProduct}'";
		$where.=(!isset($sUser) ||isNull($sUser))?"":" and b.addUser='{$sUser}'";
		$where.=(!isset($sStartDate) || isNull($sStartDate) || !preg_match($zz_date,$sStartDate))?"":" and to_days(a.addTime)>=to_days('{$sStartDate}')";
		$where.=(!isset($sEndDate) || isNull($sEndDate) || !preg_match($zz_date,$sEndDate))?"":" and to_days(a.addTime)>=to_days('{$sEndDate}')";
		$sql="select a.id as inId,a.stock,a.amount,a.unit,a.buyUser,a.buyDate,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 from instorage as a inner join stock as b on a.stock=b.id {$where} order by a.id desc";
		$row=$db->page($sql,10,$page);
		rc_require("searchin.html","storage");
	}else if($action=='searchOut'){
		$where=" where b.comId={$_SESSION['comId']}";
		$where.=(!isset($sStorage) || $sStorage==='-1')?"":" and b.storage={$sStorage}";
		$where.=(!isset($sClass) || $sClass==='-1')?"":" and b.class={$sClass}";
		$where.=(!isset($sProduct) || isNull($sProduct))?"":" and b.name='{$sProduct}'";
		$where.=(!isset($sUser) ||isNull($sUser))?"":" and b.addUser='{$sUser}'";
		$where.=(!isset($sStartDate) || isNull($sStartDate) || !preg_match($zz_date,$sStartDate))?"":" and to_days(a.addTime)>=to_days('{$sStartDate}')";
		$where.=(!isset($sEndDate) || isNull($sEndDate) || !preg_match($zz_date,$sEndDate))?"":" and to_days(a.addTime)>=to_days('{$sEndDate}')";
		$sql="select a.id as outId,a.stock,a.applyOut,a.amount,a.unit,a.apply,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 from outstorage as a inner join stock as b on a.stock=b.id {$where} order by a.id desc";
		$row=$db->page($sql,10,$page);
		rc_require("searchout.html","storage");
	}else if($action=='searchStock'){
		$where=" where comId={$_SESSION['comId']}";
		$where.=(isset($sStorage) && is_numeric($sStorage) && intval($sStorage)>=0)?" and storage={$sStorage}":'';
		$where.=(isset($sClass) && is_numeric($sClass) && intval($sClass)>=0)?" and class={$sClass}":'';
		$where.=(isset($sName) && !isNull($sName))? " and name='{$sName}'":'';
		$where.=(isset($sBrand) && !isNull($sBrand))?" and brand='{$sBrand}'":'';
		$sql="select * from stock {$where}";
		$row=$db->page($sql,10,$page);
		rc_require("showstock.html","storage");
	}else if($action==='searchTuiHuo'){
		$where=" where a.comId={$_SESSION['comId']}";
		$where.=(isset($sStatus) && is_numeric($sStatus))?" and a.status={$sStatus}":'';
		$where.=(isset($sStorage) && is_numeric($sStorage) && intval($sStorage)>=0)?" and a.stock={$sStorage}":'';
		$where.=(isset($sBarcode) && !isNull($sBarcode))?" and a.barcode='{$sBarcode}'":'';
		$where.=(isset($sUser) && preg_match($zz_userNick,$sUser))?" and a.addUser='{$sUser}'":'';
		$where.=(isset($sStartDate) && preg_match($zz_date,$sStartDate))?" and a.addTime<=unix_timestamp('{$sStartDate}')":'';
		$where.=(isset($sEndDate) && preg_match($zz_date,$sEndDate))?" and a.addTime>=unix_timestamp('{$sEndDate}')":'';
		$sql="select a.id as aId,a.comId,a.stock,a.status,a.class,a.barcode,a.amount,a.sjSum,a.unit,a.price,a.meny,a.auditUser,a.addUser,a.addTime,a.ip,a.memo,b.storage,b.id,b.name,b.brand from tuihuo as a inner join stock as b on a.stock=b.id {$where} order by a.id desc";
		$row=$db->page($sql,10,$page);
		rc_require("searchtuihuo.html","storage");
	}else if($action==='auditFanCang'){
		$where=" where a.comId={$_SESSION['comId']}";
		$where.=(isset($sStatus) && is_numeric($sStatus) && intval($sStatus)!==-1)?" and a.status={$sStatus}":'';
		$where.=(isset($sBranch) && is_numeric($sBranch) && intval($sBranch)!==-1)?" and a.branch={$sBranch}":'';
		$where.=(isset($sClass) && is_numeric($sClass) && intval($sClass)!==-1)?" and a.class={$sClass}":'';
		$where.=(isset($sBarcode) && !isNull($sBarcode))?" and b.barcode='{$sBarcode}'":'';
		$sql="select a.*,b.id as bId,b.name,b.barcode,b.brand from fancang as a inner join stock as b on a.stock=b.id {$where} order by a.id desc";
		$row=$db->page($sql,10,$page);
		rc_require("searchfancang.html","storage");
	}else if($action==='searchApplyBaoFei'){
		$where=" where comId={$_SESSION['comId']}";
		$where.=(isset($sStatus) && is_numeric($sStatus) && intval($sStatus)!==-1)?" and status={$sStatus}":'';
		$where.=(isset($sBrand) && is_numeric($sBrand) && intval($sBrand)!==-1)?" and brand={$sBrand}":'';
		$where.=(isset($sClass) && is_numeric($sClass) && intval($sClass)!==-1)?" and class={$sClass}":'';
		$where.=(isset($sBarcode) && !isNull($sBarcode))?" and barcode='{$sBarcode}'":'';
		$where.=(isset($sName) && !isNull($sName))?" and name='{$sName}'":'';
		$where.=(isset($sStorage) && !isNull($sStorage))?" and storage='{$sStorage}'":'';
		$sql="select * from baofei {$where} order by id desc";
		$row=$db->page($sql,10,$page);
		rc_require("searchbaofei.html","storage");
	}
}else if($mod==='sold'){
	if($action==='searchGoods'){
		$where=" where a.comId={$_SESSION['comId']} and a.branch={$_SESSION['branch']}";
		$where.=(isset($sStatus) && is_numeric($sStatus) && intval($sStatus)!==-1)?" and a.status={$sStatus}":'';
		$where.=(isset($sBarcode) && !isNull($sBarcode))?" and b.barcode='{$sBarcode}'":'';
		$where.=(isset($sName) && !isNull($sName))?" and b.name='{$sName}'":'';
		$where.=(isset($sClass) && is_numeric($sClass) && intval($sClass)>0)?" and b.class={$sClass}":'';
		$sql="select a.id,a.comId,a.stock,a.branch,a.status,a.retail,a.promotion,a.amount,a.unit,b.id as sId,b.comId,b.storage,b.class,b.name,b.asName,b.barcode,b.vender,b.brand,b.colour,b.wrap,b.spe from goods as a inner join stock as b on a.stock=b.id {$where} order by a.id desc";
		$row=$db->page($sql,10,$page);
		rc_require("searchgoods.html","sold");
	}else if($action==='searchFanCang'){
		$where=" where a.comId={$_SESSION['comId']}";
		$where.=(isset($sStatus) && is_numeric($sStatus) && intval($sStatus)!==-1)?" and a.status={$sStatus}":'';
		$where.=(isset($sBranch) && is_numeric($sBranch) && intval($sBranch)!==-1)?" and a.branch={$sBranch}":'';
		$where.=(isset($sClass) && is_numeric($sClass) && intval($sClass)!==-1)?" and a.class={$sClass}":'';
		$where.=(isset($sBarcode) && !isNull($sBarcode))?" and b.barcode='{$sBarcode}'":'';
		$sql="select a.*,b.id as bId,b.name,b.barcode,b.brand from fancang as a inner join stock as b on a.stock=b.id {$where} order by a.id desc";
		$row=$db->page($sql,10,$page);
		rc_require("searchfancang.html","sold");
	}else if($action==='searchPromotion'){
		$where=" where a.comId={$_SESSION['comId']}";
		$where.=(isset($sBranch) && is_numeric($sBranch))?" and a.branch={$sBranch}":'';
		$where.=(isset($sBarcode) && !isNull($sBarcode))?" and b.barcode='{$sBarcode}'":'';
		$where.=(isset($sStart) && preg_match($zz_date,$sStart))?" and a.start<=unix_timestamp('{$sStart}')":'';
		$where.=(isset($sEnd) && preg_match($zz_date,$sEnd))?" and a.end>=unix_timestamp('{$sEnd}')":'';
		$sql="select a.id,a.comId,a.goodsId,a.branch,a.stock,a.status,a.member,a.start,a.end,a.addUser,a.addTime,b.unit,b.retail,b.promotion from promotion as a inner join goods as b on a.goodsId=b.id {$where} order by a.id asc";
		$row=$db->page($sql,10,$page);
		rc_require("searchpromotion.html","sold");
	}else if($action==='addPromotion'){
		rc_require("addpromotion.html","sold");
	}else if($action==='execSold'){
		rc_require("execsold.html","sold");
	}else if($action==='saveExecSold'){
		$result='';
		$print=array();//是否是会员0，会员类型1，会员账号2，打折值3，产品名称4，品牌5，产品条码6，产品单价（最终价格）7，数量单位8，购买数量9，
		if(!isset($aBarcode) || !is_array($aBarcode) || count($aBarcode)==0){
			$result="提交无效";
		}else if(floatval($sfje)<0){
			$result="客户实付金额不能为零";
		}else{
			$sfje=floatval($sfje);
			$idList='';
			$sale=10;
			$is_member=0;
			$memberNick='0';
			$gradeName='';
			if(!isNull($member) && preg_match($zz_userNick,$member)){
				$sql="select * from member where comId={$_SESSION['comId']} and account='{$member}' limit 1";
				$memberRow=$db->myQuery($sql,false);
				if($memberRow===false || count($memberRow)==0){
					$sale=10;
				}else{
					$memberNick=$member;
					$grade=getGrade($memberRow[0]['grade']);
					if($grade===false){
						$sale=10;
					}else{
						$sale=floatval($grade['sale']);
						$gradeName=$grade['name'];
						$is_member=1;
					}
				}
			}
			$sumMeny=0;
			$noSave=array();
			$success=array();
			$sql="select * from stock where comId={$_SESSION['comId']} and barcode=:barcode limit 1";
			$stock_exists=$db->conn->prepare($sql);
			$sql="select * from goods where comId={$_SESSION['comId']} and barcode=:barcode and branch={$_SESSION['branch']} limit 1";
			$goods_exists=$db->conn->prepare($sql);
			$sql="select * from promotion where comId={$_SESSION['comId']} and goodsId=:goodsId limit 1";
			$is_promotion=$db->conn->prepare($sql);
			$sql="insert into soldnote(comId,branch,barcode,userNick,goods,member,promotion,amount,unit,retail,price,meny,addUser,ip) values(:comId,:branch,:barcode,:userNick,:goods,:member,:promotion,:amount,:unit,:retail,:price,:meny,:addUser,:ip);";
			$save=$db->conn->prepare($sql);
			$sql="update goods set amount=amount-:soldeSum where id=:goodsId";
			$soldGoods=$db->conn->prepare($sql);//如果记录销售成功，则在售商品数量减
			foreach($aBarcode as $key=>$v){
				$aSum[$key]=floatval($aSum[$key]);
				if(!is_numeric($aSum[$key]) || $aSum[$key]<=0){
					$noSave[]=array($v,$aName[$key],$aBrand[$key],$aSum[$key]);//购买数量不合法
					continue;
				}
				$stock_exists->bindValue(":barcode",$v);
				$stock_exists->execute();
				$stock=$stock_exists->fetchAll(PDO::FETCH_ASSOC);
				if(!is_array($stock) && count($stock)==0){
					$noSave[]=array($v,$aName[$key],$aBrand[$key],$aSum[$key]);//如果仓库中没有此商品的条码信息，则不允许保存销售
					continue;
				}else{
					$stock=$stock[0];
					$goods_exists->bindValue(":barcode",$v);
					$goods_exists->execute();
					$goods=$goods_exists->fetchAll(PDO::FETCH_ASSOC);
					if(!is_array($goods) || count($goods)==0){
						$noSave[]=array($v,$aName[$key],$aBrand[$key],$aSum[$key]);
					}else{
						$goods=$goods[0];
						$price=$goods['retail'];
						$is_promotion->bindValue(":goodsId",$goods['id']);
						$is_promotion->execute();
						$promotion=$is_promotion->fetchAll(PDO::FETCH_ASSOC);
						if(is_array($promotion) && count($promotion)){//做活动
							$price=floatval($goods['promotion']);
							if(intval($promotion[0]['member'])==1 && $is_member===1){
								$price=$price*$sale/10;
							}
							$meny=round($price*$aSum[$key],1);
						}else{//未做活动
							if($is_member===1){
								$price=$price*$sale/10;
							}
							$meny=round($price*$aSum[$key],1);
						}
						$print[]=array($is_member,$gradeName,$member,$sale,$stock['name'],$stock['brand'],$stock['barcode'],$price,$goods['unit'],$aSum[$key]);
						$sumMeny+=$meny;
						if(floatval($goods['amount'])>=$aSum[$key]){
							try{
								$save->bindValue(":comId",$_SESSION['comId']);
								$save->bindValue(":branch",$_SESSION['branch']);
								$save->bindValue(":barcode",$v);
								$save->bindValue(":userNick",$_SESSION['userNick']);
								$save->bindValue(":goods",$goods['id']);
								$save->bindValue(":member",$memberNick);
								$save->bindValue(":promotion",(is_array($promotion) && count($promotion))?1:0);
								$save->bindValue(":amount",$aSum[$key]);
								$save->bindValue(":unit",$goods['unit']);
								$save->bindValue(":retail",$goods['retail']);
								$save->bindValue(":price",$price);
								$save->bindValue(":meny",$meny);
								$save->bindValue(":addUser",$_SESSION['userNick']);
								$save->bindValue(":ip",$cg['ip']);
								$save->execute();
								$id=$db->conn->lastInsertId();
								$idList.="{$id},";
								if(is_numeric($id) && intval($id)>0){
									try{
										$soldGoods->bindValue(":soldeSum",$aSum[$key]);
										$soldGoods->bindValue(":goodsId",$goods['id']);
										$soldGoods->execute();
									}catch(PDOException $e){
										$sql="delete from soldnote where id={$id}";
										$db->execSql($sql,false);
										$noSave[]=array($v,$aName[$key],$aBrand[$key],$aSum[$key]);
										die($e->getMessage());
									}
								}
							}catch(PDOException $e){
								$noSave[]=array($v,$aName[$key],$aBrand[$key],$aPrice[$key],$aSum[$key]);
								die($e->getMessage());
							}
						}else{
							$noSave[]=array($v,$aName[$key],$aBrand[$key],$aPrice[$key],$aSum[$key]);
						}
					}
				}
			}
			if($sumMeny!==0 && $is_member===1){
				try{
					$monetary=floatval($memberRow[0]['monetary'])+$sumMeny;
					$allGrade=getGrade(0);
					$g=0;
					if(is_array($allGrade)){
						foreach($allGrade as $key=>$v){
							if(floatval($v['start'])<=$monetary && floatval($v['end'])>=$monetary){
								$g=$v['id'];
								break;
							}
						}
					}
					if($g!==0){
						$sql="update member set grade={$g},monetary=monetary+{sumMeny} where comId={$_SESSION['comId']} and id={$memberRow[0]['id']}";
					}else{
						$sql="update member set monetary=monetary+{$sumMeny} where comId={$_SESSION['comId']} and id={$memberRow[0]['id']}";
					}
					$db->execSql($sql,false);
				}catch(PDOException $e){
					echo $e->getMessage();
				}
			}
			if($sumMeny!==0){
				if($sfje<$sumMeny && !isNull($idList)){
					$idList=preg_replace($zz_trim,'',$idList);
					$sql="update soldnote set status=0 where id in({$idList});";
					$db->execSql($sql,false);
					writeOperation(3,"销售人员输入的客户实付金额不足，但销售人员违规（躲过前台合法验证）提交了客户购买单，消费金额：{$sumMeny}元人民币，实际支付金额：{$sfje}元人民币，ID为：{$idList}");
				}
				rc_require("printsold.html","sold");
			}
		}
		echo $result;
	}
}else if($mod==='financial'){
	if($action==='errorSearchSold'){
		$where=" where comId={$_SESSION['comId']} and operType=3";
		$where.=(isset($sUserNick) && preg_match($zz_userNick,$sUserNick))?" and userNick='{$sUserNick}'":'';
		$where.=(isset($start) && preg_match($zz_date,$start))?" and addTime>=UNIX_TIMESTAMP('{$start}')":'';
		$where.=(isset($end) && preg_match($zz_date,$end))?" and addTime<=UNIX_TIMESTAMP('{$end}')":'';
		$sql="select * from operation {$where} order by id desc";
		$row=$db->page($sql,10,$page);
		rc_require("errorsearchsold.html","financial");
	}else if($action==='searchSold'){
		$where=" where comId={$_SESSION['comId']}";
		$where.=(isset($sUserNick) && preg_match($zz_userNick))?" and userNick='{$sUserNick}'":'';
		$where.=(isset($sBranch) && intval($sBranch)!=-1)?" and branch={$sBranch}":'';
		$where.=(isset($sBarcode) && !isNull($sBarcode))?" and barcode='{$sBarcode}'":'';
		$where.=(isset($start) && preg_match($zz_date,$start))?" and addTime<=UNIX_TIMESTAMP('{$start}')":'';
		$where.=(isset($end) && preg_match($zz_date,$end))?" and addTime>=UNIX_TIMESTAMP('{$end}')":'';
		$sql="select * from soldnote {$where} order by id desc";
		$row=$db->page($sql,17,$page);
		rc_require("searchsold.html","financial");
	}else if($action==='jiduForm'){
		rc_require("jiduform.html","financial");
	}else if($action==='caigouForm'){
		$where=" where a.comId={$_SESSION['comId']}";
		$where.=(isset($sStorage) && intval($sStorage)>0)?" and b.storage={$sStorage}":'';
		$where.=(isset($sBuyUser) && preg_match($zz_userNick,$sBuyUser))?" and a.buyUser='{$sBuyUser}'":'';
		$where.=(isset($sBarcode) && !isNull($sBarcode))?" and b.barcode='{$sBarcode}'":'';
		$where.=(isset($start) && preg_match($zz_date,$start))?" and a.addTime<=UNIX_TIMESTAMP('{$start}')":'';
		$where.=(isset($end) && preg_match($zz_date,$end))?" and a.addTime>=UNIX_TIMESTAMP('{$end}')":'';
		$sql="select a.id,a.comId,a.stock,a.amount,a.unit,a.meny,a.buyUser,a.buyDate,a.addUser,a.addTime,a.ip,b.storage,b.barcode,b.name,b.id as stockId from instorage as a inner join stock as b on a.stock=b.stockId {$where} order by a.id desc";
		$row=$db->myQuery($sql,false);
		rc_require("caigouform.html","financial");
	}else if($action==='baofeiForm'){
		$where=" where comId={$_SESSION['comId']} and status=1";
		$where.=(isset($sBrand) && is_numeric($sBrand) && intval($sBrand)!==-1)?" and brand={$sBrand}":'';
		$where.=(isset($sClass) && is_numeric($sClass) && intval($sClass)!==-1)?" and class={$sClass}":'';
		$where.=(isset($sBarcode) && !isNull($sBarcode))?" and barcode='{$sBarcode}'":'';
		$where.=(isset($sName) && !isNull($sName))?" and name='{$sName}'":'';
		$where.=(isset($start) && preg_match($zz_date,$start))?" and unix_timestamp('{$start}')>=addTime":'';
		$where.=(isset($start) && preg_match($zz_date,$start))?" and unix_timestamp('{$end}')<=addTime":'';
		rc_require("baofeiform.html","financial");
	}
}
rc_require("footer.html");
?>