<?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: */

/**
 * [[機能説明]]
 *
 * @package     [[package名]]
 * @author      Your Name <mail@example.com>
 * @access      public
 */
class Customer_Mypage_Seller_Insert_Done
{
    var $actionChain;
    var $seller_db;
    var $seller_lgc;
    var $seller;
    var $seller_serial;
    var $payinfo;
    var $session;
    var $l_mail;
    var $memberview;
    
    var $c_pref;
    var $auth;
    
//    var $sellerstructure; //出品者情報構成
//    var $structureJson_serial;   //出品者情報構成のJSON文字列
    
    /**
     * [[機能説明]]
     *
     * @access  public
     */
    function execute()
    {
        // 支払先をカードのみに
        $this -> seller -> paymode = "account";
        $errorList =& $this->actionChain->getCurErrorList();
        $log =& LogFactory::getLog();
        $log->debug("Customer_Mypage_Seller_Insert_Doneが実行されました", "Customer_Mypage_Seller_Insert_Done#execute");

        // カードチェック
        if (INPUT_CARD_INFO) {
            $check = $this->seller_lgc->chackEnabledCard($this->auth->getAuthData("membercd"),
                                                         $this->seller->card_num,
                                                         $this->seller->card_limit_y,
                                                         $this->seller->card_limit_m);
            if($check['MStatus'] != 'success') {
                $errorList -> add(1, "このカードは使えません。");
                return 'error';
            }
        }
        
        
        $res = $this -> insertDB();
        if($res) {
            $res = $this->sendMail();
            if (!$res) $log->info("error", "seller sendmail");
            return 'success';
        } else {
            return 'input';
        }
    }
    
    function setSeller_serial($value)
    {
        $value = urldecode($value);
        $this -> seller_serial = $value;
        $this -> seller = unserialize($value);
    }

    function insertDB()
    {
        if($this -> seller == ""){
            return false;
        } else {
//支払方法が口座で、なおかつ銀行名・支店名・口座番号・口座名義の各値が存在しなければ
//入金先と支払先が同じであると判断する。
            $this -> checkAccount();
            $this -> checkPaymode();
            
            //すでに登録済みであれば上書きする
            $memberCd = $this -> auth -> getAuthData("membercd");
            $username = $this -> auth -> getAuthData("membercd");
            if($this -> seller_db -> checkDuplicate($memberCd)){
                $res = $this -> seller_db -> update($memberCd, $this -> seller, $this -> payinfo, $username);
            } else {
                $res = $this -> seller_db -> insert($memberCd, $this -> seller, $this -> payinfo, $username);
            }
            
            if($res == false){
                return false;
            } else {
                return true;
            }
        }
    }

    function checkAccount()
    {
        $this -> payinfo = 1;
        if($this -> seller -> paymode != 'account')   $this -> payinfo = 2;
        if($this -> seller -> pay_bank != "")         $this -> payinfo = 2;
        if($this -> seller -> pay_branch != "")       $this -> payinfo = 2;
        if($this -> seller -> pay_account_num != "")  $this -> payinfo = 2;
        if($this -> seller -> pay_account_name != "") $this -> payinfo = 2;
        
        if($this -> payinfo == 1){
            $this -> seller -> pay_bank         = $this -> seller -> receive_bank;
            $this -> seller -> pay_branch       = $this -> seller -> receive_branch;
            $this -> seller -> pay_account_num  = $this -> seller -> receive_account_num;
            $this -> seller -> pay_account_name = $this -> seller -> receive_account_name;
        }
        
    }
    
    function checkPaymode()
    {
        if($this->seller->paymode == "account"){
            $this -> seller -> card_num      = null;
            $this -> seller -> card_name     = null;
            $this -> seller -> card_limit_y  = null;
            $this -> seller -> card_limit_m  = null;
            $this -> seller -> card_sec_code = null;
        } else {
            $this -> seller -> pay_bank         = null;
            $this -> seller -> pay_branch       = null;
            $this -> seller -> pay_account      = null;
            $this -> seller -> pay_account_num  = null;
            $this -> seller -> pay_account_name = null;
        }
    }
    
    function getPrefData()
    {
        //県データを変数に取っておく
        $this->preflist = $this->c_pref->selectAll();
        $res = $this->preflist;
        return $res;
    }
    
    function prefCdToPrefName($prefcd)
    {
        if (is_array($prefcd)) $prefcd = $prefcd["code"];
        
        $prefname = $this->c_pref->selectKey($prefcd);
        $res = $prefname['prefnm'];
        return $res;
    }
    
    function setSeller(&$value)
    {
        $this -> seller =& $value;
    }
    
    function getSeller()
    {
        return $this -> seller;
    }
    
    
    function getSeller_serial()
    {
        return urlencode(serialize($this->seller));
    }
    
