<?php
class Receiving_InvoiceController extends Ec_Controller_Action
{
	public function preDispatch()
	{
		$this->tplDirectory = "receiving/views/invoice/";
	}
	
	/**
	 * @author solar zheng
	 * @todo 填写打印内容
	 */
	public function fillInvoiceAction(){
		$order_code = trim($this->_request->getParam('order_code', ''));
		if(empty($order_code)) exit('请输入订单号');
		$orderRow = Service_Orders::getByField($order_code, 'order_code');
		if(empty($orderRow)) exit('找不到订单记录['.$order_code.']');
		if($orderRow['order_status']<8) exit('订单['.$order_code.']目前的状态不能打印发票');

        $allowSM = array('HKDHL','CNDHL','BH-HKDHL','4PX-HKDHL','DHL-TH','WWDHLP','WWDHLU');
		if(!in_array($orderRow['sm_code'],$allowSM ) ){
                    exit('订单['.$order_code.']的运输方式不是'.implode( ',', $allowSM ) );
        } else if ( $orderRow['sm_code'] == 'DHL-TH' ) {
            $address = Service_OrderAddressBook::getByField( $order_code, 'order_code');
            if( empty( $address ) )
                die('找不到订单地址记录');

            $needInvoiceCountry = Service_DHLTHApiProcess::getNeedInvoiceCountry();//这些国家不支持无纸化，所以必须打发票
            if( !in_array( $address['oab_country_id'], $needInvoiceCountry ) ) {
                //die('订单发往的国家支持无纸化，无须打印发票');
            }
        }
		$skuList = Service_OrderProduct::getInvoiceSkuData($order_code);
		$this->view->order_code = $order_code;
		$this->view->skuList = $skuList;
		echo Ec::renderTpl($this->tplDirectory . "invoice_fill.tpl", 'layout');
	}
	
