Skip to content

Commit

Permalink
Merge pull request #96 in PLUG_OPEN/swagbackendorder from pt-9171/5.5…
Browse files Browse the repository at this point in the history
…/5.5-compatibility to master

* commit '72686507ba5bc7ea84ac8c52c05163d1aaf4c09e':
  PT-9171 - fix proportional tax calculation
  • Loading branch information
mitelg committed Aug 23, 2018
2 parents 98d8f33 + 7268650 commit c2daaaa
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public function calculate(PriceContext $context)

$result = new PriceResult();

$netPrice = round($this->currencyConverter->getCurrencyPrice($context->getPrice(), $context->getCurrencyFactor()), 3);
$result->setNet(round($netPrice, 2));
$netPrice = $this->currencyConverter->getCurrencyPrice($context->getPrice(), $context->getCurrencyFactor());
$result->setNet($netPrice);

$grossPrice = round($this->taxCalculation->getGrossPrice($netPrice, $context->getTaxRate()), 3);
$result->setGross(round($grossPrice, 2));
$grossPrice = $this->taxCalculation->getGrossPrice($netPrice, $context->getTaxRate());
$result->setGross($grossPrice);

$result->setTaxRate($context->getTaxRate());

Expand Down Expand Up @@ -101,11 +101,11 @@ public function calculatePrice($price, Tax $tax, ShopContextInterface $context)
$price = $price * $context->getCurrency()->getFactor();

if (!$customerGroup->displayGrossPrices()) {
return round($price, 3);
return $price;
}

$price = $price * (100 + $tax->getTax()) / 100;

return round($price, 3);
return $price;
}
}
14 changes: 10 additions & 4 deletions Components/PriceCalculation/Calculator/TotalPriceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class TotalPriceCalculator
/**
* @param PriceResult[] $positionPrices
* @param PriceResult $shippingPrice
* @param bool $isProportionalTaxCalculation
*
* @return TotalPricesResult
* @return TotalPricesResult TotalPricesResult
*/
public function calculate(array $positionPrices, PriceResult $shippingPrice)
public function calculate(array $positionPrices, PriceResult $shippingPrice, $isProportionalTaxCalculation = false)
{
$totalPrice = new TotalPricesResult();

Expand All @@ -31,7 +32,12 @@ public function calculate(array $positionPrices, PriceResult $shippingPrice)
$total = $this->getTotal($sum, $shippingPrice);
$totalPrice->setTotal($total);

$taxes = $this->getTotalTaxPrices(array_merge($positionPrices, [$shippingPrice]));
if (!$isProportionalTaxCalculation) {
$taxes = $this->getTotalTaxPrices(array_merge($positionPrices, [$shippingPrice]));
} else {
$taxes = $this->getTotalTaxPrices($positionPrices);
}

$totalPrice->setTaxes($taxes);

return $totalPrice;
Expand Down Expand Up @@ -87,7 +93,7 @@ private function getTotalTaxPrices(array $prices)
$taxes = [];

foreach ($prices as $price) {
$taxRate = (string) $price->getTaxRate();
$taxRate = (string)$price->getTaxRate();
if (!array_key_exists($taxRate, $taxes)) {
$taxes[$taxRate] = 0;
}
Expand Down
26 changes: 18 additions & 8 deletions Controllers/Backend/SwagBackendOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Shopware\Components\Model\ModelManager;
use Shopware\Components\Plugin\ConfigReader;
use Shopware\Models\Article\Detail;
Expand Down Expand Up @@ -451,11 +452,13 @@ public function calculateBasketAction()
$requestHydrator = $this->get('swag_backend_order.price_calculation.request_hydrator');
$requestStruct = $requestHydrator->hydrateFromRequest($this->Request()->getParams());

$config = $this->container->get('config');
$proportionalTaxCalculation = $config->get('proportionalTaxCalculation') && !$requestStruct->isTaxFree();

//Basket position price calculation
$positionPrices = [];
foreach ($requestStruct->getPositions() as &$position) {
foreach ($requestStruct->getPositions() as $position) {
$positionPrice = $this->getPositionPrice($position, $requestStruct);

$totalPositionPrice = new PriceResult();
$totalPositionPrice->setNet($this->getTotalPrice($positionPrice->getNet(), $position->getQuantity()));
$totalPositionPrice->setGross($this->getTotalPrice($positionPrice->getGross(), $position->getQuantity()));
Expand All @@ -480,14 +483,13 @@ public function calculateBasketAction()
$position->setTotal($this->getTotalPrice($position->getPrice(), $position->getQuantity()));
}
}
unset($position);

$dispatchPrice = $this->getShippingPrice($requestStruct);

/** @var TotalPriceCalculator $totalPriceCalculator */
$totalPriceCalculator = $this->get('swag_backend_order.price_calculation.total_price_calculator');
$totalPriceResult = $totalPriceCalculator->calculate($positionPrices, $dispatchPrice);
$result = $this->createBasketCalculationResult($totalPriceResult, $requestStruct);
$totalPriceResult = $totalPriceCalculator->calculate($positionPrices, $dispatchPrice, $proportionalTaxCalculation);
$result = $this->createBasketCalculationResult($totalPriceResult, $requestStruct, $proportionalTaxCalculation);
$result['isTaxFree'] = $requestStruct->isTaxFree();

/** @var DiscountCalculator $discountCalculator */
Expand Down Expand Up @@ -569,7 +571,7 @@ private function getListRequestParam()
*/
private function getTotalPrice($price, $quantity)
{
return $price * (float) $quantity;
return $price * $quantity;
}

/**
Expand All @@ -594,7 +596,8 @@ private function getAccountHolder($customerId)
*/
private function createBasketCalculationResult(
TotalPricesResult $totalPriceResult,
RequestStruct $requestStruct
RequestStruct $requestStruct,
$proportionalTaxCalculation
) {
$shippingCosts = $totalPriceResult->getShipping()->getRoundedGrossPrice();
$productSum = $totalPriceResult->getSum()->getRoundedGrossPrice();
Expand All @@ -615,7 +618,14 @@ private function createBasketCalculationResult(
$totalNetPrice = $totalPriceResult->getTotal()->getRoundedNetPrice();
$shippingCostsNet = $totalPriceResult->getShipping()->getRoundedNetPrice();

$proportionalTaxCalculation = $this->container->get('config')->get('proportionalTaxCalculation');
if ($proportionalTaxCalculation) {
$proportionalTaxCalculator = $this->container->get('shopware.cart.proportional_tax_calculator');
$tax = $proportionalTaxCalculator->calculate($shippingCosts, $requestStruct->getPositions(), true);
/** @var \Shopware\Components\Cart\Struct\Price $price */
foreach ($tax as $price) {
$totalPriceResult->addTax($price->getTaxRate(), $price->getTax());
}
}

return [
'totalWithoutTax' => $totalNetPrice,
Expand Down
38 changes: 19 additions & 19 deletions Tests/Functional/Controller/SwagBackendOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function testCalculateBasket()
$result = $view->getAssign('data');

$this->assertTrue($view->getAssign('success'));
$this->assertEquals(142.44, $result['totalWithoutTax']);
$this->assertEquals(142.43, $result['totalWithoutTax']);
$this->assertEquals(154.94, $result['sum']);
$this->assertEquals(16.4, $result['taxSum']);
$this->assertEquals(16.4, round($result['taxSum'], 1));
$this->assertEquals(59.99, $result['positions'][0]['price']);
$this->assertEquals(59.99, $result['positions'][0]['total']);
}
Expand Down Expand Up @@ -103,7 +103,7 @@ public function testBasketCalculationWithChangedDisplayNetFlag()
$result = $view->getAssign('data');

$this->assertTrue($view->getAssign('success'));
$this->assertEquals(50.41, $result['sum']);
$this->assertEquals(50.41, round($result['sum'], 2));

$this->assertEquals(3.90, $result['shippingCosts']);
$this->assertEquals(3.28, $result['shippingCostsNet']);
Expand All @@ -113,7 +113,7 @@ public function testBasketCalculationWithChangedDisplayNetFlag()
$this->assertEquals(63.89, $result['total']);
$this->assertEquals(53.69, $result['totalWithoutTax']);

$this->assertEquals(50.41, $result['positions'][0]['price']);
$this->assertEquals(50.41, round($result['positions'][0]['price'], 2));
}

public function testBasketCalculationWithChangedTaxfreeFlag()
Expand All @@ -130,7 +130,7 @@ public function testBasketCalculationWithChangedTaxfreeFlag()
$result = $view->getAssign('data');

$this->assertTrue($view->getAssign('success'));
$this->assertEquals(50.41, $result['sum']);
$this->assertEquals(50.41, round($result['sum'], 2));

$this->assertEquals(3.28, $result['shippingCosts']);
$this->assertEquals(3.28, $result['shippingCostsNet']);
Expand All @@ -140,7 +140,7 @@ public function testBasketCalculationWithChangedTaxfreeFlag()
$this->assertEquals(53.69, $result['total']);
$this->assertEquals(53.69, $result['totalWithoutTax']);

$this->assertEquals(50.41, $result['positions'][0]['price']);
$this->assertEquals(50.41, round($result['positions'][0]['price'], 2));
}

public function testBasketCalculationWithChangedCurrency()
Expand All @@ -157,18 +157,18 @@ public function testBasketCalculationWithChangedCurrency()
$result = $view->getAssign('data');

$this->assertTrue($view->getAssign('success'));
$this->assertEquals(271.09, $result['sum']);
$this->assertEquals(271.08, $result['sum']);

$this->assertEquals(5.31, $result['shippingCosts']);
$this->assertEquals(4.47, $result['shippingCostsNet']);

$this->assertEquals(41.68, $result['taxSum']);

$this->assertEquals(276.4, $result['total']);
$this->assertEquals(234.72, $result['totalWithoutTax']);
$this->assertEquals(276.39, $result['total']);
$this->assertEquals(234.71, $result['totalWithoutTax']);

$this->assertEquals(81.74, $result['positions'][0]['price']);
$this->assertEquals(245.22, $result['positions'][0]['total']);
$this->assertEquals(81.74, round($result['positions'][0]['price'], 2));
$this->assertEquals(245.21, round($result['positions'][0]['total'], 2));
}

public function testGetProduct()
Expand Down Expand Up @@ -269,7 +269,7 @@ public function test_getProduct_with_block_prices()
$blockPrices = $result['blockPrices'];
$expectedBlockPricesJSON = '{"1":{"net":0.84,"gross":1},"11":{"net":0.76,"gross":0.9},"21":{"net":0.67,"gross":0.8},"31":{"net":0.63,"gross":0.75},"41":{"net":0.59,"gross":0.7}}';
$this->assertEquals($expectedBlockPricesJSON, $blockPrices);
$this->assertEquals(0.904, $result['price']);
$this->assertEquals(0.9044, $result['price']);
}

