Passed
Push — master ( c34e6f...9d9530 )
by G
08:26
created

OrderDepositTaxesApplicator::apply()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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