7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
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
16 require_once($gEnvManager->getContainerPath() . '/baseIWidgetContainer.php');
17 require_once($gEnvManager->getCurrentIWidgetDbPath() . '/product_lotbuyingDb.php');
19 class product_lotbuyingWidgetContainer extends BaseIWidgetContainer
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'; // 商品コード表示フォーマット
41 function __construct()
44 parent::__construct();
47 $this->db = new product_lotbuyingDb();
50 $this->ecObj = $this->gInstance->getObject(self::PRICE_OBJ_ID);
55 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
56 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
58 * @param RequestManager $request HTTPリクエスト処理クラス
59 * @param string $act 実行処理
60 * @param object $configObj 定義情報オブジェクト
61 * @param object $optionObj 可変パラメータオブジェクト
62 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
64 function _setTemplate($request, $act, $configObj, $optionObj)
66 return 'index.tmpl.html';
71 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
73 * @param RequestManager $request HTTPリクエスト処理クラス
74 * @param string $act 実行処理
75 * @param object $configObj 定義情報オブジェクト
76 * @param object $optionObj 可変パラメータオブジェクト
79 function _assign($request, $act, $configObj, $optionObj)
82 $id = $optionObj->id; // ユニークなID
83 $init = $optionObj->init; // データ初期化を行うかどうか
86 if ($act == 'calc'){ // 計算のとき
88 $this->productClass = $configObj->productClass; // 商品クラス
89 $productId = $configObj->productId; // 商品ID
90 $rateTable = $configObj->rateTable; // 割引率表
92 $this->productArray = explode(',', $productId); // 集計対象
93 $rateArray = parseUserCustomParam($rateTable); // 割引率表を配列化
96 $this->langId = $optionObj->languageId; // 言語ID
97 $cartId = $optionObj->cartId; // 商品のカート
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));
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;
115 for ($j = 0; $j < count($rateArray); $j++){
116 if ($foreValue <= $quantity && $quantity < $rateArray[$j]->key) break;
117 $foreValue = $rateArray[$j]->key;
119 if ($j > 0) $price = $total * $rateArray[$j -1]->value * 0.01;
120 $discountTotal -= intval($price);// 割引額合計更新
124 $resultObj->price = $discountTotal; // 割引額合計。割引額は負の値
125 $this->setResultObj($resultObj);
126 } else if ($act == 'content'){ // 画面表示のとき
130 * 取得したデータをテンプレートに設定する
132 * @param int $index 行番号(0~)
133 * @param array $fetchedRow フェッチ取得した行
134 * @param object $param 未使用
135 * @return bool true=処理続行の場合、false=処理終了の場合
137 function _calcCartLoop($index, $fetchedRow, $param)
139 static $itemIndex = 0;
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']); // 価格表示用
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']; // 商品タイプコード
156 $productName = sprintf(photo_shopCommonDef::PRODUCT_NAME_FORMAT, $productTypeName, $title); // 商品名
157 $productCode = sprintf(photo_shopCommonDef::PRODUCT_CODE_FORMAT, $photoId, $productTypeCode); // 商品コード
160 if (!$fetchedRow['ht_visible']) $priceAvailable = false; // 商品が表示不可のときは価格を無効とする
163 $ret = $this->db->getPhotoInfoWithPrice($productId, $productClass, $productType, self::STANDARD_PRICE, $this->langId, $row);
165 case self::PRODUCT_CLASS_DEFAULT: // 一般商品のとき
167 $productName = $fetchedRow['pt_name']; // 商品名
168 $productCode = $fetchedRow['pt_code']; // 商品コード
171 if (!$fetchedRow['pt_visible']) $priceAvailable = false; // 商品が表示不可のときは価格を無効とする
174 $ret = $this->db->getProductByProductId($productId, $this->langId, $row, $imageRows);
180 $price = $row['pp_price']; // 価格
181 $currency = $row['pp_currency_id']; // 通貨
182 $taxType = self::TAX_TYPE; // 税種別
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;
190 $priceAvailable = false;
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;
202 if (!$fetchedRow['si_available']) $priceAvailable = false;
204 if ($priceAvailable){ // 価格無効があった場合はエラーを返す
205 $this->_productTotal += $subtotal; // 合計価格
206 $this->_productCount += $quantity; // 商品総数
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);
216 case self::PRODUCT_CLASS_DEFAULT: // 一般商品のとき
217 if (in_array($productId, $this->productArray)) $this->addProduct($productId, $subtotal, $quantity);
222 $this->isErr = true; // エラーステータス
223 return false; // 処理中断
230 * @param int $productId 商品ID
231 * @param float $total 商品価格合計
232 * @param int $quantity 商品総数
235 function addProduct($productId, $total, $quantity)
237 if (array_key_exists($productId, $this->calcTotal)){
238 $productObj = $this->calcTotal[$productId];
239 $productObj->total += $total;
240 $productObj->quantity += $quantity;
242 $productObj = new stdClass;
243 $productObj->total = $total;
244 $productObj->quantity = $quantity;
245 $this->calcTotal[$productId] = $productObj;