Commit 83fc0975 authored by Karamel's avatar Karamel
Browse files

Export fiscal tickets in json in an archive.

parent 591beb22
......@@ -175,3 +175,40 @@ $app->any('/fiscal/', function ($request, $response, $args) {
return $response;
}
});
/** Fiscal export */
$app->GET('/fiscal/export', function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$response = fiscalLogin($this, $ptApp, $request, $response);
if ($ptApp->getCurrentUser() == null) {
fiscalTpl($response, 'login.php');
return $response;
} else {
// Get all fiscal tickets
$apiResult = APICaller::run($ptApp, 'fiscal', 'getAll', [['type', 'sequence', 'number']]);
if ($apiResult->getStatus() == APIResult::STATUS_CALL_OK) {
// Convert tickets to struct
$data = $apiResult->getContent();
$ftkts = [];
for ($i = 0; $i < count($data); $i++) {
$ftkts[] = $data[$i]->toStruct();
}
// Set export file name
$now = new DateTime();
$exportName = sprintf('fiscal_export-%s', $now->format('Ymd_Hi'));
$tmpName = tempnam(sys_get_temp_dir(), 'ptq');
$zip = new ZipArchive();
$zip->open($tmpName, ZipArchive::CREATE);
$zip->addFromString(sprintf('%s.txt', $exportName), json_encode($ftkts));
$zip->close();
$response = $response->withHeader('content-type', 'application/zip');
$response = $response->withHeader('content-disposition', sprintf('attachment; filename="%s.zip"', $exportName));
$response = $response->write(file_get_contents($tmpName));
unlink($tmpName);
return $response;
} else {
fiscalTpl($response, 'apierror.php', $apiResult);
}
return $response;
}
});
\ No newline at end of file
......@@ -8,5 +8,7 @@ function render($data) {
$ret .= '<li>' . htmlspecialchars($sequence) . ' : <a href="./sequence/' . htmlspecialchars($sequence) . '/z/" target="_blank">Tickets Z</a> <a href="./sequence/' . htmlspecialchars($sequence) . '/tickets/" target="_blank">Tickets</a></li>';
}
$ret .= '</ul>';
$ret .= '<h2>Export des tickets</h2>';
$ret .= '<p><a href="./export">Exporter</a></p>';
return $ret;
}
......@@ -37,6 +37,13 @@ class FiscalAPI implements API
return new static($app->getDao());
}
/** Get all fiscal tickets, including EOS'.
* @param $order The order fields (see DAO->search) */
public function getAll($order = null) {
return $this->dao->search(FiscalTicket::class,
null, null, null, $order);
}
public function getLastFiscalTicket($type, $sequence) {
// Look for an existing fiscal ticket
$lastFTicket = null;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment