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