source: branches/camp/camp-2_13-tax/data/class/helper/SC_Helper_TaxRule.php @ 22697

Revision 22697, 11.2 KB checked in by yomoro, 11 years ago (diff)

不要な処理を削除

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