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 Accountancy - FEC/FEC2 format export with attachments #26192

Merged
merged 5 commits into from
Oct 15, 2023
Merged
Changes from all 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
138 changes: 130 additions & 8 deletions htdocs/accountancy/class/accountancyexport.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,10 @@ public function export(&$TData, $formatexportset, $withAttachment = 0, $download
$this->exportGestimumV5($TData, $exportFile);
break;
case self::$EXPORT_TYPE_FEC:
$this->exportFEC($TData, $exportFile);
$archiveFileList = $this->exportFEC($TData, $exportFile, $archiveFileList, $withAttachment);
break;
case self::$EXPORT_TYPE_FEC2:
$this->exportFEC2($TData, $exportFile);
$archiveFileList = $this->exportFEC2($TData, $exportFile, $archiveFileList, $withAttachment);
break;
case self::$EXPORT_TYPE_ISUITEEXPERT :
$this->exportiSuiteExpert($TData, $exportFile);
Expand Down Expand Up @@ -1297,14 +1297,19 @@ public function exportConfigurable($objectLines, $exportFile = null)

/**
* Export format : FEC
* Last review for this format : 2023/10/12 Alexandre Spangaro (aspangaro@open-dsi.fr)
*
* Help to import in your software: https://wiki.dolibarr.org/index.php?title=Module_Comptabilit%C3%A9_en_Partie_Double#Exports_avec_fichiers_sources
*
* @param array $objectLines data
* @param resource $exportFile [=null] File resource to export or print if null
* @return void
* @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 exportFEC($objectLines, $exportFile = null)
public function exportFEC($objectLines, $exportFile = null, $archiveFileList = array(), $withAttachment = 0)
{
global $langs;
global $conf, $langs;

$separator = "\t";
$end_line = "\r\n";
Expand All @@ -1330,6 +1335,7 @@ public function exportFEC($objectLines, $exportFile = null)
$tab[] = "Idevise";
$tab[] = "DateLimitReglmt";
$tab[] = "NumFacture";
$tab[] = "FichierFacture";

$output = implode($separator, $tab).$end_line;
if ($exportFile) {
Expand Down Expand Up @@ -1402,6 +1408,7 @@ public function exportFEC($objectLines, $exportFile = null)
// FEC:EcritureLib
// Clean label operation to prevent problem on export with tab separator & other character
$line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
$line->label_operation = str_replace(array("..."), "", $line->label_operation);
$tab[] = dol_string_unaccent($line->label_operation);

// FEC:Debit
Expand Down Expand Up @@ -1433,6 +1440,58 @@ public function exportFEC($objectLines, $exportFile = null)
$refInvoice = str_replace(array("\t", "\n", "\r"), " ", $refInvoice);
$tab[] = dol_trunc(self::toAnsi($refInvoice), 17, 'right', 'UTF-8', 1);

// FEC_suppl:FichierFacture
// get document file
$attachmentFileName = '';
if ($withAttachment == 1) {
$attachmentFileKey = trim($line->piece_num);

if (!isset($archiveFileList[$attachmentFileKey])) {
$objectDirPath = '';
$objectFileName = dol_sanitizeFileName($line->doc_ref);
if ($line->doc_type == 'customer_invoice') {
$objectDirPath = !empty($conf->invoice->multidir_output[$conf->entity]) ? $conf->invoice->multidir_output[$conf->entity] : $conf->invoice->dir_output;
} elseif ($line->doc_type == 'expense_report') {
$objectDirPath = !empty($conf->expensereport->multidir_output[$conf->entity]) ? $conf->expensereport->multidir_output[$conf->entity] : $conf->expensereport->dir_output;
} elseif ($line->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;
$objectDirPath.= '/'.rtrim(get_exdir($invoice->id, 2, 0, 0, $invoice, 'invoice_supplier'), '/');
}
$arrayofinclusion = array();
// If it is a supplier invoice, we want to use last uploaded file
$arrayofinclusion[] = '^'.preg_quote($objectFileName, '/').(($line->doc_type == 'supplier_invoice') ? '.+' : '').'\.pdf$';
$fileFoundList = dol_dir_list($objectDirPath.'/'.$objectFileName, 'files', 0, implode('|', $arrayofinclusion), '(\.meta|_preview.*\.png)$', 'date', SORT_DESC, 0, true);
if (!empty($fileFoundList)) {
$attachmentFileNameTrunc = $line->doc_ref;
foreach ($fileFoundList as $fileFound) {
if (strstr($fileFound['name'], $objectFileName)) {
// skip native invoice pdfs (canelle)
// We want to retrieve an attachment representative of the supplier invoice, not a fake document generated by Dolibarr.
if ($line->doc_type == 'supplier_invoice') {
if ($fileFound['name'] === $objectFileName.'.pdf') continue;
} elseif ($fileFound['name'] !== $objectFileName.'.pdf') {
continue;
}
$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'];
}
}

$tab[] = $attachmentFileName;

$output = implode($separator, $tab).$end_line;
if ($exportFile) {
fwrite($exportFile, $output);
Expand All @@ -1441,18 +1500,25 @@ public function exportFEC($objectLines, $exportFile = null)
}
}
}

return $archiveFileList;
}

/**
* Export format : FEC2
* Last review for this format : 2023/10/12 Alexandre Spangaro (aspangaro@open-dsi.fr)
*
* Help to import in your software: https://wiki.dolibarr.org/index.php?title=Module_Comptabilit%C3%A9_en_Partie_Double#Exports_avec_fichiers_sources
*
* @param array $objectLines data
* @param resource $exportFile [=null] File resource to export or print if null
* @return void
* @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 exportFEC2($objectLines, $exportFile = null)
public function exportFEC2($objectLines, $exportFile = null, $archiveFileList = array(), $withAttachment = 0)
{
global $langs;
global $conf, $langs;

$separator = "\t";
$end_line = "\r\n";
Expand All @@ -1478,6 +1544,7 @@ public function exportFEC2($objectLines, $exportFile = null)
$tab[] = "Idevise";
$tab[] = "DateLimitReglmt";
$tab[] = "NumFacture";
$tab[] = "FichierFacture";

$output = implode($separator, $tab).$end_line;
if ($exportFile) {
Expand Down Expand Up @@ -1550,6 +1617,7 @@ public function exportFEC2($objectLines, $exportFile = null)
// FEC:EcritureLib
// Clean label operation to prevent problem on export with tab separator & other character
$line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
$line->label_operation = str_replace(array("..."), "", $line->label_operation);
$tab[] = dol_string_unaccent($line->label_operation);

// FEC:Debit
Expand Down Expand Up @@ -1581,6 +1649,58 @@ public function exportFEC2($objectLines, $exportFile = null)
$refInvoice = str_replace(array("\t", "\n", "\r"), " ", $refInvoice);
$tab[] = dol_trunc(self::toAnsi($refInvoice), 17, 'right', 'UTF-8', 1);

// FEC_suppl:FichierFacture
// get document file
$attachmentFileName = '';
if ($withAttachment == 1) {
$attachmentFileKey = trim($line->piece_num);

if (!isset($archiveFileList[$attachmentFileKey])) {
$objectDirPath = '';
$objectFileName = dol_sanitizeFileName($line->doc_ref);
if ($line->doc_type == 'customer_invoice') {
$objectDirPath = !empty($conf->invoice->multidir_output[$conf->entity]) ? $conf->invoice->multidir_output[$conf->entity] : $conf->invoice->dir_output;
} elseif ($line->doc_type == 'expense_report') {
$objectDirPath = !empty($conf->expensereport->multidir_output[$conf->entity]) ? $conf->expensereport->multidir_output[$conf->entity] : $conf->expensereport->dir_output;
} elseif ($line->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;
$objectDirPath.= '/'.rtrim(get_exdir($invoice->id, 2, 0, 0, $invoice, 'invoice_supplier'), '/');
}
$arrayofinclusion = array();
// If it is a supplier invoice, we want to use last uploaded file
$arrayofinclusion[] = '^'.preg_quote($objectFileName, '/').(($line->doc_type == 'supplier_invoice') ? '.+' : '').'\.pdf$';
$fileFoundList = dol_dir_list($objectDirPath.'/'.$objectFileName, 'files', 0, implode('|', $arrayofinclusion), '(\.meta|_preview.*\.png)$', 'date', SORT_DESC, 0, true);
if (!empty($fileFoundList)) {
$attachmentFileNameTrunc = $line->doc_ref;
foreach ($fileFoundList as $fileFound) {
if (strstr($fileFound['name'], $objectFileName)) {
// skip native invoice pdfs (canelle)
// We want to retrieve an attachment representative of the supplier invoice, not a fake document generated by Dolibarr.
if ($line->doc_type == 'supplier_invoice') {
if ($fileFound['name'] === $objectFileName.'.pdf') continue;
} elseif ($fileFound['name'] !== $objectFileName.'.pdf') {
continue;
}
$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'];
}
}

$tab[] = $attachmentFileName;

$output = implode($separator, $tab).$end_line;
if ($exportFile) {
fwrite($exportFile, $output);
Expand All @@ -1589,6 +1709,8 @@ public function exportFEC2($objectLines, $exportFile = null)
}
}
}

return $archiveFileList;
}

/**
Expand Down
Loading