From ef190f4c73ee3820ad5340d3209e7c53fba75e64 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Thu, 9 Oct 2025 01:12:28 +0200 Subject: [PATCH] Add enhanced duplicate prevention and debugging - Add static processing check to prevent multiple calls for same declaration - Add verification after deletion to confirm entries are removed - Add affected_rows count to see how many entries were deleted - Help identify if method is called multiple times or deletion fails --- core/class/declarationtva.class.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 6e54393..a1e8fe1 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -895,6 +895,16 @@ class DeclarationTVA */ public function createAccountingEntries($declaration_id, $user) { + static $processing = array(); + + // Check if this declaration is already being processed + if (isset($processing[$declaration_id])) { + error_log("DEBUG: Declaration " . $declaration_id . " is already being processed, skipping duplicate call"); + return true; + } + + $processing[$declaration_id] = true; + error_log("DEBUG: Starting processing for declaration " . $declaration_id); // Get declaration data $fetch_result = $this->fetch($declaration_id); @@ -920,10 +930,21 @@ class DeclarationTVA AND entity = " . $this->entity; $delete_result = $this->db->query($delete_sql); if ($delete_result) { - error_log("DEBUG: Successfully deleted " . $existing_obj->count . " existing entries"); + $deleted_count = $this->db->affected_rows($delete_result); + error_log("DEBUG: Successfully deleted " . $deleted_count . " existing entries"); } else { error_log("DEBUG: Failed to delete existing entries: " . $this->db->lasterror()); } + + // Verify deletion + $verify_sql = "SELECT COUNT(*) as count FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping + WHERE doc_ref = '" . $this->db->escape($this->declaration_name) . "' + AND entity = " . $this->entity; + $verify_result = $this->db->query($verify_sql); + if ($verify_result && $this->db->num_rows($verify_result) > 0) { + $verify_obj = $this->db->fetch_object($verify_result); + error_log("DEBUG: After deletion, " . $verify_obj->count . " entries still exist"); + } } }