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

require_once 'Pager.php';

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * 基本情報変更確認
 *
 * @package     Admin.Itemmanage.Order.Confirm
 * @author      T.Shimano <shimano@fuco.jp>
 * @access      public
 */
class Admin_Itemmanage_Delivery_Confirm
{
    //検索条件
    var $suppliercode;
    var $suppliermode;
    var $deliverydatefrom;
    var $deliverydateto;
    var $ordernumber;
    var $jancode;

    //並び替え条件
    var $sortmode;

    //チェック・品目コード・構成コード
    var $ischecked;
//    var $itemcd;
//    var $structcd;
	var $stocktitlecd;

    //テーブル操作クラス
   	var $c_supplier;
    var $c_delivery;
    
    //入力値
    var $orderunitprice;
    var $ordernum;
    var $clear;//PagerのextraVars Pagerで遷移する際に、値を消去する

    //出力用
    var $deliverylist = array();
    var $selectedList = array();

    // 入力パラメータ
    var $cnt;         // 総登録件数
    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;

	// アクションチェイン
	var $actionChain;
	
	var $title;
	var $pagename;
	
	//発注数以上の納品数が入力されているかどうかのフラグ
	var $overOrderFlag = false;
	
	//エラー戻りであるかのフラグ
    var $isError = false;

    function execute()
    {
    	$errorList =& $this->actionChain->getCurErrorList();

        if(count($this -> ischecked) == 0){
            $errorList -> add(1, "納品する商品にチェックを入れてください。");
            $this->pagename = "リスト - ";
            $this->isError = true;
            return 'input';
        }
        
        $this -> selectedList = $this -> getSelectDeliveryList();
        
		if(count($this -> selectedList) == 0){
		    $errorList -> add(1, "仕入れ単価と納品数を入力してください。");
		    $this->pagename = "リスト - ";
		    $this->isError = true;
            return 'input';
		}
		
        $log =& LogFactory::getLog();
        $log->debug("Admin_Itemmanage_Delivery_Confirmが実行されました", "Admin_Itemmanage_Delivery_Confirm#execute");        
        return 'success';
    }

/*confirm**********************************************************************************************************/

    function getSelectDeliverylist()
    {
        $deliverylist = array();

        foreach($this -> ischecked as $key => $item){
            $deliverydata = array();
            if($this->orderunitprice[$key] == "" || $this->ordernum[$key] == "") continue; //未入力ならば、飛ばす
            if(!is_numeric($this->orderunitprice[$key]) || !is_numeric($this->ordernum[$key])) continue;//数字でなければ飛ばす

            $deliverydata = $this -> c_delivery -> selectDelivery("",1,0,"",$this->stocktitlecd[$key]);
            $deliverydata = $deliverydata[0];
            $deliverydata["stocktitlecd"]   = $this->stocktitlecd[$key];
            //$deliverydata["itemcd"]         = $this->itemcd[$key];
            //$deliverydata["structcd"]       = $this->structcd[$key];
            $deliverydata["orderunitprice"] = $this->orderunitprice[$key];
            $deliverydata["ordernum"]       = $this->ordernum[$key];
            $deliverydata["deliverynum"]    = $this -> c_delivery -> getOrderNumber($this->stocktitlecd[$key]);
            $deliverydata["sumprice"]       = ($this->orderunitprice[$key] * $this->ordernum[$key]);
            $deliverydata["ischecked"]      = true;
            $deliverydata["isinorder"]      = true;
            
            //発注数以上の納品数の場合の処理
            if($deliverydata["deliverynum"] < $deliverydata["ordernum"]){
                $this -> overOrderFlag     = true;
                $deliverydata["isinorder"] = false;
            }
            
            $deliverylist[$key]     = $deliverydata;
        }

        return $deliverylist;
    }

    function getSelectSupplierName()
    {
        $name = $this -> c_supplier -> selectKey($this -> suppliercode);
        return $name["suppliernm"];
    }
    
