<?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 Admin_Ordermanage_Forwarding_Detail_Fwdetaildone
{
/*
  var $order_date_start;
  var $order_date_end;
  var $order_time_start;
  var $order_time_end;
  var $orderno_start;
  var $orderno_end;
  var $ordermember;
  var $mailaddr;
  var $state;
  var $display;
*/
  var $from;
  var $forward;
  var $order;
  var $l_mail;
  
  var $orderno;
  var $orderrowno;
  var $orderdate;
  var $enquiryno;
  
  var $upduser;
  var $ms;
  var $fromdetail;
  
    /**
     * [[機能説明]]
     *
     * @access  public
     */
    function execute()
    {

        $log =& LogFactory::getLog();
        $log->debug("Admin_Ordermanage_Order_Detail_Formが実行されました", "Ordermanage_Order_Detail_Form#execute");
        
        
        $today = date("Y-m-d");
        
        foreach($this->orderrowno as $key => $value) { 
            $fwstatevalue = $value{strlen($value)-1};
            //既に出荷済み、キャンセルまたは返品の場合→更新対象外
            if($fwstatevalue >= 4 ){
                $fwstate_noup[] = explode("-", $value);
            
            //出荷より下のステータスの場合→更新対象
            } elseif($fwstatevalue  < 4) {
                $fwstate_up[] = explode("-", $value);
            }
        }

        $status = 4 ;
        $reaso = "" ;
        
        foreach($fwstate_up as $key => $value) { 
            if(empty($fwstate_up[$key][1])){
                $fwstate_up[$key][1] = NULL;
            }
            $res = $this->forward->statusUpdate($this->orderno, $fwstate_up[$key][0], $today, $status, $reaso, $this->upduser, $fwstate_up[$key][1]);
            
        }
        if($res) $this->forward->updateEnquiryno($this->orderno, $this->enquiryno, $this->upduser);
        
        if(empty($fwstate_up) AND !empty($fwstate_noup)){
            $this->ms = "fwnoup";
        }else{
            $this->ms = "fwerror";
        }
        
        $this->fromdetail = "set";
        
        if (!$res){
            return "error";
        }else{
            $this->ms = "fwsuccess";
            $res = $this->sendMail();
            if (!$res) $log->info("send_item mail ", "error");
            return 'success';
        }
    }

    function sendMail()
    {
        $results   = $this->order->OrderdetailviewMail($this->orderno);
        
        foreach($results as $key => $value){
            if ($results[$key]["typediv"] == 3) $takkyubin = true;
            $type[]=$results[$key]["typediv"];
            $price[] = $results[$key]["price"];
        }
       
        foreach($type as $key => $value){
            if($value >= 1 and $value <= 3 || $value==10 || $value==11)
                $salesprice[] = $price[$key];
            else
                $otherprice[] = $price[$key];
        }
       
        $sumprice = array_sum($salesprice);
        
        $orderno   = $results[0]["orderno"];
        $orderdate = $results[0]["orderdate"];
        $lastnm    = $results[0]["lastnm"];
        $firstnm   = $results[0]["firstnm"];
        $sendbuyername   = $results[0]["sendbuyer"];
        $sendpostalcd = $results[0]["sendpostalcd"];
        $prefnm       = $results[0]["prefnm"];
        $sendaddr1    = $results[0]["sendaddr1"];
        $sendaddr2    = $results[0]["sendaddr2"];
        $sendtelno1   = $results[0]["sendtelno1"];
        $addressee    = $results[0]["addressee"];
        $deposit_statename  = $results[0]["depostate"];
        $mail           = (!empty($results[0]["mailaddr"])) ?
                           $results[0]["mailaddr"] : $results[0]["sendmailaddr"];
        $depositdiv = $results[0]["depositdiv"];
        $depositdivname = $results[0]["depositdivname"];
    
        $from        = MAIL_FROM;

        $params = array("username"        => (empty($results[0]["ordermember"])) ? $sendbuyername : $lastnm." ".$firstnm,
                        "takkyubin"       => $takkyubin,
                        "orderdate"       => $orderdate,
                        "orderno"         => $orderno,
                        "depositdiv"      => $depositdiv,
                        "depositdivname"  => $depositdivname,
                        "post"            => $sendpostalcd,
                        "address"         => $prefnm . $sendaddr1,
                        "address2"        => $sendaddr2,
                        "tel"             => $sendtelno1,
                        "addressee"       => $addressee,
                        "enquiryno"       => $this->enquiryno
                       );
        $params["totalprice"] = $sumprice;

        foreach ($results as $key => $val) {
            $itemlist = array();
            if ($val["typediv"]<>1) {
                if ($val["typediv"] == 2 or 
                    $val["typediv"] == 3 or
                    $val["typediv"] == 10 or
                    $val["typediv"] == 11) {
                    $itemlist["itemnm"] = $val["orderdetailtypename"];
                    $itemlist["price"]  = $val["price"];
                }
            } else {
                $itemlist["itemnm"] = $val["itemnm"];
                $itemlist["count"]  = $val["count"];
                $itemlist["price"]  = $val["price"];
            }
            $params["itemlist"][] = $itemlist;
        }
        return $this -> l_mail -> sendFromTemplate("send_item", $mail, $from, $params);
    }
}
?>
