diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 931e095..c8846c1 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -1239,25 +1239,69 @@ 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); + // Get declaration details + $sql = "SELECT declaration_number, status FROM " . MAIN_DB_PREFIX . "declarationtva_declarations + WHERE rowid = " . (int)$declaration_id . " AND entity = " . $this->entity; - if (!$result_check || $this->db->num_rows($result_check) == 0) { - // Table doesn't exist, so no documents + $result = $this->db->query($sql); + if (!$result || $this->db->num_rows($result) == 0) { 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 - WHERE dd.declaration_id = " . (int)$declaration_id . " - AND dd.entity = " . $this->entity; + $obj = $this->db->fetch_object($result); + + // Only check for documents if declaration is validated + if ($obj->status != 'validated') { + return false; + } + + // Check if PDF file exists in the VAT declarations directory + $vat_declarations_dir = DOL_DATA_ROOT . '/documents/declarationtva/'; + $year_dir = $vat_declarations_dir . date('Y') . '/'; + $month_dir = $year_dir . date('m') . '/'; + + // Look for PDF files with the declaration number + $pattern = $month_dir . 'CA3_' . $obj->declaration_number . '_*.pdf'; + $files = glob($pattern); + + return !empty($files) && file_exists($files[0]); + } + + /** + * Get the PDF file path for a validated declaration + * + * @param int $declaration_id Declaration ID + * @return string|false PDF file path or false if not found + */ + public function getValidatedPDFPath($declaration_id) + { + // Get declaration details + $sql = "SELECT declaration_number, status FROM " . MAIN_DB_PREFIX . "declarationtva_declarations + WHERE rowid = " . (int)$declaration_id . " AND entity = " . $this->entity; $result = $this->db->query($sql); - if ($result && $this->db->num_rows($result) > 0) { - $obj = $this->db->fetch_object($result); - return $obj->count > 0; + if (!$result || $this->db->num_rows($result) == 0) { + return false; + } + + $obj = $this->db->fetch_object($result); + + // Only return path if declaration is validated + if ($obj->status != 'validated') { + return false; + } + + // Check if PDF file exists in the VAT declarations directory + $vat_declarations_dir = DOL_DATA_ROOT . '/documents/declarationtva/'; + $year_dir = $vat_declarations_dir . date('Y') . '/'; + $month_dir = $year_dir . date('m') . '/'; + + // Look for PDF files with the declaration number + $pattern = $month_dir . 'CA3_' . $obj->declaration_number . '_*.pdf'; + $files = glob($pattern); + + if (!empty($files) && file_exists($files[0])) { + return $files[0]; } return false; diff --git a/declarationtvaindex.php b/declarationtvaindex.php index d67d56b..cf14a0c 100644 --- a/declarationtvaindex.php +++ b/declarationtvaindex.php @@ -149,7 +149,17 @@ if (empty($declarations)) { // Check if document exists $has_document = $declarationtva->hasValidatedDocument($d['rowid']); if ($has_document) { - print ''; + $pdf_path = $declarationtva->getValidatedPDFPath($d['rowid']); + if ($pdf_path) { + // Create a download link + $relative_path = str_replace(DOL_DATA_ROOT, '', $pdf_path); + $download_url = DOL_URL_ROOT . '/documents' . $relative_path; + print ''; + print ''; + print ''; + } else { + print ''; + } } else { print ''; } diff --git a/langs/en_US/declarationtva.lang b/langs/en_US/declarationtva.lang index ed7191b..c393e2d 100644 --- a/langs/en_US/declarationtva.lang +++ b/langs/en_US/declarationtva.lang @@ -512,3 +512,4 @@ ValidatedDeclaration = Validated Declaration DraftDeclaration = Draft Declaration ValidatedPDFAvailable = Validated PDF Available NoDocument = No Document +DownloadPDF = Download PDF diff --git a/langs/fr_FR/declarationtva.lang b/langs/fr_FR/declarationtva.lang index 44c4be9..d4dfb02 100644 --- a/langs/fr_FR/declarationtva.lang +++ b/langs/fr_FR/declarationtva.lang @@ -484,3 +484,4 @@ ValidatedDeclaration = Déclaration validée DraftDeclaration = Déclaration brouillon ValidatedPDFAvailable = PDF validé disponible NoDocument = Aucun document +DownloadPDF = Télécharger le PDF