Completed
Pull Request — experimental/sf (#3412)
by Kentaro
14:51 queued 07:29
created

TaxProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 1
rs 9.9666
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 725
    public function __construct(
51
        EntityManagerInterface $entityManager,
52
        TaxRuleRepository $taxRuleRepository,
53
        TaxRuleService $taxRuleService
54
    ) {
55 725
        $this->entityManager = $entityManager;
56 725
        $this->taxRuleRepository = $taxRuleRepository;
57 725
        $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 205
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
67
    {
68 205
        if (!$itemHolder instanceof Order) {
69
            return;
70
        }
71
72 205
        foreach ($itemHolder->getOrderItems() as $item) {
73
            // 明細種別に応じて税区分, 税表示区分を設定する,
74 205
            $OrderItemType = $item->getOrderItemType();
75
76 205
            if (!$item->getTaxType()) {
77 53
                $item->setTaxType($this->getTaxType($OrderItemType));
78
            }
79 205
            if (!$item->getTaxDisplayType()) {
80 53
                $item->setTaxDisplayType($this->getTaxDisplayType($OrderItemType));
81
            }
82
83
            // 税区分: 非課税, 不課税
84 205
            if ($item->getTaxType()->getId() != TaxType::TAXATION) {
85 152
                $item->setTax(0);
86 152
                $item->setTaxRate(0);
87 152
                $item->setRoundingType(null);
88 152
                $item->setTaxRuleId(null);
89
90 152
                return;
91
            }
92
93 205
            if ($item->getTaxRuleId()) {
94 19
                $TaxRule = $this->taxRuleRepository->find($item->getTaxRuleId());
95
            } else {
96 205
                $TaxRule = $this->taxRuleRepository->getByRule($item->getProduct(), $item->getProductClass());
97
            }
98
99
            // 税込表示の場合は, priceが税込金額のため割り戻す.
100 205
            if ($item->getTaxDisplayType()->getId() == TaxDisplayType::INCLUDED) {
101 203
                $tax = $this->taxRuleService->calcTaxIncluded(
102 203
                    $item->getPrice(), $TaxRule->getTaxRate(), $TaxRule->getRoundingType()->getId(),
103 203
                    $TaxRule->getTaxAdjust());
104
            } else {
105 205
                $tax = $this->taxRuleService->calcTax(
106 205
                    $item->getPrice(), $TaxRule->getTaxRate(), $TaxRule->getRoundingType()->getId(),
107 205
                    $TaxRule->getTaxAdjust());
108
            }
109
110 205
            $item->setTax($tax);
111 205
            $item->setTaxRate($TaxRule->getTaxRate());
112 205
            $item->setRoundingType($TaxRule->getRoundingType());
113 205
            $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 53
        $TaxType = $OrderItemType == OrderItemType::POINT
137
            ? TaxType::NON_TAXABLE
138 53
            : TaxType::TAXATION;
139
140 53
        return $this->entityManager->find(TaxType::class, $TaxType);
141
    }
142
143
    /**
144
     * 税表示区分を取得する.
145
     *
146
     * - 商品: 税抜
147
     * - 送料: 税込
148
     * - 値引き: 税抜
149
     * - 手数料: 税込
150
     * - ポイント値引き: 税込
151
     *
152
     * @param $OrderItemType
153
     *
154
     * @return TaxType
155
     */
156 53
    protected function getTaxDisplayType($OrderItemType)
157
    {
158 53
        if ($OrderItemType instanceof OrderItemType) {
159 53
            $OrderItemType = $OrderItemType->getId();
160
        }
161
162
        switch ($OrderItemType) {
163 53
            case OrderItemType::PRODUCT:
164 53
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
165
            case OrderItemType::DELIVERY_FEE:
166
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
167
            case OrderItemType::DISCOUNT:
168
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
169
            case OrderItemType::CHARGE:
170
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
171
            case OrderItemType::POINT:
172
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
173
            default:
174
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
175
        }
176
    }
177
}
178