dolibarr  16.0.5
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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
32 {
36  public $id;
37 
41  public $db;
42 
43  public $type; //prelevement or bank transfer
44 
45 
53  public function __construct($db, $user, $type)
54  {
55  global $langs;
56 
57  $this->db = $db;
58  $this->user = $user;
59  $this->type = $type;
60 
61  $this->motifs = array();
62  $this->facturer = array();
63 
64  $this->motifs[0] = ""; //$langs->trans("StatusMotif0");
65  $this->motifs[1] = $langs->trans("StatusMotif1");
66  $this->motifs[2] = $langs->trans("StatusMotif2");
67  $this->motifs[3] = $langs->trans("StatusMotif3");
68  $this->motifs[4] = $langs->trans("StatusMotif4");
69  $this->motifs[5] = $langs->trans("StatusMotif5");
70  $this->motifs[6] = $langs->trans("StatusMotif6");
71  $this->motifs[7] = $langs->trans("StatusMotif7");
72  $this->motifs[8] = $langs->trans("StatusMotif8");
73 
74  $this->facturer[0] = $langs->trans("NoInvoiceRefused");
75  $this->facturer[1] = $langs->trans("InvoiceRefused");
76  }
77 
89  public function create($user, $id, $motif, $date_rejet, $bonid, $facturation = 0)
90  {
91  global $langs, $conf;
92 
93  $error = 0;
94  $this->id = $id;
95  $this->bon_id = $bonid;
96  $now = dol_now();
97 
98  dol_syslog("RejetPrelevement::Create id $id");
99  $bankaccount = ($this->type == 'bank-transfer' ? $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT : $conf->global->PRELEVEMENT_ID_BANKACCOUNT);
100  $facs = $this->getListInvoices(1);
101 
102  require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/ligneprelevement.class.php';
103  $lipre = new LignePrelevement($this->db);
104  $lipre->fetch($id);
105 
106  $this->db->begin();
107 
108  // Insert refused line into database
109  $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_rejet (";
110  $sql .= "fk_prelevement_lignes";
111  $sql .= ", date_rejet";
112  $sql .= ", motif";
113  $sql .= ", fk_user_creation";
114  $sql .= ", date_creation";
115  $sql .= ", afacturer";
116  $sql .= ") VALUES (";
117  $sql .= ((int) $id);
118  $sql .= ", '".$this->db->idate($date_rejet)."'";
119  $sql .= ", ".((int) $motif);
120  $sql .= ", ".((int) $user->id);
121  $sql .= ", '".$this->db->idate($now)."'";
122  $sql .= ", ".((int) $facturation);
123  $sql .= ")";
124 
125  $result = $this->db->query($sql);
126 
127  if (!$result) {
128  dol_syslog("RejetPrelevement::create Erreur 4");
129  dol_syslog("RejetPrelevement::create Erreur 4 $sql");
130  $error++;
131  }
132 
133  // Tag the line to refused
134  $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes ";
135  $sql .= " SET statut = 3";
136  $sql .= " WHERE rowid = ".((int) $id);
137 
138  if (!$this->db->query($sql)) {
139  dol_syslog("RejetPrelevement::create Erreur 5");
140  $error++;
141  }
142 
143  $num = count($facs);
144  for ($i = 0; $i < $num; $i++) {
145  if ($this->type == 'bank-transfer') {
146  $fac = new FactureFournisseur($this->db);
147  $pai = new PaiementFourn($this->db);
148  } else {
149  $fac = new Facture($this->db);
150  $pai = new Paiement($this->db);
151  }
152 
153  $fac->fetch($facs[$i][0]);
154 
155  // Make a negative payment
156  //$pai = new Paiement($this->db);
157 
158  $pai->amounts = array();
159 
160  /*
161  * We replace the comma with a point otherwise some
162  * PHP installs sends only the part integer negative
163  */
164 
165  $pai->amounts[$facs[$i][0]] = price2num($facs[$i][1] * ($this->type == 'bank-transfer' ? 1 : -1));
166  $pai->datepaye = $date_rejet;
167  $pai->paiementid = 3; // type of payment: withdrawal
168  $pai->num_paiement = $fac->ref;
169  $pai->num_payment = $fac->ref;
170  $pai->id_prelevement = $this->bon_id;
171  $pai->num_prelevement = $lipre->bon_ref;
172 
173  if ($pai->create($this->user) < 0) {
174  // we call with no_commit
175  $error++;
176  dol_syslog("RejetPrelevement::Create Error creation payment invoice ".$facs[$i][0]);
177  } else {
178  $result = $pai->addPaymentToBank($user, 'payment', '(InvoiceRefused)', $bankaccount, '', '');
179  if ($result < 0) {
180  dol_syslog("RejetPrelevement::Create AddPaymentToBan Error");
181  $error++;
182  }
183 
184  // Payment validation
185  if ($pai->validate($user) < 0) {
186  $error++;
187  dol_syslog("RejetPrelevement::Create Error payment validation");
188  }
189  }
190  //Tag invoice as unpaid
191  dol_syslog("RejetPrelevement::Create set_unpaid fac ".$fac->ref);
192 
193  $fac->setUnpaid($user);
194 
195  //TODO: Must be managed by notifications module
196  // Send email to sender of the standing order request
197  $this->_send_email($fac);
198  }
199 
200  if ($error == 0) {
201  dol_syslog("RejetPrelevement::Create Commit");
202  $this->db->commit();
203  } else {
204  dol_syslog("RejetPrelevement::Create Rollback");
205  $this->db->rollback();
206  }
207  }
208 
209  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
216  private function _send_email($fac)
217  {
218  // phpcs:enable
219  global $langs;
220 
221  $userid = 0;
222 
223  $sql = "SELECT fk_user_demande";
224  $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
225  $sql .= " WHERE pfd.fk_prelevement_bons = ".((int) $this->bon_id);
226  $sql .= " AND pfd.fk_facture".($this->type == 'bank-transfer' ? '_fourn' : '').' = '.((int) $fac->id);
227 
228  $resql = $this->db->query($sql);
229  if ($resql) {
230  $num = $this->db->num_rows($resql);
231  if ($num > 0) {
232  $row = $this->db->fetch_row($resql);
233  $userid = $row[0];
234  }
235  } else {
236  dol_syslog("RejetPrelevement::_send_email Erreur lecture user");
237  }
238 
239  if ($userid > 0) {
240  $emuser = new User($this->db);
241  $emuser->fetch($userid);
242 
243  $soc = new Societe($this->db);
244  $soc->fetch($fac->socid);
245 
246  require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
247 
248  $subject = $langs->transnoentities("InfoRejectSubject");
249  $sendto = $emuser->getFullName($langs)." <".$emuser->email.">";
250  $from = $this->user->getFullName($langs)." <".$this->user->email.">";
251  $msgishtml = 1;
252  $trackid = 'use'.$emuser->id;
253 
254  $arr_file = array();
255  $arr_mime = array();
256  $arr_name = array();
257  $facref = $fac->ref;
258  $socname = $soc->name;
259  $amount = price($fac->total_ttc);
260  $userinfo = $this->user->getFullName($langs);
261 
262  $message = $langs->trans("InfoRejectMessage", $facref, $socname, $amount, $userinfo);
263 
264  $mailfile = new CMailFile($subject, $sendto, $from, $message, $arr_file, $arr_mime, $arr_name, '', '', 0, $msgishtml, $this->user->email, '', $trackid);
265 
266  $result = $mailfile->sendfile();
267  if ($result) {
268  dol_syslog("RejetPrelevement::_send_email email envoye");
269  } else {
270  dol_syslog("RejetPrelevement::_send_email Erreur envoi email");
271  }
272  } else {
273  dol_syslog("RejetPrelevement::_send_email Userid invalide");
274  }
275  }
276 
284  private function getListInvoices($amounts = 0)
285  {
286  global $conf;
287 
288  $arr = array();
289 
290  //Returns all invoices of a withdrawal
291  $sql = "SELECT f.rowid as facid, pl.amount";
292  $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture as pf";
293  if ($this->type == 'bank-transfer') {
294  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON (pf.fk_facture_fourn = f.rowid)";
295  } else {
296  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON (pf.fk_facture = f.rowid)";
297  }
298  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_lignes as pl ON (pf.fk_prelevement_lignes = pl.rowid)";
299  $sql .= " WHERE pf.fk_prelevement_lignes = ".((int) $this->id);
300  $sql .= " AND f.entity IN (".getEntity('invoice').")";
301 
302  $resql = $this->db->query($sql);
303  if ($resql) {
304  $num = $this->db->num_rows($resql);
305 
306  if ($num) {
307  $i = 0;
308  while ($i < $num) {
309  $row = $this->db->fetch_row($resql);
310  if (!$amounts) {
311  $arr[$i] = $row[0];
312  } else {
313  $arr[$i] = array(
314  $row[0],
315  $row[1]
316  );
317  }
318  $i++;
319  }
320  }
321  $this->db->free($resql);
322  } else {
323  dol_syslog("getListInvoices", LOG_ERR);
324  }
325 
326  return $arr;
327  }
328 
335  public function fetch($rowid)
336  {
337 
338  $sql = "SELECT pr.date_rejet as dr, motif, afacturer";
339  $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_rejet as pr";
340  $sql .= " WHERE pr.fk_prelevement_lignes =".((int) $rowid);
341 
342  $resql = $this->db->query($sql);
343  if ($resql) {
344  if ($this->db->num_rows($resql)) {
345  $obj = $this->db->fetch_object($resql);
346 
347  $this->id = $rowid;
348  $this->date_rejet = $this->db->jdate($obj->dr);
349  $this->motif = $this->motifs[$obj->motif];
350  $this->invoicing = $this->facturer[$obj->afacturer];
351 
352  $this->db->free($resql);
353 
354  return 0;
355  } else {
356  dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid." numrows=0");
357  return -1;
358  }
359  } else {
360  dol_syslog("RejetPrelevement::Fetch Erreur rowid=".$rowid);
361  return -2;
362  }
363  }
364 }
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
db
$conf db
API class for accounts.
Definition: inc.php:41
LignePrelevement
Class to manage withdrawals.
Definition: ligneprelevement.class.php:32
FactureFournisseur
Class to manage suppliers invoices.
Definition: fournisseur.facture.class.php:53
CMailFile
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Definition: CMailFile.class.php:38
Facture
Class to manage invoices.
Definition: facture.class.php:60
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
RejetPrelevement\_send_email
_send_email($fac)
Send email to all users that has asked the withdraw request.
Definition: rejetprelevement.class.php:216
RejetPrelevement\create
create($user, $id, $motif, $date_rejet, $bonid, $facturation=0)
Create.
Definition: rejetprelevement.class.php:89
RejetPrelevement\getListInvoices
getListInvoices($amounts=0)
Retrieve the list of invoices.
Definition: rejetprelevement.class.php:284
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
RejetPrelevement\fetch
fetch($rowid)
Retrieve withdrawal object.
Definition: rejetprelevement.class.php:335
PaiementFourn
Class to manage payments for supplier invoices.
Definition: paiementfourn.class.php:37
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Paiement
Class to manage payments of customer invoices.
Definition: paiement.class.php:41
RejetPrelevement
Class to manage standing orders rejects.
Definition: rejetprelevement.class.php:31
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
price
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.
Definition: functions.lib.php:5541
user
$conf db user
Definition: repair.php:123
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
RejetPrelevement\__construct
__construct($db, $user, $type)
Constructor.
Definition: rejetprelevement.class.php:53