<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */

/**
 * 受注管理系ロジック
 *
 * @package     Onedepo_Logic_Orderview
 * @author      netnee <mail@example.com>
 * @access      public
 */
class Onedepo_Logic_Orderview {

  var $order;

  var $data;

  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 $sellercd;
  var $state;
  
  var $from;
  
  var $odno;
  
  
    function OrderSearchResults($to)
    {
            
        $where = array();
        $params = array();
      
        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Orderview#OrderSearchResults");
        

        
        // AND検索の為に半角空白で分割
        $this->ordermember = mb_convert_kana($this->ordermember, "s", "UTF-8");
        $member = explode(" ", $this->ordermember);
        $this->mailaddr = mb_convert_kana($this->mailaddr, "rns", "UTF-8");
        $mail = explode(" ", $this->mailaddr);

        
        // WHERE句で「=」を使うもの
        /*
        $eq = array("jancd"         => $this->jancode, 
                    "makercd"       => $this->maker_name, 
                    "mediacd"       => $this->media);
        $this->makeEqualParams($eq, $where, $params);
        //--
        */

        // LIKEを使うもの
        $like = array("sendbuyer" => $this->ordermember, 
                      "sendmailaddr" => $this->mailaddr);
        $this->makeLikeParams($like, $where, $params);
        //--

       //日付と時刻の整え
       $second_start = ":00.000000";
       $second_end   = ":59.999999";
       
       if(!empty($this->order_date_start)){
            if(!empty($this->order_time_start)){
              $s_time = " ".$this->order_time_start.$second_start;
            }else{
              $s_time = " 00:00".$second_start;
            }
          $s_orderdate = $this->order_date_start.$s_time;
          
       }
       
       if(!empty($this->order_date_end)){
            if(!empty($this->order_time_end)){
              $e_time = " ".$this->order_time_end.$second_end;
            }else{
              $e_time = " 23:59".$second_end;
            }
         $e_orderdate = $this->order_date_end.$e_time;
       }
       //--

        
        // 受注No と 日付
        $date = array("orderdate" => array($s_orderdate, $e_orderdate),
                      "orderno" => array($this->orderno_start,$this->orderno_end));

        foreach ($date as $key => $val) {
            if (!empty($val)) {
                if (!empty($val[0])) {
                    $where[] = $key .">= ?";
                    $params[] = str_replace("/", "-", $val[0]);
                }
                if (!empty($val[1])) {
                    $where[] = $key ."<= ?";
                    $params[] = str_replace("/", "-", $val[1]);
                }
            }
        }
        
        
        //state
        if($this->state == "2"){
                    $where[] = "forwardstate = ?";
                    $params[] = "4";
        }
        
        if($this->state == "3"){
                    $where[] = "forwardstate = ?";
                    $params[] = "1";
        }
        
        if($this->state == "4"){
                    $where[] = "depositstate = ?";
                    $params[] = "1";
        }
        
        if($this->state == "5"){
                    $where[] = "forwardstate = ?";
                    $params[] = "3";
        }
        

		// sellercd
		if($this->sellercd != "") {
                    $where[] = "sellercd = ?";
                    $params[] = $this->sellercd;
		}

		
        /*
        if ($this->sort == "title")         $sortkey = "itemnm";
        else if ($this->sort == "release")  $sortkey = "salestartdate desc";
        else if ($this->sort == "publish")  $sortkey = "releasedate desc";
        else                                $sortkey = "makernm, makeritemcd";
        */
        $sortkey = "orderno desc";
        
        if (!is_numeric($this->from))$this->from = 1;
        if (empty($to)) $to = 10;
        $from = ($this->from-1)*$to;
        

        $this->data = $this->order->SearchResults($where, 
                                                    $params, 
                                                    $sortkey,
                                                    $to,
                                                    $from);
                                                    
                                                    
                                                    
        return $this->data;
    }
    
    /**
     * LIKE 部分一致
     *
     */
    function makeLikeParams($basearr, &$where, &$params)
    {
        foreach ($basearr as $key => $val) {
            if(!empty($val)) {
                if (is_array($val)) {

                    foreach ($val as $v) {
                        if (!empty($v)) {
                            $where[] = $key ." LIKE ?";
                            $params[] = '%'. $v .'%';
                        }
                    }

                } else {
                    $where[] = $key ." LIKE ?";
                    $params[] = '%'. $val .'%';
                }
            }
        }
        
    }

