From e87e4eb18749e494075063055166321c7ce2153d Mon Sep 17 00:00:00 2001 From: Zakaria Boushaba <48571684+Boushabazakaria@users.noreply.github.com> Date: Mon, 13 Apr 2026 12:25:05 +0100 Subject: [PATCH] Fix: fallback to numeric VAT rate match when default_vat_code is null (#37830) * Fix: fallback to numeric VAT rate match when default_vat_code is not set on product * fix: restore VAT rate assignment for input box case alongside combo fallback --------- Co-authored-by: Zakaria Boushaba --- htdocs/core/tpl/objectline_create.tpl.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php index da6c68d3897..3226c49f6f5 100644 --- a/htdocs/core/tpl/objectline_create.tpl.php +++ b/htdocs/core/tpl/objectline_create.tpl.php @@ -943,13 +943,20 @@ if (!empty($usemargins) && $user->hasRight('margins', 'creer')) { } - // Set vat rate if field is an input box - $('#tva_tx').val(tva_tx); - // Set vat rate by selecting the combo - //$('#tva_tx option').val(tva_tx); // This is bugged, it replaces the vat key of all options - $('#tva_tx option').removeAttr('selected'); - console.log("stringforvatrateselection="+stringforvatrateselection+" -> value of option label for this key="+$('#tva_tx option[value="'+stringforvatrateselection+'"]').val()); - $('#tva_tx option[value="'+stringforvatrateselection+'"]').prop('selected', true); + // Set vat rate: handle both input box and combo cases + if ($('#tva_tx option').length) { + // It is a combo: try exact match first (rate + code), fallback to numeric match + if ($('#tva_tx option[value="' + stringforvatrateselection + '"]').length) { + $('#tva_tx').val(stringforvatrateselection); + } else { + $('#tva_tx option').filter(function () { + return parseFloat($(this).val()) === parseFloat(tva_tx); + }).first().prop('selected', true); + } + } else { + // It is an input box + $('#tva_tx').val(tva_tx); + }