dolibarr 18.0.6
rejetprelevement.class.php
Go to the documentation of this file.
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
32{
36 public $id;
37
41 public $db;
42
43 public $type; //prelevement or bank transfer
44
45
53 public function __construct($db, $user, $type)
54 {
55 global $langs;
56
57 $this->db = $db;
58 $this->user = $user;
59 $this->type = $type;
60
61 $this->motifs = array();
62 $this->facturer = array();
63
64 $this->motifs[0] = ""; //$langs->trans("StatusMotif0");
65 $this->motifs[1] = $langs->trans("StatusMotif1");
66 $this->motifs[2] = $langs->trans("StatusMotif2");
67 $this->motifs[3] = $langs->trans("StatusMotif3");
68 $this->motifs[4] = $langs->trans("StatusMotif4");
69 $this->motifs[5] = $langs->trans("StatusMotif5");
70 $this->motifs[6] = $langs->trans("StatusMotif6");
71 $this->motifs[7] = $langs->trans("StatusMotif7");
72 $this->motifs[8] = $langs->trans("StatusMotif8");
73
74 $this->facturer[0] = $langs->trans("NoInvoiceRefused");
75 $this->facturer[1] = $langs->trans("InvoiceRefused");
76 }
77
89 public function create($user, $id, $motif, $date_rejet, $bonid, $facturation = 0)
90 {
91 global $langs;
92
93 $error = 0;
94 $this->id = $id;
95 $this->bon_id = $bonid;
96 $now = dol_now();
97
98 dol_syslog("RejetPrelevement::Create id ".$id);
99
100 $bankaccount = ($this->type == 'bank-transfer' ? getDolGlobalString('PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT') : getDolGlobalString('PRELEVEMENT_ID_BANKACCOUNT'));
101 $facs = $this->getListInvoices(1);
102
103 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/ligneprelevement.class.php';
104 $lipre = new LignePrelevement($this->db);
105 $lipre->fetch($id);
106
107 $this->db->begin();
108
109 // Insert refused line into database
110 $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_rejet (";
111 $sql .= "fk_prelevement_lignes";
112 $sql .= ", date_rejet";
113 $sql .= ", motif";
114 $sql .= ", fk_user_creation";
115 $sql .= ", date_creation";
116 $sql .= ", afacturer";
117 $sql .= ") VALUES (";
118 $sql .= ((int) $id);
119 $sql .= ", '".$this->db->idate($date_rejet)."'";
120 $sql .= ", ".((int) $motif);
121 $sql .= ", ".((int) $user->id);
122 $sql .= ", '".$this->db->idate($now)."'";
123 $sql .= ", ".((int) $facturation);
124 $sql .= ")";
125
126 $result = $this->db->query($sql);
127
128 if (!$result) {
129 dol_syslog("RejetPrelevement::create Erreur 4");
130 dol_syslog("RejetPrelevement::create Erreur 4 $sql");
131 $error++;
132 }
133
134 // Tag the line to refused
135 $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes ";
136 $sql .= " SET statut = 3";
137 $sql .= " WHERE rowid = ".((int) $id);
138
139 if (!$this->db->query($sql)) {
140 dol_syslog("RejetPrelevement::create Erreur 5");
141 $error++;
142 }
143
144 $num = count($facs);
145 for ($i = 0; $i < $num; $i++) {
146 if ($this->type == 'bank-transfer') {
147 $fac = new FactureFournisseur($this->db);
148 $pai = new PaiementFourn($this->db);
149 } else {
150 $fac = new Facture($this->db);
151 $pai = new Paiement($this->db);
152 }
153
154 $fac->fetch($facs[$i][0]);
155
156 $amountrejected = $facs[$i][1];
157
158 // Make a negative payment
159 // Amount must be an array (id of invoice -> amount)
160 $pai->amounts = array();
161
162 //var_dump($this->type);exit;
163
164 $pai->amounts[$facs[$i][0]] = price2num($amountrejected * -1); // The payment must be negative because it is a refund
165
166 $pai->datepaye = $date_rejet;
167 $pai->paiementid = 3; // type of payment: withdrawal
168 $pai->num_paiement = $fac->ref;
169 $pai->num_payment = $fac->ref;
170 $pai->id_prelevement = $this->bon_id;
171 $pai->num_prelevement = $lipre->bon_ref;
172
173 if ($pai->create($this->user) < 0) {
174 // we call with no_commit
175 $error++;
176 dol_syslog("RejetPrelevement::Create Error creation payment invoice ".$facs[$i][0]);
177 } else {
178 // We record entry into bank
179 $mode = 'payment';
180 if ($this->type == 'bank-transfer') {
181 $mode = 'payment_supplier';
182 }
183
184 $result = $pai->addPaymentToBank($user, $mode, '(InvoiceRefused)', $bankaccount, '', '');
185 if ($result < 0) {
186 dol_syslog("RejetPrelevement::Create AddPaymentToBan Error");
187 $error++;
188 }
189
190 // Payment validation
191 if ($pai->validate($user) < 0) {
192 $error++;
193 dol_syslog("RejetPrelevement::Create Error payment validation");
194 }
195 }
196 //Tag invoice as unpaid
197 dol_syslog("RejetPrelevement::Create set_unpaid fac ".$fac->ref);
198
199 $fac->setUnpaid($user);
200
201 //TODO: Must be managed by notifications module
202 // Send email to sender of the standing order request
203 $this->_send_email($fac);
204 }
205
206 if ($error == 0) {
207 dol_syslog("RejetPrelevement::Create Commit");
208 $this->db->commit();
209
210 return 1;
211 } else {
212 dol_syslog("RejetPrelevement::Create Rollback");
213 $this->db->rollback();
214
215 return -1;
216 }
217 }
218
219 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
226 private function _send_email($fac)
227 {
228 // phpcs:enable
229 global $langs;
230
231 $userid = 0;
232
233 $sql = "SELECT fk_user_demande";
234 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_demande as pfd";
235 $sql .= " WHERE pfd.fk_prelevement_bons = ".((int) $this->bon_id);
236 $sql .= " AND pfd.fk_facture".($this->type == 'bank-transfer' ? '_fourn' : '').' = '.((int) $fac->id);
237
238 $resql = $this->db->query($sql);
239 if ($resql) {
240 $num = $this->db->num_rows($resql);
241 if ($num > 0) {
242 $row = $this->db->fetch_row($resql);
243 $userid = $row[0];
244 }
245 } else {
246 dol_syslog("RejetPrelevement::_send_email Erreur lecture user");
247 }
248
249 if ($userid > 0) {
250 $emuser = new User($this->db);
251 $emuser->fetch($userid);
252
253 $soc = new Societe($this->db);
254 $soc->fetch($fac->socid);
255
256 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
257
258 $subject = $langs->transnoentities("InfoRejectSubject");
259 $sendto = $emuser->getFullName($langs)." <".$emuser->email.">";
260 $from = $this->user->getFullName($langs)." <".$this->user->email.">";
261 $msgishtml = 1;
262 $trackid = 'use'.$emuser->id;
263
264 $arr_file = array();
265 $arr_mime = array();
266 $arr_name = array();
267 $facref = $fac->ref;
268 $socname = $soc->name;
269 $amount = price($fac->total_ttc);
270 $userinfo = $this->user->getFullName($langs);
271
272 $message = $langs->trans("InfoRejectMessage", $facref, $socname, $amount, $userinfo);
273
274 $mailfile = new CMailFile($subject, $sendto, $from, $message, $arr_file, $arr_mime, $arr_name, '', '', 0, $msgishtml, $this->user->email, '', $trackid);
275
276 $result = $mailfile->sendfile();
277 if ($result) {
278 dol_syslog("RejetPrelevement::_send_email email envoye");
279 } else {
280 dol_syslog("RejetPrelevement::_send_email Erreur envoi email");
281 }
282 } else {
283 dol_syslog("RejetPrelevement::_send_email Userid invalide");
284 }
285 }
286
294 private function getListInvoices($amounts = 0)
295 {
296 global $conf;
297
298 $arr = array();
299
300 //Returns all invoices of a withdrawal
301 $sql = "SELECT f.rowid as facid, pl.amount";
302 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement as pf";
303 if ($this->type == 'bank-transfer') {
304 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON (pf.fk_facture_fourn = f.rowid)";
305 } else {
306 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON (pf.fk_facture = f.rowid)";
307 }
308 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_lignes as pl ON (pf.fk_prelevement_lignes = pl.rowid)";
309 $sql .= " WHERE pf.fk_prelevement_lignes = ".((int) $this->id);
310 $sql .= " AND f.entity IN (".getEntity('invoice').")";
311
312 $resql = $this->db->query($sql);
313 if ($resql) {
314 $num = $this->db->num_rows($resql);
315
316 if ($num) {
317 $i = 0;
318 while ($i < $num) {
319 $row = $this->db->fetch_row($resql);
320 if (!$amounts) {
321 $arr[$i] = $row[0];
322 } else {
323 $arr[$i] = array(
324 $row[0],
325 $row[1]
326 );
327 }
328 $i++;
329 }
330 }
331 $this->db->free($resql);
332 } else {
333 dol_syslog("getListInvoices", LOG_ERR);
334 }
335
336 return $arr;
337 }
338
345 public function fetch($rowid)
346 {
347
348 $sql = "SELECT pr.date_rejet as dr, motif, afacturer";
349 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_rejet as pr";
350 $sql .= " WHERE pr.fk_prelevement_lignes =".((int) $rowid);
351
352 $resql = $this->db->query($sql);
353 if ($resql) {
354 if ($this->db->num_rows($resql)) {
355 $obj = $this->db->fetch_object($resql);
356
357 $this->id = $rowid;
358 $this->date_rejet = $this->db->jdate($obj->dr);
359 $this->motif = $this->motifs[$obj->motif];
360 $this->invoicing = $this->facturer[$obj->afacturer];
361
362 $this->db->free($resql);
363
364 return 0;
365 } else {
366 dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid." numrows=0");
367 return -1;
368 }
369 } else {
370 dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid);
371 return -2;
372 }
373 }
374}
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class to manage suppliers invoices.
Class to manage invoices.
Class to manage withdrawals.
Class to manage payments for supplier invoices.
Class to manage payments of customer invoices.
Class to manage standing orders rejects.
_send_email($fac)
Send email to all users that has asked the withdraw request.
getListInvoices($amounts=0)
Retrieve the list of invoices.
__construct($db, $user, $type)
Constructor.
create($user, $id, $motif, $date_rejet, $bonid, $facturation=0)
Create a reject.
fetch($rowid)
Retrieve withdrawal object.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db user
Definition repair.php:124
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:120