dolibarr  13.0.2
paymentexpensereport.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
3  * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26 
27 
32 {
36  public $element = 'payment_expensereport';
37 
41  public $table_element = 'payment_expensereport';
42 
46  public $picto = 'payment';
47 
51  public $rowid;
52 
56  public $fk_expensereport;
57 
58  public $datec = '';
59  public $tms = '';
60  public $datep = '';
61  public $amount; // Total amount of payment
62  public $amounts = array(); // Array of amounts
63 
67  public $fk_typepayment;
68 
69  public $num_payment;
70 
74  public $fk_bank;
75 
79  public $fk_user_creat;
80 
84  public $fk_user_modif;
85 
86  public $type_code;
87  public $type_label;
88 
89 
95  public function __construct($db)
96  {
97  $this->db = $db;
98  }
99 
107  public function create($user)
108  {
109  global $conf, $langs;
110 
111  $error = 0;
112 
113  $now = dol_now();
114 
115  // Validate parameters
116  if (!$this->datepaid) {
117  $this->error = 'ErrorBadValueForParameterCreatePaymentExpenseReport';
118  return -1;
119  }
120 
121  // Clean parameters
122  if (isset($this->fk_expensereport)) $this->fk_expensereport = trim($this->fk_expensereport);
123  if (isset($this->amount)) $this->amount = trim($this->amount);
124  if (isset($this->fk_typepayment)) $this->fk_typepayment = trim($this->fk_typepayment);
125  if (isset($this->num_payment)) $this->num_payment = trim($this->num_payment);
126  if (isset($this->note)) $this->note = trim($this->note);
127  if (isset($this->note_public)) $this->note_public = trim($this->note_public);
128  if (isset($this->fk_bank)) $this->fk_bank = trim($this->fk_bank);
129  if (isset($this->fk_user_creat)) $this->fk_user_creat = trim($this->fk_user_creat);
130  if (isset($this->fk_user_modif)) $this->fk_user_modif = trim($this->fk_user_modif);
131 
132  $totalamount = 0;
133  foreach ($this->amounts as $key => $value) // How payment is dispatch
134  {
135  $newvalue = price2num($value, 'MT');
136  $this->amounts[$key] = $newvalue;
137  $totalamount += $newvalue;
138  }
139  $totalamount = price2num($totalamount);
140 
141  // Check parameters
142  if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
143 
144 
145  $this->db->begin();
146 
147  if ($totalamount != 0)
148  {
149  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_expensereport (fk_expensereport, datec, datep, amount,";
150  $sql .= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)";
151  $sql .= " VALUES ($this->fk_expensereport, '".$this->db->idate($now)."',";
152  $sql .= " '".$this->db->idate($this->datepaid)."',";
153  $sql .= " ".$totalamount.",";
154  $sql .= " ".$this->fk_typepayment.", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ".$user->id.",";
155  $sql .= " 0)";
156 
157  dol_syslog(get_class($this)."::create", LOG_DEBUG);
158  $resql = $this->db->query($sql);
159  if ($resql)
160  {
161  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_expensereport");
162  } else {
163  $error++;
164  }
165  }
166 
167  if ($totalamount != 0 && !$error)
168  {
169  $this->amount = $totalamount;
170  $this->db->commit();
171  return $this->id;
172  } else {
173  $this->error = $this->db->error();
174  $this->db->rollback();
175  return -1;
176  }
177  }
178 
185  public function fetch($id)
186  {
187  $sql = "SELECT";
188  $sql .= " t.rowid,";
189  $sql .= " t.fk_expensereport,";
190  $sql .= " t.datec,";
191  $sql .= " t.tms,";
192  $sql .= " t.datep,";
193  $sql .= " t.amount,";
194  $sql .= " t.fk_typepayment,";
195  $sql .= " t.num_payment,";
196  $sql .= " t.note as note_public,";
197  $sql .= " t.fk_bank,";
198  $sql .= " t.fk_user_creat,";
199  $sql .= " t.fk_user_modif,";
200  $sql .= " pt.code as type_code, pt.libelle as type_label,";
201  $sql .= ' b.fk_account';
202  $sql .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as t";
203  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
204  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
205  $sql .= " WHERE t.rowid = ".$id;
206 
207  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
208  $resql = $this->db->query($sql);
209  if ($resql)
210  {
211  if ($this->db->num_rows($resql))
212  {
213  $obj = $this->db->fetch_object($resql);
214 
215  $this->id = $obj->rowid;
216  $this->ref = $obj->rowid;
217 
218  $this->fk_expensereport = $obj->fk_expensereport;
219  $this->datec = $this->db->jdate($obj->datec);
220  $this->tms = $this->db->jdate($obj->tms);
221  $this->datep = $this->db->jdate($obj->datep);
222  $this->amount = $obj->amount;
223  $this->fk_typepayment = $obj->fk_typepayment;
224  $this->num_payment = $obj->num_payment;
225  $this->note_public = $obj->note_public;
226  $this->fk_bank = $obj->fk_bank;
227  $this->fk_user_creat = $obj->fk_user_creat;
228  $this->fk_user_modif = $obj->fk_user_modif;
229 
230  $this->type_code = $obj->type_code;
231  $this->type_label = $obj->type_label;
232 
233  $this->bank_account = $obj->fk_account;
234  $this->bank_line = $obj->fk_bank;
235  }
236  $this->db->free($resql);
237 
238  return 1;
239  } else {
240  $this->error = "Error ".$this->db->lasterror();
241  return -1;
242  }
243  }
244 
245  // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
253  public function update($user = null, $notrigger = 0)
254  {
255  // phpcs:enable
256  global $conf, $langs;
257  $error = 0;
258 
259  // Clean parameters
260 
261  if (isset($this->fk_expensereport)) $this->fk_expensereport = trim($this->fk_expensereport);
262  if (isset($this->amount)) $this->amount = trim($this->amount);
263  if (isset($this->fk_typepayment)) $this->fk_typepayment = trim($this->fk_typepayment);
264  if (isset($this->num_payment)) $this->num_payment = trim($this->num_payment);
265  if (isset($this->note)) $this->note = trim($this->note);
266  if (isset($this->fk_bank)) $this->fk_bank = trim($this->fk_bank);
267  if (isset($this->fk_user_creat)) $this->fk_user_creat = trim($this->fk_user_creat);
268  if (isset($this->fk_user_modif)) $this->fk_user_modif = trim($this->fk_user_modif);
269 
270 
271  // Check parameters
272  // Put here code to add control on parameters values
273 
274  // Update request
275  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET";
276 
277  $sql .= " fk_expensereport=".(isset($this->fk_expensereport) ? $this->fk_expensereport : "null").",";
278  $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
279  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
280  $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
281  $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
282  $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
283  $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
284  $sql .= " note=".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : "null").",";
285  $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
286  $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
287  $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null")."";
288 
289 
290  $sql .= " WHERE rowid=".$this->id;
291 
292  $this->db->begin();
293 
294  dol_syslog(get_class($this)."::update", LOG_DEBUG);
295  $resql = $this->db->query($sql);
296  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
297 
298  // Commit or rollback
299  if ($error)
300  {
301  foreach ($this->errors as $errmsg)
302  {
303  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
304  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
305  }
306  $this->db->rollback();
307  return -1 * $error;
308  } else {
309  $this->db->commit();
310  return 1;
311  }
312  }
313 
314  // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
322  public function delete($user, $notrigger = 0)
323  {
324  // phpcs:enable
325  global $conf, $langs;
326  $error = 0;
327 
328  $this->db->begin();
329 
330  if (!$error)
331  {
332  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
333  $sql .= " WHERE type='payment_expensereport' AND url_id=".$this->id;
334 
335  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
336  $resql = $this->db->query($sql);
337  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
338  }
339 
340  if (!$error)
341  {
342  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_expensereport";
343  $sql .= " WHERE rowid=".$this->id;
344 
345  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
346  $resql = $this->db->query($sql);
347  if (!$resql) {
348  $error++;
349  $this->errors[] = "Error ".$this->db->lasterror();
350  }
351  }
352 
353  // Commit or rollback
354  if ($error)
355  {
356  foreach ($this->errors as $errmsg)
357  {
358  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
359  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
360  }
361  $this->db->rollback();
362  return -1 * $error;
363  } else {
364  $this->db->commit();
365  return 1;
366  }
367  }
368 
369 
370 
378  public function createFromClone(User $user, $fromid)
379  {
380  $error = 0;
381 
382  $object = new PaymentExpenseReport($this->db);
383 
384  $this->db->begin();
385 
386  // Load source object
387  $object->fetch($fromid);
388  $object->id = 0;
389  $object->statut = 0;
390 
391  // Clear fields
392  // ...
393 
394  // Create clone
395  $object->context['createfromclone'] = 'createfromclone';
396  $result = $object->create($user);
397 
398  // Other options
399  if ($result < 0)
400  {
401  $this->error = $object->error;
402  $error++;
403  }
404 
405  unset($object->context['createfromclone']);
406 
407  // End
408  if (!$error)
409  {
410  $this->db->commit();
411  return $object->id;
412  } else {
413  $this->db->rollback();
414  return -1;
415  }
416  }
417 
418 
425  public function getLibStatut($mode = 0)
426  {
427  return '';
428  }
429 
430  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
438  public function LibStatut($status, $mode = 0)
439  {
440  // phpcs:enable
441  global $langs;
442 
443  return '';
444  }
445 
446 
454  public function initAsSpecimen()
455  {
456  $this->id = 0;
457 
458  $this->fk_expensereport = '';
459  $this->datec = '';
460  $this->tms = '';
461  $this->datep = '';
462  $this->amount = '';
463  $this->fk_typepayment = '';
464  $this->num_payment = '';
465  $this->note = '';
466  $this->fk_bank = '';
467  $this->fk_user_creat = '';
468  $this->fk_user_modif = '';
469  }
470 
471 
484  public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
485  {
486  global $langs, $conf;
487 
488  $error = 0;
489 
490  if (!empty($conf->banque->enabled))
491  {
492  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
493 
494  $acc = new Account($this->db);
495  $acc->fetch($accountid);
496 
497  //Fix me field
498  $total = $this->amount;
499 
500  if ($mode == 'payment_expensereport') $amount = $total;
501 
502  // Insert payment into llx_bank
503  $bank_line_id = $acc->addline(
504  $this->datepaid,
505  $this->fk_typepayment, // Payment mode id or code ("CHQ or VIR for example")
506  $label,
507  -$amount,
508  $this->num_payment,
509  '',
510  $user,
511  $emetteur_nom,
512  $emetteur_banque
513  );
514 
515  // Update fk_bank in llx_paiement.
516  // On connait ainsi le paiement qui a genere l'ecriture bancaire
517  if ($bank_line_id > 0)
518  {
519  $result = $this->update_fk_bank($bank_line_id);
520  if ($result <= 0)
521  {
522  $error++;
523  dol_print_error($this->db);
524  }
525 
526  // Add link 'payment', 'payment_supplier', 'payment_expensereport' in bank_url between payment and bank transaction
527  $url = '';
528  if ($mode == 'payment_expensereport') $url = DOL_URL_ROOT.'/expensereport/payment/card.php?rowid=';
529  if ($url)
530  {
531  $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
532  if ($result <= 0)
533  {
534  $error++;
535  dol_print_error($this->db);
536  }
537  }
538 
539  // Add link 'user' in bank_url between user and bank transaction
540  if (!$error)
541  {
542  foreach ($this->amounts as $key => $value) // We should have always same user but we loop in case of.
543  {
544  if ($mode == 'payment_expensereport')
545  {
546  $fuser = new User($this->db);
547  $fuser->fetch($key);
548 
549  $result = $acc->add_url_line(
550  $bank_line_id,
551  $fuser->id,
552  DOL_URL_ROOT.'/user/card.php?id=',
553  $fuser->getFullName($langs),
554  'user'
555  );
556  if ($result <= 0)
557  {
558  $this->error = $this->db->lasterror();
559  dol_syslog(get_class($this).'::addPaymentToBank '.$this->error);
560  $error++;
561  }
562  }
563  }
564  }
565  } else {
566  $this->error = $acc->error;
567  $error++;
568  }
569  }
570 
571  if (!$error)
572  {
573  return 1;
574  } else {
575  return -1;
576  }
577  }
578 
579 
580  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
587  public function update_fk_bank($id_bank)
588  {
589  // phpcs:enable
590  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id;
591 
592  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
593  $result = $this->db->query($sql);
594  if ($result)
595  {
596  return 1;
597  } else {
598  $this->error = $this->db->error();
599  return 0;
600  }
601  }
602 
610  public function getNomUrl($withpicto = 0, $maxlen = 0)
611  {
612  global $langs;
613 
614  $result = '';
615 
616  if (empty($this->ref)) $this->ref = $this->label;
617  $label = $langs->trans("ShowPayment").': '.$this->ref;
618 
619  if (!empty($this->id))
620  {
621  $link = '<a href="'.DOL_URL_ROOT.'/expensereport/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
622  $linkend = '</a>';
623 
624  if ($withpicto) $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
625  if ($withpicto && $withpicto != 2) $result .= ' ';
626  if ($withpicto != 2) $result .= $link.($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
627  }
628 
629  return $result;
630  }
631 
638  public function info($id)
639  {
640  $sql = 'SELECT e.rowid, e.datec, e.fk_user_creat, e.fk_user_modif, e.tms';
641  $sql .= ' FROM '.MAIN_DB_PREFIX.'payment_expensereport as e';
642  $sql .= ' WHERE e.rowid = '.$id;
643 
644  dol_syslog(get_class($this).'::info', LOG_DEBUG);
645  $result = $this->db->query($sql);
646 
647  if ($result)
648  {
649  if ($this->db->num_rows($result))
650  {
651  $obj = $this->db->fetch_object($result);
652  $this->id = $obj->rowid;
653  if ($obj->fk_user_creat)
654  {
655  $cuser = new User($this->db);
656  $cuser->fetch($obj->fk_user_creat);
657  $this->user_creation = $cuser;
658  }
659  if ($obj->fk_user_modif)
660  {
661  $muser = new User($this->db);
662  $muser->fetch($obj->fk_user_modif);
663  $this->user_modification = $muser;
664  }
665  $this->date_creation = $this->db->jdate($obj->datec);
666  $this->date_modification = $this->db->jdate($obj->tms);
667  }
668  $this->db->free($result);
669  } else {
670  dol_print_error($this->db);
671  }
672  }
673 }
initAsSpecimen()
Initialise an instance with random values.
fetch($id)
Load object in memory from database.
update_fk_bank($id_bank)
Update link between the expense report payment and the generated line in llx_bank.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
update($user=null, $notrigger=0)
Update database.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage bank accounts.
getNomUrl($withpicto=0, $maxlen=0)
Return clicable name (with picto eventually)
info($id)
Tab information on object.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un statut donne.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;un don (brouillon, validee, abandonnee, payee)
addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
Add record into bank for payment with links between this bank record and invoices of payment...
create($user)
Create payment of expense report into database.
Class to manage payments of expense report.
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
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
if(!empty($search_group)) natural_search(array("g.nom"g note
Definition: list.php:122
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...