<?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     Customer.Mypage.Consumer.Insert.Done
 * @author      Your Name <mail@example.com>
 * @access      public
 */
class Customer_Mypage_Consumer_Insert_Done
{
    var $session;
    var $conregist;
    var $conregist_serial;
    
    var $c_consumer;
    var $l_mail;
    var $c_pref;
    var $row;
    
    var $auth;
    
    var $from;
    var $shoppingToken;
    
    var $memberview;
    var $lastnm;
    var $firstnm;
    var $lastnmkana;
    var $firstnmkana;
    
    var $mobile;
    
    function setConregist($conregist)
    {
        $this -> conregist = $conregist;
    }
    
    function getConregist()
    {
        return $this -> conregist;
    }
    
    /**
     * execute
     *
     * @access  public
     */
    function execute()
    {
        $this->mobile = &Net_UserAgent_Mobile::isMobile();
        $this->shoppingToken = $this->session->getParameter("shoppingToken");
        //$data = $this->session->getParameter("conregist");
        

        
        if($this->c_consumer->checkDuplicate($this -> auth -> getAuthData("membercd"))){
            $this->row = $this->c_consumer->UpdateForBuyerInformation($this -> auth -> getAuthData("membercd"),
                                                    $this->conregist->zip[0],
                                                    $this->conregist->zip[1],
                                                    $this->conregist->pref,
                                                    $this->conregist->addr1,
                                                    $this->conregist->addr2,
                                                    $this->conregist->phone1,
                                                    $this->conregist->phone2,
                                                    $this->conregist->subzip[0],
                                                    $this->conregist->subzip[1],
                                                    $this->conregist->subpref,
                                                    $this->conregist->subaddr1,
                                                    $this->conregist->subaddr2,
                                                    $this->conregist->subphone,
                                                    $this->conregist->atena,
                                                    $this->conregist->updCnt,
                                                    $this->lastnm,
                                                    $this->firstnm,
                                                    $this->lastnmkana,
                                                    $this->firstnmkana);
              
        } else {
        
            $this->row = $this->c_consumer->InsertForBuyerInformation($this -> auth -> getAuthData("membercd"),
                                                    $this->conregist->zip[0],
                                                    $this->conregist->zip[1],
                                                    $this->conregist->pref,
                                                    $this->conregist->addr1,
                                                    $this->conregist->addr2,
                                                    $this->conregist->phone1,
                                                    $this->conregist->phone2,
                                                    $this->conregist->subzip[0],
                                                    $this->conregist->subzip[1],
                                                    $this->conregist->subpref,
                                                    $this->conregist->subaddr1,
                                                    $this->conregist->subaddr2,
                                                    $this->conregist->subphone,
                                                    $this->conregist->atena,
                                                    $this->lastnm,
                                                    $this->firstnm,
                                                    $this->lastnmkana,
                                                    $this->firstnmkana);
        }
        if (!$this->row) return 'input';
        
        $res = $this->sendMail();

        $log =& LogFactory::getLog();
        $log->debug("Customer_Mypage_Consumer_Insert_Doneが実行されました", "Customer_Mypage_Consumer_Insert_Done#execute");

        if (!$res) $log->info("error", "consumer sendmail");
        return 'success';
    }
    
    function setConregist_serial($value)
    {
        $value = urldecode($value);
        $this -> conregist_serial = $value;
        $this -> conregist = unserialize($value);
    }


    function sendMail()
    {
        $cd = $this -> auth -> getAuthData("membercd");
        $memberdata = $this->memberview->getMemberData($cd);

        $mailaddress = $memberdata["mailaddr"];
        $from        = MAIL_FROM;

        $pref = $this->c_pref->selectKey($this->conregist->pref);
        $subpref["prefnm"] = "";
        if (!empty($this->conregist->subpref)) 
            $subpref = $this->c_pref->selectKey($this->conregist->subpref);
        $tel1 =    $this->conregist->phone1[0] ."-".
                   $this->conregist->phone1[1] ."-".
                   $this->conregist->phone1[2];
        $tel2 =    $this->conregist->phone2[0] ."-".
                   $this->conregist->phone2[1] ."-".
                   $this->conregist->phone2[2];
        $subtel1 = $this->conregist->subphone[0] ."-".
                   $this->conregist->subphone[1] ."-".
                   $this->conregist->subphone[2];
        $addr2 = (!empty($this->conregist->addr2)) ? $this->conregist->addr2:"";
        $atena = (!empty($this->conregist->atena)) ? $this->conregist->atena:"";
        $subpost = $this->conregist->subzip[0]."-".
                   $this->conregist->subzip[1];
        $subaddr1 = (!empty($this->conregist->subaddr1)) ? 
                    $this->conregist->subaddr1:"";
        $subaddr2 = (!empty($this->conregist->subaddr2)) ? 
                    $this->conregist->subaddr2:"";

        $params = array("username"    => $this->lastnm." ".$this->firstnm,
                        "name_kana"   => $this->lastnmkana." ".
                                         $this->firstnmkana,
                        "post"        => $this->conregist->zip[0]."-".
                                         $this->conregist->zip[1],
                        "address1"    => $pref["prefnm"].
                                         $this->conregist->addr1,
                        "address2"    => $addr2,
                        "tel1"        => $tel1,
                        "tel2"        => $tel2,
                        "addressee"   => $atena,
                        "subpost"     => $subpost,
                        "subaddress1" => $subpref["prefnm"].
                                         $subaddr1,
                        "subaddress2" => $subaddr2,
                        "subtel1"     => $subtel1
                       );

        return $this -> l_mail -> sendFromTemplate("consumer_complete", $mailaddress, $from, $params);
    }
    
    function isMobile()
    {
        $mobile = &Net_UserAgent_Mobile::isMobile();
        return $mobile;
    }
}
?>
