30 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
31 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
34 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
35 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
36 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/paiementfourn.class.php';
48 public $element =
'widthdraw';
53 public $table_element =
'prelevement_bons';
58 public $picto =
'payment';
60 public $date_echeance;
61 public $raison_sociale;
62 public $reference_remise;
63 public $emetteur_code_guichet;
64 public $emetteur_numero_compte;
65 public $emetteur_code_banque;
66 public $emetteur_number_key;
67 public $sepa_xml_pti_in_ctti;
69 public $emetteur_iban;
80 public $labelStatus = array();
82 public $factures = array();
84 public $invoice_in_error = array();
85 public $thirdparty_in_error = array();
96 const STATUS_DRAFT = 0;
97 const STATUS_TRANSFERED = 1;
98 const STATUS_CREDITED = 2;
99 const STATUS_DEBITED = 2;
109 global $conf, $langs;
113 $this->filename =
'';
115 $this->date_echeance =
dol_now();
116 $this->raison_sociale =
"";
117 $this->reference_remise =
"";
119 $this->emetteur_code_guichet =
"";
120 $this->emetteur_numero_compte =
"";
121 $this->emetteur_code_banque =
"";
122 $this->emetteur_number_key =
"";
123 $this->sepa_xml_pti_in_ctti =
false;
125 $this->emetteur_iban =
"";
126 $this->emetteur_bic =
"";
127 $this->emetteur_ics =
"";
129 $this->factures = array();
131 $this->methodes_trans = array();
133 $this->methodes_trans[0] =
"Internet";
153 public function AddFacture($invoice_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key, $type =
'debit-order')
160 $result = $this->
addline($line_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key);
164 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"prelevement (";
165 if ($type !=
'bank-transfer') {
166 $sql .=
"fk_facture";
168 $sql .=
"fk_facture_fourn";
170 $sql .=
",fk_prelevement_lignes";
171 $sql .=
") VALUES (";
172 $sql .= ((int) $invoice_id);
173 $sql .=
", ".((int) $line_id);
176 if ($this->
db->query($sql)) {
180 $this->errors[] = get_class($this).
"::AddFacture ".$this->
db->lasterror;
181 dol_syslog(get_class($this).
"::AddFacture Error $result");
185 $this->errors[] = get_class($this).
"::AddFacture linedid Empty";
186 dol_syslog(get_class($this).
"::AddFacture Error $result");
190 dol_syslog(get_class($this).
"::AddFacture Error $result");
209 public function addline(&$line_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key)
218 $sql =
"SELECT rowid";
219 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_lignes";
220 $sql .=
" WHERE fk_prelevement_bons = ".((int) $this->
id);
221 $sql .=
" AND fk_soc =".((int) $client_id);
222 $sql .=
" AND code_banque = '".$this->db->escape($code_banque).
"'";
223 $sql .=
" AND code_guichet = '".$this->db->escape($code_guichet).
"'";
224 $sql .=
" AND number = '".$this->db->escape($number).
"'";
236 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"prelevement_lignes (";
237 $sql .=
"fk_prelevement_bons";
239 $sql .=
", client_nom";
241 $sql .=
", code_banque";
242 $sql .=
", code_guichet";
245 $sql .=
") VALUES (";
247 $sql .=
", ".((int) $client_id);
248 $sql .=
", '".$this->db->escape($client_nom).
"'";
249 $sql .=
", ".((float)
price2num($amount));
250 $sql .=
", '".$this->db->escape($code_banque).
"'";
251 $sql .=
", '".$this->db->escape($code_guichet).
"'";
252 $sql .=
", '".$this->db->escape($number).
"'";
253 $sql .=
", '".$this->db->escape($number_key).
"'";
256 if ($this->
db->query($sql)) {
257 $line_id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"prelevement_lignes");
260 $this->errors[] = get_class($this).
"::addline Error -2 ".$this->
db->lasterror;
261 dol_syslog(get_class($this).
"::addline Error -2");
281 $errors[1027] = $langs->trans(
"DateInvalid");
283 return $errors[abs($error)];
293 public function fetch($rowid, $ref =
'')
297 $sql =
"SELECT p.rowid, p.ref, p.amount, p.note";
298 $sql .=
", p.datec as dc";
299 $sql .=
", p.date_trans as date_trans";
300 $sql .=
", p.method_trans, p.fk_user_trans";
301 $sql .=
", p.date_credit as date_credit";
302 $sql .=
", p.fk_user_credit";
304 $sql .=
", p.statut as status";
305 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_bons as p";
306 $sql .=
" WHERE p.entity IN (".getEntity(
'invoice').
")";
308 $sql .=
" AND p.rowid = ".((int) $rowid);
310 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
313 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
314 $result = $this->
db->query($sql);
316 if ($this->
db->num_rows($result)) {
317 $obj = $this->
db->fetch_object($result);
319 $this->
id = $obj->rowid;
320 $this->
ref = $obj->ref;
321 $this->amount = $obj->amount;
322 $this->note = $obj->note;
323 $this->datec = $this->
db->jdate($obj->dc);
325 $this->date_trans = $this->
db->jdate($obj->date_trans);
326 $this->method_trans = $obj->method_trans;
327 $this->user_trans = $obj->fk_user_trans;
329 $this->date_credit = $this->
db->jdate($obj->date_credit);
330 $this->user_credit = $obj->fk_user_credit;
332 $this->
type = $obj->type;
334 $this->status = $obj->status;
335 $this->statut = $obj->status;
341 dol_syslog(get_class($this).
"::Fetch Erreur aucune ligne retournee");
360 global $conf, $langs;
364 if ($this->fetched == 1) {
365 if ($date < $this->date_trans) {
366 $langs->load(
"errors");
367 $this->error = $langs->trans(
'ErrorDateOfMovementLowerThanDateOfFileTransmission');
368 dol_syslog(
"bon-prelevment::set_infocredit 1027 ".$this->error);
374 $sql =
" UPDATE ".MAIN_DB_PREFIX.
"prelevement_bons ";
375 $sql .=
" SET fk_user_credit = ".$user->id;
376 $sql .=
", statut = ".self::STATUS_CREDITED;
377 $sql .=
", date_credit = '".$this->db->idate($date).
"'";
378 $sql .=
" WHERE rowid=".((int) $this->
id);
379 $sql .=
" AND entity = ".((int) $conf->entity);
380 $sql .=
" AND statut = ".self::STATUS_TRANSFERED;
384 $langs->load(
'withdrawals');
385 $subject = $langs->trans(
"InfoCreditSubject", $this->
ref);
386 $message = $langs->trans(
"InfoCreditMessage", $this->
ref,
dol_print_date($date,
'dayhour'));
389 $bankaccount = ($this->
type ==
'bank-transfer' ? $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT : $conf->global->PRELEVEMENT_ID_BANKACCOUNT);
392 $amountsperthirdparty = array();
398 for ($i = 0; $i < $num; $i++) {
399 if ($this->
type ==
'bank-transfer') {
405 $result = $fac->fetch($facs[$i][0]);
407 $amounts[$fac->id] = $facs[$i][1];
408 $amountsperthirdparty[$fac->socid][$fac->id] = $facs[$i][1];
410 $totalpaid = $fac->getSommePaiement();
411 $totalcreditnotes = $fac->getSumCreditNotesUsed();
412 $totaldeposits = $fac->getSumDepositsUsed();
413 $alreadypayed = $totalpaid + $totalcreditnotes + $totaldeposits;
416 if (
price2num($alreadypayed + $facs[$i][1],
'MT') == $fac->total_ttc) {
417 $result = $fac->setPaid($user);
419 $this->error = $fac->error;
420 $this->errors = $fac->errors;
427 foreach ($amountsperthirdparty as $thirdpartyid => $cursoramounts) {
428 if ($this->
type ==
'bank-transfer') {
433 $paiement->datepaye = $date;
434 $paiement->amounts = $cursoramounts;
436 if ($this->
type ==
'bank-transfer') {
437 $paiement->paiementid = 2;
438 $paiement->paiementcode =
'VIR';
440 $paiement->paiementid = 3;
441 $paiement->paiementcode =
'PRE';
444 $paiement->num_payment = $this->ref;
445 $paiement->id_prelevement = $this->id;
447 $paiement_id = $paiement->create($user);
448 if ($paiement_id < 0) {
450 $this->error = $paiement->error;
451 $this->errors = $paiement->errors;
452 dol_syslog(get_class($this).
"::set_infocredit AddPayment Error ".$this->error);
454 if ($this->
type ==
'bank-transfer') {
455 $modeforaddpayment =
'payment_supplier';
456 $labelforaddpayment =
'(BankTransferPayment)';
458 $modeforaddpayment =
'payment';
459 $labelforaddpayment =
'(WithdrawalPayment)';
462 $result = $paiement->addPaymentToBank($user, $modeforaddpayment, $labelforaddpayment, $bankaccount,
'',
'');
465 $this->error = $paiement->error;
466 $this->errors = $paiement->errors;
467 dol_syslog(get_class($this).
"::set_infocredit AddPaymentToBank Error ".$this->error);
475 $sql =
" UPDATE ".MAIN_DB_PREFIX.
"prelevement_lignes";
476 $sql .=
" SET statut = 2";
477 $sql .=
" WHERE fk_prelevement_bons = ".((int) $this->
id);
479 if (!$this->
db->query($sql)) {
480 dol_syslog(get_class($this).
"::set_infocredit Update lines Error");
485 $this->error = $this->
db->lasterror();
486 dol_syslog(get_class($this).
"::set_infocredit Update Bons Error");
494 $this->date_credit = $date;
495 $this->statut = self::STATUS_CREDITED;
500 $this->
db->rollback();
520 global $conf, $langs;
524 dol_syslog(get_class($this).
"::set_infotrans Start", LOG_INFO);
525 if ($this->
db->begin()) {
526 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"prelevement_bons ";
527 $sql .=
" SET fk_user_trans = ".$user->id;
528 $sql .=
" , date_trans = '".$this->db->idate($date).
"'";
529 $sql .=
" , method_trans = ".((int) $method);
530 $sql .=
" , statut = ".self::STATUS_TRANSFERED;
531 $sql .=
" WHERE rowid = ".((int) $this->
id);
532 $sql .=
" AND entity = ".((int) $conf->entity);
533 $sql .=
" AND statut = 0";
535 if ($this->
db->query($sql)) {
536 $this->method_trans = $method;
537 $langs->load(
'withdrawals');
538 $subject = $langs->trans(
"InfoTransSubject", $this->
ref);
539 $message = $langs->trans(
"InfoTransMessage", $this->
ref,
dolGetFirstLastname($user->firstname, $user->lastname));
540 $message .= $langs->trans(
"InfoTransData",
price($this->amount), $this->methodes_trans[$this->method_trans],
dol_print_date($date,
'day'));
548 $this->date_trans = $date;
550 $this->user_trans = $user->id;
555 $this->
db->rollback();
556 dol_syslog(get_class($this).
"::set_infotrans ROLLBACK", LOG_ERR);
561 dol_syslog(get_class($this).
"::set_infotrans Ouverture transaction SQL impossible", LOG_CRIT);
582 if ($this->
type ==
'bank-transfer') {
583 $sql .=
" pf.fk_facture_fourn";
585 $sql .=
" pf.fk_facture";
588 $sql .=
", SUM(pl.amount)";
590 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_bons as p";
591 $sql .=
" , ".MAIN_DB_PREFIX.
"prelevement_lignes as pl";
592 $sql .=
" , ".MAIN_DB_PREFIX.
"prelevement as pf";
593 $sql .=
" WHERE pf.fk_prelevement_lignes = pl.rowid";
594 $sql .=
" AND pl.fk_prelevement_bons = p.rowid";
595 $sql .=
" AND p.rowid = ".((int) $this->
id);
596 $sql .=
" AND p.entity = ".((int) $conf->entity);
598 if ($this->
type ==
'bank-transfer') {
599 $sql .=
" GROUP BY fk_facture_fourn";
601 $sql .=
" GROUP BY fk_facture";
612 $row = $this->
db->fetch_row(
$resql);
626 dol_syslog(get_class($this).
"::getListInvoices Erreur");
644 $sql =
"SELECT sum(pfd.amount) as nb";
645 if ($mode !=
'bank-transfer') {
646 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f,";
648 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f,";
650 $sql .=
" ".MAIN_DB_PREFIX.
"prelevement_demande as pfd";
651 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
652 if (empty($conf->global->WITHDRAWAL_ALLOW_ANY_INVOICE_STATUS)) {
653 $sql .=
" AND f.fk_statut = ".Facture::STATUS_VALIDATED;
655 if ($mode !=
'bank-transfer') {
656 $sql .=
" AND f.rowid = pfd.fk_facture";
658 $sql .=
" AND f.rowid = pfd.fk_facture_fourn";
660 $sql .=
" AND f.paye = 0";
661 $sql .=
" AND pfd.traite = 0";
662 $sql .=
" AND pfd.ext_payment_id IS NULL";
663 $sql .=
" AND f.total_ttc > 0";
667 $obj = $this->
db->fetch_object(
$resql);
674 dol_syslog(get_class($this).
"::SommeAPrelever Erreur -1");
704 $sql =
"SELECT count(f.rowid) as nb";
705 if ($type ==
'bank-transfer') {
706 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
708 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
710 $sql .=
", ".MAIN_DB_PREFIX.
"prelevement_demande as pfd";
711 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
712 if (empty($conf->global->WITHDRAWAL_ALLOW_ANY_INVOICE_STATUS)) {
713 $sql .=
" AND f.fk_statut = ".Facture::STATUS_VALIDATED;
715 if ($type ==
'bank-transfer') {
716 $sql .=
" AND f.rowid = pfd.fk_facture_fourn";
718 $sql .=
" AND f.rowid = pfd.fk_facture";
720 $sql .=
" AND pfd.traite = 0";
721 $sql .=
" AND pfd.ext_payment_id IS NULL";
722 $sql .=
" AND f.total_ttc > 0";
724 dol_syslog(get_class($this).
"::NbFactureAPrelever");
728 $obj = $this->
db->fetch_object(
$resql);
734 $this->error = get_class($this).
"::NbFactureAPrelever Erreur -1 sql=".$this->
db->error();
754 public function create($banque = 0, $agence = 0, $mode =
'real', $format =
'ALL', $executiondate =
'', $notrigger = 0, $type =
'direct-debit')
757 global $conf, $langs, $user;
759 dol_syslog(__METHOD__.
"::Bank=".$banque.
" Office=".$agence.
" mode=".$mode.
" format=".$format, LOG_DEBUG);
761 require_once DOL_DOCUMENT_ROOT.
"/compta/facture/class/facture.class.php";
762 require_once DOL_DOCUMENT_ROOT.
"/societe/class/societe.class.php";
764 if ($type !=
'bank-transfer') {
765 if (empty($format)) {
766 $this->error =
'ErrorBadParametersForDirectDebitFileCreate';
773 $datetimeprev =
dol_now(
'gmt');
775 if (!empty($executiondate)) {
776 $datetimeprev = $executiondate;
782 $this->invoice_in_error = array();
783 $this->thirdparty_in_error = array();
787 $factures_prev = array();
788 $factures_result = array();
789 $factures_prev_id = array();
790 $factures_errors = array();
793 $sql =
"SELECT f.rowid, pfd.rowid as pfdrowid, f.fk_soc";
794 $sql .=
", pfd.code_banque, pfd.code_guichet, pfd.number, pfd.cle_rib";
795 $sql .=
", pfd.amount";
796 $sql .=
", s.nom as name";
797 if ($type !=
'bank-transfer') {
798 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
800 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
802 $sql .=
", ".MAIN_DB_PREFIX.
"societe as s";
803 $sql .=
", ".MAIN_DB_PREFIX.
"prelevement_demande as pfd";
804 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
')';
805 if ($type !=
'bank-transfer') {
806 $sql .=
" AND f.rowid = pfd.fk_facture";
808 $sql .=
" AND f.rowid = pfd.fk_facture_fourn";
810 $sql .=
" AND s.rowid = f.fk_soc";
811 $sql .=
" AND f.fk_statut = 1";
812 $sql .=
" AND f.paye = 0";
813 $sql .=
" AND pfd.traite = 0";
814 $sql .=
" AND f.total_ttc > 0";
815 $sql .=
" AND pfd.ext_payment_id IS NULL";
817 dol_syslog(__METHOD__.
"::Read invoices,", LOG_DEBUG);
825 $row = $this->
db->fetch_row(
$resql);
826 $factures[$i] = $row;
829 dol_syslog(__METHOD__.
"::Read invoices error Found a null invoice", LOG_ERR);
830 $this->invoice_in_error[$row[0]] =
"Error for invoice id ".$row[0].
", found a null amount";
836 dol_syslog(__METHOD__.
"::Read invoices, ".$i.
" invoices to withdraw", LOG_DEBUG);
839 dol_syslog(__METHOD__.
"::Read invoices error ".$this->db->error(), LOG_ERR);
844 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
849 dol_syslog(__METHOD__.
"::Check BAN", LOG_DEBUG);
851 if (count($factures) > 0) {
852 foreach ($factures as $key => $fac) {
853 if ($type !=
'bank-transfer') {
858 $resfetch = $tmpinvoice->fetch($fac[0]);
859 if ($resfetch >= 0) {
860 if ($soc->fetch($tmpinvoice->socid) >= 0) {
862 $bac->fetch(0, $soc->id);
864 if ($type !=
'bank-transfer') {
865 if ($format ==
'FRST' && $bac->frstrecur !=
'FRST') {
868 if ($format ==
'RCUR' && ($bac->frstrecur !=
'RCUR' && $bac->frstrecur !=
'RECUR')) {
873 if ($bac->verif() >= 1) {
874 $factures_prev[$i] = $fac;
876 $factures_prev_id[$i] = $fac[0];
880 dol_syslog(__METHOD__.
"::Check BAN Error on default bank number IBAN/BIC for thirdparty reported by verif() ".$tmpinvoice->socid.
" ".$soc->name, LOG_WARNING);
881 $this->invoice_in_error[$fac[0]] =
"Error on default bank number IBAN/BIC for invoice ".$tmpinvoice->getNomUrl(0).
" for thirdparty ".$soc->getNomUrl(0);
882 $this->thirdparty_in_error[$soc->id] =
"Error on default bank number IBAN/BIC for invoice ".$tmpinvoice->getNomUrl(0).
" for thirdparty ".$soc->getNomUrl(0);
885 dol_syslog(__METHOD__.
"::Check BAN Failed to read company", LOG_WARNING);
888 dol_syslog(__METHOD__.
"::Check BAN Failed to read invoice", LOG_WARNING);
892 dol_syslog(__METHOD__.
"::Check BAN No invoice to process", LOG_WARNING);
899 $out = count($factures_prev).
" invoices will be included.";
912 if (count($factures_prev) > 0) {
913 if ($mode ==
'real') {
916 print $langs->trans(
"ModeWarning");
934 $ref = substr($year, -2).$month;
936 $sql =
"SELECT substring(ref from char_length(ref) - 1)";
937 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_bons";
938 $sql .=
" WHERE ref LIKE '_".$this->db->escape($ref).
"%'";
939 $sql .=
" AND entity = ".((int) $conf->entity);
940 $sql .=
" ORDER BY ref DESC LIMIT 1";
942 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
946 $row = $this->
db->fetch_row(
$resql);
949 $ref =
"T".$ref.sprintf(
"%02d", (intval($row[0]) + 1));
955 if ($type !=
'bank-transfer') {
956 $dir = $conf->prelevement->dir_output.
'/receipts';
958 $dir = $conf->paymentbybanktransfer->dir_output.
'/receipts';
964 $this->filename = $dir.
'/'.$ref.
'.xml';
967 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"prelevement_bons (";
968 $sql .=
"ref, entity, datec, type";
969 $sql .=
") VALUES (";
970 $sql .=
"'".$this->db->escape($ref).
"'";
971 $sql .=
", ".((int) $conf->entity);
972 $sql .=
", '".$this->db->idate($now).
"'";
973 $sql .=
", '".($type ==
'bank-transfer' ?
'bank-transfer' :
'debit-order').
"'";
978 $prev_id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"prelevement_bons");
979 $this->
id = $prev_id;
983 dol_syslog(__METHOD__.
"::Create withdraw receipt ".$this->db->lasterror(), LOG_ERR);
987 dol_syslog(__METHOD__.
"::Get last withdraw receipt ".$this->db->lasterror(), LOG_ERR);
992 if ($type !=
'bank-transfer') {
1001 if (count($factures_prev) > 0) {
1002 foreach ($factures_prev as $fac) {
1004 $result = $fact->fetch($fac[0]);
1006 $this->error =
'ERRORBONPRELEVEMENT Failed to load invoice with id '.$fac[0];
1023 $ri = $this->
AddFacture($fac[0], $fac[2], $fac[8], $fac[7], $fac[3], $fac[4], $fac[5], $fac[6], $type);
1029 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"prelevement_demande";
1030 $sql .=
" SET traite = 1";
1031 $sql .=
", date_traite = '".$this->db->idate($now).
"'";
1032 $sql .=
", fk_prelevement_bons = ".((int) $this->
id);
1033 $sql .=
" WHERE rowid = ".((int) $fac[1]);
1038 $this->errors[] = $this->
db->lasterror();
1039 dol_syslog(__METHOD__.
"::Update Error=".$this->db->lasterror(), LOG_ERR);
1050 dol_syslog(__METHOD__.
"::Init direct debit or credit transfer file for ".count($factures_prev).
" invoices", LOG_DEBUG);
1052 if (count($factures_prev) > 0) {
1053 $this->date_echeance = $datetimeprev;
1054 $this->reference_remise = $ref;
1056 $id = $conf->global->PRELEVEMENT_ID_BANKACCOUNT;
1057 if ($type ==
'bank-transfer') {
1058 $id = $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT;
1061 if ($account->fetch($id) > 0) {
1062 $this->emetteur_code_banque = $account->code_banque;
1063 $this->emetteur_code_guichet = $account->code_guichet;
1064 $this->emetteur_numero_compte = $account->number;
1065 $this->emetteur_number_key = $account->cle_rib;
1066 $this->sepa_xml_pti_in_ctti = (bool) $account->pti_in_ctti;
1067 $this->emetteur_iban = $account->iban;
1068 $this->emetteur_bic = $account->bic;
1070 $this->emetteur_ics = ($type ==
'bank-transfer' ? $account->ics_transfer : $account->ics);
1072 $this->raison_sociale = $account->proprio;
1075 $this->factures = $factures_prev_id;
1076 $this->context[
'factures_prev'] = $factures_prev;
1080 $result = $this->
generate($format, $executiondate, $type);
1087 dol_syslog(__METHOD__.
"::End withdraw receipt, file ".$this->filename, LOG_DEBUG);
1095 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"prelevement_bons";
1096 $sql .=
" SET amount = ".price2num($this->total);
1097 $sql .=
" WHERE rowid = ".((int) $this->
id);
1098 $sql .=
" AND entity = ".((int) $conf->entity);
1103 dol_syslog(__METHOD__.
"::Error update total: ".$this->db->error(), LOG_ERR);
1107 if (!$error && !$notrigger) {
1108 $triggername =
'DIRECT_DEBIT_ORDER_CREATE';
1109 if ($type !=
'bank-transfer') {
1110 $triggername =
'CREDIT_TRANSFER_ORDER_CREATE';
1122 $this->
db->commit();
1123 return count($factures_prev);
1125 $this->
db->rollback();
1141 public function delete($user =
null, $notrigger = 0)
1146 $resql1 = $resql2 = $resql3 = $resql4 = 0;
1149 $triggername =
'DIRECT_DEBIT_ORDER_DELETE';
1150 if ($this->
type ==
'bank-transfer') {
1151 $triggername =
'PAYMENTBYBANKTRANFER_DELETE';
1162 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"prelevement WHERE fk_prelevement_lignes IN (SELECT rowid FROM ".MAIN_DB_PREFIX.
"prelevement_lignes WHERE fk_prelevement_bons = ".((int) $this->
id).
")";
1163 $resql1 = $this->
db->query($sql);
1170 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"prelevement_lignes WHERE fk_prelevement_bons = ".((int) $this->
id);
1171 $resql2 = $this->
db->query($sql);
1178 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"prelevement_bons WHERE rowid = ".((int) $this->
id);
1179 $resql3 = $this->
db->query($sql);
1186 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"prelevement_demande SET fk_prelevement_bons = NULL, traite = 0 WHERE fk_prelevement_bons = ".((int) $this->
id);
1187 $resql4 = $this->
db->query($sql);
1193 if ($resql1 && $resql2 && $resql3 && $resql4 && !$error) {
1194 $this->
db->commit();
1197 $this->
db->rollback();
1213 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1215 global $conf, $langs, $hookmanager;
1217 if (!empty($conf->dol_no_mouse_hover)) {
1223 $labeltoshow =
'PaymentByDirectDebit';
1224 if (!empty($this->
type) && $this->
type ==
'bank-transfer') {
1225 $labeltoshow =
'PaymentByBankTransfer';
1228 $label =
'<u>'.$langs->trans($labeltoshow).
'</u>';
1230 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1231 if (isset($this->statut)) {
1232 $label .=
'<br><b>'.$langs->trans(
"Status").
":</b> ".$this->
getLibStatut(5);
1235 $url = DOL_URL_ROOT.
'/compta/prelevement/card.php?id='.$this->id;
1236 if (!empty($this->
type) && $this->
type ==
'bank-transfer') {
1237 $url = DOL_URL_ROOT.
'/compta/prelevement/card.php?id='.$this->id;
1240 if ($option !=
'nolink') {
1242 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1243 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1244 $add_save_lastsearch_values = 1;
1246 if ($add_save_lastsearch_values) {
1247 $url .=
'&save_lastsearch_values=1';
1252 if (empty($notooltip)) {
1253 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1254 $label = $langs->trans(
"ShowMyObject");
1255 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1257 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1258 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1260 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1263 $linkstart =
'<a href="'.$url.
'"';
1264 $linkstart .= $linkclose.
'>';
1267 $result .= $linkstart;
1269 $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);
1271 if ($withpicto != 2) {
1272 $result .= $this->ref;
1274 $result .= $linkend;
1276 global $action, $hookmanager;
1277 $hookmanager->initHooks(array(
'banktransferdao'));
1278 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1279 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1281 $result = $hookmanager->resPrint;
1283 $result .= $hookmanager->resPrint;
1298 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"notify_def";
1299 $sql .=
" WHERE rowid = ".((int) $rowid);
1301 if ($this->
db->query($sql)) {
1317 if (is_object($user)) {
1318 $userid = $user->id;
1323 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"notify_def";
1324 $sql .=
" WHERE fk_user=".((int) $userid).
" AND fk_action='".$this->
db->escape($action).
"'";
1326 if ($this->
db->query($sql)) {
1347 if (is_object($user)) {
1348 $userid = $user->id;
1356 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"notify_def (datec,fk_user, fk_soc, fk_contact, fk_action)";
1357 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".((int) $userid).
", 'NULL', 'NULL', '".$this->
db->escape($action).
"')";
1360 if ($this->
db->query($sql)) {
1364 dol_syslog(get_class($this).
"::addNotification Error $result");
1384 public function generate($format =
'ALL', $executiondate =
'', $type =
'direct-debit')
1386 global $conf, $langs, $mysoc;
1392 dol_syslog(get_class($this).
"::generate build file=".$this->filename.
" type=".$type);
1394 $this->file = fopen($this->filename,
"w");
1395 if (empty($this->file)) {
1396 $this->error = $langs->trans(
'ErrorFailedToOpenFile', $this->filename);
1404 if ($mysoc->isInEEC()) {
1407 if ($type !=
'bank-transfer') {
1419 if (!empty($executiondate)) {
1420 $date_actu = $executiondate;
1425 $fileDebiteurSection =
'';
1426 $fileEmetteurSection =
'';
1433 $sql =
"SELECT soc.rowid as socid, soc.code_client as code, soc.address, soc.zip, soc.town, c.code as country_code,";
1434 $sql .=
" pl.client_nom as nom, pl.code_banque as cb, pl.code_guichet as cg, pl.number as cc, pl.amount as somme,";
1435 $sql .=
" f.ref as fac, pf.fk_facture as idfac,";
1436 $sql .=
" rib.rowid, rib.datec, rib.iban_prefix as iban, rib.bic as bic, rib.rowid as drum, rib.rum, rib.date_rum";
1438 $sql .=
" ".MAIN_DB_PREFIX.
"prelevement_lignes as pl,";
1439 $sql .=
" ".MAIN_DB_PREFIX.
"facture as f,";
1440 $sql .=
" ".MAIN_DB_PREFIX.
"prelevement as pf,";
1441 $sql .=
" ".MAIN_DB_PREFIX.
"societe as soc,";
1442 $sql .=
" ".MAIN_DB_PREFIX.
"c_country as c,";
1443 $sql .=
" ".MAIN_DB_PREFIX.
"societe_rib as rib";
1444 $sql .=
" WHERE pl.fk_prelevement_bons = ".((int) $this->
id);
1445 $sql .=
" AND pl.rowid = pf.fk_prelevement_lignes";
1446 $sql .=
" AND pf.fk_facture = f.rowid";
1447 $sql .=
" AND f.fk_soc = soc.rowid";
1448 $sql .=
" AND soc.fk_pays = c.rowid";
1449 $sql .=
" AND rib.fk_soc = f.fk_soc";
1450 $sql .=
" AND rib.default_rib = 1";
1451 $sql .=
" AND rib.type = 'ban'";
1456 $cachearraytotestduplicate = array();
1458 $num = $this->
db->num_rows(
$resql);
1460 $obj = $this->
db->fetch_object(
$resql);
1462 if (!empty($cachearraytotestduplicate[$obj->idfac])) {
1463 $this->error = $langs->trans(
'ErrorCompanyHasDuplicateDefaultBAN', $obj->socid);
1464 $this->invoice_in_error[$obj->idfac] = $this->error;
1468 $cachearraytotestduplicate[$obj->idfac] = $obj->rowid;
1470 $daterum = (!empty($obj->date_rum)) ? $this->
db->jdate($obj->date_rum) : $this->
db->jdate($obj->datec);
1471 $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);
1472 $this->total = $this->total + $obj->somme;
1475 $nbtotalDrctDbtTxInf = $i;
1477 $this->error = $this->
db->lasterror();
1478 fputs($this->file,
'ERROR DEBITOR '.$sql.$CrLf);
1483 if ($result != -2) {
1484 $fileEmetteurSection .= $this->
EnregEmetteurSEPA($conf, $date_actu, $nbtotalDrctDbtTxInf, $this->total, $CrLf, $format, $type);
1491 fputs($this->file,
'<'.
'?xml version="1.0" encoding="UTF-8" standalone="yes"?'.
'>'.$CrLf);
1492 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);
1493 fputs($this->file,
' <CstmrDrctDbtInitn>'.$CrLf);
1495 fputs($this->file,
' <GrpHdr>'.$CrLf);
1496 fputs($this->file,
' <MsgId>'.(
'DD/'.$dateTime_YMD.
'/REF'.$this->id).
'</MsgId>'.$CrLf);
1497 fputs($this->file,
' <CreDtTm>'.$dateTime_ECMA.
'</CreDtTm>'.$CrLf);
1498 fputs($this->file,
' <NbOfTxs>'.$i.
'</NbOfTxs>'.$CrLf);
1499 fputs($this->file,
' <CtrlSum>'.$this->total.
'</CtrlSum>'.$CrLf);
1500 fputs($this->file,
' <InitgPty>'.$CrLf);
1502 fputs($this->file,
' <Id>'.$CrLf);
1503 fputs($this->file,
' <PrvtId>'.$CrLf);
1504 fputs($this->file,
' <Othr>'.$CrLf);
1505 fputs($this->file,
' <Id>'.$this->emetteur_ics.
'</Id>'.$CrLf);
1506 fputs($this->file,
' </Othr>'.$CrLf);
1507 fputs($this->file,
' </PrvtId>'.$CrLf);
1508 fputs($this->file,
' </Id>'.$CrLf);
1509 fputs($this->file,
' </InitgPty>'.$CrLf);
1510 fputs($this->file,
' </GrpHdr>'.$CrLf);
1512 if ($result != -2) {
1513 fputs($this-> file, $fileEmetteurSection);
1516 if ($result != -2) {
1517 fputs($this-> file, $fileDebiteurSection);
1520 fputs($this->file,
' </PmtInf>'.$CrLf);
1521 fputs($this->file,
' </CstmrDrctDbtInitn>'.$CrLf);
1522 fputs($this->file,
'</Document>'.$CrLf);
1535 if (!empty($executiondate)) {
1536 $date_actu = $executiondate;
1541 $fileCrediteurSection =
'';
1542 $fileEmetteurSection =
'';
1549 $sql =
"SELECT soc.rowid as socid, soc.code_client as code, soc.address, soc.zip, soc.town, c.code as country_code,";
1550 $sql .=
" pl.client_nom as nom, pl.code_banque as cb, pl.code_guichet as cg, pl.number as cc, pl.amount as somme,";
1551 $sql .=
" f.ref as fac, pf.fk_facture_fourn as idfac, f.ref_supplier as fac_ref_supplier,";
1552 $sql .=
" rib.rowid, rib.datec, rib.iban_prefix as iban, rib.bic as bic, rib.rowid as drum, rib.rum, rib.date_rum";
1554 $sql .=
" ".MAIN_DB_PREFIX.
"prelevement_lignes as pl,";
1555 $sql .=
" ".MAIN_DB_PREFIX.
"facture_fourn as f,";
1556 $sql .=
" ".MAIN_DB_PREFIX.
"prelevement as pf,";
1557 $sql .=
" ".MAIN_DB_PREFIX.
"societe as soc,";
1558 $sql .=
" ".MAIN_DB_PREFIX.
"c_country as c,";
1559 $sql .=
" ".MAIN_DB_PREFIX.
"societe_rib as rib";
1560 $sql .=
" WHERE pl.fk_prelevement_bons = ".((int) $this->
id);
1561 $sql .=
" AND pl.rowid = pf.fk_prelevement_lignes";
1562 $sql .=
" AND pf.fk_facture_fourn = f.rowid";
1563 $sql .=
" AND f.fk_soc = soc.rowid";
1564 $sql .=
" AND soc.fk_pays = c.rowid";
1565 $sql .=
" AND rib.fk_soc = f.fk_soc";
1566 $sql .=
" AND rib.default_rib = 1";
1567 $sql .=
" AND rib.type = 'ban'";
1572 $cachearraytotestduplicate = array();
1574 $num = $this->
db->num_rows(
$resql);
1576 $obj = $this->
db->fetch_object(
$resql);
1578 if (!empty($cachearraytotestduplicate[$obj->idfac])) {
1579 $this->error = $langs->trans(
'ErrorCompanyHasDuplicateDefaultBAN', $obj->socid);
1580 $this->invoice_in_error[$obj->idfac] = $this->error;
1584 $cachearraytotestduplicate[$obj->idfac] = $obj->rowid;
1586 $daterum = (!empty($obj->date_rum)) ? $this->
db->jdate($obj->date_rum) : $this->
db->jdate($obj->datec);
1587 $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->fac_ref_supplier ?
' '.$obj->fac_ref_supplier :
''), $obj->idfac, $obj->iban, $obj->bic, $daterum, $obj->drum, $obj->rum, $type);
1588 $this->total = $this->total + $obj->somme;
1591 $nbtotalDrctDbtTxInf = $i;
1593 $this->error = $this->
db->lasterror();
1594 fputs($this->file,
'ERROR CREDITOR '.$sql.$CrLf);
1599 if ($result != -2) {
1600 $fileEmetteurSection .= $this->
EnregEmetteurSEPA($conf, $date_actu, $nbtotalDrctDbtTxInf, $this->total, $CrLf, $format, $type);
1607 fputs($this->file,
'<'.
'?xml version="1.0" encoding="UTF-8" standalone="yes"?'.
'>'.$CrLf);
1608 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);
1609 fputs($this->file,
' <CstmrCdtTrfInitn>'.$CrLf);
1611 fputs($this->file,
' <GrpHdr>'.$CrLf);
1612 fputs($this->file,
' <MsgId>'.(
'TRF/'.$dateTime_YMD.
'/REF'.$this->id).
'</MsgId>'.$CrLf);
1613 fputs($this->file,
' <CreDtTm>'.$dateTime_ECMA.
'</CreDtTm>'.$CrLf);
1614 fputs($this->file,
' <NbOfTxs>'.$i.
'</NbOfTxs>'.$CrLf);
1615 fputs($this->file,
' <CtrlSum>'.$this->total.
'</CtrlSum>'.$CrLf);
1616 fputs($this->file,
' <InitgPty>'.$CrLf);
1618 fputs($this->file,
' <Id>'.$CrLf);
1619 fputs($this->file,
' <PrvtId>'.$CrLf);
1620 fputs($this->file,
' <Othr>'.$CrLf);
1621 fputs($this->file,
' <Id>'.$this->emetteur_ics.
'</Id>'.$CrLf);
1622 fputs($this->file,
' </Othr>'.$CrLf);
1623 fputs($this->file,
' </PrvtId>'.$CrLf);
1624 fputs($this->file,
' </Id>'.$CrLf);
1625 fputs($this->file,
' </InitgPty>'.$CrLf);
1626 fputs($this->file,
' </GrpHdr>'.$CrLf);
1628 if ($result != -2) {
1629 fputs($this-> file, $fileEmetteurSection);
1632 if ($result != -2) {
1633 fputs($this-> file, $fileCrediteurSection);
1636 fputs($this->file,
' </PmtInf>'.$CrLf);
1637 fputs($this->file,
' </CstmrCdtTrfInitn>'.$CrLf);
1638 fputs($this->file,
'</Document>'.$CrLf);
1644 if ($type !=
'bank-transfer') {
1645 $sql =
"SELECT pl.amount";
1647 $sql .=
" ".MAIN_DB_PREFIX.
"prelevement_lignes as pl,";
1648 $sql .=
" ".MAIN_DB_PREFIX.
"facture as f,";
1649 $sql .=
" ".MAIN_DB_PREFIX.
"prelevement as pf";
1650 $sql .=
" WHERE pl.fk_prelevement_bons = ".((int) $this->
id);
1651 $sql .=
" AND pl.rowid = pf.fk_prelevement_lignes";
1652 $sql .=
" AND pf.fk_facture = f.rowid";
1658 $num = $this->
db->num_rows(
$resql);
1661 $obj = $this->
db->fetch_object(
$resql);
1662 $this->total = $this->total + $obj->amount;
1671 $sql =
"SELECT pl.amount";
1673 $sql .=
" ".MAIN_DB_PREFIX.
"prelevement_lignes as pl,";
1674 $sql .=
" ".MAIN_DB_PREFIX.
"facture_fourn as f,";
1675 $sql .=
" ".MAIN_DB_PREFIX.
"prelevement as pf";
1676 $sql .=
" WHERE pl.fk_prelevement_bons = ".((int) $this->
id);
1677 $sql .=
" AND pl.rowid = pf.fk_prelevement_lignes";
1678 $sql .=
" AND pf.fk_facture_fourn = f.rowid";
1684 $num = $this->
db->num_rows(
$resql);
1687 $obj = $this->
db->fetch_object(
$resql);
1688 $this->total = $this->total + $obj->amount;
1698 $langs->load(
'withdrawals');
1701 fputs($this->file, $langs->transnoentitiesnoconv(
'WithdrawalFileNotCapable', $mysoc->country_code));
1704 fclose($this->file);
1705 if (!empty($conf->global->MAIN_UMASK)) {
1706 @chmod($this->filename, octdec($conf->global->MAIN_UMASK));
1728 return $pre.
'-'.
dol_print_date($row_datec,
'dayhourlogsmall').
'-'.
dol_trunc($row_drum.($row_code_client ?
'-'.$row_code_client :
''), 13,
'right',
'UTF-8', 1);
1749 public function EnregDestinataire($rowid, $client_nom, $rib_banque, $rib_guichet, $rib_number, $amount, $ref, $facid, $rib_dom =
'', $type =
'direct-debit')
1752 fputs($this->file,
"06");
1753 fputs($this->file,
"08");
1755 fputs($this->file,
" ");
1757 fputs($this->file, $this->emetteur_ics);
1761 fputs($this->file,
" ");
1762 fputs($this->file,
dol_print_date($this->date_echeance,
"%d%m",
'gmt'));
1763 fputs($this->file, substr(
dol_print_date($this->date_echeance,
"%y",
'gmt'), 1));
1767 fputs($this->file, substr(strtoupper($client_nom).
" ", 0, 24));
1770 $domiciliation = strtr($rib_dom, array(
" " =>
"-", CHR(13) =>
" ", CHR(10) =>
""));
1771 fputs($this->file, substr($domiciliation.
" ", 0, 24));
1775 fputs($this->file, substr(
" ", 0, 8));
1779 fputs($this->file, $rib_guichet);
1783 fputs($this->file, substr(
"000000000000000".$rib_number, -11));
1787 $montant = (round($amount, 2) * 100);
1789 fputs($this->file, substr(
"000000000000000".$montant, -16));
1793 fputs($this->file, substr(
"*_".$ref.
"_RDVnet".$rowid.
" ", 0, 31));
1797 fputs($this->file, $rib_banque);
1801 fputs($this->file, substr(
" ", 0, 5));
1803 fputs($this->file,
"\n");
1831 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')
1836 include_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
1839 $Rowing = sprintf(
"%010d", $row_idfac);
1843 $Rum = (empty($row_rum) ? $this->
buildRumNumber($row_code_client, $row_datec, $row_drum) : $row_rum);
1848 if ($type !=
'bank-transfer') {
1851 $XML_DEBITOR .=
' <DrctDbtTxInf>'.$CrLf;
1852 $XML_DEBITOR .=
' <PmtId>'.$CrLf;
1854 $XML_DEBITOR .=
' <EndToEndId>'.(($conf->global->PRELEVEMENT_END_TO_END !=
"") ? $conf->global->PRELEVEMENT_END_TO_END : (
'DD-'.dol_trunc($row_idfac.
'-'.$row_ref, 20,
'right',
'UTF-8', 1)).
'-'.$Rowing).
'</EndToEndId>'.$CrLf;
1855 $XML_DEBITOR .=
' </PmtId>'.$CrLf;
1856 $XML_DEBITOR .=
' <InstdAmt Ccy="EUR">'.round($row_somme, 2).
'</InstdAmt>'.$CrLf;
1857 $XML_DEBITOR .=
' <DrctDbtTx>'.$CrLf;
1858 $XML_DEBITOR .=
' <MndtRltdInf>'.$CrLf;
1859 $XML_DEBITOR .=
' <MndtId>'.$Rum.
'</MndtId>'.$CrLf;
1860 $XML_DEBITOR .=
' <DtOfSgntr>'.$DtOfSgntr.
'</DtOfSgntr>'.$CrLf;
1861 $XML_DEBITOR .=
' <AmdmntInd>false</AmdmntInd>'.$CrLf;
1862 $XML_DEBITOR .=
' </MndtRltdInf>'.$CrLf;
1863 $XML_DEBITOR .=
' </DrctDbtTx>'.$CrLf;
1864 $XML_DEBITOR .=
' <DbtrAgt>'.$CrLf;
1865 $XML_DEBITOR .=
' <FinInstnId>'.$CrLf;
1866 $XML_DEBITOR .=
' <BIC>'.$row_bic.
'</BIC>'.$CrLf;
1867 $XML_DEBITOR .=
' </FinInstnId>'.$CrLf;
1868 $XML_DEBITOR .=
' </DbtrAgt>'.$CrLf;
1869 $XML_DEBITOR .=
' <Dbtr>'.$CrLf;
1871 $XML_DEBITOR .=
' <PstlAdr>'.$CrLf;
1872 $XML_DEBITOR .=
' <Ctry>'.$row_country_code.
'</Ctry>'.$CrLf;
1873 $addressline1 = strtr($row_address, array(CHR(13) =>
", ", CHR(10) =>
""));
1874 $addressline2 = strtr($row_zip.(($row_zip && $row_town) ?
' ' :
''.$row_town), array(CHR(13) =>
", ", CHR(10) =>
""));
1875 if (trim($addressline1)) {
1878 if (trim($addressline2)) {
1881 $XML_DEBITOR .=
' </PstlAdr>'.$CrLf;
1882 $XML_DEBITOR .=
' </Dbtr>'.$CrLf;
1883 $XML_DEBITOR .=
' <DbtrAcct>'.$CrLf;
1884 $XML_DEBITOR .=
' <Id>'.$CrLf;
1885 $XML_DEBITOR .=
' <IBAN>'.preg_replace(
'/\s/',
'', $row_iban).
'</IBAN>'.$CrLf;
1886 $XML_DEBITOR .=
' </Id>'.$CrLf;
1887 $XML_DEBITOR .=
' </DbtrAcct>'.$CrLf;
1888 $XML_DEBITOR .=
' <RmtInf>'.$CrLf;
1890 $XML_DEBITOR .=
' <Ustrd>'.(($conf->global->PRELEVEMENT_USTRD !=
"") ? $conf->global->PRELEVEMENT_USTRD :
dol_trunc($row_ref, 135,
'right',
'UTF-8', 1)).
'</Ustrd>'.$CrLf;
1891 $XML_DEBITOR .=
' </RmtInf>'.$CrLf;
1892 $XML_DEBITOR .=
' </DrctDbtTxInf>'.$CrLf;
1893 return $XML_DEBITOR;
1897 $XML_CREDITOR .=
' <CdtTrfTxInf>'.$CrLf;
1898 $XML_CREDITOR .=
' <PmtId>'.$CrLf;
1900 $XML_CREDITOR .=
' <EndToEndId>'.(($conf->global->PRELEVEMENT_END_TO_END !=
"") ? $conf->global->PRELEVEMENT_END_TO_END : (
'CT-'.dol_trunc($row_idfac.
'-'.$row_ref, 20,
'right',
'UTF-8', 1)).
'-'.$Rowing).
'</EndToEndId>'.$CrLf;
1901 $XML_CREDITOR .=
' </PmtId>'.$CrLf;
1902 if (!empty($this->sepa_xml_pti_in_ctti)) {
1903 $XML_CREDITOR .=
' <PmtTpInf>' . $CrLf;
1906 if (!empty($conf->global->PAYMENTBYBANKTRANSFER_FORCE_HIGH_PRIORITY)) {
1907 $instrprty =
'HIGH';
1909 $instrprty =
'NORM';
1911 $XML_CREDITOR .=
' <InstrPrty>'.$instrprty.
'</InstrPrty>' . $CrLf;
1912 $XML_CREDITOR .=
' <SvcLvl>' . $CrLf;
1913 $XML_CREDITOR .=
' <Cd>SEPA</Cd>' . $CrLf;
1914 $XML_CREDITOR .=
' </SvcLvl>' . $CrLf;
1915 $XML_CREDITOR .=
' <CtgyPurp>' . $CrLf;
1916 $XML_CREDITOR .=
' <Cd>CORE</Cd>' . $CrLf;
1917 $XML_CREDITOR .=
' </CtgyPurp>' . $CrLf;
1918 $XML_CREDITOR .=
' </PmtTpInf>' . $CrLf;
1920 $XML_CREDITOR .=
' <Amt>'.$CrLf;
1921 $XML_CREDITOR .=
' <InstdAmt Ccy="EUR">'.round($row_somme, 2).
'</InstdAmt>'.$CrLf;
1922 $XML_CREDITOR .=
' </Amt>'.$CrLf;
1933 $XML_CREDITOR .=
' <CdtrAgt>'.$CrLf;
1934 $XML_CREDITOR .=
' <FinInstnId>'.$CrLf;
1935 $XML_CREDITOR .=
' <BIC>'.$row_bic.
'</BIC>'.$CrLf;
1936 $XML_CREDITOR .=
' </FinInstnId>'.$CrLf;
1937 $XML_CREDITOR .=
' </CdtrAgt>'.$CrLf;
1938 $XML_CREDITOR .=
' <Cdtr>'.$CrLf;
1940 $XML_CREDITOR .=
' <PstlAdr>'.$CrLf;
1941 $XML_CREDITOR .=
' <Ctry>'.$row_country_code.
'</Ctry>'.$CrLf;
1942 $addressline1 = strtr($row_address, array(CHR(13) =>
", ", CHR(10) =>
""));
1943 $addressline2 = strtr($row_zip.(($row_zip && $row_town) ?
' ' :
''.$row_town), array(CHR(13) =>
", ", CHR(10) =>
""));
1944 if (trim($addressline1)) {
1947 if (trim($addressline2)) {
1950 $XML_CREDITOR .=
' </PstlAdr>'.$CrLf;
1951 $XML_CREDITOR .=
' </Cdtr>'.$CrLf;
1952 $XML_CREDITOR .=
' <CdtrAcct>'.$CrLf;
1953 $XML_CREDITOR .=
' <Id>'.$CrLf;
1954 $XML_CREDITOR .=
' <IBAN>'.preg_replace(
'/\s/',
'', $row_iban).
'</IBAN>'.$CrLf;
1955 $XML_CREDITOR .=
' </Id>'.$CrLf;
1956 $XML_CREDITOR .=
' </CdtrAcct>'.$CrLf;
1957 $XML_CREDITOR .=
' <RmtInf>'.$CrLf;
1959 $XML_CREDITOR .=
' <Ustrd>'.(($conf->global->PRELEVEMENT_USTRD !=
"") ? $conf->global->PRELEVEMENT_USTRD :
dol_trunc($row_ref, 135,
'right',
'UTF-8', 1)).
'</Ustrd>'.$CrLf;
1960 $XML_CREDITOR .=
' </RmtInf>'.$CrLf;
1961 $XML_CREDITOR .=
' </CdtTrfTxInf>'.$CrLf;
1962 return $XML_CREDITOR;
1978 fputs($this->file,
"03");
1979 fputs($this->file,
"08");
1981 fputs($this->file,
" ");
1983 fputs($this->file, $this->emetteur_ics);
1987 fputs($this->file,
" ");
1988 fputs($this->file,
dol_print_date($this->date_echeance,
"%d%m",
'gmt'));
1989 fputs($this->file, substr(
dol_print_date($this->date_echeance,
"%y",
'gmt'), 1));
1993 fputs($this->file, substr($this->raison_sociale.
" ", 0, 24));
1997 fputs($this->file, substr($this->reference_remise.
" ", 0, 7));
2001 fputs($this->file, substr(
" ", 0, 17));
2005 fputs($this->file, substr(
" ", 0, 2));
2006 fputs($this->file,
"E");
2007 fputs($this->file, substr(
" ", 0, 5));
2011 fputs($this->file, $this->emetteur_code_guichet);
2015 fputs($this->file, substr(
"000000000000000".$this->emetteur_numero_compte, -11));
2019 fputs($this->file, substr(
" ", 0, 16));
2023 fputs($this->file, substr(
" ", 0, 31));
2027 fputs($this->file, $this->emetteur_code_banque);
2031 fputs($this->file, substr(
" ", 0, 5));
2033 fputs($this->file,
"\n");
2051 public function EnregEmetteurSEPA($configuration, $ladate, $nombre, $total, $CrLf =
'\n', $format =
'FRST', $type =
'direct-debit')
2063 $id = ($type ==
'bank-transfer' ? $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT : $conf->global->PRELEVEMENT_ID_BANKACCOUNT);
2065 if ($account->fetch($id) > 0) {
2066 $this->emetteur_code_banque = $account->code_banque;
2067 $this->emetteur_code_guichet = $account->code_guichet;
2068 $this->emetteur_numero_compte = $account->number;
2069 $this->emetteur_number_key = $account->cle_rib;
2070 $this->sepa_xml_pti_in_ctti = (bool) $account->pti_in_ctti;
2071 $this->emetteur_iban = $account->iban;
2072 $this->emetteur_bic = $account->bic;
2074 $this->emetteur_ics = ($type ==
'bank-transfer' ? $account->ics_transfer : $account->ics);
2076 $this->raison_sociale = $account->proprio;
2080 $sql =
"SELECT rowid, ref";
2082 $sql .=
" ".MAIN_DB_PREFIX.
"prelevement_bons as pb";
2083 $sql .=
" WHERE pb.rowid = ".((int) $this->
id);
2087 $obj = $this->
db->fetch_object(
$resql);
2089 $country = explode(
':', $configuration->global->MAIN_INFO_SOCIETE_COUNTRY);
2090 $IdBon = sprintf(
"%05d", $obj->rowid);
2091 $RefBon = $obj->ref;
2093 if ($type !=
'bank-transfer') {
2095 $XML_SEPA_INFO =
'';
2096 $XML_SEPA_INFO .=
' <PmtInf>'.$CrLf;
2097 $XML_SEPA_INFO .=
' <PmtInfId>'.(
'DD/'.$dateTime_YMD.
'/ID'.$IdBon.
'-'.$RefBon).
'</PmtInfId>'.$CrLf;
2098 $XML_SEPA_INFO .=
' <PmtMtd>DD</PmtMtd>'.$CrLf;
2099 $XML_SEPA_INFO .=
' <NbOfTxs>'.$nombre.
'</NbOfTxs>'.$CrLf;
2100 $XML_SEPA_INFO .=
' <CtrlSum>'.$total.
'</CtrlSum>'.$CrLf;
2101 $XML_SEPA_INFO .=
' <PmtTpInf>'.$CrLf;
2102 $XML_SEPA_INFO .=
' <SvcLvl>'.$CrLf;
2103 $XML_SEPA_INFO .=
' <Cd>SEPA</Cd>'.$CrLf;
2104 $XML_SEPA_INFO .=
' </SvcLvl>'.$CrLf;
2105 $XML_SEPA_INFO .=
' <LclInstrm>'.$CrLf;
2106 $XML_SEPA_INFO .=
' <Cd>CORE</Cd>'.$CrLf;
2107 $XML_SEPA_INFO .=
' </LclInstrm>'.$CrLf;
2108 $XML_SEPA_INFO .=
' <SeqTp>'.$format.
'</SeqTp>'.$CrLf;
2109 $XML_SEPA_INFO .=
' </PmtTpInf>'.$CrLf;
2110 $XML_SEPA_INFO .=
' <ReqdColltnDt>'.$dateTime_ETAD.
'</ReqdColltnDt>'.$CrLf;
2111 $XML_SEPA_INFO .=
' <Cdtr>'.$CrLf;
2113 $XML_SEPA_INFO .=
' <PstlAdr>'.$CrLf;
2114 $XML_SEPA_INFO .=
' <Ctry>'.$country[1].
'</Ctry>'.$CrLf;
2115 $addressline1 = strtr($configuration->global->MAIN_INFO_SOCIETE_ADDRESS, array(CHR(13) =>
", ", CHR(10) =>
""));
2116 $addressline2 = 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) =>
""));
2117 if ($addressline1) {
2120 if ($addressline2) {
2123 $XML_SEPA_INFO .=
' </PstlAdr>'.$CrLf;
2124 $XML_SEPA_INFO .=
' </Cdtr>'.$CrLf;
2125 $XML_SEPA_INFO .=
' <CdtrAcct>'.$CrLf;
2126 $XML_SEPA_INFO .=
' <Id>'.$CrLf;
2127 $XML_SEPA_INFO .=
' <IBAN>'.preg_replace(
'/\s/',
'', $this->emetteur_iban).
'</IBAN>'.$CrLf;
2128 $XML_SEPA_INFO .=
' </Id>'.$CrLf;
2129 $XML_SEPA_INFO .=
' </CdtrAcct>'.$CrLf;
2130 $XML_SEPA_INFO .=
' <CdtrAgt>'.$CrLf;
2131 $XML_SEPA_INFO .=
' <FinInstnId>'.$CrLf;
2132 $XML_SEPA_INFO .=
' <BIC>'.$this->emetteur_bic.
'</BIC>'.$CrLf;
2133 $XML_SEPA_INFO .=
' </FinInstnId>'.$CrLf;
2134 $XML_SEPA_INFO .=
' </CdtrAgt>'.$CrLf;
2143 $XML_SEPA_INFO .=
' <ChrgBr>SLEV</ChrgBr>'.$CrLf;
2144 $XML_SEPA_INFO .=
' <CdtrSchmeId>'.$CrLf;
2145 $XML_SEPA_INFO .=
' <Id>'.$CrLf;
2146 $XML_SEPA_INFO .=
' <PrvtId>'.$CrLf;
2147 $XML_SEPA_INFO .=
' <Othr>'.$CrLf;
2148 $XML_SEPA_INFO .=
' <Id>'.$this->emetteur_ics.
'</Id>'.$CrLf;
2149 $XML_SEPA_INFO .=
' <SchmeNm>'.$CrLf;
2150 $XML_SEPA_INFO .=
' <Prtry>SEPA</Prtry>'.$CrLf;
2151 $XML_SEPA_INFO .=
' </SchmeNm>'.$CrLf;
2152 $XML_SEPA_INFO .=
' </Othr>'.$CrLf;
2153 $XML_SEPA_INFO .=
' </PrvtId>'.$CrLf;
2154 $XML_SEPA_INFO .=
' </Id>'.$CrLf;
2155 $XML_SEPA_INFO .=
' </CdtrSchmeId>'.$CrLf;
2158 $XML_SEPA_INFO =
'';
2159 $XML_SEPA_INFO .=
' <PmtInf>'.$CrLf;
2160 $XML_SEPA_INFO .=
' <PmtInfId>'.(
'TRF/'.$dateTime_YMD.
'/ID'.$IdBon.
'-'.$RefBon).
'</PmtInfId>'.$CrLf;
2161 $XML_SEPA_INFO .=
' <PmtMtd>TRF</PmtMtd>'.$CrLf;
2163 $XML_SEPA_INFO .=
' <NbOfTxs>'.$nombre.
'</NbOfTxs>'.$CrLf;
2164 $XML_SEPA_INFO .=
' <CtrlSum>'.$total.
'</CtrlSum>'.$CrLf;
2165 if (!empty($this->sepa_xml_pti_in_ctti) && !empty($format)) {
2166 $XML_SEPA_INFO .=
' <PmtTpInf>' . $CrLf;
2167 $XML_SEPA_INFO .=
' <SvcLvl>' . $CrLf;
2168 $XML_SEPA_INFO .=
' <Cd>SEPA</Cd>' . $CrLf;
2169 $XML_SEPA_INFO .=
' </SvcLvl>' . $CrLf;
2170 $XML_SEPA_INFO .=
' <LclInstrm>' . $CrLf;
2171 $XML_SEPA_INFO .=
' <Cd>CORE</Cd>' . $CrLf;
2172 $XML_SEPA_INFO .=
' </LclInstrm>' . $CrLf;
2173 $XML_SEPA_INFO .=
' <SeqTp>' . $format .
'</SeqTp>' . $CrLf;
2174 $XML_SEPA_INFO .=
' </PmtTpInf>' . $CrLf;
2176 $XML_SEPA_INFO .=
' <ReqdExctnDt>'.dol_print_date($dateTime_ETAD,
'dayrfc').
'</ReqdExctnDt>'.$CrLf;
2177 $XML_SEPA_INFO .=
' <Dbtr>'.$CrLf;
2179 $XML_SEPA_INFO .=
' <PstlAdr>'.$CrLf;
2180 $XML_SEPA_INFO .=
' <Ctry>'.$country[1].
'</Ctry>'.$CrLf;
2181 $addressline1 = strtr($configuration->global->MAIN_INFO_SOCIETE_ADDRESS, array(CHR(13) =>
", ", CHR(10) =>
""));
2182 $addressline2 = 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) =>
""));
2183 if ($addressline1) {
2186 if ($addressline2) {
2189 $XML_SEPA_INFO .=
' </PstlAdr>'.$CrLf;
2190 $XML_SEPA_INFO .=
' </Dbtr>'.$CrLf;
2191 $XML_SEPA_INFO .=
' <DbtrAcct>'.$CrLf;
2192 $XML_SEPA_INFO .=
' <Id>'.$CrLf;
2193 $XML_SEPA_INFO .=
' <IBAN>'.preg_replace(
'/\s/',
'', $this->emetteur_iban).
'</IBAN>'.$CrLf;
2194 $XML_SEPA_INFO .=
' </Id>'.$CrLf;
2195 $XML_SEPA_INFO .=
' </DbtrAcct>'.$CrLf;
2196 $XML_SEPA_INFO .=
' <DbtrAgt>'.$CrLf;
2197 $XML_SEPA_INFO .=
' <FinInstnId>'.$CrLf;
2198 $XML_SEPA_INFO .=
' <BIC>'.$this->emetteur_bic.
'</BIC>'.$CrLf;
2199 $XML_SEPA_INFO .=
' </FinInstnId>'.$CrLf;
2200 $XML_SEPA_INFO .=
' </DbtrAgt>'.$CrLf;
2209 $XML_SEPA_INFO .=
' <ChrgBr>SLEV</ChrgBr>'.$CrLf;
2224 fputs($this->file,
'INCORRECT EMETTEUR '.$this->raison_sociale.$CrLf);
2225 $XML_SEPA_INFO =
'';
2227 return $XML_SEPA_INFO;
2240 fputs($this->file,
"08");
2241 fputs($this->file,
"08");
2243 fputs($this->file,
" ");
2245 fputs($this->file, $this->emetteur_ics);
2249 fputs($this->file, substr(
" ", 0, 12));
2254 fputs($this->file, substr(
" ", 0, 24));
2258 fputs($this->file, substr(
" ", 0, 24));
2262 fputs($this->file, substr(
" ", 0, 8));
2266 fputs($this->file, substr(
" ", 0, 5));
2270 fputs($this->file, substr(
" ", 0, 11));
2274 $montant = ($total * 100);
2276 fputs($this->file, substr(
"000000000000000".$montant, -16));
2280 fputs($this->file, substr(
" ", 0, 31));
2284 fputs($this->file, substr(
" ", 0, 5));
2288 fputs($this->file, substr(
" ", 0, 5));
2290 fputs($this->file,
"\n");
2301 return $this->
LibStatut($this->statut, $mode);
2315 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
2318 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'StatusWaiting');
2319 $this->labelStatus[self::STATUS_TRANSFERED] = $langs->transnoentitiesnoconv(
'StatusTrans');
2320 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'StatusWaiting');
2321 $this->labelStatusShort[self::STATUS_TRANSFERED] = $langs->transnoentitiesnoconv(
'StatusTrans');
2322 if ($this->
type ==
'bank-transfer') {
2323 $this->labelStatus[self::STATUS_DEBITED] = $langs->transnoentitiesnoconv(
'StatusDebited');
2324 $this->labelStatusShort[self::STATUS_DEBITED] = $langs->transnoentitiesnoconv(
'StatusDebited');
2326 $this->labelStatus[self::STATUS_CREDITED] = $langs->transnoentitiesnoconv(
'StatusCredited');
2327 $this->labelStatusShort[self::STATUS_CREDITED] = $langs->transnoentitiesnoconv(
'StatusCredited');
2331 $statusType =
'status1';
2332 if ($status == self::STATUS_TRANSFERED) {
2333 $statusType =
'status3';
2335 if ($status == self::STATUS_CREDITED || $status == self::STATUS_DEBITED) {
2336 $statusType =
'status6';
2339 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
2353 global $conf, $langs;