Fix accounting entries not being saved - use direct SQL insert

- Replace Bookkeeping::create() with direct SQL INSERT
- Bookkeeping class was returning success but not actually saving records
- Add detailed SQL logging to track insert operations
- Ensure entries are actually committed to database
This commit is contained in:
Frank Cools 2025-10-08 22:04:41 +02:00
parent b7d6f50d27
commit 90882addee

View File

@ -1222,41 +1222,35 @@ class DeclarationTVA
return true; // No entries to save return true; // No entries to save
} }
// Use Dolibarr's bookkeeping class // Use direct SQL insert instead of Bookkeeping class
require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
$bookkeeping = new Bookkeeping($this->db);
foreach ($entries as $entry) { foreach ($entries as $entry) {
error_log("DEBUG: Creating accounting entry for account: " . $entry['account_code'] . ", debit: " . $entry['debit'] . ", credit: " . $entry['credit']); error_log("DEBUG: Creating accounting entry for account: " . $entry['account_code'] . ", debit: " . $entry['debit'] . ", credit: " . $entry['credit']);
// Create accounting entry // Prepare debit and credit amounts
$bookkeeping->doc_type = 'declarationtva'; $debit_amount = !empty($entry['debit']) ? (float)$entry['debit'] : 0;
$bookkeeping->doc_ref = $declaration->declaration_name; $credit_amount = !empty($entry['credit']) ? (float)$entry['credit'] : 0;
$bookkeeping->doc_date = $declaration->end_date; // Use declaration end date for OD entries
$bookkeeping->account_number = $entry['account_code']; // Insert accounting entry directly
$bookkeeping->account_label = $entry['account_label']; $sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_bookkeeping
$bookkeeping->debit = $entry['debit']; (doc_type, doc_ref, doc_date, account_number, account_label, debit, credit, label, entity, date_creation, tms)
$bookkeeping->credit = $entry['credit']; VALUES ('declarationtva', '" . $this->db->escape($declaration->declaration_name) . "',
$bookkeeping->label = $entry['entry_label']; '" . $this->db->escape($declaration->end_date) . "',
$bookkeeping->entity = $this->entity; '" . $this->db->escape($entry['account_code']) . "',
'" . $this->db->escape($entry['account_label']) . "',
" . $debit_amount . ",
" . $credit_amount . ",
'" . $this->db->escape($entry['entry_label']) . "',
" . $this->entity . ",
NOW(), NOW())";
error_log("DEBUG: SQL insert: " . $sql);
$result = $this->db->query($sql);
error_log("DEBUG: Direct SQL insert result: " . ($result ? "SUCCESS" : "FAILED - " . $this->db->lasterror()));
$result = $bookkeeping->create($user);
error_log("DEBUG: Bookkeeping create result: " . ($result ? "SUCCESS" : "FAILED - " . $bookkeeping->error));
if (!$result) { if (!$result) {
$this->error = 'Failed to create accounting entry: ' . $bookkeeping->error; $this->error = 'Failed to create accounting entry: ' . $this->db->lasterror();
return false; return false;
} }
// Check if entry was actually created by querying the database
$check_sql = "SELECT COUNT(*) as count FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping
WHERE doc_type = 'declarationtva' AND doc_ref = '" . $this->db->escape($declaration->declaration_name) . "'
AND account_number = '" . $this->db->escape($entry['account_code']) . "'";
$check_result = $this->db->query($check_sql);
if ($check_result && $this->db->num_rows($check_result) > 0) {
$check_obj = $this->db->fetch_object($check_result);
error_log("DEBUG: Entry verification - Found " . $check_obj->count . " entries in database for account " . $entry['account_code']);
}
} }
// Final verification - check all entries created for this declaration // Final verification - check all entries created for this declaration