Skip to content

Commit 5b36239

Browse files
committed
Issue #2901178: Add TaxRatePercentage::calculateTaxAmount()
1 parent 6571386 commit 5b36239

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

modules/tax/src/Plugin/Commerce/TaxType/LocalTaxTypeBase.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Drupal\commerce_order\Adjustment;
77
use Drupal\commerce_order\Entity\OrderInterface;
88
use Drupal\commerce_order\Entity\OrderItemInterface;
9-
use Drupal\commerce_price\Calculator;
109
use Drupal\commerce_price\RounderInterface;
1110
use Drupal\commerce_store\Entity\StoreInterface;
1211
use Drupal\commerce_tax\TaxZone;
@@ -147,12 +146,8 @@ public function apply(OrderInterface $order) {
147146
foreach ($rates as $zone_id => $rate) {
148147
$zone = $zones[$zone_id];
149148
$unit_price = $order_item->getUnitPrice();
150-
$percentage = $rate->getPercentage()->getNumber();
151-
$tax_amount = $unit_price->multiply($percentage);
152-
if ($prices_include_tax) {
153-
$divisor = Calculator::add('1', $percentage);
154-
$tax_amount = $tax_amount->divide($divisor);
155-
}
149+
$percentage = $rate->getPercentage();
150+
$tax_amount = $percentage->calculateTaxAmount($unit_price, $prices_include_tax);
156151
if ($this->shouldRound()) {
157152
$tax_amount = $this->rounder->round($tax_amount);
158153
}
@@ -169,7 +164,7 @@ public function apply(OrderInterface $order) {
169164
'type' => 'tax',
170165
'label' => $zone->getDisplayLabel(),
171166
'amount' => $negate ? $tax_amount->multiply('-1') : $tax_amount,
172-
'percentage' => $percentage,
167+
'percentage' => $percentage->getNumber(),
173168
'source_id' => $this->entityId . '|' . $zone->getId() . '|' . $rate->getId(),
174169
'included' => !$negate && $this->isDisplayInclusive(),
175170
]));

modules/tax/src/TaxRatePercentage.php

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Drupal\commerce_tax;
44

5+
use Drupal\commerce_price\Calculator;
6+
use Drupal\commerce_price\Price;
57
use Drupal\Core\Datetime\DrupalDateTime;
68

79
/**
@@ -108,4 +110,24 @@ public function getEndDate() {
108110
return $this->endDate;
109111
}
110112

113+
/**
114+
* Calculates the tax amount for the given price.
115+
*
116+
* @param \Drupal\commerce_price\Price $price
117+
* The price.
118+
* @param bool $included
119+
* Whether tax is already included in the price.
120+
*
121+
* @return \Drupal\commerce_price\Price
122+
* The unrounded tax amount.
123+
*/
124+
public function calculateTaxAmount(Price $price, $included = FALSE) {
125+
$tax_amount = $price->multiply($this->number);
126+
if ($included) {
127+
$divisor = Calculator::add('1', $this->number);
128+
$tax_amount = $tax_amount->divide($divisor);
129+
}
130+
return $tax_amount;
131+
}
132+
111133
}

modules/tax/tests/src/Kernel/TaxRatePercentageTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\Tests\commerce_tax\Kernel;
44

5+
use Drupal\commerce_price\Price;
56
use Drupal\commerce_tax\TaxRatePercentage;
67
use Drupal\Core\Datetime\DrupalDateTime;
78
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
@@ -60,4 +61,21 @@ public function testValid() {
6061
$this->assertEquals(new DrupalDateTime($definition['end_date']), $percentage->getEndDate());
6162
}
6263

64+
/**
65+
* @covers ::calculateTaxAmount
66+
*/
67+
public function testCalculation() {
68+
$definition = [
69+
'number' => '0.20',
70+
'start_date' => '2012-01-01',
71+
];
72+
$percentage = new TaxRatePercentage($definition);
73+
74+
$tax_amount = $percentage->calculateTaxAmount(new Price('12', 'USD'), FALSE);
75+
$this->assertEquals(new Price('2.4', 'USD'), $tax_amount);
76+
77+
$tax_amount = $percentage->calculateTaxAmount(new Price('12', 'USD'), TRUE);
78+
$this->assertEquals(new Price('2', 'USD'), $tax_amount);
79+
}
80+
6381
}

0 commit comments

Comments
 (0)