diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 70d211b..cc3b122 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -232,38 +232,41 @@ 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 $possible_queries = array( - // Standard Dolibarr accounting table + // Try accounting_line table (most likely) "SELECT SUM(debit) as total_debit, SUM(credit) as total_credit - FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping + 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, - // Alternative table name + // Try accounting_line with different column name "SELECT SUM(debit) as total_debit, SUM(credit) as total_credit - FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping_tmp - 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, - - // Alternative column name - "SELECT SUM(debit) as total_debit, SUM(credit) as total_credit - FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping + 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, - // Without entity filter + // 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) "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) . "' AND doc_date >= '" . $this->db->escape($start_date) . "' - AND doc_date <= '" . $this->db->escape($end_date) . "'" + AND doc_date <= '" . $this->db->escape($end_date) . "' + AND entity = " . $this->entity ); foreach ($possible_queries as $sql) { @@ -288,6 +291,61 @@ 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 *