Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retroport: FIX determine multi-currency price on object line create tpl (#28021) #30535

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions htdocs/core/class/html.form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3381,9 +3381,10 @@ 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";
$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";
Expand Down Expand Up @@ -3689,6 +3690,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) . '"';

Expand All @@ -3710,6 +3715,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,
Expand All @@ -3725,29 +3734,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 . "</option>\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 (<strong>f</strong>ff2) - ppp - 20,00 Euros/1unité (20,00 Euros/unité)"
// ["qty"]=>string(1) "1" ["discount"]=>string(1) "0" ["disabled"]=>bool(false)
Expand Down
17 changes: 16 additions & 1 deletion htdocs/core/lib/ajax.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
34 changes: 29 additions & 5 deletions htdocs/core/tpl/objectline_create.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
<?php
if (isModEnabled('multicurrency') && $object->multicurrency_code != $conf->currency) {
?>
var object_multicurrency_code = '<?php print dol_escape_js($object->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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why to use here "== undefined" and not "typeof multicurrency_code === 'undefined'" like in other parts of this JS script ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only because it's a back port of an existing PR. Maybe this change should also be done in develop?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think.
@eldy What is your advice ?

Copy link
Member

@eldy eldy Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are in a v18 submission, and it is a backport, code must be the much as possible similar than original,even if code is not as "clean" as we can.


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);
<?php
}
?>

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
Expand Down
Loading