source: branches/version-2_13-dev/data/class/helper/SC_Helper_TaxRule.php @ 22927

Revision 22927, 12.3 KB checked in by Seasoft, 11 years ago (diff)

#2044 (無駄な処理を改善する for 2.13.0)

  • デバッグ出力の消し忘れであろう。
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 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 * 税規約を管理するヘルパークラス.
26 *
27 * @package Helper
28 * @author AMUAMU
29 * @version $Id:$
30 */
31class SC_Helper_TaxRule
32{
33    /**
34     * 設定情報に基づいて税金付与した金額を返す
35     *
36     * @param integer $price 計算対象の金額
37     * @return integer 税金付与した金額
38     */
39    function sfCalcIncTax($price, $product_id = 0, $product_class_id = 0, $pref_id =0, $country_id = 0)
40    {
41        return $price + SC_Helper_TaxRule_Ex::sfTax($price, $product_id, $product_class_id, $pref_id, $country_id);
42    }
43
44    /**
45     * 設定情報に基づいて税金の金額を返す
46     *
47     * @param integer $price 計算対象の金額
48     * @return integer 税金した金額
49     */
50    function sfTax($price, $product_id = 0, $product_class_id = 0, $pref_id =0, $country_id = 0)
51    {
52        $arrTaxRule = SC_Helper_TaxRule_Ex::getTaxRule($product_id, $product_class_id, $pref_id, $country_id);
53
54        return SC_Helper_TaxRule_Ex::calcTax($price, $arrTaxRule['tax_rate'], $arrTaxRule['tax_rule'], $arrTaxRule['tax_adjust']);
55    }
56
57    /**
58     * 設定情報IDに基づいて税金付与した金額を返す
59     * (受注データのようにルールが決まっている場合用)
60     *
61     * @param integer $price 計算対象の金額
62     * @return integer 税金付与した金額
63     */
64    function calcIncTaxFromRuleId($price, $tax_rule_id = 0)
65    {
66        return $price + SC_Helper_TaxRule_Ex::calcTaxFromRuleId($price, $tax_rule_id);
67    }
68
69    /**
70     * 設定情報IDに基づいて税金の金額を返す
71     * (受注データのようにルールが決まっている場合用)
72     *
73     * @param integer $price 計算対象の金額
74     * @return integer 税金した金額
75     */
76    function calcTaxFromRuleId($price, $tax_rule_id = 0)
77    {
78        $arrTaxRule = SC_Helper_TaxRule_Ex::getTaxRuleData($tax_rule_id);
79
80        return SC_Helper_TaxRule_Ex::calcTax($price, $arrTaxRule['tax_rate'], $arrTaxRule['tax_rule'], $arrTaxRule['tax_adjust']);
81    }
82
83    /**
84     * 税金額を計算する
85     *
86     * @param integer $price 計算対象の金額
87     * @param integer $tax 税率(%単位)
88     *     XXX integer のみか不明
89     * @param integer $tax_rule 端数処理
90     * @return integer 税金額
91     */
92    function calcTax ($price, $tax, $calc_rule, $tax_adjust = 0)
93    {
94        $real_tax = $tax / 100;
95        $ret = $price * $real_tax;
96        switch ($calc_rule) {
97            // 四捨五入
98            case 1:
99                $ret = round($ret);
100                break;
101            // 切り捨て
102            case 2:
103                $ret = floor($ret);
104                break;
105            // 切り上げ
106            case 3:
107                $ret = ceil($ret);
108                break;
109            // デフォルト:切り上げ
110            default:
111                $ret = ceil($ret);
112                break;
113        }
114
115        return $ret + $tax_adjust;
116    }
117
118    /**
119     * 現在有効な税金設定情報を返す
120     *
121     * @param integer $price 計算対象の金額
122     * @return array 税設定情報
123     */
124    function getTaxRule ($product_id = 0, $product_class_id = 0, $pref_id = 0, $country_id = 0)
125    {
126        // 初期化
127        $product_id = $product_id > 0 ? $product_id : 0;
128        $product_class_id = $product_class_id > 0 ? $product_class_id : 0;
129        $pref_id = $pref_id > 0 ? $pref_id : 0;
130        $country_id = $country_id > 0 ? $country_id : 0;
131
132        $objCustomer = new SC_Customer_Ex();
133        if ($objCustomer->isLoginSuccess(true)) {
134            if ($country_id == 0) {
135                $objCustomer->getValue('country_id');
136            }
137            if ($pref_id == 0) {
138                $objCustomer->getValue('pref');
139            }
140        }
141
142        // リクエストの配列化
143        $arrRequest = array('product_id' => $product_id,
144                        'product_class_id' => $product_class_id,
145                        'pref_id' => $pref_id,
146                        'country_id' => $country_id);
147
148        // 地域設定を優先するが、システムパラメーターなどに設定を持っていくか
149        // 後に書いてあるほど優先される、詳細後述MEMO参照
150        $arrPriorityKeys = array('product_id', 'product_class_id', 'pref_id', 'country_id');    // TODO: パラメーター設定に持っていく
151        $cache_key = "$product_id,$product_class_id,$pref_id,$country_id";
152
153        // 複数回呼出があるのでキャッシュ化
154        static $data_c = array();
155
156        if (empty($data_c[$cache_key])) {
157            $arrRet = array();
158
159            // 条件に基づいて税の設定情報を取得
160            $objQuery =& SC_Query_Ex::getSingletonInstance();
161            $table = 'dtb_tax_rule';
162            $cols = '*, CASE WHEN apply_date IS NULL THEN 1 ELSE 0 END as nullorder';
163            $where = '((product_id = 0 OR product_id = ?)'
164                        . ' OR (product_class_id = 0 OR product_class_id = ?))'
165                        . ' AND (pref_id = 0 OR pref_id = ?)'
166                        . ' AND (country_id = 0 OR country_id = ?)'
167                        . ' AND (apply_date < CURRENT_TIMESTAMP OR apply_date IS NULL)'
168                        . ' AND del_flg = 0';
169
170            $arrVal = array($product_id, $product_class_id, $pref_id, $country_id);
171            $order = 'nullorder ASC, apply_date DESC';
172            $objQuery->setOrder($order);
173            $arrData = $objQuery->select($cols, $table, $where, $arrVal);
174            // 優先度付け
175            // MEMO: 税の設定は相反する設定を格納可能だが、その中で優先度を付けるため
176            //       キーの優先度により、利用する税設定を判断する
177            //       優先度が同等の場合、適用日付で判断する
178
179            // XXXX: ビット演算で優先順位を判断という雑な事してます。すいません
180            foreach ($arrData as $data_key => $data) {
181                $res = 0;
182                foreach ($arrPriorityKeys as $key_no => $key) {
183                    if ($arrRequest[$key] != 0 && $data[$key] == $arrRequest[$key]) {
184                        // 配列の数値添字を重みとして利用する
185                        $res += 1 << ($key_no + 1);
186                    }
187                }
188                $arrData[$data_key]['rank'] = $res;
189            }
190
191            // 優先順位が高いものを返却値として確定
192            // 適用日降順に並んでいるので、単に優先順位比較のみで格納判断可能
193            foreach ($arrData as $data) {
194                if (!isset($arrRet['rank']) || $arrRet['rank'] < $data['rank']) {
195                    // 優先度が高い場合, または空の場合
196                    $arrRet = $data;
197                }
198            }
199            $data_c[$cache_key] = $arrRet;
200        }
201
202        return $data_c[$cache_key];
203    }
204
205    /**
206     * 税金設定情報を登録する(商品管理用)
207     *
208     * @param decimal $tax_rate 消費税率
209     * @param integer $product_id 商品ID
210     * @param integer $product_class_id 商品規格ID
211     * @param decimal $tax_adjust 消費税加算額
212     * @param integer $pref_id 県ID
213     * @param integer $country_id 国ID
214     * @return void
215     */
216    function setTaxRuleForProduct($tax_rate, $product_id = 0, $product_class_id = 0, $tax_adjust=0, $pref_id = 0, $country_id = 0)
217    {
218        // 基本設定を取得
219        $arrRet = SC_Helper_taxRule_Ex::getTaxRule();
220        // 基本設定の消費税率と一緒であれば設定しない
221        if( $arrRet['tax_rate'] != $tax_rate ) {
222            // 課税規則は基本設定のものを使用
223            $calc_rule = $arrRet['calc_rule'];
224            // 税情報を設定
225            SC_Helper_TaxRule_Ex::setTaxRule($calc_rule, $tax_rate, $apply_date, $tax_rule_id=NULL, $tax_adjust=0, $product_id, $product_class_id, $pref_id, $country_id);
226        }
227    }
228
229    /**
230     * 税金設定情報を登録する(仮)リファクタする(memo:規格設定後に商品編集を行うと消費税が0になるのを対応が必要)
231     *
232     * @param
233     * @return
234     */
235    function setTaxRule($calc_rule, $tax_rate, $apply_date, $tax_rule_id=NULL, $tax_adjust=0, $product_id = 0, $product_class_id = 0, $pref_id = 0, $country_id = 0)
236    {
237        $table = 'dtb_tax_rule';
238        $arrValues = array();
239        $arrValues['calc_rule'] = $calc_rule;
240        $arrValues['tax_rate'] = $tax_rate;
241        $arrValues['tax_adjust'] = $tax_adjust;
242        $arrValues['apply_date'] = $apply_date;
243        $arrValues['member_id'] = $_SESSION['member_id'];
244        $arrValues['update_date'] = 'CURRENT_TIMESTAMP';
245
246        // 新規か更新か?
247        $objQuery =& SC_Query_Ex::getSingletonInstance();
248        if($tax_rule_id == NULL && $product_id != 0 && $product_class_id != 0){
249            $where = 'product_id = ? AND product_class_id= ? AND pref_id = ? AND country_id = ?';
250            $arrVal = array($product_id, $product_class_id, $pref_id, $country_id);
251            $arrCheck = $objQuery->getRow('*', 'dtb_tax_rule', $where, $arrVal);
252            $tax_rule_id = $arrCheck['tax_rule_id'];
253        }
254
255        if($tax_rule_id == NULL) {
256            // 税情報を新規
257            // INSERTの実行
258            $arrValues['tax_rule_id'] = $objQuery->nextVal('dtb_tax_rule_tax_rule_id');
259            $arrValues['country_id'] = $country_id;
260            $arrValues['pref_id'] = $pref_id;
261            $arrValues['product_id'] = $product_id;
262            $arrValues['product_class_id'] = $product_class_id;
263            $arrValues['create_date'] = 'CURRENT_TIMESTAMP';
264
265            $objQuery->insert($table, $arrValues);
266        } else {
267            // 税情報を更新
268            $where = 'tax_rule_id = ?';
269            $objQuery->update($table, $arrValues, $where, array($tax_rule_id));
270        }
271    }
272
273    function getTaxRuleList($has_deleted = false)
274    {
275        $objQuery =& SC_Query_Ex::getSingletonInstance();
276        $col = 'tax_rule_id, tax_rate, calc_rule, apply_date';
277        $where = '';
278        if (!$has_deleted) {
279            $where .= 'del_flg = 0 AND product_id = 0 AND product_class_id = 0';
280        }
281        $table = 'dtb_tax_rule';
282        // 適用日時順に更新
283        $objQuery->setOrder('apply_date DESC');
284        $arrRet = $objQuery->select($col, $table, $where);
285
286        return $arrRet;
287    }
288
289    function getTaxRuleData($tax_rule_id, $has_deleted = false)
290    {
291        $objQuery =& SC_Query_Ex::getSingletonInstance();
292        $where = 'tax_rule_id = ?';
293        if (!$has_deleted) {
294            $where .= ' AND del_flg = 0';
295        }
296
297        return $objQuery->getRow('*', 'dtb_tax_rule', $where, array($tax_rule_id));
298    }
299
300    function getTaxRuleByTime($apply_date, $has_deleted = false)
301    {
302        $objQuery =& SC_Query_Ex::getSingletonInstance();
303        $where = 'apply_date = ?';
304        if (!$has_deleted) {
305            $where .= ' AND del_flg = 0';
306        }
307        $arrRet = $objQuery->select('*', 'dtb_tax_rule', $where, array($apply_date));
308
309        return $arrRet[0];
310    }
311
312    /**
313     * 税規約の削除.
314     *
315     * @param integer $tax_rule_id 税規約ID
316     * @return void
317     */
318    function deleteTaxRuleData($tax_rule_id)
319    {
320        $objQuery =& SC_Query_Ex::getSingletonInstance();
321
322        $sqlval = array();
323        $sqlval['del_flg']     = 1;
324        $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
325        $where = 'tax_rule_id = ?';
326        $objQuery->update('dtb_tax_rule', $sqlval, $where, array($tax_rule_id));
327    }
328}
Note: See TracBrowser for help on using the repository browser.