FIX Some records were lost into margin per product report

This commit is contained in:
Laurent Destailleur 2016-04-01 12:26:42 +02:00
parent 27ed9ba3da
commit 8a1358d539
2 changed files with 21 additions and 14 deletions

View File

@ -171,6 +171,7 @@ PredefinedProductsAndServicesToSell=Predefined products/services to sell
PredefinedProductsToPurchase=Predefined product to purchase
PredefinedServicesToPurchase=Predefined services to purchase
PredefinedProductsAndServicesToPurchase=Predefined products/services to puchase
NotPredefinedProducts=Not predefined products/services
GenerateThumb=Generate thumb
ProductCanvasAbility=Use special "canvas" addons
ServiceNb=Service #%s

View File

@ -165,16 +165,15 @@ print '</form>';
$sql = "SELECT p.label, p.rowid, p.fk_product_type, p.ref, p.entity as pentity,";
if ($id > 0) $sql.= " d.fk_product,";
if ($id > 0) $sql.= " f.rowid as facid, f.facnumber, f.total as total_ht, f.datef, f.paye, f.fk_statut as statut,";
$sql.= " sum(d.total_ht) as selling_price,";
$sql.= " sum(".$db->ifsql('d.total_ht < 0','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,";
$sql.= " sum(".$db->ifsql('d.total_ht < 0','-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty))','d.total_ht - (d.buy_price_ht * d.qty)').") as marge";
$sql.= " SUM(d.total_ht) as selling_price,";
$sql.= " SUM(".$db->ifsql('d.total_ht < 0','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,";
$sql.= " SUM(".$db->ifsql('d.total_ht < 0','-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty))','d.total_ht - (d.buy_price_ht * d.qty)').") as marge";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."product as p";
$sql.= ", ".MAIN_DB_PREFIX."facture as f";
$sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = d.fk_product";
$sql.= " WHERE f.entity = ".$conf->entity;
$sql.= " AND f.fk_soc = s.rowid";
$sql.= " AND d.fk_product = p.rowid";
$sql.= " AND f.fk_statut > 0";
$sql.= " AND d.fk_facture = f.rowid";
if ($id > 0)
@ -186,8 +185,8 @@ if (!empty($enddate))
$sql .= " AND d.buy_price_ht IS NOT NULL";
if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
$sql .= " AND d.buy_price_ht <> 0";
if ($id > 0) $sql.= " GROUP BY p.label, p.rowid, p.fk_product_type, p.ref, d.fk_product, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut";
else $sql.= " GROUP BY p.label, p.rowid, p.fk_product_type, p.ref";
if ($id > 0) $sql.= " GROUP BY p.label, p.rowid, p.fk_product_type, p.ref, p.entity, d.fk_product, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut";
else $sql.= " GROUP BY p.label, p.rowid, p.fk_product_type, p.ref, p.entity";
$sql.=$db->order($sortfield,$sortorder);
// TODO: calculate total to display then restore pagination
//$sql.= $db->plimit($conf->liste_limit +1, $offset);
@ -260,13 +259,20 @@ if ($result)
}
else {
print '<td>';
$product_static->type=$objp->fk_product_type;
$product_static->id=$objp->rowid;
$product_static->ref=$objp->ref;
$product_static->label=$objp->label;
$product_static->entity=$objp->pentity;
$text=$product_static->getNomUrl(1);
print $text.= ' - '.$objp->label;
if ($objp->rowid > 0)
{
$product_static->type=$objp->fk_product_type;
$product_static->id=$objp->rowid;
$product_static->ref=$objp->ref;
$product_static->label=$objp->label;
$product_static->entity=$objp->pentity;
$text=$product_static->getNomUrl(1);
print $text.= ' - '.$objp->label;
}
else
{
print $langs->trans("NotPredefinedProducts");
}
print "</td>\n";
//print "<td>".$product_static->getNomUrl(1)."</td>\n";
}