rejetprelevement.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2021 OpenDsi <support@open-dsi.fr>
  6. * Copyright (C) 2024 Laurent Destailleur <eldy@users.sourceforge.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/compta/prelevement/class/rejetprelevement.class.php
  23. * \ingroup prelevement
  24. * \brief File of class to manage standing orders rejects
  25. */
  26. /**
  27. * Class to manage standing orders rejects
  28. */
  29. class RejetPrelevement
  30. {
  31. /**
  32. * @var int ID
  33. */
  34. public $id;
  35. /**
  36. * @var DoliDB Database handler.
  37. */
  38. public $db;
  39. public $type; //prelevement or bank transfer
  40. public $bon_id;
  41. public $user;
  42. public $date_rejet;
  43. /**
  44. * @var array Reason of error
  45. */
  46. public $motif;
  47. /**
  48. * @var array Label status of invoicing
  49. */
  50. public $invoicing;
  51. /**
  52. * @var array Labels of reason
  53. */
  54. public $motifs;
  55. /**
  56. * @var array Labels of invoicing status
  57. */
  58. public $labelsofinvoicing;
  59. /**
  60. * Constructor
  61. *
  62. * @param DoliDb $db Database handler
  63. * @param User $user Objet user
  64. * @param string $type Type ('direct-debit' for direct debit or 'bank-transfer' for credit transfer)
  65. */
  66. public function __construct($db, $user, $type)
  67. {
  68. global $langs;
  69. $this->db = $db;
  70. $this->user = $user;
  71. $this->type = $type;
  72. $this->motifs = array();
  73. $this->labelsofinvoicing = array();
  74. $this->motifs[0] = ""; //$langs->trans("StatusMotif0");
  75. $this->motifs[1] = $langs->trans("StatusMotif1");
  76. $this->motifs[2] = $langs->trans("StatusMotif2");
  77. $this->motifs[3] = $langs->trans("StatusMotif3");
  78. $this->motifs[4] = $langs->trans("StatusMotif4");
  79. $this->motifs[5] = $langs->trans("StatusMotif5");
  80. $this->motifs[6] = $langs->trans("StatusMotif6");
  81. $this->motifs[7] = $langs->trans("StatusMotif7");
  82. $this->motifs[8] = $langs->trans("StatusMotif8");
  83. $this->labelsofinvoicing[0] = $langs->trans("NoInvoiceRefused");
  84. $this->labelsofinvoicing[1] = $langs->trans("InvoiceRefused");
  85. }
  86. /**
  87. * Create
  88. *
  89. * @param User $user User object
  90. * @param int $id Id
  91. * @param string $motif Motif
  92. * @param int $date_rejet Date rejet
  93. * @param int $bonid Bon id
  94. * @param int $facturation Facturation
  95. * @return void
  96. */
  97. public function create($user, $id, $motif, $date_rejet, $bonid, $facturation = 0)
  98. {
  99. global $langs, $conf;
  100. $error = 0;
  101. $this->id = $id;
  102. $this->bon_id = $bonid;
  103. $now = dol_now();
  104. dol_syslog("RejetPrelevement::Create id $id");
  105. $bankaccount = ($this->type == 'bank-transfer' ? $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT : $conf->global->PRELEVEMENT_ID_BANKACCOUNT);
  106. $facs = $this->getListInvoices(1);
  107. require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/ligneprelevement.class.php';
  108. $lipre = new LignePrelevement($this->db);
  109. $lipre->fetch($id);
  110. $this->db->begin();
  111. // Insert refused line into database
  112. $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_rejet (";
  113. $sql .= "fk_prelevement_lignes";
  114. $sql .= ", date_rejet";
  115. $sql .= ", motif";
  116. $sql .= ", fk_user_creation";
  117. $sql .= ", date_creation";
  118. $sql .= ", afacturer";
  119. $sql .= ") VALUES (";
  120. $sql .= ((int) $id);
  121. $sql .= ", '".$this->db->idate($date_rejet)."'";
  122. $sql .= ", ".((int) $motif);
  123. $sql .= ", ".((int) $user->id);
  124. $sql .= ", '".$this->db->idate($now)."'";
  125. $sql .= ", ".((int) $facturation);
  126. $sql .= ")";
  127. $result = $this->db->query($sql);
  128. if (!$result) {
  129. dol_syslog("RejetPrelevement::create Erreur 4 $sql");
  130. $error++;
  131. }
  132. // Tag the line to refused
  133. $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes ";
  134. $sql .= " SET statut = 3";
  135. $sql .= " WHERE rowid = ".((int) $id);
  136. if (!$this->db->query($sql)) {
  137. dol_syslog("RejetPrelevement::create Erreur 5");
  138. $error++;
  139. }
  140. $num = count($facs);
  141. for ($i = 0; $i < $num; $i++) {
  142. if ($this->type == 'bank-transfer') {
  143. $fac = new FactureFournisseur($this->db);
  144. $pai = new PaiementFourn($this->db);
  145. } else {
  146. $fac = new Facture($this->db);
  147. $pai = new Paiement($this->db);
  148. }
  149. $fac->fetch($facs[$i][0]);
  150. // Make a negative payment
  151. //$pai = new Paiement($this->db);
  152. $pai->amounts = array();
  153. /*
  154. * We replace the comma with a point otherwise some
  155. * PHP installs sends only the part integer negative
  156. */
  157. $pai->amounts[$facs[$i][0]] = price2num($facs[$i][1] * ($this->type == 'bank-transfer' ? 1 : -1));
  158. $pai->datepaye = $date_rejet;
  159. $pai->paiementid = 3; // type of payment: withdrawal
  160. $pai->num_paiement = $fac->ref;
  161. $pai->num_payment = $fac->ref;
  162. $pai->id_prelevement = $this->bon_id;
  163. $pai->num_prelevement = $lipre->bon_ref;
  164. if ($pai->create($this->user) < 0) {
  165. // we call with no_commit
  166. $error++;
  167. dol_syslog("RejetPrelevement::Create Error creation payment invoice ".$facs[$i][0]);
  168. } else {
  169. $result = $pai->addPaymentToBank($user, 'payment', '(InvoiceRefused)', $bankaccount, '', '');
  170. if ($result < 0) {
  171. dol_syslog("RejetPrelevement::Create AddPaymentToBan Error");
  172. $error++;
  173. }
  174. // Payment validation
  175. if ($pai->validate($user) < 0) {
  176. $error++;
  177. dol_syslog("RejetPrelevement::Create Error payment validation");
  178. }
  179. }
  180. //Tag invoice as unpaid
  181. dol_syslog("RejetPrelevement::Create set_unpaid fac ".$fac->ref);
  182. $fac->setUnpaid($user);
  183. //TODO: Must be managed by notifications module
  184. // Send email to sender of the standing order request
  185. $this->_send_email($fac);
  186. }
  187. if ($error == 0) {
  188. dol_syslog("RejetPrelevement::Create Commit");
  189. $this->db->commit();
  190. } else {
  191. dol_syslog("RejetPrelevement::Create Rollback");
  192. $this->db->rollback();
  193. }
  194. }
  195. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  196. /**
  197. * Send email to all users that has asked the withdraw request
  198. *
  199. * @param Facture $fac Invoice object
  200. * @return void
  201. */
  202. private function _send_email($fac)
  203. {
  204. // phpcs:enable
  205. global $langs;
  206. $userid = 0;
  207. $sql = "SELECT fk_user_demande";
  208. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_demande as pfd";
  209. $sql .= " WHERE pfd.fk_prelevement_bons = ".((int) $this->bon_id);
  210. $sql .= " AND pfd.fk_facture".($this->type == 'bank-transfer' ? '_fourn' : '').' = '.((int) $fac->id);
  211. $resql = $this->db->query($sql);
  212. if ($resql) {
  213. $num = $this->db->num_rows($resql);
  214. if ($num > 0) {
  215. $row = $this->db->fetch_row($resql);
  216. $userid = $row[0];
  217. }
  218. } else {
  219. dol_syslog("RejetPrelevement::_send_email Erreur lecture user");
  220. }
  221. if ($userid > 0) {
  222. $emuser = new User($this->db);
  223. $emuser->fetch($userid);
  224. $soc = new Societe($this->db);
  225. $soc->fetch($fac->socid);
  226. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  227. $subject = $langs->transnoentities("InfoRejectSubject");
  228. $sendto = $emuser->getFullName($langs)." <".$emuser->email.">";
  229. $from = $this->user->getFullName($langs)." <".$this->user->email.">";
  230. $msgishtml = 1;
  231. $trackid = 'use'.$emuser->id;
  232. $arr_file = array();
  233. $arr_mime = array();
  234. $arr_name = array();
  235. $facref = $fac->ref;
  236. $socname = $soc->name;
  237. $amount = price($fac->total_ttc);
  238. $userinfo = $this->user->getFullName($langs);
  239. $message = $langs->trans("InfoRejectMessage", $facref, $socname, $amount, $userinfo);
  240. $mailfile = new CMailFile($subject, $sendto, $from, $message, $arr_file, $arr_mime, $arr_name, '', '', 0, $msgishtml, $this->user->email, '', $trackid);
  241. $result = $mailfile->sendfile();
  242. if ($result) {
  243. dol_syslog("RejetPrelevement::_send_email email envoye");
  244. } else {
  245. dol_syslog("RejetPrelevement::_send_email Erreur envoi email");
  246. }
  247. } else {
  248. dol_syslog("RejetPrelevement::_send_email Userid invalide");
  249. }
  250. }
  251. /**
  252. * Retrieve the list of invoices
  253. *
  254. * @param int $amounts If you want to get the amount of the order for each invoice
  255. * @return array Array List of invoices related to the withdrawal line
  256. * @todo A withdrawal line is today linked to one and only one invoice. So the function should return only one object ?
  257. */
  258. private function getListInvoices($amounts = 0)
  259. {
  260. global $conf;
  261. $arr = array();
  262. //Returns all invoices of a withdrawal
  263. $sql = "SELECT f.rowid as facid, pl.amount";
  264. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement as pf";
  265. if ($this->type == 'bank-transfer') {
  266. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON (pf.fk_facture_fourn = f.rowid)";
  267. } else {
  268. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON (pf.fk_facture = f.rowid)";
  269. }
  270. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_lignes as pl ON (pf.fk_prelevement_lignes = pl.rowid)";
  271. $sql .= " WHERE pf.fk_prelevement_lignes = ".((int) $this->id);
  272. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  273. $resql = $this->db->query($sql);
  274. if ($resql) {
  275. $num = $this->db->num_rows($resql);
  276. if ($num) {
  277. $i = 0;
  278. while ($i < $num) {
  279. $row = $this->db->fetch_row($resql);
  280. if (!$amounts) {
  281. $arr[$i] = $row[0];
  282. } else {
  283. $arr[$i] = array(
  284. $row[0],
  285. $row[1]
  286. );
  287. }
  288. $i++;
  289. }
  290. }
  291. $this->db->free($resql);
  292. } else {
  293. dol_syslog("getListInvoices", LOG_ERR);
  294. }
  295. return $arr;
  296. }
  297. /**
  298. * Retrieve withdrawal object
  299. *
  300. * @param int $rowid id of invoice to retrieve
  301. * @return int
  302. */
  303. public function fetch($rowid)
  304. {
  305. $sql = "SELECT pr.date_rejet as dr, motif, afacturer";
  306. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_rejet as pr";
  307. $sql .= " WHERE pr.fk_prelevement_lignes =".((int) $rowid);
  308. $resql = $this->db->query($sql);
  309. if ($resql) {
  310. if ($this->db->num_rows($resql)) {
  311. $obj = $this->db->fetch_object($resql);
  312. $this->id = $rowid;
  313. $this->date_rejet = $this->db->jdate($obj->dr);
  314. $this->motif = $this->motifs[$obj->motif];
  315. $this->invoicing = $this->labelsofinvoicing[$obj->afacturer];
  316. $this->db->free($resql);
  317. return 0;
  318. } else {
  319. dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid." numrows=0");
  320. return -1;
  321. }
  322. } else {
  323. dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid);
  324. return -2;
  325. }
  326. }
  327. }