mirror of
https://github.com/v2board/v2board.git
synced 2025-06-13 21:28:27 +08:00
update: add coupon limit period
This commit is contained in:
@ -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);
|
||||
|
@ -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' => '指定周期格式有误'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -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'
|
||||
];
|
||||
}
|
||||
|
@ -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', [
|
||||
|
Reference in New Issue
Block a user