Add PDF deletion when unvalidating declarations

- Add deleteValidatedPDF method to remove PDF files from disk
- Delete PDFs when unvalidating declarations (status reset to draft)
- Delete PDFs when resetting declaration status for testing
- Search for PDFs by declaration ID and declaration number
- Clean up both validated and temporary PDF directories
- Prevent stale PDFs from being shown after re-validation
This commit is contained in:
Frank Cools 2025-10-09 11:52:43 +02:00
parent a503ed6bf6
commit b6e9075ece

View File

@ -1606,6 +1606,9 @@ class DeclarationTVA
*/
public function resetDeclarationStatus($declaration_id)
{
// Delete any existing PDF files before resetting status
$this->deleteValidatedPDF($declaration_id);
$sql = "UPDATE " . MAIN_DB_PREFIX . "declarationtva_declarations
SET status = 'validated'
WHERE rowid = " . (int)$declaration_id . " AND entity = " . $this->entity;
@ -1854,6 +1857,9 @@ class DeclarationTVA
*/
public function unvalidateDeclaration($declaration_id)
{
// Delete validated PDF files before unvalidating
$this->deleteValidatedPDF($declaration_id);
// Check if the validated_date and validated_by columns exist
$sql_check_columns = "SHOW COLUMNS FROM " . MAIN_DB_PREFIX . "declarationtva_declarations LIKE 'validated_date'";
$result_check = $this->db->query($sql_check_columns);
@ -2111,6 +2117,77 @@ class DeclarationTVA
return false;
}
/**
* Delete validated PDF files for a declaration
*
* @param int $declaration_id Declaration ID
* @return bool Success
*/
public function deleteValidatedPDF($declaration_id)
{
// Get declaration details
$sql = "SELECT declaration_number 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) {
return false;
}
$obj = $this->db->fetch_object($result);
$declaration_number = $obj->declaration_number;
// Check if PDF file exists in the VAT declarations validated directory
$vat_declarations_dir = DOL_DATA_ROOT . '/declarationtva/validated/';
// Look for PDF files with the declaration ID
$pattern = $vat_declarations_dir . 'CA3_' . $declaration_id . '_*.pdf';
$files = glob($pattern);
// If no files found with the exact pattern, try a more flexible search
if (empty($files)) {
$pattern = $vat_declarations_dir . 'CA3_*' . $declaration_id . '*.pdf';
$files = glob($pattern);
}
// Also search by declaration number
if (empty($files)) {
$pattern = $vat_declarations_dir . 'CA3_' . $declaration_number . '_*.pdf';
$files = glob($pattern);
}
$deleted_count = 0;
foreach ($files as $file) {
if (file_exists($file)) {
if (unlink($file)) {
$deleted_count++;
error_log("DEBUG: Deleted validated PDF: " . $file);
} else {
error_log("DEBUG: Failed to delete PDF: " . $file);
}
}
}
// Also check and delete from temporary directory
$temp_dir = DOL_DATA_ROOT . '/declarationtva/';
$temp_pattern = $temp_dir . 'CA3_' . $declaration_id . '_*.pdf';
$temp_files = glob($temp_pattern);
foreach ($temp_files as $file) {
if (file_exists($file)) {
if (unlink($file)) {
$deleted_count++;
error_log("DEBUG: Deleted temporary PDF: " . $file);
} else {
error_log("DEBUG: Failed to delete temporary PDF: " . $file);
}
}
}
error_log("DEBUG: Deleted " . $deleted_count . " PDF files for declaration " . $declaration_id);
return true;
}
/**
* Get the VAT declarations documents directory path
*