wugg 1 ano atrás
pai
commit
4a6ea1f59c

+ 12 - 9
app/admin/controller/CombindStock.php

@@ -62,7 +62,8 @@ class CombindStock extends Base
 				if($isT->usable_stock<  $param['stock_num'])return error_show(1004,'商品库存数不足');
 			}
 		}
-		
+		$isTs = $this->combindStock->where(["spuCode"=>$param['spuCode'],"status"=>1])->findOrEmpty();
+		if($isTs->isEmpty()==false) return error_show(1004,"组合商品库存变化存在未完成得流程{$isTs->stockCode}");
 		$combindList = $this->combind->where(["spuCode"=>$param["spuCode"],"is_del"=>0])->select()->toArray();
 		if(empty($combindList)) return error_show(1004,"未找到组合商品的商品组合信息");
 		$childarr= Db::name('good')->whereIn('spuCode',array_unique(array_column($combindList,'childCode')))->column("spuCode,usable_stock,is_combind","spuCode");
@@ -86,7 +87,7 @@ class CombindStock extends Base
 			if($up==false) throw new \Exception("创建失败");
 			if($param["flag"]==0){
 				if($isT->isEmpty()==false){
-					$stockup = Db::name('good')->where('spuCode',$param['spuCode'])->dec('usable_stock',$param["stock_num"])->update();
+					$stockup = Db::name('good')->where('spuCode',$param['spuCode'])->dec('usable_stock',intval($param['stock_num']))->update();
 					if($stockup==false) throw new \Exception('组合商品库存占用失败');
 				}
 				$stock->usable_stock = bcsub($stock->usable_stock,$param['stock_num']);
@@ -107,7 +108,7 @@ class CombindStock extends Base
 					$bnArr=array_merge($bnArr,$bnlist);
 				}
 				if(empty($bnArr)) throw new \Exception('组合商品的子商品信息库存占用失败');
-				(new CombindBninfo())->AaddBNLog($param['spuCode'],$data['bnCode'],$bnArr);
+				(new CombindBninfo())->AaddBNLog($param['spuCode'],$data['bnCode'],$bnArr,$stockCode);
 			}
 			$this->combindStock->commit();
 		}catch (\Exception $exception){
@@ -183,7 +184,7 @@ class CombindStock extends Base
 		            $origin_price = $origin['nake_total'];
 		            $stcoc=GoodStockInfo::AddBn(intval($id),$bn,intval($row->stock_num),floatval($origin_price) );
 		            if($stcoc==false)  throw new \Exception( '组合商品bn库存生成失败');
-		            $stockCodeLog= (new CombindStockInfo())->save(["stockCode"=>$row->stockCode,"flag"=>1,
+		            $stockCodeLog= $this->combindStockInfo->save(["stockCode"=>$row->stockCode,"flag"=>1,
 		            "stock_num"=>$row->stock_num,"bnCode"=>$bn,"origin_price"=>$origin_price]);
 					if($stockCodeLog==false)throw new \Exception( '组合商品bn库存生成失败');
 				}
@@ -191,8 +192,11 @@ class CombindStock extends Base
 					$up =GoodStockInfo::StockBnSub($stock->id,intval($row->stock_num));
 			        if(empty($up)) throw new \Exception('未找到对应子商品BN库存信息');
 					foreach ($up as $item){
-						$bnlist =$this->combind->subStock($row->spuCode,$item['bnCode'],$item['num']);
+						$bnlist =$this->combind->subStock($row->spuCode,$item['bnCode'],$item['num'],$row->stockCode);
 						if($bnlist==false)throw new \Exception('子商品BN库存信息更新失败');
+						$stockLog=$this->combindStockInfo->save(["stockCode"=>$row->stockCode,"flag"=>$row->flag,
+						'stock_num'=>$item['num'],'bnCode'=>$item['bnCode'],'origin_price'=>$item['origin_price']]);
+						if($stockLog==false)throw new \Exception( '组合商品bn库存修改失败');
 					}
 				}
 			}
@@ -208,10 +212,9 @@ class CombindStock extends Base
 							$reld=$stock->save();
 							if($reld==false) throw new \Exception('组合商品库存解除驳回失败');
 						}else{
-							
-								$bnlist =$this->combind->subStock($row->spuCode,$row->bnCode,$row->stock_num);
-								if($bnlist==false)throw new \Exception('子商品BN库存信息更新失败');
-							
+							$bnlist =$this->combind->subStock($row->spuCode,$row->bnCode,$row->stock_num,
+							$row->stockCode,1);
+							if($bnlist==false)throw new \Exception('子商品BN库存信息更新失败');
 						}
 					}
 			

+ 41 - 7
app/admin/model/CombindBninfo.php

@@ -13,32 +13,67 @@ class CombindBninfo extends Model
     protected $createTime="addtime";
 	protected $updateTime='updatetime';
     
