FIX default_vat_code was not returned in some Ajax services of products

This commit is contained in:
Laurent Destailleur 2021-11-23 13:08:00 +01:00
parent ee14aa2597
commit 0dcbd458b8
5 changed files with 60 additions and 23 deletions

View File

@ -2414,7 +2414,7 @@ class Form
} }
} }
$selectFields = " p.rowid, p.ref, p.label, p.description, p.barcode, p.fk_country, p.fk_product_type, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.duration, p.fk_price_expression"; $selectFields = " p.rowid, p.ref, p.label, p.description, p.barcode, p.fk_country, p.fk_product_type, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.default_vat_code, p.duration, p.fk_price_expression";
if (count($warehouseStatusArray)) { if (count($warehouseStatusArray)) {
$selectFieldsGrouped = ", sum(".$this->db->ifsql("e.statut IS NULL", "0", "ps.reel").") as stock"; // e.statut is null if there is no record in stock $selectFieldsGrouped = ", sum(".$this->db->ifsql("e.statut IS NULL", "0", "ps.reel").") as stock"; // e.statut is null if there is no record in stock
} else { } else {
@ -2436,8 +2436,8 @@ class Form
//Price by customer //Price by customer
if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES) && !empty($socid)) { if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES) && !empty($socid)) {
$sql .= ', pcp.rowid as idprodcustprice, pcp.price as custprice, pcp.price_ttc as custprice_ttc,'; $sql .= ', pcp.rowid as idprodcustprice, pcp.price as custprice, pcp.price_ttc as custprice_ttc,';
$sql .= ' pcp.price_base_type as custprice_base_type, pcp.tva_tx as custtva_tx, pcp.ref_customer as custref'; $sql .= ' pcp.price_base_type as custprice_base_type, pcp.tva_tx as custtva_tx, pcp.default_vat_code as custdefault_vat_code, pcp.ref_customer as custref';
$selectFields .= ", idprodcustprice, custprice, custprice_ttc, custprice_base_type, custtva_tx, custref"; $selectFields .= ", idprodcustprice, custprice, custprice_ttc, custprice_base_type, custtva_tx, custdefault_vat_code, custref";
} }
// Units // Units
if (!empty($conf->global->PRODUCT_USE_UNITS)) { if (!empty($conf->global->PRODUCT_USE_UNITS)) {
@ -2658,6 +2658,9 @@ class Form
$objp->remise_percent = $objp2->remise_percent; $objp->remise_percent = $objp2->remise_percent;
$objp->remise = $objp2->remise; $objp->remise = $objp2->remise;
//$objp->tva_tx is not overwritten by $objp2 value
//$objp->default_vat_code is not overwritten by $objp2 value
$this->constructProductListOption($objp, $opt, $optJson, 0, $selected, $hidepriceinlabel, $filterkey); $this->constructProductListOption($objp, $opt, $optJson, 0, $selected, $hidepriceinlabel, $filterkey);
$j++; $j++;
@ -2709,8 +2712,9 @@ class Form
} }
/** /**
* constructProductListOption. * Function to forge the string with OPTIONs of SELECT.
* This define value for &$opt and &$optJson. * This define value for &$opt and &$optJson.
* This function is called by select_produits_list().
* *
* @param resource $objp Resultset of fetch * @param resource $objp Resultset of fetch
* @param string $opt Option (var used for returned value in string option format) * @param string $opt Option (var used for returned value in string option format)
@ -2740,6 +2744,7 @@ class Form
$outprice_ttc = ''; $outprice_ttc = '';
$outpricebasetype = ''; $outpricebasetype = '';
$outtva_tx = ''; $outtva_tx = '';
$outdefault_vat_code = '';
$outqty = 1; $outqty = 1;
$outdiscount = 0; $outdiscount = 0;
@ -2866,9 +2871,9 @@ class Form
$found = 0; $found = 0;
// Multiprice // Multiprice
// If we need a particular price level (from 1 to 6) // If we need a particular price level (from 1 to n)
if (empty($hidepriceinlabel) && $price_level >= 1 && (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))) { if (empty($hidepriceinlabel) && $price_level >= 1 && (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))) {
$sql = "SELECT price, price_ttc, price_base_type, tva_tx"; $sql = "SELECT price, price_ttc, price_base_type, tva_tx, default_vat_code";
$sql .= " FROM ".MAIN_DB_PREFIX."product_price"; $sql .= " FROM ".MAIN_DB_PREFIX."product_price";
$sql .= " WHERE fk_product = ".((int) $objp->rowid); $sql .= " WHERE fk_product = ".((int) $objp->rowid);
$sql .= " AND entity IN (".getEntity('productprice').")"; $sql .= " AND entity IN (".getEntity('productprice').")";
@ -2892,7 +2897,13 @@ class Form
$outprice_ht = price($objp2->price); $outprice_ht = price($objp2->price);
$outprice_ttc = price($objp2->price_ttc); $outprice_ttc = price($objp2->price_ttc);
$outpricebasetype = $objp2->price_base_type; $outpricebasetype = $objp2->price_base_type;
$outtva_tx = $objp2->tva_tx; if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) { // using this option is a bug. kept for backward compatibility
$outtva_tx = $objp2->tva_tx; // We use the vat rate on line of multiprice
$outdefault_vat_code = $objp2->default_vat_code; // We use the vat code on line of multiprice
} else {
$outtva_tx = $objp->tva_tx; // We use the vat rate of product, not the one on line of multiprice
$outdefault_vat_code = $objp->default_vat_code; // We use the vat code or product, not the one on line of multiprice
}
} }
} else { } else {
dol_print_error($this->db); dol_print_error($this->db);
@ -2919,7 +2930,8 @@ class Form
$outprice_ht = price($objp->unitprice); $outprice_ht = price($objp->unitprice);
$outprice_ttc = price($objp->unitprice * (1 + ($objp->tva_tx / 100))); $outprice_ttc = price($objp->unitprice * (1 + ($objp->tva_tx / 100)));
$outpricebasetype = $objp->price_base_type; $outpricebasetype = $objp->price_base_type;
$outtva_tx = $objp->tva_tx; $outtva_tx = $objp->tva_tx; // This value is the value on product when constructProductListOption is called by select_produits_list even if other field $objp-> are from table price_by_qty
$outdefault_vat_code = $objp->default_vat_code; // This value is the value on product when constructProductListOption is called by select_produits_list even if other field $objp-> are from table price_by_qty
} }
if (empty($hidepriceinlabel) && !empty($objp->quantity) && $objp->quantity >= 1) { if (empty($hidepriceinlabel) && !empty($objp->quantity) && $objp->quantity >= 1) {
$opt .= " (".price($objp->unitprice, 1, $langs, 0, 0, -1, $conf->currency)."/".$langs->trans("Unit").")"; // Do not use strtolower because it breaks utf8 encoding $opt .= " (".price($objp->unitprice, 1, $langs, 0, 0, -1, $conf->currency)."/".$langs->trans("Unit").")"; // Do not use strtolower because it breaks utf8 encoding
@ -2947,6 +2959,7 @@ class Form
$outprice_ttc = price($objp->custprice_ttc); $outprice_ttc = price($objp->custprice_ttc);
$outpricebasetype = $objp->custprice_base_type; $outpricebasetype = $objp->custprice_base_type;
$outtva_tx = $objp->custtva_tx; $outtva_tx = $objp->custtva_tx;
$outdefault_vat_code = $objp->custdefault_vat_code;
} }
} }
@ -2963,6 +2976,7 @@ class Form
$outprice_ttc = price($objp->price_ttc); $outprice_ttc = price($objp->price_ttc);
$outpricebasetype = $objp->price_base_type; $outpricebasetype = $objp->price_base_type;
$outtva_tx = $objp->tva_tx; $outtva_tx = $objp->tva_tx;
$outdefault_vat_code = $objp->default_vat_code;
} }
if (!empty($conf->stock->enabled) && isset($objp->stock) && ($objp->fk_product_type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES))) { if (!empty($conf->stock->enabled) && isset($objp->stock) && ($objp->fk_product_type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES))) {
@ -3011,7 +3025,9 @@ class Form
'price_ht'=>price2num($outprice_ht), 'price_ht'=>price2num($outprice_ht),
'price_ttc'=>price2num($outprice_ttc), 'price_ttc'=>price2num($outprice_ttc),
'pricebasetype'=>$outpricebasetype, 'pricebasetype'=>$outpricebasetype,
'tva_tx'=>$outtva_tx, 'qty'=>$outqty, 'tva_tx'=>$outtva_tx,
'default_vat_code'=>$outdefault_vat_code,
'qty'=>$outqty,
'discount'=>$outdiscount, 'discount'=>$outdiscount,
'duration_value'=>$outdurationvalue, 'duration_value'=>$outdurationvalue,
'duration_unit'=>$outdurationunit, 'duration_unit'=>$outdurationunit,

View File

@ -89,6 +89,11 @@ if (!empty($action) && $action == 'fetch' && !empty($id)) {
$outdesc = $object->description; $outdesc = $object->description;
$outdesc_trans = ''; $outdesc_trans = '';
$outtype = $object->type; $outtype = $object->type;
$outprice_ht = null;
$outprice_ttc = null;
$outpricebasetype = null;
$outtva_tx = 0;
$outdefault_vat_code = '';
$outqty = 1; $outqty = 1;
$outdiscount = 0; $outdiscount = 0;
$mandatory_period = $object->mandatory_period; $mandatory_period = $object->mandatory_period;
@ -132,17 +137,21 @@ if (!empty($action) && $action == 'fetch' && !empty($id)) {
$found = true; $found = true;
$outprice_ht = price($objp->unitprice); $outprice_ht = price($objp->unitprice);
$outprice_ttc = price($objp->unitprice * (1 + ($object->tva_tx / 100))); $outprice_ttc = price($objp->unitprice * (1 + ($object->tva_tx / 100)));
$outpricebasetype = $object->price_base_type; $outpricebasetype = $object->price_base_type;
$outtva_tx = $object->tva_tx; $outtva_tx = $object->tva_tx;
$outdefault_vat_code = $object->default_vat_code;
$outqty = $objp->quantity; $outqty = $objp->quantity;
$outdiscount = $objp->remise_percent; $outdiscount = $objp->remise_percent;
} }
} }
} }
// Multiprice // Multiprice (1 price per level)
if (!$found && isset($price_level) && $price_level >= 1 && (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))) { // If we need a particular price level (from 1 to 6) if (!$found && isset($price_level) && $price_level >= 1 && (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))) { // If we need a particular price level (from 1 to 6)
$sql = "SELECT price, price_ttc, price_base_type, tva_tx"; $sql = "SELECT price, price_ttc, price_base_type,";
$sql .= " tva_tx, default_vat_code"; // Vat rate and code will be used if PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL is on.
$sql .= " FROM ".MAIN_DB_PREFIX."product_price "; $sql .= " FROM ".MAIN_DB_PREFIX."product_price ";
$sql .= " WHERE fk_product = ".((int) $id); $sql .= " WHERE fk_product = ".((int) $id);
$sql .= " AND entity IN (".getEntity('productprice').")"; $sql .= " AND entity IN (".getEntity('productprice').")";
@ -158,7 +167,14 @@ if (!empty($action) && $action == 'fetch' && !empty($id)) {
$outprice_ht = price($objp->price); $outprice_ht = price($objp->price);
$outprice_ttc = price($objp->price_ttc); $outprice_ttc = price($objp->price_ttc);
$outpricebasetype = $objp->price_base_type; $outpricebasetype = $objp->price_base_type;
if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
$outtva_tx = $objp->tva_tx; $outtva_tx = $objp->tva_tx;
$outdefault_vat_code = $objp->default_vat_code;
} else {
// The common and default behaviour.
$outtva_tx = $object->tva_tx;
$outdefault_vat_code = $object->default_vat_code;
}
} }
} }
} }
@ -179,6 +195,7 @@ if (!empty($action) && $action == 'fetch' && !empty($id)) {
$outprice_ttc = price($prodcustprice->lines[0]->price_ttc); $outprice_ttc = price($prodcustprice->lines[0]->price_ttc);
$outpricebasetype = $prodcustprice->lines[0]->price_base_type; $outpricebasetype = $prodcustprice->lines[0]->price_base_type;
$outtva_tx = $prodcustprice->lines[0]->tva_tx; $outtva_tx = $prodcustprice->lines[0]->tva_tx;
$outdefault_vat_code = $prodcustprice->lines[0]->default_vat_code;
} }
} }
} }
@ -188,6 +205,7 @@ if (!empty($action) && $action == 'fetch' && !empty($id)) {
$outprice_ttc = price($object->price_ttc); $outprice_ttc = price($object->price_ttc);
$outpricebasetype = $object->price_base_type; $outpricebasetype = $object->price_base_type;
$outtva_tx = $object->tva_tx; $outtva_tx = $object->tva_tx;
$outdefault_vat_code = $object->default_vat_code;
} }
$outjson = array( $outjson = array(
@ -201,6 +219,7 @@ if (!empty($action) && $action == 'fetch' && !empty($id)) {
'price_ttc' => $outprice_ttc, 'price_ttc' => $outprice_ttc,
'pricebasetype' => $outpricebasetype, 'pricebasetype' => $outpricebasetype,
'tva_tx' => $outtva_tx, 'tva_tx' => $outtva_tx,
'default_vat_code' => $outdefault_vat_code,
'qty' => $outqty, 'qty' => $outqty,
'discount' => $outdiscount, 'discount' => $outdiscount,
'mandatory_period' => $mandatory_period, 'mandatory_period' => $mandatory_period,

View File

@ -392,9 +392,9 @@ class Productcustomerprice extends CommonObject
$sql .= " t.import_key,"; $sql .= " t.import_key,";
$sql .= " soc.nom as socname,"; $sql .= " soc.nom as socname,";
$sql .= " prod.ref as prodref"; $sql .= " prod.ref as prodref";
$sql .= " FROM ".MAIN_DB_PREFIX."product_customer_price as t "; $sql .= " FROM ".MAIN_DB_PREFIX."product_customer_price as t,";
$sql .= " ,".MAIN_DB_PREFIX."product as prod "; $sql .= " ".MAIN_DB_PREFIX."product as prod,";
$sql .= " ,".MAIN_DB_PREFIX."societe as soc "; $sql .= " ".MAIN_DB_PREFIX."societe as soc";
$sql .= " WHERE soc.rowid=t.fk_soc "; $sql .= " WHERE soc.rowid=t.fk_soc ";
$sql .= " AND prod.rowid=t.fk_product "; $sql .= " AND prod.rowid=t.fk_product ";
$sql .= " AND prod.entity IN (".getEntity('product').")"; $sql .= " AND prod.entity IN (".getEntity('product').")";

View File

@ -127,7 +127,7 @@ if (empty($reshook)) {
$reg = array(); $reg = array();
$vatratecode = ''; $vatratecode = '';
if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg)) { if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg)) {
$vat_src_code = $reg[1]; $vatratecode = $reg[1];
$tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate. $tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
} }
@ -172,6 +172,8 @@ if (empty($reshook)) {
} }
if ($error) { if ($error) {
// Force the update of the price of the product to 0 if error
//$localtaxarray=array('0'=>$localtax1_type,'1'=>$localtax1,'2'=>$localtax2_type,'3'=>$localtax2); //$localtaxarray=array('0'=>$localtax1_type,'1'=>$localtax1,'2'=>$localtax2_type,'3'=>$localtax2);
$localtaxarray = array(); // We do not store localtaxes into product, we will use instead the "vat code" to retrieve them. $localtaxarray = array(); // We do not store localtaxes into product, we will use instead the "vat code" to retrieve them.
$object->updatePrice(0, $object->price_base_type, $user, $tva_tx, '', 0, $npr, 0, 0, $localtaxarray, $vatratecode); $object->updatePrice(0, $object->price_base_type, $user, $tva_tx, '', 0, $npr, 0, 0, $localtaxarray, $vatratecode);