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)
{
$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",
// Debug logging
error_log("DeclarationTVA: Looking for account " . $account_code . " with entity " . $this->entity);
error_log("DeclarationTVA: SQL query: " . $sql);
// Without entity filter
"SELECT label FROM " . MAIN_DB_PREFIX . "accounting_account
WHERE account_number = '" . $this->db->escape($account_code) . "'
AND active = 1
LIMIT 1",
$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;
// Without active filter
"SELECT label FROM " . MAIN_DB_PREFIX . "accounting_account
WHERE account_number = '" . $this->db->escape($account_code) . "'
LIMIT 1"
);
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;
}