From c748f3d8341da3ed974058799e8d4c62d2dd3022 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sun, 16 Jun 2013 21:31:21 +0200
Subject: [PATCH] Qual: Removed useless methods Qual: Removed deprecated vars
 Qual: Free memory

---
 dev/initdata/generate-societe.php             |  4 +-
 htdocs/adherents/class/adherent.class.php     |  7 ++-
 htdocs/comm/fiche.php                         | 10 ++--
 htdocs/comm/propal.php                        | 16 +++---
 htdocs/comm/propal/apercu.php                 |  2 +-
 htdocs/comm/propal/class/propal.class.php     | 28 +++++-----
 htdocs/comm/propal/note.php                   |  2 +-
 htdocs/comm/prospect/class/prospect.class.php |  1 +
 htdocs/comm/remise.php                        |  6 +--
 htdocs/commande/class/commande.class.php      | 22 ++++----
 htdocs/commande/fiche.php                     | 12 ++---
 htdocs/compta/facture.php                     | 12 ++---
 htdocs/compta/facture/apercu.php              |  2 +-
 htdocs/compta/facture/class/facture.class.php | 38 +++++++------
 htdocs/compta/facture/prelevement.php         |  2 +-
 .../class/bonprelevement.class.php            | 25 +++++----
 htdocs/contrat/class/contrat.class.php        |  7 +--
 htdocs/contrat/contact.php                    |  6 +--
 htdocs/contrat/fiche.php                      |  6 +--
 htdocs/contrat/note.php                       |  2 +-
 .../core/class/commondocgenerator.class.php   |  6 +--
 htdocs/core/class/commonobject.class.php      |  5 +-
 .../doc/doc_generic_invoice_odt.modules.php   |  3 +-
 .../pdf/pdf_muscadet.modules.php              |  2 +-
 .../core/tpl/freeproductline_create.tpl.php   |  6 +--
 htdocs/core/tpl/objectline_add.tpl.php        |  4 +-
 .../tpl/predefinedproductline_create.tpl.php  |  4 +-
 htdocs/expedition/shipment.php                |  2 +-
 htdocs/fichinter/class/fichinter.class.php    |  6 +--
 htdocs/fourn/class/fournisseur.class.php      |  1 +
 htdocs/fourn/commande/fiche.php               |  6 +--
 htdocs/index.php                              |  4 +-
 htdocs/product/class/product.class.php        |  1 +
 htdocs/product/class/service.class.php        |  1 +
 htdocs/societe/class/client.class.php         |  4 +-
 .../class/companybankaccount.class.php        | 19 +++++++
 htdocs/societe/class/societe.class.php        | 54 ++-----------------
 htdocs/societe/soc.php                        |  6 +--
 htdocs/user/class/user.class.php              | 10 ++--
 39 files changed, 157 insertions(+), 197 deletions(-)

diff --git a/dev/initdata/generate-societe.php b/dev/initdata/generate-societe.php
index 9072709c738..2642750eb8a 100644
--- a/dev/initdata/generate-societe.php
+++ b/dev/initdata/generate-societe.php
@@ -99,8 +99,8 @@ for ($s = 0 ; $s < GEN_NUMBER_SOCIETE ; $s++)
     $soc->country_id=1;
     $soc->country_code='FR';
 	// Un client sur 3 a une remise de 5%
-    $user_remise=rand(1,3); if ($user_remise==3) $soc->remise_client=5;
-	print "> client=".$soc->client.", fournisseur=".$soc->fournisseur.", remise=".$soc->remise_client."\n";
+    $user_remise=rand(1,3); if ($user_remise==3) $soc->remise_percent=5;
+	print "> client=".$soc->client.", fournisseur=".$soc->fournisseur.", remise=".$soc->remise_percent."\n";
 	$soc->note='Company created by the script generate-societe.php';
     $socid = $soc->create();
 
diff --git a/htdocs/adherents/class/adherent.class.php b/htdocs/adherents/class/adherent.class.php
index 733e8cfdf74..f46a0fffb9f 100644
--- a/htdocs/adherents/class/adherent.class.php
+++ b/htdocs/adherents/class/adherent.class.php
@@ -1101,12 +1101,10 @@ class Adherent extends CommonObject
 
                 // Retreive all extrafield for thirdparty
                 // fetch optionals attributes and labels
-                require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
+                require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
                 $extrafields=new ExtraFields($this->db);
                 $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
-                if (count($extralabels)>0) {
-                	$this->fetch_optionals($this->id,$extralabels);
-                }
+                $this->fetch_optionals($this->id,$extralabels);
 
                 // Load other properties
                 $result=$this->fetch_subscriptions();
@@ -1662,6 +1660,7 @@ class Adherent extends CommonObject
             {
                 $this->nb["members"]=$obj->nb;
             }
+            $this->db->free($resql);
             return 1;
         }
         else
diff --git a/htdocs/comm/fiche.php b/htdocs/comm/fiche.php
index 5a1a1e4bd66..c785704d456 100644
--- a/htdocs/comm/fiche.php
+++ b/htdocs/comm/fiche.php
@@ -309,11 +309,11 @@ if ($id > 0)
 	print '</td><td colspan="3">';
 	if ($action == 'editconditions')
 	{
-		$form->form_conditions_reglement($_SERVER['PHP_SELF'].'?socid='.$object->id,$object->cond_reglement,'cond_reglement_id',-1,1);
+		$form->form_conditions_reglement($_SERVER['PHP_SELF'].'?socid='.$object->id,$object->cond_reglement_id,'cond_reglement_id',-1,1);
 	}
 	else
 	{
-		$form->form_conditions_reglement($_SERVER['PHP_SELF'].'?socid='.$object->id,$object->cond_reglement,'none');
+		$form->form_conditions_reglement($_SERVER['PHP_SELF'].'?socid='.$object->id,$object->cond_reglement_id,'none');
 	}
 	print "</td>";
 	print '</tr>';
@@ -328,11 +328,11 @@ if ($id > 0)
 	print '</td><td colspan="3">';
 	if ($action == 'editmode')
 	{
-		$form->form_modes_reglement($_SERVER['PHP_SELF'].'?socid='.$object->id,$object->mode_reglement,'mode_reglement_id');
+		$form->form_modes_reglement($_SERVER['PHP_SELF'].'?socid='.$object->id,$object->mode_reglement_id,'mode_reglement_id');
 	}
 	else
 	{
-		$form->form_modes_reglement($_SERVER['PHP_SELF'].'?socid='.$object->id,$object->mode_reglement,'none');
+		$form->form_modes_reglement($_SERVER['PHP_SELF'].'?socid='.$object->id,$object->mode_reglement_id,'none');
 	}
 	print "</td>";
 	print '</tr>';
@@ -347,7 +347,7 @@ if ($id > 0)
 		print '<a href="'.DOL_URL_ROOT.'/comm/remise.php?id='.$object->id.'">'.img_edit($langs->trans("Modify")).'</a>';
 	}
 	print '</td></tr></table>';
-	print '</td><td colspan="3">'.($object->remise_client?'<a href="'.DOL_URL_ROOT.'/comm/remise.php?id='.$object->id.'">'.$object->remise_client.'%</a>':$langs->trans("DiscountNone")).'</td>';
+	print '</td><td colspan="3">'.($object->remise_percent?'<a href="'.DOL_URL_ROOT.'/comm/remise.php?id='.$object->id.'">'.$object->remise_percent.'%</a>':$langs->trans("DiscountNone")).'</td>';
 	print '</tr>';
 
 	// Absolute discounts (Discounts-Drawbacks-Rebates)
