<?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_Cashier_Payment
{
    var $title;
    var $pagename;

    var $session;
    var $select_address;
    var $auth;
    var $c_preorder;
    var $c_cart;
    var $deposit;

    var $membercd;
    var $item_array;
    var $sum_quantity;
    var $payment;
    var $takkyubin_chk;

    var $fix;
    var $from;
    var $back;
    var $paymentflg;

    var $dlitem;
    var $dlflg;
    var $price_free;

    /**
     * [[讖溯�隱ｬ譏讃]
     *
     * @access  public
     */
    function execute()
    {
        $mobile = &Net_UserAgent_Mobile::isMobile();
        $re_str = ($mobile) ? "mobile_" : "";

        $log =& LogFactory::getLog();
        $log->debug("Customer_Itembuy_Cashier_Payment縺悟ｮ溯｡後＆繧後∪縺励◆", "Customer_Itembuy_Cashier_Payment#execute");
        $this -> set_destination();

        if($this -> auth -> getAuthData("membercd")) {
            $this->membercd = $this->auth->getAuthData("membercd");
            //仮受注見出しテーブルに登録されているかどうかを確認
            $res = $this -> c_preorder -> checkPreordermember($this->membercd);
            if($res == false) {
                $this -> c_preorder -> insertMember($this->membercd);
            }

            //セッションからデータを取り出し、値があるかどうかを判別する
            $sessionItem = $this -> session -> getParameter('cart_items');
            if(is_null($sessionItem)) {
                $items = $this -> c_preorder -> selectAll($this->membercd);
                $this->paymentflg = false;
                foreach($items as $item) {
                    $this -> item_array[] = array($item['itemcd'], $item['structcd'], $item['stockcd'], $item['count']);
                }
            } else {
                $this -> item_array = $sessionItem;
            }

        } else {
            $this -> item_array = $this -> session -> getParameter('cart_items');
        }

        if($this -> item_array != NULL)
        {
            $this->dlflg = $this->dlflg_free = false;
            $souryou = array();
            for($i=0; $i<count($this -> item_array); $i++)
            {
//商品の購入数をItem_arrayに挿入
                if(isset($this -> quantity[$i])) $this -> item_array[$i][3] = $this -> quantity[$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 (!$this->paymentflg) { 
                        if (!empty($params["sellercd"]) || $params["reserveflg"] == 1) $this->paymentflg = true; 
                        if (SELLER_PAY_ETC) $this->paymentflg = false; 
                    } 

                    $this -> item_array[$i][4] = $params['itemnm'];
                    $this -> item_array[$i][5] = intval($params['sellingprice']);
                    $this -> sum_quantity += intval($this -> item_array[$i][3]) * intval($this -> item_array[$i][5]);

                    if (empty($params["sellernm"]) && $params["reserveflg"]==0) { // 在庫
                        //$souryou["normal"] = SITE_SOURYOU;
                        $souryou["normal"] = 0;
                    } else if (empty($params["sellernm"]) && $params["reserveflg"]==1) { // 予約
                        //$souryou["reserve"] += SITE_SOURYOU;
                        $souryou["reserve"] += 0;
                    } else { // 出品
                        $souryou[$params["sellercd"]] = $params["souryouprc"];
                    }

                    $data = $this->dlitem->getDownloadItem($this->item_array[$i][0], $this->item_array[$i][1]);
                    if ($data["fileexistence"]==="t") {
                        $this->dlflg = true;
                        if (!$this->paymentflg) { 
                            $this->paymentflg = true; 
                        } 
                    }
                }
            }
            $this->sum_quantity += array_sum($souryou);

            if (empty($this->sum_quantity)) {
                $this->price_free = true;
            }
        }

        return $re_str .'success';
    }

    function getBack() {
        $this->back = "destination";
        if ($this->fix) {
            if ($this->from == "skip") $this->back = "skip";
            else                   $this->back = "check";
        }
        return $this->back;
    }

    function getPaymentarray()
    {
        $data = $this -> session -> getParameter('payment');
        if ($data[0] == 1 && empty($data[1])) {
            $order = $this->deposit->getDepositFromOrder($this->membercd);
            $data[1] = $order["cardcompany"];
            $data[3] = $order["cardname"];
            $exp = explode("/", $order["cardexp"]);
            $data[4] = $exp[0];
            $data[5] = $exp[1];
	}
        return $data;
    }

    function set_destination()
    {
        if(isset($this -> select_address)) $this -> session -> setParameter('destination',$this -> select_address);
    }

    function getDt() { 
      return $this->payment->getMi($this->sum_quantity);
    }

    function getMiSig() {
      return $this->payment->getMiSig();
    }

    function getCVSSig() {
      return $this->payment->getCVSSig($this->sum_quantity);
    }
}
?>
