Skip to content

Commit f551818

Browse files
committed
FIX: accountancy bank journal: handle payments made on behalf of sister companies
1 parent d7758f9 commit f551818

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

htdocs/accountancy/journal/bankjournal.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@
118118
$date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
119119
}
120120

121-
$sql = "SELECT b.rowid, b.dateo as do, b.datev as dv, b.amount, b.amount_main_currency, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type, b.fk_account,";
121+
$sql = "SELECT b.rowid, b.dateo as do, b.datev as dv, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type, b.fk_account,";
122122
$sql .= " ba.courant, ba.ref as baref, ba.account_number, ba.fk_accountancy_journal,";
123123
$sql .= " soc.rowid as socid, soc.nom as name, soc.email as email, bu1.type as typeop_company,";
124+
$sql .= " f.total_ttc AS amount, f.multicurrency_total_ttc AS amount_main_currency,";
124125
if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
125126
$sql .= " spe.accountancy_code_customer as code_compta,";
126127
$sql .= " spe.accountancy_code_supplier as code_compta_fournisseur,";
@@ -137,13 +138,15 @@
137138
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON bu2.fk_bank = b.rowid AND bu2.type='user'";
138139
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu3 ON bu3.fk_bank = b.rowid AND bu3.type='payment'";
139140
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu4 ON bu4.fk_bank = b.rowid AND bu4.type='payment_supplier'";
141+
$sql .= " LEFT JOIN " . $db->prefix() . "paiement_facture as pf ON pf.fk_paiement = bu3.url_id";
142+
$sql .= " LEFT JOIN " . $db->prefix() . "facture as f ON f.rowid = pf.fk_facture AND f.fk_soc = bu1.url_id";
140143
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as soc on bu1.url_id=soc.rowid";
141144
if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
142145
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = soc.rowid AND spe.entity = " . ((int) $conf->entity);
143146
}
144147
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on bu2.url_id=u.rowid";
145148
$sql .= " WHERE ba.fk_accountancy_journal=".((int) $id_journal);
146-
$sql .= ' AND b.amount <> 0 AND ba.entity IN ('.getEntity('bank_account', 0).')'; // We don't share object for accountancy
149+
$sql .= ' AND COALESCE(f.total_ttc, 0) <> 0 AND ba.entity IN ('.getEntity('bank_account', 0).')'; // We don't share object for accountancy
147150
if ($date_start && $date_end) {
148151
$sql .= " AND b.dateo >= '".$db->idate($date_start)."' AND b.dateo <= '".$db->idate($date_end)."'";
149152
}
@@ -328,6 +331,11 @@
328331
// the case the link is user (often because managed by hard coded code into another link), and we must avoid this.
329332
continue;
330333
}
334+
if ($val['type'] === 'company' && $val['url_id'] != $obj->socid) {
335+
// Payments have been enabled for companies sharing a holding. In this case, the bank entry has multiple links with companies.
336+
// We chose only the link with the original thirdparty of the invoice
337+
continue;
338+
}
331339
if (in_array($links[$key]['type'], array('sc', 'payment_sc', 'payment', 'payment_supplier', 'payment_vat', 'payment_expensereport', 'banktransfert', 'payment_donation', 'member', 'payment_loan', 'payment_salary', 'payment_various'))) {
332340
// So we excluded 'company' and 'user' here. We want only payment lines
333341

0 commit comments

Comments
 (0)