Skip to content

Commit

Permalink
Merge pull request #5066 from Zales0123/remove-adjustments-before-tax…
Browse files Browse the repository at this point in the history
…es-and-promotion-application

[Core] Remove promotion and tax adjustments before recalculation
  • Loading branch information
Paweł Jędrzejewski committed May 20, 2016
2 parents b109586 + 08bc640 commit e23a0f8
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Expand Up @@ -40,6 +40,8 @@
<parameter key="sylius.order_processing.prices_recalculator.class">Sylius\Component\Core\OrderProcessing\PricesRecalculator</parameter>
<parameter key="sylius.order_processing.order_recalculator.class">Sylius\Component\Core\OrderProcessing\OrderRecalculator</parameter>

<parameter key="sylius.adjustments_remover.class">Sylius\Component\Core\Remover\AdjustmentsRemover</parameter>

<parameter key="sylius.taxation.order_shipment_taxes_applicator.class">Sylius\Component\Core\Taxation\Applicator\OrderShipmentTaxesApplicator</parameter>
<parameter key="sylius.taxation.order_items_taxes_applicator.class">Sylius\Component\Core\Taxation\Applicator\OrderItemsTaxesApplicator</parameter>
<parameter key="sylius.taxation.order_item_units_taxes_applicator.class">Sylius\Component\Core\Taxation\Applicator\OrderItemUnitsTaxesApplicator</parameter>
Expand Down Expand Up @@ -314,6 +316,7 @@
<argument type="service" id="sylius.price_calculator" />
</service>
<service id="sylius.order_processing.order_recalculator" class="%sylius.order_processing.order_recalculator.class%">
<argument type="service" id="sylius.adjustments_remover" />
<argument type="service" id="sylius.taxation.order_taxes_processor" />
<argument type="service" id="sylius.order_processing.prices_recalculator" />
<argument type="service" id="sylius.promotion_processor" />
Expand All @@ -332,6 +335,8 @@
<argument type="service" id="sm.factory" />
</service>

<service id="sylius.adjustments_remover" class="%sylius.adjustments_remover.class%" />

<!-- listeners -->
<service id="sylius.listener.cart_blamer" class="%sylius.listener.cart_blamer.class%">
<argument type="service" id="sylius.manager.cart" />
Expand Down
11 changes: 11 additions & 0 deletions src/Sylius/Component/Core/OrderProcessing/OrderRecalculator.php
Expand Up @@ -12,14 +12,21 @@
namespace Sylius\Component\Core\OrderProcessing;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Remover\AdjustmentsRemoverInterface;
use Sylius\Component\Core\Taxation\Processor\OrderTaxesProcessorInterface;
use Sylius\Component\Promotion\Processor\PromotionProcessorInterface;

/**
* @author Jan Góralski <jan.goralski@lakion.com>
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class OrderRecalculator implements OrderRecalculatorInterface
{
/**
* @var AdjustmentsRemoverInterface
*/
private $adjustmentsRemover;

/**
* @var OrderTaxesProcessorInterface
*/
Expand All @@ -41,17 +48,20 @@ class OrderRecalculator implements OrderRecalculatorInterface
private $shippingChargesProcessor;

