<?php
class Service_InvoiceProcess
{
	##此处是环球的label ftp地址，将环球的pdf文件传到改ftp上。
	private $_ftpConfig = array(
			'hostname' => '120.76.159.123',
			'username' => 'hqlabel',
			'password' => 'hqlab$2016#bh',
			'port' => 21
	);
	protected $_ftpDir = 'invoice/';
	
	private $error = '';
	private $result = array('ask'=>0, 'message'=>'');
    protected $xmlProcessObj='';
	protected $dhlDataPath;
	protected $invoiceFile;

	public function __construct($order_code='') {
		$this->order_code = $order_code;
	}
	private function initDHLInvoicePath() {
		$this->dhlDataPath = APPLICATION_PATH.'/../data/dhlinvoice/'.date('Y/m');
		if(!file_exists($this->dhlDataPath)) {
			mkdir($this->dhlDataPath, 0777, true);
		}
	}
	public function getInvoice(){
		//error_reporting(0);
		$invoiceType = 'Commercial Invoice';//Invoice Type默认为Commercial Invoice
		$termsOfTrade = 'FOB'; //Terms of Trade默认为FOB
		$reasonForExport ='Pamanent'; //Pamanent默认为Pamanent

		$config = Zend_Registry::get('config');
		$view = new Ec_View_Smarty($config->smartyConfig->template_dir, $config->smartyConfig->config);
		$this->view = $view;

		$order_code = $this->order_code;
		if(empty($order_code)) exit('请输入订单号');
		$orderRow = Service_Orders::getByField($order_code, 'order_code');
		if(empty($orderRow)) exit('找不到订单记录['.$order_code.']');
		$proImf = '';//电池说明
		$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');
		//$html = Ec::renderTpl('receiving/views/invoice/' . "invoice_print.tpl", 'layout');
		$html = $this->view->render('receiving/views/invoice/' . "invoice_print.tpl");
		$this->html=$html;

		$this->invoiceFile = $this->dhlDataPath.'/'.'invoice-'.$orderRow['reference_no'].'.html';
		if(file_put_contents($this->invoiceFile,$html)===false){
			return false;
		}
		return true;
	}
	//发票发给环球
	public function sendInvoice(){
		$this->initDHLInvoicePath();
		$invoice = $this->getInvoice();
		if($invoice===false){
			$this->error = '生成发票失败';
			return false;
		}
		//此处发给环球ftp为了简便直接拷贝了，应该用ftp传过去
		$ftp = new Common_Ftp($this->_ftpConfig);
		//连接FTP
		if(!$ftp->connect()){
			$this->error = join('-', $ftp->getError());
			return false;
		}
		//切换至对应目录
		if(!$ftp->chgdir($this->_ftpDir, true)){
			$this->error = join('-', $ftp->getError());
			return false;
		}
		//上传文件
		if(!$ftp->upload($this->invoiceFile, basename($this->invoiceFile))){
			$this->error = join('-', $ftp->getError());
			return false;
		}
		/*if(@copy($this->getPdfFilePath(), '/home/hqlabel/')===false) {
			$this->error = '移动PDF文件失败:'.$pdfPath;
			return false;
		}*/
		return true;
	}

	//记录发票是否发送成功
	public function getResult() {
		$return = $this->sendInvoice();
		if($return){
			$this->result['message'] = '发票发送成功';
			$this->result['ask'] = 1;
			$invoiceRow = array(
					'invoice_path'=>$this->invoiceFile,
					'update_time'=>date("Y-m-d H:i:s",time())
			);
		}else{
			$this->result['message'] = $this->error;
			$this->result['ask'] = 0;
			$invoiceRow = array(
					'invoice_status'=>'0',
					'update_time'=>date("Y-m-d H:i:s",time())
			);
		}
		Service_ApiDhl::update($invoiceRow,$this->order_code,'order_code');
		return $this->result;
	}



	
}