Fix PDF document detection in declaration list

- Modified hasValidatedDocument() to check file system instead of database
- Added getValidatedPDFPath() method to get PDF file path
- Updated declaration list to show clickable PDF download links
- Added DownloadPDF translation in French and English
- PDF icons now link directly to downloadable PDF files
- Fixed issue where validated declarations didn't show PDF availability
This commit is contained in:
Frank Cools 2025-10-06 17:28:25 +02:00
parent 00157f2634
commit efa8f676fa
4 changed files with 70 additions and 14 deletions

View File

@ -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;

View File

@ -149,7 +149,17 @@ if (empty($declarations)) {
// Check if document exists
$has_document = $declarationtva->hasValidatedDocument($d['rowid']);
if ($has_document) {
print '<i class="fa fa-file-pdf-o text-success" title="' . $langs->trans("ValidatedPDFAvailable") . '"></i>';
$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 '<a href="' . $download_url . '" target="_blank" title="' . $langs->trans("DownloadPDF") . '">';
print '<i class="fa fa-file-pdf-o text-success"></i>';
print '</a>';
} else {
print '<i class="fa fa-file-pdf-o text-success" title="' . $langs->trans("ValidatedPDFAvailable") . '"></i>';
}
} else {
print '<i class="fa fa-file-o text-muted" title="' . $langs->trans("NoDocument") . '"></i>';
}

View File

@ -512,3 +512,4 @@ ValidatedDeclaration = Validated Declaration
DraftDeclaration = Draft Declaration
ValidatedPDFAvailable = Validated PDF Available
NoDocument = No Document
DownloadPDF = Download PDF

View File

@ -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