Fix accounting table discovery to use correct Dolibarr tables

Updated:
- Focus on accounting_line table (most likely to exist)
- Added comprehensive table discovery with column structure
- Check for accounting_account, accounting_journal, accounting_line
- Removed dependency on _bookkeeping tables
- Will discover actual table structure in your Dolibarr installation
This commit is contained in:
Frank Cools 2025-10-02 21:10:49 +02:00
parent a84729dd8e
commit 4fb5b26960

View File

@ -232,38 +232,41 @@ 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 // Try different possible table and column names for Dolibarr accounting
$possible_queries = array( $possible_queries = array(
// Standard Dolibarr accounting table // Try accounting_line table (most likely)
"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_line
WHERE account_number = '" . $this->db->escape($account_code) . "' WHERE account_number = '" . $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,
// Alternative table name // Try accounting_line with different column name
"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_tmp 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 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) . "' WHERE account = '" . $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 // 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 account_number = '" . $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
); );
foreach ($possible_queries as $sql) { foreach ($possible_queries as $sql) {
@ -288,6 +291,61 @@ 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
* *