diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php
index b64940bd68a..7c5b99e2945 100644
--- a/htdocs/compta/paiement/class/paiement.class.php
+++ b/htdocs/compta/paiement/class/paiement.class.php
@@ -488,7 +488,7 @@ class Paiement extends CommonObject
/**
- * Add a record into bank for payment with links between this bank record and invoices of payment.
+ * Add a record into bank for payment + links between this bank record and sources of payment.
* All payment properties (this->amount, this->amounts, ...) must have been set first like after a call to create().
*
* @param User $user Object of user making payment
@@ -577,10 +577,11 @@ class Paiement extends CommonObject
}
// Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
- if (! $error && $label != '(WithdrawalPayment)')
+ //if (! $error && $label != '(WithdrawalPayment)')
+ if (! $error)
{
$linkaddedforthirdparty=array();
- foreach ($this->amounts as $key => $value) // We should have always same third party but we loop in case of.
+ foreach ($this->amounts as $key => $value) // We should have invoices always for same third party but we loop in case of.
{
if ($mode == 'payment')
{
diff --git a/htdocs/compta/prelevement/card.php b/htdocs/compta/prelevement/card.php
index bdd0b47b0ac..97f5d96903f 100644
--- a/htdocs/compta/prelevement/card.php
+++ b/htdocs/compta/prelevement/card.php
@@ -45,6 +45,7 @@ if ($user->societe_id > 0) accessforbidden();
// Get supervariables
$action = GETPOST('action','alpha');
$id = GETPOST('id','int');
+$ref = GETPOST('ref', 'alpha');
$socid = GETPOST('socid','int');
// Load variable for pagination
@@ -396,7 +397,10 @@ if ($id > 0 || $ref)
print '
';
print '| '.$langs->trans("Total").' | ';
print ' | ';
- print ''.price($total)." | \n";
+ print '';
+ if ($total != $object->amount) print img_warning("AmountOfFileDiffersFromSumOfInvoices");
+ print price($total);
+ print " | \n";
print ' | ';
print "
\n";
}
diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php
index a35e4e50af8..0278d97d724 100644
--- a/htdocs/compta/prelevement/class/bonprelevement.class.php
+++ b/htdocs/compta/prelevement/class/bonprelevement.class.php
@@ -327,7 +327,7 @@ class BonPrelevement extends CommonObject
}
/**
- * Set credite and set status of linked invoices
+ * Set credite and set status of linked invoices. Still used ??
*
* @return int <0 if KO, >=0 if OK
*/
@@ -441,49 +441,60 @@ class BonPrelevement extends CommonObject
$bankaccount = $conf->global->PRELEVEMENT_ID_BANKACCOUNT;
$facs = array();
$amounts = array();
+ $amountsperthirdparty = array();
$facs = $this->getListInvoices(1);
+ // Loop on each invoice. $facs=array(0=>id, 1=>amount requested)
$num=count($facs);
for ($i = 0; $i < $num; $i++)
{
$fac = new Facture($this->db);
$fac->fetch($facs[$i][0]);
$amounts[$fac->id] = $facs[$i][1];
+ $amountsperthirdparty[$fac->socid][$fac->id] = $facs[$i][1];
$totalpaye = $fac->getSommePaiement();
$totalcreditnotes = $fac->getSumCreditNotesUsed();
$totaldeposits = $fac->getSumDepositsUsed();
$alreadypayed = $totalpaye + $totalcreditnotes + $totaldeposits;
- if ($alreadypayed + $facs[$i][1] >= $fac->total_ttc) {
+ if (price2num($alreadypayed + $facs[$i][1], 'MT') == $fac->total_ttc) {
$result = $fac->set_paid($user);
}
}
- $paiement = new Paiement($this->db);
- $paiement->datepaye = $date ;
- $paiement->amounts = $amounts;
- $paiement->paiementid = 3; //
- $paiement->num_paiement = $this->ref ;
- $paiement->id_prelevement = $this->id ;
+ // Make one payment per customer
+ foreach ($amountsperthirdparty as $thirdpartyid => $cursoramounts)
+ {
+ $paiement = new Paiement($this->db);
+ $paiement->datepaye = $date;
+ $paiement->amounts = $cursoramounts; // Array with detail of dispatching of payments for each invoice
+ $paiement->paiementid = 3; //
+ $paiement->num_paiement = $this->ref; // Set ref of direct debit note
+ $paiement->id_prelevement = $this->id;
- $paiement_id = $paiement->create($user);
- if ($paiement_id < 0)
- {
- dol_syslog(get_class($this)."::set_infocredit AddPayment Error");
- $error++;
+ $paiement_id = $paiement->create($user);
+ if ($paiement_id < 0)
+ {
+ dol_syslog(get_class($this)."::set_infocredit AddPayment Error");
+ $error++;
+ }
+ else
+ {
+ $result=$paiement->addPaymentToBank($user,'payment','(WithdrawalPayment)',$bankaccount,'','');
+ if ($result < 0)
+ {
+ dol_syslog(get_class($this)."::set_infocredit AddPaymentToBank Error");
+ $error++;
+ }
+ }
+ //var_dump($paiement->amounts);
+ //var_dump($thirdpartyid);
+ //var_dump($cursoramounts);
}
- else
- {
- $result=$paiement->addPaymentToBank($user,'payment','(WithdrawalPayment)',$bankaccount,'','');
- if ($result < 0)
- {
- dol_syslog(get_class($this)."::set_infocredit AddPaymentToBank Error");
- $error++;
- }
- }
- // Update withdrawal line
+
+ // Update withdrawal line
// TODO: Translate to ligneprelevement.class.php
$sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes";
$sql.= " SET statut = 2";
@@ -956,14 +967,14 @@ class BonPrelevement extends CommonObject
}
}
- /*
- * Create withdrawal receipt
- */
if (!$error)
{
- if (count($factures_prev) > 0)
+ /*
+ * Create withdrawal receipt in database
+ */
+ if (count($factures_prev) > 0)
{
- foreach ($factures_prev as $fac)
+ foreach ($factures_prev as $fac) // Add a link in database for each invoice
{
// Fetch invoice
$fact = new Facture($this->db);
@@ -1003,7 +1014,6 @@ class BonPrelevement extends CommonObject
}
}
-
}
}
@@ -1011,11 +1021,12 @@ class BonPrelevement extends CommonObject
if (!$error)
{
/*
- * Withdraw receipt
+ * Create direct debit order in a XML file
*/
dol_syslog(__METHOD__."::Init withdraw receipt for ".count($factures_prev)." invoices", LOG_DEBUG);
+
if (count($factures_prev) > 0)
{
$this->date_echeance = $datetimeprev;
@@ -1039,11 +1050,12 @@ class BonPrelevement extends CommonObject
$this->factures = $factures_prev_id;
- // Generation of SEPA file
+ // Generation of SEPA file $this->filename
$this->generate();
}
dol_syslog(__METHOD__."::End withdraw receipt, file ".$this->filename, LOG_DEBUG);
}
+ //var_dump($factures_prev);exit;
/*
* Update total
@@ -1294,8 +1306,9 @@ class BonPrelevement extends CommonObject
$sql.= " AND soc.rowid = f.fk_soc";
$sql.= " AND rib.fk_soc = f.fk_soc";
$sql.= " AND rib.default_rib = 1";
+ //print $sql;
- //echo $sql;
+ // Define $fileDebiteurSection. One section DrctDbtTxInf per invoice.
$resql=$this->db->query($sql);
if ($resql)
{
@@ -1307,27 +1320,26 @@ class BonPrelevement extends CommonObject
$this->total = $this->total + $obj->somme;
$i++;
}
+ $nbtotalDrctDbtTxInf = $i;
}
else
{
- fputs($this->file, 'ERREUR DEBITEUR '.$sql.$CrLf);
+ fputs($this->file, 'ERROR DEBITOR '.$sql.$CrLf); // DEBITOR = Customers
$result = -2;
}
- /*
- * section Emetteur(sepa Emetteur bloc lines)
- */
+ // Define $fileEmetteurSection. Start of bloc PmtInf. Will contains all DrctDbtTxInf
if ($result != -2)
{
- $fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $i, $this->total, $CrLf);
+ $fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $nbtotalDrctDbtTxInf, $this->total, $CrLf);
}
else
{
- fputs($this->file, 'ERREUR EMETTEUR'.$CrLf);
+ fputs($this->file, 'ERROR CREDITOR'.$CrLf); // CREDITOR = My company
}
/**
- * SECTION CREATION FICHIER SEPA
+ * SECTION CREATION SEPA FILE
*/
// SEPA File Header
fputs($this->file, '<'.'?xml version="1.0" encoding="UTF-8" standalone="yes"?'.'>'.$CrLf);
diff --git a/htdocs/compta/prelevement/factures.php b/htdocs/compta/prelevement/factures.php
index 27c70f993b5..f7e41bdc997 100644
--- a/htdocs/compta/prelevement/factures.php
+++ b/htdocs/compta/prelevement/factures.php
@@ -43,6 +43,7 @@ if ($user->societe_id > 0) accessforbidden();
// Get supervariables
$prev_id = GETPOST('id','int');
$socid = GETPOST('socid','int');
+$ref = GETPOST('ref', 'alpha');
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
$sortfield = GETPOST("sortfield",'alpha');
@@ -265,7 +266,10 @@ if ($result)
print '';
print '| '.$langs->trans("Total").' | ';
print ' | ';
- print ''.price($total)." | \n";
+ print '';
+ if ($total != $object->amount) print img_warning("AmountOfFileDiffersFromSumOfInvoices");
+ print price($total);
+ print " | \n";
print ' | ';
print ' | ';
print "
\n";
diff --git a/htdocs/compta/prelevement/fiche-rejet.php b/htdocs/compta/prelevement/fiche-rejet.php
index 25fe9255858..b3b9898659d 100644
--- a/htdocs/compta/prelevement/fiche-rejet.php
+++ b/htdocs/compta/prelevement/fiche-rejet.php
@@ -41,6 +41,7 @@ if ($user->societe_id > 0) accessforbidden();
// Get supervariables
$prev_id = GETPOST('id','int');
+$ref = GETPOST('ref', 'alpha');
// Load variable for pagination
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
diff --git a/htdocs/compta/prelevement/fiche-stat.php b/htdocs/compta/prelevement/fiche-stat.php
index 99965672c2e..ac2c1e4da9b 100644
--- a/htdocs/compta/prelevement/fiche-stat.php
+++ b/htdocs/compta/prelevement/fiche-stat.php
@@ -38,6 +38,7 @@ if ($user->societe_id > 0) accessforbidden();
// Get supervariables
$prev_id = GETPOST('id','int');
+$ref = GETPOST('ref', 'alpha');
// Load variable for pagination
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;