Use bank account's fk_accountancy_journal for bank entries

- Get fk_accountancy_journal from bank_account table
- Add getJournalCodeFromId method to get journal code from journal ID
- Use the correct journal code from bank account instead of configuration
- Bank entries will now use the proper journal configured for the bank account
This commit is contained in:
Frank Cools 2025-10-09 01:08:27 +02:00
parent da5820e524
commit dfc6d49441

View File

@ -1101,9 +1101,9 @@ class DeclarationTVA
);
}
// Get bank journal code from configuration
$bank_journal_code = $bank_config['bank_journal'] ?? 'BAN';
error_log("DEBUG: Using bank journal code: " . $bank_journal_code);
// Get bank journal code from bank account
$bank_journal_code = $bank_account['journal_code'] ?? 'BAN';
error_log("DEBUG: Using bank journal code from bank account: " . $bank_journal_code);
// Create accounting entries in Dolibarr
return $this->saveAccountingEntries($entries, $declaration, $user, $bank_journal_code);
@ -1425,8 +1425,8 @@ class DeclarationTVA
*/
private function getBankAccountDetails($bank_account_id)
{
// Get bank account info and its linked accounting account
$sql = "SELECT ba.rowid, ba.label, ba.number, ba.bank, ba.account_number
// Get bank account info and its linked accounting account and journal
$sql = "SELECT ba.rowid, ba.label, ba.number, ba.bank, ba.account_number, ba.fk_accountancy_journal
FROM " . MAIN_DB_PREFIX . "bank_account ba
WHERE ba.rowid = " . (int)$bank_account_id . "
AND ba.entity = " . $this->entity;
@ -1439,7 +1439,10 @@ class DeclarationTVA
// Get the accounting account label for the account code
$account_label = $this->getAccountLabel($obj->account_number);
error_log("DEBUG: Bank account data - account_number: " . $obj->account_number . ", account_label: " . $account_label);
// Get the journal code from the journal ID
$journal_code = $this->getJournalCodeFromId($obj->fk_accountancy_journal);
error_log("DEBUG: Bank account data - account_number: " . $obj->account_number . ", account_label: " . $account_label . ", journal_code: " . $journal_code);
return array(
'rowid' => $obj->rowid,
@ -1447,7 +1450,8 @@ class DeclarationTVA
'number' => $obj->number,
'bank' => $obj->bank,
'account_code' => $obj->account_number, // Use the linked accounting account code
'account_label' => $account_label
'account_label' => $account_label,
'journal_code' => $journal_code
);
}
@ -1476,6 +1480,27 @@ class DeclarationTVA
return $account_code; // Fallback to account code
}
/**
* Get journal code from journal ID
*
* @param int $journal_id Journal ID
* @return string Journal code
*/
private function getJournalCodeFromId($journal_id)
{
$sql = "SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal
WHERE rowid = " . (int)$journal_id . "
AND entity = " . $this->entity;
$result = $this->db->query($sql);
if ($result && $this->db->num_rows($result) > 0) {
$obj = $this->db->fetch_object($result);
return $obj->code;
}
return 'BAN'; // Fallback to BAN if not found
}
/**
* Submit declaration (create accounting entries and update status)