GoodCombind.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. $GoodStock=new GoodStock();
  38. $goodinfo = new Good();
  39. $combind = new CombindBninfo();
  40. foreach ($comblist as $item){
  41. $row =$GoodStock->withJoin(['wsminfo'],'left')
  42. ->field('id,usable_stock')
  43. ->where(['spuCode' => $item['childCode'], 'warehouse_info.wsm_type'=>[2,5]])
  44. ->findOrEmpty();
  45. if($row->isEmpty()) throw new \Exception('未找到对应子商品信息');
  46. $stocknum =bcmul($num,$item['child_num']);
  47. $good = $goodinfo->where(["spuCode"=>$item['childCode']])->findOrEmpty();
  48. if($good->isEmpty()==false){
  49. $good->usable_stock = bcadd($good->usable_stock,$stocknum);
  50. $gdup=$good->save();
  51. if($gdup==false)throw new \Exception('子商品库存更新失败');
  52. }
  53. $up=$combind->subBN($spuCode,$bnCode,$row->id,$stocknum);
  54. if($up){
  55. $row->usable_stock = bcadd($row->usable_stock,$stocknum);
  56. $row->total_stock = bcadd($row->total_stock,$stocknum);
  57. $stocku=$row->save();
  58. if($stocku==false)throw new \Exception('子商品库存更新失败');
  59. }else throw new \Exception('子商品库存更新失败');
  60. }
  61. return true;
  62. }
  63. }