fix(coupon): correct knows issues
This commit is contained in:
@@ -11,12 +11,18 @@ class CouponController extends Controller
|
|||||||
{
|
{
|
||||||
public function check(Request $request)
|
public function check(Request $request)
|
||||||
{
|
{
|
||||||
|
$request->validate([
|
||||||
|
'code' => 'required|string',
|
||||||
|
'plan_id' => 'required|integer',
|
||||||
|
'period' => 'nullable|string',
|
||||||
|
]);
|
||||||
if (empty($request->input('code'))) {
|
if (empty($request->input('code'))) {
|
||||||
return $this->fail([422,__('Coupon cannot be empty')]);
|
return $this->fail([422,__('Coupon cannot be empty')]);
|
||||||
}
|
}
|
||||||
$couponService = new CouponService($request->input('code'));
|
$couponService = new CouponService($request->input('code'));
|
||||||
$couponService->setPlanId($request->input('plan_id'));
|
$couponService->setPlanId($request->input('plan_id'));
|
||||||
$couponService->setUserId($request->user()->id);
|
$couponService->setUserId($request->user()->id);
|
||||||
|
$couponService->setPeriod($request->input('period'));
|
||||||
$couponService->check();
|
$couponService->check();
|
||||||
return $this->success($couponService->getCoupon());
|
return $this->success($couponService->getCoupon());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Services\PlanService;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
class Coupon extends Model
|
class Coupon extends Model
|
||||||
@@ -15,4 +16,11 @@ class Coupon extends Model
|
|||||||
'limit_plan_ids' => 'array',
|
'limit_plan_ids' => 'array',
|
||||||
'limit_period' => 'array'
|
'limit_period' => 'array'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function getLimitPeriodAttribute($value)
|
||||||
|
{
|
||||||
|
return collect(json_decode($value, true))->map(function ($item) {
|
||||||
|
return PlanService::getPeriodKey($item);
|
||||||
|
})->toArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class CouponService
|
|||||||
->first();
|
->first();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function use(Order $order):bool
|
public function use(Order $order): bool
|
||||||
{
|
{
|
||||||
$this->setPlanId($order->plan_id);
|
$this->setPlanId($order->plan_id);
|
||||||
$this->setUserId($order->user_id);
|
$this->setUserId($order->user_id);
|
||||||
@@ -39,7 +39,8 @@ class CouponService
|
|||||||
$order->discount_amount = $order->total_amount;
|
$order->discount_amount = $order->total_amount;
|
||||||
}
|
}
|
||||||
if ($this->coupon->limit_use !== NULL) {
|
if ($this->coupon->limit_use !== NULL) {
|
||||||
if ($this->coupon->limit_use <= 0) return false;
|
if ($this->coupon->limit_use <= 0)
|
||||||
|
return false;
|
||||||
$this->coupon->limit_use = $this->coupon->limit_use - 1;
|
$this->coupon->limit_use = $this->coupon->limit_use - 1;
|
||||||
if (!$this->coupon->save()) {
|
if (!$this->coupon->save()) {
|
||||||
return false;
|
return false;
|
||||||
@@ -70,16 +71,19 @@ class CouponService
|
|||||||
|
|
||||||
public function setPeriod($period)
|
public function setPeriod($period)
|
||||||
{
|
{
|
||||||
$this->period = $period;
|
if ($period) {
|
||||||
|
$this->period = PlanService::getPeriodKey($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)
|
||||||
->where('user_id', $this->userId)
|
->where('user_id', $this->userId)
|
||||||
->whereNotIn('status', [0, 2])
|
->whereNotIn('status', [0, 2])
|
||||||
->count();
|
->count();
|
||||||
if ($usedCount >= $this->coupon->limit_use_with_user) return false;
|
if ($usedCount >= $this->coupon->limit_use_with_user)
|
||||||
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+4
-4
File diff suppressed because one or more lines are too long
Vendored
+944
-920
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user