|
2 | 2 |
|
3 | 3 | namespace Drupal\Tests\commerce_tax\Kernel\Plugin\Commerce\TaxType; |
4 | 4 |
|
| 5 | +use Drupal\commerce_order\Adjustment; |
5 | 6 | use Drupal\commerce_order\Entity\Order; |
6 | 7 | use Drupal\commerce_order\Entity\OrderItem; |
7 | 8 | use Drupal\commerce_order\Entity\OrderItemType; |
@@ -188,6 +189,116 @@ public function testApplication() { |
188 | 189 | $this->assertEquals(new Price('8.61', 'USD'), $order_item->getUnitPrice()); |
189 | 190 | } |
190 | 191 |
|
| 192 | + /** |
| 193 | + * @covers ::apply |
| 194 | + */ |
| 195 | + public function testDiscountedPrices() { |
| 196 | + // Tax-inclusive prices + display-inclusive taxes. |
| 197 | + // A 10.33 USD price with a 1.00 USD discount should have a 9.33 USD total. |
| 198 | + $order = $this->buildOrder('RS', 'RS', [], TRUE); |
| 199 | + $order_items = $order->getItems(); |
| 200 | + $order_item = reset($order_items); |
| 201 | + $order_item->addAdjustment(new Adjustment([ |
| 202 | + 'type' => 'promotion', |
| 203 | + 'label' => t('Discount'), |
| 204 | + 'amount' => new Price('-1', 'USD'), |
| 205 | + ])); |
| 206 | + $this->plugin->apply($order); |
| 207 | + $order_items = $order->getItems(); |
| 208 | + $order_item = reset($order_items); |
| 209 | + |
| 210 | + $adjustments = $order->collectAdjustments(); |
| 211 | + $tax_adjustment = end($adjustments); |
| 212 | + $this->assertCount(2, $adjustments); |
| 213 | + $this->assertEquals('tax', $tax_adjustment->getType()); |
| 214 | + $this->assertEquals(t('VAT'), $tax_adjustment->getLabel()); |
| 215 | + $this->assertEquals(new Price('1.56', 'USD'), $tax_adjustment->getAmount()); |
| 216 | + $this->assertEquals('0.2', $tax_adjustment->getPercentage()); |
| 217 | + $this->assertEquals(new Price('10.33', 'USD'), $order_item->getUnitPrice()); |
| 218 | + $this->assertEquals(new Price('9.33', 'USD'), $order_item->getAdjustedUnitPrice()); |
| 219 | + |
| 220 | + // Non-tax-inclusive prices + display-inclusive taxes. |
| 221 | + // A 10.33 USD price is 12.40 USD with 20% tax included. |
| 222 | + // A 1.00 USD discount should result in a 11.40 USD total. |
| 223 | + $order = $this->buildOrder('RS', 'RS', []); |
| 224 | + $order_items = $order->getItems(); |
| 225 | + $order_item = reset($order_items); |
| 226 | + $order_item->addAdjustment(new Adjustment([ |
| 227 | + 'type' => 'promotion', |
| 228 | + 'label' => t('Discount'), |
| 229 | + 'amount' => new Price('-1', 'USD'), |
| 230 | + ])); |
| 231 | + $this->plugin->apply($order); |
| 232 | + $order_items = $order->getItems(); |
| 233 | + $order_item = reset($order_items); |
| 234 | + |
| 235 | + $adjustments = $order->collectAdjustments(); |
| 236 | + $tax_adjustment = end($adjustments); |
| 237 | + $this->assertCount(2, $adjustments); |
| 238 | + $this->assertEquals('tax', $tax_adjustment->getType()); |
| 239 | + $this->assertEquals(t('VAT'), $tax_adjustment->getLabel()); |
| 240 | + $this->assertEquals(new Price('2.28', 'USD'), $tax_adjustment->getAmount()); |
| 241 | + $this->assertEquals('0.2', $tax_adjustment->getPercentage()); |
| 242 | + $this->assertEquals(new Price('12.40', 'USD'), $order_item->getUnitPrice()); |
| 243 | + $this->assertEquals(new Price('11.40', 'USD'), $order_item->getAdjustedUnitPrice()); |
| 244 | + |
| 245 | + // Non-tax-inclusive prices + non-display-inclusive taxes. |
| 246 | + // A 10.33 USD price with a 1.00 USD discount is 9.33 USD. |
| 247 | + // And 9.33 USD plus 20% tax is 11.20 USD. |
| 248 | + $configuration = $this->plugin->getConfiguration(); |
| 249 | + $configuration['display_inclusive'] = FALSE; |
| 250 | + $this->plugin->setConfiguration($configuration); |
| 251 | + $order = $this->buildOrder('RS', 'RS', []); |
| 252 | + $order_items = $order->getItems(); |
| 253 | + $order_item = reset($order_items); |
| 254 | + $order_item->addAdjustment(new Adjustment([ |
| 255 | + 'type' => 'promotion', |
| 256 | + 'label' => t('Discount'), |
| 257 | + 'amount' => new Price('-1', 'USD'), |
| 258 | + ])); |
| 259 | + $this->plugin->apply($order); |
| 260 | + $order_items = $order->getItems(); |
| 261 | + $order_item = reset($order_items); |
| 262 | + |
| 263 | + $adjustments = $order->collectAdjustments(); |
| 264 | + $tax_adjustment = end($adjustments); |
| 265 | + $this->assertCount(2, $adjustments); |
| 266 | + $this->assertEquals('tax', $tax_adjustment->getType()); |
| 267 | + $this->assertEquals(t('VAT'), $tax_adjustment->getLabel()); |
| 268 | + $this->assertEquals(new Price('1.87', 'USD'), $tax_adjustment->getAmount()); |
| 269 | + $this->assertEquals('0.2', $tax_adjustment->getPercentage()); |
| 270 | + $this->assertEquals(new Price('10.33', 'USD'), $order_item->getUnitPrice()); |
| 271 | + $this->assertEquals(new Price('11.20', 'USD'), $order_item->getAdjustedUnitPrice()); |
| 272 | + |
| 273 | + // Tax-inclusive prices + non-display-inclusive taxes. |
| 274 | + // A 10.33 USD price is 8.61 USD once the 20% tax is removed. |
| 275 | + // A 1.00 USD discount gives 7.61 USD + 20% VAT -> 8.88 USD. |
| 276 | + $configuration = $this->plugin->getConfiguration(); |
| 277 | + $configuration['display_inclusive'] = FALSE; |
| 278 | + $this->plugin->setConfiguration($configuration); |
| 279 | + $order = $this->buildOrder('RS', 'RS', [], TRUE); |
| 280 | + $order_items = $order->getItems(); |
| 281 | + $order_item = reset($order_items); |
| 282 | + $order_item->addAdjustment(new Adjustment([ |
| 283 | + 'type' => 'promotion', |
| 284 | + 'label' => t('Discount'), |
| 285 | + 'amount' => new Price('-1', 'USD'), |
| 286 | + ])); |
| 287 | + $this->plugin->apply($order); |
| 288 | + $order_items = $order->getItems(); |
| 289 | + $order_item = reset($order_items); |
| 290 | + |
| 291 | + $adjustments = $order->collectAdjustments(); |
| 292 | + $tax_adjustment = end($adjustments); |
| 293 | + $this->assertCount(2, $adjustments); |
| 294 | + $this->assertEquals('tax', $tax_adjustment->getType()); |
| 295 | + $this->assertEquals(t('VAT'), $tax_adjustment->getLabel()); |
| 296 | + $this->assertEquals(new Price('1.27', 'USD'), $tax_adjustment->getAmount()); |
| 297 | + $this->assertEquals('0.2', $tax_adjustment->getPercentage()); |
| 298 | + $this->assertEquals(new Price('8.61', 'USD'), $order_item->getUnitPrice()); |
| 299 | + $this->assertEquals(new Price('8.88', 'USD'), $order_item->getAdjustedUnitPrice()); |
| 300 | + } |
| 301 | + |
191 | 302 | /** |
192 | 303 | * @covers ::apply |
193 | 304 | */ |
|
0 commit comments