diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 6e22836..7f88b5b 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -1255,8 +1255,33 @@ class DeclarationTVA error_log("DEBUG: Debit entry create result: " . ($result ? "SUCCESS" : "FAILED - " . $debit->error)); if (!$result) { - $this->error = 'Failed to create debit entry: ' . $debit->error; - return false; + // 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; + } } } @@ -1283,8 +1308,33 @@ class DeclarationTVA error_log("DEBUG: Credit entry create result: " . ($result ? "SUCCESS" : "FAILED - " . $credit->error)); if (!$result) { - $this->error = 'Failed to create credit entry: ' . $credit->error; - return false; + // 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; + } } } } @@ -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); } + // 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; }