Use proper label_operation from bank journal entries

- Use entry_label from bank journal entries as label_operation
- This should resolve BookkeepingRecordAlreadyExists error
- Bank entries now use 'Paiement TVA' or 'Remboursement TVA' labels
This commit is contained in:
Frank Cools 2025-10-09 11:44:07 +02:00
parent 3b55869a49
commit 7a0a7bd921

View File

@ -1301,8 +1301,22 @@ class DeclarationTVA
$this->db->begin();
error_log("DEBUG: Database transaction started for journal: " . $journal_code);
/*
// Get journal label from database and translate it
$journal_label_sql = "SELECT label FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE code = '" . $journal_code . "' AND entity = " . $this->entity;
$journal_label_result = $this->db->query($journal_label_sql);
if ($journal_label_result && $this->db->num_rows($journal_label_result) > 0) {
$journal_label_obj = $this->db->fetch_object($journal_label_result);
$debit->journal_label = $journal_label_obj->label;
error_log("DEBUG: Retrieved journal label: " . $journal_label_obj->label);
} else {
error_log("DEBUG: No journal label found for " . $journal_code . " code");
}
*/
try {
foreach ($entries as $entry) {
error_log("--------------------------------------------------------");
error_log("DEBUG: Creating accounting entry for account: " . $entry['account_code'] . ", debit: " . $entry['debit'] . ", credit: " . $entry['credit']);
// Prepare amounts
@ -1337,22 +1351,11 @@ class DeclarationTVA
$debit->tms = dol_now();
$debit->fk_user_creation = $user->id;
$debit->fk_user_modification = $user->id;
$debit->label_operation = $declaration->declaration_name;
$debit->label_operation = isset($entry['entry_label']) ? $entry['entry_label'] : $declaration->declaration_name;
// Set ref field again to ensure it's not overwritten
$debit->ref = $declaration->declaration_name;
// Get journal label from database and translate it
$journal_label_sql = "SELECT label FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE code = 'OD' AND entity = " . $this->entity;
$journal_label_result = $this->db->query($journal_label_sql);
if ($journal_label_result && $this->db->num_rows($journal_label_result) > 0) {
$journal_label_obj = $this->db->fetch_object($journal_label_result);
$debit->journal_label = $journal_label_obj->label;
error_log("DEBUG: Retrieved journal label: " . $journal_label_obj->label);
} else {
error_log("DEBUG: No journal label found for OD code");
}
$result = $debit->create($user);
if ($debit->id <= 0) {
throw new Exception('Failed to create debit entry: ' . $debit->error);
@ -1372,7 +1375,7 @@ class DeclarationTVA
$credit = new Bookkeeping($this->db);
$credit->doc_date = dol_mktime(0, 0, 0, date('m', strtotime($declaration->end_date)), date('d', strtotime($declaration->end_date)), date('Y', strtotime($declaration->end_date)));
$credit->doc_ref = $declaration->declaration_name;
$credit->ref = $declaration->declaration_name;
//$credit->ref = $declaration->declaration_name;
$credit->code_journal = $journal_code;
$credit->numero_compte = $entry['account_code'];
$credit->label_compte = $entry['account_label'];
@ -1387,22 +1390,20 @@ class DeclarationTVA
$credit->tms = dol_now();
$credit->fk_user_creation = $user->id;
$credit->fk_user_modification = $user->id;
$credit->label_operation = $declaration->declaration_name;
$credit->label_operation = isset($entry['entry_label']) ? $entry['entry_label'] : $declaration->declaration_name;
// Set ref field again to ensure it's not overwritten
$credit->ref = $declaration->declaration_name;
// Get journal label from database and translate it
$journal_label_sql = "SELECT label FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE code = 'OD' AND entity = " . $this->entity;
$journal_label_result = $this->db->query($journal_label_sql);
if ($journal_label_result && $this->db->num_rows($journal_label_result) > 0) {
$journal_label_obj = $this->db->fetch_object($journal_label_result);
$credit->journal_label = $journal_label_obj->label;
error_log("DEBUG: Retrieved journal label: " . $journal_label_obj->label);
} else {
error_log("DEBUG: No journal label found for OD code");
}
error_log("==============================================");
error_log("credit->code_journal: " . $credit->code_journal);
error_log("credit->numero_compte: " . $credit->numero_compte);
error_log("credit->doc_ref: " . $credit->doc_ref);
error_log("credit->debit: " . $credit->debit);
error_log("credit->credit: " . $credit->credit);
error_log("credit->entity: " . $credit->entity);
error_log("==============================================");
$result = $credit->create($user);
if ($credit->id <= 0) {
throw new Exception('Failed to create credit entry: ' . $credit->error);
@ -1428,7 +1429,7 @@ class DeclarationTVA
$final_check_obj = $this->db->fetch_object($final_check_result);
error_log("DEBUG: Final verification - Total entries in database for declaration " . $declaration->declaration_name . ": " . $final_check_obj->total_entries);
}
/*
// Simple verification
error_log("DEBUG: Accounting entries created successfully");
@ -1452,7 +1453,7 @@ class DeclarationTVA
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;
}