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); + }