diff --git a/ChangeLog b/ChangeLog
index 7ee5a3e341c..8a6f62aacbf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -29,6 +29,7 @@ English Dolibarr ChangeLog
- Fix: [ bug #861 ] Impossible to create a new event in agenda
- Fix: [ bug #827 ] AJAX search does not respect multiprice level
- Fix: [ bug #865 ] Dolibarr navigation array in project/task do not work
+- Fix: [ bug #866 ] Standing order from an invoice suggests invoice total amount instead of remaining to pay
- Fix: [ bug #788 ] Date of linked interventions are not shown
diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php
index 578049503c4..079e2ca6fa1 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 83b99e4b8fe..29c67ba9a2e 100644
--- a/htdocs/compta/facture/prelevement.php
+++ b/htdocs/compta/facture/prelevement.php
@@ -404,6 +404,15 @@ if ($object->id > 0)
print '
| '.$langs->trans('AmountTTC').' | '.price($object->total_ttc).' | ';
print ''.$langs->trans('Currency'.$conf->currency).' |
';
+ // 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 '| '.$langs->trans('RemainderToPay').' | '.price($resteapayer).' | ';
+ print ''.$langs->trans('Currency'.$conf->currency).' |
';
+
// Statut
print '| '.$langs->trans('Status').' | ';
print ''.($object->getLibStatut(4,$totalpaye)).' |
';
diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php
index 4182e66c919..144c4f5e451 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;
@@ -589,6 +590,7 @@ class BonPrelevement extends CommonObject
* dans un bon de prelevement
*/
$sql = "SELECT fk_facture";
+ if ($amounts) $sql .= ", 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 +598,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;
+ if ($amounts) $sql.= " GROUP BY fk_facture";
$resql=$this->db->query($sql);
if ($resql)
@@ -608,7 +611,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++;
}
}