Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Quadratus export with attachments in accountancy export #23209

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 57 additions & 41 deletions htdocs/accountancy/bookkeeping/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
/* Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
* Copyright (C) 2013-2016 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2013-2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2022 Lionel Vessiller <lvessiller@open-dsi.fr>
* Copyright (C) 2016-2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
* Copyright (C) 2022 Progiseize <a.bisotti@progiseize.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -715,58 +717,62 @@
}
}

$mimetype = $accountancyexport->getMimeType($formatexportset);

top_httphead($mimetype, 1);

// Output data on screen
$accountancyexport->export($object->lines, $formatexportset);

$notifiedexportdate = GETPOST('notifiedexportdate', 'alpha');
$notifiedvalidationdate = GETPOST('notifiedvalidationdate', 'alpha');
$withAttachment = !empty(trim(GETPOST('notifiedexportfull', 'alphanohtml'))) ? 1 : 0;

if (!empty($accountancyexport->errors)) {
dol_print_error('', '', $accountancyexport->errors);
} elseif (!empty($notifiedexportdate) || !empty($notifiedvalidationdate)) {
// Specify as export : update field date_export or date_validated
$error = 0;
$db->begin();
// Output data on screen or download
$result = $accountancyexport->export($object->lines, $formatexportset, $withAttachment);

if (is_array($object->lines)) {
foreach ($object->lines as $movement) {
$now = dol_now();

$sql = " UPDATE ".MAIN_DB_PREFIX."accounting_bookkeeping";
$sql .= " SET";
if (!empty($notifiedexportdate) && !empty($notifiedvalidationdate)) {
$sql .= " date_export = '".$db->idate($now)."'";
$sql .= ", date_validated = '".$db->idate($now)."'";
} elseif (!empty($notifiedexportdate)) {
$sql .= " date_export = '".$db->idate($now)."'";
} elseif (!empty($notifiedvalidationdate)) {
$sql .= " date_validated = '".$db->idate($now)."'";
$error = 0;
if ($result < 0) {
$error++;
} else {
if (!empty($notifiedexportdate) || !empty($notifiedvalidationdate)) {
if (is_array($object->lines)) {
// Specify as export : update field date_export or date_validated
$db->begin();

foreach ($object->lines as $movement) {
$now = dol_now();

$sql = " UPDATE ".MAIN_DB_PREFIX."accounting_bookkeeping";
$sql .= " SET";
if (!empty($notifiedexportdate) && !empty($notifiedvalidationdate)) {
$sql .= " date_export = '".$db->idate($now)."'";
$sql .= ", date_validated = '".$db->idate($now)."'";
} elseif (!empty($notifiedexportdate)) {
$sql .= " date_export = '".$db->idate($now)."'";
} elseif (!empty($notifiedvalidationdate)) {
$sql .= " date_validated = '".$db->idate($now)."'";
}
$sql .= " WHERE rowid = ".((int) $movement->id);

dol_syslog("/accountancy/bookkeeping/list.php Function export_file Specify movements as exported", LOG_DEBUG);

$result = $db->query($sql);
if (!$result) {
$error++;
break;
}
}
$sql .= " WHERE rowid = ".((int) $movement->id);

dol_syslog("/accountancy/bookkeeping/list.php Function export_file Specify movements as exported", LOG_DEBUG);

$result = $db->query($sql);
if (!$result) {
if (!$error) {
$db->commit();
} else {
$error++;
break;
$accountancyexport->errors[] = $langs->trans('NotAllExportedMovementsCouldBeRecordedAsExportedOrValidated');
$db->rollback();
}
}
}
}

if (!$error) {
$db->commit();
} else {
$error++;
$db->rollback();
dol_print_error('', $langs->trans("NotAllExportedMovementsCouldBeRecordedAsExportedOrValidated"));
}
if ($error) {
setEventMessages('', $accountancyexport->errors, 'errors');
header('Location: '.$_SERVER['PHP_SELF']);
}
exit;
exit(); // download or show errors
}
}

Expand Down Expand Up @@ -854,7 +860,17 @@
$form_question['separator3'] = array('name'=>'separator3', 'type'=>'separator');
}

$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?'.$param, $langs->trans("ExportFilteredList").' ('.$listofformat[$formatexportset].')', $langs->trans('ConfirmExportFile'), 'export_fileconfirm', $form_question, '', 1, 350, 600);
// add documents in an archive for accountancy export (Quadratus)
if (getDolGlobalString('ACCOUNTING_EXPORT_MODELCSV') == AccountancyExport::$EXPORT_TYPE_QUADRATUS) {
$form_question['notifiedexportfull'] = array(
'name' => 'notifiedexportfull',
'type' => 'checkbox',
'label' => $langs->trans('NotifiedExportFull'),
'value' => 'false',
);
}

$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?'.$param, $langs->trans("ExportFilteredList").' ('.$listofformat[$formatexportset].')', $langs->trans('ConfirmExportFile'), 'export_fileconfirm', $form_question, '', 1, 400, 600);
}

//if ($action == 'delbookkeepingyear') {
Expand Down
182 changes: 171 additions & 11 deletions htdocs/accountancy/class/accountancyexport.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
* Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
* Copyright (C) 2016 Pierre-Henry Favre <phf@atm-consulting.fr>
* Copyright (C) 2016-2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2016-2022 Open-DSI <support@open-dsi.fr>
* Copyright (C) 2013-2017 Olivier Geffroy <jeff@jeffinfo.com>
* Copyright (C) 2017 Elarifr. Ari Elbaz <github@accedinfo.com>
* Copyright (C) 2017-2019 Frédéric France <frederic.france@netlogic.fr>
* Copyright (C) 2017 André Schild <a.schild@aarboard.ch>
* Copyright (C) 2020 Guillaume Alexandre <guillaume@tag-info.fr>
* Copyright (C) 2022 Joachim Kueter <jkueter@gmx.de>
* Copyright (C) 2022 Progiseize <a.bisotti@progiseize.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -36,6 +37,7 @@
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';


/**
Expand Down Expand Up @@ -313,9 +315,10 @@ public function getMimeType($formatexportset)
*
* @param array $TData Array with data
* @param int $formatexportset Id of export format
* @return void
* @param int $withAttachment [=0] Not add files or 1 to have attached in an archive (ex : Quadratus)
* @return int <0 if KO, >0 OK
*/
public function export(&$TData, $formatexportset)
public function export(&$TData, $formatexportset, $withAttachment = 0)
{
global $conf, $langs;
global $search_date_end; // Used into /accountancy/tpl/export_journal.tpl.php
Expand All @@ -325,8 +328,44 @@ public function export(&$TData, $formatexportset)
$type_export = 'general_ledger';

global $db; // The tpl file use $db
$completefilename = '';
$exportFile = null;
$exportFileName = '';
$exportFilePath = '';
$archiveFileList = array();
if ($withAttachment == 1) {
// PHP ZIP extension must be enabled
if (!extension_loaded('zip')) {
$langs->load('install');
$this->errors[] = $langs->trans('ErrorPHPDoesNotSupport', 'ZIP');;
return -1;
}
} else {
$mimetype = $this->getMimeType($formatexportset);
top_httphead($mimetype, 1);
}
include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';

if ($withAttachment == 1 && !empty($completefilename)) {
// create export file
$tmpDir = !empty($conf->accounting->multidir_temp[$conf->entity]) ? $conf->accounting->multidir_temp[$conf->entity] : $conf->accounting->dir_temp;
$exportFileFullName = $completefilename;
$exportFileBaseName = basename($exportFileFullName);
$exportFileName = pathinfo($exportFileBaseName, PATHINFO_FILENAME);
$exportFilePath = $tmpDir.'/'.$exportFileFullName;
$exportFile = fopen($exportFilePath, 'w');
if (!$exportFile) {
$this->errors[] = $langs->trans('ErrorFileNotFound', $exportFilePath);
return -1;
}
$archiveFileList[0] = array(
'path' => $exportFilePath,
'name' => $exportFileFullName,
);

// archive name and path
$archiveFullName = $exportFileName.'.zip';
$archivePath = $tmpDir.'/'.$archiveFullName;
}

switch ($formatexportset) {
case self::$EXPORT_TYPE_CONFIGURABLE:
Expand All @@ -345,7 +384,7 @@ public function export(&$TData, $formatexportset)
$this->exportCiel($TData);
break;
case self::$EXPORT_TYPE_QUADRATUS:
$this->exportQuadratus($TData);
$archiveFileList = $this->exportQuadratus($TData, $exportFile, $archiveFileList, $withAttachment);
break;
case self::$EXPORT_TYPE_WINFIC:
$this->exportWinfic($TData);
Expand Down Expand Up @@ -399,6 +438,69 @@ public function export(&$TData, $formatexportset)
}
break;
}

