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

Revision 23415, 14.7 KB checked in by pineray, 10 years ago (diff)

#2448 (typo修正・ソース整形・ソースコメントの改善)

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 int $price 計算対象の金額
37     * @param int $product_id 商品ID
38     * @param int $product_class_id 商品規格ID
39     * @param int $pref_id 都道府県ID
40     * @param int $country_id 国ID
41     * @return int 税金付与した金額
42     */
43    public static function sfCalcIncTax($price, $product_id = 0, $product_class_id = 0, $pref_id =0, $country_id = 0)
44    {
45        return $price + SC_Helper_TaxRule_Ex::sfTax($price, $product_id, $product_class_id, $pref_id, $country_id);
46    }
47
48    /**
49     * 設定情報に基づいて税金の金額を返す
50     *
51     * @param int $price 計算対象の金額
52     * @param int $product_id 商品ID
53     * @param int $product_class_id 商品規格ID
54     * @param int $pref_id 都道府県ID
55     * @param int $country_id 国ID
56     * @return int 税金付与した金額
57     */
58    public static function sfTax($price, $product_id = 0, $product_class_id = 0, $pref_id =0, $country_id = 0)
59    {
60        $arrTaxRule = SC_Helper_TaxRule_Ex::getTaxRule($product_id, $product_class_id, $pref_id, $country_id);
61
62        return SC_Helper_TaxRule_Ex::calcTax($price, $arrTaxRule['tax_rate'], $arrTaxRule['tax_rule'], $arrTaxRule['tax_adjust']);
63    }
64
65    /**
66     * 設定情報IDに基づいて税金付与した金額を返す
67     * (受注データのようにルールが決まっている場合用)
68     *
69     * @param int $price 計算対象の金額
70     * @param int $tax_rule_id 税規約ID
71     * @return int 税金付与した金額
72     */
73    public static function calcIncTaxFromRuleId($price, $tax_rule_id = 0)
74    {
75        return $price + SC_Helper_TaxRule_Ex::calcTaxFromRuleId($price, $tax_rule_id);
76    }
77
78    /**
79     * 設定情報IDに基づいて税金の金額を返す
80     * (受注データのようにルールが決まっている場合用)
81     *
82     * @param int $price 計算対象の金額
83     * @param int $tax_rule_id 税規約ID
84     * @return int 税金付与した金額
85     */
86    public static function calcTaxFromRuleId($price, $tax_rule_id = 0)
87    {
88        $arrTaxRule = SC_Helper_TaxRule_Ex::getTaxRuleData($tax_rule_id);
89
90        return SC_Helper_TaxRule_Ex::calcTax($price, $arrTaxRule['tax_rate'], $arrTaxRule['tax_rule'], $arrTaxRule['tax_adjust']);
91    }
92
93    /**
94     * 税金額を計算する
95     *
96     * @param int $price 計算対象の金額
97     * @param int $tax 税率(%単位)
98     *     XXX int のみか不明
99     * @param int $calc_rule 端数処理
100     * @param int $tax_adjust 調整額
101     * @return int 税金額
102     */
103    public static function calcTax ($price, $tax, $calc_rule, $tax_adjust = 0)
104    {
105        $real_tax = $tax / 100;
106        $ret = $price * $real_tax;
107        switch ($calc_rule) {
108            // 四捨五入
109            case 1:
110                $ret = round($ret);
111                break;
112            // 切り捨て
113            case 2:
114                $ret = floor($ret);
115                break;
116            // 切り上げ
117            case 3:
118                $ret = ceil($ret);
119                break;
120            // デフォルト:切り上げ
121            default:
122                $ret = ceil($ret);
123                break;
124        }
125
126        return $ret + $tax_adjust;
127    }
128
129    /**
130     * 現在有効な税率設定情報を返す
131     *
132     * @param int $product_id 商品ID
133     * @param int $product_class_id 商品規格ID
134     * @param int $pref_id 都道府県ID
135     * @param int $country_id 国ID
136     * @return array 税設定情報
137     */
138    public static function getTaxRule($product_id = 0, $product_class_id = 0, $pref_id = 0, $country_id = 0)
139    {
140        // 複数回呼出があるのでキャッシュ化
141        static $data_c = array();
142
143        // 初期化
144        $product_id = $product_id > 0 ? $product_id : 0;
145        $product_class_id = $product_class_id > 0 ? $product_class_id : 0;
146        $pref_id = $pref_id > 0 ? $pref_id : 0;
147        $country_id = $country_id > 0 ? $country_id : 0;
148
149        // 一覧画面の速度向上のため商品単位税率設定がOFFの時はキャッシュキーを丸めてしまう
150        if (OPTION_PRODUCT_TAX_RULE == 1) {
151            $cache_key = "$product_id,$product_class_id,$pref_id,$country_id";
152        } else {
153            $cache_key = "$pref_id,$country_id";
154        }
155
156        if (empty($data_c[$cache_key])) {
157            // ログイン済み会員で国と地域指定が無い場合は、会員情報をデフォルトで利用。管理画面では利用しない
158            if (!(defined('ADMIN_FUNCTION') && ADMIN_FUNCTION == true)) {
159                $objCustomer = new SC_Customer_Ex();
160                if ($objCustomer->isLoginSuccess(true)) {
161                    if ($country_id == 0) {
162                        $country_id = $objCustomer->getValue('country_id');
163                    }
164                    if ($pref_id == 0) {
165                        $pref_id = $objCustomer->getValue('pref');
166                    }
167                }
168            }
169
170            $arrRet = array();
171            // リクエストの配列化
172            $arrRequest = array('product_id' => $product_id,
173                            'product_class_id' => $product_class_id,
174                            'pref_id' => $pref_id,
175                            'country_id' => $country_id);
176
177            // 地域設定を優先するが、システムパラメーターなどに設定を持っていくか
178            // 後に書いてあるほど優先される、詳細後述MEMO参照
179            $arrPriorityKeys = explode(',', TAX_RULE_PRIORITY);
180
181            // 条件に基づいて税の設定情報を取得
182            $objQuery =& SC_Query_Ex::getSingletonInstance();
183            $table = 'dtb_tax_rule';
184            $cols = '*';
185
186            // 商品税率有無設定により分岐
187            if(OPTION_PRODUCT_TAX_RULE == 1) {
188                $where = '
189                        (
190                            (product_id = 0 OR product_id = ?)
191                            AND (product_class_id = 0 OR product_class_id = ?)
192                        )
193                    AND (pref_id = 0 OR pref_id = ?)
194                    AND (country_id = 0 OR country_id = ?)
195                    AND apply_date < CURRENT_TIMESTAMP
196                    AND del_flg = 0';
197                $arrVal = array($product_id, $product_class_id, $pref_id, $country_id);
198            } else {
199                $where = '     product_id = 0 '
200                       . ' AND product_class_id = 0 '
201                       . ' AND (pref_id = 0 OR pref_id = ?)'
202                       . ' AND (country_id = 0 OR country_id = ?)'
203                       . ' AND apply_date < CURRENT_TIMESTAMP'
204                       . ' AND del_flg = 0';
205                $arrVal = array($pref_id, $country_id);
206            }
207
208            $order = 'apply_date DESC';
209            $objQuery->setOrder($order);
210            $arrData = $objQuery->select($cols, $table, $where, $arrVal);
211            // 優先度付け
212            // MEMO: 税の設定は相反する設定を格納可能だが、その中で優先度を付けるため
213            //       キーの優先度により、利用する税設定を判断する
214            //       優先度が同等の場合、適用日付で判断する
215            foreach ($arrData as $data_key => $data) {
216                $res = 0;
217                foreach ($arrPriorityKeys as $key_no => $key) {
218                    if ($arrRequest[$key] != 0 && $data[$key] == $arrRequest[$key]) {
219                        // 配列の数値添字を重みとして利用する
220                        $res += 1 << ($key_no + 1);
221                    }
222                }
223                $arrData[$data_key]['rank'] = $res;
224            }
225
226            // 優先順位が高いものを返却値として確定
227            // 適用日降順に並んでいるので、単に優先順位比較のみで格納判断可能
228            foreach ($arrData as $data) {
229                if (!isset($arrRet['rank']) || $arrRet['rank'] < $data['rank']) {
230                    // 優先度が高い場合, または空の場合
231                    $arrRet = $data;
232                }
233            }
234            // XXXX: 互換性のためtax_ruleにもcalc_ruleを設定
235            $arrRet['tax_rule'] = $arrRet['calc_rule'];
236            $data_c[$cache_key] = $arrRet;
237        }
238
239        return $data_c[$cache_key];
240    }
241
242    /**
243     * 税率設定情報を登録する(商品管理用)
244     *
245     * @param float $tax_rate 消費税率
246     * @param int $product_id 商品ID
247     * @param int $product_class_id 商品規格ID
248     * @param float|int $tax_adjust 消費税加算額
249     * @param int $pref_id 県ID
250     * @param int $country_id 国ID
251     * @return void
252     */
253    public static function setTaxRuleForProduct($tax_rate, $product_id = 0, $product_class_id = 0, $tax_adjust=0, $pref_id = 0, $country_id = 0)
254    {
255        // 基本設定を取得
256        $arrRet = SC_Helper_TaxRule_Ex::getTaxRule($product_id, $product_class_id);
257
258        // 基本設定の消費税率と一緒であれば設定しない
259        if ($arrRet['tax_rate'] != $tax_rate) {
260            // 課税規則は基本設定のものを使用
261            $calc_rule = $arrRet['calc_rule'];
262            // 日付は登録時点を設定
263            $apply_date = date('Y/m/d H:i:s');
264            // 税情報を設定
265            SC_Helper_TaxRule_Ex::setTaxRule($calc_rule, $tax_rate, $apply_date, NULL, $tax_adjust, $product_id, $product_class_id, $pref_id, $country_id);
266        }
267    }
268
269    /**
270     * 税率設定情報を登録する(仮)リファクタする(memo:規格設定後に商品編集を行うと消費税が0になるのを対応が必要)
271     *
272     * @param int $calc_rule 端数処理
273     * @param int $tax_rate 税率
274     * @param int|string $apply_date 適用日時
275     * @param null $tax_rule_id 税規約ID
276     * @param int $tax_adjust 調整額
277     * @param int $product_id 商品ID
278     * @param int $product_class_id 商品規格ID
279     * @param int $pref_id 都道府県ID
280     * @param int $country_id 国ID
281     * @return void
282     */
283    public 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)
284    {
285        $table = 'dtb_tax_rule';
286        $arrValues = array();
287        $arrValues['calc_rule'] = $calc_rule;
288        $arrValues['tax_rate'] = $tax_rate;
289        $arrValues['tax_adjust'] = $tax_adjust;
290        $arrValues['apply_date'] = $apply_date;
291        $arrValues['member_id'] = $_SESSION['member_id'];
292        $arrValues['update_date'] = 'CURRENT_TIMESTAMP';
293
294        // 新規か更新か?
295        $objQuery =& SC_Query_Ex::getSingletonInstance();
296        if ($tax_rule_id == NULL && $product_id != 0 && $product_class_id != 0) {
297            $where = 'product_id = ? AND product_class_id= ? AND pref_id = ? AND country_id = ?';
298            $arrVal = array($product_id, $product_class_id, $pref_id, $country_id);
299            $arrCheck = $objQuery->getRow('*', 'dtb_tax_rule', $where, $arrVal);
300            $tax_rule_id = $arrCheck['tax_rule_id'];
301        }
302
303        if ($tax_rule_id == NULL) {
304            // 税情報を新規
305            // INSERTの実行
306            $arrValues['tax_rule_id'] = $objQuery->nextVal('dtb_tax_rule_tax_rule_id');
307            $arrValues['country_id'] = $country_id;
308            $arrValues['pref_id'] = $pref_id;
309            $arrValues['product_id'] = $product_id;
310            $arrValues['product_class_id'] = $product_class_id;
311            $arrValues['create_date'] = 'CURRENT_TIMESTAMP';
312
313            $objQuery->insert($table, $arrValues);
314        } else {
315            // 税情報を更新
316            $where = 'tax_rule_id = ?';
317            $objQuery->update($table, $arrValues, $where, array($tax_rule_id));
318        }
319    }
320
321    /**
322     * @param bool $has_deleted
323     * @return array|null
324     */
325    public function getTaxRuleList($has_deleted = false)
326    {
327        $objQuery =& SC_Query_Ex::getSingletonInstance();
328        $col = 'tax_rule_id, tax_rate, calc_rule, apply_date';
329        $where = '';
330        if (!$has_deleted) {
331            $where .= 'del_flg = 0 AND product_id = 0 AND product_class_id = 0';
332        }
333        $table = 'dtb_tax_rule';
334        // 適用日時順に更新
335        $objQuery->setOrder('apply_date DESC');
336        $arrRet = $objQuery->select($col, $table, $where);
337
338        return $arrRet;
339    }
340
341    /**
342     * @param int $tax_rule_id
343     * @param bool $has_deleted
344     * @return array
345     */
346    public function getTaxRuleData($tax_rule_id, $has_deleted = false)
347    {
348        $objQuery =& SC_Query_Ex::getSingletonInstance();
349        $where = 'tax_rule_id = ?';
350        if (!$has_deleted) {
351            $where .= ' AND del_flg = 0';
352        }
353
354        return $objQuery->getRow('*', 'dtb_tax_rule', $where, array($tax_rule_id));
355    }
356
357    /**
358     * @param int|string $apply_date
359     * @param bool $has_deleted
360     * @return mixed
361     */
362    public function getTaxRuleByTime($apply_date, $has_deleted = false)
363    {
364        $objQuery =& SC_Query_Ex::getSingletonInstance();
365        $where = 'apply_date = ?';
366        if (!$has_deleted) {
367            $where .= ' AND del_flg = 0';
368        }
369        $arrRet = $objQuery->select('*', 'dtb_tax_rule', $where, array($apply_date));
370
371        return $arrRet[0];
372    }
373
374    /**
375     * 税規約の削除.
376     *
377     * @param  int $tax_rule_id 税規約ID
378     * @return void
379     */
380    public function deleteTaxRuleData($tax_rule_id)
381    {
382        $objQuery =& SC_Query_Ex::getSingletonInstance();
383
384        $sqlval = array();
385        $sqlval['del_flg']     = 1;
386        $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
387        $where = 'tax_rule_id = ?';
388        $objQuery->update('dtb_tax_rule', $sqlval, $where, array($tax_rule_id));
389    }
390}
Note: See TracBrowser for help on using the repository browser.