source: branches/version-2_5-dev/data/class/SC_Product.php @ 18826

Revision 18826, 19.1 KB checked in by eccuore, 14 years ago (diff)

#792(ダウンロード販売機能) カートに商品が入らない不具合修正

Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24/*  [名称] SC_Product
25 *  [概要] 商品クラス
26 */
27class SC_Product {
28
29    /** 規格名一覧 */
30    var $arrClassName;
31    /** 規格分類名一覧 */
32    var $arrClassCatName;
33    var $classCategories = array();
34    var $stock_find;
35    /** 規格1クラス名 */
36    var $className1 = '';
37    /** 規格2クラス名 */
38    var $className2 = '';
39    /** 規格1が設定されている */
40    var $classCat1_find;
41    /** 規格2が設定されている */
42    var $classCat2_find;
43    var $classCats1;
44
45    /**
46     * SC_Queryインスタンスに設定された検索条件をもとに商品IDの配列を取得する.
47     *
48     * 検索条件は, SC_Query::getWhere() 関数で設定しておく必要があります.
49     *
50     * @param SC_Query $objQuery SC_Query インスタンス
51     * @param array $arrVal 検索パラメータの配列
52     * @return array 商品IDの配列
53     */
54    function findProductIds(&$objQuery, $arrVal = array()) {
55        $table = <<< __EOS__
56                 dtb_products AS alldtl
57            JOIN dtb_product_categories AS T2
58              ON alldtl.product_id = T2.product_id
59            JOIN dtb_category
60              ON T2.category_id = dtb_category.category_id
61__EOS__;
62        // SC_Query::getCol() ではパフォーマンスが出ない
63        $results = $objQuery->select('alldtl.product_id', $table, "", $arrVal,
64                                     MDB2_FETCHMODE_ORDERED);
65        foreach ($results as $val) {
66            $resultValues[] = $val[0];
67        }
68        return array_unique($resultValues);
69    }
70
71    /**
72     * SC_Queryインスタンスに設定された検索条件をもとに商品一覧の配列を取得する.
73     *
74     * 主に SC_Product::findProductIds() で取得した商品IDを検索条件にし,
75     * SC_Query::setOrder() や SC_Query::setLimitOffset() を設定して, 商品一覧
76     * の配列を取得する.
77     *
78     * @param SC_Query $objQuery SC_Query インスタンス
79     * @param array $arrVal 検索パラメータ(ソート条件)の配列
80     * @return array 商品一覧の配列
81     */
82    function lists(&$objQuery, $arrVal = array()) {
83        $col = <<< __EOS__
84             product_id
85            ,product_code_min
86            ,product_code_max
87            ,name
88            ,comment1
89            ,comment2
90            ,comment3
91            ,main_list_comment
92            ,main_image
93            ,main_list_image
94            ,price01_min
95            ,price01_max
96            ,price02_min
97            ,price02_max
98            ,stock_min
99            ,stock_max
100            ,stock_unlimited_min
101            ,stock_unlimited_max
102            ,deliv_date_id
103            ,status
104            ,del_flg
105            ,update_date
106__EOS__;
107        return $objQuery->select($col, $this->alldtlSQL($objQuery->where),
108                                 "", $arrVal);
109    }
110
111    /**
112     * 商品詳細を取得する.
113     *
114     * @param integer $productId 商品ID
115     * @return array 商品詳細情報の配列
116     */
117    function getDetail($productId) {
118        $objQuery =& SC_Query::getSingletonInstance();
119        $result = $objQuery->select("*", $this->alldtlSQL("product_id = ?"),
120                                    "product_id = ?",
121                                    array($productId, $productId));
122        return $result[0];
123    }
124
125    /**
126     * 商品IDに紐づく商品規格を自分自身に設定する.
127     *
128     * 引数の商品IDの配列に紐づく商品規格を取得し, 自分自身のフィールドに
129     * 設定する.
130     *
131     * @param array $arrProductId 商品ID の配列
132     * @return void
133     */
134    function setProductsClassByProductIds($arrProductId) {
135
136        foreach ($arrProductId as $productId) {
137            $rows[$productId] = $this->getProductsClassFullByProductId($productId);
138        }
139
140        $arrProductsClass = array();
141        foreach ($rows as $productId => $arrProductClass) {
142            $classCats1 = array();
143            $classCats1[''] = '選択してください';
144
145            // 規格1クラス名
146            $this->className1[$productId] =
147                isset($arrProductClass[0]['class_name1'])
148                ? $arrProductClass[0]['class_name1']
149                : '';
150
151            // 規格2クラス名
152            $this->className2[$productId] =
153                isset($arrProductClass[0]['class_name2'])
154                ? $arrProductClass[0]['class_name2']
155                : '';
156
157            // 規格1が設定されている
158            $this->classCat1_find[$productId] = (!SC_Utils_Ex::isBlank($arrProductClass[0]['classcategory_id1']));
159            // 規格2が設定されている
160            $this->classCat2_find[$productId] = (!SC_Utils_Ex::isBlank($arrProductClass[0]['classcategory_id2']));
161
162            $this->stock_find[$productId] = false;
163            $classCategories = array();
164            $classCategories['']['']['name'] = '選択してください';
165            foreach ($arrProductClass as $productsClass) {
166                $productsClass1 = $productsClass['classcategory_id1'];
167                $productsClass2 = $productsClass['classcategory_id2'];
168                $classCategories[$productsClass1]['']['name'] = '選択してください';
169                // 在庫
170                $stock_find_class = ($productsClass['stock_unlimited'] || $productsClass['stock'] > 0);
171
172                $classCategories[$productsClass1][$productsClass2]['name'] = $productsClass['name2'] . ($stock_find_class ? '' : ' (品切れ中)');
173
174                $classCategories[$productsClass1][$productsClass2]['stock_find'] = $stock_find_class;
175
176                if ($stock_find_class) {
177                    $this->stock_find[$productId] = true;
178                }
179
180                if (!in_array($classcat_id1, $classCats1)) {
181                    $classCats1[$productsClass1] = $productsClass['name1']
182                        . ($productsClass2 == 0 && !$stock_find_class ? ' (品切れ中)' : '');
183                }
184
185                // 価格
186                $classCategories[$productsClass1][$productsClass2]['price01']
187                    = strlen($productsClass['price01'])
188                    ? number_format(SC_Helper_DB_Ex::sfPreTax($productsClass['price01']))
189                    : '';
190
191                $classCategories[$productsClass1][$productsClass2]['price02']
192                    = strlen($productsClass['price02'])
193                    ? number_format(SC_Helper_DB_Ex::sfPreTax($productsClass['price02']))
194                    : '';
195
196                // ポイント
197                // XXX sfPrePoint() の第4パラメータは、処理にバグがあるため現状省略している。(http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=3540&forum=1&post_id=13853#forumpost13853)
198                $classCategories[$productsClass1][$productsClass2]['point']
199                    = SC_Utils_Ex::sfPrePoint($productsClass['price02'], $productsClass['point_rate']);
200
201                // 商品コード
202                $classCategories[$productsClass1][$productsClass2]['product_code'] = $productsClass['product_code'];
203            }
204
205            $this->classCategories[$productId] = $classCategories;
206
207            // 規格1
208            $this->classCats1[$productId] = $classCats1;
209        }
210    }
211
212    /**
213     * 複数の商品IDに紐づいた, 商品規格を取得する.
214     *
215     * @param array $productIds 商品IDの配列
216     * @return array 商品規格の配列
217     */
218    function getProductsClassByProductIds($productIds = array()) {
219        if (empty($productIds)) {
220            return array();
221        }
222        $objQuery =& SC_Query::getSingletonInstance();
223        $objQuery->setWhere('product_id IN (' . implode(', ', array_pad(array(), count($productIds), '?')) . ')');
224        $objQuery->setOrder("T2.level DESC");
225        // 末端の規格を取得
226        $col = <<< __EOS__
227            T1.product_id,
228            T1.stock,
229            T1.stock_unlimited,
230            T1.price01,
231            T1.price02,
232            T1.point_rate,
233            T1.product_code,
234            T1.product_class_id,
235            T1.del_flg,
236            T1.down,
237            T1.down_filename,
238            T1.down_realfilename,
239            T2.class_combination_id,
240            T2.parent_class_combination_id,
241            T2.classcategory_id,
242            T2.level,
243            T3.name,
244            T4.name AS class_name,
245            T4.class_id
246__EOS__;
247        $table = <<< __EOS__
248                      dtb_products_class T1
249            LEFT JOIN dtb_class_combination T2
250                   ON T1.class_combination_id = T2.class_combination_id
251            LEFT JOIN dtb_classcategory T3
252                   ON T2.classcategory_id = T3.classcategory_id
253            LEFT JOIN dtb_class T4
254                   ON T3.class_id = T4.class_id
255__EOS__;
256        $arrRet = $objQuery->select($col, $table, "", $productIds);
257        $levels = array();
258        $parents = array();
259        foreach ($arrRet as $rows) {
260            $levels[] = $rows['level'];
261            $parents[] = $rows['parent_class_combination_id'];
262        }
263        $level = max($levels);
264        $parentsClass = array();
265        // 階層分の親を取得
266        for ($i = 0; $i < $level -1; $i++) {
267            $objQuery =& SC_Query::getSingletonInstance();
268            $objQuery->setWhere('T1.class_combination_id IN (' . implode(', ', array_pad(array(), count($parents), '?')) . ')');
269
270            $col = <<< __EOS__
271                T1.class_combination_id,
272                T1.classcategory_id,
273                T1.parent_class_combination_id,
274                T1.level,
275                T2.name,
276                T3.name AS class_name,
277                T3.class_id
278__EOS__;
279            $table = <<< __EOS__
280                          dtb_class_combination T1
281                LEFT JOIN dtb_classcategory T2
282                       ON T1.classcategory_id = T2.classcategory_id
283                LEFT JOIN dtb_class T3
284                       ON T2.class_id = T3.class_id
285__EOS__;
286
287            $arrParents = $objQuery->select($col, $table, "", $parents);
288
289            unset($parents);
290            foreach ($arrParents as $rows) {
291                $parents[] = $rows['parent_class_combination_id'];
292
293                foreach ($arrRet as $child) {
294                    if ($child['parent_class_combination_id']
295                        == $rows['class_combination_id']) {
296                        $rows['product_id'] = $child['product_id'];
297                    }
298                }
299                $tmpParents[] = $rows;
300            }
301            $parentsClass = array_merge($parentsClass, $tmpParents);
302        }
303
304        // 末端から枝を作成
305        $tmpClass = array_merge($arrRet, $parentsClass);
306
307        foreach ($tmpClass as $val) {
308            $val['class_id' . $val['level']] = $val['class_id'];
309            $val['class_name' . $val['level']] = $val['class_name'];
310            $val['name' . $val['level']] = $val['name'];
311            $val['classcategory_id' . $val['level']] = $val['classcategory_id'];
312            $arrProductsClass[] = $val;
313        }
314
315        return $arrProductsClass;
316    }
317
318    /**
319     * 商品IDに紐づいた, 商品規格を階層ごとに取得する.
320     *
321     * @param array $productId 商品IDの配列
322     * @return array 階層ごとの商品規格の配列
323     */
324    function getProductsClassLevelByProductId($productId) {
325        $results = $this->getProductsClassByProductIds(array($productId));
326        foreach ($results as $row) {
327            $productsClassLevel["level" . $row['level']][] = $row;
328        }
329        return $productsClassLevel;
330    }
331
332    /**
333     * 商品IDに紐づいた, 商品規格をすべての組み合わせごとに取得する.
334     *
335     * @param array $productId 商品IDの配列
336     * @return array すべての組み合わせの商品規格の配列
337     */
338    function getProductsClassFullByProductId($productId) {
339        $results = $this->getProductsClassLevelByProductId($productId);
340        $productsClass = array();
341        if (SC_Utils_Ex::isBlank($results["level1"]) && SC_Utils_Ex::isBlank($results["level2"])) {
342            return $results["level"];
343        }
344
345        foreach ($results["level1"] as $level1) {
346            foreach ($results["level2"] as $level2) {
347                if ($level2['parent_class_combination_id'] == $level1['class_combination_id']) {
348                    $level1 = array_merge($level1, $level2);
349                }
350            }
351            $productsClass[] = $level1;
352        }
353        return $productsClass;
354    }
355
356    /**
357     * 商品詳細の SQL を取得する.
358     *
359     * @param string $where 商品詳細の WHERE 句
360     * @return string 商品詳細の SQL
361     */
362    function alldtlSQL($where = "") {
363        $whereCause = "";
364        if (!SC_Utils_Ex::isBlank($where)) {
365            $whereCause = " WHERE " . $where;
366        }
367        /*
368         * point_rate は商品規格(dtb_products_class)ごとに保持しているが,
369         * 商品(dtb_products)ごとの設定なので MAX のみを取得する.
370         */
371        $sql = <<< __EOS__
372            (
373             SELECT dtb_products.product_id,
374                    dtb_products.name,
375                    dtb_products.maker_id,
376                    dtb_products.rank,
377                    dtb_products.status,
378                    dtb_products.comment1,
379                    dtb_products.comment2,
380                    dtb_products.comment3,
381                    dtb_products.comment4,
382                    dtb_products.comment5,
383                    dtb_products.comment6,
384                    dtb_products.note,
385                    dtb_products.file1,
386                    dtb_products.file2,
387                    dtb_products.file3,
388                    dtb_products.file4,
389                    dtb_products.file5,
390                    dtb_products.file6,
391                    dtb_products.main_list_comment,
392                    dtb_products.main_list_image,
393                    dtb_products.main_comment,
394                    dtb_products.main_image,
395                    dtb_products.main_large_image,
396                    dtb_products.sub_title1,
397                    dtb_products.sub_comment1,
398                    dtb_products.sub_image1,
399                    dtb_products.sub_large_image1,
400                    dtb_products.sub_title2,
401                    dtb_products.sub_comment2,
402                    dtb_products.sub_image2,
403                    dtb_products.sub_large_image2,
404                    dtb_products.sub_title3,
405                    dtb_products.sub_comment3,
406                    dtb_products.sub_image3,
407                    dtb_products.sub_large_image3,
408                    dtb_products.sub_title4,
409                    dtb_products.sub_comment4,
410                    dtb_products.sub_image4,
411                    dtb_products.sub_large_image4,
412                    dtb_products.sub_title5,
413                    dtb_products.sub_comment5,
414                    dtb_products.sub_image5,
415                    dtb_products.sub_large_image5,
416                    dtb_products.sub_title6,
417                    dtb_products.sub_comment6,
418                    dtb_products.sub_image6,
419                    dtb_products.sub_large_image6,
420                    dtb_products.del_flg,
421                    dtb_products.creator_id,
422                    dtb_products.create_date,
423                    dtb_products.update_date,
424                    dtb_products.deliv_date_id,
425                    T4.product_code_min,
426                    T4.product_code_max,
427                    T4.price01_min,
428                    T4.price01_max,
429                    T4.price02_min,
430                    T4.price02_max,
431                    T4.stock_min,
432                    T4.stock_max,
433                    T4.stock_unlimited_min,
434                    T4.stock_unlimited_max,
435                    T4.point_rate,
436                    T4.class_count
437               FROM dtb_products
438               JOIN (
439                       SELECT product_id,
440                              MIN(product_code) AS product_code_min,
441                              MAX(product_code) AS product_code_max,
442                              MIN(price01) AS price01_min,
443                              MAX(price01) AS price01_max,
444                              MIN(price02) AS price02_min,
445                              MAX(price02) AS price02_max,
446                              MIN(stock) AS stock_min,
447                              MAX(stock) AS stock_max,
448                              MIN(stock_unlimited) AS stock_unlimited_min,
449                              MAX(stock_unlimited) AS stock_unlimited_max,
450                              MAX(point_rate) AS point_rate,
451                              COUNT(*) as class_count
452                         FROM dtb_products_class
453                       $whereCause
454                     GROUP BY product_id
455                     ) AS T4
456                 ON dtb_products.product_id = T4.product_id
457        ) AS alldtl
458__EOS__;
459        return $sql;
460    }
461
462    /**
463     * 商品規格ID1、2に紐づいた,product_class_idを取得する.
464     *
465     * @param int $productId 商品ID
466     * @param int $classcategory_id1 商品規格ID1
467     * @param int $classcategory_id2 商品規格ID2
468     * @return string product_class_id
469     */
470    function getClasscategoryIdsByProductClassId($productId, $classcategory_id1, $classcategory_id2) {
471        $objQuery = new SC_Query();
472        $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";
473        $table = <<< __EOS__
474            (SELECT
475                pc.product_code AS product_code,
476                pc.product_id AS product_id,
477                pc.product_class_id AS product_class_id,
478                pc.class_combination_id AS class_combination_id,
479                COALESCE(cc2.classcategory_id,0) AS classcategory_id1,
480                COALESCE(cc1.classcategory_id,0) AS classcategory_id2
481            FROM
482                dtb_products_class pc LEFT JOIN dtb_class_combination cc1 ON pc.class_combination_id = cc1.class_combination_id
483                LEFT JOIN dtb_class_combination cc2 ON cc1.parent_class_combination_id = cc2.class_combination_id) T1
484__EOS__;
485        $where = "T1.product_id = ? AND T1.classcategory_id1 = ? AND T1.classcategory_id2 = ?";
486        $arrRet = $objQuery->select($col, $table, $where,
487                                    array($productId, $classcategory_id1, $classcategory_id2));
488        return $arrRet[0]['product_class_id'];
489    }
490
491}
492?>
Note: See TracBrowser for help on using the repository browser.