bonprelevement.class.php 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413
  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. * Copyright (C) 2019 JC Prieto <jcprieto@virtual20.com><prietojc@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/compta/prelevement/class/bonprelevement.class.php
  25. * \ingroup prelevement
  26. * \brief File of withdrawal receipts class
  27. */
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
  35. /**
  36. * Class to manage withdrawal receipts
  37. */
  38. class BonPrelevement extends CommonObject
  39. {
  40. /**
  41. * @var string ID to identify managed object
  42. */
  43. public $element = 'widthdraw';
  44. /**
  45. * @var string Name of table without prefix where object is stored
  46. */
  47. public $table_element = 'prelevement_bons';
  48. /**
  49. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  50. */
  51. public $picto = 'payment';
  52. public $date_echeance;
  53. public $raison_sociale;
  54. public $reference_remise;
  55. public $emetteur_code_guichet;
  56. public $emetteur_numero_compte;
  57. public $emetteur_code_banque;
  58. public $emetteur_number_key;
  59. public $sepa_xml_pti_in_ctti;
  60. public $emetteur_iban;
  61. public $emetteur_bic;
  62. public $emetteur_ics;
  63. public $date_trans;
  64. public $user_trans;
  65. public $method_trans;
  66. public $total;
  67. public $fetched;
  68. public $statut; // 0-Wait, 1-Trans, 2-Done
  69. public $labelStatus = array();
  70. public $factures = array();
  71. public $invoice_in_error = array();
  72. public $thirdparty_in_error = array();
  73. public $amount;
  74. public $note;
  75. public $datec;
  76. public $date_credit;
  77. public $user_credit;
  78. public $type;
  79. const STATUS_DRAFT = 0;
  80. const STATUS_TRANSFERED = 1;
  81. const STATUS_CREDITED = 2; // STATUS_CREDITED and STATUS_DEBITED is same. Difference is in ->type
  82. const STATUS_DEBITED = 2; // STATUS_CREDITED and STATUS_DEBITED is same. Difference is in ->type
  83. /**
  84. * Constructor
  85. *
  86. * @param DoliDB $db Database handler
  87. */
  88. public function __construct($db)
  89. {
  90. global $conf, $langs;
  91. $this->db = $db;
  92. $this->filename = '';
  93. $this->date_echeance = dol_now();
  94. $this->raison_sociale = "";
  95. $this->reference_remise = "";
  96. $this->emetteur_code_guichet = "";
  97. $this->emetteur_numero_compte = "";
  98. $this->emetteur_code_banque = "";
  99. $this->emetteur_number_key = "";
  100. $this->sepa_xml_pti_in_ctti = false;
  101. $this->emetteur_iban = "";
  102. $this->emetteur_bic = "";
  103. $this->emetteur_ics = "";
  104. $this->factures = array();
  105. $this->methodes_trans = array();
  106. $this->methodes_trans[0] = "Internet";
  107. $this->fetched = 0;
  108. }
  109. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  110. /**
  111. * Add invoice to withdrawal
  112. *
  113. * @param int $invoice_id id invoice to add
  114. * @param int $client_id id invoice customer
  115. * @param string $client_nom customer name
  116. * @param int $amount amount of invoice
  117. * @param string $code_banque code of bank withdrawal
  118. * @param string $code_guichet code of bank's office
  119. * @param string $number bank account number
  120. * @param string $number_key number key of account number
  121. * @param string $type 'debit-order' or 'bank-transfer'
  122. * @return int >0 if OK, <0 if KO
  123. */
  124. public function AddFacture($invoice_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key, $type = 'debit-order')
  125. {
  126. // phpcs:enable
  127. $result = 0;
  128. $line_id = 0;
  129. // Add lines
  130. $result = $this->addline($line_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key);
  131. if ($result == 0) {
  132. if ($line_id > 0) {
  133. $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement (";
  134. if ($type != 'bank-transfer') {
  135. $sql .= "fk_facture";
  136. } else {
  137. $sql .= "fk_facture_fourn";
  138. }
  139. $sql .= ",fk_prelevement_lignes";
  140. $sql .= ") VALUES (";
  141. $sql .= ((int) $invoice_id);
  142. $sql .= ", ".((int) $line_id);
  143. $sql .= ")";
  144. if ($this->db->query($sql)) {
  145. $result = 0;
  146. } else {
  147. $result = -1;
  148. $this->errors[] = get_class($this)."::AddFacture ".$this->db->lasterror;
  149. dol_syslog(get_class($this)."::AddFacture Error $result");
  150. }
  151. } else {
  152. $result = -2;
  153. $this->errors[] = get_class($this)."::AddFacture linedid Empty";
  154. dol_syslog(get_class($this)."::AddFacture Error $result");
  155. }
  156. } else {
  157. $result = -3;
  158. dol_syslog(get_class($this)."::AddFacture Error $result");
  159. }
  160. return $result;
  161. }
  162. /**
  163. * Add line to withdrawal
  164. *
  165. * @param int $line_id id line to add
  166. * @param int $client_id id invoice customer
  167. * @param string $client_nom customer name
  168. * @param int $amount amount of invoice
  169. * @param string $code_banque code of bank withdrawal
  170. * @param string $code_guichet code of bank's office
  171. * @param string $number bank account number
  172. * @param string $number_key number key of account number
  173. * @return int >0 if OK, <0 if KO
  174. */
  175. public function addline(&$line_id, $client_id, $client_nom, $amount, $code_banque, $code_guichet, $number, $number_key)
  176. {
  177. $result = -1;
  178. $concat = 0;
  179. if ($concat == 1) {
  180. /*
  181. * We aggregate the lines
  182. */
  183. $sql = "SELECT rowid";
  184. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_lignes";
  185. $sql .= " WHERE fk_prelevement_bons = ".((int) $this->id);
  186. $sql .= " AND fk_soc =".((int) $client_id);
  187. $sql .= " AND code_banque = '".$this->db->escape($code_banque)."'";
  188. $sql .= " AND code_guichet = '".$this->db->escape($code_guichet)."'";
  189. $sql .= " AND number = '".$this->db->escape($number)."'";
  190. $resql = $this->db->query($sql);
  191. if ($resql) {
  192. $num = $this->db->num_rows($resql);
  193. } else {
  194. $result = -1;
  195. }
  196. } else {
  197. /*
  198. * No aggregate
  199. */
  200. $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_lignes (";
  201. $sql .= "fk_prelevement_bons";
  202. $sql .= ", fk_soc";
  203. $sql .= ", client_nom";
  204. $sql .= ", amount";
  205. $sql .= ", code_banque";
  206. $sql .= ", code_guichet";
  207. $sql .= ", number";
  208. $sql .= ", cle_rib";
  209. $sql .= ") VALUES (";
  210. $sql .= $this->id;
  211. $sql .= ", ".((int) $client_id);
  212. $sql .= ", '".$this->db->escape($client_nom)."'";
  213. $sql .= ", ".((float) price2num($amount));
  214. $sql .= ", '".$this->db->escape($code_banque)."'";
  215. $sql .= ", '".$this->db->escape($code_guichet)."'";
  216. $sql .= ", '".$this->db->escape($number)."'";
  217. $sql .= ", '".$this->db->escape($number_key)."'";
  218. $sql .= ")";
  219. if ($this->db->query($sql)) {
  220. $line_id = $this->db->last_insert_id(MAIN_DB_PREFIX."prelevement_lignes");
  221. $result = 0;
  222. } else {
  223. $this->errors[] = get_class($this)."::addline Error -2 ".$this->db->lasterror;
  224. dol_syslog(get_class($this)."::addline Error -2");
  225. $result = -2;
  226. }
  227. }
  228. return $result;
  229. }
  230. /**
  231. * Return error string
  232. *
  233. * @param int $error Id of error
  234. * @return string Error string
  235. */
  236. public function getErrorString($error)
  237. {
  238. global $langs;
  239. $errors = array();
  240. $errors[1027] = $langs->trans("DateInvalid");
  241. return $errors[abs($error)];
  242. }
  243. /**
  244. * Get object and lines from database
  245. *
  246. * @param int $rowid Id of object to load
  247. * @param string $ref Ref of direct debit
  248. * @return int >0 if OK, <0 if KO
  249. */
  250. public function fetch($rowid, $ref = '')
  251. {
  252. global $conf;
  253. $sql = "SELECT p.rowid, p.ref, p.amount, p.note";
  254. $sql .= ", p.datec as dc";
  255. $sql .= ", p.date_trans as date_trans";
  256. $sql .= ", p.method_trans, p.fk_user_trans";
  257. $sql .= ", p.date_credit as date_credit";
  258. $sql .= ", p.fk_user_credit";
  259. $sql .= ", p.type";
  260. $sql .= ", p.statut as status";
  261. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p";
  262. $sql .= " WHERE p.entity IN (".getEntity('invoice').")";
  263. if ($rowid > 0) {
  264. $sql .= " AND p.rowid = ".((int) $rowid);
  265. } else {
  266. $sql .= " AND p.ref = '".$this->db->escape($ref)."'";
  267. }
  268. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  269. $result = $this->db->query($sql);
  270. if ($result) {
  271. if ($this->db->num_rows($result)) {
  272. $obj = $this->db->fetch_object($result);
  273. $this->id = $obj->rowid;
  274. $this->ref = $obj->ref;
  275. $this->amount = $obj->amount;
  276. $this->note = $obj->note;
  277. $this->datec = $this->db->jdate($obj->dc);
  278. $this->date_trans = $this->db->jdate($obj->date_trans);
  279. $this->method_trans = $obj->method_trans;
  280. $this->user_trans = $obj->fk_user_trans;
  281. $this->date_credit = $this->db->jdate($obj->date_credit);
  282. $this->user_credit = $obj->fk_user_credit;
  283. $this->type = $obj->type;
  284. $this->status = $obj->status;
  285. $this->statut = $obj->status; // For backward compatibility
  286. $this->fetched = 1;
  287. return 1;
  288. } else {
  289. dol_syslog(get_class($this)."::Fetch Erreur aucune ligne retournee");
  290. return -1;
  291. }
  292. } else {
  293. return -2;
  294. }
  295. }
  296. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  297. /**
  298. * Set direct debit or credit transfer order to "paid" status.
  299. *
  300. * @param User $user Id of user
  301. * @param int $date date of action
  302. * @return int >0 if OK, <0 if KO
  303. */
  304. public function set_infocredit($user, $date)
  305. {
  306. // phpcs:enable
  307. global $conf, $langs;
  308. $error = 0;
  309. if ($this->fetched == 1) {
  310. if ($date < $this->date_trans) {
  311. $langs->load("errors");
  312. $this->error = $langs->trans('ErrorDateOfMovementLowerThanDateOfFileTransmission');
  313. dol_syslog("bon-prelevment::set_infocredit 1027 ".$this->error);
  314. return -1027;
  315. }
  316. $this->db->begin();
  317. $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_bons ";
  318. $sql .= " SET fk_user_credit = ".$user->id;
  319. $sql .= ", statut = ".self::STATUS_CREDITED;
  320. $sql .= ", date_credit = '".$this->db->idate($date)."'";
  321. $sql .= " WHERE rowid=".((int) $this->id);
  322. $sql .= " AND entity = ".((int) $conf->entity);
  323. $sql .= " AND statut = ".self::STATUS_TRANSFERED;
  324. $resql = $this->db->query($sql);
  325. if ($resql) {
  326. $langs->load('withdrawals');
  327. $subject = $langs->trans("InfoCreditSubject", $this->ref);
  328. $message = $langs->trans("InfoCreditMessage", $this->ref, dol_print_date($date, 'dayhour'));
  329. //Add payment of withdrawal into bank
  330. $bankaccount = ($this->type == 'bank-transfer' ? $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT : $conf->global->PRELEVEMENT_ID_BANKACCOUNT);
  331. $facs = array();
  332. $amounts = array();
  333. $amountsperthirdparty = array();
  334. $facs = $this->getListInvoices(1);
  335. // Loop on each invoice. $facs=array(0=>id, 1=>amount requested)
  336. $num = count($facs);
  337. for ($i = 0; $i < $num; $i++) {
  338. if ($this->type == 'bank-transfer') {
  339. $fac = new FactureFournisseur($this->db);
  340. } else {
  341. $fac = new Facture($this->db);
  342. }
  343. $result = $fac->fetch($facs[$i][0]);
  344. $amounts[$fac->id] = $facs[$i][1];
  345. $amountsperthirdparty[$fac->socid][$fac->id] = $facs[$i][1];
  346. $totalpaid = $fac->getSommePaiement();
  347. $totalcreditnotes = $fac->getSumCreditNotesUsed();
  348. $totaldeposits = $fac->getSumDepositsUsed();
  349. $alreadypayed = $totalpaid + $totalcreditnotes + $totaldeposits;
  350. // @TODO Move this after creation of payment
  351. if (price2num($alreadypayed + $facs[$i][1], 'MT') == $fac->total_ttc) {
  352. $result = $fac->setPaid($user);
  353. if ($result < 0) {
  354. $this->error = $fac->error;
  355. $this->errors = $fac->errors;
  356. }
  357. }
  358. }
  359. //var_dump($amountsperthirdparty);exit;
  360. // Make one payment per customer
  361. foreach ($amountsperthirdparty as $thirdpartyid => $cursoramounts) {
  362. if ($this->type == 'bank-transfer') {
  363. $paiement = new PaiementFourn($this->db);
  364. } else {
  365. $paiement = new Paiement($this->db);
  366. }
  367. $paiement->datepaye = $date;
  368. $paiement->amounts = $cursoramounts; // Array with detail of dispatching of payments for each invoice
  369. if ($this->type == 'bank-transfer') {
  370. $paiement->paiementid = 2;
  371. $paiement->paiementcode = 'VIR';
  372. } else {
  373. $paiement->paiementid = 3;
  374. $paiement->paiementcode = 'PRE';
  375. }
  376. $paiement->num_payment = $this->ref; // Set ref of direct debit note
  377. $paiement->id_prelevement = $this->id;
  378. $paiement_id = $paiement->create($user); // This use ->paiementid, that is ID of payment mode
  379. if ($paiement_id < 0) {
  380. $error++;
  381. $this->error = $paiement->error;
  382. $this->errors = $paiement->errors;
  383. dol_syslog(get_class($this)."::set_infocredit AddPayment Error ".$this->error);
  384. } else {
  385. if ($this->type == 'bank-transfer') {
  386. $modeforaddpayment = 'payment_supplier';
  387. } else {
  388. $modeforaddpayment = 'payment';
  389. }
  390. $result = $paiement->addPaymentToBank($user, $modeforaddpayment, '(WithdrawalPayment)', $bankaccount, '', '');
  391. if ($result < 0) {
  392. $error++;
  393. $this->error = $paiement->error;
  394. $this->errors = $paiement->errors;
  395. dol_syslog(get_class($this)."::set_infocredit AddPaymentToBank Error ".$this->error);
  396. }
  397. }
  398. }
  399. // Update withdrawal line
  400. // TODO: Translate to ligneprelevement.class.php
  401. if (!$error) {
  402. $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes";
  403. $sql .= " SET statut = 2";
  404. $sql .= " WHERE fk_prelevement_bons = ".((int) $this->id);
  405. if (!$this->db->query($sql)) {
  406. dol_syslog(get_class($this)."::set_infocredit Update lines Error");
  407. $error++;
  408. }
  409. }
  410. } else {
  411. $this->error = $this->db->lasterror();
  412. dol_syslog(get_class($this)."::set_infocredit Update Bons Error");
  413. $error++;
  414. }
  415. /*
  416. * End of procedure
  417. */
  418. if ($error == 0) {
  419. $this->date_credit = $date;
  420. $this->statut = self::STATUS_CREDITED;
  421. $this->db->commit();
  422. return 0;
  423. } else {
  424. $this->db->rollback();
  425. return -1;
  426. }
  427. } else {
  428. return -1026;
  429. }
  430. }
  431. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  432. /**
  433. * Set withdrawal to transmited status
  434. *
  435. * @param User $user id of user
  436. * @param int $date date of action
  437. * @param string $method method of transmision to bank
  438. * @return int >0 if OK, <0 if KO
  439. */
  440. public function set_infotrans($user, $date, $method)
  441. {
  442. // phpcs:enable
  443. global $conf, $langs;
  444. $error = 0;
  445. dol_syslog(get_class($this)."::set_infotrans Start", LOG_INFO);
  446. if ($this->db->begin()) {
  447. $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons ";
  448. $sql .= " SET fk_user_trans = ".$user->id;
  449. $sql .= " , date_trans = '".$this->db->idate($date)."'";
  450. $sql .= " , method_trans = ".((int) $method);
  451. $sql .= " , statut = ".self::STATUS_TRANSFERED;
  452. $sql .= " WHERE rowid = ".((int) $this->id);
  453. $sql .= " AND entity = ".((int) $conf->entity);
  454. $sql .= " AND statut = 0";
  455. if ($this->db->query($sql)) {
  456. $this->method_trans = $method;
  457. $langs->load('withdrawals');
  458. $subject = $langs->trans("InfoTransSubject", $this->ref);
  459. $message = $langs->trans("InfoTransMessage", $this->ref, dolGetFirstLastname($user->firstname, $user->lastname));
  460. $message .= $langs->trans("InfoTransData", price($this->amount), $this->methodes_trans[$this->method_trans], dol_print_date($date, 'day'));
  461. // TODO Call trigger to create a notification using notification module
  462. } else {
  463. $error++;
  464. }
  465. if ($error == 0) {
  466. $this->date_trans = $date;
  467. $this->statut = 1;
  468. $this->user_trans = $user->id;
  469. $this->db->commit();
  470. return 0;
  471. } else {
  472. $this->db->rollback();
  473. dol_syslog(get_class($this)."::set_infotrans ROLLBACK", LOG_ERR);
  474. return -1;
  475. }
  476. } else {
  477. dol_syslog(get_class($this)."::set_infotrans Ouverture transaction SQL impossible", LOG_CRIT);
  478. return -2;
  479. }
  480. }
  481. /**
  482. * Get invoice list
  483. *
  484. * @param int $amounts If you want to get the amount of the order for each invoice
  485. * @return array Id of invoices
  486. */
  487. private function getListInvoices($amounts = 0)
  488. {
  489. global $conf;
  490. $arr = array();
  491. /*
  492. * Returns all invoices presented within same order
  493. */
  494. $sql = "SELECT ";
  495. if ($this->type == 'bank-transfer') {
  496. $sql .= " pf.fk_facture_fourn";
  497. } else {
  498. $sql .= " pf.fk_facture";
  499. }
  500. if ($amounts) {
  501. $sql .= ", SUM(pl.amount)";
  502. }
  503. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p";
  504. $sql .= " , ".MAIN_DB_PREFIX."prelevement_lignes as pl";
  505. $sql .= " , ".MAIN_DB_PREFIX."prelevement as pf";
  506. $sql .= " WHERE pf.fk_prelevement_lignes = pl.rowid";
  507. $sql .= " AND pl.fk_prelevement_bons = p.rowid";
  508. $sql .= " AND p.rowid = ".((int) $this->id);
  509. $sql .= " AND p.entity = ".((int) $conf->entity);
  510. if ($amounts) {
  511. if ($this->type == 'bank-transfer') {
  512. $sql .= " GROUP BY fk_facture_fourn";
  513. } else {
  514. $sql .= " GROUP BY fk_facture";
  515. }
  516. }
  517. $resql = $this->db->query($sql);
  518. if ($resql) {
  519. $num = $this->db->num_rows($resql);
  520. if ($num) {
  521. $i = 0;
  522. while ($i < $num) {
  523. $row = $this->db->fetch_row($resql);
  524. if (!$amounts) {
  525. $arr[$i] = $row[0];
  526. } else {
  527. $arr[$i] = array(
  528. $row[0],
  529. $row[1]
  530. );
  531. }
  532. $i++;
  533. }
  534. }
  535. $this->db->free($resql);
  536. } else {
  537. dol_syslog(get_class($this)."::getListInvoices Erreur");
  538. }
  539. return $arr;
  540. }
  541. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  542. /**
  543. * Returns amount waiting for direct debit payment or credit transfer payment
  544. *
  545. * @param string $mode 'direct-debit' or 'bank-transfer'
  546. * @return double <O if KO, Total amount
  547. */
  548. public function SommeAPrelever($mode = 'direct-debit')
  549. {
  550. // phpcs:enable
  551. global $conf;
  552. $sql = "SELECT sum(pfd.amount) as nb";
  553. if ($mode != 'bank-transfer') {
  554. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f,";
  555. } else {
  556. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f,";
  557. }
  558. $sql .= " ".MAIN_DB_PREFIX."prelevement_demande as pfd";
  559. $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
  560. if (empty($conf->global->WITHDRAWAL_ALLOW_ANY_INVOICE_STATUS)) {
  561. $sql .= " AND f.fk_statut = ".Facture::STATUS_VALIDATED;
  562. }
  563. if ($mode != 'bank-transfer') {
  564. $sql .= " AND f.rowid = pfd.fk_facture";
  565. } else {
  566. $sql .= " AND f.rowid = pfd.fk_facture_fourn";
  567. }
  568. $sql .= " AND f.paye = 0";
  569. $sql .= " AND pfd.traite = 0";
  570. $sql .= " AND pfd.ext_payment_id IS NULL";
  571. $sql .= " AND f.total_ttc > 0";
  572. $resql = $this->db->query($sql);
  573. if ($resql) {
  574. $obj = $this->db->fetch_object($resql);
  575. $this->db->free($resql);
  576. return $obj->nb;
  577. } else {
  578. $error = 1;
  579. dol_syslog(get_class($this)."::SommeAPrelever Erreur -1");
  580. dol_syslog($this->db->error());
  581. return -1;
  582. }
  583. }
  584. /**
  585. * Get number of invoices waiting for payment
  586. *
  587. * @param string $mode 'direct-debit' or 'bank-transfer'
  588. * @return int <O if KO, number of invoices if OK
  589. */
  590. public function nbOfInvoiceToPay($mode = 'direct-debit')
  591. {
  592. return $this->NbFactureAPrelever($mode);
  593. }
  594. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  595. /**
  596. * Get number of invoices to pay
  597. *
  598. * @param string $type 'direct-debit' or 'bank-transfer'
  599. * @return int <O if KO, number of invoices if OK
  600. */
  601. public function NbFactureAPrelever($type = 'direct-debit')
  602. {
  603. // phpcs:enable
  604. global $conf;
  605. $sql = "SELECT count(f.rowid) as nb";
  606. if ($type == 'bank-transfer') {
  607. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  608. } else {
  609. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  610. }
  611. $sql .= ", ".MAIN_DB_PREFIX."prelevement_demande as pfd";
  612. $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
  613. if (empty($conf->global->WITHDRAWAL_ALLOW_ANY_INVOICE_STATUS)) {
  614. $sql .= " AND f.fk_statut = ".Facture::STATUS_VALIDATED;
  615. }
  616. if ($type == 'bank-transfer') {
  617. $sql .= " AND f.rowid = pfd.fk_facture_fourn";
  618. } else {
  619. $sql .= " AND f.rowid = pfd.fk_facture";
  620. }
  621. $sql .= " AND pfd.traite = 0";
  622. $sql .= " AND pfd.ext_payment_id IS NULL";
  623. $sql .= " AND f.total_ttc > 0";
  624. dol_syslog(get_class($this)."::NbFactureAPrelever");
  625. $resql = $this->db->query($sql);
  626. if ($resql) {
  627. $obj = $this->db->fetch_object($resql);
  628. $this->db->free($resql);
  629. return $obj->nb;
  630. } else {
  631. $this->error = get_class($this)."::NbFactureAPrelever Erreur -1 sql=".$this->db->error();
  632. return -1;
  633. }
  634. }
  635. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  636. /**
  637. * Create a direct debit order or a credit transfer order
  638. * TODO delete params banque and agence when not necessary
  639. *
  640. * @param int $banque dolibarr mysoc bank
  641. * @param int $agence dolibarr mysoc bank office (guichet)
  642. * @param string $mode real=do action, simu=test only
  643. * @param string $format FRST, RCUR or ALL
  644. * @param string $executiondate Date to execute the transfer
  645. * @param int $notrigger Disable triggers
  646. * @param string $type 'direct-debit' or 'bank-transfer'
  647. * @return int <0 if KO, No of invoice included into file if OK
  648. */
  649. public function create($banque = 0, $agence = 0, $mode = 'real', $format = 'ALL', $executiondate = '', $notrigger = 0, $type = 'direct-debit')
  650. {
  651. // phpcs:enable
  652. global $conf, $langs, $user;
  653. dol_syslog(__METHOD__."::Bank=".$banque." Office=".$agence." mode=".$mode." format=".$format, LOG_DEBUG);
  654. require_once DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php";
  655. require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
  656. if ($type != 'bank-transfer') {
  657. if (empty($format)) {
  658. $this->error = 'ErrorBadParametersForDirectDebitFileCreate';
  659. return -1;
  660. }
  661. }
  662. $error = 0;
  663. $datetimeprev = dol_now('gmt');
  664. //Choice the date of the execution direct debit
  665. if (!empty($executiondate)) {
  666. $datetimeprev = $executiondate;
  667. }
  668. $month = dol_print_date($datetimeprev, "%m", 'gmt');
  669. $year = dol_print_date($datetimeprev, "%Y", 'gmt');
  670. $this->invoice_in_error = array();
  671. $this->thirdparty_in_error = array();
  672. // Read invoices
  673. $factures = array();
  674. $factures_prev = array();
  675. $factures_result = array();
  676. $factures_prev_id = array();
  677. $factures_errors = array();
  678. if (!$error) {
  679. $sql = "SELECT f.rowid, pfd.rowid as pfdrowid, f.fk_soc";
  680. $sql .= ", pfd.code_banque, pfd.code_guichet, pfd.number, pfd.cle_rib";
  681. $sql .= ", pfd.amount";
  682. $sql .= ", s.nom as name";
  683. if ($type != 'bank-transfer') {
  684. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  685. } else {
  686. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  687. }
  688. $sql .= ", ".MAIN_DB_PREFIX."societe as s";
  689. $sql .= ", ".MAIN_DB_PREFIX."prelevement_demande as pfd";
  690. $sql .= " WHERE f.entity IN (".getEntity('invoice').')';
  691. if ($type != 'bank-transfer') {
  692. $sql .= " AND f.rowid = pfd.fk_facture";
  693. } else {
  694. $sql .= " AND f.rowid = pfd.fk_facture_fourn";
  695. }
  696. $sql .= " AND s.rowid = f.fk_soc";
  697. $sql .= " AND f.fk_statut = 1"; // Invoice validated
  698. $sql .= " AND f.paye = 0";
  699. $sql .= " AND pfd.traite = 0";
  700. $sql .= " AND f.total_ttc > 0";
  701. $sql .= " AND pfd.ext_payment_id IS NULL";
  702. dol_syslog(__METHOD__."::Read invoices,", LOG_DEBUG);
  703. $resql = $this->db->query($sql);
  704. if ($resql) {
  705. $num = $this->db->num_rows($resql);
  706. $i = 0;
  707. while ($i < $num) {
  708. $row = $this->db->fetch_row($resql);
  709. $factures[$i] = $row; // All fields
  710. if ($row[7] == 0) {
  711. $error++;
  712. dol_syslog(__METHOD__."::Read invoices error Found a null invoice", LOG_ERR);
  713. $this->invoice_in_error[$row[0]] = "Error for invoice id ".$row[0].", found a null amount";
  714. break;
  715. }
  716. $i++;
  717. }
  718. $this->db->free($resql);
  719. dol_syslog(__METHOD__."::Read invoices, ".$i." invoices to withdraw", LOG_DEBUG);
  720. } else {
  721. $error++;
  722. dol_syslog(__METHOD__."::Read invoices error ".$this->db->error(), LOG_ERR);
  723. }
  724. }
  725. if (!$error) {
  726. require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
  727. $soc = new Societe($this->db);
  728. // Check BAN
  729. $i = 0;
  730. dol_syslog(__METHOD__."::Check BAN", LOG_DEBUG);
  731. if (count($factures) > 0) {
  732. foreach ($factures as $key => $fac) {
  733. if ($type != 'bank-transfer') {
  734. $tmpinvoice = new Facture($this->db);
  735. } else {
  736. $tmpinvoice = new FactureFournisseur($this->db);
  737. }
  738. $resfetch = $tmpinvoice->fetch($fac[0]);
  739. if ($resfetch >= 0) { // Field 0 of $fac is rowid of invoice
  740. if ($soc->fetch($tmpinvoice->socid) >= 0) {
  741. $bac = new CompanyBankAccount($this->db);
  742. $bac->fetch(0, $soc->id);
  743. if ($type != 'bank-transfer') {
  744. if ($format == 'FRST' && $bac->frstrecur != 'FRST') {
  745. continue;
  746. }
  747. if ($format == 'RCUR' && ($bac->frstrecur != 'RCUR' && $bac->frstrecur != 'RECUR')) {
  748. continue;
  749. }
  750. }
  751. if ($bac->verif() >= 1) {
  752. $factures_prev[$i] = $fac;
  753. /* second array necessary for BonPrelevement */
  754. $factures_prev_id[$i] = $fac[0];
  755. $i++;
  756. //dol_syslog(__METHOD__."::RIB is ok", LOG_DEBUG);
  757. } else {
  758. dol_syslog(__METHOD__."::Check BAN Error on default bank number IBAN/BIC for thirdparty reported by verif() ".$tmpinvoice->socid." ".$soc->name, LOG_WARNING);
  759. $this->invoice_in_error[$fac[0]] = "Error on default bank number IBAN/BIC for invoice ".$tmpinvoice->getNomUrl(0)." for thirdparty ".$soc->getNomUrl(0);
  760. $this->thirdparty_in_error[$soc->id] = "Error on default bank number IBAN/BIC for invoice ".$tmpinvoice->getNomUrl(0)." for thirdparty ".$soc->getNomUrl(0);
  761. }
  762. } else {
  763. dol_syslog(__METHOD__."::Check BAN Failed to read company", LOG_WARNING);
  764. }
  765. } else {
  766. dol_syslog(__METHOD__."::Check BAN Failed to read invoice", LOG_WARNING);
  767. }
  768. }
  769. } else {
  770. dol_syslog(__METHOD__."::Check BAN No invoice to process", LOG_WARNING);
  771. }
  772. }
  773. $ok = 0;
  774. // Withdraw invoices in factures_prev array
  775. $out = count($factures_prev)." invoices will be included.";
  776. //print $out."\n";
  777. dol_syslog($out);
  778. // Return warning
  779. /*$i=0;
  780. foreach ($this->thirdparty_in_error as $key => $val)
  781. {
  782. if ($i < 10) setEventMessages($val, null, 'warnings');
  783. else setEventMessages('More error were discarded...', null, 'warnings');
  784. $i++;
  785. }*/
  786. if (count($factures_prev) > 0) {
  787. if ($mode == 'real') {
  788. $ok = 1;
  789. } else {
  790. print $langs->trans("ModeWarning"); // "Option for real mode was not set, we stop after this simulation\n";
  791. }
  792. }
  793. if ($ok) {
  794. /*
  795. * We are in real mode.
  796. * We create order and build file into disk
  797. */
  798. $this->db->begin();
  799. $now = dol_now();
  800. $ref = '';
  801. /*
  802. * Process order generation
  803. */
  804. if (!$error) {
  805. $ref = substr($year, -2).$month;
  806. $sql = "SELECT substring(ref from char_length(ref) - 1)"; // To extract "YYMMXX" from "TYYMMXX"
  807. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_bons";
  808. $sql .= " WHERE ref LIKE '_".$this->db->escape($ref)."%'";
  809. $sql .= " AND entity = ".((int) $conf->entity);
  810. $sql .= " ORDER BY ref DESC LIMIT 1";
  811. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  812. $resql = $this->db->query($sql);
  813. if ($resql) {
  814. $row = $this->db->fetch_row($resql);
  815. // Build the new ref
  816. $ref = "T".$ref.sprintf("%02d", (intval($row[0]) + 1));
  817. // $conf->abc->dir_output may be:
  818. // /home/ldestailleur/git/dolibarr_15.0/documents/abc/
  819. // or
  820. // /home/ldestailleur/git/dolibarr_15.0/documents/X/abc with X >= 2 with multicompany.
  821. if ($type != 'bank-transfer') {
  822. $dir = $conf->prelevement->dir_output.'/receipts';
  823. } else {
  824. $dir = $conf->paymentbybanktransfer->dir_output.'/receipts';
  825. }
  826. if (!is_dir($dir)) {
  827. dol_mkdir($dir);
  828. }
  829. $this->filename = $dir.'/'.$ref.'.xml';
  830. // Create withdraw receipt in database
  831. $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_bons (";
  832. $sql .= "ref, entity, datec, type";
  833. $sql .= ") VALUES (";
  834. $sql .= "'".$this->db->escape($ref)."'";
  835. $sql .= ", ".((int) $conf->entity);
  836. $sql .= ", '".$this->db->idate($now)."'";
  837. $sql .= ", '".($type == 'bank-transfer' ? 'bank-transfer' : 'debit-order')."'";
  838. $sql .= ")";
  839. $resql = $this->db->query($sql);
  840. if ($resql) {
  841. $prev_id = $this->db->last_insert_id(MAIN_DB_PREFIX."prelevement_bons");
  842. $this->id = $prev_id;
  843. $this->ref = $ref;
  844. } else {
  845. $error++;
  846. dol_syslog(__METHOD__."::Create withdraw receipt ".$this->db->lasterror(), LOG_ERR);
  847. }
  848. } else {
  849. $error++;
  850. dol_syslog(__METHOD__."::Get last withdraw receipt ".$this->db->lasterror(), LOG_ERR);
  851. }
  852. }
  853. if (!$error) {
  854. if ($type != 'bank-transfer') {
  855. $fact = new Facture($this->db);
  856. } else {
  857. $fact = new FactureFournisseur($this->db);
  858. }
  859. /*
  860. * Create withdrawal receipt in database
  861. */
  862. if (count($factures_prev) > 0) {
  863. foreach ($factures_prev as $fac) { // Add a link in database for each invoice
  864. // Fetch invoice
  865. $result = $fact->fetch($fac[0]);
  866. if ($result < 0) {
  867. $this->error = 'ERRORBONPRELEVEMENT Failed to load invoice with id '.$fac[0];
  868. break;
  869. }
  870. /*
  871. * Add standing order. This add record into llx_prelevement_lignes
  872. *
  873. * $fac[0] : invoice_id
  874. * $fac[1] : ???
  875. * $fac[2] : third party id
  876. * $fac[3] : banque
  877. * $fac[4] : guichet
  878. * $fac[5] : number
  879. * $fac[6] : cle rib
  880. * $fac[7] : amount
  881. * $fac[8] : client nom
  882. */
  883. $ri = $this->AddFacture($fac[0], $fac[2], $fac[8], $fac[7], $fac[3], $fac[4], $fac[5], $fac[6], $type);
  884. if ($ri <> 0) {
  885. $error++;
  886. }
  887. // Update invoice requests as done
  888. $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_demande";
  889. $sql .= " SET traite = 1";
  890. $sql .= ", date_traite = '".$this->db->idate($now)."'";
  891. $sql .= ", fk_prelevement_bons = ".((int) $this->id);
  892. $sql .= " WHERE rowid = ".((int) $fac[1]);
  893. $resql = $this->db->query($sql);
  894. if (!$resql) {
  895. $error++;
  896. $this->errors[] = $this->db->lasterror();
  897. dol_syslog(__METHOD__."::Update Error=".$this->db->lasterror(), LOG_ERR);
  898. }
  899. }
  900. }
  901. }
  902. if (!$error) {
  903. /*
  904. * Create file of type='direct-debit' for direct debit order or type='bank-transfer' for credit transfer into a XML file
  905. */
  906. dol_syslog(__METHOD__."::Init direct debit or credit transfer file for ".count($factures_prev)." invoices", LOG_DEBUG);
  907. if (count($factures_prev) > 0) {
  908. $this->date_echeance = $datetimeprev;
  909. $this->reference_remise = $ref;
  910. $id = $conf->global->PRELEVEMENT_ID_BANKACCOUNT;
  911. if ($type == 'bank-transfer') {
  912. $id = $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT;
  913. }
  914. $account = new Account($this->db);
  915. if ($account->fetch($id) > 0) {
  916. $this->emetteur_code_banque = $account->code_banque;
  917. $this->emetteur_code_guichet = $account->code_guichet;
  918. $this->emetteur_numero_compte = $account->number;
  919. $this->emetteur_number_key = $account->cle_rib;
  920. $this->sepa_xml_pti_in_ctti = (bool) $account->pti_in_ctti;
  921. $this->emetteur_iban = $account->iban;
  922. $this->emetteur_bic = $account->bic;
  923. $this->emetteur_ics = ($type == 'bank-transfer' ? $account->ics_transfer : $account->ics);
  924. $this->raison_sociale = $account->proprio;
  925. }
  926. $this->factures = $factures_prev_id;
  927. $this->context['factures_prev'] = $factures_prev;
  928. // Generation of direct debit or credti transfer file $this->filename (May be a SEPA file for european countries)
  929. // This also set the property $this->total with amount that is included into file
  930. $result = $this->generate($format, $executiondate, $type);
  931. if ($result < 0) {
  932. //var_dump($this->error);
  933. //var_dump($this->invoice_in_error);
  934. $error++;
  935. }
  936. }
  937. dol_syslog(__METHOD__."::End withdraw receipt, file ".$this->filename, LOG_DEBUG);
  938. }
  939. //var_dump($this->total);exit;
  940. /*
  941. * Update total defined after generation of file
  942. */
  943. if (!$error) {
  944. $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons";
  945. $sql .= " SET amount = ".price2num($this->total);
  946. $sql .= " WHERE rowid = ".((int) $this->id);
  947. $sql .= " AND entity = ".((int) $conf->entity);
  948. $resql = $this->db->query($sql);
  949. if (!$resql) {
  950. $error++;
  951. dol_syslog(__METHOD__."::Error update total: ".$this->db->error(), LOG_ERR);
  952. }
  953. }
  954. if (!$error && !$notrigger) {
  955. $triggername = 'DIRECT_DEBIT_ORDER_CREATE';
  956. if ($type != 'bank-transfer') {
  957. $triggername = 'CREDIT_TRANSFER_ORDER_CREATE';
  958. }
  959. // Call trigger
  960. $result = $this->call_trigger($triggername, $user);
  961. if ($result < 0) {
  962. $error++;
  963. }
  964. // End call triggers
  965. }
  966. if (!$error) {
  967. $this->db->commit();
  968. return count($factures_prev);
  969. } else {
  970. $this->db->rollback();
  971. return -1;
  972. }
  973. } else {
  974. return 0;
  975. }
  976. }
  977. /**
  978. * Get object and lines from database
  979. *
  980. * @param User $user Object user that delete
  981. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  982. * @return int >0 if OK, <0 if KO
  983. */
  984. public function delete($user = null, $notrigger = 0)
  985. {
  986. $this->db->begin();
  987. $error = 0;
  988. $resql1 = $resql2 = $resql3 = $resql4 = 0;
  989. if (!$notrigger) {
  990. $triggername = 'DIRECT_DEBIT_ORDER_DELETE';
  991. if ($this->type == 'bank-transfer') {
  992. $triggername = 'PAYMENTBYBANKTRANFER_DELETE';
  993. }
  994. // Call trigger
  995. $result = $this->call_trigger($triggername, $user);
  996. if ($result < 0) {
  997. $error++;
  998. }
  999. // End call triggers
  1000. }
  1001. if (!$error) {
  1002. $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).")";
  1003. $resql1 = $this->db->query($sql);
  1004. if (!$resql1) {
  1005. dol_print_error($this->db);
  1006. }
  1007. }
  1008. if (!$error) {
  1009. $sql = "DELETE FROM ".MAIN_DB_PREFIX."prelevement_lignes WHERE fk_prelevement_bons = ".((int) $this->id);
  1010. $resql2 = $this->db->query($sql);
  1011. if (!$resql2) {
  1012. dol_print_error($this->db);
  1013. }
  1014. }
  1015. if (!$error) {
  1016. $sql = "DELETE FROM ".MAIN_DB_PREFIX."prelevement_bons WHERE rowid = ".((int) $this->id);
  1017. $resql3 = $this->db->query($sql);
  1018. if (!$resql3) {
  1019. dol_print_error($this->db);
  1020. }
  1021. }
  1022. if (!$error) {
  1023. $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_demande SET fk_prelevement_bons = NULL, traite = 0 WHERE fk_prelevement_bons = ".((int) $this->id);
  1024. $resql4 = $this->db->query($sql);
  1025. if (!$resql4) {
  1026. dol_print_error($this->db);
  1027. }
  1028. }
  1029. if ($resql1 && $resql2 && $resql3 && $resql4 && !$error) {
  1030. $this->db->commit();
  1031. return 1;
  1032. } else {
  1033. $this->db->rollback();
  1034. return -1;
  1035. }
  1036. }
  1037. /**
  1038. * Returns clickable name (with picto)
  1039. *
  1040. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  1041. * @param string $option On what the link point to ('nolink', ...)
  1042. * @param int $notooltip 1=Disable tooltip
  1043. * @param string $morecss Add more css on link
  1044. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  1045. * @return string URL of target
  1046. */
  1047. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  1048. {
  1049. global $conf, $langs, $hookmanager;
  1050. if (!empty($conf->dol_no_mouse_hover)) {
  1051. $notooltip = 1; // Force disable tooltips
  1052. }
  1053. $result = '';
  1054. $labeltoshow = 'PaymentByDirectDebit';
  1055. if (!empty($this->type) && $this->type == 'bank-transfer') {
  1056. $labeltoshow = 'PaymentByBankTransfer';
  1057. }
  1058. $label = '<u>'.$langs->trans($labeltoshow).'</u>';
  1059. $label .= '<br>';
  1060. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  1061. if (isset($this->statut)) {
  1062. $label .= '<br><b>'.$langs->trans("Status").":</b> ".$this->getLibStatut(5);
  1063. }
  1064. $url = DOL_URL_ROOT.'/compta/prelevement/card.php?id='.$this->id;
  1065. if (!empty($this->type) && $this->type == 'bank-transfer') {
  1066. $url = DOL_URL_ROOT.'/compta/prelevement/card.php?id='.$this->id;
  1067. }
  1068. if ($option != 'nolink') {
  1069. // Add param to save lastsearch_values or not
  1070. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  1071. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  1072. $add_save_lastsearch_values = 1;
  1073. }
  1074. if ($add_save_lastsearch_values) {
  1075. $url .= '&save_lastsearch_values=1';
  1076. }
  1077. }
  1078. $linkclose = '';
  1079. if (empty($notooltip)) {
  1080. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  1081. $label = $langs->trans("ShowMyObject");
  1082. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  1083. }
  1084. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  1085. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  1086. } else {
  1087. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  1088. }
  1089. $linkstart = '<a href="'.$url.'"';
  1090. $linkstart .= $linkclose.'>';
  1091. $linkend = '</a>';
  1092. $result .= $linkstart;
  1093. if ($withpicto) {
  1094. $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);
  1095. }
  1096. if ($withpicto != 2) {
  1097. $result .= $this->ref;
  1098. }
  1099. $result .= $linkend;
  1100. global $action, $hookmanager;
  1101. $hookmanager->initHooks(array('banktransferdao'));
  1102. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  1103. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  1104. if ($reshook > 0) {
  1105. $result = $hookmanager->resPrint;
  1106. } else {
  1107. $result .= $hookmanager->resPrint;
  1108. }
  1109. return $result;
  1110. }
  1111. /**
  1112. * Delete a notification def by id
  1113. *
  1114. * @param int $rowid id of notification
  1115. * @return int 0 if OK, <0 if KO
  1116. */
  1117. public function deleteNotificationById($rowid)
  1118. {
  1119. $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def";
  1120. $sql .= " WHERE rowid = ".((int) $rowid);
  1121. if ($this->db->query($sql)) {
  1122. return 0;
  1123. } else {
  1124. return -1;
  1125. }
  1126. }
  1127. /**
  1128. * Delete a notification
  1129. *
  1130. * @param int|User $user notification user
  1131. * @param string $action notification action
  1132. * @return int >0 if OK, <0 if KO
  1133. */
  1134. public function deleteNotification($user, $action)
  1135. {
  1136. if (is_object($user)) {
  1137. $userid = $user->id;
  1138. } else { // If user is an id
  1139. $userid = $user;
  1140. }
  1141. $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def";
  1142. $sql .= " WHERE fk_user=".((int) $userid)." AND fk_action='".$this->db->escape($action)."'";
  1143. if ($this->db->query($sql)) {
  1144. return 0;
  1145. } else {
  1146. return -1;
  1147. }
  1148. }
  1149. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1150. /**
  1151. * Add a notification
  1152. *
  1153. * @param DoliDB $db database handler
  1154. * @param int|User $user notification user
  1155. * @param string $action notification action
  1156. * @return int 0 if OK, <0 if KO
  1157. */
  1158. public function addNotification($db, $user, $action)
  1159. {
  1160. // phpcs:enable
  1161. $result = 0;
  1162. if (is_object($user)) {
  1163. $userid = $user->id;
  1164. } else { // If user is an id
  1165. $userid = $user;
  1166. }
  1167. if ($this->deleteNotification($user, $action) == 0) {
  1168. $now = dol_now();
  1169. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify_def (datec,fk_user, fk_soc, fk_contact, fk_action)";
  1170. $sql .= " VALUES ('".$this->db->idate($now)."', ".((int) $userid).", 'NULL', 'NULL', '".$this->db->escape($action)."')";
  1171. dol_syslog("adnotiff: ".$sql);
  1172. if ($this->db->query($sql)) {
  1173. $result = 0;
  1174. } else {
  1175. $result = -1;
  1176. dol_syslog(get_class($this)."::addNotification Error $result");
  1177. }
  1178. }
  1179. return $result;
  1180. }
  1181. /**
  1182. * Generate a direct debit or credit transfer file.
  1183. * Generation Formats:
  1184. * - Europe: SEPA (France: CFONB no more supported, Spain: AEB19 if external module EsAEB is enabled)
  1185. * - Others countries: Warning message
  1186. * File is generated with name this->filename
  1187. *
  1188. * @param string $format FRST, RCUR or ALL
  1189. * @param string $executiondate Date to execute transfer
  1190. * @param string $type 'direct-debit' or 'bank-transfer'
  1191. * @return int >=0 if OK, <0 if KO
  1192. */
  1193. public function generate($format = 'ALL', $executiondate = '', $type = 'direct-debit')
  1194. {
  1195. global $conf, $langs, $mysoc;
  1196. //TODO: Optimize code to read lines in a single function
  1197. $result = 0;
  1198. dol_syslog(get_class($this)."::generate build file=".$this->filename." type=".$type);
  1199. $this->file = fopen($this->filename, "w");
  1200. if (empty($this->file)) {
  1201. $this->error = $langs->trans('ErrorFailedToOpenFile', $this->filename);
  1202. return -1;
  1203. }
  1204. $found = 0;
  1205. $this->total = 0;
  1206. // Build file for European countries
  1207. if ($mysoc->isInEEC()) {
  1208. $found++;
  1209. if ($type != 'bank-transfer') {
  1210. /**
  1211. * SECTION CREATION FICHIER SEPA - DIRECT DEBIT
  1212. */
  1213. // SEPA Initialisation
  1214. $CrLf = "\n";
  1215. $now = dol_now();
  1216. $dateTime_ECMA = dol_print_date($now, '%Y-%m-%dT%H:%M:%S');
  1217. $date_actu = $now;
  1218. if (!empty($executiondate)) {
  1219. $date_actu = $executiondate;
  1220. }
  1221. $dateTime_YMD = dol_print_date($date_actu, '%Y%m%d');
  1222. $dateTime_YMDHMS = dol_print_date($date_actu, '%Y%m%d%H%M%S');
  1223. $fileDebiteurSection = '';
  1224. $fileEmetteurSection = '';
  1225. $i = 0;
  1226. /*
  1227. * Section Debitor (sepa Debiteurs bloc lines)
  1228. */
  1229. $sql = "SELECT soc.rowid as socid, soc.code_client as code, soc.address, soc.zip, soc.town, c.code as country_code,";
  1230. $sql .= " pl.client_nom as nom, pl.code_banque as cb, pl.code_guichet as cg, pl.number as cc, pl.amount as somme,";
  1231. $sql .= " f.ref as fac, pf.fk_facture as idfac,";
  1232. $sql .= " rib.rowid, rib.datec, rib.iban_prefix as iban, rib.bic as bic, rib.rowid as drum, rib.rum, rib.date_rum";
  1233. $sql .= " FROM";
  1234. $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
  1235. $sql .= " ".MAIN_DB_PREFIX."facture as f,";
  1236. $sql .= " ".MAIN_DB_PREFIX."prelevement as pf,";
  1237. $sql .= " ".MAIN_DB_PREFIX."societe as soc,";
  1238. $sql .= " ".MAIN_DB_PREFIX."c_country as c,";
  1239. $sql .= " ".MAIN_DB_PREFIX."societe_rib as rib";
  1240. $sql .= " WHERE pl.fk_prelevement_bons = ".((int) $this->id);
  1241. $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
  1242. $sql .= " AND pf.fk_facture = f.rowid";
  1243. $sql .= " AND f.fk_soc = soc.rowid";
  1244. $sql .= " AND soc.fk_pays = c.rowid";
  1245. $sql .= " AND rib.fk_soc = f.fk_soc";
  1246. $sql .= " AND rib.default_rib = 1";
  1247. $sql .= " AND rib.type = 'ban'";
  1248. // Define $fileDebiteurSection. One section DrctDbtTxInf per invoice.
  1249. $resql = $this->db->query($sql);
  1250. if ($resql) {
  1251. $cachearraytotestduplicate = array();
  1252. $num = $this->db->num_rows($resql);
  1253. while ($i < $num) {
  1254. $obj = $this->db->fetch_object($resql);
  1255. if (!empty($cachearraytotestduplicate[$obj->idfac])) {
  1256. $this->error = $langs->trans('ErrorCompanyHasDuplicateDefaultBAN', $obj->socid);
  1257. $this->invoice_in_error[$obj->idfac] = $this->error;
  1258. $result = -2;
  1259. break;
  1260. }
  1261. $cachearraytotestduplicate[$obj->idfac] = $obj->rowid;
  1262. $daterum = (!empty($obj->date_rum)) ? $this->db->jdate($obj->date_rum) : $this->db->jdate($obj->datec);
  1263. $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);
  1264. $this->total = $this->total + $obj->somme;
  1265. $i++;
  1266. }
  1267. $nbtotalDrctDbtTxInf = $i;
  1268. } else {
  1269. $this->error = $this->db->lasterror();
  1270. fputs($this->file, 'ERROR DEBITOR '.$sql.$CrLf); // DEBITOR = Customers
  1271. $result = -2;
  1272. }
  1273. // Define $fileEmetteurSection. Start of bloc PmtInf. Will contains all $nbtotalDrctDbtTxInf
  1274. if ($result != -2) {
  1275. $fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $nbtotalDrctDbtTxInf, $this->total, $CrLf, $format, $type);
  1276. }
  1277. /**
  1278. * SECTION CREATION SEPA FILE - ISO200022
  1279. */
  1280. // SEPA File Header
  1281. fputs($this->file, '<'.'?xml version="1.0" encoding="UTF-8" standalone="yes"?'.'>'.$CrLf);
  1282. 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);
  1283. fputs($this->file, ' <CstmrDrctDbtInitn>'.$CrLf);
  1284. // SEPA Group header
  1285. fputs($this->file, ' <GrpHdr>'.$CrLf);
  1286. fputs($this->file, ' <MsgId>'.('DD/'.$dateTime_YMD.'/REF'.$this->id).'</MsgId>'.$CrLf);
  1287. fputs($this->file, ' <CreDtTm>'.$dateTime_ECMA.'</CreDtTm>'.$CrLf);
  1288. fputs($this->file, ' <NbOfTxs>'.$i.'</NbOfTxs>'.$CrLf);
  1289. fputs($this->file, ' <CtrlSum>'.$this->total.'</CtrlSum>'.$CrLf);
  1290. fputs($this->file, ' <InitgPty>'.$CrLf);
  1291. fputs($this->file, ' <Nm>'.dolEscapeXML(strtoupper(dol_string_nospecial(dol_string_unaccent($this->raison_sociale), ' '))).'</Nm>'.$CrLf);
  1292. fputs($this->file, ' <Id>'.$CrLf);
  1293. fputs($this->file, ' <PrvtId>'.$CrLf);
  1294. fputs($this->file, ' <Othr>'.$CrLf);
  1295. fputs($this->file, ' <Id>'.$this->emetteur_ics.'</Id>'.$CrLf);
  1296. fputs($this->file, ' </Othr>'.$CrLf);
  1297. fputs($this->file, ' </PrvtId>'.$CrLf);
  1298. fputs($this->file, ' </Id>'.$CrLf);
  1299. fputs($this->file, ' </InitgPty>'.$CrLf);
  1300. fputs($this->file, ' </GrpHdr>'.$CrLf);
  1301. // SEPA File Emetteur
  1302. if ($result != -2) {
  1303. fputs($this-> file, $fileEmetteurSection);
  1304. }
  1305. // SEPA File Debiteurs
  1306. if ($result != -2) {
  1307. fputs($this-> file, $fileDebiteurSection);
  1308. }
  1309. // SEPA FILE FOOTER
  1310. fputs($this->file, ' </PmtInf>'.$CrLf);
  1311. fputs($this->file, ' </CstmrDrctDbtInitn>'.$CrLf);
  1312. fputs($this->file, '</Document>'.$CrLf);
  1313. } else {
  1314. /**
  1315. * SECTION CREATION FICHIER SEPA - CREDIT TRANSFER
  1316. */
  1317. // SEPA Initialisation
  1318. $CrLf = "\n";
  1319. $now = dol_now();
  1320. $dateTime_ECMA = dol_print_date($now, '%Y-%m-%dT%H:%M:%S');
  1321. $date_actu = $now;
  1322. if (!empty($executiondate)) {
  1323. $date_actu = $executiondate;
  1324. }
  1325. $dateTime_YMD = dol_print_date($date_actu, '%Y%m%d');
  1326. $dateTime_YMDHMS = dol_print_date($date_actu, '%Y%m%d%H%M%S');
  1327. $fileCrediteurSection = '';
  1328. $fileEmetteurSection = '';
  1329. $i = 0;
  1330. /*
  1331. * Section Creditor (sepa Crediteurs bloc lines)
  1332. */
  1333. $sql = "SELECT soc.rowid as socid, soc.code_client as code, soc.address, soc.zip, soc.town, c.code as country_code,";
  1334. $sql .= " pl.client_nom as nom, pl.code_banque as cb, pl.code_guichet as cg, pl.number as cc, pl.amount as somme,";
  1335. $sql .= " f.ref as fac, pf.fk_facture_fourn as idfac, f.ref_supplier as fac_ref_supplier,";
  1336. $sql .= " rib.rowid, rib.datec, rib.iban_prefix as iban, rib.bic as bic, rib.rowid as drum, rib.rum, rib.date_rum";
  1337. $sql .= " FROM";
  1338. $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
  1339. $sql .= " ".MAIN_DB_PREFIX."facture_fourn as f,";
  1340. $sql .= " ".MAIN_DB_PREFIX."prelevement as pf,";
  1341. $sql .= " ".MAIN_DB_PREFIX."societe as soc,";
  1342. $sql .= " ".MAIN_DB_PREFIX."c_country as c,";
  1343. $sql .= " ".MAIN_DB_PREFIX."societe_rib as rib";
  1344. $sql .= " WHERE pl.fk_prelevement_bons = ".((int) $this->id);
  1345. $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
  1346. $sql .= " AND pf.fk_facture_fourn = f.rowid";
  1347. $sql .= " AND f.fk_soc = soc.rowid";
  1348. $sql .= " AND soc.fk_pays = c.rowid";
  1349. $sql .= " AND rib.fk_soc = f.fk_soc";
  1350. $sql .= " AND rib.default_rib = 1";
  1351. $sql .= " AND rib.type = 'ban'";
  1352. // Define $fileCrediteurSection. One section DrctDbtTxInf per invoice.
  1353. $resql = $this->db->query($sql);
  1354. if ($resql) {
  1355. $cachearraytotestduplicate = array();
  1356. $num = $this->db->num_rows($resql);
  1357. while ($i < $num) {
  1358. $obj = $this->db->fetch_object($resql);
  1359. if (!empty($cachearraytotestduplicate[$obj->idfac])) {
  1360. $this->error = $langs->trans('ErrorCompanyHasDuplicateDefaultBAN', $obj->socid);
  1361. $this->invoice_in_error[$obj->idfac] = $this->error;
  1362. $result = -2;
  1363. break;
  1364. }
  1365. $cachearraytotestduplicate[$obj->idfac] = $obj->rowid;
  1366. $daterum = (!empty($obj->date_rum)) ? $this->db->jdate($obj->date_rum) : $this->db->jdate($obj->datec);
  1367. $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_ref_supplier, $obj->idfac, $obj->iban, $obj->bic, $daterum, $obj->drum, $obj->rum, $type);
  1368. $this->total = $this->total + $obj->somme;
  1369. $i++;
  1370. }
  1371. $nbtotalDrctDbtTxInf = $i;
  1372. } else {
  1373. $this->error = $this->db->lasterror();
  1374. fputs($this->file, 'ERROR CREDITOR '.$sql.$CrLf); // CREDITORS = Suppliers
  1375. $result = -2;
  1376. }
  1377. // Define $fileEmetteurSection. Start of bloc PmtInf. Will contains all $nbtotalDrctDbtTxInf
  1378. if ($result != -2) {
  1379. $fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $nbtotalDrctDbtTxInf, $this->total, $CrLf, $format, $type);
  1380. }
  1381. /**
  1382. * SECTION CREATION SEPA FILE - CREDIT TRANSFER - ISO200022
  1383. */
  1384. // SEPA File Header
  1385. fputs($this->file, '<'.'?xml version="1.0" encoding="UTF-8" standalone="yes"?'.'>'.$CrLf);
  1386. 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);
  1387. fputs($this->file, ' <CstmrCdtTrfInitn>'.$CrLf);
  1388. // SEPA Group header
  1389. fputs($this->file, ' <GrpHdr>'.$CrLf);
  1390. fputs($this->file, ' <MsgId>'.('TRF/'.$dateTime_YMD.'/REF'.$this->id).'</MsgId>'.$CrLf);
  1391. fputs($this->file, ' <CreDtTm>'.$dateTime_ECMA.'</CreDtTm>'.$CrLf);
  1392. fputs($this->file, ' <NbOfTxs>'.$i.'</NbOfTxs>'.$CrLf);
  1393. fputs($this->file, ' <CtrlSum>'.$this->total.'</CtrlSum>'.$CrLf);
  1394. fputs($this->file, ' <InitgPty>'.$CrLf);
  1395. fputs($this->file, ' <Nm>'.dolEscapeXML(strtoupper(dol_string_nospecial(dol_string_unaccent($this->raison_sociale), ' '))).'</Nm>'.$CrLf);
  1396. fputs($this->file, ' <Id>'.$CrLf);
  1397. fputs($this->file, ' <PrvtId>'.$CrLf);
  1398. fputs($this->file, ' <Othr>'.$CrLf);
  1399. fputs($this->file, ' <Id>'.$this->emetteur_ics.'</Id>'.$CrLf);
  1400. fputs($this->file, ' </Othr>'.$CrLf);
  1401. fputs($this->file, ' </PrvtId>'.$CrLf);
  1402. fputs($this->file, ' </Id>'.$CrLf);
  1403. fputs($this->file, ' </InitgPty>'.$CrLf);
  1404. fputs($this->file, ' </GrpHdr>'.$CrLf);
  1405. // SEPA File Emetteur (mycompany)
  1406. if ($result != -2) {
  1407. fputs($this-> file, $fileEmetteurSection);
  1408. }
  1409. // SEPA File Creditors
  1410. if ($result != -2) {
  1411. fputs($this-> file, $fileCrediteurSection);
  1412. }
  1413. // SEPA FILE FOOTER
  1414. fputs($this->file, ' </PmtInf>'.$CrLf);
  1415. fputs($this->file, ' </CstmrCdtTrfInitn>'.$CrLf);
  1416. fputs($this->file, '</Document>'.$CrLf);
  1417. }
  1418. }
  1419. // Build file for Other Countries with unknow format
  1420. if (!$found) {
  1421. if ($type != 'bank-transfer') {
  1422. $sql = "SELECT pl.amount";
  1423. $sql .= " FROM";
  1424. $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
  1425. $sql .= " ".MAIN_DB_PREFIX."facture as f,";
  1426. $sql .= " ".MAIN_DB_PREFIX."prelevement as pf";
  1427. $sql .= " WHERE pl.fk_prelevement_bons = ".((int) $this->id);
  1428. $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
  1429. $sql .= " AND pf.fk_facture = f.rowid";
  1430. // Lines
  1431. $i = 0;
  1432. $resql = $this->db->query($sql);
  1433. if ($resql) {
  1434. $num = $this->db->num_rows($resql);
  1435. while ($i < $num) {
  1436. $obj = $this->db->fetch_object($resql);
  1437. $this->total = $this->total + $obj->amount;
  1438. // TODO Write record into file
  1439. $i++;
  1440. }
  1441. } else {
  1442. $result = -2;
  1443. }
  1444. } else {
  1445. $sql = "SELECT pl.amount";
  1446. $sql .= " FROM";
  1447. $sql .= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
  1448. $sql .= " ".MAIN_DB_PREFIX."facture_fourn as f,";
  1449. $sql .= " ".MAIN_DB_PREFIX."prelevement as pf";
  1450. $sql .= " WHERE pl.fk_prelevement_bons = ".((int) $this->id);
  1451. $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
  1452. $sql .= " AND pf.fk_facture_fourn = f.rowid";
  1453. // Lines
  1454. $i = 0;
  1455. $resql = $this->db->query($sql);
  1456. if ($resql) {
  1457. $num = $this->db->num_rows($resql);
  1458. while ($i < $num) {
  1459. $obj = $this->db->fetch_object($resql);
  1460. $this->total = $this->total + $obj->amount;
  1461. // TODO Write record into file
  1462. $i++;
  1463. }
  1464. } else {
  1465. $result = -2;
  1466. }
  1467. }
  1468. $langs->load('withdrawals');
  1469. // TODO Add here code to generate a generic file
  1470. fputs($this->file, $langs->transnoentitiesnoconv('WithdrawalFileNotCapable', $mysoc->country_code));
  1471. }
  1472. fclose($this->file);
  1473. if (!empty($conf->global->MAIN_UMASK)) {
  1474. @chmod($this->filename, octdec($conf->global->MAIN_UMASK));
  1475. }
  1476. return $result;
  1477. }
  1478. /**
  1479. * Generate dynamically a RUM number for a customer bank account
  1480. *
  1481. * @param string $row_code_client Customer code (soc.code_client)
  1482. * @param int $row_datec Creation date of bank account (rib.datec)
  1483. * @param string $row_drum Id of customer bank account (rib.rowid)
  1484. * @return string RUM number
  1485. */
  1486. public static function buildRumNumber($row_code_client, $row_datec, $row_drum)
  1487. {
  1488. global $langs;
  1489. $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)
  1490. // 3 char + '-' + 12 + '-' + id + '-' + code Must be lower than 32.
  1491. return $pre.'-'.dol_print_date($row_datec, 'dayhourlogsmall').'-'.dol_trunc($row_drum.($row_code_client ? '-'.$row_code_client : ''), 13, 'right', 'UTF-8', 1);
  1492. }
  1493. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1494. /**
  1495. * Write recipient of request (customer)
  1496. *
  1497. * @param int $rowid id of line
  1498. * @param string $client_nom name of customer
  1499. * @param string $rib_banque code of bank
  1500. * @param string $rib_guichet code of bank office
  1501. * @param string $rib_number bank account
  1502. * @param float $amount amount
  1503. * @param string $ref ref of invoice
  1504. * @param int $facid id of invoice
  1505. * @param string $rib_dom rib domiciliation
  1506. * @param string $type 'direct-debit' or 'bank-transfer'
  1507. * @return void
  1508. * @see EnregDestinataireSEPA()
  1509. */
  1510. public function EnregDestinataire($rowid, $client_nom, $rib_banque, $rib_guichet, $rib_number, $amount, $ref, $facid, $rib_dom = '', $type = 'direct-debit')
  1511. {
  1512. // phpcs:enable
  1513. fputs($this->file, "06");
  1514. fputs($this->file, "08"); // Prelevement ordinaire
  1515. fputs($this->file, " "); // Zone Reservee B2
  1516. fputs($this->file, $this->emetteur_ics); // ICS
  1517. // Date d'echeance C1
  1518. fputs($this->file, " ");
  1519. fputs($this->file, dol_print_date($this->date_echeance, "%d%m", 'gmt'));
  1520. fputs($this->file, substr(dol_print_date($this->date_echeance, "%y", 'gmt'), 1));
  1521. // Raison Sociale Destinataire C2
  1522. fputs($this->file, substr(strtoupper($client_nom)." ", 0, 24));
  1523. // Domiciliation facultative D1
  1524. $domiciliation = strtr($rib_dom, array(" " => "-", CHR(13) => " ", CHR(10) => ""));
  1525. fputs($this->file, substr($domiciliation." ", 0, 24));
  1526. // Zone Reservee D2
  1527. fputs($this->file, substr(" ", 0, 8));
  1528. // Code Guichet D3
  1529. fputs($this->file, $rib_guichet);
  1530. // Numero de compte D4
  1531. fputs($this->file, substr("000000000000000".$rib_number, -11));
  1532. // Zone E Montant
  1533. $montant = (round($amount, 2) * 100);
  1534. fputs($this->file, substr("000000000000000".$montant, -16));
  1535. // Libelle F
  1536. fputs($this->file, substr("*_".$ref."_RDVnet".$rowid." ", 0, 31));
  1537. // Code etablissement G1
  1538. fputs($this->file, $rib_banque);
  1539. // Zone Reservee G2
  1540. fputs($this->file, substr(" ", 0, 5));
  1541. fputs($this->file, "\n");
  1542. }
  1543. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1544. /**
  1545. * Write recipient of request (customer)
  1546. *
  1547. * @param string $row_code_client soc.code_client as code,
  1548. * @param string $row_nom pl.client_nom AS name,
  1549. * @param string $row_address soc.address AS adr,
  1550. * @param string $row_zip soc.zip
  1551. * @param string $row_town soc.town
  1552. * @param string $row_country_code c.code AS country,
  1553. * @param string $row_cb pl.code_banque AS cb, Not used for SEPA
  1554. * @param string $row_cg pl.code_guichet AS cg, Not used for SEPA
  1555. * @param string $row_cc pl.number AS cc, Not used for SEPA
  1556. * @param string $row_somme pl.amount AS somme,
  1557. * @param string $row_ref f.ref
  1558. * @param string $row_idfac pf.fk_facture AS idfac,
  1559. * @param string $row_iban rib.iban_prefix AS iban,
  1560. * @param string $row_bic rib.bic AS bic,
  1561. * @param string $row_datec rib.datec,
  1562. * @param string $row_drum rib.rowid used to generate rum
  1563. * @param string $row_rum rib.rum Rum defined on company bank account
  1564. * @param string $type 'direct-debit' or 'bank-transfer'
  1565. * @return string Return string with SEPA part DrctDbtTxInf
  1566. * @see EnregDestinataire()
  1567. */
  1568. 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')
  1569. {
  1570. // phpcs:enable
  1571. global $conf;
  1572. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  1573. $CrLf = "\n";
  1574. $Rowing = sprintf("%010d", $row_idfac);
  1575. // Define value for RUM
  1576. // Example: RUM-CustomerCode-CustomerBankAccountId-01424448606 (note: Date is the timestamp of the date of creation of CustomerBankAccountId)
  1577. $Rum = (empty($row_rum) ? $this->buildRumNumber($row_code_client, $row_datec, $row_drum) : $row_rum);
  1578. // Define date of RUM signature
  1579. $DtOfSgntr = dol_print_date($row_datec, '%Y-%m-%d');
  1580. if ($type != 'bank-transfer') {
  1581. // SEPA Paiement Information of buyer for Direct Debit
  1582. $XML_DEBITOR = '';
  1583. $XML_DEBITOR .= ' <DrctDbtTxInf>'.$CrLf;
  1584. $XML_DEBITOR .= ' <PmtId>'.$CrLf;
  1585. // Add EndToEndId. Must be a unique ID for each payment (for example by including bank, buyer or seller, date, checksum)
  1586. $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; // ISO20022 states that EndToEndId has a MaxLength of 35 characters
  1587. $XML_DEBITOR .= ' </PmtId>'.$CrLf;
  1588. $XML_DEBITOR .= ' <InstdAmt Ccy="EUR">'.round($row_somme, 2).'</InstdAmt>'.$CrLf;
  1589. $XML_DEBITOR .= ' <DrctDbtTx>'.$CrLf;
  1590. $XML_DEBITOR .= ' <MndtRltdInf>'.$CrLf;
  1591. $XML_DEBITOR .= ' <MndtId>'.$Rum.'</MndtId>'.$CrLf;
  1592. $XML_DEBITOR .= ' <DtOfSgntr>'.$DtOfSgntr.'</DtOfSgntr>'.$CrLf;
  1593. $XML_DEBITOR .= ' <AmdmntInd>false</AmdmntInd>'.$CrLf;
  1594. $XML_DEBITOR .= ' </MndtRltdInf>'.$CrLf;
  1595. $XML_DEBITOR .= ' </DrctDbtTx>'.$CrLf;
  1596. $XML_DEBITOR .= ' <DbtrAgt>'.$CrLf;
  1597. $XML_DEBITOR .= ' <FinInstnId>'.$CrLf;
  1598. $XML_DEBITOR .= ' <BIC>'.$row_bic.'</BIC>'.$CrLf;
  1599. $XML_DEBITOR .= ' </FinInstnId>'.$CrLf;
  1600. $XML_DEBITOR .= ' </DbtrAgt>'.$CrLf;
  1601. $XML_DEBITOR .= ' <Dbtr>'.$CrLf;
  1602. $XML_DEBITOR .= ' <Nm>'.dolEscapeXML(strtoupper(dol_string_nospecial(dol_string_unaccent($row_nom), ' '))).'</Nm>'.$CrLf;
  1603. $XML_DEBITOR .= ' <PstlAdr>'.$CrLf;
  1604. $XML_DEBITOR .= ' <Ctry>'.$row_country_code.'</Ctry>'.$CrLf;
  1605. $addressline1 = strtr($row_address, array(CHR(13) => ", ", CHR(10) => ""));
  1606. $addressline2 = strtr($row_zip.(($row_zip && $row_town) ? ' ' : ''.$row_town), array(CHR(13) => ", ", CHR(10) => ""));
  1607. if (trim($addressline1)) {
  1608. $XML_DEBITOR .= ' <AdrLine>'.dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($addressline1), ' '), 70, 'right', 'UTF-8', 1)).'</AdrLine>'.$CrLf;
  1609. }
  1610. if (trim($addressline2)) {
  1611. $XML_DEBITOR .= ' <AdrLine>'.dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($addressline2), ' '), 70, 'right', 'UTF-8', 1)).'</AdrLine>'.$CrLf;
  1612. }
  1613. $XML_DEBITOR .= ' </PstlAdr>'.$CrLf;
  1614. $XML_DEBITOR .= ' </Dbtr>'.$CrLf;
  1615. $XML_DEBITOR .= ' <DbtrAcct>'.$CrLf;
  1616. $XML_DEBITOR .= ' <Id>'.$CrLf;
  1617. $XML_DEBITOR .= ' <IBAN>'.preg_replace('/\s/', '', $row_iban).'</IBAN>'.$CrLf;
  1618. $XML_DEBITOR .= ' </Id>'.$CrLf;
  1619. $XML_DEBITOR .= ' </DbtrAcct>'.$CrLf;
  1620. $XML_DEBITOR .= ' <RmtInf>'.$CrLf;
  1621. // A string with some information on payment - 140 max
  1622. $XML_DEBITOR .= ' <Ustrd>'.(($conf->global->PRELEVEMENT_USTRD != "") ? $conf->global->PRELEVEMENT_USTRD : dol_trunc($row_ref, 135, 'right', 'UTF-8', 1)).'</Ustrd>'.$CrLf; // 140 max
  1623. $XML_DEBITOR .= ' </RmtInf>'.$CrLf;
  1624. $XML_DEBITOR .= ' </DrctDbtTxInf>'.$CrLf;
  1625. return $XML_DEBITOR;
  1626. } else {
  1627. // SEPA Paiement Information of seller for Credit Transfer
  1628. $XML_CREDITOR = '';
  1629. $XML_CREDITOR .= ' <CdtTrfTxInf>'.$CrLf;
  1630. $XML_CREDITOR .= ' <PmtId>'.$CrLf;
  1631. // Add EndToEndId. Must be a unique ID for each payment (for example by including bank, buyer or seller, date, checksum)
  1632. $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; // ISO20022 states that EndToEndId has a MaxLength of 35 characters
  1633. $XML_CREDITOR .= ' </PmtId>'.$CrLf;
  1634. if (!empty($this->sepa_xml_pti_in_ctti)) {
  1635. $XML_CREDITOR .= ' <PmtTpInf>' . $CrLf;
  1636. // Can be 'NORM' for normal or 'HIGH' for high priority level
  1637. if (!empty($conf->global->PAYMENTBYBANKTRANSFER_FORCE_HIGH_PRIORITY)) {
  1638. $instrprty = 'HIGH';
  1639. } else {
  1640. $instrprty = 'NORM';
  1641. }
  1642. $XML_CREDITOR .= ' <InstrPrty>'.$instrprty.'</InstrPrty>' . $CrLf;
  1643. $XML_CREDITOR .= ' <SvcLvl>' . $CrLf;
  1644. $XML_CREDITOR .= ' <Cd>SEPA</Cd>' . $CrLf;
  1645. $XML_CREDITOR .= ' </SvcLvl>' . $CrLf;
  1646. $XML_CREDITOR .= ' <CtgyPurp>' . $CrLf;
  1647. $XML_CREDITOR .= ' <Cd>CORE</Cd>' . $CrLf;
  1648. $XML_CREDITOR .= ' </CtgyPurp>' . $CrLf;
  1649. $XML_CREDITOR .= ' </PmtTpInf>' . $CrLf;
  1650. }
  1651. $XML_CREDITOR .= ' <Amt>'.$CrLf;
  1652. $XML_CREDITOR .= ' <InstdAmt Ccy="EUR">'.round($row_somme, 2).'</InstdAmt>'.$CrLf;
  1653. $XML_CREDITOR .= ' </Amt>'.$CrLf;
  1654. /*
  1655. $XML_CREDITOR .= ' <DrctDbtTx>'.$CrLf;
  1656. $XML_CREDITOR .= ' <MndtRltdInf>'.$CrLf;
  1657. $XML_CREDITOR .= ' <MndtId>'.$Rum.'</MndtId>'.$CrLf;
  1658. $XML_CREDITOR .= ' <DtOfSgntr>'.$DtOfSgntr.'</DtOfSgntr>'.$CrLf;
  1659. $XML_CREDITOR .= ' <AmdmntInd>false</AmdmntInd>'.$CrLf;
  1660. $XML_CREDITOR .= ' </MndtRltdInf>'.$CrLf;
  1661. $XML_CREDITOR .= ' </DrctDbtTx>'.$CrLf;
  1662. */
  1663. //$XML_CREDITOR .= ' <ChrgBr>SLEV</ChrgBr>'.$CrLf;
  1664. $XML_CREDITOR .= ' <CdtrAgt>'.$CrLf;
  1665. $XML_CREDITOR .= ' <FinInstnId>'.$CrLf;
  1666. $XML_CREDITOR .= ' <BIC>'.$row_bic.'</BIC>'.$CrLf;
  1667. $XML_CREDITOR .= ' </FinInstnId>'.$CrLf;
  1668. $XML_CREDITOR .= ' </CdtrAgt>'.$CrLf;
  1669. $XML_CREDITOR .= ' <Cdtr>'.$CrLf;
  1670. $XML_CREDITOR .= ' <Nm>'.dolEscapeXML(strtoupper(dol_string_nospecial(dol_string_unaccent($row_nom), ' '))).'</Nm>'.$CrLf;
  1671. $XML_CREDITOR .= ' <PstlAdr>'.$CrLf;
  1672. $XML_CREDITOR .= ' <Ctry>'.$row_country_code.'</Ctry>'.$CrLf;
  1673. $addressline1 = strtr($row_address, array(CHR(13) => ", ", CHR(10) => ""));
  1674. $addressline2 = strtr($row_zip.(($row_zip && $row_town) ? ' ' : ''.$row_town), array(CHR(13) => ", ", CHR(10) => ""));
  1675. if (trim($addressline1)) {
  1676. $XML_CREDITOR .= ' <AdrLine>'.dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($addressline1), ' '), 70, 'right', 'UTF-8', 1)).'</AdrLine>'.$CrLf;
  1677. }
  1678. if (trim($addressline2)) {
  1679. $XML_CREDITOR .= ' <AdrLine>'.dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($addressline2), ' '), 70, 'right', 'UTF-8', 1)).'</AdrLine>'.$CrLf;
  1680. }
  1681. $XML_CREDITOR .= ' </PstlAdr>'.$CrLf;
  1682. $XML_CREDITOR .= ' </Cdtr>'.$CrLf;
  1683. $XML_CREDITOR .= ' <CdtrAcct>'.$CrLf;
  1684. $XML_CREDITOR .= ' <Id>'.$CrLf;
  1685. $XML_CREDITOR .= ' <IBAN>'.preg_replace('/\s/', '', $row_iban).'</IBAN>'.$CrLf;
  1686. $XML_CREDITOR .= ' </Id>'.$CrLf;
  1687. $XML_CREDITOR .= ' </CdtrAcct>'.$CrLf;
  1688. $XML_CREDITOR .= ' <RmtInf>'.$CrLf;
  1689. // A string with some information on payment - 140 max
  1690. $XML_CREDITOR .= ' <Ustrd>'.(($conf->global->PRELEVEMENT_USTRD != "") ? $conf->global->PRELEVEMENT_USTRD : dol_trunc($row_ref, 135, 'right', 'UTF-8', 1)).'</Ustrd>'.$CrLf; // 140 max
  1691. $XML_CREDITOR .= ' </RmtInf>'.$CrLf;
  1692. $XML_CREDITOR .= ' </CdtTrfTxInf>'.$CrLf;
  1693. return $XML_CREDITOR;
  1694. }
  1695. }
  1696. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1697. /**
  1698. * Write sender of request (me).
  1699. *
  1700. * @param string $type 'direct-debit' or 'bank-transfer'
  1701. * @return void
  1702. * @see EnregEmetteurSEPA()
  1703. */
  1704. public function EnregEmetteur($type = 'direct-debit')
  1705. {
  1706. // phpcs:enable
  1707. fputs($this->file, "03");
  1708. fputs($this->file, "08"); // Prelevement ordinaire
  1709. fputs($this->file, " "); // Zone Reservee B2
  1710. fputs($this->file, $this->emetteur_ics); // ICS
  1711. // Date d'echeance C1
  1712. fputs($this->file, " ");
  1713. fputs($this->file, dol_print_date($this->date_echeance, "%d%m", 'gmt'));
  1714. fputs($this->file, substr(dol_print_date($this->date_echeance, "%y", 'gmt'), 1));
  1715. // Raison Sociale C2
  1716. fputs($this->file, substr($this->raison_sociale." ", 0, 24));
  1717. // Reference de la remise creancier D1 sur 7 caracteres
  1718. fputs($this->file, substr($this->reference_remise." ", 0, 7));
  1719. // Zone Reservee D1-2
  1720. fputs($this->file, substr(" ", 0, 17));
  1721. // Zone Reservee D2
  1722. fputs($this->file, substr(" ", 0, 2));
  1723. fputs($this->file, "E");
  1724. fputs($this->file, substr(" ", 0, 5));
  1725. // Code Guichet D3
  1726. fputs($this->file, $this->emetteur_code_guichet);
  1727. // Numero de compte D4
  1728. fputs($this->file, substr("000000000000000".$this->emetteur_numero_compte, -11));
  1729. // Zone Reservee E
  1730. fputs($this->file, substr(" ", 0, 16));
  1731. // Zone Reservee F
  1732. fputs($this->file, substr(" ", 0, 31));
  1733. // Code etablissement
  1734. fputs($this->file, $this->emetteur_code_banque);
  1735. // Zone Reservee G
  1736. fputs($this->file, substr(" ", 0, 5));
  1737. fputs($this->file, "\n");
  1738. }
  1739. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1740. /**
  1741. * Write sender of request (me).
  1742. * Note: The tag PmtInf is opened here but closed into caller
  1743. *
  1744. * @param Conf $configuration conf
  1745. * @param int $ladate Date
  1746. * @param int $nombre 0 or 1
  1747. * @param float $total Total
  1748. * @param string $CrLf End of line character
  1749. * @param string $format FRST or RCUR or ALL
  1750. * @param string $type 'direct-debit' or 'bank-transfer'
  1751. * @return string String with SEPA Sender
  1752. * @see EnregEmetteur()
  1753. */
  1754. public function EnregEmetteurSEPA($configuration, $ladate, $nombre, $total, $CrLf = '\n', $format = 'FRST', $type = 'direct-debit')
  1755. {
  1756. // phpcs:enable
  1757. // SEPA INITIALISATION
  1758. global $conf;
  1759. $dateTime_YMD = dol_print_date($ladate, '%Y%m%d');
  1760. $dateTime_ETAD = dol_print_date($ladate, '%Y-%m-%d');
  1761. $dateTime_YMDHMS = dol_print_date($ladate, '%Y-%m-%dT%H:%M:%S');
  1762. // Get data of bank account
  1763. //$id = $configuration->global->PRELEVEMENT_ID_BANKACCOUNT;
  1764. $id = ($type == 'bank-transfer' ? $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT : $conf->global->PRELEVEMENT_ID_BANKACCOUNT);
  1765. $account = new Account($this->db);
  1766. if ($account->fetch($id) > 0) {
  1767. $this->emetteur_code_banque = $account->code_banque;
  1768. $this->emetteur_code_guichet = $account->code_guichet;
  1769. $this->emetteur_numero_compte = $account->number;
  1770. $this->emetteur_number_key = $account->cle_rib;
  1771. $this->sepa_xml_pti_in_ctti = (bool) $account->pti_in_ctti;
  1772. $this->emetteur_iban = $account->iban;
  1773. $this->emetteur_bic = $account->bic;
  1774. $this->emetteur_ics = ($type == 'bank-transfer' ? $account->ics_transfer : $account->ics); // Ex: PRELEVEMENT_ICS = "FR78ZZZ123456";
  1775. $this->raison_sociale = $account->proprio;
  1776. }
  1777. // Get pending payments
  1778. $sql = "SELECT rowid, ref";
  1779. $sql .= " FROM";
  1780. $sql .= " ".MAIN_DB_PREFIX."prelevement_bons as pb";
  1781. $sql .= " WHERE pb.rowid = ".((int) $this->id);
  1782. $resql = $this->db->query($sql);
  1783. if ($resql) {
  1784. $obj = $this->db->fetch_object($resql);
  1785. $country = explode(':', $configuration->global->MAIN_INFO_SOCIETE_COUNTRY);
  1786. $IdBon = sprintf("%05d", $obj->rowid);
  1787. $RefBon = $obj->ref;
  1788. if ($type != 'bank-transfer') {
  1789. // SEPA Paiement Information of my company for Direct Debit
  1790. $XML_SEPA_INFO = '';
  1791. $XML_SEPA_INFO .= ' <PmtInf>'.$CrLf;
  1792. $XML_SEPA_INFO .= ' <PmtInfId>'.('DD/'.$dateTime_YMD.'/ID'.$IdBon.'-'.$RefBon).'</PmtInfId>'.$CrLf;
  1793. $XML_SEPA_INFO .= ' <PmtMtd>DD</PmtMtd>'.$CrLf;
  1794. $XML_SEPA_INFO .= ' <NbOfTxs>'.$nombre.'</NbOfTxs>'.$CrLf;
  1795. $XML_SEPA_INFO .= ' <CtrlSum>'.$total.'</CtrlSum>'.$CrLf;
  1796. $XML_SEPA_INFO .= ' <PmtTpInf>'.$CrLf;
  1797. $XML_SEPA_INFO .= ' <SvcLvl>'.$CrLf;
  1798. $XML_SEPA_INFO .= ' <Cd>SEPA</Cd>'.$CrLf;
  1799. $XML_SEPA_INFO .= ' </SvcLvl>'.$CrLf;
  1800. $XML_SEPA_INFO .= ' <LclInstrm>'.$CrLf;
  1801. $XML_SEPA_INFO .= ' <Cd>CORE</Cd>'.$CrLf;
  1802. $XML_SEPA_INFO .= ' </LclInstrm>'.$CrLf;
  1803. $XML_SEPA_INFO .= ' <SeqTp>'.$format.'</SeqTp>'.$CrLf;
  1804. $XML_SEPA_INFO .= ' </PmtTpInf>'.$CrLf;
  1805. $XML_SEPA_INFO .= ' <ReqdColltnDt>'.$dateTime_ETAD.'</ReqdColltnDt>'.$CrLf;
  1806. $XML_SEPA_INFO .= ' <Cdtr>'.$CrLf;
  1807. $XML_SEPA_INFO .= ' <Nm>'.dolEscapeXML(strtoupper(dol_string_nospecial(dol_string_unaccent($this->raison_sociale), ' '))).'</Nm>'.$CrLf;
  1808. $XML_SEPA_INFO .= ' <PstlAdr>'.$CrLf;
  1809. $XML_SEPA_INFO .= ' <Ctry>'.$country[1].'</Ctry>'.$CrLf;
  1810. $addressline1 = strtr($configuration->global->MAIN_INFO_SOCIETE_ADDRESS, array(CHR(13) => ", ", CHR(10) => ""));
  1811. $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) => ""));
  1812. if ($addressline1) {
  1813. $XML_SEPA_INFO .= ' <AdrLine>'.dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($addressline1), ' '), 70, 'right', 'UTF-8', 1)).'</AdrLine>'.$CrLf;
  1814. }
  1815. if ($addressline2) {
  1816. $XML_SEPA_INFO .= ' <AdrLine>'.dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($addressline2), ' '), 70, 'right', 'UTF-8', 1)).'</AdrLine>'.$CrLf;
  1817. }
  1818. $XML_SEPA_INFO .= ' </PstlAdr>'.$CrLf;
  1819. $XML_SEPA_INFO .= ' </Cdtr>'.$CrLf;
  1820. $XML_SEPA_INFO .= ' <CdtrAcct>'.$CrLf;
  1821. $XML_SEPA_INFO .= ' <Id>'.$CrLf;
  1822. $XML_SEPA_INFO .= ' <IBAN>'.preg_replace('/\s/', '', $this->emetteur_iban).'</IBAN>'.$CrLf;
  1823. $XML_SEPA_INFO .= ' </Id>'.$CrLf;
  1824. $XML_SEPA_INFO .= ' </CdtrAcct>'.$CrLf;
  1825. $XML_SEPA_INFO .= ' <CdtrAgt>'.$CrLf;
  1826. $XML_SEPA_INFO .= ' <FinInstnId>'.$CrLf;
  1827. $XML_SEPA_INFO .= ' <BIC>'.$this->emetteur_bic.'</BIC>'.$CrLf;
  1828. $XML_SEPA_INFO .= ' </FinInstnId>'.$CrLf;
  1829. $XML_SEPA_INFO .= ' </CdtrAgt>'.$CrLf;
  1830. /* $XML_SEPA_INFO .= ' <UltmtCdtr>'.$CrLf;
  1831. $XML_SEPA_INFO .= ' <Nm>'.dolEscapeXML(strtoupper(dol_string_nospecial(dol_string_unaccent($this->raison_sociale), ' '))).'</Nm>'.$CrLf;
  1832. $XML_SEPA_INFO .= ' <PstlAdr>'.$CrLf;
  1833. $XML_SEPA_INFO .= ' <Ctry>'.$country[1].'</Ctry>'.$CrLf;
  1834. $XML_SEPA_INFO .= ' <AdrLine>'.dolEscapeXML(dol_string_nospecial(dol_string_unaccent($conf->global->MAIN_INFO_SOCIETE_ADDRESS), ' ')).'</AdrLine>'.$CrLf;
  1835. $XML_SEPA_INFO .= ' <AdrLine>'.dolEscapeXML(dol_string_nospecial(dol_string_unaccent($conf->global->MAIN_INFO_SOCIETE_ZIP.' '.$conf->global->MAIN_INFO_SOCIETE_TOWN), ' ')).'</AdrLine>'.$CrLf;
  1836. $XML_SEPA_INFO .= ' </PstlAdr>'.$CrLf;
  1837. $XML_SEPA_INFO .= ' </UltmtCdtr>'.$CrLf;*/
  1838. $XML_SEPA_INFO .= ' <ChrgBr>SLEV</ChrgBr>'.$CrLf; // Field "Responsible of fees". Must be SLEV
  1839. $XML_SEPA_INFO .= ' <CdtrSchmeId>'.$CrLf;
  1840. $XML_SEPA_INFO .= ' <Id>'.$CrLf;
  1841. $XML_SEPA_INFO .= ' <PrvtId>'.$CrLf;
  1842. $XML_SEPA_INFO .= ' <Othr>'.$CrLf;
  1843. $XML_SEPA_INFO .= ' <Id>'.$this->emetteur_ics.'</Id>'.$CrLf;
  1844. $XML_SEPA_INFO .= ' <SchmeNm>'.$CrLf;
  1845. $XML_SEPA_INFO .= ' <Prtry>SEPA</Prtry>'.$CrLf;
  1846. $XML_SEPA_INFO .= ' </SchmeNm>'.$CrLf;
  1847. $XML_SEPA_INFO .= ' </Othr>'.$CrLf;
  1848. $XML_SEPA_INFO .= ' </PrvtId>'.$CrLf;
  1849. $XML_SEPA_INFO .= ' </Id>'.$CrLf;
  1850. $XML_SEPA_INFO .= ' </CdtrSchmeId>'.$CrLf;
  1851. } else {
  1852. // SEPA Paiement Information of my company for Credit Transfer
  1853. $XML_SEPA_INFO = '';
  1854. $XML_SEPA_INFO .= ' <PmtInf>'.$CrLf;
  1855. $XML_SEPA_INFO .= ' <PmtInfId>'.('TRF/'.$dateTime_YMD.'/ID'.$IdBon.'-'.$RefBon).'</PmtInfId>'.$CrLf;
  1856. $XML_SEPA_INFO .= ' <PmtMtd>TRF</PmtMtd>'.$CrLf;
  1857. //$XML_SEPA_INFO .= ' <BtchBookg>False</BtchBookg>'.$CrLf;
  1858. $XML_SEPA_INFO .= ' <NbOfTxs>'.$nombre.'</NbOfTxs>'.$CrLf;
  1859. $XML_SEPA_INFO .= ' <CtrlSum>'.$total.'</CtrlSum>'.$CrLf;
  1860. if (!empty($this->sepa_xml_pti_in_ctti) && !empty($format)) { // @TODO Using $format (FRST ou RCUR) in a section for a Credit Transfer looks strange.
  1861. $XML_SEPA_INFO .= ' <PmtTpInf>' . $CrLf;
  1862. $XML_SEPA_INFO .= ' <SvcLvl>' . $CrLf;
  1863. $XML_SEPA_INFO .= ' <Cd>SEPA</Cd>' . $CrLf;
  1864. $XML_SEPA_INFO .= ' </SvcLvl>' . $CrLf;
  1865. $XML_SEPA_INFO .= ' <LclInstrm>' . $CrLf;
  1866. $XML_SEPA_INFO .= ' <Cd>CORE</Cd>' . $CrLf;
  1867. $XML_SEPA_INFO .= ' </LclInstrm>' . $CrLf;
  1868. $XML_SEPA_INFO .= ' <SeqTp>' . $format . '</SeqTp>' . $CrLf;
  1869. $XML_SEPA_INFO .= ' </PmtTpInf>' . $CrLf;
  1870. }
  1871. $XML_SEPA_INFO .= ' <ReqdExctnDt>'.dol_print_date($dateTime_ETAD, 'dayrfc').'</ReqdExctnDt>'.$CrLf;
  1872. $XML_SEPA_INFO .= ' <Dbtr>'.$CrLf;
  1873. $XML_SEPA_INFO .= ' <Nm>'.dolEscapeXML(strtoupper(dol_string_nospecial(dol_string_unaccent($this->raison_sociale), ' '))).'</Nm>'.$CrLf;
  1874. $XML_SEPA_INFO .= ' <PstlAdr>'.$CrLf;
  1875. $XML_SEPA_INFO .= ' <Ctry>'.$country[1].'</Ctry>'.$CrLf;
  1876. $addressline1 = strtr($configuration->global->MAIN_INFO_SOCIETE_ADDRESS, array(CHR(13) => ", ", CHR(10) => ""));
  1877. $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) => ""));
  1878. if ($addressline1) {
  1879. $XML_SEPA_INFO .= ' <AdrLine>'.dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($addressline1), ' '), 70, 'right', 'UTF-8', 1)).'</AdrLine>'.$CrLf;
  1880. }
  1881. if ($addressline2) {
  1882. $XML_SEPA_INFO .= ' <AdrLine>'.dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($addressline2), ' '), 70, 'right', 'UTF-8', 1)).'</AdrLine>'.$CrLf;
  1883. }
  1884. $XML_SEPA_INFO .= ' </PstlAdr>'.$CrLf;
  1885. $XML_SEPA_INFO .= ' </Dbtr>'.$CrLf;
  1886. $XML_SEPA_INFO .= ' <DbtrAcct>'.$CrLf;
  1887. $XML_SEPA_INFO .= ' <Id>'.$CrLf;
  1888. $XML_SEPA_INFO .= ' <IBAN>'.preg_replace('/\s/', '', $this->emetteur_iban).'</IBAN>'.$CrLf;
  1889. $XML_SEPA_INFO .= ' </Id>'.$CrLf;
  1890. $XML_SEPA_INFO .= ' </DbtrAcct>'.$CrLf;
  1891. $XML_SEPA_INFO .= ' <DbtrAgt>'.$CrLf;
  1892. $XML_SEPA_INFO .= ' <FinInstnId>'.$CrLf;
  1893. $XML_SEPA_INFO .= ' <BIC>'.$this->emetteur_bic.'</BIC>'.$CrLf;
  1894. $XML_SEPA_INFO .= ' </FinInstnId>'.$CrLf;
  1895. $XML_SEPA_INFO .= ' </DbtrAgt>'.$CrLf;
  1896. /* $XML_SEPA_INFO .= ' <UltmtCdtr>'.$CrLf;
  1897. $XML_SEPA_INFO .= ' <Nm>'.dolEscapeXML(strtoupper(dol_string_nospecial(dol_string_unaccent($this->raison_sociale), ' '))).'</Nm>'.$CrLf;
  1898. $XML_SEPA_INFO .= ' <PstlAdr>'.$CrLf;
  1899. $XML_SEPA_INFO .= ' <Ctry>'.$country[1].'</Ctry>'.$CrLf;
  1900. $XML_SEPA_INFO .= ' <AdrLine>'.dolEscapeXML(dol_string_nospecial(dol_string_unaccent($conf->global->MAIN_INFO_SOCIETE_ADDRESS), ' ')).'</AdrLine>'.$CrLf;
  1901. $XML_SEPA_INFO .= ' <AdrLine>'.dolEscapeXML(dol_string_nospecial(dol_string_unaccent($conf->global->MAIN_INFO_SOCIETE_ZIP.' '.$conf->global->MAIN_INFO_SOCIETE_TOWN), ' ')).'</AdrLine>'.$CrLf;
  1902. $XML_SEPA_INFO .= ' </PstlAdr>'.$CrLf;
  1903. $XML_SEPA_INFO .= ' </UltmtCdtr>'.$CrLf;*/
  1904. $XML_SEPA_INFO .= ' <ChrgBr>SLEV</ChrgBr>'.$CrLf; // Field "Responsible of fees". Must be SLEV
  1905. /*$XML_SEPA_INFO .= ' <CdtrSchmeId>'.$CrLf;
  1906. $XML_SEPA_INFO .= ' <Id>'.$CrLf;
  1907. $XML_SEPA_INFO .= ' <PrvtId>'.$CrLf;
  1908. $XML_SEPA_INFO .= ' <Othr>'.$CrLf;
  1909. $XML_SEPA_INFO .= ' <Id>'.$this->emetteur_ics.'</Id>'.$CrLf;
  1910. $XML_SEPA_INFO .= ' <SchmeNm>'.$CrLf;
  1911. $XML_SEPA_INFO .= ' <Prtry>SEPA</Prtry>'.$CrLf;
  1912. $XML_SEPA_INFO .= ' </SchmeNm>'.$CrLf;
  1913. $XML_SEPA_INFO .= ' </Othr>'.$CrLf;
  1914. $XML_SEPA_INFO .= ' </PrvtId>'.$CrLf;
  1915. $XML_SEPA_INFO .= ' </Id>'.$CrLf;
  1916. $XML_SEPA_INFO .= ' </CdtrSchmeId>'.$CrLf;*/
  1917. }
  1918. } else {
  1919. fputs($this->file, 'INCORRECT EMETTEUR '.$this->raison_sociale.$CrLf);
  1920. $XML_SEPA_INFO = '';
  1921. }
  1922. return $XML_SEPA_INFO;
  1923. }
  1924. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1925. /**
  1926. * Write end
  1927. *
  1928. * @param int $total total amount
  1929. * @return void
  1930. */
  1931. public function EnregTotal($total)
  1932. {
  1933. // phpcs:enable
  1934. fputs($this->file, "08");
  1935. fputs($this->file, "08"); // Prelevement ordinaire
  1936. fputs($this->file, " "); // Zone Reservee B2
  1937. fputs($this->file, $this->emetteur_ics); // ICS
  1938. // Reserve C1
  1939. fputs($this->file, substr(" ", 0, 12));
  1940. // Raison Sociale C2
  1941. fputs($this->file, substr(" ", 0, 24));
  1942. // D1
  1943. fputs($this->file, substr(" ", 0, 24));
  1944. // Zone Reservee D2
  1945. fputs($this->file, substr(" ", 0, 8));
  1946. // Code Guichet D3
  1947. fputs($this->file, substr(" ", 0, 5));
  1948. // Numero de compte D4
  1949. fputs($this->file, substr(" ", 0, 11));
  1950. // Zone E Montant
  1951. $montant = ($total * 100);
  1952. fputs($this->file, substr("000000000000000".$montant, -16));
  1953. // Zone Reservee F
  1954. fputs($this->file, substr(" ", 0, 31));
  1955. // Code etablissement
  1956. fputs($this->file, substr(" ", 0, 5));
  1957. // Zone Reservee F
  1958. fputs($this->file, substr(" ", 0, 5));
  1959. fputs($this->file, "\n");
  1960. }
  1961. /**
  1962. * Return status label of object
  1963. *
  1964. * @param int $mode 0=Label, 1=Picto + label, 2=Picto, 3=Label + Picto
  1965. * @return string Label
  1966. */
  1967. public function getLibStatut($mode = 0)
  1968. {
  1969. return $this->LibStatut($this->statut, $mode);
  1970. }
  1971. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1972. /**
  1973. * Return status label for a status
  1974. *
  1975. * @param int $status Id status
  1976. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  1977. * @return string Label
  1978. */
  1979. public function LibStatut($status, $mode = 0)
  1980. {
  1981. // phpcs:enable
  1982. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  1983. global $langs;
  1984. //$langs->load("mymodule");
  1985. $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('StatusWaiting');
  1986. $this->labelStatus[self::STATUS_TRANSFERED] = $langs->transnoentitiesnoconv('StatusTrans');
  1987. $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('StatusWaiting');
  1988. $this->labelStatusShort[self::STATUS_TRANSFERED] = $langs->transnoentitiesnoconv('StatusTrans');
  1989. if ($this->type == 'bank-transfer') {
  1990. $this->labelStatus[self::STATUS_DEBITED] = $langs->transnoentitiesnoconv('StatusDebited');
  1991. $this->labelStatusShort[self::STATUS_DEBITED] = $langs->transnoentitiesnoconv('StatusDebited');
  1992. } else {
  1993. $this->labelStatus[self::STATUS_CREDITED] = $langs->transnoentitiesnoconv('StatusCredited');
  1994. $this->labelStatusShort[self::STATUS_CREDITED] = $langs->transnoentitiesnoconv('StatusCredited');
  1995. }
  1996. }
  1997. $statusType = 'status1';
  1998. if ($status == self::STATUS_TRANSFERED) {
  1999. $statusType = 'status3';
  2000. }
  2001. if ($status == self::STATUS_CREDITED || $status == self::STATUS_DEBITED) {
  2002. $statusType = 'status6';
  2003. }
  2004. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  2005. }
  2006. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2007. /**
  2008. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  2009. *
  2010. * @param User $user Objet user
  2011. * @param string $mode Mode 'direct_debit' or 'credit_transfer'
  2012. * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
  2013. */
  2014. public function load_board($user, $mode)
  2015. {
  2016. // phpcs:enable
  2017. global $conf, $langs;
  2018. if ($user->socid) {
  2019. return -1; // protection pour eviter appel par utilisateur externe
  2020. }
  2021. /*
  2022. if ($mode == 'direct_debit') {
  2023. $sql = "SELECT b.rowid, f.datedue as datefin";
  2024. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  2025. $sql .= " WHERE f.entity IN (".getEntity('facture').")";
  2026. $sql .= " AND f.total_ttc > 0";
  2027. } else {
  2028. $sql = "SELECT b.rowid, f.datedue as datefin";
  2029. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  2030. $sql .= " WHERE f.entity IN (".getEntity('facture_fourn').")";
  2031. $sql .= " AND f.total_ttc > 0";
  2032. }
  2033. $resql = $this->db->query($sql);
  2034. if ($resql) {
  2035. $langs->load("banks");
  2036. $now = dol_now();
  2037. $response = new WorkboardResponse();
  2038. if ($mode == 'direct_debit') {
  2039. $response->warning_delay = $conf->prelevement->warning_delay / 60 / 60 / 24;
  2040. $response->label = $langs->trans("PendingDirectDebitToComplete");
  2041. $response->labelShort = $langs->trans("PendingDirectDebitToCompleteShort");
  2042. $response->url = DOL_URL_ROOT.'/compta/prelevement/index.php?leftmenu=checks&mainmenu=bank';
  2043. } else {
  2044. $response->warning_delay = $conf->paymentbybanktransfer->warning_delay / 60 / 60 / 24;
  2045. $response->label = $langs->trans("PendingCreditTransferToComplete");
  2046. $response->labelShort = $langs->trans("PendingCreditTransferToCompleteShort");
  2047. $response->url = DOL_URL_ROOT.'/compta/paymentbybanktransfer/index.php?leftmenu=checks&mainmenu=bank';
  2048. }
  2049. $response->img = img_object('', "payment");
  2050. while ($obj = $this->db->fetch_object($resql)) {
  2051. $response->nbtodo++;
  2052. if ($this->db->jdate($obj->datefin) < ($now - $conf->withdraw->warning_delay)) {
  2053. $response->nbtodolate++;
  2054. }
  2055. }
  2056. $response->nbtodo = 0;
  2057. $response->nbtodolate = 0;
  2058. // Return workboard only if quantity is not 0
  2059. if ($response->nbtodo) {
  2060. return $response;
  2061. } else {
  2062. return 0;
  2063. }
  2064. } else {
  2065. dol_print_error($this->db);
  2066. $this->error = $this->db->error();
  2067. return -1;
  2068. }
  2069. */
  2070. return 0;
  2071. }
  2072. }