Completed
Branch master (734dbe)
by G
12:13
created

OrderDepositTaxesApplicator::apply()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
cc 6
nc 4
nop 2
1
<?php
2
3
namespace Gweb\SyliusProductDepositPlugin\Services;
4
5
use Sylius\Component\Addressing\Model\ZoneInterface;
6
use Sylius\Component\Core\Model\AdjustmentInterface;
7
use Sylius\Component\Core\Model\OrderInterface;
8
use Sylius\Component\Core\Model\OrderItemUnitInterface;
9
use Sylius\Component\Core\Taxation\Applicator\OrderTaxesApplicatorInterface;
10
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
11
use Sylius\Component\Resource\Repository\RepositoryInterface;
12
use Sylius\Component\Taxation\Calculator\CalculatorInterface;
13
14
/**
15
 * Apply deposit tax to order item units
16
 *
17
 * @author Gerd Weitenberg <[email protected]>
18
 */
19
final class OrderDepositTaxesApplicator implements OrderTaxesApplicatorInterface
20
{
21
    /**
22
     * @var CalculatorInterface
23
     */
24
    private $calculator;
25
26
    /**
27
     * @var AdjustmentFactoryInterface
28
     */
29
    private $adjustmentFactory;
30
31
    /**
32
     * @var RepositoryInterface
33
     */
34
    private $taxRateRepository;
35
36
    /**
37
     * @param CalculatorInterface $calculator
38
     * @param AdjustmentFactoryInterface $adjustmentFactory
39
     * @param RepositoryInterface $taxRateRepository
40
     */
41
    public function __construct(
42
        CalculatorInterface $calculator,
43
        AdjustmentFactoryInterface $adjustmentFactory,
44
        RepositoryInterface $taxRateRepository
45
    ) {
46
        $this->calculator = $calculator;
47
        $this->adjustmentFactory = $adjustmentFactory;
48
        $this->taxRateRepository = $taxRateRepository;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function apply(OrderInterface $order, ZoneInterface $zone): void
55
    {
56
57
        foreach ($order->getItems() as $item) {
58
59
            if (!$item->getVariant()->hasChannelDepositForChannel($order->getChannel())) {
60
                continue;
61
            }
62
63
            $depositPrice = $item->getVariant()->getChannelDepositForChannel($order->getChannel())->getPrice();
64
65
            $taxCategory = $item->getVariant()->getDepositTaxCategory();
66
            $taxRate = $this->taxRateRepository->findOneBy(['category' => $taxCategory, 'zone' => $zone]);
67
            if (null === $taxRate) {
68
                continue;
69
            }
70
71
            foreach ($item->getUnits() as $unit) {
72
                $taxAmount = $this->calculator->calculate($depositPrice, $taxRate);
73
                if (0.00 === $taxAmount) {
74
                    continue;
75
                }
76
77
                $this->addTaxAdjustment($unit, (int)$taxAmount, $taxRate->getLabel(), $taxRate->isIncludedInPrice());
78
            }
79
        }
80
    }
81
82
    /**
83
     * @param OrderItemUnitInterface $unit
84
     * @param int $taxAmount
85
     * @param string $label
86
     * @param bool $included
87
     */
88
    private function addTaxAdjustment(OrderItemUnitInterface $unit, int $taxAmount, string $label, bool $included): void
89
    {
90
        $unitTaxAdjustment = $this->adjustmentFactory->createWithData(
91
            AdjustmentInterface::TAX_ADJUSTMENT,
92
            $label,
93
            $taxAmount,
94
            $included
95
        );
96
        $unit->addAdjustment($unitTaxAdjustment);
97
    }
98
}
99