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