    function setSellerInfo($memberCd)
    {
        $sellerInfo = $this -> seller_db -> getSellerInfo($memberCd);
        if($sellerInfo === false) return false;
        
        //前処理
        $postalCode = split("-", $sellerInfo['deppostalcd']);
        $phoneNo    = split("-", $sellerInfo['deptelno1']);
        //$news       = mberegi_replace("< *br */? *>", "",  $sellerInfo['news']);
        
        $this -> seller -> post_code_a = $postalCode[0];
        $this -> seller -> post_code_b = $postalCode[1];
        $this -> seller -> pref        = $sellerInfo['depprefcd'];
        $this -> seller -> address1    = $sellerInfo['depaddr1'];
        $this -> seller -> address2    = $sellerInfo['depaddr2'];
        $this -> seller -> phone_a     = $phoneNo[0];
        $this -> seller -> phone_b     = $phoneNo[1];
        $this -> seller -> phone_c     = $phoneNo[2];
        //$this -> seller -> destination = $sellerInfo['depaddressee'];

        $this -> seller -> receive_bank = $sellerInfo['depbanknm'];
        $this -> seller -> receive_branch = $sellerInfo['depbranchnm'];
        $this -> seller -> receive_account = $sellerInfo['depaccount'];
        $this -> seller -> receive_account_num = $sellerInfo['depaccountno'];
        $this -> seller -> receive_account_name = $sellerInfo['depaccountnm'];
        
        $this -> seller -> paymode = $sellerInfo['payinfo'];
        if($this -> seller -> paymode == 2){
            $this -> seller -> pay_bank = $sellerInfo['paybanknm'];
            $this -> seller -> pay_branch = $sellerInfo['paybranchnm'];
            $this -> seller -> pay_account = $sellerInfo['payaccount'];
            $this -> seller -> pay_account_num = $sellerInfo['payaccountno'];
            $this -> seller -> pay_account_name = $sellerInfo['payaccountnm'];
        }
        
        $this -> seller -> card_type = $sellerInfo['credittype'];
        $this -> seller -> card_name = $sellerInfo['creditaccountnm'];
        $this -> seller -> card_num = $sellerInfo['creditaccountno'];
        $this -> seller -> card_limit_y = $sellerInfo['creditexpireyear'];
        $this -> seller -> card_limit_m = $sellerInfo['creditexpiremonth'];
        $this -> seller -> card_sec_code = $sellerInfo['securitycd'];
        
        if($this -> seller -> pay_bank || $sellerInfo['paybanknm']){
            $this -> seller -> paymode = "account";
        } else {
            $this -> seller -> paymode = "card";
        }

        $this -> seller -> sellerdiv    = $sellerInfo['sellerdiv'];
        
        $this -> seller -> news = $sellerInfo['news'];

        $this -> seller -> updCnt = $sellerInfo['updcnt'];
        $this -> seller -> memberCd = $memberCd;
        
    }
    

    function sendMail()
    {
        $cd = $this->auth->getAuthData("membercd");
        $sellerInfo = $this->seller_db->getSellerInfo($cd);
        if($sellerInfo === false) return false;

        $memberdata = $this->memberview->getMemberData($cd);

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

        $pref = $this->c_pref->selectKey($sellerInfo['depprefcd']);
        switch ($sellerInfo['credittype']) {
            case 1:
            $cardtype = "VISA";
            break;
            case 2:
            $cardtype = "MASTER";
            break;
            case 3:
            $cardtype = "NICOS";
            break;
            case 4:
            $cardtype = "JCB";
            break;
            case 5:
            $cardtype = "アメックス";
            break;
        }

        $params = array("username"    => $memberdata["lastnm"]." ".
                                         $memberdata["firstnm"],
                        "post"        => $sellerInfo['deppostalcd'],
                        "address1"    => $pref["prefnm"].
                                         $sellerInfo['depaddr1'],
                        "address2"    => $sellerInfo['depaddr2'],
                        "tel1"        => $sellerInfo['deptelno1'],

                        "receive_bank"    => $sellerInfo['depbanknm'],
                        "receive_branch"  => $sellerInfo['depbranchnm'],
                        "receive_account" => ($sellerInfo['depaccount']==1) ?
                                             "普通" : "当座",
                        "receive_account_num"  => substr($sellerInfo['depaccountno'], -4)."（下４けた）",
                        "receive_account_name" => $sellerInfo['depaccountnm'],

                        "card_type"     => $cardtype,
                        "card_name"     => $sellerInfo['creditaccountnm'],
                        "card_num"      => substr($sellerInfo['creditaccountno'],-4)."（下４けた）",
                        "card_limit_y"  => $sellerInfo['creditexpireyear'],
                        "card_limit_m"  => $sellerInfo['creditexpiremonth'],
                        "card_sec_code" => $sellerInfo['securitycd'],

                        "sellerdiv"     => ($sellerInfo['sellerdiv']==1) ?
                                           "小口出品" : "大口出品",
                        "news"          => $sellerInfo['news']
                       );

        return $this -> l_mail -> sendFromTemplate("seller_complete", $mailaddress, $from, $params);
    }
    

    function getMemberInfo() {
        $cd = $this->auth->getAuthData("membercd");
        $memberdata = $this->memberview->getMemberData($cd);
        return $memberdata;
    }
}
?>
