<?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     [[package名]]
 * @author      Your Name <mail@example.com>
 * @access      public
 */
class Customer_Itembuy_Order_List
{
//POSTとGET用変数
    var $pagetitle;
    var $mode;
    var $past_select;
    var $page;

//表示用変数(未評価一覧はこの変数を用いていない)
    var $orderlist;

//表示用データに格納するための配列変数
    var $order_title;
    var $order_date;
    var $order_no;
    var $order_row;
    var $itemcd;

    var $session;
    var $auth;
    var $dlitem;
    var $c_list;
    var $c_valuation;

    var $cnt;

    // リストのページング
    var $pagerparam;  // Pager用
    var $pagedata;    // Pagerデータ
    var $links;       // Pagerリンク

    /**
     * [[機能説明]]
     *
     * @access  public
     */
    function execute()
    {
        $log =& LogFactory::getLog();
        $log->debug("Customerportal_Listが実行されました", "Customerportal_List#execute");

        // ページ設定
        if(!isset($this->page) || $this->page <= 1) $this->page = 1;

        $check = true;

        $res = $this -> check_mode();
        switch($res){
            //表示ボタンから選択された場合
            case 1:
                $this -> setItem();
            break;

            //未評価一覧が選択された場合
            case 2:
                $this -> setNonEstimateItem();
            break;

            //その他の場合　つまりエラー
            default:
                $check = false;
            break;
        }

        $pageparams = $this->pagerparam->getPageParam($this->cnt,ORDER_RECORD_LIMIT,$this->page);
        $pageparams["urlVar"] = "page";

        // Pager出力
        $pager = & Pager::factory($pageparams);
        $this->pagedata = $pager->getPageData();
        $this->links = $pager->getLinks();

        if($check) {
            return 'success';
        } else {
            return 'input';
        }
    }

    function getTitle()
    {
        return $this -> pagetitle;
    }

    function getMode()
    {
        return $this -> mode;
    }

    function getPage()
    {
        return $this -> page;
    }

    function check_mode()
    {
        if($this -> mode == "estimate") {
            return 2;
        } else {
            return 1;
        }
    }

    function setItem()
    {
        $sql_title  = "";
        $sql_detail = "";

        if($this->mode == "news"){
            $this->pagetitle = "最近の利用明細";
            $sql_title  = " AND OrderDate >= '" . date("Y-m-d", strtotime(RECENT_ORDER . " day ago")) . "'";

        } else if($this->mode == "past") {
            $this->pagetitle = "過去の利用明細　半年分";
            $sql_title  = " AND OrderDate >= '" . date("Y-m-d", strtotime("6 month ago")) . "'";

        } else if(substr($this->mode, 0, 4) == "year") {
            $year = substr($this->mode, 4);
            $this->pagetitle = "過去の利用明細　" . $year . "年分";
            $sql_title   = " AND OrderDate >= '" . $year . "-01-01'";
            $sql_title  .= " AND OrderDate <= '" . $year . "-12-31'";

        } else {
            $this->pagetitle = "過去の利用明細　全部";
        }

        $this->cnt = 0;
        $array = array();
        $membercd = $this->auth->getAuthData("membercd");
        $titles = $this->c_list->select_OrderTitle($membercd, $sql_title);

        if(is_array($titles)){
            foreach($titles as $title) {
                $orderno   = $title['orderno'];
                $orderdate = $title['orderdate'];
                $addressee = $title['addressee'];

                $details = $this->c_list->selectOrderDetail($title['orderno']);
                $orderdetail = array();
                if(is_array($details)){
                    foreach($details as $detail) {
                        if($detail['forwardstate'] != 1 && $detail['forwardstate'] != 2 && $detail['forwardstate'] != 3 && $detail['forwardstate'] != 4 && $detail['forwardstate'] != NULL) continue;
                        if($this->mode == "news" && $detail['forwardstate'] != 1 && $detail['forwardstate'] != 2 && $detail['forwardstate'] != 3 && $detail['forwardstate'] != 4 && $detail['forwardstate'] != NULL) continue;
                        $rowno         = $detail['orderrowno'];
                        $forwardstate  = $detail['forwardstate'];
                        $itemcd        = $detail['itemcd'];
                        $itemnm        = $detail['itemnm'] ." ". $detail['variationnm'];
                        $sellercd      = $detail['membercd'];// 070706 追加 冨永

                        if(!empty($detail['nicknm'])) {
                            $sellernm      = $detail['nicknm'];
                        } else if (!empty($detail['lastnm']) AND !empty($detail['firstnm']) ) {
                            $sellernm      = $detail['lastnm'] .' '.$detail['firstnm'];
                        } else {
                            $sellernm = 'onedepo';
                        }
                        $forwarddate = $detail['forwarddate'];

                        // 返品可能日付を過ぎているか？
                        $w_forwarddate = strtotime($forwarddate);
                        $w_now         = strtotime("now");
                        $past_return_day = ((($w_now - $w_forwarddate)/(60*60*24)) > ITEMBUY_RETURN_DAY);
                        //--


                        // ダウンロード
                        $dl = $this->dlitem->getDownloadItem($detail['itemcd'], $detail['structcd']);
                        $download_url = false;
                        if (!empty($dl)) {
                            $dl_ok = $this->dlitem->getDownloadOK($detail['orderno'], $detail['orderrowno'], $membercd);
                            if (!empty($dl_ok)) {
                                $fid_key = $dl_ok["orderno"] ."_". $dl_ok["orderrowno"] ."_". $membercd;
                                $fid = sha1($fid_key);
                                $download_url = $this->dlitem->getDownloadURL($fid, $dl_ok["orderno"], $dl_ok["orderrowno"]);
                            }
                        }
                        //--

                        $orderdetail[] = array("rowno" => $rowno,
                                               "forwardstate" => $forwardstate,
                                               "itemcd" => $itemcd,
                                               "itemnm" => $itemnm,
                                               "sellercd" => $sellercd,
                                               "forwarddate" => $forwarddate,
                                               "past_return_day" => $past_return_day,
                                               "download" => !empty($dl),
                                               "download_url" => $download_url
                                               );
                        $this -> cnt++;
                    }
                }
                //$orderdetailが空、つまり受注見出しがあっても該当する受注明細がひとつもない場合は、表示させない
                if(count($orderdetail) != 0){
                    $array[] = array("orderno" => $orderno, "orderdate" => $orderdate, "addressee" => $addressee, "orderdetail" => $orderdetail, "sellernm" => $sellernm);
                }
            }
        }

        $this->cnt = count($array);
        $sliceStart = (ORDER_RECORD_LIMIT*($this->page-1)-1 < 0) ? 0 : ORDER_RECORD_LIMIT*($this->page-1)-1;
        $this->orderlist = array_slice($array, $sliceStart, ORDER_RECORD_LIMIT);
    }