	public function printInvoiceAction() {
		$invoiceType = trim($this->_request->getParam('invoiceType', ''));
		if(empty($invoiceType)) exit('请选择Invoice Type');
		/* $invoiceNumber = trim($this->_request->getParam('invoiceNumber', ''));
		if(empty($invoiceNumber)) exit('请输入Invoice Number'); */
		$termsOfTrade = trim($this->_request->getParam('termsOfTrade', ''));
		if(empty($termsOfTrade)) exit('请选择Terms of Trade');
		$reasonForExport = trim($this->_request->getParam('reasonForExport', ''));
		if(empty($reasonForExport)) exit('请选择Reason for Export');	
		$order_code = trim($this->_request->getParam('order_code', ''));
		if(empty($order_code)) exit('请输入订单号');
		$orderRow = Service_Orders::getByField($order_code, 'order_code');
		if(empty($orderRow)) exit('找不到订单记录['.$order_code.']');
		$proImf = trim($this->_request->getParam('pro_imf', ''));
		$fba_content = '';
		if($orderRow['is_fba'] == '1'){
			$fba_content = 'DDP billed to HKG A/C# 631177584';
		}
		$skuList = Service_OrderProduct::getInvoiceSkuData($order_code);
		$this->view->skuList = $skuList;
		$shipOrder = Service_ShipOrder::getByField($order_code, 'order_code');
		if(empty($shipOrder)) exit('找不到物流记录');
		if(empty($shipOrder['invoice_number'])) {
			$shipOrder['invoice_number'] = Common_GetNumbers::getCode('DHLYDNo','','',true,true);
			Service_ShipOrder::update(array('invoice_number'=>$shipOrder['invoice_number']), $shipOrder['so_id']);
		}
		$this->view->fba_content = $fba_content;
		$this->view->shipDate = date('d-M-Y', strtotime($shipOrder['so_add_time']));
		$this->view->shipOrder = $shipOrder;
		//$this->view->currency_code = $orderRow['currency_code'] == 'RMB' ? 'CNY' : $orderRow['currency_code'];
		$this->view->currency_code = 'USD';
		$this->view->sender = Service_OrderAddressBook::getByWhere(array('order_code'=>$order_code,'oab_type'=>1));
		$this->view->receiver = Service_OrderAddressBook::getByWhere(array('order_code'=>$order_code,'oab_type'=>0));
		$this->view->orderRow = $orderRow;
		//$this->view->invoiceNumber = $invoiceNumber;
		$this->view->invoiceType = $invoiceType;
		$this->view->termsOfTrade = $termsOfTrade;
		$this->view->reasonForExport = $reasonForExport;
		$this->view->proImf = $proImf;
		$this->view->countryKV = Service_Country::getKeyVal('en');
        if( $orderRow['sm_code'] == 'DHL-TH' ) {
            $address = Service_OrderAddressBook::getByField( $orderRow['order_code'], 'order_code' );
            $country = Service_Country::getByField( $address['oab_country_id'], 'country_id' );
            $hubin = Service_DHLTHApiProcess::getHubIn( $country['country_name'] );
            $this->view->invoiceTitle = Service_DHLTHApiProcess::getInvoiceTitle( $hubin );
        }
		echo Ec::renderTpl($this->tplDirectory . "invoice_print.tpl", 'layout');
	}
        	/**
	 * @author 姚
	 * @todo  区外仓hkdhl发票
	 */
	public function logisticOrderInvoiceAction(){
		$order_code = trim($this->_request->getParam('order_code', ''));
		if(empty($order_code)) exit('请输入订单号');
		$orderRow = Service_LogisticsOrder::getByField($order_code, 'logistics_code');
		if(empty($orderRow)) exit('找不到订单记录['.$order_code.']');
		if($orderRow['lo_status']<3) exit('订单['.$order_code.']目前的状态不能打印发票');
		if($orderRow['sm_code']!='HKDHL') exit('订单['.$order_code.']的运输方式不是HKDHL');
		$skuList = Service_LogisticsOrderHs::getByCondition(array('logistics_code'=>$order_code));
		$this->view->order_code = $order_code;
		$this->view->skuList = $skuList;
		echo Ec::renderTpl($this->tplDirectory . "logistic_invoice_fill.tpl", 'layout');
	}
        public function printLogisticsInvoiceAction() {
		$invoiceType = trim($this->_request->getParam('invoiceType', ''));
		if(empty($invoiceType)) exit('请选择Invoice Type');
		/* $invoiceNumber = trim($this->_request->getParam('invoiceNumber', ''));
		if(empty($invoiceNumber)) exit('请输入Invoice Number'); */
		$termsOfTrade = trim($this->_request->getParam('termsOfTrade', ''));
		if(empty($termsOfTrade)) exit('请选择Terms of Trade');
		$reasonForExport = trim($this->_request->getParam('reasonForExport', ''));
		if(empty($reasonForExport)) exit('请选择Reason for Export');	
		$order_code = trim($this->_request->getParam('order_code', ''));
		if(empty($order_code)) exit('请输入订单号');
		$orderRow = Service_LogisticsOrder::getByField($order_code, 'logistics_code');
		if(empty($orderRow)) exit('找不到订单记录['.$order_code.']');
		$skuList = Service_LogisticsOrderHs::getByCondition(array('logistics_code'=>$order_code));
		$this->view->skuList = $skuList;
		$shipOrder = Service_OutsideShipOrder::getByField($order_code, 'logistics_code');
		if(empty($shipOrder)) exit('找不到物流记录');
		if(empty($shipOrder['invoice_number'])) {
			$shipOrder['invoice_number'] = Common_GetNumbers::getCode('','','',true,true);
			Service_OutsideShipOrder::update(array('invoice_number'=>$shipOrder['invoice_number']), $shipOrder['oso_id']);
		}
		$this->view->shipDate = date('d-M-Y', strtotime($shipOrder['add_time']));
		$this->view->shipOrder = $shipOrder;
		//$this->view->currency_code = $orderRow['currency_code'] == 'RMB' ? 'CNY' : $orderRow['currency_code'];
		$this->view->currency_code = 'USD';
		//$this->view->sender = Service_OrderAddressBook::getByWhere(array('order_code'=>$order_code,'oab_type'=>1));
		$this->view->receiver = Service_LogisticsOrderAddress::getByWhere(array('logistics_code'=>$order_code,'loa_type'=>0));
		$this->view->orderRow = $orderRow;
		//$this->view->invoiceNumber = $invoiceNumber;
		$this->view->invoiceType = $invoiceType;
		$this->view->termsOfTrade = $termsOfTrade;
		$this->view->reasonForExport = $reasonForExport;
		$this->view->countryKV = Service_Country::getKeyVal('en');
		echo Ec::renderTpl($this->tplDirectory . "logistic_invoice_print.tpl", 'layout');
	}

