<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 16-11-28
 * Time: 下午7:05
 * To change this template use File | Settings | File Templates.
 */
require_once("PHPExcel.php");
require_once("PHPExcel/IOFactory.php");

class Merchant_InvoiceController extends Ec_Controller_Action
{
    public function preDispatch ()
    {
        $this->tplDirectory = "merchant/views/invoice/";
        // $this->tplDirectory = "merchant/account_config/";
        $this->customerAuth = new Zend_Session_Namespace("customerAuth");
    }

    public function indexAction(){
        echo Ec::renderTpl($this->tplDirectory . "invoice-index.tpl",'noleftlayout');
    }

    public function uploadAction(){
        $order_code = trim($this->_request->getParam('order_code'));
        if(empty($order_code)) {
            header("Content-type: text/html; charset=utf-8");
            exit('请输入订单号');
        }
        $orderRow = Service_Orders::getByField($order_code, 'order_code',"*");
        if(empty($orderRow)) {
            header("Content-type: text/html; charset=utf-8");
            exit('订单号['.$order_code.']不存在');
        }
        if($orderRow['sm_code']!="BH-HKDHL"){
            header("Content-type: text/html; charset=utf-8");
            exit('订单渠道不是BH-HKDHL');
        }
        $address = Service_OrderAddressBook::getByCondition(array('oab_type'=>'0','order_code'=>$order_code),"*");
        if($address[0]['oab_country_id']!='132'&&$address[0]['oab_country_id']!='128'){
            header("Content-type: text/html; charset=utf-8");
            exit('正本发票模板只提供到黎巴嫩和科威特的订单');
        }
        $this->view->address = $address[0];
        $orderProduct = Service_OrderProduct::getByCondition(array('order_code'=>$order_code),"*");
        $totalValue = 0;
        foreach($orderProduct as $key=>$op){
            $product = Service_Product::getByField($op['product_id'],"product_id","*");
            $orderProduct[$key]['hs_code'] = $product['hs_code'];
            $orderProduct[$key]['net_weight'] = $product['product_weight'];
            $orderProduct[$key]['total_value'] = $op['op_quantity']*$op['op_declared_value'];
            $totalValue+=$orderProduct[$key]['total_value'];
        }
        $this->view->orderProduct = $orderProduct;
        $this->view->totalValue = $totalValue;
        $this->view->date = date("Y-m-d");
        if($address[0]['oab_country_id']=="132"){
            $content = $this->view->render($this->tplDirectory."/invoice-LB.tpl");
        }else{
            $content = $this->view->render($this->tplDirectory."/invoice.tpl");
        }
        header( "Pragma: public" );
        header( "Expires: 0" );
        header("Accept-Ranges: bytes");
        header("Content-Disposition: attachment; filename=exportExpress.xls");
        header("Content-Type:APPLICATION/OCTET-STREAM;charset=UTF-8");
        echo $content;
    }

    public function uploadTemplateAction(){
        $file = $this->_request->getParam('file','');
        switch($file){
            case 'KW':
                $fullPath = APPLICATION_PATH."/../data/file".'/BH-HKDHL-KW.xls';
                break;
            case 'LB':
                $fullPath = APPLICATION_PATH."/../data/file".'/BH-HKDHL-LB.xlsx';
                break;
        }
        //方法2
        $filename = basename($fullPath);

        header("Content-Type: APPLICATION/OCTET-STREAM");
        //Force the download
        $header="Content-Disposition: attachment; filename=".$filename.";";
        header($header );
        // 	header("Content-Transfer-Encoding: binary");
        // 	header("Content-Length: ".$len);
        echo file_get_contents($fullPath);
        exit;
    }
}
