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