Fix PDF storage to use /declarationtva/validated/ directory

This commit is contained in:
Frank Cools 2025-10-08 14:52:51 +02:00
parent c1d1f122fa
commit e82ec53a37
2 changed files with 21 additions and 61 deletions

View File

@ -1103,12 +1103,10 @@ class DeclarationTVA
global $conf, $user; global $conf, $user;
try { try {
// Create VAT declarations documents directory structure // Create VAT declarations validated directory structure
$vat_declarations_dir = DOL_DATA_ROOT . '/documents/declarationtva/'; $vat_declarations_dir = DOL_DATA_ROOT . '/declarationtva/validated/';
$year_dir = $vat_declarations_dir . date('Y') . '/';
$month_dir = $year_dir . date('m') . '/';
// Create directories if they don't exist // Create directory if it doesn't exist
if (!is_dir($vat_declarations_dir)) { if (!is_dir($vat_declarations_dir)) {
if (!dol_mkdir($vat_declarations_dir)) { if (!dol_mkdir($vat_declarations_dir)) {
$this->error = "Failed to create VAT declarations directory: " . $vat_declarations_dir; $this->error = "Failed to create VAT declarations directory: " . $vat_declarations_dir;
@ -1116,32 +1114,18 @@ class DeclarationTVA
} }
} }
if (!is_dir($year_dir)) {
if (!dol_mkdir($year_dir)) {
$this->error = "Failed to create year directory: " . $year_dir;
return false;
}
}
if (!is_dir($month_dir)) {
if (!dol_mkdir($month_dir)) {
$this->error = "Failed to create month directory: " . $month_dir;
return false;
}
}
// Check if PDF is already in the correct location // Check if PDF is already in the correct location
if (strpos($pdf_path, $month_dir) === 0) { if (strpos($pdf_path, $vat_declarations_dir) === 0) {
// PDF is already in the correct location, no need to copy // PDF is already in the correct location, no need to copy
$dest_path = $pdf_path; $dest_path = $pdf_path;
} else { } else {
// Generate filename with proper structure // Generate filename with proper structure
$filename = 'CA3_' . $this->declaration_number . '_' . date('Y-m-d') . '.pdf'; $filename = 'CA3_' . $this->declaration_number . '_' . date('Y-m-d') . '.pdf';
$dest_path = $month_dir . $filename; $dest_path = $vat_declarations_dir . $filename;
// Copy PDF to documents directory // Copy PDF to validated directory
if (!copy($pdf_path, $dest_path)) { if (!copy($pdf_path, $dest_path)) {
$this->error = "Failed to copy PDF to documents directory"; $this->error = "Failed to copy PDF to validated directory";
return false; return false;
} }
} }
@ -1273,24 +1257,16 @@ class DeclarationTVA
return false; return false;
} }
// Check if PDF file exists in the VAT declarations documents directory // Check if PDF file exists in the VAT declarations validated directory
$vat_declarations_dir = DOL_DATA_ROOT . '/documents/declarationtva/'; $vat_declarations_dir = DOL_DATA_ROOT . '/declarationtva/validated/';
$year_dir = $vat_declarations_dir . date('Y') . '/';
$month_dir = $year_dir . date('m') . '/';
// Look for PDF files with the declaration number in the current month directory // Look for PDF files with the declaration number
$pattern = $month_dir . 'CA3_' . $obj->declaration_number . '_*.pdf'; $pattern = $vat_declarations_dir . 'CA3_' . $obj->declaration_number . '_*.pdf';
$files = glob($pattern); $files = glob($pattern);
// If no files found with the exact pattern, try a more flexible search // If no files found with the exact pattern, try a more flexible search
if (empty($files)) { if (empty($files)) {
$pattern = $month_dir . 'CA3_*' . $obj->declaration_number . '*.pdf'; $pattern = $vat_declarations_dir . 'CA3_*' . $obj->declaration_number . '*.pdf';
$files = glob($pattern);
}
// If still no files found, search in the year directory
if (empty($files)) {
$pattern = $year_dir . 'CA3_' . $obj->declaration_number . '_*.pdf';
$files = glob($pattern); $files = glob($pattern);
} }
@ -1321,24 +1297,16 @@ class DeclarationTVA
return false; return false;
} }
// Check if PDF file exists in the VAT declarations documents directory // Check if PDF file exists in the VAT declarations validated directory
$vat_declarations_dir = DOL_DATA_ROOT . '/documents/declarationtva/'; $vat_declarations_dir = DOL_DATA_ROOT . '/declarationtva/validated/';
$year_dir = $vat_declarations_dir . date('Y') . '/';
$month_dir = $year_dir . date('m') . '/';
// Look for PDF files with the declaration number in the current month directory // Look for PDF files with the declaration number
$pattern = $month_dir . 'CA3_' . $obj->declaration_number . '_*.pdf'; $pattern = $vat_declarations_dir . 'CA3_' . $obj->declaration_number . '_*.pdf';
$files = glob($pattern); $files = glob($pattern);
// If no files found with the exact pattern, try a more flexible search // If no files found with the exact pattern, try a more flexible search
if (empty($files)) { if (empty($files)) {
$pattern = $month_dir . 'CA3_*' . $obj->declaration_number . '*.pdf'; $pattern = $vat_declarations_dir . 'CA3_*' . $obj->declaration_number . '*.pdf';
$files = glob($pattern);
}
// If still no files found, search in the year directory
if (empty($files)) {
$pattern = $year_dir . 'CA3_' . $obj->declaration_number . '_*.pdf';
$files = glob($pattern); $files = glob($pattern);
} }

View File

@ -322,23 +322,15 @@ class DeclarationTVA_PDF
// Generate PDF filename // Generate PDF filename
$filename = 'CA3_' . $declaration_id . '_' . date('Y-m-d') . '.pdf'; $filename = 'CA3_' . $declaration_id . '_' . date('Y-m-d') . '.pdf';
// Create VAT declarations documents directory structure (same as saveValidatedPDF expects) // Create VAT declarations validated directory structure
$vat_declarations_dir = DOL_DATA_ROOT . '/documents/declarationtva/'; $vat_declarations_dir = DOL_DATA_ROOT . '/declarationtva/validated/';
$year_dir = $vat_declarations_dir . date('Y') . '/';
$month_dir = $year_dir . date('m') . '/';
// Create directories if they don't exist // Create directory if it doesn't exist
if (!is_dir($vat_declarations_dir)) { if (!is_dir($vat_declarations_dir)) {
dol_mkdir($vat_declarations_dir); dol_mkdir($vat_declarations_dir);
} }
if (!is_dir($year_dir)) {
dol_mkdir($year_dir);
}
if (!is_dir($month_dir)) {
dol_mkdir($month_dir);
}
$filepath = $month_dir . $filename; $filepath = $vat_declarations_dir . $filename;
// Use the exact same method as export but with the correct output path // Use the exact same method as export but with the correct output path
// First generate the PDF using the same method as export // First generate the PDF using the same method as export