<?php
class maksymaXML {
	private $p; // paczka
	private $date;
	function maksymaXML($date) {
		$this->date = $date;
		$this->p = new XMLWriter ();
		$this->p->openURI ( 'php://output' );
		// $this->p->openMemory();
		$this->p->startDocument ( '1.0', 'UTF-8' );
		$this->p->startElement ( 'paczka' );
		$this->p->writeAttribute ( 'firma', 'E5 Polska sp z o.o.' );
		$this->p->writeAttribute ( 'wersja', '1' );
		$this->p->writeAttribute ( 'miesiac', substr($this->date,5,6));
		
	}
	function getInvoices() {
		// co z anulowanymi?? :(
		$db = $GLOBALS ['db'];
		$invoices = $db->query ( "SELECT * FROM ecminvoiceoutolds WHERE deleted='0' AND type='normal' AND register_date LIKE '$this->date%'" );
		while ( $i = $db->fetchByAssoc ( $invoices ) ) {
			$this->p->startElement ( 'dokument' );
			$this->p->writeAttribute ( 'rodzaj', ($i['canceled']=='1'?'!':'').'FVS' . $i ['pdf_type'] );
			$this->p->writeAttribute ( 'id', $i['id'] );
			$this->p->writeElement ( 'data', $i ['register_date'] );
			// dataOperacji = Data WZ? co jeśli WZ jest więcej
			$this->p->writeElement ( 'dataOperacji', $i ['register_date'] );
			// same story
			$this->p->writeElement ( 'dataDostawy', $i ['register_date'] );
			$this->p->writeElement ( 'dataWpływu', $i ['register_date'] );
			$this->p->writeElement ( 'numer', $i ['document_no'] );
			//platnosc
			$this->p->startElement('platnosc');
				$this->p->writeElement('kwota', $i['total']);
				$this->p->writeElement('data', $i['payment_date']);
			$this->p->endElement(); //</platnosc>
			
			//kontrahent
			$this->p->startElement ( 'kontrahent' );
			$this->p->writeAttribute ( 'id', $i ['parent_id'] );
					$this->p->writeElement('NIP', e(str_replace ( "-", "", $i ['to_nip'] )));
					$this->p->writeElement('nazwa', htmlspecialchars($i['parent_name']));
					$this->p->writeElement('adres', htmlspecialchars($i['parent_address_street']));
					//jaki numer??
					$this->p->writeElement('kodPocztowy', htmlspecialchars($i['parent_address_postalcode']));
					$this->p->writeElement('miasto', htmlspecialchars($i['parent_address_city']));
					$this->p->writeElement('kraj', htmlspecialchars($i['parent_address_country']));				
			$this->p->endElement (); // </kontrahent>
			//WZ
			//co dalej z wz
			$wz = $db->fetchByAssoc($db->query("SELECT id, document_no, subtotal, register_date, so_id FROM ecmstockdocouts WHERE id = '".$i['wz_id']."'"));
			$this->p->startElement('dokumentyPowiazane');
				$this->p->startElement('dokumentPowiazany');
					$this->p->writeAttribute('rodzaj', 'WZ');
					$this->p->writeAttribute ( 'id', $wz['id'] );
					$this->p->writeElement('numer', $wz['document_no']);
					$this->p->writeElement('data', $wz['register_date']);
					$this->p->writeElement('wartoscNetto', $wz['subtotal']);
				$this->p->endElement(); // </dokumentPowiazany>
			$this->p->endElement(); //</dokumentyPowiazane>
			//pozycje dokumentu
			$pozycje = $db->query("
		select sum(i.subtotal) as subtotal, p.group_ks, sum(i.quantity) as qty from ecminvoiceoutolditems as i
inner join ecmproducts as p
on p.id=i.ecmproduct_id
where i.ecminvoiceoutold_id='".$i['id']."'
group by p.group_ks;
		");
			$pp = 0;
			global $app_list_strings;
			$this->p->startElement('pozycjeDokumentu');
			while ($poz = $db->fetchByAssoc($pozycje)) {
				$pp++;
				if ($poz['group_ks']=='1')
					$rodzaj = 'T';
				else $rodzaj = 'P';

				$this->p->startElement('pozycjaDokumentu');
				$this->p->writeElement('tresc', $app_list_strings['ecmproducts_group_ks_dom'][$poz['group_ks']]);
				$this->p->writeElement('wartoscNetto', round($poz['subtotal'],2));
				$this->p->writeElement('wartoscBrutto', '');
				$this->p->writeElement('wartoscVAT', '');
				$this->p->endElement(); // </pozycjaDokumentu>				
			}
			$this->p->endElement(); // </pozycjeDokumentu>
			//rozbicie
			$this->p->startElement('rejestr');
			$rozbicie = $db->query("
				select sum(i.subtotal) as subtotal, sum(i.total) as total, v.name, v.value from ecminvoiceoutolditems as i
inner join ecmvats as v
on v.id = i.ecmvat_id
where
i.ecminvoiceoutold_id='".$i['id']."'
group by v.id;
				");
			
			while ($roz = $db->fetchByAssoc($rozbicie)) {
				$this->p->startElement('rozbicie');
					$this->p->writeAttribute('stawka', $roz['name']);
					$this->p->writeElement('netto', round($roz['subtotal'],2));
					$this->p->writeElement('brutto', round($roz['total'],2));
					$this->p->writeElement('VAT', round ($roz['total'] - $roz['subtotal'], 2 ));
					
				$this->p->endElement(); // </rozbicie>
			}
			$this->p->endElement(); // </rejestr>
			
			if ($i['pdf_type']=='K') {
				$this->p->writeElement('sumaNetto', round($i['subtotal'],2));
				$this->p->writeElement('sumaBrutto', round($i['total'],2));
				$this->p->writeElement('sumaVat', round($i['total']-$i['subtotal'],2));
			} else {
				$this->p->writeElement('sumaNetto', round($i['subtotal']*$i['currency_value'],2));
				$this->p->writeElement('sumaBrutto', round($i['total']*$i['currency_value'],2));
				$this->p->writeElement('sumaVat', round(($i['total']-$i['subtotal'])*$i['currency_value'],2));
				
				$this->p->writeElement('sumaWalNetto', round($i['subtotal'],2));
				$this->p->writeElement('sumaWalBrutto', round($i['total'],2));
				$this->p->writeElement('sumaWalVat', round($i['total']-$i['subtotal'],2));
				
				//kursy
				$this->p->startElement('kursy');
					//data dostawy
					$cur = $db->fetchByAssoc($db->query("
						SELECT * FROM currency_nbp_archive 
							WHERE currency_id = '".$i['currency_id']."'
							AND date < DATE_SUB((SELECT delivery_date FROM ecmsales WHERE id='".$wz['so_id']."'), INTERVAL 1 DAY)
							ORDER BY date DESC
							LIMIT 0,1;	
					"));
					$this->p->startElement('kurs');
						$this->p->writeElement('waluta', $cur['currency_name']);
						$this->p->writeAttribute('zastosowanie', 'dataDostawy');
						$this->p->writeElement('rodzaj', 'NBP');
						$this->p->writeElement('naDzien', $cur['date']);
						$this->p->writeElement('wartosc', $cur['value']);
					$this->p->endElement(); // </kurs>
					//data transakcji
					$cur = $db->fetchByAssoc($db->query("
						SELECT * FROM currency_nbp_archive
							WHERE currency_id = '".$i['currency_id']."'
							AND date < DATE_SUB('".$i['register_date']."', INTERVAL 1 DAY)
							ORDER BY date DESC
							LIMIT 0,1;
					"));
					$this->p->startElement('kurs');
					$this->p->writeElement('waluta', $cur['currency_name']);
					$this->p->writeAttribute('zastosowanie', 'dataTransakcji');
					$this->p->writeElement('rodzaj', 'NBP');
					$this->p->writeElement('naDzien', $cur['date']);
					$this->p->writeElement('wartosc', $cur['value']);
					$this->p->endElement(); // </kurs>
				$this->p->endElement(); // </kursy>
			}

			$this->p->endElement (); // </dokument>
		}
	}
	function getCorrectInfoices() {
		$db = $GLOBALS ['db'];
		$invoices = $db->query ( "SELECT * FROM ecminvoiceoutolds WHERE deleted='0' AND type='correct' AND register_date LIKE '$this->date%'" );
		while ( $i = $db->fetchByAssoc ( $invoices ) ) {
			$this->p->startElement ( 'dokument' );
			$this->p->writeAttribute ( 'rodzaj', ($i['canceled']=='1'?'!':'').'FKS' . $i ['pdf_type'] );
			$this->p->writeAttribute ( 'id', $i['id'] );
			$this->p->writeElement ( 'data', $i ['register_date'] );
			// dataOperacji = Data WZ? co jeśli WZ jest więcej
			$this->p->writeElement ( 'dataOperacji', $i ['register_date'] );
			// same story
			$this->p->writeElement ( 'dataDostawy', $i ['register_date'] );
			$this->p->writeElement ( 'dataWpływu', $i ['register_date'] );
			$this->p->writeElement ( 'numer', $i ['document_no'] );
			//kontrahent
			$this->p->startElement ( 'kontrahent' );
			$this->p->writeAttribute ( 'id', $i ['parent_id'] );
			$this->p->writeElement('NIP', e(str_replace ( "-", "", $i ['to_nip'] )));
			$this->p->writeElement('nazwa', htmlspecialchars($i['parent_name']));
			$this->p->writeElement('adres', htmlspecialchars($i['parent_address_street']));
			//jaki numer??
			$this->p->writeElement('kodPocztowy', htmlspecialchars($i['parent_address_postalcode']));
			$this->p->writeElement('miasto', htmlspecialchars($i['parent_address_city']));
			$this->p->writeElement('kraj', htmlspecialchars($i['parent_address_country']));
			$this->p->endElement (); // </kontrahent>
			
			//platnosc
			$this->p->startElement('platnosc');
			$this->p->writeElement('kwota', $i['total']);
			$this->p->writeElement('data', $i['payment_date']);
			$this->p->endElement(); //</platnosc>
			
			//multi correct?
			$multi = $db->query ( "
SELECT distinct o.ecminvoiceoutold_id
FROM
    ecminvoiceoutolditems as ii
INNER JOIN ecminvoiceoutolds as i
ON ii.ecminvoiceoutold_id = i.id
INNER JOIN ecminvoiceoutolditems AS o
ON o.id = ii.ecminvoiceoutolditem_id
WHERE
    ii.ecminvoiceoutold_id = '".$i['id']."'");
			
			$multi_correct = false;
			if ($multi->num_rows > 1) $multi_correct = true;
			
			if (!$multi_correct) {
				//normal correct
				$inv_id = $i['ecminvoiceoutold_id'];
				$inv_nr = $db->fetchByAssoc($db->query("SELECT id, document_no, register_date FROM ecminvoiceoutolds WHERE id='".$inv_id."'"));
				$this->p->startElement('dokumentKorygowany');
					$this->p->writeAttribute('rodzaj', 'FVS');
					$this->p->writeAttribute ( 'id', $inv_nr['id'] );
					$this->p->writeElement('numer', $inv_nr['document_no']);
					$this->p->writeElement('data', $inv_nr['register_date']);
				$this->p->endElement(); //</dokumentKorygowany>
			} else {
				//multi correct
				$this->p->startElement('dokumentyPowiazane');
				while ($inv_id = $db->fetchByAssoc($multi)) {
					$inv_nr = $db->fetchByAssoc($db->query("SELECT id, document_no, register_date FROM ecminvoiceoutolds WHERE id='".$inv_id['ecminvoiceoutold_id']."'"));
					$this->p->startElement('dokumentPowiazany');
					$this->p->writeAttribute('rodzaj', 'FVS');
					$this->p->writeAttribute ( 'id', $inv_nr['id'] );
					$this->p->writeElement('numer', $inv_nr['document_no']);
					$this->p->writeElement('data', $inv_nr['register_date']);
					$this->p->endElement(); // </dokumentPowiazany>
				}
				$this->p->endElement(); //<dokumentyPowiazane>
			}
			//pozycje dokumentu
			$this->p->startElement('pozycjeDokumentu');
			
			$pozycje = $db->query("
		select sum(i.subtotal_corrected) as subtotal, p.group_ks, sum(i.quantity_corrected) as qty from ecminvoiceoutolditems as i
inner join ecmproducts as p
on p.id=i.ecmproduct_id
where i.ecminvoiceoutold_id='".$i['id']."'
group by p.group_ks;
		");
			$pp = 0;
			global $app_list_strings;
			while ($poz = $db->fetchByAssoc($pozycje)) {
				$pp++;
				if ($poz['group_ks']=='1')
					$rodzaj = 'T';
				else $rodzaj = 'P';
			
				$this->p->startElement('pozycjaDokumentu');
				$this->p->writeElement('tresc', $app_list_strings['ecmproducts_group_ks_dom'][$poz['group_ks']]);
				$this->p->writeElement('wartoscNetto', round($poz['subtotal'],2));
				$this->p->writeElement('wartoscBrutto', '');
				$this->p->writeElement('wartoscVAT', '');
				$this->p->endElement(); // </pozycjaDokumentu>
			}
			
				$this->p->endElement(); // </pozycjeDokumentu>				
				//rozbicie
				$this->p->startElement('rejestr');
				
				$rozbicie = $db->query("
				select sum(i.subtotal_corrected) as subtotal, sum(i.total_corrected) as total, v.name, v.value from ecminvoiceoutolditems as i
inner join ecmvats as v
on v.id = i.ecmvat_id
where
i.ecminvoiceoutold_id='".$i['id']."'
group by v.id;
				");
					
				while ($roz = $db->fetchByAssoc($rozbicie)) {
					$this->p->startElement('rozbicie');
					$this->p->writeAttribute('stawka', $roz['name']);
					$this->p->writeElement('netto', round($roz['subtotal'],2));
					$this->p->writeElement('brutto', round($roz['total'],2));
					$this->p->writeElement('VAT', round ($roz['total'] - $roz['subtotal'], 2 ));
						
					$this->p->endElement(); // </rozbicie>
				}
				
				$this->p->endElement(); // </rejestr>
				if ($i['pdf_type']=='K') {
					$this->p->writeElement('sumaNetto', round($i['subtotal'],2));
					$this->p->writeElement('sumaBrutto', round($i['total'],2));
					$this->p->writeElement('sumaVat', round($i['total']-$i['subtotal'],2));
				} else {
					$this->p->writeElement('sumaNetto', round($i['subtotal']*$i['currency_value'],2));
					$this->p->writeElement('sumaBrutto', round($i['total']*$i['currency_value'],2));
					$this->p->writeElement('sumaVat', round(($i['total']-$i['subtotal'])*$i['currency_value'],2));
				
					$this->p->writeElement('sumaWalNetto', round($i['subtotal'],2));
					$this->p->writeElement('sumaWalBrutto', round($i['total'],2));
					$this->p->writeElement('sumaWalVat', round($i['total']-$i['subtotal'],2));
				
					//kursy
					$this->p->startElement('kursy');
					//data dostawy
					$cur = $db->fetchByAssoc($db->query("
						SELECT * FROM currency_nbp_archive
							WHERE currency_id = '".$i['currency_id']."'
							AND date < DATE_SUB((SELECT delivery_date FROM ecmsales WHERE id='".$wz['so_id']."'), INTERVAL 1 DAY)
							ORDER BY date DESC
							LIMIT 0,1;
					"));
					$this->p->startElement('kurs');
					$this->p->writeElement('waluta', $cur['currency_name']);
					$this->p->writeAttribute('zastosowanie', 'dataDostawy');
					$this->p->writeElement('rodzaj', 'NBP');
					$this->p->writeElement('naDzien', $cur['date']);
					$this->p->writeElement('wartosc', $cur['value']);
					$this->p->endElement(); // </kurs>
					//data transakcji
					$cur = $db->fetchByAssoc($db->query("
						SELECT * FROM currency_nbp_archive
							WHERE currency_id = '".$i['currency_id']."'
							AND date < DATE_SUB('".$i['register_date']."', INTERVAL 1 DAY)
							ORDER BY date DESC
							LIMIT 0,1;
					"));
					$this->p->startElement('kurs');
					$this->p->writeElement('waluta', $cur['currency_name']);
					$this->p->writeElement('zastosowanie', 'dataTransakcji');
					$this->p->writeElement('rodzaj', 'NBP');
					$this->p->writeElement('naDzien', $cur['date']);
					$this->p->writeElement('wartosc', $cur['value']);
					$this->p->endElement(); // </kurs>
					$this->p->endElement(); // </kursy>
				}				
				$this->p->endElement (); // </dokument>
		}
	}
	function getXML() {
		$this->p->endElement (); // </paczka>
		$this->p->flush ();
	}
}

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

$a=new maksymaXML('2014-05-01');
$a->getInvoices();
$a->getXML();
?>