source: branches/comu-ver2/data/class/helper/SC_Helper_DB.php @ 18436

Revision 18436, 72.8 KB checked in by Seasoft, 14 years ago (diff)

非公開の商品は、フロント機能では表示しない。非公開の商品も、管理機能では表示する。以上を徹底。

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id Revision Date
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2007 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 * DB関連のヘルパークラス.
26 *
27 * @package Helper
28 * @author LOCKON CO.,LTD.
29 * @version $Id:SC_Helper_DB.php 15532 2007-08-31 14:39:46Z nanasess $
30 */
31class SC_Helper_DB {
32
33    // {{{ properties
34
35    /** ルートカテゴリ取得フラグ */
36    var $g_root_on;
37
38    /** ルートカテゴリID */
39    var $g_root_id;
40
41    /** 選択中カテゴリ取得フラグ */
42    var $g_category_on;
43
44    /** 選択中カテゴリID */
45    var $g_category_id;
46
47    // }}}
48    // {{{ functions
49
50    /**
51     * データベースのバージョンを所得する.
52     *
53     * @param string $dsn データソース名
54     * @return string データベースのバージョン
55     */
56    function sfGetDBVersion($dsn = "") {
57        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
58        return $dbFactory->sfGetDBVersion($dsn);
59    }
60
61    /**
62     * テーブルの存在をチェックする.
63     *
64     * @param string $table_name チェック対象のテーブル名
65     * @param string $dsn データソース名
66     * @return テーブルが存在する場合 true
67     */
68    function sfTabaleExists($table_name, $dsn = "") {
69        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
70        $dsn = $dbFactory->getDSN($dsn);
71
72        $objQuery = new SC_Query($dsn, true, true);
73        // 正常に接続されている場合
74        if(!$objQuery->isError()) {
75            list($db_type) = split(":", $dsn);
76            $sql = $dbFactory->getTableExistsSql();
77            $arrRet = $objQuery->getAll($sql, array($table_name));
78            if(count($arrRet) > 0) {
79                return true;
80            }
81        }
82        return false;
83    }
84
85    /**
86     * カラムの存在チェックと作成を行う.
87     *
88     * チェック対象のテーブルに, 該当のカラムが存在するかチェックする.
89     * 引数 $add が true の場合, 該当のカラムが存在しない場合は, カラムの生成を行う.
90     * カラムの生成も行う場合は, $col_type も必須となる.
91     *
92     * @param string $table_name テーブル名
93     * @param string $column_name カラム名
94     * @param string $col_type カラムのデータ型
95     * @param string $dsn データソース名
96     * @param bool $add カラムの作成も行う場合 true
97     * @return bool カラムが存在する場合とカラムの生成に成功した場合 true,
98     *               テーブルが存在しない場合 false,
99     *               引数 $add == false でカラムが存在しない場合 false
100     */
101    function sfColumnExists($table_name, $col_name, $col_type = "", $dsn = "", $add = false) {
102        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
103        $dsn = $dbFactory->getDSN($dsn);
104
105        // テーブルが無ければエラー
106        if(!$this->sfTabaleExists($table_name, $dsn)) return false;
107
108        $objQuery = new SC_Query($dsn, true, true);
109        // 正常に接続されている場合
110        if(!$objQuery->isError()) {
111            list($db_type) = split(":", $dsn);
112
113            // カラムリストを取得
114            $arrRet = $dbFactory->sfGetColumnList($table_name);
115            if(count($arrRet) > 0) {
116                if(in_array($col_name, $arrRet)){
117                    return true;
118                }
119            }
120        }
121
122        // カラムを追加する
123        if($add){
124            $objQuery->query("ALTER TABLE $table_name ADD $col_name $col_type ");
125            return true;
126        }
127        return false;
128    }
129
130    /**
131     * インデックスの存在チェックと作成を行う.
132     *
133     * チェック対象のテーブルに, 該当のインデックスが存在するかチェックする.
134     * 引数 $add が true の場合, 該当のインデックスが存在しない場合は, インデックスの生成を行う.
135     * インデックスの生成も行う場合で, DB_TYPE が mysql の場合は, $length も必須となる.
136     *
137     * @param string $table_name テーブル名
138     * @param string $column_name カラム名
139     * @param string $index_name インデックス名
140     * @param integer|string $length インデックスを作成するデータ長
141     * @param string $dsn データソース名
142     * @param bool $add インデックスの生成もする場合 true
143     * @return bool インデックスが存在する場合とインデックスの生成に成功した場合 true,
144     *               テーブルが存在しない場合 false,
145     *               引数 $add == false でインデックスが存在しない場合 false
146     */
147    function sfIndexExists($table_name, $col_name, $index_name, $length = "", $dsn = "", $add = false) {
148        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
149        $dsn = $dbFactory->getDSN($dsn);
150
151        // テーブルが無ければエラー
152        if (!$this->sfTabaleExists($table_name, $dsn)) return false;
153
154        $objQuery = new SC_Query($dsn, true, true);
155        $arrRet = $dbFactory->getTableIndex($index_name, $table_name);
156
157        // すでにインデックスが存在する場合
158        if(count($arrRet) > 0) {
159            return true;
160        }
161
162        // インデックスを作成する
163        if($add){
164            $dbFactory->createTableIndex($index_name, $table_name, $col_name, $length());
165            return true;
166        }
167        return false;
168    }
169
170    /**
171     * データの存在チェックを行う.
172     *
173     * @param string $table_name テーブル名
174     * @param string $where データを検索する WHERE 句
175     * @param string $dsn データソース名
176     * @param string $sql データの追加を行う場合の SQL文
177     * @param bool $add データの追加も行う場合 true
178     * @return bool データが存在する場合 true, データの追加に成功した場合 true,
179     *               $add == false で, データが存在しない場合 false
180     */
181    function sfDataExists($table_name, $where, $arrval, $dsn = "", $sql = "", $add = false) {
182        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
183        $dsn = $dbFactory->getDSN($dsn);
184
185        $objQuery = new SC_Query($dsn, true, true);
186        $count = $objQuery->count($table_name, $where, $arrval);
187
188        if($count > 0) {
189            $ret = true;
190        } else {
191            $ret = false;
192        }
193        // データを追加する
194        if(!$ret && $add) {
195            $objQuery->exec($sql);
196        }
197        return $ret;
198    }
199
200    /**
201     * 店舗基本情報を取得する.
202     *
203     * @param boolean $force 強制的にDB取得するか
204     * @return array 店舗基本情報の配列
205     */
206    function sf_getBasisData($force = false) {
207        static $data;
208       
209        if ($force || !isset($data)) {
210            $objQuery = new SC_Query();
211            $arrRet = $objQuery->select('*', 'dtb_baseinfo');
212           
213            if (isset($arrRet[0])) {
214                $data = $arrRet[0];
215            } else {
216                $data = array();
217            }
218        }
219       
220        return $data;
221    }
222
223    /* 選択中のアイテムのルートカテゴリIDを取得する */
224    function sfGetRootId() {
225
226        if(!$this->g_root_on)   {
227            $this->g_root_on = true;
228            $objQuery = new SC_Query();
229
230            if (!isset($_GET['product_id'])) $_GET['product_id'] = "";
231            if (!isset($_GET['category_id'])) $_GET['category_id'] = "";
232
233            if(!empty($_GET['product_id']) || !empty($_GET['category_id'])) {
234                // 選択中のカテゴリIDを判定する
235                $category_id = $this->sfGetCategoryId($_GET['product_id'], $_GET['category_id']);
236                // ROOTカテゴリIDの取得
237                $arrRet = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
238                $root_id = isset($arrRet[0]) ? $arrRet[0] : "";
239            } else {
240                // ROOTカテゴリIDをなしに設定する
241                $root_id = "";
242            }
243            $this->g_root_id = $root_id;
244        }
245        return $this->g_root_id;
246    }
247
248    /**
249     * 商品規格情報を取得する.
250     *
251     * @param array $arrID 規格ID
252     * @param boolean $includePrivateProducts 非公開商品を含むか
253     * @return array 規格情報の配列
254     */
255    function sfGetProductsClass($arrID, $includePrivateProducts = false) {
256        list($product_id, $classcategory_id1, $classcategory_id2) = $arrID;
257
258        if (strlen($classcategory_id1) == 0) {
259            $classcategory_id1 = '0';
260        }
261        if (strlen($classcategory_id2) == 0) {
262            $classcategory_id2 = '0';
263        }
264
265        // 商品規格取得
266        $objQuery = new SC_Query();
267        $col = 'product_id, deliv_fee, name, product_code, main_list_image, main_image, price01, price02, point_rate, product_class_id, classcategory_id1, classcategory_id2, class_id1, class_id2, stock, stock_unlimited, sale_limit';
268        $table = 'vw_product_class AS prdcls';
269        $where = 'product_id = ? AND classcategory_id1 = ? AND classcategory_id2 = ?';
270        if (!$includePrivateProducts) {
271             $where .= ' AND status = 1';
272        }
273        $arrRet = $objQuery->select($col, $table, $where, array($product_id, $classcategory_id1, $classcategory_id2));
274        return $arrRet[0];
275    }
276
277    /**
278     * 支払い方法を取得する.
279     *
280     * @return void
281     */
282    function sfGetPayment() {
283        $objQuery = new SC_Query();
284        // 購入金額が条件額以下の項目を取得
285        $where = "del_flg = 0";
286        $objQuery->setorder("fix, rank DESC");
287        $arrRet = $objQuery->select("payment_id, payment_method, rule", "dtb_payment", $where);
288        return $arrRet;
289    }
290
291    /**
292     * カート内商品の集計処理を行う.
293     *
294     * 管理機能での利用は想定していないので注意。(非公開商品は除外される。)
295     *
296     * @param LC_Page $objPage ページクラスのインスタンス
297     * @param SC_CartSession $objCartSess カートセッションのインスタンス
298     * @param null $dummy1 互換性確保用(決済モジュール互換のため)
299     * @return LC_Page 集計処理後のページクラスインスタンス
300     */
301    function sfTotalCart(&$objPage, $objCartSess, $dummy1 = null) {
302
303        // 規格名一覧
304        $arrClassName = $this->sfGetIDValueList("dtb_class", "class_id", "name");
305        // 規格分類名一覧
306        $arrClassCatName = $this->sfGetIDValueList("dtb_classcategory", "classcategory_id", "name");
307
308        $objPage->tpl_total_pretax = 0;     // 費用合計(税込み)
309        $objPage->tpl_total_tax = 0;        // 消費税合計
310        $objPage->tpl_total_point = 0;      // ポイント合計
311
312        // カート内情報の取得
313        $arrQuantityInfo_by_product = array();
314        $cnt = 0;
315        foreach ($objCartSess->getCartList() as $arrCart) {
316            // 商品規格情報の取得
317            $arrData = $this->sfGetProductsClass($arrCart['id']);
318            $limit = null;
319            // DBに存在する商品
320            if (count($arrData) > 0) {
321
322                // 購入制限数を求める。
323                if ($arrData['stock_unlimited'] != '1' && SC_Utils_Ex::sfIsInt($arrData['sale_limit'])) {
324                    $limit = min($arrData['sale_limit'], $arrData['stock']);
325                } elseif (SC_Utils_Ex::sfIsInt($arrData['sale_limit'])) {
326                    $limit = $arrData['sale_limit'];
327                } elseif ($arrData['stock_unlimited'] != '1') {
328                    $limit = $arrData['stock'];
329                }
330
331                if (!is_null($limit) && $arrCart['quantity'] > $limit) {
332                    if ($limit > 0) {
333                        // カート内商品数を制限に合わせる
334                        $objCartSess->setProductValue($arrCart['id'], 'quantity', $limit);
335                        $quantity = $limit;
336                        $objPage->tpl_message .= "※「" . $arrData['name'] . "」は販売制限(または在庫が不足)しております。一度に数量{$limit}以上の購入はできません。\n";
337                    } else {
338                        // 売り切れ商品をカートから削除する
339                        $objCartSess->delProduct($arrCart['cart_no']);
340                        $objPage->tpl_message .= "※「" . $arrData['name'] . "」は売り切れました。\n";
341                        break;
342                    }
343                } else {
344                    $quantity = $arrCart['quantity'];
345                }
346               
347                // (商品規格単位でなく)商品単位での評価のための準備
348                $product_id = $arrCart['id'][0];
349                $arrQuantityInfo_by_product[$product_id]['quantity'] += $quantity;
350                $arrQuantityInfo_by_product[$product_id]['sale_limit'] = $arrData['sale_limit'];
351                $arrQuantityInfo_by_product[$product_id]['name'] = $arrData['name'];
352               
353                $objPage->arrProductsClass[$cnt] = $arrData;
354                $objPage->arrProductsClass[$cnt]['quantity'] = $quantity;
355                $objPage->arrProductsClass[$cnt]['cart_no'] = $arrCart['cart_no'];
356                $objPage->arrProductsClass[$cnt]['class_name1'] =
357                    isset($arrClassName[$arrData['class_id1']])
358                        ? $arrClassName[$arrData['class_id1']] : "";
359
360                $objPage->arrProductsClass[$cnt]['class_name2'] =
361                    isset($arrClassName[$arrData['class_id2']])
362                        ? $arrClassName[$arrData['class_id2']] : "";
363
364                $objPage->arrProductsClass[$cnt]['classcategory_name1'] =
365                    $arrClassCatName[$arrData['classcategory_id1']];
366
367                $objPage->arrProductsClass[$cnt]['classcategory_name2'] =
368                    $arrClassCatName[$arrData['classcategory_id2']];
369
370                // 価格の登録
371                if ($arrData['price02'] != "") {
372                    $objCartSess->setProductValue($arrCart['id'], 'price', $arrData['price02']);
373                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price02'];
374                } else {
375                    $objCartSess->setProductValue($arrCart['id'], 'price', $arrData['price01']);
376                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price01'];
377                }
378                // ポイント付与率の登録
379                if (USE_POINT !== false) {
380                    $objCartSess->setProductValue($arrCart['id'], 'point_rate', $arrData['point_rate']);
381                }
382                // 商品ごとの合計金額
383                $objPage->arrProductsClass[$cnt]['total_pretax'] = $objCartSess->getProductTotal($arrCart['id']);
384                // 送料の合計を計算する
385                $objPage->tpl_total_deliv_fee+= ($arrData['deliv_fee'] * $arrCart['quantity']);
386                $cnt++;
387            } else { // DBに商品が見つからない場合、
388                $objPage->tpl_message .= "※ 現時点で販売していない商品が含まれておりました。該当商品をカートから削除しました。\n";
389                // カート商品の削除
390                $objCartSess->delProduct($arrCart['cart_no']);
391            }
392        }
393       
394        foreach ($arrQuantityInfo_by_product as $product_id => $quantityInfo) {
395            if (SC_Utils_Ex::sfIsInt($quantityInfo['sale_limit']) && $quantityInfo['quantity'] > $quantityInfo['sale_limit']) {
396                $objPage->tpl_error = "※「{$quantityInfo['name']}」は数量「{$quantityInfo['sale_limit']}」以下に販売制限しております。一度にこれ以上の購入はできません。\n";
397                // 販売制限に引っかかった商品をマークする
398                foreach (array_keys($objPage->arrProductsClass) as $key) {
399                    $ProductsClass =& $objPage->arrProductsClass[$key];
400                    if ($ProductsClass['product_id'] == $product_id) {
401                        $ProductsClass['error'] = true;
402                    }
403                }
404            }
405        }
406       
407        // 全商品合計金額(税込み)
408        $objPage->tpl_total_pretax = $objCartSess->getAllProductsTotal();
409        // 全商品合計消費税
410        $objPage->tpl_total_tax = $objCartSess->getAllProductsTax();
411        // 全商品合計ポイント
412        if (USE_POINT !== false) {
413            $objPage->tpl_total_point = $objCartSess->getAllProductsPoint();
414        }
415
416        return $objPage;
417    }
418
419    /**
420     * 受注一時テーブルへの書き込み処理を行う.
421     *
422     * @param string $uniqid ユニークID
423     * @param array $sqlval SQLの値の配列
424     * @return void
425     */
426    function sfRegistTempOrder($uniqid, $sqlval) {
427        if($uniqid != "") {
428            // 既存データのチェック
429            $objQuery = new SC_Query();
430            $where = "order_temp_id = ?";
431            $cnt = $objQuery->count("dtb_order_temp", $where, array($uniqid));
432            // 既存データがない場合
433            if ($cnt == 0) {
434                // 初回書き込み時に会員の登録済み情報を取り込む
435                $sqlval = $this->sfGetCustomerSqlVal($uniqid, $sqlval);
436                $sqlval['create_date'] = "now()";
437                $objQuery->insert("dtb_order_temp", $sqlval);
438            } else {
439                $objQuery->update("dtb_order_temp", $sqlval, $where, array($uniqid));
440            }
441           
442            // 受注_Tempテーブルの名称列を更新
443            $this->sfUpdateOrderNameCol($uniqid, true);
444        }
445    }
446
447    /**
448     * 会員情報から SQL文の値を生成する.
449     *
450     * @param string $uniqid ユニークID
451     * @param array $sqlval SQL の値の配列
452     * @return array 会員情報を含んだ SQL の値の配列
453     */
454    function sfGetCustomerSqlVal($uniqid, $sqlval) {
455        $objCustomer = new SC_Customer();
456        // 会員情報登録処理
457        if ($objCustomer->isLoginSuccess(true)) {
458            // 登録データの作成
459            $sqlval['order_temp_id'] = $uniqid;
460            $sqlval['update_date'] = 'Now()';
461            $sqlval['customer_id'] = $objCustomer->getValue('customer_id');
462            $sqlval['order_name01'] = $objCustomer->getValue('name01');
463            $sqlval['order_name02'] = $objCustomer->getValue('name02');
464            $sqlval['order_kana01'] = $objCustomer->getValue('kana01');
465            $sqlval['order_kana02'] = $objCustomer->getValue('kana02');
466            $sqlval['order_sex'] = $objCustomer->getValue('sex');
467            $sqlval['order_zip01'] = $objCustomer->getValue('zip01');
468            $sqlval['order_zip02'] = $objCustomer->getValue('zip02');
469            $sqlval['order_pref'] = $objCustomer->getValue('pref');
470            $sqlval['order_addr01'] = $objCustomer->getValue('addr01');
471            $sqlval['order_addr02'] = $objCustomer->getValue('addr02');
472            $sqlval['order_tel01'] = $objCustomer->getValue('tel01');
473            $sqlval['order_tel02'] = $objCustomer->getValue('tel02');
474            $sqlval['order_tel03'] = $objCustomer->getValue('tel03');
475            if (defined('MOBILE_SITE')) {
476                $email_mobile = $objCustomer->getValue('email_mobile');
477                if (empty($email_mobile)) {
478                    $sqlval['order_email'] = $objCustomer->getValue('email');
479                } else {
480                    $sqlval['order_email'] = $email_mobile;
481                }
482            } else {
483                $sqlval['order_email'] = $objCustomer->getValue('email');
484            }
485            $sqlval['order_job'] = $objCustomer->getValue('job');
486            $sqlval['order_birth'] = $objCustomer->getValue('birth');
487        }
488        return $sqlval;
489    }
490
491    /**
492     * 会員編集登録処理を行う.
493     *
494     * @param array $array パラメータの配列
495     * @param array $arrRegistColumn 登録するカラムの配列
496     * @return void
497     */
498    function sfEditCustomerData($array, $arrRegistColumn) {
499        $objQuery = new SC_Query();
500
501        foreach ($arrRegistColumn as $data) {
502            if ($data["column"] != "password") {
503                if($array[ $data['column'] ] != "") {
504                    $arrRegist[ $data["column"] ] = $array[ $data["column"] ];
505                } else {
506                    $arrRegist[ $data['column'] ] = NULL;
507                }
508            }
509        }
510        if (strlen($array["year"]) > 0 && strlen($array["month"]) > 0 && strlen($array["day"]) > 0) {
511            $arrRegist["birth"] = $array["year"] ."/". $array["month"] ."/". $array["day"] ." 00:00:00";
512        } else {
513            $arrRegist["birth"] = NULL;
514        }
515
516        //-- パスワードの更新がある場合は暗号化。(更新がない場合はUPDATE文を構成しない)
517        if ($array["password"] != DEFAULT_PASSWORD) $arrRegist["password"] = sha1($array["password"] . ":" . AUTH_MAGIC);
518        $arrRegist["update_date"] = "NOW()";
519
520        //-- 編集登録実行
521        $objQuery->update("dtb_customer", $arrRegist, "customer_id = ? ", array($array['customer_id']));
522    }
523
524    /**
525     * 注文番号、利用ポイント、加算ポイントから最終ポイントを取得する.
526     *
527     * @param integer $order_id 注文番号
528     * @param integer $use_point 利用ポイント
529     * @param integer $add_point 加算ポイント
530     * @return array 最終ポイントの配列
531     */
532    function sfGetCustomerPoint($order_id, $use_point, $add_point) {
533        $objQuery = new SC_Query();
534        $arrRet = $objQuery->select("customer_id", "dtb_order", "order_id = ?", array($order_id));
535        $customer_id = $arrRet[0]['customer_id'];
536        if ($customer_id != "" && $customer_id >= 1) {
537            if (USE_POINT !== false) {
538                $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id));
539                $point = $arrRet[0]['point'];
540                $total_point = $arrRet[0]['point'] - $use_point + $add_point;
541            } else {
542                $total_point = 0;
543                $point = 0;
544            }
545        } else {
546            $total_point = "";
547            $point = "";
548        }
549        return array($point, $total_point);
550    }
551
552    /**
553     * 顧客番号、利用ポイント、加算ポイントから最終ポイントを取得する.
554     *
555     * @param integer $customer_id 顧客番号
556     * @param integer $use_point 利用ポイント
557     * @param integer $add_point 加算ポイント
558     * @return array 最終ポイントの配列
559     */
560    function sfGetCustomerPointFromCid($customer_id, $use_point, $add_point) {
561        $objQuery = new SC_Query();
562        if (USE_POINT !== false) {
563            $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id));
564            $point = $arrRet[0]['point'];
565            $total_point = $arrRet[0]['point'] - $use_point + $add_point;
566        } else {
567            $total_point = 0;
568            $point = 0;
569        }
570        return array($point, $total_point);
571    }
572    /**
573     * カテゴリツリーの取得を行う.
574     *
575     * @param integer $parent_category_id 親カテゴリID
576     * @param bool $count_check 登録商品数のチェックを行う場合 true
577     * @return array カテゴリツリーの配列
578     */
579    function sfGetCatTree($parent_category_id, $count_check = false) {
580        $objQuery = new SC_Query();
581        $col = "";
582        $col .= " cat.category_id,";
583        $col .= " cat.category_name,";
584        $col .= " cat.parent_category_id,";
585        $col .= " cat.level,";
586        $col .= " cat.rank,";
587        $col .= " cat.creator_id,";
588        $col .= " cat.create_date,";
589        $col .= " cat.update_date,";
590        $col .= " cat.del_flg, ";
591        $col .= " ttl.product_count";
592        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
593        // 登録商品数のチェック
594        if($count_check) {
595            $where = "del_flg = 0 AND product_count > 0";
596        } else {
597            $where = "del_flg = 0";
598        }
599        $objQuery->setoption("ORDER BY rank DESC");
600        $arrRet = $objQuery->select($col, $from, $where);
601
602        $arrParentID = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
603
604        foreach($arrRet as $key => $array) {
605            foreach($arrParentID as $val) {
606                if($array['category_id'] == $val) {
607                    $arrRet[$key]['display'] = 1;
608                    break;
609                }
610            }
611        }
612
613        return $arrRet;
614    }
615
616    /**
617     * カテゴリツリーの取得を複数カテゴリーで行う.
618     *
619     * @param integer $product_id 商品ID
620     * @param bool $count_check 登録商品数のチェックを行う場合 true
621     * @return array カテゴリツリーの配列
622     */
623    function sfGetMultiCatTree($product_id, $count_check = false) {
624        $objQuery = new SC_Query();
625        $col = "";
626        $col .= " cat.category_id,";
627        $col .= " cat.category_name,";
628        $col .= " cat.parent_category_id,";
629        $col .= " cat.level,";
630        $col .= " cat.rank,";
631        $col .= " cat.creator_id,";
632        $col .= " cat.create_date,";
633        $col .= " cat.update_date,";
634        $col .= " cat.del_flg, ";
635        $col .= " ttl.product_count";
636        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
637        // 登録商品数のチェック
638        if($count_check) {
639            $where = "del_flg = 0 AND product_count > 0";
640        } else {
641            $where = "del_flg = 0";
642        }
643        $objQuery->setoption("ORDER BY rank DESC");
644        $arrRet = $objQuery->select($col, $from, $where);
645
646        $arrCategory_id = $this->sfGetCategoryId($product_id);
647
648        $arrCatTree = array();
649        foreach ($arrCategory_id as $pkey => $parent_category_id) {
650            $arrParentID = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
651
652            foreach($arrParentID as $pid) {
653                foreach($arrRet as $key => $array) {
654                    if($array['category_id'] == $pid) {
655                        $arrCatTree[$pkey][] = $arrRet[$key];
656                        break;
657                    }
658                }
659            }
660        }
661
662        return $arrCatTree;
663    }
664
665    /**
666     * 親カテゴリーを連結した文字列を取得する.
667     *
668     * @param integer $category_id カテゴリID
669     * @return string 親カテゴリーを連結した文字列
670     */
671    function sfGetCatCombName($category_id){
672        // 商品が属するカテゴリIDを縦に取得
673        $objQuery = new SC_Query();
674        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
675        $ConbName = "";
676
677        // カテゴリー名称を取得する
678        foreach($arrCatID as $key => $val){
679            $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
680            $arrVal = array($val);
681            $CatName = $objQuery->getOne($sql,$arrVal);
682            $ConbName .= $CatName . ' | ';
683        }
684        // 最後の | をカットする
685        $ConbName = substr_replace($ConbName, "", strlen($ConbName) - 2, 2);
686
687        return $ConbName;
688    }
689
690    /**
691     * 指定したカテゴリーIDのカテゴリーを取得する.
692     *
693     * @param integer $category_id カテゴリID
694     * @return array 指定したカテゴリーIDのカテゴリー
695     */
696    function sfGetCat($category_id){
697        $objQuery = new SC_Query();
698
699        // カテゴリーを取得する
700        $arrVal = array($category_id);
701        $res = $objQuery->select('category_id AS id, category_name AS name', 'dtb_category', 'category_id = ?', $arrVal);
702
703        return $res[0];
704    }
705
706    /**
707     * 指定したカテゴリーIDの大カテゴリーを取得する.
708     *
709     * @param integer $category_id カテゴリID
710     * @return array 指定したカテゴリーIDの大カテゴリー
711     */
712    function sfGetFirstCat($category_id){
713        // 商品が属するカテゴリIDを縦に取得
714        $objQuery = new SC_Query();
715        $arrRet = array();
716        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
717        $arrRet['id'] = $arrCatID[0];
718
719        // カテゴリー名称を取得する
720        $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
721        $arrVal = array($arrRet['id']);
722        $arrRet['name'] = $objQuery->getOne($sql,$arrVal);
723
724        return $arrRet;
725    }
726
727    /**
728     * カテゴリツリーの取得を行う.
729     *
730     * $products_check:true商品登録済みのものだけ取得する
731     *
732     * @param string $addwhere 追加する WHERE 句
733     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
734     * @param string $head カテゴリ名のプレフィックス文字列
735     * @return array カテゴリツリーの配列
736     */
737    function sfGetCategoryList($addwhere = "", $products_check = false, $head = CATEGORY_HEAD) {
738        $objQuery = new SC_Query();
739        $where = "del_flg = 0";
740
741        if($addwhere != "") {
742            $where.= " AND $addwhere";
743        }
744
745        $objQuery->setoption("ORDER BY rank DESC");
746
747        if($products_check) {
748            $col = "T1.category_id, category_name, level";
749            $from = "dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id";
750            $where .= " AND product_count > 0";
751        } else {
752            $col = "category_id, category_name, level";
753            $from = "dtb_category";
754        }
755
756        $arrRet = $objQuery->select($col, $from, $where);
757
758        $max = count($arrRet);
759        for($cnt = 0; $cnt < $max; $cnt++) {
760            $id = $arrRet[$cnt]['category_id'];
761            $name = $arrRet[$cnt]['category_name'];
762            $arrList[$id] = str_repeat($head, $arrRet[$cnt]['level']) . $name;
763        }
764        return $arrList;
765    }
766
767    /**
768     * カテゴリーツリーの取得を行う.
769     *
770     * 親カテゴリの Value=0 を対象とする
771     *
772     * @param bool $parent_zero 親カテゴリの Value=0 の場合 true
773     * @return array カテゴリツリーの配列
774     */
775    function sfGetLevelCatList($parent_zero = true) {
776        $objQuery = new SC_Query();
777
778        // カテゴリ名リストを取得
779        $col = "category_id, parent_category_id, category_name";
780        $where = "del_flg = 0";
781        $objQuery->setoption("ORDER BY level");
782        $arrRet = $objQuery->select($col, "dtb_category", $where);
783        $arrCatName = array();
784        foreach ($arrRet as $arrTmp) {
785            $arrCatName[$arrTmp['category_id']] =
786                (($arrTmp['parent_category_id'] > 0)?
787                    $arrCatName[$arrTmp['parent_category_id']] : "")
788                . CATEGORY_HEAD . $arrTmp['category_name'];
789        }
790
791        $col = "category_id, parent_category_id, category_name, level";
792        $where = "del_flg = 0";
793        $objQuery->setoption("ORDER BY rank DESC");
794        $arrRet = $objQuery->select($col, "dtb_category", $where);
795        $max = count($arrRet);
796
797        for($cnt = 0; $cnt < $max; $cnt++) {
798            if($parent_zero) {
799                if($arrRet[$cnt]['level'] == LEVEL_MAX) {
800                    $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
801                } else {
802                    $arrValue[$cnt] = "";
803                }
804            } else {
805                $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
806            }
807
808            $arrOutput[$cnt] = $arrCatName[$arrRet[$cnt]['category_id']];
809        }
810
811        return array($arrValue, $arrOutput);
812    }
813
814    /**
815     * 選択中の商品のカテゴリを取得する.
816     *
817     * @param integer $product_id プロダクトID
818     * @param integer $category_id カテゴリID
819     * @return array 選択中の商品のカテゴリIDの配列
820     *
821     */
822    function sfGetCategoryId($product_id, $category_id = 0, $closed = false) {
823        if ($closed) {
824            $status = "";
825        } else {
826            $status = "status = 1";
827        }
828
829        if(!$this->g_category_on) {
830            $this->g_category_on = true;
831            $category_id = (int) $category_id;
832            $product_id = (int) $product_id;
833            if (SC_Utils_Ex::sfIsInt($category_id) && $category_id != 0 && $this->sfIsRecord("dtb_category","category_id", $category_id)) {
834                $this->g_category_id = array($category_id);
835            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
836                $objQuery = new SC_Query();
837                $where = "product_id = ?";
838                $category_id = $objQuery->getCol("dtb_product_categories", "category_id", "product_id = ?", array($product_id));
839                $this->g_category_id = $category_id;
840            } else {
841                // 不正な場合は、空の配列を返す。
842                $this->g_category_id = array();
843            }
844        }
845        return $this->g_category_id;
846    }
847
848    /**
849     * 商品をカテゴリの先頭に追加する.
850     *
851     * @param integer $category_id カテゴリID
852     * @param integer $product_id プロダクトID
853     * @return void
854     */
855    function addProductBeforCategories($category_id, $product_id) {
856
857        $sqlval = array("category_id" => $category_id,
858                        "product_id" => $product_id);
859
860        $objQuery = new SC_Query();
861
862        // 現在の商品カテゴリを取得
863        $arrCat = $objQuery->select("product_id, category_id, rank",
864                                    "dtb_product_categories",
865                                    "category_id = ?",
866                                    array($category_id));
867
868        $max = "0";
869        foreach ($arrCat as $val) {
870            // 同一商品が存在する場合は登録しない
871            if ($val["product_id"] == $product_id) {
872                return;
873            }
874            // 最上位ランクを取得
875            $max = ($max < $val["rank"]) ? $val["rank"] : $max;
876        }
877        $sqlval["rank"] = $max + 1;
878        $objQuery->insert("dtb_product_categories", $sqlval);
879    }
880
881    /**
882     * 商品をカテゴリの末尾に追加する.
883     *
884     * @param integer $category_id カテゴリID
885     * @param integer $product_id プロダクトID
886     * @return void
887     */
888    function addProductAfterCategories($category_id, $product_id) {
889        $sqlval = array("category_id" => $category_id,
890                        "product_id" => $product_id);
891
892        $objQuery = new SC_Query();
893
894        // 現在の商品カテゴリを取得
895        $arrCat = $objQuery->select("product_id, category_id, rank",
896                                    "dtb_product_categories",
897                                    "category_id = ?",
898                                    array($category_id));
899
900        $min = 0;
901        foreach ($arrCat as $val) {
902            // 同一商品が存在する場合は登録しない
903            if ($val["product_id"] == $product_id) {
904                return;
905            }
906            // 最下位ランクを取得
907            $min = ($min < $val["rank"]) ? $val["rank"] : $min;
908        }
909        $sqlval["rank"] = $min;
910        $objQuery->insert("dtb_product_categories", $sqlval);
911    }
912
913    /**
914     * 商品をカテゴリから削除する.
915     *
916     * @param integer $category_id カテゴリID
917     * @param integer $product_id プロダクトID
918     * @return void
919     */
920    function removeProductByCategories($category_id, $product_id) {
921        $sqlval = array("category_id" => $category_id,
922                        "product_id" => $product_id);
923        $objQuery = new SC_Query();
924        $objQuery->delete("dtb_product_categories",
925                          "category_id = ? AND product_id = ?", $sqlval);
926    }
927
928    /**
929     * 商品カテゴリを更新する.
930     *
931     * @param array $arrCategory_id 登録するカテゴリIDの配列
932     * @param integer $product_id プロダクトID
933     * @return void
934     */
935    function updateProductCategories($arrCategory_id, $product_id) {
936        $objQuery = new SC_Query();
937
938        // 現在のカテゴリ情報を取得
939        $arrCurrentCat = $objQuery->select("product_id, category_id, rank",
940                                           "dtb_product_categories",
941                                           "product_id = ?",
942                                           array($product_id));
943
944        // 登録するカテゴリ情報と比較
945        foreach ($arrCurrentCat as $val) {
946
947            // 登録しないカテゴリを削除
948            if (!in_array($val["category_id"], $arrCategory_id)) {
949                $this->removeProductByCategories($val["category_id"], $product_id);
950            }
951        }
952
953        // カテゴリを登録
954        foreach ($arrCategory_id as $category_id) {
955            $this->addProductBeforCategories($category_id, $product_id);
956        }
957    }
958
959    /**
960     * カテゴリ数の登録を行う.
961     *
962     * @param SC_Query $objQuery SC_Query インスタンス
963     * @return void
964     */
965    function sfCategory_Count($objQuery){
966
967        //テーブル内容の削除
968        $objQuery->query("DELETE FROM dtb_category_count");
969        $objQuery->query("DELETE FROM dtb_category_total_count");
970
971        $sql_where .= 'alldtl.del_flg = 0 AND alldtl.status = 1';
972        // 在庫無し商品の非表示
973        if (NOSTOCK_HIDDEN === true) {
974            $sql_where .= ' AND (alldtl.stock_max >= 1 OR alldtl.stock_unlimited_max = 1)';
975        }
976
977        //各カテゴリ内の商品数を数えて格納
978        $sql = <<< __EOS__
979            INSERT INTO dtb_category_count(category_id, product_count, create_date)
980            SELECT T1.category_id, count(T2.category_id), now()
981            FROM dtb_category AS T1
982                LEFT JOIN dtb_product_categories AS T2
983                    ON T1.category_id = T2.category_id
984                LEFT JOIN vw_products_allclass_detail AS alldtl
985                    ON T2.product_id = alldtl.product_id
986            WHERE $sql_where
987            GROUP BY T1.category_id, T2.category_id
988__EOS__;
989       
990        $objQuery->query($sql);
991
992        //子カテゴリ内の商品数を集計する
993       
994        // カテゴリ情報を取得
995        $arrCat = $objQuery->select('category_id', 'dtb_category');
996       
997        foreach ($arrCat as $row) {
998            $category_id = $row['category_id'];
999            $arrval = array();
1000           
1001            $arrval[] = $category_id;
1002           
1003            list($tmp_where, $tmp_arrval) = $this->sfGetCatWhere($category_id);
1004            if ($tmp_where != "") {
1005                $sql_where_product_ids = "alldtl.product_id IN (SELECT product_id FROM dtb_product_categories WHERE " . $tmp_where . ")";
1006                $arrval = array_merge((array)$arrval, (array)$tmp_arrval);
1007            } else {
1008                $sql_where_product_ids = '0<>0'; // 一致させない
1009            }
1010           
1011            $sql = <<< __EOS__
1012                INSERT INTO dtb_category_total_count (category_id, product_count, create_date)
1013                SELECT
1014                    ?
1015                    ,count(*)
1016                    ,now()
1017                FROM vw_products_allclass_detail AS alldtl
1018                WHERE ($sql_where) AND ($sql_where_product_ids)
1019__EOS__;
1020           
1021            $objQuery->query($sql, $arrval);
1022        }
1023    }
1024
1025    /**
1026     * 子IDの配列を返す.
1027     *
1028     * @param string $table テーブル名
1029     * @param string $pid_name 親ID名
1030     * @param string $id_name ID名
1031     * @param integer $id ID
1032     * @param array 子ID の配列
1033     */
1034    function sfGetChildsID($table, $pid_name, $id_name, $id) {
1035        $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id);
1036        return $arrRet;
1037    }
1038
1039    /**
1040     * 階層構造のテーブルから子ID配列を取得する.
1041     *
1042     * @param string $table テーブル名
1043     * @param string $pid_name 親ID名
1044     * @param string $id_name ID名
1045     * @param integer $id ID番号
1046     * @return array 子IDの配列
1047     */
1048    function sfGetChildrenArray($table, $pid_name, $id_name, $id) {
1049        $objQuery = new SC_Query();
1050        $col = $pid_name . "," . $id_name;
1051         $arrData = $objQuery->select($col, $table);
1052
1053        $arrPID = array();
1054        $arrPID[] = $id;
1055        $arrChildren = array();
1056        $arrChildren[] = $id;
1057
1058        $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID);
1059
1060        while(count($arrRet) > 0) {
1061            $arrChildren = array_merge($arrChildren, $arrRet);
1062            $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrRet);
1063        }
1064
1065        return $arrChildren;
1066    }
1067
1068    /**
1069     * 親ID直下の子IDをすべて取得する.
1070     *
1071     * @param array $arrData 親カテゴリの配列
1072     * @param string $pid_name 親ID名
1073     * @param string $id_name ID名
1074     * @param array $arrPID 親IDの配列
1075     * @return array 子IDの配列
1076     */
1077    function sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID) {
1078        $arrChildren = array();
1079        $max = count($arrData);
1080
1081        for($i = 0; $i < $max; $i++) {
1082            foreach($arrPID as $val) {
1083                if($arrData[$i][$pid_name] == $val) {
1084                    $arrChildren[] = $arrData[$i][$id_name];
1085                }
1086            }
1087        }
1088        return $arrChildren;
1089    }
1090
1091    /**
1092     * 所属するすべての階層の親IDを配列で返す.
1093     *
1094     * @param SC_Query $objQuery SC_Query インスタンス
1095     * @param string $table テーブル名
1096     * @param string $pid_name 親ID名
1097     * @param string $id_name ID名
1098     * @param integer $id ID
1099     * @return array 親IDの配列
1100     */
1101    function sfGetParents($objQuery, $table, $pid_name, $id_name, $id) {
1102        $arrRet = $this->sfGetParentsArray($table, $pid_name, $id_name, $id);
1103        // 配列の先頭1つを削除する。
1104        array_shift($arrRet);
1105        return $arrRet;
1106    }
1107
1108    /**
1109     * 階層構造のテーブルから親ID配列を取得する.
1110     *
1111     * @param string $table テーブル名
1112     * @param string $pid_name 親ID名
1113     * @param string $id_name ID名
1114     * @param integer $id ID
1115     * @return array 親IDの配列
1116     */
1117    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
1118        $objQuery = new SC_Query();
1119        $col = $pid_name . "," . $id_name;
1120        $arrData = $objQuery->select($col, $table);
1121
1122        $arrParents = array();
1123        $arrParents[] = $id;
1124        $child = $id;
1125
1126        $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
1127
1128        while($ret != "") {
1129            $arrParents[] = $ret;
1130            $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
1131        }
1132
1133        $arrParents = array_reverse($arrParents);
1134
1135        return $arrParents;
1136    }
1137
1138    /**
1139     * カテゴリから商品を検索する場合のWHERE文と値を返す.
1140     *
1141     * @param integer $category_id カテゴリID
1142     * @return array 商品を検索する場合の配列
1143     */
1144    function sfGetCatWhere($category_id) {
1145        // 子カテゴリIDの取得
1146        $arrRet = $this->sfGetChildsID("dtb_category", "parent_category_id", "category_id", $category_id);
1147        $tmp_where = "";
1148        foreach ($arrRet as $val) {
1149            if($tmp_where == "") {
1150                $tmp_where.= "category_id IN ( ?";
1151            } else {
1152                $tmp_where.= ",? ";
1153            }
1154            $arrval[] = $val;
1155        }
1156        $tmp_where.= " ) ";
1157        return array($tmp_where, $arrval);
1158    }
1159
1160    /**
1161     * 受注一時テーブルから情報を取得する.
1162     *
1163     * @param integer $order_temp_id 受注一時ID
1164     * @return array 受注一時情報の配列
1165     */
1166    function sfGetOrderTemp($order_temp_id) {
1167        $objQuery = new SC_Query();
1168        $where = "order_temp_id = ?";
1169        $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));
1170        return $arrRet[0];
1171    }
1172
1173    /**
1174     * SELECTボックス用リストを作成する.
1175     *
1176     * @param string $table テーブル名
1177     * @param string $keyname プライマリーキーのカラム名
1178     * @param string $valname データ内容のカラム名
1179     * @return array SELECT ボックス用リストの配列
1180     */
1181    function sfGetIDValueList($table, $keyname, $valname) {
1182        $objQuery = new SC_Query();
1183        $col = "$keyname, $valname";
1184        $objQuery->setwhere("del_flg = 0");
1185        $objQuery->setorder("rank DESC");
1186        $arrList = $objQuery->select($col, $table);
1187        $count = count($arrList);
1188        for($cnt = 0; $cnt < $count; $cnt++) {
1189            $key = $arrList[$cnt][$keyname];
1190            $val = $arrList[$cnt][$valname];
1191            $arrRet[$key] = $val;
1192        }
1193        return $arrRet;
1194    }
1195
1196    /**
1197     * ランキングを上げる.
1198     *
1199     * @param string $table テーブル名
1200     * @param string $colname カラム名
1201     * @param string|integer $id テーブルのキー
1202     * @param string $andwhere SQL の AND 条件である WHERE 句
1203     * @return void
1204     */
1205    function sfRankUp($table, $colname, $id, $andwhere = "") {
1206        $objQuery = new SC_Query();
1207        $objQuery->begin();
1208        $where = "$colname = ?";
1209        if($andwhere != "") {
1210            $where.= " AND $andwhere";
1211        }
1212        // 対象項目のランクを取得
1213        $rank = $objQuery->get($table, "rank", $where, array($id));
1214        // ランクの最大値を取得
1215        $maxrank = $objQuery->max($table, "rank", $andwhere);
1216        // ランクが最大値よりも小さい場合に実行する。
1217        if($rank < $maxrank) {
1218            // ランクが一つ上のIDを取得する。
1219            $where = "rank = ?";
1220            if($andwhere != "") {
1221                $where.= " AND $andwhere";
1222            }
1223            $uprank = $rank + 1;
1224            $up_id = $objQuery->get($table, $colname, $where, array($uprank));
1225            // ランク入れ替えの実行
1226            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1227            if($andwhere != "") {
1228                $sqlup.= " AND $andwhere";
1229            }
1230            $objQuery->exec($sqlup, array($rank + 1, $id));
1231            $objQuery->exec($sqlup, array($rank, $up_id));
1232        }
1233        $objQuery->commit();
1234    }
1235
1236    /**
1237     * ランキングを下げる.
1238     *
1239     * @param string $table テーブル名
1240     * @param string $colname カラム名
1241     * @param string|integer $id テーブルのキー
1242     * @param string $andwhere SQL の AND 条件である WHERE 句
1243     * @return void
1244     */
1245    function sfRankDown($table, $colname, $id, $andwhere = "") {
1246        $objQuery = new SC_Query();
1247        $objQuery->begin();
1248        $where = "$colname = ?";
1249        if($andwhere != "") {
1250            $where.= " AND $andwhere";
1251        }
1252        // 対象項目のランクを取得
1253        $rank = $objQuery->get($table, "rank", $where, array($id));
1254
1255        // ランクが1(最小値)よりも大きい場合に実行する。
1256        if($rank > 1) {
1257            // ランクが一つ下のIDを取得する。
1258            $where = "rank = ?";
1259            if($andwhere != "") {
1260                $where.= " AND $andwhere";
1261            }
1262            $downrank = $rank - 1;
1263            $down_id = $objQuery->get($table, $colname, $where, array($downrank));
1264            // ランク入れ替えの実行
1265            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1266            if($andwhere != "") {
1267                $sqlup.= " AND $andwhere";
1268            }
1269            $objQuery->exec($sqlup, array($rank - 1, $id));
1270            $objQuery->exec($sqlup, array($rank, $down_id));
1271        }
1272        $objQuery->commit();
1273    }
1274
1275    /**
1276     * 指定順位へ移動する.
1277     *
1278     * @param string $tableName テーブル名
1279     * @param string $keyIdColumn キーを保持するカラム名
1280     * @param string|integer $keyId キーの値
1281     * @param integer $pos 指定順位
1282     * @param string $where SQL の AND 条件である WHERE 句
1283     * @return void
1284     */
1285    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
1286        $objQuery = new SC_Query();
1287        $objQuery->begin();
1288
1289        // 自身のランクを取得する
1290        if($where != "") {
1291            $getWhere = "$keyIdColumn = ? AND " . $where;
1292        } else {
1293            $getWhere = "$keyIdColumn = ?";
1294        }
1295        $rank = $objQuery->get($tableName, "rank", $getWhere, array($keyId));
1296
1297        $max = $objQuery->max($tableName, "rank", $where);
1298
1299        // 値の調整(逆順)
1300        if($pos > $max) {
1301            $position = 1;
1302        } else if($pos < 1) {
1303            $position = $max;
1304        } else {
1305            $position = $max - $pos + 1;
1306        }
1307
1308        //入れ替え先の順位が入れ換え元の順位より大きい場合
1309        if( $position > $rank ) $term = "rank - 1";
1310
1311        //入れ替え先の順位が入れ換え元の順位より小さい場合
1312        if( $position < $rank ) $term = "rank + 1";
1313
1314        // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合
1315        if (!isset($term)) $term = "rank";
1316
1317        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1318        $sql = "UPDATE $tableName SET rank = $term WHERE rank BETWEEN ? AND ?";
1319        if($where != "") {
1320            $sql.= " AND $where";
1321        }
1322
1323        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
1324        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
1325
1326        // 指定した順位へrankを書き換える。
1327        $sql  = "UPDATE $tableName SET rank = ? WHERE $keyIdColumn = ? ";
1328        if($where != "") {
1329            $sql.= " AND $where";
1330        }
1331
1332        $objQuery->exec( $sql, array( $position, $keyId ) );
1333        $objQuery->commit();
1334    }
1335
1336    /**
1337     * ランクを含むレコードを削除する.
1338     *
1339     * レコードごと削除する場合は、$deleteをtrueにする
1340     *
1341     * @param string $table テーブル名
1342     * @param string $colname カラム名
1343     * @param string|integer $id テーブルのキー
1344     * @param string $andwhere SQL の AND 条件である WHERE 句
1345     * @param bool $delete レコードごと削除する場合 true,
1346     *                     レコードごと削除しない場合 false
1347     * @return void
1348     */
1349    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
1350                                $delete = false) {
1351        $objQuery = new SC_Query();
1352        $objQuery->begin();
1353        // 削除レコードのランクを取得する。
1354        $where = "$colname = ?";
1355        if($andwhere != "") {
1356            $where.= " AND $andwhere";
1357        }
1358        $rank = $objQuery->get($table, "rank", $where, array($id));
1359
1360        if(!$delete) {
1361            // ランクを最下位にする、DELフラグON
1362            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1 ";
1363            $sqlup.= "WHERE $colname = ?";
1364            // UPDATEの実行
1365            $objQuery->exec($sqlup, array($id));
1366        } else {
1367            $objQuery->delete($table, "$colname = ?", array($id));
1368        }
1369
1370        // 追加レコードのランクより上のレコードを一つずらす。
1371        $where = "rank > ?";
1372        if($andwhere != "") {
1373            $where.= " AND $andwhere";
1374        }
1375        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1376        $objQuery->exec($sqlup, array($rank));
1377        $objQuery->commit();
1378    }
1379
1380    /**
1381     * 親IDの配列を元に特定のカラムを取得する.
1382     *
1383     * @param SC_Query $objQuery SC_Query インスタンス
1384     * @param string $table テーブル名
1385     * @param string $id_name ID名
1386     * @param string $col_name カラム名
1387     * @param array $arrId IDの配列
1388     * @return array 特定のカラムの配列
1389     */
1390    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) {
1391        $col = $col_name;
1392        $len = count($arrId);
1393        $where = "";
1394
1395        for($cnt = 0; $cnt < $len; $cnt++) {
1396            if($where == "") {
1397                $where = "$id_name = ?";
1398            } else {
1399                $where.= " OR $id_name = ?";
1400            }
1401        }
1402
1403        $objQuery->setorder("level");
1404        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1405        return $arrRet;
1406    }
1407
1408    /**
1409     * カテゴリ変更時の移動処理を行う.
1410     *
1411     * @param SC_Query $objQuery SC_Query インスタンス
1412     * @param string $table テーブル名
1413     * @param string $id_name ID名
1414     * @param string $cat_name カテゴリ名
1415     * @param integer $old_catid 旧カテゴリID
1416     * @param integer $new_catid 新カテゴリID
1417     * @param integer $id ID
1418     * @return void
1419     */
1420    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1421        if ($old_catid == $new_catid) {
1422            return;
1423        }
1424        // 旧カテゴリでのランク削除処理
1425        // 移動レコードのランクを取得する。
1426        $where = "$id_name = ?";
1427        $rank = $objQuery->get($table, "rank", $where, array($id));
1428        // 削除レコードのランクより上のレコードを一つ下にずらす。
1429        $where = "rank > ? AND $cat_name = ?";
1430        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1431        $objQuery->exec($sqlup, array($rank, $old_catid));
1432        // 新カテゴリでの登録処理
1433        // 新カテゴリの最大ランクを取得する。
1434        $max_rank = $objQuery->max($table, "rank", "$cat_name = ?", array($new_catid)) + 1;
1435        $where = "$id_name = ?";
1436        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1437        $objQuery->exec($sqlup, array($max_rank, $id));
1438    }
1439
1440    /**
1441     * お届け時間を取得する.
1442     *
1443     * @param integer $payment_id 支払い方法ID
1444     * @return array お届け時間の配列
1445     */
1446    function sfGetDelivTime($payment_id = "") {
1447        $objQuery = new SC_Query();
1448
1449        $deliv_id = "";
1450        $arrRet = array();
1451
1452        if($payment_id != "") {
1453            $where = "del_flg = 0 AND payment_id = ?";
1454            $arrRet = $objQuery->select("deliv_id", "dtb_payment", $where, array($payment_id));
1455            $deliv_id = $arrRet[0]['deliv_id'];
1456        }
1457
1458        if($deliv_id != "") {
1459            $objQuery->setorder("time_id");
1460            $where = "deliv_id = ?";
1461            $arrRet= $objQuery->select("time_id, deliv_time", "dtb_delivtime", $where, array($deliv_id));
1462        }
1463
1464        return $arrRet;
1465    }
1466
1467    /**
1468     * 都道府県、支払い方法から配送料金を取得する.
1469     *
1470     * @param array $arrData 各種情報
1471     * @return string 指定の都道府県, 支払い方法の配送料金
1472     */
1473    function sfGetDelivFee($arrData) {
1474        $pref = $arrData['deliv_pref'];
1475        $payment_id = isset($arrData['payment_id']) ? $arrData['payment_id'] : "";
1476
1477        $objQuery = new SC_Query();
1478
1479        $deliv_id = "";
1480
1481        // 支払い方法が指定されている場合は、対応した配送業者を取得する
1482        if($payment_id != "") {
1483            $where = "del_flg = 0 AND payment_id = ?";
1484            $arrRet = $objQuery->select("deliv_id", "dtb_payment", $where, array($payment_id));
1485            $deliv_id = $arrRet[0]['deliv_id'];
1486        // 支払い方法が指定されていない場合は、先頭の配送業者を取得する
1487        } else {
1488            $where = "del_flg = 0";
1489            $objQuery->setOrder("rank DESC");
1490            $objQuery->setLimitOffset(1);
1491            $arrRet = $objQuery->select("deliv_id", "dtb_deliv", $where);
1492            $deliv_id = $arrRet[0]['deliv_id'];
1493        }
1494
1495        // 配送業者から配送料を取得
1496        if($deliv_id != "") {
1497
1498            // 都道府県が指定されていない場合は、東京都の番号を指定しておく
1499            if($pref == "") {
1500                $pref = 13;
1501            }
1502
1503            $objQuery = new SC_Query();
1504            $where = "deliv_id = ? AND pref = ?";
1505            $arrRet= $objQuery->select("fee", "dtb_delivfee", $where, array($deliv_id, $pref));
1506        }
1507        return $arrRet[0]['fee'];
1508    }
1509
1510    /**
1511     * 集計情報を元に最終計算を行う.
1512     *
1513     * @param array $arrData 各種情報
1514     * @param LC_Page $objPage LC_Page インスタンス
1515     * @param SC_CartSession $objCartSess SC_CartSession インスタンス
1516     * @param null $dummy1 互換性確保用(決済モジュール互換のため)
1517     * @param SC_Customer $objCustomer SC_Customer インスタンス
1518     * @return array 最終計算後の配列
1519     */
1520    function sfTotalConfirm($arrData, &$objPage, &$objCartSess, $dummy1 = null, $objCustomer = "") {
1521        // 店舗基本情報を取得する
1522        $arrInfo = SC_Helper_DB_Ex::sf_getBasisData();
1523       
1524        // 未定義変数を定義
1525        if (!isset($arrData['deliv_pref'])) $arrData['deliv_pref'] = "";
1526        if (!isset($arrData['payment_id'])) $arrData['payment_id'] = "";
1527        if (!isset($arrData['charge'])) $arrData['charge'] = "";
1528        if (!isset($arrData['use_point'])) $arrData['use_point'] = "";
1529        if (!isset($arrData['add_point'])) $arrData['add_point'] = 0;
1530
1531        // 税金の取得
1532        $arrData['tax'] = $objPage->tpl_total_tax;
1533        // 小計の取得
1534        $arrData['subtotal'] = $objPage->tpl_total_pretax;
1535
1536        // 合計送料の取得
1537        $arrData['deliv_fee'] = 0;
1538
1539        // 商品ごとの送料が有効の場合
1540        if (OPTION_PRODUCT_DELIV_FEE == 1) {
1541            // 全商品の合計送料を加算する
1542            $this->lfAddAllProductsDelivFee($arrData, $objPage, $objCartSess);
1543        }
1544
1545        // 配送業者の送料が有効の場合
1546        if (OPTION_DELIV_FEE == 1) {
1547            // 都道府県、支払い方法から配送料金を加算する
1548            $this->lfAddDelivFee($arrData);
1549        }
1550
1551        // 送料無料の購入数が設定されている場合
1552        if (DELIV_FREE_AMOUNT > 0) {
1553            // 商品の合計数量
1554            $total_quantity = $objCartSess->getTotalQuantity(true);
1555           
1556            if($total_quantity >= DELIV_FREE_AMOUNT) {
1557                $arrData['deliv_fee'] = 0;
1558            }
1559        }
1560
1561        // 送料無料条件が設定されている場合
1562        if($arrInfo['free_rule'] > 0) {
1563            // 小計が無料条件を超えている場合
1564            if($arrData['subtotal'] >= $arrInfo['free_rule']) {
1565                $arrData['deliv_fee'] = 0;
1566            }
1567        }
1568
1569        // 合計の計算
1570        $arrData['total'] = $objPage->tpl_total_pretax; // 商品合計
1571        $arrData['total']+= $arrData['deliv_fee'];      // 送料
1572        $arrData['total']+= $arrData['charge'];         // 手数料
1573        // お支払い合計
1574        $arrData['payment_total'] = $arrData['total'] - ($arrData['use_point'] * POINT_VALUE);
1575        // 加算ポイントの計算
1576        if (USE_POINT !== false) {
1577            $arrData['add_point'] = SC_Helper_DB_Ex::sfGetAddPoint($objPage->tpl_total_point, $arrData['use_point']);
1578               
1579            if($objCustomer != "") {
1580                // 誕生日月であった場合
1581                if($objCustomer->isBirthMonth()) {
1582                    $arrData['birth_point'] = BIRTH_MONTH_POINT;
1583                    $arrData['add_point'] += $arrData['birth_point'];
1584                }
1585            }
1586        }
1587
1588        if($arrData['add_point'] < 0) {
1589            $arrData['add_point'] = 0;
1590        }
1591        return $arrData;
1592    }
1593
1594    /**
1595     * レコードの存在チェックを行う.
1596     *
1597     * @param string $table テーブル名
1598     * @param string $col カラム名
1599     * @param array $arrval 要素の配列
1600     * @param array $addwhere SQL の AND 条件である WHERE 句
1601     * @return bool レコードが存在する場合 true
1602     */
1603    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1604        $objQuery = new SC_Query();
1605        $arrCol = split("[, ]", $col);
1606
1607        $where = "del_flg = 0";
1608
1609        if($addwhere != "") {
1610            $where.= " AND $addwhere";
1611        }
1612
1613        foreach($arrCol as $val) {
1614            if($val != "") {
1615                if($where == "") {
1616                    $where = "$val = ?";
1617                } else {
1618                    $where.= " AND $val = ?";
1619                }
1620            }
1621        }
1622        $ret = $objQuery->get($table, $col, $where, $arrval);
1623
1624        if($ret != "") {
1625            return true;
1626        }
1627        return false;
1628    }
1629
1630    /**
1631     * メーカー商品数数の登録を行う.
1632     *
1633     * @param SC_Query $objQuery SC_Query インスタンス
1634     * @return void
1635     */
1636    function sfMaker_Count($objQuery){
1637        $sql = "";
1638
1639        //テーブル内容の削除
1640        $objQuery->query("DELETE FROM dtb_maker_count");
1641
1642        //各メーカーの商品数を数えて格納
1643        $sql = " INSERT INTO dtb_maker_count(maker_id, product_count, create_date) ";
1644        $sql .= " SELECT T1.maker_id, count(T2.maker_id), now() ";
1645        $sql .= " FROM dtb_maker AS T1 LEFT JOIN dtb_products AS T2";
1646        $sql .= " ON T1.maker_id = T2.maker_id ";
1647        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
1648        $sql .= " GROUP BY T1.maker_id, T2.maker_id ";
1649        $objQuery->query($sql);
1650    }
1651
1652    /**
1653     * 選択中の商品のメーカーを取得する.
1654     *
1655     * @param integer $product_id プロダクトID
1656     * @param integer $maker_id メーカーID
1657     * @return array 選択中の商品のメーカーIDの配列
1658     *
1659     */
1660    function sfGetMakerId($product_id, $maker_id = 0, $closed = false) {
1661        if ($closed) {
1662            $status = "";
1663        } else {
1664            $status = "status = 1";
1665        }
1666
1667        if (!$this->g_maker_on) {
1668            $this->g_maker_on = true;
1669            $maker_id = (int) $maker_id;
1670            $product_id = (int) $product_id;
1671            if (SC_Utils_Ex::sfIsInt($maker_id) && $maker_id != 0 && $this->sfIsRecord("dtb_maker","maker_id", $maker_id)) {
1672                $this->g_maker_id = array($maker_id);
1673            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
1674                $objQuery = new SC_Query();
1675                $where = "product_id = ?";
1676                $maker_id = $objQuery->getCol("dtb_products", "maker_id", "product_id = ?", array($product_id));
1677                $this->g_maker_id = $maker_id;
1678            } else {
1679                // 不正な場合は、空の配列を返す。
1680                $this->g_maker_id = array();
1681            }
1682        }
1683        return $this->g_maker_id;
1684    }
1685
1686    /**
1687     * メーカーの取得を行う.
1688     *
1689     * $products_check:true商品登録済みのものだけ取得する
1690     *
1691     * @param string $addwhere 追加する WHERE 句
1692     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
1693     * @return array カテゴリツリーの配列
1694     */
1695    function sfGetMakerList($addwhere = "", $products_check = false) {
1696        $objQuery = new SC_Query();
1697        $where = "del_flg = 0";
1698
1699        if($addwhere != "") {
1700            $where.= " AND $addwhere";
1701        }
1702
1703        $objQuery->setoption("ORDER BY rank DESC");
1704
1705        if($products_check) {
1706            $col = "T1.maker_id, name";
1707            $from = "dtb_maker AS T1 LEFT JOIN dtb_maker_count AS T2 ON T1.maker_id = T2.maker_id";
1708            $where .= " AND product_count > 0";
1709        } else {
1710            $col = "maker_id, name";
1711            $from = "dtb_maker";
1712        }
1713
1714        $arrRet = $objQuery->select($col, $from, $where);
1715
1716        $max = count($arrRet);
1717        for($cnt = 0; $cnt < $max; $cnt++) {
1718            $id = $arrRet[$cnt]['maker_id'];
1719            $name = $arrRet[$cnt]['name'];
1720            $arrList[$id].= $name;
1721        }
1722        return $arrList;
1723    }
1724
1725    /**
1726     * 全商品の合計送料を加算する
1727     */
1728    function lfAddAllProductsDelivFee(&$arrData, &$objPage, &$objCartSess) {
1729        $arrData['deliv_fee'] += $this->lfCalcAllProductsDelivFee($arrData, $objCartSess);
1730    }
1731
1732    /**
1733     * 全商品の合計送料を計算する
1734     */
1735    function lfCalcAllProductsDelivFee(&$arrData, &$objCartSess) {
1736        $objQuery = new SC_Query();
1737        $deliv_fee_total = 0;
1738        $max = $objCartSess->getMax();
1739        for ($i = 0; $i <= $max; $i++) {
1740            // 商品送料
1741            $deliv_fee = $objQuery->getOne('SELECT deliv_fee FROM dtb_products WHERE product_id = ?', array($_SESSION[$objCartSess->key][$i]['id'][0]));
1742            // 数量
1743            $quantity = $_SESSION[$objCartSess->key][$i]['quantity'];
1744            // 累積
1745            $deliv_fee_total += $deliv_fee * $quantity;
1746        }
1747        return $deliv_fee_total;
1748    }
1749
1750    /**
1751     * 都道府県、支払い方法から配送料金を加算する.
1752     *
1753     * @param array $arrData 各種情報
1754     */
1755    function lfAddDelivFee(&$arrData) {
1756        $arrData['deliv_fee'] += $this->sfGetDelivFee($arrData);
1757    }
1758
1759    /**
1760     * 受注の名称列を更新する
1761     *
1762     * @param integer $order_id 更新対象の注文番号
1763     * @param boolean $temp_table 更新対象は「受注_Temp」か
1764     */
1765    function sfUpdateOrderNameCol($order_id, $temp_table = false) {
1766        $objQuery = new SC_Query();
1767       
1768        if ($temp_table) {
1769            $tgt_table = 'dtb_order_temp';
1770            $sql_where = 'WHERE order_temp_id = ?';
1771        } else {
1772            $tgt_table = 'dtb_order';
1773            $sql_where = 'WHERE order_id = ?';
1774        }
1775       
1776        $sql = <<< __EOS__
1777            UPDATE
1778                {$tgt_table}
1779            SET
1780                 payment_method = (SELECT payment_method FROM dtb_payment WHERE payment_id = {$tgt_table}.payment_id)
1781                ,deliv_time = (SELECT deliv_time FROM dtb_delivtime WHERE time_id = {$tgt_table}.deliv_time_id AND deliv_id = {$tgt_table}.deliv_id)
1782            $sql_where
1783__EOS__;
1784       
1785        $objQuery->query($sql, array($order_id));
1786    }
1787
1788    /**
1789     * 店舗基本情報に基づいて税金額を返す
1790     *
1791     * @param integer $price 計算対象の金額
1792     * @return integer 税金額
1793     */
1794    function sfTax($price) {
1795        // 店舗基本情報を取得
1796        $CONF = SC_Helper_DB_Ex::sf_getBasisData();
1797       
1798        return SC_Utils_Ex::sfTax($price, $CONF['tax'], $CONF['tax_rule']);
1799    }
1800
1801    /**
1802     * 店舗基本情報に基づいて税金付与した金額を返す
1803     *
1804     * @param integer $price 計算対象の金額
1805     * @return integer 税金付与した金額
1806     */
1807    function sfPreTax($price, $tax = null, $tax_rule = null) {
1808        // 店舗基本情報を取得
1809        $CONF = SC_Helper_DB_Ex::sf_getBasisData();
1810       
1811        return SC_Utils_Ex::sfPreTax($price, $CONF['tax'], $CONF['tax_rule']);
1812    }
1813
1814    /**
1815     * 店舗基本情報に基づいて加算ポイントを返す
1816     *
1817     * @param integer $totalpoint
1818     * @param integer $use_point
1819     * @return integer 加算ポイント
1820     */
1821    function sfGetAddPoint($totalpoint, $use_point) {
1822        // 店舗基本情報を取得
1823        $CONF = SC_Helper_DB_Ex::sf_getBasisData();
1824       
1825        return SC_Utils_Ex::sfGetAddPoint($totalpoint, $use_point, $CONF['point_rate']);
1826    }
1827
1828    /**
1829     * 受注.対応状況の更新
1830     *
1831     * ・必ず呼び出し元でトランザクションブロックを開いておくこと。
1832     *
1833     * @param integer $orderId 注文番号
1834     * @param integer|null $newStatus 対応状況 (null=変更無し)
1835     * @param integer|null $newAddPoint 加算ポイント (null=変更無し)
1836     * @param integer|null $newUsePoint ポイント (null=変更無し)
1837     * @return void
1838     */
1839    function sfUpdateOrderStatus($orderId, $newStatus = null, $newAddPoint = null, $newUsePoint = null) {
1840        $objQuery = new SC_Query();
1841       
1842        $arrOrderOld = $objQuery->getRow('dtb_order', 'status, add_point, use_point, customer_id', 'order_id = ?', array($orderId));
1843       
1844        // 対応状況
1845        if (is_null($newStatus)) {
1846            $newStatus = $arrOrderOld['status'];
1847        }
1848       
1849        if (USE_POINT !== false) {
1850            $addPoint = 0;
1851           
1852            // 使用ポイント
1853            if (!is_null($newUsePoint)) {
1854                $addPoint += $arrOrderOld['use_point']; // 変更前のポイントを戻す
1855                $addPoint -= $newUsePoint;              // 変更後のポイントを引く
1856            }
1857           
1858            // ▼加算ポイント
1859            // 変更前の状態が加算対象の場合、
1860            if (SC_Utils_Ex::sfIsAddPoint($arrOrderOld['status'])) {
1861                $addPoint -= $arrOrderOld['add_point'];
1862            }
1863           
1864            // 変更後の状態が加算対象の場合、
1865            if (SC_Utils_Ex::sfIsAddPoint($newStatus)) {
1866                $addPoint += is_null($newAddPoint) ? $arrOrderOld['add_point'] : $newAddPoint;
1867            }
1868            // ▲加算ポイント
1869           
1870            if ($addPoint != 0) {
1871                // ▼顧客テーブルの更新
1872                $sqlval = array();
1873                $where = '';
1874                $arrVal = array();
1875                $arrRawSql = array();
1876                $arrRawSqlVal = array();
1877               
1878                $sqlval['update_date'] = 'Now()';
1879                $arrRawSql['point'] = 'point + ?';
1880                $arrRawSqlVal[] = $addPoint;
1881                $where .= 'customer_id = ?';
1882                $arrVal[] = $arrOrderOld['customer_id'];
1883               
1884                $objQuery->update('dtb_customer', $sqlval, $where, $arrVal, $arrRawSql, $arrRawSqlVal);
1885                // ▲顧客テーブルの更新
1886               
1887                // ポイントをマイナスした場合、
1888                if ($addPoint < 0) {
1889                    $sql = 'SELECT point FROM dtb_customer WHERE customer_id = ?';
1890                    $point = $objQuery->getone($sql, array($arrOrderOld['customer_id']));
1891                    // 変更後のポイントがマイナスの場合、
1892                    if ($point < 0) {
1893                        // ロールバック
1894                        $objQuery->rollback();
1895                        // エラー
1896                        SC_Utils_Ex::sfDispSiteError(LACK_POINT);
1897                    }
1898                }
1899            }
1900        }
1901       
1902        // ▼受注テーブルの更新
1903        $sqlval = array();
1904        if (USE_POINT !== false) {
1905            if (!is_null($newAddPoint)) {
1906                $sqlval['add_point'] = $newAddPoint;
1907            }
1908            if (!is_null($newUsePoint)) {
1909                $sqlval['use_point'] = $newUsePoint;
1910            }
1911        }
1912        // ステータスが発送済みに変更の場合、発送日を更新
1913        if ($arrOrderOld['status'] != ORDER_DELIV && $newStatus == ORDER_DELIV) {
1914            $sqlval['commit_date'] = 'Now()';
1915        }
1916        $sqlval['status'] = $newStatus;
1917        $sqlval['update_date'] = 'Now()';
1918       
1919        $objQuery->update('dtb_order', $sqlval, 'order_id = ?', array($orderId));
1920        // ▲受注テーブルの更新
1921    }
1922
1923    /**
1924     * 指定ファイルが存在する場合 SQL として実行
1925     *
1926     * ・MySQL の場合、文字「;」を区切りとして、分割実行。
1927     * XXX プラグイン用に追加。将来消すかも。
1928     *
1929     * @param string $sqlFilePath SQL ファイルのパス
1930     * @return void
1931     */
1932    function sfExecSqlByFile($sqlFilePath) {
1933        if (file_exists($sqlFilePath)) {
1934            $objQuery = new SC_Query();
1935
1936            $sqls = file_get_contents($sqlFilePath);
1937            if ($sqls === false) SC_Utils_Ex::sfDispException('ファイルは存在するが読み込めない');
1938
1939            if (DB_TYPE == 'mysql') {
1940                foreach (explode(';', $sqls) as $sql) {
1941                    $sql = trim($sql);
1942                    if (strlen($sql) == 0) continue;
1943                    $objQuery->query($sql);
1944                }
1945            } else {
1946                $objQuery->query($sqls);
1947            }
1948        }
1949    }
1950
1951    /**
1952     * 商品規格を設定しているか
1953     *
1954     * @param integer $product_id 商品ID
1955     * @return bool 商品規格が存在する場合:true, それ以外:false
1956     */
1957    function sfHasProductClass($product_id) {
1958        if (!SC_Utils_Ex::sfIsInt($product_id)) return false;
1959
1960        $objQuery  = new SC_Query();
1961        $where = 'product_id = ? AND (classcategory_id1 <> 0 OR classcategory_id2 <> 0)';
1962        $count = $objQuery->count('dtb_products_class', $where, array($product_id));
1963
1964        return $count >= 1;
1965    }
1966}
1967?>
Note: See TracBrowser for help on using the repository browser.