TaxRuleService::roundByCalcRule()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 2
dl 0
loc 23
ccs 13
cts 13
cp 1
crap 4
rs 8.7972
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Service;
26
27
class TaxRuleService
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
28
{
29
    /**
30
     * @var \Eccube\Repository\TaxRuleRepository
31
     */
32
    private $taxRuleRepository;
33
34
    /**
35
     * __construct
36
     *
37
     * @param \Eccube\Repository\TaxRuleRepository $taxRuleRepository
38
     */
39 1088
    public function __construct(\Eccube\Repository\TaxRuleRepository $taxRuleRepository)
40
    {
41 1088
        $this->taxRuleRepository = $taxRuleRepository;
42
    }
43
44
    /**
45
     * 設定情報に基づいて税金の金額を返す
46
     *
47
     * @param  int                                    $price        計算対象の金額
48
     * @param  int|null|\Eccube\Entity\Product        $product      商品
49
     * @param  int|null|\Eccube\Entity\ProductClass   $productClass 商品規格
50
     * @param  int|null|\Eccube\Entity\Master\Pref    $pref         都道府県
51
     * @param  int|null|\Eccube\Entity\Master\Country $country      国
52
     * @return double                                 税金付与した金額
53
     */
54 341
    public function getTax($price, $product = null, $productClass = null, $pref = null, $country = null)
55
    {
56
        /* @var $TaxRule \Eccube\Entity\TaxRule */
57 341
        $TaxRule = $this->taxRuleRepository->getByRule($product, $productClass, $pref, $country);
58
59 341
        return $this->calcTax($price, $TaxRule->getTaxRate(), $TaxRule->getCalcRule()->getId(), $TaxRule->getTaxAdjust());
60
    }
61
62
    /**
63
     * calcIncTax
64
     *
65
     * @param  int                                    $price        計算対象の金額
66
     * @param  int|null|\Eccube\Entity\Product        $product      商品
67
     * @param  int|null|\Eccube\Entity\ProductClass   $productClass 商品規格
68
     * @param  int|null|\Eccube\Entity\Master\Pref    $pref         都道府県
69
     * @param  int|null|\Eccube\Entity\Master\Country $country      国
70
     * @return int
71
     */
72 340
    public function getPriceIncTax($price, $product = null, $productClass = null, $pref = null, $country = null)
73
    {
74 340
        return $price + $this->getTax($price, $product, $productClass, $pref, $country);
75
    }
76
77
    /**
78
     * 税金額を計算する
79
     *
80
     * @param  int    $price     計算対象の金額
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
81
     * @param  int    $taxRate   税率(%単位)
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
82
     * @param  int    $calcRule  端数処理
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
83
     * @param  int    $taxAdjust 調整額
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
84
     * @return double 税金額
85
     */
86 343
    public function calcTax($price, $taxRate, $calcRule, $taxAdjust = 0)
87
    {
88 343
        $tax = $price * $taxRate / 100;
89 343
        $roundTax = $this->roundByCalcRule($tax, $calcRule);
90
91 343
        return $roundTax + $taxAdjust;
92
    }
93
94
    /**
95
     * 課税規則に応じて端数処理を行う
96
     *
97
     * @param  float|integer $value    端数処理を行う数値
98
     * @param  integer       $calcRule 課税規則
99
     * @return double        端数処理後の数値
100
     */
101 347
    public function roundByCalcRule($value, $calcRule)
102
    {
103
        switch ($calcRule) {
104
            // 四捨五入
105 347
            case \Eccube\Entity\Master\Taxrule::ROUND:
106 344
                $ret = round($value);
107 344
                break;
108
            // 切り捨て
109 3
            case \Eccube\Entity\Master\Taxrule::FLOOR:
110 1
                $ret = floor($value);
111 1
                break;
112
            // 切り上げ
113 2
            case \Eccube\Entity\Master\Taxrule::CEIL:
114 1
                $ret = ceil($value);
115 1
                break;
116
            // デフォルト:切り上げ
117
            default:
118 1
                $ret = ceil($value);
119 1
                break;
120
        }
121
122 347
        return $ret;
123
    }
124
}
125