Commit abeadaf0 authored by Karamel's avatar Karamel
Browse files

Add periods to export fiscal tickets from the web interface.

Fix empty file and add \n at the end.
parent 807ce819
......@@ -7,7 +7,7 @@ use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\Login;
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
use \Pasteque\Server\System\DAO\DAOCondition;
function fiscalLogin($app, $ptApp, $request, $response) {
$data = $request->getParsedBody();
......@@ -186,12 +186,40 @@ $app->GET('/fiscal/export', function ($request, $response, $args) {
fiscalTpl($response, 'login.php');
return $response;
} else {
$params = $request->getQueryParams();
$interval = null;
$from = null;
$to = new DateTime();
if (!empty($params['period'])) {
try {
$interval = new DateInterval($params['period']);
$from = clone $to;
$from->sub($interval);
} catch (Exception $e) {
return $response->withStatus(400, 'Bad Request');
}
} elseif (!empty($params['from'])) {
$from = DateUtils::readDate($params['from']);
if ($from === false) {
return $response->withStatus(400, 'Bad Request');
}
if (!empty($params['to'])) {
$to = DateUtils::readDate($params['to']);
if ($to === false) {
return $response->withStatus(400, 'Bad Request');
}
}
}
$jsonFileName = tempnam(sys_get_temp_dir(), 'pasteque_fiscal_export_');
$zipFileName = tempnam(sys_get_temp_dir(), 'pasteque_fiscal_export_');
try {
// Create tmp file output
$now = new DateTime();
$exportName = sprintf('fiscal_export-%s', $now->format('Ymd_Hi'));
$exportName = '';
if ($from !== null) {
$exportName = sprintf('fiscal_export-%s-%s', $from->format('Ymd_Hi'), $to->format('Ymd_Hi'));
} else {
$exportName = sprintf('fiscal_export-%s', $to->format('Ymd_Hi'));
}
$file = fopen($jsonFileName, 'w');
fwrite($file, '['); // Init json string
// Get all fiscal tickets
......@@ -199,8 +227,14 @@ $app->GET('/fiscal/export', function ($request, $response, $args) {
$batchSize = 100;
$page = 0;
$done = false;
$found = false;
$searchConds = null;
if ($from !== null) {
$searchConds = [new DAOCondition('date', '>=', $from),
new DAOCondition('date', '<=', $to)];
}
while (!$done) {
$apiResult = APICaller::run($ptApp, 'fiscal', 'search', [null, $batchSize, $batchSize * $page, ['type', 'sequence', 'number']]);
$apiResult = APICaller::run($ptApp, 'fiscal', 'search', [$searchConds, $batchSize, $batchSize * $page, ['type', 'sequence', 'number']]);
$page++;
if ($apiResult->getStatus() == APIResult::STATUS_CALL_OK) {
// Convert tickets to struct
......@@ -208,6 +242,7 @@ $app->GET('/fiscal/export', function ($request, $response, $args) {
if (count($data) == 0) {
$done = true;
} else {
$found = true;
$ftkts = [];
for ($i = 0; $i < count($data); $i++) {
$ftkts[] = $data[$i]->toStruct();
......@@ -226,8 +261,10 @@ $app->GET('/fiscal/export', function ($request, $response, $args) {
}
}
// End json string and close file
fseek($file, -1, SEEK_END);
fwrite($file, ']');
if ($found) {
fseek($file, -1, SEEK_END);
}
fwrite($file, "]\n");
fclose($file);
// Compress the file
$zip = new ZipArchive();
......
......@@ -9,6 +9,16 @@ function render($data) {
}
$ret .= '</ul>';
$ret .= '<h2>Export des tickets</h2>';
$ret .= '<p><a href="./export">Exporter</a></p>';
$ret .= '<ul>';
$ret .= '<li><a href="./export?period=P7D">Exporter une semaine</a></li>';
$ret .= '<li><a href="./export?period=P14D">Exporter deux semaines</a></li>';
$ret .= '<li><a href="./export?period=P1M">Exporter un mois</a></li>';
$ret .= '<li><form action="export" method="get">';
$ret .= '<label for="period-from">Exporter depuis le </label><input type="date" name="from" id="period-from" placeholder="aaaa/mm/jj" required="true" /> <input type="submit" value="Exporter" />';
$ret .= '</form></li>';
$ret .= '<li><form action="export" method="get">';
$ret .= '<label for="interval-from">Exporter depuis le </label><input type="date" name="from" id="interval-from" placeholder="aaaa-mm-jj" required="true" /> <label for="interval-to">au </label><input type="date" name="to" id="interval-to" placeholder="aaaa-mm-jj" required="true" /> <input type="submit" value="Exporter" />';
$ret .= '</form></li>';
$ret .= '</ul>';
return $ret;
}
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