From e37ca000f3776918b58a4e65528839fb89417cae Mon Sep 17 00:00:00 2001 From: lvessiller-opendsi Date: Sat, 10 Feb 2024 01:09:24 +0100 Subject: [PATCH] NEW determine multi-currency price on object line create tpl (#28021) --- htdocs/core/class/html.form.class.php | 65 ++++++++++++++--------- htdocs/core/lib/ajax.lib.php | 17 +++++- htdocs/core/tpl/objectline_create.tpl.php | 34 ++++++++++-- 3 files changed, 85 insertions(+), 31 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index e262eb163c9d7..a76655ff31096 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -3381,9 +3381,12 @@ public function select_produits_fournisseurs_list($socid, $selected = '', $htmln } $sql = "SELECT p.rowid, p.ref, p.label, p.price, p.duration, p.fk_product_type, p.stock, p.tva_tx as tva_tx_sale, p.default_vat_code as default_vat_code_sale,"; - $sql .= " pfp.ref_fourn, pfp.rowid as idprodfournprice, pfp.price as fprice, pfp.quantity, pfp.remise_percent, pfp.remise, pfp.unitprice,"; - $sql .= " pfp.fk_supplier_price_expression, pfp.fk_product, pfp.tva_tx, pfp.default_vat_code, pfp.fk_soc, s.nom as name,"; - $sql .= " pfp.supplier_reputation"; + $sql .= " pfp.ref_fourn, pfp.rowid as idprodfournprice, pfp.price as fprice, pfp.quantity, pfp.remise_percent, pfp.remise, pfp.unitprice"; + if (isModEnabled('multicurrency')) { + $sql .= ", pfp.multicurrency_code, pfp.multicurrency_unitprice"; + } + $sql .= ", pfp.fk_supplier_price_expression, pfp.fk_product, pfp.tva_tx, pfp.default_vat_code, pfp.fk_soc, s.nom as name"; + $sql .= ", pfp.supplier_reputation"; // if we use supplier description of the products if (!empty($conf->global->PRODUIT_FOURN_TEXTS)) { $sql .= ", pfp.desc_fourn as description"; @@ -3689,6 +3692,10 @@ public function select_produits_fournisseurs_list($socid, $selected = '', $htmln $optstart .= ' data-tvatx-formated="' . dol_escape_htmltag(price($objp->tva_tx, 0, $langs, 1, -1, 2)) . '"'; $optstart .= ' data-default-vat-code="' . dol_escape_htmltag($objp->default_vat_code) . '"'; $optstart .= ' data-supplier-ref="' . dol_escape_htmltag($objp->ref_fourn) . '"'; + if (isModEnabled('multicurrency')) { + $optstart .= ' data-multicurrency-code="' . dol_escape_htmltag($objp->multicurrency_code) . '"'; + $optstart .= ' data-multicurrency-up="' . dol_escape_htmltag($objp->multicurrency_unitprice) . '"'; + } } $optstart .= ' data-description="' . dol_escape_htmltag($objp->description, 0, 1) . '"'; @@ -3710,6 +3717,10 @@ public function select_produits_fournisseurs_list($socid, $selected = '', $htmln 'disabled' => (empty($objp->idprodfournprice) ? true : false), 'description' => $objp->description ); + if (isModEnabled('multicurrency')) { + $outarrayentry['multicurrency_code'] = $objp->multicurrency_code; + $outarrayentry['multicurrency_unitprice'] = price2num($objp->multicurrency_unitprice, 'MU'); + } $parameters = array( 'objp' => &$objp, @@ -3725,29 +3736,33 @@ public function select_produits_fournisseurs_list($socid, $selected = '', $htmln // "key" value of json key array is used by jQuery automatically as selected value. Example: 'type' = product or service, 'price_ht' = unit price without tax // "label" value of json key array is used by jQuery automatically as text for combo box $out .= $optstart . ' data-html="' . dol_escape_htmltag($optlabel) . '">' . $optlabel . "\n"; - array_push( - $outarray, - array('key' => $outkey, - 'value' => $outref, - 'label' => $outvallabel, - 'qty' => $outqty, - 'price_qty_ht' => price2num($objp->fprice, 'MU'), // Keep higher resolution for price for the min qty - 'price_qty_ht_locale' => price($objp->fprice), - 'price_unit_ht' => price2num($objp->unitprice, 'MU'), // This is used to fill the Unit Price - 'price_unit_ht_locale' => price($objp->unitprice), - 'price_ht' => price2num($objp->unitprice, 'MU'), // This is used to fill the Unit Price (for compatibility) - 'tva_tx_formated' => price($objp->tva_tx), - 'tva_tx' => price2num($objp->tva_tx), - 'default_vat_code' => $objp->default_vat_code, - 'discount' => $outdiscount, - 'type' => $outtype, - 'duration_value' => $outdurationvalue, - 'duration_unit' => $outdurationunit, - 'disabled' => (empty($objp->idprodfournprice) ? true : false), - 'description' => $objp->description - ) + $outarraypush = array( + 'key' => $outkey, + 'value' => $outref, + 'label' => $outvallabel, + 'qty' => $outqty, + 'price_qty_ht' => price2num($objp->fprice, 'MU'), // Keep higher resolution for price for the min qty + 'price_qty_ht_locale' => price($objp->fprice), + 'price_unit_ht' => price2num($objp->unitprice, 'MU'), // This is used to fill the Unit Price + 'price_unit_ht_locale' => price($objp->unitprice), + 'price_ht' => price2num($objp->unitprice, 'MU'), // This is used to fill the Unit Price (for compatibility) + 'tva_tx_formated' => price($objp->tva_tx), + 'tva_tx' => price2num($objp->tva_tx), + 'default_vat_code' => $objp->default_vat_code, + 'discount' => $outdiscount, + 'type' => $outtype, + 'duration_value' => $outdurationvalue, + 'duration_unit' => $outdurationunit, + 'disabled' => (empty($objp->idprodfournprice) ? true : false), + 'description' => $objp->description ); - // Exemple of var_dump $outarray + if (isModEnabled('multicurrency')) { + $outarraypush['multicurrency_code'] = $objp->multicurrency_code; + $outarraypush['multicurrency_unitprice'] = price2num($objp->multicurrency_unitprice, 'MU'); + } + array_push($outarray, $outarraypush); + + // Example of var_dump $outarray // array(1) {[0]=>array(6) {[key"]=>string(1) "2" ["value"]=>string(3) "ppp" // ["label"]=>string(76) "ppp (fff2) - ppp - 20,00 Euros/1unité (20,00 Euros/unité)" // ["qty"]=>string(1) "1" ["discount"]=>string(1) "0" ["disabled"]=>bool(false) diff --git a/htdocs/core/lib/ajax.lib.php b/htdocs/core/lib/ajax.lib.php index 34ca110dd1eb2..67f64a766617c 100644 --- a/htdocs/core/lib/ajax.lib.php +++ b/htdocs/core/lib/ajax.lib.php @@ -163,6 +163,14 @@ function ajax_autocompleter($selected, $htmlname, $url, $urloption = '', $minLen price_ttc: item.price_ttc, price_unit_ht: item.price_unit_ht, price_unit_ht_locale: item.price_unit_ht_locale, + '; + if (isModEnabled('multicurrency')) { + $script .= ' + multicurrency_code: item.multicurrency_code, + multicurrency_unitprice: item.multicurrency_unitprice, + '; + } + $script .= ' description : item.description, ref_customer: item.ref_customer, tva_tx: item.tva_tx, @@ -194,7 +202,14 @@ function ajax_autocompleter($selected, $htmlname, $url, $urloption = '', $minLen $("#'.$htmlnamejquery.'").attr("data-tvatx", ui.item.tva_tx); $("#'.$htmlnamejquery.'").attr("data-default-vat-code", ui.item.default_vat_code); '; - if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) { + if (isModEnabled('multicurrency')) { + $script .= ' + // For multi-currency values + $("#'.$htmlnamejquery.'").attr("data-multicurrency-code", ui.item.multicurrency_code); + $("#'.$htmlnamejquery.'").attr("data-multicurrency-unitprice", ui.item.multicurrency_unitprice); + '; + } + if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) { $script .= ' // For customer price when PRODUIT_CUSTOMER_PRICES_BY_QTY is on console.log("PRODUIT_CUSTOMER_PRICES_BY_QTY is on, propagate also prices by quantity into data-pbqxxx properties"); diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php index 42377634ff942..7a5f36af59299 100644 --- a/htdocs/core/tpl/objectline_create.tpl.php +++ b/htdocs/core/tpl/objectline_create.tpl.php @@ -1035,12 +1035,36 @@ function(data) { var supplier_ref = $('option:selected', this).attr('data-supplier-ref'); // When select is done from HTML select if (typeof supplier_ref === 'undefined') { supplier_ref = jQuery('#idprodfournprice').attr('data-supplier-ref');} // When select is done from HTML input with ajax autocomplete - console.log("objectline_create.tpl We find supplier price : up = "+up+", up_locale = "+up_locale+", supplier_ref = "+supplier_ref+" qty = "+qty+", tva_tx = "+tva_tx+", default_vat_code = "+default_vat_code+", stringforvatrateselection="+stringforvatrateselection+", discount = "+discount+" for product supplier ref id = "+jQuery('#idprodfournprice').val()); + var has_multicurrency_up = false; + multicurrency_code != $conf->currency) { + ?> + var object_multicurrency_code = 'multicurrency_code); ?>'; - if (typeof up_locale === 'undefined') { - jQuery("#price_ht").val(up); - } else { - jQuery("#price_ht").val(up_locale); + var multicurrency_code = $('option:selected', this).attr('data-multicurrency-code'); // When select is done from HTML select + if (multicurrency_code == undefined) { multicurrency_code = jQuery('#idprodfournprice').attr('data-multicurrency-code'); } // When select is done from HTML input with ajax autocomplete + + var multicurrency_up = parseFloat($('option:selected', this).attr('data-multicurrency-unitprice')); // When select is done from HTML select + if (isNaN(multicurrency_up)) { multicurrency_up = parseFloat(jQuery('#idprodfournprice').attr('data-multicurrency-unitprice')); } // When select is done from HTML input with ajax autocomplete + + if (multicurrency_code == object_multicurrency_code) { + has_multicurrency_up = true; + jQuery("#multicurrency_price_ht").val(multicurrency_up); + } + + console.log("objectline_create.tpl Multicurrency values : object_multicurrency_code = "+object_multicurrency_code+", multicurrency_code = "+multicurrency_code+", multicurrency_up = "+multicurrency_up); + + + console.log("objectline_create.tpl We find supplier price : up = "+up+", up_locale = "+up_locale+", has_multicurrency_up = "+has_multicurrency_up+", supplier_ref = "+supplier_ref+" qty = "+qty+", tva_tx = "+tva_tx+", default_vat_code = "+default_vat_code+", stringforvatrateselection="+stringforvatrateselection+", discount = "+discount+" for product supplier ref id = "+jQuery('#idprodfournprice').val()); + + if (has_multicurrency_up === false) { + if (typeof up_locale === 'undefined') { + jQuery("#price_ht").val(up); + } else { + jQuery("#price_ht").val(up_locale); + } } // Set supplier_ref