From c400608a4006205c000c827c6b766d8a23213dcf Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Wed, 8 Oct 2025 22:23:00 +0200 Subject: [PATCH] Add proper database transaction handling - Start transaction with db->begin() before creating entries - Commit transaction with db->commit() after all entries created - Rollback transaction with db->rollback() on any failure - Follow Dolibarr best practices for accounting entry creation - Ensure entries are properly committed to database --- core/class/declarationtva.class.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 985ce51..1d3078b 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -1225,6 +1225,10 @@ class DeclarationTVA // Use Dolibarr's bookkeeping class with correct field mapping require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php'; + // Start transaction + $this->db->begin(); + error_log("DEBUG: Database transaction started"); + foreach ($entries as $entry) { error_log("DEBUG: Creating accounting entry for account: " . $entry['account_code'] . ", debit: " . $entry['debit'] . ", credit: " . $entry['credit']); @@ -1295,6 +1299,7 @@ class DeclarationTVA error_log("DEBUG: Debit entry created successfully with ID: " . $result); } else { error_log("DEBUG: Debit entry creation failed - result: " . $result . ", error: " . $debit->error); + $this->db->rollback(); $this->error = 'Failed to create debit entry: ' . $debit->error; return false; } @@ -1332,12 +1337,17 @@ class DeclarationTVA error_log("DEBUG: Credit entry created successfully with ID: " . $result); } else { error_log("DEBUG: Credit entry creation failed - result: " . $result . ", error: " . $credit->error); + $this->db->rollback(); $this->error = 'Failed to create credit entry: ' . $credit->error; return false; } } } + // Commit the transaction + $this->db->commit(); + error_log("DEBUG: Database transaction committed"); + // Final verification - check all entries created for this declaration $final_check_sql = "SELECT COUNT(*) as total_entries FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE doc_ref = '" . $this->db->escape($declaration->declaration_name) . "'";