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 <jambalaya.pyoncafe@outlook.jp>
This commit is contained in:
Jam Balaya 2026-07-22 18:37:10 +09:00 committed by GitHub
parent ed47b61151
commit f71bf6575e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 8 deletions

View file

@ -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

View file

@ -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;