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