CouponService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Coupon;
  4. use App\Models\Order;
  5. use Illuminate\Support\Facades\DB;
  6. class CouponService
  7. {
  8. public $coupon;
  9. public $planId;
  10. public $userId;
  11. public $period;
  12. public function __construct($code)
  13. {
  14. $this->coupon = Coupon::where('code', $code)
  15. ->lockForUpdate()
  16. ->first();
  17. }
  18. public function use(Order $order):bool
  19. {
  20. $this->setPlanId($order->plan_id);
  21. $this->setUserId($order->user_id);
  22. $this->setPeriod($order->period);
  23. $this->check();
  24. switch ($this->coupon->type) {
  25. case 1:
  26. $order->discount_amount = $this->coupon->value;
  27. break;
  28. case 2:
  29. $order->discount_amount = $order->total_amount * ($this->coupon->value / 100);
  30. break;
  31. }
  32. if ($order->discount_amount > $order->total_amount) {
  33. $order->discount_amount = $order->total_amount;
  34. }
  35. if ($this->coupon->limit_use !== NULL) {
  36. if ($this->coupon->limit_use <= 0) return false;
  37. $this->coupon->limit_use = $this->coupon->limit_use - 1;
  38. if (!$this->coupon->save()) {
  39. return false;
  40. }
  41. }
  42. return true;
  43. }
  44. public function getId()
  45. {
  46. return $this->coupon->id;
  47. }
  48. public function getCoupon()
  49. {
  50. return $this->coupon;
  51. }
  52. public function setPlanId($planId)
  53. {
  54. $this->planId = $planId;
  55. }
  56. public function setUserId($userId)
  57. {
  58. $this->userId = $userId;
  59. }
  60. public function setPeriod($period)
  61. {
  62. $this->period = $period;
  63. }
  64. public function checkLimitUseWithUser():bool
  65. {
  66. $usedCount = Order::where('coupon_id', $this->coupon->id)
  67. ->where('user_id', $this->userId)
  68. ->whereNotIn('status', [0, 2])
  69. ->count();
  70. if ($usedCount >= $this->coupon->limit_use_with_user) return false;
  71. return true;
  72. }
  73. public function check()
  74. {
  75. if (!$this->coupon || !$this->coupon->show) {
  76. abort(500, __('Invalid coupon'));
  77. }
  78. if ($this->coupon->limit_use <= 0 && $this->coupon->limit_use !== NULL) {
  79. abort(500, __('This coupon is no longer available'));
  80. }
  81. if (time() < $this->coupon->started_at) {
  82. abort(500, __('This coupon has not yet started'));
  83. }
  84. if (time() > $this->coupon->ended_at) {
  85. abort(500, __('This coupon has expired'));
  86. }
  87. if ($this->coupon->limit_plan_ids && $this->planId) {
  88. if (!in_array($this->planId, $this->coupon->limit_plan_ids)) {
  89. abort(500, __('The coupon code cannot be used for this subscription'));
  90. }
  91. }
  92. if ($this->coupon->limit_period && $this->period) {
  93. if (!in_array($this->period, $this->coupon->limit_period)) {
  94. abort(500, __('The coupon code cannot be used for this period'));
  95. }
  96. }
  97. if ($this->coupon->limit_use_with_user !== NULL && $this->userId) {
  98. if (!$this->checkLimitUseWithUser()) {
  99. abort(500, __('The coupon can only be used :limit_use_with_user per person', [
  100. 'limit_use_with_user' => $this->coupon->limit_use_with_user
  101. ]));
  102. }
  103. }
  104. }
  105. }