From 77027debf50bf53a8a412540ee0f1e41f46ac82e Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Mon, 6 Oct 2025 17:08:59 +0200 Subject: [PATCH] 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 --- core/class/declarationtva.class.php | 64 ++++++++++++++++++----------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 08cff94..d773891 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -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); - - $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) { + if (file_exists($ecmfiles_path)) { + try { + require_once $ecmfiles_path; + $ecmfile = new EcmFiles($this->db); + + $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;