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

Revision 18511, 73.2 KB checked in by Seasoft, 14 years ago (diff)

決済モジュールに対応するため、static メソッドとして扱う

  • 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            // ・決済モジュールに対応するため、static メソッドとして扱う
444            SC_Helper_DB_Ex::sfUpdateOrderNameCol($uniqid, true);
445        }
446    }
447
448    /**
449     * 会員情報から SQL文の値を生成する.
450     *
451     * @param string $uniqid ユニークID
452     * @param array $sqlval SQL の値の配列
453     * @return array 会員情報を含んだ SQL の値の配列
454     */
455    function sfGetCustomerSqlVal($uniqid, $sqlval) {
456        $objCustomer = new SC_Customer();
457        // 会員情報登録処理
458        if ($objCustomer->isLoginSuccess(true)) {
459            // 登録データの作成
460            $sqlval['order_temp_id'] = $uniqid;
461            $sqlval['update_date'] = 'Now()';
462            $sqlval['customer_id'] = $objCustomer->getValue('customer_id');
463            $sqlval['order_name01'] = $objCustomer->getValue('name01');
464            $sqlval['order_name02'] = $objCustomer->getValue('name02');
465            $sqlval['order_kana01'] = $objCustomer->getValue('kana01');
466            $sqlval['order_kana02'] = $objCustomer->getValue('kana02');
467            $sqlval['order_sex'] = $objCustomer->getValue('sex');
468            $sqlval['order_zip01'] = $objCustomer->getValue('zip01');
469            $sqlval['order_zip02'] = $objCustomer->getValue('zip02');
470            $sqlval['order_pref'] = $objCustomer->getValue('pref');
471            $sqlval['order_addr01'] = $objCustomer->getValue('addr01');
472            $sqlval['order_addr02'] = $objCustomer->getValue('addr02');
473            $sqlval['order_tel01'] = $objCustomer->getValue('tel01');
474            $sqlval['order_tel02'] = $objCustomer->getValue('tel02');
475            $sqlval['order_tel03'] = $objCustomer->getValue('tel03');
476            if (defined('MOBILE_SITE')) {
477                $email_mobile = $objCustomer->getValue('email_mobile');
478                if (empty($email_mobile)) {
479                    $sqlval['order_email'] = $objCustomer->getValue('email');
480                } else {
481                    $sqlval['order_email'] = $email_mobile;
482                }
483            } else {
484                $sqlval['order_email'] = $objCustomer->getValue('email');
485            }
486            $sqlval['order_job'] = $objCustomer->getValue('job');
487            $sqlval['order_birth'] = $objCustomer->getValue('birth');
488        }
489        return $sqlval;
490    }
491
492    /**
493     * 会員編集登録処理を行う.
494     *
495     * @param array $array パラメータの配列
496     * @param array $arrRegistColumn 登録するカラムの配列
497     * @return void
498     */
499    function sfEditCustomerData($array, $arrRegistColumn) {
500        $objQuery = new SC_Query();
501
502        foreach ($arrRegistColumn as $data) {
503            if ($data["column"] != "password") {
504                if($array[ $data['column'] ] != "") {
505                    $arrRegist[ $data["column"] ] = $array[ $data["column"] ];
506                } else {
507                    $arrRegist[ $data['column'] ] = NULL;
508                }
509            }
510        }
511        if (strlen($array["year"]) > 0 && strlen($array["month"]) > 0 && strlen($array["day"]) > 0) {
512            $arrRegist["birth"] = $array["year"] ."/". $array["month"] ."/". $array["day"] ." 00:00:00";
513        } else {
514            $arrRegist["birth"] = NULL;
515        }
516
517        //-- パスワードの更新がある場合は暗号化。(更新がない場合はUPDATE文を構成しない)
518        if ($array["password"] != DEFAULT_PASSWORD) $arrRegist["password"] = sha1($array["password"] . ":" . AUTH_MAGIC);
519        $arrRegist["update_date"] = "NOW()";
520
521        //-- 編集登録実行
522        $objQuery->update("dtb_customer", $arrRegist, "customer_id = ? ", array($array['customer_id']));
523    }
524
525    /**
526     * 注文番号、利用ポイント、加算ポイントから最終ポイントを取得する.
527     *
528     * @param integer $order_id 注文番号
529     * @param integer $use_point 利用ポイント
530     * @param integer $add_point 加算ポイント
531     * @return array 最終ポイントの配列
532     */
533    function sfGetCustomerPoint($order_id, $use_point, $add_point) {
534        $objQuery = new SC_Query();
535        $arrRet = $objQuery->select("customer_id", "dtb_order", "order_id = ?", array($order_id));
536        $customer_id = $arrRet[0]['customer_id'];
537        if ($customer_id != "" && $customer_id >= 1) {
538            if (USE_POINT !== false) {
539                $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id));
540                $point = $arrRet[0]['point'];
541                $total_point = $arrRet[0]['point'] - $use_point + $add_point;
542            } else {
543                $total_point = 0;
544                $point = 0;
545            }
546        } else {
547            $total_point = "";
548            $point = "";
549        }
550        return array($point, $total_point);
551    }
552
553    /**
554     * 顧客番号、利用ポイント、加算ポイントから最終ポイントを取得する.
555     *
556     * @param integer $customer_id 顧客番号
557     * @param integer $use_point 利用ポイント
558     * @param integer $add_point 加算ポイント
559     * @return array 最終ポイントの配列
560     */
561    function sfGetCustomerPointFromCid($customer_id, $use_point, $add_point) {
562        $objQuery = new SC_Query();
563        if (USE_POINT !== false) {
564            $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id));
565            $point = $arrRet[0]['point'];
566            $total_point = $arrRet[0]['point'] - $use_point + $add_point;
567        } else {
568            $total_point = 0;
569            $point = 0;
570        }
571        return array($point, $total_point);
572    }
573    /**
574     * カテゴリツリーの取得を行う.
575     *
576     * @param integer $parent_category_id 親カテゴリID
577     * @param bool $count_check 登録商品数のチェックを行う場合 true
578     * @return array カテゴリツリーの配列
579     */
580    function sfGetCatTree($parent_category_id, $count_check = false) {
581        $objQuery = new SC_Query();
582        $col = "";
583        $col .= " cat.category_id,";
584        $col .= " cat.category_name,";
585        $col .= " cat.parent_category_id,";
586        $col .= " cat.level,";
587        $col .= " cat.rank,";
588        $col .= " cat.creator_id,";
589        $col .= " cat.create_date,";
590        $col .= " cat.update_date,";
591        $col .= " cat.del_flg, ";
592        $col .= " ttl.product_count";
593        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
594        // 登録商品数のチェック
595        if($count_check) {
596            $where = "del_flg = 0 AND product_count > 0";
597        } else {
598            $where = "del_flg = 0";
599        }
600        $objQuery->setoption("ORDER BY rank DESC");
601        $arrRet = $objQuery->select($col, $from, $where);
602
603        $arrParentID = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
604
605        foreach($arrRet as $key => $array) {
606            foreach($arrParentID as $val) {
607                if($array['category_id'] == $val) {
608                    $arrRet[$key]['display'] = 1;
609                    break;
610                }
611            }
612        }
613
614        return $arrRet;
615    }
616
617    /**
618     * カテゴリツリーの取得を複数カテゴリーで行う.
619     *
620     * @param integer $product_id 商品ID
621     * @param bool $count_check 登録商品数のチェックを行う場合 true
622     * @return array カテゴリツリーの配列
623     */
624    function sfGetMultiCatTree($product_id, $count_check = false) {
625        $objQuery = new SC_Query();
626        $col = "";
627        $col .= " cat.category_id,";
628        $col .= " cat.category_name,";
629        $col .= " cat.parent_category_id,";
630        $col .= " cat.level,";
631        $col .= " cat.rank,";
632        $col .= " cat.creator_id,";
633        $col .= " cat.create_date,";
634        $col .= " cat.update_date,";
635        $col .= " cat.del_flg, ";
636        $col .= " ttl.product_count";
637        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
638        // 登録商品数のチェック
639        if($count_check) {
640            $where = "del_flg = 0 AND product_count > 0";
641        } else {
642            $where = "del_flg = 0";
643        }
644        $objQuery->setoption("ORDER BY rank DESC");
645        $arrRet = $objQuery->select($col, $from, $where);
646
647        $arrCategory_id = $this->sfGetCategoryId($product_id);
648
649        $arrCatTree = array();
650        foreach ($arrCategory_id as $pkey => $parent_category_id) {
651            $arrParentID = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
652
653            foreach($arrParentID as $pid) {
654                foreach($arrRet as $key => $array) {
655                    if($array['category_id'] == $pid) {
656                        $arrCatTree[$pkey][] = $arrRet[$key];
657                        break;
658                    }
659                }
660            }
661        }
662
663        return $arrCatTree;
664    }
665
666    /**
667     * 親カテゴリーを連結した文字列を取得する.
668     *
669     * @param integer $category_id カテゴリID
670     * @return string 親カテゴリーを連結した文字列
671     */
672    function sfGetCatCombName($category_id){
673        // 商品が属するカテゴリIDを縦に取得
674        $objQuery = new SC_Query();
675        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
676        $ConbName = "";
677
678        // カテゴリー名称を取得する
679        foreach($arrCatID as $key => $val){
680            $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
681            $arrVal = array($val);
682            $CatName = $objQuery->getOne($sql,$arrVal);
683            $ConbName .= $CatName . ' | ';
684        }
685        // 最後の | をカットする
686        $ConbName = substr_replace($ConbName, "", strlen($ConbName) - 2, 2);
687
688        return $ConbName;
689    }
690
691    /**
692     * 指定したカテゴリーIDのカテゴリーを取得する.
693     *
694     * @param integer $category_id カテゴリID
695     * @return array 指定したカテゴリーIDのカテゴリー
696     */
697    function sfGetCat($category_id){
698        $objQuery = new SC_Query();
699
700        // カテゴリーを取得する
701        $arrVal = array($category_id);
702        $res = $objQuery->select('category_id AS id, category_name AS name', 'dtb_category', 'category_id = ?', $arrVal);
703
704        return $res[0];
705    }
706
707    /**
708     * 指定したカテゴリーIDの大カテゴリーを取得する.
709     *
710     * @param integer $category_id カテゴリID
711     * @return array 指定したカテゴリーIDの大カテゴリー
712     */
713    function sfGetFirstCat($category_id){
714        // 商品が属するカテゴリIDを縦に取得
715        $objQuery = new SC_Query();
716        $arrRet = array();
717        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
718        $arrRet['id'] = $arrCatID[0];
719
720        // カテゴリー名称を取得する
721        $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
722        $arrVal = array($arrRet['id']);
723        $arrRet['name'] = $objQuery->getOne($sql,$arrVal);
724
725        return $arrRet;
726    }
727
728    /**
729     * カテゴリツリーの取得を行う.
730     *
731     * $products_check:true商品登録済みのものだけ取得する
732     *
733     * @param string $addwhere 追加する WHERE 句
734     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
735     * @param string $head カテゴリ名のプレフィックス文字列
736     * @return array カテゴリツリーの配列
737     */
738    function sfGetCategoryList($addwhere = "", $products_check = false, $head = CATEGORY_HEAD) {
739        $objQuery = new SC_Query();
740        $where = "del_flg = 0";
741
742        if($addwhere != "") {
743            $where.= " AND $addwhere";
744        }
745
746        $objQuery->setoption("ORDER BY rank DESC");
747
748        if($products_check) {
749            $col = "T1.category_id, category_name, level";
750            $from = "dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id";
751            $where .= " AND product_count > 0";
752        } else {
753            $col = "category_id, category_name, level";
754            $from = "dtb_category";
755        }
756
757        $arrRet = $objQuery->select($col, $from, $where);
758
759        $max = count($arrRet);
760        for($cnt = 0; $cnt < $max; $cnt++) {
761            $id = $arrRet[$cnt]['category_id'];
762            $name = $arrRet[$cnt]['category_name'];
763            $arrList[$id] = str_repeat($head, $arrRet[$cnt]['level']) . $name;
764        }
765        return $arrList;
766    }
767
768    /**
769     * カテゴリーツリーの取得を行う.
770     *
771     * 親カテゴリの Value=0 を対象とする
772     *
773     * @param bool $parent_zero 親カテゴリの Value=0 の場合 true
774     * @return array カテゴリツリーの配列
775     */
776    function sfGetLevelCatList($parent_zero = true) {
777        $objQuery = new SC_Query();
778
779        // カテゴリ名リストを取得
780        $col = "category_id, parent_category_id, category_name";
781        $where = "del_flg = 0";
782        $objQuery->setoption("ORDER BY level");
783        $arrRet = $objQuery->select($col, "dtb_category", $where);
784        $arrCatName = array();
785        foreach ($arrRet as $arrTmp) {
786            $arrCatName[$arrTmp['category_id']] =
787                (($arrTmp['parent_category_id'] > 0)?
788                    $arrCatName[$arrTmp['parent_category_id']] : "")
789                . CATEGORY_HEAD . $arrTmp['category_name'];
790        }
791
792        $col = "category_id, parent_category_id, category_name, level";
793        $where = "del_flg = 0";
794        $objQuery->setoption("ORDER BY rank DESC");
795        $arrRet = $objQuery->select($col, "dtb_category", $where);
796        $max = count($arrRet);
797
798        for($cnt = 0; $cnt < $max; $cnt++) {
799            if($parent_zero) {
800                if($arrRet[$cnt]['level'] == LEVEL_MAX) {
801                    $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
802                } else {
803                    $arrValue[$cnt] = "";
804                }
805            } else {
806                $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
807            }
808
809            $arrOutput[$cnt] = $arrCatName[$arrRet[$cnt]['category_id']];
810        }
811
812        return array($arrValue, $arrOutput);
813    }
814
815    /**
816     * 選択中の商品のカテゴリを取得する.
817     *
818     * @param integer $product_id プロダクトID
819     * @param integer $category_id カテゴリID
820     * @return array 選択中の商品のカテゴリIDの配列
821     *
822     */
823    function sfGetCategoryId($product_id, $category_id = 0, $closed = false) {
824        if ($closed) {
825            $status = "";
826        } else {
827            $status = "status = 1";
828        }
829
830        if(!$this->g_category_on) {
831            $this->g_category_on = true;
832            $category_id = (int) $category_id;
833            $product_id = (int) $product_id;
834            if (SC_Utils_Ex::sfIsInt($category_id) && $category_id != 0 && $this->sfIsRecord("dtb_category","category_id", $category_id)) {
835                $this->g_category_id = array($category_id);
836            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
837                $objQuery = new SC_Query();
838                $where = "product_id = ?";
839                $category_id = $objQuery->getCol("dtb_product_categories", "category_id", "product_id = ?", array($product_id));
840                $this->g_category_id = $category_id;
841            } else {
842                // 不正な場合は、空の配列を返す。
843                $this->g_category_id = array();
844            }
845        }
846        return $this->g_category_id;
847    }
848
849    /**
850     * 商品をカテゴリの先頭に追加する.
851     *
852     * @param integer $category_id カテゴリID
853     * @param integer $product_id プロダクトID
854     * @return void
855     */
856    function addProductBeforCategories($category_id, $product_id) {
857
858        $sqlval = array("category_id" => $category_id,
859                        "product_id" => $product_id);
860
861        $objQuery = new SC_Query();
862
863        // 現在の商品カテゴリを取得
864        $arrCat = $objQuery->select("product_id, category_id, rank",
865                                    "dtb_product_categories",
866                                    "category_id = ?",
867                                    array($category_id));
868
869        $max = "0";
870        foreach ($arrCat as $val) {
871            // 同一商品が存在する場合は登録しない
872            if ($val["product_id"] == $product_id) {
873                return;
874            }
875            // 最上位ランクを取得
876            $max = ($max < $val["rank"]) ? $val["rank"] : $max;
877        }
878        $sqlval["rank"] = $max + 1;
879        $objQuery->insert("dtb_product_categories", $sqlval);
880    }
881
882    /**
883     * 商品をカテゴリの末尾に追加する.
884     *
885     * @param integer $category_id カテゴリID
886     * @param integer $product_id プロダクトID
887     * @return void
888     */
889    function addProductAfterCategories($category_id, $product_id) {
890        $sqlval = array("category_id" => $category_id,
891                        "product_id" => $product_id);
892
893        $objQuery = new SC_Query();
894
895        // 現在の商品カテゴリを取得
896        $arrCat = $objQuery->select("product_id, category_id, rank",
897                                    "dtb_product_categories",
898                                    "category_id = ?",
899                                    array($category_id));
900
901        $min = 0;
902        foreach ($arrCat as $val) {
903            // 同一商品が存在する場合は登録しない
904            if ($val["product_id"] == $product_id) {
905                return;
906            }
907            // 最下位ランクを取得
908            $min = ($min < $val["rank"]) ? $val["rank"] : $min;
909        }
910        $sqlval["rank"] = $min;
911        $objQuery->insert("dtb_product_categories", $sqlval);
912    }
913
914    /**
915     * 商品をカテゴリから削除する.
916     *
917     * @param integer $category_id カテゴリID
918     * @param integer $product_id プロダクトID
919     * @return void
920     */
921    function removeProductByCategories($category_id, $product_id) {
922        $sqlval = array("category_id" => $category_id,
923                        "product_id" => $product_id);
924        $objQuery = new SC_Query();
925        $objQuery->delete("dtb_product_categories",
926                          "category_id = ? AND product_id = ?", $sqlval);
927    }
928
929    /**
930     * 商品カテゴリを更新する.
931     *
932     * @param array $arrCategory_id 登録するカテゴリIDの配列
933     * @param integer $product_id プロダクトID
934     * @return void
935     */
936    function updateProductCategories($arrCategory_id, $product_id) {
937        $objQuery = new SC_Query();
938
939        // 現在のカテゴリ情報を取得
940        $arrCurrentCat = $objQuery->select("product_id, category_id, rank",
941                                           "dtb_product_categories",
942                                           "product_id = ?",
943                                           array($product_id));
944
945        // 登録するカテゴリ情報と比較
946        foreach ($arrCurrentCat as $val) {
947
948            // 登録しないカテゴリを削除
949            if (!in_array($val["category_id"], $arrCategory_id)) {
950                $this->removeProductByCategories($val["category_id"], $product_id);
951            }
952        }
953
954        // カテゴリを登録
955        foreach ($arrCategory_id as $category_id) {
956            $this->addProductBeforCategories($category_id, $product_id);
957        }
958    }
959
960    /**
961     * カテゴリ数の登録を行う.
962     *
963     * @param SC_Query $objQuery SC_Query インスタンス
964     * @return void
965     */
966    function sfCategory_Count($objQuery){
967
968        //テーブル内容の削除
969        $objQuery->query("DELETE FROM dtb_category_count");
970        $objQuery->query("DELETE FROM dtb_category_total_count");
971
972        $sql_where .= 'alldtl.del_flg = 0 AND alldtl.status = 1';
973        // 在庫無し商品の非表示
974        if (NOSTOCK_HIDDEN === true) {
975            $sql_where .= ' AND (alldtl.stock_max >= 1 OR alldtl.stock_unlimited_max = 1)';
976        }
977
978        //各カテゴリ内の商品数を数えて格納
979        $sql = <<< __EOS__
980            INSERT INTO dtb_category_count(category_id, product_count, create_date)
981            SELECT T1.category_id, count(T2.category_id), now()
982            FROM dtb_category AS T1
983                LEFT JOIN dtb_product_categories AS T2
984                    ON T1.category_id = T2.category_id
985                LEFT JOIN vw_products_allclass_detail AS alldtl
986                    ON T2.product_id = alldtl.product_id
987            WHERE $sql_where
988            GROUP BY T1.category_id, T2.category_id
989__EOS__;
990
991        $objQuery->query($sql);
992
993        //子カテゴリ内の商品数を集計する
994
995        // カテゴリ情報を取得
996        $arrCat = $objQuery->select('category_id', 'dtb_category');
997
998        foreach ($arrCat as $row) {
999            $category_id = $row['category_id'];
1000            $arrval = array();
1001
1002            $arrval[] = $category_id;
1003
1004            list($tmp_where, $tmp_arrval) = $this->sfGetCatWhere($category_id);
1005            if ($tmp_where != "") {
1006                $sql_where_product_ids = "alldtl.product_id IN (SELECT product_id FROM dtb_product_categories WHERE " . $tmp_where . ")";
1007                $arrval = array_merge((array)$arrval, (array)$tmp_arrval);
1008            } else {
1009                $sql_where_product_ids = '0<>0'; // 一致させない
1010            }
1011
1012            $sql = <<< __EOS__
1013                INSERT INTO dtb_category_total_count (category_id, product_count, create_date)
1014                SELECT
1015                    ?
1016                    ,count(*)
1017                    ,now()
1018                FROM vw_products_allclass_detail AS alldtl
1019                WHERE ($sql_where) AND ($sql_where_product_ids)
1020__EOS__;
1021
1022            $objQuery->query($sql, $arrval);
1023        }
1024    }
1025
1026    /**
1027     * 子IDの配列を返す.
1028     *
1029     * @param string $table テーブル名
1030     * @param string $pid_name 親ID名
1031     * @param string $id_name ID名
1032     * @param integer $id ID
1033     * @param array 子ID の配列
1034     */
1035    function sfGetChildsID($table, $pid_name, $id_name, $id) {
1036        $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id);
1037        return $arrRet;
1038    }
1039
1040    /**
1041     * 階層構造のテーブルから子ID配列を取得する.
1042     *
1043     * @param string $table テーブル名
1044     * @param string $pid_name 親ID名
1045     * @param string $id_name ID名
1046     * @param integer $id ID番号
1047     * @return array 子IDの配列
1048     */
1049    function sfGetChildrenArray($table, $pid_name, $id_name, $id) {
1050        $objQuery = new SC_Query();
1051        $col = $pid_name . "," . $id_name;
1052         $arrData = $objQuery->select($col, $table);
1053
1054        $arrPID = array();
1055        $arrPID[] = $id;
1056        $arrChildren = array();
1057        $arrChildren[] = $id;
1058
1059        $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID);
1060
1061        while(count($arrRet) > 0) {
1062            $arrChildren = array_merge($arrChildren, $arrRet);
1063            $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrRet);
1064        }
1065
1066        return $arrChildren;
1067    }
1068
1069    /**
1070     * 親ID直下の子IDをすべて取得する.
1071     *
1072     * @param array $arrData 親カテゴリの配列
1073     * @param string $pid_name 親ID名
1074     * @param string $id_name ID名
1075     * @param array $arrPID 親IDの配列
1076     * @return array 子IDの配列
1077     */
1078    function sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID) {
1079        $arrChildren = array();
1080        $max = count($arrData);
1081
1082        for($i = 0; $i < $max; $i++) {
1083            foreach($arrPID as $val) {
1084                if($arrData[$i][$pid_name] == $val) {
1085                    $arrChildren[] = $arrData[$i][$id_name];
1086                }
1087            }
1088        }
1089        return $arrChildren;
1090    }
1091
1092    /**
1093     * 所属するすべての階層の親IDを配列で返す.
1094     *
1095     * @param SC_Query $objQuery SC_Query インスタンス
1096     * @param string $table テーブル名
1097     * @param string $pid_name 親ID名
1098     * @param string $id_name ID名
1099     * @param integer $id ID
1100     * @return array 親IDの配列
1101     */
1102    function sfGetParents($objQuery, $table, $pid_name, $id_name, $id) {
1103        $arrRet = $this->sfGetParentsArray($table, $pid_name, $id_name, $id);
1104        // 配列の先頭1つを削除する。
1105        array_shift($arrRet);
1106        return $arrRet;
1107    }
1108
1109    /**
1110     * 階層構造のテーブルから親ID配列を取得する.
1111     *
1112     * @param string $table テーブル名
1113     * @param string $pid_name 親ID名
1114     * @param string $id_name ID名
1115     * @param integer $id ID
1116     * @return array 親IDの配列
1117     */
1118    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
1119        $objQuery = new SC_Query();
1120        $col = $pid_name . "," . $id_name;
1121        $arrData = $objQuery->select($col, $table);
1122
1123        $arrParents = array();
1124        $arrParents[] = $id;
1125        $child = $id;
1126
1127        $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
1128
1129        while($ret != "") {
1130            $arrParents[] = $ret;
1131            $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
1132        }
1133
1134        $arrParents = array_reverse($arrParents);
1135
1136        return $arrParents;
1137    }
1138
1139    /**
1140     * カテゴリから商品を検索する場合のWHERE文と値を返す.
1141     *
1142     * @param integer $category_id カテゴリID
1143     * @return array 商品を検索する場合の配列
1144     */
1145    function sfGetCatWhere($category_id) {
1146        // 子カテゴリIDの取得
1147        $arrRet = $this->sfGetChildsID("dtb_category", "parent_category_id", "category_id", $category_id);
1148        $tmp_where = "";
1149        foreach ($arrRet as $val) {
1150            if($tmp_where == "") {
1151                $tmp_where.= "category_id IN ( ?";
1152            } else {
1153                $tmp_where.= ",? ";
1154            }
1155            $arrval[] = $val;
1156        }
1157        $tmp_where.= " ) ";
1158        return array($tmp_where, $arrval);
1159    }
1160
1161    /**
1162     * 受注一時テーブルから情報を取得する.
1163     *
1164     * @param integer $order_temp_id 受注一時ID
1165     * @return array 受注一時情報の配列
1166     */
1167    function sfGetOrderTemp($order_temp_id) {
1168        $objQuery = new SC_Query();
1169        $where = "order_temp_id = ?";
1170        $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));
1171        return $arrRet[0];
1172    }
1173
1174    /**
1175     * SELECTボックス用リストを作成する.
1176     *
1177     * @param string $table テーブル名
1178     * @param string $keyname プライマリーキーのカラム名
1179     * @param string $valname データ内容のカラム名
1180     * @return array SELECT ボックス用リストの配列
1181     */
1182    function sfGetIDValueList($table, $keyname, $valname) {
1183        $objQuery = new SC_Query();
1184        $col = "$keyname, $valname";
1185        $objQuery->setwhere("del_flg = 0");
1186        $objQuery->setorder("rank DESC");
1187        $arrList = $objQuery->select($col, $table);
1188        $count = count($arrList);
1189        for($cnt = 0; $cnt < $count; $cnt++) {
1190            $key = $arrList[$cnt][$keyname];
1191            $val = $arrList[$cnt][$valname];
1192            $arrRet[$key] = $val;
1193        }
1194        return $arrRet;
1195    }
1196
1197    /**
1198     * ランキングを上げる.
1199     *
1200     * @param string $table テーブル名
1201     * @param string $colname カラム名
1202     * @param string|integer $id テーブルのキー
1203     * @param string $andwhere SQL の AND 条件である WHERE 句
1204     * @return void
1205     */
1206    function sfRankUp($table, $colname, $id, $andwhere = "") {
1207        $objQuery = new SC_Query();
1208        $objQuery->begin();
1209        $where = "$colname = ?";
1210        if($andwhere != "") {
1211            $where.= " AND $andwhere";
1212        }
1213        // 対象項目のランクを取得
1214        $rank = $objQuery->get($table, "rank", $where, array($id));
1215        // ランクの最大値を取得
1216        $maxrank = $objQuery->max($table, "rank", $andwhere);
1217        // ランクが最大値よりも小さい場合に実行する。
1218        if($rank < $maxrank) {
1219            // ランクが一つ上のIDを取得する。
1220            $where = "rank = ?";
1221            if($andwhere != "") {
1222                $where.= " AND $andwhere";
1223            }
1224            $uprank = $rank + 1;
1225            $up_id = $objQuery->get($table, $colname, $where, array($uprank));
1226            // ランク入れ替えの実行
1227            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1228            if($andwhere != "") {
1229                $sqlup.= " AND $andwhere";
1230            }
1231            $objQuery->exec($sqlup, array($rank + 1, $id));
1232            $objQuery->exec($sqlup, array($rank, $up_id));
1233        }
1234        $objQuery->commit();
1235    }
1236
1237    /**
1238     * ランキングを下げる.
1239     *
1240     * @param string $table テーブル名
1241     * @param string $colname カラム名
1242     * @param string|integer $id テーブルのキー
1243     * @param string $andwhere SQL の AND 条件である WHERE 句
1244     * @return void
1245     */
1246    function sfRankDown($table, $colname, $id, $andwhere = "") {
1247        $objQuery = new SC_Query();
1248        $objQuery->begin();
1249        $where = "$colname = ?";
1250        if($andwhere != "") {
1251            $where.= " AND $andwhere";
1252        }
1253        // 対象項目のランクを取得
1254        $rank = $objQuery->get($table, "rank", $where, array($id));
1255
1256        // ランクが1(最小値)よりも大きい場合に実行する。
1257        if($rank > 1) {
1258            // ランクが一つ下のIDを取得する。
1259            $where = "rank = ?";
1260            if($andwhere != "") {
1261                $where.= " AND $andwhere";
1262            }
1263            $downrank = $rank - 1;
1264            $down_id = $objQuery->get($table, $colname, $where, array($downrank));
1265            // ランク入れ替えの実行
1266            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1267            if($andwhere != "") {
1268                $sqlup.= " AND $andwhere";
1269            }
1270            $objQuery->exec($sqlup, array($rank - 1, $id));
1271            $objQuery->exec($sqlup, array($rank, $down_id));
1272        }
1273        $objQuery->commit();
1274    }
1275
1276    /**
1277     * 指定順位へ移動する.
1278     *
1279     * @param string $tableName テーブル名
1280     * @param string $keyIdColumn キーを保持するカラム名
1281     * @param string|integer $keyId キーの値
1282     * @param integer $pos 指定順位
1283     * @param string $where SQL の AND 条件である WHERE 句
1284     * @return void
1285     */
1286    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
1287        $objQuery = new SC_Query();
1288        $objQuery->begin();
1289
1290        // 自身のランクを取得する
1291        if($where != "") {
1292            $getWhere = "$keyIdColumn = ? AND " . $where;
1293        } else {
1294            $getWhere = "$keyIdColumn = ?";
1295        }
1296        $rank = $objQuery->get($tableName, "rank", $getWhere, array($keyId));
1297
1298        $max = $objQuery->max($tableName, "rank", $where);
1299
1300        // 値の調整(逆順)
1301        if($pos > $max) {
1302            $position = 1;
1303        } else if($pos < 1) {
1304            $position = $max;
1305        } else {
1306            $position = $max - $pos + 1;
1307        }
1308
1309        //入れ替え先の順位が入れ換え元の順位より大きい場合
1310        if( $position > $rank ) $term = "rank - 1";
1311
1312        //入れ替え先の順位が入れ換え元の順位より小さい場合
1313        if( $position < $rank ) $term = "rank + 1";
1314
1315        // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合
1316        if (!isset($term)) $term = "rank";
1317
1318        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1319        $sql = "UPDATE $tableName SET rank = $term WHERE rank BETWEEN ? AND ?";
1320        if($where != "") {
1321            $sql.= " AND $where";
1322        }
1323
1324        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
1325        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
1326
1327        // 指定した順位へrankを書き換える。
1328        $sql  = "UPDATE $tableName SET rank = ? WHERE $keyIdColumn = ? ";
1329        if($where != "") {
1330            $sql.= " AND $where";
1331        }
1332
1333        $objQuery->exec( $sql, array( $position, $keyId ) );
1334        $objQuery->commit();
1335    }
1336
1337    /**
1338     * ランクを含むレコードを削除する.
1339     *
1340     * レコードごと削除する場合は、$deleteをtrueにする
1341     *
1342     * @param string $table テーブル名
1343     * @param string $colname カラム名
1344     * @param string|integer $id テーブルのキー
1345     * @param string $andwhere SQL の AND 条件である WHERE 句
1346     * @param bool $delete レコードごと削除する場合 true,
1347     *                     レコードごと削除しない場合 false
1348     * @return void
1349     */
1350    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
1351                                $delete = false) {
1352        $objQuery = new SC_Query();
1353        $objQuery->begin();
1354        // 削除レコードのランクを取得する。
1355        $where = "$colname = ?";
1356        if($andwhere != "") {
1357            $where.= " AND $andwhere";
1358        }
1359        $rank = $objQuery->get($table, "rank", $where, array($id));
1360
1361        if(!$delete) {
1362            // ランクを最下位にする、DELフラグON
1363            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1 ";
1364            $sqlup.= "WHERE $colname = ?";
1365            // UPDATEの実行
1366            $objQuery->exec($sqlup, array($id));
1367        } else {
1368            $objQuery->delete($table, "$colname = ?", array($id));
1369        }
1370
1371        // 追加レコードのランクより上のレコードを一つずらす。
1372        $where = "rank > ?";
1373        if($andwhere != "") {
1374            $where.= " AND $andwhere";
1375        }
1376        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1377        $objQuery->exec($sqlup, array($rank));
1378        $objQuery->commit();
1379    }
1380
1381    /**
1382     * 親IDの配列を元に特定のカラムを取得する.
1383     *
1384     * @param SC_Query $objQuery SC_Query インスタンス
1385     * @param string $table テーブル名
1386     * @param string $id_name ID名
1387     * @param string $col_name カラム名
1388     * @param array $arrId IDの配列
1389     * @return array 特定のカラムの配列
1390     */
1391    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) {
1392        $col = $col_name;
1393        $len = count($arrId);
1394        $where = "";
1395
1396        for($cnt = 0; $cnt < $len; $cnt++) {
1397            if($where == "") {
1398                $where = "$id_name = ?";
1399            } else {
1400                $where.= " OR $id_name = ?";
1401            }
1402        }
1403
1404        $objQuery->setorder("level");
1405        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1406        return $arrRet;
1407    }
1408
1409    /**
1410     * カテゴリ変更時の移動処理を行う.
1411     *
1412     * @param SC_Query $objQuery SC_Query インスタンス
1413     * @param string $table テーブル名
1414     * @param string $id_name ID名
1415     * @param string $cat_name カテゴリ名
1416     * @param integer $old_catid 旧カテゴリID
1417     * @param integer $new_catid 新カテゴリID
1418     * @param integer $id ID
1419     * @return void
1420     */
1421    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1422        if ($old_catid == $new_catid) {
1423            return;
1424        }
1425        // 旧カテゴリでのランク削除処理
1426        // 移動レコードのランクを取得する。
1427        $where = "$id_name = ?";
1428        $rank = $objQuery->get($table, "rank", $where, array($id));
1429        // 削除レコードのランクより上のレコードを一つ下にずらす。
1430        $where = "rank > ? AND $cat_name = ?";
1431        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1432        $objQuery->exec($sqlup, array($rank, $old_catid));
1433        // 新カテゴリでの登録処理
1434        // 新カテゴリの最大ランクを取得する。
1435        $max_rank = $objQuery->max($table, "rank", "$cat_name = ?", array($new_catid)) + 1;
1436        $where = "$id_name = ?";
1437        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1438        $objQuery->exec($sqlup, array($max_rank, $id));
1439    }
1440
1441    /**
1442     * お届け時間を取得する.
1443     *
1444     * @param integer $payment_id 支払い方法ID
1445     * @return array お届け時間の配列
1446     */
1447    function sfGetDelivTime($payment_id = "") {
1448        $objQuery = new SC_Query();
1449
1450        $deliv_id = "";
1451        $arrRet = array();
1452
1453        if($payment_id != "") {
1454            $where = "del_flg = 0 AND payment_id = ?";
1455            $arrRet = $objQuery->select("deliv_id", "dtb_payment", $where, array($payment_id));
1456            $deliv_id = $arrRet[0]['deliv_id'];
1457        }
1458
1459        if($deliv_id != "") {
1460            $objQuery->setorder("time_id");
1461            $where = "deliv_id = ?";
1462            $arrRet= $objQuery->select("time_id, deliv_time", "dtb_delivtime", $where, array($deliv_id));
1463        }
1464
1465        return $arrRet;
1466    }
1467
1468    /**
1469     * 都道府県、支払い方法から配送料金を取得する.
1470     *
1471     * @param array $arrData 各種情報
1472     * @return string 指定の都道府県, 支払い方法の配送料金
1473     */
1474    function sfGetDelivFee($arrData) {
1475        $pref = $arrData['deliv_pref'];
1476        $payment_id = isset($arrData['payment_id']) ? $arrData['payment_id'] : "";
1477
1478        $objQuery = new SC_Query();
1479
1480        $deliv_id = "";
1481
1482        // 支払い方法が指定されている場合は、対応した配送業者を取得する
1483        if($payment_id != "") {
1484            $where = "del_flg = 0 AND payment_id = ?";
1485            $arrRet = $objQuery->select("deliv_id", "dtb_payment", $where, array($payment_id));
1486            $deliv_id = $arrRet[0]['deliv_id'];
1487        // 支払い方法が指定されていない場合は、先頭の配送業者を取得する
1488        } else {
1489            $where = "del_flg = 0";
1490            $objQuery->setOrder("rank DESC");
1491            $objQuery->setLimitOffset(1);
1492            $arrRet = $objQuery->select("deliv_id", "dtb_deliv", $where);
1493            $deliv_id = $arrRet[0]['deliv_id'];
1494        }
1495
1496        // 配送業者から配送料を取得
1497        if($deliv_id != "") {
1498
1499            // 都道府県が指定されていない場合は、東京都の番号を指定しておく
1500            if($pref == "") {
1501                $pref = 13;
1502            }
1503
1504            $objQuery = new SC_Query();
1505            $where = "deliv_id = ? AND pref = ?";
1506            $arrRet= $objQuery->select("fee", "dtb_delivfee", $where, array($deliv_id, $pref));
1507        }
1508        return $arrRet[0]['fee'];
1509    }
1510
1511    /**
1512     * 集計情報を元に最終計算を行う.
1513     *
1514     * @param array $arrData 各種情報
1515     * @param LC_Page $objPage LC_Page インスタンス
1516     * @param SC_CartSession $objCartSess SC_CartSession インスタンス
1517     * @param null $dummy1 互換性確保用(決済モジュール互換のため)
1518     * @param SC_Customer $objCustomer SC_Customer インスタンス
1519     * @return array 最終計算後の配列
1520     */
1521    function sfTotalConfirm($arrData, &$objPage, &$objCartSess, $dummy1 = null, $objCustomer = "") {
1522        // 店舗基本情報を取得する
1523        $arrInfo = SC_Helper_DB_Ex::sf_getBasisData();
1524
1525        // 未定義変数を定義
1526        if (!isset($arrData['deliv_pref'])) $arrData['deliv_pref'] = "";
1527        if (!isset($arrData['payment_id'])) $arrData['payment_id'] = "";
1528        if (!isset($arrData['charge'])) $arrData['charge'] = "";
1529        if (!isset($arrData['use_point'])) $arrData['use_point'] = "";
1530        if (!isset($arrData['add_point'])) $arrData['add_point'] = 0;
1531
1532        // 税金の取得
1533        $arrData['tax'] = $objPage->tpl_total_tax;
1534        // 小計の取得
1535        $arrData['subtotal'] = $objPage->tpl_total_pretax;
1536
1537        // 合計送料の取得
1538        $arrData['deliv_fee'] = 0;
1539
1540        // 商品ごとの送料が有効の場合
1541        if (OPTION_PRODUCT_DELIV_FEE == 1) {
1542            // 全商品の合計送料を加算する
1543            $this->lfAddAllProductsDelivFee($arrData, $objPage, $objCartSess);
1544        }
1545
1546        // 配送業者の送料が有効の場合
1547        if (OPTION_DELIV_FEE == 1) {
1548            // 都道府県、支払い方法から配送料金を加算する
1549            $this->lfAddDelivFee($arrData);
1550        }
1551
1552        // 送料無料の購入数が設定されている場合
1553        if (DELIV_FREE_AMOUNT > 0) {
1554            // 商品の合計数量
1555            $total_quantity = $objCartSess->getTotalQuantity(true);
1556
1557            if($total_quantity >= DELIV_FREE_AMOUNT) {
1558                $arrData['deliv_fee'] = 0;
1559            }
1560        }
1561
1562        // 送料無料条件が設定されている場合
1563        if($arrInfo['free_rule'] > 0) {
1564            // 小計が無料条件を超えている場合
1565            if($arrData['subtotal'] >= $arrInfo['free_rule']) {
1566                $arrData['deliv_fee'] = 0;
1567            }
1568        }
1569
1570        // 合計の計算
1571        $arrData['total'] = $objPage->tpl_total_pretax; // 商品合計
1572        $arrData['total']+= $arrData['deliv_fee'];      // 送料
1573        $arrData['total']+= $arrData['charge'];         // 手数料
1574        // お支払い合計
1575        $arrData['payment_total'] = $arrData['total'] - ($arrData['use_point'] * POINT_VALUE);
1576        // 加算ポイントの計算
1577        if (USE_POINT !== false) {
1578            $arrData['add_point'] = SC_Helper_DB_Ex::sfGetAddPoint($objPage->tpl_total_point, $arrData['use_point']);
1579
1580            if($objCustomer != "") {
1581                // 誕生日月であった場合
1582                if($objCustomer->isBirthMonth()) {
1583                    $arrData['birth_point'] = BIRTH_MONTH_POINT;
1584                    $arrData['add_point'] += $arrData['birth_point'];
1585                }
1586            }
1587        }
1588
1589        if($arrData['add_point'] < 0) {
1590            $arrData['add_point'] = 0;
1591        }
1592        return $arrData;
1593    }
1594
1595    /**
1596     * レコードの存在チェックを行う.
1597     *
1598     * @param string $table テーブル名
1599     * @param string $col カラム名
1600     * @param array $arrval 要素の配列
1601     * @param array $addwhere SQL の AND 条件である WHERE 句
1602     * @return bool レコードが存在する場合 true
1603     */
1604    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1605        $objQuery = new SC_Query();
1606        $arrCol = split("[, ]", $col);
1607
1608        $where = "del_flg = 0";
1609
1610        if($addwhere != "") {
1611            $where.= " AND $addwhere";
1612        }
1613
1614        foreach($arrCol as $val) {
1615            if($val != "") {
1616                if($where == "") {
1617                    $where = "$val = ?";
1618                } else {
1619                    $where.= " AND $val = ?";
1620                }
1621            }
1622        }
1623        $ret = $objQuery->get($table, $col, $where, $arrval);
1624
1625        if($ret != "") {
1626            return true;
1627        }
1628        return false;
1629    }
1630
1631    /**
1632     * メーカー商品数数の登録を行う.
1633     *
1634     * @param SC_Query $objQuery SC_Query インスタンス
1635     * @return void
1636     */
1637    function sfMaker_Count($objQuery){
1638        $sql = "";
1639
1640        //テーブル内容の削除
1641        $objQuery->query("DELETE FROM dtb_maker_count");
1642
1643        //各メーカーの商品数を数えて格納
1644        $sql = " INSERT INTO dtb_maker_count(maker_id, product_count, create_date) ";
1645        $sql .= " SELECT T1.maker_id, count(T2.maker_id), now() ";
1646        $sql .= " FROM dtb_maker AS T1 LEFT JOIN dtb_products AS T2";
1647        $sql .= " ON T1.maker_id = T2.maker_id ";
1648        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
1649        $sql .= " GROUP BY T1.maker_id, T2.maker_id ";
1650        $objQuery->query($sql);
1651    }
1652
1653    /**
1654     * 選択中の商品のメーカーを取得する.
1655     *
1656     * @param integer $product_id プロダクトID
1657     * @param integer $maker_id メーカーID
1658     * @return array 選択中の商品のメーカーIDの配列
1659     *
1660     */
1661    function sfGetMakerId($product_id, $maker_id = 0, $closed = false) {
1662        if ($closed) {
1663            $status = "";
1664        } else {
1665            $status = "status = 1";
1666        }
1667
1668        if (!$this->g_maker_on) {
1669            $this->g_maker_on = true;
1670            $maker_id = (int) $maker_id;
1671            $product_id = (int) $product_id;
1672            if (SC_Utils_Ex::sfIsInt($maker_id) && $maker_id != 0 && $this->sfIsRecord("dtb_maker","maker_id", $maker_id)) {
1673                $this->g_maker_id = array($maker_id);
1674            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
1675                $objQuery = new SC_Query();
1676                $where = "product_id = ?";
1677                $maker_id = $objQuery->getCol("dtb_products", "maker_id", "product_id = ?", array($product_id));
1678                $this->g_maker_id = $maker_id;
1679            } else {
1680                // 不正な場合は、空の配列を返す。
1681                $this->g_maker_id = array();
1682            }
1683        }
1684        return $this->g_maker_id;
1685    }
1686
1687    /**
1688     * メーカーの取得を行う.
1689     *
1690     * $products_check:true商品登録済みのものだけ取得する
1691     *
1692     * @param string $addwhere 追加する WHERE 句
1693     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
1694     * @return array カテゴリツリーの配列
1695     */
1696    function sfGetMakerList($addwhere = "", $products_check = false) {
1697        $objQuery = new SC_Query();
1698        $where = "del_flg = 0";
1699
1700        if($addwhere != "") {
1701            $where.= " AND $addwhere";
1702        }
1703
1704        $objQuery->setoption("ORDER BY rank DESC");
1705
1706        if($products_check) {
1707            $col = "T1.maker_id, name";
1708            $from = "dtb_maker AS T1 LEFT JOIN dtb_maker_count AS T2 ON T1.maker_id = T2.maker_id";
1709            $where .= " AND product_count > 0";
1710        } else {
1711            $col = "maker_id, name";
1712            $from = "dtb_maker";
1713        }
1714
1715        $arrRet = $objQuery->select($col, $from, $where);
1716
1717        $max = count($arrRet);
1718        for($cnt = 0; $cnt < $max; $cnt++) {
1719            $id = $arrRet[$cnt]['maker_id'];
1720            $name = $arrRet[$cnt]['name'];
1721            $arrList[$id].= $name;
1722        }
1723        return $arrList;
1724    }
1725
1726    /**
1727     * 全商品の合計送料を加算する
1728     */
1729    function lfAddAllProductsDelivFee(&$arrData, &$objPage, &$objCartSess) {
1730        $arrData['deliv_fee'] += $this->lfCalcAllProductsDelivFee($arrData, $objCartSess);
1731    }
1732
1733    /**
1734     * 全商品の合計送料を計算する
1735     */
1736    function lfCalcAllProductsDelivFee(&$arrData, &$objCartSess) {
1737        $objQuery = new SC_Query();
1738        $deliv_fee_total = 0;
1739        $max = $objCartSess->getMax();
1740        for ($i = 0; $i <= $max; $i++) {
1741            // 商品送料
1742            $deliv_fee = $objQuery->getOne('SELECT deliv_fee FROM dtb_products WHERE product_id = ?', array($_SESSION[$objCartSess->key][$i]['id'][0]));
1743            // 数量
1744            $quantity = $_SESSION[$objCartSess->key][$i]['quantity'];
1745            // 累積
1746            $deliv_fee_total += $deliv_fee * $quantity;
1747        }
1748        return $deliv_fee_total;
1749    }
1750
1751    /**
1752     * 都道府県、支払い方法から配送料金を加算する.
1753     *
1754     * @param array $arrData 各種情報
1755     */
1756    function lfAddDelivFee(&$arrData) {
1757        $arrData['deliv_fee'] += $this->sfGetDelivFee($arrData);
1758    }
1759
1760    /**
1761     * 受注の名称列を更新する
1762     *
1763     * @param integer $order_id 更新対象の注文番号
1764     * @param boolean $temp_table 更新対象は「受注_Temp」か
1765     * @static
1766     */
1767    function sfUpdateOrderNameCol($order_id, $temp_table = false) {
1768        $objQuery = new SC_Query();
1769
1770        if ($temp_table) {
1771            $tgt_table = 'dtb_order_temp';
1772            $sql_where = 'WHERE order_temp_id = ?';
1773        } else {
1774            $tgt_table = 'dtb_order';
1775            $sql_where = 'WHERE order_id = ?';
1776        }
1777
1778        $sql = <<< __EOS__
1779            UPDATE
1780                {$tgt_table}
1781            SET
1782                 payment_method = (SELECT payment_method FROM dtb_payment WHERE payment_id = {$tgt_table}.payment_id)
1783                ,deliv_time = (SELECT deliv_time FROM dtb_delivtime WHERE time_id = {$tgt_table}.deliv_time_id AND deliv_id = {$tgt_table}.deliv_id)
1784            $sql_where
1785__EOS__;
1786
1787        $objQuery->query($sql, array($order_id));
1788    }
1789
1790    /**
1791     * 店舗基本情報に基づいて税金額を返す
1792     *
1793     * @param integer $price 計算対象の金額
1794     * @return integer 税金額
1795     */
1796    function sfTax($price) {
1797        // 店舗基本情報を取得
1798        $CONF = SC_Helper_DB_Ex::sf_getBasisData();
1799
1800        return SC_Utils_Ex::sfTax($price, $CONF['tax'], $CONF['tax_rule']);
1801    }
1802
1803    /**
1804     * 店舗基本情報に基づいて税金付与した金額を返す
1805     *
1806     * @param integer $price 計算対象の金額
1807     * @return integer 税金付与した金額
1808     */
1809    function sfPreTax($price, $tax = null, $tax_rule = null) {
1810        // 店舗基本情報を取得
1811        $CONF = SC_Helper_DB_Ex::sf_getBasisData();
1812
1813        return SC_Utils_Ex::sfPreTax($price, $CONF['tax'], $CONF['tax_rule']);
1814    }
1815
1816    /**
1817     * 店舗基本情報に基づいて加算ポイントを返す
1818     *
1819     * @param integer $totalpoint
1820     * @param integer $use_point
1821     * @return integer 加算ポイント
1822     */
1823    function sfGetAddPoint($totalpoint, $use_point) {
1824        // 店舗基本情報を取得
1825        $CONF = SC_Helper_DB_Ex::sf_getBasisData();
1826
1827        return SC_Utils_Ex::sfGetAddPoint($totalpoint, $use_point, $CONF['point_rate']);
1828    }
1829
1830    /**
1831     * 受注.対応状況の更新
1832     *
1833     * ・必ず呼び出し元でトランザクションブロックを開いておくこと。
1834     *
1835     * @param integer $orderId 注文番号
1836     * @param integer|null $newStatus 対応状況 (null=変更無し)
1837     * @param integer|null $newAddPoint 加算ポイント (null=変更無し)
1838     * @param integer|null $newUsePoint 使用ポイント (null=変更無し)
1839     * @return void
1840     */
1841    function sfUpdateOrderStatus($orderId, $newStatus = null, $newAddPoint = null, $newUsePoint = null) {
1842        $objQuery = new SC_Query();
1843
1844        $arrOrderOld = $objQuery->getRow('dtb_order', 'status, add_point, use_point, customer_id', 'order_id = ?', array($orderId));
1845
1846        // 対応状況が変更無しの場合、DB値を引き継ぐ
1847        if (is_null($newStatus)) {
1848            $newStatus = $arrOrderOld['status'];
1849        }
1850
1851        // 使用ポイント、DB値を引き継ぐ
1852        if (is_null($newUsePoint)) {
1853            $newUsePoint = $arrOrderOld['use_point'];
1854        }
1855
1856        // 加算ポイント、DB値を引き継ぐ
1857        if (is_null($newAddPoint)) {
1858            $newAddPoint = $arrOrderOld['add_point'];
1859        }
1860
1861        if (USE_POINT !== false) {
1862            // 顧客.ポイントの加減値
1863            $addCustomerPoint = 0;
1864
1865            // ▼使用ポイント
1866            // 変更前の対応状況が利用対象の場合、変更前の使用ポイント分を戻す
1867            if (SC_Utils_Ex::sfIsUsePoint($arrOrderOld['status'])) {
1868                $addCustomerPoint += $arrOrderOld['use_point'];
1869            }
1870
1871            // 変更後の対応状況が利用対象の場合、変更後の使用ポイント分を引く
1872            if (SC_Utils_Ex::sfIsUsePoint($newStatus)) {
1873                $addCustomerPoint -= $newUsePoint;
1874            }
1875            // ▲使用ポイント
1876
1877            // ▼加算ポイント
1878            // 変更前の対応状況が加算対象の場合、変更前の加算ポイント分を戻す
1879            if (SC_Utils_Ex::sfIsAddPoint($arrOrderOld['status'])) {
1880                $addCustomerPoint -= $arrOrderOld['add_point'];
1881            }
1882
1883            // 変更後の対応状況が加算対象の場合、変更後の加算ポイント分を足す
1884            if (SC_Utils_Ex::sfIsAddPoint($newStatus)) {
1885                $addCustomerPoint += $newAddPoint;
1886            }
1887            // ▲加算ポイント
1888
1889            if ($addCustomerPoint != 0) {
1890                // ▼顧客テーブルの更新
1891                $sqlval = array();
1892                $where = '';
1893                $arrVal = array();
1894                $arrRawSql = array();
1895                $arrRawSqlVal = array();
1896
1897                $sqlval['update_date'] = 'Now()';
1898                $arrRawSql['point'] = 'point + ?';
1899                $arrRawSqlVal[] = $addCustomerPoint;
1900                $where .= 'customer_id = ?';
1901                $arrVal[] = $arrOrderOld['customer_id'];
1902
1903                $objQuery->update('dtb_customer', $sqlval, $where, $arrVal, $arrRawSql, $arrRawSqlVal);
1904                // ▲顧客テーブルの更新
1905
1906                // 顧客.ポイントをマイナスした場合、
1907                if ($addCustomerPoint < 0) {
1908                    $sql = 'SELECT point FROM dtb_customer WHERE customer_id = ?';
1909                    $point = $objQuery->getone($sql, array($arrOrderOld['customer_id']));
1910                    // 変更後の顧客.ポイントがマイナスの場合、
1911                    if ($point < 0) {
1912                        // ロールバック
1913                        $objQuery->rollback();
1914                        // エラー
1915                        SC_Utils_Ex::sfDispSiteError(LACK_POINT);
1916                    }
1917                }
1918            }
1919        }
1920
1921        // ▼受注テーブルの更新
1922        $sqlval = array();
1923        if (USE_POINT !== false) {
1924            $sqlval['add_point'] = $newAddPoint;
1925            $sqlval['use_point'] = $newUsePoint;
1926        }
1927        // ステータスが発送済みに変更の場合、発送日を更新
1928        if ($arrOrderOld['status'] != ORDER_DELIV && $newStatus == ORDER_DELIV) {
1929            $sqlval['commit_date'] = 'Now()';
1930        }
1931        $sqlval['status'] = $newStatus;
1932        $sqlval['update_date'] = 'Now()';
1933
1934        $objQuery->update('dtb_order', $sqlval, 'order_id = ?', array($orderId));
1935        // ▲受注テーブルの更新
1936    }
1937
1938    /**
1939     * 指定ファイルが存在する場合 SQL として実行
1940     *
1941     * ・MySQL の場合、文字「;」を区切りとして、分割実行。
1942     * XXX プラグイン用に追加。将来消すかも。
1943     *
1944     * @param string $sqlFilePath SQL ファイルのパス
1945     * @return void
1946     */
1947    function sfExecSqlByFile($sqlFilePath) {
1948        if (file_exists($sqlFilePath)) {
1949            $objQuery = new SC_Query();
1950
1951            $sqls = file_get_contents($sqlFilePath);
1952            if ($sqls === false) SC_Utils_Ex::sfDispException('ファイルは存在するが読み込めない');
1953
1954            if (DB_TYPE == 'mysql') {
1955                foreach (explode(';', $sqls) as $sql) {
1956                    $sql = trim($sql);
1957                    if (strlen($sql) == 0) continue;
1958                    $objQuery->query($sql);
1959                }
1960            } else {
1961                $objQuery->query($sqls);
1962            }
1963        }
1964    }
1965
1966    /**
1967     * 商品規格を設定しているか
1968     *
1969     * @param integer $product_id 商品ID
1970     * @return bool 商品規格が存在する場合:true, それ以外:false
1971     */
1972    function sfHasProductClass($product_id) {
1973        if (!SC_Utils_Ex::sfIsInt($product_id)) return false;
1974
1975        $objQuery  = new SC_Query();
1976        $where = 'product_id = ? AND (classcategory_id1 <> 0 OR classcategory_id2 <> 0)';
1977        $count = $objQuery->count('dtb_products_class', $where, array($product_id));
1978
1979        return $count >= 1;
1980    }
1981}
1982?>
Note: See TracBrowser for help on using the repository browser.