    public function pdukInvoiceAction(){
        //$invoiceType = trim($this->_request->getParam('invoiceType', ''));
        //if(empty($invoiceType)) exit('请选择Invoice Type');
        //$invoiceNumber = trim($this->_request->getParam('invoiceNumber', ''));
        //if(empty($invoiceNumber)) exit('请输入Invoice Number');
        //$termsOfTrade = trim($this->_request->getParam('termsOfTrade', ''));
        //if(empty($termsOfTrade)) exit('请选择Terms of Trade');
        //$reasonForExport = trim($this->_request->getParam('reasonForExport', ''));
        //if(empty($reasonForExport)) exit('请选择Reason for Export');
        $order_code = trim($this->_request->getParam('order_code', ''));
        if(empty($order_code)) exit('请输入订单号');
        $orderRow = Service_Orders::getByField($order_code, 'order_code');
        if(empty($orderRow)) exit('找不到订单记录['.$order_code.']');
        $skuList = Service_OrderProduct::getInvoiceSkuData($order_code);
        $this->view->skuList = $skuList;
        $shipOrder = Service_ShipOrder::getByField($order_code, 'order_code');
        if(empty($shipOrder)) exit('找不到物流记录');
        if(empty($shipOrder['invoice_number'])) {
            $shipOrder['invoice_number'] = Common_GetNumbers::getCode('','','',true,true);
            Service_ShipOrder::update(array('invoice_number'=>$shipOrder['invoice_number']), $shipOrder['so_id']);
        }
        $this->view->shipDate = date('d-M-Y', strtotime($shipOrder['so_ship_time']));
        $this->view->shipOrder = $shipOrder;
        //$this->view->currency_code = $orderRow['currency_code'] == 'RMB' ? 'CNY' : $orderRow['currency_code'];
        $this->view->currency_code = 'USD';
        $this->view->sender = Service_OrderAddressBook::getByWhere(array('order_code'=>$order_code,'oab_type'=>1));
        $receiver = Service_OrderAddressBook::getByWhere(array('order_code'=>$order_code,'oab_type'=>0));
        $this->view->receiver = $receiver;
        $this->view->country = Service_Country::getByField($receiver['oab_country_id'],'country_id',"*");
        $this->view->orderRow = $orderRow;
        $opList = Service_OrderProduct::getByCondition(array('order_code'=>$order_code),"*");
        $totalOpprice = 0;
        $totalPieces = 0;
        foreach($opList as $key=>$op){
            $gbp = Service_Currency::converByCode('USD','GBP',$op['op_purpose_declared_value']);
            $opList[$key]['op_purpose_gbp'] = $gbp;
            $opList[$key]['op_purpose_gbp_total'] = $gbp*$op['op_quantity'];
            $totalOpprice+=$gbp*$op['op_quantity'];
            $totalPieces+=$op['op_quantity'];
        }
        $this->view->totalOpprice = $totalOpprice;
        $this->view->totalPieces = $totalPieces;
        $this->view->opList = $opList;
        //$this->view->invoiceNumber = $invoiceNumber;
        //$this->view->invoiceType = $invoiceType;
        //$this->view->termsOfTrade = $termsOfTrade;
        //$this->view->reasonForExport = $reasonForExport;
        //$this->view->countryKV = Service_Country::getKeyVal('en');
        echo Ec::renderTpl($this->tplDirectory . "pduk_invoice.tpl", 'layout');
    }
	
}