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
This commit is contained in:
Frank Cools 2025-10-02 21:16:03 +02:00
parent 4fb5b26960
commit a7bb88c9b1

View File

@ -232,41 +232,23 @@ class DeclarationTVA
*/ */
public function getAccountAmounts($account_code, $start_date, $end_date) 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( $possible_queries = array(
// Try accounting_line table (most likely) // 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_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)
"SELECT SUM(debit) as total_debit, SUM(credit) as total_credit "SELECT SUM(debit) as total_debit, SUM(credit) as total_credit
FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping 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($start_date) . "'
AND doc_date <= '" . $this->db->escape($end_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) { foreach ($possible_queries as $sql) {
@ -291,60 +273,6 @@ class DeclarationTVA
return array('base_amount' => 0, 'vat_amount' => 0, 'total_amount' => 0); 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 * Create CA-3 line record