source: branches/version-2_11-dev/data/class/pages/shopping/LC_Page_Shopping_Multiple.php @ 21100

Revision 21100, 12.6 KB checked in by Seasoft, 13 years ago (diff)

#1362 (お届け先の複数指定 数量0も保持される)
#1387 (お届け先の複数指定 数量空欄も許容したい)
#1294 (ソースを読みやすくする)
#1424 (LC_Page_Shopping_Multiple#lfCheckError 無駄な処理)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2011 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// {{{ requires
25require_once CLASS_EX_REALDIR . 'page_extends/LC_Page_Ex.php';
26
27/**
28 * お届け先の複数指定 のページクラス.
29 *
30 * @package Page
31 * @author LOCKON CO.,LTD.
32 * @version $Id$
33 */
34class LC_Page_Shopping_Multiple extends LC_Page_Ex {
35
36    // }}}
37    // {{{ functions
38
39    /**
40     * Page を初期化する.
41     *
42     * @return void
43     */
44    function init() {
45        parent::init();
46        $this->tpl_title = "お届け先の複数指定";
47        $this->httpCacheControl('nocache');
48    }
49
50    /**
51     * Page のプロセス.
52     *
53     * @return void
54     */
55    function process() {
56        $this->action();
57        $this->sendResponse();
58    }
59
60    /**
61     * Page のプロセス.
62     *
63     * @return void
64     */
65    function action() {
66        $objSiteSess = new SC_SiteSession_Ex();
67        $objCartSess = new SC_CartSession_Ex();
68        $objPurchase = new SC_Helper_Purchase_Ex();
69        $objCustomer = new SC_Customer_Ex();
70        $objFormParam = new SC_FormParam_Ex();
71
72        $this->tpl_uniqid = $objSiteSess->getUniqId();
73
74        $this->addrs = $this->getDelivAddrs($objCustomer, $objPurchase,
75                                            $this->tpl_uniqid);
76        $this->tpl_addrmax = count($this->addrs);
77        $this->addrs = array_merge(array('' => '選択してください'), $this->addrs);
78        $this->lfInitParam($objFormParam);
79
80        $objPurchase->verifyChangeCart($this->tpl_uniqid, $objCartSess);
81
82        switch ($this->getMode()) {
83            case 'confirm':
84                $objFormParam->setParam($_POST);
85                $this->arrErr = $this->lfCheckError($objFormParam);
86                if (SC_Utils_Ex::isBlank($this->arrErr)) {
87                    // フォームの情報を一時保存しておく
88                    $_SESSION['multiple_temp'] = $objFormParam->getHashArray();
89                    $this->saveMultipleShippings($this->tpl_uniqid, $objFormParam,
90                                                 $objCustomer, $objPurchase,
91                                                 $objCartSess);
92                    $objSiteSess->setRegistFlag();
93                    SC_Response_Ex::sendRedirect("payment.php");
94                    exit;
95                }
96                break;
97
98            default:
99                $this->setParamToSplitItems($objFormParam, $objCartSess);
100        }
101
102        // 前のページから戻ってきた場合
103        if ($_GET['from'] == 'multiple') {
104            $objFormParam->setParam($_SESSION['multiple_temp']);
105        }
106
107        $this->arrForm = $objFormParam->getFormParamList();
108    }
109
110    /**
111     * デストラクタ.
112     *
113     * @return void
114     */
115    function destroy() {
116        parent::destroy();
117    }
118
119    /**
120     * フォームを初期化する.
121     *
122     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
123     * @return void
124     */
125    function lfInitParam(&$objFormParam) {
126        $objFormParam->addParam("商品規格ID", "product_class_id", INT_LEN, 'n', array("EXIST_CHECK", "MAX_LENGTH_CHECK", "NUM_CHECK"));
127        $objFormParam->addParam("商品名", "name");
128        $objFormParam->addParam("規格1", "class_name1");
129        $objFormParam->addParam("規格2", "class_name2");
130        $objFormParam->addParam("規格分類1", "classcategory_name1");
131        $objFormParam->addParam("規格分類2", "classcategory_name2");
132        $objFormParam->addParam("メイン画像", "main_image");
133        $objFormParam->addParam("メイン一覧画像", "main_list_image");
134        $objFormParam->addParam("販売価格", "price");
135        $objFormParam->addParam("数量", 'quantity', INT_LEN, 'n', array("EXIST_CHECK", "MAX_LENGTH_CHECK", "NUM_CHECK"), 1);
136        $objFormParam->addParam("配送先住所", 'shipping', INT_LEN, 'n', array("MAX_LENGTH_CHECK", "NUM_CHECK"));
137        $objFormParam->addParam("カート番号", "cart_no", INT_LEN, 'n', array("EXIST_CHECK", "MAX_LENGTH_CHECK", "NUM_CHECK"));
138        $objFormParam->addParam("行数", "line_of_num", INT_LEN, 'n', array("EXIST_CHECK", "MAX_LENGTH_CHECK", "NUM_CHECK"));
139    }
140
141    /**
142     * カートの商品を数量ごとに分割し, フォームに設定する.
143     *
144     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
145     * @param SC_CartSession $objCartSess SC_CartSession インスタンス
146     * @return void
147     */
148    function setParamToSplitItems(&$objFormParam, &$objCartSess) {
149        $cartLists =& $objCartSess->getCartList($objCartSess->getKey());
150        $arrItems = array();
151        $index = 0;
152        foreach (array_keys($cartLists) as $key) {
153            $arrProductsClass = $cartLists[$key]['productsClass'];
154            $quantity = (int) $cartLists[$key]['quantity'];
155            for ($i = 0; $i < $quantity; $i++) {
156                foreach ($arrProductsClass as $key2 => $val) {
157                    $arrItems[$key2][$index] = $val;
158                }
159                $arrItems['quantity'][$index] = 1;
160                $arrItems['price'][$index] = $cartLists[$key]['price'];
161                $index++;
162            }
163        }
164        $objFormParam->setParam($arrItems);
165        $objFormParam->setValue('line_of_num', $index);
166    }
167
168    /**
169     * 配送住所のプルダウン用連想配列を取得する.
170     *
171     * 会員ログイン済みの場合は, 会員登録住所及び追加登録住所を取得する.
172     * 非会員の場合は, 「お届け先の指定」画面で入力した住所を取得する.
173     *
174     * @param SC_Customer $objCustomer SC_Customer インスタンス
175     * @param SC_Helper_Purchase $objPurchase SC_Helper_Purchase インスタンス
176     * @param integer $uniqid 受注一時テーブルのユニークID
177     * @return array 配送住所のプルダウン用連想配列
178     */
179    function getDelivAddrs(&$objCustomer, &$objPurchase, $uniqid) {
180        $masterData = new SC_DB_MasterData();
181        $arrPref = $masterData->getMasterData('mtb_pref');
182
183        // 会員ログイン時
184        if ($objCustomer->isLoginSuccess(true)) {
185            $arrAddrs = $objCustomer->getCustomerAddress($objCustomer->getValue('customer_id'));
186            $arrResults = array();
187            foreach ($arrAddrs as $val) {
188                $other_deliv_id = SC_Utils_Ex::isBlank($val['other_deliv_id']) ? 0 : $val['other_deliv_id'];
189                $arrResults[$other_deliv_id] = $val['name01'] . $val['name02']
190                    . " " . $arrPref[$val['pref']] . $val['addr01'] . $val['addr02'];
191            }
192        }
193        // 非会員
194        else {
195            $arrShippings = $objPurchase->getShippingTemp();
196            foreach ($arrShippings as $shipping_id => $val) {
197                $arrResults[$shipping_id] = $val['shipping_name01'] . $val['shipping_name02']
198                    . " " . $arrPref[$val['shipping_pref']]
199                    . $val['shipping_addr01'] . $val['shipping_addr02'];
200            }
201        }
202        return $arrResults;
203    }
204
205    /**
206     * 入力チェックを行う.
207     *
208     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
209     * @return array エラー情報の配列
210     */
211    function lfCheckError(&$objFormParam) {
212        $objCartSess = new SC_CartSession_Ex();
213
214        $objFormParam->convParam();
215        // 数量未入力は0に置換
216        $objFormParam->setValue('quantity', $objFormParam->getValue('quantity', 0));
217
218        $arrErr = $objFormParam->checkError();
219
220        $arrParams = $objFormParam->getSwapArray();
221
222        if (empty($arrErr)) {
223            foreach ($arrParams as $index => $arrParam) {
224                // 数量0で、お届け先を選択している場合
225                if ($arrParam['quantity'] == 0 && !SC_Utils_Ex::isBlank($arrParam['shipping'])) {
226                    $arrErr['shipping'][$index] = '※ 数量が0の場合、お届け先を入力できません。<br />';;
227                }
228                // 数量の入力があり、お届け先を選択していない場合
229                if ($arrParam['quantity'] > 0 && SC_Utils_Ex::isBlank($arrParam['shipping'])) {
230                    $arrErr['shipping'][$index] = '※ お届け先が入力されていません。<br />';
231                }
232            }
233        }
234
235        // 入力エラーが無い場合、カゴの中身との数量の整合を確認
236        if (empty($arrErr)) {
237            $arrQuantity = array();
238            // 入力内容を集計
239            foreach ($arrParams as $arrParam) {
240                $product_class_id = $arrParam['product_class_id'];
241                $arrQuantity[$product_class_id] += $arrParam['quantity'];
242            }
243            // カゴの中身と突き合わせ
244            $cartLists =& $objCartSess->getCartList($objCartSess->getKey());
245            foreach ($cartLists as $arrCartRow) {
246                $product_class_id = $arrCartRow['id'];
247                // 差異がある場合、エラーを記録
248                if ($arrCartRow['quantity'] != $arrQuantity[$product_class_id]) {
249                    foreach ($arrParams as $index => $arrParam) {
250                        if ($arrParam['product_class_id'] == $product_class_id) {
251                            $arrErr['quantity'][$index] = '※ カゴの中(数量:' . $arrCartRow['quantity'] .')と合計が一致していません。<br />';
252                        }
253                    }
254                }
255            }
256        }
257        return $arrErr;
258    }
259
260    /**
261     * 複数配送情報を一時保存する.
262     *
263     * 会員ログインしている場合は, その他のお届け先から住所情報を取得する.
264     *
265     * @param integer $uniqid 一時受注テーブルのユニークID
266     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
267     * @param SC_Customer $objCustomer SC_Customer インスタンス
268     * @param SC_Helper_Purchase $objPurchase SC_Helper_Purchase インスタンス
269     * @param SC_CartSession $objCartSess SC_CartSession インスタンス
270     * @return void
271     */
272    function saveMultipleShippings($uniqid, &$objFormParam, &$objCustomer,
273                                   &$objPurchase, &$objCartSess) {
274        $objQuery =& SC_Query_Ex::getSingletonInstance();
275
276        $arrParams = $objFormParam->getSwapArray();
277
278        foreach ($arrParams as $arrParam) {
279            $other_deliv_id = $arrParam['shipping'];
280
281            if ($objCustomer->isLoginSuccess(true)) {
282                if ($other_deliv_id != 0) {
283                    $otherDeliv = $objQuery->select("*", "dtb_other_deliv",
284                                                    "other_deliv_id = ?",
285                                                    array($other_deliv_id));
286                    foreach ($otherDeliv[0] as $key => $val) {
287                        $arrValues[$other_deliv_id]['shipping_' . $key] = $val;
288                    }
289                } else {
290                    $objPurchase->copyFromCustomer($arrValues[0], $objCustomer,
291                                                   'shipping');
292                }
293            } else {
294                $arrValues = $objPurchase->getShippingTemp();
295            }
296            $arrItemTemp[$other_deliv_id][$arrParam['product_class_id']] += $arrParam['quantity'];
297        }
298
299        $objPurchase->clearShipmentItemTemp();
300
301        foreach ($arrValues as $shipping_id => $arrVal) {
302            $objPurchase->saveShippingTemp($arrVal, $shipping_id);
303        }
304
305        foreach ($arrItemTemp as $other_deliv_id => $arrProductClassIds) {
306            foreach ($arrProductClassIds as $product_class_id => $quantity) {
307                if ($quantity == 0) continue;
308                $objPurchase->setShipmentItemTemp($other_deliv_id,
309                                                  $product_class_id,
310                                                  $quantity);
311            }
312        }
313
314        // $arrValues[0] には, 購入者の情報が格納されている
315        $objPurchase->saveOrderTemp($uniqid, $arrValues[0], $objCustomer);
316    }
317}
318?>
Note: See TracBrowser for help on using the repository browser.