wugg 1 year ago
parent
commit
765b295ebe

+ 36 - 10
app/admin/controller/CombindStock.php

@@ -20,7 +20,17 @@ class CombindStock extends Base
 	}
  
 	public function list(){
-	
+		$param=$this->request->only(['spuCode'=>'','wsm_code'=>'','good_name'=>'','cat_id'=>'','status'=>'','flag'=>'',
+		"create_start"=>"2023-01-01 00:00:00","create_end"=>date("Y-m-d H:i:s")],'post','trim');
+		$where=[["createtime","between",[$param['create_start'],$param['create_end']]]];
+		$param['spuCode']?? $where[]=["spuCode","like","%{$param['spuCode']}%"];
+		$param['wsm_code']?? $where[]=["wsm_code","like","%{$param['wsm_code']}%"];
+		$param['good_name']?? $where[]=["good_name","like","%{$param['good_name']}%"];
+		$param['cat_id']?? $where[]=["good_basic.cat_id","=",$param['cat_id']];
+		$list = $this->combindStock->withJoin(["wsminfo","good"],"left")->order("id desc")->paginate
+		(["page"=>$param['page'],"list_rows"=>$param['size']]);
+		return app_show(0,"获取成功",["list"=>$list->items(),"count"=>$list->total()]);
+		
 	}
 	/**@param spuCode 商品编号
 	 **@param wsm_code 仓库编号
@@ -121,10 +131,9 @@ class CombindStock extends Base
 			$up =$row->save();
 			if($up==false) throw new \Exception("数据状态更新失败");
 			if($param['status']==2){
-				
 				$GoodStock =new GoodStock();
 				$stock =$GoodStock->where(["spuCode"=>$row->spuCode,"wsm_code"=>$row->wsm_code])->findOrEmpty();
-				if($param['flag']==1){
+				if($row->flag==1){
 					$bn = makeNo('BN');
 					if($stock->isEmpty()){
 						$wsm_stock = [
@@ -162,17 +171,34 @@ class CombindStock extends Base
 		            $stockCodeLog= (new 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库存生成失败');
-					(new CombindBninfo())->AaddBNLog($row->spuCode,$bn,$stockCodeLog);
+					$bnlist =$this->combind->AddStock($row->spuCode,$row->stock_num);
+					(new CombindBninfo())->AaddBNLog($row->spuCode,$bn,$bnlist);
 				}else{
-				
+					if($stock->usable_stock < $row->stock_num)  throw new \Exception('仓库可用库数不足');
+					$up =GoodStockInfo::SaleBn($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']);
+							if($bnlist==false)throw new \Exception('子商品BN库存信息更新失败');
+					}
 				}
 			}
 			if($param['status']==3){
-				if($param['flag']==1){
-				
-				}else{
-				
-				}
+				$good= (new \app\admin\model\Good())->where(['spuCode'=>$row->spuCode])->findOrEmpty();
+					if($good->isEmpty()==false){
+						if($row->flag==0){
+							$good->usable_stock = bcadd($good->usable_stock ,$row->stock_num);
+						$stockup = $good->save();
+						if($stockup==false) throw new \Exception('组合商品库存解除驳回失败');
+						}else{
+							foreach ($comblist as $item){
+								$num = bcmul($item['child_num'],$row->stock_num);
+								$stockup = Db::name('good')->where('spuCode',$item['childCode'])->inc('usable_stock',intval
+								($num))->update();
+								if($stockup==false) throw new \Exception('组合商品的子商品信息库存占用驳回失败');
+							}
+						}
+					}
 			}
 			$row->commit();
 		}catch (\Exception $exception){

+ 28 - 0
app/admin/model/CombindBninfo.php

@@ -21,6 +21,7 @@ class CombindBninfo extends Model
                 $tmp["spuCode"]=$spuCode;
                 $tmp["child_bnCode"]=$item['bnCode'];
                 $tmp["stock_id"]=$item['stock_id'];
+                $tmp["childCode"]=$item['spuCode'];
                 $tmp["childBnNum"]=$item['num'];
                 $tmp["origin_price"]=$item['origin_price'];
                 $list[]=$tmp;
@@ -28,4 +29,31 @@ class CombindBninfo extends Model
         
         $this->saveAll($list);
     }
+    /**
+	* @param string $spuCode
+	* @param string $bnCode
+	* @param string $num
+	*/
+    public function  subBN($spuCode='',$bnCode='',$stock_id='',$num=''){
+        $stockinfo = $this->where(["spuCode"=>$spuCode,"bnCode"=>$bnCode,"stock_id"=>$stock_id])->where("childBnNum",">",0)->order("id desc")
+        ->select()->toArray();
+        if(empty($stockinfo)) throw new \Exception("未找到关联得bn库存数据");
+        foreach ($stockinfo as $item){
+        	$tempnum=0;
+        	if($num > $item['childBnNum']){
+        		$tempnum = $item['childBnNum'];
+        		$num= $num -$item['childBnNum'];
+        		$item['childBnNum']=0;
+        	}else{
+        		
+        		$tempnum = $num;
+        		$item['childBnNum']=$item['childBnNum'] -$num;
+        		$num =0;
+        	}
+        	$this->save($item);
+        	$up=GoodStockInfo::AddBn($item['stock_id'],$item['child_bnCode'],$tempnum,$item['origin_price']);
+        	if($up==false) throw new \Exception('bn库存数据更新失败');
+        }
+       return true;
+    }
 }

