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

Revision 20553, 22.0 KB checked in by kotani, 15 years ago (diff)

#1111 ([フロント]商品購入(お届け先指定):ダウンロード商品の場合でも表示されるケースがある)

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