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 *
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] = "";
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 $pai->amounts[$facs[$i][0]] = price2num($amountrejected * -1); // The payment must be negative because it is a refund
182
183 $pai->datepaye = $date_rejet;
184 $pai->paiementid = 3; // type of payment: withdrawal
185 $pai->num_paiement = $langs->trans('Rejection').' '.$fac->ref;
186 $pai->num_payment = $langs->trans('Rejection').' '.$fac->ref;
187 $pai->id_prelevement = $this->bon_id;
188 $pai->num_prelevement = $lipre->bon_ref;
189
190 if ($pai->create($this->user) < 0) {
191 $error++;
192 dol_syslog("RejetPrelevement::Create Error creation payment invoice ".$facs[$i][0]);
193 } else {
194 // We record entry into bank
195 $mode = 'payment';
196 if ($this->type == 'bank-transfer') {
197 $mode = 'payment_supplier';
198 }
199
200 $result = $pai->addPaymentToBank($user, $mode, '(InvoiceRefused)', $bankaccount, '', '');
201 if ($result < 0) {
202 dol_syslog("RejetPrelevement::Create AddPaymentToBan Error");
203 $error++;
204 }
205
206 // Payment validation
207 if ($pai->validate($user) < 0) {
208 $error++;
209 dol_syslog("RejetPrelevement::Create Error payment validation");
210 }
211 }
212 //Tag invoice as unpaid
213 dol_syslog("RejetPrelevement::Create set_unpaid fac ".$fac->ref);
214
215 $fac->setUnpaid($user);
216
217 //TODO: Must be managed by notifications module
218 // Send email to sender of the standing order request
219 $this->_send_email($fac);
220 }
221
222 if ($error == 0) {
223 dol_syslog("RejetPrelevement::Create Commit");
224 $this->db->commit();
225
226 return 1;
227 } else {
228 dol_syslog("RejetPrelevement::Create Rollback");
229 $this->db->rollback();
230
231 return -1;
232 }
233 }
234
235 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
242 private function _send_email($fac)
243 {
244 // phpcs:enable
245 global $langs;
246
247 $userid = 0;
248
249 $sql = "SELECT fk_user_demande";
250 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_demande as pfd";
251 $sql .= " WHERE pfd.fk_prelevement_bons = ".((int) $this->bon_id);
252 $sql .= " AND pfd.fk_facture".($this->type == 'bank-transfer' ? '_fourn' : '').' = '.((int) $fac->id);
253
254 $resql = $this->db->query($sql);
255 if ($resql) {
256 $num = $this->db->num_rows($resql);
257 if ($num > 0) {
258 $row = $this->db->fetch_row($resql);
259 $userid = $row[0];
260 }
261 } else {
262 dol_syslog("RejetPrelevement::_send_email Erreur lecture user");
263 }
264
265 if ($userid > 0) {
266 $emuser = new User($this->db);
267 $emuser->fetch($userid);
268
269 $soc = new Societe($this->db);
270 $soc->fetch($fac->socid);
271
272 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
273
274 $subject = $langs->transnoentities("InfoRejectSubject");
275 $sendto = $emuser->getFullName($langs)." <".$emuser->email.">";
276 $from = $this->user->getFullName($langs)." <".$this->user->email.">";
277 $msgishtml = 1;
278 $trackid = 'use'.$emuser->id;
279
280 $arr_file = array();
281 $arr_mime = array();
282 $arr_name = array();
283 $facref = $fac->ref;
284 $socname = $soc->name;
285 $amount = price($fac->total_ttc);
286 $userinfo = $this->user->getFullName($langs);
287
288 $message = $langs->trans("InfoRejectMessage", $facref, $socname, $amount, $userinfo);
289
290 $mailfile = new CMailFile($subject, $sendto, $from, $message, $arr_file, $arr_mime, $arr_name, '', '', 0, $msgishtml, $this->user->email, '', $trackid);
291
292 $result = $mailfile->sendfile();
293 if ($result) {
294 dol_syslog("RejetPrelevement::_send_email email envoye");
295 } else {
296 dol_syslog("RejetPrelevement::_send_email Erreur envoi email");
297 }
298 } else {
299 dol_syslog("RejetPrelevement::_send_email Userid invalid");
300 }
301 }
302
310 private function getListInvoices($amounts = 0)
311 {
312 global $conf;
313
314 $arr = array();
315
316 //Returns all invoices of a withdrawal
317 $sql = "SELECT f.rowid as facid, pl.amount";
318 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement as pf";
319 if ($this->type == 'bank-transfer') {
320 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON (pf.fk_facture_fourn = f.rowid)";
321 } else {
322 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON (pf.fk_facture = f.rowid)";
323 }
324 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_lignes as pl ON (pf.fk_prelevement_lignes = pl.rowid)";
325 $sql .= " WHERE pf.fk_prelevement_lignes = ".((int) $this->id);
326 $sql .= " AND f.entity IN (".getEntity('invoice').")";
327
328 $resql = $this->db->query($sql);
329 if ($resql) {
330 $num = $this->db->num_rows($resql);
331
332 if ($num) {
333 $i = 0;
334 while ($i < $num) {
335 $row = $this->db->fetch_row($resql);
336 if (!$amounts) {
337 $arr[$i] = $row[0];
338 } else {
339 $arr[$i] = array(
340 $row[0],
341 $row[1]
342 );
343 }
344 $i++;
345 }
346 }
347 $this->db->free($resql);
348 } else {
349 dol_syslog("getListInvoices", LOG_ERR);
350 }
351
352 return $arr;
353 }
354
361 public function fetch($rowid)
362 {
363 $sql = "SELECT pr.date_rejet as dr, motif, afacturer";
364 $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_rejet as pr";
365 $sql .= " WHERE pr.fk_prelevement_lignes =".((int) $rowid);
366
367 $resql = $this->db->query($sql);
368 if ($resql) {
369 if ($this->db->num_rows($resql)) {
370 $obj = $this->db->fetch_object($resql);
371
372 $this->id = $rowid;
373 $this->date_rejet = $this->db->jdate($obj->dr);
374 $this->motif = $this->motifs[$obj->motif];
375 $this->invoicing = $this->labelsofinvoicing[$obj->afacturer];
376
377 $this->db->free($resql);
378
379 return 0;
380 } else {
381 dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid." numrows=0");
382 return -1;
383 }
384 } else {
385 dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid);
386 return -2;
387 }
388 }
389}
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
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