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

require_once 'Pager.php';

class Admin_Itemmanage_Order_Fileoutput
{   
	// DBインターフェイス
	var $itemorder;
	var $supplier;
	
	// 入力データ
    var $searchkey;
    var $sort;
	
	//　検索結果
    var $data;
    var $count;
    
    // 絞込み
    var $ischecked;
    var $itemcd;
    var $structcd;
    
    var $perpage;
    

    // タイトル
    var $title;
    var $pagename;
    var $div;
    
    // カラム名
    var $columntitle;
    
    function execute()
    {
    	$where = array();
    	$searchkey = $this->searchkey;
    	
    	
       	// 検索条件で絞込み
       	// 発注締切日
       	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";
    	}
        
    	header('Content-type: application/csv');
    	header('Content-Disposition: attachment; filename="order.csv"');
    	$this->data = $this->itemorder->getItemorderList(-1, 0, $where, array(), $this->sort, $searchkey->status);
    	$this->count = array_pop($this->data);
    	
    	$this->columntitle  = "発注締切日 , 予約数 , 定価 , 発売日 , 品番 , メーカー , レーベル , タイトル , メディア ,".
    	                      " 発注済み数 , 仕入単価 , 金額 , 発注数 , 仕入先 , 仕入れ区分 , 注文番号 , 発注日";
    	               
    	
        // UTF8->SJIS変換

        $this->columntitle = mb_convert_encoding($this->columntitle, "SJIS", "UTF8");
        
        foreach ($this->data as $key=>$val) {
            foreach ( $val as $key2=>$val2 ) {
            	$val[$key2] = mb_convert_encoding($val2, "SJIS", "UTF-8");
            }
            $this->data[$key] = $val;
        }
    	
        $log =& LogFactory::getLog();
        $log->debug("Admin_Itemmanage_Order_Listが実行されました", "Admin_Itemmanage_Order_List#execute");

        return 'success';
    }
}
?>