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

feature: calculate shipping tax using cart items #1283

Merged
merged 34 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f3318f2
add cartBasedTaxation Flag
lukadschaak Feb 13, 2020
ca63fb7
show cartBasedTaxation Flag in Admin Backend
lukadschaak Feb 13, 2020
0cd4cc8
WIP: handle cartBasedTaxation in shipping tax calculation
lukadschaak Feb 13, 2020
0ef26c4
change admin backend from checkbox to combo
lukadschaak Feb 20, 2020
be0e1d5
change shipping orm model
lukadschaak Feb 20, 2020
c459557
add shipping tax services
lukadschaak Feb 20, 2020
d552a77
add calculation via cart items implementation
lukadschaak Feb 20, 2020
e00c24b
add translations
lukadschaak Feb 21, 2020
4b15a5c
refactor implementation and fix little bugs
lukadschaak Feb 25, 2020
36d8317
modify test data. add a second taxrulegroup (tax rate: 10), add a fou…
lukadschaak Feb 25, 2020
16eea33
add tests for ShippingTaxationCartItems.php
lukadschaak Feb 25, 2020
60fb7e6
remove cartBasedTaxation
lukadschaak Feb 25, 2020
314527c
move ServiceRegistry to shipping-taxation.yml
lukadschaak Feb 25, 2020
3cfa764
use translation
lukadschaak Feb 25, 2020
ecf108a
fix typo
lukadschaak Feb 25, 2020
8d60972
add auto-tagging for interface
lukadschaak Feb 25, 2020
c9b455b
restore german translations
lukadschaak Feb 25, 2020
dbd8c8b
when altering the carrier table, make sure that currently working tax…
lukadschaak Feb 25, 2020
6a510ed
fix typo
lukadschaak Feb 25, 2020
25a5998
fix typo
lukadschaak Feb 25, 2020
ee678ef
make phpstan happy
lukadschaak Feb 26, 2020
2516081
add behat test for shipping tax calculation based on cart items
lukadschaak Feb 26, 2020
2f13c85
fix tests. Make tax strategy 'taxRule' as default
lukadschaak Feb 26, 2020
7ffee87
a tax rule must be chosen
lukadschaak Feb 26, 2020
3868491
fix Type changes
lukadschaak Feb 27, 2020
a410a1e
revert unit tests to master state
lukadschaak Feb 27, 2020
48fcca5
fix typo
lukadschaak Feb 27, 2020
22ef79a
remove unused service
lukadschaak Feb 27, 2020
d2ac4e5
rename (shipping)taxStrategy to (shipping)taxCalculationStrategy
lukadschaak Feb 27, 2020
d7532ec
remove comment
lukadschaak Feb 28, 2020
18c42a0
[Shipping] refactor tax-calculation strategy implementation
dpfaffenbauer Mar 2, 2020
ba7b221
[Shipping] process shipping tax-rate if more tax rates are used
dpfaffenbauer Mar 2, 2020
7d3725e
fix division by zero
dpfaffenbauer Mar 3, 2020
399cf4b
fix division by zero
dpfaffenbauer Mar 3, 2020
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
55 changes: 55 additions & 0 deletions features/cart/cart_shipping_tax_by_cart_items_gross.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@cart @cart_shipping_tax_rate
Feature: Use a different calculation strategy for shipping tax calculation
The tax calculation for the shipping can be based on the cart items
We create and store a cart with different products to
check the shipping taxes.

Background:
Given the site operates on a store in "Austria" with gross values
And the site has a currency "Euro" with iso "EUR"
And I am in country "Austria"
And the site has a tax rate "AT20" with "20%" rate
And the site has a tax rule group "AT20"
And the tax rule group has a tax rule for country "Austria" with tax rate "AT20"
And the site has a tax rate "AT10" with "10%" rate
And the site has a tax rule group "AT10"
And the tax rule group has a tax rule for country "Austria" with tax rate "AT10"
And the site has a carrier "Post"
And the carrier uses the tax calculation strategy "cartItems"
And adding a shipping rule named "post"
And the shipping rule is active
And the shipping rule has a action price of 10 in currency "EUR"
And the shipping rule belongs to carrier "Post"
And the site has a product "Book" priced at 1000
And the product has the tax rule group "AT10"
And the site has a product "Another Book" priced at 1000
And the product has the tax rule group "AT10"
And the site has a product "DVD" priced at 3000
And the product has the tax rule group "AT20"

Scenario: Create a new cart, add two products and the correct shipping should be applied
And I add the product "Book" to my cart
And I add the product "DVD" to my cart
Then the cart shipping should be "1000" including tax
And the cart shipping should be "852" excluding tax
And the loaded carts shipping tax rate should be "17.37"

