<?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: */

/**
 * 出荷指示済更新＆CSVデータ出力
 *
 * @package     Admin_Ordermanage_Instruction_Export
 * @author      netnee <mail@example.com>
 * @access      public
 */
class Admin_Ordermanage_Instruction_Export
{
    var $orderview;
    var $forward;
    
    var $pagename;
    var $title;
    var $upduser;
    
    var $orderno;
    
    var $res = false;
    var $created = false;
    var $odrarray = array();
    var $odrlist;
    
    
    /**
     * execute
     *
     * @access  public
     */
    function execute()
    {
        $log =& LogFactory::getLog();
        $log->debug("Admin_Ordermanage_Instruction_Exportが実行されました", "Admin_Ordermanage_Instruction_Export#execute");
        
        
        // 更新前データ取得
        $this->odrlist = $this->orderview->getInstructionOrderList();
        
        // 出荷指示更新
        if (is_array($this->orderno)){
            foreach($this->orderno as $val){
                $num_arr = explode("-", $val);
                if (is_numeric($num_arr[0]) && is_numeric($num_arr[1])){
                    $this->odrarray[] = array( "no" => $num_arr[0], "row"  => $num_arr[1], "updcnt" => $num_arr[2]);
                }
            }
        }
        
        if (!empty($this->odrarray)) $this->res = $this->forward->instructionUpdate($this->odrarray, 3, $this->upduser);
        
        // CSVデータ作成
        if ($this->res){
            $this->createCSV();
            $this->created = true;
        }
        
        return 'success';
    }
    
    
    /**
    * CSVファイル作成
    *
    */
    function createCSV(){
        $tmp = array();
        
        // 更新前データから更新したデータだけ抜き出す
        foreach ( $this->odrlist as $val ){
            foreach ($this->odrarray as $k){
                if ( $val["orderno"] == $k["no"] && $val["orderrowno"] == $k["row"] ){
                    //$send = ($val["depositdiv"]==3) ? "コレクト" : ($val["takkyubin"]==1) ? "宅急便" : "メール便";
                    $send = "";
                    $sendcode = "";
                    if($val["depositdiv"]==3) {
                        $send = "コレクト";
                        $sendcode = 2;
                    } else if($val["takkyubin"]==1) {
                        $send = "宅急便";
                        $sendcode = 0;
                    } else {
                        $send = "メール便";
                        $sendcode = 3;
                    }
                    $tmp[] = array($val["rackid"], $val["makeritemcd"], $val["jancd"], $val["itemnm"], $val["count"], $val["salestartdate"], $val["orderno"], $val["orderdate"],$sendcode, $send,
                                   (!empty($val["ordermember"])) ? $val["lastnm"]." ".$val["firstnm"] : $val["sendbuyer"], 
                                   $val["addressee"], $val["sendpostalcd"], 
                                   $val["prefnm"].$val["sendaddr1"].$val["sendaddr2"],
                                   $val["sendtelno1"]);
                }
            }
        }
        
        $this->odrlist =  array_merge(array(array('棚番ID','品番','JANコード','タイトル','数量','発売日','受注番号','受注日', '配送コード', '配送',
                                                  '購入者', '宛名','郵便番号', '送り先','送付先TEL')), $tmp);

        $fullpath = DATAEXPORT_DL_DIR . "/instruction-list.csv";
        // =>CSVファイルのファイル名を変更する時は関連テンプレートのリンクも変更する必要あり
        
        $fp = fopen($fullpath, 'w');
        for ($i = 0; $i < count($this->odrlist); $i++) {
            fputcsv($fp, $this->odrlist[$i]);
        }
        fclose($fp);
        
        // ファイル内容のエンコードを UTF-8 から SJIS へ変換
        //$enc = mb_convert_encoding(file_get_contents($fullpath), "SJIS","UTF-8");
        //file_put_contents($fullpath, $enc);
        
    }
    
    function exportCSV()
    {
        $filepathname = DATAEXPORT_DL_DIR . "/instruction-list.csv";
        $filename = "instruction-list.csv";
        
        $len = filesize($filepathname);
        header("Cache-Control: public");
        header("Pragma: public");
        header ("Content-Disposition: inline ; filename=$filename");
        header ("Content-type: text/octet-stream");
        header ("Content-Length: ". $len);
        $handle = fopen ($filepathname, "r");
        while (!feof ($handle)) {
            $buffer = fgets($handle, 4096);
            $buffer = mb_convert_encoding($buffer, "SJIS","UTF-8");
            echo $buffer;
        }
        fclose ($handle);

        return TRUE;
    }
}
?>
