diff --git a/htdocs/comm/card.php b/htdocs/comm/card.php
index a51ef902eb62b6be4bc94f5211d0ff5ed3f60f12..dfdc91838860b6ed36f84b35bb6d39baed80bbf1 100644
--- a/htdocs/comm/card.php
+++ b/htdocs/comm/card.php
@@ -3,7 +3,7 @@
  * Copyright (C) 2004-2015 Laurent Destailleur         <eldy@users.sourceforge.net>
  * Copyright (C) 2004      Eric Seigne                 <eric.seigne@ryxeo.com>
  * Copyright (C) 2006      Andre Cianfarani            <acianfa@free.fr>
- * Copyright (C) 2005-2012 Regis Houssin               <regis.houssin@capnetworks.com>
+ * Copyright (C) 2005-2017 Regis Houssin               <regis.houssin@capnetworks.com>
  * Copyright (C) 2008      Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
  * Copyright (C) 2010-2014 Juanjo Menent               <jmenent@2byte.es>
  * Copyright (C) 2013      Alexandre Spangaro          <aspangaro.dolibarr@gmail.com>
@@ -534,74 +534,73 @@ if ($id > 0)
 	$boxstat.='<table summary="'.dol_escape_htmltag($langs->trans("DolibarrStateBoard")).'" class="noborder boxtable boxtablenobottom" width="100%">';
 	$boxstat.='<tr class="impair"><td colspan="2" class="tdboxstats nohover">';
 
