|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Component\Core\Taxation; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Component\Addressing\Model\ZoneInterface; |
|
15
|
|
|
use Sylius\Component\Core\Model\AdjustmentInterface; |
|
16
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
17
|
|
|
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface; |
|
18
|
|
|
use Sylius\Component\Taxation\Calculator\CalculatorInterface; |
|
19
|
|
|
use Sylius\Component\Taxation\Resolver\TaxRateResolverInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Mateusz Zalewski <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class OrderShipmentTaxesByZoneApplicator implements OrderShipmentTaxesByZoneApplicatorInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var CalculatorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $calculator; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var AdjustmentFactoryInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $adjustmentFactory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var TaxRateResolverInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $taxRateResolver; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param CalculatorInterface $calculator |
|
43
|
|
|
* @param AdjustmentFactoryInterface $adjustmentFactory |
|
44
|
|
|
* @param TaxRateResolverInterface $taxRateResolver |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(CalculatorInterface $calculator, AdjustmentFactoryInterface $adjustmentFactory, TaxRateResolverInterface $taxRateResolver) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->calculator = $calculator; |
|
49
|
|
|
$this->adjustmentFactory = $adjustmentFactory; |
|
50
|
|
|
$this->taxRateResolver = $taxRateResolver; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function apply(OrderInterface $order, ZoneInterface $zone) |
|
57
|
|
|
{ |
|
58
|
|
|
$lastShipment = $order->getLastShipment(); |
|
59
|
|
|
if (!$lastShipment) { |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$shippingAdjustments = $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT); |
|
64
|
|
|
if ($shippingAdjustments->isEmpty()) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$taxRate = $this->taxRateResolver->resolve($lastShipment->getMethod(), array('zone' => $zone)); |
|
69
|
|
|
if (null === $taxRate) { |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$lastShippingAdjustment = $shippingAdjustments->last(); |
|
74
|
|
|
$taxAmount = $this->calculator->calculate($lastShippingAdjustment->getAmount(), $taxRate); |
|
75
|
|
|
|
|
76
|
|
|
$this->addAdjustment($order, $taxAmount, $taxRate->getLabel(), $taxRate->isIncludedInPrice()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param OrderInterface $order |
|
81
|
|
|
* @param int $taxAmount |
|
82
|
|
|
* @param string $label |
|
83
|
|
|
* @param bool $included |
|
84
|
|
|
*/ |
|
85
|
|
|
private function addAdjustment($order, $taxAmount, $label, $included) |
|
86
|
|
|
{ |
|
87
|
|
|
/** @var AdjustmentInterface $shippingTaxAdjustment */ |
|
88
|
|
|
$shippingTaxAdjustment = $this->adjustmentFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, $label, $taxAmount, $included); |
|
89
|
|
|
$order->addAdjustment($shippingTaxAdjustment); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|