diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 4ef352f..5fdfae2 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -999,6 +999,7 @@ class DeclarationTVA */ public function validateDeclaration($declaration_id) { + // First, try to update the declaration status $sql = "UPDATE " . MAIN_DB_PREFIX . "declarationtva_declarations SET status = 'validated', validated_date = NOW(), @@ -1007,12 +1008,18 @@ class DeclarationTVA AND entity = " . $this->entity; $result = $this->db->query($sql); - if ($result) { - return true; - } else { - $this->error = $this->db->lasterror(); + if (!$result) { + $this->error = "Database error updating declaration: " . $this->db->lasterror(); return false; } + + // Check if the update actually affected any rows + if ($this->db->affected_rows($result) == 0) { + $this->error = "No declaration found with ID " . $declaration_id . " or insufficient permissions"; + return false; + } + + return true; } /** @@ -1026,18 +1033,26 @@ class DeclarationTVA { global $conf, $user; - // Create documents directory if it doesn't exist - $doc_dir = DOL_DATA_ROOT . '/declarationtva/validated/'; - if (!is_dir($doc_dir)) { - dol_mkdir($doc_dir); - } - - // Generate filename - $filename = 'CA3_Validated_' . $this->declaration_number . '_' . date('Y-m-d') . '.pdf'; - $dest_path = $doc_dir . $filename; - - // Copy PDF to documents directory - if (copy($pdf_path, $dest_path)) { + try { + // Create documents directory if it doesn't exist + $doc_dir = DOL_DATA_ROOT . '/declarationtva/validated/'; + if (!is_dir($doc_dir)) { + if (!dol_mkdir($doc_dir)) { + $this->error = "Failed to create documents directory: " . $doc_dir; + return false; + } + } + + // Generate filename + $filename = 'CA3_Validated_' . $this->declaration_number . '_' . date('Y-m-d') . '.pdf'; + $dest_path = $doc_dir . $filename; + + // Copy PDF to documents directory + if (!copy($pdf_path, $dest_path)) { + $this->error = "Failed to copy PDF to documents directory"; + return false; + } + // Add document record to Dolibarr require_once DOL_DOCUMENT_ROOT . '/core/class/ecmfiles.class.php'; $ecmfile = new EcmFiles($this->db); @@ -1057,13 +1072,21 @@ class DeclarationTVA $result = $ecmfile->create($user); if ($result > 0) { - // Link document to declaration - $this->linkDocumentToDeclaration($declaration_id, $ecmfile->id); + // 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); + } return true; + } else { + $this->error = "Failed to create ECM file record: " . $ecmfile->error; + return false; } + } catch (Exception $e) { + $this->error = "Exception in saveValidatedPDF: " . $e->getMessage(); + return false; } - - return false; } /** @@ -1075,11 +1098,26 @@ class DeclarationTVA */ private function linkDocumentToDeclaration($declaration_id, $document_id) { - $sql = "INSERT INTO " . MAIN_DB_PREFIX . "declarationtva_documents - (declaration_id, document_id, created_date) - VALUES (" . (int)$declaration_id . ", " . (int)$document_id . ", NOW())"; + // First check if the table exists + $sql_check = "SHOW TABLES LIKE '" . MAIN_DB_PREFIX . "declarationtva_documents'"; + $result_check = $this->db->query($sql_check); - return $this->db->query($sql); + if (!$result_check || $this->db->num_rows($result_check) == 0) { + $this->error = "Documents table does not exist. Please run the database migration."; + return false; + } + + $sql = "INSERT INTO " . MAIN_DB_PREFIX . "declarationtva_documents + (declaration_id, document_id, created_date, entity) + VALUES (" . (int)$declaration_id . ", " . (int)$document_id . ", NOW(), " . $this->entity . ")"; + + $result = $this->db->query($sql); + if (!$result) { + $this->error = "Database error linking document: " . $this->db->lasterror(); + return false; + } + + return true; } /** @@ -1090,6 +1128,15 @@ class DeclarationTVA */ public function hasValidatedDocument($declaration_id) { + // First check if the table exists + $sql_check = "SHOW TABLES LIKE '" . MAIN_DB_PREFIX . "declarationtva_documents'"; + $result_check = $this->db->query($sql_check); + + if (!$result_check || $this->db->num_rows($result_check) == 0) { + // Table doesn't exist, so no documents + return false; + } + $sql = "SELECT COUNT(*) as count FROM " . MAIN_DB_PREFIX . "declarationtva_documents dd INNER JOIN " . MAIN_DB_PREFIX . "ecm_files ef ON dd.document_id = ef.rowid diff --git a/declarationtva_view.php b/declarationtva_view.php index cbaf062..5504541 100644 --- a/declarationtva_view.php +++ b/declarationtva_view.php @@ -110,10 +110,15 @@ if ($action == 'validate' && $token) { $pdf_path = $pdf_generator->generateDetailedCA3PDF($id); if ($pdf_path && file_exists($pdf_path)) { // Save PDF to Dolibarr documents - $declarationtva->saveValidatedPDF($id, $pdf_path); + $save_result = $declarationtva->saveValidatedPDF($id, $pdf_path); + if (!$save_result) { + setEventMessages("Warning: Declaration validated but PDF save failed: " . $declarationtva->error, null, 'warnings'); + } + } else { + setEventMessages("Warning: Declaration validated but PDF generation failed: " . $pdf_generator->error, null, 'warnings'); } } else { - setEventMessages($langs->trans("ErrorValidatingDeclaration"), null, 'errors'); + setEventMessages($langs->trans("ErrorValidatingDeclaration") . ": " . $declarationtva->error, null, 'errors'); } }