<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */


//JSONを扱うためのパーサ
//require_once(WEBAPP_DIR."/JSON.php");

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
require_once 'Net/UserAgent/Mobile.php';

/**
 * [[機能説明]]
 *
 * @package     [[package名]]
 * @author      Your Name <mail@example.com>
 * @access      public
 */
class Customer_Itembuy_Checkstatus
{
    var $title;
    var $pagename;

    var $session;
    var $auth;
    var $c_preorder;
    var $c_cart;
    var $deposit;
    var $payment;
    var $dlitem;
    var $payment_array;
    var $item_array;
    var $sum_quantity;
    var $paymentflg;
    var $takkyubin_chk;

    var $takkyubin;
    var $quantity;

    /**
     * [[機能説明]]
     *
     * @access  public
     */
    function execute()
    {
        $log =& LogFactory::getLog();
        $log->debug("Customer_Itembuy_Checkstatusが実行されました", "Customer_Itembuy_Checkstatus#execute");

        if (isset($this->quantity)) {
            $items = $this->c_preorder->selectAll($this->auth->getAuthData("membercd"));
            $i = 0;
            foreach($items as $item) {
                if (is_numeric($this->quantity[$i]) && $this->quantity[$i] > 0) {
                    $data = $this->dlitem->getDownloadItem($item['itemcd'], $item['structcd']);
                    if ($data===FALSE || is_null($data)) {
                        $this->c_preorder->update($this->auth->getAuthData("membercd"), 0, $item['itemcd'], $item['structcd'], $item['stockcd'], $this->quantity[$i],"");
                        $i++;
                    } else {
                        // ダウンロード商品は購入数を「1」で固定
                        $this->c_preorder->update($this->auth->getAuthData("membercd"), 0, $item['itemcd'], $item['structcd'], $item['stockcd'], 1,"");
                    }
                }
            }
        }

        //カートの中身取得
        $items = $this->c_preorder->selectAll($this->auth->getAuthData("membercd"));
        if (count($items)<=0) {
            return "success5";
        }
        $this->paymentflg = false;
        foreach($items as $item) {
            $this -> item_array[] = array($item['itemcd'], $item['structcd'], $item['stockcd'], $item['count']);
        }
        if($this -> item_array != NULL)
        {
            for($i=0; $i<count($this -> item_array); $i++)
            {
//品名コード・構成コード・在庫コードを元にテーブルから商品名と価格を入手　Item_arrayに挿入
                $params = $this -> c_cart -> selectOne($this -> item_array[$i][0], $this -> item_array[$i][1], $this -> item_array[$i][2]);
                if($params) {
                    //購入できない商品がある
                    if (empty($params['itemnm']) && empty($params['totalprice'])) return 'success5';
                    //--

                    $data = $this->dlitem->getDownloadItem($this->item_array[$i][0], $this->item_array[$i][1]);
                    if ($data!==FALSE && !is_null($data)) {
                        $mobile = &Net_UserAgent_Mobile::isMobile();
                        if ($mobile) return 'success5';
                    }

                    if ($data!==FALSE && !is_null($data) && $data["fileexistence"]==="f") {
                        return 'success5';
                    }

                    if (!$this->paymentflg) { 
                        if (!empty($params["sellercd"]) || $params["reserveflg"] == 1) $this->paymentflg = true; 
                        if (SELLER_PAY_ETC) $this->paymentflg = false; 
                        // ダウンロード商品は、カード決済のみ 
                        if ($data!==FALSE && !is_null($data) && $data["fileexistence"]==="t") $this->paymentflg = true; 
                    }

                    $this -> item_array[$i][4] = $params['itemnm'];
                    $this -> item_array[$i][5] = intval($params['totalprice']);

                    $this -> sum_quantity += intval($this -> item_array[$i][3]) * intval($this -> item_array[$i][5]);
                }
            }
        }

        $res = $this -> check_member_status();
        switch($res)
        {
//非会員の場合
            case 1:
                return 'success1';
                break;
//購入者情報未登録の場合
            case 2:
                return 'success2';
                break;
//購入者情報登録済みで購入初回の場合
            case 3:
                return 'success3';
                break;
//購入2回目以降の場合
            case 4:
                return 'success4';
                break;
        }
    }

    function check_member_status()
    {
        $membercd = $this->auth->getAuthData("membercd");

        $this->session->removeParameter("payment");

        if(empty($membercd))
        {
            return 1;
        }
        else
        {
            // 決済会社との契約で、カードの再利用ができない場合
            if (CARD_REPAYMENT==FALSE) {
                return 4;
            }
            
            $order = $this->deposit->getDepositFromOrder($membercd);
            if(is_array($order))
            {
                $payment_array = array();
                $payment_array[6] = $this->payment->getMi($this->sum_quantity);
                $payment_array[7] = $this->payment->getMiSig();
                $payment_array[8] = $this->sum_quantity;

                switch ($order["depositdiv"]) {
                    case 1:
                    $payment_array[0] = 1;
                    break;

                    case 2:  // コンビニ決済は、2回目以降でも入力
                    return 4;
                    break;

                    case 3:
                    if ($this->paymentflg) return 4;
                    $payment_array[0] = 2;
                    break;

                    case 4:
                    if ($this->paymentflg) return 4;
                    $payment_array[0] = 3;
                    break;

                    case 5:
                    if ($this->paymentflg) return 4;
                    $payment_array[0] = 5;
                    break;

                }
                $s = $this->session->getParameter('payment');
                switch ($s[0]) {
                    case 2: // 代引
                    case 3: // 銀行
                    if ($this->paymentflg && !SELLER_PAY_ETC) return 4; 
                    break;

                    case 4:   // コンビニ
                    return 4; // 2回目以降の購入でも、再度入力してもらう
                    break;
                }
                if (empty($s)) {
                    $this->session->setParameter('payment',$payment_array);
                }


                return 3;
            } else {
                return 4;
            }
        }
    }
}
?>
