Invoice list shows error "Bill with id X or ref not found" when source template invoice has been deleted
Bug
After deleting a template (recurring) invoice, the customer invoice list displays a red error message for every invoice that was generated from that template, as soon as the column "Generated from template" (f.fk_fac_rec_source) is displayed:
Bill with id 3 or ref not foundThe invoice card itself handles the same situation correctly and shows "Generated from template: Object deleted", so only the list is affected.
Dolibarr Version
24.0.1. The same code is still present on develop.
Environment PHP
8.1
Steps to reproduce the behavior
- Create a template invoice and generate at least one invoice from it.
- Delete the template invoice.
- Go to Billing › Customer invoices › List and enable the column "Generated from template".
- A red error
Bill with id <id> or ref not foundis displayed.
Expected and actual behavior
Expected: the column stays empty, or shows "Object deleted" as the invoice card does, with no error message.
Actual: a technical error message in English is shown on every list load.
Analysis
htdocs/compta/facture/list.php(around line 3095 in 24.0):
if (!empty($obj->fk_fac_rec_source)) {
$facrec = new FactureRec($db);
$result = $facrec->fetch($obj->fk_fac_rec_source);
if ($result < 0) {
setEventMessages($facrec->error, $facrec->errors, 'errors');
} else {
print $facrec->getNomUrl();
}
}FactureRec::fetch()returns-2when the record is not found (htdocs/compta/facture/class/facture-rec.class.phparound line 761). Its docblock says it should return0if not found. Because of this, "not found" is treated as a real error.FactureRec::delete()removes the template and its lines. It does not resetllx_facture.fk_fac_rec_sourceon the invoices generated from it, so a dangling reference remains.
Suggested fix
- In
list.php, handle the "not found" case without an error message. For example, print$langs->trans("ObjectDeleted")like the invoice card does, and keepsetEventMessages()for real SQL errors only. - Optionally, make
FactureRec::fetch()return0when the record is not found, as its docblock states. - Optionally, in
FactureRec::delete(), runUPDATE llx_facture SET fk_fac_rec_source = NULL WHERE fk_fac_rec_source = <id>. This is debatable, because the invoice card uses the dangling id to display "Object deleted".
Fetching the template once per row also causes one query per line. The list already LEFT JOINs facture_rec as facrec, so the title could be taken from that join instead.
Source: Dolibarr/dolibarr