diff --git a/htdocs/telephonie/script/facturation-details-tableur.php b/htdocs/telephonie/script/facturation-details-tableur.php
index 3f36197100c487af7a302529f3e53112474a4994..d6495fadb1db4b01c9efd1e2c39bc0a2347f655a 100644
--- a/htdocs/telephonie/script/facturation-details-tableur.php
+++ b/htdocs/telephonie/script/facturation-details-tableur.php
@@ -66,11 +66,11 @@ dolibarr_syslog("Mois $month Ann
 if (!$error)
 {
   
-  $sql = "SELECT distinct(ligne) as client";
-  $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_communications_details";
-  $sql .= " ORDER BY ligne ASC";
+  $sql = "SELECT fk_contrat as contrat";
+  $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_contrat_service";
+  $sql .= " WHERE fk_service = 3";
   
-  $clients = array();
+  $contrats = array();
   
   if ( $db->query($sql) )
     {
@@ -82,7 +82,7 @@ if (!$error)
 	{
 	  $objp = $db->fetch_object( $i);
 	  
-	  $clients[$i] = $objp->client;
+	  $contrats[$i] = $objp->contrat;
 	  
 	  $i++;
 	}            
@@ -102,17 +102,45 @@ if (!$error)
 
 if (!$error)
 {
-   foreach ($clients as $client)
+  foreach ($contrats as $contrat)
     {
 
-      $facdet = new FactureDetailTableurOne($db);
-      $resg = $facdet->GenerateFile ($client, $year, $month);
+      $sql = "SELECT rowid as ligne";
+      $sql .= " FROM ".MAIN_DB_PREFIX."telephonie_societe_ligne";
+      $sql .= " WHERE fk_contrat = ".$contrat;
       
-      if ($resg <> 0)
+      $resql=  $db->query($sql) ;
+
+      if ($resql)
 	{
-	  dolibarr_syslog("ERREUR lors de G�n�ration du d�tail tableur");
-	  $error = 19;
+	  $num = $db->num_rows($resql);
+	  
+	  $i = 0;
+	  
+	  while ($i < $num)
+	    {
+	      $obj = $db->fetch_object($resql);
+	      
+	      $contrats[$i] = $objp->contrat;
+
+	      $facdet = new FactureDetailTableurOne($db);
+	      $resg = $facdet->GenerateFile ($obj->ligne, $year, $month);
+	      
+	      if ($resg <> 0)
+		{
+		  dolibarr_syslog("ERREUR lors de G�n�ration du d�tail tableur");
+		  $error = 19;
+		}
+	      
+	      $i++;
+	    }            
+	  $db->free();
 	}
+      else
+	{
+	  $error = 1;
+	  dolibarr_syslog($db->error());
+	}           
     }
 }