<?php
/**
 * 生成商品备案XML 
 * @author solar<solarzheng@cargofe.com.cn>
 */
class Service_ProductXmlProcess
{
	public $version = '1.0';
	public $charset = 'UTF-8';
	public $sendtime = 0;
    public $beianType = "";
	
	private $dom = null;
	private $elEntryRelease = null;
	private $elHead = null;
	private $elBody = null;
	
	private $product_id = 0;
	private $aProduct = array();
	private $aGoods = array();
	
	public function __construct($goodsRow,$type) {
		$this->aGoods = $goodsRow;
		$this->product_id = $goodsRow['product_id'];
		$this->aProduct = Service_Product::getByField($goodsRow['product_id']);
		$this->sendtime = time();
        $this->beianType =$type;
	}
	
	/**
	 * 创建根节点EntryRelease
	 */
	private function createMessage() {
		$this->dom = new DOMDocument($this->version, $this->charset);
		$this->elEntryRelease = $this->dom->createElement('EntryRelease');
		$this->dom->appendChild($this->elEntryRelease);
	}
	
	/**
	 * 创建Head节点
	 */
	private function createHead() {
		$this->elHead = $this->dom->createElement('Head');
		$MessageID = 'QHW035'.date('YmdHis', $this->sendtime).str_pad(rand(1,999999), 6, '0', STR_PAD_LEFT);
		$this->elHead->appendChild($this->dom->createElement('MessageID', $MessageID));
		$this->elHead->appendChild($this->dom->createElement('FunctionCode', '001'));
		$this->elHead->appendChild($this->dom->createElement('MessageType', 'QHW035'));
		$this->elHead->appendChild($this->dom->createElement('SenderID', 'SZCPORT'));
		$this->elHead->appendChild($this->dom->createElement('ReceiverID', 'SZC.FTPA'));
		$this->elHead->appendChild($this->dom->createElement('SendTime', date('Y-m-d H:i:s', $this->sendtime)));
		$this->elHead->appendChild($this->dom->createElement('Version', $this->version));
		$this->elEntryRelease->appendChild($this->elHead);
	}
		
	/**
	 * 创建BODY节点
	 */
	private function createBody($G_MODEL) {
		$this->elBody = $this->dom->createElement('Body');
		$this->elBody->appendChild($this->dom->createElement('SEQ_ID', '1'));
        if($this->beianType=='ZZC'){
            $this->elBody->appendChild($this->dom->createElement('GOODS_ID', $this->aProduct['goods_id']));
            $this->elBody->appendChild($this->dom->createElement('OwnerCompanyID', 'SZBH'));
        }else{
            $this->elBody->appendChild($this->dom->createElement('GOODS_ID', $this->aProduct['goods_id']));
            $this->elBody->appendChild($this->dom->createElement('OwnerCompanyID', 'Cmbl'));
        }

		$this->elBody->appendChild($this->dom->createElement('CODE_TS', $this->aProduct['hs_code']));
		$this->elBody->appendChild($this->dom->createElement('G_NAME', $this->aProduct['hs_goods_name']));
		$this->elBody->appendChild($this->dom->createElement('G_MODEL', $G_MODEL));
        $this->elBody->appendChild($this->dom->createElement('NET_WT', $this->aProduct['product_weight']));
		$this->elBody->appendChild($this->dom->createElement('ENTRY_ID'));
		$this->elBody->appendChild($this->dom->createElement('G_NO'));
		$is_accessories = $this->aGoods['is_accessories']==1 ? 'L' : '';
		$this->elBody->appendChild($this->dom->createElement('AGENT_CODE', $is_accessories));
		$this->elBody->appendChild($this->dom->createElement('IsExport', '1'));
		$this->elBody->appendChild($this->dom->createElement('CompanyShortName', ''));
		$this->elBody->appendChild($this->dom->createElement('FEEDBACK_FLAG'));
		$this->elBody->appendChild($this->dom->createElement('FEEDBACK_MESS'));
		$this->elBody->appendChild($this->dom->createElement('FEEDBACK_DATE'));
		$this->elEntryRelease->appendChild($this->elBody);
	}
	
	private function getGModel() {
		$where['product_id'] = $this->product_id;
		$list = Service_HsElementMap::listByWhere($where);
		$elements = array();
		foreach($list as $row) {
            //如果结尾有竖线，招保会以为申报要素没结束
            if( is_scalar( $row['hem_detail'] ) ) {
                $tmpElement = trim($row['hem_detail']);
                if( $tmpElement!=="" ) {
                    $elements[] = $tmpElement;
                }
            }
		}
		return implode('|', $elements);
	}
		
	/**
	 * 生成XML文档
	 * @param string $path
	 * @return int|string
	 */
	public function generate($path='') {
		if(empty($this->aGoods) || empty($this->aProduct)) return false;
		$this->createMessage();
		$this->createHead();
		$G_MODEL = $this->getGModel();
		if(empty($G_MODEL)) return false;
		$this->createBody($G_MODEL);
		if($path) return $this->dom->save($path);
		return $this->dom->saveXML();
	}
	
}