    function NowSaleList() {
        $nowsalelist = array();

        //昨日
        $yesterday_s_orderdate = date("Y-m-d", time()-(24*60*60)) ." 00:00:00.000000";
        $yesterday_e_orderdate = date("Y-m-d", time()-(24*60*60)) ." 23:59:59.999999";
        $date = array("orderdate" => array($yesterday_s_orderdate, $yesterday_e_orderdate));

        foreach ($date as $key => $val) {
            if (!empty($val)) {
                if (!empty($val[0])) {
                    $where[] = "orderdate >= ?";
                    $params[] = str_replace("/", "-", $val[0]);
                }
                if (!empty($val[1])) {
                    $where[] = "orderdate <= ?";
                    $params[] = str_replace("/", "-", $val[1]);
                }
            }
        }
        $nowsale = $this->order->NowSaleList($where, $params);

        foreach($nowsale as $key => $value){
             $res[] = $this->order->Orderdetail($nowsale[$key]["orderno"]);
        }

        //購入合計金額取得の為に
        foreach ($res as $key => $value) {
            foreach ($res[$key] as $key => $value) {
                if (($value["typediv"]==1 || 
                    $value["typediv"]==2 ||
                    $value["typediv"]==3 || 
                    $value["typediv"]==10 ||
                    $value["typediv"]==11) &&
                    ($value["forwardstate"]!=8 && $value["forwardstate"]!=9)) {
                    $salesprice[] = $value["price"];
                }
            }
        }
        $nowsalelist["yesterday"]["price"] = number_format(array_sum($salesprice));
        $nowsalelist["yesterday"]["count"] = count($nowsale);

        //今月
        $date = $where = $params = $res = $salesprice = array();
        $thismonth_s_orderdate = date("Y-m-01") ." 00:00:00.000000";
        $thismonth_e_orderdate = date("Y-m-d", time()-(24*60*60)) ." 23:59:59.999999";
        $date = array("orderdate" => array($thismonth_s_orderdate, $thismonth_e_orderdate));

        foreach ($date as $key => $val) {
            if (!empty($val)) {
                if (!empty($val[0])) {
                    $where[] = "orderdate >= ?";
                    $params[] = str_replace("/", "-", $val[0]);
                }
                if (!empty($val[1])) {
                    $where[] = "orderdate <= ?";
                    $params[] = str_replace("/", "-", $val[1]);
                }
            }
        }
        $nowsale = $this->order->NowSaleList($where, $params);

        foreach($nowsale as $key => $value){
             $res[] = $this->order->Orderdetail($nowsale[$key]["orderno"]);
        }

        //購入合計金額取得の為に
        foreach ($res as $key => $value) {
            foreach ($res[$key] as $key => $value) {
                if (($value["typediv"]==1 || 
                    $value["typediv"]==2 ||
                    $value["typediv"]==3 || 
                    $value["typediv"]==10 ||
                    $value["typediv"]==11) &&
                    ($value["forwardstate"]!=8 && $value["forwardstate"]!=9)) {
                    $salesprice[] = $value["price"];
                }
            }
        }
        $nowsalelist["thismonth"]["price"] = number_format(array_sum($salesprice));
        $nowsalelist["thismonth"]["count"] = count($nowsale);

        //今年
        $date = $where = $params = $res = $salesprice = array();
        $thisyear_s_orderdate = date("Y") ."-01-01 00:00:00.000000";
        $thisyear_e_orderdate = date("Y-m-d", time()-(24*60*60)) ." 23:59:59.999999";
        $date = array("orderdate" => array($thisyear_s_orderdate, $thisyear_e_orderdate));

        foreach ($date as $key => $val) {
            if (!empty($val)) {
                if (!empty($val[0])) {
                    $where[] = "orderdate >= ?";
                    $params[] = str_replace("/", "-", $val[0]);
                }
                if (!empty($val[1])) {
                    $where[] = "orderdate <= ?";
                    $params[] = str_replace("/", "-", $val[1]);
                }
            }
        }
        $nowsale = $this->order->NowSaleList($where, $params);

        foreach($nowsale as $key => $value){
             $res[] = $this->order->Orderdetail($nowsale[$key]["orderno"]);
        }

        //購入合計金額取得の為に
        foreach ($res as $key => $value) {
            foreach ($res[$key] as $key => $value) {
                if (($value["typediv"]==1 || 
                    $value["typediv"]==2 ||
                    $value["typediv"]==3 || 
                    $value["typediv"]==10 ||
                    $value["typediv"]==11) &&
                    ($value["forwardstate"]!=8 && $value["forwardstate"]!=9)) {
                    $salesprice[] = $value["price"];
                }
            }
        }
        $nowsalelist["thisyear"]["price"] = number_format(array_sum($salesprice));
        $nowsalelist["thisyear"]["count"] = count($nowsale);

        return $nowsalelist;
    }
    function UntransCount() {
        $where[] = "fwstate_cnt > 0";
        $untranscount = $this->order->NowSaleList($where, array());

        return count($untranscount);
    }
   
    
    //詳細表示
    function Orderdetailview()
    {

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Orderdetailview#Orderdetailview");

        $no = $this->odno;
        $this->data = $this->order->OrderdetailSortTypeDiv($no);
        
        
        return $this->data;
    }
    
    //詳細表示、メール送信用
    function OrderdetailviewMail($no)
    {
        $this->data = $this->order->OrderdetailMail($no);
        return $this->data;
    }
    
    //詳細表示2
    function listprice($odno)
    {

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Orderdetailview#Orderdetailview");

        $this->data = $this->order->Orderdetail($odno);
        
        
        return $this->data;
    }
    
    /**
    * メーカ品番ごとに予約状態のオーダを取得
    */
    function getReserveItemList($date=""){
        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Orderdetailview#getReserveItemList");
        
        $this->data = $this->order->getReserveItemList($date);
        
        return $this->data;
    }
    
    
    /**
    * メーカ品番のリストからそれぞれのオーダーを取得
    */
    function getReserveOrderList(){
        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Orderdetailview#getReserveOrderList");
        
        $return = array();
        foreach($this->data as $val){
            $return[$val["makeritemcd"]] = $this->order->getReserveOrderList($val["makeritemcd"]);
        }
        
        return $return;
    
    }
    
    
    /**
    * 出荷指示待ち（在庫有・入金済）のオーダーを取得
    *
    */
    function getInstructionOrderList(){
        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Orderdetailview#getInstructionOrderList");
        
        return $this->order->getInstructionOrderList();
    }
    
    
}
?>