dolibarr  16.0.5
expensereport.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2011 Dimitri Mouillard <dmouillard@teclib.com>
3  * Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
5  * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
6  * Copyright (c) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
7  * Copyright (C) 2016-2020 Ferran Marcet <fmarcet@2byte.es>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
28 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_ik.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_rule.class.php';
32 
37 {
41  public $element = 'expensereport';
42 
46  public $table_element = 'expensereport';
47 
51  public $table_element_line = 'expensereport_det';
52 
56  public $fk_element = 'fk_expensereport';
57 
61  public $picto = 'trip';
62 
63  public $lines = array();
64 
65  public $date_debut;
66 
67  public $date_fin;
68 
74  public $status;
75 
82  public $fk_statut;
83 
84  public $fk_c_paiement;
85  public $paid;
86 
87  public $user_author_infos;
88  public $user_validator_infos;
89 
90  public $rule_warning_message;
91 
92  // ACTIONS
93 
94  // Create
95  public $date_create;
96  public $fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
97 
98  // Update
99  public $date_modif;
100  public $fk_user_modif;
101 
102  // Refus
103  public $date_refuse;
104  public $detail_refuse;
105  public $fk_user_refuse;
106 
107  // Annulation
108  public $date_cancel;
109  public $detail_cancel;
110  public $fk_user_cancel;
111 
112  public $fk_user_validator; // User that is defined to approve
113 
114  // Validation
115  /* @deprecated */
116  public $datevalid;
117 
118  public $date_valid; // User making validation
119  public $fk_user_valid;
120  public $user_valid_infos;
121 
122  // Approve
123  public $date_approve;
124  public $fk_user_approve; // User that has approved
125 
126  // Paiement
127  public $user_paid_infos;
128 
129  public $localtax1; // for backward compatibility (real field should be total_localtax1 defined into CommonObject)
130  public $localtax2; // for backward compatibility (real field should be total_localtax2 defined into CommonObject)
131 
132  public $statuts = array();
133  public $statuts_short = array();
134  public $statuts_logo;
135 
136 
140  const STATUS_DRAFT = 0;
141 
145  const STATUS_VALIDATED = 2;
146 
150  const STATUS_CANCELED = 4;
151 
155  const STATUS_APPROVED = 5;
156 
160  const STATUS_REFUSED = 99;
161 
165  const STATUS_CLOSED = 6;
166 
167 
168  public $fields = array(
169  'rowid' =>array('type'=>'integer', 'label'=>'ID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
170  'ref' =>array('type'=>'varchar(50)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>15),
171  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>20),
172  'ref_number_int' =>array('type'=>'integer', 'label'=>'Ref number int', 'enabled'=>1, 'visible'=>-1, 'position'=>25),
173  'ref_ext' =>array('type'=>'integer', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>-1, 'position'=>30),
174  'total_ht' =>array('type'=>'double(24,8)', 'label'=>'Total ht', 'enabled'=>1, 'visible'=>-1, 'position'=>35),
175  'total_tva' =>array('type'=>'double(24,8)', 'label'=>'Total tva', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
176  'localtax1' =>array('type'=>'double(24,8)', 'label'=>'Localtax1', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
177  'localtax2' =>array('type'=>'double(24,8)', 'label'=>'Localtax2', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
178  'total_ttc' =>array('type'=>'double(24,8)', 'label'=>'Total ttc', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
179  'date_debut' =>array('type'=>'date', 'label'=>'Date debut', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>60),
180  'date_fin' =>array('type'=>'date', 'label'=>'Date fin', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>65),
181  'date_valid' =>array('type'=>'datetime', 'label'=>'Date valid', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
182  'date_approve' =>array('type'=>'datetime', 'label'=>'Date approve', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
183  'date_refuse' =>array('type'=>'datetime', 'label'=>'Date refuse', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
184  'date_cancel' =>array('type'=>'datetime', 'label'=>'Date cancel', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
185  'fk_user_author' =>array('type'=>'integer', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>100),
186  'fk_user_modif' =>array('type'=>'integer', 'label'=>'Fk user modif', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
187  'fk_user_valid' =>array('type'=>'integer', 'label'=>'Fk user valid', 'enabled'=>1, 'visible'=>-1, 'position'=>110),
188  'fk_user_validator' =>array('type'=>'integer', 'label'=>'Fk user validator', 'enabled'=>1, 'visible'=>-1, 'position'=>115),
189  'fk_user_approve' =>array('type'=>'integer', 'label'=>'Fk user approve', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
190  'fk_user_refuse' =>array('type'=>'integer', 'label'=>'Fk user refuse', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
191  'fk_user_cancel' =>array('type'=>'integer', 'label'=>'Fk user cancel', 'enabled'=>1, 'visible'=>-1, 'position'=>130),
192  'fk_c_paiement' =>array('type'=>'integer', 'label'=>'Fk c paiement', 'enabled'=>1, 'visible'=>-1, 'position'=>140),
193  'paid' =>array('type'=>'integer', 'label'=>'Paid', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>145),
194  'note_public' =>array('type'=>'text', 'label'=>'Note public', 'enabled'=>1, 'visible'=>0, 'position'=>150),
195  'note_private' =>array('type'=>'text', 'label'=>'Note private', 'enabled'=>1, 'visible'=>0, 'position'=>155),
196  'detail_refuse' =>array('type'=>'varchar(255)', 'label'=>'Detail refuse', 'enabled'=>1, 'visible'=>-1, 'position'=>160),
197  'detail_cancel' =>array('type'=>'varchar(255)', 'label'=>'Detail cancel', 'enabled'=>1, 'visible'=>-1, 'position'=>165),
198  'integration_compta' =>array('type'=>'integer', 'label'=>'Integration compta', 'enabled'=>1, 'visible'=>-1, 'position'=>170),
199  'fk_bank_account' =>array('type'=>'integer', 'label'=>'Fk bank account', 'enabled'=>1, 'visible'=>-1, 'position'=>175),
200  'fk_multicurrency' =>array('type'=>'integer', 'label'=>'Fk multicurrency', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
201  'multicurrency_code' =>array('type'=>'varchar(255)', 'label'=>'Multicurrency code', 'enabled'=>1, 'visible'=>-1, 'position'=>190),
202  'multicurrency_tx' =>array('type'=>'double(24,8)', 'label'=>'Multicurrency tx', 'enabled'=>1, 'visible'=>-1, 'position'=>195),
203  'multicurrency_total_ht' =>array('type'=>'double(24,8)', 'label'=>'Multicurrency total ht', 'enabled'=>1, 'visible'=>-1, 'position'=>200),
204  'multicurrency_total_tva' =>array('type'=>'double(24,8)', 'label'=>'Multicurrency total tva', 'enabled'=>1, 'visible'=>-1, 'position'=>205),
205  'multicurrency_total_ttc' =>array('type'=>'double(24,8)', 'label'=>'Multicurrency total ttc', 'enabled'=>1, 'visible'=>-1, 'position'=>210),
206  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>220),
207  'date_create' =>array('type'=>'datetime', 'label'=>'Date create', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>300),
208  'tms' =>array('type'=>'timestamp', 'label'=>'Tms', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>305),
209  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'position'=>1000),
210  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>1010),
211  'fk_statut' =>array('type'=>'integer', 'label'=>'Fk statut', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
212  );
213 
219  public function __construct($db)
220  {
221  $this->db = $db;
222  $this->total_ht = 0;
223  $this->total_ttc = 0;
224  $this->total_tva = 0;
225  $this->total_localtax1 = 0;
226  $this->total_localtax2 = 0;
227  $this->localtax1 = 0; // For backward compatibility
228  $this->localtax2 = 0; // For backward compatibility
229  $this->modepaymentid = 0;
230 
231  // List of language codes for status
232  $this->statuts_short = array(0 => 'Draft', 2 => 'Validated', 4 => 'Canceled', 5 => 'Approved', 6 => 'Paid', 99 => 'Refused');
233  $this->statuts = array(0 => 'Draft', 2 => 'ValidatedWaitingApproval', 4 => 'Canceled', 5 => 'Approved', 6 => 'Paid', 99 => 'Refused');
234  $this->statuts_logo = array(0 => 'status0', 2 => 'status1', 4 => 'status6', 5 => 'status4', 6 => 'status6', 99 => 'status5');
235  }
236 
244  public function create($user, $notrigger = 0)
245  {
246  global $conf, $langs;
247 
248  $now = dol_now();
249 
250  $error = 0;
251 
252  // Check parameters
253  if (empty($this->date_debut) || empty($this->date_fin)) {
254  $this->error = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Date'));
255  return -1;
256  }
257 
258  $fuserid = $this->fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
259  if (empty($fuserid)) {
260  $fuserid = $user->id;
261  }
262 
263  $this->db->begin();
264 
265  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
266  $sql .= "ref";
267  $sql .= ",total_ht";
268  $sql .= ",total_ttc";
269  $sql .= ",total_tva";
270  $sql .= ",date_debut";
271  $sql .= ",date_fin";
272  $sql .= ",date_create";
273  $sql .= ",fk_user_creat";
274  $sql .= ",fk_user_author";
275  $sql .= ",fk_user_validator";
276  $sql .= ",fk_user_approve";
277  $sql .= ",fk_user_modif";
278  $sql .= ",fk_statut";
279  $sql .= ",fk_c_paiement";
280  $sql .= ",paid";
281  $sql .= ",note_public";
282  $sql .= ",note_private";
283  $sql .= ",entity";
284  $sql .= ") VALUES(";
285  $sql .= "'(PROV)'";
286  $sql .= ", ".price2num($this->total_ht, 'MT');
287  $sql .= ", ".price2num($this->total_ttc, 'MT');
288  $sql .= ", ".price2num($this->total_tva, 'MT');
289  $sql .= ", '".$this->db->idate($this->date_debut)."'";
290  $sql .= ", '".$this->db->idate($this->date_fin)."'";
291  $sql .= ", '".$this->db->idate($now)."'";
292  $sql .= ", ".((int) $user->id);
293  $sql .= ", ".((int) $fuserid);
294  $sql .= ", ".($this->fk_user_validator > 0 ? ((int) $this->fk_user_validator) : "null");
295  $sql .= ", ".($this->fk_user_approve > 0 ? ((int) $this->fk_user_approve) : "null");
296  $sql .= ", ".($this->fk_user_modif > 0 ? ((int) $this->fk_user_modif) : "null");
297  $sql .= ", ".($this->fk_statut > 1 ? ((int) $this->fk_statut) : 0);
298  $sql .= ", ".($this->modepaymentid ? ((int) $this->modepaymentid) : "null");
299  $sql .= ", 0";
300  $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
301  $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
302  $sql .= ", ".((int) $conf->entity);
303  $sql .= ")";
304 
305  $result = $this->db->query($sql);
306  if ($result) {
307  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
308  $this->ref = '(PROV'.$this->id.')';
309 
310  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element." SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".((int) $this->id);
311  $resql = $this->db->query($sql);
312  if (!$resql) {
313  $this->error = $this->db->lasterror();
314  $error++;
315  }
316 
317  if (!$error) {
318  if (is_array($this->lines) && count($this->lines) > 0) {
319  foreach ($this->lines as $line) {
320  // Test and convert into object this->lines[$i]. When coming from REST API, we may still have an array
321  //if (! is_object($line)) $line=json_decode(json_encode($line), false); // convert recursively array into object.
322  if (!is_object($line)) {
323  $line = (object) $line;
324  $newndfline = new ExpenseReportLine($this->db);
325  $newndfline->fk_expensereport = $line->fk_expensereport;
326  $newndfline->fk_c_type_fees = $line->fk_c_type_fees;
327  $newndfline->fk_project = $line->fk_project;
328  $newndfline->vatrate = $line->vatrate;
329  $newndfline->vat_src_code = $line->vat_src_code;
330  $newndfline->localtax1_tx = $line->localtax1_tx;
331  $newndfline->localtax2_tx = $line->localtax2_tx;
332  $newndfline->localtax1_type = $line->localtax1_type;
333  $newndfline->localtax2_type = $line->localtax2_type;
334  $newndfline->comments = $line->comments;
335  $newndfline->qty = $line->qty;
336  $newndfline->value_unit = $line->value_unit;
337  $newndfline->total_ht = $line->total_ht;
338  $newndfline->total_ttc = $line->total_ttc;
339  $newndfline->total_tva = $line->total_tva;
340  $newndfline->total_localtax1 = $line->total_localtax1;
341  $newndfline->total_localtax2 = $line->total_localtax2;
342  $newndfline->date = $line->date;
343  $newndfline->rule_warning_message = $line->rule_warning_message;
344  $newndfline->fk_c_exp_tax_cat = $line->fk_c_exp_tax_cat;
345  $newndfline->fk_ecm_files = $line->fk_ecm_files;
346  } else {
347  $newndfline = $line;
348  }
349  //$newndfline=new ExpenseReportLine($this->db);
350  $newndfline->fk_expensereport = $this->id;
351  $result = $newndfline->insert();
352  if ($result < 0) {
353  $this->error = $newndfline->error;
354  $this->errors = $newndfline->errors;
355  $error++;
356  break;
357  }
358  }
359  }
360  }
361 
362  if (!$error) {
363  $result = $this->insertExtraFields();
364  if ($result < 0) {
365  $error++;
366  }
367  }
368 
369  if (!$error) {
370  $result = $this->update_price();
371  if ($result > 0) {
372  if (!$notrigger) {
373  // Call trigger
374  $result = $this->call_trigger('EXPENSE_REPORT_CREATE', $user);
375 
376  if ($result < 0) {
377  $error++;
378  }
379  // End call triggers
380  }
381 
382  if (empty($error)) {
383  $this->db->commit();
384  return $this->id;
385  } else {
386  $this->db->rollback();
387  return -4;
388  }
389  } else {
390  $this->db->rollback();
391  return -3;
392  }
393  } else {
394  dol_syslog(get_class($this)."::create error ".$this->error, LOG_ERR);
395  $this->db->rollback();
396  return -2;
397  }
398  } else {
399  $this->error = $this->db->lasterror()." sql=".$sql;
400  $this->db->rollback();
401  return -1;
402  }
403  }
404 
412  public function createFromClone(User $user, $fk_user_author)
413  {
414  global $hookmanager;
415 
416  $error = 0;
417 
418  if (empty($fk_user_author)) {
419  $fk_user_author = $user->id;
420  }
421 
422  $this->db->begin();
423 
424  // get extrafields so they will be clone
425  //foreach($this->lines as $line)
426  //$line->fetch_optionals();
427 
428  // Load source object
429  $objFrom = clone $this;
430 
431  $this->id = 0;
432  $this->ref = '';
433  $this->status = 0;
434  $this->fk_statut = 0; // deprecated
435 
436  // Clear fields
437  $this->fk_user_creat = $user->id;
438  $this->fk_user_author = $fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
439  $this->fk_user_valid = '';
440  $this->date_create = '';
441  $this->date_creation = '';
442  $this->date_validation = '';
443 
444  // Remove link on lines to a joined file
445  if (is_array($this->lines) && count($this->lines) > 0) {
446  foreach ($this->lines as $key => $line) {
447  $this->lines[$key]->fk_ecm_files = 0;
448  }
449  }
450 
451  // Create clone
452  $this->context['createfromclone'] = 'createfromclone';
453  $result = $this->create($user);
454  if ($result < 0) {
455  $error++;
456  }
457 
458  if (!$error) {
459  // Hook of thirdparty module
460  if (is_object($hookmanager)) {
461  $parameters = array('objFrom'=>$objFrom);
462  $action = '';
463  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
464  if ($reshook < 0) {
465  $this->errors += $hookmanager->errors;
466  $this->error = $hookmanager->error;
467  $error++;
468  }
469  }
470  }
471 
472  unset($this->context['createfromclone']);
473 
474  // End
475  if (!$error) {
476  $this->db->commit();
477  return $this->id;
478  } else {
479  $this->db->rollback();
480  return -1;
481  }
482  }
483 
484 
493  public function update($user, $notrigger = 0, $userofexpensereport = null)
494  {
495  global $langs;
496 
497  $error = 0;
498  $this->db->begin();
499 
500  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
501  $sql .= " total_ht = ".$this->total_ht;
502  $sql .= " , total_ttc = ".$this->total_ttc;
503  $sql .= " , total_tva = ".$this->total_tva;
504  $sql .= " , date_debut = '".$this->db->idate($this->date_debut)."'";
505  $sql .= " , date_fin = '".$this->db->idate($this->date_fin)."'";
506  if ($userofexpensereport && is_object($userofexpensereport)) {
507  $sql .= " , fk_user_author = ".($userofexpensereport->id > 0 ? $userofexpensereport->id : "null"); // Note fk_user_author is not the 'author' but the guy the expense report is for.
508  }
509  $sql .= " , fk_user_validator = ".($this->fk_user_validator > 0 ? $this->fk_user_validator : "null");
510  $sql .= " , fk_user_valid = ".($this->fk_user_valid > 0 ? $this->fk_user_valid : "null");
511  $sql .= " , fk_user_approve = ".($this->fk_user_approve > 0 ? $this->fk_user_approve : "null");
512  $sql .= " , fk_user_modif = ".$user->id;
513  $sql .= " , fk_statut = ".($this->fk_statut >= 0 ? $this->fk_statut : '0');
514  $sql .= " , fk_c_paiement = ".($this->fk_c_paiement > 0 ? $this->fk_c_paiement : "null");
515  $sql .= " , note_public = ".(!empty($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "''");
516  $sql .= " , note_private = ".(!empty($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "''");
517  $sql .= " , detail_refuse = ".(!empty($this->detail_refuse) ? "'".$this->db->escape($this->detail_refuse)."'" : "''");
518  $sql .= " WHERE rowid = ".((int) $this->id);
519 
520  dol_syslog(get_class($this)."::update", LOG_DEBUG);
521  $result = $this->db->query($sql);
522  if ($result) {
523  if (!$notrigger) {
524  // Call trigger
525  $result = $this->call_trigger('EXPENSE_REPORT_MODIFY', $user);
526 
527  if ($result < 0) {
528  $error++;
529  }
530  // End call triggers
531  }
532 
533  if (empty($error)) {
534  $this->db->commit();
535  return 1;
536  } else {
537  $this->db->rollback();
538  $this->error = $this->db->error();
539  return -2;
540  }
541  } else {
542  $this->db->rollback();
543  $this->error = $this->db->error();
544  return -1;
545  }
546  }
547 
555  public function fetch($id, $ref = '')
556  {
557  global $conf;
558 
559  $sql = "SELECT d.rowid, d.entity, d.ref, d.note_public, d.note_private,"; // DEFAULT
560  $sql .= " d.detail_refuse, d.detail_cancel, d.fk_user_refuse, d.fk_user_cancel,"; // ACTIONS
561  $sql .= " d.date_refuse, d.date_cancel,"; // ACTIONS
562  $sql .= " d.total_ht, d.total_ttc, d.total_tva,";
563  $sql .= " d.localtax1 as total_localtax1, d.localtax2 as total_localtax2,";
564  $sql .= " d.date_debut, d.date_fin, d.date_create, d.tms as date_modif, d.date_valid, d.date_approve,"; // DATES (datetime)
565  $sql .= " d.fk_user_creat, d.fk_user_author, d.fk_user_modif, d.fk_user_validator,";
566  $sql .= " d.fk_user_valid, d.fk_user_approve,";
567  $sql .= " d.fk_statut as status, d.fk_c_paiement, d.paid";
568  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as d";
569  if ($ref) {
570  $sql .= " WHERE d.ref = '".$this->db->escape($ref)."'";
571  } else {
572  $sql .= " WHERE d.rowid = ".((int) $id);
573  }
574  //$sql.= $restrict;
575 
576  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
577  $resql = $this->db->query($sql);
578  if ($resql) {
579  $obj = $this->db->fetch_object($resql);
580  if ($obj) {
581  $this->id = $obj->rowid;
582  $this->ref = $obj->ref;
583 
584  $this->entity = $obj->entity;
585 
586  $this->total_ht = $obj->total_ht;
587  $this->total_tva = $obj->total_tva;
588  $this->total_ttc = $obj->total_ttc;
589  $this->localtax1 = $obj->total_localtax1; // For backward compatibility
590  $this->localtax2 = $obj->total_localtax2; // For backward compatibility
591  $this->total_localtax1 = $obj->total_localtax1;
592  $this->total_localtax2 = $obj->total_localtax2;
593 
594  $this->note_public = $obj->note_public;
595  $this->note_private = $obj->note_private;
596  $this->detail_refuse = $obj->detail_refuse;
597  $this->detail_cancel = $obj->detail_cancel;
598 
599  $this->date_debut = $this->db->jdate($obj->date_debut);
600  $this->date_fin = $this->db->jdate($obj->date_fin);
601  $this->date_valid = $this->db->jdate($obj->date_valid);
602  $this->date_approve = $this->db->jdate($obj->date_approve);
603  $this->date_create = $this->db->jdate($obj->date_create);
604  $this->date_modif = $this->db->jdate($obj->date_modif);
605  $this->date_refuse = $this->db->jdate($obj->date_refuse);
606  $this->date_cancel = $this->db->jdate($obj->date_cancel);
607 
608  $this->fk_user_creat = $obj->fk_user_creat;
609  $this->fk_user_author = $obj->fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
610  $this->fk_user_modif = $obj->fk_user_modif;
611  $this->fk_user_validator = $obj->fk_user_validator;
612  $this->fk_user_valid = $obj->fk_user_valid;
613  $this->fk_user_refuse = $obj->fk_user_refuse;
614  $this->fk_user_cancel = $obj->fk_user_cancel;
615  $this->fk_user_approve = $obj->fk_user_approve;
616 
617  $user_author = new User($this->db);
618  if ($this->fk_user_author > 0) {
619  $user_author->fetch($this->fk_user_author);
620  }
621 
622  $this->user_author_infos = dolGetFirstLastname($user_author->firstname, $user_author->lastname);
623 
624  $user_approver = new User($this->db);
625  if ($this->fk_user_approve > 0) {
626  $user_approver->fetch($this->fk_user_approve);
627  } elseif ($this->fk_user_validator > 0) {
628  $user_approver->fetch($this->fk_user_validator); // For backward compatibility
629  }
630  $this->user_validator_infos = dolGetFirstLastname($user_approver->firstname, $user_approver->lastname);
631 
632  $this->fk_statut = $obj->status; // deprecated
633  $this->status = $obj->status;
634  $this->fk_c_paiement = $obj->fk_c_paiement;
635  $this->paid = $obj->paid;
636 
637  if ($this->status == self::STATUS_APPROVED || $this->status == self::STATUS_CLOSED) {
638  $user_valid = new User($this->db);
639  if ($this->fk_user_valid > 0) {
640  $user_valid->fetch($this->fk_user_valid);
641  }
642  $this->user_valid_infos = dolGetFirstLastname($user_valid->firstname, $user_valid->lastname);
643  }
644 
645  $this->fetch_optionals();
646 
647  $result = $this->fetch_lines();
648 
649  return $result;
650  } else {
651  return 0;
652  }
653  } else {
654  $this->error = $this->db->lasterror();
655  return -1;
656  }
657  }
658 
659  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
670  public function set_paid($id, $fuser, $notrigger = 0)
671  {
672  // phpcs:enable
673  dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
674  return $this->setPaid($id, $fuser, $notrigger);
675  }
676 
685  public function setPaid($id, $fuser, $notrigger = 0)
686  {
687  $error = 0;
688  $this->db->begin();
689 
690  $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport";
691  $sql .= " SET fk_statut = ".self::STATUS_CLOSED.", paid=1";
692  $sql .= " WHERE rowid = ".((int) $id)." AND fk_statut = ".self::STATUS_APPROVED;
693 
694  dol_syslog(get_class($this)."::set_paid", LOG_DEBUG);
695  $resql = $this->db->query($sql);
696  if ($resql) {
697  if ($this->db->affected_rows($resql)) {
698  if (!$notrigger) {
699  // Call trigger
700  $result = $this->call_trigger('EXPENSE_REPORT_PAID', $fuser);
701 
702  if ($result < 0) {
703  $error++;
704  }
705  // End call triggers
706  }
707 
708  if (empty($error)) {
709  $this->db->commit();
710  return 1;
711  } else {
712  $this->db->rollback();
713  $this->error = $this->db->error();
714  return -2;
715  }
716  } else {
717  $this->db->commit();
718  return 0;
719  }
720  } else {
721  $this->db->rollback();
722  dol_print_error($this->db);
723  return -1;
724  }
725  }
726 
733  public function getLibStatut($mode = 0)
734  {
735  return $this->LibStatut($this->status, $mode);
736  }
737 
738  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
746  public function LibStatut($status, $mode = 0)
747  {
748  // phpcs:enable
749  global $langs;
750 
751  $labelStatus = $langs->transnoentitiesnoconv($this->statuts[$status]);
752  $labelStatusShort = $langs->transnoentitiesnoconv($this->statuts_short[$status]);
753 
754  $statusType = $this->statuts_logo[$status];
755 
756  return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
757  }
758 
759 
766  public function info($id)
767  {
768  global $conf;
769 
770  $sql = "SELECT f.rowid,";
771  $sql .= " f.date_create as datec,";
772  $sql .= " f.tms as date_modification,";
773  $sql .= " f.date_valid as datev,";
774  $sql .= " f.date_approve as datea,";
775  $sql .= " f.fk_user_creat as fk_user_creation,";
776  $sql .= " f.fk_user_modif as fk_user_modification,";
777  $sql .= " f.fk_user_valid,";
778  $sql .= " f.fk_user_approve";
779  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as f";
780  $sql .= " WHERE f.rowid = ".((int) $id);
781  $sql .= " AND f.entity = ".$conf->entity;
782 
783  $resql = $this->db->query($sql);
784  if ($resql) {
785  if ($this->db->num_rows($resql)) {
786  $obj = $this->db->fetch_object($resql);
787 
788  $this->id = $obj->rowid;
789 
790  $this->date_creation = $this->db->jdate($obj->datec);
791  $this->date_modification = $this->db->jdate($obj->date_modification);
792  $this->date_validation = $this->db->jdate($obj->datev);
793  $this->date_approbation = $this->db->jdate($obj->datea);
794 
795  $cuser = new User($this->db);
796  $cuser->fetch($obj->fk_user_author);
797  $this->user_creation = $cuser;
798 
799  if ($obj->fk_user_creation) {
800  $cuser = new User($this->db);
801  $cuser->fetch($obj->fk_user_creation);
802  $this->user_creation = $cuser;
803  }
804  if ($obj->fk_user_valid) {
805  $vuser = new User($this->db);
806  $vuser->fetch($obj->fk_user_valid);
807  $this->user_validation = $vuser;
808  }
809  if ($obj->fk_user_modification) {
810  $muser = new User($this->db);
811  $muser->fetch($obj->fk_user_modification);
812  $this->user_modification = $muser;
813  }
814  if ($obj->fk_user_approve) {
815  $auser = new User($this->db);
816  $auser->fetch($obj->fk_user_approve);
817  $this->user_approve = $auser;
818  }
819  }
820  $this->db->free($resql);
821  } else {
822  dol_print_error($this->db);
823  }
824  }
825 
826 
827 
835  public function initAsSpecimen()
836  {
837  global $user, $langs, $conf;
838 
839  $now = dol_now();
840 
841  // Initialise parametres
842  $this->id = 0;
843  $this->ref = 'SPECIMEN';
844  $this->specimen = 1;
845  $this->entity = 1;
846  $this->date_create = $now;
847  $this->date_debut = $now;
848  $this->date_fin = $now;
849  $this->date_valid = $now;
850  $this->date_approve = $now;
851 
852  $type_fees_id = 2; // TF_TRIP
853 
854  $this->status = 5;
855  $this->fk_statut = 5;
856 
857  $this->fk_user_author = $user->id;
858  $this->fk_user_validator = $user->id;
859  $this->fk_user_valid = $user->id;
860  $this->fk_user_approve = $user->id;
861 
862  $this->note_private = 'Private note';
863  $this->note_public = 'SPECIMEN';
864  $nbp = 5;
865  $xnbp = 0;
866  while ($xnbp < $nbp) {
867  $line = new ExpenseReportLine($this->db);
868  $line->comments = $langs->trans("Comment")." ".$xnbp;
869  $line->date = ($now - 3600 * (1 + $xnbp));
870  $line->total_ht = 100;
871  $line->total_tva = 20;
872  $line->total_ttc = 120;
873  $line->qty = 1;
874  $line->vatrate = 20;
875  $line->value_unit = 120;
876  $line->fk_expensereport = 0;
877  $line->type_fees_code = 'TRA';
878  $line->fk_c_type_fees = $type_fees_id;
879 
880  $line->projet_ref = 'ABC';
881 
882  $this->lines[$xnbp] = $line;
883  $xnbp++;
884 
885  $this->total_ht += $line->total_ht;
886  $this->total_tva += $line->total_tva;
887  $this->total_ttc += $line->total_ttc;
888  }
889  }
890 
891  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
899  public function fetch_line_by_project($projectid, $user = '')
900  {
901  // phpcs:enable
902  global $conf, $db, $langs;
903 
904  $langs->load('trips');
905 
906  if ($user->rights->expensereport->lire) {
907  $sql = "SELECT de.fk_expensereport, de.date, de.comments, de.total_ht, de.total_ttc";
908  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_det as de";
909  $sql .= " WHERE de.fk_projet = ".((int) $projectid);
910 
911  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
912  $result = $this->db->query($sql);
913  if ($result) {
914  $num = $this->db->num_rows($result);
915  $i = 0;
916  $total_HT = 0;
917  $total_TTC = 0;
918 
919  while ($i < $num) {
920  $objp = $this->db->fetch_object($result);
921 
922  $sql2 = "SELECT d.rowid, d.fk_user_author, d.ref, d.fk_statut as status";
923  $sql2 .= " FROM ".MAIN_DB_PREFIX."expensereport as d";
924  $sql2 .= " WHERE d.rowid = ".((int) $objp->fk_expensereport);
925 
926  $result2 = $this->db->query($sql2);
927  $obj = $this->db->fetch_object($result2);
928 
929  $objp->fk_user_author = $obj->fk_user_author;
930  $objp->ref = $obj->ref;
931  $objp->fk_c_expensereport_status = $obj->status;
932  $objp->rowid = $obj->rowid;
933 
934  $total_HT = $total_HT + $objp->total_ht;
935  $total_TTC = $total_TTC + $objp->total_ttc;
936  $author = new User($this->db);
937  $author->fetch($objp->fk_user_author);
938 
939  print '<tr>';
940  print '<td><a href="'.DOL_URL_ROOT.'/expensereport/card.php?id='.$objp->rowid.'">'.$objp->ref_num.'</a></td>';
941  print '<td class="center">'.dol_print_date($objp->date, 'day').'</td>';
942  print '<td>'.$author->getNomUrl(1).'</td>';
943  print '<td>'.$objp->comments.'</td>';
944  print '<td class="right">'.price($objp->total_ht).'</td>';
945  print '<td class="right">'.price($objp->total_ttc).'</td>';
946  print '<td class="right">';
947 
948  switch ($objp->fk_c_expensereport_status) {
949  case 4:
950  print img_picto($langs->trans('StatusOrderCanceled'), 'statut5');
951  break;
952  case 1:
953  print $langs->trans('Draft').' '.img_picto($langs->trans('Draft'), 'statut0');
954  break;
955  case 2:
956  print $langs->trans('TripForValid').' '.img_picto($langs->trans('TripForValid'), 'statut3');
957  break;
958  case 5:
959  print $langs->trans('TripForPaid').' '.img_picto($langs->trans('TripForPaid'), 'statut3');
960  break;
961  case 6:
962  print $langs->trans('TripPaid').' '.img_picto($langs->trans('TripPaid'), 'statut4');
963  break;
964  }
965  /*
966  if ($status==4) return img_picto($langs->trans('StatusOrderCanceled'),'statut5');
967  if ($status==1) return img_picto($langs->trans('StatusOrderDraft'),'statut0');
968  if ($status==2) return img_picto($langs->trans('StatusOrderValidated'),'statut1');
969  if ($status==2) return img_picto($langs->trans('StatusOrderOnProcess'),'statut3');
970  if ($status==5) return img_picto($langs->trans('StatusOrderToBill'),'statut4');
971  if ($status==6) return img_picto($langs->trans('StatusOrderOnProcess'),'statut6');
972  */
973  print '</td>';
974  print '</tr>';
975 
976  $i++;
977  }
978 
979  print '<tr class="liste_total"><td colspan="4">'.$langs->trans("Number").': '.$i.'</td>';
980  print '<td class="right" width="100">'.$langs->trans("TotalHT").' : '.price($total_HT).'</td>';
981  print '<td class="right" width="100">'.$langs->trans("TotalTTC").' : '.price($total_TTC).'</td>';
982  print '<td>&nbsp;</td>';
983  print '</tr>';
984  } else {
985  $this->error = $this->db->lasterror();
986  return -1;
987  }
988  }
989  }
990 
991  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
997  public function fetch_lines()
998  {
999  // phpcs:enable
1000  global $conf;
1001 
1002  $this->lines = array();
1003 
1004  $sql = ' SELECT de.rowid, de.comments, de.qty, de.value_unit, de.date, de.rang,';
1005  $sql .= " de.".$this->fk_element.", de.fk_c_type_fees, de.fk_c_exp_tax_cat, de.fk_projet as fk_project,";
1006  $sql .= ' de.tva_tx, de.vat_src_code,';
1007  $sql .= ' de.localtax1_tx, de.localtax2_tx, de.localtax1_type, de.localtax2_type,';
1008  $sql .= ' de.fk_ecm_files,';
1009  $sql .= ' de.total_ht, de.total_tva, de.total_ttc,';
1010  $sql .= ' de.total_localtax1, de.total_localtax2, de.rule_warning_message,';
1011  $sql .= ' ctf.code as code_type_fees, ctf.label as libelle_type_fees, ctf.accountancy_code as accountancy_code_type_fees,';
1012  $sql .= ' p.ref as ref_projet, p.title as title_projet';
1013  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as de';
1014  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON de.fk_c_type_fees = ctf.id';
1015  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as p ON de.fk_projet = p.rowid';
1016  $sql .= " WHERE de.".$this->fk_element." = ".((int) $this->id);
1017  if (!empty($conf->global->EXPENSEREPORT_LINES_SORTED_BY_ROWID)) {
1018  $sql .= ' ORDER BY de.rang ASC, de.rowid ASC';
1019  } else {
1020  $sql .= ' ORDER BY de.rang ASC, de.date ASC';
1021  }
1022 
1023  $resql = $this->db->query($sql);
1024  if ($resql) {
1025  $num = $this->db->num_rows($resql);
1026  $i = 0;
1027  while ($i < $num) {
1028  $objp = $this->db->fetch_object($resql);
1029 
1030  $deplig = new ExpenseReportLine($this->db);
1031 
1032  $deplig->rowid = $objp->rowid;
1033  $deplig->id = $objp->rowid;
1034  $deplig->comments = $objp->comments;
1035  $deplig->qty = $objp->qty;
1036  $deplig->value_unit = $objp->value_unit;
1037  $deplig->date = $objp->date;
1038  $deplig->dates = $this->db->jdate($objp->date);
1039 
1040  $deplig->fk_expensereport = $objp->fk_expensereport;
1041  $deplig->fk_c_type_fees = $objp->fk_c_type_fees;
1042  $deplig->fk_c_exp_tax_cat = $objp->fk_c_exp_tax_cat;
1043  $deplig->fk_projet = $objp->fk_project; // deprecated
1044  $deplig->fk_project = $objp->fk_project;
1045  $deplig->fk_ecm_files = $objp->fk_ecm_files;
1046 
1047  $deplig->total_ht = $objp->total_ht;
1048  $deplig->total_tva = $objp->total_tva;
1049  $deplig->total_ttc = $objp->total_ttc;
1050  $deplig->total_localtax1 = $objp->total_localtax1;
1051  $deplig->total_localtax2 = $objp->total_localtax2;
1052 
1053  $deplig->type_fees_code = empty($objp->code_type_fees) ? 'TF_OTHER' : $objp->code_type_fees;
1054  $deplig->type_fees_libelle = $objp->libelle_type_fees;
1055  $deplig->type_fees_accountancy_code = $objp->accountancy_code_type_fees;
1056 
1057  $deplig->tva_tx = $objp->tva_tx;
1058  $deplig->vatrate = $objp->tva_tx;
1059  $deplig->vat_src_code = $objp->vat_src_code;
1060  $deplig->localtax1_tx = $objp->localtax1_tx;
1061  $deplig->localtax2_tx = $objp->localtax2_tx;
1062  $deplig->localtax1_type = $objp->localtax1_type;
1063  $deplig->localtax2_type = $objp->localtax2_type;
1064 
1065  $deplig->projet_ref = $objp->ref_projet;
1066  $deplig->projet_title = $objp->title_projet;
1067 
1068  $deplig->rule_warning_message = $objp->rule_warning_message;
1069 
1070  $deplig->rang = $objp->rang;
1071 
1072  $this->lines[$i] = $deplig;
1073 
1074  $i++;
1075  }
1076  $this->db->free($resql);
1077  return 1;
1078  } else {
1079  $this->error = $this->db->lasterror();
1080  dol_syslog(get_class($this)."::fetch_lines: Error ".$this->error, LOG_ERR);
1081  return -3;
1082  }
1083  }
1084 
1085 
1093  public function delete(User $user = null, $notrigger = false)
1094  {
1095  global $conf;
1096  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1097 
1098  $error = 0;
1099 
1100  $this->db->begin();
1101 
1102  if (!$notrigger) {
1103  // Call trigger
1104  $result = $this->call_trigger('EXPENSE_REPORT_DELETE', $user);
1105  if ($result < 0) {
1106  $error++;
1107  }
1108  // End call triggers
1109  }
1110 
1111  // Delete extrafields of lines and lines
1112  if (!$error && !empty($this->table_element_line)) {
1113  $tabletodelete = $this->table_element_line;
1114  //$sqlef = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete."_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".((int) $this->id).")";
1115  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".((int) $this->id);
1116  if (!$this->db->query($sql)) {
1117  $error++;
1118  $this->error = $this->db->lasterror();
1119  $this->errors[] = $this->error;
1120  dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
1121  }
1122  }
1123 
1124  if (!$error) {
1125  // Delete linked object
1126  $res = $this->deleteObjectLinked();
1127  if ($res < 0) {
1128  $error++;
1129  }
1130  }
1131 
1132  if (!$error) {
1133  // Delete linked contacts
1134  $res = $this->delete_linked_contact();
1135  if ($res < 0) {
1136  $error++;
1137  }
1138  }
1139 
1140  // Removed extrafields of object
1141  if (!$error) {
1142  $result = $this->deleteExtraFields();
1143  if ($result < 0) {
1144  $error++;
1145  dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
1146  }
1147  }
1148 
1149  // Delete main record
1150  if (!$error) {
1151  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id);
1152  $res = $this->db->query($sql);
1153  if (!$res) {
1154  $error++;
1155  $this->error = $this->db->lasterror();
1156  $this->errors[] = $this->error;
1157  dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
1158  }
1159  }
1160 
1161  // Delete record into ECM index and physically
1162  if (!$error) {
1163  $res = $this->deleteEcmFiles(0); // Deleting files physically is done later with the dol_delete_dir_recursive
1164  if (!$res) {
1165  $error++;
1166  }
1167  }
1168 
1169  if (!$error) {
1170  // We remove directory
1171  $ref = dol_sanitizeFileName($this->ref);
1172  if ($conf->expensereport->multidir_output[$this->entity] && !empty($this->ref)) {
1173  $dir = $conf->expensereport->multidir_output[$this->entity]."/".$ref;
1174  $file = $dir."/".$ref.".pdf";
1175  if (file_exists($file)) {
1176  dol_delete_preview($this);
1177 
1178  if (!dol_delete_file($file, 0, 0, 0, $this)) {
1179  $this->error = 'ErrorFailToDeleteFile';
1180  $this->errors[] = $this->error;
1181  $this->db->rollback();
1182  return 0;
1183  }
1184  }
1185  if (file_exists($dir)) {
1186  $res = @dol_delete_dir_recursive($dir);
1187  if (!$res) {
1188  $this->error = 'ErrorFailToDeleteDir';
1189  $this->errors[] = $this->error;
1190  $this->db->rollback();
1191  return 0;
1192  }
1193  }
1194  }
1195  }
1196 
1197  if (!$error) {
1198  dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
1199  $this->db->commit();
1200  return 1;
1201  } else {
1202  $this->db->rollback();
1203  return -1;
1204  }
1205  }
1206 
1214  public function setValidate($fuser, $notrigger = 0)
1215  {
1216  global $conf, $langs, $user;
1217 
1218  $error = 0;
1219  $now = dol_now();
1220 
1221  // Protection
1222  if ($this->status == self::STATUS_VALIDATED) {
1223  dol_syslog(get_class($this)."::valid action abandonned: already validated", LOG_WARNING);
1224  return 0;
1225  }
1226 
1227  $this->date_valid = $now; // Required for the getNextNum later.
1228 
1229  // Define new ref
1230  if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
1231  $num = $this->getNextNumRef();
1232  } else {
1233  $num = $this->ref;
1234  }
1235  if (empty($num) || $num < 0) {
1236  return -1;
1237  }
1238 
1239  $this->newref = dol_sanitizeFileName($num);
1240 
1241  $this->db->begin();
1242 
1243  // Validate
1244  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1245  $sql .= " SET ref = '".$this->db->escape($num)."',";
1246  $sql .= " fk_statut = ".self::STATUS_VALIDATED.",";
1247  $sql .= " date_valid = '".$this->db->idate($this->date_valid)."',";
1248  $sql .= " fk_user_valid = ".((int) $user->id);
1249  $sql .= " WHERE rowid = ".((int) $this->id);
1250 
1251  $resql = $this->db->query($sql);
1252  if ($resql) {
1253  if (!$error && !$notrigger) {
1254  // Call trigger
1255  $result = $this->call_trigger('EXPENSE_REPORT_VALIDATE', $fuser);
1256  if ($result < 0) {
1257  $error++;
1258  }
1259  // End call triggers
1260  }
1261 
1262  if (!$error) {
1263  $this->oldref = $this->ref;
1264 
1265  // Rename directory if dir was a temporary ref
1266  if (preg_match('/^[\(]?PROV/i', $this->ref)) {
1267  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1268 
1269  // Now we rename also files into index
1270  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'expensereport/".$this->db->escape($this->newref)."'";
1271  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'expensereport/".$this->db->escape($this->ref)."' AND entity = ".((int) $this->entity);
1272  $resql = $this->db->query($sql);
1273  if (!$resql) {
1274  $error++; $this->error = $this->db->lasterror();
1275  }
1276 
1277  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
1278  $oldref = dol_sanitizeFileName($this->ref);
1279  $newref = dol_sanitizeFileName($num);
1280  $dirsource = $conf->expensereport->multidir_output[$this->entity].'/'.$oldref;
1281  $dirdest = $conf->expensereport->multidir_output[$this->entity].'/'.$newref;
1282  if (!$error && file_exists($dirsource)) {
1283  dol_syslog(get_class($this)."::setValidate() rename dir ".$dirsource." into ".$dirdest);
1284 
1285  if (@rename($dirsource, $dirdest)) {
1286  dol_syslog("Rename ok");
1287  // Rename docs starting with $oldref with $newref
1288  $listoffiles = dol_dir_list($dirdest, 'files', 1, '^'.preg_quote($oldref, '/'));
1289  foreach ($listoffiles as $fileentry) {
1290  $dirsource = $fileentry['name'];
1291  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
1292  $dirsource = $fileentry['path'].'/'.$dirsource;
1293  $dirdest = $fileentry['path'].'/'.$dirdest;
1294  @rename($dirsource, $dirdest);
1295  }
1296  }
1297  }
1298  }
1299  }
1300 
1301  // Set new ref and current status
1302  if (!$error) {
1303  $this->ref = $num;
1304  $this->status = self::STATUS_VALIDATED;
1305  }
1306 
1307  if (empty($error)) {
1308  $this->db->commit();
1309  return 1;
1310  } else {
1311  $this->db->rollback();
1312  $this->error = $this->db->error();
1313  return -2;
1314  }
1315  } else {
1316  $this->db->rollback();
1317  $this->error = $this->db->lasterror();
1318  return -1;
1319  }
1320  }
1321 
1322  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1329  public function set_save_from_refuse($fuser)
1330  {
1331  // phpcs:enable
1332  global $conf, $langs;
1333 
1334  // Sélection de la date de début de la NDF
1335  $sql = 'SELECT date_debut';
1336  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element;
1337  $sql .= " WHERE rowid = ".((int) $this->id);
1338 
1339  $result = $this->db->query($sql);
1340 
1341  $objp = $this->db->fetch_object($result);
1342 
1343  $this->date_debut = $this->db->jdate($objp->date_debut);
1344 
1345  if ($this->status != self::STATUS_VALIDATED) {
1346  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1347  $sql .= " SET fk_statut = ".self::STATUS_VALIDATED;
1348  $sql .= " WHERE rowid = ".((int) $this->id);
1349 
1350  dol_syslog(get_class($this)."::set_save_from_refuse", LOG_DEBUG);
1351 
1352  if ($this->db->query($sql)) {
1353  return 1;
1354  } else {
1355  $this->error = $this->db->lasterror();
1356  return -1;
1357  }
1358  } else {
1359  dol_syslog(get_class($this)."::set_save_from_refuse expensereport already with save status", LOG_WARNING);
1360  }
1361  }
1362 
1370  public function setApproved($fuser, $notrigger = 0)
1371  {
1372  $now = dol_now();
1373  $error = 0;
1374 
1375  // date approval
1376  $this->date_approve = $now;
1377  if ($this->status != self::STATUS_APPROVED) {
1378  $this->db->begin();
1379 
1380  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1381  $sql .= " SET ref = '".$this->db->escape($this->ref)."', fk_statut = ".self::STATUS_APPROVED.", fk_user_approve = ".((int) $fuser->id).",";
1382  $sql .= " date_approve='".$this->db->idate($this->date_approve)."'";
1383  $sql .= " WHERE rowid = ".((int) $this->id);
1384  if ($this->db->query($sql)) {
1385  if (!$notrigger) {
1386  // Call trigger
1387  $result = $this->call_trigger('EXPENSE_REPORT_APPROVE', $fuser);
1388 
1389  if ($result < 0) {
1390  $error++;
1391  }
1392  // End call triggers
1393  }
1394 
1395  if (empty($error)) {
1396  $this->db->commit();
1397  return 1;
1398  } else {
1399  $this->db->rollback();
1400  $this->error = $this->db->error();
1401  return -2;
1402  }
1403  } else {
1404  $this->db->rollback();
1405  $this->error = $this->db->lasterror();
1406  return -1;
1407  }
1408  } else {
1409  dol_syslog(get_class($this)."::setApproved expensereport already with approve status", LOG_WARNING);
1410  }
1411 
1412  return 0;
1413  }
1414 
1423  public function setDeny($fuser, $details, $notrigger = 0)
1424  {
1425  $now = dol_now();
1426  $error = 0;
1427 
1428  // date de refus
1429  if ($this->status != self::STATUS_REFUSED) {
1430  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1431  $sql .= " SET ref = '".$this->db->escape($this->ref)."', fk_statut = ".self::STATUS_REFUSED.", fk_user_refuse = ".((int) $fuser->id).",";
1432  $sql .= " date_refuse='".$this->db->idate($now)."',";
1433  $sql .= " detail_refuse='".$this->db->escape($details)."',";
1434  $sql .= " fk_user_approve = NULL";
1435  $sql .= " WHERE rowid = ".((int) $this->id);
1436  if ($this->db->query($sql)) {
1437  $this->fk_statut = 99; // deprecated
1438  $this->status = 99;
1439  $this->fk_user_refuse = $fuser->id;
1440  $this->detail_refuse = $details;
1441  $this->date_refuse = $now;
1442 
1443  if (!$notrigger) {
1444  // Call trigger
1445  $result = $this->call_trigger('EXPENSE_REPORT_DENY', $fuser);
1446 
1447  if ($result < 0) {
1448  $error++;
1449  }
1450  // End call triggers
1451  }
1452 
1453  if (empty($error)) {
1454  $this->db->commit();
1455  return 1;
1456  } else {
1457  $this->db->rollback();
1458  $this->error = $this->db->error();
1459  return -2;
1460  }
1461  } else {
1462  $this->db->rollback();
1463  $this->error = $this->db->lasterror();
1464  return -1;
1465  }
1466  } else {
1467  dol_syslog(get_class($this)."::setDeny expensereport already with refuse status", LOG_WARNING);
1468  }
1469  }
1470 
1471  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1481  public function set_unpaid($fuser, $notrigger = 0)
1482  {
1483  // phpcs:enable
1484  dol_syslog(get_class($this)."::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
1485  return $this->setUnpaid($fuser, $notrigger);
1486  }
1487 
1495  public function setUnpaid($fuser, $notrigger = 0)
1496  {
1497  $error = 0;
1498 
1499  if ($this->paid) {
1500  $this->db->begin();
1501 
1502  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1503  $sql .= " SET paid = 0, fk_statut = ".self::STATUS_APPROVED;
1504  $sql .= " WHERE rowid = ".((int) $this->id);
1505 
1506  dol_syslog(get_class($this)."::set_unpaid", LOG_DEBUG);
1507 
1508  if ($this->db->query($sql)) {
1509  if (!$notrigger) {
1510  // Call trigger
1511  $result = $this->call_trigger('EXPENSE_REPORT_UNPAID', $fuser);
1512 
1513  if ($result < 0) {
1514  $error++;
1515  }
1516  // End call triggers
1517  }
1518 
1519  if (empty($error)) {
1520  $this->db->commit();
1521  return 1;
1522  } else {
1523  $this->db->rollback();
1524  $this->error = $this->db->error();
1525  return -2;
1526  }
1527  } else {
1528  $this->db->rollback();
1529  $this->error = $this->db->error();
1530  return -1;
1531  }
1532  } else {
1533  dol_syslog(get_class($this)."::set_unpaid expensereport already with unpaid status", LOG_WARNING);
1534  }
1535  }
1536 
1537  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1546  public function set_cancel($fuser, $detail, $notrigger = 0)
1547  {
1548  // phpcs:enable
1549  $error = 0;
1550  $this->date_cancel = $this->db->idate(dol_now());
1551  if ($this->status != self::STATUS_CANCELED) {
1552  $this->db->begin();
1553 
1554  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1555  $sql .= " SET fk_statut = ".self::STATUS_CANCELED.", fk_user_cancel = ".((int) $fuser->id);
1556  $sql .= ", date_cancel='".$this->db->idate($this->date_cancel)."'";
1557  $sql .= " ,detail_cancel='".$this->db->escape($detail)."'";
1558  $sql .= " WHERE rowid = ".((int) $this->id);
1559 
1560  dol_syslog(get_class($this)."::set_cancel", LOG_DEBUG);
1561 
1562  if ($this->db->query($sql)) {
1563  if (!$notrigger) {
1564  // Call trigger
1565  $result = $this->call_trigger('EXPENSE_REPORT_CANCEL', $fuser);
1566 
1567  if ($result < 0) {
1568  $error++;
1569  }
1570  // End call triggers
1571  }
1572 
1573  if (empty($error)) {
1574  $this->db->commit();
1575  return 1;
1576  } else {
1577  $this->db->rollback();
1578  $this->error = $this->db->error();
1579  return -2;
1580  }
1581  } else {
1582  $this->db->rollback();
1583  $this->error = $this->db->error();
1584  return -1;
1585  }
1586  } else {
1587  dol_syslog(get_class($this)."::set_cancel expensereport already with cancel status", LOG_WARNING);
1588  }
1589  }
1590 
1596  public function getNextNumRef()
1597  {
1598  global $langs, $conf;
1599  $langs->load("trips");
1600 
1601  if (!empty($conf->global->EXPENSEREPORT_ADDON)) {
1602  $mybool = false;
1603 
1604  $file = $conf->global->EXPENSEREPORT_ADDON.".php";
1605  $classname = $conf->global->EXPENSEREPORT_ADDON;
1606 
1607  // Include file with class
1608  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
1609  foreach ($dirmodels as $reldir) {
1610  $dir = dol_buildpath($reldir."core/modules/expensereport/");
1611 
1612  // Load file with numbering class (if found)
1613  $mybool |= @include_once $dir.$file;
1614  }
1615 
1616  if ($mybool === false) {
1617  dol_print_error('', "Failed to include file ".$file);
1618  return '';
1619  }
1620 
1621  $obj = new $classname();
1622  $numref = $obj->getNextValue($this);
1623 
1624  if ($numref != "") {
1625  return $numref;
1626  } else {
1627  $this->error = $obj->error;
1628  $this->errors = $obj->errors;
1629  //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
1630  return -1;
1631  }
1632  } else {
1633  $this->error = "Error_EXPENSEREPORT_ADDON_NotDefined";
1634  return -2;
1635  }
1636  }
1637 
1650  public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1)
1651  {
1652  global $langs, $conf, $hookmanager;
1653 
1654  $result = '';
1655 
1656  $url = DOL_URL_ROOT.'/expensereport/card.php?id='.$this->id;
1657 
1658  if ($short) {
1659  return $url;
1660  }
1661 
1662  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("ExpenseReport").'</u>';
1663  if (isset($this->status)) {
1664  $label .= ' '.$this->getLibStatut(5);
1665  }
1666  if (!empty($this->ref)) {
1667  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1668  }
1669  if (!empty($this->total_ht)) {
1670  $label .= '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
1671  }
1672  if (!empty($this->total_tva)) {
1673  $label .= '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
1674  }
1675  if (!empty($this->total_ttc)) {
1676  $label .= '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
1677  }
1678  if ($moretitle) {
1679  $label .= ' - '.$moretitle;
1680  }
1681 
1682  if ($option != 'nolink') {
1683  // Add param to save lastsearch_values or not
1684  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1685  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1686  $add_save_lastsearch_values = 1;
1687  }
1688  if ($add_save_lastsearch_values) {
1689  $url .= '&save_lastsearch_values=1';
1690  }
1691  }
1692 
1693  $ref = $this->ref;
1694  if (empty($ref)) {
1695  $ref = $this->id;
1696  }
1697 
1698  $linkclose = '';
1699  if (empty($notooltip)) {
1700  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1701  $label = $langs->trans("ShowExpenseReport");
1702  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1703  }
1704  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1705  $linkclose .= ' class="classfortooltip"';
1706  }
1707 
1708  $linkstart = '<a href="'.$url.'"';
1709  $linkstart .= $linkclose.'>';
1710  $linkend = '</a>';
1711 
1712  $result .= $linkstart;
1713  if ($withpicto) {
1714  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1715  }
1716  if ($withpicto != 2) {
1717  $result .= ($max ? dol_trunc($ref, $max) : $ref);
1718  }
1719  $result .= $linkend;
1720 
1721  global $action;
1722  $hookmanager->initHooks(array($this->element . 'dao'));
1723  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1724  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1725  if ($reshook > 0) {
1726  $result = $hookmanager->resPrint;
1727  } else {
1728  $result .= $hookmanager->resPrint;
1729  }
1730  return $result;
1731  }
1732 
1733  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1741  public function update_totaux_add($ligne_total_ht, $ligne_total_tva)
1742  {
1743  // phpcs:enable
1744  $this->total_ht = $this->total_ht + $ligne_total_ht;
1745  $this->total_tva = $this->total_tva + $ligne_total_tva;
1746  $this->total_ttc = $this->total_ht + $this->total_tva;
1747 
1748  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
1749  $sql .= " total_ht = ".$this->total_ht;
1750  $sql .= " , total_ttc = ".$this->total_ttc;
1751  $sql .= " , total_tva = ".$this->total_tva;
1752  $sql .= " WHERE rowid = ".((int) $this->id);
1753 
1754  $result = $this->db->query($sql);
1755  if ($result) {
1756  return 1;
1757  } else {
1758  $this->error = $this->db->error();
1759  return -1;
1760  }
1761  }
1762 
1778  public function addline($qty = 0, $up = 0, $fk_c_type_fees = 0, $vatrate = 0, $date = '', $comments = '', $fk_project = 0, $fk_c_exp_tax_cat = 0, $type = 0, $fk_ecm_files = 0)
1779  {
1780  global $conf, $langs, $mysoc;
1781 
1782  dol_syslog(get_class($this)."::addline qty=$qty, up=$up, fk_c_type_fees=$fk_c_type_fees, vatrate=$vatrate, date=$date, fk_project=$fk_project, type=$type, comments=$comments", LOG_DEBUG);
1783 
1784  if ($this->status == self::STATUS_DRAFT) {
1785  if (empty($qty)) {
1786  $qty = 0;
1787  }
1788  if (empty($fk_c_type_fees) || $fk_c_type_fees < 0) {
1789  $fk_c_type_fees = 0;
1790  }
1791  if (empty($fk_c_exp_tax_cat) || $fk_c_exp_tax_cat < 0) {
1792  $fk_c_exp_tax_cat = 0;
1793  }
1794  if (empty($vatrate) || $vatrate < 0) {
1795  $vatrate = 0;
1796  }
1797  if (empty($date)) {
1798  $date = '';
1799  }
1800  if (empty($fk_project)) {
1801  $fk_project = 0;
1802  }
1803 
1804  $qty = price2num($qty);
1805  if (!preg_match('/\s*\((.*)\)/', $vatrate)) {
1806  $vatrate = price2num($vatrate); // $txtva can have format '5.0 (XXX)' or '5'
1807  }
1808  $up = price2num($up);
1809 
1810  $this->db->begin();
1811 
1812  $this->line = new ExpenseReportLine($this->db);
1813 
1814  // We don't know seller and buyer for expense reports
1815  $seller = $mysoc; // We use same than current company (expense report are often done in same country)
1816  $seller->tva_assuj = 1; // Most seller uses vat
1817  $buyer = new Societe($this->db);
1818 
1819  $localtaxes_type = getLocalTaxesFromRate($vatrate, 0, $buyer, $seller);
1820 
1821  $vat_src_code = '';
1822  $reg = array();
1823  if (preg_match('/\s*\((.*)\)/', $vatrate, $reg)) {
1824  $vat_src_code = $reg[1];
1825  $vatrate = preg_replace('/\s*\(.*\)/', '', $vatrate); // Remove code into vatrate.
1826  }
1827  $vatrate = preg_replace('/\*/', '', $vatrate);
1828 
1829  $tmp = calcul_price_total($qty, $up, 0, $vatrate, -1, -1, 0, 'TTC', 0, $type, $seller, $localtaxes_type);
1830 
1831  $this->line->value_unit = $up;
1832 
1833  $this->line->vat_src_code = $vat_src_code;
1834  $this->line->vatrate = price2num($vatrate);
1835  $this->line->localtax1_tx = $localtaxes_type[1];
1836  $this->line->localtax2_tx = $localtaxes_type[3];
1837  $this->line->localtax1_type = $localtaxes_type[0];
1838  $this->line->localtax2_type = $localtaxes_type[2];
1839 
1840  $this->line->total_ttc = $tmp[2];
1841  $this->line->total_ht = $tmp[0];
1842  $this->line->total_tva = $tmp[1];
1843  $this->line->total_localtax1 = $tmp[9];
1844  $this->line->total_localtax2 = $tmp[10];
1845 
1846  $this->line->fk_expensereport = $this->id;
1847  $this->line->qty = $qty;
1848  $this->line->date = $date;
1849  $this->line->fk_c_type_fees = $fk_c_type_fees;
1850  $this->line->fk_c_exp_tax_cat = $fk_c_exp_tax_cat;
1851  $this->line->comments = $comments;
1852  $this->line->fk_projet = $fk_project; // deprecated
1853  $this->line->fk_project = $fk_project;
1854 
1855  $this->line->fk_ecm_files = $fk_ecm_files;
1856 
1857  $this->applyOffset();
1858  $this->checkRules($type, $seller);
1859 
1860  $result = $this->line->insert(0, true);
1861  if ($result > 0) {
1862  $result = $this->update_price(); // This method is designed to add line from user input so total calculation must be done using 'auto' mode.
1863  if ($result > 0) {
1864  $this->db->commit();
1865  return $this->line->id;
1866  } else {
1867  $this->db->rollback();
1868  return -1;
1869  }
1870  } else {
1871  $this->error = $this->line->error;
1872  dol_syslog(get_class($this)."::addline error=".$this->error, LOG_ERR);
1873  $this->db->rollback();
1874  return -2;
1875  }
1876  } else {
1877  dol_syslog(get_class($this)."::addline status of expense report must be Draft to allow use of ->addline()", LOG_ERR);
1878  $this->error = 'ErrorExpenseNotDraft';
1879  return -3;
1880  }
1881  }
1882 
1890  public function checkRules($type = 0, $seller = '')
1891  {
1892  global $user, $conf, $db, $langs, $mysoc;
1893 
1894  $langs->load('trips');
1895 
1896  // We don't know seller and buyer for expense reports
1897  if (!is_object($seller)) {
1898  $seller = $mysoc; // We use same than current company (expense report are often done in same country)
1899  $seller->tva_assuj = 1; // Most seller uses vat
1900  }
1901 
1902  $expensereportrule = new ExpenseReportRule($db);
1903  $rulestocheck = $expensereportrule->getAllRule($this->line->fk_c_type_fees, $this->line->date, $this->fk_user_author);
1904 
1905  $violation = 0;
1906  $rule_warning_message_tab = array();
1907 
1908  $current_total_ttc = $this->line->total_ttc;
1909  $new_current_total_ttc = $this->line->total_ttc;
1910 
1911  // check if one is violated
1912  foreach ($rulestocheck as $rule) {
1913  if (in_array($rule->code_expense_rules_type, array('EX_DAY', 'EX_MON', 'EX_YEA'))) {
1914  $amount_to_test = $this->line->getExpAmount($rule, $this->fk_user_author, $rule->code_expense_rules_type);
1915  } else {
1916  $amount_to_test = $current_total_ttc; // EX_EXP
1917  }
1918 
1919  $amount_to_test = $amount_to_test - $current_total_ttc + $new_current_total_ttc; // if amount as been modified by a previous rule
1920 
1921  if ($amount_to_test > $rule->amount) {
1922  $violation++;
1923 
1924  if ($rule->restrictive) {
1925  $this->error = 'ExpenseReportConstraintViolationError';
1926  $this->errors[] = $this->error;
1927 
1928  $new_current_total_ttc -= $amount_to_test - $rule->amount; // ex, entered 16€, limit 12€, subtracts 4€;
1929  $rule_warning_message_tab[] = $langs->trans('ExpenseReportConstraintViolationError', $rule->id, price($amount_to_test, 0, $langs, 1, -1, -1, $conf->currency), price($rule->amount, 0, $langs, 1, -1, -1, $conf->currency));
1930  } else {
1931  $this->error = 'ExpenseReportConstraintViolationWarning';
1932  $this->errors[] = $this->error;
1933 
1934  $rule_warning_message_tab[] = $langs->trans('ExpenseReportConstraintViolationWarning', $rule->id, price($amount_to_test, 0, $langs, 1, -1, -1, $conf->currency), price($rule->amount, 0, $langs, 1, -1, -1, $conf->currency));
1935  }
1936 
1937  // No break, we sould test if another rule is violated
1938  }
1939  }
1940 
1941  $this->line->rule_warning_message = implode('\n', $rule_warning_message_tab);
1942 
1943  if ($violation > 0) {
1944  $tmp = calcul_price_total($this->line->qty, $new_current_total_ttc / $this->line->qty, 0, $this->line->vatrate, 0, 0, 0, 'TTC', 0, $type, $seller);
1945 
1946  $this->line->value_unit = $tmp[5];
1947  $this->line->total_ttc = $tmp[2];
1948  $this->line->total_ht = $tmp[0];
1949  $this->line->total_tva = $tmp[1];
1950  $this->line->total_localtax1 = $tmp[9];
1951  $this->line->total_localtax2 = $tmp[10];
1952 
1953  return false;
1954  } else {
1955  return true;
1956  }
1957  }
1958 
1966  public function applyOffset($type = 0, $seller = '')
1967  {
1968  global $conf, $mysoc;
1969 
1970  if (empty($conf->global->MAIN_USE_EXPENSE_IK)) {
1971  return false;
1972  }
1973 
1974  $userauthor = new User($this->db);
1975  if ($userauthor->fetch($this->fk_user_author) <= 0) {
1976  $this->error = 'ErrorCantFetchUser';
1977  $this->errors[] = 'ErrorCantFetchUser';
1978  return false;
1979  }
1980 
1981  // We don't know seller and buyer for expense reports
1982  if (!is_object($seller)) {
1983  $seller = $mysoc; // We use same than current company (expense report are often done in same country)
1984  $seller->tva_assuj = 1; // Most seller uses vat
1985  }
1986 
1987  $expenseik = new ExpenseReportIk($this->db);
1988  $range = $expenseik->getRangeByUser($userauthor, $this->line->fk_c_exp_tax_cat);
1989 
1990  if (empty($range)) {
1991  $this->error = 'ErrorNoRangeAvailable';
1992  $this->errors[] = 'ErrorNoRangeAvailable';
1993  return false;
1994  }
1995 
1996  if (!empty($conf->global->MAIN_EXPENSE_APPLY_ENTIRE_OFFSET)) {
1997  $ikoffset = $range->ikoffset;
1998  } else {
1999  $ikoffset = $range->ikoffset / 12; // The amount of offset is a global value for the year
2000  }
2001 
2002  // Test if ikoffset has been applied for the current month
2003  if (!$this->offsetAlreadyGiven()) {
2004  $new_up = $range->coef + ($ikoffset / $this->line->qty);
2005  $tmp = calcul_price_total($this->line->qty, $new_up, 0, $this->line->vatrate, 0, 0, 0, 'TTC', 0, $type, $seller);
2006 
2007  $this->line->value_unit = $tmp[5];
2008  $this->line->total_ttc = $tmp[2];
2009  $this->line->total_ht = $tmp[0];
2010  $this->line->total_tva = $tmp[1];
2011  $this->line->total_localtax1 = $tmp[9];
2012  $this->line->total_localtax2 = $tmp[10];
2013 
2014  return true;
2015  }
2016 
2017  return false;
2018  }
2019 
2025  public function offsetAlreadyGiven()
2026  {
2027  $sql = 'SELECT e.rowid FROM '.MAIN_DB_PREFIX.'expensereport e';
2028  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."expensereport_det d ON (e.rowid = d.fk_expensereport)";
2029  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."c_type_fees f ON (d.fk_c_type_fees = f.id AND f.code = 'EX_KME')";
2030  $sql .= " WHERE e.fk_user_author = ".(int) $this->fk_user_author;
2031  $sql .= " AND YEAR(d.date) = '".dol_print_date($this->line->date, '%Y')."' AND MONTH(d.date) = '".dol_print_date($this->line->date, '%m')."'";
2032  if (!empty($this->line->id)) {
2033  $sql .= ' AND d.rowid <> '.((int) $this->line->id);
2034  }
2035 
2036  dol_syslog(get_class($this)."::offsetAlreadyGiven");
2037  $resql = $this->db->query($sql);
2038  if ($resql) {
2039  $num = $this->db->num_rows($resql);
2040  if ($num > 0) {
2041  return true;
2042  }
2043  } else {
2044  dol_print_error($this->db);
2045  }
2046 
2047  return false;
2048  }
2049 
2067  public function updateline($rowid, $type_fees_id, $projet_id, $vatrate, $comments, $qty, $value_unit, $date, $expensereport_id, $fk_c_exp_tax_cat = 0, $fk_ecm_files = 0, $notrigger = 0)
2068  {
2069  global $user, $mysoc;
2070 
2071  if ($this->status == self::STATUS_DRAFT || $this->status == self::STATUS_REFUSED) {
2072  $this->db->begin();
2073 
2074  $error = 0;
2075  $type = 0; // TODO What if type is service ?
2076 
2077  // We don't know seller and buyer for expense reports
2078  $seller = $mysoc; // We use same than current company (expense report are often done in same country)
2079  $seller->tva_assuj = 1; // Most seller uses vat
2080  $seller->localtax1_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
2081  $seller->localtax2_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
2082  $buyer = new Societe($this->db);
2083 
2084  $localtaxes_type = getLocalTaxesFromRate($vatrate, 0, $buyer, $seller);
2085 
2086  // Clean vat code
2087  $reg = array();
2088  $vat_src_code = '';
2089  if (preg_match('/\((.*)\)/', $vatrate, $reg)) {
2090  $vat_src_code = $reg[1];
2091  $vatrate = preg_replace('/\s*\(.*\)/', '', $vatrate); // Remove code into vatrate.
2092  }
2093  $vatrate = preg_replace('/\*/', '', $vatrate);
2094 
2095  $tmp = calcul_price_total($qty, $value_unit, 0, $vatrate, -1, -1, 0, 'TTC', 0, $type, $seller, $localtaxes_type);
2096  //var_dump($vatrate);var_dump($localtaxes_type);var_dump($tmp);exit;
2097  // calcul total of line
2098  //$total_ttc = price2num($qty*$value_unit, 'MT');
2099 
2100  $tx_tva = $vatrate / 100;
2101  $tx_tva = $tx_tva + 1;
2102 
2103  $this->line = new ExpenseReportLine($this->db);
2104  $this->line->comments = $comments;
2105  $this->line->qty = $qty;
2106  $this->line->value_unit = $value_unit;
2107  $this->line->date = $date;
2108 
2109  $this->line->fk_expensereport = $expensereport_id;
2110  $this->line->fk_c_type_fees = $type_fees_id;
2111  $this->line->fk_c_exp_tax_cat = $fk_c_exp_tax_cat;
2112  $this->line->fk_projet = $projet_id; // deprecated
2113  $this->line->fk_project = $projet_id;
2114 
2115  $this->line->vat_src_code = $vat_src_code;
2116  $this->line->vatrate = price2num($vatrate);
2117  $this->line->localtax1_tx = $localtaxes_type[1];
2118  $this->line->localtax2_tx = $localtaxes_type[3];
2119  $this->line->localtax1_type = $localtaxes_type[0];
2120  $this->line->localtax2_type = $localtaxes_type[2];
2121 
2122  $this->line->total_ttc = $tmp[2];
2123  $this->line->total_ht = $tmp[0];
2124  $this->line->total_tva = $tmp[1];
2125  $this->line->total_localtax1 = $tmp[9];
2126  $this->line->total_localtax2 = $tmp[10];
2127 
2128  $this->line->fk_ecm_files = $fk_ecm_files;
2129 
2130  $this->line->id = ((int) $rowid);
2131 
2132  // Select des infos sur le type fees
2133  $sql = "SELECT c.code as code_type_fees, c.label as libelle_type_fees";
2134  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees as c";
2135  $sql .= " WHERE c.id = ".((int) $type_fees_id);
2136  $resql = $this->db->query($sql);
2137  if ($resql) {
2138  $objp_fees = $this->db->fetch_object($resql);
2139  $this->line->type_fees_code = $objp_fees->code_type_fees;
2140  $this->line->type_fees_libelle = $objp_fees->libelle_type_fees;
2141  $this->db->free($resql);
2142  }
2143 
2144  // Select des informations du projet
2145  $sql = "SELECT p.ref as ref_projet, p.title as title_projet";
2146  $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2147  $sql .= " WHERE p.rowid = ".((int) $projet_id);
2148  $resql = $this->db->query($sql);
2149  if ($resql) {
2150  $objp_projet = $this->db->fetch_object($resql);
2151  $this->line->projet_ref = $objp_projet->ref_projet;
2152  $this->line->projet_title = $objp_projet->title_projet;
2153  $this->db->free($resql);
2154  }
2155 
2156  $this->applyOffset();
2157  $this->checkRules();
2158 
2159  $result = $this->line->update($user);
2160  if ($result < 0) {
2161  $error++;
2162  }
2163 
2164  if (!$error && !$notrigger) {
2165  // Call triggers
2166  $result = $this->call_trigger('EXPENSE_REPORT_DET_MODIFY', $user);
2167  if ($result < 0) {
2168  $error++;
2169  }
2170  // End call triggers
2171  }
2172 
2173  if (!$error) {
2174  $this->db->commit();
2175  return 1;
2176  } else {
2177  $this->error = $this->line->error;
2178  $this->errors = $this->line->errors;
2179  $this->db->rollback();
2180  return -2;
2181  }
2182  }
2183  }
2184 
2193  public function deleteline($rowid, $fuser = '', $notrigger = 0)
2194  {
2195  $error=0;
2196 
2197  $this->db->begin();
2198 
2199  if (!$notrigger) {
2200  // Call triggers
2201  $result = $this->call_trigger('EXPENSE_REPORT_DET_DELETE', $fuser);
2202  if ($result < 0) {
2203  $error++;
2204  }
2205  // End call triggers
2206  }
2207 
2208  $sql = ' DELETE FROM '.MAIN_DB_PREFIX.$this->table_element_line;
2209  $sql .= ' WHERE rowid = '.((int) $rowid);
2210 
2211  dol_syslog(get_class($this)."::deleteline sql=".$sql);
2212  $result = $this->db->query($sql);
2213 
2214  if (!$result || $error > 0 ) {
2215  $this->error = $this->db->error();
2216  dol_syslog(get_class($this)."::deleteline Error ".$this->error, LOG_ERR);
2217  $this->db->rollback();
2218  return -1;
2219  }
2220 
2221  $this->update_price();
2222 
2223  $this->db->commit();
2224 
2225  return 1;
2226  }
2227 
2228  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2237  public function periode_existe($fuser, $date_debut, $date_fin)
2238  {
2239  // phpcs:enable
2240  $sql = "SELECT rowid, date_debut, date_fin";
2241  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
2242  $sql .= " WHERE fk_user_author = '{$fuser->id}'";
2243 
2244  dol_syslog(get_class($this)."::periode_existe sql=".$sql);
2245  $result = $this->db->query($sql);
2246  if ($result) {
2247  $num_rows = $this->db->num_rows($result); $i = 0;
2248 
2249  if ($num_rows > 0) {
2250  $date_d_form = $date_debut;
2251  $date_f_form = $date_fin;
2252 
2253  while ($i < $num_rows) {
2254  $objp = $this->db->fetch_object($result);
2255 
2256  $date_d_req = $this->db->jdate($objp->date_debut); // 3
2257  $date_f_req = $this->db->jdate($objp->date_fin); // 4
2258 
2259  if (!($date_f_form < $date_d_req || $date_d_form > $date_f_req)) {
2260  return $objp->rowid;
2261  }
2262 
2263  $i++;
2264  }
2265 
2266  return 0;
2267  } else {
2268  return 0;
2269  }
2270  } else {
2271  $this->error = $this->db->lasterror();
2272  dol_syslog(get_class($this)."::periode_existe Error ".$this->error, LOG_ERR);
2273  return -1;
2274  }
2275  }
2276 
2277 
2278  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2286  {
2287  // phpcs:enable
2288  $users_validator = array();
2289 
2290  $sql = "SELECT DISTINCT ur.fk_user";
2291  $sql .= " FROM ".MAIN_DB_PREFIX."user_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd";
2292  $sql .= " WHERE ur.fk_id = rd.id and rd.module = 'expensereport' AND rd.perms = 'approve'"; // Permission 'Approve';
2293  $sql .= " UNION";
2294  $sql .= " SELECT DISTINCT ugu.fk_user";
2295  $sql .= " FROM ".MAIN_DB_PREFIX."usergroup_user as ugu, ".MAIN_DB_PREFIX."usergroup_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd";
2296  $sql .= " WHERE ugu.fk_usergroup = ur.fk_usergroup AND ur.fk_id = rd.id and rd.module = 'expensereport' AND rd.perms = 'approve'"; // Permission 'Approve';
2297  //print $sql;
2298 
2299  dol_syslog(get_class($this)."::fetch_users_approver_expensereport sql=".$sql);
2300  $result = $this->db->query($sql);
2301  if ($result) {
2302  $num_rows = $this->db->num_rows($result); $i = 0;
2303  while ($i < $num_rows) {
2304  $objp = $this->db->fetch_object($result);
2305  array_push($users_validator, $objp->fk_user);
2306  $i++;
2307  }
2308  return $users_validator;
2309  } else {
2310  $this->error = $this->db->lasterror();
2311  dol_syslog(get_class($this)."::fetch_users_approver_expensereport Error ".$this->error, LOG_ERR);
2312  return -1;
2313  }
2314  }
2315 
2327  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
2328  {
2329  global $conf;
2330 
2331  $outputlangs->load("trips");
2332 
2333  if (!dol_strlen($modele)) {
2334  if (!empty($this->model_pdf)) {
2335  $modele = $this->model_pdf;
2336  } elseif (!empty($this->modelpdf)) { // deprecated
2337  $modele = $this->modelpdf;
2338  } elseif (!empty($conf->global->EXPENSEREPORT_ADDON_PDF)) {
2339  $modele = $conf->global->EXPENSEREPORT_ADDON_PDF;
2340  }
2341  }
2342 
2343  if (!empty($modele)) {
2344  $modelpath = "core/modules/expensereport/doc/";
2345 
2346  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2347  } else {
2348  return 0;
2349  }
2350  }
2351 
2358  public function listOfTypes($active = 1)
2359  {
2360  global $langs;
2361  $ret = array();
2362  $sql = "SELECT id, code, label";
2363  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees";
2364  $sql .= " WHERE active = ".((int) $active);
2365  dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
2366  $result = $this->db->query($sql);
2367  if ($result) {
2368  $num = $this->db->num_rows($result);
2369  $i = 0;
2370  while ($i < $num) {
2371  $obj = $this->db->fetch_object($result);
2372  $ret[$obj->code] = (($langs->transnoentitiesnoconv($obj->code) != $obj->code) ? $langs->transnoentitiesnoconv($obj->code) : $obj->label);
2373  $i++;
2374  }
2375  } else {
2376  dol_print_error($this->db);
2377  }
2378  return $ret;
2379  }
2380 
2381  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2387  public function load_state_board()
2388  {
2389  // phpcs:enable
2390  global $conf, $user;
2391 
2392  $this->nb = array();
2393 
2394  $sql = "SELECT count(ex.rowid) as nb";
2395  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as ex";
2396  $sql .= " WHERE ex.fk_statut > 0";
2397  $sql .= " AND ex.entity IN (".getEntity('expensereport').")";
2398  if (empty($user->rights->expensereport->readall)) {
2399  $userchildids = $user->getAllChildIds(1);
2400  $sql .= " AND (ex.fk_user_author IN (".$this->db->sanitize(join(',', $userchildids)).")";
2401  $sql .= " OR ex.fk_user_validator IN (".$this->db->sanitize(join(',', $userchildids))."))";
2402  }
2403 
2404  $resql = $this->db->query($sql);
2405  if ($resql) {
2406  while ($obj = $this->db->fetch_object($resql)) {
2407  $this->nb["expensereports"] = $obj->nb;
2408  }
2409  $this->db->free($resql);
2410  return 1;
2411  } else {
2412  dol_print_error($this->db);
2413  $this->error = $this->db->error();
2414  return -1;
2415  }
2416  }
2417 
2418  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2426  public function load_board($user, $option = 'topay')
2427  {
2428  // phpcs:enable
2429  global $conf, $langs;
2430 
2431  if ($user->socid) {
2432  return -1; // protection pour eviter appel par utilisateur externe
2433  }
2434 
2435  $now = dol_now();
2436 
2437  $sql = "SELECT ex.rowid, ex.date_valid";
2438  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as ex";
2439  if ($option == 'toapprove') {
2440  $sql .= " WHERE ex.fk_statut = ".self::STATUS_VALIDATED;
2441  } else {
2442  $sql .= " WHERE ex.fk_statut = ".self::STATUS_APPROVED;
2443  }
2444  $sql .= " AND ex.entity IN (".getEntity('expensereport').")";
2445  if (empty($user->rights->expensereport->readall)) {
2446  $userchildids = $user->getAllChildIds(1);
2447  $sql .= " AND (ex.fk_user_author IN (".$this->db->sanitize(join(',', $userchildids)).")";
2448  $sql .= " OR ex.fk_user_validator IN (".$this->db->sanitize(join(',', $userchildids))."))";
2449  }
2450 
2451  $resql = $this->db->query($sql);
2452  if ($resql) {
2453  $langs->load("trips");
2454 
2455  $response = new WorkboardResponse();
2456  if ($option == 'toapprove') {
2457  $response->warning_delay = $conf->expensereport->approve->warning_delay / 60 / 60 / 24;
2458  $response->label = $langs->trans("ExpenseReportsToApprove");
2459  $response->labelShort = $langs->trans("ToApprove");
2460  $response->url = DOL_URL_ROOT.'/expensereport/list.php?mainmenu=hrm&amp;statut='.self::STATUS_VALIDATED;
2461  } else {
2462  $response->warning_delay = $conf->expensereport->payment->warning_delay / 60 / 60 / 24;
2463  $response->label = $langs->trans("ExpenseReportsToPay");
2464  $response->labelShort = $langs->trans("StatusToPay");
2465  $response->url = DOL_URL_ROOT.'/expensereport/list.php?mainmenu=hrm&amp;statut='.self::STATUS_APPROVED;
2466  }
2467  $response->img = img_object('', "trip");
2468 
2469  while ($obj = $this->db->fetch_object($resql)) {
2470  $response->nbtodo++;
2471 
2472  if ($option == 'toapprove') {
2473  if ($this->db->jdate($obj->date_valid) < ($now - $conf->expensereport->approve->warning_delay)) {
2474  $response->nbtodolate++;
2475  }
2476  } else {
2477  if ($this->db->jdate($obj->date_valid) < ($now - $conf->expensereport->payment->warning_delay)) {
2478  $response->nbtodolate++;
2479  }
2480  }
2481  }
2482 
2483  return $response;
2484  } else {
2485  dol_print_error($this->db);
2486  $this->error = $this->db->error();
2487  return -1;
2488  }
2489  }
2490 
2497  public function hasDelay($option)
2498  {
2499  global $conf;
2500 
2501  // Only valid expenses reports
2502  if ($option == 'toapprove' && $this->status != 2) {
2503  return false;
2504  }
2505  if ($option == 'topay' && $this->status != 5) {
2506  return false;
2507  }
2508 
2509  $now = dol_now();
2510  if ($option == 'toapprove') {
2511  return (!empty($this->datevalid) ? $this->datevalid : $this->date_valid) < ($now - $conf->expensereport->approve->warning_delay);
2512  } else {
2513  return (!empty($this->datevalid) ? $this->datevalid : $this->date_valid) < ($now - $conf->expensereport->payment->warning_delay);
2514  }
2515  }
2516 
2522  public function getVentilExportCompta()
2523  {
2524  $alreadydispatched = 0;
2525 
2526  $type = 'expense_report';
2527 
2528  $sql = " SELECT COUNT(ab.rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type='".$this->db->escape($type)."' AND ab.fk_doc = ".((int) $this->id);
2529  $resql = $this->db->query($sql);
2530  if ($resql) {
2531  $obj = $this->db->fetch_object($resql);
2532  if ($obj) {
2533  $alreadydispatched = $obj->nb;
2534  }
2535  } else {
2536  $this->error = $this->db->lasterror();
2537  return -1;
2538  }
2539 
2540  if ($alreadydispatched) {
2541  return 1;
2542  }
2543  return 0;
2544  }
2545 
2551  public function getSumPayments()
2552  {
2553  $table = 'payment_expensereport';
2554  $field = 'fk_expensereport';
2555 
2556  $sql = 'SELECT sum(amount) as amount';
2557  $sql .= ' FROM '.MAIN_DB_PREFIX.$table;
2558  $sql .= " WHERE ".$field." = ".((int) $this->id);
2559 
2560  dol_syslog(get_class($this)."::getSumPayments", LOG_DEBUG);
2561  $resql = $this->db->query($sql);
2562  if ($resql) {
2563  $obj = $this->db->fetch_object($resql);
2564  $this->db->free($resql);
2565  return (empty($obj->amount) ? 0 : $obj->amount);
2566  } else {
2567  $this->error = $this->db->lasterror();
2568  return -1;
2569  }
2570  }
2571 
2580  public function computeTotalKm($fk_cat, $qty, $tva)
2581  {
2582  global $langs,$user,$db,$conf;
2583 
2584 
2585  $cumulYearQty = 0;
2586  $ranges = array();
2587  $coef = 0;
2588 
2589 
2590  if ($fk_cat < 0) {
2591  $this->error = $langs->trans('ErrorBadParameterCat');
2592  return -1;
2593  }
2594 
2595  if ($qty <= 0) {
2596  $this->error = $langs->trans('ErrorBadParameterQty');
2597  return -1;
2598  }
2599 
2600  $currentUser = new User($db);
2601  $currentUser->fetch($this->fk_user);
2602  $currentUser->getrights('expensereport');
2603  //Clean
2604  $qty = price2num($qty);
2605 
2606  $sql = " SELECT r.range_ik, t.ikoffset, t.coef";
2607  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_ik t";
2608  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_exp_tax_range r ON r.rowid = t.fk_range";
2609  $sql .= " WHERE t.fk_c_exp_tax_cat = ".(int) $fk_cat;
2610  $sql .= " ORDER BY r.range_ik ASC";
2611 
2612  dol_syslog("expenseReport::computeTotalkm sql=".$sql, LOG_DEBUG);
2613 
2614  $result = $this->db->query($sql);
2615 
2616  if ($result) {
2617  if ($conf->global->EXPENSEREPORT_CALCULATE_MILEAGE_EXPENSE_COEFFICIENT_ON_CURRENT_YEAR) {
2618  $arrayDate = dol_getdate(dol_now());
2619  $sql = " SELECT count(n.qty) as cumul FROM ".MAIN_DB_PREFIX."expensereport_det n";
2620  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport e ON e.rowid = n.fk_expensereport";
2621  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_fees tf ON tf.id = n.fk_c_type_fees";
2622  $sql.= " WHERE e.fk_user_author = ".(int) $this->fk_user_author;
2623  $sql.= " AND YEAR(n.date) = ".(int) $arrayDate['year'];
2624  $sql.= " AND tf.code = 'EX_KME' ";
2625  $sql.= " AND e.fk_statut = ".(int) ExpenseReport::STATUS_VALIDATED;
2626 
2627  $resql = $this->db->query($sql);
2628 
2629  if ($resql) {
2630  $obj = $this->db->fetch_object($resql);
2631  $cumulYearQty = $obj->cumul;
2632  }
2633 
2634  $qty = $cumulYearQty + $qty;
2635  }
2636 
2637  $num = $this->db->num_rows($result);
2638 
2639  if ($num) {
2640  for ($i = 0; $i < $num; $i++) {
2641  $obj = $this->db->fetch_object($result);
2642 
2643  $ranges[$i] = $obj;
2644  }
2645 
2646 
2647  for ($i = 0; $i < $num; $i++) {
2648  if ($i < ($num - 1)) {
2649  if ($qty > $ranges[$i]->range_ik && $qty < $ranges[$i+1]->range_ik) {
2650  $coef = $ranges[$i]->coef;
2651  $offset = $ranges[$i]->ikoffset;
2652  }
2653  } else {
2654  if ($qty > $ranges[$i]->range_ik) {
2655  $coef = $ranges[$i]->coef;
2656  $offset = $ranges[$i]->ikoffset;
2657  }
2658  }
2659  }
2660  $total_ht = $coef;
2661  return $total_ht;
2662  } else {
2663  $this->error = $langs->trans('TaxUndefinedForThisCategory');
2664  return 0;
2665  }
2666  } else {
2667  $this->error = $this->db->error()." sql=".$sql;
2668 
2669  return -1;
2670  }
2671  }
2672 }
2673 
2674 
2679 {
2683  public $db;
2684 
2688  public $table_element = 'expensereport_det';
2689 
2693  public $error = '';
2694 
2698  public $rowid;
2699 
2700  public $comments;
2701  public $qty;
2702  public $value_unit;
2703  public $date;
2704 
2708  public $fk_c_type_fees;
2709 
2713  public $fk_c_exp_tax_cat;
2714 
2718  public $fk_projet;
2719 
2723  public $fk_expensereport;
2724 
2725  public $type_fees_code;
2726  public $type_fees_libelle;
2727  public $type_fees_accountancy_code;
2728 
2729  public $projet_ref;
2730  public $projet_title;
2731  public $rang;
2732 
2733  public $vatrate;
2734  public $vat_src_code;
2735  public $tva_tx;
2736  public $localtax1_tx;
2737  public $localtax2_tx;
2738  public $localtax1_type;
2739  public $localtax2_type;
2740 
2741  public $total_ht;
2742  public $total_tva;
2743  public $total_ttc;
2744  public $total_localtax1;
2745  public $total_localtax2;
2746 
2750  public $fk_ecm_files;
2751 
2752  public $rule_warning_message;
2753 
2754 
2760  public function __construct($db)
2761  {
2762  $this->db = $db;
2763  }
2764 
2771  public function fetch($rowid)
2772  {
2773  $sql = 'SELECT fde.rowid, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_c_exp_tax_cat, fde.fk_projet as fk_project, fde.date,';
2774  $sql .= ' fde.tva_tx as vatrate, fde.vat_src_code, fde.comments, fde.qty, fde.value_unit, fde.total_ht, fde.total_tva, fde.total_ttc, fde.fk_ecm_files,';
2775  $sql .= ' fde.localtax1_tx, fde.localtax2_tx, fde.localtax1_type, fde.localtax2_type, fde.total_localtax1, fde.total_localtax2, fde.rule_warning_message,';
2776  $sql .= ' ctf.code as type_fees_code, ctf.label as type_fees_libelle,';
2777  $sql .= ' pjt.rowid as projet_id, pjt.title as projet_title, pjt.ref as projet_ref';
2778  $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_det as fde';
2779  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON fde.fk_c_type_fees=ctf.id'; // Sometimes type of expense report has been removed, so we use a left join here.
2780  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as pjt ON fde.fk_projet=pjt.rowid';
2781  $sql .= ' WHERE fde.rowid = '.((int) $rowid);
2782 
2783  $result = $this->db->query($sql);
2784 
2785  if ($result) {
2786  $objp = $this->db->fetch_object($result);
2787 
2788  $this->rowid = $objp->rowid;
2789  $this->id = $objp->rowid;
2790  $this->ref = $objp->ref;
2791  $this->fk_expensereport = $objp->fk_expensereport;
2792  $this->comments = $objp->comments;
2793  $this->qty = $objp->qty;
2794  $this->date = $objp->date;
2795  $this->dates = $this->db->jdate($objp->date);
2796  $this->value_unit = $objp->value_unit;
2797  $this->fk_c_type_fees = $objp->fk_c_type_fees;
2798  $this->fk_c_exp_tax_cat = $objp->fk_c_exp_tax_cat;
2799  $this->fk_projet = $objp->fk_project; // deprecated
2800  $this->fk_project = $objp->fk_project;
2801  $this->type_fees_code = $objp->type_fees_code;
2802  $this->type_fees_libelle = $objp->type_fees_libelle;
2803  $this->projet_ref = $objp->projet_ref;
2804  $this->projet_title = $objp->projet_title;
2805 
2806  $this->vatrate = $objp->vatrate;
2807  $this->vat_src_code = $objp->vat_src_code;
2808  $this->localtax1_tx = $objp->localtax1_tx;
2809  $this->localtax2_tx = $objp->localtax2_tx;
2810  $this->localtax1_type = $objp->localtax1_type;
2811  $this->localtax2_type = $objp->localtax2_type;
2812 
2813  $this->total_ht = $objp->total_ht;
2814  $this->total_tva = $objp->total_tva;
2815  $this->total_ttc = $objp->total_ttc;
2816  $this->total_localtax1 = $objp->total_localtax1;
2817  $this->total_localtax2 = $objp->total_localtax2;
2818 
2819  $this->fk_ecm_files = $objp->fk_ecm_files;
2820 
2821  $this->rule_warning_message = $objp->rule_warning_message;
2822 
2823  $this->db->free($result);
2824  } else {
2825  dol_print_error($this->db);
2826  }
2827  }
2828 
2836  public function insert($notrigger = 0, $fromaddline = false)
2837  {
2838  global $langs, $user, $conf;
2839 
2840  $error = 0;
2841 
2842  dol_syslog("ExpenseReportLine::Insert", LOG_DEBUG);
2843 
2844  // Clean parameters
2845  $this->comments = trim($this->comments);
2846  if (empty($this->value_unit)) {
2847  $this->value_unit = 0;
2848  }
2849  $this->qty = price2num($this->qty);
2850  $this->vatrate = price2num($this->vatrate);
2851  if (empty($this->fk_c_exp_tax_cat)) {
2852  $this->fk_c_exp_tax_cat = 0;
2853  }
2854 
2855  $this->db->begin();
2856 
2857  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'expensereport_det';
2858  $sql .= ' (fk_expensereport, fk_c_type_fees, fk_projet,';
2859  $sql .= ' tva_tx, vat_src_code,';
2860  $sql .= ' localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
2861  $sql .= ' comments, qty, value_unit,';
2862  $sql .= ' total_ht, total_tva, total_ttc,';
2863  $sql .= ' total_localtax1, total_localtax2,';
2864  $sql .= ' date, rule_warning_message, fk_c_exp_tax_cat, fk_ecm_files)';
2865  $sql .= " VALUES (".$this->db->escape($this->fk_expensereport).",";
2866  $sql .= " ".((int) $this->fk_c_type_fees).",";
2867  $sql .= " ".((int) (!empty($this->fk_project) && $this->fk_project > 0) ? $this->fk_project : ((!empty($this->fk_projet) && $this->fk_projet > 0) ? $this->fk_projet : 'null')).",";
2868  $sql .= " ".((float) $this->vatrate).",";
2869  $sql .= " '".$this->db->escape(empty($this->vat_src_code) ? '' : $this->vat_src_code)."',";
2870  $sql .= " ".((float) price2num($this->localtax1_tx)).",";
2871  $sql .= " ".((float) price2num($this->localtax2_tx)).",";
2872  $sql .= " '".$this->db->escape($this->localtax1_type)."',";
2873  $sql .= " '".$this->db->escape($this->localtax2_type)."',";
2874  $sql .= " '".$this->db->escape($this->comments)."',";
2875  $sql .= " ".((float) $this->qty).",";
2876  $sql .= " ".((float) $this->value_unit).",";
2877  $sql .= " ".((float) price2num($this->total_ht)).",";
2878  $sql .= " ".((float) price2num($this->total_tva)).",";
2879  $sql .= " ".((float) price2num($this->total_ttc)).",";
2880  $sql .= " ".((float) price2num($this->total_localtax1)).",";
2881  $sql .= " ".((float) price2num($this->total_localtax2)).",";
2882  $sql .= " '".$this->db->idate($this->date)."',";
2883  $sql .= " ".(empty($this->rule_warning_message) ? 'null' : "'".$this->db->escape($this->rule_warning_message)."'").",";
2884  $sql .= " ".((int) $this->fk_c_exp_tax_cat).",";
2885  $sql .= " ".($this->fk_ecm_files > 0 ? ((int) $this->fk_ecm_files) : 'null');
2886  $sql .= ")";
2887 
2888  $resql = $this->db->query($sql);
2889  if ($resql) {
2890  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'expensereport_det');
2891 
2892 
2893  if (!$error && !$notrigger) {
2894  // Call triggers
2895  $result = $this->call_trigger('EXPENSE_REPORT_DET_CREATE', $user);
2896  if ($result < 0) {
2897  $error++;
2898  }
2899  // End call triggers
2900  }
2901 
2902 
2903  if (!$fromaddline) {
2904  $tmpparent = new ExpenseReport($this->db);
2905  $tmpparent->fetch($this->fk_expensereport);
2906  $result = $tmpparent->update_price();
2907  if ($result < 0) {
2908  $error++;
2909  $this->error = $tmpparent->error;
2910  $this->errors = $tmpparent->errors;
2911  }
2912  }
2913  } else {
2914  $error++;
2915  }
2916 
2917  if (!$error) {
2918  $this->db->commit();
2919  return $this->id;
2920  } else {
2921  $this->error = $this->db->lasterror();
2922  dol_syslog("ExpenseReportLine::insert Error ".$this->error, LOG_ERR);
2923  $this->db->rollback();
2924  return -2;
2925  }
2926  }
2927 
2936  public function getExpAmount(ExpenseReportRule $rule, $fk_user, $mode = 'day')
2937  {
2938  $amount = 0;
2939 
2940  $sql = 'SELECT SUM(d.total_ttc) as total_amount';
2941  $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport_det d';
2942  $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'expensereport e ON (d.fk_expensereport = e.rowid)';
2943  $sql .= ' WHERE e.fk_user_author = '.((int) $fk_user);
2944  if (!empty($this->id)) {
2945  $sql .= ' AND d.rowid <> '.((int) $this->id);
2946  }
2947  $sql .= ' AND d.fk_c_type_fees = '.((int) $rule->fk_c_type_fees);
2948  if ($mode == 'day' || $mode == 'EX_DAY') {
2949  $sql .= " AND d.date = '".dol_print_date($this->date, '%Y-%m-%d')."'";
2950  } elseif ($mode == 'mon' || $mode == 'EX_MON') {
2951  $sql .= " AND DATE_FORMAT(d.date, '%Y-%m') = '".dol_print_date($this->date, '%Y-%m')."'"; // @todo DATE_FORMAT is forbidden
2952  } elseif ($mode == 'year' || $mode == 'EX_YEA') {
2953  $sql .= " AND DATE_FORMAT(d.date, '%Y') = '".dol_print_date($this->date, '%Y')."'"; // @todo DATE_FORMAT is forbidden
2954  }
2955 
2956  dol_syslog('ExpenseReportLine::getExpAmount');
2957 
2958  $resql = $this->db->query($sql);
2959  if ($resql) {
2960  $num = $this->db->num_rows($resql);
2961  if ($num > 0) {
2962  $obj = $this->db->fetch_object($resql);
2963  $amount = (double) $obj->total_amount;
2964  }
2965  } else {
2966  dol_print_error($this->db);
2967  }
2968 
2969  return $amount + $this->total_ttc;
2970  }
2971 
2978  public function update(User $user)
2979  {
2980  global $langs, $conf;
2981 
2982  $error = 0;
2983 
2984  // Clean parameters
2985  $this->comments = trim($this->comments);
2986  $this->vatrate = price2num($this->vatrate);
2987  $this->value_unit = price2num($this->value_unit);
2988  if (empty($this->fk_c_exp_tax_cat)) {
2989  $this->fk_c_exp_tax_cat = 0;
2990  }
2991 
2992  $this->db->begin();
2993 
2994  // Update line in database
2995  $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport_det SET";
2996  $sql .= " comments='".$this->db->escape($this->comments)."'";
2997  $sql .= ", value_unit = ".((float) $this->value_unit);
2998  $sql .= ", qty=".((float) $this->qty);
2999  $sql .= ", date='".$this->db->idate($this->date)."'";
3000  $sql .= ", total_ht=".((float) price2num($this->total_ht, 'MT'));
3001  $sql .= ", total_tva=".((float) price2num($this->total_tva, 'MT'));
3002  $sql .= ", total_ttc=".((float) price2num($this->total_ttc, 'MT'));
3003  $sql .= ", total_localtax1=".((float) price2num($this->total_localtax1, 'MT'));
3004  $sql .= ", total_localtax2=".((float) price2num($this->total_localtax2, 'MT'));
3005  $sql .= ", tva_tx=".((float) $this->vatrate);
3006  $sql .= ", vat_src_code='".$this->db->escape($this->vat_src_code)."'";
3007  $sql .= ", localtax1_tx=".((float) $this->localtax1_tx);
3008  $sql .= ", localtax2_tx=".((float) $this->localtax2_tx);
3009  $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
3010  $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
3011  $sql .= ", rule_warning_message='".$this->db->escape($this->rule_warning_message)."'";
3012  $sql .= ", fk_c_exp_tax_cat=".$this->db->escape($this->fk_c_exp_tax_cat);
3013  $sql .= ", fk_ecm_files=".($this->fk_ecm_files > 0 ? ((int) $this->fk_ecm_files) : 'null');
3014  if ($this->fk_c_type_fees) {
3015  $sql .= ", fk_c_type_fees = ".((int) $this->fk_c_type_fees);
3016  } else {
3017  $sql .= ", fk_c_type_fees=null";
3018  }
3019  if ($this->fk_project > 0) {
3020  $sql .= ", fk_projet=".((int) $this->fk_project);
3021  } else {
3022  $sql .= ", fk_projet=null";
3023  }
3024  $sql .= " WHERE rowid = ".((int) ($this->rowid ? $this->rowid : $this->id));
3025 
3026  dol_syslog("ExpenseReportLine::update");
3027 
3028  $resql = $this->db->query($sql);
3029  if ($resql) {
3030  $tmpparent = new ExpenseReport($this->db);
3031  $result = $tmpparent->fetch($this->fk_expensereport);
3032  if ($result > 0) {
3033  $result = $tmpparent->update_price();
3034  if ($result < 0) {
3035  $error++;
3036  $this->error = $tmpparent->error;
3037  $this->errors = $tmpparent->errors;
3038  }
3039  } else {
3040  $error++;
3041  $this->error = $tmpparent->error;
3042  $this->errors = $tmpparent->errors;
3043  }
3044  } else {
3045  $error++;
3046  dol_print_error($this->db);
3047  }
3048 
3049  if (!$error) {
3050  $this->db->commit();
3051  return 1;
3052  } else {
3053  $this->error = $this->db->lasterror();
3054  dol_syslog("ExpenseReportLine::update Error ".$this->error, LOG_ERR);
3055  $this->db->rollback();
3056  return -2;
3057  }
3058  }
3059 
3060  // ajouter ici comput_ ...
3061 }
dol_getdate
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
Definition: functions.lib.php:2713
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
getLocalTaxesFromRate
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
Definition: functions.lib.php:6097
ExpenseReport\createFromClone
createFromClone(User $user, $fk_user_author)
Load an object from its id and create a new one in database.
Definition: expensereport.class.php:412
ExpenseReport\load_state_board
load_state_board()
Charge indicateurs this->nb pour le tableau de bord.
Definition: expensereport.class.php:2387
db
$conf db
API class for accounts.
Definition: inc.php:41
ExpenseReportLine\fetch
fetch($rowid)
Fetch record for expense report detailed line.
Definition: expensereport.class.php:2771
ExpenseReport\fetch_users_approver_expensereport
fetch_users_approver_expensereport()
Return list of people with permission to validate expense reports.
Definition: expensereport.class.php:2285
dol_trunc
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.
Definition: functions.lib.php:3805
ExpenseReport\setApproved
setApproved($fuser, $notrigger=0)
Set status to approved.
Definition: expensereport.class.php:1370
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
ExpenseReport\STATUS_VALIDATED
const STATUS_VALIDATED
Validated (need to be paid)
Definition: expensereport.class.php:145
dol_delete_dir_recursive
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
Definition: files.lib.php:1383
ExpenseReport\STATUS_APPROVED
const STATUS_APPROVED
Classified approved.
Definition: expensereport.class.php:155
ExpenseReport\addline
addline($qty=0, $up=0, $fk_c_type_fees=0, $vatrate=0, $date='', $comments='', $fk_project=0, $fk_c_exp_tax_cat=0, $type=0, $fk_ecm_files=0)
Add expense report line.
Definition: expensereport.class.php:1778
ExpenseReport\__construct
__construct($db)
Constructor.
Definition: expensereport.class.php:219
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
ExpenseReport\updateline
updateline($rowid, $type_fees_id, $projet_id, $vatrate, $comments, $qty, $value_unit, $date, $expensereport_id, $fk_c_exp_tax_cat=0, $fk_ecm_files=0, $notrigger=0)
Update an expense report line.
Definition: expensereport.class.php:2067
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
ExpenseReportLine\__construct
__construct($db)
Constructor.
Definition: expensereport.class.php:2760
ExpenseReportIk
Class to manage inventories.
Definition: expensereport_ik.class.php:30
ExpenseReportLine\getExpAmount
getExpAmount(ExpenseReportRule $rule, $fk_user, $mode='day')
Function to get total amount in expense reports for a same rule.
Definition: expensereport.class.php:2936
CommonObject\deleteObjectLinked
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='', $f_user=null, $notrigger=0)
Delete all links between an object $this.
Definition: commonobject.class.php:4228
ref
$object ref
Definition: info.php:77
ExpenseReport\setDeny
setDeny($fuser, $details, $notrigger=0)
setDeny
Definition: expensereport.class.php:1423
ExpenseReport\fetch_lines
fetch_lines()
fetch_lines
Definition: expensereport.class.php:997
rowid
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
Definition: email_expire_services_to_representatives.php:79
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
ExpenseReport\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: expensereport.class.php:835
ExpenseReport\set_save_from_refuse
set_save_from_refuse($fuser)
set_save_from_refuse
Definition: expensereport.class.php:1329
CommonObjectLine
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Definition: commonobjectline.class.php:32
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
ExpenseReport\checkRules
checkRules($type=0, $seller='')
Check constraint of rules and update price if needed.
Definition: expensereport.class.php:1890
ExpenseReportLine\update
update(User $user)
Update line.
Definition: expensereport.class.php:2978
ExpenseReport\listOfTypes
listOfTypes($active=1)
List of types.
Definition: expensereport.class.php:2358
ExpenseReport\set_unpaid
set_unpaid($fuser, $notrigger=0)
set_unpaid
Definition: expensereport.class.php:1481
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
WorkboardResponse
Definition: workboardresponse.class.php:25
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
ExpenseReport\getNomUrl
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $moretitle='', $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Definition: expensereport.class.php:1650
CommonObject\commonGenerateDocument
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
Definition: commonobject.class.php:5406
ExpenseReport\set_paid
set_paid($id, $fuser, $notrigger=0)
Classify the expense report as paid.
Definition: expensereport.class.php:670
ExpenseReport\getNextNumRef
getNextNumRef()
Return next reference of expense report not already used.
Definition: expensereport.class.php:1596
calcul_price_total
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array='', $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:86
dol_delete_file
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
Definition: files.lib.php:1231
ExpenseReport\deleteline
deleteline($rowid, $fuser='', $notrigger=0)
deleteline
Definition: expensereport.class.php:2193
ExpenseReport\setUnpaid
setUnpaid($fuser, $notrigger=0)
set_unpaid
Definition: expensereport.class.php:1495
CommonObject\update_price
update_price($exclspec=0, $roundingadjust='none', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
Definition: commonobject.class.php:3525
ExpenseReport\periode_existe
periode_existe($fuser, $date_debut, $date_fin)
periode_existe
Definition: expensereport.class.php:2237
ExpenseReport\fetch_line_by_project
fetch_line_by_project($projectid, $user='')
fetch_line_by_project
Definition: expensereport.class.php:899
ExpenseReport\STATUS_CLOSED
const STATUS_CLOSED
Classified paid.
Definition: expensereport.class.php:165
ExpenseReport\setValidate
setValidate($fuser, $notrigger=0)
Set to status validate.
Definition: expensereport.class.php:1214
ExpenseReport\STATUS_DRAFT
const STATUS_DRAFT
Draft status.
Definition: expensereport.class.php:140
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6156
ExpenseReport\update
update($user, $notrigger=0, $userofexpensereport=null)
update
Definition: expensereport.class.php:493
ExpenseReport\STATUS_REFUSED
const STATUS_REFUSED
Classified refused.
Definition: expensereport.class.php:160
CommonObject\delete_linked_contact
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
Definition: commonobject.class.php:1319
ExpenseReportLine\insert
insert($notrigger=0, $fromaddline=false)
Insert a line of expense report.
Definition: expensereport.class.php:2836
ExpenseReport\info
info($id)
Load information on object.
Definition: expensereport.class.php:766
ExpenseReport\hasDelay
hasDelay($option)
Return if an expense report is late or not.
Definition: expensereport.class.php:2497
ExpenseReport\getLibStatut
getLibStatut($mode=0)
Returns the label status.
Definition: expensereport.class.php:733
CommonObject\deleteEcmFiles
deleteEcmFiles($mode=0)
Delete related files of object in database.
Definition: commonobject.class.php:9966
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
ExpenseReport\update_totaux_add
update_totaux_add($ligne_total_ht, $ligne_total_tva)
Update total of an expense report when you add a line.
Definition: expensereport.class.php:1741
CommonObject\fetch_optionals
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
Definition: commonobject.class.php:6007
ExpenseReport\computeTotalKm
computeTotalKm($fk_cat, $qty, $tva)
Compute the cost of the kilometers expense based on the number of kilometers and the vehicule categor...
Definition: expensereport.class.php:2580
ExpenseReportLine
Class of expense report details lines.
Definition: expensereport.class.php:2678
ExpenseReport\setPaid
setPaid($id, $fuser, $notrigger=0)
Classify the expense report as paid.
Definition: expensereport.class.php:685
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
ExpenseReport\getSumPayments
getSumPayments()
Return amount of payments already done.
Definition: expensereport.class.php:2551
dolGetFirstLastname
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
Definition: functions.lib.php:8062
User
Class to manage Dolibarr users.
Definition: user.class.php:44
ExpenseReport\getVentilExportCompta
getVentilExportCompta()
Return if object was dispatched into bookkeeping.
Definition: expensereport.class.php:2522
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6116
ExpenseReport\applyOffset
applyOffset($type=0, $seller='')
Method to apply the offset if needed.
Definition: expensereport.class.php:1966
dol_delete_preview
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1435
ExpenseReportRule
Class to manage inventories.
Definition: expensereport_rule.class.php:30
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10338
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
ExpenseReport\LibStatut
LibStatut($status, $mode=0)
Returns the label of a status.
Definition: expensereport.class.php:746
ExpenseReport
Class to manage Trips and Expenses.
Definition: expensereport.class.php:36
ExpenseReport\offsetAlreadyGiven
offsetAlreadyGiven()
If the sql find any rows then the ikoffset is already given (ikoffset is applied at the first expense...
Definition: expensereport.class.php:2025
ExpenseReport\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk accordign to template module.
Definition: expensereport.class.php:2327
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
ExpenseReport\load_board
load_board($user, $option='topay')
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
Definition: expensereport.class.php:2426
price
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.
Definition: functions.lib.php:5541
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
ExpenseReport\create
create($user, $notrigger=0)
Create object in database.
Definition: expensereport.class.php:244
ExpenseReport\STATUS_CANCELED
const STATUS_CANCELED
Classified canceled.
Definition: expensereport.class.php:150
vatrate
vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0, $html=0)
Return a string with VAT rate label formated for view output Used into pdf and HTML pages.
Definition: functions.lib.php:5492
ExpenseReport\set_cancel
set_cancel($fuser, $detail, $notrigger=0)
set_cancel
Definition: expensereport.class.php:1546