Scenario: Create a new cart, add one product and the correct shipping should be applied
And I add the product "Book" to my cart
Then the cart shipping should be "1000" including tax
And the cart shipping should be "909" excluding tax
And the loaded carts shipping tax rate should be "10"

Scenario: Create a new cart, add two of the same product and the correct shipping should be applied
And I add the product "Book" to my cart
And I add the product "Book" to my cart
Then the cart shipping should be "1000" including tax
And the cart shipping should be "909" excluding tax
And the loaded carts shipping tax rate should be "10"

Scenario: Create a new cart, add two products with the same tax-rate and the correct shipping should be applied
And I add the product "Book" to my cart
And I add the product "Another Book" to my cart
Then the cart shipping should be "1000" including tax
And the cart shipping should be "909" excluding tax
And the loaded carts shipping tax rate should be "10"
55 changes: 55 additions & 0 deletions features/cart/cart_shipping_tax_by_cart_items_net.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@cart @cart_shipping_tax_rate
Feature: Use a different calculation strategy for shipping tax calculation
The tax calculation for the shipping can be based on the cart items
We create and store a cart with different products to
check the shipping taxes.

Background:
Given the site operates on a store in "Austria"
And the site has a currency "Euro" with iso "EUR"
And I am in country "Austria"
And the site has a tax rate "AT20" with "20%" rate
And the site has a tax rule group "AT20"
And the tax rule group has a tax rule for country "Austria" with tax rate "AT20"
And the site has a tax rate "AT10" with "10%" rate
And the site has a tax rule group "AT10"
And the tax rule group has a tax rule for country "Austria" with tax rate "AT10"
And the site has a carrier "Post"
And the carrier uses the tax calculation strategy "cartItems"
And adding a shipping rule named "post"
And the shipping rule is active
And the shipping rule has a action price of 10 in currency "EUR"
And the shipping rule belongs to carrier "Post"
And the site has a product "Book" priced at 1000
And the product has the tax rule group "AT10"
And the site has a product "Another Book" priced at 1000
And the product has the tax rule group "AT10"
And the site has a product "DVD" priced at 3000
And the product has the tax rule group "AT20"

Scenario: Create a new cart, add two products and the correct shipping should be applied
And I add the product "Book" to my cart
And I add the product "DVD" to my cart
Then the cart shipping should be "1176" including tax
And the cart shipping should be "1000" excluding tax
And the loaded carts shipping tax rate should be "17.6"

Scenario: Create a new cart, add one product and the correct shipping should be applied
And I add the product "Book" to my cart
Then the cart shipping should be "1100" including tax
And the cart shipping should be "1000" excluding tax
And the loaded carts shipping tax rate should be "10"

Scenario: Create a new cart, add two of the same product and the correct shipping should be applied
And I add the product "Book" to my cart
And I add the product "Book" to my cart
Then the cart shipping should be "1100" including tax
And the cart shipping should be "1000" excluding tax
And the loaded carts shipping tax rate should be "10"

Scenario: Create a new cart, add two products with the same tax-rate and the correct shipping should be applied
And I add the product "Book" to my cart
And I add the product "Another Book" to my cart
Then the cart shipping should be "1100" including tax
And the cart shipping should be "1000" excluding tax
And the loaded carts shipping tax rate should be "10"
26 changes: 26 additions & 0 deletions features/cart/cart_shipping_tax_rate_gross.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@cart @cart_shipping_tax_rate
Feature: Create a new cart
In order to know what taxes are applied
we store the shipping tax rate into the cart

Background:
Given the site operates on a store in "Austria" with gross values
And the site has a currency "Swiss Franc" with iso "CHF"
And I am in country "Austria"
And the site has a tax rate "CH" with "7.7%" rate
And the site has a tax rule group "CH"
And the tax rule group has a tax rule for country "Austria" with tax rate "CH"
And the site has a carrier "Post"
And the carrier has the tax rule group "CH"
And adding a shipping rule named "post"
And the shipping rule is active
And the shipping rule has a action price of 10 in currency "CHF"
And the shipping rule belongs to carrier "Post"
And the site has a product "Shoe" priced at 10000
And the product has the tax rule group "CH"

Scenario: Create a new cart, add a product and shipping should be applied
And I add the product "Shoe" to my cart
Then the cart shipping should be "929" excluding tax
And the cart shipping should be "1000" including tax
And the loaded carts shipping tax rate should be "7.7"
9 changes: 9 additions & 0 deletions src/CoreShop/Behat/Context/Setup/ShippingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,14 @@ public function theShippingRuleHasADiscountPercentAction(ShippingRuleInterface $
]));
}

