dolibarr 21.0.0-beta
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
48 public $type; //prelevement or bank transfer
52 public $bon_id;
56 public $user;
60 public $date_rejet;
61
65 public $motif;
69 public $invoicing;
70
74 public $motifs;
78 public $labelsofinvoicing;
79
87 public function __construct($db, $user, $type)
88 {
89 global $langs;
90
91 $this->db = $db;
92 $this->user = $user;
93 $this->type = $type;
94
95 $this->motifs = array();
96 $this->labelsofinvoicing = array();
97
98 $this->motifs[0] = "";
99 $this->motifs[1] = $langs->trans("StatusMotif1");
100 $this->motifs[2] = $langs->trans("StatusMotif2");
101 $this->motifs[3] = $langs->trans("StatusMotif3");
102 $this->motifs[4] = $langs->trans("StatusMotif4");
103 $this->motifs[5] = $langs->trans("StatusMotif5");
104 $this->motifs[6] = $langs->trans("StatusMotif6");
105 $this->motifs[7] = $langs->trans("StatusMotif7");
106 $this->motifs[8] = $langs->trans("StatusMotif8");
107
108 $this->labelsofinvoicing[0] = $langs->trans("NoInvoiceRefused");
109 $this->labelsofinvoicing[1] = $langs->trans("InvoiceRefused");
110 }
111
123 public function create($user, $id, $motif, $date_rejet, $bonid, $facturation = 0)
124 {
125 global $langs;
126
127 $error = 0;
128 $this->id = $id;
129 $this->bon_id = $bonid;
130 $now = dol_now();
131
132 dol_syslog("RejetPrelevement::Create id ".$id);
133
134 $bankaccount = ($this->type == 'bank-transfer' ? getDolGlobalString('PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT') : getDolGlobalString('PRELEVEMENT_ID_BANKACCOUNT'));
135 $facs = $this->getListInvoices(1);
136
137 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/ligneprelevement.class.php';
138 $lipre = new LignePrelevement($this->db);
139 $lipre->fetch($id);
140
141 $this->db->begin();
142
143 // Insert refused line into database
144 $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_rejet (";
145 $sql .= "fk_prelevement_lignes";
146 $sql .= ", date_rejet";
147 $sql .= ", motif";
148 $sql .= ", fk_user_creation";
149 $sql .= ", date_creation";
150 $sql .= ", afacturer";
151 $sql .= ") VALUES (";
152 $sql .= ((int) $id);
153 $sql .= ", '".$this->db->idate($date_rejet)."'";
154 $sql .= ", ".((int) $motif);
155 $sql .= ", ".((int) $user->id);
156 $sql .= ", '".$this->db->idate($now)."'";
157 $sql .= ", ".((int) $facturation);
158 $sql .= ")";
159
160 $result = $this->db->query($sql);
161
162 if (!$result) {
163 dol_syslog("RejetPrelevement::create Erreur 4 $sql");
164 $error++;
165 }
166
167 // Tag the line to refused
168 $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes ";
169 $sql .= " SET statut = 3";
170 $sql .= " WHERE rowid = ".((int) $id);
171
172 if (!$this->db->query($sql)) {
173 dol_syslog("RejetPrelevement::create Erreur 5");
174 $error++;
175 }
176
177 $num = count($facs);
178 for ($i = 0; $i < $num; $i++) {
179 if ($this->type == 'bank-transfer') {
180 $fac = new FactureFournisseur($this->db);
181 $pai = new PaiementFourn($this->db);
182 } else {
183 $fac = new Facture($this->db);
184 $pai = new Paiement($this->db);
185 }
186
187 $fac->fetch($facs[$i][0]);
188
189 $amountrejected = $facs[$i][1];
190
191 // Make a negative payment
192 // Amount must be an array (id of invoice -> amount)
193 $pai->amounts = array();
194 $pai->amounts[$facs[$i][0]] = price2num($amountrejected * -1); // The payment must be negative because it is a refund
195
196 $pai->datepaye = $date_rejet;
197 $pai->paiementid = 3; // type of payment: withdrawal
198 $pai->num_paiement = $langs->trans('Rejection').' '.$fac->ref;
199 $pai->num_payment = $langs->trans('Rejection').' '.$fac->ref;
200 $pai->id_prelevement = $this->bon_id;
201 $pai->num_prelevement = $lipre->bon_ref;
202
203 if ($pai->create($this->user) < 0) {
204 $error++;
205 dol_syslog("RejetPrelevement::Create Error creation payment invoice ".$facs[$i][0]);
206 } else {
207 // We record entry into bank
208 $mode = 'payment';
209 if ($this->type == 'bank-transfer') {
210 $mode = 'payment_supplier';
211 }
212
213 $result = $pai->addPaymentToBank($user, $mode, '(InvoiceRefused)', $bankaccount, '', '');
214 if ($result < 0) {
215 dol_syslog("RejetPrelevement::Create AddPaymentToBan Error");
216 $error++;
217 }
218
219 // Payment validation
220 if ($pai->validate($user) < 0) {
221 $error++;
222 dol_syslog("RejetPrelevement::Create Error payment validation");
223 }
224 }
225 //Tag invoice as unpaid
226 dol_syslog("RejetPrelevement::Create set_unpaid fac ".$fac->ref);
227
228 $fac->setUnpaid($user);
229
230 //TODO: Must be managed by notifications module
231 // Send email to sender of the standing order request
232 $this->_send_email($fac);
233 }
234
235 if ($error == 0) {
236 dol_syslog("RejetPrelevement::Create Commit");
237 $this->db->commit();
238
239 return 1;
240 } else {
241 dol_syslog("RejetPrelevement::Create Rollback");
242 $this->db->rollback();
243
244 return -1;
245 }
246 }
247
248 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
255 private function _send_email($fac)
256 {
257 // phpcs:enable
258 global $langs;
259
260 $userid = 0;
261
262 $sql = "SELECT fk_user_demande";
263 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_demande as pfd";
264 $sql .= " WHERE pfd.fk_prelevement_bons = ".((int) $this->bon_id);
265 $sql .= " AND pfd.fk_facture".($this->type == 'bank-transfer' ? '_fourn' : '').' = '.((int) $fac->id);
266
267 $resql = $this->db->query($sql);
268 if ($resql) {
269 $num = $this->db->num_rows($resql);
270 if ($num > 0) {
271 $row = $this->db->fetch_row($resql);
272 $userid = $row[0];
273 }
274 } else {
275 dol_syslog("RejetPrelevement::_send_email Erreur lecture user");
276 }
277
278 if ($userid > 0) {
279 $emuser = new User($this->db);
280 $emuser->fetch($userid);
281
282 $soc = new Societe($this->db);
283 $soc->fetch($fac->socid);
284
285 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
286
287 $subject = $langs->transnoentities("InfoRejectSubject");
288 $sendto = $emuser->getFullName($langs)." <".$emuser->email.">";
289 $from = $this->user->getFullName($langs)." <".$this->user->email.">";
290 $msgishtml = 1;
291 $trackid = 'use'.$emuser->id;
292
293 $arr_file = array();
294 $arr_mime = array();
295 $arr_name = array();
296 $facref = $fac->ref;
297 $socname = $soc->name;
298 $amount = price($fac->total_ttc);
299 $userinfo = $this->user->getFullName($langs);
300
301 $message = $langs->trans("InfoRejectMessage", $facref, $socname, $amount, $userinfo);
302
303 $mailfile = new CMailFile($subject, $sendto, $from, $message, $arr_file, $arr_mime, $arr_name, '', '', 0, $msgishtml, $this->user->email, '', $trackid);
304
305 $result = $mailfile->sendfile();
306 if ($result) {
307 dol_syslog("RejetPrelevement::_send_email email envoye");
308 } else {
309 dol_syslog("RejetPrelevement::_send_email Erreur envoi email");
310 }
311 } else {
312 dol_syslog("RejetPrelevement::_send_email Userid invalid");
313 }
314 }
315
324 private function getListInvoices($amounts = 0)
325 {
326 global $conf;
327
328 $arr = array();
329
330 //Returns all invoices of a withdrawal
331 $sql = "SELECT f.rowid as facid, pl.amount";
332 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement as pf";
333 if ($this->type == 'bank-transfer') {
334 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON (pf.fk_facture_fourn = f.rowid)";
335 } else {
336 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON (pf.fk_facture = f.rowid)";
337 }
338 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_lignes as pl ON (pf.fk_prelevement_lignes = pl.rowid)";
339 $sql .= " WHERE pf.fk_prelevement_lignes = ".((int) $this->id);
340 $sql .= " AND f.entity IN (".getEntity('invoice').")";
341
342 $resql = $this->db->query($sql);
343 if ($resql) {
344 $num = $this->db->num_rows($resql);
345
346 if ($num) {
347 $i = 0;
348 while ($i < $num) {
349 $row = $this->db->fetch_row($resql);
350 if (!$amounts) {
351 $arr[$i] = $row[0];
352 } else {
353 $arr[$i] = array(
354 $row[0],
355 $row[1]
356 );
357 }
358 $i++;
359 }
360 }
361 $this->db->free($resql);
362 } else {
363 dol_syslog("getListInvoices", LOG_ERR);
364 }
365
366 return $arr;
367 }
368
375 public function fetch($rowid)
376 {
377 $sql = "SELECT pr.date_rejet as dr, motif, afacturer";
378 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_rejet as pr";
379 $sql .= " WHERE pr.fk_prelevement_lignes =".((int) $rowid);
380
381 $resql = $this->db->query($sql);
382 if ($resql) {
383 if ($this->db->num_rows($resql)) {
384 $obj = $this->db->fetch_object($resql);
385
386 $this->id = $rowid;
387 $this->date_rejet = $this->db->jdate($obj->dr);
388 $this->motif = $this->motifs[$obj->motif];
389 $this->invoicing = $this->labelsofinvoicing[$obj->afacturer];
390
391 $this->db->free($resql);
392
393 return 0;
394 } else {
395 dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid." numrows=0");
396 return -1;
397 }
398 } else {
399 dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid);
400 return -2;
401 }
402 }
403}
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:153
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:149