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