FIX SEPA recording payment must save one payment in bank per customer

This commit is contained in:
Laurent Destailleur 2017-09-15 18:39:10 +02:00
parent 17eec5f9e9
commit 59f644a3e2
6 changed files with 67 additions and 44 deletions

View File

@ -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(). * 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 * @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) // 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(); $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') if ($mode == 'payment')
{ {

View File

@ -45,6 +45,7 @@ if ($user->societe_id > 0) accessforbidden();
// Get supervariables // Get supervariables
$action = GETPOST('action','alpha'); $action = GETPOST('action','alpha');
$id = GETPOST('id','int'); $id = GETPOST('id','int');
$ref = GETPOST('ref', 'alpha');
$socid = GETPOST('socid','int'); $socid = GETPOST('socid','int');
// Load variable for pagination // Load variable for pagination
@ -396,7 +397,10 @@ if ($id > 0 || $ref)
print '<tr class="liste_total">'; print '<tr class="liste_total">';
print '<td>'.$langs->trans("Total").'</td>'; print '<td>'.$langs->trans("Total").'</td>';
print '<td>&nbsp;</td>'; print '<td>&nbsp;</td>';
print '<td align="right">'.price($total)."</td>\n"; print '<td align="right">';
if ($total != $object->amount) print img_warning("AmountOfFileDiffersFromSumOfInvoices");
print price($total);
print "</td>\n";
print '<td>&nbsp;</td>'; print '<td>&nbsp;</td>';
print "</tr>\n"; print "</tr>\n";
} }

View File

@ -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 * @return int <0 if KO, >=0 if OK
*/ */
@ -441,49 +441,60 @@ class BonPrelevement extends CommonObject
$bankaccount = $conf->global->PRELEVEMENT_ID_BANKACCOUNT; $bankaccount = $conf->global->PRELEVEMENT_ID_BANKACCOUNT;
$facs = array(); $facs = array();
$amounts = array(); $amounts = array();
$amountsperthirdparty = array();
$facs = $this->getListInvoices(1); $facs = $this->getListInvoices(1);
// Loop on each invoice. $facs=array(0=>id, 1=>amount requested)
$num=count($facs); $num=count($facs);
for ($i = 0; $i < $num; $i++) for ($i = 0; $i < $num; $i++)
{ {
$fac = new Facture($this->db); $fac = new Facture($this->db);
$fac->fetch($facs[$i][0]); $fac->fetch($facs[$i][0]);
$amounts[$fac->id] = $facs[$i][1]; $amounts[$fac->id] = $facs[$i][1];
$amountsperthirdparty[$fac->socid][$fac->id] = $facs[$i][1];
$totalpaye = $fac->getSommePaiement(); $totalpaye = $fac->getSommePaiement();
$totalcreditnotes = $fac->getSumCreditNotesUsed(); $totalcreditnotes = $fac->getSumCreditNotesUsed();
$totaldeposits = $fac->getSumDepositsUsed(); $totaldeposits = $fac->getSumDepositsUsed();
$alreadypayed = $totalpaye + $totalcreditnotes + $totaldeposits; $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); $result = $fac->set_paid($user);
} }
} }
$paiement = new Paiement($this->db); // Make one payment per customer
$paiement->datepaye = $date ; foreach ($amountsperthirdparty as $thirdpartyid => $cursoramounts)
$paiement->amounts = $amounts; {
$paiement->paiementid = 3; // $paiement = new Paiement($this->db);
$paiement->num_paiement = $this->ref ; $paiement->datepaye = $date;
$paiement->id_prelevement = $this->id ; $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); $paiement_id = $paiement->create($user);
if ($paiement_id < 0) if ($paiement_id < 0)
{ {
dol_syslog(get_class($this)."::set_infocredit AddPayment Error"); dol_syslog(get_class($this)."::set_infocredit AddPayment Error");
$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
{ // Update withdrawal line
$result=$paiement->addPaymentToBank($user,'payment','(WithdrawalPayment)',$bankaccount,'','');
if ($result < 0)
{
dol_syslog(get_class($this)."::set_infocredit AddPaymentToBank Error");
$error++;
}
}
// Update withdrawal line
// TODO: Translate to ligneprelevement.class.php // TODO: Translate to ligneprelevement.class.php
$sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes"; $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes";
$sql.= " SET statut = 2"; $sql.= " SET statut = 2";
@ -956,14 +967,14 @@ class BonPrelevement extends CommonObject
} }
} }
/*
* Create withdrawal receipt
*/
if (!$error) 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 // Fetch invoice
$fact = new Facture($this->db); $fact = new Facture($this->db);
@ -1003,7 +1014,6 @@ class BonPrelevement extends CommonObject
} }
} }
} }
} }
@ -1011,11 +1021,12 @@ class BonPrelevement extends CommonObject
if (!$error) 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); dol_syslog(__METHOD__."::Init withdraw receipt for ".count($factures_prev)." invoices", LOG_DEBUG);
if (count($factures_prev) > 0) if (count($factures_prev) > 0)
{ {
$this->date_echeance = $datetimeprev; $this->date_echeance = $datetimeprev;
@ -1039,11 +1050,12 @@ class BonPrelevement extends CommonObject
$this->factures = $factures_prev_id; $this->factures = $factures_prev_id;
// Generation of SEPA file // Generation of SEPA file $this->filename
$this->generate(); $this->generate();
} }
dol_syslog(__METHOD__."::End withdraw receipt, file ".$this->filename, LOG_DEBUG); dol_syslog(__METHOD__."::End withdraw receipt, file ".$this->filename, LOG_DEBUG);
} }
//var_dump($factures_prev);exit;
/* /*
* Update total * Update total
@ -1294,8 +1306,9 @@ class BonPrelevement extends CommonObject
$sql.= " AND soc.rowid = f.fk_soc"; $sql.= " AND soc.rowid = f.fk_soc";
$sql.= " AND rib.fk_soc = f.fk_soc"; $sql.= " AND rib.fk_soc = f.fk_soc";
$sql.= " AND rib.default_rib = 1"; $sql.= " AND rib.default_rib = 1";
//print $sql;
//echo $sql; // Define $fileDebiteurSection. One section DrctDbtTxInf per invoice.
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -1307,27 +1320,26 @@ class BonPrelevement extends CommonObject
$this->total = $this->total + $obj->somme; $this->total = $this->total + $obj->somme;
$i++; $i++;
} }
$nbtotalDrctDbtTxInf = $i;
} }
else else
{ {
fputs($this->file, 'ERREUR DEBITEUR '.$sql.$CrLf); fputs($this->file, 'ERROR DEBITOR '.$sql.$CrLf); // DEBITOR = Customers
$result = -2; $result = -2;
} }
/* // Define $fileEmetteurSection. Start of bloc PmtInf. Will contains all DrctDbtTxInf
* section Emetteur(sepa Emetteur bloc lines)
*/
if ($result != -2) if ($result != -2)
{ {
$fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $i, $this->total, $CrLf); $fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $nbtotalDrctDbtTxInf, $this->total, $CrLf);
} }
else 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 // SEPA File Header
fputs($this->file, '<'.'?xml version="1.0" encoding="UTF-8" standalone="yes"?'.'>'.$CrLf); fputs($this->file, '<'.'?xml version="1.0" encoding="UTF-8" standalone="yes"?'.'>'.$CrLf);

