From e672783adba4afa3d598bd267a0f113ef0ec1df3 Mon Sep 17 00:00:00 2001 From: Christophe Battarel Date: Thu, 22 Aug 2013 11:39:55 +0200 Subject: [PATCH] add ability to modify margin rate on line edit if authorized --- htdocs/core/tpl/objectline_edit.tpl.php | 117 ++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 6 deletions(-) diff --git a/htdocs/core/tpl/objectline_edit.tpl.php b/htdocs/core/tpl/objectline_edit.tpl.php index 328964bc0df..a7a744be7ee 100644 --- a/htdocs/core/tpl/objectline_edit.tpl.php +++ b/htdocs/core/tpl/objectline_edit.tpl.php @@ -28,7 +28,7 @@ ?> -
id; ?>" method="POST"> +id; ?>" method="POST"> @@ -133,11 +133,25 @@ if (! empty($conf->global->MAIN_VIEW_LINE_NUMBER)) { margin->enabled)) { ?> - - - - - + + + + + rights->margins->creer) { + if (! empty($conf->global->DISPLAY_MARGIN_RATES)) + { + $margin_rate = (isset($_POST["marginRate"])?$_POST["marginRate"]:(($line->pa_ht == 0)?'':price($line->marge_tx))); + echo '%'; + $coldisplay++; + } + if (! empty($conf->global->DISPLAY_MARK_RATES)) + { + $mark_rate = (isset($_POST["markRate"])?$_POST["markRate"]:price($line->marque_tx)); + echo '%'; + $coldisplay++; + } + } + } ?> ">
@@ -386,5 +400,96 @@ $(document).ready(function() { }, 'json'); }); + var npRate = null; +global->DISPLAY_MARGIN_RATES)) { ?> + npRate = "marginRate"; + global->DISPLAY_MARK_RATES)) { ?> + npRate = "markRate"; + + +$("form#editproduct").submit(function(e) { + if (npRate) return checkEditLine(e, npRate); + else return true; +}); +if (npRate == 'marginRate') { + $("input[name='marginRate']:first").blur(function(e) { + return checkEditLine(e, npRate); + }); +} +else { + if (npRate == 'markRate') { + $("input[name='markRate']:first").blur(function(e) { + return checkEditLine(e, npRate); + }); + } +} + +function checkEditLine(e, npRate) +{ + var buying_price = $("input[name='buying_price']:first"); + var remise = $("input[name='remise_percent']:first"); + + var rate = $("input[name='"+npRate+"']:first"); + if (rate.val() == '') + return true; + if (! $.isNumeric(rate.val().replace(',','.'))) + { + alert('trans("rateMustBeNumeric"); ?>'); + e.stopPropagation(); + setTimeout(function () { rate.focus() }, 50); + return false; + } + if (npRate == "markRate" && rate.val() >= 100) + { + alert('trans("markRateShouldBeLesserThan100"); ?>'); + e.stopPropagation(); + setTimeout(function () { rate.focus() }, 50); + return false; + } + + var price = 0; + if (remise.val().replace(',','.') != 100) + { + if (npRate == "marginRate") + price = ((buying_price.val().replace(',','.') * (1 + rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100)); + else { + if (npRate == "markRate") + price = ((buying_price.val().replace(',','.') / (1 - rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100)); + } + } + $("input[name='price_ht']:first").val(formatFloat(price)); + + return true; +} +function roundFloat(num) { + var main_max_dec_shown = global->MAIN_MAX_DECIMALS_SHOWN; ?>; + var main_rounding = global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT); ?>; + + var amount = num.toString().replace(',','.'); // should be useless + var nbdec = 0; + var rounding = main_rounding; + var pos = amount.indexOf('.'); + var decpart = ''; + if (pos >= 0) + decpart = amount.substr(pos+1).replace('/0+$/i',''); // Supprime les 0 de fin de partie decimale + nbdec = decpart.length; + if (nbdec > rounding) + rounding = nbdec; + // Si on depasse max + if (rounding > main_max_dec_shown) + { + rounding = main_max_dec_shown; + } + //amount = parseFloat(amount) + (1 / Math.pow(100, rounding)); // to avoid floating-point errors + return parseFloat(amount).toFixed(rounding); +} + +function formatFloat(num) { + return roundFloat(num).replace('.', ','); +} +