GoodCombind.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // 定义全局的查询范围
  14. protected $globalScope = ['is_del'];
  15. public function scopeIsDel($query)
  16. {
  17. $query->where('is_del',0);
  18. }
  19. public function getUsableStockAttr($v,$row){
  20. return (new GoodStock())->withJoin(['wsminfo'] , 'left')
  21. ->where(['spuCode'=>$row['childCode'] , 'wsm_type'=>[2 , 5]])
  22. ->sum('usable_stock');
  23. }
  24. public function AddStock($spuCode='',$num="0"){
  25. $comblist = $this->where(['spuCode'=>$spuCode,'is_del'=>0])->select()->toArray();
  26. if(empty($comblist))throw new \Exception("未找到对应子商品信息");
  27. $GoodStock=new GoodStock();
  28. $returnBn=[];
  29. foreach ($comblist as $item){
  30. $row =$GoodStock->withJoin(["wsminfo"],"left")
  31. ->field('id,usable_stock')
  32. ->where(['spuCode' => $item['childCode'], 'warehouse_info.wsm_type'=>[2,5]])
  33. ->findOrEmpty();
  34. if($row->isEmpty()) throw new \Exception('未找到对应子商品信息');
  35. $stocknum =bcmul($num,$item["child_num"]);
  36. $up =GoodStockInfo::StockBnSub($row->id,intval($stocknum));
  37. if(empty($up)) throw new \Exception('未找到对应子商品BN库存信息');
  38. $returnBn=array_merge($returnBn,$up);
  39. }
  40. return $returnBn;
  41. }
  42. /**
  43. * @param string $spuCode
  44. * @param string $bnCode
  45. * @param string $num
  46. * @param string $stockCode 库存变化申请编号
  47. * @param int $flag 0 解除库存 1 占用库存驳回
  48. * @return bool
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function subStock($spuCode='',$bnCode='',$num='0',$stockCode=''){
  54. $comblist = $this->where(['spuCode'=>$spuCode,'is_del'=>0])->select()->toArray();
  55. if(empty($comblist))throw new \Exception('未找到对应子商品信息');
  56. $goodinfo = new Good();
  57. $combind = new CombindBninfo();
  58. foreach ($comblist as $item){
  59. $stocknum =bcmul($num.'',$item['child_num']);
  60. $good = $goodinfo->where(["spuCode"=>$item['childCode']])->findOrEmpty();
  61. if($good->isEmpty()==false){
  62. $good->usable_stock = bcadd($good->usable_stock,$stocknum);
  63. $gdup=$good->save();
  64. if($gdup==false)throw new \Exception('子商品库存更新失败');
  65. }
  66. $up=$combind->subBN($spuCode,$bnCode,$item['childCode'],$stocknum,$stockCode);
  67. if($up==false)throw new \Exception('子商品库存更新失败');
  68. }
  69. return true;
  70. }
  71. }