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 = $request->validated();
$coupon['created_at'] = $coupon['updated_at'] = time(); $coupon['created_at'] = $coupon['updated_at'] = time();
$coupon['limit_plan_ids'] = json_encode($coupon['limit_plan_ids']); $coupon['limit_plan_ids'] = json_encode($coupon['limit_plan_ids']);
$coupon['limit_period'] = json_encode($coupon['limit_period']);
unset($coupon['generate_count']); unset($coupon['generate_count']);
for ($i = 0;$i < $request->input('generate_count');$i++) { for ($i = 0;$i < $request->input('generate_count');$i++) {
$coupon['code'] = Helper::randomChar(8); $coupon['code'] = Helper::randomChar(8);

View File

@ -23,6 +23,7 @@ class CouponGenerate extends FormRequest
'limit_use' => 'nullable|integer', 'limit_use' => 'nullable|integer',
'limit_use_with_user' => 'nullable|integer', 'limit_use_with_user' => 'nullable|integer',
'limit_plan_ids' => 'nullable|array', 'limit_plan_ids' => 'nullable|array',
'limit_period' => 'nullable|array',
'code' => '' 'code' => ''
]; ];
} }
@ -43,7 +44,8 @@ class CouponGenerate extends FormRequest
'ended_at.integer' => '结束时间格式有误', 'ended_at.integer' => '结束时间格式有误',
'limit_use.integer' => '最大使用次数格式有误', 'limit_use.integer' => '最大使用次数格式有误',
'limit_use_with_user.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 = [ protected $casts = [
'created_at' => 'timestamp', 'created_at' => 'timestamp',
'updated_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 $coupon;
public $planId; public $planId;
public $userId; public $userId;
public $period;
public function __construct($code) public function __construct($code)
{ {
@ -21,6 +22,7 @@ class CouponService
{ {
$this->setPlanId($order->plan_id); $this->setPlanId($order->plan_id);
$this->setUserId($order->user_id); $this->setUserId($order->user_id);
$this->setPeriod($order->period);
$this->check(); $this->check();
switch ($this->coupon->type) { switch ($this->coupon->type) {
case 1: case 1:
@ -62,6 +64,11 @@ class CouponService
$this->userId = $userId; $this->userId = $userId;
} }
public function setPeriod($period)
{
$this->period = $period;
}
public function checkLimitUseWithUser():bool public function checkLimitUseWithUser():bool
{ {
$usedCount = Order::where('coupon_id', $this->coupon->id) $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')); 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->coupon->limit_use_with_user !== NULL && $this->userId) {
if (!$this->checkLimitUseWithUser()) { if (!$this->checkLimitUseWithUser()) {
abort(500, __('The coupon can only be used :limit_use_with_user per person', [ abort(500, __('The coupon can only be used :limit_use_with_user per person', [

File diff suppressed because one or more lines are too long

View File

@ -85,5 +85,6 @@
"Password can not be empty": "Password can not be empty", "Password can not be empty": "Password can not be empty",
"The traffic usage in :app_name has reached 80%": "The traffic usage in :app_name has reached 80%", "The traffic usage in :app_name has reached 80%": "The traffic usage in :app_name has reached 80%",
"The service in :app_name is about to expire": "The service in :app_name is about to expire", "The service in :app_name is about to expire": "The service in :app_name is about to expire",
"The coupon can only be used :limit_use_with_user per person": "The coupon can only be used :limit_use_with_user per person" "The coupon can only be used :limit_use_with_user per person": "The coupon can only be used :limit_use_with_user per person",
"The coupon code cannot be used for this period": "The coupon code cannot be used for this period"
} }

View File

@ -85,5 +85,6 @@
"Password can not be empty": "密码不能为空", "Password can not be empty": "密码不能为空",
"The traffic usage in :app_name has reached 80%": "在:app_name的流量使用已达到80%", "The traffic usage in :app_name has reached 80%": "在:app_name的流量使用已达到80%",
"The service in :app_name is about to expire": "在:app_name的服务即将到期", "The service in :app_name is about to expire": "在:app_name的服务即将到期",
"The coupon can only be used :limit_use_with_user per person": "该优惠券每人只能用:limit_use_with_user次" "The coupon can only be used :limit_use_with_user per person": "该优惠券每人只能用:limit_use_with_user次",
"The coupon code cannot be used for this period": "该优惠券无法用于这个付款周期"
} }