dolibarr  9.0.0
paymentexpensereport.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
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 <http://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  //Unknow field
87  public $chid;
88  public $total;
89 
95  function __construct($db)
96  {
97  $this->db = $db;
98  }
99 
107  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  {
118  $this->error='ErrorBadValueForParameterCreatePaymentExpenseReport';
119  return -1;
120  }
121 
122  // Clean parameters
123  if (isset($this->fk_expensereport)) $this->fk_expensereport=trim($this->fk_expensereport);
124  if (isset($this->amount)) $this->amount=trim($this->amount);
125  if (isset($this->fk_typepayment)) $this->fk_typepayment=trim($this->fk_typepayment);
126  if (isset($this->num_payment)) $this->num_payment=trim($this->num_payment);
127  if (isset($this->note)) $this->note=trim($this->note);
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  if (! empty($this->fk_expensereport)) $this->chid = $this->fk_expensereport;
132 
133  $totalamount = 0;
134  foreach ($this->amounts as $key => $value) // How payment is dispatch
135  {
136  $newvalue = price2num($value,'MT');
137  $this->amounts[$key] = $newvalue;
138  $totalamount += $newvalue;
139  }
140  $totalamount = price2num($totalamount);
141 
142  // Check parameters
143  if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
144 
145 
146  $this->db->begin();
147 
148  if ($totalamount != 0)
149  {
150  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_expensereport (fk_expensereport, datec, datep, amount,";
151  $sql.= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)";
152  $sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
153  $sql.= " '".$this->db->idate($this->datepaid)."',";
154  $sql.= " ".$totalamount.",";
155  $sql.= " ".$this->fk_typepayment.", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".$user->id.",";
156  $sql.= " 0)";
157 
158  dol_syslog(get_class($this)."::create", LOG_DEBUG);
159  $resql=$this->db->query($sql);
160  if ($resql)
161  {
162  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_expensereport");
163  }
164  else
165  {
166  $error++;
167  }
168  }
169 
170  if ($totalamount != 0 && ! $error)
171  {
172  $this->amount=$totalamount;
173  $this->db->commit();
174  return $this->id;
175  }
176  else
177  {
178  $this->error=$this->db->error();
179  $this->db->rollback();
180  return -1;
181  }
182  }
183 
190  function fetch($id)
191  {
192  global $langs;
193  $sql = "SELECT";
194  $sql.= " t.rowid,";
195  $sql.= " t.fk_expensereport,";
196  $sql.= " t.datec,";
197  $sql.= " t.tms,";
198  $sql.= " t.datep,";
199  $sql.= " t.amount,";
200  $sql.= " t.fk_typepayment,";
201  $sql.= " t.num_payment,";
202  $sql.= " t.note,";
203  $sql.= " t.fk_bank,";
204  $sql.= " t.fk_user_creat,";
205  $sql.= " t.fk_user_modif,";
206  $sql.= " pt.code as type_code, pt.libelle as type_libelle,";
207  $sql.= ' b.fk_account';
208  $sql.= " FROM ".MAIN_DB_PREFIX."payment_expensereport as t";
209  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
210  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
211  $sql.= " WHERE t.rowid = ".$id;
212 
213  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
214  $resql=$this->db->query($sql);
215  if ($resql)
216  {
217  if ($this->db->num_rows($resql))
218  {
219  $obj = $this->db->fetch_object($resql);
220 
221  $this->id = $obj->rowid;
222  $this->ref = $obj->rowid;
223 
224  $this->fk_expensereport = $obj->fk_expensereport;
225  $this->datec = $this->db->jdate($obj->datec);
226  $this->tms = $this->db->jdate($obj->tms);
227  $this->datep = $this->db->jdate($obj->datep);
228  $this->amount = $obj->amount;
229  $this->fk_typepayment = $obj->fk_typepayment;
230  $this->num_payment = $obj->num_payment;
231  $this->note = $obj->note;
232  $this->fk_bank = $obj->fk_bank;
233  $this->fk_user_creat = $obj->fk_user_creat;
234  $this->fk_user_modif = $obj->fk_user_modif;
235 
236  $this->type_code = $obj->type_code;
237  $this->type_libelle = $obj->type_libelle;
238 
239  $this->bank_account = $obj->fk_account;
240  $this->bank_line = $obj->fk_bank;
241  }
242  $this->db->free($resql);
243 
244  return 1;
245  }
246  else
247  {
248  $this->error="Error ".$this->db->lasterror();
249  return -1;
250  }
251  }
252 
253 
261  function update($user=null, $notrigger=0)
262  {
263  global $conf, $langs;
264  $error=0;
265 
266  // Clean parameters
267 
268  if (isset($this->fk_expensereport)) $this->fk_expensereport=trim($this->fk_expensereport);
269  if (isset($this->amount)) $this->amount=trim($this->amount);
270  if (isset($this->fk_typepayment)) $this->fk_typepayment=trim($this->fk_typepayment);
271  if (isset($this->num_payment)) $this->num_payment=trim($this->num_payment);
272  if (isset($this->note)) $this->note=trim($this->note);
273  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
274  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
275  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
276 
277 
278  // Check parameters
279  // Put here code to add control on parameters values
280 
281  // Update request
282  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET";
283 
284  $sql.= " fk_expensereport=".(isset($this->fk_expensereport)?$this->fk_expensereport:"null").",";
285  $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
286  $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
287  $sql.= " datep=".(dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
288  $sql.= " amount=".(isset($this->amount)?$this->amount:"null").",";
289  $sql.= " fk_typepayment=".(isset($this->fk_typepayment)?$this->fk_typepayment:"null").",";
290  $sql.= " num_payment=".(isset($this->num_payment)?"'".$this->db->escape($this->num_payment)."'":"null").",";
291  $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
292  $sql.= " fk_bank=".(isset($this->fk_bank)?$this->fk_bank:"null").",";
293  $sql.= " fk_user_creat=".(isset($this->fk_user_creat)?$this->fk_user_creat:"null").",";
294  $sql.= " fk_user_modif=".(isset($this->fk_user_modif)?$this->fk_user_modif:"null")."";
295 
296 
297  $sql.= " WHERE rowid=".$this->id;
298 
299  $this->db->begin();
300 
301  dol_syslog(get_class($this)."::update", LOG_DEBUG);
302  $resql = $this->db->query($sql);
303  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
304 
305  if (! $error)
306  {
307  if (! $notrigger)
308  {
309  // Uncomment this and change MYOBJECT to your own tag if you
310  // want this action call a trigger.
311 
313  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
314  //$interface=new Interfaces($this->db);
315  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
316  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
318  }
319  }
320 
321  // Commit or rollback
322  if ($error)
323  {
324  foreach($this->errors as $errmsg)
325  {
326  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
327  $this->error.=($this->error?', '.$errmsg:$errmsg);
328  }
329  $this->db->rollback();
330  return -1*$error;
331  }
332  else
333  {
334  $this->db->commit();
335  return 1;
336  }
337  }
338 
339 
347  function delete($user, $notrigger=0)
348  {
349  global $conf, $langs;
350  $error=0;
351 
352  $this->db->begin();
353 
354  if (! $error)
355  {
356  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
357  $sql.= " WHERE type='payment_expensereport' AND url_id=".$this->id;
358 
359  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
360  $resql = $this->db->query($sql);
361  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
362  }
363 
364  if (! $error)
365  {
366  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_expensereport";
367  $sql.= " WHERE rowid=".$this->id;
368 
369  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
370  $resql = $this->db->query($sql);
371  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
372  }
373 
374  if (! $error)
375  {
376  if (! $notrigger)
377  {
378  // Uncomment this and change MYOBJECT to your own tag if you
379  // want this action call a trigger.
380 
382  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
383  //$interface=new Interfaces($this->db);
384  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
385  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
387  }
388  }
389 
390  // Commit or rollback
391  if ($error)
392  {
393  foreach($this->errors as $errmsg)
394  {
395  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
396  $this->error.=($this->error?', '.$errmsg:$errmsg);
397  }
398  $this->db->rollback();
399  return -1*$error;
400  }
401  else
402  {
403  $this->db->commit();
404  return 1;
405  }
406  }
407 
408 
409 
416  function createFromClone($fromid)
417  {
418  global $user,$langs;
419 
420  $error=0;
421 
422  $object=new PaymentExpenseReport($this->db);
423 
424  $this->db->begin();
425 
426  // Load source object
427  $object->fetch($fromid);
428  $object->id=0;
429  $object->statut=0;
430 
431  // Clear fields
432  // ...
433 
434  // Create clone
435  $object->context['createfromclone'] = 'createfromclone';
436  $result=$object->create($user);
437 
438  // Other options
439  if ($result < 0)
440  {
441  $this->error=$object->error;
442  $error++;
443  }
444 
445  if (! $error)
446  {
447 
448  }
449 
450  unset($object->context['createfromclone']);
451 
452  // End
453  if (! $error)
454  {
455  $this->db->commit();
456  return $object->id;
457  }
458  else
459  {
460  $this->db->rollback();
461  return -1;
462  }
463  }
464 
465 
472  function getLibStatut($mode=0)
473  {
474  return '';
475  }
476 
477  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
485  function LibStatut($statut,$mode=0)
486  {
487  // phpcs:enable
488  global $langs;
489 
490  return '';
491  }
492 
493 
501  function initAsSpecimen()
502  {
503  $this->id=0;
504 
505  $this->fk_expensereport='';
506  $this->datec='';
507  $this->tms='';
508  $this->datep='';
509  $this->amount='';
510  $this->fk_typepayment='';
511  $this->num_payment='';
512  $this->note='';
513  $this->fk_bank='';
514  $this->fk_user_creat='';
515  $this->fk_user_modif='';
516  }
517 
518 
531  function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
532  {
533  global $langs,$conf;
534 
535  $error=0;
536 
537  if (! empty($conf->banque->enabled))
538  {
539  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
540 
541  $acc = new Account($this->db);
542  $acc->fetch($accountid);
543 
544  //Fix me field
545  $this->total = $this->amount;
546  $total = $this->total;
547 
548  if ($mode == 'payment_expensereport') $amount=$total;
549 
550  // Insert payment into llx_bank
551  $bank_line_id = $acc->addline(
552  $this->datepaid,
553  $this->fk_typepayment, // Payment mode id or code ("CHQ or VIR for example")
554  $label,
555  -$amount,
556  $this->num_payment,
557  '',
558  $user,
559  $emetteur_nom,
560  $emetteur_banque
561  );
562 
563  // Update fk_bank in llx_paiement.
564  // On connait ainsi le paiement qui a genere l'ecriture bancaire
565  if ($bank_line_id > 0)
566  {
567  $result=$this->update_fk_bank($bank_line_id);
568  if ($result <= 0)
569  {
570  $error++;
571  dol_print_error($this->db);
572  }
573 
574  // Add link 'payment', 'payment_supplier', 'payment_expensereport' in bank_url between payment and bank transaction
575  $url='';
576  if ($mode == 'payment_expensereport') $url=DOL_URL_ROOT.'/expensereport/payment/card.php?rowid=';
577  if ($url)
578  {
579  $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
580  if ($result <= 0)
581  {
582  $error++;
583  dol_print_error($this->db);
584  }
585  }
586 
587  // Add link 'user' in bank_url between user and bank transaction
588  if (! $error)
589  {
590  foreach ($this->amounts as $key => $value) // We should have always same user but we loop in case of.
591  {
592  if ($mode == 'payment_expensereport')
593  {
594  $fuser = new User($this->db);
595  $fuser->fetch($key);
596 
597  $result=$acc->add_url_line(
598  $bank_line_id,
599  $fuser->id,
600  DOL_URL_ROOT.'/user/card.php?id=',
601  $fuser->getFullName($langs),
602  'user'
603  );
604  if ($result <= 0)
605  {
606  $this->error=$this->db->lasterror();
607  dol_syslog(get_class($this).'::addPaymentToBank '.$this->error);
608  $error++;
609  }
610  }
611  }
612  }
613  }
614  else
615  {
616  $this->error=$acc->error;
617  $error++;
618  }
619  }
620 
621  if (! $error)
622  {
623  return 1;
624  }
625  else
626  {
627  return -1;
628  }
629  }
630 
631 
632  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
639  function update_fk_bank($id_bank)
640  {
641  // phpcs:enable
642  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id;
643 
644  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
645  $result = $this->db->query($sql);
646  if ($result)
647  {
648  return 1;
649  }
650  else
651  {
652  $this->error=$this->db->error();
653  return 0;
654  }
655  }
656 
664  function getNomUrl($withpicto=0,$maxlen=0)
665  {
666  global $langs;
667 
668  $result='';
669 
670  if (empty($this->ref)) $this->ref=$this->lib;
671  $label = $langs->trans("ShowPayment").': '.$this->ref;
672 
673  if (!empty($this->id))
674  {
675  $link = '<a href="'.DOL_URL_ROOT.'/expensereport/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
676  $linkend='</a>';
677 
678  if ($withpicto) $result.=($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
679  if ($withpicto && $withpicto != 2) $result.=' ';
680  if ($withpicto != 2) $result.=$link.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$linkend;
681  }
682 
683  return $result;
684  }
685 
692  function info($id)
693  {
694  $sql = 'SELECT e.rowid, e.datec, e.fk_user_creat, e.fk_user_modif, e.tms';
695  $sql.= ' FROM '.MAIN_DB_PREFIX.'payment_expensereport as e';
696  $sql.= ' WHERE e.rowid = '.$id;
697 
698  dol_syslog(get_class($this).'::info', LOG_DEBUG);
699  $result = $this->db->query($sql);
700 
701  if ($result)
702  {
703  if ($this->db->num_rows($result))
704  {
705  $obj = $this->db->fetch_object($result);
706  $this->id = $obj->rowid;
707  if ($obj->fk_user_creat)
708  {
709  $cuser = new User($this->db);
710  $cuser->fetch($obj->fk_user_creat);
711  $this->user_creation = $cuser;
712  }
713  if ($obj->fk_user_modif)
714  {
715  $muser = new User($this->db);
716  $muser->fetch($obj->fk_user_modif);
717  $this->user_modification = $muser;
718  }
719  $this->date_creation = $this->db->jdate($obj->datec);
720  $this->date_modification = $this->db->jdate($obj->tms);
721  }
722  $this->db->free($result);
723  }
724  else
725  {
726  dol_print_error($this->db);
727  }
728  }
729 }
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.
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
initAsSpecimen()
Initialise an instance with random values.
fetch($id)
Load object in memory from database.
if(! empty($search_group)) natural_search(array("g.nom" g note
Definition: list.php:123
update_fk_bank($id_bank)
Update link between the expense report payment and the generated line in llx_bank.
Class to manage Dolibarr users.
Definition: user.class.php:41
update($user=null, $notrigger=0)
Update database.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
Class to manage bank accounts.
getNomUrl($withpicto=0, $maxlen=0)
Return clicable name (with picto eventually)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
info($id)
Tab information on object.
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...
dol_now($mode='gmt')
Return date for now.
create($user)
Create payment of expense report into database.
Class to manage payments of expense report.
LibStatut($statut, $mode=0)
Renvoi le libelle d&#39;un statut donne.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
createFromClone($fromid)
Load an object from its id and create a new one in database.