source: branches/version-2_5-dev/data/class/helper/SC_Helper_DB.php @ 19732

Revision 19732, 79.5 KB checked in by Seasoft, 13 years ago (diff)

#855(SC_Query の #select, #getRow, #getCol, #get, #min, #max の引数順を統一する)

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