From 04d96f489ba28292e4e75d9a8a7407e2c41ea0f6 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 23 Apr 2023 15:24:10 +0200 Subject: [PATCH] FIX Default VAT of product added does not take account buyer country --- htdocs/core/tpl/objectline_create.tpl.php | 6 ++-- htdocs/product/ajax/products.php | 39 ++++++++++++++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php index 883c3f677d8..321a2a44672 100644 --- a/htdocs/core/tpl/objectline_create.tpl.php +++ b/htdocs/core/tpl/objectline_create.tpl.php @@ -738,11 +738,11 @@ if (!empty($usemargins) && $user->rights->margins->creer) { if (isNaN(pbq)) { console.log("We use experimental option PRODUIT_CUSTOMER_PRICES_BY_QTY or PRODUIT_CUSTOMER_PRICES_BY_QTY but we could not get the id of pbq from product combo list, so load of price may be 0 if product has differet prices"); } // Get the price for the product and display it - console.log("Load unit price without tax and set it into #price_ht for product id="+$(this).val()+" socid=socid; ?>"); + console.log("Load unit price and set it into #price_ht or #price_ttc for product id="+$(this).val()+" socid=socid; ?>"); $.post('/product/ajax/products.php?action=fetch', - { 'id': $(this).val(), 'socid': socid; ?>, 'token': '' }, + { 'id': $(this).val(), 'socid': socid; ?>, 'token': '', 'addalsovatforthirdpartyid': 1 }, function(data) { - console.log("objectline_create.tpl Load unit price end, we got value ht="+data.price_ht+" ttc="+data.price_ttc+" pricebasetype="+data.pricebasetype); + console.log("objectline_create.tpl Load unit price ends, we got value ht="+data.price_ht+" ttc="+data.price_ttc+" pricebasetype="+data.pricebasetype); $('#date_start').removeAttr('type'); $('#date_end').removeAttr('type'); diff --git a/htdocs/product/ajax/products.php b/htdocs/product/ajax/products.php index e07aa4ab5c5..3f60355301f 100644 --- a/htdocs/product/ajax/products.php +++ b/htdocs/product/ajax/products.php @@ -20,7 +20,7 @@ /** * \file htdocs/product/ajax/products.php - * \brief File to return Ajax response on product list request. + * \brief File to return Ajax response on product list request, with default VAT rate. */ if (!defined('NOTOKENRENEWAL')) { @@ -35,9 +35,6 @@ if (!defined('NOREQUIREHTML')) { if (!defined('NOREQUIREAJAX')) { define('NOREQUIREAJAX', '1'); } -if (!defined('NOREQUIRESOC')) { - define('NOREQUIRESOC', '1'); -} if (empty($_GET['keysearch']) && !defined('NOREQUIREHTML')) { define('NOREQUIREHTML', '1'); } @@ -225,6 +222,32 @@ if ($action == 'fetch' && !empty($id)) { $outdefault_vat_code = $object->default_vat_code; } + // VAT to use and default VAT for product are set to same value by default + $product_outtva_tx_formated = $outtva_tx_formated; + $product_outtva_tx = $outtva_tx; + $product_outdefault_vat_code = $outdefault_vat_code; + + // If we ask the price according to buyer, we change it. + if (GETPOST('addalsovatforthirdpartyid', 'int')) { + $thirdparty_buyer = new Societe($db); + $thirdparty_buyer->fetch($socid); + + $tmpvatwithcode = get_default_tva($mysoc, $thirdparty_buyer, $id, 0); + + if (!is_numeric($tmpvatwithcode) || $tmpvatwithcode != -1) { + $reg =array(); + if (preg_match('/(.+)\s\((.+)\)/', $tmpvatwithcode, $reg)) { + $outtva_tx = price2num($reg[1]); + $outtva_tx_formated = price($outtva_tx); + $outdefault_vat_code = $reg[2]; + } else { + $outtva_tx = price2num($tmpvatwithcode); + $outtva_tx_formated = price($outtva_tx); + $outdefault_vat_code = ''; + } + } + } + $outjson = array( 'ref' => $outref, 'label' => $outlabel, @@ -235,13 +258,19 @@ if ($action == 'fetch' && !empty($id)) { 'price_ht' => $outprice_ht, 'price_ttc' => $outprice_ttc, 'pricebasetype' => $outpricebasetype, + 'product_tva_tx_formated' => $product_outtva_tx_formated, + 'product_tva_tx' => $product_outtva_tx, + 'product_default_vat_code' => $product_outdefault_vat_code, + 'tva_tx_formated' => $outtva_tx_formated, 'tva_tx' => $outtva_tx, 'default_vat_code' => $outdefault_vat_code, + 'qty' => $outqty, 'discount' => $outdiscount, 'mandatory_period' => $mandatory_period, - 'array_options'=>$object->array_options); + 'array_options'=>$object->array_options + ); } echo json_encode($outjson);