    function getSelectedList()
    {
        return $this -> selectedList;
    }
    
/*confirm**********************************************************************************************************/




/*
    function getDeliverylist()
    {
        // ページ設定
        if(is_null($this->pageID))
        $this->pageID = 1;

        // 一覧表示件数
        if (!empty($this->srcdispnum)){
            $this->Pager_perPage = $this->srcdispnum;
        } else {
            $this->Pager_perPage = ADMIN_NEWS_VIEW_LIMIT;
        }

        //JANコードから注文番号の取得 と、SQLの条件作成
        $conditions = $this -> getSQL();
        $deliverylist = $this -> c_delivery -> selectDelivery($conditions,
        $this->Pager_perPage, $this->Pager_perPage*($this->pageID - 1), $this->sortmode);

        $this -> cnt = $this -> c_delivery -> countDelivery($conditions);

        $pageparams = $this->pageparam->getPageParam($this->cnt, $this->Pager_perPage, $this->pageID);

        // Pager出力
        $extraVars = array();
        $extraVars["suppliercode"]     = $this->suppliercode;
        $extraVars["suppliermode"]     = $this->suppliermode;
        $extraVars["deliverydatefrom"] = $this->deliverydatefrom;
        $extraVars["deliverydateto"]   = $this->deliverydateto;
        $extraVars["ordernumber"]      = $this->ordernumber;
        $extraVars["jancode"]          = $this->jancode;
        $extraVars["action"]           = "admin_itemmanage_delivery_list";
        
        $pageparams["extraVars"] = $extraVars;
        $pageparams["importQuery"] =false;
        
        $pager =& Pager::factory($pageparams);
        $this->pagedata = $pager->getPageData();

        $this->links = $pager->getLinks();
        
        return $deliverylist;
    }
*/
/*list**********************************************************************************************************/
	function getPager()
	{
        //JANコードから注文番号の取得 と、SQLの条件作成
        $conditions = $this -> getSQL();
        $this -> cnt = $this -> c_delivery -> countDelivery($conditions);

        // Pager設定
        $pageparams = array(
        'totalItems'    => $this->cnt,
        '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'     => array('clear' => 1),
        );
        
        // Pager出力
        $pager = & Pager::factory($pageparams);
        $this->pagedata = $pager->getPageData();
        $link =	$pager->getLinks();
        return $link["all"];
	}

    function getSQL()
    {
        $conditions ="";
        if($this -> jancode != ""){
            $array = $this -> c_delivery -> getOrderNoFromJancd($this -> jancode);
            if($array != null){
                $arrayOrderNo = array();
                foreach($array as $oneOrderNo){
                    $arrayOrderNo[] = "stk.orderno = '" . $oneOrderNo["orderno"] . "'";
                }
                $implodeOrderNo = implode($arrayOrderNo, ' OR ');
            }else{
                //JANコードが存在しない誤ったコードの場合
                $implodeOrderNo = " FALSE ";
            }
            
            $conditions .= "AND (" . $implodeOrderNo . ")"; 
        }

        if($this -> suppliercode != "")
            $conditions .= " AND stk.suppliercd = " . $this -> suppliercode;

        if($this -> deliverydatefrom != "")
            $conditions .= " AND stk.orderdate >= '" . $this -> deliverydatefrom . "'";

        if($this -> deliverydateto != "")
            $conditions .= " AND stk.orderdate <= '" . $this -> deliverydateto . "'";

        if($this -> ordernumber != "")
            $conditions .= " AND stk.orderno LIKE '%" . $this -> ordernumber ."%'";

        if($this -> suppliermode != "")
            $conditions .= " AND stk.supplidiv = "  . $this -> suppliermode;
        
        return $conditions;    	
    }

    function getCnt()
    {
        //JANコードから注文番号の取得 と、SQLの条件作成
        $conditions = $this -> getSQL();
        $this -> cnt = $this -> c_delivery -> countDelivery($conditions);
        return $this -> cnt;
    }

    function getLink()
    {
        return $this->links;
    }

    function getDeliverylist()
    {
        //JANコードから注文番号の取得 と、SQLの条件作成
        $conditions = $this -> getSQL();
        $this->deliverylist = $this -> c_delivery -> selectDelivery($conditions, 
        $this->Pager_perPage, $this->Pager_perPage*($this->pageID - 1), $this->sortmode);
        return $this->deliverylist;
    }

    function getSupplierName()
    {
        $name = $this -> c_supplier -> selectKey($this -> suppliercode);
        return $name["suppliernm"];
    }

    function getCheckedArray()
    {
//        if($this->isError) return array();
        if(is_null($this -> ischecked)  || $this -> clear == 1) return array();
        return $this -> ischecked;
    }

    function getOrderUnitPrice()
    {
//        if($this->isError) return array();
        if(is_null($this -> orderunitprice)  || $this -> clear == 1) return array();
        return $this -> orderunitprice;
    }

    function getOrderNum()
    {
//        if($this->isError) return array();
        if(is_null($this -> ordernum)  || $this -> clear == 1) return array();
        return $this -> ordernum;
    }
/*list**********************************************************************************************************/
}
?>
