'bigint',// 'product_id' =>'bigint',//商品id 'residue_stock' =>'bigint',//剩余库存 'pending_stock' =>'bigint',//待出库存 'total_stock' =>'bigint',//总库存 'create_time' =>'datetime',// 'update_time' =>'datetime',// ]; protected $createTime = 'create_time'; protected $updateTime = 'update_time'; protected $append= ['combind_stock']; public function Product(){ return $this->belongsTo(FinancialProducts::class,'product_id','id')->bind(['goodName','skuCode']); } public function getCombindStockAttr($value,$data){ $isCombind=FinancialProducts::where('id',$data['product_id'])->value('is_combind'); if($isCombind==1){ $combind_stock=ProductsCombind::with(['ProductStock'=>['ProductStock']])->where('parent_id',$data['product_id'])->select(); $num=0; $combind_stock->each(function ($item,$key) use (&$num){ $child_num = $item->child_num; $Total_stock = array_sum(array_column($item->ProductStock->toArray(),'residue_stock')); $Parent_stock = bcdiv($Total_stock,$child_num,8); if($key==0) $num=$Parent_stock; elseif($Parent_stock<$num) $num=$Parent_stock; }); return $num; }else{ return 0; } } public static function AddStock($product_id,$num){ $product=self::where("product_id",$product_id)->findOrEmpty(); if(!$product->isEmpty()){ $product->residue_stock=bcadd($product->residue_stock,$num,8); $product->total_stock=bcadd($product->total_stock,$num,8); $save=$product->save(); if(!$save) throw new \Exception('库存添加失败'); }else{ $product_data=[ 'product_id'=>$product_id, 'residue_stock'=>$num, 'total_stock'=>$num, ]; $creste= self::create($product_data); if($creste->isEmpty()) throw new \Exception('库存添加失败'); } return true; } // 库存减少 public static function SubSingleStock($model,$num){ $info = self::where('product_id',$model->id)->findOrEmpty(); $info->residue_stock=bcsub($model->residue_stock,$num,8); $info->pending_stock=bcsub($model->pending_stock,$num,8); $save=$info->save(); if(!$save) throw new \Exception('库存修改失败'); return ['product_id'=>$model->id,'type'=>2,'num'=>$num,'unit_price'=>$model['unit_price'],'rate'=>$model['cat_tax'],'subunit_price'=>$model['subunit_price']]; } // 库存增加 public static function AddSingleStock($model,$num){ $info = self::where('product_id',$model->id)->findOrEmpty(); $info->product_id=$model->id; $info->residue_stock=bcadd($info->residue_stock??"0",$num,8); $info->total_stock=bcadd($info->total_stock??"0",$num,8); $save=$info->save(); if(!$save) throw new \Exception('库存修改失败'); return ['product_id'=>$model->id,'type'=>1,'num'=>$num,'unit_price'=>$model['unit_price'],'rate'=>$model['cat_tax'],'subunit_price'=>$model['subunit_price']]; } public static function SubCombindStock($product_id,$num,$type=1){ $list = ProductsCombind::where('parent_id',$product_id)->select(); if($list->isEmpty()) throw new \Exception('组合商品不存在'); $productID = []; foreach ($list as $k=>$v){ $where = $type==1? ['skuCode'=>$v['skuCode'],"basic_status"=>1] : ['skuCode'=>$v['skuCode']]; $stock=FinancialProducts::with(['ProductStock'])->where($where)->select(); if($stock->isEmpty()) throw new \Exception('组合商品子商品不存在'); $child_num = bcmul( $v['child_num'],$num,8); $total= count($stock); $stock->each(function ($item,$key) use (&$productID,&$child_num,$total,$type){ $residue_stock = $item->ProductStock->residue_stock; if($residue_stock > $child_num || ($type==2 && $total==$key+1)) { $sub_stock = $child_num; $child_num = "0"; }else{ $child_num = bcsub($child_num,$residue_stock,8); $sub_stock = $residue_stock; } $productID[] = ['product_id' => $item->id, 'type'=>2,'num'=> $sub_stock,'unit_price'=>$item['unit_price'],'rate'=>$item['cat_tax'],'subunit_price'=>$item['subunit_price']]; if($child_num == 0) return false; }); if($child_num != 0 && $type==1 ) throw new \Exception('组合商品子'.$v['skuCode'].'商品库存不足') ; } return $productID; } public static function AddCombindStock($product_id,$num){ $list = ProductsCombind::where('parent_id',$product_id)->select(); if($list->isEmpty()) throw new \Exception('组合商品不存在'); $productID = []; foreach ($list as $k=>$v) { $child=FinancialProducts::findOrEmpty($v['child_id']); if($child->isEmpty()) throw new \Exception('组合商品子'.$v['skuCode'].'商品不存在'); $child_num = bcmul( $v['child_num'],$num,8); $productID[]=self::AddSingleStock($child,$child_num); } return $productID; } /** * @param $product_id 商品id * @param $num 待出库数量 * @param $type 1 正常出库 2 强制出库 * @return true * @throws \Exception */ public static function OutStock($product_id,$num,$type=1){ $product=self::where("product_id",$product_id)->findOrEmpty(); if(!$product->isEmpty()){ if($product->residue_stock<$num && $type==1) throw new \Exception('库存不足'); $product->residue_stock=bcsub($product->residue_stock,$num,8); $product->pending_stock=bcadd($product->pending_stock,$num,8); $save=$product->save(); if(!$save) throw new \Exception('库存修改失败'); }else{ throw new \Exception('商品库存未找到'.$product_id); } return true; } }