2023-05-17 16:17:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
|
|
use App\Settings\CouponSettings;
|
|
|
|
use App\Models\Coupon as CouponModel;
|
|
|
|
use App\Models\ShopProduct;
|
2023-05-26 14:30:55 +00:00
|
|
|
use App\Models\User;
|
2023-05-17 16:17:51 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use stdClass;
|
|
|
|
|
|
|
|
trait Coupon
|
|
|
|
{
|
2023-05-18 01:13:10 +00:00
|
|
|
public function validateCoupon($requestUser, $couponCode, $productId): JsonResponse
|
2023-05-17 16:17:51 +00:00
|
|
|
{
|
2023-05-18 01:13:10 +00:00
|
|
|
$coupon = CouponModel::where('code', $couponCode)->first();
|
|
|
|
$shopProduct = ShopProduct::findOrFail($productId);
|
2023-05-17 16:17:51 +00:00
|
|
|
$coupon_settings = new CouponSettings;
|
|
|
|
$response = response()->json([
|
|
|
|
'isValid' => false,
|
|
|
|
'error' => __('This coupon does not exist.')
|
|
|
|
], 404);
|
|
|
|
|
|
|
|
if (!is_null($coupon)) {
|
|
|
|
if ($coupon->getStatus() == 'USES_LIMIT_REACHED') {
|
|
|
|
$response = response()->json([
|
|
|
|
'isValid' => false,
|
|
|
|
'error' => __('This coupon has reached the maximum amount of uses.')
|
|
|
|
], 422);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($coupon->getStatus() == 'EXPIRED') {
|
|
|
|
$response = response()->json([
|
|
|
|
'isValid' => false,
|
|
|
|
'error' => __('This coupon has expired.')
|
|
|
|
], 422);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2023-05-26 14:30:55 +00:00
|
|
|
if ($coupon->isMaxUsesReached($requestUser, $coupon_settings)) {
|
2023-05-17 16:17:51 +00:00
|
|
|
$response = response()->json([
|
|
|
|
'isValid' => false,
|
|
|
|
'error' => __('You have reached the maximum uses of this coupon.')
|
|
|
|
], 422);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2023-05-18 01:13:10 +00:00
|
|
|
if ($coupon->type === 'amount' && $coupon->value >= $shopProduct->price) {
|
|
|
|
$response = response()->json([
|
|
|
|
'isValid' => false,
|
|
|
|
'error' => __('The coupon you are trying to use would give you 100% off, so it cannot be used for this product, sorry.')
|
|
|
|
], 422);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:17:51 +00:00
|
|
|
$response = response()->json([
|
|
|
|
'isValid' => true,
|
|
|
|
'couponCode' => $coupon->code,
|
|
|
|
'couponType' => $coupon->type,
|
|
|
|
'couponValue' => $coupon->value
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2023-05-26 14:30:55 +00:00
|
|
|
public function isCouponValid(string $couponCode, User $user, string $productId): bool
|
|
|
|
{
|
|
|
|
if (is_null($couponCode)) return false;
|
|
|
|
|
|
|
|
$coupon = CouponModel::where('code', $couponCode)->first();
|
|
|
|
$shopProduct = ShopProduct::findOrFail($productId);
|
|
|
|
|
|
|
|
if ($coupon->getStatus() == 'USES_LIMIT_REACHED') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($coupon->getStatus() == 'EXPIRED') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($coupon->isMaxUsesReached($user)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($coupon->type === 'amount' && $coupon->value >= $shopProduct->price) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-18 01:13:10 +00:00
|
|
|
public function calcDiscount($productPrice, stdClass $data)
|
2023-05-17 16:17:51 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if ($data->isValid) {
|
|
|
|
if ($data->couponType === 'percentage') {
|
|
|
|
return $productPrice - ($productPrice * $data->couponValue / 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($data->couponType === 'amount') {
|
|
|
|
// There is no discount if the value of the coupon is greater than or equal to the value of the product.
|
|
|
|
if ($data->couponValue >= $productPrice) {
|
|
|
|
return $productPrice;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $productPrice - $data->couponValue;
|
|
|
|
}
|
2023-05-18 01:13:10 +00:00
|
|
|
|
|
|
|
return $productPrice;
|
2023-05-17 16:17:51 +00:00
|
|
|
}
|
2023-05-26 14:30:55 +00:00
|
|
|
|
|
|
|
public function applyCoupon(string $couponCode, float $price)
|
|
|
|
{
|
|
|
|
$coupon = CouponModel::where('code', $couponCode)->first();
|
|
|
|
|
|
|
|
if ($coupon->type === 'percentage') {
|
|
|
|
return $price - ($price * $coupon->value / 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($coupon->type === 'amount') {
|
|
|
|
// There is no discount if the value of the coupon is greater than or equal to the value of the product.
|
|
|
|
if ($coupon->value >= $price) {
|
|
|
|
return $price;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $price - $coupon->value;
|
|
|
|
}
|
2023-05-17 16:17:51 +00:00
|
|
|
}
|