Debug credit transfer module

This commit is contained in:
Laurent Destailleur 2020-07-03 19:27:23 +02:00
parent 636f1e642d
commit e256ce5c4b
2 changed files with 181 additions and 131 deletions

View File

@ -819,7 +819,7 @@ class BonPrelevement extends CommonObject
* @param string $executiondate Date to execute the transfer * @param string $executiondate Date to execute the transfer
* @param int $notrigger Disable triggers * @param int $notrigger Disable triggers
* @param string $type 'direct-debit' or 'bank-transfer' * @param string $type 'direct-debit' or 'bank-transfer'
* @return int <0 if KO, nbre of invoice withdrawed if OK * @return int <0 if KO, No of invoice included into file if OK
*/ */
public function create($banque = 0, $agence = 0, $mode = 'real', $format = 'ALL', $executiondate = '', $notrigger = 0, $type = 'direct-debit') public function create($banque = 0, $agence = 0, $mode = 'real', $format = 'ALL', $executiondate = '', $notrigger = 0, $type = 'direct-debit')
{ {
@ -831,7 +831,12 @@ class BonPrelevement extends CommonObject
require_once DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php"; require_once DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php";
require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php"; require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
if (empty($format)) return 'ErrorBadParametersForDirectDebitFileCreate'; if ($type != 'bank-transfer') {
if (empty($format)) {
$this->error = 'ErrorBadParametersForDirectDebitFileCreate';
return -1;
}
}
$error = 0; $error = 0;
@ -1091,6 +1096,7 @@ class BonPrelevement extends CommonObject
// Fetch invoice // Fetch invoice
$fact = new Facture($this->db); $fact = new Facture($this->db);
$fact->fetch($fac[0]); $fact->fetch($fac[0]);
/* /*
* Add standing order * Add standing order
* *
@ -1163,14 +1169,15 @@ class BonPrelevement extends CommonObject
$this->context['factures_prev'] = $factures_prev; $this->context['factures_prev'] = $factures_prev;
// Generation of SEPA file $this->filename // Generation of SEPA file $this->filename
$result = $this->generate($format, $executiondate); // This also the the property $this->total that is included into file
$result = $this->generate($format, $executiondate, $type);
} }
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; //var_dump($factures_prev);exit;
/* /*
* Update total * Update total defined after generation of file
*/ */
$sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons"; $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons";
$sql .= " SET amount = ".price2num($this->total); $sql .= " SET amount = ".price2num($this->total);
@ -1455,7 +1462,7 @@ class BonPrelevement extends CommonObject
/** /**
* Generate a withdrawal file. * Generate a direct debit or credit transfer file.
* Generation Formats: * Generation Formats:
* - Europe: SEPA (France: CFONB no more supported, Spain: AEB19 if external module EsAEB is enabled) * - Europe: SEPA (France: CFONB no more supported, Spain: AEB19 if external module EsAEB is enabled)
* - Others countries: Warning message * - Others countries: Warning message
@ -1463,9 +1470,10 @@ class BonPrelevement extends CommonObject
* *
* @param string $format FRST, RCUR or ALL * @param string $format FRST, RCUR or ALL
* @param string $executiondate Date to execute transfer * @param string $executiondate Date to execute transfer
* @param string $type 'direct-debit' or 'credit-transfer'
* @return int >=0 if OK, <0 if KO * @return int >=0 if OK, <0 if KO
*/ */
public function generate($format = 'ALL', $executiondate = '') public function generate($format = 'ALL', $executiondate = '', $type = 'direct-debit')
{ {
global $conf, $langs, $mysoc; global $conf, $langs, $mysoc;
@ -1473,7 +1481,7 @@ class BonPrelevement extends CommonObject
$result = 0; $result = 0;
dol_syslog(get_class($this)."::generate build file ".$this->filename); dol_syslog(get_class($this)."::generate build file=".$this->filename." type=".$type);
$this->file = fopen($this->filename, "w"); $this->file = fopen($this->filename, "w");
if (empty($this->file)) if (empty($this->file))
@ -1483,153 +1491,191 @@ class BonPrelevement extends CommonObject
} }
$found = 0; $found = 0;
$this->total = 0;
// Build file for European countries // Build file for European countries
if ($mysoc->isInEEC()) if ($mysoc->isInEEC())
{ {
$found++; $found++;
/** if ($type == 'bank-transfer') {
* SECTION CREATION FICHIER SEPA print 'TODO';
*/ exit;
// SEPA Initialisation } else {
$CrLf = "\n"; /**
* SECTION CREATION FICHIER SEPA
*/
// SEPA Initialisation
$CrLf = "\n";
$now = dol_now(); $now = dol_now();
$dateTime_ECMA = dol_print_date($now, '%Y-%m-%dT%H:%M:%S'); $dateTime_ECMA = dol_print_date($now, '%Y-%m-%dT%H:%M:%S');
$date_actu = $now; $date_actu = $now;
if (!empty($executiondate)) $date_actu = $executiondate; if (!empty($executiondate)) $date_actu = $executiondate;
$dateTime_YMD = dol_print_date($date_actu, '%Y%m%d'); $dateTime_YMD = dol_print_date($date_actu, '%Y%m%d');
$dateTime_YMDHMS = dol_print_date($date_actu, '%Y%m%d%H%M%S'); $dateTime_YMDHMS = dol_print_date($date_actu, '%Y%m%d%H%M%S');
$fileDebiteurSection = ''; $fileDebiteurSection = '';
$fileEmetteurSection = ''; $fileEmetteurSection = '';
$i = 0; $i = 0;
$this->total = 0;
/* /*
* Section Debitor (sepa Debiteurs bloc lines) * Section Debitor (sepa Debiteurs bloc lines)
*/ */
$sql = "SELECT soc.code_client as code, soc.address, soc.zip, soc.town, c.code as country_code,"; $sql = "SELECT soc.code_client as code, soc.address, soc.zip, soc.town, c.code as country_code,";
$sql .= " pl.client_nom as nom, pl.code_banque as cb, pl.code_guichet as cg, pl.number as cc, pl.amount as somme,"; $sql .= " pl.client_nom as nom, pl.code_banque as cb, pl.code_guichet as cg, pl.number as cc, pl.amount as somme,";
$sql .= " f.ref as fac, pf.fk_facture as idfac, rib.datec, rib.iban_prefix as iban, rib.bic as bic, rib.rowid as drum, rib.rum, rib.date_rum"; $sql .= " f.ref as fac, pf.fk_facture as idfac, rib.datec, rib.iban_prefix as iban, rib.bic as bic, rib.rowid as drum, rib.rum, rib.date_rum";
$sql .= " FROM"; $sql .= " FROM";
$sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,"; $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
$sql .= " ".MAIN_DB_PREFIX."facture as f,"; $sql .= " ".MAIN_DB_PREFIX."facture as f,";
$sql .= " ".MAIN_DB_PREFIX."prelevement_facture as pf,"; $sql .= " ".MAIN_DB_PREFIX."prelevement_facture as pf,";
$sql .= " ".MAIN_DB_PREFIX."societe as soc,"; $sql .= " ".MAIN_DB_PREFIX."societe as soc,";
$sql .= " ".MAIN_DB_PREFIX."c_country as c,"; $sql .= " ".MAIN_DB_PREFIX."c_country as c,";
$sql .= " ".MAIN_DB_PREFIX."societe_rib as rib"; $sql .= " ".MAIN_DB_PREFIX."societe_rib as rib";
$sql .= " WHERE pl.fk_prelevement_bons = ".$this->id; $sql .= " WHERE pl.fk_prelevement_bons = ".$this->id;
$sql .= " AND pl.rowid = pf.fk_prelevement_lignes"; $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
$sql .= " AND pf.fk_facture = f.rowid"; $sql .= " AND pf.fk_facture = f.rowid";
$sql .= " AND soc.fk_pays = c.rowid"; $sql .= " AND soc.fk_pays = c.rowid";
$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";
$sql .= " AND rib.type = 'ban'"; $sql .= " AND rib.type = 'ban'";
//print $sql; //print $sql;
// Define $fileDebiteurSection. One section DrctDbtTxInf per invoice. // Define $fileDebiteurSection. One section DrctDbtTxInf per invoice.
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{
$num = $this->db->num_rows($resql);
while ($i < $num)
{ {
$obj = $this->db->fetch_object($resql); $num = $this->db->num_rows($resql);
$daterum = (!empty($obj->date_rum)) ? $this->db->jdate($obj->date_rum) : $this->db->jdate($obj->datec); while ($i < $num)
$fileDebiteurSection .= $this->EnregDestinataireSEPA($obj->code, $obj->nom, $obj->address, $obj->zip, $obj->town, $obj->country_code, $obj->cb, $obj->cg, $obj->cc, $obj->somme, $obj->fac, $obj->idfac, $obj->iban, $obj->bic, $daterum, $obj->drum, $obj->rum); {
$this->total = $this->total + $obj->somme; $obj = $this->db->fetch_object($resql);
$i++; $daterum = (!empty($obj->date_rum)) ? $this->db->jdate($obj->date_rum) : $this->db->jdate($obj->datec);
$fileDebiteurSection .= $this->EnregDestinataireSEPA($obj->code, $obj->nom, $obj->address, $obj->zip, $obj->town, $obj->country_code, $obj->cb, $obj->cg, $obj->cc, $obj->somme, $obj->fac, $obj->idfac, $obj->iban, $obj->bic, $daterum, $obj->drum, $obj->rum);
$this->total = $this->total + $obj->somme;
$i++;
}
$nbtotalDrctDbtTxInf = $i;
}
else
{
fputs($this->file, 'ERROR DEBITOR '.$sql.$CrLf); // DEBITOR = Customers
$result = -2;
} }
$nbtotalDrctDbtTxInf = $i;
}
else
{
fputs($this->file, 'ERROR DEBITOR '.$sql.$CrLf); // DEBITOR = Customers
$result = -2;
}
// Define $fileEmetteurSection. Start of bloc PmtInf. Will contains all DrctDbtTxInf // Define $fileEmetteurSection. Start of bloc PmtInf. Will contains all DrctDbtTxInf
if ($result != -2) if ($result != -2)
{ {
$fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $nbtotalDrctDbtTxInf, $this->total, $CrLf, $format); $fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $nbtotalDrctDbtTxInf, $this->total, $CrLf, $format);
} }
else else
{ {
fputs($this->file, 'ERROR CREDITOR'.$CrLf); // CREDITOR = My company fputs($this->file, 'ERROR CREDITOR'.$CrLf); // CREDITOR = My company
} }
/** /**
* SECTION CREATION SEPA FILE * 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);
fputs($this->file, '<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'.$CrLf); fputs($this->file, '<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'.$CrLf);
fputs($this->file, ' <CstmrDrctDbtInitn>'.$CrLf); fputs($this->file, ' <CstmrDrctDbtInitn>'.$CrLf);
// SEPA Group header // SEPA Group header
fputs($this->file, ' <GrpHdr>'.$CrLf); fputs($this->file, ' <GrpHdr>'.$CrLf);
fputs($this->file, ' <MsgId>'.('PREL'.$dateTime_YMD.'/REF'.$this->id).'</MsgId>'.$CrLf); fputs($this->file, ' <MsgId>'.('PREL'.$dateTime_YMD.'/REF'.$this->id).'</MsgId>'.$CrLf);
fputs($this->file, ' <CreDtTm>'.$dateTime_ECMA.'</CreDtTm>'.$CrLf); fputs($this->file, ' <CreDtTm>'.$dateTime_ECMA.'</CreDtTm>'.$CrLf);
fputs($this->file, ' <NbOfTxs>'.$i.'</NbOfTxs>'.$CrLf); fputs($this->file, ' <NbOfTxs>'.$i.'</NbOfTxs>'.$CrLf);
fputs($this->file, ' <CtrlSum>'.$this->total.'</CtrlSum>'.$CrLf); fputs($this->file, ' <CtrlSum>'.$this->total.'</CtrlSum>'.$CrLf);
fputs($this->file, ' <InitgPty>'.$CrLf); fputs($this->file, ' <InitgPty>'.$CrLf);
fputs($this->file, ' <Nm>'.strtoupper(dol_string_unaccent($this->raison_sociale)).'</Nm>'.$CrLf); fputs($this->file, ' <Nm>'.strtoupper(dol_string_unaccent($this->raison_sociale)).'</Nm>'.$CrLf);
fputs($this->file, ' <Id>'.$CrLf); fputs($this->file, ' <Id>'.$CrLf);
fputs($this->file, ' <PrvtId>'.$CrLf); fputs($this->file, ' <PrvtId>'.$CrLf);
fputs($this->file, ' <Othr>'.$CrLf); fputs($this->file, ' <Othr>'.$CrLf);
fputs($this->file, ' <Id>'.$conf->global->PRELEVEMENT_ICS.'</Id>'.$CrLf); fputs($this->file, ' <Id>'.$conf->global->PRELEVEMENT_ICS.'</Id>'.$CrLf);
fputs($this->file, ' </Othr>'.$CrLf); fputs($this->file, ' </Othr>'.$CrLf);
fputs($this->file, ' </PrvtId>'.$CrLf); fputs($this->file, ' </PrvtId>'.$CrLf);
fputs($this->file, ' </Id>'.$CrLf); fputs($this->file, ' </Id>'.$CrLf);
fputs($this->file, ' </InitgPty>'.$CrLf); fputs($this->file, ' </InitgPty>'.$CrLf);
fputs($this->file, ' </GrpHdr>'.$CrLf); fputs($this->file, ' </GrpHdr>'.$CrLf);
// SEPA File Emetteur // SEPA File Emetteur
if ($result != -2) if ($result != -2)
{ fputs($this-> file, $fileEmetteurSection); } { fputs($this-> file, $fileEmetteurSection); }
// SEPA File Debiteurs // SEPA File Debiteurs
if ($result != -2) if ($result != -2)
{ fputs($this-> file, $fileDebiteurSection); } { fputs($this-> file, $fileDebiteurSection); }
// SEPA FILE FOOTER // SEPA FILE FOOTER
fputs($this->file, ' </PmtInf>'.$CrLf); fputs($this->file, ' </PmtInf>'.$CrLf);
fputs($this->file, ' </CstmrDrctDbtInitn>'.$CrLf); fputs($this->file, ' </CstmrDrctDbtInitn>'.$CrLf);
fputs($this->file, '</Document>'.$CrLf); fputs($this->file, '</Document>'.$CrLf);
}
} }
// Build file for Other Countries with unknow format // Build file for Other Countries with unknow format
if (!$found) if (!$found)
{ {
$this->total = 0; if ($type == 'bank-transfer') {
$sql = "SELECT pl.amount"; $sql = "SELECT pl.amount";
$sql .= " FROM"; $sql .= " FROM";
$sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,"; $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
$sql .= " ".MAIN_DB_PREFIX."facture as f,"; $sql .= " ".MAIN_DB_PREFIX."facture_fourn as f,";
$sql .= " ".MAIN_DB_PREFIX."prelevement_facture as pf"; $sql .= " ".MAIN_DB_PREFIX."prelevement_facture as pf";
$sql .= " WHERE pl.fk_prelevement_bons = ".$this->id; $sql .= " WHERE pl.fk_prelevement_bons = ".$this->id;
$sql .= " AND pl.rowid = pf.fk_prelevement_lignes"; $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
$sql .= " AND pf.fk_facture = f.rowid"; $sql .= " AND pf.fk_facture_fourn = f.rowid";
//Lines // Lines
$i = 0; $i = 0;
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{
$num = $this->db->num_rows($resql);
while ($i < $num)
{ {
$obj = $this->db->fetch_object($resql); $num = $this->db->num_rows($resql);
$this->total = $this->total + $obj->amount;
$i++; while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$this->total = $this->total + $obj->amount;
// TODO Write record into file
$i++;
}
}
else
{
$result = -2;
}
} else {
$sql = "SELECT pl.amount";
$sql .= " FROM";
$sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
$sql .= " ".MAIN_DB_PREFIX."facture as f,";
$sql .= " ".MAIN_DB_PREFIX."prelevement_facture as pf";
$sql .= " WHERE pl.fk_prelevement_bons = ".$this->id;
$sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
$sql .= " AND pf.fk_facture = f.rowid";
// Lines
$i = 0;
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$this->total = $this->total + $obj->amount;
// TODO Write record into file
$i++;
}
}
else
{
$result = -2;
} }
}
else
{
$result = -2;
} }
$langs->load('withdrawals'); $langs->load('withdrawals');

View File

@ -191,7 +191,8 @@ print '<input type="hidden" name="type" value="'.$type.'">';
if ($nb) { if ($nb) {
if ($pricetowithdraw) { if ($pricetowithdraw) {
print $langs->trans('ExecutionDate').' '; print $langs->trans('ExecutionDate').' ';
print $form->selectDate(); $datere = dol_mktime(0, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
print $form->selectDate($datere, 're');
if ($mysoc->isInEEC()) { if ($mysoc->isInEEC()) {
$title = $langs->trans("CreateForSepa"); $title = $langs->trans("CreateForSepa");
@ -200,7 +201,10 @@ if ($nb) {
} }
if ($type != 'bank-transfer') { if ($type != 'bank-transfer') {
print '<select name="format"><option value="FRST">'.$langs->trans('SEPAFRST').'</option><option value="RCUR">'.$langs->trans('SEPARCUR').'</option></select>'; print '<select name="format">';
print '<option value="FRST"'.(GETPOST('format', 'aZ09') == 'FRST' ? ' selected="selected"' : '').'>'.$langs->trans('SEPAFRST').'</option>';
print '<option value="RCUR"'.(GETPOST('format', 'aZ09') == 'RCUR' ? ' selected="selected"' : '').'>'.$langs->trans('SEPARCUR').'</option>';
print '</select>';
} }
print '<input class="butAction" type="submit" value="'.$title.'"/>'; print '<input class="butAction" type="submit" value="'.$title.'"/>';
} else { } else {