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

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