diff --git a/ChangeLog b/ChangeLog
index 4decd2e66f73e2be3ef1ab5c081e3636763bfd76..b3d85e1c980540c6776822aeeb325e6e2ddf71d7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -28,6 +28,7 @@ For users:
 - Can attach several files to email when sending an invoice, order or
   proposal by email.
 - More informations reported in system information pages.
+- Add a budget report.
 - New translation: Added spanish language.
 - Added a security audit report.
 - Other minor changes (features, look, fixes)
diff --git a/htdocs/chargesociales.class.php b/htdocs/chargesociales.class.php
index 7a604ee750fbe8d44e80b82e5168c02720794357..7d5238b9b9f196c041b9ce5c84c25ae2890f1194 100644
--- a/htdocs/chargesociales.class.php
+++ b/htdocs/chargesociales.class.php
@@ -15,15 +15,13 @@
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * $Id$
  */
 
 /**
         \file       htdocs/chargesociales.class.php
 		\ingroup    facture
 		\brief      Fichier de la classe des charges sociales
-		\version    $Revision$
+		\version    $Id$
 */
 
 
@@ -304,11 +302,37 @@ class ChargeSociales
 		$lien = '<a href="'.DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$this->id.'">';
 		$lienfin='</a>';
 
-		if ($withpicto) $result.=($lien.img_object($langs->trans("ShowBill"),'bill').$lienfin.' ');
+		if ($withpicto) $result.=($lien.img_object($langs->trans("ShowSocialContribution"),'bill').$lienfin.' ');
 		$result.=$lien.($maxlen?dolibarr_trunc($this->lib,$maxlen):$this->lib).$lienfin;
 		return $result;
 	}
-	
+
+	/**
+	 * 	\brief     	Return amount aof payments already done
+	 *	\return		int		Amount of payment already done, <0 if KO
+	 */
+	function getSommePaiement()
+	{
+		$table='paiementcharge';
+		$field='amount';
+
+		$sql = 'SELECT sum(amount) as amount';
+		$sql.= ' FROM '.MAIN_DB_PREFIX.$table;
+		$sql.= ' WHERE '.$field.' = '.$this->id;
+
+		dolibarr_syslog("ChargeSociales::getSommePaiement sql=".$sql, LOG_DEBUG);
+		$resql=$this->db->query($sql);
+		if ($resql)
+		{
+			$obj = $this->db->fetch_object($resql);
+			$this->db->free($resql);
+			return $obj->amount;
+		}
+		else
+		{
+			return -1;
+		}
+	}
 }
 
 
diff --git a/htdocs/compta/bank/account.class.php b/htdocs/compta/bank/account.class.php
index b2a7bd7d5696b7369eb3865c4c1140899bed5af5..f093aafa9f4c35fc518e7937802b693d6842c2c1 100644
--- a/htdocs/compta/bank/account.class.php
+++ b/htdocs/compta/bank/account.class.php
@@ -20,145 +20,145 @@
  */
 
 /**
-   \file       htdocs/compta/bank/account.class.php
-   \ingroup    banque
-   \brief      Fichier de la classe des comptes bancaires
-   \version    $Id$
-*/
+ *	\file       htdocs/compta/bank/account.class.php
+ *	\ingroup    banque
+ *	\brief      Fichier de la classe des comptes bancaires
+ *	\version    $Id$
+ */
 
 require_once(DOL_DOCUMENT_ROOT ."/commonobject.class.php");
 
 
 /**
-   \class      Account
-   \brief      Classe permettant la gestion des comptes bancaires
-*/
+ *	\class      Account
+ *	\brief      Class to manage bank accounts
+ */
 class Account extends CommonObject
 {
-    var $db;
-    var $error;
-    var $element='bank_account';
-    var $table_element='bank_account';
+	var $db;
+	var $error;
+	var $element='bank_account';
+	var $table_element='bank_account';
 
 	var $rowid;
-  var $ref;
-  var $label;
-  var $type;
-  var $bank;
-  var $clos;
-  var $rappro;
-  var $url;
-  //! Code banque dans le RIB
-  var $code_banque;
-  //! Code guichet dans le RIB
-  var $code_guichet;
-  //! Numero du compte dans le RIB
-  var $number;
-  //! Cle de controle du RIB
-  var $cle_rib;
-  //! Numero BIC du compte
-  var $bic;
-  //! Prefix IBAN a utiliser pour creer la cle IBAN International Bank Account Number
-  var $iban_prefix;
-  var $proprio;
-  var $adresse_proprio;
-  var $type_lib=array();
-  
-  var $account_number;
-  
-  var $currency_code;
-  var $min_allowed;
-  var $min_desired;
-  var $comment;
-  
-	
-  /**
-   *  Constructeur
-   */
-  function Account($DB, $rowid=0)
-  {
-    global $langs;
-    
-    $this->db = $DB;
-    $this->rowid = $rowid;
-    
-    $this->clos = 0;
-    $this->solde = 0;
-    
-    $this->type_lib[0]=$langs->trans("BankType0");
-    $this->type_lib[1]=$langs->trans("BankType1");
-    $this->type_lib[2]=$langs->trans("BankType2");
-    
-    $this->status[0]=$langs->trans("StatusAccountOpened");
-    $this->status[1]=$langs->trans("StatusAccountClosed");
-    
-    return 1;
-  }
+	var $ref;
+	var $label;
+	var $type;
+	var $bank;
+	var $clos;
+	var $rappro;
+	var $url;
+	//! Code banque dans le RIB
+	var $code_banque;
+	//! Code guichet dans le RIB
+	var $code_guichet;
+	//! Numero du compte dans le RIB
+	var $number;
+	//! Cle de controle du RIB
+	var $cle_rib;
+	//! Numero BIC du compte
+	var $bic;
+	//! Prefix IBAN a utiliser pour creer la cle IBAN International Bank Account Number
+	var $iban_prefix;
+	var $proprio;
+	var $adresse_proprio;
+	var $type_lib=array();
+
+	var $account_number;
+
+	var $currency_code;
+	var $min_allowed;
+	var $min_desired;
+	var $comment;
+
+
+	/**
+	 *  Constructeur
+	 */
+	function Account($DB, $rowid=0)
+	{
+		global $langs;
+
+		$this->db = $DB;
+		$this->rowid = $rowid;
+
+		$this->clos = 0;
+		$this->solde = 0;
+
+		$this->type_lib[0]=$langs->trans("BankType0");
+		$this->type_lib[1]=$langs->trans("BankType1");
+		$this->type_lib[2]=$langs->trans("BankType2");
+
+		$this->status[0]=$langs->trans("StatusAccountOpened");
+		$this->status[1]=$langs->trans("StatusAccountClosed");
+
+		return 1;
+	}
 
 
 	/**
-     *      \brief      Ajoute lien entre ecriture bancaire et sources
-     *      \param      line_id     Id ecriture bancaire
-     *      \param      url_id      Id parametre url
-     *      \param      url         Url
-     *      \param      label       Libell� du lien
-     *      \param      type        Type de lien (payment, company, member, ...)
-     *      \return     int         <0 si ko, id line si ok
-     */
-    function add_url_line($line_id, $url_id, $url, $label, $type)
-    {
-        $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (fk_bank, url_id, url, label, type)";
-        $sql .= " VALUES ('".$line_id."', '".$url_id."', '".$url."', '".addslashes($label)."', '".$type."')";
+	 *      \brief      Ajoute lien entre ecriture bancaire et sources
+	 *      \param      line_id     Id ecriture bancaire
+	 *      \param      url_id      Id parametre url
+	 *      \param      url         Url
+	 *      \param      label       Libell� du lien
+	 *      \param      type        Type de lien (payment, company, member, ...)
+	 *      \return     int         <0 si ko, id line si ok
+	 */
+	function add_url_line($line_id, $url_id, $url, $label, $type)
+	{
+		$sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (fk_bank, url_id, url, label, type)";
+		$sql .= " VALUES ('".$line_id."', '".$url_id."', '".$url."', '".addslashes($label)."', '".$type."')";
 
 		dolibarr_syslog("Account::add_url_line sql=".$sql);
-        if ($this->db->query($sql))
-        {
-            $rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
-            return $rowid;
-        }
-        else
-        {
-            $this->error=$this->db->error();
-            return -1;
-        }
-    }
-
-    /**
-     *      \brief      Renvoi tableau des liens
-     *      \param      line_id         Id ligne �criture
-     *      \retuen     array           Tableau des liens
-     */
-    function get_url($line_id)
-    {
-        $lines = array();
-        $sql = "SELECT url_id, url, label, type";
-        $sql.= " FROM ".MAIN_DB_PREFIX."bank_url";
-        $sql.= " WHERE fk_bank = ".$line_id;
-        $sql.= " ORDER BY type, label";
-        
-        $result = $this->db->query($sql);
-        if ($result)
-        {
-            $i = 0;
-            $num = $this->db->num_rows($result);
-            while ($i < $num)
-            {
-                $obj = $this->db->fetch_object($result);
-                // Anciens liens (pour compatibilit�)
-                $lines[$i][0] = $obj->url;
-                $lines[$i][1] = $obj->url_id;
-                $lines[$i][2] = $obj->label;
-                $lines[$i][3] = $obj->type;
-                // Nouveaux liens
-                $lines[$i]['url'] = $obj->url;
-                $lines[$i]['url_id'] = $obj->url_id;
-                $lines[$i]['label'] = $obj->label;
-                $lines[$i]['type'] = $obj->type;
-                $i++;
-            }
-            return $lines;
-        }
-    }
+		if ($this->db->query($sql))
+		{
+			$rowid = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_url");
+			return $rowid;
+		}
+		else
+		{
+			$this->error=$this->db->error();
+			return -1;
+		}
+	}
+
+	/**
+	 *      \brief      Renvoi tableau des liens
+	 *      \param      line_id         Id ligne �criture
+	 *      \retuen     array           Tableau des liens
+	 */
+	function get_url($line_id)
+	{
+		$lines = array();
+		$sql = "SELECT url_id, url, label, type";
+		$sql.= " FROM ".MAIN_DB_PREFIX."bank_url";
+		$sql.= " WHERE fk_bank = ".$line_id;
+		$sql.= " ORDER BY type, label";
+
+		$result = $this->db->query($sql);
+		if ($result)
+		{
+			$i = 0;
+			$num = $this->db->num_rows($result);
+			while ($i < $num)
+			{
+				$obj = $this->db->fetch_object($result);
+				// Anciens liens (pour compatibilit�)
+				$lines[$i][0] = $obj->url;
+				$lines[$i][1] = $obj->url_id;
+				$lines[$i][2] = $obj->label;
+				$lines[$i][3] = $obj->type;
+				// Nouveaux liens
+				$lines[$i]['url'] = $obj->url;
+				$lines[$i]['url_id'] = $obj->url_id;
+				$lines[$i]['label'] = $obj->label;
+				$lines[$i]['type'] = $obj->type;
+				$i++;
+			}
+			return $lines;
+		}
+	}
 
 	/**
 		\brief     	Ajoute une entree dans la table ".MAIN_DB_PREFIX."bank
@@ -172,7 +172,7 @@ class Account extends CommonObject
 		\param		$emetteur		Nom emetteur
 		\param		$banque			Banque emettrice
 		\return		int				Rowid of added entry, <0 si erreur
-	*/
+		*/
 	function addline($date, $oper, $label, $amount, $num_chq='', $categorie='', $user, $emetteur='',$banque='')
 	{
 		// Clean parameters
@@ -202,7 +202,7 @@ class Account extends CommonObject
 				$oper = 'CHQ';
 				break;
 		}
-		
+
 		// Check parameters
 		if (! $oper)
 		{
@@ -219,8 +219,8 @@ class Account extends CommonObject
 			$this->error="ErrorCashAccountAcceptsOnlyCashMoney";
 			return -3;
 		}
-		
-		
+
+
 		$this->db->begin();
 			
 		$datev = $date;
@@ -262,267 +262,267 @@ class Account extends CommonObject
 		}
 	}
 