public function testGetProductWithDisplayNet()
Expand Down Expand Up @@ -535,13 +535,13 @@ private function getDemoDataWithEmptyPositions()
private function getDemoDataWithChangedCurrency()
{
return [
'positions' => '[{"id":0,"create_backend_order_id":0,"mode":0,"articleId":2,"detailId":0,"articleNumber":"SW10002.1","articleName":"M\u00fcnsterl\u00e4nder Lagerkorn 32% 1,5 Liter","quantity":3,"statusId":0,"statusDescription":"","taxId":1,"taxRate":19,"taxDescription":"","inStock":-4,"price":"59.99","total":"179.97"},{"id":0,"create_backend_order_id":0,"mode":0,"articleId":272,"detailId":0,"articleNumber":"SW10239","articleName":"Spachtelmasse","quantity":1,"statusId":0,"statusDescription":"","taxId":4,"taxRate":7,"taxDescription":"","inStock":-1,"price":"18.99","total":"18.99"}]',
'shippingCosts' => 3.90,
'shippingCostsNet' => 3.28,
'displayNet' => 'false',
'oldCurrencyId' => '1',
'newCurrencyId' => '2',
] + $this->getDemoData();
'positions' => '[{"id":0,"create_backend_order_id":0,"mode":0,"articleId":2,"detailId":0,"articleNumber":"SW10002.1","articleName":"M\u00fcnsterl\u00e4nder Lagerkorn 32% 1,5 Liter","quantity":3,"statusId":0,"statusDescription":"","taxId":1,"taxRate":19,"taxDescription":"","inStock":-4,"price":"59.99","total":"179.97"},{"id":0,"create_backend_order_id":0,"mode":0,"articleId":272,"detailId":0,"articleNumber":"SW10239","articleName":"Spachtelmasse","quantity":1,"statusId":0,"statusDescription":"","taxId":4,"taxRate":7,"taxDescription":"","inStock":-1,"price":"18.99","total":"18.99"}]',
'shippingCosts' => 3.90,
'shippingCostsNet' => 3.28,
'displayNet' => 'false',
'oldCurrencyId' => '1',
'newCurrencyId' => '2',
] + $this->getDemoData();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function testCalculate()
$context = new PriceContext(50.41, 19.00, true, false, 1.3625);

$price = $this->productPriceCalculator->calculate($context);
$this->assertEquals(68.68, $price->getNet());
$this->assertEquals(81.73, $price->getGross());
$this->assertEquals(68.68, round($price->getNet(), 2));
$this->assertEquals(81.73, round($price->getGross(), 2));
}

public function testCalculateBasePriceFromGrossPriceWithCurrencyFactor()
Expand Down

0 comments on commit c2daaaa

Please sign in to comment.