From a1cb48c8cf6a1602fd2cbe0b5a855f38e961233b Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Wed, 8 Oct 2025 22:06:49 +0200 Subject: [PATCH] Fix Bookkeeping class field mapping - Use correct field names for Bookkeeping class - Set all required fields: doc_date, doc_ref, code_journal, numero_compte, etc. - Use proper debit/credit logic with sens field - Set journal code to 'OD' for VAT declarations - Link entries to declaration via fk_doc - Update verification query to use correct field names --- core/class/declarationtva.class.php | 54 +++++++++++++++++++---------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 2e821b4..7807548 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -1222,40 +1222,56 @@ class DeclarationTVA return true; // No entries to save } - // Use direct SQL insert instead of Bookkeeping class + // Use Dolibarr's bookkeeping class with correct field mapping + require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php'; + + $bookkeeping = new Bookkeeping($this->db); + foreach ($entries as $entry) { error_log("DEBUG: Creating accounting entry for account: " . $entry['account_code'] . ", debit: " . $entry['debit'] . ", credit: " . $entry['credit']); - // Prepare debit and credit amounts + // Prepare amounts and determine debit/credit $debit_amount = !empty($entry['debit']) ? (float)$entry['debit'] : 0; $credit_amount = !empty($entry['credit']) ? (float)$entry['credit'] : 0; - // Insert accounting entry directly - $sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_bookkeeping - (doc_type, doc_ref, doc_date, account_number, account_label, debit, credit, label, entity, date_creation, tms) - VALUES ('declarationtva', '" . $this->db->escape($declaration->declaration_name) . "', - '" . $this->db->escape($declaration->end_date) . "', - '" . $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())"; + // Determine the amount and direction + if ($debit_amount > 0) { + $amount = $debit_amount; + $sens = 'D'; // Debit + } else { + $amount = $credit_amount; + $sens = 'C'; // Credit + } - error_log("DEBUG: SQL insert: " . $sql); - $result = $this->db->query($sql); - error_log("DEBUG: Direct SQL insert result: " . ($result ? "SUCCESS" : "FAILED - " . $this->db->lasterror())); + // Set all required fields for Bookkeeping class + $bookkeeping->doc_date = $declaration->end_date; + $bookkeeping->doc_ref = $declaration->declaration_name; + $bookkeeping->code_journal = 'OD'; // OD journal for VAT declarations + $bookkeeping->numero_compte = $entry['account_code']; + $bookkeeping->label_compte = $entry['account_label']; + $bookkeeping->montant = $amount; + $bookkeeping->sens = $sens; + $bookkeeping->fk_doc = $declaration->rowid; + $bookkeeping->fk_docdet = 0; + $bookkeeping->societe_type = 1; + $bookkeeping->fk_user_author = $user->id; + $bookkeeping->code_tiers = ''; + $bookkeeping->piece_num = ''; + $bookkeeping->import_key = ''; + $bookkeeping->entity = $this->entity; + + $result = $bookkeeping->create($user); + error_log("DEBUG: Bookkeeping create result: " . ($result ? "SUCCESS" : "FAILED - " . $bookkeeping->error)); if (!$result) { - $this->error = 'Failed to create accounting entry: ' . $this->db->lasterror(); + $this->error = 'Failed to create accounting entry: ' . $bookkeeping->error; return false; } } // 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_type = 'declarationtva' AND doc_ref = '" . $this->db->escape($declaration->declaration_name) . "'"; + WHERE doc_ref = '" . $this->db->escape($declaration->declaration_name) . "'"; $final_check_result = $this->db->query($final_check_sql); if ($final_check_result && $this->db->num_rows($final_check_result) > 0) { $final_check_obj = $this->db->fetch_object($final_check_result);