// create and download export file or archive
if ($withAttachment == 1) {
$error = 0;

// close export file
if ($exportFile) {
fclose($exportFile);
}

if (!empty($archiveFullName) && !empty($archivePath) && !empty($archiveFileList)) {
// archive files
$downloadFileMimeType = 'application/zip';
$downloadFileFullName = $archiveFullName;
$downloadFilePath = $archivePath;

// create archive
$archive = new ZipArchive();
lvessiller-opendsi marked this conversation as resolved.
Show resolved Hide resolved
$res = $archive->open($archivePath, ZipArchive::OVERWRITE | ZipArchive::CREATE);
if ($res !== true) {
$error++;
$this->errors[] = $langs->trans('ErrorFileNotFound', $archivePath);
}
if (!$error) {
// add files
foreach ($archiveFileList as $archiveFileArr) {
$res = $archive->addFile($archiveFileArr['path'], $archiveFileArr['name']);
if (!$res) {
$error++;
$this->errors[] = $langs->trans('ErrorArchiveAddFile', $archiveFileArr['name']);
break;
}
}
}
if (!$error) {
// close archive
$archive->close();
}
} elseif (!empty($exportFileFullName) && !empty($exportFilePath)) {
// only one file to download
$downloadFileMimeType = 'text/csv';
$downloadFileFullName = $exportFileFullName;
$downloadFilePath = $exportFilePath;
}

if (!$error) {
// download export file
if (!empty($downloadFileMimeType) && !empty($downloadFileFullName) && !empty($downloadFilePath)) {
header('Content-Type: '.$downloadFileMimeType);
header('Content-Disposition: attachment; filename='.$downloadFileFullName);
header('Cache-Control: Public, must-revalidate');
header('Pragma: public');
header('Content-Length: '.dol_filesize($downloadFilePath));
readfileLowMemory($downloadFilePath);
}
}

if ($error) {
return -1;
}
}

