Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Shipping] make shipping calculation more independent from cart #1562

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
namespace CoreShop\Component\Core\Shipping\Calculator;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Core\Model\StoreInterface;
use CoreShop\Component\Registry\ServiceRegistryInterface;
use CoreShop\Component\Shipping\Calculator\CarrierPriceCalculatorInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface as BaseCarrierInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Shipping\Taxation\TaxCalculationStrategyInterface;
use CoreShop\Component\Store\Model\StoreAwareInterface;
use CoreShop\Component\Taxation\Model\TaxItemInterface;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -57,9 +57,9 @@ public function getPrice(
$withTax = true
) {
/**
* @var CartInterface $shippable
* @var StoreAwareInterface $shippable
*/
Assert::isInstanceOf($shippable, CartInterface::class);
Assert::isInstanceOf($shippable, StoreAwareInterface::class);

$store = $shippable->getStore();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
namespace CoreShop\Component\Core\Shipping\Taxation;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Core\Model\CartItemInterface;
use CoreShop\Component\Core\Model\StoreInterface;
use CoreShop\Component\Core\Taxation\TaxCalculatorFactoryInterface;
use CoreShop\Component\Order\Distributor\ProportionalIntegerDistributorInterface;
use CoreShop\Component\Order\Model\PurchasableInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Shipping\Model\ShippableItemInterface;
use CoreShop\Component\Shipping\Taxation\TaxCalculationStrategyInterface;
use CoreShop\Component\Store\Model\StoreAwareInterface;
use CoreShop\Component\Taxation\Collector\TaxCollectorInterface;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -61,9 +62,9 @@ public function calculateShippingTax(
int $shippingAmountNet
): array {
/**
* @var CartInterface $shippable
* @var StoreAwareInterface $shippable
*/
Assert::isInstanceOf($shippable, CartInterface::class);
Assert::isInstanceOf($shippable, StoreAwareInterface::class);

$store = $shippable->getStore();

Expand All @@ -83,16 +84,22 @@ public function calculateShippingTax(
return $this->collectTaxes($address, $taxRules, $distributedAmount, $store->getUseGrossPrice());
}

private function collectCartItemsTaxRules(CartInterface $cart): array
private function collectCartItemsTaxRules(ShippableInterface $cart): array
{
$totalAmount = [];
$taxRules = [];

/**
* @var CartItemInterface $item
* @var ShippableItemInterface $item
*/
foreach ($cart->getItems() as $item) {
if ($item->getDigitalProduct() === true) {
if ($item instanceof \CoreShop\Component\Core\Model\CartItemInterface &&
$item->getDigitalProduct() === true
) {
continue;
}

if (!$item->getProduct() instanceof PurchasableInterface) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
namespace CoreShop\Component\Core\Shipping\Taxation;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Core\Model\StoreInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface;
use CoreShop\Component\Core\Model\CarrierInterface as CoreCarrierInterface;
use CoreShop\Component\Core\Taxation\TaxCalculatorFactoryInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Shipping\Taxation\TaxCalculationStrategyInterface;
use CoreShop\Component\Store\Model\StoreAwareInterface;
use CoreShop\Component\Taxation\Calculator\TaxCalculatorInterface;
use CoreShop\Component\Taxation\Collector\TaxCollectorInterface;
use CoreShop\Component\Taxation\Model\TaxRuleGroup;
Expand Down Expand Up @@ -59,9 +59,9 @@ public function calculateShippingTax(
int $shippingAmountNet
) {
/**
* @var CartInterface $shippable
* @var StoreAwareInterface $shippable
*/
Assert::isInstanceOf($shippable, CartInterface::class);
Assert::isInstanceOf($shippable, StoreAwareInterface::class);

$store = $shippable->getStore();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CoreShop\Component\Rule\Condition\RuleValidationProcessorInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Shipping\Model\ShippingRuleInterface;

class CarrierShippingRuleChecker implements CarrierShippingRuleCheckerInterface
{
Expand Down Expand Up @@ -44,15 +45,17 @@ public function isShippingRuleValid(CarrierInterface $carrier, ShippableInterfac
}

foreach ($shippingRules as $rule) {
$isValid = $this->ruleValidationProcessor->isValid($carrier, $rule->getShippingRule(), [
$isValid = $this->ruleValidationProcessor->isValid($carrier, $rule instanceof ShippingRuleInterface ? $rule : $rule->getShippingRule(), [
$carrier,
'shippable' => $shippable,
'address' => $address,
]);

if ($isValid === false && $rule->getStopPropagation() === true) {
return false;
} elseif ($isValid === true) {
}

if ($isValid === true) {
return $rule;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/CoreShop/Component/Shipping/Model/ShippableItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@

interface ShippableItemInterface
{
/**
* @param bool $withTax
* @return int
*/
public function getTotal(bool $withTax = true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is actually a BC break. In Core currency only implemented from CartItemInterface so there it is available. Anyone against this break?


/**
* @param int $total
* @param bool $withTax
*/
public function setTotal(int $total, bool $withTax = true);

/**
* @return float
*/
Expand Down