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

Revision 19860, 16.9 KB checked in by nanasess, 13 years ago (diff)

#843(複数配送先の指定)

  • とりあえず通常配送が通るように修正
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
66        $uniqId = $objSiteSession->getUniqId();
67        $this->verifyChangeCart($uniqId, $objCartSession);
68
69        $orderTemp = $this->getOrderTemp($uniqId);
70
71        $orderTemp['status'] = $orderStatus;
72        $orderId = $this->registerOrder($orderTemp, $objCartSession,
73                                        $_SESSION['cartKey']);
74        $objQuery->commit();
75        $objCustomer->updateSession();
76    }
77
78    /**
79     * カートに変化が無いか検証する.
80     *
81     * ユニークIDとセッションのユニークIDを比較し, 異なる場合は
82     * エラー画面を表示する.
83     *
84     * カートが空の場合, 購入ボタン押下後にカートが変更された場合は
85     * カート画面へ遷移する.
86     *
87     * @param string $uniqId ユニークID
88     * @param SC_CartSession $objCartSession
89     * @return void
90     */
91    function verifyChangeCart($uniqId, &$objCartSession) {
92        $cartkeys = $objCartSession->getKeys();
93
94        foreach ($cartKeys as $cartKey) {
95            // 初回のみカートの内容を保存
96            $objCartSess->saveCurrentCart($uniqid, $cartKey);
97            /*
98             * POSTのユニークIDとセッションのユニークIDを比較
99             *(ユニークIDがPOSTされていない場合はスルー)
100             */
101            if(!SC_SiteSession::checkUniqId()) {
102                // エラーページの表示
103                // XXX $objSiteSess インスタンスは未使用?
104                SC_Utils_Ex::sfDispSiteError(CANCEL_PURCHASE, $objSiteSess);
105            }
106
107            // カート内が空でないか || 購入ボタンを押してから変化がないか
108            $quantity = $objCartSess->getTotalQuantity($cartKey);
109            if($objCartSess->checkChangeCart($cartKey) || !($quantity > 0)) {
110                // カート情報表示に強制移動する
111                if (Net_UserAgent_Mobile::isMobile()) {
112                    header("Location: ". MOBILE_CART_URL_PATH
113                           . "?" . session_name() . "=" . session_id());
114                } else {
115                    header("Location: ".CART_URL_PATH);
116                }
117                exit;
118            }
119        }
120    }
121
122    /**
123     * 受注一時情報を取得する.
124     *
125     * @param integer $uniqId 受注一時情報ID
126     * @return array 受注一時情報の配列
127     */
128    function getOrderTemp($uniqId) {
129        $objQuery =& SC_Query::getSingletonInstance();
130        return $objQuery->getRow("*", "dtb_order_temp", "order_temp_id = ?",
131                                 array($uniqId));
132    }
133
134    /**
135     * 受注一時情報を保存する.
136     *
137     * 既存のデータが存在しない場合は新規保存. 存在する場合は更新する.
138     * 既存のデータが存在せず, ユーザーがログインしている場合は,
139     * 会員情報をコピーする.
140     *
141     * @param integer $uniqId 受注一時情報ID
142     * @param array $params 登録する受注情報の配列
143     * @param SC_Customer $objCustomer SC_Customer インスタンス
144     * @return array void
145     */
146    function saveOrderTemp($uniqId, $params, &$objCustomer) {
147        if (SC_Utils_Ex::isBlank($uniqId)) {
148            return;
149        }
150
151        $objQuery =& SC_Query::getSingletonInstance();
152        // 存在するカラムのみを対象とする
153        $cols = $objQuery->listTableFields('dtb_order_temp');
154        foreach ($params as $key => $val) {
155            if (in_array($key, $cols)) {
156                $sqlval[$key] = $val;
157            }
158        }
159
160        $sqlval['session'] = serialize($_SESSION);
161        $exists = $this->getOrderTemp($uniqId);
162        if (SC_Utils_Ex::isBlank($exists)) {
163            $this->copyFromCustomer($sqlval, $objCustomer);
164            $sqlval['order_temp_id'] = $uniqId;
165            $sqlval['create_date'] = "now()";
166            $objQuery->insert("dtb_order_temp", $sqlval);
167        } else {
168            $objQuery->update("dtb_order_temp", $sqlval, 'order_temp_id = ?',
169                              array($uniqId));
170        }
171    }
172
173    /**
174     * 配送情報をセッションに保存する.
175     */
176    function saveShippingTemp(&$src, $other_deliv_id = 0,
177                              $keys = array('name01', 'name02', 'kana01', 'kana02',
178                                            'sex', 'zip01', 'zip02', 'pref',
179                                            'addr01', 'addr02',
180                                            'tel01', 'tel02', 'tel03'),
181                              $prefix = 'shipping') {
182        $dest = array();
183        foreach ($keys as $key) {
184            $dest[$prefix . '_' . $key] = $src[$prefix . '_' . $key];
185        }
186        $_SESSION['shipping'][$other_deliv_id] = $src;
187    }
188
189    /**
190     * 会員情報を受注情報にコピーする.
191     *
192     * ユーザーがログインしていない場合は何もしない.
193     * 会員情報を $dest の order_* へコピーする.
194     * customer_id は強制的にコピーされる.
195     *
196     * @param array $dest コピー先の配列
197     * @param SC_Customer $objCustomer SC_Customer インスタンス
198     * @param array $keys コピー対象のキー
199     * @param string $prefix コピー先の接頭辞. デフォルト order
200     * @return void
201     */
202    function copyFromCustomer(&$dest, &$objCustomer,
203                              $keys = array('name01', 'name02', 'kana01', 'kana02',
204                                            'sex', 'zip01', 'zip02', 'pref',
205                                            'addr01', 'addr02',
206                                            'tel01', 'tel02', 'tel03', 'job',
207                                            'birth', 'email'),
208                              $prefix = 'order') {
209        if ($objCustomer->isLoginSuccess(true)) {
210
211            foreach ($keys as $key) {
212                if (in_array($key, $keys)) {
213                    $dest[$prefix . '_' . $key] = $objCustomer->getValue($key);
214                }
215            }
216
217            if (Net_UserAgent_Mobile::isMobile()
218                && in_array('email', $keys)) {
219                $email_mobile = $objCustomer->getValue('email_mobile');
220                if (empty($email_mobile)) {
221                    $dest[$prefix . '_email'] = $objCustomer->getValue('email');
222                } else {
223                    $dest[$prefix . '_email'] = $email_mobile;
224                }
225            }
226
227            $dest['customer_id'] = $objCustomer->getValue('customer_id');
228            $dest['update_date'] = 'Now()';
229        }
230    }
231
232    /**
233     * 受注情報を配送情報にコピーする.
234     *
235     * 受注情報($src)を $dest の order_* へコピーする.
236     *
237     * @param array $dest コピー先の配列
238     * @param array $src コピー元の配列
239     * @param array $keys コピー対象のキー
240     * @param string $prefix コピー先の接頭辞. デフォルト shipping
241     * @param string $src_prefix コピー元の接頭辞. デフォルト order
242     * @return void
243     */
244    function copyFromOrder(&$dest, $src,
245                           $keys = array('name01', 'name02', 'kana01', 'kana02',
246                                         'sex', 'zip01', 'zip02', 'pref',
247                                         'addr01', 'addr02',
248                                         'tel01', 'tel02', 'tel03'),
249                           $prefix = 'shipping', $src_prefix = 'order') {
250        foreach ($keys as $key) {
251            if (in_array($key, $keys)) {
252                $dest[$prefix . '_' . $key] = $src[$src_prefix . '_' . $key];
253            }
254        }
255    }
256
257    /**
258     * 購入金額に応じた支払方法を取得する.
259     *
260     * @param integer $total 購入金額
261     * @param array $productClassIds 購入する商品規格IDの配列
262     * @return array 購入金額に応じた支払方法の配列
263     */
264    function getPayment($total, $productClassIds) {
265        // 有効な支払方法を取得
266        $objProduct = new SC_Product();
267        $paymentIds = $objProduct->getEnablePaymentIds($productClassIds);
268
269        $objQuery =& SC_Query::getSingletonInstance();
270
271        // 削除されていない支払方法を取得
272        $where = 'del_flg = 0 AND payment_id IN (' . implode(', ', array_pad(array(), count($paymentIds), '?')) . ')';
273        $objQuery->setOrder("rank DESC");
274        $payments = $objQuery->select("payment_id, payment_method, rule, upper_rule, note, payment_image", "dtb_payment", $where, $paymentIds);
275
276        foreach ($payments as $data) {
277            // 下限と上限が設定されている
278            if (strlen($data['rule']) != 0 && strlen($data['upper_rule']) != 0) {
279                if ($data['rule'] <= $total_inctax && $data['upper_rule'] >= $total_inctax) {
280                    $arrPayment[] = $data;
281                }
282            }
283            // 下限のみ設定されている
284            elseif (strlen($data['rule']) != 0) {
285                if($data['rule'] <= $total_inctax) {
286                    $arrPayment[] = $data;
287                }
288            }
289            // 上限のみ設定されている
290            elseif (strlen($data['upper_rule']) != 0) {
291                if($data['upper_rule'] >= $total_inctax) {
292                    $arrPayment[] = $data;
293                }
294            }
295            // いずれも設定なし
296            else {
297                $arrPayment[] = $data;
298            }
299          }
300        return $arrPayment;
301    }
302
303    /**
304     * 商品規格IDの配列からお届け予定日の配列を取得する.
305     *
306     * @param array $productClassIds 商品規格IDの配列
307     */
308    function getDelivDate($productClassIds) {
309        // TODO
310    }
311
312    /**
313     * 商品種別ID からお届け時間の配列を取得する.
314     */
315    function getDelivTime($productTypeId) {
316        $objQuery =& SC_Query::getSingletonInstance();
317        $from = <<< __EOS__
318                 dtb_deliv T1
319            JOIN dtb_delivtime T2
320              ON T1.deliv_id = T2. deliv_id
321__EOS__;
322            $objQuery->setOrder("time_id");
323            $where = "deliv_id = ?";
324            $results = $objQuery->select("time_id, deliv_time", $from,
325                                         "product_type_id = ?", array($productTypeId));
326            $arrDelivTime = array();
327            foreach ($results as $val) {
328                $arrDelivTime[$val['time_id']] = $val['deliv_time'];
329            }
330            return $arrDelivTime;
331    }
332
333    /**
334     * 受注情報を登録する.
335     *
336     * 引数の受注情報を受注テーブル及び受注詳細テーブルに登録する.
337     * 登録後, 受注一時テーブルに削除フラグを立て, カートの内容を削除する.
338     *
339     * TODO ダウンロード商品の場合の扱いを検討
340     *
341     * @param array $orderParams 登録する受注情報の配列
342     * @param SC_CartSession $objCartSession カート情報のインスタンス
343     * @param integer $cartKey 登録を行うカート情報のキー
344     * @param integer 受注ID
345     */
346    function registerOrder($orderParams, &$objCartSession, $cartKey) {
347        $objQuery =& SC_Query::getSingletonInstance();
348
349        // 別のお届け先を指定が無ければ, お届け先に登録住所をコピー
350        /* FIXME
351        if ($orderParams['deliv_check'] == "-1") {
352            $keys = array('name01', 'name02', 'kana01', 'kana02', 'pref', 'zip01',
353                          'zip02', 'addr01', 'addr02', 'tel01', 'tel02', 'tel03');
354            foreach ($keys as $key) {
355                $orderParams['deliv_' . $key] = $orderParams['order_' . $key];
356            }
357        }
358        */
359        // 不要な変数を unset
360        $unsets = array('mailmaga_flg', 'deliv_check', 'point_check', 'password',
361                        'reminder', 'reminder_answer', 'mail_flag', 'session');
362        foreach ($unsets as $unset) {
363            unset($orderParams[$unset]);
364        }
365
366        // ポイントは別登録
367        $addPoint = $orderParams['add_point'];
368        $usePoint = $orderParams['use_point'];
369        $orderParams['add_point'] = 0;
370        $orderParams['use_point'] = 0;
371
372        // 注文ステータスの指定が無い場合は新規受付
373        if(SC_Utils_Ex::isBlank($orderParams['status'])) {
374            $orderParams['status'] = ORDER_NEW;
375        }
376
377        $orderParams['create_date'] = 'Now()';
378        $orderParams['update_date'] = 'Now()';
379
380        $objQuery->insert("dtb_order", $orderParams);
381
382        // 受注.対応状況の更新
383        SC_Helper_DB_Ex::sfUpdateOrderStatus($orderParams['order_id'],
384                                             null, $addPoint, $usePoint);
385
386        // 詳細情報を取得
387        $cartItems = $objCartSession->getCartList($cartKey);
388
389        // 既に存在する詳細レコードを消しておく。
390        $objQuery->delete("dtb_order_detail", "order_id = ?",
391                          array($orderParams['order_id']));
392
393        $objProduct = new SC_Product();
394        foreach ($cartItems as $item) {
395            $p =& $item['productsClass'];
396            $detail['order_id'] = $orderParams['order_id'];
397            $detail['product_id'] = $p['product_id'];
398            $detail['product_class_id'] = $p['product_class_id'];
399            $detail['product_name'] = $p['name'];
400            $detail['product_code'] = $p['product_code'];
401            $detail['classcategory_name1'] = $p['classcategory_name1'];
402            $detail['classcategory_name2'] = $p['classcategory_name2'];
403            $detail['point_rate'] = $item['point_rate'];
404            $detail['price'] = $item['price'];
405            $detail['quantity'] = $item['quantity'];
406
407            // 在庫の減少処理
408            if (!$objProduct->reduceStock($p['product_class_id'], $item['quantity'])) {
409                $objQuery->rollback();
410                SC_Utils_Ex::sfDispSiteError(SOLD_OUT, "", true);
411            }
412            $objQuery->insert("dtb_order_detail", $detail);
413        }
414
415        $objQuery->update("dtb_order_temp", array('del_flg' => 1),
416                          "order_temp_id = ?",
417                          array(SC_SiteSession::getUniqId()));
418
419        $objCartSession->delAllProducts($cartKey);
420        SC_SiteSession::unsetUniqId();
421        return $orderParams['order_id'];
422    }
423
424    /**
425     * 受注完了メールを送信する.
426     *
427     * HTTP_USER_AGENT の種別により, 携帯電話の場合は携帯用の文面,
428     * PC の場合は PC 用の文面でメールを送信する.
429     *
430     * @param integer $orderId 受注ID
431     * @return void
432     */
433    function sendOrderMail($orderId) {
434        $mailHelper = new SC_Helper_Mail_Ex();
435        $mailHelper->sfSendOrderMail($orderId,
436                                     SC_MobileUserAgent::isMobile() ? 2 : 1);
437    }
438}
Note: See TracBrowser for help on using the repository browser.