-    /*
-     *      \brief          Creation du compte bancaire en base
-     *      \return         int     < 0 si erreur, > 0 si ok
-     */
-    function create()
-    {
-        global $langs;
+	/*
+	 *      \brief          Creation du compte bancaire en base
+	 *      \return         int     < 0 si erreur, > 0 si ok
+	 */
+	function create()
+	{
+		global $langs;
 
 		// Verification parametres
 		if (! $this->min_allowed) $this->min_allowed=0;
 		if (! $this->min_desired) $this->min_desired=0;
-		
-        // Chargement librairie pour acces fonction controle RIB
-        require_once DOL_DOCUMENT_ROOT.'/lib/bank.lib.php';
-
-        if (! verif_rib($this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban_prefix)) {
-            $this->error="Le controle de la cle indique que les informations de votre compte bancaire sont incorrectes.";
-            return 0;
-        }
-
-        if (! $this->ref)
-        {
-        	$this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Ref"));
-        	return -1;
-        }
-
-        $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
-        $sql.= "datec, ref, label, account_number, currency_code, ";
-        $sql.= "rappro, min_allowed, min_desired, ";
-        $sql.= "comment";
-        $sql.= ") values (";
-        $sql.= "now(),'" . addslashes($this->ref) . "', '" . addslashes($this->label) . "', ";
-        $sql.= "'".addslashes($this->account_number) . "', '".$this->currency_code."', ";
-        $sql.= $this->rappro.", ".price2num($this->min_allowed).", ".price2num($this->min_desired).", ";
-        $sql.= "'".addslashes($this->comment)."'";
-        $sql.= ")";
+
+		// Chargement librairie pour acces fonction controle RIB
+		require_once DOL_DOCUMENT_ROOT.'/lib/bank.lib.php';
+
+		if (! verif_rib($this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban_prefix)) {
+			$this->error="Le controle de la cle indique que les informations de votre compte bancaire sont incorrectes.";
+			return 0;
+		}
+
+		if (! $this->ref)
+		{
+			$this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Ref"));
+			return -1;
+		}
+
+		$sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_account (";
+		$sql.= "datec, ref, label, account_number, currency_code, ";
+		$sql.= "rappro, min_allowed, min_desired, ";
+		$sql.= "comment";
+		$sql.= ") values (";
+		$sql.= "now(),'" . addslashes($this->ref) . "', '" . addslashes($this->label) . "', ";
+		$sql.= "'".addslashes($this->account_number) . "', '".$this->currency_code."', ";
+		$sql.= $this->rappro.", ".price2num($this->min_allowed).", ".price2num($this->min_desired).", ";
+		$sql.= "'".addslashes($this->comment)."'";
+		$sql.= ")";
 
 		dolibarr_syslog("Account::create sql=".$sql);
-        $resql=$this->db->query($sql);
-        if ($resql)
-        {
-            if ($this->db->affected_rows($resql))
-            {
-                $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
-                if ( $this->update() )
-                {
-                    $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (datec, label, amount, fk_account, datev, dateo, fk_type, rappro) ";
-                    $sql .= " VALUES (now(),'(".$langs->trans("InitialBankBalance").")'," . price2num($this->solde) . ",'$this->id','".$this->db->idate($this->date_solde)."','".$this->db->idate($this->date_solde)."','SOLD',1);";
-                    $this->db->query($sql);
-                }
-                return $this->id;
-            }
-        }
-        else
-        {
-            if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
-            {
-                $this->error=$langs->trans("ErrorBankLabelAlreadyExists");
+		$resql=$this->db->query($sql);
+		if ($resql)
+		{
+			if ($this->db->affected_rows($resql))
+			{
+				$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_account");
+				if ( $this->update() )
+				{
+					$sql = "INSERT INTO ".MAIN_DB_PREFIX."bank (datec, label, amount, fk_account, datev, dateo, fk_type, rappro) ";
+					$sql .= " VALUES (now(),'(".$langs->trans("InitialBankBalance").")'," . price2num($this->solde) . ",'$this->id','".$this->db->idate($this->date_solde)."','".$this->db->idate($this->date_solde)."','SOLD',1);";
+					$this->db->query($sql);
+				}
+				return $this->id;
+			}
+		}
+		else
+		{
+			if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
+			{
+				$this->error=$langs->trans("ErrorBankLabelAlreadyExists");
 				dolibarr_syslog($this->error);
-                return -1;
-            }
-            else {
-                $this->error=$this->db->error()." sql=".$sql;
+				return -1;
+			}
+			else {
+				$this->error=$this->db->error()." sql=".$sql;
 				dolibarr_syslog($this->error);
-                return -2;
-            }
-        }
-    }
-
-    /*
-     *    	\brief      Mise a jour compte, partie generale
-     *    	\param      user        Object utilisateur qui modifie
+				return -2;
+			}
+		}
+	}
+
+	/*
+	 *    	\brief      Mise a jour compte, partie generale
+	 *    	\param      user        Object utilisateur qui modifie
 	 *		\return		int			<0 si ko, >0 si ok
-     */
-    function update($user='')
-    {
-        global $langs;
-        
+	 */
+	function update($user='')
+	{
+		global $langs;
+
 		dolibarr_syslog("Account::update");
 
-        if (! $this->ref)
-        {
-        	$this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Ref"));
-        	return -1;
-        }
-        if (! $this->label) $this->label = "???";
+		if (! $this->ref)
+		{
+			$this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Ref"));
+			return -1;
+		}
+		if (! $this->label) $this->label = "???";
 
-        $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
+		$sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
 
-        $sql .= " ref   = '".addslashes($this->ref)."'";
-        $sql .= ",label = '".addslashes($this->label)."'";
+		$sql .= " ref   = '".addslashes($this->ref)."'";
+		$sql .= ",label = '".addslashes($this->label)."'";
 
-        $sql .= ",courant = ".$this->courant;
-        $sql .= ",clos = ".$this->clos;
-        $sql .= ",rappro = ".$this->rappro;
-        $sql .= ",url = ".($this->url?"'".$this->url."'":"null");
-        $sql .= ",account_number = '".$this->account_number."'";
+		$sql .= ",courant = ".$this->courant;
+		$sql .= ",clos = ".$this->clos;
+		$sql .= ",rappro = ".$this->rappro;
+		$sql .= ",url = ".($this->url?"'".$this->url."'":"null");
+		$sql .= ",account_number = '".$this->account_number."'";
 
-        $sql .= ",currency_code = '".$this->currency_code."'";
+		$sql .= ",currency_code = '".$this->currency_code."'";
 
-        $sql .= ",min_allowed = '".price2num($this->min_allowed)."'";
-        $sql .= ",min_desired = '".price2num($this->min_desired)."'";
-        $sql .= ",comment     = '".addslashes($this->comment)."'";
+		$sql .= ",min_allowed = '".price2num($this->min_allowed)."'";
+		$sql .= ",min_desired = '".price2num($this->min_desired)."'";
+		$sql .= ",comment     = '".addslashes($this->comment)."'";
 
-        $sql .= " WHERE rowid = ".$this->id;
+		$sql .= " WHERE rowid = ".$this->id;
 
 		dolibarr_syslog("Account::update sql=$sql");
-        $result = $this->db->query($sql);
-        if ($result)
-        {
-            return 1;
-        }
-        else
-        {
-        	$this->error=$this->db.' sql='.$sql;
-            dolibarr_print_error($this->error);
-            return -1;
-        }
-    }
-
-
-    /*
-     *    	\brief      Mise a jour compte, partie RIB
-     *    	\param      user        Object utilisateur qui modifie
+		$result = $this->db->query($sql);
+		if ($result)
+		{
+			return 1;
+		}
+		else
+		{
+			$this->error=$this->db.' sql='.$sql;
+			dolibarr_print_error($this->error);
+			return -1;
+		}
+	}
+
+
+	/*
+	 *    	\brief      Mise a jour compte, partie RIB
+	 *    	\param      user        Object utilisateur qui modifie
 	 *		\return		int			<0 si ko, >0 si ok
-     */
-    function update_rib($user='')
-    {
-        global $langs;
-        
-        // Chargement librairie pour acces fonction controle RIB
-        require_once(DOL_DOCUMENT_ROOT.'/lib/bank.lib.php');
+	 */
+	function update_rib($user='')
+	{
+		global $langs;
+
+		// Chargement librairie pour acces fonction controle RIB
+		require_once(DOL_DOCUMENT_ROOT.'/lib/bank.lib.php');
 
 		dolibarr_syslog("Account::update $this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban_prefix");
 
-        // Verification parametres
-        if (! verif_rib($this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban_prefix)) {
-            $this->error="Le contr�le de la cl� indique que les informations de votre compte bancaire sont incorrectes.";
-            return 0;
-        }
-
-        if (! $this->ref)
-        {
-        	$this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Ref"));
-        	return -1;
-        }
-
-        $sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
-        $sql .= " bank  = '".addslashes($this->bank)."'";
-        $sql .= ",code_banque='".$this->code_banque."'";
-        $sql .= ",code_guichet='".$this->code_guichet."'";
-        $sql .= ",number='".$this->number."'";
-        $sql .= ",cle_rib='".$this->cle_rib."'";
-        $sql .= ",bic='".$this->bic."'";
-        $sql .= ",iban_prefix = '".$this->iban_prefix."'";
-        $sql .= ",domiciliation='".addslashes($this->domiciliation)."'";
-        $sql .= ",proprio = '".addslashes($this->proprio)."'";
-        $sql .= ",adresse_proprio = '".addslashes($this->adresse_proprio)."'";
-        $sql .= " WHERE rowid = ".$this->id;
+		// Verification parametres
+		if (! verif_rib($this->code_banque,$this->code_guichet,$this->number,$this->cle_rib,$this->iban_prefix)) {
+			$this->error="Le contr�le de la cl� indique que les informations de votre compte bancaire sont incorrectes.";
+			return 0;
+		}
+
+		if (! $this->ref)
+		{
+			$this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Ref"));
+			return -1;
+		}
+
+		$sql = "UPDATE ".MAIN_DB_PREFIX."bank_account SET ";
+		$sql .= " bank  = '".addslashes($this->bank)."'";
+		$sql .= ",code_banque='".$this->code_banque."'";
+		$sql .= ",code_guichet='".$this->code_guichet."'";
+		$sql .= ",number='".$this->number."'";
+		$sql .= ",cle_rib='".$this->cle_rib."'";
+		$sql .= ",bic='".$this->bic."'";
+		$sql .= ",iban_prefix = '".$this->iban_prefix."'";
+		$sql .= ",domiciliation='".addslashes($this->domiciliation)."'";
+		$sql .= ",proprio = '".addslashes($this->proprio)."'";
+		$sql .= ",adresse_proprio = '".addslashes($this->adresse_proprio)."'";
+		$sql .= " WHERE rowid = ".$this->id;
 
 		dolibarr_syslog("Account::update_rib sql=$sql");
 
-        $result = $this->db->query($sql);
-        if ($result)
-        {
-            return 1;
-        }
-        else
-        {
-        	$this->error=$this->db.' sql='.$sql;
-            dolibarr_print_error($this->error);
-            return -1;
-        }
-    }
-    
-    
-    /*
-     *      \brief      Charge un compte en memoire depuis la base
-     *      \param      id      Id du compte � r�cup�rer
-     *      \param      ref     Ref du compte � r�cup�rer
-     */
-    function fetch($id,$ref='')
-    {
-        $sql = "SELECT rowid, ref, label, bank, number, courant, clos, rappro, url,";
-        $sql.= " code_banque, code_guichet, cle_rib, bic, iban_prefix,";
-        $sql.= " domiciliation, proprio, adresse_proprio,";
-        $sql.= " account_number, currency_code,";
-        $sql.= " min_allowed, min_desired, comment";
-        $sql.= " FROM ".MAIN_DB_PREFIX."bank_account";
-        if ($id)  $sql.= " WHERE rowid  = ".$id;
-        if ($ref) $sql.= " WHERE ref = '".addslashes($ref)."'";
+		$result = $this->db->query($sql);
+		if ($result)
+		{
+			return 1;
+		}
+		else
+		{
+			$this->error=$this->db.' sql='.$sql;
+			dolibarr_print_error($this->error);
+			return -1;
+		}
+	}
+
+
+	/*
+	 *      \brief      Charge un compte en memoire depuis la base
+	 *      \param      id      Id du compte � r�cup�rer
+	 *      \param      ref     Ref du compte � r�cup�rer
+	 */
+	function fetch($id,$ref='')
+	{
+		$sql = "SELECT rowid, ref, label, bank, number, courant, clos, rappro, url,";
+		$sql.= " code_banque, code_guichet, cle_rib, bic, iban_prefix,";
+		$sql.= " domiciliation, proprio, adresse_proprio,";
+		$sql.= " account_number, currency_code,";
+		$sql.= " min_allowed, min_desired, comment";
+		$sql.= " FROM ".MAIN_DB_PREFIX."bank_account";
+		if ($id)  $sql.= " WHERE rowid  = ".$id;
+		if ($ref) $sql.= " WHERE ref = '".addslashes($ref)."'";
 
 		dolibarr_syslog("Account::fetch sql=".$sql);
-        $result = $this->db->query($sql);
-        if ($result)
-        {
-            if ($this->db->num_rows($result))
-            {
-                $obj = $this->db->fetch_object($result);
+		$result = $this->db->query($sql);
+		if ($result)
+		{
+			if ($this->db->num_rows($result))
+			{
+				$obj = $this->db->fetch_object($result);
 
 				$this->id            = $obj->rowid;		// deprecated
 				$this->rowid         = $obj->rowid;
 				$this->ref           = $obj->ref;
-                $this->label         = $obj->label;
-                $this->type          = $obj->courant;
-                $this->courant       = $obj->courant;
-                $this->bank          = $obj->bank;
-                $this->clos          = $obj->clos;
-                $this->rappro        = $obj->rappro;
-                $this->url           = $obj->url;
-                
-                $this->code_banque   = $obj->code_banque;
-                $this->code_guichet  = $obj->code_guichet;
-                $this->number        = $obj->number;
-                $this->cle_rib       = $obj->cle_rib;
-                $this->bic           = $obj->bic;
-                $this->iban_prefix   = $obj->iban_prefix;
-                $this->domiciliation = $obj->domiciliation;
-                $this->proprio       = $obj->proprio;
-                $this->adresse_proprio = $obj->adresse_proprio;
-
-                $this->account_number = $obj->account_number;
-
-                $this->currency_code  = $obj->currency_code;
-                $this->min_allowed    = $obj->min_allowed;
-                $this->min_desired    = $obj->min_desired;
-                $this->comment        = $obj->comment;
+				$this->label         = $obj->label;
+				$this->type          = $obj->courant;
+				$this->courant       = $obj->courant;
+				$this->bank          = $obj->bank;
+				$this->clos          = $obj->clos;
+				$this->rappro        = $obj->rappro;
+				$this->url           = $obj->url;
+
+				$this->code_banque   = $obj->code_banque;
+				$this->code_guichet  = $obj->code_guichet;
+				$this->number        = $obj->number;
+				$this->cle_rib       = $obj->cle_rib;
+				$this->bic           = $obj->bic;
+				$this->iban_prefix   = $obj->iban_prefix;
+				$this->domiciliation = $obj->domiciliation;
+				$this->proprio       = $obj->proprio;
+				$this->adresse_proprio = $obj->adresse_proprio;
+
+				$this->account_number = $obj->account_number;
+
+				$this->currency_code  = $obj->currency_code;
+				$this->min_allowed    = $obj->min_allowed;
+				$this->min_desired    = $obj->min_desired;
+				$this->comment        = $obj->comment;
 				return 1;
-            }
+			}
 			else
 			{
 				return 0;
 			}
-            $this->db->free($result);
-        }
-        else
-        {
-            dolibarr_print_error($this->db);
+			$this->db->free($result);
+		}
+		else
+		{
+			dolibarr_print_error($this->db);
 			return -1;
-        }
-    }
+		}
+	}
 
 
-    /*
-     *    \brief      Efface le compte
-     */
-    function delete()
-    {
-        $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account";
-        $sql .= " WHERE rowid  = ".$this->rowid;
+	/*
+	 *    \brief      Efface le compte
+	 */
+	function delete()
+	{
+		$sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account";
+		$sql .= " WHERE rowid  = ".$this->rowid;
 
 		dolibarr_syslog("Account::delete sql=".$sql);
-        $result = $this->db->query($sql);
-        if ($result) {
-            return 1;
-        }
-        else {
-            dolibarr_print_error($this->db);
-            return -1;
-        }
-    }
+		$result = $this->db->query($sql);
+		if ($result) {
+			return 1;
+		}
+		else {
+			dolibarr_print_error($this->db);
+			return -1;
+		}
+	}
 
 
 	/**
@@ -578,149 +578,152 @@ class Account extends CommonObject
 		}
 	}
 
-	
+
+	/*
+	 *    \brief      Renvoi si un compte peut etre supprimer ou non (sans mouvements)
+	 *    \return     boolean     vrai si peut etre supprim�, faux sinon
+	 */
+	function can_be_deleted()
+	{
+		$can_be_deleted=false;
+
+		$sql = "SELECT COUNT(rowid) as nb";
+		$sql.= " FROM ".MAIN_DB_PREFIX."bank";
+		$sql.= " WHERE fk_account=".$this->id;
+		$resql = $this->db->query($sql);
+		if ($resql) {
+			$obj=$this->db->fetch_object($resql);
+			if ($obj->nb <= 1) $can_be_deleted=true;    // Juste le solde
+		}
+		else {
+			dolibarr_print_error($this->db);
+		}
+		return $can_be_deleted;
+	}
+
+
 	/*
-     *    \brief      Renvoi si un compte peut etre supprimer ou non (sans mouvements)
-     *    \return     boolean     vrai si peut etre supprim�, faux sinon
-     */
-    function can_be_deleted()
-    {
-        $can_be_deleted=false;
-
-        $sql = "SELECT COUNT(rowid) as nb";
-        $sql.= " FROM ".MAIN_DB_PREFIX."bank";
-        $sql.= " WHERE fk_account=".$this->id;
-        $resql = $this->db->query($sql);
-        if ($resql) {
-            $obj=$this->db->fetch_object($resql);
-            if ($obj->nb <= 1) $can_be_deleted=true;    // Juste le solde
-        }
-        else {
-            dolibarr_print_error($this->db);
-        }
-        return $can_be_deleted;
-    }
-
-
-    /*
-     *
-     */
-    function error()
-    {
-        return $this->error;
-    }
-
-    /*
-     *
-     */
-    function solde()
-    {
-        $sql = "SELECT sum(amount) as amount FROM ".MAIN_DB_PREFIX."bank";
-        $sql.= " WHERE fk_account=".$this->id." AND dateo <= ".$this->db->idate(time());
-
-        $resql = $this->db->query($sql);
-        if ($resql)
-        {
-            if ($this->db->num_rows($resql))
-            {
-            	$obj=$this->db->fetch_object($resql);
-                $solde = $obj->amount;
-            }
-            $this->db->free($resql);
-            return $solde;
-        }
-    }
-
-    /*
-     *
-     */
-    function datev_next($rowid)
-    {
-        $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET ";
-
-        $sql .= " datev = adddate(datev, interval 1 day)";
-
-        $sql .= " WHERE rowid = $rowid";
-
-        $result = $this->db->query($sql);
-
-        if ($result)
-        {
-            if ($this->db->affected_rows())
-            {
-                return 1;
-            }
-        }
-        else
-        {
-            dolibarr_print_error($this->db);
-            return 0;
-        }
-    }
- 
-    /*
-     *
-     */
-    function datev_previous($rowid)
-    {
-        $sql = "UPDATE ".MAIN_DB_PREFIX."bank SET ";
-
-        $sql .= " datev = adddate(datev, interval -1 day)";
-
-        $sql .= " WHERE rowid = $rowid";
-
-        $result = $this->db->query($sql);
-
-        if ($result)
-        {
-            if ($this->db->affected_rows())
-            {
-                return 1;
-            }
-        }
-        else
-        {
-            dolibarr_print_error($this->db);
-            return 0;
-        }
-    }
-
-
-    /**
-     *      \brief      Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord
-     *      \param      user        		Objet user
+	 *
+	 */
+	function error()
+	{
+		return $this->error;
+	}
+
+	/**	
+	 * 	\brief		Return current sold
+	 * 	\param		option		1=Exclude future operation date (this is to exclude input made in advance and have real account sold)
+	 *	\return		int			Current sold (value date <= today)
+	 */
+	function solde($option=0)
+	{
+		$sql = "SELECT sum(amount) as amount FROM ".MAIN_DB_PREFIX."bank";
+		$sql.= " WHERE fk_account=".$this->id;
+		if ($option == 1) $sql.= " AND dateo <= ".$this->db->idate(time());
+
+		$resql = $this->db->query($sql);
+		if ($resql)
+		{
+			if ($this->db->num_rows($resql))
+			{
+				$obj=$this->db->fetch_object($resql);
+				$solde = $obj->amount;
+			}
+			$this->db->free($resql);
+			return $solde;
+		}
+	}
+
+	/*
+	 *
+	 */
+	function datev_next($rowid)
+	{
+		$sql = "UPDATE ".MAIN_DB_PREFIX."bank SET ";
+
+		$sql .= " datev = adddate(datev, interval 1 day)";
+
+		$sql .= " WHERE rowid = $rowid";
+
+		$result = $this->db->query($sql);
+
+		if ($result)
+		{
+			if ($this->db->affected_rows())
+			{
+				return 1;
+			}
+		}
+		else
+		{
+			dolibarr_print_error($this->db);
+			return 0;
+		}
+	}
+
+	/*
+	 *
+	 */
+	function datev_previous($rowid)
+	{
+		$sql = "UPDATE ".MAIN_DB_PREFIX."bank SET ";
+
+		$sql .= " datev = adddate(datev, interval -1 day)";
+
+		$sql .= " WHERE rowid = $rowid";
+
+		$result = $this->db->query($sql);
+
+		if ($result)
+		{
+			if ($this->db->affected_rows())
+			{
+				return 1;
+			}
+		}
+		else
+		{
+			dolibarr_print_error($this->db);
+			return 0;
+		}
+	}
+
+
+	/**
+	 *      \brief      Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord
+	 *      \param      user        		Objet user
 	 *		\param		filteraccountid		To get info for a particular account id
-     *      \return     int         		<0 si ko, >0 si ok
-     */
-    function load_board($user,$filteraccountid=0)
-    {
-        global $conf;
-        
-        if ($user->societe_id) return -1;   // protection pour eviter appel par utilisateur externe
-
-        $this->nbtodo=$this->nbtodolate=0;
-        $sql = "SELECT b.rowid,".$this->db->pdate("b.datev")." as datefin";
-        $sql.= " FROM ".MAIN_DB_PREFIX."bank as b, ".MAIN_DB_PREFIX."bank_account as ba";
-        $sql.= " WHERE b.rappro=0 AND b.fk_account = ba.rowid";
-        $sql.= " AND ba.rappro = 1";	// Compte rapprochable
+	 *      \return     int         		<0 si ko, >0 si ok
+	 */
+	function load_board($user,$filteraccountid=0)
+	{
+		global $conf;
+
+		if ($user->societe_id) return -1;   // protection pour eviter appel par utilisateur externe
+
+		$this->nbtodo=$this->nbtodolate=0;
+		$sql = "SELECT b.rowid,".$this->db->pdate("b.datev")." as datefin";
+		$sql.= " FROM ".MAIN_DB_PREFIX."bank as b, ".MAIN_DB_PREFIX."bank_account as ba";
+		$sql.= " WHERE b.rappro=0 AND b.fk_account = ba.rowid";
+		$sql.= " AND ba.rappro = 1";	// Compte rapprochable
 		if ($filteraccountid) $sql.=" AND ba.rowid = ".$filteraccountid;
-        $resql=$this->db->query($sql);
-        if ($resql)
-        {
-            while ($obj=$this->db->fetch_object($resql))
-            {
-                $this->nbtodo++;
-                if ($obj->datefin < (time() - $conf->bank->rappro->warning_delay)) $this->nbtodolate++;
-            }
-            return 1;
-        }
-        else 
-        {
-            dolibarr_print_error($this->db);
-            $this->error=$this->db->error();
-            return -1;
-        }
-    }
+		$resql=$this->db->query($sql);
+		if ($resql)
+		{
+			while ($obj=$this->db->fetch_object($resql))
+			{
+				$this->nbtodo++;
+				if ($obj->datefin < (time() - $conf->bank->rappro->warning_delay)) $this->nbtodolate++;
+			}
+			return 1;
+		}
+		else
+		{
+			dolibarr_print_error($this->db);
+			$this->error=$this->db->error();
+			return -1;
+		}
+	}
 
 
 	/**
@@ -731,9 +734,9 @@ class Account extends CommonObject
 	function getNomUrl($withpicto=0)
 	{
 		global $langs;
-		
+
 		$result='';
-		
+
 		$lien = '<a href="'.DOL_URL_ROOT.'/compta/bank/fiche.php?id='.$this->id.'">';
 		$lienfin='</a>';
 
@@ -741,18 +744,18 @@ class Account extends CommonObject
 		$result.=$lien.$this->label.$lienfin;
 		return $result;
 	}
-	
+
 }
 
 
 /**
-   \class      AccountLine
-   \brief      Classe permettant la gestion des lignes de transactions bancaires
-*/
+ \class      AccountLine
+ \brief      Classe permettant la gestion des lignes de transactions bancaires
+ */
 class AccountLine
 {
-    var $db;
-    
+	var $db;
+
 	var $rowid;
 	var $rappro;
 	var $datec;
@@ -761,119 +764,119 @@ class AccountLine
 	var $amount;
 	var $label;
 	var $note;
-	
-	
-    /**
-     *  Constructeur
-     */
-    function AccountLine($DB, $rowid=0)
-    {
-        global $langs;
-
-        $this->db = $DB;
-        $this->rowid = $rowid;
-
-        return 1;
-    }
-
-    /**
-     *      \brief      Charge en memoire depuis la base, une ecriture sur le compte
-     *      \param      id      Id de la ligne �criture � r�cup�rer
+
+
+	/**
+	 *  Constructeur
+	 */
+	function AccountLine($DB, $rowid=0)
+	{
+		global $langs;
+
+		$this->db = $DB;
+		$this->rowid = $rowid;
+
+		return 1;
+	}
+
+	/**
+	 *      \brief      Charge en memoire depuis la base, une ecriture sur le compte
+	 *      \param      id      Id de la ligne �criture � r�cup�rer
 	 *		\return		int		<0 if KO, >0 if OK
-     */
-    function fetch($rowid)
-    {
-        $sql = "SELECT datec, datev, dateo, amount, label, fk_account,";
+	 */
+	function fetch($rowid)
+	{
+		$sql = "SELECT datec, datev, dateo, amount, label, fk_account,";
 		$sql.= " fk_user_author, fk_user_rappro,";
-        $sql.= " fk_type, num_releve, num_chq, rappro, note";
-        $sql.= " FROM ".MAIN_DB_PREFIX."bank";
-        $sql.= " WHERE rowid  = ".$rowid;
+		$sql.= " fk_type, num_releve, num_chq, rappro, note";
+		$sql.= " FROM ".MAIN_DB_PREFIX."bank";
+		$sql.= " WHERE rowid  = ".$rowid;
 
 		dolibarr_syslog("AccountLine::fetch sql=".$sql);
-        $result = $this->db->query($sql);
-        if ($result)
-        {
-            if ($this->db->num_rows($result))
-            {
-                $obj = $this->db->fetch_object($result);
-
-                $this->rowid         = $rowid;
-                $this->ref           = $rowid;
-
-                $this->datec         = $obj->datec;
-                $this->datev         = $obj->datev;
-                $this->dateo         = $obj->dateo;
-                $this->amount        = $obj->amount;
-                $this->label         = $obj->label;
+		$result = $this->db->query($sql);
+		if ($result)
+		{
+			if ($this->db->num_rows($result))
+			{
+				$obj = $this->db->fetch_object($result);
+
+				$this->rowid         = $rowid;
+				$this->ref           = $rowid;
+
+				$this->datec         = $obj->datec;
+				$this->datev         = $obj->datev;
+				$this->dateo         = $obj->dateo;
+				$this->amount        = $obj->amount;
+				$this->label         = $obj->label;
 				$this->fk_account    = $obj->fk_account;
-                $this->note          = $obj->note;
+				$this->note          = $obj->note;
 
-                $this->fk_user_author = $obj->fk_user_author;
-                $this->fk_user_rappro = $obj->fk_user_rappro;
+				$this->fk_user_author = $obj->fk_user_author;
+				$this->fk_user_rappro = $obj->fk_user_rappro;
 
-                $this->fk_type        = $obj->fk_type;
-                $this->num_releve     = $obj->num_releve;
-                $this->num_chq        = $obj->num_chq;
+				$this->fk_type        = $obj->fk_type;
+				$this->num_releve     = $obj->num_releve;
+				$this->num_chq        = $obj->num_chq;
 
-                $this->rappro        = $obj->rappro;
-            }
-            $this->db->free($result);
+				$this->rappro        = $obj->rappro;
+			}
+			$this->db->free($result);
 			return 1;
-        }
-        else
-        {
-            dolibarr_print_error($this->db);
+		}
+		else
+		{
+			dolibarr_print_error($this->db);
 			return -1;
-        }
-    }
+		}
+	}
 
 
-    /**
-     *      \brief      Efface ligne bancaire
+	/**
+	 *      \brief      Efface ligne bancaire
 	 *		\param		user	User object that delete
-     *      \return		int 	<0 si KO, >0 si OK
-     */
-    function delete($user=0)
-    {
-        $nbko=0;
-        
+	 *      \return		int 	<0 si KO, >0 si OK
+	 */
+	function delete($user=0)
+	{
+		$nbko=0;
+
 		if ($this->rappro)
 		{
 			// Protection pour eviter tout suppression d'une ligne consolid�e
 			$this->error="DeleteNotPossibleLineIsConsolidated";
 			return -1;
 		}
-		
-        $this->db->begin();
-        
-        $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".$this->rowid;
+
+		$this->db->begin();
+
+		$sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".$this->rowid;
 		dolibarr_syslog("AccountLine::delete sql=".$sql);
-        $result = $this->db->query($sql);
-        if (! $result) $nbko++;
+		$result = $this->db->query($sql);
+		if (! $result) $nbko++;
 
-        $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".$this->rowid;
+		$sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".$this->rowid;
 		dolibarr_syslog("AccountLine::delete sql=".$sql);
-        $result = $this->db->query($sql);
-        if (! $result) $nbko++;
+		$result = $this->db->query($sql);
+		if (! $result) $nbko++;
 
-        $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".$this->rowid;
+		$sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".$this->rowid;
 		dolibarr_syslog("AccountLine::delete sql=".$sql);
-        $result = $this->db->query($sql);
-        if (! $result) $nbko++;
-
-        if (! $nbko) 
-        {
-            $this->db->commit();
-            return 1;
-        }
-        else
-        {
-            $this->db->rollback();
-            return -$nbko;
-        }
+		$result = $this->db->query($sql);
+		if (! $result) $nbko++;
+
+		if (! $nbko)
+		{
+			$this->db->commit();
+			return 1;
+		}
+		else
+		{
+			$this->db->rollback();
+			return -$nbko;
+		}
 	}
-	
-	
+
+
 	/**
 	 *		\brief 		Met a jour en base la ligne
 	 *		\param 		user			Objet user qui met a jour
@@ -953,9 +956,9 @@ class AccountLine
 	function getNomUrl($withpicto=0)
 	{
 		global $langs;
-		
+
 		$result='';
-		
+
 		$lien = '<a href="'.DOL_URL_ROOT.'/compta/bank/ligne.php?rowid='.$this->rowid.'">';
 		$lienfin='</a>';
 
diff --git a/htdocs/compta/bank/index.php b/htdocs/compta/bank/index.php
index b83ea7e58ebf8a3fa5d689d8dca64a7da33b1f8d..9638af5f2f017f7f6205ae69f34e6fdf702ff011 100644
--- a/htdocs/compta/bank/index.php
+++ b/htdocs/compta/bank/index.php
@@ -94,7 +94,7 @@ foreach ($accounts as $key=>$type)
 		$acc->fetch($key);
 
 		$var = !$var;
-		$solde = $acc->solde();
+		$solde = $acc->solde(1);
 
 		print '<tr '.$bc[$var].'>';
 		print '<td width="30%">'.$acc->getNomUrl(1).'</td>';
@@ -148,7 +148,7 @@ foreach ($accounts as $key=>$type)
 		$acc->fetch($key);
 
 		$var = !$var;
-		$solde = $acc->solde();
+		$solde = $acc->solde(1);
 
 		print "<tr ".$bc[$var].">";
 		print '<td width="30%">'.$acc->getNomUrl(1).'</td>';
@@ -200,7 +200,7 @@ foreach ($accounts as $key=>$type)
         $acc->fetch($key);
 
         $var = !$var;
-        $solde = $acc->solde();
+        $solde = $acc->solde(1);
 
         print "<tr ".$bc[$var].">";
 		print '<td width="30%">'.$acc->getNomUrl(1).'</td>';
diff --git a/htdocs/compta/bank/treso.php b/htdocs/compta/bank/treso.php
index 860ba4fab59a1f85b856764fd92a3523213111e2..0d060c5a60c012c8d31c70732087b6271a1ca21b 100644
--- a/htdocs/compta/bank/treso.php
+++ b/htdocs/compta/bank/treso.php
@@ -19,25 +19,25 @@
  */
 
 /**
-	    \file       htdocs/compta/bank/treso.php
-		\ingroup    banque
-		\brief      Page de d�tail du budget de tr�sorerie
-		\version    $Id$
-*/
+ \file       htdocs/compta/bank/treso.php
+ \ingroup    banque
+ \brief      Page de d�tail du budget de tr�sorerie
+ \version    $Id$
+ */
 
 require("./pre.inc.php");
 require_once(DOL_DOCUMENT_ROOT."/lib/bank.lib.php");
+require_once(DOL_DOCUMENT_ROOT."/societe.class.php");
 require_once(DOL_DOCUMENT_ROOT.'/facture.class.php');
 require_once(DOL_DOCUMENT_ROOT.'/fourn/fournisseur.facture.class.php');
-require_once(DOL_DOCUMENT_ROOT."/societe.class.php");
+require_once(DOL_DOCUMENT_ROOT.'/chargesociales.class.php');
 
 $langs->load("banks");
 $langs->load("bills");
 
 if (!$user->admin && !$user->rights->banque)
-  accessforbidden();
+accessforbidden();
 
-$account=isset($_GET["account"])?$_GET["account"]:$_POST["account"];
 $vline=isset($_GET["vline"])?$_GET["vline"]:$_POST["vline"];
 $page=isset($_GET["page"])?$_GET["page"]:0;
 
@@ -46,14 +46,15 @@ $mesg='';
 
 
 /*
-* Affichage page
-*/
+ * View
+ */
 
 llxHeader();
 
 $societestatic = new Societe($db);
 $facturestatic=new Facture($db);
 $facturefournstatic=new FactureFournisseur($db);
+$socialcontribstatic=new ChargeSociales($db);
 
 $html = new Form($db);
 
@@ -69,11 +70,11 @@ if ($_REQUEST["account"] || $_REQUEST["ref"])
 	}
 
 	$acct = new Account($db);
-	if ($_GET["account"]) 
+	if ($_GET["account"])
 	{
 		$result=$acct->fetch($_GET["account"]);
 	}
-	if ($_GET["ref"]) 
+	if ($_GET["ref"])
 	{
 		$result=$acct->fetch(0,$_GET["ref"]);
 		$_GET["account"]=$acct->id;
@@ -81,13 +82,13 @@ if ($_REQUEST["account"] || $_REQUEST["ref"])
 
 
 	/*
-	*
-	*
-	*/
+	 *
+	 *
+	 */
 	// Onglets
 	$head=bank_prepare_head($acct);
 	dolibarr_fiche_head($head,'cash',$langs->trans("FinancialAccount"),0);
-	
+
 	print '<table class="border" width="100%">';
 
 	// Ref
@@ -103,173 +104,210 @@ if ($_REQUEST["account"] || $_REQUEST["ref"])
 	print '</table>';
 
 	print '<br>';
-	
-	
-	if ($mesg) print '<div class="error">'.$mesg.'</div>';
 
 
-	/*
-	* Calcul du solde du compte bancaire
-	*/
-	$sql = "SELECT sum( amount ) AS solde";
-	$sql.= " FROM ".MAIN_DB_PREFIX."bank";
-	$sql.= " WHERE fk_account =".$account;
+	if ($mesg) print '<div class="error">'.$mesg.'</div>';
 
-	$result = $db->query($sql);
-	if ($result)
-	{
-		$obj = $db->fetch_object($result);
-		if ($obj) $solde = $obj->solde;
-	}
 
-	/*
-	* Affiche tableau des echeances � venir
-	*
-	*/
+	$solde = $acct->solde(0);
 
+	/*
+	 * Affiche tableau des echeances � venir
+	 */
 	print '<table class="notopnoleftnoright" width="100% border="1">';
 
 	// Ligne de titre tableau des ecritures
 	print '<tr class="liste_titre">';
 	print '<td>'.$langs->trans("Invoices").'</td>';
 	print '<td>'.$langs->trans("ThirdParty").'</td>';
-	print '<td>'.$langs->trans("DateEcheance").'</td>';
+	print '<td align="center">'.$langs->trans("DateEcheance").'</td>';
 	print '<td align="right">'.$langs->trans("Debit").'</td>';
 	print '<td align="right">'.$langs->trans("Credit").'</td>';
 	print '<td align="right" width="80">'.$langs->trans("BankBalance").'</td>';
 	print '</tr>';
 
-	// Solde initial
-	print '<tr class="liste_total"><td align="left" colspan="5">'.$langs->trans("CurrentBalance").'</td>';
+	$var=true;
+
+	// Solde actuel
+	$var=!$var;
+	print '<tr '.$bc[$var].'>';
+	print '<td align="left" colspan="5">'.$langs->trans("CurrentBalance").'</td>';
+	print '<td align="right" nowrap>&nbsp;</td>';
+	print '</tr>';
+
+	$var=!$var;
+	print '<tr '.$bc[$var].'>';
+	print '<td align="left" colspan="5">&nbsp;</td>';
 	print '<td align="right" nowrap>'.price($solde).'</td>';
 	print '</tr>';
 
+	$var=!$var;
+	print '<tr '.$bc[$var].'>';
+	print '<td align="left" colspan="5">'.$langs->trans("RemainderToPay").'</td>';
+	print '<td align="right" nowrap>&nbsp;</td>';
+	print '</tr>';
+
 
+	// Remainder to pay in future
 
-	// Recuperation des factures clients et fournisseurs impayes
-	$sql = "SELECT f.rowid as facid, f.facnumber, f.total_ttc, f.type, ".$db->pdate("f.date_lim_reglement")." as dlr,";
+	// Customer invoices
+	$sql = "SELECT 'invoice' as family, f.rowid as objid, f.facnumber as ref, f.total_ttc, f.type, ".$db->pdate("f.date_lim_reglement")." as dlr,";
 	$sql.= " s.rowid as socid, s.nom, s.fournisseur";
 	$sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
 	$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON f.fk_soc = s.rowid";
-	$sql.= " WHERE f.paye = 0 AND fk_statut = 1";
+	$sql.= " WHERE f.paye = 0 AND fk_statut = 1";	// Not payed
 	$sql.= " ORDER BY dlr ASC";
-	//$sql.= " UNION DISTINCT";
-	$sql2= " SELECT ff.rowid as facid, ff.facnumber, (-1*ff.total_ttc) as total_ttc, ff.type, ".$db->pdate("ff.date_lim_reglement")." as dlr,";
+
+	// Supplier invoices
+	$sql2= " SELECT 'supplier_invoice' as family, ff.rowid as objid, ff.facnumber as ref, (-1*ff.total_ttc) as total_ttc, ff.type, ".$db->pdate("ff.date_lim_reglement")." as dlr,";
 	$sql2.= " s.rowid as socid, s.nom, s.fournisseur";
 	$sql2.= " FROM ".MAIN_DB_PREFIX."facture_fourn as ff";
 	$sql2.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON ff.fk_soc = s.rowid";
-	$sql2.= " WHERE ff.paye = 0 AND fk_statut = 1";
+	$sql2.= " WHERE ff.paye = 0 AND fk_statut = 1";	// Not payed
 	$sql2.= " ORDER BY dlr ASC";
-	
 
+	// Social contributions
+	$sql3= " SELECT 'social_contribution' as family, cs.rowid as objid, cs.libelle as ref, (-1*cs.amount) as total_ttc, ccs.libelle as type, ".$db->pdate("cs.date_ech")." as dlr";
+	$sql3.= " FROM ".MAIN_DB_PREFIX."chargesociales as cs";
+	$sql3.= " LEFT JOIN ".MAIN_DB_PREFIX."c_chargesociales as ccs ON cs.fk_type = ccs.id";
+	$sql3.= " WHERE cs.paye = 0";	// Not payed
+	$sql3.= " ORDER BY dlr ASC";
+
+	$error=0;
+	$tab_sqlobj=array();
+	
+	// List customer invoices
 	$result = $db->query($sql);
-	$result2=false;
-	if ($result) 
-	{
-		$result2=$db->query($sql2);
-	}
-	if ($result2)
+	if ($result)
 	{
-		$tab_sqlobj=array();
-
 		$num = $db->num_rows($result);
 		for ($i = 0;$i < $num;$i++)
-			{
+		{
 			$sqlobj = $db->fetch_object($result);
 			$tab_sqlobj[] = $sqlobj;
 			$tab_sqlobjOrder[]= $sqlobj->dlr;
-			}
-		$db->free($result);	
-		
+		}
+		$db->free($result);
+	}
+	else $error++;
+	
+	// List supplier invoices
+	$result2=$db->query($sql2);
+	if ($result2)
+	{
 		$num = $db->num_rows($result2);
 		for ($i = 0;$i < $num;$i++)
-			{
+		{
 			$sqlobj = $db->fetch_object($result2);
 			$tab_sqlobj[] = $sqlobj;
 			$tab_sqlobjOrder[]= $sqlobj->dlr;
-			}
+		}
 		$db->free($result2);
+	}
+	else $error++;
+	
+	// List social contributions
+	$result3=$db->query($sql3);
+	if ($result3)
+	{
+		$num = $db->num_rows($result3);
 
+		for ($i = 0;$i < $num;$i++)
+		{
+			$sqlobj = $db->fetch_object($result3);
+			$tab_sqlobj[] = $sqlobj;
+			$tab_sqlobjOrder[]= $sqlobj->dlr;
+		}
+		$db->free($result3);
+	}
+	else $error++;
+	
+	
+	// Sort array
+	if (! $error)
+	{
 		array_multisort ($tab_sqlobjOrder,$tab_sqlobj);
-		
+
 		//Apply distinct filter
-		foreach ($tab_sqlobj as $key=>$value) { 
-		  $tab_sqlobj[$key] = "'" . serialize($value) . "'"; 
-		} 
-		$tab_sqlobj = array_unique($tab_sqlobj); 
-		foreach ($tab_sqlobj as $key=>$value) { 
-		  $tab_sqlobj[$key] = unserialize(trim($value, "'")); 
-		} 
-		
+		foreach ($tab_sqlobj as $key=>$value) {
+			$tab_sqlobj[$key] = "'" . serialize($value) . "'";
+		}
+		$tab_sqlobj = array_unique($tab_sqlobj);
+		foreach ($tab_sqlobj as $key=>$value) {
+			$tab_sqlobj[$key] = unserialize(trim($value, "'"));
+		}
+
 		$num = sizeOf($tab_sqlobj);
-	
+
 		$var=False;
 		//$num = $db->num_rows($result);
 		$i = 0;
 		while ($i < $num)
 		{
 			$paiement = '';
+			$ref = '';
+			$refcomp = '';
+			
 			$var=!$var;
 			//$obj = $db->fetch_object($result);
 			$obj = array_shift($tab_sqlobj);
-			
-			$societestatic->id = $obj->socid;
-			$societestatic->nom = $obj->nom;
-
-			// Todo: Ajouter gestion des avoirs fournisseurs, champ ff.type = 2
-			if ($obj->fournisseur == 1 && ($obj->total_ttc < 0 && $obj->type != 2) || ($obj->total_ttc > 0 && $obj->type == 2))
-			{
-				$facturefournstatic->ref=$obj->facnumber;
-				$facturefournstatic->id=$obj->facid;
-				$facturefournstatic->type=$obj->type;
-				$facture = $facturefournstatic->getNomUrl(1,'');
 				
-				// On recherche les paiements deja effectue pour les deduires
-				$sqlp = "SELECT sum(-1*amount) as paiement";
-				$sqlp.= " FROM ".MAIN_DB_PREFIX.'paiementfourn_facturefourn';
-				$sqlp.= " WHERE fk_facturefourn = ".$obj->facid;
-				$resql = $db->query($sqlp);
-				if ($resql)
+			if ($obj->family == 'supplier_invoice')
+			{
+				// \TODO This code is to avoid to count suppliers credit note (ff.type = 2)
+				// Ajouter gestion des avoirs fournisseurs, champ 
+				if (($obj->total_ttc < 0 && $obj->type != 2)
+				 || ($obj->total_ttc > 0 && $obj->type == 2))
 				{
-					$objp = $db->fetch_object($resql);
-					if ($objp) $paiement = $objp->paiement;
+					$facturefournstatic->ref=$obj->ref;
+					$facturefournstatic->id=$obj->objid;
+					$facturefournstatic->type=$obj->type;
+					$ref = $facturefournstatic->getNomUrl(1,'');
+
+					$societestatic->id = $obj->socid;
+					$societestatic->nom = $obj->nom;
+					$refcomp=$societestatic->getNomUrl(0,'',24);
+
+					$paiement = -1*$facturefournstatic->getSommePaiement();	// Payment already done
 				}
 			}
-			else
+			if ($obj->family == 'invoice')
 			{
-				$facturestatic->ref=$obj->facnumber;
-				$facturestatic->id=$obj->facid;
+				$facturestatic->ref=$obj->ref;
+				$facturestatic->id=$obj->objid;
 				$facturestatic->type=$obj->type;
-				$facture = $facturestatic->getNomUrl(1,'');
-				
-				// On recherche les paiements deja effectue pour les deduires
-				$sqlp = "SELECT sum(amount) as paiement";
-				$sqlp.= " FROM ".MAIN_DB_PREFIX.'paiement_facture';
-				$sqlp.= " WHERE fk_facture = ".$obj->facid;
-				$resql = $db->query($sqlp);
-				if ($resql)
-				{
-					$objp = $db->fetch_object($resql);
-					if ($objp) $paiement = $objp->paiement;
-				}
-			}
+				$ref = $facturestatic->getNomUrl(1,'');
+
+				$societestatic->id = $obj->socid;
+				$societestatic->nom = $obj->nom;
+				$refcomp=$societestatic->getNomUrl(0,'',24);
 
+				$paiement = $facturestatic->getSommePaiement();	// Payment already done
+			}
+			if ($obj->family == 'social_contribution')
+			{
+				$socialcontribstatic->ref=$obj->ref;
+				$socialcontribstatic->id=$obj->objid;
+				$socialcontribstatic->lib=$obj->type;
+				$ref = $socialcontribstatic->getNomUrl(1,24);
+			
+				$paiement = $socialcontribstatic->getSommePaiement();	// Payment already done
+			}
+			
 			$total_ttc = $obj->total_ttc;
 			if ($paiement) $total_ttc = $obj->total_ttc - $paiement;
 			$solde += $total_ttc;
 
 			print "<tr $bc[$var]>";
-			print "<td>".$facture."</td>";
-			print "<td>".$societestatic->getNomUrl(0,'',16)."</td>";
-			print "<td>".dolibarr_print_date($obj->dlr,"day")."</td>";
-
+			print "<td>".$ref."</td>";
+			print "<td>".$refcomp."</td>";
+			print '<td align="center">'.dolibarr_print_date($obj->dlr,"day")."</td>";
 			if ($obj->total_ttc < 0) { print "<td align=\"right\">".price($total_ttc)."</td><td>&nbsp;</td>"; };
-			if ($obj->total_ttc >= 0) { print "<td>&nbsp;</td><td align=\"right\">".price($total_ttc)."</td>"; };			
+			if ($obj->total_ttc >= 0) { print "<td>&nbsp;</td><td align=\"right\">".price($total_ttc)."</td>"; };
 			print "<td align=\"right\">".price($solde)."</td>";
 			print "</tr>";
 			$i++;
 		}
-		//$db->free($result);
 	}
 	else
 	{
diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php
index 87054929e8b479600a6586b53253f7eb7f144e99..d9d8acebbed0503f22e344d1de1c99804b26f4df 100644
--- a/htdocs/facture.class.php
+++ b/htdocs/facture.class.php
@@ -35,10 +35,9 @@ require_once(DOL_DOCUMENT_ROOT ."/product.class.php");
 require_once(DOL_DOCUMENT_ROOT ."/client.class.php");
 
 /**
- \class      Facture
- \brief      Classe permettant la gestion des factures clients
+ *	\class      Facture
+ *	\brief      Classe permettant la gestion des factures clients
  */
-
 class Facture extends CommonObject
 {
 	var $db;
@@ -1639,8 +1638,8 @@ class Facture extends CommonObject
 
 
 	/**
-	 * 	\brief     	Renvoie la sommes des paiements deja effectu�s
-	 *	\return		Montant deja vers�, <0 si ko
+	 * 	\brief     	Return amount aof payments already done
+	 *	\return		int		Amount of payment already done, <0 if KO
 	 */
 	function getSommePaiement()
 	{
@@ -1661,6 +1660,7 @@ class Facture extends CommonObject
 		if ($resql)
 		{
 			$obj = $this->db->fetch_object($resql);
+			$this->db->free($resql);
 			return $obj->amount;
 		}
 		else
diff --git a/htdocs/includes/boxes/box_comptes.php b/htdocs/includes/boxes/box_comptes.php
index c3441b77e27cee6cc0b85c6dea6012c13ab52e56..330375fd09213c728de47cacd86600f48da812d8 100644
--- a/htdocs/includes/boxes/box_comptes.php
+++ b/htdocs/includes/boxes/box_comptes.php
@@ -92,7 +92,7 @@ class box_comptes extends ModeleBoxes {
 					$objp = $db->fetch_object($result);
 					
 					$account_static->id = $objp->rowid;
-					$solde=$account_static->solde();
+					$solde=$account_static->solde(0);
 					
 					$solde_total += $solde;
 
diff --git a/htdocs/langs/en_US/bills.lang b/htdocs/langs/en_US/bills.lang
index fa5f44309b37039c83503817025b7f2e5b82a992..c6ab4dd14d78a5c2d179a68d4bc73b19d88692a5 100644
--- a/htdocs/langs/en_US/bills.lang
+++ b/htdocs/langs/en_US/bills.lang
@@ -152,6 +152,7 @@ ConfirmCustomerPayment=Do you confirm this paiement input for <b>%s</b> %s ?
 ValidateBill=Validate invoice
 NumberOfBills=Nb of invoices
 NumberOfBillsByMonth=Nb of invoices by month
+ShowSocialContribution=Show social contribution
 ShowBill=Show invoice
 ShowInvoice=Show invoice
 ShowInvoiceReplace=Show replacing invoice
diff --git a/htdocs/langs/fr_BE/bills.lang b/htdocs/langs/fr_BE/bills.lang
index bc5c71fd85c5a5fc71056d7789c36d39292b1230..7072acd36f56701f8d95565d1513afccf290b630 100644
--- a/htdocs/langs/fr_BE/bills.lang
+++ b/htdocs/langs/fr_BE/bills.lang
@@ -122,6 +122,7 @@ ConfirmClassifyPayedPartiallyAbandon=Ce choix sera celui dans le cas d'un mauvai
 ValidateBill=Valider facture
 NumberOfBills=Nb de factures
 NumberOfBillsByMonth=Nb de factures par mois
+ShowSocialContribution=Afficher charge sociale
 ShowBill=Afficher facture
 ShowInvoice=Afficher la facture
 ShowInvoiceReplace=Afficher la facture de remplacement
diff --git a/htdocs/langs/fr_FR/bills.lang b/htdocs/langs/fr_FR/bills.lang
index 7636ca255aae21b0e4f3daca073a741e04bb461b..5c5c20f240fe53568a4026795478aecb50c8dd08 100644
--- a/htdocs/langs/fr_FR/bills.lang
+++ b/htdocs/langs/fr_FR/bills.lang
@@ -152,6 +152,7 @@ ConfirmCustomerPayment=Confirmez-vous la saisie de ce r
 ValidateBill=Valider facture
 NumberOfBills=Nb de factures
 NumberOfBillsByMonth=Nb de factures par mois
+ShowSocialContribution=Afficher charge sociale
 ShowBill=Afficher facture
 ShowInvoice=Afficher facture
 ShowInvoiceReplace=Afficher facture de remplacement
diff --git a/htdocs/lib/databases/mysql.lib.php b/htdocs/lib/databases/mysql.lib.php
index e6aabeda0524e0560dd4d3ac61e195b6bf058851..d48f39e130486ec05b6ce63acf0d4d2f9f68336e 100644
--- a/htdocs/lib/databases/mysql.lib.php
+++ b/htdocs/lib/databases/mysql.lib.php
@@ -404,7 +404,7 @@ class DoliDb
     */
     function fetch_object($resultset=0)
     {
-        // Si le resultset n'est pas fourni, on prend le dernier utilis� sur cette connexion
+    	// Si le resultset n'est pas fourni, on prend le dernier utilis� sur cette connexion
         if (! is_resource($resultset)) { $resultset=$this->results; }
         return mysql_fetch_object($resultset);
     }