From 7627bba443ee2657fb478bb09a94e0c35f37a3f4 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 1 Dec 2022 17:28:26 +0100 Subject: [PATCH] Debug v17 --- htdocs/core/class/html.form.class.php | 17 ++++++++++++++--- htdocs/core/lib/functions.lib.php | 9 +++++---- htdocs/fourn/commande/card.php | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 3d929307e2f..4ee0a99121b 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -6279,8 +6279,10 @@ class Form // Definition du taux a pre-selectionner (si defaulttx non force et donc vaut -1 ou '') if ($defaulttx < 0 || dol_strlen($defaulttx) == 0) { $tmpthirdparty = new Societe($this->db); + $defaulttx = get_default_tva($societe_vendeuse, (is_object($societe_acheteuse) ? $societe_acheteuse : $tmpthirdparty), $idprod); $defaultnpr = get_default_npr($societe_vendeuse, (is_object($societe_acheteuse) ? $societe_acheteuse : $tmpthirdparty), $idprod); + if (preg_match('/\((.*)\)/', $defaulttx, $reg)) { $defaultcode = $reg[1]; $defaulttx = preg_replace('/\s*\(.*\)/', '', $defaulttx); @@ -6290,13 +6292,22 @@ class Form } } - // Si taux par defaut n'a pu etre determine, on prend dernier de la liste. - // Comme ils sont tries par ordre croissant, dernier = plus eleve = taux courant + // If we fails to find a default vat rate, we take the last one in list + // Because they are sorted in ascending order, the last one will be the higher one (we suppose the higher one is the current rate) if ($defaulttx < 0 || dol_strlen($defaulttx) == 0) { if (empty($conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS)) { + // We take the last one found in list $defaulttx = $this->cache_vatrates[$num - 1]['txtva']; } else { - $defaulttx = ($conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS == 'none' ? '' : $conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS); + // We will use the rate defined into MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS + $defaulttx = ''; + if ($conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS != 'none') { + $defaulttx = $conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS; + } + if (preg_match('/\((.*)\)/', $defaulttx, $reg)) { + $defaultcode = $reg[1]; + $defaulttx = preg_replace('/\s*\(.*\)/', '', $defaulttx); + } } } diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 7c28fd4dc19..f167b23fc98 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -6306,11 +6306,11 @@ function get_product_vat_for_country($idprod, $thirdpartytouse, $idprodfournpric if (!$found) { if (empty($conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS)) { - // If vat of product for the country not found or not defined, we return the first higher vat of country. + // If vat of product for the country not found or not defined, we return the first found (sorting on use_default then higher vat of country). $sql = "SELECT t.taux as vat_rate, t.code as default_vat_code"; $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c"; - $sql .= " WHERE t.active=1 AND t.fk_pays = c.rowid AND c.code='".$db->escape($thirdpartytouse->country_code)."'"; - $sql .= " ORDER BY t.taux DESC, t.code ASC, t.recuperableonly ASC"; + $sql .= " WHERE t.active=1 AND t.fk_pays = c.rowid AND c.code = '".$db->escape($thirdpartytouse->country_code)."'"; + $sql .= " ORDER BY t.use_default DESC, t.taux DESC, t.code ASC, t.recuperableonly ASC"; $sql .= $db->plimit(1); $resql = $db->query($sql); @@ -6327,7 +6327,8 @@ function get_product_vat_for_country($idprod, $thirdpartytouse, $idprodfournpric dol_print_error($db); } } else { - $ret = $conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS; // Forced value if autodetect fails + // Forced value if autodetect fails. MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS can be '1.23' or '1.23 (CODE)' + $ret = $conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS; } } diff --git a/htdocs/fourn/commande/card.php b/htdocs/fourn/commande/card.php index bbab7cc14fb..1df6c37736b 100644 --- a/htdocs/fourn/commande/card.php +++ b/htdocs/fourn/commande/card.php @@ -413,7 +413,7 @@ if (empty($reshook)) { // Add a product line if ($action == 'addline' && GETPOST('submitforalllines', 'aZ09') && GETPOST('vatforalllines', 'alpha') && $usercancreate) { - // Define vat_rate + // Define new vat_rate for all lines $vat_rate = (GETPOST('vatforalllines') ? GETPOST('vatforalllines') : 0); $vat_rate = str_replace('*', '', $vat_rate); $localtax1_rate = get_localtax($vat_rate, 1, $object->thirdparty, $mysoc);