diff --git a/htdocs/margin/agentMargins.php b/htdocs/margin/agentMargins.php
index f5826435ffa24b8a8a899f2a620b69bc99ca602f..5dc39a1c6eb60000d51969882fbdfc815e7e6799 100644
--- a/htdocs/margin/agentMargins.php
+++ b/htdocs/margin/agentMargins.php
@@ -127,8 +127,8 @@ print '</form>';
 $sql = "SELECT s.nom, s.rowid as socid, s.code_client, s.client, u.rowid as agent,";
 $sql.= " u.login, u.name, u.firstname,";
 $sql.= " sum(d.total_ht) as selling_price,";
-$sql.= " IF (f.type =2,sum(d.buy_price_ht * d.qty *-1),sum(d.buy_price_ht * d.qty)) as buying_price, ";
-$sql.= " IF (f.type =2,sum((d.price + d.buy_price_ht) * d.qty),sum((d.price - d.buy_price_ht) * d.qty)) as marge" ;
+$sql.= $db->ifsql('f.type =2','sum(d.buy_price_ht * d.qty *-1)','sum(d.buy_price_ht * d.qty)')." as buying_price, ";
+$sql.= $db->ifsql('f.type =2','sum((d.price + d.buy_price_ht) * d.qty)','sum((d.price - d.buy_price_ht) * d.qty)')." as marge" ;
 $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
 $sql.= ", ".MAIN_DB_PREFIX."facture as f";
 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_contact e ON e.element_id = f.rowid and e.statut = 4 and e.fk_c_type_contact = ".(empty($conf->global->AGENT_CONTACT_TYPE)?-1:$conf->global->AGENT_CONTACT_TYPE);
diff --git a/htdocs/margin/customerMargins.php b/htdocs/margin/customerMargins.php
index d44feb263aee9f61ee42ab2f5825135c20556017..d46133193f6b68b22083cb707cc68979a75cb470 100644
--- a/htdocs/margin/customerMargins.php
+++ b/htdocs/margin/customerMargins.php
@@ -156,8 +156,8 @@ print '</form>';
 $sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client, s.client,";
 $sql.= " f.facnumber, f.total as total_ht,";
 $sql.= " sum(d.total_ht) as selling_price,";
-$sql.= " IF (f.type =2,sum(d.buy_price_ht * d.qty *-1),sum(d.buy_price_ht * d.qty)) as buying_price, ";
-$sql.= " IF (f.type =2,sum((d.price + d.buy_price_ht) * d.qty),sum((d.price - d.buy_price_ht) * d.qty)) as marge," ;
+$sql.= $db->ifsql('f.type =2','sum(d.buy_price_ht * d.qty *-1)','sum(d.buy_price_ht * d.qty)')." as buying_price, ";
+$sql.= $db->ifsql('f.type =2','sum((d.price + d.buy_price_ht) * d.qty)','sum((d.price - d.buy_price_ht) * d.qty)')." as marge," ;
 $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
 $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
 $sql.= ", ".MAIN_DB_PREFIX."facture as f";
diff --git a/htdocs/margin/productMargins.php b/htdocs/margin/productMargins.php
index dd0c0901a1e0145e84b971452c7b366b6cd814d7..04c22bb793f49f31d0086d6e64edb0ec6552e624 100644
--- a/htdocs/margin/productMargins.php
+++ b/htdocs/margin/productMargins.php
@@ -158,8 +158,8 @@ print '</form>';
 $sql = "SELECT DISTINCT d.fk_product, p.label, p.rowid, p.fk_product_type, p.ref,";
 $sql.= " f.facnumber, f.total as total_ht,";
 $sql.= " sum(d.total_ht) as selling_price,";
-$sql.= " IF (f.type =2,sum(d.buy_price_ht * d.qty *-1),sum(d.buy_price_ht * d.qty)) as buying_price, ";
-$sql.= " IF (f.type =2,sum((d.price + d.buy_price_ht) * d.qty),sum((d.price - d.buy_price_ht) * d.qty)) as marge," ;
+$sql.= $db->ifsql('f.type =2','sum(d.buy_price_ht * d.qty *-1)','sum(d.buy_price_ht * d.qty)')." as buying_price, ";
+$sql.= $db->ifsql('f.type =2','sum((d.price + d.buy_price_ht) * d.qty)','sum((d.price - d.buy_price_ht) * d.qty)')." as marge," ;
 $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
 $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
 $sql.= ", ".MAIN_DB_PREFIX."product as p";
diff --git a/htdocs/margin/tabs/productMargins.php b/htdocs/margin/tabs/productMargins.php
index c4f37667ba8331273f67f86dab1c906d5e49a967..19aa66390b1b6ac65fb01e7d07d1cad5612b8b40 100644
--- a/htdocs/margin/tabs/productMargins.php
+++ b/htdocs/margin/tabs/productMargins.php
@@ -131,9 +131,9 @@ if ($id > 0 || ! empty($ref))
 		$sql = "SELECT DISTINCT s.nom, s.rowid as socid, s.code_client,";
 		$sql.= " f.facnumber, f.total as total_ht,";
 		$sql.= " d.total_ht as selling_price,";
-        $sql.= " IF (f.type =2,(d.buy_price_ht * d.qty *-1),(d.buy_price_ht * d.qty)) as buying_price, ";
-        $sql.= " IF (f.type =2,d.qty *-1,d.qty) as qty,";
-        $sql.= " IF (f.type =2,((d.price + d.buy_price_ht) * d.qty),((d.price - d.buy_price_ht) * d.qty)) as marge," ;
+        $sql.= $db->ifsql('f.type =2','(d.buy_price_ht * d.qty *-1)','(d.buy_price_ht * d.qty)')." as buying_price, ";
+        $sql.= $db->ifsql('f.type =2','d.qty *-1','d.qty')." as qty,";
+        $sql.= $db->ifsql('f.type =2','((d.price + d.buy_price_ht) * d.qty)','((d.price - d.buy_price_ht) * d.qty)')." as marge," ;
 		$sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
 		if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user ";
 		$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
diff --git a/htdocs/margin/tabs/thirdpartyMargins.php b/htdocs/margin/tabs/thirdpartyMargins.php
index d0c0b9a67a4264bbb8788d8dc7f6dc2ba2be3605..be56cbb5589e202942151cb9de07c21fbe2ff0a9 100644
--- a/htdocs/margin/tabs/thirdpartyMargins.php
+++ b/htdocs/margin/tabs/thirdpartyMargins.php
@@ -130,8 +130,8 @@ if ($socid > 0)
 		$sql.= " f.facnumber, f.total as total_ht,";
 		$sql.= " sum(d.total_ht) as selling_price,";
 
-        $sql.= " IF (f.type =2,sum(d.buy_price_ht * d.qty *-1),sum(d.buy_price_ht * d.qty)) as buying_price, ";
-        $sql.= " IF (f.type =2,sum((d.price + d.buy_price_ht) * d.qty),sum((d.price - d.buy_price_ht) * d.qty)) as marge," ;
+        $sql.= $db->ifsql('f.type =2','sum(d.buy_price_ht * d.qty *-1)','sum(d.buy_price_ht * d.qty)')." as buying_price, ";
+        $sql.= $db->ifsql('f.type =2','sum((d.price + d.buy_price_ht) * d.qty)','sum((d.price - d.buy_price_ht) * d.qty)')." as marge," ;
 		$sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
 		$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
 		$sql.= ", ".MAIN_DB_PREFIX."facture as f";