<?php
/**
 * 生成入库单报文XML 
 * @author solar<solarzheng@cargofe.com.cn>
 */
class Service_ReceiveXmlProcess
{
	public $version = '1.0';
	public $sendtime = '';
	public $MessageID = 0;
	public $formID = '';
	
	private $dom = null;
	private $elMessage = null;
	private $elHead = null;
	private $elDeclaration = null;
	private $elBillHead = null;
	
	private $receivingCode = '';	//ASN单号
	private $receivingRow = array();	//ASN信息
	private $fmtRow = array();	//ftp_message_type
	private $productList = array();	//产品行
	private $customerRow = array(); //客户信息
	private $currencyRow = array();
	private $netWeight = 0.00;	//净重
	private $kvCurrency = array();
	
	public function __construct($receiving_code) {
		$this->formID = $receiving_code;
		$this->receivingCode = $receiving_code;
		$this->receivingRow = Service_Receiving::getByField($receiving_code, 'receiving_code');
		$this->customerRow = Service_Customer::getByField($this->receivingRow['customer_id']);
		$this->currencyRow = Service_Currency::getByField($this->customerRow['customer_currency'], 'currency_code');
		$this->fmtRow = Service_FtpMessageType::getByField('MRK', 'fmt_code');
		if($this->receivingRow['receive_model_type']==0) {
			$this->productList = Service_ReceivingDetail::getXmlProducts($receiving_code);
			$this->netWeight = number_format(Service_ReceivingDetail::sumNetWeight($receiving_code),2,'.','');
		} else {
			$orderCodes = Service_ReceivingOrderDetail::getOrderCodes($receiving_code);
			$this->productList = Service_ReceivingOrderDetail::getXmlProducts($orderCodes, $receiving_code);
			$this->netWeight = number_format(Service_ReceivingOrderDetail::sumNetWeight($receiving_code),2,'.','');
		}
		$this->kvCurrency = Service_Currency::getKeyVal();
		//发送时间
		$this->sendtime = date('YmdHis').str_pad(rand(1,888), 3, '0', STR_PAD_LEFT);
		//MessageID
		$this->MessageID = ++$this->fmtRow['fmt_form_id'];
		Service_FtpMessageType::update(array('fmt_form_id'=>$this->MessageID), $this->fmtRow['fmt_id']);
	}
	
	/**
	 * 创建根节点Message
	 */
	private function createMessage() {
		$this->dom = new DOMDocument('1.0', 'UTF-8');
		$this->elMessage = $this->dom->createElement('Message');
		$this->elMessage->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
		$this->dom->appendChild($this->elMessage);
	}
	
	/**
	 * 创建Head节点
	 */
	private function createHead() {
		$this->elHead = $this->dom->createElement('Head');
		$this->elHead->appendChild($this->dom->createElement('MessageID', $this->MessageID));
		$this->elHead->appendChild($this->dom->createElement('FunctionCode', '2'));
		$this->elHead->appendChild($this->dom->createElement('MessageType', $this->fmtRow['fmt_type']));
		$this->elHead->appendChild($this->dom->createElement('SenderID', $this->fmtRow['fml_sender_id']));
		$this->elHead->appendChild($this->dom->createElement('ReceiverID', $this->fmtRow['fml_receiver_id']));
		$this->elHead->appendChild($this->dom->createElement('SendTime', $this->sendtime));
		$this->elHead->appendChild($this->dom->createElement('Version', $this->version));
		$this->elMessage->appendChild($this->elHead);
	}
	
	/**
	 * 创建Declaration节点
	 */
	private function createDeclaration() {
		$this->elDeclaration = $this->dom->createElement('Declaration');
		$this->elMessage->appendChild($this->elDeclaration);		
	}
	
