GoodStock.php 1.4 KB

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