Completed
Pull Request — experimental/sf (#3420)
by chihiro
244:13 queued 235:15
created

TaxProcessor::process()   B

Complexity

Conditions 8
Paths 22

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 8.0025

Importance

Changes 0
Metric Value
cc 8
nc 22
nop 2
dl 0
loc 50
ccs 28
cts 29
cp 0.9655
crap 8.0025
rs 7.8464
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Service\PurchaseFlow\Processor;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Eccube\Entity\ItemHolderInterface;
18
use Eccube\Entity\Master\OrderItemType;
19
use Eccube\Entity\Master\TaxDisplayType;
20
use Eccube\Entity\Master\TaxType;
21
use Eccube\Entity\Order;
22
use Eccube\Repository\TaxRuleRepository;
23
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
24
use Eccube\Service\PurchaseFlow\PurchaseContext;
25
use Eccube\Service\TaxRuleService;
26
27
class TaxProcessor implements ItemHolderPreprocessor
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
28
{
29
    /**
30
     * @var EntityManagerInterface
31
     */
32
    protected $entityManager;
33
34
    /**
35
     * @var TaxRuleRepository
36
     */
37
    protected $taxRuleRepository;
38
39
    /**
40
     * @var TaxRuleService
41
     */
42
    protected $taxRuleService;
43
44
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$entityManager" missing
Loading history...
45
     * TaxProcessor constructor.
46
     *
47
     * @param TaxRuleRepository $taxRuleRepository
0 ignored issues
show
introduced by
Doc comment for parameter $taxRuleRepository does not match actual variable name $entityManager
Loading history...
48
     * @param TaxRuleService $taxRuleService
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
introduced by
Doc comment for parameter $taxRuleService does not match actual variable name $taxRuleRepository
Loading history...
49
     */
50 686
    public function __construct(
51
        EntityManagerInterface $entityManager,
52
        TaxRuleRepository $taxRuleRepository,
53
        TaxRuleService $taxRuleService
54
    ) {
55 686
        $this->entityManager = $entityManager;
56 686
        $this->taxRuleRepository = $taxRuleRepository;
57 686
        $this->taxRuleService = $taxRuleService;
58
    }
59
60
    /**
61
     * @param ItemHolderInterface $itemHolder
62
     * @param PurchaseContext $context
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
63
     *
64
     * @throws \Doctrine\ORM\NoResultException
65
     */
66 209
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
67
    {
68 209
        if (!$itemHolder instanceof Order) {
69
            return;
70
        }
71
72 209
        foreach ($itemHolder->getOrderItems() as $item) {
73
            // 明細種別に応じて税区分, 税表示区分を設定する,
74 209
            $OrderItemType = $item->getOrderItemType();
75
76 209
            if (!$item->getTaxType()) {
77 53
                $item->setTaxType($this->getTaxType($OrderItemType));
78
            }
79 209
            if (!$item->getTaxDisplayType()) {
80 53
                $item->setTaxDisplayType($this->getTaxDisplayType($OrderItemType));
81
            }
82
83
            // 税区分: 非課税, 不課税
84 209
            if ($item->getTaxType()->getId() != TaxType::TAXATION) {
85 156
                $item->setTax(0);
86 156
                $item->setTaxRate(0);
87 156
                $item->setRoundingType(null);
88 156
                $item->setTaxRuleId(null);
89
90 156
                return;
91
            }
92
93 209
            if ($item->getTaxRuleId()) {
94 19
                $TaxRule = $this->taxRuleRepository->find($item->getTaxRuleId());
95
            } else {
96 209
                $TaxRule = $this->taxRuleRepository->getByRule($item->getProduct(), $item->getProductClass());
97
            }
98
99
            // 税込表示の場合は, priceが税込金額のため割り戻す.
100 209
            if ($item->getTaxDisplayType()->getId() == TaxDisplayType::INCLUDED) {
101 207
                $tax = $this->taxRuleService->calcTaxIncluded(
102 207
                    $item->getPrice(), $TaxRule->getTaxRate(), $TaxRule->getRoundingType()->getId(),
103 207
                    $TaxRule->getTaxAdjust());
104
            } else {
105 209
                $tax = $this->taxRuleService->calcTax(
106 209
                    $item->getPrice(), $TaxRule->getTaxRate(), $TaxRule->getRoundingType()->getId(),
107 209
                    $TaxRule->getTaxAdjust());
108
            }
109
110 209
            $item->setTax($tax);
111 209
            $item->setTaxRate($TaxRule->getTaxRate());
112 209
            $item->setRoundingType($TaxRule->getRoundingType());
113 209
            $item->setTaxRuleId($TaxRule->getId());
114
        }
115
    }
116
117
    /**
118
     * 税区分を取得する.
119
     *
120
     * - 商品: 課税
121
     * - 送料: 課税
122
     * - 値引き: 課税
123
     * - 手数料: 課税
124
     * - ポイント値引き: 不課税
125
     *
126
     * @param $OrderItemType
127
     *
128
     * @return TaxType
129
     */
130 53
    protected function getTaxType($OrderItemType)
131
    {
132 53
        if ($OrderItemType instanceof OrderItemType) {
133 53
            $OrderItemType = $OrderItemType->getId();
134
        }
135
136
        switch ($OrderItemType) {
137 53
            case OrderItemType::PRODUCT:
138
            case OrderItemType::DELIVERY_FEE:
139
            case OrderItemType::DISCOUNT:
140
            case OrderItemType::CHARGE:
141
            default:
142 53
                return $this->entityManager->find(TaxType::class, TaxType::TAXATION);
143
        }
144
    }
145
146
    /**
147
     * 税表示区分を取得する.
148
     *
149
     * - 商品: 税抜
150
     * - 送料: 税込
151
     * - 値引き: 税抜
152
     * - 手数料: 税込
153
     * - ポイント値引き: 税込
154
     *
155
     * @param $OrderItemType
156
     *
157
     * @return TaxType
158
     */
159 53
    protected function getTaxDisplayType($OrderItemType)
160
    {
161 53
        if ($OrderItemType instanceof OrderItemType) {
162 53
            $OrderItemType = $OrderItemType->getId();
163
        }
164
165
        switch ($OrderItemType) {
166 53
            case OrderItemType::PRODUCT:
167 53
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
168
            case OrderItemType::DELIVERY_FEE:
169
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
170
            case OrderItemType::DISCOUNT:
171
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
172
            case OrderItemType::CHARGE:
173
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
174
            default:
175
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
176
        }
177
    }
178
}
179