¨4.0.1¨
This commit is contained in:
@@ -11,6 +11,7 @@ class CartTax implements JsonSerializable
|
||||
private $taxRate;
|
||||
private $taxCondition;
|
||||
|
||||
|
||||
public function __construct($cart, $taxRate, $taxCondition)
|
||||
{
|
||||
$this->cart = $cart;
|
||||
@@ -18,33 +19,60 @@ class CartTax implements JsonSerializable
|
||||
$this->taxCondition = $taxCondition;
|
||||
}
|
||||
|
||||
|
||||
public function id()
|
||||
{
|
||||
return $this->taxRate->id;
|
||||
}
|
||||
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return json_encode($this->jsonSerialize());
|
||||
}
|
||||
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->name(),
|
||||
'amount' => $this->amount(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function name()
|
||||
{
|
||||
return $this->taxRate->name;
|
||||
}
|
||||
|
||||
public function amount()
|
||||
|
||||
public function amount(): Money
|
||||
{
|
||||
return Money::inDefaultCurrency($this->calculate());
|
||||
}
|
||||
|
||||
|
||||
private function calculate()
|
||||
{
|
||||
return $this->taxCondition->getCalculatedValue($this->productsTotalPrice());
|
||||
return $this->taxCondition->getCalculatedValue($this->taxApplicableProductsTotalPrice());
|
||||
}
|
||||
|
||||
private function productsTotalPrice()
|
||||
|
||||
private function taxApplicableProductsTotalPrice()
|
||||
{
|
||||
return $this->taxApplicableProducts()->sum(function ($cartItem) {
|
||||
return $cartItem->total()->amount();
|
||||
return $cartItem->totalPrice()->amount();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private function taxApplicableProducts()
|
||||
{
|
||||
return $this->cart->items()->filter(function ($cartItem) {
|
||||
@@ -52,26 +80,9 @@ class CartTax implements JsonSerializable
|
||||
});
|
||||
}
|
||||
|
||||
private function hasMatchingTaxClass($cartItem)
|
||||
|
||||
private function hasMatchingTaxClass($cartItem): bool
|
||||
{
|
||||
return $cartItem->product->tax_class_id === $this->taxRate->tax_class_id;
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'name' => $this->name(),
|
||||
'amount' => $this->amount(),
|
||||
];
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return json_encode($this->jsonSerialize());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user