OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / ec_main / include / iwidgets / product_lotbuying / include / container / product_lotbuyingWidgetContainer.php
1 <?php
2 /**
3  * コンテナクラス
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: product_lotbuyingWidgetContainer.php 5437 2012-12-07 13:14:59Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseIWidgetContainer.php');
17 require_once($gEnvManager->getCurrentIWidgetDbPath() . '/product_lotbuyingDb.php');
18
19 class product_lotbuyingWidgetContainer extends BaseIWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $ecObj;                 // 価格計算用オブジェクト
23         private $langId;                        // 言語ID
24         private $productClass;          // 商品クラス
25         private $productTotal;                                  // 合計価格
26         private $productCount;                                  // 商品総数
27         private $productArray;                                  // 集計対象
28         private $calcTotal;                                     // 割引額計算用データ
29         const PRICE_OBJ_ID = "eclib";           // 価格計算オブジェクトID
30         const PRODUCT_CLASS_DEFAULT     = '';           // 商品クラス
31         const PRODUCT_CLASS_PHOTO       = 'photo';              // 商品クラス
32         const STANDARD_PRICE            = 'selling';            // 通常価格
33         const PRODUCT_TYPE_DOWNLOAD = 'download';               // 商品タイプ
34         const TAX_TYPE                          = 'sales';                                              // 課税タイプ(外税)
35         const PRODUCT_NAME_FORMAT       = '%s(%s)';             // 商品名表示フォーマット
36         const PRODUCT_CODE_FORMAT       = '%s-%s';              // 商品コード表示フォーマット
37         
38         /**
39          * コンストラクタ
40          */
41         function __construct()
42         {
43                 // 親クラスを呼び出す
44                 parent::__construct();
45                 
46                 // DBオブジェクト取得
47                 $this->db = new product_lotbuyingDb();
48                 
49                 // 価格計算用オブジェクト取得
50                 $this->ecObj = $this->gInstance->getObject(self::PRICE_OBJ_ID);
51         }
52         /**
53          * テンプレートファイルを設定
54          *
55          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
56          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
57          *
58          * @param RequestManager $request               HTTPリクエスト処理クラス
59          * @param string         $act                   実行処理
60          * @param object         $configObj             定義情報オブジェクト
61          * @param object         $optionObj             可変パラメータオブジェクト
62          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
63          */
64         function _setTemplate($request, $act, $configObj, $optionObj)
65         {
66                 return 'index.tmpl.html';
67         }
68         /**
69          * テンプレートにデータ埋め込む
70          *
71          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
72          *
73          * @param RequestManager $request               HTTPリクエスト処理クラス
74          * @param string         $act                   実行処理
75          * @param object         $configObj             定義情報オブジェクト
76          * @param object         $optionObj             可変パラメータオブジェクト
77          * @param                                                               なし
78          */
79         function _assign($request, $act, $configObj, $optionObj)
80         {
81                 // 基本情報を取得
82                 $id             = $optionObj->id;               // ユニークなID
83                 $init   = $optionObj->init;             // データ初期化を行うかどうか
84                 
85                 // 入力値取得
86                 if ($act == 'calc'){            // 計算のとき
87                         // 定義値取得
88                         $this->productClass     = $configObj->productClass;             // 商品クラス
89                         $productId                      = $configObj->productId;                        // 商品ID
90                         $rateTable                      = $configObj->rateTable;                // 割引率表
91                         
92                         $this->productArray = explode(',', $productId);         // 集計対象
93                         $rateArray = parseUserCustomParam($rateTable);  // 割引率表を配列化
94                         
95                         // 可変データ取得
96                         $this->langId   = $optionObj->languageId;               // 言語ID
97                         $cartId                 = $optionObj->cartId;                   // 商品のカート
98
99                         // カート内容を取得
100                         $this->isErr = false;                   // エラーステータス
101                         $this->_productTotal = 0;                                       // 合計価格
102                         $this->_productCount = 0;                                       // 商品総数
103                         $this->calcTotal = array();                                     // 割引額計算用データ
104                         $this->ecObj->db->getCartItems($cartId, $this->langId, $this->productClass, array($this, _calcCartLoop));
105
106                         // 割引額を求める
107                         $discountTotal = 0;
108                         $keys = array_keys($this->calcTotal);
109                         for ($i = 0; $i < count($keys); $i++){
110                                 $productObj = $this->calcTotal[$keys[$i]];
111                                 $total = $productObj->total;
112                                 $quantity = $productObj->quantity;
113                                 $price = 0;
114                                 $foreValue = 0;
115                                 for ($j = 0; $j < count($rateArray); $j++){
116                                         if ($foreValue <= $quantity && $quantity < $rateArray[$j]->key) break;
117                                         $foreValue = $rateArray[$j]->key;
118                                 }
119                                 if ($j > 0) $price = $total * $rateArray[$j -1]->value * 0.01;
120                                 $discountTotal -= intval($price);// 割引額合計更新
121                         }
122                         
123                         // 計算結果オブジェクトに設定
124                         $resultObj->price = $discountTotal;     // 割引額合計。割引額は負の値
125                         $this->setResultObj($resultObj);
126                 } else if ($act == 'content'){          // 画面表示のとき
127                 }
128         }
129         /**
130          * 取得したデータをテンプレートに設定する
131          *
132          * @param int $index                    行番号(0~)
133          * @param array $fetchedRow             フェッチ取得した行
134          * @param object $param                 未使用
135          * @return bool                                 true=処理続行の場合、false=処理終了の場合
136          */
137         function _calcCartLoop($index, $fetchedRow, $param)
138         {
139                 static $itemIndex = 0;
140
141                 $priceAvailable = true; // 価格が有効であるかどうか
142                 $productClass = $fetchedRow['si_product_class'];                // 商品クラス
143                 $productType = $fetchedRow['si_product_type_id'];               // 商品タイプ
144                 $productId = $fetchedRow['si_product_id'];                              // 商品ID
145                 $prePrice = $this->convertToDispString($fetchedRow['cu_symbol']);               // 価格表示用
146                 $postPrice = $this->convertToDispString($fetchedRow['cu_post_symbol']); // 価格表示用
147                 
148                 switch ($productClass){
149                         case self::PRODUCT_CLASS_PHOTO:         // フォトギャラリー画像のとき
150                                 $photoId = $fetchedRow['ht_public_id'];         // 公開画像ID
151                                 $title = $fetchedRow['ht_name'];                // サムネール画像タイトル
152                                 $productTypeName = $fetchedRow['py_name'];              // 商品タイプ名
153                                 $productTypeCode = $fetchedRow['py_code'];              // 商品タイプコード
154
155                                 // 表示用の商品名、商品コード作成
156                                 $productName = sprintf(photo_shopCommonDef::PRODUCT_NAME_FORMAT, $productTypeName, $title);             // 商品名
157                                 $productCode = sprintf(photo_shopCommonDef::PRODUCT_CODE_FORMAT, $photoId, $productTypeCode);           // 商品コード
158                                 
159                                 // 商品の状態
160                                 if (!$fetchedRow['ht_visible']) $priceAvailable = false;                // 商品が表示不可のときは価格を無効とする
161                                 
162                                 // 画像価格情報を取得
163                                 $ret = $this->db->getPhotoInfoWithPrice($productId, $productClass, $productType, self::STANDARD_PRICE, $this->langId, $row);
164                                 break;
165                         case self::PRODUCT_CLASS_DEFAULT:       // 一般商品のとき
166                                 // 表示用の商品名、商品コード作成
167                                 $productName = $fetchedRow['pt_name'];          // 商品名
168                                 $productCode = $fetchedRow['pt_code'];          // 商品コード
169                                 
170                                 // 商品の状態
171                                 if (!$fetchedRow['pt_visible']) $priceAvailable = false;                // 商品が表示不可のときは価格を無効とする
172                                 
173                                 // 商品価格情報を取得
174                                 $ret = $this->db->getProductByProductId($productId, $this->langId, $row, $imageRows);
175                                 break;
176                 }
177                 
178                 if ($ret){
179                         // 価格を取得
180                         $price = $row['pp_price'];      // 価格
181                         $currency = $row['pp_currency_id'];     // 通貨
182                         $taxType = self::TAX_TYPE;                                      // 税種別
183
184                         // 価格作成
185                         $this->ecObj->setCurrencyType($currency, $this->langId);                // 通貨設定
186                         $this->ecObj->setTaxType($taxType, $this->langId);              // 税種別設定
187                         $unitPrice = $this->ecObj->getPriceWithTax($price, $dispUnitPrice);     // 税込み価格取得
188                         $dispUnitPrice = $prePrice . $dispUnitPrice . $postPrice;
189                 } else {
190                         $priceAvailable = false;
191                 }
192                 
193                 // ##### カートの内容のチェック #####
194                 // 価格が変更のときは、価格を無効にする
195                 $quantity = $fetchedRow['si_quantity'];
196                 $subtotal = $fetchedRow['si_subtotal'];
197                 $oldCurrency = $fetchedRow['si_currency_id'];
198                 if ($unitPrice * $quantity != $subtotal) $priceAvailable = false;
199                 if ($oldCurrency != $currency) $priceAvailable = false;
200
201                 // 価格の有効判断
202                 if (!$fetchedRow['si_available']) $priceAvailable = false;
203
204                 if ($priceAvailable){           // 価格無効があった場合はエラーを返す
205                         $this->_productTotal += $subtotal;                                      // 合計価格
206                         $this->_productCount += $quantity;                                      // 商品総数
207                         $itemIndex++;
208                         
209                         // 商品ごとの集計
210                         switch ($productClass){
211                                 case self::PRODUCT_CLASS_PHOTO:         // フォト商品のとき
212                                         if ($productType != self::PRODUCT_TYPE_DOWNLOAD){               // ダウンロード商品以外を集計
213                                                 if (in_array($productType, $this->productArray)) $this->addProduct($productType, $subtotal, $quantity);
214                                         }
215                                         break;
216                                 case self::PRODUCT_CLASS_DEFAULT:       // 一般商品のとき
217                                         if (in_array($productId, $this->productArray)) $this->addProduct($productId, $subtotal, $quantity);
218                                         break;
219                         }
220                         return true;
221                 } else {
222                         $this->isErr = true;                    // エラーステータス
223                         return false;           // 処理中断
224                 }
225         }
226         
227         /**
228          * 割引額計算用に商品を追加
229          *
230          * @param int $productId        商品ID
231          * @param float $total          商品価格合計
232          * @param int $quantity         商品総数
233          * @return                                      なし
234          */
235         function addProduct($productId, $total, $quantity)
236         {
237                 if (array_key_exists($productId, $this->calcTotal)){
238                         $productObj = $this->calcTotal[$productId];
239                         $productObj->total += $total;
240                         $productObj->quantity += $quantity;
241                 } else {
242                         $productObj = new stdClass;
243                         $productObj->total = $total;
244                         $productObj->quantity = $quantity;
245                         $this->calcTotal[$productId] = $productObj;
246                 }
247         }
248 }
249 ?>