From 990280038569a1ea24b32eb92a5c253d672865fc Mon Sep 17 00:00:00 2001 From: w3houde Date: Thu, 11 Jun 2026 23:46:31 +0200 Subject: [PATCH] Fix #38036 keep vat_src_code when converting deposit invoice into discount (#38714) When a paid deposit invoice is auto-converted into a discount, the per-rate buckets in Paiement::create were keyed on tva_tx alone. Lines that shared a rate but had distinct vat_src_code values were merged into one bucket, and the generated discount was always created with an empty vat_src_code. A later 'Apply discount' on the standard invoice therefore inserts a line without the source VAT code, while the deposit lines keep it. The PDF then shows two VAT lines for the same rate (one with the code, one without). Bucket by tva_tx + vat_src_code instead, parse both back when looping the buckets, and assign vat_src_code on the generated discount before create(). Signed-off-by: Dolicraft Co-authored-by: Dolicraft --- .../compta/paiement/class/paiement.class.php | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php index 11f566ee519..ec277d76b5f 100644 --- a/htdocs/compta/paiement/class/paiement.class.php +++ b/htdocs/compta/paiement/class/paiement.class.php @@ -541,41 +541,47 @@ class Paiement extends CommonObject $discount->fk_facture_source = $invoice->id; // Loop on each vat rate + // Bucket by tva_tx and vat_src_code so the generated discount keeps the source VAT code $i = 0; foreach ($invoice->lines as $line) { if ($line->product_type != 9 && $line->total_ht != 0) { // no need to create discount if amount is null or is special product - if (!array_key_exists($line->tva_tx, $amount_ht)) { - $amount_ht[$line->tva_tx] = 0.0; - $amount_tva[$line->tva_tx] = 0.0; - $amount_ttc[$line->tva_tx] = 0.0; - $multicurrency_amount_ht[$line->tva_tx] = 0.0; - $multicurrency_amount_tva[$line->tva_tx] = 0.0; - $multicurrency_amount_ttc[$line->tva_tx] = 0.0; + $key = $line->tva_tx.'|'.(string) $line->vat_src_code; + if (!array_key_exists($key, $amount_ht)) { + $amount_ht[$key] = 0.0; + $amount_tva[$key] = 0.0; + $amount_ttc[$key] = 0.0; + $multicurrency_amount_ht[$key] = 0.0; + $multicurrency_amount_tva[$key] = 0.0; + $multicurrency_amount_ttc[$key] = 0.0; } - $amount_ht[$line->tva_tx] += $line->total_ht; - $amount_tva[$line->tva_tx] += $line->total_tva; - $amount_ttc[$line->tva_tx] += $line->total_ttc; - $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht; - $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva; - $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc; + $amount_ht[$key] += $line->total_ht; + $amount_tva[$key] += $line->total_tva; + $amount_ttc[$key] += $line->total_ttc; + $multicurrency_amount_ht[$key] += $line->multicurrency_total_ht; + $multicurrency_amount_tva[$key] += $line->multicurrency_total_tva; + $multicurrency_amount_ttc[$key] += $line->multicurrency_total_ttc; $i++; } } - foreach ($amount_ht as $tva_tx => $xxx) { - $discount->amount_ht = abs($amount_ht[$tva_tx]); - $discount->total_ht = abs($amount_ht[$tva_tx]); - $discount->amount_tva = abs($amount_tva[$tva_tx]); - $discount->total_tva = abs($amount_tva[$tva_tx]); - $discount->amount_ttc = abs($amount_ttc[$tva_tx]); - $discount->total_ttc = abs($amount_ttc[$tva_tx]); - $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]); - $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$tva_tx]); - $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]); - $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$tva_tx]); - $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]); - $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$tva_tx]); + foreach ($amount_ht as $key => $xxx) { + $parts = explode('|', (string) $key, 2); + $tva_tx = $parts[0]; + $vat_src_code = isset($parts[1]) ? $parts[1] : ''; + $discount->amount_ht = abs($amount_ht[$key]); + $discount->total_ht = abs($amount_ht[$key]); + $discount->amount_tva = abs($amount_tva[$key]); + $discount->total_tva = abs($amount_tva[$key]); + $discount->amount_ttc = abs($amount_ttc[$key]); + $discount->total_ttc = abs($amount_ttc[$key]); + $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$key]); + $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$key]); + $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$key]); + $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$key]); + $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$key]); + $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$key]); $discount->tva_tx = abs((float) $tva_tx); + $discount->vat_src_code = $vat_src_code; $result = $discount->create($user); if ($result < 0) {