/**
  * 受注を完了する.
  *
  * 下記のフローで受注を完了する.
  *
  * 1. トランザクションを開始する
  * 2. カートの内容を検証する.
  * 3. 受注一時テーブルから受注データを読み込む
  * 4. ユーザーがログインしている場合はその他の発送先へ登録する
  * 5. 受注データを受注テーブルへ登録する
  * 6. トランザクションをコミットする
  *
  * 実行中に, 何らかのエラーが発生した場合, 処理を中止しエラーページへ遷移する
  *
  * 決済モジュールを使用する場合は対応状況を「決済処理中」に設定し,
  * 決済完了後「新規受付」に変更すること
  *
  * @param  integer $orderStatus 受注処理を完了する際に設定する対応状況
  * @return void
  */
 public function completeOrder($orderStatus = ORDER_NEW)
 {
     $objQuery =& SC_Query_Ex::getSingletonInstance();
     $objSiteSession = new SC_SiteSession_Ex();
     $objCartSession = new SC_CartSession_Ex();
     $objCustomer = new SC_Customer_Ex();
     $customerId = $objCustomer->getValue('customer_id');
     $objQuery->begin();
     if (!$objSiteSession->isPrePage()) {
         SC_Utils_Ex::sfDispSiteError(PAGE_ERROR, $objSiteSession);
     }
     $uniqId = $objSiteSession->getUniqId();
     $this->verifyChangeCart($uniqId, $objCartSession);
     $orderTemp = $this->getOrderTemp($uniqId);
     $orderTemp['status'] = $orderStatus;
     $cartkey = $objCartSession->getKey();
     $order_id = $this->registerOrderComplete($orderTemp, $objCartSession, $cartkey);
     $isMultiple = SC_Helper_Purchase::isMultiple();
     $shippingTemp =& $this->getShippingTemp($isMultiple);
     foreach ($shippingTemp as $shippingId => $val) {
         $this->registerShipmentItem($order_id, $shippingId, $val['shipment_item']);
     }
     $this->registerShipping($order_id, $shippingTemp);
     $objQuery->commit();
     //会員情報の最終購入日、購入合計を更新
     if ($customerId > 0) {
         SC_Customer_Ex::updateOrderSummary($customerId);
     }
     $this->cleanupSession($order_id, $objCartSession, $objCustomer, $cartkey);
     GC_Utils_Ex::gfPrintLog('order complete. order_id=' . $order_id);
 }
 /**
  * Page のアクション.
  *
  * @return void
  */
 function action()
 {
     $objCartSess = new SC_CartSession_Ex();
     $objSiteSess = new SC_SiteSession_Ex();
     $objCustomer = new SC_Customer_Ex();
     $objPurchase = new SC_Helper_Purchase_Ex();
     $objHelperMail = new SC_Helper_Mail_Ex();
     $this->is_multiple = $objPurchase->isMultiple();
     // 前のページで正しく登録手続きが行われた記録があるか判定
     if (!$objSiteSess->isPrePage()) {
         SC_Utils_Ex::sfDispSiteError(PAGE_ERROR, $objSiteSess);
     }
     // ユーザユニークIDの取得と購入状態の正当性をチェック
     $this->tpl_uniqid = $objSiteSess->getUniqId();
     $objPurchase->verifyChangeCart($this->tpl_uniqid, $objCartSess);
     $this->cartKey = $objCartSess->getKey();
     // カート内商品のチェック
     $this->tpl_message = $objCartSess->checkProducts($this->cartKey);
     if (!SC_Utils_Ex::isBlank($this->tpl_message)) {
         SC_Response_Ex::sendRedirect(CART_URLPATH);
         SC_Response_Ex::actionExit();
     }
     // カートの商品を取得
     $this->arrShipping = $objPurchase->getShippingTemp($this->is_multiple);
     $this->arrCartItems = $objCartSess->getCartList($this->cartKey);
     // 合計金額
     $this->tpl_total_inctax[$this->cartKey] = $objCartSess->getAllProductsTotal($this->cartKey);
     // 税額
     $this->tpl_total_tax[$this->cartKey] = $objCartSess->getAllProductsTax($this->cartKey);
     // ポイント合計
     $this->tpl_total_point[$this->cartKey] = $objCartSess->getAllProductsPoint($this->cartKey);
     // 一時受注テーブルの読込
     $arrOrderTemp = $objPurchase->getOrderTemp($this->tpl_uniqid);
     // カート集計を元に最終計算
     $arrCalcResults = $objCartSess->calculate($this->cartKey, $objCustomer, $arrOrderTemp['use_point'], $objPurchase->getShippingPref($this->is_multiple), $arrOrderTemp['charge'], $arrOrderTemp['discount'], $arrOrderTemp['deliv_id']);
     $this->arrForm = array_merge($arrOrderTemp, $arrCalcResults);
     // 会員ログインチェック
     if ($objCustomer->isLoginSuccess(true)) {
         $this->tpl_login = '******';
         $this->tpl_user_point = $objCustomer->getValue('point');
     }
     // 決済モジュールを使用するかどうか
     $this->use_module = SC_Helper_Payment_Ex::useModule($this->arrForm['payment_id']);
     switch ($this->getMode()) {
         // 前のページに戻る
         case 'return':
             // 正常な推移であることを記録しておく
             $objSiteSess->setRegistFlag();
             SC_Response_Ex::sendRedirect(SHOPPING_PAYMENT_URLPATH);
             SC_Response_Ex::actionExit();
             break;
         case 'confirm':
             /*
              * 決済モジュールで必要なため, 受注番号を取得
              */
             $this->arrForm['order_id'] = $objPurchase->getNextOrderID();
             $_SESSION['order_id'] = $this->arrForm['order_id'];
             // 集計結果を受注一時テーブルに反映
             $objPurchase->saveOrderTemp($this->tpl_uniqid, $this->arrForm, $objCustomer);
             // 正常に登録されたことを記録しておく
             $objSiteSess->setRegistFlag();
             // 決済モジュールを使用する場合
             if ($this->use_module) {
                 $objPurchase->completeOrder(ORDER_PENDING);
                 SC_Response_Ex::sendRedirect(SHOPPING_MODULE_URLPATH);
             } else {
                 $objPurchase->completeOrder(ORDER_NEW);
                 $template_id = SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE ? 2 : 1;
                 $objHelperMail->sfSendOrderMail($this->arrForm['order_id'], $template_id);
                 SC_Response_Ex::sendRedirect(SHOPPING_COMPLETE_URLPATH);
             }
             SC_Response_Ex::actionExit();
             break;
         default:
             break;
     }
 }