Skip to content

Commit

Permalink
Merge pull request #1190 from altairis-fr/develop
Browse files Browse the repository at this point in the history
add ability to modify margin rate on line edit if authorized
  • Loading branch information
Juanjo Menent committed Aug 23, 2013
2 parents 10ced74 + 4df216a commit 376b281
Show file tree
Hide file tree
Showing 2 changed files with 260 additions and 43 deletions.
186 changes: 149 additions & 37 deletions htdocs/core/tpl/objectline_add.tpl.php
Expand Up @@ -41,17 +41,31 @@
?>
<td align="right"><?php echo $langs->trans('BuyingPrice'); ?></td>
<?php
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
$colspan++;
$colspan2++;
}
if (! empty($conf->global->DISPLAY_MARK_RATES)) {
$colspan++;
$colspan2++;
}
if (! empty($conf->global->MAIN_VIEW_LINE_NUMBER)) {
$colspan2++;
}
if ($user->rights->margins->creer)
{
if(! empty($conf->global->DISPLAY_MARGIN_RATES))
{
echo '<td align="right">'.$langs->trans('MarginRate').'</td>';
}
if(! empty($conf->global->DISPLAY_MARK_RATES))
{
echo '<td align="right">'.$langs->trans('MarkRate').'</td>';
}
}
else
{
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
$colspan++;
$colspan2++;
}
if (! empty($conf->global->DISPLAY_MARK_RATES)) {
$colspan++;
$colspan2++;
}
}
if (! empty($conf->global->MAIN_VIEW_LINE_NUMBER)) {
$colspan2++;
}
}
?>
<td colspan="<?php echo $colspan; ?>">&nbsp;</td>
Expand Down Expand Up @@ -164,21 +178,35 @@
<input type="text" size="1" value="<?php echo $buyer->remise_percent; ?>" id="remise_percent" name="remise_percent">%
<input type="hidden" id="origin_remise_percent" name="origin_remise_percent" value="<?php echo $buyer->remise_percent; ?>" />
</td>
<?php
$colspan = 4;
if (! empty($conf->margin->enabled)) {
?>
<td align="right">
<select id="fournprice" name="fournprice" style="display: none;"></select>
<input type="text" size="5" id="buying_price" name="buying_price" value="<?php echo (GETPOST('buying_price')?GETPOST('buying_price'):''); ?>">
</td>
<?php
if (! empty($conf->global->DISPLAY_MARGIN_RATES))
$colspan++;
if (! empty($conf->global->DISPLAY_MARK_RATES))
$colspan++;
}
?>
<?php
$colspan = 4;
if (! empty($conf->margin->enabled)) {
?>
<td align="right">
<select id="fournprice" name="fournprice" style="display: none;"></select>
<input type="text" size="5" id="buying_price" name="buying_price" value="<?php echo (GETPOST('buying_price')?GETPOST('buying_price'):''); ?>">
</td>
<?php
if ($user->rights->margins->creer)
{
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
echo '<td align="right"><input type="text" size="2" name="np_marginRate" value="'.(isset($_POST["np_marginRate"])?$_POST["np_marginRate"]:'').'">%</td>';
}
elseif (! empty($conf->global->DISPLAY_MARK_RATES)) {
echo '<td align="right"><input type="text" size="2" name="np_markRate" value="'.(isset($_POST["np_markRate"])?$_POST["np_markRate"]:'').'">%</td>';
}
}
else
{
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
$colspan++;
}
if (! empty($conf->global->DISPLAY_MARK_RATES)) {
$colspan++;
}
}
}
?>
<td align="center" valign="middle" colspan="<?php echo $colspan; ?>"><input type="submit" class="button" id="addlinebutton" name="addline" value="<?php echo $langs->trans('Add'); ?>"></td>
</tr>

Expand All @@ -188,12 +216,7 @@
$colspan = 12;
else
$colspan = 11;
if (! empty($conf->margin->enabled)) {
if (! empty($conf->global->DISPLAY_MARGIN_RATES))
$colspan++;
if (! empty($conf->global->DISPLAY_MARK_RATES))
$colspan++;
}

?>
<tr id="service_duration_area" <?php echo $bcnd[$var]; ?>>
<td colspan="<?php echo $colspan; ?>">
Expand Down Expand Up @@ -491,11 +514,7 @@ function(data) {
});
<?php } ?>

});
</script>

<?php if (! empty($conf->margin->enabled)) { ?>
<script type="text/javascript">
$("#idprod").change(function() {
$("#fournprice").empty();
$("#buying_price").show();
Expand Down Expand Up @@ -536,6 +555,99 @@ function(data) {
$('#buying_price').val('');
}
});
</script>
var npRate = null;
<?php
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) { ?>
npRate = "np_marginRate";
<?php }
elseif (! empty($conf->global->DISPLAY_MARK_RATES)) { ?>
npRate = "np_markRate";
<?php }
?>

