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


/* 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_Skip
{
    var $title;
    var $pagename;
//paymentかdestinationからきたかを識別する変数
    var $from;
//destination　お届け先を自宅か送付先かを識別する変数　１は自宅　２は送付先
    var $select_address;

//payment　支払方法を識別する変数　１はカード　２は代金引換　３は振込み
    var $payment_radio;
//カード名　カード番号　カードの名義　カードの有効期限の年と月
    var $card_company;
    var $card_number;
    var $card_name;
    var $exp_year;
    var $exp_month;
    var $dt;
    var $MI_SIG;
    var $price;

    var $actionChain;
    var $session;
    var $auth;

    var $c_systeminfo;
    var $c_consumer;
    var $c_member;
    var $c_pref;
    var $member_data;
    var $m;
    var $membercd;
    var $c_preorder;
    var $c_cart;
    var $item_array;
    var $takkyubin_chk;
    var $pricedata;
    var $payment;
    var $deposit;
    var $totalprice;

    var $beforeorder;
    var $payment_data;

    var $buynote;
  
    var $dl_price_free;

    /**
     * [[機能説明]]
     *
     * @access  public
     */
    function execute()
    {
        $mobile = &Net_UserAgent_Mobile::isMobile();
        $re_str = ($mobile) ? "mobile_" : "";

        $log =& LogFactory::getLog();
        $log->debug("Customer_Itembuy_Cashier_Checkが実行されました", "Customer_Itembuy_Cashier_Check#execute");
        $this->membercd = $this->auth->getAuthData('membercd');
        $this->member_data = $this->c_consumer->select_one($this->membercd);
        $this->m = $this->c_member->select_one($this->membercd);

        $order = $this->deposit->getDepositFromOrder($this->membercd);
        if(!is_array($order)) {
            return "error";
        }
        $this->beforeorder = $order;
        $this->payment_data = $this->session->getParameter('payment');
        // チェックすり抜けのため
        $_POST["dt"] = $_GET["dt"] = $this->payment_data[6];
        $_POST["MI_SIG"] = $_GET["MI_SIG"] = $this->payment_data[7];
        $_POST["price"] = $_GET["price"] = $this->payment_data[8];
        //--

        $errorList =& $this->actionChain->getCurErrorList();
        if ($this->payment_data[0] == 1 && !$this->payment->checkSignature($this->payment_data[8])){
            $errorList->add(1, "エラーが発生しました。正しい情報を入力してください。");
            return $re_str ."input";
        }
        if ($this->membercd) {
            $r = $this->makeItemarray();
            if ($r=="error") return $r;
        }

        // 備考の初期値を取得
        $data = $this->c_systeminfo->selectAll();
        $this->buynote = $data[0]["buynote"];
        //--

        return $re_str .'success';
    }

    function makeItemarray() {
        $select_address = "";
        if (isset($this->select_address)) {
            $select_address = $this->select_address;
            $this->session->setParameter('destination', $this->select_address);
        } elseif ($this->session->getParameter('destination') != "") {
            $select_address = $this->session->getParameter('destination');
        }

        if($select_address == 1) {
            $this->payment->setSouryouDest($this->member_data['prefcd']);
        } elseif ($select_address == 2) {
            $this->payment->setSouryouDest($this->member_data['sendprefcd']);
        } else {
            $this->payment->setSouryouDest($this->beforeorder['sendprefcd']);
        }

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

        $items = $this->c_preorder->selectAll($this->membercd);

        $pa = $this->session->getParameter('payment');


        foreach($items as $item) {
            $item_array[] = array($item['itemcd'], $item['structcd'], $item['stockcd'], $item['count']);
        }

        if($item_array != NULL)
        {
            $r = $this->payment->makeItemArrayForCheck($item_array,
                                                       $this->item_array,
                                                       $this->pricedata,
                                                       $this->totalprice,
                                                       $this->paymentflg,
                                                       $this->sellerflg,
                                                       0);
        }

        // 代引手数料
        $p = $this->session->getParameter('payment');
        if ($p[0]==2) {
            foreach ($this->pricedata as $key => $val) { 
                if ($this->paymentflg) break; 
                if ($key == 0) { 
                    $daibiki_cost = $this->payment->getDaibikiCost($this->pricedata[$key]["totalprice"]); 

                    $this->pricedata[$key]["daibiki"] = $daibiki_cost; 
                    $this->pricedata[$key]["totalprice"] += $daibiki_cost; 
                    $this->totalprice += $daibiki_cost; 
                } else { 
                    foreach ($this->pricedata[$key] as $k => $v) { 
                        $daibiki_cost = $this->payment->getDaibikiCost($this->pricedata[$key][$k]["totalprice"]); 

                        $this->pricedata[$key][$k]["daibiki"] = $daibiki_cost; 
                        $this->pricedata[$key][$k]["totalprice"] += $daibiki_cost; 
                        $this->totalprice += $daibiki_cost; 
                    } 
                } 
            } 
        }
        //--
        // 郵便振替手数料
        if ($p[0]==5) {
            foreach ($this->pricedata as $key => $val) { 
                if ($this->paymentflg) break; 
                if ($key == 0) { 
                    $this->pricedata[$key]["yuubin"] = SITE_YUUBIN; 
                    $this->pricedata[$key]["totalprice"] += SITE_YUUBIN; 
                    $this->totalprice += SITE_YUUBIN; 
                } else { 
                    foreach ($this->pricedata[$key] as $k => $v) { 
                        $this->pricedata[$key][$k]["yuubin"] = SITE_YUUBIN; 
                        $this->pricedata[$key][$k]["totalprice"] += SITE_YUUBIN; 
                        $this->totalprice += SITE_YUUBIN; 
                    } 
                } 
            } 
        }

        $this->dl_price_free = false;
        if (isset($this->pricedata[3])) {
            if (empty($this->totalprice)) {
                $this->dl_price_free = true;
            }
        }
        return $r;
    }

    function setActionChain(&$value)
    {
        $this->actionChain =& $value;
    }

    function getCart_items()
    {
        return $this->item_array;
    }

    function getPaymentarray()
    {
        $data = array();
        $data[0] = $this->payment_data[0];
        if ($data[0] == 1) {
            $data[1] = $this->beforeorder["cardcompany"];
            $data[2] = $this->beforeorder["cardnumber"]."(下4桁のみ表示)";
            $data[3] = $this->beforeorder["cardname"];
            $exp = explode("/", $this->beforeorder["cardexp"]);
            $data[4] = $exp[0];
            $data[5] = $exp[1];
        }
        return $data;
    }

    function getDestination()
    {
        $select_address = "";
        if (isset($this->select_address)) {
            $select_address = $this->select_address;
            $this->session->setParameter('destination', $this->select_address);
        } elseif ($this->session->getParameter('destination') != "") {
            $select_address = $this->session->getParameter('destination');
        }

        if($select_address == 1) {
            $pref             = $this->c_pref->selectKey($this->member_data['prefcd']);
            $array["post"]    = $this->member_data['postalcd'];
            $array["pref"]    = $pref['prefnm'];
            $array["address1"] = $this->member_data['addr1'];
            $array["address2"] = $this->member_data['addr2'];
            $array["tel"]     = $this->member_data['telno1'];
            $array["addressee"] = $this->m["lastnm"]." ".$this->m["firstnm"];
        } elseif ($select_address == 2) {
            $send_pref = $this->c_pref->selectKey($this->member_data['sendprefcd']);
            $array["post"]     = $this->member_data['sendpostalcd'];
            $array["pref"]     = $send_pref['prefnm'];
            $array["address1"]  = $this->member_data['sendaddr1'];
            $array["address2"]  = $this->member_data['sendaddr2'];
            $array["tel"]      = $this->member_data['sendtelno1'];
            $array["addressee"] = (empty($this->member_data['addressee'])) ? $this->m["lastnm"] ." ". $this->m["firstnm"] : $this->member_data['addressee'];
        } else {
            $pref = $this->c_pref->selectKey($this->beforeorder['sendprefcd']);
            $array["post"]    = $this->beforeorder['sendpostalcd'];
            $array["pref"]    = $pref['prefnm'];
            $array["address1"] = $this->beforeorder['sendaddr1'];
            $array["address2"] = $this->beforeorder['sendaddr2'];
            $array["tel"]     = $this->beforeorder['sendtelno1'];
            $array["addressee"] = $this->beforeorder['addressee'];
	}

        return $array;
    }

