diff --git a/ChangeLog b/ChangeLog
index 6c7dc510b48c53b09dcde998d5fe5b2776628c60..89b4eb2c689bfbbfb63443731f70e19b7310136a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -26,6 +26,7 @@ English Dolibarr ChangeLog
 - Fix: [ bug #855 ] Holiday approval email in French
 - Fix: [ bug #856 ] (Holidays module) Mail error if destination user doesn't have an email
 - Fix: [ bug #857 ] Invoice created from shipment does not have the order discount
+- Fix: [ bug #866 ] Standing order from an invoice suggests invoice total amount instead of remaining to pay
 
 
 ***** ChangeLog for 3.3.1 compared to 3.3 *****
diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php
index 578049503c4c3c3860873fc765263cf0444545be..079e2ca6fa139d77d7637de5e41054d35253b434 100644
--- a/htdocs/compta/facture/class/facture.class.php
+++ b/htdocs/compta/facture/class/facture.class.php
@@ -2701,10 +2701,21 @@ class Facture extends CommonInvoice
                 {
                 	$now=dol_now();
 
+                    $totalpaye  = $this->getSommePaiement();
+                    $totalcreditnotes = $this->getSumCreditNotesUsed();
+                    $totaldeposits = $this->getSumDepositsUsed();
+                    //print "totalpaye=".$totalpaye." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits;
+
+                    // We can also use bcadd to avoid pb with floating points
+                    // For example print 239.2 - 229.3 - 9.9; does not return 0.
+                    //$resteapayer=bcadd($this->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT);
+                    //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
+                    $resteapayer = price2num($this->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits,'MT');
+
                     $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'prelevement_facture_demande';
                     $sql .= ' (fk_facture, amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib)';
                     $sql .= ' VALUES ('.$this->id;
-                    $sql .= ",'".price2num($this->total_ttc)."'";
+                    $sql .= ",'".price2num($resteapayer)."'";
                     $sql .= ",".$this->db->idate($now).",".$user->id;
                     $sql .= ",'".$soc->bank_account->code_banque."'";
                     $sql .= ",'".$soc->bank_account->code_guichet."'";
diff --git a/htdocs/compta/facture/prelevement.php b/htdocs/compta/facture/prelevement.php
index 83b99e4b8febdd65ca5c314ae1360ad456b31536..29c67ba9a2ec4057c3b14eb697fe917375ebc041 100644
--- a/htdocs/compta/facture/prelevement.php
+++ b/htdocs/compta/facture/prelevement.php
@@ -404,6 +404,15 @@ if ($object->id > 0)
 	print '<tr><td>'.$langs->trans('AmountTTC').'</td><td align="right" colspan="2" nowrap>'.price($object->total_ttc).'</td>';
 	print '<td>'.$langs->trans('Currency'.$conf->currency).'</td></tr>';
 
+	// We can also use bcadd to avoid pb with floating points
+    // For example print 239.2 - 229.3 - 9.9; does not return 0.
+    //$resteapayer=bcadd($object->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT);
+    //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
+    $resteapayer = price2num($object->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits,'MT');
+
+    print '<tr><td>'.$langs->trans('RemainderToPay').'</td><td align="right" colspan="2" nowrap>'.price($resteapayer).'</td>';
+    print '<td>'.$langs->trans('Currency'.$conf->currency).'</td></tr>';
+
 	// Statut
 	print '<tr><td>'.$langs->trans('Status').'</td>';
 	print '<td align="left" colspan="3">'.($object->getLibStatut(4,$totalpaye)).'</td></tr>';
diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php
index 4182e66c919271d71ceb0eda303797fec34dec5d..41198fd90c8170e6f7009fa3a6cd5d6f0b375b47 100644
--- a/htdocs/compta/prelevement/class/bonprelevement.class.php
+++ b/htdocs/compta/prelevement/class/bonprelevement.class.php
@@ -426,14 +426,14 @@ class BonPrelevement extends CommonObject
                         $facs = array();
                         $amounts = array();
 
-                        $facs = $this->getListInvoices();
+                        $facs = $this->getListInvoices(1);
 
                         $num=count($facs);
                         for ($i = 0; $i < $num; $i++)
                         {
                             $fac = new Facture($this->db);
-                            $fac->fetch($facs[$i]);
-                            $amounts[$fac->id] = $fac->total_ttc;
+                            $fac->fetch($facs[$i][0]);
+                            $amounts[$fac->id] = $facs[$i][1];
                             $result = $fac->set_paid($user);
                         }
                         $paiement = new Paiement($this->db);
@@ -576,9 +576,10 @@ class BonPrelevement extends CommonObject
     /**
      *	Get invoice list
      *
+     *  @param $amounts If you want to get the amount of the order for each invoice
      *	@return	array id of invoices
      */
-    private function getListInvoices()
+    private function getListInvoices($amounts=0)
     {
         global $conf;
 
@@ -588,7 +589,7 @@ class BonPrelevement extends CommonObject
          * Renvoie toutes les factures presente
          * dans un bon de prelevement
          */
-        $sql = "SELECT fk_facture";
+        $sql = "SELECT fk_facture, SUM(pl.amount)";
         $sql.= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p";
         $sql.= " , ".MAIN_DB_PREFIX."prelevement_lignes as pl";
         $sql.= " , ".MAIN_DB_PREFIX."prelevement_facture as pf";
@@ -596,6 +597,7 @@ class BonPrelevement extends CommonObject
         $sql.= " AND pl.fk_prelevement_bons = p.rowid";
         $sql.= " AND p.rowid = ".$this->id;
         $sql.= " AND p.entity = ".$conf->entity;
+        $sql.= " GROUP BY fk_facture";
 
         $resql=$this->db->query($sql);
         if ($resql)
@@ -608,7 +610,14 @@ class BonPrelevement extends CommonObject
                 while ($i < $num)
                 {
                     $row = $this->db->fetch_row($resql);
-                    $arr[$i] = $row[0];
+                    if (!$amounts) $arr[$i] = $row[0];
+                    else
+                    {
+                        $arr[$i] = array(
+                            $row[0],
+                            $row[1]
+                        );
+                    }
                     $i++;
                 }
             }