dolibarr  13.0.2
bonprelevement.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2010-2015 Juanjo Menent <jmenent@2byte.es>
5  * Copyright (C) 2010-2014 Laurent Destailleur <eldy@users.sourceforge.net>
6  * Copyright (C) 2014-2016 Ferran Marcet <fmarcet@2byte.es>
7  * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
29 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
35 require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
36 
37 
38 
43 {
47  public $element = 'widthdraw';
48 
52  public $table_element = 'prelevement_bons';
53 
57  public $picto = 'payment';
58 
59  public $date_echeance;
60  public $raison_sociale;
61  public $reference_remise;
62  public $emetteur_code_guichet;
63  public $emetteur_numero_compte;
64  public $emetteur_code_banque;
65  public $emetteur_number_key;
66 
67  public $emetteur_iban;
68  public $emetteur_bic;
69  public $emetteur_ics;
70 
71  public $date_trans;
72  public $user_trans;
73 
74  public $total;
75  public $fetched;
76  public $statut; // 0-Wait, 1-Trans, 2-Done
77  public $labelStatus = array();
78 
79  public $invoice_in_error = array();
80  public $thirdparty_in_error = array();
81 
82  const STATUS_DRAFT = 0;
83  const STATUS_TRANSFERED = 1;
84  const STATUS_CREDITED = 2;
85 
86 
92  public function __construct($db)
93  {
94  global $conf, $langs;
95 
96  $this->db = $db;
97 
98  $this->filename = '';
99 
100  $this->date_echeance = dol_now();
101  $this->raison_sociale = "";
102  $this->reference_remise = "";
103 
104  $this->emetteur_code_guichet = "";
105  $this->emetteur_numero_compte = "";
106  $this->emetteur_code_banque = "";
107  $this->emetteur_number_key = "";
108 
109  $this->emetteur_iban = "";
110  $this->emetteur_bic = "";
111  $this->emetteur_ics = "";
112 
113  $this->factures = array();
114 
115  $this->methodes_trans = array();
116 
117  $this->methodes_trans[0] = "Internet";
118 
119  $this->fetched = 0;
120  }
121 
122  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
137  public function AddFacture($invoice_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key, $type = 'debit-order')
138  {
139  // phpcs:enable
140  $result = 0;
141  $line_id = 0;
142 
143  // Add lines
144  $result = $this->addline($line_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key);
145 
146  if ($result == 0)
147  {
148  if ($line_id > 0)
149  {
150  $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_facture (";
151  if ($type != 'bank-transfer') {
152  $sql .= "fk_facture";
153  } else {
154  $sql .= "fk_facture_fourn";
155  }
156  $sql .= ",fk_prelevement_lignes";
157  $sql .= ") VALUES (";
158  $sql .= $invoice_id;
159  $sql .= ", ".$line_id;
160  $sql .= ")";
161 
162  if ($this->db->query($sql))
163  {
164  $result = 0;
165  } else {
166  $result = -1;
167  $this->errors[] = get_class($this)."::AddFacture ".$this->db->lasterror;
168  dol_syslog(get_class($this)."::AddFacture Error $result");
169  }
170  } else {
171  $result = -2;
172  $this->errors[] = get_class($this)."::AddFacture linedid Empty";
173  dol_syslog(get_class($this)."::AddFacture Error $result");
174  }
175  } else {
176  $result = -3;
177  dol_syslog(get_class($this)."::AddFacture Error $result");
178  }
179 
180  return $result;
181  }
182 
196  public function addline(&$line_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key)
197  {
198  $result = -1;
199  $concat = 0;
200 
201  if ($concat == 1)
202  {
203  /*
204  * We aggregate the lines
205  */
206  $sql = "SELECT rowid";
207  $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_lignes";
208  $sql .= " WHERE fk_prelevement_bons = ".$this->id;
209  $sql .= " AND fk_soc =".$client_id;
210  $sql .= " AND code_banque = '".$this->db->escape($code_banque)."'";
211  $sql .= " AND code_guichet = '".$this->db->escape($code_guichet)."'";
212  $sql .= " AND number = '".$this->db->escape($number)."'";
213 
214  $resql = $this->db->query($sql);
215  if ($resql)
216  {
217  $num = $this->db->num_rows($resql);
218  } else {
219  $result = -1;
220  }
221  } else {
222  /*
223  * No aggregate
224  */
225  $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_lignes (";
226  $sql .= "fk_prelevement_bons";
227  $sql .= ", fk_soc";
228  $sql .= ", client_nom";
229  $sql .= ", amount";
230  $sql .= ", code_banque";
231  $sql .= ", code_guichet";
232  $sql .= ", number";
233  $sql .= ", cle_rib";
234  $sql .= ") VALUES (";
235  $sql .= $this->id;
236  $sql .= ", ".$client_id;
237  $sql .= ", '".$this->db->escape($client_nom)."'";
238  $sql .= ", '".price2num($amount)."'";
239  $sql .= ", '".$this->db->escape($code_banque)."'";
240  $sql .= ", '".$this->db->escape($code_guichet)."'";
241  $sql .= ", '".$this->db->escape($number)."'";
242  $sql .= ", '".$this->db->escape($number_key)."'";
243  $sql .= ")";
244 
245  if ($this->db->query($sql))
246  {
247  $line_id = $this->db->last_insert_id(MAIN_DB_PREFIX."prelevement_lignes");
248  $result = 0;
249  } else {
250  $this->errors[] = get_class($this)."::addline Error -2 ".$this->db->lasterror;
251  dol_syslog(get_class($this)."::addline Error -2");
252  $result = -2;
253  }
254  }
255 
256  return $result;
257  }
258 
265  public function getErrorString($error)
266  {
267  global $langs;
268 
269  $errors = array();
270 
271  $errors[1027] = $langs->trans("DateInvalid");
272 
273  return $errors[abs($error)];
274  }
275 
283  public function fetch($rowid, $ref = '')
284  {
285  global $conf;
286 
287  $sql = "SELECT p.rowid, p.ref, p.amount, p.note";
288  $sql .= ", p.datec as dc";
289  $sql .= ", p.date_trans as date_trans";
290  $sql .= ", p.method_trans, p.fk_user_trans";
291  $sql .= ", p.date_credit as date_credit";
292  $sql .= ", p.fk_user_credit";
293  $sql .= ", p.type";
294  $sql .= ", p.statut as status";
295  $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p";
296  $sql .= " WHERE p.entity IN (".getEntity('invoice').")";
297  if ($rowid > 0) $sql .= " AND p.rowid = ".$rowid;
298  else $sql .= " AND p.ref = '".$this->db->escape($ref)."'";
299 
300  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
301  $result = $this->db->query($sql);
302  if ($result)
303  {
304  if ($this->db->num_rows($result))
305  {
306  $obj = $this->db->fetch_object($result);
307 
308  $this->id = $obj->rowid;
309  $this->ref = $obj->ref;
310  $this->amount = $obj->amount;
311  $this->note = $obj->note;
312  $this->datec = $this->db->jdate($obj->dc);
313 
314  $this->date_trans = $this->db->jdate($obj->date_trans);
315  $this->method_trans = $obj->method_trans;
316  $this->user_trans = $obj->fk_user_trans;
317 
318  $this->date_credit = $this->db->jdate($obj->date_credit);
319  $this->user_credit = $obj->fk_user_credit;
320 
321  $this->type = $obj->type;
322 
323  $this->status = $obj->status;
324  $this->statut = $obj->status; // For backward compatibility
325 
326  $this->fetched = 1;
327 
328  return 1;
329  } else {
330  dol_syslog(get_class($this)."::Fetch Erreur aucune ligne retournee");
331  return -1;
332  }
333  } else {
334  return -2;
335  }
336  }
337 
338  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
344  public function set_credite()
345  {
346  // phpcs:enable
347  global $user, $conf;
348 
349  $error = 0;
350 
351  if ($this->db->begin())
352  {
353  $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_bons";
354  $sql .= " SET statut = ".self::STATUS_TRANSFERED;
355  $sql .= " WHERE rowid = ".$this->id;
356  $sql .= " AND entity = ".$conf->entity;
357 
358  $result = $this->db->query($sql);
359  if (!$result)
360  {
361  dol_syslog(get_class($this)."::set_credite Erreur 1");
362  $error++;
363  }
364 
365  if (!$error)
366  {
367  $facs = array();
368  $facs = $this->getListInvoices();
369 
370  $num = count($facs);
371  for ($i = 0; $i < $num; $i++)
372  {
373  /* Tag invoice as paid */
374  dol_syslog(get_class($this)."::set_credite set_paid fac ".$facs[$i]);
375  $fac = new Facture($this->db);
376  $fac->fetch($facs[$i]);
377  $result = $fac->set_paid($user);
378  }
379  }
380 
381  if (!$error)
382  {
383  $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes";
384  $sql .= " SET statut = 2";
385  $sql .= " WHERE fk_prelevement_bons = ".$this->id;
386 
387  if (!$this->db->query($sql))
388  {
389  dol_syslog(get_class($this)."::set_credite Erreur 1");
390  $error++;
391  }
392  }
393 
394  /*
395  * End of procedure
396  */
397  if (!$error)
398  {
399  $this->db->commit();
400  return 0;
401  } else {
402  $this->db->rollback();
403  dol_syslog(get_class($this)."::set_credite ROLLBACK ");
404 
405  return -1;
406  }
407  } else {
408  dol_syslog(get_class($this)."::set_credite Ouverture transaction SQL impossible ");
409  return -2;
410  }
411  }
412 
413  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
421  public function set_infocredit($user, $date)
422  {
423  // phpcs:enable
424  global $conf, $langs;
425 
426  $error = 0;
427 
428  if ($this->fetched == 1)
429  {
430  if ($date < $this->date_trans)
431  {
432  $this->error = 'DateOfMovementLowerThanDateOfFileTransmission';
433  dol_syslog("bon-prelevment::set_infocredit 1027 ".$this->error);
434  return -1027;
435  }
436 
437  $this->db->begin();
438 
439  $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_bons ";
440  $sql .= " SET fk_user_credit = ".$user->id;
441  $sql .= ", statut = ".self::STATUS_CREDITED;
442  $sql .= ", date_credit = '".$this->db->idate($date)."'";
443  $sql .= " WHERE rowid=".$this->id;
444  $sql .= " AND entity = ".$conf->entity;
445  $sql .= " AND statut = ".self::STATUS_TRANSFERED;
446 
447  $resql = $this->db->query($sql);
448  if ($resql)
449  {
450  $langs->load('withdrawals');
451  $subject = $langs->trans("InfoCreditSubject", $this->ref);
452  $message = $langs->trans("InfoCreditMessage", $this->ref, dol_print_date($date, 'dayhour'));
453 
454  //Add payment of withdrawal into bank
455  $bankaccount = ($this->type == 'bank-transfer' ? $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT : $conf->global->PRELEVEMENT_ID_BANKACCOUNT);
456  $facs = array();
457  $amounts = array();
458  $amountsperthirdparty = array();
459 
460  $facs = $this->getListInvoices(1);
461 
462  // Loop on each invoice. $facs=array(0=>id, 1=>amount requested)
463  $num = count($facs);
464  for ($i = 0; $i < $num; $i++)
465  {
466  if ($this->type == 'bank-transfer') {
467  $fac = new FactureFournisseur($this->db);
468  } else {
469  $fac = new Facture($this->db);
470  }
471 
472  $result = $fac->fetch($facs[$i][0]);
473 
474  $amounts[$fac->id] = $facs[$i][1];
475  $amountsperthirdparty[$fac->socid][$fac->id] = $facs[$i][1];
476 
477  $totalpaye = $fac->getSommePaiement();
478  $totalcreditnotes = $fac->getSumCreditNotesUsed();
479  $totaldeposits = $fac->getSumDepositsUsed();
480  $alreadypayed = $totalpaye + $totalcreditnotes + $totaldeposits;
481 
482  // @TODO Move this after creation of payment
483  if (price2num($alreadypayed + $facs[$i][1], 'MT') == $fac->total_ttc) {
484  $result = $fac->set_paid($user);
485  if ($result < 0) {
486  $this->error = $fac->error;
487  $this->errors = $fac->errors;
488  }
489  }
490  }
491  //var_dump($amountsperthirdparty);exit;
492 
493  // Make one payment per customer
494  foreach ($amountsperthirdparty as $thirdpartyid => $cursoramounts)
495  {
496  if ($this->type == 'bank-transfer') {
497  $paiement = new PaiementFourn($this->db);
498  } else {
499  $paiement = new Paiement($this->db);
500  }
501  $paiement->datepaye = $date;
502  $paiement->amounts = $cursoramounts; // Array with detail of dispatching of payments for each invoice
503 
504  if ($this->type == 'bank-transfer') {
505  $paiement->paiementid = 2;
506  $paiement->paiementcode = 'VIR';
507  } else {
508  $paiement->paiementid = 3;
509  $paiement->paiementcode = 'PRE';
510  }
511 
512  $paiement->num_payment = $this->ref; // Set ref of direct debit note
513  $paiement->id_prelevement = $this->id;
514 
515  $paiement_id = $paiement->create($user); // This use ->paiementid, that is ID of payment mode
516  if ($paiement_id < 0)
517  {
518  $error++;
519  $this->error = $paiement->error;
520  $this->errors = $paiement->errors;
521  dol_syslog(get_class($this)."::set_infocredit AddPayment Error ".$this->error);
522  } else {
523  if ($this->type == 'bank-transfer') {
524  $modeforaddpayment = 'payment_supplier';
525  } else {
526  $modeforaddpayment = 'payment';
527  }
528 
529  $result = $paiement->addPaymentToBank($user, $modeforaddpayment, '(WithdrawalPayment)', $bankaccount, '', '');
530  if ($result < 0)
531  {
532  $error++;
533  $this->error = $paiement->error;
534  $this->errors = $paiement->errors;
535  dol_syslog(get_class($this)."::set_infocredit AddPaymentToBank Error ".$this->error);
536  }
537  }
538  //var_dump($paiement->amounts);
539  //var_dump($thirdpartyid);
540  //var_dump($cursoramounts);
541  }
542 
543  // Update withdrawal line
544  // TODO: Translate to ligneprelevement.class.php
545  if (!$error) {
546  $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes";
547  $sql .= " SET statut = 2";
548  $sql .= " WHERE fk_prelevement_bons = ".$this->id;
549 
550  if (!$this->db->query($sql)) {
551  dol_syslog(get_class($this)."::set_infocredit Update lines Error");
552  $error++;
553  }
554  }
555  } else {
556  $this->error = $this->db->lasterror();
557  dol_syslog(get_class($this)."::set_infocredit Update Bons Error");
558  $error++;
559  }
560 
561  /*
562  * End of procedure
563  */
564  if ($error == 0)
565  {
566  $this->date_credit = $date;
567  $this->statut = self::STATUS_CREDITED;
568 
569  $this->db->commit();
570  return 0;
571  } else {
572  $this->db->rollback();
573  return -1;
574  }
575  } else {
576  return -1026;
577  }
578  }
579 
580  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
589  public function set_infotrans($user, $date, $method)
590  {
591  // phpcs:enable
592  global $conf, $langs;
593 
594  $error = 0;
595 
596  dol_syslog(get_class($this)."::set_infotrans Start", LOG_INFO);
597  if ($this->db->begin())
598  {
599  $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons ";
600  $sql .= " SET fk_user_trans = ".$user->id;
601  $sql .= " , date_trans = '".$this->db->idate($date)."'";
602  $sql .= " , method_trans = ".$method;
603  $sql .= " , statut = ".self::STATUS_TRANSFERED;
604  $sql .= " WHERE rowid = ".$this->id;
605  $sql .= " AND entity = ".$conf->entity;
606  $sql .= " AND statut = 0";
607 
608  if ($this->db->query($sql))
609  {
610  $this->method_trans = $method;
611  $langs->load('withdrawals');
612  $subject = $langs->trans("InfoTransSubject", $this->ref);
613  $message = $langs->trans("InfoTransMessage", $this->ref, dolGetFirstLastname($user->firstname, $user->lastname));
614  $message .= $langs->trans("InfoTransData", price($this->amount), $this->methodes_trans[$this->method_trans], dol_print_date($date, 'day'));
615 
616  // TODO Call trigger to create a notification using notification module
617  } else {
618  $error++;
619  }
620 
621  if ($error == 0)
622  {
623  $this->date_trans = $date;
624  $this->statut = 1;
625  $this->user_trans = $user->id;
626  $this->db->commit();
627 
628  return 0;
629  } else {
630  $this->db->rollback();
631  dol_syslog(get_class($this)."::set_infotrans ROLLBACK", LOG_ERR);
632 
633  return -1;
634  }
635  } else {
636  dol_syslog(get_class($this)."::set_infotrans Ouverture transaction SQL impossible", LOG_CRIT);
637  return -2;
638  }
639  }
640 
647  private function getListInvoices($amounts = 0)
648  {
649  global $conf;
650 
651  $arr = array();
652 
653  /*
654  * Returns all invoices presented within same order
655  */
656  $sql = "SELECT ";
657  if ($this->type == 'bank-transfer') {
658  $sql .= " pf.fk_facture_fourn";
659  } else {
660  $sql .= " pf.fk_facture";
661  }
662  if ($amounts) $sql .= ", SUM(pl.amount)";
663  $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p";
664  $sql .= " , ".MAIN_DB_PREFIX."prelevement_lignes as pl";
665  $sql .= " , ".MAIN_DB_PREFIX."prelevement_facture as pf";
666  $sql .= " WHERE pf.fk_prelevement_lignes = pl.rowid";
667  $sql .= " AND pl.fk_prelevement_bons = p.rowid";
668  $sql .= " AND p.rowid = ".$this->id;
669  $sql .= " AND p.entity = ".$conf->entity;
670  if ($amounts) {
671  if ($this->type == 'bank-transfer') {
672  $sql .= " GROUP BY fk_facture_fourn";
673  } else {
674  $sql .= " GROUP BY fk_facture";
675  }
676  }
677 
678  $resql = $this->db->query($sql);
679  if ($resql)
680  {
681  $num = $this->db->num_rows($resql);
682 
683  if ($num)
684  {
685  $i = 0;
686  while ($i < $num)
687  {
688  $row = $this->db->fetch_row($resql);
689  if (!$amounts) $arr[$i] = $row[0];
690  else {
691  $arr[$i] = array(
692  $row[0],
693  $row[1]
694  );
695  }
696  $i++;
697  }
698  }
699  $this->db->free($resql);
700  } else {
701  dol_syslog(get_class($this)."::getListInvoices Erreur");
702  }
703 
704  return $arr;
705  }
706 
707  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
714  public function SommeAPrelever($mode = 'direct-debit')
715  {
716  // phpcs:enable
717  global $conf;
718 
719  $sql = "SELECT sum(pfd.amount) as nb";
720  if ($mode != 'bank-transfer') {
721  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f,";
722  } else {
723  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f,";
724  }
725  $sql .= " ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
726  $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
727  if (empty($conf->global->WITHDRAWAL_ALLOW_ANY_INVOICE_STATUS))
728  {
729  $sql .= " AND f.fk_statut = ".Facture::STATUS_VALIDATED;
730  }
731  if ($mode != 'bank-transfer') {
732  $sql .= " AND f.rowid = pfd.fk_facture";
733  } else {
734  $sql .= " AND f.rowid = pfd.fk_facture_fourn";
735  }
736  $sql .= " AND f.paye = 0";
737  $sql .= " AND pfd.traite = 0";
738  $sql .= " AND pfd.ext_payment_id IS NULL";
739  $sql .= " AND f.total_ttc > 0";
740 
741  $resql = $this->db->query($sql);
742  if ($resql)
743  {
744  $obj = $this->db->fetch_object($resql);
745 
746  $this->db->free($resql);
747 
748  return $obj->nb;
749  } else {
750  $error = 1;
751  dol_syslog(get_class($this)."::SommeAPrelever Erreur -1");
752  dol_syslog($this->db->error());
753 
754  return -1;
755  }
756  }
757 
764  public function nbOfInvoiceToPay($mode = 'direct-debit')
765  {
766  return $this->NbFactureAPrelever($mode);
767  }
768 
769  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
776  public function NbFactureAPrelever($type = 'direct-debit')
777  {
778  // phpcs:enable
779  global $conf;
780 
781  $sql = "SELECT count(f.rowid) as nb";
782  if ($type == 'bank-transfer') {
783  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
784  } else {
785  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
786  }
787  $sql .= ", ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
788  $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
789  if (empty($conf->global->WITHDRAWAL_ALLOW_ANY_INVOICE_STATUS))
790  {
791  $sql .= " AND f.fk_statut = ".Facture::STATUS_VALIDATED;
792  }
793  if ($type == 'bank-transfer') {
794  $sql .= " AND f.rowid = pfd.fk_facture_fourn";
795  } else {
796  $sql .= " AND f.rowid = pfd.fk_facture";
797  }
798  $sql .= " AND pfd.traite = 0";
799  $sql .= " AND pfd.ext_payment_id IS NULL";
800  $sql .= " AND f.total_ttc > 0";
801 
802  dol_syslog(get_class($this)."::NbFactureAPrelever");
803  $resql = $this->db->query($sql);
804 
805  if ($resql)
806  {
807  $obj = $this->db->fetch_object($resql);
808 
809  $this->db->free($resql);
810 
811  return $obj->nb;
812  } else {
813  $this->error = get_class($this)."::NbFactureAPrelever Erreur -1 sql=".$this->db->error();
814  return -1;
815  }
816  }
817 
818 
819  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
833  public function create($banque = 0, $agence = 0, $mode = 'real', $format = 'ALL', $executiondate = '', $notrigger = 0, $type = 'direct-debit')
834  {
835  // phpcs:enable
836  global $conf, $langs, $user;
837 
838  dol_syslog(__METHOD__."::Bank=".$banque." Office=".$agence." mode=".$mode." format=".$format, LOG_DEBUG);
839 
840  require_once DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php";
841  require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
842 
843  if ($type != 'bank-transfer') {
844  if (empty($format)) {
845  $this->error = 'ErrorBadParametersForDirectDebitFileCreate';
846  return -1;
847  }
848  }
849 
850  $error = 0;
851 
852  $datetimeprev = time();
853  //Choice the date of the execution direct debit
854  if (!empty($executiondate)) $datetimeprev = $executiondate;
855 
856  $month = strftime("%m", $datetimeprev);
857  $year = strftime("%Y", $datetimeprev);
858 
859  $this->invoice_in_error = array();
860  $this->thirdparty_in_error = array();
861 
862  // Read invoices
863  $factures = array();
864  $factures_prev = array();
865  $factures_result = array();
866  $factures_prev_id = array();
867  $factures_errors = array();
868 
869  if (!$error)
870  {
871  $sql = "SELECT f.rowid, pfd.rowid as pfdrowid, f.fk_soc";
872  $sql .= ", pfd.code_banque, pfd.code_guichet, pfd.number, pfd.cle_rib";
873  $sql .= ", pfd.amount";
874  $sql .= ", s.nom as name";
875  if ($type != 'bank-transfer') {
876  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
877  } else {
878  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
879  }
880  $sql .= ", ".MAIN_DB_PREFIX."societe as s";
881  $sql .= ", ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
882  $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
883  if ($type != 'bank-transfer') {
884  $sql .= " AND f.rowid = pfd.fk_facture";
885  } else {
886  $sql .= " AND f.rowid = pfd.fk_facture_fourn";
887  }
888  $sql .= " AND s.rowid = f.fk_soc";
889  $sql .= " AND f.fk_statut = 1"; // Invoice validated
890  $sql .= " AND f.paye = 0";
891  $sql .= " AND pfd.traite = 0";
892  $sql .= " AND f.total_ttc > 0";
893  $sql .= " AND pfd.ext_payment_id IS NULL";
894 
895  dol_syslog(__METHOD__."::Read invoices, sql=".$sql, LOG_DEBUG);
896 
897  $resql = $this->db->query($sql);
898  if ($resql)
899  {
900  $num = $this->db->num_rows($resql);
901  $i = 0;
902 
903  while ($i < $num)
904  {
905  $row = $this->db->fetch_row($resql);
906  $factures[$i] = $row; // All fields
907  if ($row[7] == 0) {
908  $error++;
909  dol_syslog(__METHOD__."::Read invoices error Found a null invoice", LOG_ERR);
910  $this->invoice_in_error[$row[0]] = "Error for invoice id ".$row[0].", found a null amount";
911  break;
912  }
913  $i++;
914  }
915  $this->db->free($resql);
916  dol_syslog(__METHOD__."::Read invoices, ".$i." invoices to withdraw", LOG_DEBUG);
917  } else {
918  $error++;
919  dol_syslog(__METHOD__."::Read invoices error ".$this->db->error(), LOG_ERR);
920  }
921  }
922 
923  if (!$error)
924  {
925  require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
926  $soc = new Societe($this->db);
927 
928  // Check BAN
929  $i = 0;
930  dol_syslog(__METHOD__."::Check BAN", LOG_DEBUG);
931 
932  if (count($factures) > 0)
933  {
934  foreach ($factures as $key => $fac)
935  {
936  if ($type != 'bank-transfer') {
937  $tmpinvoice = new Facture($this->db);
938  } else {
939  $tmpinvoice = new FactureFournisseur($this->db);
940  }
941  $resfetch = $tmpinvoice->fetch($fac[0]);
942  if ($resfetch >= 0) // Field 0 of $fac is rowid of invoice
943  {
944  if ($soc->fetch($tmpinvoice->socid) >= 0)
945  {
946  $bac = new CompanyBankAccount($this->db);
947  $bac->fetch(0, $soc->id);
948 
949  if ($type != 'bank-transfer') {
950  if ($format == 'FRST' && $bac->frstrecur != 'FRST')
951  {
952  continue;
953  }
954  if ($format == 'RCUR' && ($bac->frstrecur != 'RCUR' && $bac->frstrecur != 'RECUR'))
955  {
956  continue;
957  }
958  }
959 
960  if ($bac->verif() >= 1)
961  {
962  $factures_prev[$i] = $fac;
963  /* second array necessary for BonPrelevement */
964  $factures_prev_id[$i] = $fac[0];
965  $i++;
966  //dol_syslog(__METHOD__."::RIB is ok", LOG_DEBUG);
967  } else {
968  dol_syslog(__METHOD__."::Check BAN Error on default bank number IBAN/BIC for thirdparty reported by verif() ".$tmpinvoice->socid." ".$soc->name, LOG_WARNING);
969  $this->invoice_in_error[$fac[0]] = "Error on default bank number IBAN/BIC for invoice ".$tmpinvoice->getNomUrl(0)." for thirdparty ".$soc->getNomUrl(0);
970  $this->thirdparty_in_error[$soc->id] = "Error on default bank number IBAN/BIC for invoice ".$tmpinvoice->getNomUrl(0)." for thirdparty ".$soc->getNomUrl(0);
971  }
972  } else {
973  dol_syslog(__METHOD__."::Check BAN Failed to read company", LOG_WARNING);
974  }
975  } else {
976  dol_syslog(__METHOD__."::Check BAN Failed to read invoice", LOG_WARNING);
977  }
978  }
979  } else {
980  dol_syslog(__METHOD__."::Check BAN No invoice to process", LOG_WARNING);
981  }
982  }
983 
984  $ok = 0;
985 
986  // Withdraw invoices in factures_prev array
987  $out = count($factures_prev)." invoices will be included.";
988  //print $out."\n";
989  dol_syslog($out);
990 
991  // Return warning
992  /*$i=0;
993  foreach ($this->thirdparty_in_error as $key => $val)
994  {
995  if ($i < 10) setEventMessages($val, null, 'warnings');
996  else setEventMessages('More error were discarded...', null, 'warnings');
997  $i++;
998  }*/
999 
1000  if (count($factures_prev) > 0)
1001  {
1002  if ($mode == 'real') {
1003  $ok = 1;
1004  } else {
1005  print $langs->trans("ModeWarning"); // "Option for real mode was not set, we stop after this simulation\n";
1006  }
1007  }
1008 
1009  if ($ok)
1010  {
1011  /*
1012  * We are in real mode.
1013  * We create order and build file into disk
1014  */
1015  $this->db->begin();
1016 
1017  $now = dol_now();
1018 
1019  /*
1020  * Process order generation
1021  */
1022  if (!$error)
1023  {
1024  $ref = substr($year, -2).$month;
1025 
1026  $sql = "SELECT substring(ref from char_length(ref) - 1)";
1027  $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_bons";
1028  $sql .= " WHERE ref LIKE '%".$this->db->escape($ref)."%'";
1029  $sql .= " AND entity = ".$conf->entity;
1030  $sql .= " ORDER BY ref DESC LIMIT 1";
1031 
1032  dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
1033  $resql = $this->db->query($sql);
1034 
1035  if ($resql)
1036  {
1037  $row = $this->db->fetch_row($resql);
1038  $ref = "T".$ref.str_pad(dol_substr("00".intval($row[0]) + 1, 0, 2), 2, "0", STR_PAD_LEFT);
1039 
1040  if ($type != 'bank-transfer') {
1041  $dir = $conf->prelevement->dir_output.'/receipts';
1042  } else {
1043  $dir = $conf->paymentbybanktransfer->dir_output.'/receipts';
1044  }
1045  if (!is_dir($dir)) dol_mkdir($dir);
1046 
1047  $this->filename = $dir.'/'.$ref.'.xml';
1048 
1049  // Create withdraw receipt in database
1050  $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_bons (";
1051  $sql .= "ref, entity, datec, type";
1052  $sql .= ") VALUES (";
1053  $sql .= "'".$this->db->escape($ref)."'";
1054  $sql .= ", ".$conf->entity;
1055  $sql .= ", '".$this->db->idate($now)."'";
1056  $sql .= ", '".($type == 'bank-transfer' ? 'bank-transfer' : 'debit-order')."'";
1057  $sql .= ")";
1058 
1059  $resql = $this->db->query($sql);
1060  if ($resql)
1061  {
1062  $prev_id = $this->db->last_insert_id(MAIN_DB_PREFIX."prelevement_bons");
1063  $this->id = $prev_id;
1064  $this->ref = $ref;
1065  } else {
1066  $error++;
1067  dol_syslog(__METHOD__."::Create withdraw receipt ".$this->db->lasterror(), LOG_ERR);
1068  }
1069  } else {
1070  $error++;
1071  dol_syslog(__METHOD__."::Get last withdraw receipt ".$this->db->lasterror(), LOG_ERR);
1072  }
1073  }
1074 
1075  if (!$error)
1076  {
1077  if ($type != 'bank-transfer') {
1078  $fact = new Facture($this->db);
1079  } else {
1080  $fact = new FactureFournisseur($this->db);
1081  }
1082 
1083  /*
1084  * Create withdrawal receipt in database
1085  */
1086  if (count($factures_prev) > 0)
1087  {
1088  foreach ($factures_prev as $fac) // Add a link in database for each invoice
1089  {
1090  // Fetch invoice
1091  $result = $fact->fetch($fac[0]);
1092  if ($result < 0) {
1093  $this->error = 'ERRORBONPRELEVEMENT Failed to load invoice with id '.$fac[0];
1094  break;
1095  }
1096 
1097  /*
1098  * Add standing order. This add record into llx_prelevement_lignes
1099  *
1100  * $fac[0] : invoice_id
1101  * $fac[1] : ???
1102  * $fac[2] : third party id
1103  * $fac[3] : banque
1104  * $fac[4] : guichet
1105  * $fac[5] : number
1106  * $fac[6] : cle rib
1107  * $fac[7] : amount
1108  * $fac[8] : client nom
1109  */
1110  $ri = $this->AddFacture($fac[0], $fac[2], $fac[8], $fac[7], $fac[3], $fac[4], $fac[5], $fac[6], $type);
1111  if ($ri <> 0)
1112  {
1113  $error++;
1114  }
1115 
1116  // Update invoice requests as done
1117  $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_facture_demande";
1118  $sql .= " SET traite = 1";
1119  $sql .= ", date_traite = '".$this->db->idate($now)."'";
1120  $sql .= ", fk_prelevement_bons = ".$this->id;
1121  $sql .= " WHERE rowid = ".$fac[1];
1122 
1123  $resql = $this->db->query($sql);
1124  if (!$resql)
1125  {
1126  $error++;
1127  $this->errors[] = $this->db->lasterror();
1128  dol_syslog(__METHOD__."::Update Error=".$this->db->lasterror(), LOG_ERR);
1129  }
1130  }
1131  }
1132  }
1133 
1134  if (!$error)
1135  {
1136  /*
1137  * Create file of type='direct-debit' for direct debit order or type='bank-transfer' for credit transfer into a XML file
1138  */
1139 
1140  dol_syslog(__METHOD__."::Init direct debit or credit transfer file for ".count($factures_prev)." invoices", LOG_DEBUG);
1141 
1142  if (count($factures_prev) > 0)
1143  {
1144  $this->date_echeance = $datetimeprev;
1145  $this->reference_remise = $ref;
1146 
1147  $id = $conf->global->PRELEVEMENT_ID_BANKACCOUNT;
1148  if ($type == 'bank-transfer') {
1149  $id = $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT;
1150  }
1151  $account = new Account($this->db);
1152  if ($account->fetch($id) > 0)
1153  {
1154  $this->emetteur_code_banque = $account->code_banque;
1155  $this->emetteur_code_guichet = $account->code_guichet;
1156  $this->emetteur_numero_compte = $account->number;
1157  $this->emetteur_number_key = $account->cle_rib;
1158  $this->emetteur_iban = $account->iban;
1159  $this->emetteur_bic = $account->bic;
1160 
1161  $this->emetteur_ics = $conf->global->PRELEVEMENT_ICS; // Ex: PRELEVEMENT_ICS = "FR78ZZZ123456";
1162 
1163  $this->raison_sociale = $account->proprio;
1164  }
1165 
1166  $this->factures = $factures_prev_id;
1167  $this->context['factures_prev'] = $factures_prev;
1168 
1169  // Generation of direct debit or credti transfer file $this->filename (May be a SEPA file for european countries)
1170  // This also set the property $this->total with amount that is included into file
1171  $result = $this->generate($format, $executiondate, $type);
1172  if ($result < 0) {
1173  /*var_dump($this->error);
1174  var_dump($this->invoice_in_error); */
1175  $error++;
1176  }
1177  }
1178  dol_syslog(__METHOD__."::End withdraw receipt, file ".$this->filename, LOG_DEBUG);
1179  }
1180  //var_dump($this->total);exit;
1181 
1182  /*
1183  * Update total defined after generation of file
1184  */
1185  if (!$error) {
1186  $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons";
1187  $sql .= " SET amount = ".price2num($this->total);
1188  $sql .= " WHERE rowid = ".$this->id;
1189  $sql .= " AND entity = ".$conf->entity;
1190 
1191  $resql = $this->db->query($sql);
1192  if (!$resql)
1193  {
1194  $error++;
1195  dol_syslog(__METHOD__."::Error update total: ".$this->db->error(), LOG_ERR);
1196  }
1197  }
1198 
1199  if (!$error && !$notrigger)
1200  {
1201  $triggername = 'DIRECT_DEBIT_ORDER_CREATE';
1202  if ($type != 'bank-transfer') {
1203  $triggername = 'CREDIT_TRANSFER_ORDER_CREATE';
1204  }
1205 
1206  // Call trigger
1207  $result = $this->call_trigger($triggername, $user);
1208  if ($result < 0) $error++;
1209  // End call triggers
1210  }
1211 
1212  if (!$error)
1213  {
1214  $this->db->commit();
1215  return count($factures_prev);
1216  } else {
1217  $this->db->rollback();
1218  return -1;
1219  }
1220  } else {
1221  return 0;
1222  }
1223  }
1224 
1225 
1233  public function delete($user = null, $notrigger = 0)
1234  {
1235  $this->db->begin();
1236 
1237  $error = 0;
1238  $resql1 = $resql2 = $resql3 = $resql4 = 0;
1239 
1240  if (!$notrigger)
1241  {
1242  $triggername = 'DIRECT_DEBIT_ORDER_DELETE';
1243  if ($this->type == 'bank-transfer') {
1244  $triggername = 'PAYMENTBYBANKTRANFER_DELETE';
1245  }
1246  // Call trigger
1247  $result = $this->call_trigger($triggername, $user);
1248  if ($result < 0) $error++;
1249  // End call triggers
1250  }
1251 
1252  if (!$error)
1253  {
1254  $sql = "DELETE FROM ".MAIN_DB_PREFIX."prelevement_facture WHERE fk_prelevement_lignes IN (SELECT rowid FROM ".MAIN_DB_PREFIX."prelevement_lignes WHERE fk_prelevement_bons = ".$this->id.")";
1255  $resql1 = $this->db->query($sql);
1256  if (!$resql1) dol_print_error($this->db);
1257  }
1258 
1259  if (!$error)
1260  {
1261  $sql = "DELETE FROM ".MAIN_DB_PREFIX."prelevement_lignes WHERE fk_prelevement_bons = ".$this->id;
1262  $resql2 = $this->db->query($sql);
1263  if (!$resql2) dol_print_error($this->db);
1264  }
1265 
1266  if (!$error)
1267  {
1268  $sql = "DELETE FROM ".MAIN_DB_PREFIX."prelevement_bons WHERE rowid = ".$this->id;
1269  $resql3 = $this->db->query($sql);
1270  if (!$resql3) dol_print_error($this->db);
1271  }
1272 
1273  if (!$error)
1274  {
1275  $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_facture_demande SET fk_prelevement_bons = NULL, traite = 0 WHERE fk_prelevement_bons = ".$this->id;
1276  $resql4 = $this->db->query($sql);
1277  if (!$resql4) dol_print_error($this->db);
1278  }
1279 
1280  if ($resql1 && $resql2 && $resql3 && $resql4 && !$error)
1281  {
1282  $this->db->commit();
1283  return 1;
1284  } else {
1285  $this->db->rollback();
1286  return -1;
1287  }
1288  }
1289 
1290 
1301  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
1302  {
1303  global $conf, $langs, $hookmanager;
1304 
1305  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
1306 
1307  $result = '';
1308 
1309  $labeltoshow = 'PaymentByDirectDebit';
1310  if ($this->type == 'bank-transfer') {
1311  $labeltoshow = 'PaymentByBankTransfer';
1312  }
1313 
1314  $label = '<u>'.$langs->trans($labeltoshow).'</u>';
1315  $label .= '<br>';
1316  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
1317  if (isset($this->statut)) {
1318  $label .= '<br><b>'.$langs->trans("Status").":</b> ".$this->getLibStatut(5);
1319  }
1320 
1321  $url = DOL_URL_ROOT.'/compta/prelevement/card.php?id='.$this->id;
1322  if ($this->type == 'bank-transfer') {
1323  $url = DOL_URL_ROOT.'/compta/prelevement/card.php?id='.$this->id;
1324  }
1325 
1326  if ($option != 'nolink')
1327  {
1328  // Add param to save lastsearch_values or not
1329  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1330  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
1331  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
1332  }
1333 
1334  $linkclose = '';
1335  if (empty($notooltip))
1336  {
1337  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
1338  {
1339  $label = $langs->trans("ShowMyObject");
1340  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1341  }
1342  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1343  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1344 
1345  /*
1346  $hookmanager->initHooks(array('myobjectdao'));
1347  $parameters=array('id'=>$this->id);
1348  $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
1349  if ($reshook > 0) $linkclose = $hookmanager->resPrint;
1350  */
1351  } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1352 
1353  $linkstart = '<a href="'.$url.'"';
1354  $linkstart .= $linkclose.'>';
1355  $linkend = '</a>';
1356 
1357  $result .= $linkstart;
1358  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1359  if ($withpicto != 2) $result .= $this->ref;
1360  $result .= $linkend;
1361  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1362 
1363  global $action, $hookmanager;
1364  $hookmanager->initHooks(array('banktransferdao'));
1365  $parameters = array('id'=>$this->id, 'getnomurl'=>$result);
1366  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1367  if ($reshook > 0) $result = $hookmanager->resPrint;
1368  else $result .= $hookmanager->resPrint;
1369 
1370  return $result;
1371  }
1372 
1373 
1374  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1381  public function DeleteNotificationById($rowid)
1382  {
1383  // phpcs:enable
1384  $result = 0;
1385 
1386  $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def";
1387  $sql .= " WHERE rowid = ".((int) $rowid);
1388 
1389  if ($this->db->query($sql))
1390  {
1391  return 0;
1392  } else {
1393  return -1;
1394  }
1395  }
1396 
1397  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1405  public function DeleteNotification($user, $action)
1406  {
1407  // phpcs:enable
1408  $result = 0;
1409 
1410  $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def";
1411  $sql .= " WHERE fk_user=".$user." AND fk_action='".$this->db->escape($action)."'";
1412 
1413  if ($this->db->query($sql))
1414  {
1415  return 0;
1416  } else {
1417  return -1;
1418  }
1419  }
1420 
1421  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1430  public function AddNotification($db, $user, $action)
1431  {
1432  // phpcs:enable
1433  $result = 0;
1434 
1435  if ($this->DeleteNotification($user, $action) == 0)
1436  {
1437  $now = dol_now();
1438 
1439  $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify_def (datec,fk_user, fk_soc, fk_contact, fk_action)";
1440  $sql .= " VALUES ('".$this->db->idate($now)."', ".$user.", 'NULL', 'NULL', '".$this->db->escape($action)."')";
1441 
1442  dol_syslog("adnotiff: ".$sql);
1443  if ($this->db->query($sql))
1444  {
1445  $result = 0;
1446  } else {
1447  $result = -1;
1448  dol_syslog(get_class($this)."::AddNotification Error $result");
1449  }
1450  }
1451 
1452  return $result;
1453  }
1454 
1455 
1468  public function generate($format = 'ALL', $executiondate = '', $type = 'direct-debit')
1469  {
1470  global $conf, $langs, $mysoc;
1471 
1472  //TODO: Optimize code to read lines in a single function
1473 
1474  $result = 0;
1475 
1476  dol_syslog(get_class($this)."::generate build file=".$this->filename." type=".$type);
1477 
1478  $this->file = fopen($this->filename, "w");
1479  if (empty($this->file))
1480  {
1481  $this->error = $langs->trans('ErrorFailedToOpenFile', $this->filename);
1482  return -1;
1483  }
1484 
1485  $found = 0;
1486  $this->total = 0;
1487 
1488  // Build file for European countries
1489  if ($mysoc->isInEEC())
1490  {
1491  $found++;
1492 
1493  if ($type != 'bank-transfer') {
1497  // SEPA Initialisation
1498  $CrLf = "\n";
1499 
1500  $now = dol_now();
1501 
1502  $dateTime_ECMA = dol_print_date($now, '%Y-%m-%dT%H:%M:%S');
1503 
1504  $date_actu = $now;
1505  if (!empty($executiondate)) $date_actu = $executiondate;
1506 
1507  $dateTime_YMD = dol_print_date($date_actu, '%Y%m%d');
1508  $dateTime_YMDHMS = dol_print_date($date_actu, '%Y%m%d%H%M%S');
1509  $fileDebiteurSection = '';
1510  $fileEmetteurSection = '';
1511  $i = 0;
1512 
1513  /*
1514  * Section Debitor (sepa Debiteurs bloc lines)
1515  */
1516 
1517  $sql = "SELECT soc.rowid as socid, soc.code_client as code, soc.address, soc.zip, soc.town, c.code as country_code,";
1518  $sql .= " pl.client_nom as nom, pl.code_banque as cb, pl.code_guichet as cg, pl.number as cc, pl.amount as somme,";
1519  $sql .= " f.ref as fac, pf.fk_facture as idfac,";
1520  $sql .= " rib.rowid, rib.datec, rib.iban_prefix as iban, rib.bic as bic, rib.rowid as drum, rib.rum, rib.date_rum";
1521  $sql .= " FROM";
1522  $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
1523  $sql .= " ".MAIN_DB_PREFIX."facture as f,";
1524  $sql .= " ".MAIN_DB_PREFIX."prelevement_facture as pf,";
1525  $sql .= " ".MAIN_DB_PREFIX."societe as soc,";
1526  $sql .= " ".MAIN_DB_PREFIX."c_country as c,";
1527  $sql .= " ".MAIN_DB_PREFIX."societe_rib as rib";
1528  $sql .= " WHERE pl.fk_prelevement_bons = ".$this->id;
1529  $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
1530  $sql .= " AND pf.fk_facture = f.rowid";
1531  $sql .= " AND f.fk_soc = soc.rowid";
1532  $sql .= " AND soc.fk_pays = c.rowid";
1533  $sql .= " AND rib.fk_soc = f.fk_soc";
1534  $sql .= " AND rib.default_rib = 1";
1535  $sql .= " AND rib.type = 'ban'";
1536 
1537  // Define $fileDebiteurSection. One section DrctDbtTxInf per invoice.
1538  $resql = $this->db->query($sql);
1539  if ($resql)
1540  {
1541  $cachearraytotestduplicate = array();
1542 
1543  $num = $this->db->num_rows($resql);
1544  while ($i < $num)
1545  {
1546  $obj = $this->db->fetch_object($resql);
1547 
1548  if (!empty($cachearraytotestduplicate[$obj->idfac])) {
1549  $this->error = $langs->trans('ErrorCompanyHasDuplicateDefaultBAN', $obj->socid);
1550  $this->invoice_in_error[$obj->idfac] = $this->error;
1551  $result = -2;
1552  break;
1553  }
1554  $cachearraytotestduplicate[$obj->idfac] = $obj->rowid;
1555 
1556  $daterum = (!empty($obj->date_rum)) ? $this->db->jdate($obj->date_rum) : $this->db->jdate($obj->datec);
1557  $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, $type);
1558  $this->total = $this->total + $obj->somme;
1559  $i++;
1560  }
1561  $nbtotalDrctDbtTxInf = $i;
1562  } else {
1563  $this->error = $this->db->lasterror();
1564  fputs($this->file, 'ERROR DEBITOR '.$sql.$CrLf); // DEBITOR = Customers
1565  $result = -2;
1566  }
1567 
1568  // Define $fileEmetteurSection. Start of bloc PmtInf. Will contains all $nbtotalDrctDbtTxInf
1569  if ($result != -2)
1570  {
1571  $fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $nbtotalDrctDbtTxInf, $this->total, $CrLf, $format, $type);
1572  }
1573 
1577  // SEPA File Header
1578  fputs($this->file, '<'.'?xml version="1.0" encoding="UTF-8" standalone="yes"?'.'>'.$CrLf);
1579  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);
1580  fputs($this->file, ' <CstmrDrctDbtInitn>'.$CrLf);
1581  // SEPA Group header
1582  fputs($this->file, ' <GrpHdr>'.$CrLf);
1583  fputs($this->file, ' <MsgId>'.('DD/'.$dateTime_YMD.'/REF'.$this->id).'</MsgId>'.$CrLf);
1584  fputs($this->file, ' <CreDtTm>'.$dateTime_ECMA.'</CreDtTm>'.$CrLf);
1585  fputs($this->file, ' <NbOfTxs>'.$i.'</NbOfTxs>'.$CrLf);
1586  fputs($this->file, ' <CtrlSum>'.$this->total.'</CtrlSum>'.$CrLf);
1587  fputs($this->file, ' <InitgPty>'.$CrLf);
1588  fputs($this->file, ' <Nm>'.strtoupper(dol_string_unaccent($this->raison_sociale)).'</Nm>'.$CrLf);
1589  fputs($this->file, ' <Id>'.$CrLf);
1590  fputs($this->file, ' <PrvtId>'.$CrLf);
1591  fputs($this->file, ' <Othr>'.$CrLf);
1592  fputs($this->file, ' <Id>'.$conf->global->PRELEVEMENT_ICS.'</Id>'.$CrLf);
1593  fputs($this->file, ' </Othr>'.$CrLf);
1594  fputs($this->file, ' </PrvtId>'.$CrLf);
1595  fputs($this->file, ' </Id>'.$CrLf);
1596  fputs($this->file, ' </InitgPty>'.$CrLf);
1597  fputs($this->file, ' </GrpHdr>'.$CrLf);
1598  // SEPA File Emetteur
1599  if ($result != -2)
1600  { fputs($this-> file, $fileEmetteurSection); }
1601  // SEPA File Debiteurs
1602  if ($result != -2)
1603  { fputs($this-> file, $fileDebiteurSection); }
1604  // SEPA FILE FOOTER
1605  fputs($this->file, ' </PmtInf>'.$CrLf);
1606  fputs($this->file, ' </CstmrDrctDbtInitn>'.$CrLf);
1607  fputs($this->file, '</Document>'.$CrLf);
1608  } else {
1612  // SEPA Initialisation
1613  $CrLf = "\n";
1614 
1615  $now = dol_now();
1616 
1617  $dateTime_ECMA = dol_print_date($now, '%Y-%m-%dT%H:%M:%S');
1618 
1619  $date_actu = $now;
1620  if (!empty($executiondate)) $date_actu = $executiondate;
1621 
1622  $dateTime_YMD = dol_print_date($date_actu, '%Y%m%d');
1623  $dateTime_YMDHMS = dol_print_date($date_actu, '%Y%m%d%H%M%S');
1624  $fileCrediteurSection = '';
1625  $fileEmetteurSection = '';
1626  $i = 0;
1627 
1628  /*
1629  * Section Creditor (sepa Crediteurs bloc lines)
1630  */
1631 
1632  $sql = "SELECT soc.rowid as socid, soc.code_client as code, soc.address, soc.zip, soc.town, c.code as country_code,";
1633  $sql .= " pl.client_nom as nom, pl.code_banque as cb, pl.code_guichet as cg, pl.number as cc, pl.amount as somme,";
1634  $sql .= " f.ref as fac, pf.fk_facture_fourn as idfac,";
1635  $sql .= " rib.rowid, rib.datec, rib.iban_prefix as iban, rib.bic as bic, rib.rowid as drum, rib.rum, rib.date_rum";
1636  $sql .= " FROM";
1637  $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
1638  $sql .= " ".MAIN_DB_PREFIX."facture_fourn as f,";
1639  $sql .= " ".MAIN_DB_PREFIX."prelevement_facture as pf,";
1640  $sql .= " ".MAIN_DB_PREFIX."societe as soc,";
1641  $sql .= " ".MAIN_DB_PREFIX."c_country as c,";
1642  $sql .= " ".MAIN_DB_PREFIX."societe_rib as rib";
1643  $sql .= " WHERE pl.fk_prelevement_bons = ".$this->id;
1644  $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
1645  $sql .= " AND pf.fk_facture_fourn = f.rowid";
1646  $sql .= " AND f.fk_soc = soc.rowid";
1647  $sql .= " AND soc.fk_pays = c.rowid";
1648  $sql .= " AND rib.fk_soc = f.fk_soc";
1649  $sql .= " AND rib.default_rib = 1";
1650  $sql .= " AND rib.type = 'ban'";
1651 
1652  // Define $fileCrediteurSection. One section DrctDbtTxInf per invoice.
1653  $resql = $this->db->query($sql);
1654  if ($resql)
1655  {
1656  $cachearraytotestduplicate = array();
1657 
1658  $num = $this->db->num_rows($resql);
1659  while ($i < $num)
1660  {
1661  $obj = $this->db->fetch_object($resql);
1662 
1663  if (!empty($cachearraytotestduplicate[$obj->idfac])) {
1664  $this->error = $langs->trans('ErrorCompanyHasDuplicateDefaultBAN', $obj->socid);
1665  $this->invoice_in_error[$obj->idfac] = $this->error;
1666  $result = -2;
1667  break;
1668  }
1669  $cachearraytotestduplicate[$obj->idfac] = $obj->rowid;
1670 
1671  $daterum = (!empty($obj->date_rum)) ? $this->db->jdate($obj->date_rum) : $this->db->jdate($obj->datec);
1672  $fileCrediteurSection .= $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, $type);
1673  $this->total = $this->total + $obj->somme;
1674  $i++;
1675  }
1676  $nbtotalDrctDbtTxInf = $i;
1677  } else {
1678  $this->error = $this->db->lasterror();
1679  fputs($this->file, 'ERROR CREDITOR '.$sql.$CrLf); // CREDITORS = Suppliers
1680  $result = -2;
1681  }
1682 
1683  // Define $fileEmetteurSection. Start of bloc PmtInf. Will contains all $nbtotalDrctDbtTxInf
1684  if ($result != -2) {
1685  $fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $nbtotalDrctDbtTxInf, $this->total, $CrLf, $format, $type);
1686  }
1687 
1691  // SEPA File Header
1692  fputs($this->file, '<'.'?xml version="1.0" encoding="UTF-8" standalone="yes"?'.'>'.$CrLf);
1693  fputs($this->file, '<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'.$CrLf);
1694  fputs($this->file, ' <CstmrCdtTrfInitn>'.$CrLf);
1695  // SEPA Group header
1696  fputs($this->file, ' <GrpHdr>'.$CrLf);
1697  fputs($this->file, ' <MsgId>'.('TRF/'.$dateTime_YMD.'/REF'.$this->id).'</MsgId>'.$CrLf);
1698  fputs($this->file, ' <CreDtTm>'.$dateTime_ECMA.'</CreDtTm>'.$CrLf);
1699  fputs($this->file, ' <NbOfTxs>'.$i.'</NbOfTxs>'.$CrLf);
1700  fputs($this->file, ' <CtrlSum>'.$this->total.'</CtrlSum>'.$CrLf);
1701  fputs($this->file, ' <InitgPty>'.$CrLf);
1702  fputs($this->file, ' <Nm>'.strtoupper(dol_string_unaccent($this->raison_sociale)).'</Nm>'.$CrLf);
1703  fputs($this->file, ' <Id>'.$CrLf);
1704  fputs($this->file, ' <PrvtId>'.$CrLf);
1705  fputs($this->file, ' <Othr>'.$CrLf);
1706  fputs($this->file, ' <Id>'.$conf->global->PAYMENTBYBANKTRANSFER_ICS.'</Id>'.$CrLf);
1707  fputs($this->file, ' </Othr>'.$CrLf);
1708  fputs($this->file, ' </PrvtId>'.$CrLf);
1709  fputs($this->file, ' </Id>'.$CrLf);
1710  fputs($this->file, ' </InitgPty>'.$CrLf);
1711  fputs($this->file, ' </GrpHdr>'.$CrLf);
1712  // SEPA File Emetteur (mycompany)
1713  if ($result != -2)
1714  { fputs($this-> file, $fileEmetteurSection); }
1715  // SEPA File Creditors
1716  if ($result != -2)
1717  { fputs($this-> file, $fileCrediteurSection); }
1718  // SEPA FILE FOOTER
1719  fputs($this->file, ' </PmtInf>'.$CrLf);
1720  fputs($this->file, ' </CstmrCdtTrfInitn>'.$CrLf);
1721  fputs($this->file, '</Document>'.$CrLf);
1722  }
1723  }
1724 
1725  // Build file for Other Countries with unknow format
1726  if (!$found)
1727  {
1728  if ($type != 'bank-transfer') {
1729  $sql = "SELECT pl.amount";
1730  $sql .= " FROM";
1731  $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
1732  $sql .= " ".MAIN_DB_PREFIX."facture as f,";
1733  $sql .= " ".MAIN_DB_PREFIX."prelevement_facture as pf";
1734  $sql .= " WHERE pl.fk_prelevement_bons = ".$this->id;
1735  $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
1736  $sql .= " AND pf.fk_facture = f.rowid";
1737 
1738  // Lines
1739  $i = 0;
1740  $resql = $this->db->query($sql);
1741  if ($resql)
1742  {
1743  $num = $this->db->num_rows($resql);
1744 
1745  while ($i < $num)
1746  {
1747  $obj = $this->db->fetch_object($resql);
1748  $this->total = $this->total + $obj->amount;
1749 
1750  // TODO Write record into file
1751  $i++;
1752  }
1753  } else {
1754  $result = -2;
1755  }
1756  } else {
1757  $sql = "SELECT pl.amount";
1758  $sql .= " FROM";
1759  $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
1760  $sql .= " ".MAIN_DB_PREFIX."facture_fourn as f,";
1761  $sql .= " ".MAIN_DB_PREFIX."prelevement_facture as pf";
1762  $sql .= " WHERE pl.fk_prelevement_bons = ".$this->id;
1763  $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
1764  $sql .= " AND pf.fk_facture_fourn = f.rowid";
1765 
1766  // Lines
1767  $i = 0;
1768  $resql = $this->db->query($sql);
1769  if ($resql)
1770  {
1771  $num = $this->db->num_rows($resql);
1772 
1773  while ($i < $num)
1774  {
1775  $obj = $this->db->fetch_object($resql);
1776  $this->total = $this->total + $obj->amount;
1777 
1778  // TODO Write record into file
1779  $i++;
1780  }
1781  } else {
1782  $result = -2;
1783  }
1784  }
1785 
1786  $langs->load('withdrawals');
1787 
1788  // TODO Add here code to generate a generic file
1789  fputs($this->file, $langs->transnoentitiesnoconv('WithdrawalFileNotCapable', $mysoc->country_code));
1790  }
1791 
1792  fclose($this->file);
1793  if (!empty($conf->global->MAIN_UMASK)) {
1794  @chmod($this->file, octdec($conf->global->MAIN_UMASK));
1795  }
1796 
1797  return $result;
1798  }
1799 
1800 
1809  public static function buildRumNumber($row_code_client, $row_datec, $row_drum)
1810  {
1811  global $langs;
1812  $pre = substr(dol_string_nospecial(dol_string_unaccent($langs->transnoentitiesnoconv('RUM'))), 0, 3); // Must always be on 3 char ('RUM' or 'UMR'. This is a protection against bad translation)
1813  return $pre.'-'.$row_code_client.'-'.$row_drum.'-'.date('U', $row_datec);
1814  }
1815 
1816 
1817  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1834  public function EnregDestinataire($rowid, $client_nom, $rib_banque, $rib_guichet, $rib_number, $amount, $ref, $facid, $rib_dom = '', $type = 'direct-debit')
1835  {
1836  // phpcs:enable
1837  fputs($this->file, "06");
1838  fputs($this->file, "08"); // Prelevement ordinaire
1839 
1840  fputs($this->file, " "); // Zone Reservee B2
1841 
1842  fputs($this->file, $this->emetteur_ics); // ICS
1843 
1844  // Date d'echeance C1
1845 
1846  fputs($this->file, " ");
1847  fputs($this->file, strftime("%d%m", $this->date_echeance));
1848  fputs($this->file, substr(strftime("%y", $this->date_echeance), 1));
1849 
1850  // Raison Sociale Destinataire C2
1851 
1852  fputs($this->file, substr(strtoupper($client_nom)." ", 0, 24));
1853 
1854  // Domiciliation facultative D1
1855  $domiciliation = strtr($rib_dom, array(" " => "-", CHR(13) => " ", CHR(10) => ""));
1856  fputs($this->file, substr($domiciliation." ", 0, 24));
1857 
1858  // Zone Reservee D2
1859 
1860  fputs($this->file, substr(" ", 0, 8));
1861 
1862  // Code Guichet D3
1863 
1864  fputs($this->file, $rib_guichet);
1865 
1866  // Numero de compte D4
1867 
1868  fputs($this->file, substr("000000000000000".$rib_number, -11));
1869 
1870  // Zone E Montant
1871 
1872  $montant = (round($amount, 2) * 100);
1873 
1874  fputs($this->file, substr("000000000000000".$montant, -16));
1875 
1876  // Libelle F
1877 
1878  fputs($this->file, substr("*_".$ref."_RDVnet".$rowid." ", 0, 31));
1879 
1880  // Code etablissement G1
1881 
1882  fputs($this->file, $rib_banque);
1883 
1884  // Zone Reservee G2
1885 
1886  fputs($this->file, substr(" ", 0, 5));
1887 
1888  fputs($this->file, "\n");
1889  }
1890 
1891  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1916  public function EnregDestinataireSEPA($row_code_client, $row_nom, $row_address, $row_zip, $row_town, $row_country_code, $row_cb, $row_cg, $row_cc, $row_somme, $row_ref, $row_idfac, $row_iban, $row_bic, $row_datec, $row_drum, $row_rum, $type = 'direct-debit')
1917  {
1918  // phpcs:enable
1919  global $conf;
1920 
1921  include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
1922 
1923  $CrLf = "\n";
1924  $Rowing = sprintf("%010d", $row_idfac);
1925 
1926  // Define value for RUM
1927  // Example: RUMCustomerCode-CustomerBankAccountId-01424448606 (note: Date is date of creation of CustomerBankAccountId)
1928  $Rum = empty($row_rum) ? $this->buildRumNumber($row_code_client, $row_datec, $row_drum) : $row_rum;
1929 
1930  // Define date of RUM signature
1931  $DtOfSgntr = dol_print_date($row_datec, '%Y-%m-%d');
1932 
1933  if ($type != 'bank-transfer') {
1934  // SEPA Paiement Information of buyer for Direct debit
1935  $XML_DEBITOR = '';
1936  $XML_DEBITOR .= ' <DrctDbtTxInf>'.$CrLf;
1937  $XML_DEBITOR .= ' <PmtId>'.$CrLf;
1938  // Add EndToEndId. Must be a unique ID for each payment (for example by including bank, buyer or seller, date, checksum)
1939  $XML_DEBITOR .= ' <EndToEndId>'.(($conf->global->PRELEVEMENT_END_TO_END != "") ? $conf->global->PRELEVEMENT_END_TO_END : ('AS-'.dol_trunc($row_ref, 20)).'-'.$Rowing).'</EndToEndId>'.$CrLf; // ISO20022 states that EndToEndId has a MaxLength of 35 characters
1940  $XML_DEBITOR .= ' </PmtId>'.$CrLf;
1941  $XML_DEBITOR .= ' <InstdAmt Ccy="EUR">'.round($row_somme, 2).'</InstdAmt>'.$CrLf;
1942  $XML_DEBITOR .= ' <DrctDbtTx>'.$CrLf;
1943  $XML_DEBITOR .= ' <MndtRltdInf>'.$CrLf;
1944  $XML_DEBITOR .= ' <MndtId>'.$Rum.'</MndtId>'.$CrLf;
1945  $XML_DEBITOR .= ' <DtOfSgntr>'.$DtOfSgntr.'</DtOfSgntr>'.$CrLf;
1946  $XML_DEBITOR .= ' <AmdmntInd>false</AmdmntInd>'.$CrLf;
1947  $XML_DEBITOR .= ' </MndtRltdInf>'.$CrLf;
1948  $XML_DEBITOR .= ' </DrctDbtTx>'.$CrLf;
1949  $XML_DEBITOR .= ' <DbtrAgt>'.$CrLf;
1950  $XML_DEBITOR .= ' <FinInstnId>'.$CrLf;
1951  $XML_DEBITOR .= ' <BIC>'.$row_bic.'</BIC>'.$CrLf;
1952  $XML_DEBITOR .= ' </FinInstnId>'.$CrLf;
1953  $XML_DEBITOR .= ' </DbtrAgt>'.$CrLf;
1954  $XML_DEBITOR .= ' <Dbtr>'.$CrLf;
1955  $XML_DEBITOR .= ' <Nm>'.dolEscapeXML(strtoupper(dol_string_unaccent($row_nom))).'</Nm>'.$CrLf;
1956  $XML_DEBITOR .= ' <PstlAdr>'.$CrLf;
1957  $XML_DEBITOR .= ' <Ctry>'.$row_country_code.'</Ctry>'.$CrLf;
1958  $addressline1 = dol_string_unaccent(strtr($row_address, array(CHR(13) => ", ", CHR(10) => "")));
1959  $addressline2 = dol_string_unaccent(strtr($row_zip.(($row_zip && $row_town) ? ' ' : ''.$row_town), array(CHR(13) => ", ", CHR(10) => "")));
1960  if (trim($addressline1)) $XML_DEBITOR .= ' <AdrLine>'.dolEscapeXML(dol_trunc($addressline1, 70, 'right', 'UTF-8', true)).'</AdrLine>'.$CrLf;
1961  if (trim($addressline2)) $XML_DEBITOR .= ' <AdrLine>'.dolEscapeXML(dol_trunc($addressline2, 70, 'right', 'UTF-8', true)).'</AdrLine>'.$CrLf;
1962  $XML_DEBITOR .= ' </PstlAdr>'.$CrLf;
1963  $XML_DEBITOR .= ' </Dbtr>'.$CrLf;
1964  $XML_DEBITOR .= ' <DbtrAcct>'.$CrLf;
1965  $XML_DEBITOR .= ' <Id>'.$CrLf;
1966  $XML_DEBITOR .= ' <IBAN>'.preg_replace('/\s/', '', $row_iban).'</IBAN>'.$CrLf;
1967  $XML_DEBITOR .= ' </Id>'.$CrLf;
1968  $XML_DEBITOR .= ' </DbtrAcct>'.$CrLf;
1969  $XML_DEBITOR .= ' <RmtInf>'.$CrLf;
1970  // A string with some information on payment - 140 max
1971  $XML_DEBITOR .= ' <Ustrd>'.(($conf->global->PRELEVEMENT_USTRD != "") ? $conf->global->PRELEVEMENT_USTRD : dol_trunc($row_ref, 135)).'</Ustrd>'.$CrLf; // 140 max
1972  $XML_DEBITOR .= ' </RmtInf>'.$CrLf;
1973  $XML_DEBITOR .= ' </DrctDbtTxInf>'.$CrLf;
1974  return $XML_DEBITOR;
1975  } else {
1976  // SEPA Paiement Information of seller for Credit Transfer
1977  $XML_CREDITOR = '';
1978  $XML_CREDITOR .= ' <CdtTrfTxInf>'.$CrLf;
1979  $XML_CREDITOR .= ' <PmtId>'.$CrLf;
1980  // Add EndToEndId. Must be a unique ID for each payment (for example by including bank, buyer or seller, date, checksum)
1981  $XML_CREDITOR .= ' <EndToEndId>'.(($conf->global->PRELEVEMENT_END_TO_END != "") ? $conf->global->PRELEVEMENT_END_TO_END : ('AS-'.dol_trunc($row_ref, 20)).'-'.$Rowing).'</EndToEndId>'.$CrLf; // ISO20022 states that EndToEndId has a MaxLength of 35 characters
1982  $XML_CREDITOR .= ' </PmtId>'.$CrLf;
1983  $XML_CREDITOR .= ' <Amt>'.$CrLf;
1984  $XML_CREDITOR .= ' <InstdAmt Ccy="EUR">'.round($row_somme, 2).'</InstdAmt>'.$CrLf;
1985  $XML_CREDITOR .= ' </Amt>'.$CrLf;
1986  /*
1987  $XML_CREDITOR .= ' <DrctDbtTx>'.$CrLf;
1988  $XML_CREDITOR .= ' <MndtRltdInf>'.$CrLf;
1989  $XML_CREDITOR .= ' <MndtId>'.$Rum.'</MndtId>'.$CrLf;
1990  $XML_CREDITOR .= ' <DtOfSgntr>'.$DtOfSgntr.'</DtOfSgntr>'.$CrLf;
1991  $XML_CREDITOR .= ' <AmdmntInd>false</AmdmntInd>'.$CrLf;
1992  $XML_CREDITOR .= ' </MndtRltdInf>'.$CrLf;
1993  $XML_CREDITOR .= ' </DrctDbtTx>'.$CrLf;
1994  */
1995  //$XML_CREDITOR .= ' <ChrgBr>SLEV</ChrgBr>'.$CrLf;
1996  $XML_CREDITOR .= ' <CdtrAgt>'.$CrLf;
1997  $XML_CREDITOR .= ' <FinInstnId>'.$CrLf;
1998  $XML_CREDITOR .= ' <BIC>'.$row_bic.'</BIC>'.$CrLf;
1999  $XML_CREDITOR .= ' </FinInstnId>'.$CrLf;
2000  $XML_CREDITOR .= ' </CdtrAgt>'.$CrLf;
2001  $XML_CREDITOR .= ' <Cdtr>'.$CrLf;
2002  $XML_CREDITOR .= ' <Nm>'.dolEscapeXML(strtoupper(dol_string_unaccent($row_nom))).'</Nm>'.$CrLf;
2003  $XML_CREDITOR .= ' <PstlAdr>'.$CrLf;
2004  $XML_CREDITOR .= ' <Ctry>'.$row_country_code.'</Ctry>'.$CrLf;
2005  $addressline1 = dol_string_unaccent(strtr($row_address, array(CHR(13) => ", ", CHR(10) => "")));
2006  $addressline2 = dol_string_unaccent(strtr($row_zip.(($row_zip && $row_town) ? ' ' : ''.$row_town), array(CHR(13) => ", ", CHR(10) => "")));
2007  if (trim($addressline1)) $XML_CREDITOR .= ' <AdrLine>'.dolEscapeXML(dol_trunc($addressline1, 70, 'right', 'UTF-8', true)).'</AdrLine>'.$CrLf;
2008  if (trim($addressline2)) $XML_CREDITOR .= ' <AdrLine>'.dolEscapeXML(dol_trunc($addressline2, 70, 'right', 'UTF-8', true)).'</AdrLine>'.$CrLf;
2009  $XML_CREDITOR .= ' </PstlAdr>'.$CrLf;
2010  $XML_CREDITOR .= ' </Cdtr>'.$CrLf;
2011  $XML_CREDITOR .= ' <CdtrAcct>'.$CrLf;
2012  $XML_CREDITOR .= ' <Id>'.$CrLf;
2013  $XML_CREDITOR .= ' <IBAN>'.preg_replace('/\s/', '', $row_iban).'</IBAN>'.$CrLf;
2014  $XML_CREDITOR .= ' </Id>'.$CrLf;
2015  $XML_CREDITOR .= ' </CdtrAcct>'.$CrLf;
2016  $XML_CREDITOR .= ' <RmtInf>'.$CrLf;
2017  // A string with some information on payment - 140 max
2018  $XML_CREDITOR .= ' <Ustrd>'.(($conf->global->PRELEVEMENT_USTRD != "") ? $conf->global->PRELEVEMENT_USTRD : dol_trunc($row_ref, 135)).'</Ustrd>'.$CrLf; // 140 max
2019  $XML_CREDITOR .= ' </RmtInf>'.$CrLf;
2020  $XML_CREDITOR .= ' </CdtTrfTxInf>'.$CrLf;
2021  return $XML_CREDITOR;
2022  }
2023  }
2024 
2025 
2026  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2034  public function EnregEmetteur($type = 'direct-debit')
2035  {
2036  // phpcs:enable
2037  fputs($this->file, "03");
2038  fputs($this->file, "08"); // Prelevement ordinaire
2039 
2040  fputs($this->file, " "); // Zone Reservee B2
2041 
2042  fputs($this->file, $this->emetteur_ics); // ICS
2043 
2044  // Date d'echeance C1
2045 
2046  fputs($this->file, " ");
2047  fputs($this->file, strftime("%d%m", $this->date_echeance));
2048  fputs($this->file, substr(strftime("%y", $this->date_echeance), 1));
2049 
2050  // Raison Sociale C2
2051 
2052  fputs($this->file, substr($this->raison_sociale." ", 0, 24));
2053 
2054  // Reference de la remise creancier D1 sur 7 caracteres
2055 
2056  fputs($this->file, substr($this->reference_remise." ", 0, 7));
2057 
2058  // Zone Reservee D1-2
2059 
2060  fputs($this->file, substr(" ", 0, 17));
2061 
2062  // Zone Reservee D2
2063 
2064  fputs($this->file, substr(" ", 0, 2));
2065  fputs($this->file, "E");
2066  fputs($this->file, substr(" ", 0, 5));
2067 
2068  // Code Guichet D3
2069 
2070  fputs($this->file, $this->emetteur_code_guichet);
2071 
2072  // Numero de compte D4
2073 
2074  fputs($this->file, substr("000000000000000".$this->emetteur_numero_compte, -11));
2075 
2076  // Zone Reservee E
2077 
2078  fputs($this->file, substr(" ", 0, 16));
2079 
2080  // Zone Reservee F
2081 
2082  fputs($this->file, substr(" ", 0, 31));
2083 
2084  // Code etablissement
2085 
2086  fputs($this->file, $this->emetteur_code_banque);
2087 
2088  // Zone Reservee G
2089 
2090  fputs($this->file, substr(" ", 0, 5));
2091 
2092  fputs($this->file, "\n");
2093  }
2094 
2095  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2110  public function EnregEmetteurSEPA($configuration, $ladate, $nombre, $total, $CrLf = '\n', $format = 'FRST', $type = 'direct-debit')
2111  {
2112  // phpcs:enable
2113  // SEPA INITIALISATION
2114  global $conf;
2115 
2116  $dateTime_YMD = dol_print_date($ladate, '%Y%m%d');
2117  $dateTime_ETAD = dol_print_date($ladate, '%Y-%m-%d');
2118  $dateTime_YMDHMS = dol_print_date($ladate, '%Y-%m-%dT%H:%M:%S');
2119 
2120  // Get data of bank account
2121  //$id = $configuration->global->PRELEVEMENT_ID_BANKACCOUNT;
2122  $id = ($type == 'bank-transfer' ? $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT : $conf->global->PRELEVEMENT_ID_BANKACCOUNT);
2123  $account = new Account($this->db);
2124  if ($account->fetch($id) > 0)
2125  {
2126  $this->emetteur_code_banque = $account->code_banque;
2127  $this->emetteur_code_guichet = $account->code_guichet;
2128  $this->emetteur_numero_compte = $account->number;
2129  $this->emetteur_number_key = $account->cle_rib;
2130  $this->emetteur_iban = $account->iban;
2131  $this->emetteur_bic = $account->bic;
2132 
2133  $this->emetteur_ics = $conf->global->PRELEVEMENT_ICS; // Ex: PRELEVEMENT_ICS = "FR78ZZZ123456";
2134 
2135  $this->raison_sociale = $account->proprio;
2136  }
2137 
2138  // Get pending payments
2139  $sql = "SELECT rowid, ref";
2140  $sql .= " FROM";
2141  $sql .= " ".MAIN_DB_PREFIX."prelevement_bons as pb";
2142  $sql .= " WHERE pb.rowid = ".$this->id;
2143 
2144  $resql = $this->db->query($sql);
2145  if ($resql)
2146  {
2147  $obj = $this->db->fetch_object($resql);
2148 
2149  $country = explode(':', $configuration->global->MAIN_INFO_SOCIETE_COUNTRY);
2150  $IdBon = sprintf("%05d", $obj->rowid);
2151  $RefBon = $obj->ref;
2152 
2153  if ($type != 'bank-transfer') {
2154  // SEPA Paiement Information of my company for Direct debit
2155  $XML_SEPA_INFO = '';
2156  $XML_SEPA_INFO .= ' <PmtInf>'.$CrLf;
2157  $XML_SEPA_INFO .= ' <PmtInfId>'.('DD/'.$dateTime_YMD.'/ID'.$IdBon.'-'.$RefBon).'</PmtInfId>'.$CrLf;
2158  $XML_SEPA_INFO .= ' <PmtMtd>DD</PmtMtd>'.$CrLf;
2159  $XML_SEPA_INFO .= ' <NbOfTxs>'.$nombre.'</NbOfTxs>'.$CrLf;
2160  $XML_SEPA_INFO .= ' <CtrlSum>'.$total.'</CtrlSum>'.$CrLf;
2161  $XML_SEPA_INFO .= ' <PmtTpInf>'.$CrLf;
2162  $XML_SEPA_INFO .= ' <SvcLvl>'.$CrLf;
2163  $XML_SEPA_INFO .= ' <Cd>SEPA</Cd>'.$CrLf;
2164  $XML_SEPA_INFO .= ' </SvcLvl>'.$CrLf;
2165  $XML_SEPA_INFO .= ' <LclInstrm>'.$CrLf;
2166  $XML_SEPA_INFO .= ' <Cd>CORE</Cd>'.$CrLf;
2167  $XML_SEPA_INFO .= ' </LclInstrm>'.$CrLf;
2168  $XML_SEPA_INFO .= ' <SeqTp>'.$format.'</SeqTp>'.$CrLf;
2169  $XML_SEPA_INFO .= ' </PmtTpInf>'.$CrLf;
2170  $XML_SEPA_INFO .= ' <ReqdColltnDt>'.$dateTime_ETAD.'</ReqdColltnDt>'.$CrLf;
2171  $XML_SEPA_INFO .= ' <Cdtr>'.$CrLf;
2172  $XML_SEPA_INFO .= ' <Nm>'.strtoupper(dol_string_unaccent($this->raison_sociale)).'</Nm>'.$CrLf;
2173  $XML_SEPA_INFO .= ' <PstlAdr>'.$CrLf;
2174  $XML_SEPA_INFO .= ' <Ctry>'.$country[1].'</Ctry>'.$CrLf;
2175  $addressline1 = dol_string_unaccent(strtr($configuration->global->MAIN_INFO_SOCIETE_ADDRESS, array(CHR(13) => ", ", CHR(10) => "")));
2176  $addressline2 = dol_string_unaccent(strtr($configuration->global->MAIN_INFO_SOCIETE_ZIP.(($configuration->global->MAIN_INFO_SOCIETE_ZIP || ' '.$configuration->global->MAIN_INFO_SOCIETE_TOWN) ? ' ' : '').$configuration->global->MAIN_INFO_SOCIETE_TOWN, array(CHR(13) => ", ", CHR(10) => "")));
2177  if ($addressline1) $XML_SEPA_INFO .= ' <AdrLine>'.$addressline1.'</AdrLine>'.$CrLf;
2178  if ($addressline2) $XML_SEPA_INFO .= ' <AdrLine>'.$addressline2.'</AdrLine>'.$CrLf;
2179  $XML_SEPA_INFO .= ' </PstlAdr>'.$CrLf;
2180  $XML_SEPA_INFO .= ' </Cdtr>'.$CrLf;
2181  $XML_SEPA_INFO .= ' <CdtrAcct>'.$CrLf;
2182  $XML_SEPA_INFO .= ' <Id>'.$CrLf;
2183  $XML_SEPA_INFO .= ' <IBAN>'.preg_replace('/\s/', '', $this->emetteur_iban).'</IBAN>'.$CrLf;
2184  $XML_SEPA_INFO .= ' </Id>'.$CrLf;
2185  $XML_SEPA_INFO .= ' </CdtrAcct>'.$CrLf;
2186  $XML_SEPA_INFO .= ' <CdtrAgt>'.$CrLf;
2187  $XML_SEPA_INFO .= ' <FinInstnId>'.$CrLf;
2188  $XML_SEPA_INFO .= ' <BIC>'.$this->emetteur_bic.'</BIC>'.$CrLf;
2189  $XML_SEPA_INFO .= ' </FinInstnId>'.$CrLf;
2190  $XML_SEPA_INFO .= ' </CdtrAgt>'.$CrLf;
2191  /* $XML_SEPA_INFO .= ' <UltmtCdtr>'.$CrLf;
2192  $XML_SEPA_INFO .= ' <Nm>'.$this->raison_sociale.'</Nm>'.$CrLf;
2193  $XML_SEPA_INFO .= ' <PstlAdr>'.$CrLf;
2194  $XML_SEPA_INFO .= ' <Ctry>'.$country[1].'</Ctry>'.$CrLf;
2195  $XML_SEPA_INFO .= ' <AdrLine>'.$conf->global->MAIN_INFO_SOCIETE_ADDRESS.'</AdrLine>'.$CrLf;
2196  $XML_SEPA_INFO .= ' <AdrLine>'.$conf->global->MAIN_INFO_SOCIETE_ZIP.' '.$conf->global->MAIN_INFO_SOCIETE_TOWN.'</AdrLine>'.$CrLf;
2197  $XML_SEPA_INFO .= ' </PstlAdr>'.$CrLf;
2198  $XML_SEPA_INFO .= ' </UltmtCdtr>'.$CrLf;*/
2199  $XML_SEPA_INFO .= ' <ChrgBr>SLEV</ChrgBr>'.$CrLf; // Field "Responsible of fees". Must be SLEV
2200  $XML_SEPA_INFO .= ' <CdtrSchmeId>'.$CrLf;
2201  $XML_SEPA_INFO .= ' <Id>'.$CrLf;
2202  $XML_SEPA_INFO .= ' <PrvtId>'.$CrLf;
2203  $XML_SEPA_INFO .= ' <Othr>'.$CrLf;
2204  $XML_SEPA_INFO .= ' <Id>'.$this->emetteur_ics.'</Id>'.$CrLf;
2205  $XML_SEPA_INFO .= ' <SchmeNm>'.$CrLf;
2206  $XML_SEPA_INFO .= ' <Prtry>SEPA</Prtry>'.$CrLf;
2207  $XML_SEPA_INFO .= ' </SchmeNm>'.$CrLf;
2208  $XML_SEPA_INFO .= ' </Othr>'.$CrLf;
2209  $XML_SEPA_INFO .= ' </PrvtId>'.$CrLf;
2210  $XML_SEPA_INFO .= ' </Id>'.$CrLf;
2211  $XML_SEPA_INFO .= ' </CdtrSchmeId>'.$CrLf;
2212  } else {
2213  // SEPA Paiement Information of my company for Credit Transfer
2214  $XML_SEPA_INFO = '';
2215  $XML_SEPA_INFO .= ' <PmtInf>'.$CrLf;
2216  $XML_SEPA_INFO .= ' <PmtInfId>'.('TRF/'.$dateTime_YMD.'/ID'.$IdBon.'-'.$RefBon).'</PmtInfId>'.$CrLf;
2217  $XML_SEPA_INFO .= ' <PmtMtd>TRF</PmtMtd>'.$CrLf;
2218  //$XML_SEPA_INFO .= ' <BtchBookg>False</BtchBookg>'.$CrLf;
2219  $XML_SEPA_INFO .= ' <NbOfTxs>'.$nombre.'</NbOfTxs>'.$CrLf;
2220  $XML_SEPA_INFO .= ' <CtrlSum>'.$total.'</CtrlSum>'.$CrLf;
2221  /*
2222  $XML_SEPA_INFO .= ' <PmtTpInf>'.$CrLf;
2223  $XML_SEPA_INFO .= ' <SvcLvl>'.$CrLf;
2224  $XML_SEPA_INFO .= ' <Cd>SEPA</Cd>'.$CrLf;
2225  $XML_SEPA_INFO .= ' </SvcLvl>'.$CrLf;
2226  $XML_SEPA_INFO .= ' <LclInstrm>'.$CrLf;
2227  $XML_SEPA_INFO .= ' <Cd>TRF</Cd>'.$CrLf;
2228  $XML_SEPA_INFO .= ' </LclInstrm>'.$CrLf;
2229  $XML_SEPA_INFO .= ' <CtgyPurp><Cd>SECU</Cd></CtgyPurp>'.$CrLf;
2230  $XML_SEPA_INFO .= ' </PmtTpInf>'.$CrLf;
2231  */
2232  $XML_SEPA_INFO .= ' <ReqdExctnDt>'.dol_print_date($dateTime_ETAD, 'dayrfc').'</ReqdExctnDt>'.$CrLf;
2233  $XML_SEPA_INFO .= ' <Dbtr>'.$CrLf;
2234  $XML_SEPA_INFO .= ' <Nm>'.strtoupper(dol_string_unaccent($this->raison_sociale)).'</Nm>'.$CrLf;
2235  $XML_SEPA_INFO .= ' <PstlAdr>'.$CrLf;
2236  $XML_SEPA_INFO .= ' <Ctry>'.$country[1].'</Ctry>'.$CrLf;
2237  $addressline1 = dol_string_unaccent(strtr($configuration->global->MAIN_INFO_SOCIETE_ADDRESS, array(CHR(13) => ", ", CHR(10) => "")));
2238  $addressline2 = dol_string_unaccent(strtr($configuration->global->MAIN_INFO_SOCIETE_ZIP.(($configuration->global->MAIN_INFO_SOCIETE_ZIP || ' '.$configuration->global->MAIN_INFO_SOCIETE_TOWN) ? ' ' : '').$configuration->global->MAIN_INFO_SOCIETE_TOWN, array(CHR(13) => ", ", CHR(10) => "")));
2239  if ($addressline1) $XML_SEPA_INFO .= ' <AdrLine>'.$addressline1.'</AdrLine>'.$CrLf;
2240  if ($addressline2) $XML_SEPA_INFO .= ' <AdrLine>'.$addressline2.'</AdrLine>'.$CrLf;
2241  $XML_SEPA_INFO .= ' </PstlAdr>'.$CrLf;
2242  $XML_SEPA_INFO .= ' </Dbtr>'.$CrLf;
2243  $XML_SEPA_INFO .= ' <DbtrAcct>'.$CrLf;
2244  $XML_SEPA_INFO .= ' <Id>'.$CrLf;
2245  $XML_SEPA_INFO .= ' <IBAN>'.preg_replace('/\s/', '', $this->emetteur_iban).'</IBAN>'.$CrLf;
2246  $XML_SEPA_INFO .= ' </Id>'.$CrLf;
2247  $XML_SEPA_INFO .= ' </DbtrAcct>'.$CrLf;
2248  $XML_SEPA_INFO .= ' <DbtrAgt>'.$CrLf;
2249  $XML_SEPA_INFO .= ' <FinInstnId>'.$CrLf;
2250  $XML_SEPA_INFO .= ' <BIC>'.$this->emetteur_bic.'</BIC>'.$CrLf;
2251  $XML_SEPA_INFO .= ' </FinInstnId>'.$CrLf;
2252  $XML_SEPA_INFO .= ' </DbtrAgt>'.$CrLf;
2253  /* $XML_SEPA_INFO .= ' <UltmtCdtr>'.$CrLf;
2254  $XML_SEPA_INFO .= ' <Nm>'.$this->raison_sociale.'</Nm>'.$CrLf;
2255  $XML_SEPA_INFO .= ' <PstlAdr>'.$CrLf;
2256  $XML_SEPA_INFO .= ' <Ctry>'.$country[1].'</Ctry>'.$CrLf;
2257  $XML_SEPA_INFO .= ' <AdrLine>'.$conf->global->MAIN_INFO_SOCIETE_ADDRESS.'</AdrLine>'.$CrLf;
2258  $XML_SEPA_INFO .= ' <AdrLine>'.$conf->global->MAIN_INFO_SOCIETE_ZIP.' '.$conf->global->MAIN_INFO_SOCIETE_TOWN.'</AdrLine>'.$CrLf;
2259  $XML_SEPA_INFO .= ' </PstlAdr>'.$CrLf;
2260  $XML_SEPA_INFO .= ' </UltmtCdtr>'.$CrLf;*/
2261  $XML_SEPA_INFO .= ' <ChrgBr>SLEV</ChrgBr>'.$CrLf; // Field "Responsible of fees". Must be SLEV
2262  /*$XML_SEPA_INFO .= ' <CdtrSchmeId>'.$CrLf;
2263  $XML_SEPA_INFO .= ' <Id>'.$CrLf;
2264  $XML_SEPA_INFO .= ' <PrvtId>'.$CrLf;
2265  $XML_SEPA_INFO .= ' <Othr>'.$CrLf;
2266  $XML_SEPA_INFO .= ' <Id>'.$this->emetteur_ics.'</Id>'.$CrLf;
2267  $XML_SEPA_INFO .= ' <SchmeNm>'.$CrLf;
2268  $XML_SEPA_INFO .= ' <Prtry>SEPA</Prtry>'.$CrLf;
2269  $XML_SEPA_INFO .= ' </SchmeNm>'.$CrLf;
2270  $XML_SEPA_INFO .= ' </Othr>'.$CrLf;
2271  $XML_SEPA_INFO .= ' </PrvtId>'.$CrLf;
2272  $XML_SEPA_INFO .= ' </Id>'.$CrLf;
2273  $XML_SEPA_INFO .= ' </CdtrSchmeId>'.$CrLf;*/
2274  }
2275  } else {
2276  fputs($this->file, 'INCORRECT EMETTEUR '.$XML_SEPA_INFO.$CrLf);
2277  }
2278  return $XML_SEPA_INFO;
2279  }
2280 
2281  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2288  public function EnregTotal($total)
2289  {
2290  // phpcs:enable
2291  fputs($this->file, "08");
2292  fputs($this->file, "08"); // Prelevement ordinaire
2293 
2294  fputs($this->file, " "); // Zone Reservee B2
2295 
2296  fputs($this->file, $this->emetteur_ics); // ICS
2297 
2298  // Reserve C1
2299 
2300  fputs($this->file, substr(" ", 0, 12));
2301 
2302 
2303  // Raison Sociale C2
2304 
2305  fputs($this->file, substr(" ", 0, 24));
2306 
2307  // D1
2308 
2309  fputs($this->file, substr(" ", 0, 24));
2310 
2311  // Zone Reservee D2
2312 
2313  fputs($this->file, substr(" ", 0, 8));
2314 
2315  // Code Guichet D3
2316 
2317  fputs($this->file, substr(" ", 0, 5));
2318 
2319  // Numero de compte D4
2320 
2321  fputs($this->file, substr(" ", 0, 11));
2322 
2323  // Zone E Montant
2324 
2325  $montant = ($total * 100);
2326 
2327  fputs($this->file, substr("000000000000000".$montant, -16));
2328 
2329  // Zone Reservee F
2330 
2331  fputs($this->file, substr(" ", 0, 31));
2332 
2333  // Code etablissement
2334 
2335  fputs($this->file, substr(" ", 0, 5));
2336 
2337  // Zone Reservee F
2338 
2339  fputs($this->file, substr(" ", 0, 5));
2340 
2341  fputs($this->file, "\n");
2342  }
2343 
2350  public function getLibStatut($mode = 0)
2351  {
2352  return $this->LibStatut($this->statut, $mode);
2353  }
2354 
2355  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2363  public function LibStatut($status, $mode = 0)
2364  {
2365  // phpcs:enable
2366  if (empty($this->labelStatus) || empty($this->labelStatusShort))
2367  {
2368  global $langs;
2369  //$langs->load("mymodule");
2370  $this->labelStatus[self::STATUS_DRAFT] = $langs->trans('StatusWaiting');
2371  $this->labelStatus[self::STATUS_TRANSFERED] = $langs->trans('StatusTrans');
2372  $this->labelStatus[self::STATUS_CREDITED] = $langs->trans('StatusCredited');
2373  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->trans('StatusWaiting');
2374  $this->labelStatusShort[self::STATUS_TRANSFERED] = $langs->trans('StatusTrans');
2375  $this->labelStatusShort[self::STATUS_CREDITED] = $langs->trans('StatusCredited');
2376  }
2377 
2378  $statusType = 'status1';
2379  if ($status == self::STATUS_TRANSFERED) $statusType = 'status3';
2380  if ($status == self::STATUS_CREDITED) $statusType = 'status6';
2381 
2382  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
2383  }
2384 }
addline(&$line_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key)
Add line to withdrawal.
EnregTotal($total)
Write end.
AddFacture($invoice_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key, $type= 'debit-order')
Add invoice to withdrawal.
dol_substr($string, $start, $length, $stringencoding= '', $trunconbytes=0)
Make a substring.
__construct($db)
Constructor.
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
Definition: list.php:632
nbOfInvoiceToPay($mode= 'direct-debit')
Get number of invoices waiting for payment.
EnregDestinataire($rowid, $client_nom, $rib_banque, $rib_guichet, $rib_number, $amount, $ref, $facid, $rib_dom= '', $type= 'direct-debit')
Write recipient of request (customer)
SommeAPrelever($mode= 'direct-debit')
Returns amount waiting for direct debit payment or credit transfer payment.
dol_now($mode= 'auto')
Return date for now.
set_credite()
Set credite and set status of linked invoices.
static buildRumNumber($row_code_client, $row_datec, $row_drum)
Generate dynamically a RUM number for a customer bank account.
Class to manage bank accounts description of third parties.
set_infotrans($user, $date, $method)
Set withdrawal to transmited status.
AddNotification($db, $user, $action)
Add a notification.
Class to manage suppliers invoices.
set_infocredit($user, $date)
Set direct debit or credit transfer order to &quot;paid&quot; status.
getListInvoices($amounts=0)
Get invoice list.
NbFactureAPrelever($type= 'direct-debit')
Get number of invoices to pay.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage bank accounts.
price($amount, $form=0, $outlangs= '', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code= '')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
dol_string_nospecial($str, $newstr= '_', $badcharstoreplace= '')
Clean a string from all punctuation characters to use it as a ref or login.
getNomUrl($withpicto=0, $option= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Returns clickable name (with picto)
Class to manage third parties objects (customers, suppliers, prospects...)
EnregDestinataireSEPA($row_code_client, $row_nom, $row_address, $row_zip, $row_town, $row_country_code, $row_cb, $row_cg, $row_cc, $row_somme, $row_ref, $row_idfac, $row_iban, $row_bic, $row_datec, $row_drum, $row_rum, $type= 'direct-debit')
Write recipient of request (customer)
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
create($banque=0, $agence=0, $mode= 'real', $format= 'ALL', $executiondate= '', $notrigger=0, $type= 'direct-debit')
Create a direct debit order or a credit transfer order TODO delete params banque and agence when not ...
Class to manage withdrawal receipts.
getErrorString($error)
Return error string.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage payments of customer invoices.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
generate($format= 'ALL', $executiondate= '', $type= 'direct-debit')
Generate a direct debit or credit transfer file.
print $_SERVER["PHP_SELF"]
Edit parameters.
print
Draft customers invoices.
Definition: index.php:89
getLibStatut($mode=0)
Return status label of object.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
call_trigger($triggerName, $user)
Call trigger based on this instance.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
EnregEmetteur($type= 'direct-debit')
Write sender of request (me).
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
Class to manage invoices.
DeleteNotification($user, $action)
Delete a notification.
EnregEmetteurSEPA($configuration, $ladate, $nombre, $total, $CrLf= '\n', $format= 'FRST', $type= 'direct-debit')
Write sender of request (me).
fetch($rowid, $ref= '')
Get object and lines from database.
Class to manage payments for supplier invoices.
DeleteNotificationById($rowid)
Delete a notification def by id.
LibStatut($status, $mode=0)
Return status label for a status.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
if(!empty($search_group)) natural_search(array("g.nom"g note
Definition: list.php:122
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.