Skip to content
Snippets Groups Projects
Commit cfec85c2 authored by Laurent Destailleur's avatar Laurent Destailleur
Browse files

Fix: Rewrite content of getMarginInfos.

This solve bug of rounding with total price when we need a unit price.
This solve lso bug of rounding on non decimal basis that we don't want.
Also this remove calling database when not usefull so make possible to
call this function into fetch (this remove the FIXME not into comment
so).
parent 7864fe3c
Branches
No related tags found
No related merge requests found
...@@ -81,50 +81,49 @@ function marges_prepare_head() ...@@ -81,50 +81,49 @@ function marges_prepare_head()
} }
/** /**
* getMarginInfos * Return an array with margins information of a line
* *
* @param float $pvht Buying price with tax * @param float $pvht Selling price without tax
* @param float $remise_percent Discount percent * @param float $remise_percent_ligne Discount percent on line
* @param float $tva_tx Vat rate * @param float $tva_tx Vat rate
* @param float $localtax1_tx Vat rate special 1 * @param float $localtax1_tx Vat rate special 1
* @param float $localtax2_tx Vat rate special 2 * @param float $localtax2_tx Vat rate special 2
* @param int $fk_pa ??? * @param int $fk_pa Id of buying price (prefer set this to 0 and provide $paht instead. With id, buying price may have change)
* @param float $paht Buying price without tax * @param float $paht Buying price without tax
* @param int $type Type of line (product or service)
* @param Societe $seller Object of seller
* @param array $localtaxes_array Array of localtaxes
* @return array Array of margin info * @return array Array of margin info
*
* FIXME This function is called too frequently without type, seller and without localtaxes_array defined. This make vat rate detection wrong.
*/ */
function getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht, $type=0, $seller='', $localtaxes_array='') function getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
{ {
global $db, $conf, $mysoc; global $db, $conf;
$marge_tx_ret=''; $marge_tx_ret='';
$marque_tx_ret=''; $marque_tx_ret='';
if (empty($seller) || ! is_object($seller)) $seller=$mysoc;
if (empty($localtaxes_array) || ! is_array($localtaxes_array)) $localtaxes_array=array();
if ($fk_pa > 0) { if ($fk_pa > 0) {
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
$product = new ProductFournisseur($db); $product = new ProductFournisseur($db);
if ($product->fetch_product_fournisseur_price($fk_pa)) { if ($product->fetch_product_fournisseur_price($fk_pa))
{
$paht_ret = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100); $paht_ret = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
if ($conf->global->MARGIN_TYPE == "2" && $product->fourn_unitcharges > 0) if ($conf->global->MARGIN_TYPE == "2" && $product->fourn_unitcharges > 0)
$paht_ret += $product->fourn_unitcharges; $paht_ret += $product->fourn_unitcharges;
} }
else else
{
$paht_ret = $paht; $paht_ret = $paht;
} }
}
else else
{
$paht_ret = $paht; $paht_ret = $paht;
}
// Calculate selling unit price including line discount
// We don't use calculate_price, because this function is dedicated to calculation of total with accuracy of total. We need an accuracy of a unit price.
// Also we must not apply rounding on non decimal rule defined by option MAIN_ROUNDING_RULE_TOT
$pu_ht_remise = $pvht * (1 - ($remise_percent / 100));
$pu_ht_remise = price2num($pu_ht_remise, 'MU');
require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
// calcul pu_ht remisés
$tabprice=calcul_price_total(1, $pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, 0, 'HT', 0, $type, $seller, $localtaxes_array);
$pu_ht_remise = $tabprice[0];
// calcul marge // calcul marge
if ($pu_ht_remise < 0) if ($pu_ht_remise < 0)
$marge = -1 * (abs($pu_ht_remise) - $paht_ret); $marge = -1 * (abs($pu_ht_remise) - $paht_ret);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment