dolibarr  17.0.4
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)) {
123  $this->fk_expensereport = trim($this->fk_expensereport);
124  }
125  if (isset($this->amount)) {
126  $this->amount = trim($this->amount);
127  }
128  if (isset($this->fk_typepayment)) {
129  $this->fk_typepayment = trim($this->fk_typepayment);
130  }
131  if (isset($this->num_payment)) {
132  $this->num_payment = trim($this->num_payment);
133  }
134  if (isset($this->note)) {
135  $this->note = trim($this->note);
136  }
137  if (isset($this->note_public)) {
138  $this->note_public = trim($this->note_public);
139  }
140  if (isset($this->note_private)) {
141  $this->note_private = trim($this->note_private);
142  }
143  if (isset($this->fk_bank)) {
144  $this->fk_bank = ((int) $this->fk_bank);
145  }
146  if (isset($this->fk_user_creat)) {
147  $this->fk_user_creat = ((int) $this->fk_user_creat);
148  }
149  if (isset($this->fk_user_modif)) {
150  $this->fk_user_modif = ((int) $this->fk_user_modif);
151  }
152 
153  $totalamount = 0;
154  foreach ($this->amounts as $key => $value) { // How payment is dispatch
155  $newvalue = price2num($value, 'MT');
156  $this->amounts[$key] = $newvalue;
157  $totalamount += $newvalue;
158  }
159  $totalamount = price2num($totalamount);
160 
161  // Check parameters
162  if ($totalamount == 0) {
163  return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
164  }
165 
166 
167  $this->db->begin();
168 
169  if ($totalamount != 0) {
170  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_expensereport (fk_expensereport, datec, datep, amount,";
171  $sql .= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)";
172  $sql .= " VALUES ($this->fk_expensereport, '".$this->db->idate($now)."',";
173  $sql .= " '".$this->db->idate($this->datepaid)."',";
174  $sql .= " ".price2num($totalamount).",";
175  $sql .= " ".((int) $this->fk_typepayment).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ".((int) $user->id).",";
176  $sql .= " 0)"; // fk_bank is ID of transaction into ll_bank
177 
178  dol_syslog(get_class($this)."::create", LOG_DEBUG);
179  $resql = $this->db->query($sql);
180  if ($resql) {
181  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_expensereport");
182  } else {
183  $error++;
184  }
185  }
186 
187  if ($totalamount != 0 && !$error) {
188  $this->amount = $totalamount;
189  $this->db->commit();
190  return $this->id;
191  } else {
192  $this->error = $this->db->error();
193  $this->db->rollback();
194  return -1;
195  }
196  }
197 
204  public function fetch($id)
205  {
206  $sql = "SELECT";
207  $sql .= " t.rowid,";
208  $sql .= " t.fk_expensereport,";
209  $sql .= " t.datec,";
210  $sql .= " t.tms,";
211  $sql .= " t.datep,";
212  $sql .= " t.amount,";
213  $sql .= " t.fk_typepayment,";
214  $sql .= " t.num_payment,";
215  $sql .= " t.note as note_public,";
216  $sql .= " t.fk_bank,";
217  $sql .= " t.fk_user_creat,";
218  $sql .= " t.fk_user_modif,";
219  $sql .= " pt.code as type_code, pt.libelle as type_label,";
220  $sql .= ' b.fk_account';
221  $sql .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as t";
222  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
223  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
224  $sql .= " WHERE t.rowid = ".((int) $id);
225 
226  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
227  $resql = $this->db->query($sql);
228  if ($resql) {
229  if ($this->db->num_rows($resql)) {
230  $obj = $this->db->fetch_object($resql);
231 
232  $this->id = $obj->rowid;
233  $this->ref = $obj->rowid;
234 
235  $this->fk_expensereport = $obj->fk_expensereport;
236  $this->datec = $this->db->jdate($obj->datec);
237  $this->tms = $this->db->jdate($obj->tms);
238  $this->datep = $this->db->jdate($obj->datep);
239  $this->amount = $obj->amount;
240  $this->fk_typepayment = $obj->fk_typepayment;
241  $this->num_payment = $obj->num_payment;
242  $this->note_public = $obj->note_public;
243  $this->fk_bank = $obj->fk_bank;
244  $this->fk_user_creat = $obj->fk_user_creat;
245  $this->fk_user_modif = $obj->fk_user_modif;
246 
247  $this->type_code = $obj->type_code;
248  $this->type_label = $obj->type_label;
249 
250  $this->bank_account = $obj->fk_account;
251  $this->bank_line = $obj->fk_bank;
252  }
253  $this->db->free($resql);
254 
255  return 1;
256  } else {
257  $this->error = "Error ".$this->db->lasterror();
258  return -1;
259  }
260  }
261 
262  // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
270  public function update($user = null, $notrigger = 0)
271  {
272  // phpcs:enable
273  global $conf, $langs;
274  $error = 0;
275 
276  // Clean parameters
277 
278  if (isset($this->fk_expensereport)) {
279  $this->fk_expensereport = trim($this->fk_expensereport);
280  }
281  if (isset($this->amount)) {
282  $this->amount = trim($this->amount);
283  }
284  if (isset($this->fk_typepayment)) {
285  $this->fk_typepayment = trim($this->fk_typepayment);
286  }
287  if (isset($this->num_payment)) {
288  $this->num_payment = trim($this->num_payment);
289  }
290  if (isset($this->note)) {
291  $this->note = trim($this->note);
292  }
293  if (isset($this->fk_bank)) {
294  $this->fk_bank = trim($this->fk_bank);
295  }
296  if (isset($this->fk_user_creat)) {
297  $this->fk_user_creat = trim($this->fk_user_creat);
298  }
299  if (isset($this->fk_user_modif)) {
300  $this->fk_user_modif = trim($this->fk_user_modif);
301  }
302 
303 
304  // Check parameters
305  // Put here code to add control on parameters values
306 
307  // Update request
308  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET";
309 
310  $sql .= " fk_expensereport=".(isset($this->fk_expensereport) ? $this->fk_expensereport : "null").",";
311  $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
312  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
313  $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
314  $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
315  $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
316  $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
317  $sql .= " note=".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : "null").",";
318  $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
319  $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
320  $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null")."";
321 
322 
323  $sql .= " WHERE rowid=".((int) $this->id);
324 
325  $this->db->begin();
326 
327  dol_syslog(get_class($this)."::update", LOG_DEBUG);
328  $resql = $this->db->query($sql);
329  if (!$resql) {
330  $error++; $this->errors[] = "Error ".$this->db->lasterror();
331  }
332 
333  // Commit or rollback
334  if ($error) {
335  foreach ($this->errors as $errmsg) {
336  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
337  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
338  }
339  $this->db->rollback();
340  return -1 * $error;
341  } else {
342  $this->db->commit();
343  return 1;
344  }
345  }
346 
347  // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
355  public function delete($user, $notrigger = 0)
356  {
357  // phpcs:enable
358  global $conf, $langs;
359  $error = 0;
360 
361  $this->db->begin();
362 
363  if (!$error) {
364  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
365  $sql .= " WHERE type='payment_expensereport' AND url_id=".((int) $this->id);
366 
367  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
368  $resql = $this->db->query($sql);
369  if (!$resql) {
370  $error++; $this->errors[] = "Error ".$this->db->lasterror();
371  }
372  }
373 
374  if (!$error) {
375  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_expensereport";
376  $sql .= " WHERE rowid=".((int) $this->id);
377 
378  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
379  $resql = $this->db->query($sql);
380  if (!$resql) {
381  $error++;
382  $this->errors[] = "Error ".$this->db->lasterror();
383  }
384  }
385 
386  // Commit or rollback
387  if ($error) {
388  foreach ($this->errors as $errmsg) {
389  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
390  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
391  }
392  $this->db->rollback();
393  return -1 * $error;
394  } else {
395  $this->db->commit();
396  return 1;
397  }
398  }
399 
400 
401 
409  public function createFromClone(User $user, $fromid)
410  {
411  $error = 0;
412 
413  $object = new PaymentExpenseReport($this->db);
414 
415  $this->db->begin();
416 
417  // Load source object
418  $object->fetch($fromid);
419  $object->id = 0;
420  $object->statut = 0;
421 
422  // Clear fields
423  // ...
424 
425  // Create clone
426  $object->context['createfromclone'] = 'createfromclone';
427  $result = $object->create($user);
428 
429  // Other options
430  if ($result < 0) {
431  $this->error = $object->error;
432  $error++;
433  }
434 
435  unset($object->context['createfromclone']);
436 
437  // End
438  if (!$error) {
439  $this->db->commit();
440  return $object->id;
441  } else {
442  $this->db->rollback();
443  return -1;
444  }
445  }
446 
447 
454  public function getLibStatut($mode = 0)
455  {
456  return '';
457  }
458 
459  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
467  public function LibStatut($status, $mode = 0)
468  {
469  // phpcs:enable
470  global $langs;
471 
472  return '';
473  }
474 
475 
483  public function initAsSpecimen()
484  {
485  $this->id = 0;
486 
487  $this->fk_expensereport = '';
488  $this->datec = '';
489  $this->tms = '';
490  $this->datep = '';
491  $this->amount = '';
492  $this->fk_typepayment = '';
493  $this->num_payment = '';
494  $this->note = '';
495  $this->fk_bank = '';
496  $this->fk_user_creat = '';
497  $this->fk_user_modif = '';
498  }
499 
500 
513  public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
514  {
515  global $langs, $conf;
516 
517  $error = 0;
518 
519  if (isModEnabled("banque")) {
520  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
521 
522  $acc = new Account($this->db);
523  $acc->fetch($accountid);
524 
525  //Fix me field
526  $total = $this->amount;
527 
528  if ($mode == 'payment_expensereport') {
529  $amount = $total;
530  }
531 
532  // Insert payment into llx_bank
533  $bank_line_id = $acc->addline(
534  $this->datepaid,
535  $this->fk_typepayment, // Payment mode id or code ("CHQ or VIR for example")
536  $label,
537  -$amount,
538  $this->num_payment,
539  '',
540  $user,
541  $emetteur_nom,
542  $emetteur_banque
543  );
544 
545  // Update fk_bank in llx_paiement.
546  // So we wil know the payment that have generated the bank transaction
547  if ($bank_line_id > 0) {
548  $result = $this->update_fk_bank($bank_line_id);
549  if ($result <= 0) {
550  $error++;
551  dol_print_error($this->db);
552  }
553 
554  // Add link 'payment', 'payment_supplier', 'payment_expensereport' in bank_url between payment and bank transaction
555  $url = '';
556  if ($mode == 'payment_expensereport') {
557  $url = DOL_URL_ROOT.'/expensereport/payment/card.php?rowid=';
558  }
559  if ($url) {
560  $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
561  if ($result <= 0) {
562  $error++;
563  dol_print_error($this->db);
564  }
565  }
566 
567  // Add link 'user' in bank_url between user and bank transaction
568  if (!$error) {
569  foreach ($this->amounts as $key => $value) { // We should have always same user but we loop in case of.
570  if ($mode == 'payment_expensereport') {
571  $fuser = new User($this->db);
572  $fuser->fetch($key);
573 
574  $result = $acc->add_url_line(
575  $bank_line_id,
576  $fuser->id,
577  DOL_URL_ROOT.'/user/card.php?id=',
578  $fuser->getFullName($langs),
579  'user'
580  );
581  if ($result <= 0) {
582  $this->error = $this->db->lasterror();
583  dol_syslog(get_class($this).'::addPaymentToBank '.$this->error);
584  $error++;
585  }
586  }
587  }
588  }
589  } else {
590  $this->error = $acc->error;
591  $this->errors = $acc->errors;
592  $error++;
593  }
594  }
595 
596  if (!$error) {
597  return 1;
598  } else {
599  return -1;
600  }
601  }
602 
603 
604  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
611  public function update_fk_bank($id_bank)
612  {
613  // phpcs:enable
614  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
615 
616  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
617  $result = $this->db->query($sql);
618  if ($result) {
619  return 1;
620  } else {
621  $this->error = $this->db->error();
622  return 0;
623  }
624  }
625 
633  public function getNomUrl($withpicto = 0, $maxlen = 0)
634  {
635  global $langs, $hookmanager;
636 
637  $result = '';
638 
639  if (empty($this->ref)) {
640  $this->ref = $this->label;
641  }
642  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Payment").'</u>';
643  if (isset($this->status)) {
644  $label .= ' '.$this->getLibStatut(5);
645  }
646  if (!empty($this->ref)) {
647  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
648  }
649  if (!empty($this->datep)) {
650  $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->datep, 'dayhour');
651  }
652 
653  if (!empty($this->id)) {
654  $link = '<a href="'.DOL_URL_ROOT.'/expensereport/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
655  $linkend = '</a>';
656 
657  if ($withpicto) {
658  $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
659  }
660  if ($withpicto && $withpicto != 2) {
661  $result .= ' ';
662  }
663  if ($withpicto != 2) {
664  $result .= $link.($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
665  }
666  }
667  global $action;
668  $hookmanager->initHooks(array($this->element . 'dao'));
669  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
670  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
671  if ($reshook > 0) {
672  $result = $hookmanager->resPrint;
673  } else {
674  $result .= $hookmanager->resPrint;
675  }
676  return $result;
677  }
678 
685  public function info($id)
686  {
687  $sql = 'SELECT e.rowid, e.datec, e.fk_user_creat, e.fk_user_modif, e.tms';
688  $sql .= ' FROM '.MAIN_DB_PREFIX.'payment_expensereport as e';
689  $sql .= ' WHERE e.rowid = '.((int) $id);
690 
691  dol_syslog(get_class($this).'::info', LOG_DEBUG);
692  $result = $this->db->query($sql);
693 
694  if ($result) {
695  if ($this->db->num_rows($result)) {
696  $obj = $this->db->fetch_object($result);
697  $this->id = $obj->rowid;
698  if ($obj->fk_user_creat) {
699  $cuser = new User($this->db);
700  $cuser->fetch($obj->fk_user_creat);
701  $this->user_creation = $cuser;
702  }
703  if ($obj->fk_user_modif) {
704  $muser = new User($this->db);
705  $muser->fetch($obj->fk_user_modif);
706  $this->user_modification = $muser;
707  }
708  $this->date_creation = $this->db->jdate($obj->datec);
709  $this->date_modification = $this->db->jdate($obj->tms);
710  }
711  $this->db->free($result);
712  } else {
713  dol_print_error($this->db);
714  }
715  }
716 }
$object ref
Definition: info.php:78
Class to manage bank accounts.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage payments of expense report.
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.
fetch($id)
Load object in memory from database.
create($user)
Create payment of expense report into database.
info($id)
Tab information on object.
getLibStatut($mode=0)
Retourne le libelle du statut d'un don (brouillon, validee, abandonnee, payee)
initAsSpecimen()
Initialise an instance with random values.
getNomUrl($withpicto=0, $maxlen=0)
Return clicable name (with picto eventually)
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
update($user=null, $notrigger=0)
Update 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:47
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41