diff --git a/htdocs/comm/propal.php b/htdocs/comm/propal.php
index c93e7d6d966..eb81c4a46ae 100644
--- a/htdocs/comm/propal.php
+++ b/htdocs/comm/propal.php
@@ -905,7 +905,7 @@ else if ($action == 'updateligne' && $user->rights->propal->creer && GETPOST('sa
 			unset($_POST["options_".$key]);
 		}
 	}
-	
+
 	// Define special_code for special lines
 	$special_code=0;
 	if (! GETPOST('qty')) $special_code=3;
@@ -1302,7 +1302,7 @@ if ($action == 'create')
 
 		// Ligne info remises tiers
 		print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="2">';
-		if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client);
+		if ($soc->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_percent);
 		else print $langs->trans("CompanyHasNoRelativeDiscount");
 		$absolute_discount=$soc->getAvailableDiscounts();
 		print '. ';
@@ -1322,12 +1322,12 @@ if ($action == 'create')
 
 	// Terms of payment
 	print '<tr><td class="nowrap fieldrequired">'.$langs->trans('PaymentConditionsShort').'</td><td colspan="2">';
-	$form->select_conditions_paiements($soc->cond_reglement,'cond_reglement_id');
+	$form->select_conditions_paiements($soc->cond_reglement_id,'cond_reglement_id');
 	print '</td></tr>';
 
 	// Mode of payment
 	print '<tr><td>'.$langs->trans('PaymentMode').'</td><td colspan="2">';
-	$form->select_types_paiements($soc->mode_reglement,'mode_reglement_id');
+	$form->select_types_paiements($soc->mode_reglement_id,'mode_reglement_id');
 	print '</td></tr>';
 
 	// What trigger creation
@@ -1369,9 +1369,9 @@ if ($action == 'create')
 	// Project
 	if (! empty($conf->projet->enabled) && $socid>0)
 	{
-		
+
 		$formproject=new FormProjets($db);
-		
+
 		$projectid = 0;
 		if ($origin == 'project') $projectid = ($originid?$originid:0);
 
@@ -1476,7 +1476,7 @@ if ($action == 'create')
 					$form->select_produits('',"idprod".$i,'',$conf->product->limit_size);
 				print '</td>';
 				print '<td><input type="text" size="2" name="qty'.$i.'" value="1"></td>';
-				print '<td><input type="text" size="2" name="remise'.$i.'" value="'.$soc->remise_client.'">%</td>';
+				print '<td><input type="text" size="2" name="remise'.$i.'" value="'.$soc->remise_percent.'">%</td>';
 				print '</tr>';
 			}
 
@@ -1624,7 +1624,7 @@ else
 
 	// Ligne info remises tiers
 	print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="5">';
-	if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client);
+	if ($soc->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_percent);
 	else print $langs->trans("CompanyHasNoRelativeDiscount");
 	print '. ';
 	$absolute_discount=$soc->getAvailableDiscounts('','fk_facture_source IS NULL');
diff --git a/htdocs/comm/propal/apercu.php b/htdocs/comm/propal/apercu.php
index 8972baaac88..78f57e15f14 100644
--- a/htdocs/comm/propal/apercu.php
+++ b/htdocs/comm/propal/apercu.php
@@ -97,7 +97,7 @@ if ($id > 0 || ! empty($ref))
 
 		// Ligne info remises tiers
 		print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="5">';
-		if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client);
+		if ($soc->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_percent);
 		else print $langs->trans("CompanyHasNoRelativeDiscount");
 		$absolute_discount=$soc->getAvailableDiscounts();
 		print '. ';
diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php
index 3d325e4c540..a6f3647b45e 100644
--- a/htdocs/comm/propal/class/propal.class.php
+++ b/htdocs/comm/propal/class/propal.class.php
@@ -408,7 +408,7 @@ class Propal extends CommonObject
             $this->line->date_start=$date_start;
             $this->line->date_end=$date_end;
 
-            
+
 			// infos marge
 			$this->line->fk_fournprice = $fk_fournprice;
 			$this->line->pa_ht = $pa_ht;
@@ -1109,13 +1109,10 @@ class Propal extends CommonObject
 
                 // Retreive all extrafield for invoice
                 // fetch optionals attributes and labels
-                if(!class_exists('Extrafields'))
-                	require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
+                require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
                 $extrafields=new ExtraFields($this->db);
                 $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
-                if (count($extralabels)>0) {
-                	$this->fetch_optionals($this->id,$extralabels);
-                }
+                $this->fetch_optionals($this->id,$extralabels);
 
                 $this->db->free($resql);
 
@@ -1202,13 +1199,11 @@ class Propal extends CommonObject
 
                 // Retreive all extrafield for propal
                 // fetch optionals attributes and labels
-                if(!class_exists('Extrafields'))
-                	require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
+                require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
                 $extrafields=new ExtraFields($this->db);
                 $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
-                if (count($extralabels)>0) {
-                	$this->fetch_optionals($this->id,$extralabels);
-                }
+                $this->fetch_optionals($this->id,$extralabels);
+
                 return 1;
             }
 
@@ -2449,6 +2444,7 @@ class Propal extends CommonObject
             {
                 $this->nb["proposals"]=$obj->nb;
             }
+            $this->db->free($resql);
             return 1;
         }
         else
@@ -2850,7 +2846,7 @@ class PropaleLigne  extends CommonObject
         if ($resql)
         {
             $this->rowid=$this->db->last_insert_id(MAIN_DB_PREFIX.'propaldet');
-            
+
             if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
             {
             	$this->id=$this->rowid;
@@ -2860,7 +2856,7 @@ class PropaleLigne  extends CommonObject
             		$error++;
             	}
             }
-            
+
             if (! $notrigger)
             {
                 // Appel des triggers
@@ -2901,7 +2897,7 @@ class PropaleLigne  extends CommonObject
         dol_syslog("PropaleLigne::delete sql=".$sql, LOG_DEBUG);
         if ($this->db->query($sql) )
         {
-        	
+
         	// Remove extrafields
         	if ((! $error) && (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used
         	{
@@ -2913,7 +2909,7 @@ class PropaleLigne  extends CommonObject
         			dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
         		}
         	}
-        	
+
             // Appel des triggers
             include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
             $interface=new Interfaces($this->db);
@@ -3016,7 +3012,7 @@ class PropaleLigne  extends CommonObject
         			$error++;
         		}
         	}
-        	
+
             if (! $notrigger)
             {
                 // Appel des triggers
diff --git a/htdocs/comm/propal/note.php b/htdocs/comm/propal/note.php
index 09670340b89..633c0dd4cf4 100644
--- a/htdocs/comm/propal/note.php
+++ b/htdocs/comm/propal/note.php
@@ -113,7 +113,7 @@ if ($id > 0 || ! empty($ref))
 
 			// Ligne info remises tiers
 			print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="3">';
-			if ($societe->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$societe->remise_client);
+			if ($societe->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$societe->remise_percent);
 			else print $langs->trans("CompanyHasNoRelativeDiscount");
 			$absolute_discount=$societe->getAvailableDiscounts();
 			print '. ';
diff --git a/htdocs/comm/prospect/class/prospect.class.php b/htdocs/comm/prospect/class/prospect.class.php
index ec0454c4847..c322d6af6d6 100644
--- a/htdocs/comm/prospect/class/prospect.class.php
+++ b/htdocs/comm/prospect/class/prospect.class.php
@@ -81,6 +81,7 @@ class Prospect extends Societe
                 if ($obj->client == 1 || $obj->client == 3) $this->nb["customers"]+=$obj->nb;
                 if ($obj->client == 2 || $obj->client == 3) $this->nb["prospects"]+=$obj->nb;
             }
+            $this->db->free($resql);
             return 1;
         }
         else