//支払方法の入力値をSESSIONに保存する関数
    function set_payment()
    {
        $payment_array = array();
        if(isset($this->payment_radio))
        {
            $payment_array[0] = $this->payment_radio;
            $payment_array[6] = $this->dt;
            $payment_array[7] = $this->MI_SIG;
            $payment_array[8] = $this->price;
            if($this->payment_radio == 1)
            {
                $payment_array[1] = $this->card_company;
                $payment_array[2] = $this->card_number;
                $payment_array[3] = $this->card_name;
                $payment_array[4] = $this->exp_year;
                $payment_array[5] = $this->exp_month;
            }
            if($this->payment_radio == 4) // コンビニ
            {
                $payment_array[9]  = $this->cvs_shop;
                $payment_array[10] = $this->cvs_name1;
                $payment_array[11] = $this->cvs_name2;
                $payment_array[12] = $this->cvs_kana;
                $payment_array[13] = $this->cvs_tel;
            }
        }
        $this->session->setParameter('payment',$payment_array);
    }

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

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

    function getCardNumberHide() {
        $payment = $this->session->getParameter('payment');
        $cardnumber = $payment[2];
        $hide = "";
        if (!empty($cardnumber)) {
	  /*
            for ($i = 0; $i < strlen($cardnumber)-4; $i++) {
                $hide .= "*";
            }
	  */
            $hide .= substr($cardnumber, -4) ."(下4桁のみ表示)";
        }
        return $hide;
    }
}
?>
