CouponHandler.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace addons\TinyShop\common\components\marketing;
  3. use Yii;
  4. use yii\web\UnprocessableEntityHttpException;
  5. use common\enums\UseStateEnum;
  6. use addons\TinyShop\common\forms\PreviewForm;
  7. use addons\TinyShop\common\components\PreviewInterface;
  8. use addons\TinyShop\common\enums\MarketingEnum;
  9. /**
  10. * 优惠券
  11. *
  12. * Class CouponHandler
  13. * @package addons\TinyShop\common\components\marketing
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. class CouponHandler extends PreviewInterface
  17. {
  18. /**
  19. * @param PreviewForm $form
  20. * @return PreviewForm
  21. * @throws UnprocessableEntityHttpException
  22. */
  23. public function execute(PreviewForm $form): PreviewForm
  24. {
  25. if (empty($form->coupon_id)) {
  26. return $form;
  27. }
  28. if (!($coupon = Yii::$app->tinyShopService->marketingCoupon->findByMemberId($form->coupon_id, $form->member->id))) {
  29. throw new UnprocessableEntityHttpException('找不到优惠券');
  30. }
  31. if ($coupon['state'] == UseStateEnum::USE) {
  32. throw new UnprocessableEntityHttpException('优惠券已使用');
  33. }
  34. if ($coupon['state'] == UseStateEnum::PAST_DUE) {
  35. throw new UnprocessableEntityHttpException('优惠券已过期');
  36. }
  37. if ($coupon['state'] == UseStateEnum::GET && (time() <= $coupon['start_time'] || time() >= $coupon['end_time'])) {
  38. throw new UnprocessableEntityHttpException('优惠券不在有效使用时间内');
  39. }
  40. if ($coupon['merchant_id'] != 0 && $form->merchant_id != $coupon['merchant_id']) {
  41. throw new UnprocessableEntityHttpException('无效的优惠券');
  42. }
  43. $validCoupon = Yii::$app->tinyShopService->marketingCoupon->getPredictDiscountByCoupon($coupon, $form->groupOrderProducts);
  44. if ($validCoupon == false) {
  45. throw new UnprocessableEntityHttpException('优惠券不可用');
  46. }
  47. $couponMoney = $validCoupon['predictDiscount'];
  48. // 记录营销
  49. $form->marketingDetails[] = [
  50. 'uuid' => $validCoupon['productIds'],
  51. 'marketing_id' => $coupon['coupon_type_id'],
  52. 'marketing_type' => MarketingEnum::COUPON,
  53. 'marketing_condition' => '满' . $coupon['at_least'] . '元,折扣减' . $couponMoney,
  54. 'discount_money' => $couponMoney,
  55. ];
  56. if ($validCoupon['predictTotalMoney'] < $coupon['at_least']) {
  57. throw new UnprocessableEntityHttpException('优惠券需满 ' . $coupon['at_least'] . ' 元才可使用.');
  58. }
  59. $form->coupon = $coupon;
  60. // 成功触发
  61. return $this->success($form);
  62. }
  63. /**
  64. * @param array $couponProductIds 优惠券可用商品id
  65. * @param array $productIds 已有商品id
  66. * @return array
  67. * @throws UnprocessableEntityHttpException
  68. */
  69. public function usableVerify(array $couponProductIds, array $productIds)
  70. {
  71. // 有效的商品id
  72. $usableIds = [];
  73. $couponUsable = false;
  74. foreach ($couponProductIds as $couponProductId) {
  75. if (in_array($couponProductId, $productIds)) {
  76. $couponUsable = true;
  77. $usableIds[] = $couponProductId;
  78. }
  79. }
  80. if ($couponUsable == false) {
  81. throw new UnprocessableEntityHttpException('该优惠券不可用');
  82. }
  83. return $usableIds;
  84. }
  85. /**
  86. * 排斥营销
  87. *
  88. * @return array
  89. */
  90. public function rejectNames()
  91. {
  92. return [];
  93. }
  94. /**
  95. * 营销名称
  96. *
  97. * @return string
  98. */
  99. public static function getName(): string
  100. {
  101. return 'coupon';
  102. }
  103. }
粤ICP备19079148号