diff --git a/htdocs/comm/remise.php b/htdocs/comm/remise.php
index 306be40a5db..81bc362695e 100644
--- a/htdocs/comm/remise.php
+++ b/htdocs/comm/remise.php
@@ -114,7 +114,7 @@ if ($socid > 0)
 
 	// Remise
 	print '<tr><td colspan="2" width="25%">';
-	print $langs->trans("CustomerRelativeDiscount").'</td><td colspan="2">'.price2num($objsoc->remise_client)."%</td></tr>";
+	print $langs->trans("CustomerRelativeDiscount").'</td><td colspan="2">'.price2num($objsoc->remise_percent)."%</td></tr>";
 
 	print '</table>';
 	print '<br>';
@@ -130,7 +130,7 @@ if ($socid > 0)
 
 	// Nouvelle valeur
 	print '<tr><td colspan="2">';
-	print $langs->trans("NewValue").'</td><td colspan="2"><input type="text" size="5" name="remise" value="'.($_POST["remise"]?$_POST["remise"]:$objsoc->remise_client).'">%</td></tr>';
+	print $langs->trans("NewValue").'</td><td colspan="2"><input type="text" size="5" name="remise" value="'.($_POST["remise"]?$_POST["remise"]:$objsoc->remise_percent).'">%</td></tr>';
 
 	// Motif/Note
 	print '<tr><td colspan="2" width="25%">';
@@ -184,7 +184,7 @@ if ($socid > 0)
 			$tag = !$tag;
 			print '<tr '.$bc[$tag].'>';
 			print '<td>'.dol_print_date($db->jdate($obj->dc),"dayhour").'</td>';
-			print '<td align="center">'.price2num($obj->remise_client).'%</td>';
+			print '<td align="center">'.price2num($obj->remise_percent).'%</td>';
 			print '<td align="left">'.$obj->note.'</td>';
 			print '<td align="center"><a href="'.DOL_URL_ROOT.'/user/fiche.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"),'user').' '.$obj->login.'</a></td>';
 			print '</tr>';
diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php
index d630a993331..8a2307aca81 100644
--- a/htdocs/commande/class/commande.class.php
+++ b/htdocs/commande/class/commande.class.php
@@ -1344,16 +1344,13 @@ class Commande extends CommonOrder
                 $this->lines				= array();
 
                 if ($this->statut == 0) $this->brouillon = 1;
-                
+
                 // Retreive all extrafield for invoice
                 // fetch optionals attributes and labels
-                if(!class_exists('Extrafields'))
-                	require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
+                require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
                 $extrafields=new ExtraFields($this->db);
                 $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
-                if (count($extralabels)>0) {
-                	$this->fetch_optionals($this->id,$extralabels);
-                }
+               	$this->fetch_optionals($this->id,$extralabels);
 
                 $this->db->free();
 
@@ -2430,7 +2427,7 @@ class Commande extends CommonOrder
         	// Delete linked contacts
         	$res = $this->delete_linked_contact();
         	if ($res < 0) $error++;
-        	
+
         	// Remove extrafields
         	if ((! $error) && (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used
         	{
@@ -2468,8 +2465,8 @@ class Commande extends CommonOrder
         			}
         		}
         	}
-        	
-        	
+
+
         }
 
         if (! $error)
@@ -2833,6 +2830,7 @@ class Commande extends CommonOrder
             {
                 $this->nb["orders"]=$obj->nb;
             }
+            $this->db->free($resql);
             return 1;
         }
         else
@@ -3127,7 +3125,7 @@ class OrderLine extends CommonOrderLine
 					dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
 				}
 			}
-			
+
             // Appel des triggers
             include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
             $interface=new Interfaces($this->db);
@@ -3235,7 +3233,7 @@ class OrderLine extends CommonOrderLine
 					$error++;
 				}
 			}
-			
+
             if (! $notrigger)
             {
                 // Appel des triggers
@@ -3339,7 +3337,7 @@ class OrderLine extends CommonOrderLine
 					$error++;
 				}
 			}
-			
+
 			if (! $notrigger)
 			{
 				// Appel des triggers
diff --git a/htdocs/commande/fiche.php b/htdocs/commande/fiche.php
index 686d148d953..2c60093ab06 100644
--- a/htdocs/commande/fiche.php
+++ b/htdocs/commande/fiche.php
@@ -320,7 +320,7 @@ else if ($action == 'add' && $user->rights->commande->creer)
 							$lines[$i]->fetch_optionals($lines[$i]->rowid);
 							$array_option=$lines[$i]->array_options;
 						}
-						
+
 						$result = $object->addline(
 							$object_id,
 							$desc,
@@ -1535,7 +1535,7 @@ if ($action == 'create' && $user->rights->commande->creer)
 	print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
 	print '<input type="hidden" name="action" value="add">';
 	print '<input type="hidden" name="socid" value="'.$soc->id.'">' ."\n";
-	print '<input type="hidden" name="remise_percent" value="'.$soc->remise_client.'">';
+	print '<input type="hidden" name="remise_percent" value="'.$soc->remise_percent.'">';
 	print '<input type="hidden" name="origin" value="'.$origin.'">';
 	print '<input type="hidden" name="originid" value="'.$originid.'">';
 
@@ -1579,7 +1579,7 @@ if ($action == 'create' && $user->rights->commande->creer)
 
 		// Ligne info remises tiers
 		print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="2">';
-		if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client);
+		if ($soc->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_percent);
 		else print $langs->trans("CompanyHasNoRelativeDiscount");
 		print '. ';
 		$absolute_discount=$soc->getAvailableDiscounts();
@@ -1627,7 +1627,7 @@ if ($action == 'create' && $user->rights->commande->creer)
 	if (! empty($conf->projet->enabled) && $socid>0)
 	{
 		$formproject=new FormProjets($db);
-		
+
 		print '<tr><td>'.$langs->trans('Project').'</td><td colspan="2">';
 		$numprojet=$formproject->select_projects($soc->id,$projectid);
 		if ($numprojet==0)
@@ -1739,7 +1739,7 @@ if ($action == 'create' && $user->rights->commande->creer)
 					print $form->select_produits('','idprod'.$i,'',$conf->product->limit_size);
 				print '</td>';
 				print '<td><input type="text" size="3" name="qty'.$i.'" value="1"></td>';
-				print '<td><input type="text" size="3" name="remise_percent'.$i.'" value="'.$soc->remise_client.'">%</td></tr>';
+				print '<td><input type="text" size="3" name="remise_percent'.$i.'" value="'.$soc->remise_percent.'">%</td></tr>';
 			}
 
 			print '</table>';
@@ -1993,7 +1993,7 @@ else
 		$addcreditnote='<a href="'.DOL_URL_ROOT.'/compta/facture.php?action=create&socid='.$soc->id.'&type=2&backtopage='.urlencode($_SERVER["PHP_SELF"]).'?facid='.$object->id.'">'.$langs->trans("AddCreditNote").'</a>';
 
 		print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="3">';
-		if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client);
+		if ($soc->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_percent);
 		else print $langs->trans("CompanyHasNoRelativeDiscount");
 		print '. ';
 		$absolute_discount=$soc->getAvailableDiscounts('','fk_facture_source IS NULL');
diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php
index 4e7ede6b50c..c22a0becde1 100644
--- a/htdocs/compta/facture.php
+++ b/htdocs/compta/facture.php
@@ -1127,7 +1127,7 @@ else if (($action == 'addline' || $action == 'addline_predef') && $user->rights-
 	$product_desc = (GETPOST('product_desc')?GETPOST('product_desc'):(GETPOST('np_desc')?GETPOST('np_desc'):(GETPOST('dp_desc')?GETPOST('dp_desc'):'')));
 	$price_ht = GETPOST('price_ht');
 	$tva_tx=(GETPOST('tva_tx')?GETPOST('tva_tx'):0);
-	
+
 	//Extrafields
 	$extrafieldsline = new ExtraFields($db);
 	$extralabelsline =$extrafieldsline->fetch_name_optionals_label($object->table_element_line);
@@ -1424,7 +1424,7 @@ else if ($action == 'updateligne' && $user->rights->facture->creer && $_POST['sa
 			unset($_POST["options_".$key]);
 		}
 	}
-	
+
 
 	// Check minimum price
 	$productid = GETPOST('productid', 'int');
@@ -2238,7 +2238,7 @@ if ($action == 'create')
 	{
 		// Discounts for third party
 		print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="2">';
-		if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",'<a href="'.DOL_URL_ROOT.'/comm/remise.php?id='.$soc->id.'&backtopage='.urlencode($_SERVER["PHP_SELF"].'?socid='.$soc->id.'&action='.$action.'&origin='.GETPOST('origin').'&originid='.GETPOST('originid')).'">'.$soc->remise_client.'</a>');
+		if ($soc->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",'<a href="'.DOL_URL_ROOT.'/comm/remise.php?id='.$soc->id.'&backtopage='.urlencode($_SERVER["PHP_SELF"].'?socid='.$soc->id.'&action='.$action.'&origin='.GETPOST('origin').'&originid='.GETPOST('originid')).'">'.$soc->remise_percent.'</a>');
 		else print $langs->trans("CompanyHasNoRelativeDiscount");
 		print ' <a href="'.DOL_URL_ROOT.'/comm/remise.php?id='.$soc->id.'&backtopage='.urlencode($_SERVER["PHP_SELF"].'?socid='.$soc->id.'&action='.$action.'&origin='.GETPOST('origin').'&originid='.GETPOST('originid')).'">('.$langs->trans("EditRelativeDiscount").')</a>';
 		print '. ';
@@ -2269,7 +2269,7 @@ if ($action == 'create')
 	if (! empty($conf->projet->enabled) && $socid>0)
 	{
 		$formproject=new FormProjets($db);
-		
+
 		$langs->load('projects');
 		print '<tr><td>'.$langs->trans('Project').'</td><td colspan="2">';
 		$formproject->select_projects($soc->id, $projectid, 'projectid');
@@ -2390,7 +2390,7 @@ if ($action == 'create')
 					$form->select_produits('','idprod'.$i,'',$conf->product->limit_size);
 				print '</td>';
 				print '<td><input type="text" size="2" name="qty'.$i.'" value="1"></td>';
-				print '<td class="nowrap"><input type="text" size="1" name="remise_percent'.$i.'" value="'.$soc->remise_client.'">%</td>';
+				print '<td class="nowrap"><input type="text" size="1" name="remise_percent'.$i.'" value="'.$soc->remise_percent.'">%</td>';
 				print '<td>&nbsp;</td>';
 				// Si le module service est actif, on propose des dates de debut et fin a la ligne
 				if (! empty($conf->service->enabled))
@@ -2855,7 +2855,7 @@ else if ($id > 0 || ! empty($ref))
 
 		print '<tr><td>'.$langs->trans('Discounts');
 		print '</td><td colspan="5">';
-		if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client);
+		if ($soc->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_percent);
 		else print $langs->trans("CompanyHasNoRelativeDiscount");
 		//print ' ('.$addrelativediscount.')';
 
diff --git a/htdocs/compta/facture/apercu.php b/htdocs/compta/facture/apercu.php
index f31f0a50f0f..2adb3941595 100644
--- a/htdocs/compta/facture/apercu.php
+++ b/htdocs/compta/facture/apercu.php
@@ -149,7 +149,7 @@ if ($id > 0 || ! empty($ref))
 
         print '<tr><td>'.$langs->trans('Discounts');
         print '</td><td colspan="5">';
-        if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client);
+        if ($soc->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_percent);
         else print $langs->trans("CompanyHasNoRelativeDiscount");
 
         if ($absolute_discount > 0)
diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php
index ddb1f85b9fd..1e71d781e7f 100644
--- a/htdocs/compta/facture/class/facture.class.php
+++ b/htdocs/compta/facture/class/facture.class.php
@@ -181,9 +181,7 @@ class Facture extends CommonInvoice
 			$result=$_facrec->fetch($this->fac_rec);
 
 			$this->fk_project        = $_facrec->fk_project;
-			$this->cond_reglement    = $_facrec->cond_reglement_id;
 			$this->cond_reglement_id = $_facrec->cond_reglement_id;
-			$this->mode_reglement    = $_facrec->mode_reglement_id;
 			$this->mode_reglement_id = $_facrec->mode_reglement_id;
 			$this->remise_absolue    = $_facrec->remise_absolue;
 			$this->remise_percent    = $_facrec->remise_percent;
@@ -437,7 +435,7 @@ class Facture extends CommonInvoice
 
 			if (! $error)
 			{
-            	           	
+
 				$result=$this->update_price(1);
 				if ($result > 0)
 				{
@@ -877,13 +875,10 @@ class Facture extends CommonInvoice
 
 				// Retreive all extrafield for invoice
 				// fetch optionals attributes and labels
-				if(!class_exists('Extrafields'))
-					require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
+				require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
 				$extrafields=new ExtraFields($this->db);
 				$extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
-				if (count($extralabels)>0) {
-					$this->fetch_optionals($this->id,$extralabels);
-				}
+				$this->fetch_optionals($this->id,$extralabels);
 
 				/*
 				 * Lines
@@ -2859,17 +2854,18 @@ class Facture extends CommonInvoice
 	{
 		dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
 
-		$soc = new Societe($this->db);
-		$soc->id = $this->socid;
-		$soc->load_ban();
-
 		if ($this->statut > 0 && $this->paye == 0)
 		{
-			$sql = 'SELECT count(*)';
+	        require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
+	        $bac = new CompanyBankAccount($this->db);
+	        $bac->fetch(0,$this->socid);
+
+        	$sql = 'SELECT count(*)';
 			$sql.= ' FROM '.MAIN_DB_PREFIX.'prelevement_facture_demande';
 			$sql.= ' WHERE fk_facture = '.$this->id;
 			$sql.= ' AND traite = 0';
 
+			dol_syslot(get_clas($this)."::demande_prelevement sql=".$sql);
 			$resql=$this->db->query($sql);
 			if ($resql)
 			{
@@ -2893,11 +2889,12 @@ class Facture extends CommonInvoice
                     $sql .= ' (fk_facture, amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib)';
                     $sql .= ' VALUES ('.$this->id;
                     $sql .= ",'".price2num($resteapayer)."'";
-                    $sql .= ",".$this->db->idate($now).",".$user->id;
-                    $sql .= ",'".$soc->bank_account->code_banque."'";
-                    $sql .= ",'".$soc->bank_account->code_guichet."'";
-                    $sql .= ",'".$soc->bank_account->number."'";
-                    $sql .= ",'".$soc->bank_account->cle_rib."')";
+                    $sql .= ",'".$this->db->idate($now)."',";
+                    $sql .= ",".$user->id;
+                    $sql .= ",'".$bac->code_banque."'";
+                    $sql .= ",'".$bac->code_guichet."'";
+                    $sql .= ",'".$bac->number."'";
+                    $sql .= ",'".$bac->cle_rib."')";
                     if ( $this->db->query($sql))
                     {
                         return 1;
@@ -3185,6 +3182,7 @@ class Facture extends CommonInvoice
 			{
 				$this->nb["invoices"]=$obj->nb;
 			}
+            $this->db->free($resql);
 			return 1;
 		}
 		else
@@ -3522,7 +3520,7 @@ class FactureLigne  extends CommonInvoiceLine
             		$error++;
             	}
             }
-            
+
 			// Si fk_remise_except defini, on lie la remise a la facture
 			// ce qui la flague comme "consommee".
 			if ($this->fk_remise_except)
@@ -3679,7 +3677,7 @@ class FactureLigne  extends CommonInvoiceLine
         			$error++;
         		}
         	}
-        	
+
 			if (! $notrigger)
 			{
 				// Appel des triggers
diff --git a/htdocs/compta/facture/prelevement.php b/htdocs/compta/facture/prelevement.php
index 03af99cc6fd..51432a3e4d2 100644
--- a/htdocs/compta/facture/prelevement.php
+++ b/htdocs/compta/facture/prelevement.php
@@ -224,7 +224,7 @@ if ($object->id > 0)
 
 	// Discounts
 	print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="5">';
-	if ($object->thirdparty->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$object->thirdparty->remise_client);
+	if ($object->thirdparty->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$object->thirdparty->remise_percent);
 	else print $langs->trans("CompanyHasNoRelativeDiscount");
 	print '. ';
 	if ($absolute_discount > 0)
diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php
index 7b2db3003dd..a1ac8052222 100644
--- a/htdocs/compta/prelevement/class/bonprelevement.class.php
+++ b/htdocs/compta/prelevement/class/bonprelevement.class.php
@@ -793,7 +793,11 @@ class BonPrelevement extends CommonObject
 
         if (! $error)
         {
-            // Check RIB
+            require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
+            $soc = new Societe($this->db);
+            $bac = new CompanyBankAccount($this->db);
+
+        	// Check RIB
             $i = 0;
             dol_syslog("Start RIB check");
 
@@ -802,13 +806,12 @@ class BonPrelevement extends CommonObject
                 foreach ($factures as $fac)
                 {
                     $fact = new Facture($this->db);
-
                     if ($fact->fetch($fac[0]) >= 0)
                     {
-                        $soc = new Societe($this->db);
                         if ($soc->fetch($fact->socid) >= 0)
                         {
-                            if ($soc->verif_rib() == 1)
+                        	$bac->fetch(0,$soc->id);
+                            if ($bac->verif() >= 1)
                             {
                                 $factures_prev[$i] = $fac;
                                 /* second tableau necessaire pour BonPrelevement */
@@ -816,24 +819,24 @@ class BonPrelevement extends CommonObject
                                 $i++;
                             }
                             else
-                            {
-                                dol_syslog("Error on third party bank number RIB/IBAN $fact->socid $soc->nom", LOG_ERR);
-                                $facture_errors[$fac[0]]="Error on third party bank number RIB/IBAN $fact->socid $soc->nom";
+							{
+                                dol_syslog("Error on third party bank number RIB/IBAN ".$fact->socid." ".$soc->nom, LOG_ERR);
+                                $facture_errors[$fac[0]]="Error on third party bank number RIB/IBAN ".$fact->socid." ".$soc->nom;
                             }
                         }
                         else
-                        {
+						{
                             dol_syslog("Failed to read company", LOG_ERR);
                         }
                     }
                     else
-                    {
+					{
                         dol_syslog("Failed to read invoice", LOG_ERR);
                     }
                 }
             }
             else
-            {
+			{
                 dol_syslog("No invoice to process");
             }
         }
@@ -1252,7 +1255,7 @@ class BonPrelevement extends CommonObject
                     $num = $this->db->num_rows($resql);
 
 					$client = new Societe($this->db);
-                        
+
                     while ($i < $num)
                     {
                     	$obj = $this->db->fetch_object($resql);
diff --git a/htdocs/contrat/class/contrat.class.php b/htdocs/contrat/class/contrat.class.php
index d0599d75ef9..58670f48676 100644
--- a/htdocs/contrat/class/contrat.class.php
+++ b/htdocs/contrat/class/contrat.class.php
@@ -1531,6 +1531,7 @@ class Contrat extends CommonObject
 			{
 				$this->nb["Contracts"]=$obj->nb;
 			}
+            $this->db->free($resql);
 			return 1;
 		}
 		else
@@ -2071,7 +2072,7 @@ class ContratLigne
 			return -2;
 		}
 	}
