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
This commit is contained in:
Frank Cools 2025-10-07 11:59:43 +02:00
parent 61bc28597b
commit 009ad50d65

View File

@ -2117,23 +2117,39 @@ class DeclarationTVA_PDF
*/ */
private function getAccountLabel($account_code) private function getAccountLabel($account_code)
{ {
$sql = "SELECT label FROM " . MAIN_DB_PREFIX . "accounting_account // Try multiple approaches to find the account
WHERE account_number = '" . $this->db->escape($account_code) . "' $possible_queries = array(
AND active = 1 // With entity filter
LIMIT 1"; "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 foreach ($possible_queries as $sql) {
error_log("DeclarationTVA: Looking for account " . $account_code . " with entity " . $this->entity); error_log("DeclarationTVA: Trying query: " . $sql);
error_log("DeclarationTVA: SQL query: " . $sql);
$result = $this->db->query($sql);
$result = $this->db->query($sql); if ($result && $this->db->num_rows($result) > 0) {
if ($result && $this->db->num_rows($result) > 0) { $obj = $this->db->fetch_object($result);
$obj = $this->db->fetch_object($result); error_log("DeclarationTVA: Found account label: " . $obj->label);
error_log("DeclarationTVA: Found account label: " . $obj->label); return $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 // If account not found in chart of accounts, return generic label
return 'Compte ' . $account_code; return 'Compte ' . $account_code;
} }