<?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     Admin.Itemmanage.Order.
 * @author      T.Shimano <shimano@fuco.jp>
 * @access      public
 */

require_once 'Pager.php';

class Admin_Itemmanage_Order_List
{   
	// DBインターフェイス
	var $itemorder;
	var $supplier;
    var $tname;
	
	// 入力データ
    var $searchkey;
    var $sort;
	
	// 入力値(confirmから)
    var $orderunitprice;
    var $ordernum;
    var $suppliercd;
	var $suppliediv;
	var $ordercd;
	
	//　検索結果
    var $data;
    var $count;
    
    // 絞込み
    var $ischecked;
    var $itemcd;
    var $structcd;
    
    // 絞込み保持
    var $o_itemcd;
    var $o_structcd;
    
    // タイトル
    var $title;
    var $pagename;
    var $div;

    // 入力パラメータ
    var $pageID;      // ページ番号
    
	var $Pager_perPage;
	var $Pager_delta;
	var $Pager_append;
	var $Pager_clearIfVoid;
	var $Pager_urlVar;
	var $Pager_useSessions;
	var $Pager_closeSession;
	var $Pager_mode;
	var $Pager_prevImg;
	var $Pager_nextImg;
	var $Pager_altPage;
	var $Pager_altPrev;
	var $Pager_altNext;
	var $Pager_altFirst;
	var $Pager_altLast;
	var $Pager_firstPagePre;
	var $Pager_firstPageText;
	var $Pager_firstPagePost;
	var $Pager_lastPagePre;
	var $Pager_lastPageText;
	var $Pager_lastPagePost;
	var $Pager_separator;
	var $Pager_spacesBeforeSeparator;
	var $Pager_spacesAfterSeparator;

	function getPager()
	{
    	$search = array();
    	foreach($this->searchkey as $key=>$val) {
    	    $search [$key] = $val;
    	}
    	$search["sort"] = $this->sort;
    	$search["action"] = "admin_itemmanage_order_list";
    	$search["div"] = (empty($this->div)) ? 2 : 1;

        // Pager設定
        $pageparams = array(
        'totalItems'    => $this->count,
        'perPage'       => $this->Pager_perPage,       // １ページの行数
        'delta'         => $this->Pager_delta,       // 表示ページから、前後に表示するページ数
        'append'        => $this->Pager_append,
        'currentPage'   => $this->pageID,
        'clearIfVoid'   => $this->Pager_clearIfVoid,
        'urlVar'        => $this->Pager_urlVar,
        'useSessions'   => $this->Pager_useSessions,
        'closeSession'  => $this->Pager_closeSession,
        'mode'          => $this->Pager_mode,
        'prevImg'       => $this->Pager_prevImg,
        'nextImg'       => $this->Pager_nextImg,
        'altPage'       => $this->Pager_altPage,
        'altPrev'       => $this->Pager_altPrev,
        'altNext'       => $this->Pager_altNext,
        'altFirst'      => $this->Pager_altFirst,
        'altLast'       => $this->Pager_altLast,
        'firstPagePre'  => $this->Pager_firstPagePre,
        'firstPageText' => $this->Pager_firstPageText,
        'firstPagePost' => $this->Pager_firstPagePost,
        'lastPagePre'   => $this->Pager_lastPagePre,
        'lastPageText'  => $this->Pager_lastPageText,
        'lastPagePost'  => $this->Pager_lastPagePost,
        'separator'     => $this->Pager_separator,
        'spacesBeforeSeparator' => $this->Pager_spacesBeforeSeparator,
        'spacesAfterSeparator'  => $this->Pager_spacesAfterSeparator,
        'extraVars' => $search,
        'importQuery' => false,
        );

        // Pager出力
        $pager = & Pager::factory($pageparams);
        $this->pagedata = $pager->getPageData();
        $link =	$pager->getLinks();
        return $link["all"];
	}