-	
+
 	/**
 	 *      Load elements linked to contract (only intervention for the moment)
 	 *
@@ -2081,12 +2082,12 @@ class ContratLigne
 	function get_element_list($type)
 	{
 		$elements = array();
-		
+
 		$sql = '';
 		if ($type == 'intervention')
 			$sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "fichinter WHERE fk_contrat=" . $this->id;
 		if (! $sql) return -1;
-		
+
 		//print $sql;
 		dol_syslog(get_class($this)."::get_element_list sql=" . $sql);
 		$result = $this->db->query($sql);
diff --git a/htdocs/contrat/contact.php b/htdocs/contrat/contact.php
index a05ffb6f90d..d01f01e16cc 100644
--- a/htdocs/contrat/contact.php
+++ b/htdocs/contrat/contact.php
@@ -157,7 +157,7 @@ if ($id > 0 || ! empty($ref))
 
 		// Ligne info remises tiers
 	    print '<tr><td>'.$langs->trans('Discount').'</td><td>';
-		if ($object->thirdparty->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$object->thirdparty->remise_client);
+		if ($object->thirdparty->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$object->thirdparty->remise_percent);
 		else print $langs->trans("CompanyHasNoRelativeDiscount");
 		$absolute_discount=$object->thirdparty->getAvailableDiscounts();
 		print '. ';
@@ -169,9 +169,9 @@ if ($id > 0 || ! empty($ref))
 		print "</table>";
 
 		print '</div>';
-		
+
 		print '<br>';
-		
+
 		// Contacts lines
 		include DOL_DOCUMENT_ROOT.'/core/tpl/contacts.tpl.php';
 
diff --git a/htdocs/contrat/fiche.php b/htdocs/contrat/fiche.php
index 7b3432aa07e..060d9c546b0 100644
--- a/htdocs/contrat/fiche.php
+++ b/htdocs/contrat/fiche.php
@@ -838,7 +838,7 @@ if ($action == 'create')
 	{
 		// Ligne info remises tiers
 		print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="2">';
-		if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client);
+		if ($soc->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_percent);
 		else print $langs->trans("CompanyHasNoRelativeDiscount");
 		print '. ';
 		$absolute_discount=$soc->getAvailableDiscounts();
@@ -865,7 +865,7 @@ if ($action == 'create')
     if (! empty($conf->projet->enabled))
     {
     	$formproject=new FormProjets($db);
-    	
+
         print '<tr><td>'.$langs->trans("Project").'</td><td>';
         $formproject->select_projects($soc->id,$projectid,"projectid");
         print "</td></tr>";
@@ -1012,7 +1012,7 @@ else
 
         // Ligne info remises tiers
         print '<tr><td>'.$langs->trans('Discount').'</td><td colspan="3">';
-        if ($object->thirdparty->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$object->thirdparty->remise_client);
+        if ($object->thirdparty->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$object->thirdparty->remise_percent);
         else print $langs->trans("CompanyHasNoRelativeDiscount");
         $absolute_discount=$object->thirdparty->getAvailableDiscounts();
         print '. ';
diff --git a/htdocs/contrat/note.php b/htdocs/contrat/note.php
index c8edf493f63..e2bac1c6016 100644
--- a/htdocs/contrat/note.php
+++ b/htdocs/contrat/note.php
@@ -96,7 +96,7 @@ if ($id > 0 || ! empty($ref))
 
 	// Ligne info remises tiers
     print '<tr><td>'.$langs->trans('Discount').'</td><td>';
-	if ($object->thirdparty->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$object->thirdparty->remise_client);
+	if ($object->thirdparty->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$object->thirdparty->remise_percent);
 	else print $langs->trans("CompanyHasNoRelativeDiscount");
 	$absolute_discount=$object->thirdparty->getAvailableDiscounts();
 	print '. ';
diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php
index f00aa7be96f..1cf128cc150 100755
--- a/htdocs/core/class/commondocgenerator.class.php
+++ b/htdocs/core/class/commondocgenerator.class.php
@@ -176,8 +176,7 @@ abstract class CommonDocGenerator
         // Retrieve extrafields
         if(is_array($object->array_options) && count($object->array_options))
         {
-        	if(!class_exists('Extrafields'))
-        		require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
+        	require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
         	$extrafields = new ExtraFields($this->db);
         	$extralabels = $extrafields->fetch_name_optionals_label('societe',true);
         	$object->fetch_optionals($object->id,$extralabels);
@@ -267,8 +266,7 @@ abstract class CommonDocGenerator
     	// Retrieve extrafields
     	if(is_array($object->array_options) && count($object->array_options))
     	{
-    		if(!class_exists('Extrafields'))
-    			require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
+    		require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
     		$extrafields = new ExtraFields($this->db);
     		$extralabels = $extrafields->fetch_name_optionals_label('propal',true);
     		$object->fetch_optionals($object->id,$extralabels);
diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php
index 8d0e20d42d9..b8c2ab9ea35 100644
--- a/htdocs/core/class/commonobject.class.php
+++ b/htdocs/core/class/commonobject.class.php
@@ -909,7 +909,6 @@ abstract class CommonObject
     		if ($this->db->query($sql))
     		{
     			$this->mode_reglement_id = $id;
-    			$this->mode_reglement = $id;	// for compatibility
     			return 1;
     		}
     		else
@@ -2664,7 +2663,7 @@ abstract class CommonObject
     	//Line extrafield
     	require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
     	$extrafieldsline = new ExtraFields($this->db);
-    	$extralabelslines=$extrafieldsline->fetch_name_optionals_label($this->table_element_line); 	 
+    	$extralabelslines=$extrafieldsline->fetch_name_optionals_label($this->table_element_line);
 
     	// Use global variables + $dateSelector + $seller and $buyer
     	include(DOL_DOCUMENT_ROOT.'/core/tpl/freeproductline_create.tpl.php');
@@ -2781,7 +2780,7 @@ abstract class CommonObject
 		require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
 		$extrafieldsline = new ExtraFields($this->db);
 		$extralabelslines=$extrafieldsline->fetch_name_optionals_label($this->table_element_line);
-		
+
 		foreach ($this->lines as $line)
 		{
 			//Line extrafield
diff --git a/htdocs/core/modules/facture/doc/doc_generic_invoice_odt.modules.php b/htdocs/core/modules/facture/doc/doc_generic_invoice_odt.modules.php
index 4b5f472f58e..d3611f2bea8 100644
--- a/htdocs/core/modules/facture/doc/doc_generic_invoice_odt.modules.php
+++ b/htdocs/core/modules/facture/doc/doc_generic_invoice_odt.modules.php
@@ -145,8 +145,7 @@ class doc_generic_invoice_odt extends ModelePDFFactures
 		// Retrieve extrafields
 		if(is_array($object->array_options) && count($object->array_options))
 		{
-			if(!class_exists('Extrafields'))
-				require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
+			require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
 			$extrafields = new ExtraFields($this->db);
 			$extralabels = $extrafields->fetch_name_optionals_label('facture',true);
 			$object->fetch_optionals($object->id,$extralabels);
diff --git a/htdocs/core/modules/supplier_order/pdf/pdf_muscadet.modules.php b/htdocs/core/modules/supplier_order/pdf/pdf_muscadet.modules.php
index 30f34efd219..f2292249f1e 100644
--- a/htdocs/core/modules/supplier_order/pdf/pdf_muscadet.modules.php
+++ b/htdocs/core/modules/supplier_order/pdf/pdf_muscadet.modules.php
@@ -550,7 +550,7 @@ class pdf_muscadet extends ModelePDFSuppliersOrders
 
 			$pdf->SetFont('','', $default_font_size - 2);
 			$pdf->SetXY($posxval, $posy);
-			$lib_condition_paiement=$outputlangs->transnoentities("PaymentCondition".$object->cond_reglement_code)!=('PaymentCondition'.$object->cond_reglement_code)?$outputlangs->transnoentities("PaymentCondition".$object->cond_reglement_code):$outputlangs->convToOutputCharset($object->cond_reglement_doc);
+			$lib_condition_paiement=$outputlangs->transnoentities("PaymentCondition".$object->cond_reglement_code)!=('PaymentCondition'.$object->cond_reglement_code)?$outputlangs->transnoentities("PaymentCondition".$object->cond_reglement_code):$outputlangs->convToOutputCharset($object->cond_reglement);
 			$lib_condition_paiement=str_replace('\n',"\n",$lib_condition_paiement);
 			$pdf->MultiCell(80, 4, $lib_condition_paiement,0,'L');
 
diff --git a/htdocs/core/tpl/freeproductline_create.tpl.php b/htdocs/core/tpl/freeproductline_create.tpl.php
index e0dec028baa..2c5a90f6d4e 100644
--- a/htdocs/core/tpl/freeproductline_create.tpl.php
+++ b/htdocs/core/tpl/freeproductline_create.tpl.php
@@ -69,13 +69,13 @@ if (! empty($conf->margin->enabled) && ! empty($object->element) && in_array($ob
 </tr>
 
 <tr <?php echo $bcnd[$var]; ?>>
-	<?php 
+	<?php
 	if (! empty($conf->global->MAIN_VIEW_LINE_NUMBER)) {
 			$coldisplay=2; }
 	else {
 			$coldisplay=0; }
 	?>
-	
+
 	<td<?php echo (! empty($conf->global->MAIN_VIEW_LINE_NUMBER) ? ' colspan="2"' : ''); ?>>
 		<?php
 
@@ -109,7 +109,7 @@ if (! empty($conf->margin->enabled) && ! empty($object->element) && in_array($ob
 	<td align="right"><input type="text" size="5" name="price_ht" class="flat" value="<?php echo (isset($_POST["price_ht"])?$_POST["price_ht"]:''); ?>">
 	</td>
 	<td align="right"><input type="text" size="2" name="qty" class="flat" value="<?php echo (isset($_POST["qty"])?$_POST["qty"]:1); ?>"></td>
-	<td align="right" class="nowrap"><input type="text" size="1" class="flat" value="<?php echo $buyer->remise_client; ?>" name="remise_percent"><span class="hideonsmartphone">%</span></td>
+	<td align="right" class="nowrap"><input type="text" size="1" class="flat" value="<?php echo $buyer->remise_percent; ?>" name="remise_percent"><span class="hideonsmartphone">%</span></td>
 	<?php
 	$colspan = 4;
 	if (! empty($usemargins))
diff --git a/htdocs/core/tpl/objectline_add.tpl.php b/htdocs/core/tpl/objectline_add.tpl.php
index ebf63f6b08d..5f4c83a1a71 100644
--- a/htdocs/core/tpl/objectline_add.tpl.php
+++ b/htdocs/core/tpl/objectline_add.tpl.php
@@ -161,8 +161,8 @@ if (! empty($conf->margin->enabled)) {
 	</td>
 	<td align="right"><input type="text" size="3" id="qty" name="qty" value="<?php echo (GETPOST('qty')?GETPOST('qty'):1); ?>"></td>
 	<td align="right" class="nowrap">
-		<input type="text" size="1" value="<?php echo $buyer->remise_client; ?>" id="remise_percent" name="remise_percent">%
-		<input type="hidden" id="origin_remise_percent" name="origin_remise_percent" value="<?php echo $buyer->remise_client; ?>" />
+		<input type="text" size="1" value="<?php echo $buyer->remise_percent; ?>" id="remise_percent" name="remise_percent">%
+		<input type="hidden" id="origin_remise_percent" name="origin_remise_percent" value="<?php echo $buyer->remise_percent; ?>" />
 	</td>
 <?php
 $colspan = 4;
diff --git a/htdocs/core/tpl/predefinedproductline_create.tpl.php b/htdocs/core/tpl/predefinedproductline_create.tpl.php
index 1d6dac70120..214a03fd6e7 100644
--- a/htdocs/core/tpl/predefinedproductline_create.tpl.php
+++ b/htdocs/core/tpl/predefinedproductline_create.tpl.php
@@ -81,7 +81,7 @@ if (! empty($usemargins))
 </tr>
 
 <tr <?php echo $bcnd[$var]; ?>>
-<?php 
+<?php
 if (! empty($conf->global->MAIN_VIEW_LINE_NUMBER)) {
 		$coldisplay=4; }
 else {
@@ -114,7 +114,7 @@ else {
 	?>
 	</td>
 	<td align="right"><input type="text" size="2" name="qty" class="flat" value="1"></td>
-	<td align="right" nowrap><input type="text" size="1" class="flat" name="remise_percent" value="<?php echo $buyer->remise_client; ?>"><span class="hideonsmartphone">%</span></td>
+	<td align="right" nowrap><input type="text" size="1" class="flat" name="remise_percent" value="<?php echo $buyer->remise_percent; ?>"><span class="hideonsmartphone">%</span></td>
 	<?php
 	$colspan = 4;
 	if (! empty($usemargins))
diff --git a/htdocs/expedition/shipment.php b/htdocs/expedition/shipment.php
index 222eb280f26..f381b5bbd1c 100644
--- a/htdocs/expedition/shipment.php
+++ b/htdocs/expedition/shipment.php
@@ -214,7 +214,7 @@ if ($id > 0 || ! empty($ref))
 
 		// Discounts for third party
 		print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="3">';
-		if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client);
+		if ($soc->remise_percent) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_percent);
 		else print $langs->trans("CompanyHasNoRelativeDiscount");
 		print '. ';
 		$absolute_discount=$soc->getAvailableDiscounts('','fk_facture_source IS NULL');
diff --git a/htdocs/fichinter/class/fichinter.class.php b/htdocs/fichinter/class/fichinter.class.php
index 5133061a811..8da9b42b587 100644
--- a/htdocs/fichinter/class/fichinter.class.php
+++ b/htdocs/fichinter/class/fichinter.class.php
@@ -257,7 +257,7 @@ class Fichinter extends CommonObject
 			}
 			// Fin appel triggers
 			}
-			
+
 			$this->db->commit();
 			return 1;
 		}
@@ -317,9 +317,7 @@ class Fichinter extends CommonObject
 				require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
 				$extrafields=new ExtraFields($this->db);
 				$extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
-				if (count($extralabels)>0) {
-					$this->fetch_optionals($this->id,$extralabels);
-				}
+				$this->fetch_optionals($this->id,$extralabels);
 
 				/*
 				 * Lines
diff --git a/htdocs/fourn/class/fournisseur.class.php b/htdocs/fourn/class/fournisseur.class.php
index e06d1d86080..708f75a9b2a 100644
--- a/htdocs/fourn/class/fournisseur.class.php
+++ b/htdocs/fourn/class/fournisseur.class.php
@@ -133,6 +133,7 @@ class Fournisseur extends Societe
 			{
 				$this->nb["suppliers"]=$obj->nb;
 			}
+            $this->db->free($resql);
 			return 1;
 		}
 		else
diff --git a/htdocs/fourn/commande/fiche.php b/htdocs/fourn/commande/fiche.php
index 8bdd44adcf3..516a975e5ab 100644
--- a/htdocs/fourn/commande/fiche.php
+++ b/htdocs/fourn/commande/fiche.php
@@ -339,7 +339,7 @@ else if ($action == 'updateligne' && $user->rights->fournisseur->commande->creer
     if ($_POST["elrowid"])
     {
         $line = new CommandeFournisseurLigne($db);
-        $res = $line->fetch($_POST["elrowid"]); 
+        $res = $line->fetch($_POST["elrowid"]);
         if (!$res) dol_print_error($db);
     }
 
@@ -1606,7 +1606,7 @@ elseif (! empty($object->id))
 		print '</td>';
 		print '<td align="right"><input type="text" name="pu" size="5" value="'.GETPOST('pu').'"></td>';
 		print '<td align="right"><input type="text" name="qty" value="'.(GETPOST('qty')?GETPOST('qty'):'1').'" size="2"></td>';
-		print '<td align="right" class="nowrap"><input type="text" name="remise_percent" size="1" value="'.(GETPOST('remise_percent')?GETPOST('remise_percent'):$object->thirdparty->remise_client).'"><span class="hideonsmartphone">%</span></td>';
+		print '<td align="right" class="nowrap"><input type="text" name="remise_percent" size="1" value="'.(GETPOST('remise_percent')?GETPOST('remise_percent'):$object->thirdparty->remise_percent).'"><span class="hideonsmartphone">%</span></td>';
 		print '<td align="center" colspan="4"><input type="submit" class="button" value="'.$langs->trans('Add').'"></td>';
 		print '</tr>';
 
@@ -1663,7 +1663,7 @@ elseif (! empty($object->id))
 
 			print '</td>';
 			print '<td align="right"><input type="text" size="2" id="pqty" name="pqty" value="'.(GETPOST('pqty')?GETPOST('pqty'):'1').'"></td>';
-			print '<td align="right" class="nowrap"><input type="text" size="1" id="p_remise_percent" name="p_remise_percent" value="'.(GETPOST('p_remise_percent')?GETPOST('p_remise_percent'):$object->thirdparty->remise_client).'"><span class="hideonsmartphone">%</span></td>';
+			print '<td align="right" class="nowrap"><input type="text" size="1" id="p_remise_percent" name="p_remise_percent" value="'.(GETPOST('p_remise_percent')?GETPOST('p_remise_percent'):$object->thirdparty->remise_percent).'"><span class="hideonsmartphone">%</span></td>';
 			print '<td align="center" colspan="4"><input type="submit" id="addPredefinedProductButton" class="button" value="'.$langs->trans('Add').'"></td>';
 			print '</tr>';
 
diff --git a/htdocs/index.php b/htdocs/index.php
index d5d0a21d9b2..5d423b18921 100644
--- a/htdocs/index.php
+++ b/htdocs/index.php
@@ -98,6 +98,7 @@ $langs->load("commercial");
 $langs->load("bills");
 $langs->load("orders");
 
+//print memory_get_usage();
 if ($user->societe_id == 0)
 {
     print '<br>';
@@ -209,7 +210,7 @@ if ($user->societe_id == 0)
             // Search in cache if load_state_board is already realized
             if (! isset($boardloaded[$classe]) || ! is_object($boardloaded[$classe]))
             {
-                include_once $includes[$key];
+            	include_once $includes[$key];	// Loading a class cost around 1Mb
 
                 $board=new $classe($db);
                 $board->load_state_board($user);
@@ -235,7 +236,6 @@ if ($user->societe_id == 0)
     print '</table>';
 }
 
-
 print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
 
 
diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php
index 26e30b75431..0cad8a59ede 100644
--- a/htdocs/product/class/product.class.php
+++ b/htdocs/product/class/product.class.php
@@ -2983,6 +2983,7 @@ class Product extends CommonObject
 			{
 				$this->nb["products"]=$obj->nb;
 			}
+            $this->db->free($resql);
 			return 1;
 		}
 		else
diff --git a/htdocs/product/class/service.class.php b/htdocs/product/class/service.class.php
index 6ea03ad25f6..b3f96434a73 100644
--- a/htdocs/product/class/service.class.php
+++ b/htdocs/product/class/service.class.php
@@ -76,6 +76,7 @@ class Service extends CommonObject
 			{
 				$this->nb["services"]=$obj->nb;
 			}
+            $this->db->free($resql);
 			return 1;
 		}
 		else
diff --git a/htdocs/societe/class/client.class.php b/htdocs/societe/class/client.class.php
index 790584e720b..7c11b074e3b 100644
--- a/htdocs/societe/class/client.class.php
+++ b/htdocs/societe/class/client.class.php
@@ -25,8 +25,7 @@ include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
 
 
 /**
- *      \class      Client
- *		\brief      Class to manage customers
+ *	Class to manage customers
  */
 class Client extends Societe
 {
@@ -74,6 +73,7 @@ class Client extends Societe
                 if ($obj->client == 1 || $obj->client == 3) $this->nb["customers"]+=$obj->nb;
                 if ($obj->client == 2 || $obj->client == 3) $this->nb["prospects"]+=$obj->nb;
             }
