| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2010 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 | /** |
|---|
| 25 | * 商品を扱うサービスクラス. |
|---|
| 26 | * |
|---|
| 27 | * @author LOCKON CO.,LTD. |
|---|
| 28 | * @author Kentaro Ohkouchi |
|---|
| 29 | * @version $Id$ |
|---|
| 30 | */ |
|---|
| 31 | class SC_Product { |
|---|
| 32 | |
|---|
| 33 | /** 規格名一覧 */ |
|---|
| 34 | var $arrClassName; |
|---|
| 35 | /** 規格分類名一覧 */ |
|---|
| 36 | var $arrClassCatName; |
|---|
| 37 | var $classCategories = array(); |
|---|
| 38 | var $stock_find; |
|---|
| 39 | /** 規格1クラス名 */ |
|---|
| 40 | var $className1 = ''; |
|---|
| 41 | /** 規格2クラス名 */ |
|---|
| 42 | var $className2 = ''; |
|---|
| 43 | /** 規格1が設定されている */ |
|---|
| 44 | var $classCat1_find; |
|---|
| 45 | /** 規格2が設定されている */ |
|---|
| 46 | var $classCat2_find; |
|---|
| 47 | var $classCats1; |
|---|
| 48 | /** 検索用並び替え条件配列 */ |
|---|
| 49 | var $arrOrderData; |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * 商品検索結果の並び順を指定する。 |
|---|
| 53 | * |
|---|
| 54 | * ただし指定できるテーブルはproduct_idを持っているテーブルであることが必要. |
|---|
| 55 | * |
|---|
| 56 | * @param string $col 並び替えの基準とするフィールド |
|---|
| 57 | * @param string $table 並び替えの基準とするフィールドがあるテーブル |
|---|
| 58 | * @param string $order 並び替えの順序 ASC / DESC |
|---|
| 59 | * @return void |
|---|
| 60 | */ |
|---|
| 61 | function setProductsOrder($col, $table = 'dtb_products', $order = 'ASC') { |
|---|
| 62 | $this->arrOrderData = array('col' => $col, 'table' => $table, 'order' => $order); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * SC_Queryインスタンスに設定された検索条件を元に並び替え済みの検索結果商品IDの配列を取得する。 |
|---|
| 67 | * |
|---|
| 68 | * 検索条件は, SC_Query::getWhere() 関数で設定しておく必要があります. |
|---|
| 69 | * |
|---|
| 70 | * @param SC_Query $objQuery SC_Query インスタンス |
|---|
| 71 | * @param array $arrVal 検索パラメータの配列 |
|---|
| 72 | * @return array 商品IDの配列 |
|---|
| 73 | */ |
|---|
| 74 | function findProductIdsOrder(&$objQuery, $arrVal = array(), $where) { |
|---|
| 75 | $table = <<< __EOS__ |
|---|
| 76 | dtb_products AS alldtl |
|---|
| 77 | JOIN dtb_products_class AS T1 |
|---|
| 78 | ON alldtl.product_id = T1.product_id |
|---|
| 79 | JOIN dtb_product_categories AS T2 |
|---|
| 80 | ON alldtl.product_id = T2.product_id |
|---|
| 81 | JOIN dtb_category |
|---|
| 82 | ON T2.category_id = dtb_category.category_id |
|---|
| 83 | __EOS__; |
|---|
| 84 | $objQuery->setGroupBy('alldtl.product_id'); |
|---|
| 85 | if(is_array($this->arrOrderData) and $objQuery->order == ""){ |
|---|
| 86 | $o_col = $this->arrOrderData['col']; |
|---|
| 87 | $o_table = $this->arrOrderData['table']; |
|---|
| 88 | $o_order = $this->arrOrderData['order']; |
|---|
| 89 | $order = <<< __EOS__ |
|---|
| 90 | ( |
|---|
| 91 | SELECT $o_col |
|---|
| 92 | FROM |
|---|
| 93 | $o_table as T2 |
|---|
| 94 | WHERE T2.product_id = alldtl.product_id |
|---|
| 95 | ORDER BY T2.$o_col $o_order |
|---|
| 96 | LIMIT 1 |
|---|
| 97 | ) $o_order, product_id |
|---|
| 98 | __EOS__; |
|---|
| 99 | $objQuery->setOrder($order); |
|---|
| 100 | } |
|---|
| 101 | $results = $objQuery->select('alldtl.product_id', $table, "", $arrVal, |
|---|
| 102 | MDB2_FETCHMODE_ORDERED); |
|---|
| 103 | foreach ($results as $val) { |
|---|
| 104 | $resultValues[] = $val[0]; |
|---|
| 105 | } |
|---|
| 106 | return $resultValues; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * SC_Queryインスタンスに設定された検索条件をもとに対象商品数を取得する. |
|---|
| 111 | * |
|---|
| 112 | * 検索条件は, SC_Query::getWhere() 関数で設定しておく必要があります. |
|---|
| 113 | * |
|---|
| 114 | * @param SC_Query $objQuery SC_Query インスタンス |
|---|
| 115 | * @param array $arrVal 検索パラメータの配列 |
|---|
| 116 | * @return array 対象商品ID数 |
|---|
| 117 | */ |
|---|
| 118 | function findProductCount(&$objQuery, $arrVal = array()) { |
|---|
| 119 | $table = <<< __EOS__ |
|---|
| 120 | dtb_products AS alldtl |
|---|
| 121 | JOIN dtb_product_categories AS T2 |
|---|
| 122 | ON alldtl.product_id = T2.product_id |
|---|
| 123 | JOIN dtb_category |
|---|
| 124 | ON T2.category_id = dtb_category.category_id |
|---|
| 125 | __EOS__; |
|---|
| 126 | $objQuery->setGroupBy('alldtl.product_id'); |
|---|
| 127 | $sql_base = $objQuery->getSql('alldtl.product_id',$table); |
|---|
| 128 | return $objQuery->getOne( "SELECT count(*) FROM ( $sql_base ) as t" , $arrVal); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * SC_Queryインスタンスに設定された検索条件をもとに商品一覧の配列を取得する. |
|---|
| 133 | * |
|---|
| 134 | * 主に SC_Product::findProductIds() で取得した商品IDを検索条件にし, |
|---|
| 135 | * SC_Query::setOrder() や SC_Query::setLimitOffset() を設定して, 商品一覧 |
|---|
| 136 | * の配列を取得する. |
|---|
| 137 | * |
|---|
| 138 | * @param SC_Query $objQuery SC_Query インスタンス |
|---|
| 139 | * @param array $arrVal 検索パラメータ(ソート条件)の配列 |
|---|
| 140 | * @return array 商品一覧の配列 |
|---|
| 141 | */ |
|---|
| 142 | function lists(&$objQuery, $arrVal = array()) { |
|---|
| 143 | $col = <<< __EOS__ |
|---|
| 144 | product_id |
|---|
| 145 | ,product_code_min |
|---|
| 146 | ,product_code_max |
|---|
| 147 | ,name |
|---|
| 148 | ,comment1 |
|---|
| 149 | ,comment2 |
|---|
| 150 | ,comment3 |
|---|
| 151 | ,main_list_comment |
|---|
| 152 | ,main_image |
|---|
| 153 | ,main_list_image |
|---|
| 154 | ,price01_min |
|---|
| 155 | ,price01_max |
|---|
| 156 | ,price02_min |
|---|
| 157 | ,price02_max |
|---|
| 158 | ,stock_min |
|---|
| 159 | ,stock_max |
|---|
| 160 | ,stock_unlimited_min |
|---|
| 161 | ,stock_unlimited_max |
|---|
| 162 | ,deliv_date_id |
|---|
| 163 | ,status |
|---|
| 164 | ,del_flg |
|---|
| 165 | ,update_date |
|---|
| 166 | __EOS__; |
|---|
| 167 | $res = $objQuery->select($col, $this->alldtlSQL($objQuery->where), |
|---|
| 168 | "", $arrVal); |
|---|
| 169 | return $res; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * 商品詳細を取得する. |
|---|
| 174 | * |
|---|
| 175 | * @param integer $productId 商品ID |
|---|
| 176 | * @return array 商品詳細情報の配列 |
|---|
| 177 | */ |
|---|
| 178 | function getDetail($productId) { |
|---|
| 179 | $objQuery =& SC_Query::getSingletonInstance(); |
|---|
| 180 | $result = $objQuery->select("*", $this->alldtlSQL("product_id = ?"), |
|---|
| 181 | "product_id = ?", |
|---|
| 182 | array($productId, $productId)); |
|---|
| 183 | return $result[0]; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * 商品詳細情報と商品規格を取得する. |
|---|
| 188 | * |
|---|
| 189 | * @param integer $productClassId 商品規格ID |
|---|
| 190 | * @return array 商品詳細情報と商品規格の配列 |
|---|
| 191 | */ |
|---|
| 192 | function getDetailAndProductsClass($productClassId) { |
|---|
| 193 | $result = $this->getProductsClass($productClassId); |
|---|
| 194 | $result = array_merge($result, $this->getDetail($result['product_id'])); |
|---|
| 195 | return $result; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * 商品IDに紐づく商品規格を自分自身に設定する. |
|---|
| 200 | * |
|---|
| 201 | * 引数の商品IDの配列に紐づく商品規格を取得し, 自分自身のフィールドに |
|---|
| 202 | * 設定する. |
|---|
| 203 | * |
|---|
| 204 | * @param array $arrProductId 商品ID の配列 |
|---|
| 205 | * @return void |
|---|
| 206 | */ |
|---|
| 207 | function setProductsClassByProductIds($arrProductId) { |
|---|
| 208 | |
|---|
| 209 | foreach ($arrProductId as $productId) { |
|---|
| 210 | $rows[$productId] = $this->getProductsClassFullByProductId($productId); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | $arrProductsClass = array(); |
|---|
| 214 | foreach ($rows as $productId => $arrProductClass) { |
|---|
| 215 | $classCats1 = array(); |
|---|
| 216 | $classCats1[''] = '選択してください'; |
|---|
| 217 | |
|---|
| 218 | // 規格1クラス名 |
|---|
| 219 | $this->className1[$productId] = |
|---|
| 220 | isset($arrProductClass[0]['class_name1']) |
|---|
| 221 | ? $arrProductClass[0]['class_name1'] |
|---|
| 222 | : ''; |
|---|
| 223 | |
|---|
| 224 | // 規格2クラス名 |
|---|
| 225 | $this->className2[$productId] = |
|---|
| 226 | isset($arrProductClass[0]['class_name2']) |
|---|
| 227 | ? $arrProductClass[0]['class_name2'] |
|---|
| 228 | : ''; |
|---|
| 229 | |
|---|
| 230 | // 規格1が設定されている |
|---|
| 231 | $this->classCat1_find[$productId] = (!SC_Utils_Ex::isBlank($arrProductClass[0]['classcategory_id1'])); |
|---|
| 232 | // 規格2が設定されている |
|---|
| 233 | $this->classCat2_find[$productId] = (!SC_Utils_Ex::isBlank($arrProductClass[0]['classcategory_id2'])); |
|---|
| 234 | |
|---|
| 235 | $this->stock_find[$productId] = false; |
|---|
| 236 | $classCategories = array(); |
|---|
| 237 | $classCategories['']['']['name'] = '選択してください'; |
|---|
| 238 | $classCategories['']['']['product_class_id'] = $arrProductClass[0]['product_class_id']; |
|---|
| 239 | // 商品種別 |
|---|
| 240 | $classCategories['']['']['product_type'] = $arrProductClass[0]['product_type_id']; |
|---|
| 241 | $this->product_class_id[$productId] = $arrProductClass[0]['product_class_id']; |
|---|
| 242 | // 商品種別 |
|---|
| 243 | $this->product_type[$productId] = $arrProductClass[0]['product_type_id']; |
|---|
| 244 | foreach ($arrProductClass as $productsClass) { |
|---|
| 245 | $productsClass1 = $productsClass['classcategory_id1']; |
|---|
| 246 | $productsClass2 = $productsClass['classcategory_id2']; |
|---|
| 247 | $classCategories[$productsClass1]['']['name'] = '選択してください'; |
|---|
| 248 | // 在庫 |
|---|
| 249 | $stock_find_class = ($productsClass['stock_unlimited'] || $productsClass['stock'] > 0); |
|---|
| 250 | |
|---|
| 251 | $classCategories[$productsClass1][$productsClass2]['name'] = $productsClass['classcategory_name2'] . ($stock_find_class ? '' : ' (品切れ中)'); |
|---|
| 252 | |
|---|
| 253 | $classCategories[$productsClass1][$productsClass2]['stock_find'] = $stock_find_class; |
|---|
| 254 | |
|---|
| 255 | if ($stock_find_class) { |
|---|
| 256 | $this->stock_find[$productId] = true; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | if (!in_array($classcat_id1, $classCats1)) { |
|---|
| 260 | $classCats1[$productsClass1] = $productsClass['classcategory_name1'] |
|---|
| 261 | . ($productsClass2 == 0 && !$stock_find_class ? ' (品切れ中)' : ''); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | // 価格 |
|---|
| 265 | $classCategories[$productsClass1][$productsClass2]['price01'] |
|---|
| 266 | = strlen($productsClass['price01']) |
|---|
| 267 | ? number_format(SC_Helper_DB_Ex::sfPreTax($productsClass['price01'])) |
|---|
| 268 | : ''; |
|---|
| 269 | |
|---|
| 270 | $classCategories[$productsClass1][$productsClass2]['price02'] |
|---|
| 271 | = strlen($productsClass['price02']) |
|---|
| 272 | ? number_format(SC_Helper_DB_Ex::sfPreTax($productsClass['price02'])) |
|---|
| 273 | : ''; |
|---|
| 274 | |
|---|
| 275 | // ポイント |
|---|
| 276 | // XXX sfPrePoint() の第4パラメータは、処理にバグがあるため現状省略している。(http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=3540&forum=1&post_id=13853#forumpost13853) |
|---|
| 277 | $classCategories[$productsClass1][$productsClass2]['point'] |
|---|
| 278 | = SC_Utils_Ex::sfPrePoint($productsClass['price02'], $productsClass['point_rate']); |
|---|
| 279 | |
|---|
| 280 | // 商品コード |
|---|
| 281 | $classCategories[$productsClass1][$productsClass2]['product_code'] = $productsClass['product_code']; |
|---|
| 282 | // 商品規格ID |
|---|
| 283 | $classCategories[$productsClass1][$productsClass2]['product_class_id'] = $productsClass['product_class_id']; |
|---|
| 284 | // 商品種別 |
|---|
| 285 | $classCategories[$productsClass1][$productsClass2]['product_type'] = $productsClass['product_type_id']; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | $this->classCategories[$productId] = $classCategories; |
|---|
| 289 | |
|---|
| 290 | // 規格1 |
|---|
| 291 | $this->classCats1[$productId] = $classCats1; |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | /** |
|---|
| 296 | * SC_Query インスタンスに設定された検索条件を使用して商品規格を取得する. |
|---|
| 297 | * |
|---|
| 298 | * @param SC_Query $objQuery SC_Queryインスタンス |
|---|
| 299 | * @param array $params 検索パラメータの配列 |
|---|
| 300 | * @return array 商品規格の配列 |
|---|
| 301 | */ |
|---|
| 302 | function getProductsClassByQuery(&$objQuery, $params) { |
|---|
| 303 | // 末端の規格を取得 |
|---|
| 304 | $col = <<< __EOS__ |
|---|
| 305 | T1.product_id, |
|---|
| 306 | T1.stock, |
|---|
| 307 | T1.stock_unlimited, |
|---|
| 308 | T1.price01, |
|---|
| 309 | T1.price02, |
|---|
| 310 | T1.point_rate, |
|---|
| 311 | T1.product_code, |
|---|
| 312 | T1.product_class_id, |
|---|
| 313 | T1.del_flg, |
|---|
| 314 | T1.product_type_id, |
|---|
| 315 | T1.down_filename, |
|---|
| 316 | T1.down_realfilename, |
|---|
| 317 | T2.class_combination_id, |
|---|
| 318 | T2.parent_class_combination_id, |
|---|
| 319 | T2.classcategory_id, |
|---|
| 320 | T2.level, |
|---|
| 321 | T3.name AS classcategory_name, |
|---|
| 322 | T4.name AS class_name, |
|---|
| 323 | T4.class_id |
|---|
| 324 | __EOS__; |
|---|
| 325 | $table = <<< __EOS__ |
|---|
| 326 | dtb_products_class T1 |
|---|
| 327 | LEFT JOIN dtb_class_combination T2 |
|---|
| 328 | ON T1.class_combination_id = T2.class_combination_id |
|---|
| 329 | LEFT JOIN dtb_classcategory T3 |
|---|
| 330 | ON T2.classcategory_id = T3.classcategory_id |
|---|
| 331 | LEFT JOIN dtb_class T4 |
|---|
| 332 | ON T3.class_id = T4.class_id |
|---|
| 333 | __EOS__; |
|---|
| 334 | $arrRet = $objQuery->select($col, $table, "", $params); |
|---|
| 335 | $levels = array(); |
|---|
| 336 | $parents = array(); |
|---|
| 337 | foreach ($arrRet as $rows) { |
|---|
| 338 | $levels[] = $rows['level']; |
|---|
| 339 | $parents[] = $rows['parent_class_combination_id']; |
|---|
| 340 | } |
|---|
| 341 | $level = max($levels); |
|---|
| 342 | $parentsClass = array(); |
|---|
| 343 | // 階層分の親を取得 |
|---|
| 344 | for ($i = 0; $i < $level -1; $i++) { |
|---|
| 345 | $objQuery =& SC_Query::getSingletonInstance(); |
|---|
| 346 | $objQuery->setWhere('T1.class_combination_id IN (' . implode(', ', array_pad(array(), count($parents), '?')) . ')'); |
|---|
| 347 | |
|---|
| 348 | $col = <<< __EOS__ |
|---|
| 349 | T1.class_combination_id, |
|---|
| 350 | T1.classcategory_id, |
|---|
| 351 | T1.parent_class_combination_id, |
|---|
| 352 | T1.level, |
|---|
| 353 | T2.name AS classcategory_name, |
|---|
| 354 | T3.name AS class_name, |
|---|
| 355 | T3.class_id |
|---|
| 356 | __EOS__; |
|---|
| 357 | $table = <<< __EOS__ |
|---|
| 358 | dtb_class_combination T1 |
|---|
| 359 | LEFT JOIN dtb_classcategory T2 |
|---|
| 360 | ON T1.classcategory_id = T2.classcategory_id |
|---|
| 361 | LEFT JOIN dtb_class T3 |
|---|
| 362 | ON T2.class_id = T3.class_id |
|---|
| 363 | __EOS__; |
|---|
| 364 | |
|---|
| 365 | $arrParents = $objQuery->select($col, $table, "", $parents); |
|---|
| 366 | |
|---|
| 367 | unset($parents); |
|---|
| 368 | foreach ($arrParents as $rows) { |
|---|
| 369 | $parents[] = $rows['parent_class_combination_id']; |
|---|
| 370 | |
|---|
| 371 | foreach ($arrRet as $child) { |
|---|
| 372 | if ($child['parent_class_combination_id'] |
|---|
| 373 | == $rows['class_combination_id']) { |
|---|
| 374 | $rows['product_id'] = $child['product_id']; |
|---|
| 375 | } |
|---|
| 376 | } |
|---|
| 377 | $tmpParents[] = $rows; |
|---|
| 378 | } |
|---|
| 379 | $parentsClass = array_merge($parentsClass, $tmpParents); |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | // 末端から枝を作成 |
|---|
| 383 | $tmpClass = array_merge($arrRet, $parentsClass); |
|---|
| 384 | |
|---|
| 385 | foreach ($tmpClass as $val) { |
|---|
| 386 | $val['class_id' . $val['level']] = $val['class_id']; |
|---|
| 387 | $val['class_name' . $val['level']] = $val['class_name']; |
|---|
| 388 | $val['classcategory_name' . $val['level']] = $val['classcategory_name']; |
|---|
| 389 | $val['classcategory_id' . $val['level']] = $val['classcategory_id']; |
|---|
| 390 | $arrProductsClass[] = $val; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | return $arrProductsClass; |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | /** |
|---|
| 397 | * 商品規格IDから商品規格を取得する. |
|---|
| 398 | */ |
|---|
| 399 | function getProductsClass($productClassId) { |
|---|
| 400 | $objQuery =& SC_Query::getSingletonInstance(); |
|---|
| 401 | $objQuery->setWhere('product_class_id = ?'); |
|---|
| 402 | $objQuery->setOrder("T2.level DESC"); |
|---|
| 403 | $results = $this->getProductsClassByQuery($objQuery, $productClassId); |
|---|
| 404 | $productsClass = $this->getProductsClassFull($results); |
|---|
| 405 | return $productsClass[0]; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | /** |
|---|
| 409 | * 複数の商品IDに紐づいた, 商品規格を取得する. |
|---|
| 410 | * |
|---|
| 411 | * @param array $productIds 商品IDの配列 |
|---|
| 412 | * @return array 商品規格の配列 |
|---|
| 413 | */ |
|---|
| 414 | function getProductsClassByProductIds($productIds = array()) { |
|---|
| 415 | if (empty($productIds)) { |
|---|
| 416 | return array(); |
|---|
| 417 | } |
|---|
| 418 | $objQuery =& SC_Query::getSingletonInstance(); |
|---|
| 419 | $objQuery->setWhere('product_id IN (' . implode(', ', array_pad(array(), count($productIds), '?')) . ')'); |
|---|
| 420 | $objQuery->setOrder("T2.level DESC"); |
|---|
| 421 | return $this->getProductsClassByQuery($objQuery, $productIds); |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | /** |
|---|
| 425 | * 商品IDに紐づいた, 商品規格を階層ごとに取得する. |
|---|
| 426 | * |
|---|
| 427 | * @param array $productId 商品ID |
|---|
| 428 | * @return array 階層ごとの商品規格の配列 |
|---|
| 429 | */ |
|---|
| 430 | function getProductsClassLevelByProductId($productId) { |
|---|
| 431 | $results = $this->getProductsClassByProductIds(array($productId)); |
|---|
| 432 | return $this->getProductsClassLevel($results); |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | /** |
|---|
| 436 | * 商品IDに紐づいた, 商品規格をすべての組み合わせごとに取得する. |
|---|
| 437 | * |
|---|
| 438 | * @param array $productId 商品ID |
|---|
| 439 | * @return array すべての組み合わせの商品規格の配列 |
|---|
| 440 | */ |
|---|
| 441 | function getProductsClassFullByProductId($productId) { |
|---|
| 442 | $results = $this->getProductsClassByProductIds(array($productId)); |
|---|
| 443 | return $this->getProductsClassFull($results); |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | /** |
|---|
| 447 | * 商品規格の配列から, 商品規格を階層ごとに取得する. |
|---|
| 448 | * |
|---|
| 449 | * @access private |
|---|
| 450 | * @param array $productsClassResults 商品規格の結果の配列 |
|---|
| 451 | * @return array 階層ごとの商品規格の配列 |
|---|
| 452 | */ |
|---|
| 453 | function getProductsClassLevel($productsClassResults) { |
|---|
| 454 | foreach ($productsClassResults as $row) { |
|---|
| 455 | $productsClassLevel["level" . $row['level']][] = $row; |
|---|
| 456 | } |
|---|
| 457 | return $productsClassLevel; |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | /** |
|---|
| 461 | * 商品規格の配列から, 商品規格のすべての組み合わせを取得する. |
|---|
| 462 | * |
|---|
| 463 | * @access private |
|---|
| 464 | * @param array $productsClassResults 商品規格の結果の配列 |
|---|
| 465 | * @ array 階層ごとの商品規格の配列 |
|---|
| 466 | */ |
|---|
| 467 | function getProductsClassFull($productsClassResults) { |
|---|
| 468 | $results = $this->getProductsClassLevel($productsClassResults); |
|---|
| 469 | $productsClass = array(); |
|---|
| 470 | if (SC_Utils_Ex::isBlank($results["level1"]) |
|---|
| 471 | && SC_Utils_Ex::isBlank($results["level2"])) { |
|---|
| 472 | return $results["level"]; |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | foreach ($results["level1"] as $level1) { |
|---|
| 476 | foreach ($results["level2"] as $level2) { |
|---|
| 477 | if ($level2['parent_class_combination_id'] == $level1['class_combination_id']) { |
|---|
| 478 | $level1 = array_merge($level1, $level2); |
|---|
| 479 | } |
|---|
| 480 | } |
|---|
| 481 | $productsClass[] = $level1; |
|---|
| 482 | } |
|---|
| 483 | return $productsClass; |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | /** |
|---|
| 487 | * 商品IDをキーにした, 商品ステータスIDの配列を取得する. |
|---|
| 488 | * |
|---|
| 489 | * @param array 商品ID の配列 |
|---|
| 490 | * @return array 商品IDをキーにした商品ステータスIDの配列 |
|---|
| 491 | */ |
|---|
| 492 | function getProductStatus($productIds) { |
|---|
| 493 | $objQuery =& SC_Query::getSingletonInstance(); |
|---|
| 494 | $productStatus = $objQuery->select("product_id, product_status_id", |
|---|
| 495 | "dtb_product_status", |
|---|
| 496 | 'del_flg = 0 AND product_id IN (' . implode(', ', array_pad(array(), count($productIds), '?')) . ')', $productIds); |
|---|
| 497 | $results = array(); |
|---|
| 498 | foreach ($productStatus as $status) { |
|---|
| 499 | $results[$status['product_id']][] = $status['product_status_id']; |
|---|
| 500 | } |
|---|
| 501 | return $results; |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | /** |
|---|
| 505 | * 商品ステータスを設定する. |
|---|
| 506 | * |
|---|
| 507 | * TODO 現在は DELETE/INSERT だが, UPDATE を検討する. |
|---|
| 508 | * |
|---|
| 509 | * @param integer $productId 商品ID |
|---|
| 510 | * @param array $productStatusIds ON にする商品ステータスIDの配列 |
|---|
| 511 | */ |
|---|
| 512 | function setProductStatus($productId, $productStatusIds) { |
|---|
| 513 | |
|---|
| 514 | $val['product_id'] = $productId; |
|---|
| 515 | $val['creator_id'] = $_SESSION['member_id']; |
|---|
| 516 | $val['create_date'] = 'Now()'; |
|---|
| 517 | $val['update_date'] = 'Now()'; |
|---|
| 518 | $val['del_flg'] = '0'; |
|---|
| 519 | |
|---|
| 520 | $objQuery =& SC_Query::getSingletonInstance(); |
|---|
| 521 | $objQuery->delete('dtb_product_status', 'product_id = ?', array($productId)); |
|---|
| 522 | foreach ($productStatusIds as $productStatusId) { |
|---|
| 523 | $val['product_status_id'] = $productStatusId; |
|---|
| 524 | $objQuery->insert('dtb_product_status', $val); |
|---|
| 525 | } |
|---|
| 526 | } |
|---|
| 527 | |
|---|
| 528 | /** |
|---|
| 529 | * 商品詳細の結果から, 購入制限数を取得する. |
|---|
| 530 | * |
|---|
| 531 | * getDetailAndProductsClass() の結果から, 購入制限数を取得する. |
|---|
| 532 | * |
|---|
| 533 | * @param array $p 商品詳細の検索結果の配列 |
|---|
| 534 | * @return integer 商品詳細の結果から求めた購入制限数. |
|---|
| 535 | * @see getDetailAndProductsClass() |
|---|
| 536 | */ |
|---|
| 537 | function getBuyLimit($p) { |
|---|
| 538 | $limit = null; |
|---|
| 539 | if ($p['stock_unlimited'] != '1' && is_numeric($p['sale_limit'])) { |
|---|
| 540 | $limit = min($p['sale_limit'], $p['stock']); |
|---|
| 541 | } elseif (is_numeric($p['sale_limit'])) { |
|---|
| 542 | $limit = $p['sale_limit']; |
|---|
| 543 | } elseif ($p['stock_unlimited'] != '1') { |
|---|
| 544 | $limit = $p['stock']; |
|---|
| 545 | } |
|---|
| 546 | return $limit; |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | /** |
|---|
| 550 | * 在庫を減少させる. |
|---|
| 551 | * |
|---|
| 552 | * 指定の在庫数まで, 在庫を減少させる. |
|---|
| 553 | * 減少させた結果, 在庫数が 0 未満になった場合, 引数 $quantity が 0 の場合は, |
|---|
| 554 | * 在庫の減少を中止し, false を返す. |
|---|
| 555 | * 在庫の減少に成功した場合は true を返す. |
|---|
| 556 | * |
|---|
| 557 | * @param integer $productClassId 商品規格ID |
|---|
| 558 | * @param integer $quantity 減少させる在庫数 |
|---|
| 559 | * @return boolean 在庫の減少に成功した場合 true; 失敗した場合 false |
|---|
| 560 | */ |
|---|
| 561 | function reduceStock($productClassId, $quantity) { |
|---|
| 562 | |
|---|
| 563 | $productsClass = $this->getDetailAndProductsClass($productClassId); |
|---|
| 564 | if (($productsClass['stock_unlimited'] != '1' |
|---|
| 565 | && $productsClass['stock'] < $quantity) |
|---|
| 566 | || $quantity == 0) { |
|---|
| 567 | return false; |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | $objQuery =& SC_Query::getSingletonInstance(); |
|---|
| 571 | $objQuery->update('dtb_products_class', array(), |
|---|
| 572 | "product_class_id = ?", array($productClassId), |
|---|
| 573 | array('stock' => 'stock - ?'), array($quantity)); |
|---|
| 574 | // TODO エラーハンドリング |
|---|
| 575 | return true; |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | /** |
|---|
| 579 | * 商品詳細の SQL を取得する. |
|---|
| 580 | * |
|---|
| 581 | * @param string $where 商品詳細の WHERE 句 |
|---|
| 582 | * @return string 商品詳細の SQL |
|---|
| 583 | */ |
|---|
| 584 | function alldtlSQL($where = "") { |
|---|
| 585 | $whereCause = ""; |
|---|
| 586 | if (!SC_Utils_Ex::isBlank($where)) { |
|---|
| 587 | $whereCause = " WHERE " . $where; |
|---|
| 588 | } |
|---|
| 589 | /* |
|---|
| 590 | * point_rate は商品規格(dtb_products_class)ごとに保持しているが, |
|---|
| 591 | * 商品(dtb_products)ごとの設定なので MAX のみを取得する. |
|---|
| 592 | */ |
|---|
| 593 | $sql = <<< __EOS__ |
|---|
| 594 | ( |
|---|
| 595 | SELECT dtb_products.product_id, |
|---|
| 596 | dtb_products.name, |
|---|
| 597 | dtb_products.maker_id, |
|---|
| 598 | dtb_products.rank, |
|---|
| 599 | dtb_products.status, |
|---|
| 600 | dtb_products.comment1, |
|---|
| 601 | dtb_products.comment2, |
|---|
| 602 | dtb_products.comment3, |
|---|
| 603 | dtb_products.comment4, |
|---|
| 604 | dtb_products.comment5, |
|---|
| 605 | dtb_products.comment6, |
|---|
| 606 | dtb_products.note, |
|---|
| 607 | dtb_products.main_list_comment, |
|---|
| 608 | dtb_products.main_list_image, |
|---|
| 609 | dtb_products.main_comment, |
|---|
| 610 | dtb_products.main_image, |
|---|
| 611 | dtb_products.main_large_image, |
|---|
| 612 | dtb_products.sub_title1, |
|---|
| 613 | dtb_products.sub_comment1, |
|---|
| 614 | dtb_products.sub_image1, |
|---|
| 615 | dtb_products.sub_large_image1, |
|---|
| 616 | dtb_products.sub_title2, |
|---|
| 617 | dtb_products.sub_comment2, |
|---|
| 618 | dtb_products.sub_image2, |
|---|
| 619 | dtb_products.sub_large_image2, |
|---|
| 620 | dtb_products.sub_title3, |
|---|
| 621 | dtb_products.sub_comment3, |
|---|
| 622 | dtb_products.sub_image3, |
|---|
| 623 | dtb_products.sub_large_image3, |
|---|
| 624 | dtb_products.sub_title4, |
|---|
| 625 | dtb_products.sub_comment4, |
|---|
| 626 | dtb_products.sub_image4, |
|---|
| 627 | dtb_products.sub_large_image4, |
|---|
| 628 | dtb_products.sub_title5, |
|---|
| 629 | dtb_products.sub_comment5, |
|---|
| 630 | dtb_products.sub_image5, |
|---|
| 631 | dtb_products.sub_large_image5, |
|---|
| 632 | dtb_products.sub_title6, |
|---|
| 633 | dtb_products.sub_comment6, |
|---|
| 634 | dtb_products.sub_image6, |
|---|
| 635 | dtb_products.sub_large_image6, |
|---|
| 636 | dtb_products.del_flg, |
|---|
| 637 | dtb_products.creator_id, |
|---|
| 638 | dtb_products.create_date, |
|---|
| 639 | dtb_products.update_date, |
|---|
| 640 | dtb_products.deliv_date_id, |
|---|
| 641 | T4.product_code_min, |
|---|
| 642 | T4.product_code_max, |
|---|
| 643 | T4.price01_min, |
|---|
| 644 | T4.price01_max, |
|---|
| 645 | T4.price02_min, |
|---|
| 646 | T4.price02_max, |
|---|
| 647 | T4.stock_min, |
|---|
| 648 | T4.stock_max, |
|---|
| 649 | T4.stock_unlimited_min, |
|---|
| 650 | T4.stock_unlimited_max, |
|---|
| 651 | T4.point_rate, |
|---|
| 652 | T4.class_count |
|---|
| 653 | FROM dtb_products |
|---|
| 654 | JOIN ( |
|---|
| 655 | SELECT product_id, |
|---|
| 656 | MIN(product_code) AS product_code_min, |
|---|
| 657 | MAX(product_code) AS product_code_max, |
|---|
| 658 | MIN(price01) AS price01_min, |
|---|
| 659 | MAX(price01) AS price01_max, |
|---|
| 660 | MIN(price02) AS price02_min, |
|---|
| 661 | MAX(price02) AS price02_max, |
|---|
| 662 | MIN(stock) AS stock_min, |
|---|
| 663 | MAX(stock) AS stock_max, |
|---|
| 664 | MIN(stock_unlimited) AS stock_unlimited_min, |
|---|
| 665 | MAX(stock_unlimited) AS stock_unlimited_max, |
|---|
| 666 | MAX(point_rate) AS point_rate, |
|---|
| 667 | COUNT(*) as class_count |
|---|
| 668 | FROM dtb_products_class |
|---|
| 669 | $whereCause |
|---|
| 670 | GROUP BY product_id |
|---|
| 671 | ) AS T4 |
|---|
| 672 | ON dtb_products.product_id = T4.product_id |
|---|
| 673 | ) AS alldtl |
|---|
| 674 | __EOS__; |
|---|
| 675 | return $sql; |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | /** |
|---|
| 679 | * 商品規格ID1、2に紐づいた,product_class_idを取得する. |
|---|
| 680 | * |
|---|
| 681 | * @param int $productId 商品ID |
|---|
| 682 | * @param int $classcategory_id1 商品規格ID1 |
|---|
| 683 | * @param int $classcategory_id2 商品規格ID2 |
|---|
| 684 | * @return string product_class_id |
|---|
| 685 | */ |
|---|
| 686 | function getClasscategoryIdsByProductClassId($productId, $classcategory_id1, $classcategory_id2) { |
|---|
| 687 | $objQuery = new SC_Query(); |
|---|
| 688 | $col = "T1.product_id AS product_id,T1.product_class_id AS product_class_id,T1.classcategory_id1 AS classcategory_id1,T1.classcategory_id2 AS classcategory_id2"; |
|---|
| 689 | $table = <<< __EOS__ |
|---|
| 690 | (SELECT |
|---|
| 691 | pc.product_code AS product_code, |
|---|
| 692 | pc.product_id AS product_id, |
|---|
| 693 | pc.product_class_id AS product_class_id, |
|---|
| 694 | pc.class_combination_id AS class_combination_id, |
|---|
| 695 | COALESCE(cc2.classcategory_id,0) AS classcategory_id1, |
|---|
| 696 | COALESCE(cc1.classcategory_id,0) AS classcategory_id2 |
|---|
| 697 | FROM |
|---|
| 698 | dtb_products_class pc LEFT JOIN dtb_class_combination cc1 ON pc.class_combination_id = cc1.class_combination_id |
|---|
| 699 | LEFT JOIN dtb_class_combination cc2 ON cc1.parent_class_combination_id = cc2.class_combination_id) T1 |
|---|
| 700 | __EOS__; |
|---|
| 701 | $where = "T1.product_id = ? AND T1.classcategory_id1 = ? AND T1.classcategory_id2 = ?"; |
|---|
| 702 | $arrRet = $objQuery->select($col, $table, $where, |
|---|
| 703 | array($productId, $classcategory_id1, $classcategory_id2)); |
|---|
| 704 | return $arrRet[0]['product_class_id']; |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | } |
|---|
| 708 | ?> |
|---|