return 1;
}


Expand Down Expand Up @@ -584,10 +686,13 @@ public function exportCiel(&$TData)
* Help : https://docplayer.fr/20769649-Fichier-d-entree-ascii-dans-quadracompta.html
* In QuadraCompta | Use menu : "Outils" > "Suivi des dossiers" > "Import ASCII(Compta)"
*
* @param array $TData data
* @return void
* @param array $TData Data
* @param resource $exportFile [=null] File resource to export or print if null
* @param array $archiveFileList [=array()] Archive file list : array of ['path', 'name']
* @param bool $withAttachment [=0] Not add files or 1 to have attached in an archive
* @return array Archive file list : array of ['path', 'name']
*/
public function exportQuadratus(&$TData)
public function exportQuadratus(&$TData, $exportFile = null, $archiveFileList = array(), $withAttachment = 0)
{
global $conf, $db;

Expand Down Expand Up @@ -637,7 +742,11 @@ public function exportQuadratus(&$TData)

$Tab['end_line'] = $end_line;

print implode($Tab);
if ($exportFile) {
fwrite($exportFile, implode($Tab));
} else {
print implode($Tab);
}
}

$Tab = array();
Expand Down Expand Up @@ -708,12 +817,63 @@ public function exportQuadratus(&$TData)
// We need to keep the 10 lastest number of invoice doc_ref not the beginning part that is the unusefull almost same part
// $Tab['num_piece3'] = str_pad(self::trunc($data->piece_num, 10), 10);
$Tab['num_piece3'] = substr(self::trunc($data->doc_ref, 20), -10);
$Tab['filler4'] = str_repeat(' ', 73);
$Tab['reserved'] = str_repeat(' ', 10); // position 159
$Tab['currency_amount'] = str_repeat(' ', 13); // position 169
// get document file
$attachmentFileName = '';
if ($withAttachment == 1) {
$attachmentFileKey = trim($data->piece_num);

if (!isset($archiveFileList[$attachmentFileKey])) {
$objectDirPath = '';
$objectFileName = dol_sanitizeFileName($data->doc_ref);
if ($data->doc_type == 'customer_invoice') {
$objectDirPath = !empty($conf->facture->multidir_output[$conf->entity]) ? $conf->facture->multidir_output[$conf->entity] : $conf->facture->dir_output;
} elseif ($data->doc_type == 'expense_report') {
$objectDirPath = !empty($conf->expensereport->multidir_output[$conf->entity]) ? $conf->expensereport->multidir_output[$conf->entity] : $conf->factureexpensereport->dir_output;
} elseif ($data->doc_type == 'supplier_invoice') {
$objectDirPath = !empty($conf->fournisseur->facture->multidir_output[$conf->entity]) ? $conf->fournisseur->facture->multidir_output[$conf->entity] : $conf->fournisseur->facture->dir_output;
}
$arrayofinclusion = array();
$arrayofinclusion[] = '^'.preg_quote($objectFileName, '/').'\.pdf$';
$fileFoundList = dol_dir_list($objectDirPath.'/'.$objectFileName, 'files', 0, implode('|', $arrayofinclusion), '(\.meta|_preview.*\.png)$', 'date', SORT_DESC, 0, true);
if (!empty($fileFoundList)) {
$attachmentFileNameTrunc = str_pad(self::trunc($data->piece_num, 8), 8, '0', STR_PAD_LEFT);
foreach ($fileFoundList as $fileFound) {
if (strstr($fileFound['name'], $objectFileName)) {
$fileFoundPath = $objectDirPath.'/'.$objectFileName.'/'.$fileFound['name'];
if (file_exists($fileFoundPath)) {
$archiveFileList[$attachmentFileKey] = array(
'path' => $fileFoundPath,
'name' => $attachmentFileNameTrunc.'.pdf',
);
break;
}
}
}
}
}

if (isset($archiveFileList[$attachmentFileKey])) {
$attachmentFileName = $archiveFileList[$attachmentFileKey]['name'];
}
}
if (dol_strlen($attachmentFileName) == 12) {
$Tab['attachment'] = $attachmentFileName; // position 182
} else {
$Tab['attachment'] = str_repeat(' ', 12); // position 182
}
$Tab['filler4'] = str_repeat(' ', 38);
$Tab['end_line'] = $end_line;

print implode($Tab);
if ($exportFile) {
fwrite($exportFile, implode($Tab));
} else {
print implode($Tab);
}
}

return $archiveFileList;
}

/**
Expand Down
Loading