From c3166b0dcc229911563ba365a2ba4d6a290d0815 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Thu, 2 Oct 2025 20:00:31 +0200 Subject: [PATCH] Add delete functionality for draft declarations Delete Feature: - Added deleteDeclaration() method to DeclarationTVA class - Only allows deletion of draft declarations (safety check) - Deletes CA-3 lines first, then declaration record - Added delete button to main interface for draft declarations - Added confirmation dialog before deletion - Added success/error messages for delete operations - Added translations in both English and French Security: - Only draft declarations can be deleted - Confirmation dialog prevents accidental deletion - Proper error handling and user feedback The interface now shows a delete button for each draft declaration! --- core/class/declarationtva.class.php | 39 +++++++++++++++++++++++++++++ declarationtvaindex.php | 7 ++++++ langs/en_US/declarationtva.lang | 4 +++ langs/fr_FR/declarationtva.lang | 4 +++ 4 files changed, 54 insertions(+) diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index dca1478..244e6de 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -456,4 +456,43 @@ class DeclarationTVA $result = $this->db->query($sql); return $result !== false; } + + /** + * Delete a declaration + * + * @param int $declaration_id Declaration ID + * @return bool Success + */ + public function deleteDeclaration($declaration_id) + { + $this->db->begin(); + + // Check if declaration exists and is in draft status + $sql = "SELECT status FROM " . MAIN_DB_PREFIX . "declarationtva_declarations + WHERE rowid = " . (int)$declaration_id . " AND entity = " . $this->entity; + + $result = $this->db->query($sql); + if ($result) { + $obj = $this->db->fetch_object($result); + if ($obj && $obj->status == 'draft') { + // Delete CA-3 lines first + $sql_lines = "DELETE FROM " . MAIN_DB_PREFIX . "declarationtva_ca3_lines + WHERE declaration_id = " . (int)$declaration_id; + $this->db->query($sql_lines); + + // Delete declaration + $sql_declaration = "DELETE FROM " . MAIN_DB_PREFIX . "declarationtva_declarations + WHERE rowid = " . (int)$declaration_id . " AND entity = " . $this->entity; + + $result = $this->db->query($sql_declaration); + if ($result) { + $this->db->commit(); + return true; + } + } + } + + $this->db->rollback(); + return false; + } } diff --git a/declarationtvaindex.php b/declarationtvaindex.php index c33b360..4603ae0 100644 --- a/declarationtvaindex.php +++ b/declarationtvaindex.php @@ -61,6 +61,12 @@ if ($action == 'create_declaration' && $period_id > 0) { } else { setEventMessages($langs->trans("ErrorSubmittingDeclaration"), null, 'errors'); } +} elseif ($action == 'delete_declaration' && $declaration_id > 0) { + if ($declarationtva->deleteDeclaration($declaration_id)) { + setEventMessages($langs->trans("DeclarationDeleted"), null, 'mesgs'); + } else { + setEventMessages($langs->trans("ErrorDeletingDeclaration"), null, 'errors'); + } } // Get data for display @@ -129,6 +135,7 @@ if (empty($declarations)) { if ($d['status'] == 'draft') { print '' . $langs->trans("Validate") . ''; + print '' . $langs->trans("Delete") . ''; } elseif ($d['status'] == 'validated') { print '' . $langs->trans("Submit") . ''; } diff --git a/langs/en_US/declarationtva.lang b/langs/en_US/declarationtva.lang index db5987a..d361d43 100644 --- a/langs/en_US/declarationtva.lang +++ b/langs/en_US/declarationtva.lang @@ -402,7 +402,11 @@ DeclarationSubmitted = Declaration submitted successfully ErrorCreatingDeclaration = Error creating declaration ErrorValidatingDeclaration = Error validating declaration ErrorSubmittingDeclaration = Error submitting declaration +ErrorDeletingDeclaration = Error deleting declaration ErrorMissingDates = Please provide both start and end dates +DeclarationDeleted = Declaration deleted successfully +Delete = Delete +ConfirmDeleteDeclaration = Are you sure you want to delete this declaration? This action cannot be undone. # Create Declaration Page DeclarationDetails = Declaration Details diff --git a/langs/fr_FR/declarationtva.lang b/langs/fr_FR/declarationtva.lang index 5b93839..ee6c395 100644 --- a/langs/fr_FR/declarationtva.lang +++ b/langs/fr_FR/declarationtva.lang @@ -391,7 +391,11 @@ DeclarationSubmitted = Déclaration soumise avec succès ErrorCreatingDeclaration = Erreur lors de la création de la déclaration ErrorValidatingDeclaration = Erreur lors de la validation de la déclaration ErrorSubmittingDeclaration = Erreur lors de la soumission de la déclaration +ErrorDeletingDeclaration = Erreur lors de la suppression de la déclaration ErrorMissingDates = Veuillez fournir les dates de début et de fin +DeclarationDeleted = Déclaration supprimée avec succès +Delete = Supprimer +ConfirmDeleteDeclaration = Êtes-vous sûr de vouloir supprimer cette déclaration ? Cette action ne peut pas être annulée. # Create Declaration Page DeclarationDetails = Détails de la déclaration