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

    var $c_consumer;
    var $c_member;
    var $c_pref;
    var $deposit;
    
    var $member_data;
    var $m;
    var $membercd;
    var $beforeorder;
    
    var $fix;
    var $home_address;
    var $send_address;
    var $session;
    var $auth;

    var $actionChain;

    var $from;
    var $back;
    
    var $memberview; 
    var $lastnm; 
    var $firstnm; 
    var $lastnmkana; 
    var $firstnmkana;

    var $c_preorder;
    var $c_cart;
    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_Destinationが実行されました", "Customer_Itembuy_Cashier_Destination#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);
        $this->setDl();
        $res = $this -> set_address();
        if($res){
            return $re_str .'success';
        } else {
            $errorList =& $this->actionChain->getCurErrorList();
            $errorList->add(1, "お届け先となるお客様の情報が未登録です。");
            $this->from = "cashier";

            $mem = $this->memberview->SelectMemberNm($this->membercd); 
            if (!isset($this->lastnm) &&  
                !isset($this->firstnm) &&  
                !isset($this->lastnmkana) &&  
                !isset($this->firstnmkana)) { 

                $this->lastnm      = $mem["lastnm"]; 
                $this->firstnm     = $mem["firstnm"]; 
                $this->lastnmkana  = $mem["lastnmkana"]; 
                $this->firstnmkana = $mem["firstnmkana"]; 
            } 

            return $re_str .'input';
        }
    }
    
    function setFix($value)
    {
        $this -> fix = $value;
    }
    
    function getAddress()
    {
        $destination = $this -> session -> getParameter('destination');
        if (!empty($destination)) {
            return $destination;
        }
        $order = $this->deposit->getDepositFromOrder($this->membercd);
        if(is_array($order)) {
            $addressee = (empty($this->member_data['addressee'])) ? $this->m["lastnm"] ." ". $this->m["firstnm"] : $this->member_data['addressee'];
            if ($this->member_data["postalcd"]==$order["sendpostalcd"] &&
                $this->member_data["prefcd"]==$order["sendprefcd"] &&
                $this->member_data["addr1"]==$order["sendaddr1"] && 
                $this->member_data["addr2"]==$order["sendaddr2"] &&
                $this->member_data["telno1"]==$order["sendtelno1"] &&
                $this->m["lastnm"]." ".$this->m["firstnm"]==$order["addressee"]) {
                return 1;
            } else if ($this->member_data["sendpostalcd"]==$order["sendpostalcd"] &&
                $this->member_data["sendprefcd"]==$order["sendprefcd"] &&
                $this->member_data["sendaddr1"]==$order["sendaddr1"] && 
                $this->member_data["sendaddr2"]==$order["sendaddr2"] &&
                $this->member_data["sendtelno1"]==$order["sendtelno1"] &&
                $addressee==$order["addressee"]) {
                return 2;
            }
	}
        return $destination;
    }

    function getHome_address()
    {
        return $this -> home_address;
    }
    
    function getSend_address()
    {
        return $this -> send_address;
    }
    
    function getNext()
    {
        if(isset($this -> fix)) {
            if ($this->from == "skip") return "customer_itembuy_cashier_skip";
            else                       return "customer_itembuy_cashier_check";
        } else {
            return "customer_itembuy_cashier_payment";
        }
    }

    function getBack() {
        $this->back = "cart";
        if ($this->fix) {
             if ($this->from == "skip") $this->back = "cashier_skip";
             else                       $this->back = "cashier_check";
	}
        return $this->back;
    }
  
    function set_address()
    {
        $pref = $this -> c_pref -> selectKey($this -> member_data['prefcd']);
        if($pref == "") {
            return false;
        } else {
            if($this->member_data['sendprefcd'] != "") {
                 $send_pref = $this -> c_pref -> selectKey($this -> member_data['sendprefcd']);
             } else {
                 $send_pref['prefnm'] = "";
             }

         $this -> home_address["post"]    = $this -> member_data['postalcd'];
         $this -> home_address["pref"]    = $pref['prefnm'];
         $this -> home_address["address1"] = $this -> member_data['addr1'];
         $this -> home_address["address2"] = $this -> member_data['addr2'];
         $this -> home_address["tel"]     = $this -> member_data['telno1'];
         $this -> home_address["addressee"] = $this->m['lastnm']." ".$this->m['firstnm'];

         $addressee = (empty($this->member_data['addressee'])) ? $this->m["lastnm"] ." ". $this->m["firstnm"] : $this->member_data['addressee'];
         $this -> send_address["send_post"]    = $this -> member_data['sendpostalcd'];
         $this -> send_address["send_pref"]    = $send_pref['prefnm'];
         $this -> send_address["send_address1"] = $this -> member_data['sendaddr1'];
         $this -> send_address["send_address2"] = $this -> member_data['sendaddr2'];
         $this -> send_address["send_tel"]     = $this -> member_data['sendtelno1'];
         $this -> send_address["addressee"] = $addressee;
         return true;
         }
     }

    function getConregist()
    {
        return "";
    }

    function getPrefData()
    {
        return $this->c_pref->selectAll();
    }

    function setDl()
    {
        //カートの中身取得
        $items = $this->c_preorder->selectAll($this->auth->getAuthData("membercd"));
        $i = 0;
        foreach($items as $item) {
            $item_array[] = array($item['itemcd'], $item['structcd'], $item['stockcd'], $item['count']);
        }
        $sumprice = 0;
        if($item_array != NULL)
        {
            $this->dlflg = $this->price_free = false;
            for($i=0; $i< count($item_array); $i++)
            {
//品名コード・構成コード・在庫コードを元にテーブルから商品名と価格を入手　Item_arrayに挿入
                $params = $this->c_cart->selectOne($item_array[$i][0], $item_array[$i][1], $item_array[$i][2]);
                if($params) {
                    $sumprice += $item['count'] * intval($params['sellingprice']);

                    $data = $this->dlitem->getDownloadItem($item_array[$i][0], $item_array[$i][1]);
                    if ($data["fileexistence"]==="t") {
                        $this->dlflg = true;
                    }
                }
            }
            if (empty($sumprice)) {
                $this->price_free = true;
            }
        }
    }
}
?>
