Ignore:
Timestamp:
2013/03/10 09:21:47 (11 years ago)
Author:
AMUAMU
Message:

税金設定を取得する部分の実装

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/camp/camp-2_13-tax/data/class/helper/SC_Helper_TaxRule.php

    r22666 r22691  
    114114    } 
    115115 
     116 
    116117    /** 
    117118     * 現在有効な税金設定情報を返す 
     
    120121     * @return array 税設定情報 
    121122     */ 
    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]; 
     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'); 
     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('key=' . $cache_key . ' result_tax=' . print_r($data_c[$cache_key],true)); 
     186        return $data_c[$cache_key]; 
    140187    } 
    141188 
     
    160207    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) 
    161208    { 
    162         $table = 'dtb_tax_rule'; 
    163         $arrValues = array(); 
    164         $arrValues['calc_rule'] = $calc_rule; 
    165         $arrValues['tax_rate'] = $tax_rate; 
    166         $arrValues['tax_adjust'] = $tax_adjust; 
    167         $arrValues['apply_date'] = $apply_date; 
    168         $arrValues['member_id'] = $_SESSION['member_id']; 
    169         $arrValues['update_date'] = 'CURRENT_TIMESTAMP'; 
    170          
     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 
    171218        // 新規か更新か? 
    172219        $objQuery =& SC_Query_Ex::getSingletonInstance(); 
    173         if($tax_rule_id == NULL && $product_id != 0 && $product_class_id != 0){ 
     220        if($tax_rule_id == NULL && $product_id != 0 && $product_class_id != 0){ 
    174221        $where = 'product_id = ? AND product_class_id= ? AND pref_id = ? AND country_id = ?'; 
    175222        $arrVal = array($product_id, $product_class_id, $pref_id, $country_id); 
    176         $arrCheck = $objQuery->getRow('*', 'dtb_tax_rule', $where, $arrVal); 
    177         $tax_rule_id = $arrCheck['tax_rule_id']; 
    178         } 
    179          
     223        $arrCheck = $objQuery->getRow('*', 'dtb_tax_rule', $where, $arrVal); 
     224        $tax_rule_id = $arrCheck['tax_rule_id']; 
     225        } 
     226 
    180227        if($tax_rule_id == NULL) { 
    181228            // 税情報を新規 
     
    186233            $arrValues['product_id'] = $product_id; 
    187234            $arrValues['product_class_id'] = $product_class_id; 
    188             $arrValues['create_date'] = 'CURRENT_TIMESTAMP'; 
    189          
     235            $arrValues['create_date'] = 'CURRENT_TIMESTAMP'; 
     236 
    190237            $objQuery->insert($table, $arrValues); 
    191238        } else { 
     
    195242        } 
    196243    } 
    197      
    198      
     244 
     245 
    199246    function getTaxRuleList($has_deleted = false) 
    200247    { 
     
    222269    } 
    223270 
    224      
     271 
    225272 
    226273    function getTaxRuleByTime($apply_date, $has_deleted = false) 
Note: See TracChangeset for help on using the changeset viewer.