From 2dc927914319e08d3be9648944a0e83f979b13fd Mon Sep 17 00:00:00 2001 From: data Date: Fri, 20 Feb 2026 09:09:32 +0100 Subject: [PATCH] Fix: linkExistingPayment and linkMultipleExistingPayments not updating transaction When linking paid invoices that already have bank entries: - Added missing commit() and return after update() in bank_line branch - Added proper error handling for update() failures - Fixed both single invoice (linkExistingPayment) and multi-invoice linking Co-Authored-By: Claude Opus 4.5 --- class/banktransaction.class.php | 110 +++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 31 deletions(-) diff --git a/class/banktransaction.class.php b/class/banktransaction.class.php index 5971c9c..8ce492c 100755 --- a/class/banktransaction.class.php +++ b/class/banktransaction.class.php @@ -1687,10 +1687,16 @@ class BankImportTransaction extends CommonObject $this->status = self::STATUS_MATCHED; $this->fk_user_match = $user->id; $this->date_match = dol_now(); - $this->update($user); + + $result = $this->update($user); + if ($result <= 0) { + $this->error = 'Failed to update transaction: '.$this->error; + $this->db->rollback(); + return -6; + } $this->db->commit(); - return $paiementfourn->bank_line; + return $paiementfourn->bank_line > 0 ? $paiementfourn->bank_line : 1; } // Create bank entry for existing payment @@ -2125,7 +2131,28 @@ class BankImportTransaction extends CommonObject // Check if payment already has a bank entry if (!empty($paiementfourn->bank_line) && $paiementfourn->bank_line > 0) { + // Payment already has a bank entry in Dolibarr + // Just link this imported transaction to the existing payment WITHOUT creating new bank entry $bankLineId = $paiementfourn->bank_line; + + $this->fk_paiementfourn = $firstPaymentId; + $this->fk_bank = $bankLineId; + $this->fk_facture_fourn = $invoices[0]['id']; + $this->fk_societe = $socid; + $this->status = self::STATUS_MATCHED; + $this->fk_user_match = $user->id; + $this->date_match = dol_now(); + $this->note_private = ($this->note_private ? $this->note_private."\n" : '').'Multi-invoice link: '.implode(', ', $invoiceRefs); + + $result = $this->update($user); + if ($result <= 0) { + $this->error = 'Failed to update transaction: '.$this->error; + $this->db->rollback(); + return -6; + } + + $this->db->commit(); + return $bankLineId > 0 ? $bankLineId : 1; } else { // Create bank entry for existing payment $bankLineId = $paiementfourn->addPaymentToBank( @@ -2136,21 +2163,21 @@ class BankImportTransaction extends CommonObject $this->name, '' ); - } - if ($bankLineId > 0) { - $this->fk_paiementfourn = $firstPaymentId; - $this->fk_bank = $bankLineId; - $this->fk_facture_fourn = $invoices[0]['id']; - $this->fk_societe = $socid; - $this->status = self::STATUS_MATCHED; - $this->fk_user_match = $user->id; - $this->date_match = dol_now(); - $this->note_private = ($this->note_private ? $this->note_private."\n" : '').'Multi-invoice link: '.implode(', ', $invoiceRefs); - $this->update($user); - } else { - $this->error = 'Failed to add payment to bank'; - $error++; + if ($bankLineId > 0) { + $this->fk_paiementfourn = $firstPaymentId; + $this->fk_bank = $bankLineId; + $this->fk_facture_fourn = $invoices[0]['id']; + $this->fk_societe = $socid; + $this->status = self::STATUS_MATCHED; + $this->fk_user_match = $user->id; + $this->date_match = dol_now(); + $this->note_private = ($this->note_private ? $this->note_private."\n" : '').'Multi-invoice link: '.implode(', ', $invoiceRefs); + $this->update($user); + } else { + $this->error = 'Failed to add payment to bank'; + $error++; + } } } else { require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php'; @@ -2163,7 +2190,28 @@ class BankImportTransaction extends CommonObject // Check if payment already has a bank entry if (!empty($paiement->bank_line) && $paiement->bank_line > 0) { + // Payment already has a bank entry in Dolibarr + // Just link this imported transaction to the existing payment WITHOUT creating new bank entry $bankLineId = $paiement->bank_line; + + $this->fk_paiement = $firstPaymentId; + $this->fk_bank = $bankLineId; + $this->fk_facture = $invoices[0]['id']; + $this->fk_societe = $socid; + $this->status = self::STATUS_MATCHED; + $this->fk_user_match = $user->id; + $this->date_match = dol_now(); + $this->note_private = ($this->note_private ? $this->note_private."\n" : '').'Multi-invoice link: '.implode(', ', $invoiceRefs); + + $result = $this->update($user); + if ($result <= 0) { + $this->error = 'Failed to update transaction: '.$this->error; + $this->db->rollback(); + return -6; + } + + $this->db->commit(); + return $bankLineId > 0 ? $bankLineId : 1; } else { // Create bank entry for existing payment $bankLineId = $paiement->addPaymentToBank( @@ -2174,21 +2222,21 @@ class BankImportTransaction extends CommonObject $this->name, '' ); - } - if ($bankLineId > 0) { - $this->fk_paiement = $firstPaymentId; - $this->fk_bank = $bankLineId; - $this->fk_facture = $invoices[0]['id']; - $this->fk_societe = $socid; - $this->status = self::STATUS_MATCHED; - $this->fk_user_match = $user->id; - $this->date_match = dol_now(); - $this->note_private = ($this->note_private ? $this->note_private."\n" : '').'Multi-invoice link: '.implode(', ', $invoiceRefs); - $this->update($user); - } else { - $this->error = 'Failed to add payment to bank'; - $error++; + if ($bankLineId > 0) { + $this->fk_paiement = $firstPaymentId; + $this->fk_bank = $bankLineId; + $this->fk_facture = $invoices[0]['id']; + $this->fk_societe = $socid; + $this->status = self::STATUS_MATCHED; + $this->fk_user_match = $user->id; + $this->date_match = dol_now(); + $this->note_private = ($this->note_private ? $this->note_private."\n" : '').'Multi-invoice link: '.implode(', ', $invoiceRefs); + $this->update($user); + } else { + $this->error = 'Failed to add payment to bank'; + $error++; + } } } @@ -2198,7 +2246,7 @@ class BankImportTransaction extends CommonObject } $this->db->commit(); - return $bankLineId; + return $bankLineId > 0 ? $bankLineId : 1; } /**