/**
* @param AdjustmentsRemoverInterface $adjustmentsRemover
* @param OrderTaxesProcessorInterface $taxesProcessor
* @param PricesRecalculatorInterface $pricesRecalculator
* @param PromotionProcessorInterface $promotionProcessor
* @param ShippingChargesProcessorInterface $shippingChargesProcessor
*/
public function __construct(
AdjustmentsRemoverInterface $adjustmentsRemover,
OrderTaxesProcessorInterface $taxesProcessor,
PricesRecalculatorInterface $pricesRecalculator,
PromotionProcessorInterface $promotionProcessor,
ShippingChargesProcessorInterface $shippingChargesProcessor
) {
$this->adjustmentsRemover = $adjustmentsRemover;
$this->taxesProcessor = $taxesProcessor;
$this->pricesRecalculator = $pricesRecalculator;
$this->promotionProcessor = $promotionProcessor;
Expand All @@ -65,6 +75,7 @@ public function __construct(
*/
public function recalculate(OrderInterface $order)
{
$this->adjustmentsRemover->removeFrom($order);
$this->pricesRecalculator->recalculate($order);
$this->shippingChargesProcessor->applyShippingCharges($order);
$this->promotionProcessor->process($order);
Expand Down
38 changes: 38 additions & 0 deletions src/Sylius/Component/Core/Remover/AdjustmentsRemover.php
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Component\Core\Remover;

use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemUnitInterface;

/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class AdjustmentsRemover implements AdjustmentsRemoverInterface
{
/**
* {@inheritdoc}
*/
public function removeFrom(OrderInterface $order)
{
$adjustmentsToRemove = [
AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT,
AdjustmentInterface::ORDER_ITEM_PROMOTION_ADJUSTMENT,
AdjustmentInterface::TAX_ADJUSTMENT
];

foreach ($adjustmentsToRemove as $type) {
$order->removeAdjustmentsRecursively($type);
}
}
}
25 changes: 25 additions & 0 deletions src/Sylius/Component/Core/Remover/AdjustmentsRemoverInterface.php
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Component\Core\Remover;

use Sylius\Component\Core\Model\OrderInterface;

/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
interface AdjustmentsRemoverInterface
{
/**
* @param OrderInterface $order
*/
public function removeFrom(OrderInterface $order);
}
Expand Up @@ -17,6 +17,7 @@
use Sylius\Component\Core\OrderProcessing\OrderRecalculatorInterface;
use Sylius\Component\Core\OrderProcessing\PricesRecalculatorInterface;
use Sylius\Component\Core\OrderProcessing\ShippingChargesProcessorInterface;
use Sylius\Component\Core\Remover\AdjustmentsRemoverInterface;
use Sylius\Component\Core\Taxation\Processor\OrderTaxesProcessorInterface;
use Sylius\Component\Promotion\Processor\PromotionProcessorInterface;

Expand All @@ -28,12 +29,19 @@
class OrderRecalculatorSpec extends ObjectBehavior
{
function let(
AdjustmentsRemoverInterface $adjustmentsRemover,
OrderTaxesProcessorInterface $taxesProcessor,
PricesRecalculatorInterface $pricesRecalculator,
PromotionProcessorInterface $promotionProcessor,
ShippingChargesProcessorInterface $shippingChargesProcessor
) {
$this->beConstructedWith($taxesProcessor, $pricesRecalculator, $promotionProcessor, $shippingChargesProcessor);
$this->beConstructedWith(
$adjustmentsRemover,
$taxesProcessor,
$pricesRecalculator,
$promotionProcessor,
$shippingChargesProcessor
);
}

function it_is_initializable()
Expand All @@ -47,13 +55,15 @@ function it_implements_order_recalculator_interface()
}

function it_recalculates_order_promotions_taxes_and_shipping_charges(
AdjustmentsRemoverInterface $adjustmentsRemover,
PromotionProcessorInterface $promotionProcessor,
PricesRecalculatorInterface $pricesRecalculator,
OrderTaxesProcessorInterface $taxesProcessor,
ShippingChargesProcessorInterface $shippingChargesProcessor,
OrderInterface $order
) {
$pricesRecalculator->recalculate($order);
$adjustmentsRemover->removeFrom($order)->shouldBeCalled();
$pricesRecalculator->recalculate($order)->shouldBeCalled();
$promotionProcessor->process($order)->shouldBeCalled();
$taxesProcessor->process($order)->shouldBeCalled();
$shippingChargesProcessor->applyShippingCharges($order)->shouldBeCalled();
Expand Down
46 changes: 46 additions & 0 deletions src/Sylius/Component/Core/spec/Remover/AdjustmentsRemoverSpec.php
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace spec\Sylius\Component\Core\Remover;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemUnitInterface;
use Sylius\Component\Core\Remover\AdjustmentsRemover;
use Sylius\Component\Core\Remover\AdjustmentsRemoverInterface;

/**
* @mixin AdjustmentsRemover
*
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class AdjustmentsRemoverSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Component\Core\Remover\AdjustmentsRemover');
}

function it_implements_adjustments_remover_interface()
{
$this->shouldImplement(AdjustmentsRemoverInterface::class);
}

function it_removes_adjustments_from_order_recursively(OrderInterface $order)
{
$order->removeAdjustmentsRecursively(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT)->shouldBeCalled();
$order->removeAdjustmentsRecursively(AdjustmentInterface::ORDER_ITEM_PROMOTION_ADJUSTMENT)->shouldBeCalled();
$order->removeAdjustmentsRecursively(AdjustmentInterface::TAX_ADJUSTMENT)->shouldBeCalled();

$this->removeFrom($order);
}
}
11 changes: 11 additions & 0 deletions src/Sylius/Component/Order/Model/Order.php
Expand Up @@ -460,6 +460,17 @@ public function removeAdjustments($type)
}
}

/**
* {@inheritdoc}
*/
public function removeAdjustmentsRecursively($type = null)
{
$this->removeAdjustments($type);
foreach ($this->items as $item) {
$item->removeAdjustmentsRecursively($type);
}
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Sylius/Component/Order/Model/OrderInterface.php
Expand Up @@ -158,4 +158,9 @@ public function getAdjustmentsRecursively($type = null);
* @return int
*/
public function getAdjustmentsTotalRecursively($type = null);

/**
* @param string|null $type
*/
public function removeAdjustmentsRecursively($type = null);
}
42 changes: 42 additions & 0 deletions src/Sylius/Component/Order/spec/Model/OrderSpec.php
Expand Up @@ -201,6 +201,48 @@ function it_removes_adjustments_properly(AdjustmentInterface $adjustment)
$this->hasAdjustment($adjustment)->shouldReturn(false);
}

function it_removes_adjustments_recursively_properly(
AdjustmentInterface $orderAdjustment,
OrderItemInterface $item
) {
$this->addAdjustment($orderAdjustment);
$this->addItem($item);

$item->removeAdjustmentsRecursively(null)->shouldBeCalled();

$this->removeAdjustmentsRecursively();

$this->hasAdjustment($orderAdjustment)->shouldReturn(false);
}

function it_removes_adjustments_recursively_by_type_properly(
AdjustmentInterface $orderPromotionAdjustment,
AdjustmentInterface $orderTaxAdjustment,
OrderItemInterface $item
) {
$orderPromotionAdjustment->getType()->willReturn('promotion');
$orderPromotionAdjustment->isNeutral()->willReturn(true);
$orderPromotionAdjustment->setAdjustable($this)->shouldBeCalled();
$orderPromotionAdjustment->isLocked()->willReturn(false);

$orderTaxAdjustment->getType()->willReturn('tax');
$orderTaxAdjustment->isNeutral()->willReturn(true);
$orderTaxAdjustment->setAdjustable($this)->shouldBeCalled();
$orderTaxAdjustment->isLocked()->willReturn(false);

$this->addAdjustment($orderPromotionAdjustment);
$this->addAdjustment($orderTaxAdjustment);
$this->addItem($item);

$item->removeAdjustmentsRecursively('tax')->shouldBeCalled();
$orderTaxAdjustment->setAdjustable(null)->shouldBeCalled();

$this->removeAdjustmentsRecursively('tax');

$this->hasAdjustment($orderPromotionAdjustment)->shouldReturn(true);
$this->hasAdjustment($orderTaxAdjustment)->shouldReturn(false);
}

function it_returns_adjustments_recursively(
AdjustmentInterface $orderAdjustment,
AdjustmentInterface $itemAdjustment1,
Expand Down

0 comments on commit e23a0f8

Please sign in to comment.