/**
* @Given /^the (carrier) uses the tax calculation strategy "([^"]+)"$/
*/
public function theCarrierUsedTheTaxCalculationStrategy(CarrierInterface $carrier, string $strategyKey)
{
$carrier->setTaxCalculationStrategy($strategyKey);
}

/**
* @param string $name
*/
Expand All @@ -561,6 +569,7 @@ private function createCarrier($name)
$carrier = $this->carrierFactory->createNew();
$carrier->setIdentifier($name);
$carrier->setTitle($name, 'en');
$carrier->setTaxCalculationStrategy('taxRule');

if ($this->sharedStorage->has('store')) {
$carrier->addStore($this->sharedStorage->get('store'));
Expand Down
2 changes: 1 addition & 1 deletion src/CoreShop/Behat/Context/Transform/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(

/**
* @Transform /^my cart/
* @Transform /^cart(?:|s)/
* @Transform /^cart(?:s)/
*/
public function cart()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace CoreShop\Bundle\CoreBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Pimcore\Migrations\Migration\AbstractPimcoreMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20200213132916 extends AbstractPimcoreMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$this->addSql('ALTER TABLE coreshop_carrier ADD taxCalculationStrategy VARCHAR(255) DEFAULT NULL AFTER logo;');
$this->addSql("UPDATE coreshop_carrier SET `taxCalculationStrategy` = 'taxRule' WHERE `taxRuleGroupId` IS NOT NULL;");
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// do nothing due to potential data loss
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ CoreShop\Component\Core\Model\Carrier:
joinColumn:
name: taxRuleGroupId
referencedColumnName: id
onDelete: 'SET NULL'
onDelete: 'SET NULL'
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ imports:
- { resource: "services/validators.yml" }
- { resource: "services/product-store-values.yml" }
- { resource: "services/menu.yml" }
- { resource: "services/shipping-taxation.yml" }

services:
_defaults:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ services:
arguments:
- '@coreshop.collector.taxes'
- '@coreshop.provider.store_based_default_address_provider'
- '@coreshop.taxation.factory.tax_calculator'
- '@coreshop.registry.shipping.tax_calculation_strategies'
tags:
- { name: coreshop.cart_processor, priority: 350 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
services:
_defaults:
public: true

coreshop.shipping.tax_calculation_strategy.tax_rule: '@CoreShop\Component\Core\Shipping\Taxation\TaxCalculationStrategyTaxRule'
CoreShop\Component\Core\Shipping\Taxation\TaxCalculationStrategyTaxRule:
arguments:
- '@coreshop.collector.taxes'
- '@coreshop.taxation.factory.tax_calculator'
tags:
- { name: coreshop.shipping.tax_calculation_strategy, type: taxRule }

coreshop.shipping.tax_calculation_strategy.cart_items: '@CoreShop\Component\Core\Shipping\Taxation\TaxCalculationStrategyCartItems'
CoreShop\Component\Core\Shipping\Taxation\TaxCalculationStrategyCartItems:
arguments:
- '@coreshop.collector.taxes'
- '@coreshop.taxation.factory.tax_calculator'
- '@coreshop.proportional_integer_distributor'
tags:
- { name: coreshop.shipping.tax_calculation_strategy, type: cartItems }
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ services:
class: CoreShop\Component\Core\Shipping\Calculator\TaxedCarrierPriceRuleCalculator
arguments:
- '@coreshop.carrier.price_calculator.default'
- '@coreshop.taxation.factory.tax_calculator'
- '@coreshop.taxation.tax_applicator'
- '@coreshop.registry.shipping.tax_calculation_strategies'

coreshop.carrier.cart.store_based_resolver:
class: CoreShop\Component\Core\Shipping\Discover\StoreBasedShippableCarriersDiscovery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ coreshop.store.item = Class.create(coreshop.store.item, {
{
xtype: 'coreshop.country',
fieldLabel: t('coreshop_base_country'),
name: 'baseCountry',
value: this.data.baseCountry,
store: {
proxy: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2020 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\ShippingBundle\Controller;

use CoreShop\Bundle\ResourceBundle\Controller\ResourceController;

class CarrierController extends ResourceController
{
public function getConfigAction()
{
$strategies = $this->getParameter('coreshop.shipping.tax_calculation_strategies');

$convertedStrategies = [];
foreach ($strategies as $strategy) {
$convertedStrategies[] = [
'value' => $strategy,
// key length has a maximum
'label' => 'coreshop_shipping_tax_strategy_' . $strategy
];
}

return $this->viewHandler->handle(
[
'success' => true,
'taxCalculationStrategies' => $convertedStrategies
]
);
}
}
2 changes: 2 additions & 0 deletions src/CoreShop/Bundle/ShippingBundle/CoreShopShippingBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use CoreShop\Bundle\ShippingBundle\DependencyInjection\Compiler\ShippingPriceCalculatorsPass;
use CoreShop\Bundle\ShippingBundle\DependencyInjection\Compiler\ShippingRuleActionPass;
use CoreShop\Bundle\ShippingBundle\DependencyInjection\Compiler\ShippingRuleConditionPass;
use CoreShop\Bundle\ShippingBundle\DependencyInjection\Compiler\ShippingTaxCalculationStrategyPass;
use Pimcore\HttpKernel\BundleCollection\BundleCollection;
use Symfony\Component\DependencyInjection\ContainerBuilder;

Expand All @@ -48,6 +49,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new ShippingRuleActionPass());
$container->addCompilerPass(new ShippingPriceCalculatorsPass());
$container->addCompilerPass(new CompositeShippableValidatorPass());
$container->addCompilerPass(new ShippingTaxCalculationStrategyPass());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2020 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\ShippingBundle\DependencyInjection\Compiler;

use CoreShop\Bundle\PimcoreBundle\DependencyInjection\Compiler\RegisterRegistryTypePass;

final class ShippingTaxCalculationStrategyPass extends RegisterRegistryTypePass
{
public const SHIPPING_TAX_STRATEGY_TAG = 'coreshop.shipping.tax_calculation_strategy';

public function __construct()
{
parent::__construct(
'coreshop.registry.shipping.tax_calculation_strategies',
'coreshop.form_registry.shipping.tax_calculation_strategies',
'coreshop.shipping.tax_calculation_strategies',
self::SHIPPING_TAX_STRATEGY_TAG
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

namespace CoreShop\Bundle\ShippingBundle\DependencyInjection;

use CoreShop\Bundle\ResourceBundle\Controller\ResourceController;
use CoreShop\Bundle\ResourceBundle\CoreShopResourceBundle;
use CoreShop\Bundle\RuleBundle\Doctrine\ORM\RuleRepository;
use CoreShop\Bundle\ShippingBundle\Controller\CarrierController;
use CoreShop\Bundle\ShippingBundle\Controller\ShippingRuleController;
use CoreShop\Bundle\ShippingBundle\Form\Type\CarrierTranslationType;
use CoreShop\Bundle\ShippingBundle\Form\Type\CarrierType;
Expand Down Expand Up @@ -80,7 +80,7 @@ private function addModelsSection(ArrayNodeDefinition $node)
->children()
->scalarNode('model')->defaultValue(Carrier::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(CarrierInterface::class)->cannotBeEmpty()->end()
->scalarNode('admin_controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('admin_controller')->defaultValue(CarrierController::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end()
->scalarNode('repository')->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(CarrierType::class)->cannotBeEmpty()->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
use CoreShop\Bundle\ShippingBundle\DependencyInjection\Compiler\ShippingPriceCalculatorsPass;
use CoreShop\Bundle\ShippingBundle\DependencyInjection\Compiler\ShippingRuleActionPass;
use CoreShop\Bundle\ShippingBundle\DependencyInjection\Compiler\ShippingRuleConditionPass;
use CoreShop\Bundle\ShippingBundle\DependencyInjection\Compiler\ShippingTaxCalculationStrategyPass;
use CoreShop\Component\Shipping\Calculator\CarrierPriceCalculatorInterface;
use CoreShop\Component\Shipping\Rule\Condition\ShippingConditionCheckerInterface;
use CoreShop\Component\Shipping\Rule\Processor\ShippingRuleActionProcessorInterface;
use CoreShop\Component\Shipping\Taxation\TaxCalculationStrategyInterface;
use CoreShop\Component\Shipping\Validator\ShippableCarrierValidatorInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
Expand Down Expand Up @@ -70,5 +72,9 @@ public function load(array $config, ContainerBuilder $container)
$container
->registerForAutoconfiguration(ShippingConditionCheckerInterface::class)
->addTag(ShippingRuleConditionPass::SHIPPING_RULE_CONDITION_TAG);

$container
->registerForAutoconfiguration(TaxCalculationStrategyInterface::class)
->addTag(ShippingTaxCalculationStrategyPass::SHIPPING_TAX_STRATEGY_TAG);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('trackingUrl', TextType::class)
->add('isFree', CheckboxType::class)
->add('logo', PimcoreAssetChoiceType::class)
->add('taxCalculationStrategy', ShippingTaxCalculationStrategyChoiceType::class)
->add('shippingRules', ShippingRuleGroupCollectionType::class)
->add('translations', ResourceTranslationsType::class, [
'entry_type' => CarrierTranslationType::class,
Expand Down
Loading