source: branches/version-2_5-dev/data/class/helper/SC_Helper_Purchase.php @ 18871

Revision 18871, 9.9 KB checked in by nanasess, 13 years ago (diff)

決済フローの改善(#844)

  • 決済処理中(ORDER_PENDING)の受注ステータスを追加
  • 決済モジュールをロードする前に, 受注情報を dtb_order へ登録するよう修正
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 * 商品購入関連のヘルパークラス.
26 *
27 * TODO 購入時強制会員登録機能(#521)の実装を検討
28 * TODO dtb_customer.buy_times, dtb_customer.buy_total の更新
29 *
30 * @package Helper
31 * @author Kentaro Ohkouchi
32 * @version $Id$
33 */
34class SC_Helper_Purchase {
35
36    /**
37     * 受注を完了する.
38     *
39     * 下記のフローで受注を完了する.
40     *
41     * 1. トランザクションを開始する
42     * 2. カートの内容を検証する.
43     * 3. 受注一時テーブルから受注データを読み込む
44     * 4. ユーザーがログインしている場合はその他の発送先へ登録する
45     * 5. 受注データを受注テーブルへ登録する
46     * 6. トランザクションをコミットする
47     *
48     * 実行中に, 何らかのエラーが発生した場合, 処理を中止しエラーページへ遷移する
49     *
50     * 決済モジュールを使用する場合は受注ステータスを「決済処理中」に設定し,
51     * 決済完了後「新規受付」に変更すること
52     *
53     * @param integer $orderStatus 受注処理を完了する際に設定する受注ステータス
54     * @return void
55     */
56    function completeOrder($orderStatus = ORDER_NEW) {
57        $objQuery =& SC_Query::getSingletonInstance();
58        $objSiteSession = new SC_SiteSession();
59        $objCartSession = new SC_CartSession();
60        $objCustomer = new SC_Customer();
61        $customerId = $objCustomer->getValue('customer_id');
62
63        $objQuery->begin();
64        SC_Utils_Ex::sfIsPrePage($objSiteSession);
65        $uniqId = SC_Utils_Ex::sfCheckNormalAccess($objSiteSession,
66                                                   $objCartSession);
67        $orderTemp = $this->getOrderTemp($uniqId);
68
69        if ($objCustomer->isLoginSuccess(true)) {
70            $this->registerOtherDeliv($uniqId, $customerId);
71        }
72
73        $orderTemp['status'] = $orderStatus;
74        $orderId = $this->registerOrder($orderTemp, $objCartSession,
75                                        $_SESSION['cartKey']);
76        $objQuery->commit();
77        $objCustomer->updateSession();
78    }
79
80    /**
81     * 受注一時情報を取得する.
82     *
83     * @param integer $uniqId 受注一時情報ID
84     * @return array 受注一時情報の配列
85     */
86    function getOrderTemp($uniqId) {
87        $objQuery =& SC_Query::getSingletonInstance();
88        return $objQuery->getRow("dtb_order_temp", "*", "order_temp_id = ?",
89                                 array($uniqId));
90    }
91
92    /**
93     * 受注情報を登録する.
94     *
95     * 引数の受注情報を受注テーブル及び受注詳細テーブルに登録する.
96     * 登録後, 受注一時テーブルに削除フラグを立て, カートの内容を削除する.
97     *
98     * TODO ダウンロード商品の場合の扱いを検討
99     *
100     * @param array $orderParams 登録する受注情報の配列
101     * @param SC_CartSession $objCartSession カート情報のインスタンス
102     * @param integer $cartKey 登録を行うカート情報のキー
103     * @param integer 受注ID
104     */
105    function registerOrder($orderParams, &$objCartSession, $cartKey) {
106        $objQuery =& SC_Query::getSingletonInstance();
107
108        // 別のお届け先を指定が無ければ, お届け先に登録住所をコピー
109        if ($orderParams['deliv_check'] == "-1") {
110            $keys = array('name01', 'name02', 'kana01', 'kana02', 'pref', 'zip01',
111                          'zip02', 'addr01', 'addr02', 'tel01', 'tel02', 'tel03');
112            foreach ($keys as $key) {
113                $orderParams['deliv_' . $key] = $orderParams['order_' . $key];
114            }
115        }
116
117        // 不要な変数を unset
118        $unsets = array('mailmaga_flg', 'deliv_check', 'point_check', 'password',
119                        'reminder', 'reminder_answer', 'mail_flag', 'session');
120        foreach ($unsets as $unset) {
121            unset($orderParams[$unset]);
122        }
123
124        // ポイントは別登録
125        $addPoint = $orderParams['add_point'];
126        $usePoint = $orderParams['use_point'];
127        $orderParams['add_point'] = 0;
128        $orderParams['use_point'] = 0;
129
130        // 注文ステータスの指定が無い場合は新規受付
131        if(SC_Utils_Ex::isBlank($orderParams['status'])) {
132            $orderParams['status'] = ORDER_NEW;
133        }
134
135        $orderParams['create_date'] = 'Now()';
136        $orderParams['update_date'] = 'Now()';
137
138        $objQuery->insert("dtb_order", $orderParams);
139
140        // 受注.対応状況の更新
141        SC_Helper_DB_Ex::sfUpdateOrderStatus($orderParams['order_id'],
142                                             null, $addPoint, $usePoint);
143
144        // 詳細情報を取得
145        $cartItems = $objCartSession->getCartList($cartKey);
146
147        // 既に存在する詳細レコードを消しておく。
148        $objQuery->delete("dtb_order_detail", "order_id = ?",
149                          array($orderParams['order_id']));
150
151        $objProduct = new SC_Product();
152        foreach ($cartItems as $item) {
153            $p =& $item['productsClass'];
154            $detail['order_id'] = $orderParams['order_id'];
155            $detail['product_id'] = $p['product_id'];
156            $detail['product_class_id'] = $p['product_class_id'];
157            $detail['product_name'] = $p['name'];
158            $detail['product_code'] = $p['product_code'];
159            $detail['classcategory_name1'] = $p['classcategory_name1'];
160            $detail['classcategory_name2'] = $p['classcategory_name2'];
161            $detail['point_rate'] = $item['point_rate'];
162            $detail['price'] = $item['price'];
163            $detail['quantity'] = $item['quantity'];
164
165            // 在庫の減少処理
166            if (!$objProduct->reduceStock($p['product_class_id'], $item['quantity'])) {
167                $objQuery->rollback();
168                SC_Utils_Ex::sfDispSiteError(SOLD_OUT, "", true);
169            }
170            $objQuery->insert("dtb_order_detail", $detail);
171        }
172
173        $objQuery->update("dtb_order_temp", array('del_flg' => 1),
174                          "order_temp_id = ?",
175                          array(SC_SiteSession::getUniqId()));
176
177        $objCartSession->delAllProducts($cartKey);
178        SC_SiteSession::unsetUniqId();
179        return $orderParams['order_id'];
180    }
181
182    /**
183     * 会員登録住所と配送先住所を比較し, 差異があった場合は新規登録を行う.
184     *
185     * 別のお届け先に同一の配送先住所が存在する場合は登録しない.
186     *
187     * @param string $uniqId 配送先住所を特定するための一時テーブルのユニークID
188     * @param integer $customerId 顧客ID
189     * @return boolean 差異があり新規登録を行った場合 true; それ以外は false
190     */
191    function registerOtherDeliv($uniqId, $customerId) {
192        $keys = array('name01', 'name02', 'kana01', 'kana02', 'tel01', 'tel02',
193                      'tel03', 'zip01', 'zip02', 'pref', 'addr01', 'addr02');
194        $delivCols = "";
195        $cols = "";
196        $i = 0;
197        foreach ($keys as $key) {
198            $delivCols .= "deliv_" . $key;
199            $cols .= $key;
200            if ($i < count($keys) - 1) {
201                $delivCols .= ", ";
202                $cols .= ", ";
203            }
204            $i++;
205        }
206
207        $objQuery =& SC_Query::getSingletonInstance();
208        $orderTemp = $objQuery->select($delivCols, "dtb_order_temp",
209                                       "order_temp_id = ?", array($uniqId),
210                                       MDB2_FETCHMODE_ORDERED);
211
212        $customerAddrs = $objQuery->select($cols, "dtb_customer",
213                                           "customer_id = ?", array($customerId),
214                                           MDB2_FETCHMODE_ORDERED);
215
216        $hasAddr = false;
217        if ($orderTemp[0] != $customerAddrs[0]) {
218            $otherAddrs = $objQuery->select($cols, "dtb_other_deliv",
219                                           "customer_id = ?", array($customerId),
220                                            MDB2_FETCHMODE_ORDERED);
221            foreach ($otherAddrs as $otherAddr) {
222                if ($orderTemp[0] == $otherAddr) {
223                    $hasAddr = true;
224                }
225            }
226        }
227        if ($hasAddr) {
228            $i = 0;
229            foreach ($keys as $key) {
230                $addrs[$key] = $orderTemp[0][$i];
231                $i++;
232            }
233            $addrs['customer_id'] = $customerId;
234            $addrs['order_deliv_id'] = $objQuery->nextVal('dtb_other_deliv_other_deliv_id');
235            $objQuery->insert("dtb_other_deliv", $addrs);
236            return true;
237        }
238        return false;
239    }
240
241    /**
242     * 受注完了メールを送信する.
243     *
244     * HTTP_USER_AGENT の種別により, 携帯電話の場合は携帯用の文面,
245     * PC の場合は PC 用の文面でメールを送信する.
246     *
247     * @param integer $orderId 受注ID
248     * @return void
249     */
250    function sendOrderMail($orderId) {
251        $mailHelper = new SC_Helper_Mail_Ex();
252        $mailHelper->sfSendOrderMail($orderId,
253                                     SC_MobileUserAgent::isMobile() ? 2 : 1);
254    }
255}
Note: See TracBrowser for help on using the repository browser.