Fix #29052 preserve IRPF / localtax2 when cloning an invoice (#38538)

`Facture::createFromClone()` calls `Facture::fetch($fromid)` to
populate the in-memory object, then immediately runs
`$object->create($user)` to persist the clone. `fetch()` explicitly
sets `$this->thirdparty = null;` (see facture.class.php fetch()), and
the clone path never calls `fetch_thirdparty()` before `create()`.

When `create()` walks the cloned lines it goes through
`addLine()` -> `getLocalTaxesFromRate($txtva, 0, $this->thirdparty,
$mysoc)` -> `get_localtax($vatrate, 2, $buyer, $seller)`. The buyer
argument is the now-null $this->thirdparty, and `get_localtax()`
guards on `!$thirdparty_buyer->localtax2_assuj` and returns 0 as soon
as that property is unreadable. `calcul_price_total()` then computes
total_localtax2 = 0, and addLine() resets the line's localtax2_tx to
0 ("if ($total_localtax2)" branch in addLine()). The cloned line is
inserted with the IRPF (localtax2) rate stripped, which is the symptom
the reporter and the four other thread participants observed.

Call `fetch_thirdparty()` once in createFromClone() so the buyer is
populated for the rest of the clone flow. The fix is local to the
clone path; the regular "create from form" flow already had a
thirdparty set by its caller and is not affected.

Signed-off-by: Dolicraft <contact@dolicraft.com>
Co-authored-by: Dolicraft <contact@dolicraft.com>
This commit is contained in:
w3houde 2026-06-09 18:40:11 +02:00 committed by GitHub
parent 9ca97adc0b
commit aa06ea6590
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1276,6 +1276,12 @@ class Facture extends CommonInvoice
$this->db->begin();
$object->fetch($fromid);
// fetch() clears $object->thirdparty, but the subsequent $object->create() path
// reaches addLine() -> getLocalTaxesFromRate() -> get_localtax() and uses the
// buyer thirdparty to decide whether IRPF/localtax2 applies. Without this fetch,
// get_localtax() sees $thirdparty_buyer = null, returns 0 and the cloned lines
// lose their localtax2_tx (see issue #29052).
$object->fetch_thirdparty();
// Load source object
$objFrom = clone $object;