	/**
	 * 创建WAREHOUSE_BILL_HEAD节点
	 */
	private function createBillHead() {
		$this->elBillHead = $this->dom->createElement('WAREHOUSE_BILL_HEAD');
		$this->elBillHead->appendChild($this->dom->createElement('SERIAL_NO', $this->receivingRow['receiving_id']));
		$this->elBillHead->appendChild($this->dom->createElement('FORM_ID', $this->formID));
		$this->elBillHead->appendChild($this->dom->createElement('DECL_PORT', $this->fmtRow['fmt_decl_port']));
		$this->elBillHead->appendChild($this->dom->createElement('I_E_PORT', $this->receivingRow['ie_port']));
		$this->elBillHead->appendChild($this->dom->createElement('I_E_MODE', $this->fmtRow['fmt_ie_mode']));
		$this->elBillHead->appendChild($this->dom->createElement('PS_TYPE', $this->fmtRow['fml_ps_type']));
		$this->elBillHead->appendChild($this->dom->createElement('RELATIVE_FORM_TYPE', $this->fmtRow['relative_form_type']));
		$this->elBillHead->appendChild($this->dom->createElement('FORM_TYPE', $this->fmtRow['form_type']));
		$this->elBillHead->appendChild($this->dom->createElement('EMS_NO', $this->fmtRow['fml_ems_no']));
		$this->elBillHead->appendChild($this->dom->createElement('EMS_COP_NO'));
		$this->elBillHead->appendChild($this->dom->createElement('TRAF_NAME'));
		$this->elBillHead->appendChild($this->dom->createElement('VOYAGE_NO'));
		$this->elBillHead->appendChild($this->dom->createElement('BILL_NO'));
		$this->elBillHead->appendChild($this->dom->createElement('GROSS_WT', $this->receivingRow['roughweight']));
		$this->elBillHead->appendChild($this->dom->createElement('NET_WT', $this->netWeight));
		$this->elBillHead->appendChild($this->dom->createElement('PACK_NO', $this->receivingRow['pack_no']));
		$this->elBillHead->appendChild($this->dom->createElement('WRAP_TYPE', $this->receivingRow['wrap_type']));
		$this->elBillHead->appendChild($this->dom->createElement('I_E_DATE'));
		$this->elBillHead->appendChild($this->dom->createElement('TRAF_MODE', $this->receivingRow['traf_mode']));
		$this->elBillHead->appendChild($this->dom->createElement('TRADE_CO', $this->customerRow['trade_co']));	//原来是5875344345
		$this->elBillHead->appendChild($this->dom->createElement('TRADE_NAME', $this->customerRow['trade_name']));
		$this->elBillHead->appendChild($this->dom->createElement('TRADE_MODE', $this->receivingRow['trade_mode']));
		$this->elBillHead->appendChild($this->dom->createElement('TRANS_MODE', $this->receivingRow['trans_mode']));
		$this->elBillHead->appendChild($this->dom->createElement('AGENT_CODE', $this->fmtRow['agent_code']));
		$this->elBillHead->appendChild($this->dom->createElement('AGENT_NAME', $this->fmtRow['agent_name']));
		$this->elBillHead->appendChild($this->dom->createElement('TRADE_COUNTRY', '142'));
		$this->elBillHead->appendChild($this->dom->createElement('OWNER_CODE', $this->customerRow['trade_co']));
		$this->elBillHead->appendChild($this->dom->createElement('OWNER_NAME', $this->customerRow['trade_name']));
		$this->elBillHead->appendChild($this->dom->createElement('NOTE_S'));
		$this->elBillHead->appendChild($this->dom->createElement('RELATIVE_FORM_ID'));
		$this->elBillHead->appendChild($this->dom->createElement('RELATIVE_FORM_STATUS'));
		$this->elBillHead->appendChild($this->dom->createElement('INPUT_DATE'));
		$this->elBillHead->appendChild($this->dom->createElement('INPUT_NO'));
		$this->elBillHead->appendChild($this->dom->createElement('FORM_STATUS'));
		$this->elBillHead->appendChild($this->dom->createElement('D_DATE'));
		$this->elBillHead->appendChild($this->dom->createElement('CHK_DATE'));
		$this->elBillHead->appendChild($this->dom->createElement('CHK_STATUS'));
		$this->elBillHead->appendChild($this->dom->createElement('AUDIT_DATE'));
		$this->elBillHead->appendChild($this->dom->createElement('CAN_BINDING_DATE'));
		$this->elBillHead->appendChild($this->dom->createElement('CAN_CHANNEL_DATE'));
		$this->elBillHead->appendChild($this->dom->createElement('DEDUC_DATE'));
		$this->elBillHead->appendChild($this->dom->createElement('COP_GB_CODE', $this->customerRow['trade_co']));
		$this->elBillHead->appendChild($this->dom->createElement('EMC_EPORTCARD_ID'));
		$this->elBillHead->appendChild($this->dom->createElement('SIGN'));
		$this->elBillHead->appendChild($this->dom->createElement('DESTINATION_PORT', '142'));
		$this->elBillHead->appendChild($this->dom->createElement('TURN_NO'));
		$this->elBillHead->appendChild($this->dom->createElement('CUS_CODE', $this->fmtRow['outbound_trade_co']));
		$this->elBillHead->appendChild($this->dom->createElement('CUS_NAME', $this->fmtRow['outbound_trade_name']));
		/*$this->elBillHead->appendChild($this->dom->createElement('CUS_CODE', $this->customerRow['cus_code']));
		$this->elBillHead->appendChild($this->dom->createElement('CUS_NAME', $this->customerRow['cus_name']));*/
		$this->elBillHead->appendChild($this->dom->createElement('DECLARE_REMARK'));
		$this->elDeclaration->appendChild($this->elBillHead);
	}
	
