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
This commit is contained in:
Frank Cools 2025-10-08 22:27:30 +02:00
parent 08aa825fe8
commit a678348f42

View File

@ -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;