Commit ca5441bc authored by Karamel's avatar Karamel
Browse files

Optimize fiscal ticket export for less memory usage (but slower execution).

parent d029afbc
......@@ -186,32 +186,74 @@ $app->GET('/fiscal/export', function ($request, $response, $args) {
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
$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'));
$tmpName = tempnam(sys_get_temp_dir(), 'ptq');
$file = fopen($jsonFileName, 'w');
fwrite($file, '['); // Init json string
// Get all fiscal tickets
// Run by batches to limit memory consumption
$batchSize = 100;
$page = 0;
$done = false;
while (!$done) {
$apiResult = APICaller::run($ptApp, 'fiscal', 'search', [null, $batchSize, $batchSize * $page, ['type', 'sequence', 'number']]);
$page++;
if ($apiResult->getStatus() == APIResult::STATUS_CALL_OK) {
// Convert tickets to struct
$data = $apiResult->getContent();
if (count($data) == 0) {
$done = true;
} else {
$ftkts = [];
for ($i = 0; $i < count($data); $i++) {
$ftkts[] = $data[$i]->toStruct();
}
$strData = json_encode($ftkts);
// Remove enclosing '[' and ']' before appending and add ',' for next the record
$strData = substr($strData, 1, -1) . ',';
fwrite($file, $strData);
}
} else {
fclose($file);
unlink($jsonFileName);
unlink($zipFileName);
fiscalTpl($response, 'apierror.php', $apiResult);
return $response;
}
}
// End json string and close file
fseek($file, -1, SEEK_END);
fwrite($file, ']');
fclose($file);
// Compress the file
$zip = new ZipArchive();
$zip->open($tmpName, ZipArchive::CREATE);
$zip->addFromString(sprintf('%s.txt', $exportName), json_encode($ftkts));
$zip->open($zipFileName, ZipArchive::CREATE);
$zip->addFile($jsonFileName, sprintf('%s.txt', $exportName));
$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);
$zipFile = fopen($zipFileName, 'r+');
$body = $response->getBody();
while (!feof($zipFile)) {
$body->write(fread($zipFile, 20480));
}
unlink($jsonFileName);
unlink($zipFileName);
return $response;
} else {
fiscalTpl($response, 'apierror.php', $apiResult);
} catch (Exception $e) {
// Clean temp files on error
if (file_exists($jsonFileName)) {
unlink($jsonFileName);
}
if (file_exists($zipFileName)) {
unlink($zipFileName);
}
throw $e;
}
return $response;
}
});
......
......@@ -39,11 +39,16 @@ class FiscalAPI implements API
/** Get all fiscal tickets, including EOS'.
* @param $order The order fields (see DAO->search) */
public function getAll($order = null) {
public function getAll($order) {
return $this->dao->search(FiscalTicket::class,
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;
......
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