From 009ad50d65660e29552bd7ff6b3ab38bdb910482 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Tue, 7 Oct 2025 11:59:43 +0200 Subject: [PATCH] Improve getAccountLabel with multiple query approaches to find account names - Added multiple query strategies to handle different database configurations - Tries with entity filter, without entity filter, and without active filter - Enhanced debugging to show which query succeeds - Based on successful patterns from other methods in the codebase - Should now properly retrieve account names from Dolibarr chart of accounts --- core/class/declarationtva_pdf.class.php | 44 +++++++++++++++++-------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index 8407ffe..2cd8e57 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -2117,23 +2117,39 @@ class DeclarationTVA_PDF */ private function getAccountLabel($account_code) { - $sql = "SELECT label FROM " . MAIN_DB_PREFIX . "accounting_account - WHERE account_number = '" . $this->db->escape($account_code) . "' - AND active = 1 - LIMIT 1"; + // Try multiple approaches to find the account + $possible_queries = array( + // With entity filter + "SELECT label FROM " . MAIN_DB_PREFIX . "accounting_account + WHERE account_number = '" . $this->db->escape($account_code) . "' + AND entity = " . $this->entity . " + AND active = 1 + LIMIT 1", + + // Without entity filter + "SELECT label FROM " . MAIN_DB_PREFIX . "accounting_account + WHERE account_number = '" . $this->db->escape($account_code) . "' + AND active = 1 + LIMIT 1", + + // Without active filter + "SELECT label FROM " . MAIN_DB_PREFIX . "accounting_account + WHERE account_number = '" . $this->db->escape($account_code) . "' + LIMIT 1" + ); - // Debug logging - error_log("DeclarationTVA: Looking for account " . $account_code . " with entity " . $this->entity); - error_log("DeclarationTVA: SQL query: " . $sql); - - $result = $this->db->query($sql); - if ($result && $this->db->num_rows($result) > 0) { - $obj = $this->db->fetch_object($result); - error_log("DeclarationTVA: Found account label: " . $obj->label); - return $obj->label; + foreach ($possible_queries as $sql) { + error_log("DeclarationTVA: Trying query: " . $sql); + + $result = $this->db->query($sql); + if ($result && $this->db->num_rows($result) > 0) { + $obj = $this->db->fetch_object($result); + error_log("DeclarationTVA: Found account label: " . $obj->label); + return $obj->label; + } } - error_log("DeclarationTVA: Account " . $account_code . " not found, using generic label"); + error_log("DeclarationTVA: Account " . $account_code . " not found in any query, using generic label"); // If account not found in chart of accounts, return generic label return 'Compte ' . $account_code; }