<?php
class merlinXML {
	private $p; // paczka
	private $date;
	function merlinXML($date) {
		$this->date = $date;
		$this->p = new XMLWriter ();
		$this->p->openURI ( 'cache/file.xml' );
		// $this->p->openMemory();
		$this->p->startDocument ( '1.0', 'UTF-8' );
		$this->p->startElement ( 'PRODUCTS' );
		if($date!=''){
			$this->p->writeAttribute('create-date', date(DATE_ATOM));
		}
		$this->p->writeAttribute('currency-code', 'PLN');
		
	}
	function addProduct($id){
		$db = $GLOBALS ['db'];
		$product = $db->query ("select * from ecmproducts where id='".$id."'");
		$i = $db->fetchByAssoc ( $product );
		$this->p->startElement ( 'PRODUCT' );
			$this->p->writeAttribute('group-id', '???');
			$this->p->writeAttribute('group-property', '???');
			$this->p->writeAttribute('availability', '???');
			
		$this->p->writeElement( 'ID',$i['id'] );
		
		$this->p->startElement('NAME');
		$this->p->writeCData(html_entity_decode($i['name']));
		$this->p->endElement();
		
		$this->p->writeElement( 'EAN',$i['ean'] );
		$this->p->writeElement( 'PRICE','???' ); // cena zakupu merlin
		$this->p->writeElement( 'MARKET_PRICE','???' ); // cena detaliczna brutto
		
		//vat
		$v = new EcmVat;
		$v->retrieve($i['vat_id']);
		$vat=substr($v->name, 0, -1);
		$this->p->writeElement( 'VAT',$vat );
		
		$this->p->writeElement( 'DISCOUNT','???' ); // UPUST jesli istnieje
		
		//opis
		$desc = $db->query ("select * from ecmproduct_language_pl_view where ecmproduct_id='".$id."'");
		$opis = $db->fetchByAssoc ( $desc );
		$this->p->startElement('DESCRIPTION');
		$this->p->writeCData( html_entity_decode($opis['long_description']));
		$this->p->endElement();
		
		$this->p->writeElement( 'DATE',date('Y-m-d',strtotime($i['date_entered'])) ); // data utworzenia produktu

		//kategorie
		$catq = $db->query ("select c.name,c.id from ecmproductcategories_bean as b
join ecmproductcategories as c on b.ecmproductcategory_id=c.id
 where b.bean_id='".$id."'");
		$this->p->startElement ( 'CATEGORIES' );
		while($cat = $db->fetchByAssoc ( $catq )){
			$this->p->startElement ( 'CATEGORY');
				$this->p->writeAttribute ( 'id', $cat['id'] );
				$this->p->text($cat['name']);
				
			$this->p->endElement();
		}
		$this->p->endElement();
		
		//firma
		$this->p->startElement ( 'FIRMS' );
			$this->p->startElement ( 'FIRM');
			$this->p->writeAttribute ( 'role', 'producent' );
			$this->p->writeCData('E5 Polska Sp. z o.o.');
			$this->p->endElement();
		$this->p->endElement();
		
		// opakowanie
		$this->p->startElement ( 'PACKAGE' );
		$this->p->startElement ( 'DIMENSIONS');
			$this->p->writeAttribute ( 'height', $i['packing_dimensions_1'] );
			$this->p->writeAttribute ( 'width', $i['packing_dimensions_2'] );
			$this->p->writeAttribute ( 'depth', $i['packing_dimensions_3'] );
			$this->p->writeAttribute ( 'unit', 'cm' );
		$this->p->endElement ();
		$this->p->endElement ();
		
		//stany magazynowe
		$this->p->startElement ( 'STOCK' );
		$this->p->text('???'); // REALNE, T czy N
		$this->p->endElement ();
		
		//zdjecia
		$this->p->startElement ( 'IMAGES' );
		$this->p->startElement ( 'IMAGE');
		$this->p->writeAttribute ( 'name', 'zdjęcie' );
		$this->p->writeCData('http://???.??/????.jpg');
		$this->p->endElement();
		$this->p->endElement();
		//url
		$this->p->startElement ( 'URL_PRODUCT' );
		$this->p->writeCData('http://???.??/????.jpg');
		$this->p->endElement();

		
		$this->p->endElement ();

	}
	function getXML() {
		$this->p->endElement (); // </produkty>
		$this->p->flush ();
	}
}

function e($value) {
	if (!is_null($value) && $value!='')
		return $value;
	else 
		return ' ';
}


?>