diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index e4551ae..2e821b4 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -1222,41 +1222,35 @@ class DeclarationTVA return true; // No entries to save } - // Use Dolibarr's bookkeeping class - require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php'; - - $bookkeeping = new Bookkeeping($this->db); - + // Use direct SQL insert instead of Bookkeeping class foreach ($entries as $entry) { error_log("DEBUG: Creating accounting entry for account: " . $entry['account_code'] . ", debit: " . $entry['debit'] . ", credit: " . $entry['credit']); - // Create accounting entry - $bookkeeping->doc_type = 'declarationtva'; - $bookkeeping->doc_ref = $declaration->declaration_name; - $bookkeeping->doc_date = $declaration->end_date; // Use declaration end date for OD entries - $bookkeeping->account_number = $entry['account_code']; - $bookkeeping->account_label = $entry['account_label']; - $bookkeeping->debit = $entry['debit']; - $bookkeeping->credit = $entry['credit']; - $bookkeeping->label = $entry['entry_label']; - $bookkeeping->entity = $this->entity; + // Prepare debit and credit amounts + $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())"; + + 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) { - $this->error = 'Failed to create accounting entry: ' . $bookkeeping->error; + $this->error = 'Failed to create accounting entry: ' . $this->db->lasterror(); 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