$("form#addproduct").submit(function(e) {
if (npRate) return checkFreeLine(e, npRate);
else return true;
});
if (npRate == 'np_marginRate') {
$("input[name='np_marginRate']:first").blur(function(e) {
return checkFreeLine(e, npRate);
});
}
else {
if (npRate == 'np_markRate') {
$("input[name='np_markRate']:first").blur(function(e) {
return checkFreeLine(e, npRate);
});
}
}

function checkFreeLine(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('<?php echo $langs->trans("rateMustBeNumeric"); ?>');
e.stopPropagation();
setTimeout(function () { rate.focus() }, 50);
return false;
}
if (npRate == "np_markRate" && rate.val() >= 100)
{
alert('<?php echo $langs->trans("markRateShouldBeLesserThan100"); ?>');
e.stopPropagation();
setTimeout(function () { rate.focus() }, 50);
return false;
}

var np_price = 0;
if (remise.val().replace(',','.') != 100)
{
if (npRate == "np_marginRate")
np_price = ((buying_price.val().replace(',','.') * (1 + rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100));
else {
if (npRate == "np_markRate")
np_price = ((buying_price.val().replace(',','.') / (1 - rate.val().replace(',','.') / 100)) / (1 - remise.val().replace(',','.') / 100));
}
}
$("input[name='price_ht']:first").val(formatFloat(np_price));
update_price('price_ht', 'price_ttc');

return true;
}
function roundFloat(num) {
var main_max_dec_shown = <?php echo $conf->global->MAIN_MAX_DECIMALS_SHOWN; ?>;
var main_rounding = <?php echo min($conf->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('.', ',');
}
<?php } ?>
});
</script>

<!-- END PHP TEMPLATE objectline_add.tpl.php -->
117 changes: 111 additions & 6 deletions htdocs/core/tpl/objectline_edit.tpl.php
Expand Up @@ -28,7 +28,7 @@
?>

<!-- BEGIN PHP TEMPLATE objectline_edit.tpl.php -->
<form action="<?php echo $_SERVER["PHP_SELF"].'?id='.$this->id.'#'.$line->id; ?>" method="POST">
<form id="editproduct" action="<?php echo $_SERVER["PHP_SELF"].'?id='.$this->id.'#'.$line->id; ?>" method="POST">
<input type="hidden" name="token" value="<?php echo $_SESSION['newtoken']; ?>">
<input type="hidden" name="action" value="updateligne">
<input type="hidden" name="usenewupdatelineform" value="1" />
Expand Down Expand Up @@ -133,11 +133,25 @@
</td>

<?php if (! empty($conf->margin->enabled)) { ?>
<td align="right"><?php $coldisplay++; ?>
<select id="fournprice" name="fournprice" class="hideobject"></select>
<input type="text" size="5" id="buying_price" name="buying_price" class="hideobject" value="<?php echo price($line->pa_ht,0,'',0); ?>">
</td>
<?php } ?>
<td align="right"><?php $coldisplay++; ?>
<select id="fournprice" name="fournprice" class="hideobject"></select>
<input type="text" size="5" id="buying_price" name="buying_price" class="hideobject" value="<?php echo price($line->pa_ht,0,'',0); ?>">
</td>
<?php if ($user->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 '<td align="right"><input type="text" size="2" name="marginRate" value="'.$margin_rate.'">%</td>';
$coldisplay++;
}
elseif (! empty($conf->global->DISPLAY_MARK_RATES))
{
$mark_rate = (isset($_POST["markRate"])?$_POST["markRate"]:price($line->marque_tx));
echo '<td align="right"><input type="text" size="2" name="markRate" value="'.$mark_rate.'">%</td>';
$coldisplay++;
}
}
} ?>

<td align="center" colspan="5" valign="middle">
<input type="submit" class="button" id="savelinebutton" name="save" value="<?php echo $langs->trans("Save"); ?>"><br>
Expand Down Expand Up @@ -386,5 +400,96 @@ function(data) {
}, 'json');
<?php } ?>
});
var npRate = null;
<?php
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) { ?>
npRate = "marginRate";
<?php }
elseif (! empty($conf->global->DISPLAY_MARK_RATES)) { ?>
npRate = "markRate";
<?php }
?>

$("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('<?php echo $langs->trans("rateMustBeNumeric"); ?>');
e.stopPropagation();
setTimeout(function () { rate.focus() }, 50);
return false;
}
if (npRate == "markRate" && rate.val() >= 100)
{
alert('<?php echo $langs->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 = <?php echo $conf->global->MAIN_MAX_DECIMALS_SHOWN; ?>;
var main_rounding = <?php echo min($conf->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('.', ',');
}

</script>
<!-- END PHP TEMPLATE objectline_edit.tpl.php -->

0 comments on commit 376b281

Please sign in to comment.