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; return false;
} }
// Add document record to Dolibarr // Try to add document record to Dolibarr (optional feature)
require_once DOL_DOCUMENT_ROOT . '/core/class/ecmfiles.class.php'; $ecmfiles_path = DOL_DOCUMENT_ROOT . '/core/class/ecmfiles.class.php';
$ecmfile = new EcmFiles($this->db);
$ecmfile->filepath = 'declarationtva/validated/'; if (file_exists($ecmfiles_path)) {
$ecmfile->filename = $filename; try {
$ecmfile->label = 'CA-3 Validated Declaration - ' . $this->declaration_number; require_once $ecmfiles_path;
$ecmfile->description = 'Validated CA-3 declaration with detailed breakdown'; $ecmfile = new EcmFiles($this->db);
$ecmfile->entity = $this->entity;
$ecmfile->fk_user_author = $user->id; $ecmfile->filepath = 'declarationtva/validated/';
$ecmfile->fk_user_modif = $user->id; $ecmfile->filename = $filename;
$ecmfile->datec = dol_now(); $ecmfile->label = 'CA-3 Validated Declaration - ' . $this->declaration_number;
$ecmfile->tms = dol_now(); $ecmfile->description = 'Validated CA-3 declaration with detailed breakdown';
$ecmfile->mimetype = 'application/pdf'; $ecmfile->entity = $this->entity;
$ecmfile->filesize = filesize($dest_path); $ecmfile->fk_user_author = $user->id;
$ecmfile->checksum = md5_file($dest_path); $ecmfile->fk_user_modif = $user->id;
$ecmfile->datec = dol_now();
$result = $ecmfile->create($user); $ecmfile->tms = dol_now();
if ($result > 0) { $ecmfile->mimetype = 'application/pdf';
// Try to link document to declaration (this might fail if table doesn't exist) $ecmfile->filesize = filesize($dest_path);
$link_result = $this->linkDocumentToDeclaration($declaration_id, $ecmfile->id); $ecmfile->checksum = md5_file($dest_path);
if (!$link_result) {
$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 // 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 { } else {
$this->error = "Failed to create ECM file record: " . $ecmfile->error; // Log that ECM files class is not available
return false; 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) { } catch (Exception $e) {
$this->error = "Exception in saveValidatedPDF: " . $e->getMessage(); $this->error = "Exception in saveValidatedPDF: " . $e->getMessage();
return false; return false;