diff --git a/ChangeLog b/ChangeLog index 6711c0ce1ff..05db29b146b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -114,6 +114,7 @@ WARNING: If you used external modules, some of them may need to be upgraded due - 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 bd1c6d29d28..e7276a46845 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -2867,46 +2867,57 @@ class Facture extends CommonInvoice { $now=dol_now(); - $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 .= ",".$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."')"; - if ( $this->db->query($sql)) - { - return 1; - } - else - { - $this->error=$this->db->error(); - dol_syslog(get_class($this).'::demandeprelevement Erreur'); - return -1; - } - } - else - { - $this->error="A request already exists"; - dol_syslog(get_class($this).'::demandeprelevement Impossible de creer une demande, demande deja en cours'); - } - } - else - { - $this->error=$this->db->error(); - dol_syslog(get_class($this).'::demandeprelevement Erreur -2'); - return -2; - } - } - else - { - $this->error="Status of invoice does not allow this"; - dol_syslog(get_class($this)."::demandeprelevement ".$this->error." $this->statut, $this->paye, $this->mode_reglement_id"); - return -3; - } - } + $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($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."')"; + if ( $this->db->query($sql)) + { + return 1; + } + else + { + $this->error=$this->db->error(); + dol_syslog(get_class($this).'::demandeprelevement Erreur'); + return -1; + } + } + else + { + $this->error="A request already exists"; + dol_syslog(get_class($this).'::demandeprelevement Impossible de creer une demande, demande deja en cours'); + } + } + else + { + $this->error=$this->db->error(); + dol_syslog(get_class($this).'::demandeprelevement Erreur -2'); + return -2; + } + } + else + { + $this->error="Status of invoice does not allow this"; + dol_syslog(get_class($this)."::demandeprelevement ".$this->error." $this->statut, $this->paye, $this->mode_reglement_id"); + return -3; + } + } /** * Supprime une demande de prelevement diff --git a/htdocs/compta/facture/prelevement.php b/htdocs/compta/facture/prelevement.php index e4f5ed4cc25..03af99cc6fd 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 4c044314c63..7b2db3003dd 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++; } } @@ -1650,4 +1660,4 @@ class BonPrelevement extends CommonObject } -?> \ No newline at end of file +?>