Add debugging and SQL fallback for accounting entries

- Add detailed debugging to check what's actually in accounting_bookkeeping table
- Add direct SQL insert fallback when Bookkeeping::create() fails
- Show recent entries in table to debug the issue
- Ensure entries are actually saved to database
This commit is contained in:
Frank Cools 2025-10-08 22:11:17 +02:00
parent 097765bbf5
commit b195156e2b

View File

@ -1255,10 +1255,35 @@ class DeclarationTVA
error_log("DEBUG: Debit entry create result: " . ($result ? "SUCCESS" : "FAILED - " . $debit->error)); error_log("DEBUG: Debit entry create result: " . ($result ? "SUCCESS" : "FAILED - " . $debit->error));
if (!$result) { if (!$result) {
$this->error = 'Failed to create debit entry: ' . $debit->error; // Try direct SQL insert as fallback
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_bookkeeping
(doc_date, doc_ref, code_journal, numero_compte, label_compte, montant, sens, fk_doc, fk_docdet, societe_type, fk_user_author, code_tiers, piece_num, import_key, entity, date_creation, tms)
VALUES ('" . $this->db->escape($declaration->end_date) . "',
'" . $this->db->escape($declaration->declaration_name) . "',
'OD',
'" . $this->db->escape($entry['account_code']) . "',
'" . $this->db->escape($entry['account_label']) . "',
" . $debit_amount . ",
'D',
" . $declaration->rowid . ",
0,
1,
" . $user->id . ",
'',
'',
'',
" . $this->entity . ",
NOW(), NOW())";
$sql_result = $this->db->query($sql);
error_log("DEBUG: Direct SQL debit insert result: " . ($sql_result ? "SUCCESS" : "FAILED - " . $this->db->lasterror()));
if (!$sql_result) {
$this->error = 'Failed to create debit entry: ' . $this->db->lasterror();
return false; return false;
} }
} }
}
// Create credit entry if amount > 0 // Create credit entry if amount > 0
if ($credit_amount > 0) { if ($credit_amount > 0) {
@ -1283,11 +1308,36 @@ class DeclarationTVA
error_log("DEBUG: Credit entry create result: " . ($result ? "SUCCESS" : "FAILED - " . $credit->error)); error_log("DEBUG: Credit entry create result: " . ($result ? "SUCCESS" : "FAILED - " . $credit->error));
if (!$result) { if (!$result) {
$this->error = 'Failed to create credit entry: ' . $credit->error; // Try direct SQL insert as fallback
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_bookkeeping
(doc_date, doc_ref, code_journal, numero_compte, label_compte, montant, sens, fk_doc, fk_docdet, societe_type, fk_user_author, code_tiers, piece_num, import_key, entity, date_creation, tms)
VALUES ('" . $this->db->escape($declaration->end_date) . "',
'" . $this->db->escape($declaration->declaration_name) . "',
'OD',
'" . $this->db->escape($entry['account_code']) . "',
'" . $this->db->escape($entry['account_label']) . "',
" . $credit_amount . ",
'C',
" . $declaration->rowid . ",
0,
1,
" . $user->id . ",
'',
'',
'',
" . $this->entity . ",
NOW(), NOW())";
$sql_result = $this->db->query($sql);
error_log("DEBUG: Direct SQL credit insert result: " . ($sql_result ? "SUCCESS" : "FAILED - " . $this->db->lasterror()));
if (!$sql_result) {
$this->error = 'Failed to create credit entry: ' . $this->db->lasterror();
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
@ -1298,6 +1348,17 @@ class DeclarationTVA
error_log("DEBUG: Final verification - Total entries in database for declaration " . $declaration->declaration_name . ": " . $final_check_obj->total_entries); error_log("DEBUG: Final verification - Total entries in database for declaration " . $declaration->declaration_name . ": " . $final_check_obj->total_entries);
} }
// Also check what's actually in the accounting_bookkeeping table
$debug_sql = "SELECT doc_ref, numero_compte, montant, sens FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping
WHERE doc_ref LIKE '%AOUT%' ORDER BY rowid DESC LIMIT 10";
$debug_result = $this->db->query($debug_sql);
if ($debug_result && $this->db->num_rows($debug_result) > 0) {
error_log("DEBUG: Recent entries in accounting_bookkeeping table:");
while ($debug_obj = $this->db->fetch_object($debug_result)) {
error_log("DEBUG: Entry - doc_ref: " . $debug_obj->doc_ref . ", account: " . $debug_obj->numero_compte . ", amount: " . $debug_obj->montant . ", sens: " . $debug_obj->sens);
}
}
return true; return true;
} }