Files
crm.e5.pl/modules/EcmInvoiceOuts/maksymaXML.php
2024-04-27 09:23:34 +02:00

361 lines
14 KiB
PHP

<?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 ecminvoiceouts 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 ( 'dataWplywu', $i ['register_date'] );
$this->p->writeElement ( 'numer', $i ['document_no'] );
//platnosc
$this->p->startElement('platnosc');
$this->p->writeElement('kwota', $i['total_brutto']);
$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 ['parent_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, total, register_date, ecmsale_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['total']);
$this->p->endElement(); // </dokumentPowiazany>
$this->p->endElement(); //</dokumentyPowiazane>
//pozycje dokumentu
$pozycje = $db->query("
select sum(i.total_netto) as total_netto, p.group_ks, sum(i.quantity) as qty from ecminvoiceoutitems as i
inner join ecmproducts as p
on p.id=i.ecmproduct_id
where i.ecminvoiceout_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';
$tresc = 'TH';
} elseif ($poz['group_ks']=='2') {
$rodzaj = 'P';
$tresc = 'WG';
}
elseif (($poz['group_ks']=='3')) {
$rodzaj = 'S';
$tresc = 'SU';
}
$this->p->startElement('pozycjaDokumentu');
$this->p->writeElement('tresc', $tresc);
$this->p->writeElement('wartoscNetto', round($poz['total_netto'],2));
$this->p->endElement(); // </pozycjaDokumentu>
}
$this->p->endElement(); // </pozycjeDokumentu>
//rozbicie
$this->p->startElement('rejestr');
$rozbicie = $db->query("
select sum(i.total_netto) as total_netto, sum(i.total_brutto) as total_brutto, sum(i.total_vat) as total_vat, v.name, v.value from ecminvoiceoutitems as i
inner join ecmvats as v
on v.id = i.ecmvat_id
where
i.ecminvoiceout_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['total_netto'],2));
$this->p->writeElement('brutto', round($roz['total_brutto'],2));
$this->p->writeElement('VAT', round ($roz['total_vat'], 2 ));
$this->p->endElement(); // </rozbicie>
}
$this->p->endElement(); // </rejestr>
if ($i['pdf_type']=='K') {
$this->p->writeElement('sumaNetto', round($i['total_netto'],2));
$this->p->writeElement('sumaBrutto', round($i['total_brutto'],2));
$this->p->writeElement('sumaVat', round($i['total_vat'],2));
} else {
$this->p->writeElement('sumaNetto', round($i['total_netto']*$i['currency_value'],2));
$this->p->writeElement('sumaBrutto', round($i['total_brutto']*$i['currency_value'],2));
$this->p->writeElement('sumaVat', round(($i['total_vat'])*$i['currency_value'],2));
$this->p->writeElement('sumaWalNetto', round($i['total_netto'],2));
$this->p->writeElement('sumaWalBrutto', round($i['total_brutto'],2));
$this->p->writeElement('sumaWalVat', round($i['total_vat'],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;
"));
if ($cur['value'] && $cur['value']!= "") {
$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', e($cur['currency_name']));
$this->p->writeAttribute('zastosowanie', 'dataTransakcji');
$this->p->writeElement('rodzaj', 'NBP');
$this->p->writeElement('naDzien', e($cur['date']));
$this->p->writeElement('wartosc', e($cur['value']));
$this->p->endElement(); // </kurs>
$this->p->endElement(); // </kursy>
}
$this->p->endElement (); // </dokument>
}
}
function getCorrectInfoices() {
$db = $GLOBALS ['db'];
$invoices = $db->query ( "SELECT * FROM ecminvoiceouts 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 ( 'dataWplywu', $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 ['parent_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_brutto']);
$this->p->writeElement('data', $i['payment_date']);
$this->p->endElement(); //</platnosc>
//multi correct?
$multi = $db->query ( "
SELECT distinct o.ecminvoiceout_id
FROM
ecminvoiceoutitems as ii
INNER JOIN ecminvoiceouts as i
ON ii.ecminvoiceout_id = i.id
INNER JOIN ecminvoiceoutitems AS o
ON o.id = ii.ecminvoiceoutitem_id
WHERE
ii.ecminvoiceout_id = '".$i['id']."'");
$multi_correct = false;
if ($multi->num_rows > 1) $multi_correct = true;
if (!$multi_correct) {
//normal correct
$inv_id = $i['ecminvoiceout_id'];
$inv_nr = $db->fetchByAssoc($db->query("SELECT id, document_no, register_date FROM ecminvoiceouts 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 ecminvoiceouts WHERE id='".$inv_id['ecminvoiceout_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.total_netto_corrected) as total_netto, p.group_ks, sum(i.quantity_corrected) as qty from ecminvoiceoutitems as i
inner join ecmproducts as p
on p.id=i.ecmproduct_id
where i.ecminvoiceout_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';
$tresc = 'TH';
} elseif ($poz['group_ks']=='2') {
$rodzaj = 'P';
$tresc = 'WG';
}
elseif (($poz['group_ks']=='3')) {
$rodzaj = 'S';
$tresc = 'SU';
}
$this->p->startElement('pozycjaDokumentu');
$this->p->writeElement('tresc', $tresc);
$this->p->writeElement('wartoscNetto', round($poz['total_netto'],2));
$this->p->endElement(); // </pozycjaDokumentu>
}
$this->p->endElement(); // </pozycjeDokumentu>
//rozbicie
$this->p->startElement('rejestr');
$rozbicie = $db->query("
select sum(i.total_netto_corrected) as total_netto, sum(i.total_brutto_corrected) as total_brutto, v.name, v.value from ecminvoiceoutitems as i
inner join ecmvats as v
on v.id = i.ecmvat_id
where
i.ecminvoiceout_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['total_netto'],2));
$this->p->writeElement('brutto', round($roz['total_brutto'],2));
$this->p->writeElement('VAT', round ($roz['total_brutto'] - $roz['total_netto'], 2 ));
$this->p->endElement(); // </rozbicie>
}
$this->p->endElement(); // </rejestr>
if ($i['pdf_type']=='K') {
$this->p->writeElement('sumaNetto', round($i['total_netto'],2));
$this->p->writeElement('sumaBrutto', round($i['total_brutto'],2));
$this->p->writeElement('sumaVat', round($i['total_vat'],2));
} else {
$this->p->writeElement('sumaNetto', round($i['total_netto']*$i['currency_value'],2));
$this->p->writeElement('sumaBrutto', round($i['total_brutto']*$i['currency_value'],2));
$this->p->writeElement('sumaVat', round(($i['total_vat'])*$i['currency_value'],2));
$this->p->writeElement('sumaWalNetto', round($i['total_netto'],2));
$this->p->writeElement('sumaWalBrutto', round($i['total_brutto'],2));
$this->p->writeElement('sumaWalVat', round($i['total_vat'],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;
"));
if ($cur['value'] && $cur['value']!= "") {
$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 ' ';
}
?>