| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2014 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 | public $arrClassName; |
|---|
| 35 | /** 規格分類名一覧 */ |
|---|
| 36 | public $arrClassCatName; |
|---|
| 37 | /** このプロパティが保持する price01 及び price02 は、税金付与した金額である。 */ |
|---|
| 38 | public $classCategories = array(); |
|---|
| 39 | public $stock_find; |
|---|
| 40 | /** 規格1クラス名 */ |
|---|
| 41 | public $className1 = ''; |
|---|
| 42 | /** 規格2クラス名 */ |
|---|
| 43 | public $className2 = ''; |
|---|
| 44 | /** 規格1が設定されている */ |
|---|
| 45 | public $classCat1_find; |
|---|
| 46 | /** 規格2が設定されている */ |
|---|
| 47 | public $classCat2_find; |
|---|
| 48 | public $classCats1; |
|---|
| 49 | /** 検索用並び替え条件配列 */ |
|---|
| 50 | public $arrOrderData; |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * 商品検索結果の並び順を指定する。 |
|---|
| 54 | * |
|---|
| 55 | * ただし指定できるテーブルはproduct_idを持っているテーブルであることが必要. |
|---|
| 56 | * |
|---|
| 57 | * @param string $col 並び替えの基準とするフィールド |
|---|
| 58 | * @param string $table 並び替えの基準とするフィールドがあるテーブル |
|---|
| 59 | * @param string $order 並び替えの順序 ASC / DESC |
|---|
| 60 | * @return void |
|---|
| 61 | */ |
|---|
| 62 | public function setProductsOrder($col, $table = 'dtb_products', $order = 'ASC') |
|---|
| 63 | { |
|---|
| 64 | $this->arrOrderData = array('col' => $col, 'table' => $table, 'order' => $order); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * SC_Queryインスタンスに設定された検索条件を元に並び替え済みの検索結果商品IDの配列を取得する。 |
|---|
| 69 | * |
|---|
| 70 | * 検索条件は, SC_Query::setWhere() 関数で設定しておく必要があります. |
|---|
| 71 | * |
|---|
| 72 | * @param SC_Query $objQuery SC_Query インスタンス |
|---|
| 73 | * @param array $arrVal 検索パラメーターの配列 |
|---|
| 74 | * @return array 商品IDの配列 |
|---|
| 75 | */ |
|---|
| 76 | public function findProductIdsOrder(&$objQuery, $arrVal = array()) |
|---|
| 77 | { |
|---|
| 78 | $table = 'dtb_products AS alldtl'; |
|---|
| 79 | |
|---|
| 80 | if (is_array($this->arrOrderData) and $objQuery->order == '') { |
|---|
| 81 | $o_col = $this->arrOrderData['col']; |
|---|
| 82 | $o_table = $this->arrOrderData['table']; |
|---|
| 83 | $o_order = $this->arrOrderData['order']; |
|---|
| 84 | $objQuery->setOrder("T2.$o_col $o_order"); |
|---|
| 85 | $sub_sql = $objQuery->getSql($o_col, "$o_table AS T2", 'T2.product_id = alldtl.product_id'); |
|---|
| 86 | $sub_sql = $objQuery->dbFactory->addLimitOffset($sub_sql, 1); |
|---|
| 87 | |
|---|
| 88 | $objQuery->setOrder("($sub_sql) $o_order, product_id"); |
|---|
| 89 | } |
|---|
| 90 | $arrReturn = $objQuery->getCol('alldtl.product_id', $table, '', $arrVal); |
|---|
| 91 | |
|---|
| 92 | return $arrReturn; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * SC_Queryインスタンスに設定された検索条件をもとに対象商品数を取得する. |
|---|
| 97 | * |
|---|
| 98 | * 検索条件は, SC_Query::setWhere() 関数で設定しておく必要があります. |
|---|
| 99 | * |
|---|
| 100 | * @param SC_Query $objQuery SC_Query インスタンス |
|---|
| 101 | * @param array $arrVal 検索パラメーターの配列 |
|---|
| 102 | * @return integer 対象商品ID数 |
|---|
| 103 | */ |
|---|
| 104 | public function findProductCount(&$objQuery, $arrVal = array()) |
|---|
| 105 | { |
|---|
| 106 | $table = 'dtb_products AS alldtl'; |
|---|
| 107 | |
|---|
| 108 | return $objQuery->count($table, '', $arrVal); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * SC_Queryインスタンスに設定された検索条件をもとに商品一覧の配列を取得する. |
|---|
| 113 | * |
|---|
| 114 | * 主に SC_Product::findProductIds() で取得した商品IDを検索条件にし, |
|---|
| 115 | * SC_Query::setOrder() や SC_Query::setLimitOffset() を設定して, 商品一覧 |
|---|
| 116 | * の配列を取得する. |
|---|
| 117 | * |
|---|
| 118 | * @param SC_Query $objQuery SC_Query インスタンス |
|---|
| 119 | * @return array 商品一覧の配列 |
|---|
| 120 | */ |
|---|
| 121 | public function lists(&$objQuery) |
|---|
| 122 | { |
|---|
| 123 | $col = <<< __EOS__ |
|---|
| 124 | product_id |
|---|
| 125 | ,product_code_min |
|---|
| 126 | ,product_code_max |
|---|
| 127 | ,name |
|---|
| 128 | ,comment1 |
|---|
| 129 | ,comment2 |
|---|
| 130 | ,comment3 |
|---|
| 131 | ,main_list_comment |
|---|
| 132 | ,main_image |
|---|
| 133 | ,main_list_image |
|---|
| 134 | ,price01_min |
|---|
| 135 | ,price01_max |
|---|
| 136 | ,price02_min |
|---|
| 137 | ,price02_max |
|---|
| 138 | ,stock_min |
|---|
| 139 | ,stock_max |
|---|
| 140 | ,stock_unlimited_min |
|---|
| 141 | ,stock_unlimited_max |
|---|
| 142 | ,deliv_date_id |
|---|
| 143 | ,status |
|---|
| 144 | ,del_flg |
|---|
| 145 | ,update_date |
|---|
| 146 | __EOS__; |
|---|
| 147 | $res = $objQuery->select($col, $this->alldtlSQL()); |
|---|
| 148 | |
|---|
| 149 | return $res; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * 商品IDを指定し、商品一覧を取得する |
|---|
| 154 | * |
|---|
| 155 | * SC_Query::setOrder() や SC_Query::setLimitOffset() を設定して, 商品一覧 |
|---|
| 156 | * の配列を取得する. |
|---|
| 157 | * FIXME: 呼び出し元で設定した、SC_Query::setWhere() も有効に扱いたい。 |
|---|
| 158 | * |
|---|
| 159 | * @param SC_Query $objQuery SC_Query インスタンス |
|---|
| 160 | * @param array $arrProductId 商品ID |
|---|
| 161 | * @return array 商品一覧の配列 (キー: 商品ID) |
|---|
| 162 | */ |
|---|
| 163 | public function getListByProductIds(&$objQuery, $arrProductId = array()) |
|---|
| 164 | { |
|---|
| 165 | if (empty($arrProductId)) { |
|---|
| 166 | return array(); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | $where = 'alldtl.product_id IN (' . SC_Utils_Ex::repeatStrWithSeparator('?', count($arrProductId)) . ')'; |
|---|
| 170 | $where .= ' AND alldtl.del_flg = 0'; |
|---|
| 171 | |
|---|
| 172 | $objQuery->setWhere($where, $arrProductId); |
|---|
| 173 | $arrProducts = $this->lists($objQuery); |
|---|
| 174 | |
|---|
| 175 | // 配列のキーを商品IDに |
|---|
| 176 | $arrProducts = SC_Utils_Ex::makeArrayIDToKey('product_id', $arrProducts); |
|---|
| 177 | |
|---|
| 178 | // SC_Query::setOrder() の指定がない場合、$arrProductId で指定された商品IDの順に配列要素を並び替え |
|---|
| 179 | if (strlen($objQuery->order) === 0) { |
|---|
| 180 | $arrTmp = array(); |
|---|
| 181 | foreach ($arrProductId as $product_id) { |
|---|
| 182 | $arrTmp[$product_id] = $arrProducts[$product_id]; |
|---|
| 183 | } |
|---|
| 184 | $arrProducts =& $arrTmp; |
|---|
| 185 | unset($arrTmp); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | // 税込金額を設定する |
|---|
| 189 | SC_Product_Ex::setIncTaxToProducts($arrProducts); |
|---|
| 190 | |
|---|
| 191 | return $arrProducts; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | /** |
|---|
| 195 | * 商品詳細を取得する. |
|---|
| 196 | * |
|---|
| 197 | * @param integer $product_id 商品ID |
|---|
| 198 | * @return array 商品詳細情報の配列 |
|---|
| 199 | */ |
|---|
| 200 | public function getDetail($product_id) |
|---|
| 201 | { |
|---|
| 202 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 203 | |
|---|
| 204 | $from = $this->alldtlSQL(); |
|---|
| 205 | $where = 'product_id = ?'; |
|---|
| 206 | $arrWhereVal = array($product_id); |
|---|
| 207 | $arrProduct = (array)$objQuery->getRow('*', $from, $where, $arrWhereVal); |
|---|
| 208 | |
|---|
| 209 | // 税込金額を設定する |
|---|
| 210 | SC_Product_Ex::setIncTaxToProduct($arrProduct); |
|---|
| 211 | |
|---|
| 212 | return $arrProduct; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | /** |
|---|
| 216 | * 商品詳細情報と商品規格を取得する. |
|---|
| 217 | * |
|---|
| 218 | * @param integer $productClassId 商品規格ID |
|---|
| 219 | * @return array 商品詳細情報と商品規格の配列 |
|---|
| 220 | */ |
|---|
| 221 | public function getDetailAndProductsClass($productClassId) |
|---|
| 222 | { |
|---|
| 223 | $result = $this->getProductsClass($productClassId); |
|---|
| 224 | $result = array_merge($result, $this->getDetail($result['product_id'])); |
|---|
| 225 | |
|---|
| 226 | return $result; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | /** |
|---|
| 230 | * 商品IDに紐づく商品規格を自分自身に設定する. |
|---|
| 231 | * |
|---|
| 232 | * 引数の商品IDの配列に紐づく商品規格を取得し, 自分自身のフィールドに |
|---|
| 233 | * 設定する. |
|---|
| 234 | * |
|---|
| 235 | * @param array $arrProductId 商品ID の配列 |
|---|
| 236 | * @param boolean $has_deleted 削除された商品規格も含む場合 true; 初期値 false |
|---|
| 237 | * @return void |
|---|
| 238 | */ |
|---|
| 239 | public function setProductsClassByProductIds($arrProductId, $has_deleted = false) |
|---|
| 240 | { |
|---|
| 241 | foreach ($arrProductId as $productId) { |
|---|
| 242 | $arrProductClasses = $this->getProductsClassFullByProductId($productId, $has_deleted); |
|---|
| 243 | |
|---|
| 244 | $classCats1 = array(); |
|---|
| 245 | $classCats1['__unselected'] = '選択してください'; |
|---|
| 246 | |
|---|
| 247 | // 規格1クラス名 |
|---|
| 248 | $this->className1[$productId] = |
|---|
| 249 | isset($arrProductClasses[0]['class_name1']) |
|---|
| 250 | ? $arrProductClasses[0]['class_name1'] |
|---|
| 251 | : ''; |
|---|
| 252 | |
|---|
| 253 | // 規格2クラス名 |
|---|
| 254 | $this->className2[$productId] = |
|---|
| 255 | isset($arrProductClasses[0]['class_name2']) |
|---|
| 256 | ? $arrProductClasses[0]['class_name2'] |
|---|
| 257 | : ''; |
|---|
| 258 | |
|---|
| 259 | // 規格1が設定されている |
|---|
| 260 | $this->classCat1_find[$productId] = $arrProductClasses[0]['classcategory_id1'] > 0; // 要変更ただし、他にも改修が必要となる |
|---|
| 261 | // 規格2が設定されている |
|---|
| 262 | $this->classCat2_find[$productId] = $arrProductClasses[0]['classcategory_id2'] > 0; // 要変更ただし、他にも改修が必要となる |
|---|
| 263 | |
|---|
| 264 | $this->stock_find[$productId] = false; |
|---|
| 265 | $classCategories = array(); |
|---|
| 266 | $classCategories['__unselected']['__unselected']['name'] = '選択してください'; |
|---|
| 267 | $classCategories['__unselected']['__unselected']['product_class_id'] = $arrProductClasses[0]['product_class_id']; |
|---|
| 268 | // 商品種別 |
|---|
| 269 | $classCategories['__unselected']['__unselected']['product_type'] = $arrProductClasses[0]['product_type_id']; |
|---|
| 270 | $this->product_class_id[$productId] = $arrProductClasses[0]['product_class_id']; |
|---|
| 271 | // 商品種別 |
|---|
| 272 | $this->product_type[$productId] = $arrProductClasses[0]['product_type_id']; |
|---|
| 273 | foreach ($arrProductClasses as $arrProductsClass) { |
|---|
| 274 | $arrClassCats2 = array(); |
|---|
| 275 | $classcategory_id1 = $arrProductsClass['classcategory_id1']; |
|---|
| 276 | $classcategory_id2 = $arrProductsClass['classcategory_id2']; |
|---|
| 277 | // 在庫 |
|---|
| 278 | $stock_find_class = ($arrProductsClass['stock_unlimited'] || $arrProductsClass['stock'] > 0); |
|---|
| 279 | |
|---|
| 280 | $arrClassCats2['classcategory_id2'] = $classcategory_id2; |
|---|
| 281 | $arrClassCats2['name'] = $arrProductsClass['classcategory_name2'] . ($stock_find_class ? '' : ' (品切れ中)'); |
|---|
| 282 | |
|---|
| 283 | $arrClassCats2['stock_find'] = $stock_find_class; |
|---|
| 284 | |
|---|
| 285 | if ($stock_find_class) { |
|---|
| 286 | $this->stock_find[$productId] = true; |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | if (!in_array($classcategory_id1, $classCats1)) { |
|---|
| 290 | $classCats1[$classcategory_id1] = $arrProductsClass['classcategory_name1'] |
|---|
| 291 | . ($classcategory_id2 == 0 && !$stock_find_class ? ' (品切れ中)' : ''); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | // 価格 |
|---|
| 295 | // TODO: ここでprice01,price02を税込みにしてよいのか? _inctax を付けるべき?要検証 |
|---|
| 296 | $arrClassCats2['price01'] |
|---|
| 297 | = strlen($arrProductsClass['price01']) |
|---|
| 298 | ? number_format(SC_Helper_TaxRule_Ex::sfCalcIncTax($arrProductsClass['price01'], $productId, $arrProductsClass['product_class_id'])) |
|---|
| 299 | : ''; |
|---|
| 300 | |
|---|
| 301 | $arrClassCats2['price02'] |
|---|
| 302 | = strlen($arrProductsClass['price02']) |
|---|
| 303 | ? number_format(SC_Helper_TaxRule_Ex::sfCalcIncTax($arrProductsClass['price02'], $productId, $arrProductsClass['product_class_id'])) |
|---|
| 304 | : ''; |
|---|
| 305 | |
|---|
| 306 | // ポイント |
|---|
| 307 | $arrClassCats2['point'] |
|---|
| 308 | = number_format(SC_Utils_Ex::sfPrePoint($arrProductsClass['price02'], $arrProductsClass['point_rate'])); |
|---|
| 309 | |
|---|
| 310 | // 商品コード |
|---|
| 311 | $arrClassCats2['product_code'] = $arrProductsClass['product_code']; |
|---|
| 312 | // 商品規格ID |
|---|
| 313 | $arrClassCats2['product_class_id'] = $arrProductsClass['product_class_id']; |
|---|
| 314 | // 商品種別 |
|---|
| 315 | $arrClassCats2['product_type'] = $arrProductsClass['product_type_id']; |
|---|
| 316 | |
|---|
| 317 | // #929(GC8 規格のプルダウン順序表示不具合)対応のため、2次キーは「#」を前置 |
|---|
| 318 | if (!$this->classCat1_find[$productId]) { |
|---|
| 319 | $classcategory_id1 = '__unselected2'; |
|---|
| 320 | } |
|---|
| 321 | $classCategories[$classcategory_id1]['#'] = array( |
|---|
| 322 | 'classcategory_id2' => '', |
|---|
| 323 | 'name' => '選択してください', |
|---|
| 324 | ); |
|---|
| 325 | $classCategories[$classcategory_id1]['#' . $classcategory_id2] = $arrClassCats2; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | $this->classCategories[$productId] = $classCategories; |
|---|
| 329 | |
|---|
| 330 | // 規格1 |
|---|
| 331 | $this->classCats1[$productId] = $classCats1; |
|---|
| 332 | } |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | /** |
|---|
| 336 | * SC_Query インスタンスに設定された検索条件を使用して商品規格を取得する. |
|---|
| 337 | * |
|---|
| 338 | * @param SC_Query $objQuery SC_Queryインスタンス |
|---|
| 339 | * @param array $params 検索パラメーターの配列 |
|---|
| 340 | * @return array 商品規格の配列 |
|---|
| 341 | */ |
|---|
| 342 | public function getProductsClassByQuery(&$objQuery, $params) |
|---|
| 343 | { |
|---|
| 344 | // 末端の規格を取得 |
|---|
| 345 | $col = <<< __EOS__ |
|---|
| 346 | T1.product_id, |
|---|
| 347 | T1.stock, |
|---|
| 348 | T1.stock_unlimited, |
|---|
| 349 | T1.sale_limit, |
|---|
| 350 | T1.price01, |
|---|
| 351 | T1.price02, |
|---|
| 352 | T1.point_rate, |
|---|
| 353 | T1.product_code, |
|---|
| 354 | T1.product_class_id, |
|---|
| 355 | T1.del_flg, |
|---|
| 356 | T1.product_type_id, |
|---|
| 357 | T1.down_filename, |
|---|
| 358 | T1.down_realfilename, |
|---|
| 359 | T3.name AS classcategory_name1, |
|---|
| 360 | T3.rank AS rank1, |
|---|
| 361 | T4.name AS class_name1, |
|---|
| 362 | T4.class_id AS class_id1, |
|---|
| 363 | T1.classcategory_id1, |
|---|
| 364 | T1.classcategory_id2, |
|---|
| 365 | dtb_classcategory2.name AS classcategory_name2, |
|---|
| 366 | dtb_classcategory2.rank AS rank2, |
|---|
| 367 | dtb_class2.name AS class_name2, |
|---|
| 368 | dtb_class2.class_id AS class_id2 |
|---|
| 369 | __EOS__; |
|---|
| 370 | $table = <<< __EOS__ |
|---|
| 371 | dtb_products_class T1 |
|---|
| 372 | LEFT JOIN dtb_classcategory T3 |
|---|
| 373 | ON T1.classcategory_id1 = T3.classcategory_id |
|---|
| 374 | LEFT JOIN dtb_class T4 |
|---|
| 375 | ON T3.class_id = T4.class_id |
|---|
| 376 | LEFT JOIN dtb_classcategory dtb_classcategory2 |
|---|
| 377 | ON T1.classcategory_id2 = dtb_classcategory2.classcategory_id |
|---|
| 378 | LEFT JOIN dtb_class dtb_class2 |
|---|
| 379 | ON dtb_classcategory2.class_id = dtb_class2.class_id |
|---|
| 380 | __EOS__; |
|---|
| 381 | |
|---|
| 382 | $objQuery->andWhere(' T3.classcategory_id is not null AND dtb_classcategory2.classcategory_id is not null '); |
|---|
| 383 | $objQuery->setOrder('T3.rank DESC, dtb_classcategory2.rank DESC'); // XXX |
|---|
| 384 | $arrRet = $objQuery->select($col, $table, '', $params); |
|---|
| 385 | |
|---|
| 386 | return $arrRet; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | /** |
|---|
| 390 | * 商品規格IDから商品規格を取得する. |
|---|
| 391 | * |
|---|
| 392 | * 削除された商品規格は取得しない. |
|---|
| 393 | * |
|---|
| 394 | * @param integer $productClassId 商品規格ID |
|---|
| 395 | * @return array 商品規格の配列 |
|---|
| 396 | */ |
|---|
| 397 | public function getProductsClass($productClassId) |
|---|
| 398 | { |
|---|
| 399 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 400 | $objQuery->setWhere('product_class_id = ? AND T1.del_flg = 0'); |
|---|
| 401 | $arrRes = $this->getProductsClassByQuery($objQuery, $productClassId); |
|---|
| 402 | |
|---|
| 403 | $arrProduct = (array) $arrRes[0]; |
|---|
| 404 | |
|---|
| 405 | // 税込計算 |
|---|
| 406 | if (!SC_Utils_Ex::isBlank($arrProduct['price01'])) { |
|---|
| 407 | $arrProduct['price01_inctax'] = SC_Helper_TaxRule_Ex::sfCalcIncTax($arrProduct['price01'], $arrProduct['product_id'], $productClassId); |
|---|
| 408 | } |
|---|
| 409 | if (!SC_Utils_Ex::isBlank($arrProduct['price02'])) { |
|---|
| 410 | $arrProduct['price02_inctax'] = SC_Helper_TaxRule_Ex::sfCalcIncTax($arrProduct['price02'], $arrProduct['product_id'], $productClassId); |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | return $arrProduct; |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | /** |
|---|
| 417 | * 複数の商品IDに紐づいた, 商品規格を取得する. |
|---|
| 418 | * |
|---|
| 419 | * @param array $productIds 商品IDの配列 |
|---|
| 420 | * @param boolean $has_deleted 削除された商品規格も含む場合 true; 初期値 false |
|---|
| 421 | * @return array 商品規格の配列 |
|---|
| 422 | */ |
|---|
| 423 | public function getProductsClassByProductIds($productIds = array(), $has_deleted = false) |
|---|
| 424 | { |
|---|
| 425 | if (empty($productIds)) { |
|---|
| 426 | return array(); |
|---|
| 427 | } |
|---|
| 428 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 429 | $where = 'product_id IN (' . SC_Utils_Ex::repeatStrWithSeparator('?', count($productIds)) . ')'; |
|---|
| 430 | if (!$has_deleted) { |
|---|
| 431 | $where .= ' AND T1.del_flg = 0'; |
|---|
| 432 | } |
|---|
| 433 | $objQuery->setWhere($where); |
|---|
| 434 | |
|---|
| 435 | return $this->getProductsClassByQuery($objQuery, $productIds); |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | /** |
|---|
| 439 | * 商品IDに紐づいた, 商品規格を全ての組み合わせごとに取得する. |
|---|
| 440 | * |
|---|
| 441 | * @param array $productId 商品ID |
|---|
| 442 | * @param boolean $has_deleted 削除された商品規格も含む場合 true; 初期値 false |
|---|
| 443 | * @return array 全ての組み合わせの商品規格の配列 |
|---|
| 444 | */ |
|---|
| 445 | public function getProductsClassFullByProductId($productId, $has_deleted = false) |
|---|
| 446 | { |
|---|
| 447 | $arrRet = $this->getProductsClassByProductIds(array($productId), $has_deleted); |
|---|
| 448 | |
|---|
| 449 | return $arrRet; |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | /** |
|---|
| 453 | * 商品IDをキーにした, 商品ステータスIDの配列を取得する. |
|---|
| 454 | * |
|---|
| 455 | * @param array 商品ID の配列 |
|---|
| 456 | * @return array 商品IDをキーにした商品ステータスIDの配列 |
|---|
| 457 | */ |
|---|
| 458 | public function getProductStatus($productIds) |
|---|
| 459 | { |
|---|
| 460 | if (empty($productIds)) { |
|---|
| 461 | return array(); |
|---|
| 462 | } |
|---|
| 463 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 464 | $cols = 'product_id, product_status_id'; |
|---|
| 465 | $from = 'dtb_product_status'; |
|---|
| 466 | $where = 'del_flg = 0 AND product_id IN (' . SC_Utils_Ex::repeatStrWithSeparator('?', count($productIds)) . ')'; |
|---|
| 467 | $productStatus = $objQuery->select($cols, $from, $where, $productIds); |
|---|
| 468 | $results = array(); |
|---|
| 469 | foreach ($productStatus as $status) { |
|---|
| 470 | $results[$status['product_id']][] = $status['product_status_id']; |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | return $results; |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | /** |
|---|
| 477 | * 商品ステータスを設定する. |
|---|
| 478 | * |
|---|
| 479 | * TODO 現在は DELETE/INSERT だが, UPDATE を検討する. |
|---|
| 480 | * |
|---|
| 481 | * @param integer $productId 商品ID |
|---|
| 482 | * @param array $productStatusIds ON にする商品ステータスIDの配列 |
|---|
| 483 | */ |
|---|
| 484 | public function setProductStatus($productId, $productStatusIds) |
|---|
| 485 | { |
|---|
| 486 | $val['product_id'] = $productId; |
|---|
| 487 | $val['creator_id'] = $_SESSION['member_id']; |
|---|
| 488 | $val['create_date'] = 'CURRENT_TIMESTAMP'; |
|---|
| 489 | $val['update_date'] = 'CURRENT_TIMESTAMP'; |
|---|
| 490 | $val['del_flg'] = '0'; |
|---|
| 491 | |
|---|
| 492 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 493 | $objQuery->delete('dtb_product_status', 'product_id = ?', array($productId)); |
|---|
| 494 | foreach ($productStatusIds as $productStatusId) { |
|---|
| 495 | if ($productStatusId == '') continue; |
|---|
| 496 | $val['product_status_id'] = $productStatusId; |
|---|
| 497 | $objQuery->insert('dtb_product_status', $val); |
|---|
| 498 | } |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | /** |
|---|
| 502 | * 商品詳細の結果から, 販売制限数を取得する. |
|---|
| 503 | * |
|---|
| 504 | * getDetailAndProductsClass() の結果から, 販売制限数を取得する. |
|---|
| 505 | * |
|---|
| 506 | * @param array $p 商品詳細の検索結果の配列 |
|---|
| 507 | * @return integer 商品詳細の結果から求めた販売制限数. |
|---|
| 508 | * @see getDetailAndProductsClass() |
|---|
| 509 | */ |
|---|
| 510 | public function getBuyLimit($p) |
|---|
| 511 | { |
|---|
| 512 | $limit = null; |
|---|
| 513 | if ($p['stock_unlimited'] != '1' && is_numeric($p['sale_limit'])) { |
|---|
| 514 | $limit = min($p['sale_limit'], $p['stock']); |
|---|
| 515 | } elseif (is_numeric($p['sale_limit'])) { |
|---|
| 516 | $limit = $p['sale_limit']; |
|---|
| 517 | } elseif ($p['stock_unlimited'] != '1') { |
|---|
| 518 | $limit = $p['stock']; |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | return $limit; |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | /** |
|---|
| 525 | * 在庫を減少させる. |
|---|
| 526 | * |
|---|
| 527 | * 指定の在庫数まで, 在庫を減少させる. |
|---|
| 528 | * 減少させた結果, 在庫数が 0 未満になった場合, 引数 $quantity が 0 の場合は, |
|---|
| 529 | * 在庫の減少を中止し, false を返す. |
|---|
| 530 | * 在庫の減少に成功した場合は true を返す. |
|---|
| 531 | * |
|---|
| 532 | * @param integer $productClassId 商品規格ID |
|---|
| 533 | * @param integer $quantity 減少させる在庫数 |
|---|
| 534 | * @return boolean 在庫の減少に成功した場合 true; 失敗した場合 false |
|---|
| 535 | */ |
|---|
| 536 | public function reduceStock($productClassId, $quantity) |
|---|
| 537 | { |
|---|
| 538 | if ($quantity == 0) { |
|---|
| 539 | return false; |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 543 | $objQuery->update('dtb_products_class', array(), |
|---|
| 544 | 'product_class_id = ?', array($productClassId), |
|---|
| 545 | array('stock' => 'stock - ?'), array($quantity)); |
|---|
| 546 | // TODO エラーハンドリング |
|---|
| 547 | |
|---|
| 548 | $productsClass = $this->getDetailAndProductsClass($productClassId); |
|---|
| 549 | if ($productsClass['stock_unlimited'] != '1' && $productsClass['stock'] < 0) { |
|---|
| 550 | return false; |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | return true; |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | /** |
|---|
| 557 | * 商品情報の配列に, 税込金額を設定して返す. |
|---|
| 558 | * |
|---|
| 559 | * この関数は, 主にスマートフォンで使用します. |
|---|
| 560 | * |
|---|
| 561 | * @param array $arrProducts 商品情報の配列 |
|---|
| 562 | * @return array 旧バージョン互換用のデータ |
|---|
| 563 | */ |
|---|
| 564 | public static function setPriceTaxTo(&$arrProducts) |
|---|
| 565 | { |
|---|
| 566 | foreach ($arrProducts as &$arrProduct) { |
|---|
| 567 | $arrProduct['price01_min_format'] = number_format($arrProduct['price01_min']); |
|---|
| 568 | $arrProduct['price01_max_format'] = number_format($arrProduct['price01_max']); |
|---|
| 569 | $arrProduct['price02_min_format'] = number_format($arrProduct['price02_min']); |
|---|
| 570 | $arrProduct['price02_max_format'] = number_format($arrProduct['price02_max']); |
|---|
| 571 | |
|---|
| 572 | SC_Product_Ex::setIncTaxToProduct($arrProduct); |
|---|
| 573 | |
|---|
| 574 | $arrProduct['price01_min_inctax_format'] = number_format($arrProduct['price01_min_inctax']); |
|---|
| 575 | $arrProduct['price01_max_inctax_format'] = number_format($arrProduct['price01_max_inctax']); |
|---|
| 576 | $arrProduct['price02_min_inctax_format'] = number_format($arrProduct['price02_min_inctax']); |
|---|
| 577 | $arrProduct['price02_max_inctax_format'] = number_format($arrProduct['price02_max_inctax']); |
|---|
| 578 | |
|---|
| 579 | // @deprecated 2.12.4 |
|---|
| 580 | // 旧バージョン互換用 |
|---|
| 581 | // 本来は、税額の代入で使用すべきキー名。 |
|---|
| 582 | $arrProduct['price01_min_tax_format'] =& $arrProduct['price01_min_inctax_format']; |
|---|
| 583 | $arrProduct['price01_max_tax_format'] =& $arrProduct['price01_max_inctax_format']; |
|---|
| 584 | $arrProduct['price02_min_tax_format'] =& $arrProduct['price02_min_inctax_format']; |
|---|
| 585 | $arrProduct['price02_max_tax_format'] =& $arrProduct['price02_max_inctax_format']; |
|---|
| 586 | } |
|---|
| 587 | // @deprecated 2.12.4 |
|---|
| 588 | // 旧バージョン互換用 |
|---|
| 589 | // 現在は参照渡しで戻せる |
|---|
| 590 | return $arrProducts; |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | /** |
|---|
| 594 | * 商品情報の配列に税込金額を設定する |
|---|
| 595 | * |
|---|
| 596 | * @param array $arrProducts 商品情報の配列 |
|---|
| 597 | * @return void |
|---|
| 598 | */ |
|---|
| 599 | public static function setIncTaxToProducts(&$arrProducts) |
|---|
| 600 | { |
|---|
| 601 | foreach ($arrProducts as &$arrProduct) { |
|---|
| 602 | SC_Product_Ex::setIncTaxToProduct($arrProduct); |
|---|
| 603 | } |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | /** |
|---|
| 607 | * 商品情報の配列に税込金額を設定する |
|---|
| 608 | * |
|---|
| 609 | * @param array $arrProduct 商品情報の配列 |
|---|
| 610 | * @return void |
|---|
| 611 | */ |
|---|
| 612 | public static function setIncTaxToProduct(&$arrProduct) |
|---|
| 613 | { |
|---|
| 614 | $arrProduct['price01_min_inctax'] = isset($arrProduct['price01_min']) ? SC_Helper_TaxRule_Ex::sfCalcIncTax($arrProduct['price01_min'], $arrProduct['product_id']) : null; |
|---|
| 615 | $arrProduct['price01_max_inctax'] = isset($arrProduct['price01_max']) ? SC_Helper_TaxRule_Ex::sfCalcIncTax($arrProduct['price01_max'], $arrProduct['product_id']) : null; |
|---|
| 616 | $arrProduct['price02_min_inctax'] = isset($arrProduct['price02_min']) ? SC_Helper_TaxRule_Ex::sfCalcIncTax($arrProduct['price02_min'], $arrProduct['product_id']) : null; |
|---|
| 617 | $arrProduct['price02_max_inctax'] = isset($arrProduct['price02_max']) ? SC_Helper_TaxRule_Ex::sfCalcIncTax($arrProduct['price02_max'], $arrProduct['product_id']) : null; |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | /** |
|---|
| 621 | * 商品詳細の SQL を取得する. |
|---|
| 622 | * |
|---|
| 623 | * @param string $where_products_class 商品規格情報の WHERE 句 |
|---|
| 624 | * @return string 商品詳細の SQL |
|---|
| 625 | */ |
|---|
| 626 | public function alldtlSQL($where_products_class = '') |
|---|
| 627 | { |
|---|
| 628 | $objDBFactory = SC_DB_DBFactory_Ex::getInstance(); |
|---|
| 629 | |
|---|
| 630 | return $objDBFactory->alldtlSQL($where_products_class); |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | /** |
|---|
| 634 | * 商品規格詳細の SQL を取得する. |
|---|
| 635 | * |
|---|
| 636 | * MEMO: 2.4系 vw_product_classに相当(?)するイメージ |
|---|
| 637 | * |
|---|
| 638 | * @param string $where 商品詳細の WHERE 句 |
|---|
| 639 | * @return string 商品規格詳細の SQL |
|---|
| 640 | */ |
|---|
| 641 | public function prdclsSQL($where = '') |
|---|
| 642 | { |
|---|
| 643 | $where_clause = ''; |
|---|
| 644 | if (!SC_Utils_Ex::isBlank($where)) { |
|---|
| 645 | $where_clause = ' WHERE ' . $where; |
|---|
| 646 | } |
|---|
| 647 | $sql = <<< __EOS__ |
|---|
| 648 | ( |
|---|
| 649 | SELECT dtb_products.*, |
|---|
| 650 | dtb_products_class.product_class_id, |
|---|
| 651 | dtb_products_class.product_type_id, |
|---|
| 652 | dtb_products_class.product_code, |
|---|
| 653 | dtb_products_class.stock, |
|---|
| 654 | dtb_products_class.stock_unlimited, |
|---|
| 655 | dtb_products_class.sale_limit, |
|---|
| 656 | dtb_products_class.price01, |
|---|
| 657 | dtb_products_class.price02, |
|---|
| 658 | dtb_products_class.deliv_fee, |
|---|
| 659 | dtb_products_class.point_rate, |
|---|
| 660 | dtb_products_class.down_filename, |
|---|
| 661 | dtb_products_class.down_realfilename, |
|---|
| 662 | dtb_products_class.classcategory_id1 AS classcategory_id, /* 削除 */ |
|---|
| 663 | dtb_products_class.classcategory_id1, |
|---|
| 664 | dtb_products_class.classcategory_id2 AS parent_classcategory_id, /* 削除 */ |
|---|
| 665 | dtb_products_class.classcategory_id2, |
|---|
| 666 | Tcc1.class_id as class_id, |
|---|
| 667 | Tcc1.name as classcategory_name, |
|---|
| 668 | Tcc2.class_id as parent_class_id, |
|---|
| 669 | Tcc2.name as parent_classcategory_name |
|---|
| 670 | FROM dtb_products |
|---|
| 671 | LEFT JOIN dtb_products_class |
|---|
| 672 | ON dtb_products.product_id = dtb_products_class.product_id |
|---|
| 673 | LEFT JOIN dtb_classcategory as Tcc1 |
|---|
| 674 | ON dtb_products_class.classcategory_id1 = Tcc1.classcategory_id |
|---|
| 675 | LEFT JOIN dtb_classcategory as Tcc2 |
|---|
| 676 | ON dtb_products_class.classcategory_id2 = Tcc2.classcategory_id |
|---|
| 677 | $where_clause |
|---|
| 678 | ) as prdcls |
|---|
| 679 | __EOS__; |
|---|
| 680 | |
|---|
| 681 | return $sql; |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | /** |
|---|
| 685 | * @param string $tablename |
|---|
| 686 | */ |
|---|
| 687 | public function getProductDispConditions($tablename = null) |
|---|
| 688 | { |
|---|
| 689 | $tablename = ($tablename) ? $tablename . '.' : null; |
|---|
| 690 | |
|---|
| 691 | return $tablename . 'del_flg = 0 AND ' . $tablename . 'status = 1 '; |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | /** |
|---|
| 695 | * 商品が属しているカテゴリーIDを取得する. |
|---|
| 696 | * |
|---|
| 697 | * @param int $product_id |
|---|
| 698 | * @param bool $include_hidden |
|---|
| 699 | * @return array |
|---|
| 700 | */ |
|---|
| 701 | public function getCategoryIds($product_id, $include_hidden = false) { |
|---|
| 702 | if ($this->isValidProductId($product_id, $include_hidden)) { |
|---|
| 703 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 704 | $category_id = $objQuery->getCol('category_id', 'dtb_product_categories', 'product_id = ?', array($product_id)); |
|---|
| 705 | } else { |
|---|
| 706 | // 不正な場合は、空の配列を返す。 |
|---|
| 707 | $category_id = array(); |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | return $category_id; |
|---|
| 711 | } |
|---|
| 712 | |
|---|
| 713 | /** |
|---|
| 714 | * 有効な商品IDかチェックする. |
|---|
| 715 | * |
|---|
| 716 | * @param int $product_id |
|---|
| 717 | * @param bool $include_hidden |
|---|
| 718 | * @param bool $include_deleted |
|---|
| 719 | * @return bool |
|---|
| 720 | */ |
|---|
| 721 | public function isValidProductId($product_id, $include_hidden = false, $include_deleted = false) { |
|---|
| 722 | $where = ''; |
|---|
| 723 | if (!$include_hidden) { |
|---|
| 724 | $where .= 'status = 1'; |
|---|
| 725 | } |
|---|
| 726 | if (!$include_deleted) { |
|---|
| 727 | if ($where != '') { |
|---|
| 728 | $where .= ' AND '; |
|---|
| 729 | } |
|---|
| 730 | $where .= 'del_flg = 0'; |
|---|
| 731 | } |
|---|
| 732 | if ( |
|---|
| 733 | SC_Utils_Ex::sfIsInt($product_id) |
|---|
| 734 | && !SC_Utils_Ex::sfIsZeroFilling($product_id) |
|---|
| 735 | && SC_Helper_DB_Ex::sfIsRecord('dtb_products', 'product_id', array($product_id), $where) |
|---|
| 736 | ) { |
|---|
| 737 | return true; |
|---|
| 738 | } |
|---|
| 739 | return false; |
|---|
| 740 | } |
|---|
| 741 | } |
|---|