dolibarr  7.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@capnetworks.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 {
33  var $id;
34  var $db;
35 
36 
43  function __construct($db, $user)
44  {
45  global $langs;
46 
47  $this->db = $db;
48  $this->user = $user;
49 
50  $this->motifs = array();
51  $this->facturer = array();
52 
53  $this->motifs[0] = ""; //$langs->trans("StatusMotif0");
54  $this->motifs[1] = $langs->trans("StatusMotif1");
55  $this->motifs[2] = $langs->trans("StatusMotif2");
56  $this->motifs[3] = $langs->trans("StatusMotif3");
57  $this->motifs[4] = $langs->trans("StatusMotif4");
58  $this->motifs[5] = $langs->trans("StatusMotif5");
59  $this->motifs[6] = $langs->trans("StatusMotif6");
60  $this->motifs[7] = $langs->trans("StatusMotif7");
61  $this->motifs[8] = $langs->trans("StatusMotif8");
62 
63  $this->facturer[0]=$langs->trans("NoInvoiceRefused");
64  $this->facturer[1]=$langs->trans("InvoiceRefused");
65 
66  }
67 
79  function create($user, $id, $motif, $date_rejet, $bonid, $facturation=0)
80  {
81  global $langs,$conf;
82 
83  $error = 0;
84  $this->id = $id;
85  $this->bon_id = $bonid;
86  $now=dol_now();
87 
88  dol_syslog("RejetPrelevement::Create id $id");
89  $bankaccount = $conf->global->PRELEVEMENT_ID_BANKACCOUNT;
90  $facs = $this->getListInvoices(1);
91 
92  $this->db->begin();
93 
94  // Insert refused line into database
95  $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_rejet (";
96  $sql.= "fk_prelevement_lignes";
97  $sql.= ", date_rejet";
98  $sql.= ", motif";
99  $sql.= ", fk_user_creation";
100  $sql.= ", date_creation";
101  $sql.= ", afacturer";
102  $sql.= ") VALUES (";
103  $sql.= $id;
104  $sql.= ", '".$this->db->idate($date_rejet)."'";
105  $sql.= ", ".$motif;
106  $sql.= ", ".$user->id;
107  $sql.= ", '".$this->db->idate($now)."'";
108  $sql.= ", ".$facturation;
109  $sql.= ")";
110 
111  $result=$this->db->query($sql);
112 
113  if (!$result)
114  {
115  dol_syslog("RejetPrelevement::create Erreur 4");
116  dol_syslog("RejetPrelevement::create Erreur 4 $sql");
117  $error++;
118  }
119 
120  // Tag the line to refused
121  $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes ";
122  $sql.= " SET statut = 3";
123  $sql.= " WHERE rowid = ".$id;
124 
125  if (! $this->db->query($sql))
126  {
127  dol_syslog("RejetPrelevement::create Erreur 5");
128  $error++;
129  }
130 
131  $num=count($facs);
132  for ($i = 0; $i < $num; $i++)
133  {
134  $fac = new Facture($this->db);
135  $fac->fetch($facs[$i][0]);
136 
137  // Make a negative payment
138  $pai = new Paiement($this->db);
139 
140  $pai->amounts = array();
141 
142  /*
143  * We replace the comma with a point otherwise some
144  * PHP installs sends only the part integer negative
145  */
146 
147  $pai->amounts[$facs[$i][0]] = price2num($facs[$i][1] * -1);
148  $pai->datepaye = $date_rejet;
149  $pai->paiementid = 3; // type of payment: withdrawal
150  $pai->num_paiement = $fac->ref;
151 
152  if ($pai->create($this->user) < 0) // we call with no_commit
153  {
154  $error++;
155  dol_syslog("RejetPrelevement::Create Error creation payment invoice ".$facs[$i][0]);
156  }
157  else
158  {
159  $result=$pai->addPaymentToBank($user,'payment','(InvoiceRefused)',$bankaccount,'','');
160  if ($result < 0)
161  {
162  dol_syslog("RejetPrelevement::Create AddPaymentToBan Error");
163  $error++;
164  }
165 
166  // Payment validation
167  if ($pai->valide() < 0)
168  {
169  $error++;
170  dol_syslog("RejetPrelevement::Create Error payment validation");
171  }
172 
173  }
174  //Tag invoice as unpaid
175  dol_syslog("RejetPrelevement::Create set_unpaid fac ".$fac->ref);
176 
177  $fac->set_unpaid($user);
178 
179  //TODO: Must be managed by notifications module
180  // Send email to sender of the standing order request
181  $this->_send_email($fac);
182  }
183 
184  if ($error == 0)
185  {
186  dol_syslog("RejetPrelevement::Create Commit");
187  $this->db->commit();
188  }
189  else
190  {
191  dol_syslog("RejetPrelevement::Create Rollback");
192  $this->db->rollback();
193  }
194 
195  }
196 
203  function _send_email($fac)
204  {
205  global $langs;
206 
207  $userid = 0;
208 
209  $sql = "SELECT fk_user_demande";
210  $sql.= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
211  $sql.= " WHERE pfd.fk_prelevement_bons = ".$this->bon_id;
212  $sql.= " AND pfd.fk_facture = ".$fac->id;
213 
214  $resql=$this->db->query($sql);
215  if ($resql)
216  {
217  $num = $this->db->num_rows($resql);
218  if ($num > 0)
219  {
220  $row = $this->db->fetch_row($resql);
221  $userid = $row[0];
222  }
223  }
224  else
225  {
226  dol_syslog("RejetPrelevement::_send_email Erreur lecture user");
227  }
228 
229  if ($userid > 0)
230  {
231  $emuser = new User($this->db);
232  $emuser->fetch($userid);
233 
234  $soc = new Societe($this->db);
235  $soc->fetch($fac->socid);
236 
237  require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
238 
239  $subject = $langs->trans("InfoRejectSubject");
240  $sendto = $emuser->getFullName($langs)." <".$emuser->email.">";
241  $from = $this->user->getFullName($langs)." <".$this->user->email.">";
242  $msgishtml=0;
243 
244  $arr_file = array();
245  $arr_mime = array();
246  $arr_name = array();
247  $facref = $fac->ref;
248  $socname = $soc->name;
249  $amount = price($fac->total_ttc);
250  $userinfo = $this->user->getFullName($langs);
251 
252  $message = $langs->trans("InfoRejectMessage",$facref,$socname, $amount, $userinfo);
253 
254  $mailfile = new CMailFile($subject,$sendto,$from,$message,$arr_file,$arr_mime,$arr_name,'', '', 0, $msgishtml,$this->user->email);
255 
256  $result=$mailfile->sendfile();
257  if ($result)
258  {
259  dol_syslog("RejetPrelevement::_send_email email envoye");
260  }
261  else
262  {
263  dol_syslog("RejetPrelevement::_send_email Erreur envoi email");
264  }
265  }
266  else
267  {
268  dol_syslog("RejetPrelevement::_send_email Userid invalide");
269  }
270  }
271 
279  private function getListInvoices($amounts=0)
280  {
281  global $conf;
282 
283  $arr = array();
284 
285  //Returns all invoices of a withdrawal
286  $sql = "SELECT f.rowid as facid, pl.amount";
287  $sql.= " FROM ".MAIN_DB_PREFIX."prelevement_facture as pf";
288  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON (pf.fk_facture = f.rowid)";
289  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_lignes as pl ON (pf.fk_prelevement_lignes = pl.rowid)";
290  $sql.= " WHERE pf.fk_prelevement_lignes = ".$this->id;
291  $sql.= " AND f.entity = ".$conf->entity;
292 
293  $resql=$this->db->query($sql);
294  if ($resql)
295  {
296  $num = $this->db->num_rows($resql);
297 
298  if ($num)
299  {
300  $i = 0;
301  while ($i < $num)
302  {
303  $row = $this->db->fetch_row($resql);
304  if (!$amounts) $arr[$i] = $row[0];
305  else
306  {
307  $arr[$i] = array(
308  $row[0],
309  $row[1]
310  );
311  }
312  $i++;
313  }
314  }
315  $this->db->free($resql);
316  }
317  else
318  {
319  dol_syslog("getListInvoices", LOG_ERR);
320  }
321 
322  return $arr;
323 
324  }
325 
332  function fetch($rowid)
333  {
334 
335  $sql = "SELECT pr.date_rejet as dr, motif, afacturer";
336  $sql.= " FROM ".MAIN_DB_PREFIX."prelevement_rejet as pr";
337  $sql.= " WHERE pr.fk_prelevement_lignes =".$rowid;
338 
339  $resql=$this->db->query($sql);
340  if ($resql)
341  {
342  if ($this->db->num_rows($resql))
343  {
344  $obj = $this->db->fetch_object($resql);
345 
346  $this->id = $rowid;
347  $this->date_rejet = $this->db->jdate($obj->dr);
348  $this->motif = $this->motifs[$obj->motif];
349  $this->invoicing = $this->facturer[$obj->afacturer];
350 
351  $this->db->free($resql);
352 
353  return 0;
354  }
355  else
356  {
357  dol_syslog("RejetPrelevement::Fetch Erreur rowid=$rowid numrows=0");
358  return -1;
359  }
360  }
361  else
362  {
363  dol_syslog("RejetPrelevement::Fetch Erreur rowid=$rowid");
364  return -2;
365  }
366  }
367 
368 }
369 
fetch($rowid)
Retrieve withdrawal object.
Class to manage Dolibarr users.
Definition: user.class.php:39
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:105
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...
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:1013
Class to manage invoices.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...
Class to manage standing orders rejects.