+            $this->db->free($resql);
             return 1;
         }
         else
diff --git a/htdocs/societe/class/companybankaccount.class.php b/htdocs/societe/class/companybankaccount.class.php
index 29e227f1cfa..add9a5f1b5c 100644
--- a/htdocs/societe/class/companybankaccount.class.php
+++ b/htdocs/societe/class/companybankaccount.class.php
@@ -187,6 +187,25 @@ class CompanyBankAccount extends Account
         }
     }
 
+    /**
+     * Return RIB
+     *
+     * @return	string		RIB
+     */
+    function getRibLabel()
+    {
+    	if ($this->code_banque || $this->code_guichet || $this->number || $this->cle_rib)
+    	{
+    		$rib = $this->code_banque." ".$this->code_guichet." ".$this->number;
+    		$rib.=($this->cle_rib?" (".$this->cle_rib.")":"");
+    	}
+    	else
+    	{
+    		$rib=$langs->trans("NoRIB");
+    	}
+
+    	return $rib;
+    }
 }
 
 ?>
diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php
index b6bee432087..6278b7bd8ab 100644
--- a/htdocs/societe/class/societe.class.php
+++ b/htdocs/societe/class/societe.class.php
@@ -106,9 +106,6 @@ class Societe extends CommonObject
     var $remise_percent;
     var $mode_reglement_id;
     var $cond_reglement_id;
