GoodStock.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\model;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class GoodStock extends Model
  9. {
  10. protected $createTime="addtime";
  11. protected $updateTime="updatetime";
  12. public function WsmInfo(){
  13. return $this->belongsTo(WarehouseInfo::class,"wsm_code","wsm_code")->bind(["wsm_name"=>"name","wsm_supplierNo"=>"supplierNo","wsm_supplierName"=>"supplierName"]);
  14. }
  15. //组合商品占用 库存
  16. public function stockSub($spuCode,$num){
  17. $row =$this->withJoin(['wsminfo'],'left')
  18. ->field('good_stock.id,usable_stock')
  19. ->where(['spuCode' => $spuCode, 'wsm_type'=>[2,5]])
  20. ->where("usable_stock",">",0)
  21. ->select();
  22. if($row->isEmpty()) throw new \Exception('未找到对应子商品库存信息');
  23. $usable = array_sum(array_column($row->toArray(),"usable_stock"));
  24. if($usable < $num)throw new \Exception('对应子商品库存数不足');
  25. $returen=[];
  26. foreach ($row as $item){
  27. if($num<=0) break;
  28. if($item->usable_stock >= $num){
  29. $temp = $num;
  30. $num =0;
  31. }else{
  32. $temp = $item->usable_stock;
  33. $num =bcsub($num,$item->usable_stock);
  34. }
  35. $bn=GoodStockInfo::StockBnSub($item->id,intval($temp),1);
  36. $returen=array_merge($returen,$bn);
  37. }
  38. return $returen;
  39. }
  40. }