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

@ -64,6 +64,7 @@ class CouponController extends Controller
$coupon = $request->validated();
$coupon['created_at'] = $coupon['updated_at'] = time();
$coupon['limit_plan_ids'] = json_encode($coupon['limit_plan_ids']);
$coupon['limit_period'] = json_encode($coupon['limit_period']);
unset($coupon['generate_count']);
for ($i = 0;$i < $request->input('generate_count');$i++) {
$coupon['code'] = Helper::randomChar(8);

View File

@ -23,6 +23,7 @@ class CouponGenerate extends FormRequest
'limit_use' => 'nullable|integer',
'limit_use_with_user' => 'nullable|integer',
'limit_plan_ids' => 'nullable|array',
'limit_period' => 'nullable|array',
'code' => ''
];
}
@ -43,7 +44,8 @@ class CouponGenerate extends FormRequest
'ended_at.integer' => '结束时间格式有误',
'limit_use.integer' => '最大使用次数格式有误',
'limit_use_with_user.integer' => '限制用户使用次数格式有误',
'limit_plan_ids.array' => '指定订阅格式有误'
'limit_plan_ids.array' => '指定订阅格式有误',
'limit_period.array' => '指定周期格式有误'
];
}
}

View File

@ -12,6 +12,7 @@ class Coupon extends Model
protected $casts = [
'created_at' => 'timestamp',
'updated_at' => 'timestamp',
'limit_plan_ids' => 'array'
'limit_plan_ids' => 'array',
'limit_period' => 'array'
];
}

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', [