|
118 | 118 | $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
|
119 | 119 | }
|
120 | 120 |
|
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,"; |
122 | 122 | $sql .= " ba.courant, ba.ref as baref, ba.account_number, ba.fk_accountancy_journal,";
|
123 | 123 | $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,"; |
124 | 125 | if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
|
125 | 126 | $sql .= " spe.accountancy_code_customer as code_compta,";
|
126 | 127 | $sql .= " spe.accountancy_code_supplier as code_compta_fournisseur,";
|
|
137 | 138 | $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON bu2.fk_bank = b.rowid AND bu2.type='user'";
|
138 | 139 | $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu3 ON bu3.fk_bank = b.rowid AND bu3.type='payment'";
|
139 | 140 | $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"; |
140 | 143 | $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as soc on bu1.url_id=soc.rowid";
|
141 | 144 | if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
|
142 | 145 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = soc.rowid AND spe.entity = " . ((int) $conf->entity);
|
143 | 146 | }
|
144 | 147 | $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on bu2.url_id=u.rowid";
|
145 | 148 | $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 |
147 | 150 | if ($date_start && $date_end) {
|
148 | 151 | $sql .= " AND b.dateo >= '".$db->idate($date_start)."' AND b.dateo <= '".$db->idate($date_end)."'";
|
149 | 152 | }
|
|
328 | 331 | // the case the link is user (often because managed by hard coded code into another link), and we must avoid this.
|
329 | 332 | continue;
|
330 | 333 | }
|
| 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 | + } |
331 | 339 | 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'))) {
|
332 | 340 | // So we excluded 'company' and 'user' here. We want only payment lines
|
333 | 341 |
|
|
0 commit comments