-    var $remise_client;  // TODO obsolete
-    var $mode_reglement; // TODO obsolete
-    var $cond_reglement; // TODO obsolete
 
     var $client;					// 0=no customer, 1=customer, 2=prospect, 3=customer and prospect
     var $prospect;					// 0=no prospect, 1=prospect
@@ -829,14 +826,11 @@ class Societe extends CommonObject
                 $this->remise_percent		= $obj->remise_client;
                 $this->mode_reglement_id 	= $obj->mode_reglement;
                 $this->cond_reglement_id 	= $obj->cond_reglement;
-                $this->remise_client  		= $obj->remise_client;  // TODO obsolete
-                $this->mode_reglement 		= $obj->mode_reglement; // TODO obsolete
-                $this->cond_reglement 		= $obj->cond_reglement; // TODO obsolete
 
                 $this->client      = $obj->client;
                 $this->fournisseur = $obj->fournisseur;
 
-                $this->note = $obj->note_private; //TODO Deprecatedfor backward comtability
+                $this->note = $obj->note_private; // TODO Deprecated for backward comtability
                 $this->note_private = $obj->note_private;
                 $this->note_public = $obj->note_public;
                 $this->default_lang = $obj->default_lang;
@@ -854,9 +848,7 @@ class Societe extends CommonObject
                 require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
                 $extrafields=new ExtraFields($this->db);
                 $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
