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

Revision 18860, 8.6 KB checked in by nanasess, 13 years ago (diff)

ページ間の遷移方法の改善(#783)

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