first commit

This commit is contained in:
2025-08-02 16:30:27 +02:00
commit 23646bfcee
14851 changed files with 1750626 additions and 0 deletions

View File

@@ -0,0 +1,135 @@
<?php
namespace Export;
/**
* ExportData is the base class for exporters to specific file formats. See other
* classes below.
*
* php-export-data by Eli Dickinson, http://github.com/elidickinson/php-export-data
*/
abstract class ExportData
{
protected $exportTo; // Set in constructor to one of 'browser', 'file', 'string'
protected $stringData; // stringData so far, used if export string mode
protected $tempFile; // handle to temp file (for export file mode)
protected $tempFilename; // temp file name and path (for export file mode)
public $filename; // file mode: the output file name; browser mode: file name for download; string mode: not used
protected $encoding;
public function __construct($exportTo = 'browser', $filename = 'exportdata')
{
if (!in_array($exportTo, ['browser', 'file', 'string'])) {
throw new Exception("{$exportTo} is not a valid ExportData export type");
}
$this->exportTo = $exportTo;
$this->filename = $filename;
}
public function initialize()
{
global $cfg;
switch ($this->exportTo) {
case 'browser':
$this->sendHttpHeaders();
break;
case 'string':
$this->stringData = '';
break;
case 'file':
$this->tempFilename = tempnam($cfg['Path']['data'], 'exportdata');
$this->tempFile = fopen($this->tempFilename, 'w');
break;
}
$this->write($this->generateHeader());
}
public function addRow($row)
{
$this->write($this->generateRow($row));
}
public function loadOrdersData($order_ids)
{
foreach ($order_ids as $id) {
$this->addRow($this->getOrder($id));
}
}
public function finalize()
{
$this->write($this->generateFooter());
switch ($this->exportTo) {
case 'browser':
flush();
break;
case 'string':
// do nothing
break;
case 'file':
// close temp file and move it to correct location
fclose($this->tempFile);
rename($this->tempFilename, $this->filename);
break;
}
}
public function getString()
{
return $this->stringData;
}
abstract public function sendHttpHeaders();
protected function write($data)
{
if (!empty($data)) {
switch ($this->exportTo) {
case 'browser':
echo $data;
break;
case 'string':
$this->stringData .= $data;
break;
case 'file':
fwrite($this->tempFile, $data);
break;
}
}
}
protected function generateHeader()
{
// can be overridden by subclass to return any data that goes at the top of the exported file
}
protected function generateFooter()
{
// can be overridden by subclass to return any data that goes at the bottom of the exported file
}
// In subclasses generateRow will take $row array and return string of it formatted for export type
abstract protected function generateRow($row);
protected function getOrder($id)
{
$order = new \Order();
$order->createFromDB($id);
$order->fetchItems();
return $order;
}
public function setCoding($encoding)
{
$this->encoding = $encoding;
}
public function encodeData($string)
{
return iconv('utf-8', $this->encoding.'//TRANSLIT', $string);
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Export;
/**
* ExportDataCSV - Exports to CSV (comma separated value) format.
*/
class ExportDataCSV extends ExportData
{
public function generateRow($row)
{
foreach ($row as $key => $value) {
// Escape inner quotes and wrap all contents in new quotes.
// Note that we are using \" to escape double quote not ""
$row[$key] = '"'.str_replace('"', '\"', $value).'"';
}
return implode(',', $row)."\n";
}
public function sendHttpHeaders()
{
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename='.basename($this->filename));
}
}

View File

@@ -0,0 +1,156 @@
<?php
namespace Export;
use XBase\Record;
use XBase\WritableTable;
/**
* Own class for DBF export.
*/
class ExportDataDBF extends ExportData
{
/**
* @var WritableTable
*/
private $table;
public function initialize()
{
global $cfg;
$this->tempFilename = $cfg['Path']['data'].'export_data.dbf';
$this->table = WritableTable::create($this->tempFilename, $this->columnDefs(), $this->tempFilename);
}
public function finalize()
{
$this->sendHttpHeaders();
$this->table->close();
readfile($this->tempFilename);
exit;
}
/**
* @param $order \Order
*/
public function generateRow($order)
{
$row_pointer = $this->table->appendRecord();
$this->appendToRow($row_pointer, $order);
$this->table->writeRecord();
}
public function sendHttpHeaders()
{
header('Content-Type: application/x-dbase; charset='.$this->encoding);
header('Content-Disposition: attachment; filename="'.$this->filename.'"');
header('Expires: 0');
header('Pragma: no-cache');
}
protected function columnDefs()
{
/* FIXME: dodělat zbytek a přesměrovat DBF z export_sracka sem */
return [
['DATUM_VYST', Record::DBFFIELD_TYPE_DATE], // datum vystavení
// ['DATUM_SPL', DBFFIELD_TYPE_DATE], //prázdné
// ['DOKLAD_1', DBFFIELD_TYPE_CHAR, 12], //aby tam bylo fčíslo objednávky např. f11521
// ['TYP_ZAP', DBFFIELD_TYPE_CHAR, 1], //hodnota P
// ['ZNAK_UCT', DBFFIELD_TYPE_CHAR, 5], //prázdné
// ['SYMBOLY', DBFFIELD_TYPE_CHAR, 20], //včísloobjednávky např. v11521
// ['PAR_ZNAK', DBFFIELD_TYPE_CHAR, 12], //to samé jako DOKLAD_1 např. f11521
// ['BANK_UCET', DBFFIELD_TYPE_CHAR, 44], //prázdné
// ['SPEC_SYMB', DBFFIELD_TYPE_CHAR, 10], //prázdné
// ['POPIS_TEXT', DBFFIELD_TYPE_CHAR, 30], //libovolný text - např. prodej eshop-objednávka 11521
// ['DRUH_UCT', DBFFIELD_TYPE_CHAR, 5], //hodnota PZ
// ['MENA', DBFFIELD_TYPE_CHAR, 3], //buď Kč nebo EUR
// ['CELKEM_KC', DBFFIELD_TYPE_NUMERIC, 8, 2], //celková částka dokladu včetně DPH v Kč
// ['CELKEMCIZI', DBFFIELD_TYPE_NUMERIC, 8, 2], //celková částka dokladu v EUR - prodej na Slovensko?
// ['KURZ', DBFFIELD_TYPE_NUMERIC, 5, 3], //kurz pro přepočet
// ['DATUM_DPH', DBFFIELD_TYPE_DATE], //datum DPH = datum vystavení
// ['TYP_DPH', DBFFIELD_TYPE_CHAR, 5], //hodnota U
// ['ZAKL_DPH_Z', DBFFIELD_TYPE_NUMERIC, 8, 2], //Částka bez daně v základní sazbě
// ['DPH_Z', DBFFIELD_TYPE_NUMERIC, 8, 2], //Daň s azba 21 %
// ['ZAKL_DPH_S', DBFFIELD_TYPE_NUMERIC, 8, 2], //Částka bez daně ve snížené sazbě
// ['DPH_S', DBFFIELD_TYPE_NUMERIC, 8, 2], //Daň s azba 15 %
// ['TYPMIMODPH', DBFFIELD_TYPE_CHAR, 1], //prázdné
// ['CASTKAMIMO', DBFFIELD_TYPE_NUMERIC, 8, 2], //prázdné
// ['STREDISKO', DBFFIELD_TYPE_CHAR, 5], //PRÁZDNÉ
// ['vykon', DBFFIELD_TYPE_CHAR, 5], //prázdné
// ['zakazka', DBFFIELD_TYPE_CHAR, 5], //prázdné
// ['POZNAMKA', DBFFIELD_TYPE_CHAR, 150], //sem naimportovat jméno a příjmění a město kupujícího JAN NOVAK TURNOV
// ['FIRMA', DBFFIELD_TYPE_CHAR, 30], // Firma
// ['JMENO', DBFFIELD_TYPE_CHAR, 30], //Jmeno
// ['PRIJMENI', DBFFIELD_TYPE_CHAR, 30], //Prijmeni
// ['ICO', DBFFIELD_TYPE_CHAR, 15], //ICO
// ['DIC', DBFFIELD_TYPE_CHAR, 20], //DIC
//
// ['ZAKAZNIK', DBFFIELD_TYPE_CHAR, 80], //Prijmeni nebo firma
// ['ULICE', DBFFIELD_TYPE_CHAR, 50], //Adresa
// ['MESTO', DBFFIELD_TYPE_CHAR, 30], //Adresa
// ['PSC', DBFFIELD_TYPE_CHAR, 10], //Adresa
// ['ZEME', DBFFIELD_TYPE_CHAR, 30], //Adresa
//
// ['STAV', DBFFIELD_TYPE_CHAR, 10], // STORNO/PRODEJ
// ['DATUM_SPLAT', DBFFIELD_TYPE_DATE], //datum splatnosti
];
}
/**
* @param $row_pointer Record
* @param $order \Order
*/
protected function appendToRow($row_pointer, $order)
{
/* FIXME: dodělat zbytek a přesměrovat DBF z export_sracka sem */
if ($order->date_handle) {
$row_pointer->setObjectByName('DATUM_VYST', $order->date_handle->format('Ymd')); // datum vystavení
}
// $r->setObjectByName("DATUM_SPL", $row['DBFFIELD_TYPE_DATE']); //prázdné
// $row_pointer->setObjectByName('DOKLAD_1', $order['order_no']); //aby tam bylo fčíslo objednávky např. f11521
// $row_pointer->setObjectByName('TYP_ZAP', 'P'); //hodnota P
// $row_pointer->setObjectByName('ZNAK_UCT', ''); //prázdné
// $row_pointer->setObjectByName('SYMBOLY', $order['id']); //včísloobjednávky např. v11521
// $row_pointer->setObjectByName('PAR_ZNAK', $order['order_no']); //to samé jako DOKLAD_1 např. f11521
// $row_pointer->setObjectByName('BANK_UCET', ''); //prázdné
// $row_pointer->setObjectByName('SPEC_SYMB', ''); //prázdné
// $row_pointer->setObjectByName('POPIS_TEXT', $encode('Objednávka: ').$order['id']); //libovolný text - např. prodej eshop-objednávka 11521
// $row_pointer->setObjectByName('DRUH_UCT', 'PZ'); //hodnota PZ
// $row_pointer->setObjectByName('MENA', $encode('Kč')); //buď Kč nebo EUR
// $row_pointer->setObjectByName('CELKEM_KC', $total_price); //celková částka dokladu včetně DPH v Kč
// $row_pointer->setObjectByName('CELKEMCIZI', 0); //celková částka dokladu v EUR - prodej na Slovensko?
// $row_pointer->setObjectByName('KURZ', 0); //kurz pro přepočet
// $row_pointer->setObjectByName('DATUM_DPH', strtotime($order['date_handle'])); //datum DPH = datum vystavení
// $row_pointer->setObjectByName('TYP_DPH', 'U'); //hodnota U
// $row_pointer->setObjectByName('ZAKL_DPH_Z', $taxes['21']); //Částka bez daně v základní sazbě
// $row_pointer->setObjectByName('DPH_Z', $taxes['21']->mul(toDecimal(0.21))); //Daň sazba 21 %
// $row_pointer->setObjectByName('ZAKL_DPH_S', $taxes['15']); //Částka bez daně ve snížené sazbě
// $row_pointer->setObjectByName('DPH_S', $taxes['15']->mul(toDecimal(0.15))); //Částka bez daně ve snížené sazbě
// $row_pointer->setObjectByName('TYPMIMODPH', ''); //prázdné
// $row_pointer->setObjectByName('CASTKAMIMO', $taxes['0']); //prázdné
// $row_pointer->setObjectByName('STREDISKO', ''); //PRÁZDNÉ
// $row_pointer->setObjectByName('vykon', ''); //prázdné
// $row_pointer->setObjectByName('zakazka', ''); //prázdné
// $row_pointer->setObjectByName('POZNAMKA', "{$order['invoice_name']} {$order['invoice_surname']}, {$order['invoice_city']}"); //sem naimportovat jméno a příjmění a město kupujícího JAN NOVAK TURNOV
// $row_pointer->setObjectByName('FIRMA', $order['invoice_firm']); // Firma
// $row_pointer->setObjectByName('JMENO', $order['invoice_name']); //Jmeno
// $row_pointer->setObjectByName('PRIJMENI', $order['invoice_surname']); //Prijmeni
//
// $row_pointer->setObjectByName('ICO', $order['invoice_ico']); //ICO
// $row_pointer->setObjectByName('DIC', $order['invoice_dic']); //DIC
//
// $row_pointer->setObjectByName('ZAKAZNIK', $order['invoice_firm'] ? $order['invoice_firm'] : "{$order['invoice_name']} {$order['invoice_surname']}"); //Prijmeni nebo firma
// $row_pointer->setObjectByName('ULICE', $order['invoice_street']);
// $row_pointer->setObjectByName('MESTO', $order['invoice_city']);
// $row_pointer->setObjectByName('PSC', $order['invoice_zip']);
// $row_pointer->setObjectByName('ZEME', $order['invoice_country']);
//
// $row_pointer->setObjectByName('STAV', $order['status_storno'] ? 'STORNO' : 'PRODEJ'); // STORNO/PRODEJ
// $row_pointer->setObjectByName('DATUM_SPLAT', strtotime($order['date_due']));
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace Export;
use XBase\Record;
/**
* Own class for DBF export.
*/
class ExportDataDBFUcto extends ExportDataDBF
{
protected function columnDefs()
{
return [
['Veta', Record::DBFFIELD_TYPE_NUMERIC, 6, 0], // datum vystavení
['DatumVyst', Record::DBFFIELD_TYPE_DATE], // datum vystavení
['DatumSpl', Record::DBFFIELD_TYPE_DATE], // datum splatnosti
['Datum', Record::DBFFIELD_TYPE_DATE], // datum zaplacení - většinou prázdné
['Doklad', Record::DBFFIELD_TYPE_CHAR, 30], // Doklad
['DokladKH', Record::DBFFIELD_TYPE_CHAR, 20],
['TypD', Record::DBFFIELD_TYPE_CHAR, 3],
['Text', Record::DBFFIELD_TYPE_CHAR, 30], // text
['Druh', Record::DBFFIELD_TYPE_CHAR, 3], // klic do ciselniku druhu
['Firma', Record::DBFFIELD_TYPE_CHAR, 5], // cislo firmy v adresari ucta
['Ico', Record::DBFFIELD_TYPE_CHAR, 10], // ico firmy
['Vykon', Record::DBFFIELD_TYPE_CHAR, 5], // klic do ciselniku vykonu
['Plat', Record::DBFFIELD_TYPE_CHAR, 1], // platba: B-banka, H-hotovos
['Celkem', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // celkova castka
['DatumDPH', Record::DBFFIELD_TYPE_DATE], // datum zd. pl
['DatumKH', Record::DBFFIELD_TYPE_DATE],
['BezDaneZ', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
['DphZ', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
['BezDaneS', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
['DphS', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
['BezDaneS2', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
['DphS2', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
['BezDaneO', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
['Zaloha', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
['Prikaz', Record::DBFFIELD_TYPE_LOGICAL], // nevyplnovat, vyber pro prikaz k uhrade
['DatumPrik', Record::DBFFIELD_TYPE_DATE], // nevyplnovat, datum pro prikaz k uhrade
['Pozn', Record::DBFFIELD_TYPE_MEMO], // poznámka (memo)
['Ozn', Record::DBFFIELD_TYPE_LOGICAL], // označené věty
['Měna2', Record::DBFFIELD_TYPE_CHAR, 3], // Měna2
['Kč2', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // Kč2
['Naz0', Record::DBFFIELD_TYPE_CHAR, 30], // označené věty
['Dic0', Record::DBFFIELD_TYPE_CHAR, 14], // označené věty
];
}
/**
* @param $row_pointer Record
* @param $order \Order
*/
protected function appendToRow($row_pointer, $order)
{
// $row_pointer->setObjectByName('Veta', '');
$row_pointer->setObjectByName('DatumVyst', $order->date_handle ? strtotime($order->date_handle->format('Ymd')) : '');
// $row_pointer->setObjectByName('DatumPrik', '');
/* TODO: dořešit due_days, nejlíp na order */
$row_pointer->setObjectByName('DatumSpl', ($order->date_handle) ? strtotime($order->date_handle->modify('+14 days')->format('Ymd')) : '');
// $row_pointer->setObjectByName('Datum', '');
$row_pointer->setObjectByName('Doklad', 'f/'.$order->order_no);
$row_pointer->setObjectByName('DokladKH', $order->order_no);
$row_pointer->setObjectByName('TypD', ' ');
$row_pointer->setObjectByName('Text', $this->encodeData($order->invoice_firm ? $order->invoice_firm : $order->invoice_name.' '.$order->invoice_surname));
$row_pointer->setObjectByName('Druh', 'PZ ');
$row_pointer->setObjectByName('Firma', ' ');
$row_pointer->setObjectByName('Ico', strtoupper($order->invoice_ico));
$row_pointer->setObjectByName('Vykon', ' ');
$pay_type = $order->getDeliveryType()->getPayment();
if ($pay_type && $pay_type->getPayMethod() == \Payment::METHOD_CASH) {
$pay_type = 'H';
} else {
$pay_type = 'B';
}
$row_pointer->setObjectByName('Plat', $this->encodeData($pay_type));
$row_pointer->setObjectByName('Celkem', !$order->status_storno ? $order->total_price->printFloatValue(2) : 0);
/* TODO: datum zd. plneni */
$row_pointer->setObjectByName('DatumDPH', $order->date_handle ? strtotime($order->date_handle->format('Ymd')) : '');
// $row_pointer->setObjectByName('DatumKH', '');
if (!empty($order->vats[21]) && !$order->status_storno) {
$row_pointer->setObjectByName('BezDaneZ', $order->vats[21]['total_price_without_vat']->printFloatValue(2));
$row_pointer->setObjectByName('DphZ', $order->vats[21]['tax']['value_vat']->printFloatValue(2));
} else {
$row_pointer->setObjectByName('BezDaneZ', 0);
$row_pointer->setObjectByName('DphZ', 0);
}
// if (!empty($order->vats[15])) {
// $row_pointer->setObjectByName('BezDaneS', $order->vats[15]['total_price_without_vat']->printFloatValue(2));
// $row_pointer->setObjectByName('DphS', $order->vats[15]['tax']['value_vat']->printFloatValue(2));
// } else {
// $row_pointer->setObjectByName('BezDaneS', 0);
// $row_pointer->setObjectByName('DphS', 0);
// }
// $row_pointer->setObjectByName('BezDaneS2', 0);
// $row_pointer->setObjectByName('DphS2', 0);
// $row_pointer->setObjectByName('BezDaneO', 0);
// $row_pointer->setObjectByName('Zaloha', 0);
$row_pointer->setObjectByName('Prikaz', false);
// $row_pointer->setObjectByName('DatumPrik', 0);
// $row_pointer->setObjectByName('Pozn', '');
$row_pointer->setObjectByName('Ozn', false);
$row_pointer->setObjectByName('Měna2', ' ');
// $row_pointer->setObjectByName('Kč2', $order->currency_rate);
$row_pointer->setObjectByName('Naz0', ' ');
$row_pointer->setObjectByName('Dic0', strtoupper($order->invoice_dic));
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Export;
/**
* ExportDataExcel exports data into an XML format (spreadsheetML) that can be
* read by MS Excel 2003 and newer as well as OpenOffice.
*
* Creates a workbook with a single worksheet (title specified by
* $title).
*
* Note that using .XML is the "correct" file extension for these files, but it
* generally isn't associated with Excel. Using .XLS is tempting, but Excel 2007 will
* throw a scary warning that the extension doesn't match the file type.
*
* Based on Excel XML code from Excel_XML (http://github.com/oliverschwarz/php-excel)
* by Oliver Schwarz
*/
class ExportDataExcel extends ExportData
{
public const XmlHeader = "<?xml version=\"1.0\" encoding=\"%s\"?\>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
public const XmlFooter = '</Workbook>';
public $encoding = 'UTF-8'; // encoding type to specify in file.
// Note that you're on your own for making sure your data is actually encoded to this encoding
public $title = 'Sheet1'; // title for Worksheet
public function generateHeader()
{
// workbook header
$output = stripslashes(sprintf(self::XmlHeader, $this->encoding))."\n";
// Set up styles
$output .= "<Styles>\n";
$output .= "<Style ss:ID=\"sDT\"><NumberFormat ss:Format=\"Short Date\"/></Style>\n";
$output .= "</Styles>\n";
// worksheet header
$output .= sprintf("<Worksheet ss:Name=\"%s\">\n <Table>\n", htmlentities($this->title));
return $output;
}
public function generateFooter()
{
$output = '';
// worksheet footer
$output .= " </Table>\n</Worksheet>\n";
// workbook footer
$output .= self::XmlFooter;
return $output;
}
public function generateRow($row)
{
$output = '';
$output .= " <Row>\n";
foreach ($row as $k => $v) {
$output .= $this->generateCell($v);
}
$output .= " </Row>\n";
return $output;
}
private function generateCell($item)
{
$output = '';
$style = '';
// Tell Excel to treat as a number. Note that Excel only stores roughly 15 digits, so keep
// as text if number is longer than that.
if (preg_match("/^-?\d+(?:[.,]\d+)?$/", $item) && (strlen($item) < 15)) {
$type = 'Number';
}
// Sniff for valid dates; should look something like 2010-07-14 or 7/14/2010 etc. Can
// also have an optional time after the date.
//
// Note we want to be very strict in what we consider a date. There is the possibility
// of really screwing up the data if we try to reformat a string that was not actually
// intended to represent a date.
elseif (preg_match("/^(\d{1,2}|\d{4})[\/\-]\d{1,2}[\/\-](\d{1,2}|\d{4})([^\d].+)?$/", $item)
&& ($timestamp = strtotime($item))
&& ($timestamp > 0)
&& ($timestamp < strtotime('+500 years'))) {
$type = 'DateTime';
$item = strftime('%Y-%m-%dT%H:%M:%S', $timestamp);
$style = 'sDT'; // defined in header; tells excel to format date for display
} else {
$type = 'String';
}
$item = str_replace('&#039;', '&apos;', htmlspecialchars($item, ENT_QUOTES));
$output .= ' ';
$output .= $style ? "<Cell ss:StyleID=\"{$style}\">" : '<Cell>';
$output .= sprintf('<Data ss:Type="%s">%s</Data>', $type, $item);
$output .= "</Cell>\n";
return $output;
}
public function sendHttpHeaders()
{
header('Content-Type: application/vnd.ms-excel; charset='.$this->encoding);
header('Content-Disposition: inline; filename="'.basename($this->filename).'"');
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Export;
/**
* ExportDataTSV - Exports to TSV (tab separated value) format.
*/
class ExportDataTSV extends ExportData
{
public function generateRow($row)
{
foreach ($row as $key => $value) {
// Escape inner quotes and wrap all contents in new quotes.
// Note that we are using \" to escape double quote not ""
$row[$key] = '"'.str_replace('"', '\"', $value).'"';
}
return implode("\t", $row)."\n";
}
public function sendHttpHeaders()
{
header('Content-type: text/tab-separated-values');
header('Content-Disposition: attachment; filename='.basename($this->filename));
}
}