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

Revision 20112, 21.8 KB checked in by nanasess, 15 years ago (diff)

#990(配送設定・支払方法設定の仕様変更)

  • 支払方法設定ページ
  • 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        $this->registerKey($productTypeId);
54    }
55
56    // 商品購入中の変更があったかをチェックする。
57    function getCancelPurchase($productTypeId) {
58        $ret = isset($this->cartSession[$productTypeId]['cancel_purchase'])
59            ? $this->cartSession[$productTypeId]['cancel_purchase'] : "";
60        $this->cartSession[$productTypeId]['cancel_purchase'] = false;
61        return $ret;
62    }
63
64    // 購入処理中に商品に変更がなかったかを判定
65    function checkChangeCart($productTypeId) {
66        $change = false;
67        $max = $this->getMax($productTypeId);
68        for($i = 1; $i <= $max; $i++) {
69            if ($this->cartSession[$productTypeId][$i]['quantity']
70                != $_SESSION[$this->key_tmp][$i]['quantity']) {
71
72                $change = true;
73                break;
74            }
75            if ($this->cartSession[$productTypeId][$i]['id']
76                != $_SESSION[$this->key_tmp][$i]['id']) {
77
78                $change = true;
79                break;
80            }
81        }
82        if ($change) {
83            // 一時カートのクリア
84            unset($_SESSION[$this->key_tmp]);
85            $this->cartSession[$productTypeId][$key]['cancel_purchase'] = true;
86        } else {
87            $this->cartSession[$productTypeId]['cancel_purchase'] = false;
88        }
89        return $this->cartSession[$productTypeId]['cancel_purchase'];
90    }
91
92    // 次に割り当てるカートのIDを取得する
93    function getNextCartID($productTypeId) {
94        foreach($this->cartSession[$productTypeId] as $key => $val){
95            $arrRet[] = $this->cartSession[$productTypeId][$key]['cart_no'];
96        }
97        return (max($arrRet) + 1);
98    }
99
100    /**
101     * 商品ごとの合計価格
102     * XXX 実際には、「商品」ではなく、「カートの明細行(≒商品規格)」のような気がします。
103     *
104     * @param integer $id
105     * @return string 商品ごとの合計価格(税込み)
106     * @deprecated SC_CartSession::getCartList() を使用してください
107     */
108    function getProductTotal($id, $productTypeId) {
109        $max = $this->getMax($productTypeId);
110        for($i = 0; $i <= $max; $i++) {
111            if(isset($this->cartSession[$productTypeId][$i]['id'])
112               && $this->cartSession[$productTypeId][$i]['id'] == $id) {
113
114                // 税込み合計
115                $price = $this->cartSession[$productTypeId][$i]['price'];
116                $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
117                $incTax = SC_Helper_DB_Ex::sfCalcIncTax($price);
118                $total = $incTax * $quantity;
119                return $total;
120            }
121        }
122        return 0;
123    }
124
125    // 値のセット
126    function setProductValue($id, $key, $val, $productTypeId) {
127        $max = $this->getMax($productTypeId);
128        for($i = 0; $i <= $max; $i++) {
129            if(isset($this->cartSession[$productTypeId][$i]['id'])
130               && $this->cartSession[$productTypeId][$i]['id'] == $id) {
131                $this->cartSession[$productTypeId][$i][$key] = $val;
132            }
133        }
134    }
135
136    // カート内商品の最大要素番号を取得する。
137    function getMax($productTypeId) {
138        $max = 0;
139        if (count($this->cartSession[$productTypeId]) > 0){
140            foreach($this->cartSession[$productTypeId] as $key => $val) {
141                if (is_numeric($key)) {
142                    if($max < $key) {
143                        $max = $key;
144                    }
145                }
146            }
147        }
148        return ($max);
149    }
150
151    // カート内商品数の合計
152    function getTotalQuantity($productTypeId) {
153        $total = 0;
154        $max = $this->getMax($productTypeId);
155        for($i = 0; $i <= $max; $i++) {
156            $total+= $this->cartSession[$productTypeId][$i]['quantity'];
157        }
158        return $total;
159    }
160
161
162    // 全商品の合計価格
163    function getAllProductsTotal($productTypeId) {
164        // 税込み合計
165        $total = 0;
166        $max = $this->getMax($productTypeId);
167        for($i = 0; $i <= $max; $i++) {
168
169            if (!isset($this->cartSession[$productTypeId][$i]['price'])) {
170                $this->cartSession[$productTypeId][$i]['price'] = "";
171            }
172
173            $price = $this->cartSession[$productTypeId][$i]['price'];
174
175            if (!isset($this->cartSession[$productTypeId][$i]['quantity'])) {
176                $this->cartSession[$productTypeId][$i]['quantity'] = "";
177            }
178            $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
179
180            $incTax = SC_Helper_DB_Ex::sfCalcIncTax($price);
181            $total+= ($incTax * $quantity);
182        }
183        return $total;
184    }
185
186    // 全商品の合計税金
187    function getAllProductsTax($productTypeId) {
188        // 税合計
189        $total = 0;
190        $max = $this->getMax($productTypeId);
191        for($i = 0; $i <= $max; $i++) {
192            $price = $this->cartSession[$productTypeId][$i]['price'];
193            $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
194            $tax = SC_Helper_DB_Ex::sfTax($price);
195            $total+= ($tax * $quantity);
196        }
197        return $total;
198    }
199
200    // 全商品の合計ポイント
201    function getAllProductsPoint($productTypeId) {
202        // ポイント合計
203        $total = 0;
204        if (USE_POINT !== false) {
205            $max = $this->getMax($productTypeId);
206            for($i = 0; $i <= $max; $i++) {
207                $price = $this->cartSession[$productTypeId][$i]['price'];
208                $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
209
210                if (!isset($this->cartSession[$productTypeId][$i]['point_rate'])) {
211                    $this->cartSession[$productTypeId][$i]['point_rate'] = "";
212                }
213                $point_rate = $this->cartSession[$productTypeId][$i]['point_rate'];
214
215                if (!isset($this->cartSession[$productTypeId][$i]['id'][0])) {
216                    $this->cartSession[$productTypeId][$i]['id'][0] = "";
217                }
218                $id = $this->cartSession[$productTypeId][$i]['id'][0];
219                $point = SC_Utils_Ex::sfPrePoint($price, $point_rate, POINT_RULE, $id);
220                $total+= ($point * $quantity);
221            }
222        }
223        return $total;
224    }
225
226    // カートへの商品追加
227    function addProduct($id, $quantity, $productTypeId) {
228        $find = false;
229        $max = $this->getMax($productTypeId);
230        for($i = 0; $i <= $max; $i++) {
231
232            if($this->cartSession[$productTypeId][$i]['id'] == $id) {
233                $val = $this->cartSession[$productTypeId][$i]['quantity'] + $quantity;
234                if(strlen($val) <= INT_LEN) {
235                    $this->cartSession[$productTypeId][$i]['quantity'] += $quantity;
236                }
237                $find = true;
238            }
239        }
240        if(!$find) {
241            $this->cartSession[$productTypeId][$max+1]['id'] = $id;
242            $this->cartSession[$productTypeId][$max+1]['quantity'] = $quantity;
243            $this->cartSession[$productTypeId][$max+1]['cart_no'] = $this->getNextCartID($productTypeId);
244        }
245    }
246
247    // 前頁のURLを記録しておく
248    function setPrevURL($url) {
249        // 前頁として記録しないページを指定する。
250        $arrExclude = array(
251            "/shopping/"
252        );
253        $exclude = false;
254        // ページチェックを行う。
255        foreach($arrExclude as $val) {
256            if(preg_match("|" . preg_quote($val) . "|", $url)) {
257                $exclude = true;
258                break;
259            }
260        }
261        // 除外ページでない場合は、前頁として記録する。
262        if(!$exclude) {
263            $_SESSION['prev_url'] = $url;
264        }
265    }
266
267    // 前頁のURLを取得する
268    function getPrevURL() {
269        return isset($_SESSION['prev_url']) ? $_SESSION['prev_url'] : "";
270    }
271
272    // キーが一致した商品の削除
273    function delProductKey($keyname, $val, $productTypeId) {
274        $max = count($this->cartSession[$productTypeId]);
275        for($i = 0; $i < $max; $i++) {
276            if($this->cartSession[$productTypeId][$i][$keyname] == $val) {
277                unset($this->cartSession[$productTypeId][$i]);
278            }
279        }
280    }
281
282    function setValue($key, $val, $productTypeId) {
283        $this->cartSession[$productTypeId][$key] = $val;
284    }
285
286    function getValue($key, $productTypeId) {
287        return $this->cartSession[$productTypeId][$key];
288    }
289
290    function getCartList($productTypeId) {
291        $objProduct = new SC_Product();
292        $max = $this->getMax($productTypeId);
293        $arrRet = array();
294        for($i = 0; $i <= $max; $i++) {
295            if(isset($this->cartSession[$productTypeId][$i]['cart_no'])
296               && $this->cartSession[$productTypeId][$i]['cart_no'] != "") {
297
298                if (SC_Utils_Ex::isBlank($this->cartSession[$productTypeId][$i]['productsClass'])) {
299                    $this->cartSession[$productTypeId][$i]['productsClass'] =&
300                            $objProduct->getDetailAndProductsClass(
301                                    $this->cartSession[$productTypeId][$i]['id']);
302                }
303
304                $price = $this->cartSession[$productTypeId][$i]['productsClass']['price02'];
305                $this->cartSession[$productTypeId][$i]['price'] = $price;
306
307                $this->cartSession[$productTypeId][$i]['point_rate'] =
308                        $this->cartSession[$productTypeId][$i]['productsClass']['point_rate'];
309
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();
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.