dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 Laurent Destailleur <eldy@users.sourceforge.net>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
34{
38 public $id;
39
43 public $db;
44
45 public $type; //prelevement or bank transfer
46 public $bon_id;
47 public $user;
48 public $date_rejet;
49
53 public $motif;
57 public $invoicing;
58
62 public $motifs;
66 public $labelsofinvoicing;
67
75 public function __construct($db, $user, $type)
76 {
77 global $langs;
78
79 $this->db = $db;
80 $this->user = $user;
81 $this->type = $type;
82
83 $this->motifs = array();
84 $this->labelsofinvoicing = array();
85
86 $this->motifs[0] = "";
87 $this->motifs[1] = $langs->trans("StatusMotif1");
88 $this->motifs[2] = $langs->trans("StatusMotif2");
89 $this->motifs[3] = $langs->trans("StatusMotif3");
90 $this->motifs[4] = $langs->trans("StatusMotif4");
91 $this->motifs[5] = $langs->trans("StatusMotif5");
92 $this->motifs[6] = $langs->trans("StatusMotif6");
93 $this->motifs[7] = $langs->trans("StatusMotif7");
94 $this->motifs[8] = $langs->trans("StatusMotif8");
95
96 $this->labelsofinvoicing[0] = $langs->trans("NoInvoiceRefused");
97 $this->labelsofinvoicing[1] = $langs->trans("InvoiceRefused");
98 }
99
111 public function create($user, $id, $motif, $date_rejet, $bonid, $facturation = 0)
112 {
113 global $langs;
114
115 $error = 0;
116 $this->id = $id;
117 $this->bon_id = $bonid;
118 $now = dol_now();
119
120 dol_syslog("RejetPrelevement::Create id ".$id);
121
122 $bankaccount = ($this->type == 'bank-transfer' ? getDolGlobalString('PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT') : getDolGlobalString('PRELEVEMENT_ID_BANKACCOUNT'));
123 $facs = $this->getListInvoices(1);
124
125 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/ligneprelevement.class.php';
126 $lipre = new LignePrelevement($this->db);
127 $lipre->fetch($id);
128
129 $this->db->begin();
130
131 // Insert refused line into database
132 $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_rejet (";
133 $sql .= "fk_prelevement_lignes";
134 $sql .= ", date_rejet";
135 $sql .= ", motif";
136 $sql .= ", fk_user_creation";
137 $sql .= ", date_creation";
138 $sql .= ", afacturer";
139 $sql .= ") VALUES (";
140 $sql .= ((int) $id);
141 $sql .= ", '".$this->db->idate($date_rejet)."'";
142 $sql .= ", ".((int) $motif);
143 $sql .= ", ".((int) $user->id);
144 $sql .= ", '".$this->db->idate($now)."'";
145 $sql .= ", ".((int) $facturation);
146 $sql .= ")";
147
148 $result = $this->db->query($sql);
149
150 if (!$result) {
151 dol_syslog("RejetPrelevement::create Erreur 4 $sql");
152 $error++;
153 }
154
155 // Tag the line to refused
156 $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes ";
157 $sql .= " SET statut = 3";
158 $sql .= " WHERE rowid = ".((int) $id);
159
160 if (!$this->db->query($sql)) {
161 dol_syslog("RejetPrelevement::create Erreur 5");
162 $error++;
163 }
164
165 $num = count($facs);
166 for ($i = 0; $i < $num; $i++) {
167 if ($this->type == 'bank-transfer') {
168 $fac = new FactureFournisseur($this->db);
169 $pai = new PaiementFourn($this->db);
170 } else {
171 $fac = new Facture($this->db);
172 $pai = new Paiement($this->db);
173 }
174
175 $fac->fetch($facs[$i][0]);
176
177 $amountrejected = $facs[$i][1];
178
179 // Make a negative payment
180 // Amount must be an array (id of invoice -> amount)
181 $pai->amounts = array();
182 $pai->amounts[$facs[$i][0]] = price2num($amountrejected * -1); // The payment must be negative because it is a refund
183
184 $pai->datepaye = $date_rejet;
185 $pai->paiementid = 3; // type of payment: withdrawal
186 $pai->num_paiement = $langs->trans('Rejection').' '.$fac->ref;
187 $pai->num_payment = $langs->trans('Rejection').' '.$fac->ref;
188 $pai->id_prelevement = $this->bon_id;
189 $pai->num_prelevement = $lipre->bon_ref;
190
191 if ($pai->create($this->user) < 0) {
192 $error++;
193 dol_syslog("RejetPrelevement::Create Error creation payment invoice ".$facs[$i][0]);
194 } else {
195 // We record entry into bank
196 $mode = 'payment';
197 if ($this->type == 'bank-transfer') {
198 $mode = 'payment_supplier';
199 }
200
201 $result = $pai->addPaymentToBank($user, $mode, '(InvoiceRefused)', $bankaccount, '', '');
202 if ($result < 0) {
203 dol_syslog("RejetPrelevement::Create AddPaymentToBan Error");
204 $error++;
205 }
206
207 // Payment validation
208 if ($pai->validate($user) < 0) {
209 $error++;
210 dol_syslog("RejetPrelevement::Create Error payment validation");
211 }
212 }
213 //Tag invoice as unpaid
214 dol_syslog("RejetPrelevement::Create set_unpaid fac ".$fac->ref);
215
216 $fac->setUnpaid($user);
217
218 //TODO: Must be managed by notifications module
219 // Send email to sender of the standing order request
220 $this->_send_email($fac);
221 }
222
223 if ($error == 0) {
224 dol_syslog("RejetPrelevement::Create Commit");
225 $this->db->commit();
226
227 return 1;
228 } else {
229 dol_syslog("RejetPrelevement::Create Rollback");
230 $this->db->rollback();
231
232 return -1;
233 }
234 }
235
236 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
243 private function _send_email($fac)
244 {
245 // phpcs:enable
246 global $langs;
247
248 $userid = 0;
249
250 $sql = "SELECT fk_user_demande";
251 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_demande as pfd";
252 $sql .= " WHERE pfd.fk_prelevement_bons = ".((int) $this->bon_id);
253 $sql .= " AND pfd.fk_facture".($this->type == 'bank-transfer' ? '_fourn' : '').' = '.((int) $fac->id);
254
255 $resql = $this->db->query($sql);
256 if ($resql) {
257 $num = $this->db->num_rows($resql);
258 if ($num > 0) {
259 $row = $this->db->fetch_row($resql);
260 $userid = $row[0];
261 }
262 } else {
263 dol_syslog("RejetPrelevement::_send_email Erreur lecture user");
264 }
265
266 if ($userid > 0) {
267 $emuser = new User($this->db);
268 $emuser->fetch($userid);
269
270 $soc = new Societe($this->db);
271 $soc->fetch($fac->socid);
272
273 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
274
275 $subject = $langs->transnoentities("InfoRejectSubject");
276 $sendto = $emuser->getFullName($langs)." <".$emuser->email.">";
277 $from = $this->user->getFullName($langs)." <".$this->user->email.">";
278 $msgishtml = 1;
279 $trackid = 'use'.$emuser->id;
280
281 $arr_file = array();
282 $arr_mime = array();
283 $arr_name = array();
284 $facref = $fac->ref;
285 $socname = $soc->name;
286 $amount = price($fac->total_ttc);
287 $userinfo = $this->user->getFullName($langs);
288
289 $message = $langs->trans("InfoRejectMessage", $facref, $socname, $amount, $userinfo);
290
291 $mailfile = new CMailFile($subject, $sendto, $from, $message, $arr_file, $arr_mime, $arr_name, '', '', 0, $msgishtml, $this->user->email, '', $trackid);
292
293 $result = $mailfile->sendfile();
294 if ($result) {
295 dol_syslog("RejetPrelevement::_send_email email envoye");
296 } else {
297 dol_syslog("RejetPrelevement::_send_email Erreur envoi email");
298 }
299 } else {
300 dol_syslog("RejetPrelevement::_send_email Userid invalid");
301 }
302 }
303
311 private function getListInvoices($amounts = 0)
312 {
313 global $conf;
314
315 $arr = array();
316
317 //Returns all invoices of a withdrawal
318 $sql = "SELECT f.rowid as facid, pl.amount";
319 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement as pf";
320 if ($this->type == 'bank-transfer') {
321 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON (pf.fk_facture_fourn = f.rowid)";
322 } else {
323 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON (pf.fk_facture = f.rowid)";
324 }
325 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_lignes as pl ON (pf.fk_prelevement_lignes = pl.rowid)";
326 $sql .= " WHERE pf.fk_prelevement_lignes = ".((int) $this->id);
327 $sql .= " AND f.entity IN (".getEntity('invoice').")";
328
329 $resql = $this->db->query($sql);
330 if ($resql) {
331 $num = $this->db->num_rows($resql);
332
333 if ($num) {
334 $i = 0;
335 while ($i < $num) {
336 $row = $this->db->fetch_row($resql);
337 if (!$amounts) {
338 $arr[$i] = $row[0];
339 } else {
340 $arr[$i] = array(
341 $row[0],
342 $row[1]
343 );
344 }
345 $i++;
346 }
347 }
348 $this->db->free($resql);
349 } else {
350 dol_syslog("getListInvoices", LOG_ERR);
351 }
352
353 return $arr;
354 }
355
362 public function fetch($rowid)
363 {
364 $sql = "SELECT pr.date_rejet as dr, motif, afacturer";
365 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_rejet as pr";
366 $sql .= " WHERE pr.fk_prelevement_lignes =".((int) $rowid);
367
368 $resql = $this->db->query($sql);
369 if ($resql) {
370 if ($this->db->num_rows($resql)) {
371 $obj = $this->db->fetch_object($resql);
372
373 $this->id = $rowid;
374 $this->date_rejet = $this->db->jdate($obj->dr);
375 $this->motif = $this->motifs[$obj->motif];
376 $this->invoicing = $this->labelsofinvoicing[$obj->afacturer];
377
378 $this->db->free($resql);
379
380 return 0;
381 } else {
382 dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid." numrows=0");
383 return -1;
384 }
385 } else {
386 dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid);
387 return -2;
388 }
389 }
390}
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 a 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
Active Directory does not allow anonymous connections.
Definition repair.php:143
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:139