GoodsAndStoreScope.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: weifeng
  5. * Date: 2021-04-20
  6. * Time: 10:06
  7. *
  8. * .--, .--,
  9. * ( ( \.---./ ) )
  10. * '.__/o o\__.'
  11. * {= ^ =}
  12. * > - <
  13. * / \
  14. * // \\
  15. * //| . |\\
  16. * "'\ /'"_.-~^`'-.
  17. * \ _ /--' `
  18. * ___)( )(___
  19. * (((__) (__))) 梦之所想,心之所向.
  20. */
  21. namespace app\frontend\modules\coupon\services\models\UseScope;
  22. use app\common\exceptions\AppException;
  23. use app\common\models\Goods;
  24. use app\common\models\Store;
  25. use app\common\modules\orderGoods\models\PreOrderGoods;
  26. use app\frontend\modules\orderGoods\models\PreOrderGoodsCollection;
  27. use Yunshop\StoreCashier\common\models\CashierGoods;
  28. use Yunshop\StoreCashier\common\models\StoreGoods;
  29. class GoodsAndStoreScope extends CouponUseScope
  30. {
  31. public function _getOrderGoodsOfUsedCoupon()
  32. {
  33. $order_goods = new PreOrderGoodsCollection();
  34. if (!app('plugins')->isEnabled('store-cashier')) {
  35. return $order_goods;
  36. }
  37. $order_goods = $this->coupon->getPreOrder()->orderGoods->filter(function ($orderGoods) {
  38. $use_conditions = unserialize($this->coupon->getMemberCoupon()->belongsToCoupon->use_conditions);
  39. if (empty($use_conditions)) {
  40. return false;
  41. }
  42. if ($use_conditions['is_all_store'] == 1) {
  43. $store_ids = Store::uniacid()->pluck('id')->all();
  44. } else {
  45. $store_ids = $use_conditions['store_ids'];
  46. }
  47. $appoint_store_good_ids = StoreGoods::whereIn('store_id', $store_ids)->pluck('goods_id')->all(); //指定门店(商品id)
  48. $cashier_good_ids = Store::uniacid()->whereIn('id', $store_ids)->pluck('cashier_id')->all(); //收银台id(商品id)
  49. if ($use_conditions['is_all_good'] == 1) {
  50. $except_plugin_id = [92,101];
  51. $appoint_good_ids = Goods::uniacid()->where('status', 1)->whereNotIn('plugin_id', $except_plugin_id)->pluck('id')->all();
  52. } else {
  53. $appoint_good_ids = $use_conditions['good_ids']; //指定商品id
  54. }
  55. $all_good_ids = array_merge($appoint_store_good_ids, $appoint_good_ids, $cashier_good_ids);
  56. return in_array($orderGoods->goods_id, $all_good_ids);
  57. });
  58. if ($order_goods->unique('is_plugin')->count() > 1) {
  59. throw new AppException('自营商品与第三方商品不能共用一张优惠券');
  60. }
  61. return $order_goods;
  62. }
  63. }