<?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_List_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 $l_order;
  var $l_mail;

  var $check;
  var $orderrowno;
  var $orderdate;
  var $enquiryno;
  
  var $odno;
  
  var $upduser;
  var $ms;


  var $order;
  
  var $stockstate_false;
  var $depositstate_false;
  var $forwardstate_false;
  
  
    /**
     * [[機能説明]]
     *
     * @access  public
     */
    function execute()
    {

        $log =& LogFactory::getLog();
        $log->debug("Admin_Ordermanage_Forwarding_List_Fwdetaildoneが実行されました", "Admin_Ordermanage_Forwarding_List_Fwdetaildone#execute");
        if(!$this->check){
            $this->ms = "nocheck";
            return "error";
        }
        foreach($this->check as $key => $value){ 
            $check[]= explode("-", $value);
        }

        
        foreach($check as $key => $value){
            if($check[$key][4] <> 3 ){                  //在庫状態が「3.納品(在庫)」かどうか
                $stockstate_false[] = $check[$key][0];
            }elseif($check[$key][5] < 20 and              //入金済みかどうか 
                    $check[$key][5] != 13 and
                    $check[$key][5] != 15){              // 代引は未入金
                $depositstate_false[] = $check[$key][0];
            }elseif($check[$key][3] == 4 or $check[$key][3] == 8 or $check[$key][3] == 9){    //出荷済み、もしくはキャンセル、返品でないか
                $forwardstate_false[] = $check[$key][0];
            }else{                                      //出荷済みにしてよいもの
                $fwodno_ture[] = $check[$key][0]; 
                $fwcont_ture[] = $check[$key][2];
            }
        }
        
        if(!empty($stockstate_false))    $this->stockstate_false   = $stockstate_false;
        if(!empty($depositstate_false))  $this->depositstate_false = $depositstate_false;
        if(!empty($forwardstate_false))  $this->forwardstate_false = $forwardstate_false;

        
        $today = date("Y-m-d");
        $state = 4 ;
                
        foreach($fwodno_ture as $key => $value){
            $res = $this->forward->UpdateRowcont($fwodno_ture[$key],$fwcont_ture[$key], $today, $state, $this->upduser);
            
            if ($res) {
                $this->forward->updateEnquiryno($fwodno_ture[$key], $this->enquiryno[$fwodno_ture[$key]], $this->upduser);
                $r = $this->sendMail($fwodno_ture[$key]);
                if (!$r) $log->info("send_item mail ", "error");
            }

        }
      

        if(!$fwodno_ture){
            $this->ms = "noupdate";
            return "error";
        }

        if (!$res){
            $this->ms = "fwerror";
            return "error";
        }
  
        $this->ms = "fwsuccess";

        return 'success';
    }
    
    function sendMail($no)
    {
        $results   = $this->l_order->OrderdetailviewMail($no);
        
        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[$no]
                       );
        $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);
    }
}
?>
