MemberCart.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/2
  6. * Time: 下午4:47
  7. */
  8. namespace app\common\models;
  9. use app\common\exceptions\AppException;
  10. use Illuminate\Database\Eloquent\SoftDeletes;
  11. /**
  12. * Class MemberCart
  13. * @package app\common\models
  14. * @property int plugin_id
  15. * @property int option_id
  16. * @property int total
  17. * @property int member_id
  18. * @property int goods_id
  19. * @property Goods goods
  20. * @property GoodsOption goodsOption
  21. * @property Member member
  22. */
  23. class MemberCart extends BaseModel
  24. {
  25. use SoftDeletes;
  26. protected $guarded = ['id'];
  27. protected $table = 'yz_member_cart';
  28. public function isOption()
  29. {
  30. return !empty($this->option_id);
  31. }
  32. public function goodsOption()
  33. {
  34. return $this->belongsTo(app('GoodsManager')->make('GoodsOption'), 'option_id');
  35. }
  36. public function goods()
  37. {
  38. return $this->belongsTo(app('GoodsManager')->make('Goods'));
  39. }
  40. /**
  41. * 购物车验证
  42. * @throws AppException
  43. */
  44. public function validate()
  45. {
  46. if (!isset($this->goods)) {
  47. throw new AppException('(ID:' . $this->goods_id . ')未找到商品或已经删除');
  48. }
  49. //todo 验证商品是否启用规格
  50. $this->goods->verifyOption($this->option_id);
  51. //商品基本验证
  52. $this->goods->generalValidate($this->member, $this->total);
  53. //商品购买权限验证
  54. if (isset($this->goods->hasOnePrivilege)) {
  55. //开启按规格限制购买
  56. if ( $this->goods->hasOnePrivilege->option_id_array && $this->isOption()) {
  57. $privilegeValidate = new \app\common\services\goods\GoodsOptionBuyLimit($this->goods->hasOnePrivilege, $this->goodsOption);
  58. $privilegeValidate->goodsValidate($this->member, $this->total);
  59. } else {
  60. $this->goods->hasOnePrivilege->validate($this->member, $this->total);
  61. }
  62. }
  63. if ($this->isOption()) {
  64. $this->goodsOptionValidate();
  65. } else {
  66. $this->goodsValidate();
  67. }
  68. //插件下单购物车验证配置
  69. $configs = \app\common\modules\shop\ShopConfig::current()->get('shop-foundation.member-cart.validate');
  70. if ($configs) {
  71. foreach ($configs as $configK => $pluginConfig) {
  72. $class = array_get($pluginConfig,'class');
  73. $function =array_get($pluginConfig,'function');
  74. if(class_exists($class) && method_exists($class,$function) && is_callable([$class,$function])){
  75. $class::$function($this);
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * 商品购买验证
  82. * @throws AppException
  83. */
  84. public function goodsValidate()
  85. {
  86. if (!$this->goods->stockEnough($this->total)) {
  87. throw new AppException('(ID:' . $this->goods_id . ')'.$this->goods->title.',库存不足');
  88. }
  89. }
  90. /**
  91. * 规格验证
  92. * @throws AppException
  93. */
  94. public function goodsOptionValidate()
  95. {
  96. if (!$this->goods->has_option) {
  97. throw new AppException('(ID:' . $this->goods_id . ')商品未启用规格');
  98. }
  99. if (!isset($this->goodsOption)) {
  100. throw new AppException('(ID:' . $this->goods_id . ')未找到商品规格或已经删除');
  101. }
  102. if ($this->goods_id != $this->goodsOption->goods_id) {
  103. throw new AppException('规格('.$this->option_id.')'.$this->goodsOption->title.'不属于商品('.$this->goods_id.')'.$this->goods->title);
  104. }
  105. if (!$this->goodsOption->stockEnough($this->total)) {
  106. throw new AppException('(ID:' . $this->goods_id . ')规格'.$this->goodsOption->title.',库存不足');
  107. }
  108. }
  109. public function member()
  110. {
  111. return $this->belongsTo(Member::class, 'member_id', 'uid');
  112. }
  113. /**
  114. * 获取购物车分组id
  115. * @return int
  116. */
  117. public function getGroupId()
  118. {
  119. // 判断是否拆单。如果开启商品拆单,则将每种商品拆成不同订单,不考虑规格数量.只拆商城的商品订单
  120. if ($this->goods->plugin_id == 0 && !request()->is_shop_pos && \Setting::get('shop.order.order_apart')){
  121. return $this->goods_id;
  122. }
  123. // 厂家拆单
  124. if (!is_null($this->goods->producerGoods)) {
  125. if ($this->goods->producerGoods->switch == 1) {
  126. return $this->goods->producerGoods->producer_id;
  127. }
  128. }
  129. if (!$this->goods->getPlugin()) {
  130. return 0;
  131. }
  132. if (!$this->goods->getPlugin()->app()->bound(MemberCart::class)) {
  133. return 0;
  134. }
  135. return $this->goods->getPlugin()->app()->make(MemberCart::class, [$this->getAttributes()])->getGroupId();
  136. }
  137. }