CouponService.php 3.2 KB

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