123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\model;
- use think\Model;
- /**
- * @mixin \think\Model
- */
- class GoodStock extends Model
- {
- protected $autoWriteTimestamp = true;
- protected $createTime="addtime";
- protected $updateTime="updatetime";
-
- public function WsmInfo(){
- return $this->belongsTo(WarehouseInfo::class,"wsm_code","wsm_code")->bind(["wsm_name"=>"name","wsm_supplierNo"=>"supplierNo","wsm_supplierName"=>"supplierName"]);
- }
-
- //组合商品占用 库存
- public function stockSub($spuCode,$num){
- $row =$this->withJoin(['wsminfo'],'left')
- ->field('good_stock.id,usable_stock')
- ->where(['spuCode' => $spuCode, 'wsm_type'=>[2,5]])
- ->where("usable_stock",">",0)
- ->select();
- if($row->isEmpty()) throw new \Exception('未找到对应子商品库存信息');
- $usable = array_sum(array_column($row->toArray(),"usable_stock"));
- if($usable < $num)throw new \Exception('对应子商品库存数不足');
- $returen=[];
- foreach ($row as $item){
- if($num<=0) break;
- if($item->usable_stock >= $num){
- $temp = $num;
- $num =0;
- }else{
- $temp = $item->usable_stock;
- $num =bcsub($num,$item->usable_stock);
- }
- $bn=GoodStockInfo::StockBnSub($item->id,intval($temp),1);
- $returen=array_merge($returen,$bn);
- }
-
- return $returen;
- }
-
- }
|