<?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 Seller_Itemexhibit_Send_Mailcomplete
{
    
    var $c_itemexhibit;
    var $l_mail;           //メール送信ロジック
    var $c_member;         //メンバーテーブル用のクラス
    var $auth;

    
    var $checklist;  //売上済み未発送リストのチェックリスト
    var $orderrow;   //売上済み未発送リストのオーダーリスト
    var $memberlist; //売上済み未発送リストのメンバーリスト

    var $comment;

    var $preaction;
    
    /**
     * [[機能説明]]
     *
     * @access  public
     */
    function execute()
    {
        $log =& LogFactory::getLog();
        $log->debug("Seller_Itemexhibit_Send_Mailcompleteが実行されました", "Seller_Itemexhibit_Send_Mailcomplete#execute");
        foreach($this->checklist as $orderNo){           
            $this -> sendMail($orderNo);
        }
        if(is_array($this->orderrow)){
            $this->updateSendItemList();
        }
        return 'success';
    }

      function updateSendItemList()
      {
          if(count($this->checklist) > 0){
              $checklist = $this->checklist;
              $orderrow = $this->orderrow;
              foreach($orderrow as $orderNo => $orderRowNoArray){
                  foreach($checklist as $checkNo){
                      if($orderNo == $checkNo){
                          $this->c_itemexhibit->updateSendItem($checkNo,$orderrow[$checkNo]);
                      }
                  }
              }
          }
      }
    
    function sendMail($orderNo)
    {


        if ($this->preaction == "seller_itemexhibit_valuation_list" || 
            $this->preaction == "seller_itemexhibit_history_list") {


            // 購入者へメール

            //出品者情報の取得
            $memberinfo = $this->c_itemexhibit->getMemberInfo($this->auth->getAuthData("membercd"));
            
            //購入者メールアドレスの取得
            $buyermemberCd = $this->c_itemexhibit->getMemberCd($orderNo);
            if (!empty($buyermemberCd)) {
                $buyermemberinfo = $this->c_itemexhibit->getMemberInfo($buyermemberCd);
                $mailaddress = $buyermemberinfo["mailaddr"];
            } else {
                $buyermemberinfo = $this->c_itemexhibit->getMemberConsumer($orderNo);
                $mailaddress = $buyermemberinfo["sendmailaddr"];
            }

            $from        = MAIL_FROM;

            $params = array(
            "nicknm" => $memberinfo["nicknm"],
            "sellernm" => $memberinfo["lastnm"].$memberinfo["firstnm"],
            "sellermail" => $memberinfo["mailaddr"],
            "comment" => $this->comment,
            );

            $this->l_mail->sendFromTemplate("send_consumer", $mailaddress, $from, $params);

        } else {

          // 出品者 商品発送
            
            //購入者情報の取得
            $buyermemberCd = $this->c_itemexhibit->getMemberCd($orderNo);
            if (!empty($buyermemberCd)) {
                $buyermemberinfo = $this->c_itemexhibit->getMemberInfo($buyermemberCd);
            }
            $buyermemberConsumer = $this->c_itemexhibit->getMemberConsumer($orderNo);
            $price = $this->c_itemexhibit->getPrice($orderNo);
            $prefnm = $this->c_itemexhibit->getPref($buyermemberConsumer["sendprefcd"]);
            $depositdiv = $this->c_itemexhibit->getDepositdiv($orderNo);
            
            foreach($price as $kye => $value){
              if($price[$kye]["typediv"]=="1"){
                $p[] = $value["price"]; 
              }
              elseif($price[$kye]["typediv"]=="2"){
                $s[] = $value["price"];
              }
              elseif($price[$kye]["typediv"]=="3"){
                $t[] = $value["price"];
              }
              elseif($price[$kye]["typediv"]=="10"){
                $t[] = $value["price"];
              }
              elseif($price[$kye]["typediv"]=="11"){
                $t[] = $value["price"];
              }
            }
            
            $sumprice = array_sum($p);
            $sendprice = array_sum($s);
            $takkyuuprice = array_sum($t);
            
            $totalprice = $sumprice + $sendprice + $takkyuuprice;

            //出品者情報の取得
            $memberinfo = $this->c_itemexhibit->getMemberInfo($this->auth->getAuthData("membercd"));
            
//            $iteminfo = $this->c_itemexhibit->getSelledNotSendListByOrderNo($orderNo);
//            foreach ($iteminfo as $key => $val) {
//                $iteminfo[$key]["variationnm"] = $this->c_itemexhibit->getVariationNm($val["itemcd"], $val["structcd"]);
//            }

            if (!empty($buyermemberinfo)) {
                $mailaddress   = $buyermemberinfo["mailaddr"];
                $consumer_name = $buyermemberinfo["lastnm"].$buyermemberinfo["firstnm"];
            } else {
                $mailaddress   = $buyermemberConsumer["sendmailaddr"];
                $consumer_name = $buyermemberConsumer["sendbuyer"];
            }
            $from        = MAIL_FROM;
            
            $params = array(
            "nicknm" => $memberinfo["nicknm"],
            "sellernm" => $memberinfo["lastnm"].$memberinfo["firstnm"],
            "mailaddr" => $memberinfo["mailaddr"],
            "comment" => $this->comment,
            "username" => $consumer_name,
            "orderdate" => $buyermemberConsumer["orderdate"],
            "orderno" => $orderNo,
            "addressee" => $buyermemberConsumer["addressee"],
            "post" => $buyermemberConsumer["sendpostalcd"],
            "address" => $prefnm." ".$buyermemberConsumer["sendaddr1"],
            "address2" => $buyermemberConsumer["sendaddr2"],
            "tel" => $buyermemberConsumer["sendtelno1"],
            "totalprice" => $totalprice,
            "sendprice" => $sendprice,
            "takkyubin" => $takkyuuprice,
            "depositdivname" => $depositdiv,
            "itemlist" => $iteminfo
            );

            $this->l_mail->sendFromTemplate("sell_send_item", $mailaddress, $from, $params);

        }
        return true;

    }
    
    //売上済み未発送リスト用アクセッサ
    function getOrderrow()
    {
        return $this->orderrow;
    }

    function setOrderrow($orderrow)
    {
        $this->orderrow = $orderrow;
    }

    function getCheckList()
    {
        return $this->checklist;
    }

    function setCheckList($checklist)
    {
        $this->checklist = $checklist;
    }

}
?>
