dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
18  */
19 
31 {
35  public $id;
36 
40  public $db;
41 
42  public $type; //prelevement or bank transfer
43 
44 
52  public function __construct($db, $user, $type)
53  {
54  global $langs;
55 
56  $this->db = $db;
57  $this->user = $user;
58  $this->type = $type;
59 
60  $this->motifs = array();
61  $this->facturer = array();
62 
63  $this->motifs[0] = ""; //$langs->trans("StatusMotif0");
64  $this->motifs[1] = $langs->trans("StatusMotif1");
65  $this->motifs[2] = $langs->trans("StatusMotif2");
66  $this->motifs[3] = $langs->trans("StatusMotif3");
67  $this->motifs[4] = $langs->trans("StatusMotif4");
68  $this->motifs[5] = $langs->trans("StatusMotif5");
69  $this->motifs[6] = $langs->trans("StatusMotif6");
70  $this->motifs[7] = $langs->trans("StatusMotif7");
71  $this->motifs[8] = $langs->trans("StatusMotif8");
72 
73  $this->facturer[0] = $langs->trans("NoInvoiceRefused");
74  $this->facturer[1] = $langs->trans("InvoiceRefused");
75  }
76 
88  public function create($user, $id, $motif, $date_rejet, $bonid, $facturation = 0)
89  {
90  global $langs, $conf;
91 
92  $error = 0;
93  $this->id = $id;
94  $this->bon_id = $bonid;
95  $now = dol_now();
96 
97  dol_syslog("RejetPrelevement::Create id $id");
98  $bankaccount = ($this->type == 'bank-transfer' ? $conf->global->PAYMENTBYBANKTRANSFER_ID_BANKACCOUNT : $conf->global->PRELEVEMENT_ID_BANKACCOUNT);
99  $facs = $this->getListInvoices(1);
100 
101  $this->db->begin();
102 
103  // Insert refused line into database
104  $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_rejet (";
105  $sql .= "fk_prelevement_lignes";
106  $sql .= ", date_rejet";
107  $sql .= ", motif";
108  $sql .= ", fk_user_creation";
109  $sql .= ", date_creation";
110  $sql .= ", afacturer";
111  $sql .= ") VALUES (";
112  $sql .= $id;
113  $sql .= ", '".$this->db->idate($date_rejet)."'";
114  $sql .= ", ".$motif;
115  $sql .= ", ".$user->id;
116  $sql .= ", '".$this->db->idate($now)."'";
117  $sql .= ", ".$facturation;
118  $sql .= ")";
119 
120  $result = $this->db->query($sql);
121 
122  if (!$result)
123  {
124  dol_syslog("RejetPrelevement::create Erreur 4");
125  dol_syslog("RejetPrelevement::create Erreur 4 $sql");
126  $error++;
127  }
128 
129  // Tag the line to refused
130  $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_lignes ";
131  $sql .= " SET statut = 3";
132  $sql .= " WHERE rowid = ".$id;
133 
134  if (!$this->db->query($sql))
135  {
136  dol_syslog("RejetPrelevement::create Erreur 5");
137  $error++;
138  }
139 
140  $num = count($facs);
141  for ($i = 0; $i < $num; $i++) {
142  if ($this->type == 'bank-transfer') {
143  $fac = new FactureFournisseur($this->db);
144  $pai = new PaiementFourn($this->db);
145  } else {
146  $fac = new Facture($this->db);
147  $pai = new Paiement($this->db);
148  }
149 
150  $fac->fetch($facs[$i][0]);
151 
152  // Make a negative payment
153  //$pai = new Paiement($this->db);
154 
155  $pai->amounts = array();
156 
157  /*
158  * We replace the comma with a point otherwise some
159  * PHP installs sends only the part integer negative
160  */
161 
162  $pai->amounts[$facs[$i][0]] = price2num($facs[$i][1] * ($this->type == 'bank-transfer' ? 1 : -1));
163  $pai->datepaye = $date_rejet;
164  $pai->paiementid = 3; // type of payment: withdrawal
165  $pai->num_payment = $fac->ref;
166 
167  if ($pai->create($this->user) < 0) {
168  // we call with no_commit
169  $error++;
170  dol_syslog("RejetPrelevement::Create Error creation payment invoice ".$facs[$i][0]);
171  } else {
172  $result = $pai->addPaymentToBank($user, 'payment', '(InvoiceRefused)', $bankaccount, '', '');
173  if ($result < 0) {
174  dol_syslog("RejetPrelevement::Create AddPaymentToBan Error");
175  $error++;
176  }
177 
178  // Payment validation
179  if ($pai->validate($user) < 0) {
180  $error++;
181  dol_syslog("RejetPrelevement::Create Error payment validation");
182  }
183  }
184  //Tag invoice as unpaid
185  dol_syslog("RejetPrelevement::Create set_unpaid fac ".$fac->ref);
186 
187  $fac->set_unpaid($user);
188 
189  //TODO: Must be managed by notifications module
190  // Send email to sender of the standing order request
191  $this->_send_email($fac);
192  }
193 
194  if ($error == 0)
195  {
196  dol_syslog("RejetPrelevement::Create Commit");
197  $this->db->commit();
198  } else {
199  dol_syslog("RejetPrelevement::Create Rollback");
200  $this->db->rollback();
201  }
202  }
203 
204  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
211  private function _send_email($fac)
212  {
213  // phpcs:enable
214  global $langs;
215 
216  $userid = 0;
217 
218  $sql = "SELECT fk_user_demande";
219  $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
220  $sql .= " WHERE pfd.fk_prelevement_bons = ".$this->bon_id;
221  $sql .= " AND pfd.fk_facture".($this->type == 'bank-transfer' ? '_fourn=' : '=').$fac->id;
222 
223  $resql = $this->db->query($sql);
224  if ($resql)
225  {
226  $num = $this->db->num_rows($resql);
227  if ($num > 0)
228  {
229  $row = $this->db->fetch_row($resql);
230  $userid = $row[0];
231  }
232  } else {
233  dol_syslog("RejetPrelevement::_send_email Erreur lecture user");
234  }
235 
236  if ($userid > 0)
237  {
238  $emuser = new User($this->db);
239  $emuser->fetch($userid);
240 
241  $soc = new Societe($this->db);
242  $soc->fetch($fac->socid);
243 
244  require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
245 
246  $subject = $langs->transnoentities("InfoRejectSubject");
247  $sendto = $emuser->getFullName($langs)." <".$emuser->email.">";
248  $from = $this->user->getFullName($langs)." <".$this->user->email.">";
249  $msgishtml = 1;
250  $trackid = 'use'.$emuser->id;
251 
252  $arr_file = array();
253  $arr_mime = array();
254  $arr_name = array();
255  $facref = $fac->ref;
256  $socname = $soc->name;
257  $amount = price($fac->total_ttc);
258  $userinfo = $this->user->getFullName($langs);
259 
260  $message = $langs->trans("InfoRejectMessage", $facref, $socname, $amount, $userinfo);
261 
262  $mailfile = new CMailFile($subject, $sendto, $from, $message, $arr_file, $arr_mime, $arr_name, '', '', 0, $msgishtml, $this->user->email, '', $trackid);
263 
264  $result = $mailfile->sendfile();
265  if ($result) {
266  dol_syslog("RejetPrelevement::_send_email email envoye");
267  } else {
268  dol_syslog("RejetPrelevement::_send_email Erreur envoi email");
269  }
270  } else {
271  dol_syslog("RejetPrelevement::_send_email Userid invalide");
272  }
273  }
274 
282  private function getListInvoices($amounts = 0)
283  {
284  global $conf;
285 
286  $arr = array();
287 
288  //Returns all invoices of a withdrawal
289  $sql = "SELECT f.rowid as facid, pl.amount";
290  $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture as pf";
291  if ($this->type == 'bank-transfer') $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON (pf.fk_facture_fourn = f.rowid)";
292  else $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON (pf.fk_facture = f.rowid)";
293  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_lignes as pl ON (pf.fk_prelevement_lignes = pl.rowid)";
294  $sql .= " WHERE pf.fk_prelevement_lignes = ".$this->id;
295  $sql .= " AND f.entity IN (".getEntity('invoice').")";
296 
297  $resql = $this->db->query($sql);
298  if ($resql)
299  {
300  $num = $this->db->num_rows($resql);
301 
302  if ($num)
303  {
304  $i = 0;
305  while ($i < $num)
306  {
307  $row = $this->db->fetch_row($resql);
308  if (!$amounts) {
309  $arr[$i] = $row[0];
310  } else {
311  $arr[$i] = array(
312  $row[0],
313  $row[1]
314  );
315  }
316  $i++;
317  }
318  }
319  $this->db->free($resql);
320  } else {
321  dol_syslog("getListInvoices", LOG_ERR);
322  }
323 
324  return $arr;
325  }
326 
333  public function fetch($rowid)
334  {
335 
336  $sql = "SELECT pr.date_rejet as dr, motif, afacturer";
337  $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_rejet as pr";
338  $sql .= " WHERE pr.fk_prelevement_lignes =".$rowid;
339 
340  $resql = $this->db->query($sql);
341  if ($resql)
342  {
343  if ($this->db->num_rows($resql))
344  {
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 }
fetch($rowid)
Retrieve withdrawal object.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
create($user, $id, $motif, $date_rejet, $bonid, $facturation=0)
Create.
Class to manage suppliers invoices.
getListInvoices($amounts=0)
Retrieve the list of invoices.
$conf db
API class for accounts.
Definition: inc.php:54
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 third parties objects (customers, suppliers, prospects...)
__construct($db, $user, $type)
Constructor.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
$conf db user
Definition: repair.php:109
_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,$replyto); $mailfile-&gt;sendfile();.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage payments of customer invoices.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
Class to manage invoices.
Class to manage payments for supplier invoices.
Class to manage standing orders rejects.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105