	/**
	 * 创建WAREHOUSE_BILL_CONTAINER节点
	 * @return DOMElement
	 */
	private function createBillContainer() {
		$elBillContainer = $this->dom->createElement('WAREHOUSE_BILL_CONTAINER');
		$elBillContainer->appendChild($this->dom->createElement('FORM_ID', $this->sb_code));
		$elBillContainer->appendChild($this->dom->createElement('CONTA_ID', $this->sbaRow['conta_id']));
		$elBillContainer->appendChild($this->dom->createElement('CONTA_MODEL', $this->sbaRow['conta_model']));
		$elBillContainer->appendChild($this->dom->createElement('CONTA_WT', $this->sbaRow['conta_wt']));
		$this->elBillHead->appendChild($elBillContainer);		
	}
	
	/**
	 * 创建WAREHOUSE_BILL_LIST节点
	 * @param int $no
	 * @param array $info
	 * @return DOMElement
	 */
	private function createBillList($no, $info) {
		$elBillList = $this->dom->createElement('WAREHOUSE_BILL_LIST');
		$elBillList->appendChild($this->dom->createElement('FORM_ID', $this->formID));
		$elBillList->appendChild($this->dom->createElement('G_NO', $no));
		$elBillList->appendChild($this->dom->createElement('GOODS_ID', $info['goods_id']));
		$elBillList->appendChild($this->dom->createElement('MERGER_G_NO', $no));
		$elBillList->appendChild($this->dom->createElement('MERGER_MODE', 1));	//固定传1
		$elBillList->appendChild($this->dom->createElement('CODE_TS', $info['hs_code']));
		$elBillList->appendChild($this->dom->createElement('G_NAME_CN', $info['hs_goods_name']));
		$hemList = Service_HsElementMap::getByCondition(array('product_id'=>$info['product_id']), 'hem_detail');
		$elements = array();
		foreach($hemList as $hemRow) $elements[] = $hemRow['hem_detail'];
		$elBillList->appendChild($this->dom->createElement('G_MODEL', implode('|',$elements)));
		$elBillList->appendChild($this->dom->createElement('G_NAME_EN', ''));
		$QTY = $info['hum_quantity_law'] * $info['quantity'];
		$elBillList->appendChild($this->dom->createElement('G_QTY', $QTY));
		//$elBillList->appendChild($this->dom->createElement('G_UNIT', str_pad($info['pu_code'], 3, '0', STR_PAD_LEFT)));
		$elBillList->appendChild($this->dom->createElement('G_UNIT', $info['pu_code_law']));
		$DECL_PRICE = sprintf("%.2f", $info['product_declared_value']/$info['hum_quantity_law']);
		$elBillList->appendChild($this->dom->createElement('DECL_PRICE', $DECL_PRICE));
		//$elBillList->appendChild($this->dom->createElement('CURR', $this->customerRow['currency_code']));		
		$elBillList->appendChild($this->dom->createElement('CURR', $this->kvCurrency[$this->currencyRow['currency_code']]));
		$elBillList->appendChild($this->dom->createElement('QTY_1', $QTY));
		$elBillList->appendChild($this->dom->createElement('UNIT_1', $info['pu_code_law']));
		$hum_quantity_second = $info['pu_code_second'] ? $info['hum_quantity_second'] : '';
		$elBillList->appendChild($this->dom->createElement('QTY_2', $hum_quantity_second));
		$elBillList->appendChild($this->dom->createElement('UNIT_2', $info['pu_code_second']));
		$elBillList->appendChild($this->dom->createElement('ORIGIN_COUNTRY', '110'));	//固定110
		$elBillList->appendChild($this->dom->createElement('DUTY_MODE', '1'));	//固定1
		$elBillList->appendChild($this->dom->createElement('USE_TO', '11'));	//固定11
		$elBillList->appendChild($this->dom->createElement('NOTE_S', ''));
		$elBillList->appendChild($this->dom->createElement('CAR_NO', ''));
		$elBillList->appendChild($this->dom->createElement('URM_NO', ''));
		$elBillList->appendChild($this->dom->createElement('BIND_DATE', ''));
		$elBillList->appendChild($this->dom->createElement('CHANNEL_DATE', ''));
		$decl_total = sprintf("%.2f", $info['product_declared_value']*$info['quantity']);
		$elBillList->appendChild($this->dom->createElement('DECL_TOTAL', $decl_total));
		return $elBillList;
	}
	
	/**
	 * 生成XML文档
	 * @param string $path
	 * @return int|string
	 */
	public function generate($path='') {
		$this->createMessage();
		$this->createHead();
		$this->createDeclaration();
		$this->createBillHead();
		foreach($this->productList as $no=>$product) {
			++$no;
			$this->elBillHead->appendChild($this->createBillList($no, $product));
		}
		//$this->createBillContainer();
		if($path) return $this->dom->save($path);
		return $this->dom->saveXML();
	}
	
}
