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 | */ |
---|
34 | class 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 | $orderTemp['status'] = $orderStatus; |
---|
70 | $orderId = $this->registerOrder($orderTemp, $objCartSession, |
---|
71 | $_SESSION['cartKey']); |
---|
72 | $objQuery->commit(); |
---|
73 | $objCustomer->updateSession(); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * 受注一時情報を取得する. |
---|
78 | * |
---|
79 | * @param integer $uniqId 受注一時情報ID |
---|
80 | * @return array 受注一時情報の配列 |
---|
81 | */ |
---|
82 | function getOrderTemp($uniqId) { |
---|
83 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
84 | return $objQuery->getRow("*", "dtb_order_temp", "order_temp_id = ?", |
---|
85 | array($uniqId)); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * 受注情報を登録する. |
---|
90 | * |
---|
91 | * 引数の受注情報を受注テーブル及び受注詳細テーブルに登録する. |
---|
92 | * 登録後, 受注一時テーブルに削除フラグを立て, カートの内容を削除する. |
---|
93 | * |
---|
94 | * TODO ダウンロード商品の場合の扱いを検討 |
---|
95 | * |
---|
96 | * @param array $orderParams 登録する受注情報の配列 |
---|
97 | * @param SC_CartSession $objCartSession カート情報のインスタンス |
---|
98 | * @param integer $cartKey 登録を行うカート情報のキー |
---|
99 | * @param integer 受注ID |
---|
100 | */ |
---|
101 | function registerOrder($orderParams, &$objCartSession, $cartKey) { |
---|
102 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
103 | |
---|
104 | // 別のお届け先を指定が無ければ, お届け先に登録住所をコピー |
---|
105 | if ($orderParams['deliv_check'] == "-1") { |
---|
106 | $keys = array('name01', 'name02', 'kana01', 'kana02', 'pref', 'zip01', |
---|
107 | 'zip02', 'addr01', 'addr02', 'tel01', 'tel02', 'tel03'); |
---|
108 | foreach ($keys as $key) { |
---|
109 | $orderParams['deliv_' . $key] = $orderParams['order_' . $key]; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | // 不要な変数を unset |
---|
114 | $unsets = array('mailmaga_flg', 'deliv_check', 'point_check', 'password', |
---|
115 | 'reminder', 'reminder_answer', 'mail_flag', 'session'); |
---|
116 | foreach ($unsets as $unset) { |
---|
117 | unset($orderParams[$unset]); |
---|
118 | } |
---|
119 | |
---|
120 | // ポイントは別登録 |
---|
121 | $addPoint = $orderParams['add_point']; |
---|
122 | $usePoint = $orderParams['use_point']; |
---|
123 | $orderParams['add_point'] = 0; |
---|
124 | $orderParams['use_point'] = 0; |
---|
125 | |
---|
126 | // 注文ステータスの指定が無い場合は新規受付 |
---|
127 | if(SC_Utils_Ex::isBlank($orderParams['status'])) { |
---|
128 | $orderParams['status'] = ORDER_NEW; |
---|
129 | } |
---|
130 | |
---|
131 | $orderParams['create_date'] = 'Now()'; |
---|
132 | $orderParams['update_date'] = 'Now()'; |
---|
133 | |
---|
134 | $objQuery->insert("dtb_order", $orderParams); |
---|
135 | |
---|
136 | // 受注.対応状況の更新 |
---|
137 | SC_Helper_DB_Ex::sfUpdateOrderStatus($orderParams['order_id'], |
---|
138 | null, $addPoint, $usePoint); |
---|
139 | |
---|
140 | // 詳細情報を取得 |
---|
141 | $cartItems = $objCartSession->getCartList($cartKey); |
---|
142 | |
---|
143 | // 既に存在する詳細レコードを消しておく。 |
---|
144 | $objQuery->delete("dtb_order_detail", "order_id = ?", |
---|
145 | array($orderParams['order_id'])); |
---|
146 | |
---|
147 | $objProduct = new SC_Product(); |
---|
148 | foreach ($cartItems as $item) { |
---|
149 | $p =& $item['productsClass']; |
---|
150 | $detail['order_id'] = $orderParams['order_id']; |
---|
151 | $detail['product_id'] = $p['product_id']; |
---|
152 | $detail['product_class_id'] = $p['product_class_id']; |
---|
153 | $detail['product_name'] = $p['name']; |
---|
154 | $detail['product_code'] = $p['product_code']; |
---|
155 | $detail['classcategory_name1'] = $p['classcategory_name1']; |
---|
156 | $detail['classcategory_name2'] = $p['classcategory_name2']; |
---|
157 | $detail['point_rate'] = $item['point_rate']; |
---|
158 | $detail['price'] = $item['price']; |
---|
159 | $detail['quantity'] = $item['quantity']; |
---|
160 | |
---|
161 | // 在庫の減少処理 |
---|
162 | if (!$objProduct->reduceStock($p['product_class_id'], $item['quantity'])) { |
---|
163 | $objQuery->rollback(); |
---|
164 | SC_Utils_Ex::sfDispSiteError(SOLD_OUT, "", true); |
---|
165 | } |
---|
166 | $objQuery->insert("dtb_order_detail", $detail); |
---|
167 | } |
---|
168 | |
---|
169 | $objQuery->update("dtb_order_temp", array('del_flg' => 1), |
---|
170 | "order_temp_id = ?", |
---|
171 | array(SC_SiteSession::getUniqId())); |
---|
172 | |
---|
173 | $objCartSession->delAllProducts($cartKey); |
---|
174 | SC_SiteSession::unsetUniqId(); |
---|
175 | return $orderParams['order_id']; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * 受注完了メールを送信する. |
---|
180 | * |
---|
181 | * HTTP_USER_AGENT の種別により, 携帯電話の場合は携帯用の文面, |
---|
182 | * PC の場合は PC 用の文面でメールを送信する. |
---|
183 | * |
---|
184 | * @param integer $orderId 受注ID |
---|
185 | * @return void |
---|
186 | */ |
---|
187 | function sendOrderMail($orderId) { |
---|
188 | $mailHelper = new SC_Helper_Mail_Ex(); |
---|
189 | $mailHelper->sfSendOrderMail($orderId, |
---|
190 | SC_MobileUserAgent::isMobile() ? 2 : 1); |
---|
191 | } |
---|
192 | } |
---|