Commit 68aeca24 authored by Karamel's avatar Karamel
Browse files

Add a web form for importing fiscal tickets on fiscal mirrors.

parent abeadaf0
......@@ -32,11 +32,11 @@ function fiscalLogin($app, $ptApp, $request, $response) {
}
return $response;
}
function fiscalTpl($response, $template, $data = []) {
function fiscalTpl($ptApp, $response, $template, $data = []) {
$body = $response->getBody();
$body->write(file_get_contents(__DIR__ . '/../templates/header.html'));
require_once(__DIR__ . '/../templates/' . $template);
$content = render($data);
$content = render($ptApp, $data);
$body->write($content);
$body->write(file_get_contents(__DIR__ . '/../templates/footer.html'));
}
......@@ -45,10 +45,10 @@ $app->any('/fiscal/z/{sequence}', function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$response = fiscalLogin($this, $ptApp, $request, $response);
if ($ptApp->getCurrentUser() == null) {
fiscalTpl($response, 'login.php');
fiscalTpl($ptApp, $response, 'login.php');
return $response;
} else {
fiscalTpl($response, 'z.html');
fiscalTpl($ptApp, $response, 'z.html');
return $response;
}
});
......@@ -58,7 +58,7 @@ $app->any('/fiscal/sequence/{sequence}/z/', function ($request, $response, $args
$ptApp = $this->get('settings')['ptApp'];
$response = fiscalLogin($this, $ptApp, $request, $response);
if ($ptApp->getCurrentUser() == null) {
fiscalTpl($response, 'login.php');
fiscalTpl($ptApp, $response, 'login.php');
return $response;
} else {
// Get pagination input
......@@ -71,7 +71,7 @@ $app->any('/fiscal/sequence/{sequence}/z/', function ($request, $response, $args
['sequence' => $sequence]);
$pageCount = 0;
if ($apiCount->getStatus() != APIResult::STATUS_CALL_OK) {
fiscalTpl($response, 'apierror.php', $apiCount);
fiscalTpl($ptApp, $response, 'apierror.php', $apiCount);
return $response;
} else {
$zCount = $apiCount->getContent();
......@@ -96,11 +96,11 @@ $app->any('/fiscal/sequence/{sequence}/z/', function ($request, $response, $args
$structs[] = $struct;
}
// Render
fiscalTpl($response, 'listz.php', ['sequence' => $sequence,
fiscalTpl($ptApp, $response, 'listz.php', ['sequence' => $sequence,
'page' => $page, 'pageCount' => $pageCount,
'z' => $structs]);
} else {
fiscalTpl($response, 'apierror.php', $apiResult);
fiscalTpl($ptApp, $response, 'apierror.php', $apiResult);
}
return $response;
}
......@@ -111,7 +111,7 @@ $app->any('/fiscal/sequence/{sequence}/tickets/', function ($request, $response,
$ptApp = $this->get('settings')['ptApp'];
$response = fiscalLogin($this, $ptApp, $request, $response);
if ($ptApp->getCurrentUser() == null) {
fiscalTpl($response, 'login.php');
fiscalTpl($ptApp, $response, 'login.php');
return $response;
} else {
// Get pagination input
......@@ -124,7 +124,7 @@ $app->any('/fiscal/sequence/{sequence}/tickets/', function ($request, $response,
['sequence' => $sequence]);
$pageCount = 0;
if ($apiCount->getStatus() != APIResult::STATUS_CALL_OK) {
fiscalTpl($response, 'apierror.php', $apiCount);
fiscalTpl($ptApp, $response, 'apierror.php', $apiCount);
return $response;
} else {
$zCount = $apiCount->getContent();
......@@ -149,11 +149,11 @@ $app->any('/fiscal/sequence/{sequence}/tickets/', function ($request, $response,
$structs[] = $struct;
}
// Render
fiscalTpl($response, 'listtkts.php', ['sequence' => $sequence,
fiscalTpl($ptApp, $response, 'listtkts.php', ['sequence' => $sequence,
'page' => $page, 'pageCount' => $pageCount,
'tickets' => $structs]);
} else {
fiscalTpl($response, 'apierror.php', $apiResult);
fiscalTpl($ptApp, $response, 'apierror.php', $apiResult);
}
return $response;
}
......@@ -164,15 +164,15 @@ $app->any('/fiscal/', function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$response = fiscalLogin($this, $ptApp, $request, $response);
if ($ptApp->getCurrentUser() == null) {
fiscalTpl($response, 'login.php');
fiscalTpl($ptApp, $response, 'login.php');
return $response;
} else {
$apiResult = APICaller::run($ptApp, 'fiscal', 'getSequences');
if ($apiResult->getStatus() == APIResult::STATUS_CALL_OK) {
$data = $apiResult->getContent();
fiscalTpl($response, 'menu.php', $data);
fiscalTpl($ptApp, $response, 'menu.php', $data);
} else {
fiscalTpl($response, 'apierror.php', $apiResult);
fiscalTpl($ptApp, $response, 'apierror.php', $apiResult);
}
return $response;
}
......@@ -183,7 +183,7 @@ $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');
fiscalTpl($ptApp, $response, 'login.php');
return $response;
} else {
$params = $request->getQueryParams();
......@@ -256,7 +256,7 @@ $app->GET('/fiscal/export', function ($request, $response, $args) {
fclose($file);
unlink($jsonFileName);
unlink($zipFileName);
fiscalTpl($response, 'apierror.php', $apiResult);
fiscalTpl($ptApp, $response, 'apierror.php', $apiResult);
return $response;
}
}
......@@ -301,13 +301,7 @@ $app->GET('/api/fiscal/export', function ($request, $response, $args) {
return $response->withAPIResult($apiResult);
});
/** Fiscal import API */
$app->POST('/api/fiscal/import', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
if (!$ptApp->isFiscalMirror()) {
return $response->withStatus(400, 'Only available for Fiscal Mirrors');
}
$data = $request->getParsedBody();
function fiscal_importTickets($ptApp, $data) {
$tkts = [];
foreach ($data as $tkt) {
$tkt['date'] = DateUtils::readDate($tkt['date']);
......@@ -327,6 +321,58 @@ $app->POST('/api/fiscal/import', function($request, $response, $args) {
$tkts[] = FiscalTicket::fromStruct($tkt, $ptApp->getDao());
}
$apiResult = APICaller::run($ptApp, 'fiscal', 'batchImport', [$tkts]);
return $response->withAPIResult($apiResult);
return $apiResult;
}
/** Fiscal import API */
$app->POST('/api/fiscal/import', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
if (!$ptApp->isFiscalMirror()) {
return $response->withStatus(400, 'Only available for Fiscal Mirrors');
}
$data = $request->getParsedBody();
return $response->withAPIResult(fiscal_importTickets($ptApp, $data));
});
/** Fiscal import interface */
$app->POST('/fiscal/import', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
if (!$ptApp->isFiscalMirror()) {
return $response->withStatus(400, 'Only available for Fiscal Mirrors');
}
$files = $request->getUploadedFiles();
if (empty($files['file'])) {
return $response->withStatus(400, 'Bad Request');
}
$file = $files['file'];
$stream = $file->getStream();
$fileName = $stream->getMetadata('uri');
// Try to unzip if required
$zip = new ZipArchive();
$res = $zip->open($fileName);
if ($res === true) {
if ($zip->count() != 1) {
$apiResult = APIResult::reject("le fichier zip ne doit contenir qu'un seul fichier.");
fiscalTpl($ptApp, $response, 'imported.php', $apiResult);
return $response;
}
$data = $zip->getFromIndex(0);
$zip->close();
} else {
$data = '';
$read = $stream->read(2048);
while ($read != '') {
$data .= $read;
$read = $stream->read(2048);
}
}
$data = json_decode($data, true);
if ($data === null) {
$apiResult = APIResult::reject("les tickets fiscaux n'ont pu être lus depuis le fichier envoyé.");
fiscalTpl($ptApp, $response, 'imported.php', $apiResult);
return $response;
}
$apiResult = fiscal_importTickets($ptApp, $data);
fiscalTPL($ptApp, $response, 'imported.php', $apiResult);
return $response;
});
<?php
function render($data) {
function render($ptApp, $data) {
$ret = '<h2>Error</h2>';
$ret .= '<p>Status: ' . htmlspecialchars($data->getStatus()) . '</p>';
$ret .= '<pre>' . htmlspecialchars($data->getContent()) . '</pre>';
......
<?php
use \Pasteque\Server\System\API\APIResult;
function render($ptApp, $apiResult) {
$ret = '<h1>Import des tickets fiscaux</h1>';
if ($apiResult->getStatus() != APIResult::STATUS_CALL_OK) {
$ret .= "<p>L'import a échoué : " . $apiResult->getContent() . '</p>';
return $ret;
}
$res = $apiResult->getContent();
if (count($res->get('successes')) == 0 && count($res->get('failures')) == 0) {
$ret .= "<p>Aucun ticket à importer.</p>";
$ret .= "<p><a href=\".\">Retourner au menu</a></p>";
return $ret;
}
if (count($res->get('successes')) > 0) {
$ret .= "<p>" . count($res->get('successes')) . " tickets ont été importés avec succès.</p>";
}
if (count($res->get('failures')) > 0) {
$ret .= "<p>" . count($res->get('failures')) . " tickets n'ont pu être importés :</p>";
$ret .= '<ul>';
foreach ($res->get('failures') as $fail) {
$tkt = $fail->get('ticket');
$ret .= '<li>Ticket ' . $tkt->getType() . '-' . $tkt->getSequence() . ' #' . $tkt->getNumber() . ' : ' . $fail->get('reason') . '</li>';
}
$ret .= '</ul>';
}
$ret .= "<p><a href=\".\">Retourner au menu</a></p>";
return $ret;
}
......@@ -30,7 +30,7 @@ function renderPagination($data) {
return $ret;
}
function render($data) {
function render($ptApp, $data) {
$ret = '<h2>Liste des tickets</h2>';
$ret .= renderPagination($data);
foreach ($data['tickets'] as $tkt) {
......
......@@ -30,7 +30,7 @@ function renderPagination($data) {
return $ret;
}
function render($data) {
function render($ptApp, $data) {
$ret = '<h2>Liste des tickets Z</h2>';
$ret .= renderPagination($data);
foreach ($data['z'] as $z) {
......
<?php
function render($data) {
function render($ptApp, $data) {
return <<<HTML
<div id="content" class="container-fluid">
<div id="login" style="margin: auto; width: 30ex; padding-top: 8%;">
......
<?php
function render($data) {
function render($ptApp, $data) {
$ret = '<h1>Consultation des données fiscale</h1>';
$ret .= '<h2>Lister tous les tickets par caisse</h2>';
$ret .= '<ul>';
......@@ -20,5 +20,11 @@ function render($data) {
$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>';
if ($ptApp->isFiscalMirror()) {
$ret .= '<h2>Import des tickets</h2>';
$ret .= '<p>Exportez les tickets depuis votre instance d\'usage pour en reverser une copie sur ce miroir. Les tickets déjà importés seront ignorés.</p>';
$ret .= '<p><form method="POST" action="import" enctype="multipart/form-data">';
$ret .= '<input type="file" name="file" required="true" /> <input type="submit" value="Envoyer" /></form></p>';
}
return $ret;
}
Markdown is supported
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