Fix ECM files class path issue in PDF saving

- Made ECM files integration optional and graceful
- Added file existence check before requiring ecmfiles.class.php
- Wrapped ECM file creation in try-catch to prevent fatal errors
- PDF is still saved to disk even if ECM integration fails
- Added detailed error logging for troubleshooting
- System now works regardless of ECM files availability
This commit is contained in:
Frank Cools 2025-10-06 17:08:59 +02:00
parent 8d42e47b52
commit 77027debf5

View File

@ -1066,36 +1066,50 @@ class DeclarationTVA
return false;
}
// Add document record to Dolibarr
require_once DOL_DOCUMENT_ROOT . '/core/class/ecmfiles.class.php';
$ecmfile = new EcmFiles($this->db);
// Try to add document record to Dolibarr (optional feature)
$ecmfiles_path = DOL_DOCUMENT_ROOT . '/core/class/ecmfiles.class.php';
$ecmfile->filepath = 'declarationtva/validated/';
$ecmfile->filename = $filename;
$ecmfile->label = 'CA-3 Validated Declaration - ' . $this->declaration_number;
$ecmfile->description = 'Validated CA-3 declaration with detailed breakdown';
$ecmfile->entity = $this->entity;
$ecmfile->fk_user_author = $user->id;
$ecmfile->fk_user_modif = $user->id;
$ecmfile->datec = dol_now();
$ecmfile->tms = dol_now();
$ecmfile->mimetype = 'application/pdf';
$ecmfile->filesize = filesize($dest_path);
$ecmfile->checksum = md5_file($dest_path);
if (file_exists($ecmfiles_path)) {
try {
require_once $ecmfiles_path;
$ecmfile = new EcmFiles($this->db);
$result = $ecmfile->create($user);
if ($result > 0) {
// Try to link document to declaration (this might fail if table doesn't exist)
$link_result = $this->linkDocumentToDeclaration($declaration_id, $ecmfile->id);
if (!$link_result) {
$ecmfile->filepath = 'declarationtva/validated/';
$ecmfile->filename = $filename;
$ecmfile->label = 'CA-3 Validated Declaration - ' . $this->declaration_number;
$ecmfile->description = 'Validated CA-3 declaration with detailed breakdown';
$ecmfile->entity = $this->entity;
$ecmfile->fk_user_author = $user->id;
$ecmfile->fk_user_modif = $user->id;
$ecmfile->datec = dol_now();
$ecmfile->tms = dol_now();
$ecmfile->mimetype = 'application/pdf';
$ecmfile->filesize = filesize($dest_path);
$ecmfile->checksum = md5_file($dest_path);
$result = $ecmfile->create($user);
if ($result > 0) {
// Try to link document to declaration (this might fail if table doesn't exist)
$link_result = $this->linkDocumentToDeclaration($declaration_id, $ecmfile->id);
if (!$link_result) {
// Log the error but don't fail the whole process
error_log("DeclarationTVA: Failed to link document to declaration: " . $this->error);
}
} else {
// Log the error but don't fail the whole process
error_log("DeclarationTVA: Failed to create ECM file record: " . $ecmfile->error);
}
} catch (Exception $e) {
// Log the error but don't fail the whole process
error_log("DeclarationTVA: Failed to link document to declaration: " . $this->error);
error_log("DeclarationTVA: Exception creating ECM file: " . $e->getMessage());
}
return true;
} else {
$this->error = "Failed to create ECM file record: " . $ecmfile->error;
return false;
// Log that ECM files class is not available
error_log("DeclarationTVA: ECM files class not found at: " . $ecmfiles_path);
}
// Return true even if ECM file creation failed - the PDF is still saved to disk
return true;
} catch (Exception $e) {
$this->error = "Exception in saveValidatedPDF: " . $e->getMessage();
return false;