dolibarr  7.0.0-beta
paymentexpensereport.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
24 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
25 
26 
31 {
32  public $element='payment_expensereport';
33  public $table_element='payment_expensereport';
34  public $picto = 'payment';
35 
36  var $rowid;
37 
38  var $fk_expensereport;
39  var $datec='';
40  var $tms='';
41  var $datep='';
42  var $amount; // Total amount of payment
43  var $amounts=array(); // Array of amounts
44  var $fk_typepayment;
45  var $num_payment;
46  var $fk_bank;
47  var $fk_user_creat;
48  var $fk_user_modif;
49 
55  function __construct($db)
56  {
57  $this->db = $db;
58  }
59 
67  function create($user)
68  {
69  global $conf, $langs;
70 
71  $error=0;
72 
73  $now=dol_now();
74 
75  // Validate parameters
76  if (! $this->datepaid)
77  {
78  $this->error='ErrorBadValueForParameterCreatePaymentExpenseReport';
79  return -1;
80  }
81 
82  // Clean parameters
83  if (isset($this->fk_expensereport)) $this->fk_expensereport=trim($this->fk_expensereport);
84  if (isset($this->amount)) $this->amount=trim($this->amount);
85  if (isset($this->fk_typepayment)) $this->fk_typepayment=trim($this->fk_typepayment);
86  if (isset($this->num_payment)) $this->num_payment=trim($this->num_payment);
87  if (isset($this->note)) $this->note=trim($this->note);
88  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
89  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
90  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
91 
92  $totalamount = 0;
93  foreach ($this->amounts as $key => $value) // How payment is dispatch
94  {
95  $newvalue = price2num($value,'MT');
96  $this->amounts[$key] = $newvalue;
97  $totalamount += $newvalue;
98  }
99  $totalamount = price2num($totalamount);
100 
101  // Check parameters
102  if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
103 
104 
105  $this->db->begin();
106 
107  if ($totalamount != 0)
108  {
109  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_expensereport (fk_expensereport, datec, datep, amount,";
110  $sql.= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)";
111  $sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
112  $sql.= " '".$this->db->idate($this->datepaid)."',";
113  $sql.= " ".$totalamount.",";
114  $sql.= " ".$this->fk_typepayment.", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".$user->id.",";
115  $sql.= " 0)";
116 
117  dol_syslog(get_class($this)."::create", LOG_DEBUG);
118  $resql=$this->db->query($sql);
119  if ($resql)
120  {
121  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_expensereport");
122  }
123  else
124  {
125  $error++;
126  }
127 
128  }
129 
130  if ($totalamount != 0 && ! $error)
131  {
132  $this->amount=$totalamount;
133  $this->db->commit();
134  return $this->id;
135  }
136  else
137  {
138  $this->error=$this->db->error();
139  $this->db->rollback();
140  return -1;
141  }
142  }
143 
150  function fetch($id)
151  {
152  global $langs;
153  $sql = "SELECT";
154  $sql.= " t.rowid,";
155  $sql.= " t.fk_expensereport,";
156  $sql.= " t.datec,";
157  $sql.= " t.tms,";
158  $sql.= " t.datep,";
159  $sql.= " t.amount,";
160  $sql.= " t.fk_typepayment,";
161  $sql.= " t.num_payment,";
162  $sql.= " t.note,";
163  $sql.= " t.fk_bank,";
164  $sql.= " t.fk_user_creat,";
165  $sql.= " t.fk_user_modif,";
166  $sql.= " pt.code as type_code, pt.libelle as type_libelle,";
167  $sql.= ' b.fk_account';
168  $sql.= " FROM ".MAIN_DB_PREFIX."payment_expensereport as t";
169  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id AND pt.entity IN (".getEntity('c_paiement').")";
170  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
171  $sql.= " WHERE t.rowid = ".$id;
172 
173  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
174  $resql=$this->db->query($sql);
175  if ($resql)
176  {
177  if ($this->db->num_rows($resql))
178  {
179  $obj = $this->db->fetch_object($resql);
180 
181  $this->id = $obj->rowid;
182  $this->ref = $obj->rowid;
183 
184  $this->fk_expensereport = $obj->fk_expensereport;
185  $this->datec = $this->db->jdate($obj->datec);
186  $this->tms = $this->db->jdate($obj->tms);
187  $this->datep = $this->db->jdate($obj->datep);
188  $this->amount = $obj->amount;
189  $this->fk_typepayment = $obj->fk_typepayment;
190  $this->num_payment = $obj->num_payment;
191  $this->note = $obj->note;
192  $this->fk_bank = $obj->fk_bank;
193  $this->fk_user_creat = $obj->fk_user_creat;
194  $this->fk_user_modif = $obj->fk_user_modif;
195 
196  $this->type_code = $obj->type_code;
197  $this->type_libelle = $obj->type_libelle;
198 
199  $this->bank_account = $obj->fk_account;
200  $this->bank_line = $obj->fk_bank;
201  }
202  $this->db->free($resql);
203 
204  return 1;
205  }
206  else
207  {
208  $this->error="Error ".$this->db->lasterror();
209  return -1;
210  }
211  }
212 
213 
221  function update($user=null, $notrigger=0)
222  {
223  global $conf, $langs;
224  $error=0;
225 
226  // Clean parameters
227 
228  if (isset($this->fk_expensereport)) $this->fk_expensereport=trim($this->fk_expensereport);
229  if (isset($this->amount)) $this->amount=trim($this->amount);
230  if (isset($this->fk_typepayment)) $this->fk_typepayment=trim($this->fk_typepayment);
231  if (isset($this->num_payment)) $this->num_payment=trim($this->num_payment);
232  if (isset($this->note)) $this->note=trim($this->note);
233  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
234  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
235  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
236 
237 
238 
239  // Check parameters
240  // Put here code to add control on parameters values
241 
242  // Update request
243  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET";
244 
245  $sql.= " fk_expensereport=".(isset($this->fk_expensereport)?$this->fk_expensereport:"null").",";
246  $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
247  $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
248  $sql.= " datep=".(dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
249  $sql.= " amount=".(isset($this->amount)?$this->amount:"null").",";
250  $sql.= " fk_typepayment=".(isset($this->fk_typepayment)?$this->fk_typepayment:"null").",";
251  $sql.= " num_payment=".(isset($this->num_payment)?"'".$this->db->escape($this->num_payment)."'":"null").",";
252  $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
253  $sql.= " fk_bank=".(isset($this->fk_bank)?$this->fk_bank:"null").",";
254  $sql.= " fk_user_creat=".(isset($this->fk_user_creat)?$this->fk_user_creat:"null").",";
255  $sql.= " fk_user_modif=".(isset($this->fk_user_modif)?$this->fk_user_modif:"null")."";
256 
257 
258  $sql.= " WHERE rowid=".$this->id;
259 
260  $this->db->begin();
261 
262  dol_syslog(get_class($this)."::update", LOG_DEBUG);
263  $resql = $this->db->query($sql);
264  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
265 
266  if (! $error)
267  {
268  if (! $notrigger)
269  {
270  // Uncomment this and change MYOBJECT to your own tag if you
271  // want this action call a trigger.
272 
274  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
275  //$interface=new Interfaces($this->db);
276  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
277  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
279  }
280  }
281 
282  // Commit or rollback
283  if ($error)
284  {
285  foreach($this->errors as $errmsg)
286  {
287  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
288  $this->error.=($this->error?', '.$errmsg:$errmsg);
289  }
290  $this->db->rollback();
291  return -1*$error;
292  }
293  else
294  {
295  $this->db->commit();
296  return 1;
297  }
298  }
299 
300 
308  function delete($user, $notrigger=0)
309  {
310  global $conf, $langs;
311  $error=0;
312 
313  $this->db->begin();
314 
315  if (! $error)
316  {
317  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
318  $sql.= " WHERE type='payment_expensereport' AND url_id=".$this->id;
319 
320  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
321  $resql = $this->db->query($sql);
322  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
323  }
324 
325  if (! $error)
326  {
327  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_expensereport";
328  $sql.= " WHERE rowid=".$this->id;
329 
330  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
331  $resql = $this->db->query($sql);
332  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
333  }
334 
335  if (! $error)
336  {
337  if (! $notrigger)
338  {
339  // Uncomment this and change MYOBJECT to your own tag if you
340  // want this action call a trigger.
341 
343  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
344  //$interface=new Interfaces($this->db);
345  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
346  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
348  }
349  }
350 
351  // Commit or rollback
352  if ($error)
353  {
354  foreach($this->errors as $errmsg)
355  {
356  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
357  $this->error.=($this->error?', '.$errmsg:$errmsg);
358  }
359  $this->db->rollback();
360  return -1*$error;
361  }
362  else
363  {
364  $this->db->commit();
365  return 1;
366  }
367  }
368 
369 
370 
377  function createFromClone($fromid)
378  {
379  global $user,$langs;
380 
381  $error=0;
382 
383  $object=new PaymentExpenseReport($this->db);
384 
385  $object->context['createfromclone'] = 'createfromclone';
386 
387  $this->db->begin();
388 
389  // Load source object
390  $object->fetch($fromid);
391  $object->id=0;
392  $object->statut=0;
393 
394  // Clear fields
395  // ...
396 
397  // Create clone
398  $result=$object->create($user);
399 
400  // Other options
401  if ($result < 0)
402  {
403  $this->error=$object->error;
404  $error++;
405  }
406 
407  if (! $error)
408  {
409 
410 
411 
412  }
413 
414  unset($this->context['createfromclone']);
415 
416  // End
417  if (! $error)
418  {
419  $this->db->commit();
420  return $object->id;
421  }
422  else
423  {
424  $this->db->rollback();
425  return -1;
426  }
427  }
428 
429 
436  function getLibStatut($mode=0)
437  {
438  return '';
439  }
440 
448  function LibStatut($statut,$mode=0)
449  {
450  global $langs;
451 
452  return '';
453  }
454 
455 
463  function initAsSpecimen()
464  {
465  $this->id=0;
466 
467  $this->fk_expensereport='';
468  $this->datec='';
469  $this->tms='';
470  $this->datep='';
471  $this->amount='';
472  $this->fk_typepayment='';
473  $this->num_payment='';
474  $this->note='';
475  $this->fk_bank='';
476  $this->fk_user_creat='';
477  $this->fk_user_modif='';
478 
479 
480  }
481 
482 
495  function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
496  {
497  global $langs,$conf;
498 
499  $error=0;
500 
501  if (! empty($conf->banque->enabled))
502  {
503  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
504 
505  $acc = new Account($this->db);
506  $acc->fetch($accountid);
507 
508  $total=$this->total;
509  if ($mode == 'payment_expensereport') $amount=$total;
510 
511  // Insert payment into llx_bank
512  $bank_line_id = $acc->addline(
513  $this->datepaid,
514  $this->fk_typepayment, // Payment mode id or code ("CHQ or VIR for example")
515  $label,
516  -$amount,
517  $this->num_payment,
518  '',
519  $user,
520  $emetteur_nom,
521  $emetteur_banque
522  );
523 
524  // Update fk_bank in llx_paiement.
525  // On connait ainsi le paiement qui a genere l'ecriture bancaire
526  if ($bank_line_id > 0)
527  {
528  $result=$this->update_fk_bank($bank_line_id);
529  if ($result <= 0)
530  {
531  $error++;
532  dol_print_error($this->db);
533  }
534 
535  // Add link 'payment', 'payment_supplier', 'payment_expensereport' in bank_url between payment and bank transaction
536  $url='';
537  if ($mode == 'payment_expensereport') $url=DOL_URL_ROOT.'/expensereport/payment/card.php?rowid=';
538  if ($url)
539  {
540  $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
541  if ($result <= 0)
542  {
543  $error++;
544  dol_print_error($this->db);
545  }
546  }
547 
548  // Add link 'user' in bank_url between user and bank transaction
549  if (! $error)
550  {
551  foreach ($this->amounts as $key => $value) // We should have always same user but we loop in case of.
552  {
553  if ($mode == 'payment_expensereport')
554  {
555  $fuser = new User($this->db);
556  $fuser->fetch($key);
557 
558  $result=$acc->add_url_line(
559  $bank_line_id,
560  $fuser->id,
561  DOL_URL_ROOT.'/user/card.php?id=',
562  $fuser->getFullName($langs),
563  'user'
564  );
565  if ($result <= 0)
566  {
567  $this->error=$this->db->lasterror();
568  dol_syslog(get_class($this).'::addPaymentToBank '.$this->error);
569  $error++;
570  }
571  }
572  }
573  }
574  }
575  else
576  {
577  $this->error=$acc->error;
578  $error++;
579  }
580  }
581 
582  if (! $error)
583  {
584  return 1;
585  }
586  else
587  {
588  return -1;
589  }
590  }
591 
592 
599  function update_fk_bank($id_bank)
600  {
601  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id;
602 
603  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
604  $result = $this->db->query($sql);
605  if ($result)
606  {
607  return 1;
608  }
609  else
610  {
611  $this->error=$this->db->error();
612  return 0;
613  }
614  }
615 
623  function getNomUrl($withpicto=0,$maxlen=0)
624  {
625  global $langs;
626 
627  $result='';
628 
629  if (empty($this->ref)) $this->ref=$this->lib;
630  $label = $langs->trans("ShowPayment").': '.$this->ref;
631 
632  if (!empty($this->id))
633  {
634  $link = '<a href="'.DOL_URL_ROOT.'/expensereport/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
635  $linkend='</a>';
636 
637  if ($withpicto) $result.=($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
638  if ($withpicto && $withpicto != 2) $result.=' ';
639  if ($withpicto != 2) $result.=$link.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$linkend;
640  }
641 
642  return $result;
643  }
644 
651  function info($id)
652  {
653  $sql = 'SELECT e.rowid, e.datec, e.fk_user_creat, e.fk_user_modif, e.tms';
654  $sql.= ' FROM '.MAIN_DB_PREFIX.'payment_expensereport as e';
655  $sql.= ' WHERE e.rowid = '.$id;
656 
657  dol_syslog(get_class($this).'::info', LOG_DEBUG);
658  $result = $this->db->query($sql);
659 
660  if ($result)
661  {
662  if ($this->db->num_rows($result))
663  {
664  $obj = $this->db->fetch_object($result);
665  $this->id = $obj->rowid;
666  if ($obj->fk_user_creat)
667  {
668  $cuser = new User($this->db);
669  $cuser->fetch($obj->fk_user_creat);
670  $this->user_creation = $cuser;
671  }
672  if ($obj->fk_user_modif)
673  {
674  $muser = new User($this->db);
675  $muser->fetch($obj->fk_user_modif);
676  $this->user_modification = $muser;
677  }
678  $this->date_creation = $this->db->jdate($obj->datec);
679  $this->date_modification = $this->db->jdate($obj->tms);
680  }
681  $this->db->free($result);
682  }
683  else
684  {
685  dol_print_error($this->db);
686  }
687  }
688 }
$table_element
Name of table without prefix where object is stored.
$element
Id that identify managed objects.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
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.
Class to manage Dolibarr users.
Definition: user.class.php:39
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.
getEntity($element, $shared=1, $forceentity=null)
Get list of entity id to use.
getLibStatut($mode=0)
Retourne le libelle du statut d'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.
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
LibStatut($statut, $mode=0)
Renvoi le libelle d'un statut donne.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...
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.