<?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     Seller.Itemexhibit.Register.Complete
 * @author      S.Oguma <shinichiro.oguma@netnee.jp>
 * @access      public
 */
class Seller_Itemexhibit_Register_Complete
{

    var $stockview;
    var $auth;
    var $session;
    var $c_mypage;
    var $status;
    var $tname;
    var $memberview;
    var $l_mail;
    var $t_tag;
    var $dlitem;


    var $title;

    var $results;
    var $actionChain;

    var $c;           // 商品コード
    var $sc;     // 構成コード
    var $slrc;     // 出品者コード
    var $supplidiv;    // 仕入区分

    var $number; // 出品数
    var $condition;    // コンディション
    var $price; // 販売価格
    var $comment;      // 商品説明
    var $shopping;   // 送料

    var $t_condition;
    var $stockstate;   // 在庫状態
    var $costprice;    // 仕入価格
    var $upduser;      // 更新ユーザ

    var $page;

    var $from;
    var $stkc;
    
    var $res;
    var $view;
 
    /**
     * Actionの実行
     *
     * @access  public
     */
    function execute()
    {
        $log =& LogFactory::getLog();
        $func_name = "Seller_Itemexhibit_Register_Complete#execute";
        $log->debug("Seller_Itemexhibit_Register_Completeが実行されました", $func_name);

        $membercd = $this->auth->getAuthData("membercd");
        if (!empty($membercd)) {
            $this->status = $this->c_mypage->checkStatus($membercd);
        }

        $errorList =& $this->actionChain->getCurErrorList();
        if (!$this->status['scd'])
        {
            $errorList -> add(1, "出品者情報が登録されていません。");
            return 'invalidToken';
        }
        $this->slrc = $this->status['scd'];

        $dl = $this->dlitem->getDownloadItem($this->c, $this->sc);
//        if ($dl!==FALSE && !is_null($dl)) {
//            $errorList -> add(1, "出品できません");
//            return 'invalidToken';
//        }
        // ダウンロード商品のsellercdには9999を追加
        if (!empty($dl)) {
            $sellercd = '9999';
            $this->shopping = 0; // 送料0円
        } else {
            $sellercd = $this->slrc;
        }

        if (empty($this->price)) {
            $this->price = NULL; // 販売価格
        }
        if (empty($this->shopping)) {
            $this->shopping = 0;   // 送料
        }
        $this->supplidiv = NULL;    // 仕入区分
        $this->stockstate = "3";    // 納品
        $this->costprice = NULL;    // 仕入価格
        // 商品在庫登録
        $results = $this->stockview->insert_ex(
                   $this->c,           // 商品コード
                   $this->sc,     // 構成コード
                   $sellercd,     // 出品者コード
                   $this->supplidiv,    // 仕入区分
                   $this->number, // 出品数
                   $this->condition,    // コンディション
                   $this->price, // 販売価格
                   $this->shopping,   // 送料
                   $this->stockstate,   // 在庫状態
                   NULL, // 開始予定日
                   NULL,  // 完了予定日
                   $this->comment,      // 商品説明
                   $this->costprice,    // 仕入価格
                   $this->upduser);     // 更新ユーザ
                
        if (!$results) return "invalidToken";
        
        $this->stkc = $results;
        
        
        $this->t_condition = $this->condition;

        $res = $this->sendMail();
        if (!$res) $log->info("error", "exhibit_complete sendmail");

        return 'success';
    }

    function getItemData() {
        $results = $this->view->getStructData($this->c, $this->sc);
        $results["stock"] = $this->view->getStockDetail($this->c,
                                                        $this->sc,
                                                        "onedepo");

        $gnr = $this->view->getGenreForItemcd();
        foreach ($gnr[$this->c] as $val) $c[] = $val[0];
        $results["genre"] = $this->view->getGenreTree($c);
        $results["tags"] = $this->t_tag->getPopularTag(NULL, $this->c);

        return $results;
    }
    
    function getBasicOption() {
        return $this->itemupdate->getOptionList(100,200);
    }
    function getStructOption() {
        return $this->itemupdate->getOptionList(200,300);
    }


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

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

        $condition = $this->tname->selectKey(9, $this->condition);
        $item = $this->getItemData();

        $params = array("username"  => $memberdata["lastnm"]." ".
                                       $memberdata["firstnm"],
                        "itemnm"    => $item["itemnm"] ." ". $item["variationnm"],
                        "condition" => $condition["name"],
                        "comment"   => $this->comment,
                        "price"     => $this->price,
                        "souryou"   => $this->shopping,
                        "count"     => $this->number);


        return $this->l_mail->sendFromTemplate("exhibit_complete", $mailaddress, $from, $params);
    }
    
    function getConditionList()
    {
        return $this->tname->selectDivAll(9);
    }

    function getConditionName()
    {
        $condition = $this->tname->selectKey(9, $this->itemregist->condition);
        return $condition["name"];
    }

}
?>
