Add comprehensive debugging for accounting data queries

Debug Features:
- Try multiple possible table names (accounting_bookkeeping, accounting_bookkeeping_tmp)
- Try different column names (account_number, account)
- Try with and without entity filter
- Log successful queries and amounts found
- Identify which query works with your Dolibarr setup
This commit is contained in:
Frank Cools 2025-10-02 21:06:20 +02:00
parent ca8b50241b
commit a84729dd8e

View File

@ -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) . "'";
// 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,
$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;
// 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,
return array(
'base_amount' => $total_amount,
'vat_amount' => $total_amount,
'total_amount' => $total_amount
);
// 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);
}