    function execute()
    {
    	$where = array();
    	$searchkey = $this->searchkey;
    	
    	
    	// 選択で絞り込み
    	if (!empty($this->ischecked)) {
    		$ischecked = $this->ischecked;
    		$itemcd = $this->itemcd;
    		$structcd = $this->structcd;
    		
    		foreach ($ischecked as $index => $val) {
    			if ($val==1) {
    				if ( count($where) == 0 ) {
    					$this->o_itemcd = array();
    					$this->o_structcd = array();
    			    }
    				$where[] = array(" st.itemcd = {$itemcd[$index]} "," st.structcd = {$structcd[$index]} ");
    				$this->o_itemcd[] = $itemcd[$index];
    				$this->o_structcd[] = $structcd[$index];
    			}
    		}
    		
    		// 絞込みがあった場合はページを０に
            //if(count($where) != 0) {
            //	$this->pageID = 0;
            //}
    	}
    	
    	// もともと絞り込まれていた場合
    	if( count($where) == 0 && !empty($this->o_itemcd) ) {
    		foreach ($this->o_itemcd as $index => $val) {
    			$where[] = array(" st.itemcd = {$this->o_itemcd[$index]} "," st.structcd = {$this->o_structcd[$index]} ");
    	    }
    	}
    	
    	if (count($where) == 0) {
        	// 検索条件で絞込み
        	// 発注締切日
        	if (!empty($searchkey->orderclosedatefrom)) {
        	    $where[0][] = "orderclosedate >= '$searchkey->orderclosedatefrom' ";
	    	}
	    	if (!empty($searchkey->orderclosedateto)) {
	    		$where[0][] = "orderclosedate <= '$searchkey->orderclosedateto' ";
	    	}
	    	
	    	// 発売日
	    	if (!empty($searchkey->salestartdatefrom)) {
	    		$where[0][] = "salestartdate >= '$searchkey->salestartdatefrom' ";
	    	}
	    	if (!empty($searchkey->salestartdateto)) {
	    		$where[0][] = "salestartdate <= '$searchkey->salestartdateto' ";
	    	}
    	
	    	// メーカー
	        if ( $searchkey->maker_all != 1 ) {
	        	if (!empty($searchkey->maker_name)) {
	        		$where[0][] = "mk.makercd = $searchkey->maker_name ";
	        	}
	        	if (!empty($searchkey->maker_label)) {
	        		$where[0][] = "lb.labelcd = '$searchkey->maker_label' ";
	        	}
	        }
        
	        // メーカー品番
	        if (!empty($searchkey->makeritemcdfrom)) {
	        	$where[0][] = "makeritemcd >= '$searchkey->makeritemcdfrom' ";
	        }
	        if (!empty($searchkey->makeritemcdto)) {
	        	$where[0][] = "makeritemcd <= '$searchkey->makeritemcdto' ";
	        }
	        
	        // 作品区分
	        if (!empty($searchkey->itemdiv)) {
	        	$where[0][] = "itemdivcd = '$searchkey->itemdiv' ";
	        }
	        
	        // メディアコード
	        if (!empty($searchkey->mediacd)) {
	        	$where[0][] = "mediacd = '$searchkey->mediacd' ";
	        }
	        
	        // ステータス
	        if (!empty($searchkey->status) && $searchkey->status > 0) {
	        	// 未実装
	        	
	        }
	        
	        // 予約数
	        if (!empty($searchkey->advancenummin)) {
	        	$where[0][] = "advancenum >= '$searchkey->advancenummin' ";
	        }
	        if (!empty($searchkey->advancenummax)) {
	        	$where[0][] = "advancenum <= '$searchkey->advancenummax' ";
	        }
    	}
    	
    	if(empty($this->pageID)) {
    		$this->pageID = 1;
    	}

    	if (empty($this->sort)) {
    		$this->sort = "makernm";
    	}
        
    	$this->data = $this->itemorder->getItemorderList($this->Pager_perPage, ($this->pageID-1)*$this->Pager_perPage, $where, array(), $this->sort, $searchkey->status);
    	$this->count = array_pop($this->data);
        $log =& LogFactory::getLog();
        $log->debug("Admin_Itemmanage_Order_Listが実行されました", "Admin_Itemmanage_Order_List#execute");

        return 'success';
    }
    
    // 検索パラメータ（URL埋め込み用）
    function getRequestGET()
    {
        $str = "";
        foreach ($this->searchkey as $key=>$val) {
        	if (!empty($val)) {
        		if($str != "") {
        			$str.= "&$key=$val";
        		} else {
        		    $str = "$key=$val";
        		}
        	}
        }
        if( !empty($this->o_itemcd)) {
        	foreach ($this->o_itemcd as $key=>$val) {
        		if($str != "") {
        			$str.= "&o_itemcd[]=".$this->o_itemcd[$key]."&o_structcd[]=".$this->o_structcd[$key];
        		} else {
        			$str = "o_itemcd[]=".$this->o_itemcd[$key]."&o_structcd[]=".$this->o_structcd[$key];
        		}
        	}
        }
        return $str;
    }
    
    // 検索パラメータ（FORM埋め込み用)
    function getRequestPOST()
    {
    	$str = "";
    	foreach ($this->searchkey as $key=>$val) {
    		if (!empty($val)) {
    			$str .= "<input type=\"hidden\" name=\"$key\" value=\"$val\">\r";
    		}
    	}
    	if( !empty($this->o_itemcd) ) {
    		foreach ($this->o_itemcd as $key=>$val) {
    			$str .= "<input type=\"hidden\" name=\"o_itemcd[]\" value=\"".$this->o_itemcd[$key]."\">\r";
    			$str .= "<input type=\"hidden\" name=\"o_structcd[]\" value=\"".$this->o_structcd[$key]."\">\r";
    		}
    	}
    	return $str;
    }
          
    function getSupplier()
    {
    	return $this->supplier->selectAll();
    }

    function getSearchKey()
    {
    	return $this->searchkey;
    }

    function getItemdivRadio()
    {
        $div = (empty($this->div)) ? 2 : 1;
        if (empty($div)) {
            $itemdivradio = $this->tname->selectDivAll(1);
        } elseif ($div==1) {
            $where = " AND namecd <= 10 ";
            $itemdivradio = $this->tname->selectDivTerm(1, $where);
        } elseif ($div==2) {
            $where = " AND namecd >= 11 ";
            $itemdivradio = $this->tname->selectDivTerm(1, $where);
        }
        return $itemdivradio;
    }
    function getMediaRadio()
    {
        $mediaradio = $this->tname->selectDivAll(4);
        return $mediaradio;
    }
}
?>
