source: branches/version-2_5-dev/data/class/SC_CartSession.php @ 18859

Revision 18859, 20.3 KB checked in by nanasess, 14 years ago (diff)

ページ間の遷移方法の改善(#783)

  • カート内集計の関数が VIEW に依存しないように修正
  • SC_Helper_DB::sfTotalCart(), SC_Helper_DB::sfTotalConfirm() を SC_CartSession に移動
  • 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-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/* カートセッション管理クラス */
25class SC_CartSession {
26    /** ユニークIDを指定する. */
27    var $key_tmp;
28
29    /** カートのセッション変数. */
30    var $cartSession;
31
32    /* コンストラクタ */
33    function SC_CartSession($cartKey = "cart") {
34        $this->cartSession =& $_SESSION[$cartKey];
35    }
36
37    // 商品購入処理中のロック
38    function saveCurrentCart($key_tmp, $productTypeId) {
39        $this->key_tmp = "savecart_" . $key_tmp;
40        // すでに情報がなければ現状のカート情報を記録しておく
41        if(count($_SESSION[$this->key_tmp]) == 0) {
42            $_SESSION[$this->key_tmp] = $this->cartSession[$productTypeId];
43        }
44        // 1世代古いコピー情報は、削除しておく
45        foreach($_SESSION as $k => $val) {
46            if($k != $this->key_tmp && preg_match("/^savecart_/", $k)) {
47                unset($this->cartSession[$productTypeId][$k]);
48            }
49        }
50        $this->registerKey($productTypeId);
51    }
52
53    // 商品購入中の変更があったかをチェックする。
54    function getCancelPurchase($productTypeId) {
55        $ret = isset($this->cartSession[$productTypeId]['cancel_purchase'])
56            ? $this->cartSession[$productTypeId]['cancel_purchase'] : "";
57        $this->cartSession[$productTypeId]['cancel_purchase'] = false;
58        return $ret;
59    }
60
61    // 購入処理中に商品に変更がなかったかを判定
62    function checkChangeCart($productTypeId) {
63        $change = false;
64        $max = $this->getMax($productTypeId);
65        for($i = 1; $i <= $max; $i++) {
66            if ($this->cartSession[$productTypeId][$i]['quantity']
67                != $_SESSION[$this->key_tmp][$i]['quantity']) {
68
69                $change = true;
70                break;
71            }
72            if ($this->cartSession[$productTypeId][$i]['id']
73                != $_SESSION[$this->key_tmp][$i]['id']) {
74
75                $change = true;
76                break;
77            }
78        }
79        if ($change) {
80            // 一時カートのクリア
81            unset($_SESSION[$this->key_tmp]);
82            $this->cartSession[$productTypeId][$key]['cancel_purchase'] = true;
83        } else {
84            $this->cartSession[$productTypeId]['cancel_purchase'] = false;
85        }
86        return $this->cartSession[$productTypeId]['cancel_purchase'];
87    }
88
89    // 次に割り当てるカートのIDを取得する
90    function getNextCartID($productTypeId) {
91        foreach($this->cartSession[$productTypeId] as $key => $val){
92            $arrRet[] = $this->cartSession[$productTypeId][$key]['cart_no'];
93        }
94        return (max($arrRet) + 1);
95    }
96
97    /**
98     * 商品ごとの合計価格
99     * XXX 実際には、「商品」ではなく、「カートの明細行(≒商品規格)」のような気がします。
100     *
101     * @param integer $id
102     * @return string 商品ごとの合計価格(税込み)
103     * @deprecated SC_CartSession::getCartList() を使用してください
104     */
105    function getProductTotal($id, $productTypeId) {
106        $max = $this->getMax($productTypeId);
107        for($i = 0; $i <= $max; $i++) {
108            if(isset($this->cartSession[$productTypeId][$i]['id'])
109               && $this->cartSession[$productTypeId][$i]['id'] == $id) {
110
111                // 税込み合計
112                $price = $this->cartSession[$productTypeId][$i]['price'];
113                $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
114                $pre_tax = SC_Helper_DB_Ex::sfPreTax($price);
115                $total = $pre_tax * $quantity;
116                return $total;
117            }
118        }
119        return 0;
120    }
121
122    // 値のセット
123    function setProductValue($id, $key, $val, $productTypeId) {
124        $max = $this->getMax($productTypeId);
125        for($i = 0; $i <= $max; $i++) {
126            if(isset($this->cartSession[$productTypeId][$i]['id'])
127               && $this->cartSession[$productTypeId][$i]['id'] == $id) {
128                $this->cartSession[$productTypeId][$i][$key] = $val;
129            }
130        }
131    }
132
133    // カート内商品の最大要素番号を取得する。
134    function getMax($productTypeId) {
135        $max = 0;
136        if (count($this->cartSession[$productTypeId]) > 0){
137            foreach($this->cartSession[$productTypeId] as $key => $val) {
138                if (is_numeric($key)) {
139                    if($max < $key) {
140                        $max = $key;
141                    }
142                }
143            }
144        }
145        return ($max);
146    }
147
148    // カート内商品数の合計
149    function getTotalQuantity($productTypeId) {
150        $total = 0;
151        $max = $this->getMax($productTypeId);
152        for($i = 0; $i <= $max; $i++) {
153            $total+= $this->cartSession[$productTypeId][$i]['quantity'];
154        }
155        return $total;
156    }
157
158
159    // 全商品の合計価格
160    function getAllProductsTotal($productTypeId) {
161        // 税込み合計
162        $total = 0;
163        $max = $this->getMax($productTypeId);
164        for($i = 0; $i <= $max; $i++) {
165
166            if (!isset($this->cartSession[$productTypeId][$i]['price'])) {
167                $this->cartSession[$productTypeId][$i]['price'] = "";
168            }
169
170            $price = $this->cartSession[$productTypeId][$i]['price'];
171
172            if (!isset($this->cartSession[$productTypeId][$i]['quantity'])) {
173                $this->cartSession[$productTypeId][$i]['quantity'] = "";
174            }
175            $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
176
177            $pre_tax = SC_Helper_DB_Ex::sfPreTax($price);
178            $total+= ($pre_tax * $quantity);
179        }
180        return $total;
181    }
182
183    // 全商品の合計税金
184    function getAllProductsTax($productTypeId) {
185        // 税合計
186        $total = 0;
187        $max = $this->getMax($productTypeId);
188        for($i = 0; $i <= $max; $i++) {
189            $price = $this->cartSession[$productTypeId][$i]['price'];
190            $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
191            $tax = SC_Helper_DB_Ex::sfTax($price);
192            $total+= ($tax * $quantity);
193        }
194        return $total;
195    }
196
197    // 全商品の合計ポイント
198    function getAllProductsPoint($productTypeId) {
199        // ポイント合計
200        $total = 0;
201        if (USE_POINT !== false) {
202            $max = $this->getMax($productTypeId);
203            for($i = 0; $i <= $max; $i++) {
204                $price = $this->cartSession[$productTypeId][$i]['price'];
205                $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
206
207                if (!isset($this->cartSession[$productTypeId][$i]['point_rate'])) {
208                    $this->cartSession[$productTypeId][$i]['point_rate'] = "";
209                }
210                $point_rate = $this->cartSession[$productTypeId][$i]['point_rate'];
211
212                if (!isset($this->cartSession[$productTypeId][$i]['id'][0])) {
213                    $this->cartSession[$productTypeId][$i]['id'][0] = "";
214                }
215                $id = $this->cartSession[$productTypeId][$i]['id'][0];
216                $point = SC_Utils_Ex::sfPrePoint($price, $point_rate, POINT_RULE, $id);
217                $total+= ($point * $quantity);
218            }
219        }
220        return $total;
221    }
222
223    // カートへの商品追加
224    function addProduct($id, $quantity, $productTypeId) {
225        $find = false;
226        $max = $this->getMax($productTypeId);
227        for($i = 0; $i <= $max; $i++) {
228
229            if($this->cartSession[$productTypeId][$i]['id'] == $id) {
230                $val = $this->cartSession[$productTypeId][$i]['quantity'] + $quantity;
231                if(strlen($val) <= INT_LEN) {
232                    $this->cartSession[$productTypeId][$i]['quantity'] += $quantity;
233                }
234                $find = true;
235            }
236        }
237        if(!$find) {
238            $this->cartSession[$productTypeId][$max+1]['id'] = $id;
239            $this->cartSession[$productTypeId][$max+1]['quantity'] = $quantity;
240            $this->cartSession[$productTypeId][$max+1]['cart_no'] = $this->getNextCartID($productTypeId);
241        }
242    }
243
244    // 前頁のURLを記録しておく
245    function setPrevURL($url) {
246        // 前頁として記録しないページを指定する。
247        $arrExclude = array(
248            "/shopping/"
249        );
250        $exclude = false;
251        // ページチェックを行う。
252        foreach($arrExclude as $val) {
253            if(preg_match("/" . preg_quote($val) . "/", $url)) {
254                $exclude = true;
255                break;
256            }
257        }
258        // 除外ページでない場合は、前頁として記録する。
259        if(!$exclude) {
260            $_SESSION['prev_url'] = $url;
261        }
262    }
263
264    // 前頁のURLを取得する
265    function getPrevURL() {
266        return isset($_SESSION['prev_url']) ? $_SESSION['prev_url'] : "";
267    }
268
269    // キーが一致した商品の削除
270    function delProductKey($keyname, $val, $productTypeId) {
271        $max = count($this->cartSession[$productTypeId]);
272        for($i = 0; $i < $max; $i++) {
273            if($this->cartSession[$productTypeId][$i][$keyname] == $val) {
274                unset($this->cartSession[$productTypeId][$i]);
275            }
276        }
277    }
278
279    function setValue($key, $val, $productTypeId) {
280        $this->cartSession[$productTypeId][$key] = $val;
281    }
282
283    function getValue($key, $productTypeId) {
284        return $this->cartSession[$productTypeId][$key];
285    }
286
287    function getCartList($productTypeId) {
288        $objProduct = new SC_Product();
289        $max = $this->getMax($productTypeId);
290        $arrRet = array();
291        for($i = 0; $i <= $max; $i++) {
292            if(isset($this->cartSession[$productTypeId][$i]['cart_no'])
293               && $this->cartSession[$productTypeId][$i]['cart_no'] != "") {
294
295                if (SC_Utils_Ex::isBlank($this->cartSession[$productTypeId][$i]['productsClass'])) {
296                    $this->cartSession[$productTypeId][$i]['productsClass'] =&
297                            $objProduct->getDetailAndProductsClass(
298                                    $this->cartSession[$productTypeId][$i]['id']);
299                }
300
301                $price = $this->cartSession[$productTypeId][$i]['productsClass']['price02'];
302                $this->cartSession[$productTypeId][$i]['price'] = $price;
303
304                $this->cartSession[$productTypeId][$i]['point_rate'] =
305                        $this->cartSession[$productTypeId][$i]['productsClass']['point_rate'];
306
307
308                $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
309                $pre_tax = SC_Helper_DB_Ex::sfPreTax($price);
310                $total = $pre_tax * $quantity;
311
312                $this->cartSession[$productTypeId][$i]['total_pretax'] = $total;
313
314                $arrRet[] =& $this->cartSession[$productTypeId][$i];
315            }
316        }
317        return $arrRet;
318    }
319
320    /**
321     * すべてのカートの内容を取得する.
322     *
323     * @return array すべてのカートの内容
324     */
325    function getAllCartList() {
326        $results = array();
327        $cartKeys = $this->getKeys();
328        $i = 0;
329        foreach ($cartKeys as $key) {
330            $cartItems = $this->getCartList($key);
331            foreach (array_keys($cartItems) as $itemKey) {
332                $cartItem =& $cartItems[$itemKey];
333                $results[$key][$i] =& $cartItem;
334                $i++;
335            }
336        }
337        return $results;
338    }
339
340    // カート内にある商品IDを全て取得する
341    function getAllProductID($productTypeId) {
342        $max = $this->getMax($productTypeId);
343        for($i = 0; $i <= $max; $i++) {
344            if($this->cartSession[$productTypeId][$i]['cart_no'] != "") {
345                $arrRet[] = $this->cartSession[$productTypeId][$i]['id'][0];
346            }
347        }
348        return $arrRet;
349    }
350
351    function delAllProducts($productTypeId) {
352        $max = $this->getMax($productTypeId);
353        for($i = 0; $i <= $max; $i++) {
354            unset($this->cartSession[$productTypeId][$i]);
355        }
356    }
357
358    // 商品の削除
359    function delProduct($cart_no, $productTypeId) {
360        $max = $this->getMax($productTypeId);
361        for($i = 0; $i <= $max; $i++) {
362            if($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) {
363                unset($this->cartSession[$productTypeId][$i]);
364            }
365        }
366    }
367
368    // 数量の増加
369    function upQuantity($cart_no, $productTypeId) {
370        $quantity = $this->getQuantity($cart_no, $productTypeId);
371        if (strlen($quantity + 1) <= INT_LEN) {
372            $this->setQuantity($quantity + 1, $cart_no, $productTypeId);
373        }
374    }
375
376    // 数量の減少
377    function downQuantity($cart_no, $productTypeId) {
378        $quantity = $this->getQuantity($cart_no, $productTypeId);
379        if ($quantity > 1) {
380            $this->setQuantity($quantity - 1, $cart_no, $productTypeId);
381        }
382    }
383
384    function getQuantity($cart_no, $productTypeId) {
385        $max = $this->getMax($productTypeId);
386        for ($i = 0; $i <= $max; $i++) {
387            if ($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) {
388                return $this->cartSession[$productTypeId][$i]['quantity'];
389            }
390        }
391    }
392
393    function setQuantity($quantity, $cart_no, $productTypeId) {
394        $max = $this->getMax($productTypeId);
395        for ($i = 0; $i <= $max; $i++) {
396            if ($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) {
397                $this->cartSession[$productTypeId][$i]['quantity'] = $quantity;
398            }
399        }
400    }
401
402    function getProductClassId($cart_no, $productTypeId) {
403        for ($i = 0; $i <= $max; $i++) {
404            if ($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) {
405                return $this->cartSession[$productTypeId][$i]['id'];
406            }
407        }
408    }
409
410    /**
411     * カート内の商品の妥当性をチェックする.
412     *
413     * エラーが発生した場合は, 商品をカート内から削除又は数量を調整し,
414     * エラーメッセージを返す.
415     *
416     * 1. 削除/非表示商品のチェック
417     * 2. 商品購入制限数のチェック
418     * 3. 在庫数チェック
419     *
420     * @param string $key 商品種別ID
421     * @return string エラーが発生した場合はエラーメッセージ
422     */
423    function checkProducts($productTypeId) {
424        $objProduct = new SC_Product();
425        $tpl_message = "";
426
427        // カート内の情報を取得
428        $items = $this->getCartList($productTypeId);
429        foreach (array_keys($items) as $key) {
430            $item =& $items[$key];
431            $product =& $item['productsClass'];
432            /*
433             * 表示/非表示商品のチェック
434             */
435            if (SC_Utils_Ex::isBlank($product)) {
436                $this->delProduct($item['cart_no'], $productTypeId);
437                $tpl_message .= "※ 現時点で販売していない商品が含まれておりました。該当商品をカートから削除しました。\n";
438            }
439
440            /*
441             * 商品購入制限数, 在庫数のチェック
442             */
443            $limit = $objProduct->getBuyLimit($product);
444            if (!is_null($limit) && $item['quantity'] > $limit) {
445                if ($limit > 0) {
446                    $this->setProductValue($item['id'], 'quantity', $limit, $productTypeId);
447                    $tpl_message .= "※「" . $product['name'] . "」は販売制限(または在庫が不足)しております。一度に数量{$limit}以上の購入はできません。\n";
448                } else {
449                    $this->delProduct($item['cart_no'], $productTypeId);
450                    $tpl_message .= "※「" . $product['name'] . "」は売り切れました。\n";
451                    continue;
452                }
453            }
454        }
455        return $tpl_message;
456    }
457
458    /**
459     * カートの内容を計算する.
460     *
461     * カートの内容を計算し, 下記のキーを保持する連想配列を返す.
462     *
463     * - tax: 税額
464     * - subtotal: カート内商品の小計
465     * - deliv_fee: カート内商品の合計送料
466     * - total: 合計金額
467     * - payment_total: お支払い合計
468     * - add_point: 加算ポイント
469     *
470     *  TODO ダウンロード商品のみの場合の送料を検討する
471     *  TODO 使用ポイント, 配送都道府県, 支払い方法, 手数料の扱いを検討
472     *
473     * @param integer $productTypeId 商品種別ID
474     * @param SC_Customer $objCustomer ログイン中の SC_Customer インスタンス
475     * @param integer $use_point 今回使用ポイント
476     * @param integer $deliv_pref 配送先都道府県ID
477     * @param integer $payment_id 支払い方法ID
478     * @param integer $charge 手数料
479     * @return array カートの計算結果の配列
480     */
481    function calculate($productTypeId, &$objCustomer = null, $use_point = 0,
482                       $deliv_pref = "", $payment_id = "", $charge = 0) {
483        $objDb = new SC_Helper_DB_Ex();
484
485        $total_point = $this->getAllProductsPoint($productTypeId);
486        $results['tax'] = $this->getAllProductsTax($productTypeId);
487        $results['subtotal'] = $this->getAllProductsTotal($productTypeId);
488        $results['deliv_fee'] = 0;
489
490        // 商品ごとの送料を加算
491        if (OPTION_PRODUCT_DELIV_FEE == 1) {
492            $cartItems = $this->getCartList($productTypeId);
493            foreach ($cartItems as $item) {
494                $results['deliv_fee'] += $item['deliv_fee'] * $item['quantity'];
495            }
496        }
497
498        // 配送業者の送料を加算
499        if (OPTION_DELIV_FEE == 1) {
500            $results['deliv_fee'] += $objDb->sfGetDelivFee(
501                                             array('deliv_pref' => $deliv_pref,
502                                                   'payment_id' => $payment_id));
503        }
504
505        // 送料無料の購入数が設定されている場合
506        if (DELIV_FREE_AMOUNT > 0) {
507            // 商品の合計数量
508            $total_quantity = $this->getTotalQuantity($productTypeId);
509
510            if($total_quantity >= DELIV_FREE_AMOUNT) {
511                $results['deliv_fee'] = 0;
512            }
513        }
514
515        // 送料無料条件が設定されている場合
516        $arrInfo = $objDb->sf_getBasisData();
517        if($arrInfo['free_rule'] > 0) {
518            // 小計が無料条件を超えている場合
519            if($results['subtotal'] >= $arrInfo['free_rule']) {
520                $results['deliv_fee'] = 0;
521            }
522        }
523
524        // 合計を計算
525        $results['total'] = $results['subtotal'];
526        $results['total'] += $results['deliv_fee'];
527        $results['total'] += $charge;
528
529        // お支払い合計
530        $results['payment_total'] = $results['total'] - $use_point * POINT_VALUE;
531
532        // 加算ポイントの計算
533        if (USE_POINT !== false) {
534            $results['add_point'] = SC_Helper_DB_Ex::sfGetAddPoint($total_point,
535                                                                   $use_point);
536            if($objCustomer != "") {
537                // 誕生日月であった場合
538                if($objCustomer->isBirthMonth()) {
539                    $results['birth_point'] = BIRTH_MONTH_POINT;
540                    $results['add_point'] += $results['birth_point'];
541                }
542            }
543            if($results['add_point'] < 0) {
544                $results['add_point'] = 0;
545            }
546        }
547        return $results;
548    }
549
550    function getKeys() {
551        return array_keys($this->cartSession);
552    }
553
554    function registerKey($key) {
555        $_SESSION['cartKey'] = $key;
556    }
557
558    function unsetKey() {
559        unset($_SESSION['cartKey']);
560    }
561}
562?>
Note: See TracBrowser for help on using the repository browser.