Use casts instead of converting values

This commit is contained in:
Ferks-FK 2023-05-18 14:33:42 +00:00 committed by IceToast
parent 8f7ad95506
commit 490e11572d
4 changed files with 22 additions and 20 deletions

View file

@ -47,13 +47,9 @@ class PayPalExtension extends AbstractExtension
$discount = PartnerDiscount::getDiscount(); $discount = PartnerDiscount::getDiscount();
$couponCode = $request->input('couponCode'); $couponCode = $request->input('couponCode');
$isValidCoupon = $this->validateCoupon($request->user(), $couponCode, $request->shopProduct); $isValidCoupon = $this->validateCoupon($request->user(), $couponCode, $request->shopProduct);
if (is_string($shopProduct->price)) {
$shopProduct->price = floatval($shopProduct->price);
}
$price = $shopProduct->price; $price = $shopProduct->price;
// Coupon Discount.
if ($isValidCoupon->getStatusCode() == 200) { if ($isValidCoupon->getStatusCode() == 200) {
$price = $this->calcDiscount($price, $isValidCoupon->getData()); $price = $this->calcDiscount($price, $isValidCoupon->getData());
} }

View file

@ -10,6 +10,9 @@ class Coupon extends Model
{ {
use HasFactory; use HasFactory;
/**
* @var string[]
*/
protected $fillable = [ protected $fillable = [
'code', 'code',
'type', 'type',
@ -19,6 +22,16 @@ class Coupon extends Model
'expires_at' 'expires_at'
]; ];
/**
* @var string[]
*/
protected $casts = [
'value' => 'float',
'uses' => 'integer',
'max_uses' => 'integer',
'expires_at' => 'timestamp'
];
/** /**
* Returns the date format used by the coupons. * Returns the date format used by the coupons.
* *
@ -41,9 +54,7 @@ class Coupon extends Model
} }
if (!is_null($this->expires_at)) { if (!is_null($this->expires_at)) {
$expires_at = Carbon::createFromTimeString($this->expires_at)->timestamp; if ($this->expires_at <= Carbon::now()->timestamp) {
if ($expires_at <= Carbon::now()->timestamp) {
return __('EXPIRED'); return __('EXPIRED');
} }
} }

View file

@ -36,6 +36,13 @@ class ShopProduct extends Model
'disabled', 'disabled',
]; ];
/**
* @var string[]
*/
protected $casts = [
'price' => 'float'
];
public static function boot() public static function boot()
{ {
parent::boot(); parent::boot();

View file

@ -22,14 +22,6 @@ trait Coupon
], 404); ], 404);
if (!is_null($coupon)) { if (!is_null($coupon)) {
if (is_string($coupon->value)) {
$coupon->value = floatval($coupon->value);
}
if (is_string($shopProduct->price)) {
$shopProduct->price = floatval($shopProduct->price);
}
if ($coupon->getStatus() == 'USES_LIMIT_REACHED') { if ($coupon->getStatus() == 'USES_LIMIT_REACHED') {
$response = response()->json([ $response = response()->json([
'isValid' => false, 'isValid' => false,
@ -81,10 +73,6 @@ trait Coupon
{ {
if ($data->isValid) { if ($data->isValid) {
if (is_string($productPrice)) {
$productPrice = floatval($productPrice);
}
if ($data->couponType === 'percentage') { if ($data->couponType === 'percentage') {
return $productPrice - ($productPrice * $data->couponValue / 100); return $productPrice - ($productPrice * $data->couponValue / 100);
} }