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
This commit is contained in:
Frank Cools 2025-10-08 22:06:49 +02:00
parent 90882addee
commit a1cb48c8cf

View File

@ -1222,40 +1222,56 @@ class DeclarationTVA
return true; // No entries to save 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) { 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']);
// Prepare debit and credit amounts // Prepare amounts and determine debit/credit
$debit_amount = !empty($entry['debit']) ? (float)$entry['debit'] : 0; $debit_amount = !empty($entry['debit']) ? (float)$entry['debit'] : 0;
$credit_amount = !empty($entry['credit']) ? (float)$entry['credit'] : 0; $credit_amount = !empty($entry['credit']) ? (float)$entry['credit'] : 0;
// Insert accounting entry directly // Determine the amount and direction
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_bookkeeping if ($debit_amount > 0) {
(doc_type, doc_ref, doc_date, account_number, account_label, debit, credit, label, entity, date_creation, tms) $amount = $debit_amount;
VALUES ('declarationtva', '" . $this->db->escape($declaration->declaration_name) . "', $sens = 'D'; // Debit
'" . $this->db->escape($declaration->end_date) . "', } else {
'" . $this->db->escape($entry['account_code']) . "', $amount = $credit_amount;
'" . $this->db->escape($entry['account_label']) . "', $sens = 'C'; // Credit
" . $debit_amount . ", }
" . $credit_amount . ",
'" . $this->db->escape($entry['entry_label']) . "',
" . $this->entity . ",
NOW(), NOW())";
error_log("DEBUG: SQL insert: " . $sql); // Set all required fields for Bookkeeping class
$result = $this->db->query($sql); $bookkeeping->doc_date = $declaration->end_date;
error_log("DEBUG: Direct SQL insert result: " . ($result ? "SUCCESS" : "FAILED - " . $this->db->lasterror())); $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) { if (!$result) {
$this->error = 'Failed to create accounting entry: ' . $this->db->lasterror(); $this->error = 'Failed to create accounting entry: ' . $bookkeeping->error;
return false; return false;
} }
} }
// Final verification - check all entries created for this declaration // Final verification - check all entries created for this declaration
$final_check_sql = "SELECT COUNT(*) as total_entries FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping $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); $final_check_result = $this->db->query($final_check_sql);
if ($final_check_result && $this->db->num_rows($final_check_result) > 0) { if ($final_check_result && $this->db->num_rows($final_check_result) > 0) {
$final_check_obj = $this->db->fetch_object($final_check_result); $final_check_obj = $this->db->fetch_object($final_check_result);