diff --git a/htdocs/margin/customerMargins.php b/htdocs/margin/customerMargins.php index 85f13a881ab..c83ca406347 100644 --- a/htdocs/margin/customerMargins.php +++ b/htdocs/margin/customerMargins.php @@ -180,26 +180,27 @@ $sql = "SELECT"; $sql.= " s.rowid as socid, s.nom as name, s.code_client, s.client,"; if ($client) $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,"; -// Note: qty and buy_price_ht is always positive (if not your database may be corrupted, you can update this) +// Note: qty and buy_price_ht is always positive (if not, your database may be corrupted, you can update this) $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."facture as f"; $sql.= ", ".MAIN_DB_PREFIX."facturedet as d"; +if (! $user->rights->societe->client->voir && ! $socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE f.fk_soc = s.rowid"; +if ($socid > 0) $sql.= ' AND s.rowid = '.$socid; +if (!$user->rights->societe->client->voir && ! $socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; $sql.= " AND f.fk_statut > 0"; $sql.= ' AND s.entity IN ('.getEntity('societe').')'; $sql.= " AND d.fk_facture = f.rowid"; $sql.= " AND (d.product_type = 0 OR d.product_type = 1)"; -if ($client) - $sql.= " AND f.fk_soc = ".$socid; if (!empty($startdate)) - $sql.= " AND f.datef >= '".$db->idate($startdate)."'"; +$sql.= " AND f.datef >= '".$db->idate($startdate)."'"; if (!empty($enddate)) - $sql.= " AND f.datef <= '".$db->idate($enddate)."'"; +$sql.= " AND f.datef <= '".$db->idate($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"; +$sql .= " AND d.buy_price_ht <> 0"; if ($client) $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut"; else $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client"; $sql.=$db->order($sortfield,$sortorder);