OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / include / addons / eclib / ecLib.php
1 <?php
2 /**
3  * Eコマース価格処理クラス
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2012 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: ecLib.php 5385 2012-11-16 01:27:36Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once(dirname(__FILE__) . '/ecLibDb.php');
17
18 class ecLib
19 {
20         private $currencyType;          // 通貨種別
21         private $taxType;                       // 税種別
22         private $taxRate;                       // 税率
23         private $decimalPlace;          // 小数点位置
24         private $configArray;           // Eコマース定義
25         public $db;     // DB接続オブジェクト
26         
27         // 会員番号自動生成
28         const MEMBER_NO_HEAD = 'WEB';           // 自動生成する会員番号のヘッダ部
29         const MEMBER_NO_LENGTH = 5;                     // 自動生成する会員番号の数値桁数
30         
31         /**
32          * コンストラクタ
33          */
34         function __construct()
35         {
36                 // DBオブジェクト作成
37                 $this->db = new ecLibDb();
38                 
39                 // オブジェクトを初期化
40                 $this->_initByDefault();
41                 
42                 // Eコマース定義を読み込む
43                 $this->configArray = $this->_loadConfig();
44         }
45         /**
46          * オブジェクトをデフォルト通貨、言語で初期化
47          */
48         public function _initByDefault()
49         {
50                 global $gEnvManager;
51                 
52                 $type = $this->db->getDefaultCurrency();
53                 $lang = $gEnvManager->getDefaultLanguage();
54                 $this->setCurrencyType($type, $lang);
55         }
56         /**
57          * 通貨種別設定
58          *
59          * @param string        $type           通貨種別
60          * @param string        $lang           言語
61          */
62         public function setCurrencyType($type, $lang)
63         {
64                 if ($type != $this->currencyType){
65                         // 価格表示方法(小数桁数)を取得
66                         $ret = $this->db->getCurrency($type, $lang, $row);
67                         if ($ret){
68                                 $this->decimalPlace = $row['cu_decimal_place'];
69                                 $this->currencyType = $type;
70                         }
71                 }
72         }
73         /**
74          * 税種別設定
75          *
76          * @param string        $type           税種別
77          * @param string        $lang           言語
78          */
79         public function setTaxType($type, $lang)
80         {
81                 if (empty($type)){                      // 税タイプ未選択のとき
82                         $this->taxRate = 0;
83                         $this->taxType = '';
84                 } else if ($type != $this->taxType){
85                         // 税種別情報を取得
86                         $ret = $this->db->getTaxType($type, $lang, $row);
87                         if ($ret){
88                                 // 税率を取得
89                                 // 税率が取得できないときは0とする
90                                 $rate = $this->db->getTaxRate($row['tt_tax_rate_id']);
91                                 $this->taxRate = $rate;
92                                 $this->taxType = $type;
93                         }
94                 }
95         }
96         /**
97          * 税率を取得
98          *
99          * @param string        $type           税種別
100          * @param string        $lang           言語
101          * @return float                                税率
102          */
103         public function getTaxRate($type, $lang)
104         {
105                 $rate = 0;
106                 // 税種別情報を取得
107                 $ret = $this->db->getTaxType($type, $lang, $row);
108                 if ($ret){
109                         // 税率を取得
110                         $rate = $this->db->getTaxRate($row['tt_tax_rate_id']);
111                 }
112                 return $rate;
113         }
114         /**
115          * 税込み価格を取得
116          *
117          * 税抜き価格から、現在の税種別の設定で税額を求める
118          * 現在の通貨の設定で価格の文字列を作成する
119          *
120          * @param float  $srcPrice              税抜き価格
121          * @param string $dispPrice             税込み価格文字列表現
122          * @return float                                税込み価格(数値)
123          */
124         public function getPriceWithTax($srcPrice, &$dispPrice)
125         {
126                 $tax = $srcPrice * $this->taxRate * 0.01;
127                 $total = $srcPrice + $tax;
128                 
129                 // 価格文字列作成
130                 $dispPrice = number_format($total, $this->decimalPlace);
131                 return $total;
132         }
133         /**
134          * 税抜き価格を取得
135          *
136          * 税抜き価格から、現在の税種別の設定で税額を求める
137          * 現在の通貨の設定で価格の文字列を作成する
138          *
139          * @param float  $srcPrice              税抜き価格
140          * @param string $dispPrice             税抜き価格文字列表現
141          * @return float                                税抜き価格(数値)
142          */
143         public function getPriceWithoutTax($srcPrice, &$dispPrice)
144         {       
145                 // 価格文字列作成
146                 $dispPrice = number_format($srcPrice, $this->decimalPlace);
147                 return $srcPrice;
148         }
149         /**
150          * クッキーに保存するカートIDを生成
151          *
152          * @return string                               カートID
153          */
154         public function createCartId()
155         {
156                 // 最大シリアルNoを取得
157                 $max = $this->db->getMaxSerialOfBasket();
158                 $cartId = md5(time() . ($max + 1));
159                 return $cartId;
160         }
161         /**
162          * 数値を価格表示用の数値に変換
163          *
164          * @param string        $currencyType           通貨種別
165          * @param string        $lang                           言語
166          * @param float         $price                          変換する価格
167          */
168         public function convertByCurrencyFormat($currencyType, $lang, $price)
169         {
170                 $dispPrice = '';
171                 
172                 // 価格表示方法(小数桁数)を取得
173                 $ret = $this->db->getCurrency($currencyType, $lang, $row);
174                 if ($ret){
175                         $decimalPlace = $row['cu_decimal_place'];
176                         $dispPrice = number_format($price, $decimalPlace);
177                 }
178                 return $dispPrice;
179         }
180         /**
181          * デフォルトの通貨を取得
182          *
183          * @return string                               デフォルトの通貨ID
184          */
185         public function getDefaultCurrency()
186         {
187                 return $this->db->getDefaultCurrency();
188         }
189         /**
190          * 会員Noを生成
191          *
192          * @return string               生成した会員NO
193          */
194         public function generateMemberNo()
195         {
196                 // 最大Noを取得
197                 $max = $this->db->getMaxMemberNo(self::MEMBER_NO_HEAD);
198                 $max++;
199                 $no = self::MEMBER_NO_HEAD . sprintf("%0" . self::MEMBER_NO_LENGTH . "d", $max);
200                 return $no;
201         }
202         /**
203          * 仮会員を正会員に変更
204          *
205          * @param int $userId                   ユーザID兼データ更新ユーザ
206          * @param bool $generateMemNo   会員NOを自動生成するかどうか
207          * @return                                              true=成功、false=失敗
208          */
209         function makeTmpMemberToProperMember($userId, $generateMemNo=true)
210         {
211                 $no = '';
212                 if ($generateMemNo) $no = $this->generateMemberNo();
213                 return $this->db->makeTmpMemberToProperMember($userId, $no);
214         }
215         /**
216          * 規定の端数処理を行う
217          *
218          * @param float $price                  処理を行う価格
219          * @param float                                 処理結果
220          */
221         public function getCurrencyPrice($price)
222         {
223                 return floor($price);
224         }
225         /**
226          * 定義値を取得
227          *
228          * @param string $key           定義キー
229          * @param string $default       デフォルト値
230          * @return string                       値
231          */
232         function getConfig($key, $default = '')
233         {
234                 $value = $this->configArray[$key];
235                 if (!isset($value)) $value = $default;
236                 return $value;
237         }
238         /**
239          * Eコマース定義値をDBから取得
240          *
241          * @return array                取得データ
242          */
243         function _loadConfig()
244         {
245                 $retVal = array();
246
247                 // 定義を読み込み
248                 $ret = $this->db->getAllConfig($rows);
249                 if ($ret){
250                         // 取得データを連想配列にする
251                         $configCount = count($rows);
252                         for ($i = 0; $i < $configCount; $i++){
253                                 $key = $rows[$i]['cg_id'];
254                                 $value = $rows[$i]['cg_value'];
255                                 $retVal[$key] = $value;
256                         }
257                 }
258                 return $retVal;
259         }
260 }
261 ?>