-	if ($conf->propal->enabled)
+	if (! empty($conf->propal->enabled))
 	{
-    	// Box proposals
-    	$tmp = $object->getOutstandingProposals();
-    	$outstandingOpened=$tmp['opened'];
-    	$outstandingTotal=$tmp['total_ht'];
-    	$outstandingTotalIncTax=$tmp['total_ttc'];
-	    $text=$langs->trans("OverAllProposals");
-    	$link='';
-    	$icon='bill';
-    	if ($link) $boxstat.='<a href="'.$link.'" class="boxstatsindicator thumbstat nobold nounderline">';
-    	$boxstat.='<div class="boxstats">';
-    	$boxstat.='<span class="boxstatstext">'.img_object("",$icon).' '.$text.'</span><br>';
-    	$boxstat.='<span class="boxstatsindicator">'.price($outstandingTotal, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
-    	$boxstat.='</div>';
-    	if ($link) $boxstat.='</a>';
+		// Box proposals
+		$tmp = $object->getOutstandingProposals();
+		$outstandingOpened=$tmp['opened'];
+		$outstandingTotal=$tmp['total_ht'];
+		$outstandingTotalIncTax=$tmp['total_ttc'];
+		$text=$langs->trans("OverAllProposals");
+		$link='';
+		$icon='bill';
+		if ($link) $boxstat.='<a href="'.$link.'" class="boxstatsindicator thumbstat nobold nounderline">';
+		$boxstat.='<div class="boxstats">';
+		$boxstat.='<span class="boxstatstext">'.img_object("",$icon).' '.$text.'</span><br>';
+		$boxstat.='<span class="boxstatsindicator">'.price($outstandingTotal, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
+		$boxstat.='</div>';
+		if ($link) $boxstat.='</a>';
 	}
 
-	if ($conf->commande->enabled)
+	if (! empty($conf->commande->enabled))
 	{
-	    // Box proposals
-	    $tmp = $object->getOutstandingOrders();
-	    $outstandingOpened=$tmp['opened'];
-	    $outstandingTotal=$tmp['total_ht'];
-	    $outstandingTotalIncTax=$tmp['total_ttc'];
-	    $text=$langs->trans("OverAllOrders");
-	    $link='';
-	    $icon='bill';
-	    if ($link) $boxstat.='<a href="'.$link.'" class="boxstatsindicator thumbstat nobold nounderline">';
-	    $boxstat.='<div class="boxstats">';
-	    $boxstat.='<span class="boxstatstext">'.img_object("",$icon).' '.$text.'</span><br>';
-	    $boxstat.='<span class="boxstatsindicator">'.price($outstandingTotal, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
-	    $boxstat.='</div>';
-	    if ($link) $boxstat.='</a>';
+		// Box proposals
+		$tmp = $object->getOutstandingOrders();
+		$outstandingOpened=$tmp['opened'];
+		$outstandingTotal=$tmp['total_ht'];
+		$outstandingTotalIncTax=$tmp['total_ttc'];
+		$text=$langs->trans("OverAllOrders");
+		$link='';
+		$icon='bill';
+		if ($link) $boxstat.='<a href="'.$link.'" class="boxstatsindicator thumbstat nobold nounderline">';
+		$boxstat.='<div class="boxstats">';
+		$boxstat.='<span class="boxstatstext">'.img_object("",$icon).' '.$text.'</span><br>';
+		$boxstat.='<span class="boxstatsindicator">'.price($outstandingTotal, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
+		$boxstat.='</div>';
+		if ($link) $boxstat.='</a>';
 	}
 
-	if ($conf->facture->enabled)
+	if (! empty($conf->facture->enabled))
 	{
-    	$tmp = $object->getOutstandingBills();
-    	$outstandingOpened=$tmp['opened'];
-    	$outstandingTotal=$tmp['total_ht'];
-    	$outstandingTotalIncTax=$tmp['total_ttc'];
-
-    	$text=$langs->trans("OverAllInvoices");
-    	$link='';
-    	$icon='bill';
-    	if ($link) $boxstat.='<a href="'.$link.'" class="boxstatsindicator thumbstat nobold nounderline">';
-    	$boxstat.='<div class="boxstats">';
-    	$boxstat.='<span class="boxstatstext">'.img_object("",$icon).' '.$text.'</span><br>';
-    	$boxstat.='<span class="boxstatsindicator">'.price($outstandingTotal, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
-    	$boxstat.='</div>';
-    	if ($link) $boxstat.='</a>';
-
-    	// Box outstanding bill
-    	$warn = '';
-    	if ($object->outstanding_limit != '' && $object->outstanding_limit < $outstandingOpened)
-    	{
-    	    $warn = ' '.img_warning($langs->trans("OutstandingBillReached"));
-    	}
-    	$text=$langs->trans("CurrentOutstandingBill");
-    	$link=DOL_URL_ROOT.'/compta/recap-compta.php?socid='.$object->id;
-    	$icon='bill';
-    	if ($link) $boxstat.='<a href="'.$link.'" class="boxstatsindicator thumbstat nobold nounderline">';
-    	$boxstat.='<div class="boxstats">';
-    	$boxstat.='<span class="boxstatstext">'.img_object("",$icon).' '.$text.'</span><br>';
-    	$boxstat.='<span class="boxstatsindicator'.($outstandingOpened>0?' amountremaintopay':'').'">'.price($outstandingOpened, 1, $langs, 1, -1, -1, $conf->currency).$warn.'</span>';
-    	$boxstat.='</div>';
-    	if ($link) $boxstat.='</a>';
+		$tmp = $object->getOutstandingBills();
+		$outstandingOpened=$tmp['opened'];
+		$outstandingTotal=$tmp['total_ht'];
+		$outstandingTotalIncTax=$tmp['total_ttc'];
+		$text=$langs->trans("OverAllInvoices");
+		$link='';
+		$icon='bill';
+		if ($link) $boxstat.='<a href="'.$link.'" class="boxstatsindicator thumbstat nobold nounderline">';
+		$boxstat.='<div class="boxstats">';
+		$boxstat.='<span class="boxstatstext">'.img_object("",$icon).' '.$text.'</span><br>';
+		$boxstat.='<span class="boxstatsindicator">'.price($outstandingTotal, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
+		$boxstat.='</div>';
+		if ($link) $boxstat.='</a>';
+
+		// Box outstanding bill
+		$warn = '';
+		if ($object->outstanding_limit != '' && $object->outstanding_limit < $outstandingOpened)
+		{
+			$warn = ' '.img_warning($langs->trans("OutstandingBillReached"));
+		}
+		$text=$langs->trans("CurrentOutstandingBill");
+		$link=DOL_URL_ROOT.'/compta/recap-compta.php?socid='.$object->id;
+		$icon='bill';
+		if ($link) $boxstat.='<a href="'.$link.'" class="boxstatsindicator thumbstat nobold nounderline">';
+		$boxstat.='<div class="boxstats">';
+		$boxstat.='<span class="boxstatstext">'.img_object("",$icon).' '.$text.'</span><br>';
+		$boxstat.='<span class="boxstatsindicator'.($outstandingOpened>0?' amountremaintopay':'').'">'.price($outstandingOpened, 1, $langs, 1, -1, -1, $conf->currency).$warn.'</span>';
+		$boxstat.='</div>';
+		if ($link) $boxstat.='</a>';
 	}
 
 	$boxstat.='</td></tr>';
diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php
index 1605e7281b8d16a90d738d1150539214583ab74d..2bd764c92ede1ae834e234bec7a87aa2b8943f5b 100644
--- a/htdocs/societe/class/societe.class.php
+++ b/htdocs/societe/class/societe.class.php
@@ -4,7 +4,7 @@
  * Copyright (C) 2004       Eric Seigne             <eric.seigne@ryxeo.com>
  * Copyright (C) 2003       Brian Fraval            <brian@fraval.org>
  * Copyright (C) 2006       Andre Cianfarani        <acianfa@free.fr>
- * Copyright (C) 2005-2016  Regis Houssin           <regis.houssin@capnetworks.com>
+ * Copyright (C) 2005-2017  Regis Houssin           <regis.houssin@capnetworks.com>
  * Copyright (C) 2008       Patrick Raguin          <patrick.raguin@auguria.net>
  * Copyright (C) 2010-2014  Juanjo Menent           <jmenent@2byte.es>
  * Copyright (C) 2013       Florian Henry           <florian.henry@open-concept.pro>
@@ -3346,31 +3346,36 @@ class Societe extends CommonObject
 	 */
 	function getOutstandingProposals($mode='customer')
 	{
-	    $table='propal';
-	    if ($mode == 'supplier') $table = 'supplier_proposal';
+		$table='propal';
+		if ($mode == 'supplier') $table = 'supplier_proposal';
 
-	    $sql  = "SELECT rowid, total_ht, total as total_ttc, fk_statut FROM ".MAIN_DB_PREFIX.$table." as f";
-	    $sql .= " WHERE fk_soc = ". $this->id;
+		$sql  = "SELECT rowid, total_ht, total as total_ttc, fk_statut FROM ".MAIN_DB_PREFIX.$table." as f";
+		$sql .= " WHERE fk_soc = ". $this->id;
+		if ($mode == 'supplier') {
+			$sql .= " AND entity IN (".getEntity('supplier_proposal').")";
+		} else {
+			$sql .= " AND entity IN (".getEntity('propal').")";
+		}
 
-	    dol_syslog("getOutstandingProposals", LOG_DEBUG);
-	    $resql=$this->db->query($sql);
-	    if ($resql)
-	    {
-	        $outstandingOpened = 0;
-	        $outstandingTotal = 0;
-	        $outstandingTotalIncTax = 0;
-	        while($obj=$this->db->fetch_object($resql)) {
-	            $outstandingTotal+= $obj->total_ht;
-	            $outstandingTotalIncTax+= $obj->total_ttc;
-	            if ($obj->fk_statut != 0)    // Not a draft
-	            {
-	                $outstandingOpened+=$obj->total_ttc;
-	            }
-	        }
-	        return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax);
-	    }
-	    else
-	        return array();
+		dol_syslog("getOutstandingProposals", LOG_DEBUG);
+		$resql=$this->db->query($sql);
+		if ($resql)
+		{
+			$outstandingOpened = 0;
+			$outstandingTotal = 0;
+			$outstandingTotalIncTax = 0;
+			while($obj=$this->db->fetch_object($resql)) {
+				$outstandingTotal+= $obj->total_ht;
+				$outstandingTotalIncTax+= $obj->total_ttc;
+				if ($obj->fk_statut != 0)    // Not a draft
+				{
+					$outstandingOpened+=$obj->total_ttc;
+				}
+			}
+			return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax);
+		}
+		else
+			return array();
 	}
 
 	/**
@@ -3381,31 +3386,36 @@ class Societe extends CommonObject
 	 */
 	function getOutstandingOrders($mode='customer')
 	{
-	    $table='commande';
-	    if ($mode == 'supplier') $table = 'commande_fournisseur';
+		$table='commande';
+		if ($mode == 'supplier') $table = 'commande_fournisseur';
 
-	    $sql  = "SELECT rowid, total_ht, total_ttc, fk_statut FROM ".MAIN_DB_PREFIX.$table." as f";
-	    $sql .= " WHERE fk_soc = ". $this->id;
+		$sql  = "SELECT rowid, total_ht, total_ttc, fk_statut FROM ".MAIN_DB_PREFIX.$table." as f";
+		$sql .= " WHERE fk_soc = ". $this->id;
+		if ($mode == 'supplier') {
+			$sql .= " AND entity IN (".getEntity('supplier_order').")";
+		} else {
+			$sql .= " AND entity IN (".getEntity('commande').")";
+		}
 
-	    dol_syslog("getOutstandingOrders", LOG_DEBUG);
-	    $resql=$this->db->query($sql);
-	    if ($resql)
-	    {
-	        $outstandingOpened = 0;
-	        $outstandingTotal = 0;
-	        $outstandingTotalIncTax = 0;
-	        while($obj=$this->db->fetch_object($resql)) {
-	            $outstandingTotal+= $obj->total_ht;
-	            $outstandingTotalIncTax+= $obj->total_ttc;
-	            if ($obj->fk_statut != 0)    // Not a draft
-	            {
-	                $outstandingOpened+=$obj->total_ttc;
-	            }
-	        }
-	        return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax);
-	    }
-	    else
-	        return array();
+		dol_syslog("getOutstandingOrders", LOG_DEBUG);
+		$resql=$this->db->query($sql);
+		if ($resql)
+		{
+			$outstandingOpened = 0;
+			$outstandingTotal = 0;
+			$outstandingTotalIncTax = 0;
+			while($obj=$this->db->fetch_object($resql)) {
+				$outstandingTotal+= $obj->total_ht;
+				$outstandingTotalIncTax+= $obj->total_ttc;
+				if ($obj->fk_statut != 0)    // Not a draft
+				{
+					$outstandingOpened+=$obj->total_ttc;
+				}
+			}
+			return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax);
+		}
+		else
+			return array();
 	}
 
 	/**
@@ -3416,64 +3426,69 @@ class Societe extends CommonObject
 	 */
 	function getOutstandingBills($mode='customer')
 	{
-	    $table='facture';
-	    if ($mode == 'supplier') $table = 'facture_fourn';
-
-	    /* Accurate value of remain to pay is to sum remaintopay for each invoice
-	     $paiement = $invoice->getSommePaiement();
-	     $creditnotes=$invoice->getSumCreditNotesUsed();
-	     $deposits=$invoice->getSumDepositsUsed();
-	     $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
-	     $remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT');
-	     */
-	    if ($mode == 'supplier') $sql  = "SELECT rowid, total_ht as total_ht, total_ttc, paye, fk_statut, close_code FROM ".MAIN_DB_PREFIX.$table." as f";
-	    else $sql  = "SELECT rowid, total as total_ht, total_ttc, paye, fk_statut, close_code FROM ".MAIN_DB_PREFIX.$table." as f";
-	    $sql .= " WHERE fk_soc = ". $this->id;
+		$table='facture';
+		if ($mode == 'supplier') $table = 'facture_fourn';
+
+		/* Accurate value of remain to pay is to sum remaintopay for each invoice
+		 $paiement = $invoice->getSommePaiement();
+		 $creditnotes=$invoice->getSumCreditNotesUsed();
+		 $deposits=$invoice->getSumDepositsUsed();
+		 $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
+		 $remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT');
+		 */
+		if ($mode == 'supplier') $sql  = "SELECT rowid, total_ht as total_ht, total_ttc, paye, fk_statut, close_code FROM ".MAIN_DB_PREFIX.$table." as f";
+		else $sql  = "SELECT rowid, total as total_ht, total_ttc, paye, fk_statut, close_code FROM ".MAIN_DB_PREFIX.$table." as f";
+		$sql .= " WHERE fk_soc = ". $this->id;
+		if ($mode == 'supplier') {
+			$sql .= " AND entity IN (".getEntity('facture_fourn').")";
+		} else {
+			$sql .= " AND entity IN (".getEntity('facture').")";
+		}
 
-	    dol_syslog("getOutstandingBills", LOG_DEBUG);
-	    $resql=$this->db->query($sql);
-	    if ($resql)
-	    {
-	        $outstandingOpened = 0;
-	        $outstandingTotal = 0;
-	        $outstandingTotalIncTax = 0;
-	        if ($mode == 'supplier')
-	        {
-	            require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
-	            $tmpobject=new FactureFournisseur($this->db);
-	        }
-	        else
-	        {
-	           require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
-	           $tmpobject=new Facture($this->db);
-	        }
-	        while($obj=$this->db->fetch_object($resql)) {
-	            $tmpobject->id=$obj->rowid;
-	            if ($obj->fk_statut != 0                                           // Not a draft
-	                && ! ($obj->fk_statut == 3 && $obj->close_code == 'replaced')  // Not a replaced invoice
-	                )
-	            {
-	                $outstandingTotal+= $obj->total_ht;
-	                $outstandingTotalIncTax+= $obj->total_ttc;
-	            }
-	            if ($obj->paye == 0
-	                && $obj->fk_statut != 0    // Not a draft
-	                && $obj->fk_statut != 3	   // Not abandonned
-	                && $obj->fk_statut != 2)   // Not classified as paid
-    	            //$sql .= " AND (fk_statut <> 3 OR close_code <> 'abandon')";		// Not abandonned for undefined reason
-	            {
-    	            $paiement = $tmpobject->getSommePaiement();
-    	            $creditnotes = $tmpobject->getSumCreditNotesUsed();
-    	            $deposits = $tmpobject->getSumDepositsUsed();
-	                $outstandingOpened+=$obj->total_ttc - $paiement - $creditnotes - $deposits;
-	            }
-	        }
-	        return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax);
-	    }
-	    else
-	    {
-	        return array();
-	    }
+		dol_syslog("getOutstandingBills", LOG_DEBUG);
+		$resql=$this->db->query($sql);
+		if ($resql)
+		{
+			$outstandingOpened = 0;
+			$outstandingTotal = 0;
+			$outstandingTotalIncTax = 0;
+			if ($mode == 'supplier')
+			{
+				require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
+				$tmpobject=new FactureFournisseur($this->db);
+			}
+			else
+			{
+				require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
+				$tmpobject=new Facture($this->db);
+			}
+			while($obj=$this->db->fetch_object($resql)) {
+				$tmpobject->id=$obj->rowid;
+				if ($obj->fk_statut != 0                                           // Not a draft
+					&& ! ($obj->fk_statut == 3 && $obj->close_code == 'replaced')  // Not a replaced invoice
+					)
+				{
+					$outstandingTotal+= $obj->total_ht;
+					$outstandingTotalIncTax+= $obj->total_ttc;
+				}
+				if ($obj->paye == 0
+					&& $obj->fk_statut != 0    // Not a draft
+					&& $obj->fk_statut != 3	   // Not abandonned
+					&& $obj->fk_statut != 2)   // Not classified as paid
+				//$sql .= " AND (fk_statut <> 3 OR close_code <> 'abandon')";		// Not abandonned for undefined reason
+				{
+					$paiement = $tmpobject->getSommePaiement();
+					$creditnotes = $tmpobject->getSumCreditNotesUsed();
+					$deposits = $tmpobject->getSumDepositsUsed();
+					$outstandingOpened+=$obj->total_ttc - $paiement - $creditnotes - $deposits;
+				}
+			}
+			return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax);
+		}
+		else
+		{
+			return array();
+		}
 	}
 
 	/**