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

Revision 19864, 76.5 KB checked in by fukuda, 13 years ago (diff)

SC_Helper_DB内の顧客編集に関する部分は、SC_Helper_Customerを新規作成して
そちらで共通化するように変更

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