View File

@ -43,6 +43,7 @@ if ($user->societe_id > 0) accessforbidden();
// Get supervariables // Get supervariables
$prev_id = GETPOST('id','int'); $prev_id = GETPOST('id','int');
$socid = GETPOST('socid','int'); $socid = GETPOST('socid','int');
$ref = GETPOST('ref', 'alpha');
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit; $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
$sortfield = GETPOST("sortfield",'alpha'); $sortfield = GETPOST("sortfield",'alpha');
@ -265,7 +266,10 @@ if ($result)
print '<tr class="liste_total">'; print '<tr class="liste_total">';
print '<td>'.$langs->trans("Total").'</td>'; print '<td>'.$langs->trans("Total").'</td>';
print '<td>&nbsp;</td>'; print '<td>&nbsp;</td>';
print '<td align="right">'.price($total)."</td>\n"; print '<td align="right">';
if ($total != $object->amount) print img_warning("AmountOfFileDiffersFromSumOfInvoices");
print price($total);
print "</td>\n";
print '<td>&nbsp;</td>'; print '<td>&nbsp;</td>';
print '<td>&nbsp;</td>'; print '<td>&nbsp;</td>';
print "</tr>\n"; print "</tr>\n";

View File

@ -41,6 +41,7 @@ if ($user->societe_id > 0) accessforbidden();
// Get supervariables // Get supervariables
$prev_id = GETPOST('id','int'); $prev_id = GETPOST('id','int');
$ref = GETPOST('ref', 'alpha');
// Load variable for pagination // Load variable for pagination
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit; $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;

View File

@ -38,6 +38,7 @@ if ($user->societe_id > 0) accessforbidden();
// Get supervariables // Get supervariables
$prev_id = GETPOST('id','int'); $prev_id = GETPOST('id','int');
$ref = GETPOST('ref', 'alpha');
// Load variable for pagination // Load variable for pagination
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit; $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;