    function setNonEstimateItem()
    {
        $this -> pagetitle = "未評価一覧";
        $this -> mode  =  "estimate";
        $this -> cnt = 0;
        $array = array();
        $membercd = $this -> auth -> getAuthData("membercd");
        $titles = $this -> c_list -> select_OrderTitle($membercd, "");

        if(is_array($titles)){
            foreach($titles as $title) {
                $orderno   = $title['orderno'];
                $orderdate = $title['orderdate'];
                $addressee = $title['addressee'];

                $details = $this -> c_list -> selectOrderDetail($title['orderno']);
                $orderdetail = array();
                if(is_array($details) && $this->c_valuation->checkEstimate($orderno)){

                    foreach($details as $detail) {
                        if($detail['forwardstate'] != 1 && $detail['forwardstate'] != 2 && $detail['forwardstate'] != 3 && $detail['forwardstate'] != 4 && $detail['forwardstate'] != NULL) continue;

                        $rowno         = $detail['orderrowno'];
                        $forwardstate  = $detail['forwardstate'];
                        $itemcd        = $detail['itemcd'];
                        $itemnm        = $detail['itemnm'] ." ". $detail['variationnm'];

                        if(!empty($detail['nicknm'])) {
                            $sellernm      = $detail['nicknm'];
                        } else if (!empty($detail['lastnm']) AND !empty($detail['firstnm']) ) {
                            $sellernm      = $detail['lastnm'] .' '.$detail['firstnm'];
                        } else {
                            $sellernm = 'onedepo';
                        }

                        $orderdetail[] = array("rowno" => $rowno, "forwardstate" => $forwardstate, "itemcd" => $itemcd, "itemnm" => $itemnm);
                        //$this -> cnt++;
                    }
                }
                //$orderdetailが空、つまり受注見出しがあっても該当する受注明細がひとつもない場合は、表示させない
                if(count($orderdetail) != 0){
                    $array[] = array("orderno" => $orderno, "orderdate" => $orderdate, "addressee" => $addressee, "orderdetail" => $orderdetail, "sellernm" => $sellernm);
                }
            }
        }
        //$this -> orderlist = $array;
        $this -> cnt = count($array);
        $sliceStart = (ORDER_RECORD_LIMIT*($this->page-1) < 0) ? 0 : ORDER_RECORD_LIMIT*($this->page-1);
        $this -> orderlist = array_slice($array, $sliceStart, ORDER_RECORD_LIMIT);
    }


    function getLink()
    {
        return $this->links["all"];
    }
}
?>
