diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 4272998..70d211b 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -232,24 +232,59 @@ class DeclarationTVA */ public function getAccountAmounts($account_code, $start_date, $end_date) { - // Query Dolibarr accounting entries - $sql = "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) . "'"; - - $result = $this->db->query($sql); - if ($result && $this->db->num_rows($result) > 0) { - $obj = $this->db->fetch_object($result); - $total_amount = $obj->total_debit - $obj->total_credit; + // Try different possible table and column names for Dolibarr accounting + $possible_queries = array( + // Standard Dolibarr accounting table + "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 entity = " . $this->entity, - return array( - 'base_amount' => $total_amount, - 'vat_amount' => $total_amount, - 'total_amount' => $total_amount - ); + // Alternative table 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 + 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 + "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) . "'" + ); + + foreach ($possible_queries as $sql) { + $result = $this->db->query($sql); + if ($result && $this->db->num_rows($result) > 0) { + $obj = $this->db->fetch_object($result); + $total_amount = $obj->total_debit - $obj->total_credit; + + // Log successful query for debugging + error_log("DeclarationTVA: Found data with query: " . substr($sql, 0, 100) . "... Amount: $total_amount"); + + return array( + 'base_amount' => $total_amount, + 'vat_amount' => $total_amount, + 'total_amount' => $total_amount + ); + } } + + // If no data found, log the account code for debugging + error_log("DeclarationTVA: No data found for account $account_code in any accounting table"); return array('base_amount' => 0, 'vat_amount' => 0, 'total_amount' => 0); }