Skip to content

Commit

Permalink
FIX #6259
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jan 28, 2017
1 parent e2ce31e commit c88e63e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
15 changes: 14 additions & 1 deletion htdocs/compta/facture/class/facture.class.php
Expand Up @@ -1164,7 +1164,7 @@ function fetch_lines()
{
$this->lines=array();

$sql = 'SELECT l.rowid, l.fk_product, l.fk_parent_line, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.tva_tx, ';
$sql = 'SELECT l.rowid, l.fk_facture, l.fk_product, l.fk_parent_line, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.tva_tx, ';
$sql.= ' l.situation_percent, l.fk_prev_id,';
$sql.= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise_percent, l.fk_remise_except, l.subprice,';
$sql.= ' l.rang, l.special_code,';
Expand All @@ -1191,6 +1191,7 @@ function fetch_lines()

$line->id = $objp->rowid;
$line->rowid = $objp->rowid; // deprecated
$line->fk_facture = $objp->fk_facture;
$line->label = $objp->custom_label; // deprecated
$line->desc = $objp->description; // Description line
$line->description = $objp->description; // Description line
Expand Down Expand Up @@ -1443,6 +1444,18 @@ function insert_discount($idremise)
$facligne->rang=-1;
$facligne->info_bits=2;

// Get buy/cost price of invoice that is source of discount
if ($remise->fk_facture_source > 0)
{
$srcinvoice=new Facture($this->db);
$srcinvoice->fetch($remise->fk_facture_source);
$totalcostpriceofinvoice=0;
include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmargin.class.php'; // TODO Move this into commonobject
$formmargin=new FormMargin($this->db);
$arraytmp=$formmargin->getMarginInfosArray($srcinvoice, false);
$facligne->pa_ht = $arraytmp['pa_total'];
}

$facligne->total_ht = -$remise->amount_ht;
$facligne->total_tva = -$remise->amount_tva;
$facligne->total_ttc = -$remise->amount_ttc;
Expand Down
49 changes: 27 additions & 22 deletions htdocs/core/class/html.formmargin.class.php
Expand Up @@ -48,6 +48,7 @@ function __construct($db)

/**
* get array with margin information from lines of object
* TODO Move this in common class.
*
* @param CommonObject $object Object we want to get margin information for
* @param boolean $force_price True of not
Expand Down Expand Up @@ -92,19 +93,23 @@ function getMarginInfosArray($object, $force_price=false)
$line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
}

$pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100);
$pa_ht = ($pv < 0 ? - $line->pa_ht : $line->pa_ht); // We choosed to have line->pa_ht always positive in database, so we guess the correct sign
$pa = $line->qty * $pa_ht;

// calcul des marges
if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise
$pa = $line->qty * $line->pa_ht;
$pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100);
if ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '1') { // remise globale considérée comme produit
$marginInfos['pa_products'] += $pa;
$marginInfos['pv_products'] += $pv;
$marginInfos['pa_total'] += $pa;
$marginInfos['pv_total'] += $pv;
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
if ($pv < 0)
$marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
else
//if ($pv < 0)
//{
// $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
//}
//else
$marginInfos['margin_on_products'] += $pv - $pa;
}
elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '2') { // remise globale considérée comme service
Expand All @@ -113,9 +118,9 @@ function getMarginInfosArray($object, $force_price=false)
$marginInfos['pa_total'] += $pa;
$marginInfos['pv_total'] += $pv;
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
if ($pv < 0)
$marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
else
//if ($pv < 0)
// $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
//else
$marginInfos['margin_on_services'] += $pv - $pa;
}
elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '3') { // remise globale prise en compte uniqt sur total
Expand All @@ -126,29 +131,29 @@ function getMarginInfosArray($object, $force_price=false)
else {
$type=$line->product_type?$line->product_type:$line->fk_product_type;
if ($type == 0) { // product
$pa = $line->qty * $line->pa_ht;
$pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100);
$marginInfos['pa_products'] += $pa;
$marginInfos['pv_products'] += $pv;
$marginInfos['pa_total'] += $pa;
$marginInfos['pv_total'] += $pv;
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
if ($pv < 0)
$marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
else
$marginInfos['margin_on_products'] += $pv - $pa;
//if ($pv < 0)
//{
// $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
//}
//else
//{
$marginInfos['margin_on_products'] += $pv - $pa;
//}
}
elseif ($type == 1) { // service
$pa = $line->qty * $line->pa_ht;
$pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100);
$marginInfos['pa_services'] += $pa;
$marginInfos['pv_services'] += $pv;
$marginInfos['pa_total'] += $pa;
$marginInfos['pv_total'] += $pv;
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
if ($pv < 0)
$marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
else
//if ($pv < 0)
// $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
//else
$marginInfos['margin_on_services'] += $pv - $pa;
}
}
Expand All @@ -164,9 +169,9 @@ function getMarginInfosArray($object, $force_price=false)
$marginInfos['mark_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pv_services'];

// if credit note, margin = -1 * (abs(selling_price) - buying_price)
if ($marginInfos['pv_total'] < 0)
$marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
else
//if ($marginInfos['pv_total'] < 0)
// $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
//else
$marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
if ($marginInfos['pa_total'] > 0)
$marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
Expand Down

0 comments on commit c88e63e

Please sign in to comment.