FleetCart/Modules/Cart/CartCoupon.php

180 lines
3.9 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Modules\Cart;
use JsonSerializable;
use Modules\Support\Money;
class CartCoupon implements JsonSerializable
{
private $cart;
private $coupon;
private $couponCondition;
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function __construct($cart, $coupon, $couponCondition)
{
$this->cart = $cart;
$this->coupon = $coupon;
$this->couponCondition = $couponCondition;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function entity()
{
return $this->coupon;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function id()
{
return $this->coupon->id;
}
public function isFreeShipping()
{
return $this->coupon->free_shipping;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function usageLimitReached($customerEmail)
{
return $this->fresh()->coupon->usageLimitReached($customerEmail);
}
2023-12-03 14:07:47 +00:00
public function fresh(): static
{
$this->coupon = $this->coupon->refresh();
return $this;
}
2023-06-11 12:14:03 +00:00
public function didNotSpendTheRequiredAmount()
{
return $this->fresh()->coupon->didNotSpendTheRequiredAmount();
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function spentMoreThanMaximumAmount()
{
return $this->fresh()->coupon->spentMoreThanMaximumAmount();
}
2023-12-03 14:07:47 +00:00
public function usedOnce()
2023-06-11 12:14:03 +00:00
{
2023-12-03 14:07:47 +00:00
$this->coupon->increment('used');
}
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
public function __toString()
{
return json_encode($this->jsonSerialize());
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
public function jsonSerialize()
2023-06-11 12:14:03 +00:00
{
2023-12-03 14:07:47 +00:00
return $this->toArray();
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
public function toArray(): array
2023-06-11 12:14:03 +00:00
{
2023-12-03 14:07:47 +00:00
return [
'code' => $this->code(),
'value' => $this->value(),
'free_shipping' => $this->coupon->free_shipping,
];
}
public function code()
{
return $this->coupon->code;
}
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
public function value()
{
2023-06-11 12:14:03 +00:00
return Money::inDefaultCurrency($this->calculate());
}
2023-12-03 14:07:47 +00:00
public function __get($attribute)
{
return $this->coupon->{$attribute};
}
2023-06-11 12:14:03 +00:00
private function calculate()
{
2023-12-03 14:07:47 +00:00
return $this->couponCondition->getCalculatedValue($this->couponApplicableProductsTotalPrice());
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
private function couponApplicableProductsTotalPrice()
2023-06-11 12:14:03 +00:00
{
return $this->couponApplicableProducts()->sum(function ($cartItem) {
2023-12-03 14:07:47 +00:00
return $cartItem->totalPrice()->amount();
2023-06-11 12:14:03 +00:00
});
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function couponApplicableProducts()
{
return $this->cart->items()->filter(function ($cartItem) {
return $this->inApplicableProducts($cartItem);
})->reject(function ($cartItem) {
return $this->inExcludedProducts($cartItem);
})->filter(function ($cartItem) {
return $this->inApplicableCategories($cartItem);
})->reject(function ($cartItem) {
return $this->inExcludedCategories($cartItem);
});
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function inApplicableProducts($cartItem)
{
if ($this->coupon->products->isEmpty()) {
return true;
}
return $this->coupon->products->contains($cartItem->product);
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function inExcludedProducts($cartItem)
{
if ($this->coupon->excludeProducts->isEmpty()) {
return false;
}
return $this->coupon->excludeProducts->contains($cartItem->product);
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function inApplicableCategories($cartItem)
{
if ($this->coupon->categories->isEmpty()) {
return true;
}
return $this->coupon->categories->intersect($cartItem->product->categories)->isNotEmpty();
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function inExcludedCategories($cartItem)
{
if ($this->coupon->excludeCategories->isEmpty()) {
return false;
}
return $this->coupon->excludeCategories->intersect($cartItem->product->categories)->isNotEmpty();
}
}