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

Revision 22653, 10.3 KB checked in by maeken, 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     * @param integer $price 計算対象の金額
120     * @return array 税設定情報
121     */
122    function getTaxRule($product_id = 0, $product_class_id = 0, $pref_id = 0, $country_id = 0)
123    {
124        // 条件に基づいて税情報を取得
125        $objQuery =& SC_Query_Ex::getSingletonInstance();
126        $table = 'dtb_tax_rule';
127        $where = '(product_id = 0 OR product_id = ?)'
128                    . ' AND (product_class_id = 0 OR product_class_id = ?)'
129                    . ' AND (pref_id = 0 OR pref_id = ?)'
130                    . ' AND (country_id = 0 OR country_id = ?)';
131
132        $arrVal = array($product_id, $product_class_id, $pref_id, $country_id);
133        $order = 'apply_date DESC';
134        $objQuery->setOrder($order);
135        $arrData = $objQuery->select('*', $table, $where, $arrVal);
136        // 日付や条件でこねて選択は、作り中。取りあえずスタブ的にデフォルトを返却
137        // 一旦配列の最後の項目を返すように変更
138        // return $arrData[0];
139        return $arrData[count($arrData)-1];
140    }
141
142    /**
143     * 税金設定情報を登録する(商品管理用)
144     *
145     * @param
146     * @return
147     */
148    function setTaxRuleForProduct($tax_rate, $product_id = 0, $product_class_id = 0, $tax_adjust=0, $pref_id = 0, $country_id = 0)
149    {
150        // 税情報を設定
151        SC_Helper_TaxRule_Ex::setTaxRule($tax_rate,
152                                         $tax_adjust,
153                                         $product_id,
154                                         $product_class_id,
155                                         $pref_id,
156                                         $country_id);
157    }
158
159    /**
160     * 税金設定情報を登録する(仮)リファクタする(memo:規格設定後に商品編集を行うと消費税が0になるのを対応が必要)
161     *
162     * @param
163     * @return
164     */
165    function setTaxRule($tax_rate, $tax_adjust=0, $product_id = 0, $product_class_id = 0, $pref_id = 0, $country_id = 0)
166    {
167        // デフォルトの設定とtax_rateの値が同じ場合は登録しない
168        $arrRet = SC_Helper_TaxRule_Ex::getTaxRule();
169        if( $arrRet['tax_rate'] == $tax_rate ) {
170            return;
171        }
172        // 新規か更新か?
173        $objQuery =& SC_Query_Ex::getSingletonInstance();
174        $where = 'product_id=? and product_class_id=? and pref_id=? and country_id=?';
175        $arrVal = array($product_id, $product_class_id, $pref_id, $country_id);
176        $arrCheck = $objQuery->select('*', 'dtb_tax_rule', $where, $arrVal);
177       
178        if(empty($arrCheck)) {
179            // 税情報を新規
180            $table = 'dtb_tax_rule';
181            $arrValues = array();
182            // todo idを計算して設定する必要あり(nextvalに変更?)
183            $arrTaxruleid = $objQuery->select('max(tax_rule_id)', 'dtb_tax_rule');
184            $arrValues['tax_rule_id'] = $arrTaxruleid[0]['max(tax_rule_id)']+1;
185            $arrValues['country_id'] = $country_id;
186            $arrValues['pref_id'] = $pref_id;
187            $arrValues['product_id'] = $product_id;
188            $arrValues['product_class_id'] = $product_class_id;
189            $arrValues['calc_rule'] = $arrRet['calc_rule'];
190            $arrValues['tax_rate'] = $tax_rate;
191            $arrValues['tax_adjust'] = $tax_adjust;
192            $arrValues['apply_date'] = $arrRet['apply_date'];
193            $arrValues['create_date'] = 'CURRENT_TIMESTAMP';
194            $arrValues['update_date'] = 'CURRENT_TIMESTAMP';
195       
196            $objQuery->insert($table, $arrValues);
197        } else {
198            // 税情報を更新
199            $objQuery->update('dtb_tax_rule', array('tax_rate' => $tax_rate), $where, $arrVal);
200        }
201    }
202   
203   
204    function getTaxRuleList($has_deleted = false)
205    {
206        $objQuery =& SC_Query_Ex::getSingletonInstance();
207        $col = 'tax_rule_id, tax_rate, calc_rule, apply_date';
208        $where = '';
209        if (!$has_deleted) {
210            $where .= 'del_flg = 0';
211        }
212        $table = 'dtb_tax_rule';
213        $objQuery->setOrder('tax_rule_id DESC');
214        $arrRet = $objQuery->select($col, $table, $where);
215        return $arrRet;
216
217    }
218
219    function getTaxRuleData($tax_rule_id, $has_deleted = false)
220    {
221        $objQuery =& SC_Query_Ex::getSingletonInstance();
222        $where = 'tax_rule_id = ?';
223        if (!$has_deleted) {
224            $where .= ' AND del_flg = 0';
225        }
226        return $objQuery->getRow('*', 'dtb_tax_rule', 'tax_rule_id = ?', array($tax_rule_id));
227    }
228
229   
230
231    function getTaxRuleByTime($apply_date, $has_deleted = false)
232    {
233        $objQuery =& SC_Query_Ex::getSingletonInstance();
234        $where = 'apply_date = ?';
235        if (!$has_deleted) {
236            $where .= ' AND del_flg = 0';
237        }
238        $arrRet = $objQuery->select('*', 'dtb_tax_rule', $where, array($apply_date));
239        return $arrRet[0];
240    }
241
242    function registerTaxRuleData($sqlval) {
243        $objQuery =& SC_Query_Ex::getSingletonInstance();
244
245        $sqlval['apply_date'] = SC_Utils_Ex::sfGetTimestampistime($sqlval['apply_date_year'], $sqlval['apply_date_month'], $sqlval['apply_date_day'],$sqlval['apply_date_hour'], $sqlval['apply_date_minutes']);
246
247        unset($sqlval['apply_date_year']);
248        unset($sqlval['apply_date_month']);
249        unset($sqlval['apply_date_day']);
250        unset($sqlval['apply_date_hour']);
251        unset($sqlval['apply_date_minutes']);
252
253        $tax_rule_id = $sqlval['tax_rule_id'];
254        $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
255        // 新規登録
256        if ($tax_rule_id == '') {
257            // INSERTの実行
258            $sqlval['create_date'] = 'CURRENT_TIMESTAMP';
259            $sqlval['tax_rule_id'] = $objQuery->nextVal('dtb_tax_rule_tax_rule_id');
260            $ret = $objQuery->insert('dtb_tax_rule', $sqlval);
261            // 既存編集
262        } else {
263            unset($sqlval['tax_rule_id']);
264            unset($sqlval['create_date']);
265            $where = 'tax_rule_id = ?';
266            $ret = $objQuery->update('dtb_tax_rule', $sqlval, $where, array($tax_rule_id));
267        }
268        return ($ret) ? $sqlval['tax_rule_id'] : FALSE;
269    }
270
271    /**
272     * 税規約の削除.
273     *
274     * @param integer $tax_rule_id 税規約ID
275     * @return void
276     */
277    public function deleteTaxRuleData($tax_rule_id)
278    {
279        $objQuery =& SC_Query_Ex::getSingletonInstance();
280        $sqlval['del_flg']     = 1;
281        $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
282        $where = 'tax_rule_id = ?';
283        $objQuery->update('dtb_tax_rule', $sqlval, $where, array($tax_rule_id));
284    }
285
286
287}
Note: See TracBrowser for help on using the repository browser.