-    public function AaddBNLog($spuCode='',$bnCode='',$bnArr=[]){
+    public function AaddBNLog($spuCode='',$bnCode='',$bnArr=[],$stockCode=''){
         if(empty($bnArr)) return ;
         $list=[];
         foreach ($bnArr as $item){
                 $tmp=[];
                 $tmp["bnCode"]=$bnCode;
+                $tmp["stockCode"]=$stockCode;
+                $tmp["flag"]=1;
                 $tmp["spuCode"]=$spuCode;
                 $tmp["child_bnCode"]=$item['bnCode'];
                 $tmp["stock_id"]=$item['stockid'];
                 $tmp["childCode"]=$item['spuCode'];
                 $tmp["childBnNum"]=$item['num'];
+                $tmp["bnNum"]=$item['num'];
                 $tmp["origin_price"]=$item['origin_price'];
                 $list[]=$tmp;
         }
         
         $this->saveAll($list);
     }
-    /**
+    /**解除库存
 	* @param string $spuCode
 	* @param string $bnCode
 	* @param string $num
 	*/
-    public function  subBN($spuCode='',$bnCode='',$childCode='',$num=''){
-        $stockinfo = $this->where(["spuCode"=>$spuCode,"bnCode"=>$bnCode,"childCode"=>$childCode])->where("childBnNum",">",0)->order("id desc")
+    public function  subBN($spuCode='',$bnCode='',$childCode='',$num='',$stockCode=''){
+        $stockinfo = $this->where(["spuCode"=>$spuCode,"bnCode"=>$bnCode,"childCode"=>$childCode,"flag"=>1])->where("childBnNum",">",0)->order("id desc")
         ->select()->toArray();
         if(empty($stockinfo)) throw new \Exception("未找到关联得bn库存数据");
+        foreach ($stockinfo as $item){
+        	$tempnum=0;
+        	if($num<=0) break;
+        	if($num > $item['childBnNum']){
+        		$tempnum = $item['childBnNum'];
+        		$num= $num -$item['childBnNum'];
+        	}else{
+        		$tempnum = $num;
+        		$item['childBnNum']=$num;
+        		$num =0;
+        	}
+        	unset($item['id']);
+        	$item['stockCode']=$stockCode;
+        	$item['flag']=0;
+        	$this->save($item);
+        	$up=GoodStockInfo::AddBn(intval($item['stock_id']),$item['child_bnCode'],intval($tempnum),floatval($item['origin_price']));
+        	if($up==false) throw new \Exception('bn库存数据更新失败');
+        	$save=  (new GoodStock())->where(["id"=>$item['stock_id']])->inc("usable_stock",intval($tempnum))->inc("total_stock",intval($tempnum))->update();
+        	if($save==false) throw new \Exception('bn库存数据更新失败');
+        }
+       return true;
+    }
+    
+    /**解除库存
+	* @param string $spuCode
+	* @param string $bnCode
+	* @param string $num
+	*/
+    public function  subBNAdd($spuCode='',$bnCode='',$childCode='',$num='',$stockCode=''){
+        $stockinfo = $this->where(['spuCode'=>$spuCode,'bnCode'=>$bnCode,'childCode'=>$childCode,'flag'=>1])->where('childBnNum','>',0)->order('id desc')
+        ->select()->toArray();
+        if(empty($stockinfo)) throw new \Exception('未找到关联得bn库存数据');
         foreach ($stockinfo as $item){
         	$tempnum=0;
         	if($num<=0) break;
@@ -47,15 +82,14 @@ class CombindBninfo extends Model
         		$num= $num -$item['childBnNum'];
         		$item['childBnNum']=0;
         	}else{
-        		
         		$tempnum = $num;
-        		$item['childBnNum']=$item['childBnNum'] -$num;
+        		$item['childBnNum']=$item['childBnNum']-$num;
         		$num =0;
         	}
         	self::update($item);
         	$up=GoodStockInfo::AddBn(intval($item['stock_id']),$item['child_bnCode'],intval($tempnum),floatval($item['origin_price']));
         	if($up==false) throw new \Exception('bn库存数据更新失败');
-        	$save=  (new GoodStock())->where(["id"=>$item['stock_id']])->inc("usable_stock",intval($tempnum))->inc("total_stock",intval($tempnum))->update();
+        	$save=  (new GoodStock())->where(['id'=>$item['stock_id']])->inc('usable_stock',intval($tempnum))->inc('total_stock',intval($tempnum))->update();
         	if($save==false) throw new \Exception('bn库存数据更新失败');
         }
        return true;

+ 14 - 3
app/admin/model/GoodCombind.php

@@ -39,8 +39,18 @@ class GoodCombind extends Model
 		
 		return $returnBn;
 	}
-	
-	public function subStock($spuCode='',$bnCode='',$num='0'){
+	/**
+	* @param string $spuCode
+	* @param string $bnCode
+	* @param string $num
+	* @param string $stockCode
+	* @param int $flag
+	 * @return bool
+	* @throws \think\db\exception\DataNotFoundException
+	* @throws \think\db\exception\DbException
+	* @throws \think\db\exception\ModelNotFoundException
+	 */
+	public function subStock($spuCode='',$bnCode='',$num='0',$stockCode='',$flag=0){
 		$comblist = $this->where(['spuCode'=>$spuCode,'is_del'=>0])->select()->toArray();
 		if(empty($comblist))throw new \Exception('未找到对应子商品信息');
 				$goodinfo = new Good();
@@ -53,7 +63,8 @@ class GoodCombind extends Model
 			 	$gdup=$good->save();
 			 	if($gdup==false)throw new \Exception('子商品库存更新失败');
 			 }
-			 $up=$combind->subBN($spuCode,$bnCode,$item['childCode'],$stocknum);
+			 if($flag==0)$up=$combind->subBN($spuCode,$bnCode,$item['childCode'],$stocknum,$stockCode);
+			 else   $up=$combind->subBNAdd($spuCode,$bnCode,$item['childCode'],$stocknum,$stockCode);
 			if($up==false)throw new \Exception('子商品库存更新失败');
 		}
 		return true;