update: add coupon limit period

This commit is contained in:
tokumeikoi
2021-12-28 01:59:59 +08:00
parent 6ed9cc559e
commit a557aa6d32
7 changed files with 23 additions and 5 deletions

View File

@ -11,6 +11,7 @@ class CouponService
public $coupon;
public $planId;
public $userId;
public $period;
public function __construct($code)
{
@ -21,6 +22,7 @@ class CouponService
{
$this->setPlanId($order->plan_id);
$this->setUserId($order->user_id);
$this->setPeriod($order->period);
$this->check();
switch ($this->coupon->type) {
case 1:
@ -62,6 +64,11 @@ class CouponService
$this->userId = $userId;
}
public function setPeriod($period)
{
$this->period = $period;
}
public function checkLimitUseWithUser():bool
{
$usedCount = Order::where('coupon_id', $this->coupon->id)
@ -91,6 +98,11 @@ class CouponService
abort(500, __('The coupon code cannot be used for this subscription'));
}
}
if ($this->coupon->limit_period && $this->period) {
if (!in_array($this->period, $this->coupon->limit_period)) {
abort(500, __('The coupon code cannot be used for this period'));
}
}
if ($this->coupon->limit_use_with_user !== NULL && $this->userId) {
if (!$this->checkLimitUseWithUser()) {
abort(500, __('The coupon can only be used :limit_use_with_user per person', [