Merge pull request #1179 from altatof/saisie_marges

ability to define margin rate on quotes, orders, invoices
This commit is contained in:
Juanjo Menent 2013-08-14 01:00:29 -07:00
commit 8ba3410766
5 changed files with 246 additions and 24 deletions

View File

@ -7,7 +7,7 @@
* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr> * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
* Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2010-2011 Philippe Grand <philippe.grand@atoo-net.com> * Copyright (C) 2010-2011 Philippe Grand <philippe.grand@atoo-net.com>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr> * Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro> * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -724,9 +724,16 @@ else if ($action == "addline" && $user->rights->propal->creer)
$price_base_type = $prod->price_base_type; $price_base_type = $prod->price_base_type;
} }
// if price ht is forced (ie: calculated by margin rate and cost price)
if (!empty($price_ht))
{
$pu_ht = price2num($price_ht, 'MU');
$pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU');
}
// On reevalue prix selon taux tva car taux tva transaction peut etre different // On reevalue prix selon taux tva car taux tva transaction peut etre different
// de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur). // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
if ($tva_tx != $prod->tva_tx) elseif ($tva_tx != $prod->tva_tx)
{ {
if ($price_base_type != 'HT') if ($price_base_type != 'HT')
{ {
@ -861,6 +868,8 @@ else if ($action == "addline" && $user->rights->propal->creer)
unset($_POST['product_desc']); unset($_POST['product_desc']);
unset($_POST['fournprice']); unset($_POST['fournprice']);
unset($_POST['buying_price']); unset($_POST['buying_price']);
unset($_POST['np_marginRate']);
unset($_POST['np_markRate']);
// old method // old method
unset($_POST['np_desc']); unset($_POST['np_desc']);

View File

@ -6,7 +6,7 @@
* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr> * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
* Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2011 Philippe Grand <philippe.grand@atoo-net.com> * Copyright (C) 2011 Philippe Grand <philippe.grand@atoo-net.com>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr> * Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
* Copyright (C) 2012 Marcos García <marcosgdf@gmail.com> * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro> * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
* *
@ -666,9 +666,16 @@ else if ($action == 'addline' && $user->rights->commande->creer)
$price_base_type = $prod->price_base_type; $price_base_type = $prod->price_base_type;
} }
// if price ht is forced (ie: calculated by margin rate and cost price)
if (!empty($price_ht))
{
$pu_ht = price2num($price_ht, 'MU');
$pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU');
}
// On reevalue prix selon taux tva car taux tva transaction peut etre different // On reevalue prix selon taux tva car taux tva transaction peut etre different
// de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur). // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
if ($tva_tx != $prod->tva_tx) elseif ($tva_tx != $prod->tva_tx)
{ {
if ($price_base_type != 'HT') if ($price_base_type != 'HT')
{ {
@ -806,6 +813,8 @@ else if ($action == 'addline' && $user->rights->commande->creer)
unset($_POST['product_desc']); unset($_POST['product_desc']);
unset($_POST['fournprice']); unset($_POST['fournprice']);
unset($_POST['buying_price']); unset($_POST['buying_price']);
unset($_POST['np_marginRate']);
unset($_POST['np_markRate']);
// old method // old method
unset($_POST['np_desc']); unset($_POST['np_desc']);

View File

@ -6,7 +6,7 @@
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com> * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr> * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
* Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr> * Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
* Copyright (C) 2013 Jean-Francois FERRY <jfefe@aternatik.fr> * Copyright (C) 2013 Jean-Francois FERRY <jfefe@aternatik.fr>
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro> * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
* *
@ -1227,9 +1227,16 @@ else if (($action == 'addline' || $action == 'addline_predef') && $user->rights-
$price_base_type = $prod->price_base_type; $price_base_type = $prod->price_base_type;
} }
// if price ht is forced (ie: calculated by margin rate and cost price)
if (!empty($price_ht))
{
$pu_ht = price2num($price_ht, 'MU');
$pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU');
}
// On reevalue prix selon taux tva car taux tva transaction peut etre different // On reevalue prix selon taux tva car taux tva transaction peut etre different
// de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur). // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
if ($tva_tx != $prod->tva_tx) elseif ($tva_tx != $prod->tva_tx)
{ {
if ($price_base_type != 'HT') if ($price_base_type != 'HT')
{ {
@ -1367,6 +1374,8 @@ else if (($action == 'addline' || $action == 'addline_predef') && $user->rights-
unset($_POST['product_desc']); unset($_POST['product_desc']);
unset($_POST['fournprice']); unset($_POST['fournprice']);
unset($_POST['buying_price']); unset($_POST['buying_price']);
unset($_POST['np_marginRate']);
unset($_POST['np_markRate']);
// old method // old method
unset($_POST['np_desc']); unset($_POST['np_desc']);

View File

@ -1,7 +1,7 @@
<?php <?php
/* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@capnetworks.com> /* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2010-2013 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2010-2013 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr> * Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro> * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -61,8 +61,14 @@ if (! empty($conf->margin->enabled) && ! empty($object->element) && in_array($ob
?> ?>
</td> </td>
<?php <?php
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) $colspan++; if(! empty($conf->global->DISPLAY_MARGIN_RATES))
if (! empty($conf->global->DISPLAY_MARK_RATES)) $colspan++; {
echo '<td align="right">'.$langs->trans('MarginRate').'</td>';
}
if(! empty($conf->global->DISPLAY_MARK_RATES))
{
echo '<td align="right">'.$langs->trans('MarkRate').'</td>';
}
} }
?> ?>
<td colspan="<?php echo $colspan; ?>">&nbsp;</td> <td colspan="<?php echo $colspan; ?>">&nbsp;</td>
@ -109,7 +115,7 @@ if (! empty($conf->margin->enabled) && ! empty($object->element) && in_array($ob
<td align="right"><input type="text" size="5" name="price_ht" class="flat" value="<?php echo (isset($_POST["price_ht"])?$_POST["price_ht"]:''); ?>"> <td align="right"><input type="text" size="5" name="price_ht" class="flat" value="<?php echo (isset($_POST["price_ht"])?$_POST["price_ht"]:''); ?>">
</td> </td>
<td align="right"><input type="text" size="2" name="qty" class="flat" value="<?php echo (isset($_POST["qty"])?$_POST["qty"]:1); ?>"></td> <td align="right"><input type="text" size="2" name="qty" class="flat" value="<?php echo (isset($_POST["qty"])?$_POST["qty"]:1); ?>"></td>
<td align="right" class="nowrap"><input type="text" size="1" class="flat" value="<?php echo $buyer->remise_percent; ?>" name="remise_percent"><span class="hideonsmartphone">%</span></td> <td align="right" class="nowrap"><input type="text" size="1" class="flat" value="<?php echo (isset($_POST["remise_percent"])?$_POST["remise_percent"]:$buyer->remise_client); ?>" name="remise_percent"><span class="hideonsmartphone">%</span></td>
<?php <?php
$colspan = 4; $colspan = 4;
if (! empty($usemargins)) if (! empty($usemargins))
@ -120,12 +126,10 @@ if (! empty($conf->margin->enabled) && ! empty($object->element) && in_array($ob
</td> </td>
<?php <?php
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) { if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
$colspan++; echo '<td align="right"><input type="text" size="2" name="np_marginRate" value="'.(isset($_POST["np_marginRate"])?$_POST["np_marginRate"]:'').'">%</td>';
$coldisplay++;
} }
if (! empty($conf->global->DISPLAY_MARK_RATES)) { elseif (! empty($conf->global->DISPLAY_MARK_RATES)) {
$colspan++; echo '<td align="right"><input type="text" size="2" name="np_markRate" value="'.(isset($_POST["np_markRate"])?$_POST["np_markRate"]:'').'">%</td>';
$coldisplay++;
} }
} }
?> ?>
@ -185,4 +189,100 @@ if (! empty($conf->service->enabled) && $dateSelector)
?> ?>
</form> </form>
<?php if ($conf->margin->enabled) { ?>
<script type="text/javascript">
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));
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>
<?php } ?>
<!-- END PHP TEMPLATE freeproductline_create.tpl.php --> <!-- END PHP TEMPLATE freeproductline_create.tpl.php -->

View File

@ -1,7 +1,7 @@
<?php <?php
/* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@capnetworks.com> /* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr> * Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro> * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -63,8 +63,14 @@ jQuery(document).ready(function() {
$colspan = 4; $colspan = 4;
if (! empty($usemargins)) if (! empty($usemargins))
{ {
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) $colspan++; if(! empty($conf->global->DISPLAY_MARGIN_RATES))
if (! empty($conf->global->DISPLAY_MARK_RATES)) $colspan++; {
echo '<td align="right">'.$langs->trans('MarginRate').'</td>';
}
if(! empty($conf->global->DISPLAY_MARK_RATES))
{
echo '<td align="right">'.$langs->trans('MarkRate').'</td>';
}
?> ?>
<td align="right"> <td align="right">
<?php <?php
@ -113,19 +119,17 @@ else {
$doleditor->Create(); $doleditor->Create();
?> ?>
</td> </td>
<td align="right"><input type="text" size="2" name="qty" class="flat" value="1"></td> <td align="right"><input type="hidden" name="price_ht"><input type="text" size="2" name="qty" class="flat" value="1"></td>
<td align="right" nowrap><input type="text" size="1" class="flat" name="remise_percent" value="<?php echo $buyer->remise_percent; ?>"><span class="hideonsmartphone">%</span></td> <td align="right" nowrap><input type="text" size="1" class="flat" name="remise_percent" value="<?php echo $buyer->remise_percent; ?>"><span class="hideonsmartphone">%</span></td>
<?php <?php
$colspan = 4; $colspan = 4;
if (! empty($usemargins)) if (! empty($usemargins))
{ {
if (! empty($conf->global->DISPLAY_MARGIN_RATES)) { if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
$colspan++; echo '<td align="right"><input type="text" size="2" name="np_marginRate" value="'.(isset($_POST["np_marginRate"])?$_POST["np_marginRate"]:'').'">%</td>';
$coldisplay++;
} }
if (! empty($conf->global->DISPLAY_MARK_RATES)) { elseif (! empty($conf->global->DISPLAY_MARK_RATES)) {
$colspan++; echo '<td align="right"><input type="text" size="2" name="np_markRate" value="'.(isset($_POST["np_markRate"])?$_POST["np_markRate"]:'').'">%</td>';
$coldisplay++;
} }
?> ?>
<td align="right"> <td align="right">
@ -200,6 +204,97 @@ if (! empty($usemargins))
{ {
?> ?>
<script type="text/javascript"> <script type="text/javascript">
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#addpredefinedproduct").submit(function(e) {
if (npRate) return checkLine(e, npRate);
else return true;
});
if (npRate == 'np_marginRate') {
$("input[name='np_marginRate']:last").blur(function(e) {
return checkLine(e, npRate);
});
}
else {
if (npRate == 'np_markRate') {
$("input[name='np_markRate']:last").blur(function(e) {
return checkLine(e, npRate);
});
}
}
function checkLine(e, npRate)
{
var buying_price = $("input[name='buying_price']:last");
var remise = $("input[name='remise_percent']:last");
var rate = $("input[name='"+npRate+"']:last");
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']:last").val(formatFloat(np_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('.', ',');
}
$("#idprod").change(function() { $("#idprod").change(function() {
$("#fournprice options").remove(); $("#fournprice options").remove();
$("#fournprice").hide(); $("#fournprice").hide();