GoodCombind.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\model;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class GoodCombind extends Model
  9. {
  10. protected $createTime="createtime";
  11. protected $updateTime="updatetime";
  12. protected $append=["usable_stock"];
  13. public function getUsabelStockAttr($v,$row){
  14. return Good::where(["spuCode"=>$row['childCode']])->value("usable_stock",0);
  15. }
  16. public function AddStock($spuCode='',$num="0"){
  17. $comblist = $this->where(['spuCode'=>$spuCode,'is_del'=>0])->select()->toArray();
  18. if(empty($comblist))throw new \Exception("未找到对应子商品信息");
  19. $GoodStock=new GoodStock();
  20. $returnBn=[];
  21. foreach ($comblist as $item){
  22. $row =$GoodStock->withJoin(["wsminfo"],"left")
  23. ->field('id,usable_stock')
  24. ->where(['spuCode' => $item['childCode'], 'warehouse_info.wsm_type'=>[2,5]])
  25. ->findOrEmpty();
  26. if($row->isEmpty()) throw new \Exception('未找到对应子商品信息');
  27. $stocknum =bcmul($num,$item["child_num"]);
  28. $up =GoodStockInfo::StockBnSub($row->id,intval($stocknum));
  29. if(empty($up)) throw new \Exception('未找到对应子商品BN库存信息');
  30. $returnBn=array_merge($returnBn,$up);
  31. }
  32. return $returnBn;
  33. }
  34. public function subStock($spuCode='',$bnCode='',$num='0'){
  35. $comblist = $this->where(['spuCode'=>$spuCode,'is_del'=>0])->select()->toArray();
  36. if(empty($comblist))throw new \Exception('未找到对应子商品信息');
  37. $goodinfo = new Good();
  38. $combind = new CombindBninfo();
  39. foreach ($comblist as $item){
  40. $stocknum =bcmul($num,$item['child_num']);
  41. $good = $goodinfo->where(["spuCode"=>$item['childCode']])->findOrEmpty();
  42. if($good->isEmpty()==false){
  43. $good->usable_stock = bcadd($good->usable_stock,$stocknum);
  44. $gdup=$good->save();
  45. if($gdup==false)throw new \Exception('子商品库存更新失败');
  46. }
  47. $up=$combind->subBN($spuCode,$bnCode,$stocknum);
  48. if($up==false)throw new \Exception('子商品库存更新失败');
  49. }
  50. return true;
  51. }
  52. }