#41130·dolibarr

Invoice list shows error "Bill with id X or ref not found" when source template invoice has been deleted

Author: lhalfaouiCreated Sep 30, 2026Updated Sep 30, 2026

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 found

The 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

  1. Create a template invoice and generate at least one invoice from it.
  2. Delete the template invoice.
  3. Go to Billing › Customer invoices › List and enable the column "Generated from template".
  4. A red error Bill with id <id> or ref not found is 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

  1. htdocs/compta/facture/list.php (around line 3095 in 24.0):
php
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();
    }
}
  1. FactureRec::fetch() returns -2 when the record is not found (htdocs/compta/facture/class/facture-rec.class.php around line 761). Its docblock says it should return 0 if not found. Because of this, "not found" is treated as a real error.

  2. FactureRec::delete() removes the template and its lines. It does not reset llx_facture.fk_fac_rec_source on 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 keep setEventMessages() for real SQL errors only.
  • Optionally, make FactureRec::fetch() return 0 when the record is not found, as its docblock states.
  • Optionally, in FactureRec::delete(), run UPDATE 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.