Commit ba7c0143 authored by Karamel's avatar Karamel
Browse files

List all ticket types on /fiscal/, mostly to mirror some custom API.

parent 673cc4be
......@@ -104,9 +104,9 @@ $app->any('/fiscal/sequence/{sequence}/z/',
$structs[] = $struct;
}
// Render
fiscalTpl($ptApp, $response, 'listz.php', ['sequence' => $sequence,
fiscalTpl($ptApp, $response, 'listtkts.php', ['sequence' => $sequence,
'page' => $page, 'pageCount' => $pageCount,
'z' => $structs]);
'tickets' => $structs, 'typeName' => 'tickets Z']);
} else {
fiscalTpl($ptApp, $response, 'apierror.php', $apiResult);
}
......@@ -160,7 +160,7 @@ $app->any('/fiscal/sequence/{sequence}/tickets/',
// Render
fiscalTpl($ptApp, $response, 'listtkts.php', ['sequence' => $sequence,
'page' => $page, 'pageCount' => $pageCount,
'tickets' => $structs]);
'tickets' => $structs, 'typeName' => 'tickets']);
} else {
fiscalTpl($ptApp, $response, 'apierror.php', $apiResult);
}
......@@ -168,6 +168,80 @@ $app->any('/fiscal/sequence/{sequence}/tickets/',
}
});
/** Other listing */
$app->any('/fiscal/sequence/{sequence}/other',
function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$response = fiscalLogin($this, $ptApp, $request, $response);
if ($ptApp->getCurrentUser() == null) {
fiscalTpl($ptApp, $response, 'login.php');
return $response;
}
// Check type
$types = null;
$apiTypeRes = APICaller::run($ptApp, 'fiscal', 'getTypes');
if ($apiTypeRes->getStatus() == APIResult::STATUS_CALL_OK) {
$types = $apiTypeRes->getContent();
} else {
fiscalTpl($ptApp, $response, 'apierror.php', $apiTypeRes);
return $response;
}
$validType = false;
$queryParams = $request->getQueryParams();
$type = $queryParams['type'];
foreach ($types as $t) {
if ($t == $type) {
$validType = true;
break;
}
}
if (!$validType) {
return fiscalHomePage();
}
// Get pagination input
$page = (!empty($queryParams['page'])) ? $queryParams['page'] : 0;
$sequence = $args['sequence'];
$count = 10;
// Get total count of tickets (for pagination)
$apiCount = APICaller::run($ptApp, 'fiscal', 'count', [$sequence, $type]);
$pageCount = 0;
if ($apiCount->getStatus() != APIResult::STATUS_CALL_OK) {
fiscalTpl($ptApp, $response, 'apierror.php', $apiCount);
return $response;
} else {
$ticketCount = $apiCount->getContent();
$pageCount = intval($apiCount->getContent() / $count);
if ($ticketCount % $count > 0) { $pageCount++; }
}
// Get tickets
$apiResult = APICaller::run($ptApp, 'fiscal', 'listByType',
[$type, $sequence, $count, $page]);
if ($apiResult->getStatus() == APIResult::STATUS_CALL_OK) {
// Convert tickets to structs and check signature
$data = $apiResult->getContent();
$structs = [];
if ($page == 0 && count($data) > 0) {
$struct = $data[0]->toStruct();
$struct['signature'] = $data[0]->checkSignature(null) ? 'ok' : 'NOK';
$structs[] = $struct;
}
for ($i = 1; $i < count($data); $i++) {
$struct = $data[$i]->toStruct();
$struct['signature'] = $data[$i]->checkSignature($data[$i - 1]) ? 'ok' : 'NOK';
$structs[] = $struct;
}
// Render
$typeName = sprintf('%ss', $type);
fiscalTpl($ptApp, $response, 'listtkts.php', ['sequence' => $sequence,
'page' => $page, 'pageCount' => $pageCount,
'tickets' => $structs, 'typeName' => $typeName]);
} else {
fiscalTpl($ptApp, $response, 'apierror.php', $apiResult);
}
return $response;
});
function fiscalHomePage($ptApp, $response) {
$data = ['user' => $ptApp->getCurrentUser()['id']];
$apiResult = APICaller::run($ptApp, 'fiscal', 'getSequences');
......@@ -177,6 +251,13 @@ function fiscalHomePage($ptApp, $response) {
fiscalTpl($ptApp, $response, 'apierror.php', $apiResult);
return $response;
}
$apiTypeRes = APICaller::run($ptApp, 'fiscal', 'getTypes');
if ($apiTypeRes->getStatus() == APIResult::STATUS_CALL_OK) {
$data['types'] = $apiTypeRes->getContent();
} else {
fiscalTpl($ptApp, $response, 'apierror.php', $apiTypeRes);
return $response;
}
if ($ptApp->isGPGEnabled()) {
$data['gpg'] = true;
$apiResult = APICaller::run($ptApp, 'archive', 'listArchives');
......
......@@ -31,7 +31,7 @@ function renderPagination($data) {
}
function render($ptApp, $data) {
$ret = '<h2>Liste des tickets</h2>';
$ret = '<h2>Liste des ' . htmlspecialchars($data['typeName']) . '</h2>';
$ret .= renderPagination($data);
foreach ($data['tickets'] as $tkt) {
$ret .= renderTicket($tkt);
......
<?php
use \Pasteque\Server\System\DateUtils;
function renderZ($z) {
$data = json_decode($z['content']);
$output = ($z['content']);
if ($data !== null) {
$output = json_encode($data, \JSON_PRETTY_PRINT);
}
$ret = '<div>';
$ret .= '<h3>Numéro : ' . $z['number'] . '</h3>';
$ret .= '<p>Signature : ' . $z['signature'] . '</p>' ;
$ret .= '<pre>' . htmlspecialchars($output) . '</pre>';
$ret .= '</div>';
return $ret;
}
function renderPagination($data) {
$ret = '<p>Page</p>';
$ret .= '<p>';
for ($i = 0; $i < $data['pageCount']; $i++) {
if ($data['page'] == $i) {
$ret .= $i . ' ';
} else {
$ret .= '<a href=".?page=' . htmlspecialchars($i) . '">' . htmlspecialchars($i) . '</a> ';
}
}
$ret .= '</p>';
return $ret;
}
function render($ptApp, $data) {
$ret = '<h2>Liste des tickets Z</h2>';
$ret .= renderPagination($data);
foreach ($data['z'] as $z) {
$ret .= renderZ($z);
}
$ret .= renderPagination($data);
return $ret;
}
<?php
use \Pasteque\Server\Model\FiscalTicket;
function render($ptApp, $data) {
$ret = sprintf("<p>Connecté·e en tant que %s. <a href=\"./disconnect\">Déconnexion</a></p>", $data['user']);
$ret .= '<h1>Consultation des données fiscale</h1>';
$ret .= '<h2>Lister tous les tickets par caisse</h2>';
$ret .= '<ul>';
foreach ($data['sequences'] as $sequence) {
$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 .= '<li>' . htmlspecialchars($sequence) . ' : <a href="./sequence/' . htmlspecialchars($sequence) . '/z/" target="_blank">Tickets Z</a> <a href="./sequence/' . htmlspecialchars($sequence) . '/tickets/" target="_blank">Tickets</a>';
foreach ($data['types'] as $customType) {
if ($customType != FiscalTicket::TYPE_ZTICKET
&& $customType != FiscalTicket::TYPE_TICKET) {
$ret .= ' <a href="./sequence/' . htmlspecialchars($sequence) . '/other?type=' . htmlspecialchars($customType) . '" target="_blank">' . htmlspecialchars($customType) . '</a>';
}
}
$ret .= '</li>';
}
$ret .= '</ul>';
if (!empty($data['gpg'])) {
......
......@@ -44,11 +44,6 @@ class FiscalAPI implements API
null, null, null, $order);
}
public function search($conditions, $count = null, $offset = null, $order) {
return $this->dao->search(static::MODEL_NAME, $conditions,
$count, $offset, $order);
}
public function getLastFiscalTicket($type, $sequence) {
// Look for an existing fiscal ticket
$lastFTicket = null;
......@@ -80,30 +75,26 @@ class FiscalAPI implements API
return $result;
}
/** Count the number of z tickets, including EOS. */
public function countZ($sequence) {
public function getTypes() {
$em = $this->dao->getEntityManager();
$q = $em->createQuery('select count(s.sequence) '
$q = $em->createQuery('select distinct(s.type) '
. 'from \Pasteque\Server\Model\FiscalTicket s '
. 'where s.type = :type and s.sequence = :sequence') ;
$q->setParameters(['type' => FiscalTicket::TYPE_ZTICKET,
'sequence' => $sequence]);
$sequences = $q->getResult();
. 'order by s.type');
$types = $q->getResult();
$result = [];
if (count($sequences) > 0) {
return $sequences[0][1];
} else {
return 0;
foreach ($types as $type) {
$result[] = $type[1];
}
return $result;
}
/** Count the number of tickets, including EOS. */
public function countTickets($sequence) {
public function count($sequence, $type) {
$em = $this->dao->getEntityManager();
$q = $em->createQuery('select count(s.sequence) '
. 'from \Pasteque\Server\Model\FiscalTicket s '
. 'where s.type = :type and s.sequence = :sequence');
$q->setParameters(['type' => FiscalTicket::TYPE_TICKET,
. 'where s.type = :type and s.sequence = :sequence') ;
$q->setParameters(['type' => $type,
'sequence' => $sequence]);
$sequences = $q->getResult();
$result = [];
......@@ -112,55 +103,41 @@ class FiscalAPI implements API
} else {
return 0;
}
}
/** Count the number of z tickets, including EOS. */
public function countZ($sequence) {
return $this->count($sequence, FiscalTicket::TYPE_ZTICKET);
}
/** Count the number of tickets, including EOS. */
public function countTickets($sequence) {
return $this->count($sequence, FiscalTicket::TYPE_TICKET);
}
/** List Z tickets from a $sequence. If $page > 0 it will also fetch
* the previous Z to be able to check the signature. */
public function listZ($sequence, $count, $page) {
$em = $this->dao->getEntityManager();
$q = $em->createQuery('select s '
. 'from \Pasteque\Server\Model\FiscalTicket s '
. 'where s.sequence = :seq and s.type = :type '
. 'order by s.number asc');
$q->setParameters(['seq' => $sequence,
'type' => FiscalTicket::TYPE_ZTICKET]);
$offset = 1; // Avoid EOS
$includePrevious = false;
if ($page > 0) {
$offset = $count * $page; // +1 (EOS) - 1 (previous)
$includePrevious = true;
}
$q->setMaxResults(($includePrevious) ? $count + 1 : $count);
$q->setFirstResult($offset);
$z = $q->getResult();
$result = [];
foreach ($z as $res) {
$result[] = $res;
}
if (count($result) < $count) {
// Add EOS on the last page
$eosSearch = $this->dao->search(static::MODEL_NAME,
[new DAOCondition('type', '=', FiscalTicket::TYPE_ZTICKET),
new DAOCondition('sequence', '=', $sequence),
new DAOCondition('number', '=', 0)]);
if (count($eosSearch) > 0) {
$result[] = $eosSearch[0];
}
}
return $result;
return $this->listByType(FiscalTicket::TYPE_ZTICKET, $sequence, $count, $page);
}
/** List tickets from a $sequence. If $page > 0 it will also fetch
* the previous ticket to be able to check the signature. */
public function listTickets($sequence, $count, $page) {
return $this->listByType(FiscalTicket::TYPE_TICKET, $sequence, $count, $page);
}
/** List custom tickets from a $sequence. If $page > 0 it will also
* fetch the previous ticket to be able to check the signature. */
public function listByType($type, $sequence, $count, $page) {
$em = $this->dao->getEntityManager();
// order by date asc then number desc because EOS has the same date
$q = $em->createQuery('select s '
. 'from \Pasteque\Server\Model\FiscalTicket s '
. 'where s.sequence = :seq and s.type = :type '
. 'order by s.number asc');
$q->setParameters(['seq' => $sequence,
'type' => FiscalTicket::TYPE_TICKET]);
'type' => $type]);
$offset = 1; // Avoid EOS
$includePrevious = false;
if ($page > 0) {
......@@ -169,15 +146,15 @@ class FiscalAPI implements API
}
$q->setMaxResults(($includePrevious) ? $count + 1 : $count);
$q->setFirstResult($offset);
$z = $q->getResult();
$tkts = $q->getResult();
$result = [];
foreach ($z as $res) {
foreach ($tkts as $res) {
$result[] = $res;
}
if (count($result) < $count) {
// Add EOS on the last page
$eosSearch = $this->dao->search(static::MODEL_NAME,
[new DAOCondition('type', '=', FiscalTicket::TYPE_TICKET),
[new DAOCondition('type', '=', $type),
new DAOCondition('sequence', '=', $sequence),
new DAOCondition('number', '=', 0)]);
if (count($eosSearch) > 0) {
......
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