From a7bb88c9b1309d33bd05156f9af91e1b4e78b9b4 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Thu, 2 Oct 2025 21:16:03 +0200 Subject: [PATCH] Fix accounting queries with correct Dolibarr table structure Fixed: - Use correct table: llx_accounting_bookkeeping - Use correct column: numero_compte (not account_number) - Use correct date column: doc_date - Removed unnecessary table discovery - Queries now match actual Dolibarr database structure --- core/class/declarationtva.class.php | 94 ++++------------------------- 1 file changed, 11 insertions(+), 83 deletions(-) diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index cc3b122..3cd0269 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -232,41 +232,23 @@ class DeclarationTVA */ public function getAccountAmounts($account_code, $start_date, $end_date) { - // First, let's discover what accounting tables exist - $this->discoverAccountingTables(); - // Try different possible table and column names for Dolibarr accounting + // Use the correct Dolibarr accounting table structure $possible_queries = array( - // Try accounting_line table (most likely) - "SELECT SUM(debit) as total_debit, SUM(credit) as total_credit - FROM " . MAIN_DB_PREFIX . "accounting_line - WHERE account_number = '" . $this->db->escape($account_code) . "' - AND doc_date >= '" . $this->db->escape($start_date) . "' - AND doc_date <= '" . $this->db->escape($end_date) . "' - AND entity = " . $this->entity, - - // Try accounting_line with different column name - "SELECT SUM(debit) as total_debit, SUM(credit) as total_credit - FROM " . MAIN_DB_PREFIX . "accounting_line - WHERE account = '" . $this->db->escape($account_code) . "' - AND doc_date >= '" . $this->db->escape($start_date) . "' - AND doc_date <= '" . $this->db->escape($end_date) . "' - AND entity = " . $this->entity, - - // Try accounting_line without entity filter - "SELECT SUM(debit) as total_debit, SUM(credit) as total_credit - FROM " . MAIN_DB_PREFIX . "accounting_line - WHERE account_number = '" . $this->db->escape($account_code) . "' - AND doc_date >= '" . $this->db->escape($start_date) . "' - AND doc_date <= '" . $this->db->escape($end_date) . "'", - - // Try accounting_bookkeeping (if it exists) + // Correct table and column names based on your Dolibarr structure "SELECT SUM(debit) as total_debit, SUM(credit) as total_credit FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping - WHERE account_number = '" . $this->db->escape($account_code) . "' + WHERE numero_compte = '" . $this->db->escape($account_code) . "' AND doc_date >= '" . $this->db->escape($start_date) . "' AND doc_date <= '" . $this->db->escape($end_date) . "' - AND entity = " . $this->entity + AND entity = " . $this->entity, + + // Without entity filter (in case entity is not set correctly) + "SELECT SUM(debit) as total_debit, SUM(credit) as total_credit + FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping + WHERE numero_compte = '" . $this->db->escape($account_code) . "' + AND doc_date >= '" . $this->db->escape($start_date) . "' + AND doc_date <= '" . $this->db->escape($end_date) . "'" ); foreach ($possible_queries as $sql) { @@ -291,60 +273,6 @@ class DeclarationTVA return array('base_amount' => 0, 'vat_amount' => 0, 'total_amount' => 0); } - /** - * Discover accounting tables in Dolibarr - */ - private function discoverAccountingTables() - { - // Get all tables that might contain accounting data - $sql = "SHOW TABLES LIKE '" . MAIN_DB_PREFIX . "accounting%'"; - $result = $this->db->query($sql); - - if ($result) { - error_log("DeclarationTVA: Found accounting tables:"); - while ($obj = $this->db->fetch_object($result)) { - $table_name = array_values((array)$obj)[0]; - error_log("DeclarationTVA: - $table_name"); - - // Get table structure - $desc_sql = "DESCRIBE $table_name"; - $desc_result = $this->db->query($desc_sql); - if ($desc_result) { - error_log("DeclarationTVA: Columns in $table_name:"); - while ($col = $this->db->fetch_object($desc_result)) { - error_log("DeclarationTVA: - " . $col->Field . " (" . $col->Type . ")"); - } - } - } - } - - // Also check for other possible accounting-related tables - $other_tables = array( - MAIN_DB_PREFIX . "accounting_account", - MAIN_DB_PREFIX . "accounting_journal", - MAIN_DB_PREFIX . "accounting_line", - MAIN_DB_PREFIX . "accounting_bookkeeping", - MAIN_DB_PREFIX . "accounting_bookkeeping_tmp" - ); - - foreach ($other_tables as $table) { - $check_sql = "SHOW TABLES LIKE '$table'"; - $check_result = $this->db->query($check_sql); - if ($check_result && $this->db->num_rows($check_result) > 0) { - error_log("DeclarationTVA: Found table: $table"); - - // Get table structure - $desc_sql = "DESCRIBE $table"; - $desc_result = $this->db->query($desc_sql); - if ($desc_result) { - error_log("DeclarationTVA: Columns in $table:"); - while ($col = $this->db->fetch_object($desc_result)) { - error_log("DeclarationTVA: - " . $col->Field . " (" . $col->Type . ")"); - } - } - } - } - } /** * Create CA-3 line record