From f71bf6575edbd9b64a2e9c2394c9af76270f2aed Mon Sep 17 00:00:00 2001 From: Jam Balaya Date: Wed, 22 Jul 2026 18:37:10 +0900 Subject: [PATCH] FIX #32217 Fire BILL_CREATE with create-form extrafields on next situation invoice (#39254) When creating the next situation invoice, the create-form extrafields were saved AFTER createFromCurrent() had already fired BILL_CREATE, so triggers listening to BILL_CREATE never saw them. Passing 'BILL_CREATE' to insertExtraFields() (as first proposed) would fire the trigger a second time. Instead, set the extrafields on $object before createFromCurrent() and let create() insert them and fire BILL_CREATE once with the values present. createFromCurrent() now skips its fetch_optionals() when array_options is already set by the caller, so the form values are no longer overwritten by the source invoice values. The two other createFromCurrent() callers do not pre-set array_options, so their behaviour is unchanged. Supersedes #32217. Signed-off-by: Jam Balaya --- htdocs/compta/facture/card.php | 13 ++++++------- htdocs/compta/facture/class/facture.class.php | 5 ++++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/htdocs/compta/facture/card.php b/htdocs/compta/facture/card.php index a3bdeb5c12d..eb84e7b2b12 100644 --- a/htdocs/compta/facture/card.php +++ b/htdocs/compta/facture/card.php @@ -2087,6 +2087,12 @@ if (empty($reshook)) { $object->situation_counter += 1; + // Set extrafields from the create form BEFORE createFromCurrent(), so they are already + // present when create() inserts them and fires the BILL_CREATE trigger. This avoids firing + // BILL_CREATE a second time just to expose the extrafields (#32217). + $extrafields->fetch_name_optionals_label($object->table_element); + $extrafields->setOptionalsFromPost(null, $object); + $id = $object->createFromCurrent($user); if ($id <= 0) { $mesg = $object->error; @@ -2094,13 +2100,6 @@ if (empty($reshook)) { $nextSituationInvoice = new Facture($db); $nextSituationInvoice->fetch($id); - // create extrafields with data from create form - $extrafields->fetch_name_optionals_label($nextSituationInvoice->table_element); - $ret = $extrafields->setOptionalsFromPost(null, $nextSituationInvoice); - if ($ret > 0) { - $nextSituationInvoice->insertExtraFields(); - } - // Hooks $parameters = array('origin_type' => $object->origin_type, 'origin_id' => $object->origin_id); $reshook = $hookmanager->executeHooks('createFrom', $parameters, $nextSituationInvoice, $action); // Note that $action and $object may have been diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index e50ddfbc420..f8cd4900f31 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -1178,7 +1178,10 @@ class Facture extends CommonInvoice // Retrieve all extrafield // fetch optionals attributes and labels - $this->fetch_optionals(); + // (unless the caller already set array_options, e.g. from a create form, so we don't overwrite them) + if (empty($this->array_options)) { + $this->fetch_optionals(); + } if (!empty($this->array_options)) { $facture->array_options = $this->array_options;