Remove direct SQL approach and use proper Dolibarr error handling

- Remove direct SQL insert fallback that bypasses Dolibarr logic
- Use proper result checking: if (result > 0) for success
- Log actual entry IDs when successfully created
- Use bookkeeping->error for proper error messages
- Maintain Dolibarr's internal validation and business logic
This commit is contained in:
Frank Cools 2025-10-08 22:13:34 +02:00
parent b195156e2b
commit 038cd3ed67

View File

@ -1252,38 +1252,16 @@ class DeclarationTVA
$debit->entity = $this->entity;
$result = $debit->create($user);
error_log("DEBUG: Debit entry create result: " . ($result ? "SUCCESS" : "FAILED - " . $debit->error));
error_log("DEBUG: Debit entry create result: " . ($result ? "SUCCESS" : "FAILED"));
if (!$result) {
// 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();
if ($result > 0) {
error_log("DEBUG: Debit entry created successfully with ID: " . $result);
} else {
error_log("DEBUG: Debit entry creation failed: " . $debit->error);
$this->error = 'Failed to create debit entry: ' . $debit->error;
return false;
}
}
}
// Create credit entry if amount > 0
if ($credit_amount > 0) {
@ -1305,39 +1283,17 @@ class DeclarationTVA
$credit->entity = $this->entity;
$result = $credit->create($user);
error_log("DEBUG: Credit entry create result: " . ($result ? "SUCCESS" : "FAILED - " . $credit->error));
error_log("DEBUG: Credit entry create result: " . ($result ? "SUCCESS" : "FAILED"));
if (!$result) {
// 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();
if ($result > 0) {
error_log("DEBUG: Credit entry created successfully with ID: " . $result);
} else {
error_log("DEBUG: Credit entry creation failed: " . $credit->error);
$this->error = 'Failed to create credit entry: ' . $credit->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