<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 18-9-19
 * Time: 上午11:20
 * To change this template use File | Settings | File Templates.
 */
class Service_OrderXmlProcess
{

    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 $elBillBody = null;

    //private $sb_code = '';
    private $sbRow = array();
    //private $sbaRow = array();	//ship_batch_attribute
    //private $fmtRow = array();	//ftp_message_type
    //private $fmtRows = array();	//ftp_message_type
    private $productList = array();
    //private $customerRow = array();
    //private $gross_weight = 0;
    //private $net_weight = 0;
    //private $kvCurrency = array();
    //private $totalPrice = 0;	//总价

    public function __construct($sendRow) {
        $this->sbRow = $sendRow;
        $this->productList = Service_OrderProduct::getByCondition(array('order_code'=>$sendRow['order_code']),"*",0,0);
        $this->sendtime = date('YmdHis').str_pad(rand(1,888), 3, '0', STR_PAD_LEFT);
    }

    private function createMessage() {
        $this->dom = new DOMDocument('1.0', 'UTF-8');
        $this->elMessage = $this->dom->createElement('EntryRelease');
        //$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', 1));
        $this->elHead->appendChild($this->dom->createElement('FunctionCode', '2'));
        $this->elHead->appendChild($this->dom->createElement('MessageType', 'JK'));
        $this->elHead->appendChild($this->dom->createElement('SenderID', 'BH'));
        $this->elHead->appendChild($this->dom->createElement('ReceiverID', 'Cmbl'));
        $this->elHead->appendChild($this->dom->createElement('SendTime', $this->sendtime));
        $this->elHead->appendChild($this->dom->createElement('Version', $this->version));
        $this->elMessage->appendChild($this->elHead);
    }

    private function createBody(){
        $this->elBillBody = $this->dom->createElement('Body');
        $this->elMessage->appendChild($this->elBillBody);
    }

    private function createBillList( $info) {
        $elBillList = $this->dom->createElement('SummaryOrder');
        $product = Service_Product::getByField($info['product_id'],"product_id","*");
        $elBillList->appendChild($this->dom->createElement('FORM_ID', $this->sbRow['refercence_no']));
        $elBillList->appendChild($this->dom->createElement('FORM_ID_XML', $this->sbRow['invt_no']));
        $elBillList->appendChild($this->dom->createElement('ORDER_ID', $info['op_id']));
        $elBillList->appendChild($this->dom->createElement('GOODS_ID', $product['goods_id']));
        $elBillList->appendChild($this->dom->createElement('G_QTY', $info['op_quantity']));	//固定传1
        return $elBillList;
    }

    public function generate($path='') {
        if(empty($this->productList)) return false;
        $this->createMessage();
        $this->createHead();
        $this->createBody();
        $no = 0;
        foreach($this->productList as $product) {
            ++$no;
            $this->elBillBody->appendChild($this->createBillList($product));
        }
        //$this->createBillContainer();
        //$nodeList = $this->elBillHead->getElementsByTagName('NOTE_S');
        //if($nodeList->length == 0) return false;
        //$consignee = '收货人：'.$this->sbaRow['consignee'];
        //$total = '总价：'.sprintf("%.2f", $this->totalPrice);
        //$nodeList->item(0)->nodeValue = $consignee.'  '.$total;
        if($path) return $this->dom->save($path);
        return $this->dom->saveXML();
    }
}
