From a678348f421b59f7562fb9a17ac239a14920946f Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Wed, 8 Oct 2025 22:27:30 +0200 Subject: [PATCH] Add account validation and detailed debugging - Check if account numbers exist in chart of accounts before creating entries - Add detailed debugging for error messages and entry IDs - Validate account existence to prevent creation failures - Show exact error messages from Bookkeeping class --- core/class/declarationtva.class.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 70580c8..2ecd495 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -1239,6 +1239,14 @@ class DeclarationTVA // Create debit entry if amount > 0 if ($debit_amount > 0) { + // Check if account exists + $account_check_sql = "SELECT account_number, label FROM " . MAIN_DB_PREFIX . "accounting_account WHERE account_number = '" . $this->db->escape($entry['account_code']) . "' AND entity = " . $this->entity; + $account_check_result = $this->db->query($account_check_sql); + if (!$account_check_result || $this->db->num_rows($account_check_result) == 0) { + error_log("DEBUG: Account " . $entry['account_code'] . " not found in system"); + throw new Exception('Account ' . $entry['account_code'] . ' not found in chart of accounts'); + } + $debit = new Bookkeeping($this->db); $debit->doc_date = dol_now(); $debit->doc_ref = $declaration->declaration_name; @@ -1252,6 +1260,8 @@ class DeclarationTVA $result = $debit->create($user); error_log("DEBUG: Debit entry create result: " . $result . " (type: " . gettype($result) . ")"); + error_log("DEBUG: Debit entry error message: '" . $debit->error . "'"); + error_log("DEBUG: Debit entry ID after create: " . $debit->id); if ($result <= 0) { throw new Exception('Failed to create debit entry: ' . $debit->error); @@ -1260,6 +1270,14 @@ class DeclarationTVA // Create credit entry if amount > 0 if ($credit_amount > 0) { + // Check if account exists + $account_check_sql = "SELECT account_number, label FROM " . MAIN_DB_PREFIX . "accounting_account WHERE account_number = '" . $this->db->escape($entry['account_code']) . "' AND entity = " . $this->entity; + $account_check_result = $this->db->query($account_check_sql); + if (!$account_check_result || $this->db->num_rows($account_check_result) == 0) { + error_log("DEBUG: Account " . $entry['account_code'] . " not found in system"); + throw new Exception('Account ' . $entry['account_code'] . ' not found in chart of accounts'); + } + $credit = new Bookkeeping($this->db); $credit->doc_date = dol_now(); $credit->doc_ref = $declaration->declaration_name;