+ 29 - 3
app/admin/model/CombindStock.php

@@ -3,7 +3,7 @@ declare (strict_types = 1);
 
 namespace app\admin\model;
 
-use think\Model;
+use think\facade\Db;use think\Model;
 
 /**
  * @mixin \think\Model
@@ -13,9 +13,11 @@ class CombindStock extends Model
    protected $createTime='createtime';
    protected $updateTime='updatetime';
    
-   protected $hidden=["wsminfo"];
+   protected $hidden=["wsminfo","good"];
+   
+   protected $append=["specInfo","catInfo"];
    public function good(){
-   	return $this->belongsTo(Good::class,"spuCode","spuCode");
+   	    return $this->belongsTo(GoodBasic::class,"spuCode","spuCode")->bind(["cat_id"]);
    }
    
    public function WsmInfo(){
@@ -25,4 +27,28 @@ class CombindStock extends Model
    public function CombindGood(){
    		return $this->hasMany(GoodCombind::class,'spuCode','spuCode');
    }
+   
+   public function  getSpecInfoAttr($v,$row){
+         $spec = Db::name('good_spec')->where(['spuCode'=>$row['spuCode'],'is_del'=>0])->select()->toArray();
+          $speclist=[];
+         if(!empty($spec)){
+            foreach ($spec as $value){
+                $temp=[];
+                $temp['id']=$value['id'];
+                $temp['spuCode']=$value['spuCode'];
+                $temp['spec_id']=$value['spec_id'];
+                $temp['spec_value_id']=$value['spec_value_id'];
+                $temp['is_del']=$value['is_del'];
+                $sp = Db::name('specs')->where(['id'=>$value['spec_id']])->find();
+                $temp['spec_name']=isset($sp['spec_name']) ? $sp['spec_name']:'';
+                $spv = Db::name('spec_value')->where(['id'=>$value['spec_value_id']])->find();
+                $temp['spec_value']=isset($spv['spec_value']) ? $spv['spec_value']:'';
+                $speclist[]=$temp;
+            }
+        }
+         return  $speclist;
+   }
+   public function  getCatInfoAttr($v,$row){
+        return made($row['cat_id']);
+   }
 }

+ 2 - 0
app/admin/model/CombindStockInfo.php

@@ -11,4 +11,6 @@ use think\Model;
 class CombindStockInfo extends Model
 {
  protected $createTime="addtime";
+ 
+
 }

+ 30 - 0
app/admin/model/GoodCombind.php

@@ -37,4 +37,34 @@ class GoodCombind extends Model
 		
 		return $returnBn;
 	}
+	
+	public function subStock($spuCode='',$bnCode='',$num='0'){
+		$comblist = $this->where(['spuCode'=>$spuCode,'is_del'=>0])->select()->toArray();
+		if(empty($comblist))throw new \Exception('未找到对应子商品信息');
+				$GoodStock=new GoodStock();
+				$goodinfo = new Good();
+				$combind = new CombindBninfo();
+		foreach ($comblist as $item){
+			 $row =$GoodStock->withJoin(['wsminfo'],'left')
+            ->field('id,usable_stock')
+            ->where(['spuCode' => $item['childCode'], 'warehouse_info.wsm_type'=>[2,5]])
+            ->findOrEmpty();
+			 if($row->isEmpty()) throw new \Exception('未找到对应子商品信息');
+			 $stocknum =bcmul($num,$item['child_num']);
+			 $good = $goodinfo->where(["spuCode"=>$item['childCode']])->findOrEmpty();
+			 if($good->isEmpty()==false){
+			 	$good->usable_stock = bcadd($good->usable_stock,$stocknum);
+			 	$gdup=$good->save();
+			 	if($gdup==false)throw new \Exception('子商品库存更新失败');
+			 }
+			 $up=$combind->subBN($spuCode,$bnCode,$row->id,$stocknum);
+			 if($up){
+			 	$row->usable_stock =  bcadd($row->usable_stock,$stocknum);
+			 	$row->total_stock =  bcadd($row->total_stock,$stocknum);
+			 	$stocku=$row->save();
+			 	if($stocku==false)throw new \Exception('子商品库存更新失败');
+			 }else throw new \Exception('子商品库存更新失败');
+		}
+		return true;
+	}
 }

+ 3 - 3
app/admin/model/GoodStockInfo.php

@@ -60,7 +60,7 @@ class GoodStockInfo extends Model
 		if(empty($arr)) throw new Exception("库存批次数量不足",1006);
 		$list=[];
 		foreach ($arr as $item) {
-			$stock = self::name("good_stock")->where(["id"=>$item['stockid']])->field("id,usable_stock,wait_out_stock")->find()->toArray();
+			$stock = self::name("good_stock")->where(["id"=>$item['stockid']])->field("id,usable_stock,wait_out_stock,spuCode")->find()->toArray();
 			if($num<=0) break;
 			$update=[];
 		    if($item['balance_num']<=$num){
@@ -68,13 +68,13 @@ class GoodStockInfo extends Model
 		    	$update['used_num']= $item['used_num']+$item['balance_num'];
 		    	$num-=$item['balance_num'];
 		    	$update['updatetime'] =date("Y-m-d H:i:s");
-		    	$temp=["stockid"=>$item['stockid'],"bnCode"=>$item['bnCode'],"num"=>$item['balance_num'],"origin_price"=>$item["origin_price"]];
+		    	$temp=["stockid"=>$item['stockid'],"bnCode"=>$item['bnCode'],"spuCode"=>$stock['spuCode'],"num"=>$item['balance_num'],"origin_price"=>$item["origin_price"]];
 		    	$stockup=["usable_stock"=>$stock['usable_stock']-$item['balance_num'],"wait_out_stock"=>$stock['wait_out_stock']+$item['balance_num'],"updatetime"=>date("Y-m-d H:i:s")];
 		    }else{
 				$update['balance_num']=$item['balance_num']-$num;
 		    	$update['used_num']=$item['used_num']+$num;
 		    	$update['updatetime'] =date("Y-m-d H:i:s");
-		    	$temp=["stockid"=>$item['stockid'],"bnCode"=>$item['bnCode'],"num"=>$num,"origin_price"=>$item["origin_price"]];
+		    	$temp=["stockid"=>$item['stockid'],"bnCode"=>$item['bnCode'],"spuCode"=>$stock['spuCode'],"num"=>$num,"origin_price"=>$item["origin_price"]];
 		    	$stockup=["usable_stock"=>$stock['usable_stock']-$num,"wait_out_stock"=>$stock['wait_out_stock']+$num,"updatetime"=>date("Y-m-d H:i:s")];
 				$num=0;
 		    }