rejetprelevement.class.php 10 KB

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