[18277] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | * This file is part of EC-CUBE |
---|
| 4 | * |
---|
[20764] | 5 | * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved. |
---|
[18277] | 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 | |
---|
[18863] | 24 | /** |
---|
| 25 | * 商品を扱うサービスクラス. |
---|
| 26 | * |
---|
| 27 | * @author LOCKON CO.,LTD. |
---|
| 28 | * @author Kentaro Ohkouchi |
---|
| 29 | * @version $Id$ |
---|
[18277] | 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; |
---|
[19662] | 48 | /** 検索用並び替え条件配列 */ |
---|
| 49 | var $arrOrderData; |
---|
[19688] | 50 | |
---|
[19662] | 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 | } |
---|
[18815] | 64 | |
---|
| 65 | /** |
---|
[19662] | 66 | * SC_Queryインスタンスに設定された検索条件を元に並び替え済みの検索結果商品IDの配列を取得する。 |
---|
[18815] | 67 | * |
---|
[20990] | 68 | * 検索条件は, SC_Query::setWhere() 関数で設定しておく必要があります. |
---|
[18815] | 69 | * |
---|
| 70 | * @param SC_Query $objQuery SC_Query インスタンス |
---|
[20970] | 71 | * @param array $arrVal 検索パラメーターの配列 |
---|
[18815] | 72 | * @return array 商品IDの配列 |
---|
| 73 | */ |
---|
[20990] | 74 | function findProductIdsOrder(&$objQuery, $arrVal = array()) { |
---|
[18815] | 75 | $table = <<< __EOS__ |
---|
| 76 | dtb_products AS alldtl |
---|
[19662] | 77 | JOIN dtb_products_class AS T1 |
---|
| 78 | ON alldtl.product_id = T1.product_id |
---|
[18815] | 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__; |
---|
[19662] | 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 | } |
---|
[18815] | 101 | $results = $objQuery->select('alldtl.product_id', $table, "", $arrVal, |
---|
| 102 | MDB2_FETCHMODE_ORDERED); |
---|
| 103 | foreach ($results as $val) { |
---|
| 104 | $resultValues[] = $val[0]; |
---|
[18277] | 105 | } |
---|
[19662] | 106 | return $resultValues; |
---|
[18277] | 107 | } |
---|
| 108 | |
---|
[18815] | 109 | /** |
---|
[19662] | 110 | * SC_Queryインスタンスに設定された検索条件をもとに対象商品数を取得する. |
---|
| 111 | * |
---|
[20990] | 112 | * 検索条件は, SC_Query::setWhere() 関数で設定しておく必要があります. |
---|
[19662] | 113 | * |
---|
| 114 | * @param SC_Query $objQuery SC_Query インスタンス |
---|
[20970] | 115 | * @param array $arrVal 検索パラメーターの配列 |
---|
[19662] | 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 | /** |
---|
[18815] | 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 インスタンス |
---|
[20970] | 139 | * @param array $arrVal 検索パラメーター(ソート条件)の配列 |
---|
[18815] | 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__; |
---|
[19662] | 167 | $res = $objQuery->select($col, $this->alldtlSQL($objQuery->where), |
---|
[18815] | 168 | "", $arrVal); |
---|
[19662] | 169 | return $res; |
---|
[18815] | 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * 商品詳細を取得する. |
---|
| 174 | * |
---|
| 175 | * @param integer $productId 商品ID |
---|
| 176 | * @return array 商品詳細情報の配列 |
---|
| 177 | */ |
---|
| 178 | function getDetail($productId) { |
---|
[20507] | 179 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[20595] | 180 | $result = $objQuery->select("*", $this->alldtlSQL("product_id = ? AND del_flg = 0"), |
---|
[18815] | 181 | "product_id = ?", |
---|
| 182 | array($productId, $productId)); |
---|
| 183 | return $result[0]; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
[18829] | 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 | /** |
---|
[18815] | 199 | * 商品IDに紐づく商品規格を自分自身に設定する. |
---|
| 200 | * |
---|
| 201 | * 引数の商品IDの配列に紐づく商品規格を取得し, 自分自身のフィールドに |
---|
| 202 | * 設定する. |
---|
| 203 | * |
---|
| 204 | * @param array $arrProductId 商品ID の配列 |
---|
[20595] | 205 | * @param boolean $has_deleted 削除された商品規格も含む場合 true; 初期値 false |
---|
[18815] | 206 | * @return void |
---|
| 207 | */ |
---|
[20595] | 208 | function setProductsClassByProductIds($arrProductId, $has_deleted = false) { |
---|
[18815] | 209 | |
---|
[20048] | 210 | $arrProductsClass = array(); |
---|
[18815] | 211 | foreach ($arrProductId as $productId) { |
---|
[20595] | 212 | $arrProductClass = $this->getProductsClassFullByProductId($productId, $has_deleted); |
---|
[18815] | 213 | |
---|
[18277] | 214 | $classCats1 = array(); |
---|
[21245] | 215 | $classCats1['__unselected'] = '選択してください'; |
---|
[18815] | 216 | |
---|
[18277] | 217 | // 規格1クラス名 |
---|
| 218 | $this->className1[$productId] = |
---|
[18815] | 219 | isset($arrProductClass[0]['class_name1']) |
---|
| 220 | ? $arrProductClass[0]['class_name1'] |
---|
[18277] | 221 | : ''; |
---|
| 222 | |
---|
| 223 | // 規格2クラス名 |
---|
| 224 | $this->className2[$productId] = |
---|
[18815] | 225 | isset($arrProductClass[0]['class_name2']) |
---|
| 226 | ? $arrProductClass[0]['class_name2'] |
---|
[18277] | 227 | : ''; |
---|
[18815] | 228 | |
---|
[18277] | 229 | // 規格1が設定されている |
---|
[18815] | 230 | $this->classCat1_find[$productId] = (!SC_Utils_Ex::isBlank($arrProductClass[0]['classcategory_id1'])); |
---|
[18277] | 231 | // 規格2が設定されている |
---|
[18815] | 232 | $this->classCat2_find[$productId] = (!SC_Utils_Ex::isBlank($arrProductClass[0]['classcategory_id2'])); |
---|
[18277] | 233 | |
---|
| 234 | $this->stock_find[$productId] = false; |
---|
| 235 | $classCategories = array(); |
---|
[21245] | 236 | $classCategories['__unselected']['__unselected']['name'] = '選択してください'; |
---|
| 237 | $classCategories['__unselected']['__unselected']['product_class_id'] = $arrProductClass[0]['product_class_id']; |
---|
[18882] | 238 | // 商品種別 |
---|
[21245] | 239 | $classCategories['__unselected']['__unselected']['product_type'] = $arrProductClass[0]['product_type_id']; |
---|
[18829] | 240 | $this->product_class_id[$productId] = $arrProductClass[0]['product_class_id']; |
---|
[18882] | 241 | // 商品種別 |
---|
| 242 | $this->product_type[$productId] = $arrProductClass[0]['product_type_id']; |
---|
[18277] | 243 | foreach ($arrProductClass as $productsClass) { |
---|
[20048] | 244 | $classCats2 = array(); |
---|
[18277] | 245 | $productsClass1 = $productsClass['classcategory_id1']; |
---|
| 246 | $productsClass2 = $productsClass['classcategory_id2']; |
---|
| 247 | // 在庫 |
---|
| 248 | $stock_find_class = ($productsClass['stock_unlimited'] || $productsClass['stock'] > 0); |
---|
[18815] | 249 | |
---|
[20048] | 250 | $classCats2['classcategory_id2'] = $productsClass2; |
---|
| 251 | $classCats2['name'] = $productsClass['classcategory_name2'] . ($stock_find_class ? '' : ' (品切れ中)'); |
---|
[18815] | 252 | |
---|
[20048] | 253 | $classCats2['stock_find'] = $stock_find_class; |
---|
[18815] | 254 | |
---|
[18277] | 255 | if ($stock_find_class) { |
---|
| 256 | $this->stock_find[$productId] = true; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | if (!in_array($classcat_id1, $classCats1)) { |
---|
[18852] | 260 | $classCats1[$productsClass1] = $productsClass['classcategory_name1'] |
---|
[18277] | 261 | . ($productsClass2 == 0 && !$stock_find_class ? ' (品切れ中)' : ''); |
---|
| 262 | } |
---|
[18815] | 263 | |
---|
[18277] | 264 | // 価格 |
---|
[20048] | 265 | $classCats2['price01'] |
---|
[18277] | 266 | = strlen($productsClass['price01']) |
---|
[19680] | 267 | ? number_format(SC_Helper_DB_Ex::sfCalcIncTax($productsClass['price01'])) |
---|
[18277] | 268 | : ''; |
---|
[18815] | 269 | |
---|
[20048] | 270 | $classCats2['price02'] |
---|
[18277] | 271 | = strlen($productsClass['price02']) |
---|
[19680] | 272 | ? number_format(SC_Helper_DB_Ex::sfCalcIncTax($productsClass['price02'])) |
---|
[18277] | 273 | : ''; |
---|
[18815] | 274 | |
---|
[18277] | 275 | // ポイント |
---|
[20048] | 276 | $classCats2['point'] |
---|
[20478] | 277 | = number_format(SC_Utils_Ex::sfPrePoint($productsClass['price02'], $productsClass['point_rate'])); |
---|
[18815] | 278 | |
---|
[18278] | 279 | // 商品コード |
---|
[20048] | 280 | $classCats2['product_code'] = $productsClass['product_code']; |
---|
[18829] | 281 | // 商品規格ID |
---|
[20048] | 282 | $classCats2['product_class_id'] = $productsClass['product_class_id']; |
---|
[18882] | 283 | // 商品種別 |
---|
[20048] | 284 | $classCats2['product_type'] = $productsClass['product_type_id']; |
---|
| 285 | |
---|
| 286 | // #929(GC8 規格のプルダウン順序表示不具合)対応のため、2次キーは「#」を前置 |
---|
| 287 | $classCategories[$productsClass1]['#'] = array( |
---|
| 288 | 'classcategory_id2' => '', |
---|
| 289 | 'name' => '選択してください', |
---|
| 290 | ); |
---|
| 291 | $classCategories[$productsClass1]['#' . $productsClass2] = $classCats2; |
---|
[18277] | 292 | } |
---|
[18815] | 293 | |
---|
[18277] | 294 | $this->classCategories[$productId] = $classCategories; |
---|
[18815] | 295 | |
---|
[18277] | 296 | // 規格1 |
---|
| 297 | $this->classCats1[$productId] = $classCats1; |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
[18815] | 301 | /** |
---|
[18829] | 302 | * SC_Query インスタンスに設定された検索条件を使用して商品規格を取得する. |
---|
[18815] | 303 | * |
---|
[18829] | 304 | * @param SC_Query $objQuery SC_Queryインスタンス |
---|
[20970] | 305 | * @param array $params 検索パラメーターの配列 |
---|
[18815] | 306 | * @return array 商品規格の配列 |
---|
| 307 | */ |
---|
[18829] | 308 | function getProductsClassByQuery(&$objQuery, $params) { |
---|
[18815] | 309 | // 末端の規格を取得 |
---|
| 310 | $col = <<< __EOS__ |
---|
| 311 | T1.product_id, |
---|
| 312 | T1.stock, |
---|
| 313 | T1.stock_unlimited, |
---|
[20790] | 314 | T1.sale_limit, |
---|
[18815] | 315 | T1.price01, |
---|
| 316 | T1.price02, |
---|
| 317 | T1.point_rate, |
---|
| 318 | T1.product_code, |
---|
| 319 | T1.product_class_id, |
---|
| 320 | T1.del_flg, |
---|
[18882] | 321 | T1.product_type_id, |
---|
[18819] | 322 | T1.down_filename, |
---|
| 323 | T1.down_realfilename, |
---|
[18815] | 324 | T2.class_combination_id, |
---|
| 325 | T2.parent_class_combination_id, |
---|
| 326 | T2.classcategory_id, |
---|
| 327 | T2.level, |
---|
[18852] | 328 | T3.name AS classcategory_name, |
---|
[20595] | 329 | T3.rank, |
---|
[18815] | 330 | T4.name AS class_name, |
---|
| 331 | T4.class_id |
---|
| 332 | __EOS__; |
---|
| 333 | $table = <<< __EOS__ |
---|
| 334 | dtb_products_class T1 |
---|
| 335 | LEFT JOIN dtb_class_combination T2 |
---|
| 336 | ON T1.class_combination_id = T2.class_combination_id |
---|
| 337 | LEFT JOIN dtb_classcategory T3 |
---|
| 338 | ON T2.classcategory_id = T3.classcategory_id |
---|
| 339 | LEFT JOIN dtb_class T4 |
---|
| 340 | ON T3.class_id = T4.class_id |
---|
| 341 | __EOS__; |
---|
[20595] | 342 | |
---|
| 343 | $objQuery->setOrder('T3.rank DESC'); // XXX |
---|
[18829] | 344 | $arrRet = $objQuery->select($col, $table, "", $params); |
---|
[18815] | 345 | $levels = array(); |
---|
| 346 | $parents = array(); |
---|
[18819] | 347 | foreach ($arrRet as $rows) { |
---|
[18815] | 348 | $levels[] = $rows['level']; |
---|
| 349 | $parents[] = $rows['parent_class_combination_id']; |
---|
| 350 | } |
---|
| 351 | $level = max($levels); |
---|
| 352 | $parentsClass = array(); |
---|
| 353 | // 階層分の親を取得 |
---|
| 354 | for ($i = 0; $i < $level -1; $i++) { |
---|
[20507] | 355 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[18815] | 356 | $objQuery->setWhere('T1.class_combination_id IN (' . implode(', ', array_pad(array(), count($parents), '?')) . ')'); |
---|
| 357 | |
---|
| 358 | $col = <<< __EOS__ |
---|
| 359 | T1.class_combination_id, |
---|
| 360 | T1.classcategory_id, |
---|
| 361 | T1.parent_class_combination_id, |
---|
| 362 | T1.level, |
---|
[18852] | 363 | T2.name AS classcategory_name, |
---|
[20595] | 364 | T2.rank, |
---|
[18815] | 365 | T3.name AS class_name, |
---|
| 366 | T3.class_id |
---|
| 367 | __EOS__; |
---|
| 368 | $table = <<< __EOS__ |
---|
| 369 | dtb_class_combination T1 |
---|
| 370 | LEFT JOIN dtb_classcategory T2 |
---|
| 371 | ON T1.classcategory_id = T2.classcategory_id |
---|
| 372 | LEFT JOIN dtb_class T3 |
---|
| 373 | ON T2.class_id = T3.class_id |
---|
| 374 | __EOS__; |
---|
| 375 | |
---|
[20595] | 376 | $objQuery->setOrder('T2.rank DESC'); // XXX |
---|
[18815] | 377 | $arrParents = $objQuery->select($col, $table, "", $parents); |
---|
| 378 | |
---|
| 379 | foreach ($arrParents as $rows) { |
---|
| 380 | $parents[] = $rows['parent_class_combination_id']; |
---|
| 381 | |
---|
| 382 | foreach ($arrRet as $child) { |
---|
| 383 | if ($child['parent_class_combination_id'] |
---|
| 384 | == $rows['class_combination_id']) { |
---|
| 385 | $rows['product_id'] = $child['product_id']; |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | $tmpParents[] = $rows; |
---|
| 389 | } |
---|
| 390 | $parentsClass = array_merge($parentsClass, $tmpParents); |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | // 末端から枝を作成 |
---|
| 394 | $tmpClass = array_merge($arrRet, $parentsClass); |
---|
| 395 | |
---|
| 396 | foreach ($tmpClass as $val) { |
---|
| 397 | $val['class_id' . $val['level']] = $val['class_id']; |
---|
| 398 | $val['class_name' . $val['level']] = $val['class_name']; |
---|
[18852] | 399 | $val['classcategory_name' . $val['level']] = $val['classcategory_name']; |
---|
[18815] | 400 | $val['classcategory_id' . $val['level']] = $val['classcategory_id']; |
---|
| 401 | $arrProductsClass[] = $val; |
---|
| 402 | } |
---|
| 403 | return $arrProductsClass; |
---|
[18277] | 404 | } |
---|
[18815] | 405 | |
---|
| 406 | /** |
---|
[18829] | 407 | * 商品規格IDから商品規格を取得する. |
---|
[20595] | 408 | * |
---|
| 409 | * 削除された商品規格は取得しない. |
---|
| 410 | * |
---|
| 411 | * @param integer $productClassId 商品規格ID |
---|
| 412 | * @return array 商品規格の配列 |
---|
[18829] | 413 | */ |
---|
| 414 | function getProductsClass($productClassId) { |
---|
[20507] | 415 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[20595] | 416 | $objQuery->setWhere('product_class_id = ? AND T1.del_flg = 0'); |
---|
[18829] | 417 | $objQuery->setOrder("T2.level DESC"); |
---|
| 418 | $results = $this->getProductsClassByQuery($objQuery, $productClassId); |
---|
| 419 | $productsClass = $this->getProductsClassFull($results); |
---|
| 420 | return $productsClass[0]; |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | /** |
---|
| 424 | * 複数の商品IDに紐づいた, 商品規格を取得する. |
---|
| 425 | * |
---|
| 426 | * @param array $productIds 商品IDの配列 |
---|
[20595] | 427 | * @param boolean $has_deleted 削除された商品規格も含む場合 true; 初期値 false |
---|
[18829] | 428 | * @return array 商品規格の配列 |
---|
| 429 | */ |
---|
[20595] | 430 | function getProductsClassByProductIds($productIds = array(), $has_deleted = false) { |
---|
[18829] | 431 | if (empty($productIds)) { |
---|
| 432 | return array(); |
---|
| 433 | } |
---|
[20507] | 434 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[20595] | 435 | $where = 'product_id IN (' . implode(', ', array_pad(array(), count($productIds), '?')) . ')'; |
---|
| 436 | if (!$has_deleted) { |
---|
| 437 | $where .= ' AND T1.del_flg = 0'; |
---|
| 438 | } |
---|
| 439 | $objQuery->setWhere($where); |
---|
[18829] | 440 | $objQuery->setOrder("T2.level DESC"); |
---|
| 441 | return $this->getProductsClassByQuery($objQuery, $productIds); |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | /** |
---|
[18815] | 445 | * 商品IDに紐づいた, 商品規格を階層ごとに取得する. |
---|
| 446 | * |
---|
[18829] | 447 | * @param array $productId 商品ID |
---|
[18815] | 448 | * @return array 階層ごとの商品規格の配列 |
---|
| 449 | */ |
---|
| 450 | function getProductsClassLevelByProductId($productId) { |
---|
| 451 | $results = $this->getProductsClassByProductIds(array($productId)); |
---|
[18829] | 452 | return $this->getProductsClassLevel($results); |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | /** |
---|
| 456 | * 商品IDに紐づいた, 商品規格をすべての組み合わせごとに取得する. |
---|
| 457 | * |
---|
| 458 | * @param array $productId 商品ID |
---|
[20595] | 459 | * @param boolean $has_deleted 削除された商品規格も含む場合 true; 初期値 false |
---|
[18829] | 460 | * @return array すべての組み合わせの商品規格の配列 |
---|
| 461 | */ |
---|
[20595] | 462 | function getProductsClassFullByProductId($productId, $has_deleted = false) { |
---|
| 463 | $results = $this->getProductsClassByProductIds(array($productId), $has_deleted); |
---|
[18829] | 464 | return $this->getProductsClassFull($results); |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | /** |
---|
| 468 | * 商品規格の配列から, 商品規格を階層ごとに取得する. |
---|
| 469 | * |
---|
| 470 | * @access private |
---|
| 471 | * @param array $productsClassResults 商品規格の結果の配列 |
---|
| 472 | * @return array 階層ごとの商品規格の配列 |
---|
| 473 | */ |
---|
| 474 | function getProductsClassLevel($productsClassResults) { |
---|
| 475 | foreach ($productsClassResults as $row) { |
---|
[20538] | 476 | $productsClassLevel['level' . $row['level']][] = $row; |
---|
[18815] | 477 | } |
---|
| 478 | return $productsClassLevel; |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | /** |
---|
[18829] | 482 | * 商品規格の配列から, 商品規格のすべての組み合わせを取得する. |
---|
[18815] | 483 | * |
---|
[18829] | 484 | * @access private |
---|
| 485 | * @param array $productsClassResults 商品規格の結果の配列 |
---|
| 486 | * @ array 階層ごとの商品規格の配列 |
---|
[18815] | 487 | */ |
---|
[18829] | 488 | function getProductsClassFull($productsClassResults) { |
---|
| 489 | $results = $this->getProductsClassLevel($productsClassResults); |
---|
[18815] | 490 | $productsClass = array(); |
---|
[18829] | 491 | if (SC_Utils_Ex::isBlank($results["level1"]) |
---|
| 492 | && SC_Utils_Ex::isBlank($results["level2"])) { |
---|
[20538] | 493 | return $results['level']; |
---|
[18820] | 494 | } |
---|
| 495 | |
---|
[18815] | 496 | foreach ($results["level1"] as $level1) { |
---|
| 497 | foreach ($results["level2"] as $level2) { |
---|
| 498 | if ($level2['parent_class_combination_id'] == $level1['class_combination_id']) { |
---|
| 499 | $level1 = array_merge($level1, $level2); |
---|
| 500 | } |
---|
| 501 | } |
---|
| 502 | $productsClass[] = $level1; |
---|
| 503 | } |
---|
| 504 | return $productsClass; |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | /** |
---|
[18862] | 508 | * 商品IDをキーにした, 商品ステータスIDの配列を取得する. |
---|
| 509 | * |
---|
| 510 | * @param array 商品ID の配列 |
---|
| 511 | * @return array 商品IDをキーにした商品ステータスIDの配列 |
---|
| 512 | */ |
---|
| 513 | function getProductStatus($productIds) { |
---|
[20003] | 514 | if (empty($productIds)) { |
---|
| 515 | return array(); |
---|
| 516 | } |
---|
[20507] | 517 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[20003] | 518 | $cols = 'product_id, product_status_id'; |
---|
| 519 | $from = 'dtb_product_status'; |
---|
| 520 | $where = 'del_flg = 0 AND product_id IN (' . implode(', ', array_pad(array(), count($productIds), '?')) . ')'; |
---|
| 521 | $productStatus = $objQuery->select($cols, $from, $where, $productIds); |
---|
[18862] | 522 | $results = array(); |
---|
| 523 | foreach ($productStatus as $status) { |
---|
| 524 | $results[$status['product_id']][] = $status['product_status_id']; |
---|
| 525 | } |
---|
| 526 | return $results; |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | /** |
---|
[18863] | 530 | * 商品ステータスを設定する. |
---|
| 531 | * |
---|
| 532 | * TODO 現在は DELETE/INSERT だが, UPDATE を検討する. |
---|
| 533 | * |
---|
| 534 | * @param integer $productId 商品ID |
---|
| 535 | * @param array $productStatusIds ON にする商品ステータスIDの配列 |
---|
| 536 | */ |
---|
| 537 | function setProductStatus($productId, $productStatusIds) { |
---|
| 538 | |
---|
| 539 | $val['product_id'] = $productId; |
---|
| 540 | $val['creator_id'] = $_SESSION['member_id']; |
---|
[21185] | 541 | $val['create_date'] = 'CURRENT_TIMESTAMP'; |
---|
| 542 | $val['update_date'] = 'CURRENT_TIMESTAMP'; |
---|
[18863] | 543 | $val['del_flg'] = '0'; |
---|
| 544 | |
---|
[20507] | 545 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[18863] | 546 | $objQuery->delete('dtb_product_status', 'product_id = ?', array($productId)); |
---|
| 547 | foreach ($productStatusIds as $productStatusId) { |
---|
[20386] | 548 | if($productStatusId == '') continue; |
---|
[18863] | 549 | $val['product_status_id'] = $productStatusId; |
---|
| 550 | $objQuery->insert('dtb_product_status', $val); |
---|
| 551 | } |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | /** |
---|
[18852] | 555 | * 商品詳細の結果から, 購入制限数を取得する. |
---|
| 556 | * |
---|
| 557 | * getDetailAndProductsClass() の結果から, 購入制限数を取得する. |
---|
| 558 | * |
---|
| 559 | * @param array $p 商品詳細の検索結果の配列 |
---|
| 560 | * @return integer 商品詳細の結果から求めた購入制限数. |
---|
| 561 | * @see getDetailAndProductsClass() |
---|
| 562 | */ |
---|
| 563 | function getBuyLimit($p) { |
---|
| 564 | $limit = null; |
---|
| 565 | if ($p['stock_unlimited'] != '1' && is_numeric($p['sale_limit'])) { |
---|
| 566 | $limit = min($p['sale_limit'], $p['stock']); |
---|
| 567 | } elseif (is_numeric($p['sale_limit'])) { |
---|
| 568 | $limit = $p['sale_limit']; |
---|
| 569 | } elseif ($p['stock_unlimited'] != '1') { |
---|
| 570 | $limit = $p['stock']; |
---|
| 571 | } |
---|
| 572 | return $limit; |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | /** |
---|
[18860] | 576 | * 在庫を減少させる. |
---|
| 577 | * |
---|
| 578 | * 指定の在庫数まで, 在庫を減少させる. |
---|
| 579 | * 減少させた結果, 在庫数が 0 未満になった場合, 引数 $quantity が 0 の場合は, |
---|
| 580 | * 在庫の減少を中止し, false を返す. |
---|
| 581 | * 在庫の減少に成功した場合は true を返す. |
---|
| 582 | * |
---|
| 583 | * @param integer $productClassId 商品規格ID |
---|
| 584 | * @param integer $quantity 減少させる在庫数 |
---|
| 585 | * @return boolean 在庫の減少に成功した場合 true; 失敗した場合 false |
---|
| 586 | */ |
---|
| 587 | function reduceStock($productClassId, $quantity) { |
---|
| 588 | |
---|
[21005] | 589 | if ($quantity == 0) { |
---|
[18860] | 590 | return false; |
---|
| 591 | } |
---|
| 592 | |
---|
[20507] | 593 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[18860] | 594 | $objQuery->update('dtb_products_class', array(), |
---|
| 595 | "product_class_id = ?", array($productClassId), |
---|
| 596 | array('stock' => 'stock - ?'), array($quantity)); |
---|
| 597 | // TODO エラーハンドリング |
---|
[21005] | 598 | |
---|
| 599 | $productsClass = $this->getDetailAndProductsClass($productClassId); |
---|
| 600 | if ($productsClass['stock_unlimited'] != '1' && $productsClass['stock'] < 0) { |
---|
| 601 | return false; |
---|
| 602 | } |
---|
| 603 | |
---|
[18860] | 604 | return true; |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | /** |
---|
[21131] | 608 | * 商品情報の配列に, 税込金額を設定して返す. |
---|
| 609 | * |
---|
| 610 | * この関数は, 主にスマートフォンで使用します. |
---|
| 611 | * |
---|
| 612 | * @param array $arrProducts 商品情報の配列 |
---|
| 613 | * @return array 税込金額を設定した商品情報の配列 |
---|
| 614 | */ |
---|
| 615 | function setPriceTaxTo($arrProducts) { |
---|
| 616 | foreach ($arrProducts as $key=>$val) { |
---|
| 617 | $arrProducts[$key]['price01_min_format'] = number_format($arrProducts[$key]['price01_min']); |
---|
| 618 | $arrProducts[$key]['price01_max_format'] = number_format($arrProducts[$key]['price01_max']); |
---|
| 619 | $arrProducts[$key]['price02_min_format'] = number_format($arrProducts[$key]['price02_min']); |
---|
| 620 | $arrProducts[$key]['price02_max_format'] = number_format($arrProducts[$key]['price02_max']); |
---|
| 621 | |
---|
| 622 | $arrProducts[$key]['price01_min_tax'] = SC_Helper_DB::sfCalcIncTax($arrProducts[$key]['price01_min']); |
---|
| 623 | $arrProducts[$key]['price01_max_tax'] = SC_Helper_DB::sfCalcIncTax($arrProducts[$key]['price01_max']); |
---|
| 624 | $arrProducts[$key]['price02_min_tax'] = SC_Helper_DB::sfCalcIncTax($arrProducts[$key]['price02_min']); |
---|
| 625 | $arrProducts[$key]['price02_max_tax'] = SC_Helper_DB::sfCalcIncTax($arrProducts[$key]['price02_max']); |
---|
| 626 | |
---|
| 627 | $arrProducts[$key]['price01_min_tax_format'] = number_format($arrProducts[$key]['price01_min_tax']); |
---|
| 628 | $arrProducts[$key]['price01_max_tax_format'] = number_format($arrProducts[$key]['price01_max_tax']); |
---|
| 629 | $arrProducts[$key]['price02_min_tax_format'] = number_format($arrProducts[$key]['price02_min_tax']); |
---|
| 630 | $arrProducts[$key]['price02_max_tax_format'] = number_format($arrProducts[$key]['price02_max_tax']); |
---|
| 631 | } |
---|
| 632 | return $arrProducts; |
---|
| 633 | } |
---|
| 634 | |
---|
| 635 | /** |
---|
[18815] | 636 | * 商品詳細の SQL を取得する. |
---|
| 637 | * |
---|
| 638 | * @param string $where 商品詳細の WHERE 句 |
---|
| 639 | * @return string 商品詳細の SQL |
---|
| 640 | */ |
---|
| 641 | function alldtlSQL($where = "") { |
---|
[21239] | 642 | $where_clause = ""; |
---|
[18815] | 643 | if (!SC_Utils_Ex::isBlank($where)) { |
---|
[21239] | 644 | $where_clause = " WHERE " . $where; |
---|
[18815] | 645 | } |
---|
[18821] | 646 | /* |
---|
[20877] | 647 | * point_rate, deliv_fee は商品規格(dtb_products_class)ごとに保持しているが, |
---|
[18821] | 648 | * 商品(dtb_products)ごとの設定なので MAX のみを取得する. |
---|
| 649 | */ |
---|
[18815] | 650 | $sql = <<< __EOS__ |
---|
| 651 | ( |
---|
[20916] | 652 | SELECT 0 |
---|
| 653 | ,dtb_products.product_id |
---|
| 654 | ,dtb_products.name |
---|
| 655 | ,dtb_products.maker_id |
---|
| 656 | ,dtb_products.status |
---|
| 657 | ,dtb_products.comment1 |
---|
| 658 | ,dtb_products.comment2 |
---|
| 659 | ,dtb_products.comment3 |
---|
| 660 | ,dtb_products.comment4 |
---|
| 661 | ,dtb_products.comment5 |
---|
| 662 | ,dtb_products.comment6 |
---|
| 663 | ,dtb_products.note |
---|
| 664 | ,dtb_products.main_list_comment |
---|
| 665 | ,dtb_products.main_list_image |
---|
| 666 | ,dtb_products.main_comment |
---|
| 667 | ,dtb_products.main_image |
---|
| 668 | ,dtb_products.main_large_image |
---|
| 669 | ,dtb_products.sub_title1 |
---|
| 670 | ,dtb_products.sub_comment1 |
---|
| 671 | ,dtb_products.sub_image1 |
---|
| 672 | ,dtb_products.sub_large_image1 |
---|
| 673 | ,dtb_products.sub_title2 |
---|
| 674 | ,dtb_products.sub_comment2 |
---|
| 675 | ,dtb_products.sub_image2 |
---|
| 676 | ,dtb_products.sub_large_image2 |
---|
| 677 | ,dtb_products.sub_title3 |
---|
| 678 | ,dtb_products.sub_comment3 |
---|
| 679 | ,dtb_products.sub_image3 |
---|
| 680 | ,dtb_products.sub_large_image3 |
---|
| 681 | ,dtb_products.sub_title4 |
---|
| 682 | ,dtb_products.sub_comment4 |
---|
| 683 | ,dtb_products.sub_image4 |
---|
| 684 | ,dtb_products.sub_large_image4 |
---|
| 685 | ,dtb_products.sub_title5 |
---|
| 686 | ,dtb_products.sub_comment5 |
---|
| 687 | ,dtb_products.sub_image5 |
---|
| 688 | ,dtb_products.sub_large_image5 |
---|
| 689 | ,dtb_products.sub_title6 |
---|
| 690 | ,dtb_products.sub_comment6 |
---|
| 691 | ,dtb_products.sub_image6 |
---|
| 692 | ,dtb_products.sub_large_image6 |
---|
| 693 | ,dtb_products.del_flg |
---|
| 694 | ,dtb_products.creator_id |
---|
| 695 | ,dtb_products.create_date |
---|
| 696 | ,dtb_products.update_date |
---|
| 697 | ,dtb_products.deliv_date_id |
---|
| 698 | ,T4.product_code_min |
---|
| 699 | ,T4.product_code_max |
---|
| 700 | ,T4.price01_min |
---|
| 701 | ,T4.price01_max |
---|
| 702 | ,T4.price02_min |
---|
| 703 | ,T4.price02_max |
---|
| 704 | ,T4.stock_min |
---|
| 705 | ,T4.stock_max |
---|
| 706 | ,T4.stock_unlimited_min |
---|
| 707 | ,T4.stock_unlimited_max |
---|
| 708 | ,T4.point_rate |
---|
| 709 | ,T4.deliv_fee |
---|
| 710 | ,T4.class_count |
---|
| 711 | ,dtb_maker.name AS maker_name |
---|
| 712 | FROM dtb_products |
---|
| 713 | JOIN ( |
---|
[18815] | 714 | SELECT product_id, |
---|
| 715 | MIN(product_code) AS product_code_min, |
---|
| 716 | MAX(product_code) AS product_code_max, |
---|
| 717 | MIN(price01) AS price01_min, |
---|
| 718 | MAX(price01) AS price01_max, |
---|
| 719 | MIN(price02) AS price02_min, |
---|
| 720 | MAX(price02) AS price02_max, |
---|
| 721 | MIN(stock) AS stock_min, |
---|
| 722 | MAX(stock) AS stock_max, |
---|
| 723 | MIN(stock_unlimited) AS stock_unlimited_min, |
---|
| 724 | MAX(stock_unlimited) AS stock_unlimited_max, |
---|
[18821] | 725 | MAX(point_rate) AS point_rate, |
---|
[20877] | 726 | MAX(deliv_fee) AS deliv_fee, |
---|
[18815] | 727 | COUNT(*) as class_count |
---|
[20916] | 728 | FROM dtb_products_class |
---|
[21239] | 729 | $where_clause |
---|
[20916] | 730 | GROUP BY product_id |
---|
| 731 | ) AS T4 |
---|
| 732 | ON dtb_products.product_id = T4.product_id |
---|
| 733 | LEFT JOIN dtb_maker |
---|
| 734 | ON dtb_products.maker_id = dtb_maker.maker_id |
---|
| 735 | ) AS alldtl |
---|
[18815] | 736 | __EOS__; |
---|
| 737 | return $sql; |
---|
| 738 | } |
---|
[18826] | 739 | |
---|
| 740 | /** |
---|
[19742] | 741 | * 商品規格詳細の SQL を取得する. |
---|
| 742 | * |
---|
| 743 | * MEMO: 2.4系 vw_product_classに相当(?)するイメージ |
---|
| 744 | * |
---|
| 745 | * @param string $where 商品詳細の WHERE 句 |
---|
| 746 | * @return string 商品規格詳細の SQL |
---|
| 747 | */ |
---|
| 748 | function prdclsSQL($where = "") { |
---|
[21239] | 749 | $where_clause = ""; |
---|
[19742] | 750 | if (!SC_Utils_Ex::isBlank($where)) { |
---|
[21239] | 751 | $where_clause = " WHERE " . $where; |
---|
[19742] | 752 | } |
---|
| 753 | $sql = <<< __EOS__ |
---|
| 754 | ( |
---|
| 755 | SELECT dtb_products.*, |
---|
| 756 | dtb_products_class.product_class_id, |
---|
| 757 | dtb_products_class.class_combination_id, |
---|
| 758 | dtb_products_class.product_type_id, |
---|
| 759 | dtb_products_class.product_code, |
---|
| 760 | dtb_products_class.stock, |
---|
| 761 | dtb_products_class.stock_unlimited, |
---|
| 762 | dtb_products_class.sale_limit, |
---|
| 763 | dtb_products_class.price01, |
---|
| 764 | dtb_products_class.price02, |
---|
| 765 | dtb_products_class.deliv_fee, |
---|
| 766 | dtb_products_class.point_rate, |
---|
| 767 | dtb_products_class.down_filename, |
---|
| 768 | dtb_products_class.down_realfilename, |
---|
| 769 | dtb_class_combination.parent_class_combination_id, |
---|
| 770 | dtb_class_combination.classcategory_id, |
---|
| 771 | dtb_class_combination.level as classlevel, |
---|
| 772 | Tpcm.classcategory_id as parent_classcategory_id, |
---|
| 773 | Tpcm.level as parent_classlevel, |
---|
| 774 | Tcc1.class_id as class_id, |
---|
| 775 | Tcc1.name as classcategory_name, |
---|
| 776 | Tcc2.class_id as parent_class_id, |
---|
| 777 | Tcc2.name as parent_classcategory_name |
---|
| 778 | FROM dtb_products |
---|
| 779 | LEFT JOIN dtb_products_class |
---|
| 780 | ON dtb_products.product_id = dtb_products_class.product_id |
---|
| 781 | LEFT JOIN dtb_class_combination |
---|
| 782 | ON dtb_products_class.class_combination_id = dtb_class_combination.class_combination_id |
---|
| 783 | LEFT JOIN dtb_class_combination as Tpcm |
---|
| 784 | ON dtb_class_combination.parent_class_combination_id = Tpcm.class_combination_id |
---|
| 785 | LEFT JOIN dtb_classcategory as Tcc1 |
---|
| 786 | ON dtb_class_combination.classcategory_id = Tcc1.classcategory_id |
---|
| 787 | LEFT JOIN dtb_classcategory as Tcc2 |
---|
| 788 | ON Tpcm.classcategory_id = Tcc2.classcategory_id |
---|
[21239] | 789 | $where_clause |
---|
[19742] | 790 | ) as prdcls |
---|
| 791 | __EOS__; |
---|
| 792 | return $sql; |
---|
| 793 | } |
---|
[20148] | 794 | |
---|
[19742] | 795 | /** |
---|
[18826] | 796 | * 商品規格ID1、2に紐づいた,product_class_idを取得する. |
---|
| 797 | * |
---|
| 798 | * @param int $productId 商品ID |
---|
| 799 | * @param int $classcategory_id1 商品規格ID1 |
---|
| 800 | * @param int $classcategory_id2 商品規格ID2 |
---|
| 801 | * @return string product_class_id |
---|
| 802 | */ |
---|
| 803 | function getClasscategoryIdsByProductClassId($productId, $classcategory_id1, $classcategory_id2) { |
---|
[20507] | 804 | $objQuery = new SC_Query_Ex(); |
---|
[18826] | 805 | $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"; |
---|
| 806 | $table = <<< __EOS__ |
---|
| 807 | (SELECT |
---|
| 808 | pc.product_code AS product_code, |
---|
| 809 | pc.product_id AS product_id, |
---|
| 810 | pc.product_class_id AS product_class_id, |
---|
| 811 | pc.class_combination_id AS class_combination_id, |
---|
| 812 | COALESCE(cc2.classcategory_id,0) AS classcategory_id1, |
---|
| 813 | COALESCE(cc1.classcategory_id,0) AS classcategory_id2 |
---|
| 814 | FROM |
---|
| 815 | dtb_products_class pc LEFT JOIN dtb_class_combination cc1 ON pc.class_combination_id = cc1.class_combination_id |
---|
| 816 | LEFT JOIN dtb_class_combination cc2 ON cc1.parent_class_combination_id = cc2.class_combination_id) T1 |
---|
| 817 | __EOS__; |
---|
| 818 | $where = "T1.product_id = ? AND T1.classcategory_id1 = ? AND T1.classcategory_id2 = ?"; |
---|
| 819 | $arrRet = $objQuery->select($col, $table, $where, |
---|
| 820 | array($productId, $classcategory_id1, $classcategory_id2)); |
---|
| 821 | return $arrRet[0]['product_class_id']; |
---|
| 822 | } |
---|
| 823 | |
---|
[18277] | 824 | } |
---|
| 825 | ?> |
---|