-                if (count($extralabels)>0) {
-                	$this->fetch_optionals($this->id,$extralabels);
-                }
+               	$this->fetch_optionals($this->id,$extralabels);
             }
             else
             {
@@ -1688,51 +1680,11 @@ class Societe extends CommonObject
      *    @return	string		Bank number
      */
     function display_rib()
-    {
-        global $langs;
-
-        require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
-
-        $bac = new CompanyBankAccount($this->db);
-        $bac->fetch(0,$this->id);
-
-        if ($bac->code_banque || $bac->code_guichet || $bac->number || $bac->cle_rib)
-        {
-            $rib = $bac->code_banque." ".$bac->code_guichet." ".$bac->number;
-            $rib.=($bac->cle_rib?" (".$bac->cle_rib.")":"");
-        }
-        else
-        {
-            $rib=$langs->trans("NoRIB");
-        }
-        return $rib;
-    }
-
-    /**
-     * Load this->bank_account attribut
-     *
-     * @return	int		1
-     */
-    function load_ban()
     {
         require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
-
         $bac = new CompanyBankAccount($this->db);
         $bac->fetch(0,$this->id);
-
-        $this->bank_account = $bac;
-        return 1;
-    }
-
-    /**
-     * Check bank numbers
-     *
-     * @return int		<0 if KO, >0 if OK
-     */
-    function verif_rib()
-    {
-        $this->load_ban();
-        return $this->bank_account->verif();
+        return $bac->getRibLabel();
     }
 
     /**
diff --git a/htdocs/societe/soc.php b/htdocs/societe/soc.php
index 19da5101422..87c31baeecd 100644
--- a/htdocs/societe/soc.php
+++ b/htdocs/societe/soc.php
@@ -1707,10 +1707,8 @@ else
             print '<table width="100%" class="nobordernopadding"><tr><td>';
             print $langs->trans('RIB');
             print '<td><td align="right">';
-            if ($user->rights->societe->creer)
-            print '<a href="'.DOL_URL_ROOT.'/societe/rib.php?socid='.$object->id.'">'.img_edit().'</a>';
-            else
-            print '&nbsp;';
+            if ($user->rights->societe->creer) print '<a href="'.DOL_URL_ROOT.'/societe/rib.php?socid='.$object->id.'">'.img_edit().'</a>';
+            else print '&nbsp;';
             print '</td></tr></table>';
             print '</td>';
             print '<td colspan="3">';
diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php
index aa6fb883a1d..a98e5c5dbd8 100644
--- a/htdocs/user/class/user.class.php
+++ b/htdocs/user/class/user.class.php
@@ -219,15 +219,13 @@ class User extends CommonObject
 				$this->contact_id           = $obj->fk_socpeople;
 				$this->fk_member            = $obj->fk_member;
 				$this->fk_user        		= $obj->fk_user;
-				
+
 				// Retreive all extrafield for thirdparty
 				// fetch optionals attributes and labels
 				require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
 				$extrafields=new ExtraFields($this->db);
 				$extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
-				if (count($extralabels)>0) {
-					$this->fetch_optionals($this->id,$extralabels);
-				}
+				$this->fetch_optionals($this->id,$extralabels);
 
 				$this->db->free($result);
 			}
@@ -1116,7 +1114,7 @@ class User extends CommonObject
 		$this->address		= empty($this->address)?'':$this->address;
 		$this->zip			= empty($this->zip)?'':$this->zip;
 		$this->town			= empty($this->town)?'':$this->town;
-		
+
 		// Check parameters
 		if (! empty($conf->global->USER_MAIL_REQUIRED) && ! isValidEMail($this->email))
 		{
@@ -1947,7 +1945,7 @@ class User extends CommonObject
 		global $user,$langs;
 
 		$now=dol_now();
-		
+
 		// Initialise parametres
 		$this->id=0;
 		$this->ref = 'SPECIMEN';
-- 
GitLab