dolibarr 21.0.0-beta
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-2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2016-2020 Ferran Marcet <fmarcet@2byte.es>
8 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
30require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
31require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereportline.class.php';
32require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_ik.class.php';
33require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_rule.class.php';
34
35
40{
44 public $element = 'expensereport';
45
49 public $table_element = 'expensereport';
50
54 public $table_element_line = 'expensereport_det';
55
59 public $fk_element = 'fk_expensereport';
60
64 public $picto = 'trip';
65
69 public $lines = array();
70
74 public $line;
75
79 public $date_debut;
80
84 public $date_fin;
85
89 public $date_approbation;
90
94 public $fk_user;
95
99 public $user_approve_id;
100
106 public $status;
107
114 public $fk_statut;
115
119 public $fk_c_paiement;
120
124 public $modepaymentid;
125
129 public $paid;
130
131 // Paiement
135 public $user_paid_infos;
136
140 public $user_author_infos;
141
145 public $user_validator_infos;
146
150 public $rule_warning_message;
151
152 // ACTIONS
153
154 // Create
158 public $date_create;
159
163 public $fk_user_creat;
164
168 public $fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
169
170 // Update
174 public $date_modif;
175
179 public $fk_user_modif;
180
181 // Refus
185 public $date_refuse;
186
190 public $detail_refuse;
191
195 public $fk_user_refuse;
196
197 // Annulation
201 public $date_cancel;
202
206 public $detail_cancel;
207
211 public $fk_user_cancel;
212
216 public $fk_user_validator;
217
224 public $datevalid;
225
230 public $date_valid;
231
235 public $fk_user_valid;
236
240 public $user_valid_infos;
241
242 // Approve
246 public $date_approve;
247
251 public $fk_user_approve;
252
257 public $localtax1; // for backward compatibility (real field should be total_localtax1 defined into CommonObject)
262 public $localtax2; // for backward compatibility (real field should be total_localtax2 defined into CommonObject)
263
267 const STATUS_DRAFT = 0;
268
273
278
283
287 const STATUS_CLOSED = 6;
288
292 const STATUS_REFUSED = 99;
293
294 public $fields = array(
295 'rowid' => array('type' => 'integer', 'label' => 'ID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
296 'ref' => array('type' => 'varchar(50)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'showoncombobox' => 1, 'position' => 15),
297 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 20),
298 'ref_number_int' => array('type' => 'integer', 'label' => 'Ref number int', 'enabled' => 1, 'visible' => -1, 'position' => 25),
299 'ref_ext' => array('type' => 'integer', 'label' => 'Ref ext', 'enabled' => 1, 'visible' => -1, 'position' => 30),
300 'total_ht' => array('type' => 'double(24,8)', 'label' => 'Total ht', 'enabled' => 1, 'visible' => -1, 'position' => 35),
301 'total_tva' => array('type' => 'double(24,8)', 'label' => 'Total tva', 'enabled' => 1, 'visible' => -1, 'position' => 40),
302 'localtax1' => array('type' => 'double(24,8)', 'label' => 'Localtax1', 'enabled' => 1, 'visible' => -1, 'position' => 45),
303 'localtax2' => array('type' => 'double(24,8)', 'label' => 'Localtax2', 'enabled' => 1, 'visible' => -1, 'position' => 50),
304 'total_ttc' => array('type' => 'double(24,8)', 'label' => 'Total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 55),
305 'date_debut' => array('type' => 'date', 'label' => 'Date debut', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 60),
306 'date_fin' => array('type' => 'date', 'label' => 'Date fin', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 65),
307 'date_valid' => array('type' => 'datetime', 'label' => 'Date valid', 'enabled' => 1, 'visible' => -1, 'position' => 75),
308 'date_approve' => array('type' => 'datetime', 'label' => 'Date approve', 'enabled' => 1, 'visible' => -1, 'position' => 80),
309 'date_refuse' => array('type' => 'datetime', 'label' => 'Date refuse', 'enabled' => 1, 'visible' => -1, 'position' => 85),
310 'date_cancel' => array('type' => 'datetime', 'label' => 'Date cancel', 'enabled' => 1, 'visible' => -1, 'position' => 90),
311 'fk_user_author' => array('type' => 'integer', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 100),
312 'fk_user_modif' => array('type' => 'integer', 'label' => 'Fk user modif', 'enabled' => 1, 'visible' => -1, 'position' => 105),
313 'fk_user_valid' => array('type' => 'integer', 'label' => 'Fk user valid', 'enabled' => 1, 'visible' => -1, 'position' => 110),
314 'fk_user_validator' => array('type' => 'integer', 'label' => 'Fk user validator', 'enabled' => 1, 'visible' => -1, 'position' => 115),
315 'fk_user_approve' => array('type' => 'integer', 'label' => 'Fk user approve', 'enabled' => 1, 'visible' => -1, 'position' => 120),
316 'fk_user_refuse' => array('type' => 'integer', 'label' => 'Fk user refuse', 'enabled' => 1, 'visible' => -1, 'position' => 125),
317 'fk_user_cancel' => array('type' => 'integer', 'label' => 'Fk user cancel', 'enabled' => 1, 'visible' => -1, 'position' => 130),
318 'fk_c_paiement' => array('type' => 'integer', 'label' => 'Fk c paiement', 'enabled' => 1, 'visible' => -1, 'position' => 140),
319 'paid' => array('type' => 'integer', 'label' => 'Paid', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 145),
320 'note_public' => array('type' => 'html', 'label' => 'Note public', 'enabled' => 1, 'visible' => 0, 'position' => 150),
321 'note_private' => array('type' => 'html', 'label' => 'Note private', 'enabled' => 1, 'visible' => 0, 'position' => 155),
322 'detail_refuse' => array('type' => 'varchar(255)', 'label' => 'Detail refuse', 'enabled' => 1, 'visible' => -1, 'position' => 160),
323 'detail_cancel' => array('type' => 'varchar(255)', 'label' => 'Detail cancel', 'enabled' => 1, 'visible' => -1, 'position' => 165),
324 'integration_compta' => array('type' => 'integer', 'label' => 'Integration compta', 'enabled' => 1, 'visible' => -1, 'position' => 170),
325 'fk_bank_account' => array('type' => 'integer', 'label' => 'Fk bank account', 'enabled' => 1, 'visible' => -1, 'position' => 175),
326 'fk_multicurrency' => array('type' => 'integer', 'label' => 'Fk multicurrency', 'enabled' => 1, 'visible' => -1, 'position' => 185),
327 'multicurrency_code' => array('type' => 'varchar(255)', 'label' => 'Multicurrency code', 'enabled' => 1, 'visible' => -1, 'position' => 190),
328 'multicurrency_tx' => array('type' => 'double(24,8)', 'label' => 'Multicurrency tx', 'enabled' => 1, 'visible' => -1, 'position' => 195),
329 'multicurrency_total_ht' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ht', 'enabled' => 1, 'visible' => -1, 'position' => 200),
330 'multicurrency_total_tva' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total tva', 'enabled' => 1, 'visible' => -1, 'position' => 205),
331 'multicurrency_total_ttc' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 210),
332 'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 220),
333 'date_create' => array('type' => 'datetime', 'label' => 'Date create', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 300),
334 'tms' => array('type' => 'timestamp', 'label' => 'Tms', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 305),
335 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 1000),
336 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 1010),
337 'fk_statut' => array('type' => 'integer', 'label' => 'Fk statut', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 500),
338 );
339
345 public function __construct($db)
346 {
347 $this->db = $db;
348 $this->total_ht = 0;
349 $this->total_ttc = 0;
350 $this->total_tva = 0;
351 $this->total_localtax1 = 0;
352 $this->total_localtax2 = 0;
353 $this->localtax1 = 0; // For backward compatibility
354 $this->localtax2 = 0; // For backward compatibility
355 $this->modepaymentid = 0;
356
357 // List of language codes for status
358 $this->labelStatusShort = array(0 => 'Draft', 2 => 'Validated', 4 => 'Canceled', 5 => 'Approved', 6 => 'Paid', 99 => 'Refused');
359 $this->labelStatus = array(0 => 'Draft', 2 => 'ValidatedWaitingApproval', 4 => 'Canceled', 5 => 'Approved', 6 => 'Paid', 99 => 'Refused');
360 }
361
369 public function create($user, $notrigger = 0)
370 {
371 global $conf, $langs;
372
373 $now = dol_now();
374
375 $error = 0;
376
377 // Check parameters
378 if (empty($this->date_debut) || empty($this->date_fin)) {
379 $this->error = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Date'));
380 return -1;
381 }
382
383 $fuserid = $this->fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
384 if (empty($fuserid)) {
385 $fuserid = $user->id;
386 }
387
388 $this->db->begin();
389
390 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
391 $sql .= "ref";
392 $sql .= ",total_ht";
393 $sql .= ",total_ttc";
394 $sql .= ",total_tva";
395 $sql .= ",date_debut";
396 $sql .= ",date_fin";
397 $sql .= ",date_create";
398 $sql .= ",fk_user_creat";
399 $sql .= ",fk_user_author";
400 $sql .= ",fk_user_validator";
401 $sql .= ",fk_user_approve";
402 $sql .= ",fk_user_modif";
403 $sql .= ",fk_statut";
404 $sql .= ",fk_c_paiement";
405 $sql .= ",paid";
406 $sql .= ",note_public";
407 $sql .= ",note_private";
408 $sql .= ",entity";
409 $sql .= ") VALUES(";
410 $sql .= "'(PROV)'";
411 $sql .= ", ".price2num($this->total_ht, 'MT');
412 $sql .= ", ".price2num($this->total_ttc, 'MT');
413 $sql .= ", ".price2num($this->total_tva, 'MT');
414 $sql .= ", '".$this->db->idate($this->date_debut)."'";
415 $sql .= ", '".$this->db->idate($this->date_fin)."'";
416 $sql .= ", '".$this->db->idate($now)."'";
417 $sql .= ", ".((int) $user->id);
418 $sql .= ", ".((int) $fuserid);
419 $sql .= ", ".($this->fk_user_validator > 0 ? ((int) $this->fk_user_validator) : "null");
420 $sql .= ", ".($this->fk_user_approve > 0 ? ((int) $this->fk_user_approve) : "null");
421 $sql .= ", ".($this->fk_user_modif > 0 ? ((int) $this->fk_user_modif) : "null");
422 $sql .= ", ".($this->fk_statut > 1 ? ((int) $this->fk_statut) : 0);
423 $sql .= ", ".($this->modepaymentid ? ((int) $this->modepaymentid) : "null");
424 $sql .= ", 0";
425 $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
426 $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
427 $sql .= ", ".((int) $conf->entity);
428 $sql .= ")";
429
430 $result = $this->db->query($sql);
431 if ($result) {
432 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
433 $this->ref = '(PROV'.$this->id.')';
434
435 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element." SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".((int) $this->id);
436 $resql = $this->db->query($sql);
437 if (!$resql) {
438 $this->error = $this->db->lasterror();
439 $error++;
440 }
441
442 if (!$error) {
443 if (is_array($this->lines) && count($this->lines) > 0) {
444 foreach ($this->lines as $line) {
445 // Test and convert into object this->lines[$i]. When coming from REST API, we may still have an array
446 //if (! is_object($line)) $line=json_decode(json_encode($line), false); // convert recursively array into object.
447 if (!is_object($line)) {
448 $line = (object) $line;
449 $newndfline = new ExpenseReportLine($this->db);
450 $newndfline->fk_expensereport = $line->fk_expensereport;
451 $newndfline->fk_c_type_fees = $line->fk_c_type_fees;
452 $newndfline->fk_project = $line->fk_project;
453 $newndfline->vatrate = $line->vatrate;
454 $newndfline->vat_src_code = $line->vat_src_code;
455 $newndfline->localtax1_tx = $line->localtax1_tx;
456 $newndfline->localtax2_tx = $line->localtax2_tx;
457 $newndfline->localtax1_type = $line->localtax1_type;
458 $newndfline->localtax2_type = $line->localtax2_type;
459 $newndfline->comments = $line->comments;
460 $newndfline->qty = $line->qty;
461 $newndfline->value_unit = $line->value_unit;
462 $newndfline->total_ht = $line->total_ht;
463 $newndfline->total_ttc = $line->total_ttc;
464 $newndfline->total_tva = $line->total_tva;
465 $newndfline->total_localtax1 = $line->total_localtax1;
466 $newndfline->total_localtax2 = $line->total_localtax2;
467 $newndfline->date = $line->date;
468 $newndfline->rule_warning_message = $line->rule_warning_message;
469 $newndfline->fk_c_exp_tax_cat = $line->fk_c_exp_tax_cat;
470 $newndfline->fk_ecm_files = $line->fk_ecm_files;
471 } else {
472 $newndfline = $line;
473 }
474 //$newndfline=new ExpenseReportLine($this->db);
475 $newndfline->fk_expensereport = $this->id;
476 $result = $newndfline->insert();
477 if ($result < 0) {
478 $this->error = $newndfline->error;
479 $this->errors = $newndfline->errors;
480 $error++;
481 break;
482 }
483 }
484 }
485 }
486
487 if (!$error) {
488 $result = $this->insertExtraFields();
489 if ($result < 0) {
490 $error++;
491 }
492 }
493
494 if (!$error) {
495 $result = $this->update_price(1);
496 if ($result > 0) {
497 if (!$notrigger) {
498 // Call trigger
499 $result = $this->call_trigger('EXPENSE_REPORT_CREATE', $user);
500
501 if ($result < 0) {
502 $error++;
503 }
504 // End call triggers
505 }
506
507 if (empty($error)) {
508 $this->db->commit();
509 return $this->id;
510 } else {
511 $this->db->rollback();
512 return -4;
513 }
514 } else {
515 $this->db->rollback();
516 return -3;
517 }
518 } else {
519 dol_syslog(get_class($this)."::create error ".$this->error, LOG_ERR);
520 $this->db->rollback();
521 return -2;
522 }
523 } else {
524 $this->error = $this->db->lasterror()." sql=".$sql;
525 $this->db->rollback();
526 return -1;
527 }
528 }
529
537 public function createFromClone(User $user, $fk_user_author)
538 {
539 global $hookmanager;
540
541 $error = 0;
542
543 if (empty($fk_user_author)) {
544 $fk_user_author = $user->id;
545 }
546
547 $this->db->begin();
548
549 // get extrafields so they will be clone
550 //foreach($this->lines as $line)
551 //$line->fetch_optionals();
552
553 // Load source object
554 $objFrom = clone $this;
555
556 $this->id = 0;
557 $this->ref = '';
558 $this->status = 0;
559 $this->fk_statut = 0; // deprecated
560
561 // Clear fields
562 $this->fk_user_creat = $user->id;
563 $this->fk_user_author = $fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
564 $this->fk_user_valid = 0;
565 $this->date_create = '';
566 $this->date_creation = '';
567 $this->date_validation = '';
568
569 // Remove link on lines to a joined file
570 if (is_array($this->lines) && count($this->lines) > 0) {
571 foreach ($this->lines as $key => $line) {
572 $this->lines[$key]->fk_ecm_files = 0;
573 }
574 }
575
576 // Create clone
577 $this->context['createfromclone'] = 'createfromclone';
578 $result = $this->create($user);
579 if ($result < 0) {
580 $error++;
581 }
582
583 if (!$error) {
584 // Hook of thirdparty module
585 if (is_object($hookmanager)) {
586 $parameters = array('objFrom' => $objFrom);
587 $action = '';
588 $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
589 if ($reshook < 0) {
590 $this->setErrorsFromObject($hookmanager);
591 $error++;
592 }
593 }
594 }
595
596 unset($this->context['createfromclone']);
597
598 // End
599 if (!$error) {
600 $this->db->commit();
601 return $this->id;
602 } else {
603 $this->db->rollback();
604 return -1;
605 }
606 }
607
608
617 public function update($user, $notrigger = 0, $userofexpensereport = null)
618 {
619 $error = 0;
620 $this->db->begin();
621
622 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
623 $sql .= " total_ht = ".((float) $this->total_ht);
624 $sql .= " , total_ttc = ".((float) $this->total_ttc);
625 $sql .= " , total_tva = ".((float) $this->total_tva);
626 $sql .= " , date_debut = '".$this->db->idate($this->date_debut)."'";
627 $sql .= " , date_fin = '".$this->db->idate($this->date_fin)."'";
628 if ($userofexpensereport && is_object($userofexpensereport)) {
629 $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.
630 }
631 $sql .= " , fk_user_validator = ".($this->fk_user_validator > 0 ? $this->fk_user_validator : "null");
632 $sql .= " , fk_user_valid = ".($this->fk_user_valid > 0 ? $this->fk_user_valid : "null");
633 $sql .= " , fk_user_approve = ".($this->fk_user_approve > 0 ? $this->fk_user_approve : "null");
634 $sql .= " , fk_user_modif = ".((int) $user->id);
635 $sql .= " , fk_statut = ".($this->fk_statut >= 0 ? $this->fk_statut : '0');
636 $sql .= " , fk_c_paiement = ".($this->fk_c_paiement > 0 ? $this->fk_c_paiement : "null");
637 $sql .= " , note_public = ".(!empty($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "''");
638 $sql .= " , note_private = ".(!empty($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "''");
639 $sql .= " , detail_refuse = ".(!empty($this->detail_refuse) ? "'".$this->db->escape($this->detail_refuse)."'" : "''");
640 $sql .= " WHERE rowid = ".((int) $this->id);
641
642 dol_syslog(get_class($this)."::update", LOG_DEBUG);
643 $result = $this->db->query($sql);
644 if ($result) {
645 $result = $this->insertExtraFields();
646 if ($result < 0) {
647 $error++;
648 }
649
650 if (!$error && !$notrigger) {
651 // Call trigger
652 $result = $this->call_trigger('EXPENSE_REPORT_MODIFY', $user);
653 if ($result < 0) {
654 $error++;
655 }
656 // End call triggers
657 }
658
659 if (!$error) {
660 $this->db->commit();
661 return 1;
662 } else {
663 $this->db->rollback();
664 $this->error = $this->db->error();
665 return -2;
666 }
667 } else {
668 $this->db->rollback();
669 $this->error = $this->db->error();
670 return -1;
671 }
672 }
673
681 public function fetch($id, $ref = '')
682 {
683 $sql = "SELECT d.rowid, d.entity, d.ref, d.note_public, d.note_private,"; // DEFAULT
684 $sql .= " d.detail_refuse, d.detail_cancel, d.fk_user_refuse, d.fk_user_cancel,"; // ACTIONS
685 $sql .= " d.date_refuse, d.date_cancel,"; // ACTIONS
686 $sql .= " d.total_ht, d.total_ttc, d.total_tva,";
687 $sql .= " d.localtax1 as total_localtax1, d.localtax2 as total_localtax2,";
688 $sql .= " d.date_debut, d.date_fin, d.date_create, d.tms as date_modif, d.date_valid, d.date_approve,"; // DATES (datetime)
689 $sql .= " d.fk_user_creat, d.fk_user_author, d.fk_user_modif, d.fk_user_validator,";
690 $sql .= " d.fk_user_valid, d.fk_user_approve,";
691 $sql .= " d.fk_statut as status, d.fk_c_paiement, d.paid";
692 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as d";
693 if ($ref) {
694 $sql .= " WHERE d.ref = '".$this->db->escape($ref)."'";
695 } else {
696 $sql .= " WHERE d.rowid = ".((int) $id);
697 }
698 //$sql.= $restrict;
699
700 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
701 $resql = $this->db->query($sql);
702 if ($resql) {
703 $obj = $this->db->fetch_object($resql);
704 if ($obj) {
705 $this->id = $obj->rowid;
706 $this->ref = $obj->ref;
707
708 $this->entity = $obj->entity;
709
710 $this->total_ht = $obj->total_ht;
711 $this->total_tva = $obj->total_tva;
712 $this->total_ttc = $obj->total_ttc;
713 $this->localtax1 = $obj->total_localtax1; // For backward compatibility
714 $this->localtax2 = $obj->total_localtax2; // For backward compatibility
715 $this->total_localtax1 = $obj->total_localtax1;
716 $this->total_localtax2 = $obj->total_localtax2;
717
718 $this->note_public = $obj->note_public;
719 $this->note_private = $obj->note_private;
720 $this->detail_refuse = $obj->detail_refuse;
721 $this->detail_cancel = $obj->detail_cancel;
722
723 $this->date_debut = $this->db->jdate($obj->date_debut);
724 $this->date_fin = $this->db->jdate($obj->date_fin);
725 $this->date_valid = $this->db->jdate($obj->date_valid);
726 $this->date_approve = $this->db->jdate($obj->date_approve);
727 $this->date_create = $this->db->jdate($obj->date_create);
728 $this->date_modif = $this->db->jdate($obj->date_modif);
729 $this->date_refuse = $this->db->jdate($obj->date_refuse);
730 $this->date_cancel = $this->db->jdate($obj->date_cancel);
731
732 $this->fk_user_creat = $obj->fk_user_creat;
733 $this->fk_user_author = $obj->fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
734 $this->fk_user_modif = $obj->fk_user_modif;
735 $this->fk_user_validator = $obj->fk_user_validator;
736 $this->fk_user_valid = $obj->fk_user_valid;
737 $this->fk_user_refuse = $obj->fk_user_refuse;
738 $this->fk_user_cancel = $obj->fk_user_cancel;
739 $this->fk_user_approve = $obj->fk_user_approve;
740
741 $user_author = new User($this->db);
742 if ($this->fk_user_author > 0) {
743 $user_author->fetch($this->fk_user_author);
744 }
745
746 $this->user_author_infos = dolGetFirstLastname($user_author->firstname, $user_author->lastname);
747
748 $user_approver = new User($this->db);
749 if ($this->fk_user_approve > 0) {
750 $user_approver->fetch($this->fk_user_approve);
751 } elseif ($this->fk_user_validator > 0) {
752 $user_approver->fetch($this->fk_user_validator); // For backward compatibility
753 }
754 $this->user_validator_infos = dolGetFirstLastname($user_approver->firstname, $user_approver->lastname);
755
756 $this->fk_statut = $obj->status; // deprecated
757 $this->status = $obj->status;
758 $this->fk_c_paiement = $obj->fk_c_paiement;
759 $this->paid = $obj->paid;
760
761 if ($this->status == self::STATUS_APPROVED || $this->status == self::STATUS_CLOSED) {
762 $user_valid = new User($this->db);
763 if ($this->fk_user_valid > 0) {
764 $user_valid->fetch($this->fk_user_valid);
765 }
766 $this->user_valid_infos = dolGetFirstLastname($user_valid->firstname, $user_valid->lastname);
767 }
768
769 $this->fetch_optionals();
770
771 $result = $this->fetch_lines();
772
773 return $result;
774 } else {
775 return 0;
776 }
777 } else {
778 $this->error = $this->db->lasterror();
779 return -1;
780 }
781 }
782
783 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
794 public function set_paid($id, $fuser, $notrigger = 0)
795 {
796 // phpcs:enable
797 dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
798 return $this->setPaid($id, $fuser, $notrigger);
799 }
800
809 public function setPaid($id, $fuser, $notrigger = 0)
810 {
811 $error = 0;
812 $this->db->begin();
813
814 $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport";
815 $sql .= " SET fk_statut = ".self::STATUS_CLOSED.", paid=1";
816 $sql .= " WHERE rowid = ".((int) $id)." AND fk_statut = ".self::STATUS_APPROVED;
817
818 dol_syslog(get_class($this)."::setPaid", LOG_DEBUG);
819 $resql = $this->db->query($sql);
820 if ($resql) {
821 if ($this->db->affected_rows($resql)) {
822 if (!$notrigger) {
823 // Call trigger
824 $result = $this->call_trigger('EXPENSE_REPORT_PAID', $fuser);
825
826 if ($result < 0) {
827 $error++;
828 }
829 // End call triggers
830 }
831
832 if (empty($error)) {
833 $this->db->commit();
834 return 1;
835 } else {
836 $this->db->rollback();
837 $this->error = $this->db->error();
838 return -2;
839 }
840 } else {
841 $this->db->commit();
842 return 0;
843 }
844 } else {
845 $this->db->rollback();
846 dol_print_error($this->db);
847 return -1;
848 }
849 }
850
857 public function getLibStatut($mode = 0)
858 {
859 return $this->LibStatut($this->status, $mode);
860 }
861
862 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
870 public function LibStatut($status, $mode = 0)
871 {
872 // phpcs:enable
873 global $langs;
874
875 $labelStatus = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
876 $labelStatusShort = $langs->transnoentitiesnoconv($this->labelStatusShort[$status]);
877
878 $statuslogo = array(0 => 'status0', 2 => 'status1', 4 => 'status6', 5 => 'status4', 6 => 'status6', 99 => 'status5');
879
880 $statusType = $statuslogo[$status];
881
882 return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
883 }
884
885
892 public function info($id)
893 {
894 global $conf;
895
896 $sql = "SELECT f.rowid,";
897 $sql .= " f.date_create as datec,";
898 $sql .= " f.tms as date_modification,";
899 $sql .= " f.date_valid as datev,";
900 $sql .= " f.date_approve as datea,";
901 $sql .= " f.fk_user_creat as fk_user_creation,";
902 $sql .= " f.fk_user_author as fk_user_author,";
903 $sql .= " f.fk_user_modif as fk_user_modification,";
904 $sql .= " f.fk_user_valid,";
905 $sql .= " f.fk_user_approve";
906 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as f";
907 $sql .= " WHERE f.rowid = ".((int) $id);
908 $sql .= " AND f.entity = ".$conf->entity;
909
910
911
912 $resql = $this->db->query($sql);
913 if ($resql) {
914 if ($this->db->num_rows($resql)) {
915 $obj = $this->db->fetch_object($resql);
916
917 $this->id = $obj->rowid;
918
919 $this->date_creation = $this->db->jdate($obj->datec);
920 $this->date_modification = $this->db->jdate($obj->date_modification);
921 $this->date_validation = $this->db->jdate($obj->datev);
922 $this->date_approbation = $this->db->jdate($obj->datea);
923
924 $this->user_creation_id = $obj->fk_user_author;
925 $this->user_creation_id = $obj->fk_user_creation;
926 $this->user_validation_id = $obj->fk_user_valid;
927 $this->user_modification_id = $obj->fk_user_modification;
928 $this->user_approve_id = $obj->fk_user_approve;
929 }
930 $this->db->free($resql);
931 } else {
932 dol_print_error($this->db);
933 }
934 }
935
936
937
945 public function initAsSpecimen()
946 {
947 global $user, $langs;
948
949 $now = dol_now();
950
951 // Initialise parameters
952 $this->id = 0;
953 $this->ref = 'SPECIMEN';
954 $this->specimen = 1;
955 $this->entity = 1;
956 $this->date_create = $now;
957 $this->date_debut = $now;
958 $this->date_fin = $now;
959 $this->date_valid = $now;
960 $this->date_approve = $now;
961
962 $type_fees_id = 2; // TF_TRIP
963
964 $this->status = 5;
965
966 $this->fk_user_author = $user->id;
967 $this->fk_user_validator = $user->id;
968 $this->fk_user_valid = $user->id;
969 $this->fk_user_approve = $user->id;
970
971 $this->note_private = 'Private note';
972 $this->note_public = 'SPECIMEN';
973 $nbp = min(1000, GETPOSTINT('nblines') ? GETPOSTINT('nblines') : 5); // We can force the nb of lines to test from command line (but not more than 1000)
974 $xnbp = 0;
975 while ($xnbp < $nbp) {
976 $line = new ExpenseReportLine($this->db);
977 $line->comments = $langs->trans("Comment")." ".$xnbp;
978 $line->date = ($now - 3600 * (1 + $xnbp));
979 $line->total_ht = 100;
980 $line->total_tva = 20;
981 $line->total_ttc = 120;
982 $line->qty = 1;
983 $line->vatrate = 20;
984 $line->value_unit = 120;
985 $line->fk_expensereport = 0;
986 $line->type_fees_code = 'TRA';
987 $line->fk_c_type_fees = $type_fees_id;
988
989 $line->projet_ref = 'ABC';
990
991 $this->lines[$xnbp] = $line;
992 $xnbp++;
993
994 $this->total_ht += $line->total_ht;
995 $this->total_tva += $line->total_tva;
996 $this->total_ttc += $line->total_ttc;
997 }
998
999 return 1;
1000 }
1001
1002 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1010 public function fetch_line_by_project($projectid, $user)
1011 {
1012 // phpcs:enable
1013 global $langs;
1014
1015 $langs->load('trips');
1016
1017 if ($user->hasRight('expensereport', 'lire')) {
1018 $sql = "SELECT de.fk_expensereport, de.date, de.comments, de.total_ht, de.total_ttc";
1019 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_det as de";
1020 $sql .= " WHERE de.fk_projet = ".((int) $projectid);
1021
1022 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1023 $result = $this->db->query($sql);
1024 if ($result) {
1025 $num = $this->db->num_rows($result);
1026 $i = 0;
1027 $total_HT = 0;
1028 $total_TTC = 0;
1029
1030 while ($i < $num) {
1031 $objp = $this->db->fetch_object($result);
1032
1033 $sql2 = "SELECT d.rowid, d.fk_user_author, d.ref, d.fk_statut as status";
1034 $sql2 .= " FROM ".MAIN_DB_PREFIX."expensereport as d";
1035 $sql2 .= " WHERE d.rowid = ".((int) $objp->fk_expensereport);
1036
1037 $result2 = $this->db->query($sql2);
1038 $obj = $this->db->fetch_object($result2);
1039
1040 $objp->fk_user_author = $obj->fk_user_author;
1041 $objp->ref = $obj->ref;
1042 $objp->fk_c_expensereport_status = $obj->status;
1043 $objp->rowid = $obj->rowid;
1044
1045 $total_HT += $objp->total_ht;
1046 $total_TTC += $objp->total_ttc;
1047 $author = new User($this->db);
1048 $author->fetch($objp->fk_user_author);
1049
1050 print '<tr>';
1051 print '<td><a href="'.DOL_URL_ROOT.'/expensereport/card.php?id='.$objp->rowid.'">'.$objp->ref_num.'</a></td>';
1052 print '<td class="center">'.dol_print_date($objp->date, 'day').'</td>';
1053 print '<td>'.$author->getNomUrl(1).'</td>';
1054 print '<td>'.$objp->comments.'</td>';
1055 print '<td class="right">'.price($objp->total_ht).'</td>';
1056 print '<td class="right">'.price($objp->total_ttc).'</td>';
1057 print '<td class="right">';
1058
1059 switch ($objp->fk_c_expensereport_status) {
1060 case 4:
1061 print img_picto($langs->trans('StatusOrderCanceled'), 'statut5');
1062 break;
1063 case 1:
1064 print $langs->trans('Draft').' '.img_picto($langs->trans('Draft'), 'statut0');
1065 break;
1066 case 2:
1067 print $langs->trans('TripForValid').' '.img_picto($langs->trans('TripForValid'), 'statut3');
1068 break;
1069 case 5:
1070 print $langs->trans('TripForPaid').' '.img_picto($langs->trans('TripForPaid'), 'statut3');
1071 break;
1072 case 6:
1073 print $langs->trans('TripPaid').' '.img_picto($langs->trans('TripPaid'), 'statut4');
1074 break;
1075 }
1076 /*
1077 if ($status==4) return img_picto($langs->trans('StatusOrderCanceled'),'statut5');
1078 if ($status==1) return img_picto($langs->trans('StatusOrderDraft'),'statut0');
1079 if ($status==2) return img_picto($langs->trans('StatusOrderValidated'),'statut1');
1080 if ($status==2) return img_picto($langs->trans('StatusOrderOnProcess'),'statut3');
1081 if ($status==5) return img_picto($langs->trans('StatusOrderToBill'),'statut4');
1082 if ($status==6) return img_picto($langs->trans('StatusOrderOnProcess'),'statut6');
1083 */
1084 print '</td>';
1085 print '</tr>';
1086
1087 $i++;
1088 }
1089
1090 print '<tr class="liste_total"><td colspan="4">'.$langs->trans("Number").': '.$i.'</td>';
1091 print '<td class="right" width="100">'.$langs->trans("TotalHT").' : '.price($total_HT).'</td>';
1092 print '<td class="right" width="100">'.$langs->trans("TotalTTC").' : '.price($total_TTC).'</td>';
1093 print '<td>&nbsp;</td>';
1094 print '</tr>';
1095 } else {
1096 $this->error = $this->db->lasterror();
1097 return -1;
1098 }
1099 }
1100
1101 return 0;
1102 }
1103
1104 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1110 public function fetch_lines()
1111 {
1112 // phpcs:enable
1113 $this->lines = array();
1114
1115 $sql = ' SELECT de.rowid, de.comments, de.qty, de.value_unit, de.date, de.rang,';
1116 $sql .= " de.".$this->fk_element.", de.fk_c_type_fees, de.fk_c_exp_tax_cat, de.fk_projet as fk_project,";
1117 $sql .= ' de.tva_tx, de.vat_src_code,';
1118 $sql .= ' de.localtax1_tx, de.localtax2_tx, de.localtax1_type, de.localtax2_type,';
1119 $sql .= ' de.fk_ecm_files,';
1120 $sql .= ' de.total_ht, de.total_tva, de.total_ttc,';
1121 $sql .= ' de.total_localtax1, de.total_localtax2, de.rule_warning_message,';
1122 $sql .= ' ctf.code as code_type_fees, ctf.label as label_type_fees, ctf.accountancy_code as accountancy_code_type_fees,';
1123 $sql .= ' p.ref as ref_projet, p.title as title_projet';
1124 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as de';
1125 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON de.fk_c_type_fees = ctf.id';
1126 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as p ON de.fk_projet = p.rowid';
1127 $sql .= " WHERE de.".$this->fk_element." = ".((int) $this->id);
1128 if (getDolGlobalString('EXPENSEREPORT_LINES_SORTED_BY_ROWID')) {
1129 $sql .= ' ORDER BY de.rang ASC, de.rowid ASC';
1130 } else {
1131 $sql .= ' ORDER BY de.rang ASC, de.date ASC';
1132 }
1133
1134 $resql = $this->db->query($sql);
1135 if ($resql) {
1136 $num = $this->db->num_rows($resql);
1137 $i = 0;
1138 while ($i < $num) {
1139 $objp = $this->db->fetch_object($resql);
1140
1141 $deplig = new ExpenseReportLine($this->db);
1142
1143 $deplig->rowid = $objp->rowid;
1144 $deplig->id = $objp->rowid;
1145 $deplig->comments = $objp->comments;
1146 $deplig->qty = $objp->qty;
1147 $deplig->value_unit = $objp->value_unit;
1148 $deplig->date = $objp->date;
1149 $deplig->dates = $this->db->jdate($objp->date);
1150
1151 $deplig->fk_expensereport = $objp->fk_expensereport;
1152 $deplig->fk_c_type_fees = $objp->fk_c_type_fees;
1153 $deplig->fk_c_exp_tax_cat = $objp->fk_c_exp_tax_cat;
1154 $deplig->fk_projet = $objp->fk_project; // deprecated
1155 $deplig->fk_project = $objp->fk_project;
1156 $deplig->fk_ecm_files = $objp->fk_ecm_files;
1157
1158 $deplig->total_ht = $objp->total_ht;
1159 $deplig->total_tva = $objp->total_tva;
1160 $deplig->total_ttc = $objp->total_ttc;
1161 $deplig->total_localtax1 = $objp->total_localtax1;
1162 $deplig->total_localtax2 = $objp->total_localtax2;
1163
1164 $deplig->type_fees_code = empty($objp->code_type_fees) ? 'TF_OTHER' : $objp->code_type_fees;
1165 $deplig->type_fees_libelle = $objp->label_type_fees;
1166 $deplig->type_fees_accountancy_code = $objp->accountancy_code_type_fees;
1167
1168 $deplig->tva_tx = $objp->tva_tx;
1169 $deplig->vatrate = $objp->tva_tx;
1170 $deplig->vat_src_code = $objp->vat_src_code;
1171 $deplig->localtax1_tx = $objp->localtax1_tx;
1172 $deplig->localtax2_tx = $objp->localtax2_tx;
1173 $deplig->localtax1_type = $objp->localtax1_type;
1174 $deplig->localtax2_type = $objp->localtax2_type;
1175
1176 $deplig->projet_ref = $objp->ref_projet;
1177 $deplig->projet_title = $objp->title_projet;
1178
1179 $deplig->rule_warning_message = $objp->rule_warning_message;
1180
1181 $deplig->rang = $objp->rang;
1182
1183 $this->lines[$i] = $deplig;
1184
1185 $i++;
1186 }
1187 $this->db->free($resql);
1188 return 1;
1189 } else {
1190 $this->error = $this->db->lasterror();
1191 dol_syslog(get_class($this)."::fetch_lines: Error ".$this->error, LOG_ERR);
1192 return -3;
1193 }
1194 }
1195
1196
1204 public function delete($user = null, $notrigger = 0)
1205 {
1206 global $conf;
1207 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1208
1209 $error = 0;
1210
1211 $this->db->begin();
1212
1213 if (!$notrigger) {
1214 // Call trigger
1215 $result = $this->call_trigger('EXPENSE_REPORT_DELETE', $user);
1216 if ($result < 0) {
1217 $error++;
1218 }
1219 // End call triggers
1220 }
1221
1222 // Delete extrafields of lines and lines
1223 if (!$error && !empty($this->table_element_line)) {
1224 $tabletodelete = $this->table_element_line;
1225 //$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).")";
1226 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".((int) $this->id);
1227 if (!$this->db->query($sql)) {
1228 $error++;
1229 $this->error = $this->db->lasterror();
1230 $this->errors[] = $this->error;
1231 dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
1232 }
1233 }
1234
1235 if (!$error) {
1236 // Delete linked object
1237 $res = $this->deleteObjectLinked();
1238 if ($res < 0) {
1239 $error++;
1240 }
1241 }
1242
1243 if (!$error) {
1244 // Delete linked contacts
1245 $res = $this->delete_linked_contact();
1246 if ($res < 0) {
1247 $error++;
1248 }
1249 }
1250
1251 // Removed extrafields of object
1252 if (!$error) {
1253 $result = $this->deleteExtraFields();
1254 if ($result < 0) {
1255 $error++;
1256 dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
1257 }
1258 }
1259
1260 // Delete main record
1261 if (!$error) {
1262 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id);
1263 $res = $this->db->query($sql);
1264 if (!$res) {
1265 $error++;
1266 $this->error = $this->db->lasterror();
1267 $this->errors[] = $this->error;
1268 dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
1269 }
1270 }
1271
1272 // Delete record into ECM index and physically
1273 if (!$error) {
1274 $res = $this->deleteEcmFiles(0); // Deleting files physically is done later with the dol_delete_dir_recursive
1275 $res = $this->deleteEcmFiles(1); // Deleting files physically is done later with the dol_delete_dir_recursive
1276 if (!$res) {
1277 $error++;
1278 }
1279 }
1280
1281 if (!$error) {
1282 // We remove directory
1283 $ref = dol_sanitizeFileName($this->ref);
1284 if ($conf->expensereport->multidir_output[$this->entity] && !empty($this->ref)) {
1285 $dir = $conf->expensereport->multidir_output[$this->entity]."/".$ref;
1286 $file = $dir."/".$ref.".pdf";
1287 if (file_exists($file)) {
1288 dol_delete_preview($this);
1289
1290 if (!dol_delete_file($file, 0, 0, 0, $this)) {
1291 $this->error = 'ErrorFailToDeleteFile';
1292 $this->errors[] = $this->error;
1293 $this->db->rollback();
1294 return 0;
1295 }
1296 }
1297 if (file_exists($dir)) {
1298 $res = @dol_delete_dir_recursive($dir);
1299 if (!$res) {
1300 $this->error = 'ErrorFailToDeleteDir';
1301 $this->errors[] = $this->error;
1302 $this->db->rollback();
1303 return 0;
1304 }
1305 }
1306 }
1307 }
1308
1309 if (!$error) {
1310 dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
1311 $this->db->commit();
1312 return 1;
1313 } else {
1314 $this->db->rollback();
1315 return -1;
1316 }
1317 }
1318
1326 public function setValidate($fuser, $notrigger = 0)
1327 {
1328 global $conf, $langs, $user;
1329
1330 $error = 0;
1331 $now = dol_now();
1332
1333 // Protection
1334 if ($this->status == self::STATUS_VALIDATED) {
1335 dol_syslog(get_class($this)."::valid action abandoned: already validated", LOG_WARNING);
1336 return 0;
1337 }
1338
1339 $this->date_valid = $now; // Required for the getNextNum later.
1340
1341 // Define new ref
1342 if (!$error && (preg_match('/^[\‍(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
1343 $num = $this->getNextNumRef();
1344 } else {
1345 $num = $this->ref;
1346 }
1347 if (empty($num) || $num < 0) {
1348 return -1;
1349 }
1350
1351 $this->newref = dol_sanitizeFileName($num);
1352
1353 $this->db->begin();
1354
1355 // Validate
1356 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1357 $sql .= " SET ref = '".$this->db->escape($num)."',";
1358 $sql .= " fk_statut = ".self::STATUS_VALIDATED.",";
1359 $sql .= " date_valid = '".$this->db->idate($this->date_valid)."',";
1360 $sql .= " fk_user_valid = ".((int) $user->id);
1361 $sql .= " WHERE rowid = ".((int) $this->id);
1362
1363 $resql = $this->db->query($sql);
1364 if ($resql) {
1365 if (!$error && !$notrigger) {
1366 // Call trigger
1367 $result = $this->call_trigger('EXPENSE_REPORT_VALIDATE', $fuser);
1368 if ($result < 0) {
1369 $error++;
1370 }
1371 // End call triggers
1372 }
1373
1374 if (!$error) {
1375 $this->oldref = $this->ref;
1376
1377 // Rename directory if dir was a temporary ref
1378 if (preg_match('/^[\‍(]?PROV/i', $this->ref)) {
1379 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1380
1381 // Now we rename also files into index
1382 $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)."'";
1383 $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'expensereport/".$this->db->escape($this->ref)."' AND entity = ".((int) $this->entity);
1384 $resql = $this->db->query($sql);
1385 if (!$resql) {
1386 $error++;
1387 $this->error = $this->db->lasterror();
1388 }
1389 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'expensereport/".$this->db->escape($this->newref)."'";
1390 $sql .= " WHERE filepath = 'expensereport/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
1391 $resql = $this->db->query($sql);
1392 if (!$resql) {
1393 $error++;
1394 $this->error = $this->db->lasterror();
1395 }
1396
1397 // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
1398 $oldref = dol_sanitizeFileName($this->ref);
1399 $newref = dol_sanitizeFileName($num);
1400 $dirsource = $conf->expensereport->multidir_output[$this->entity].'/'.$oldref;
1401 $dirdest = $conf->expensereport->multidir_output[$this->entity].'/'.$newref;
1402 if (!$error && file_exists($dirsource)) {
1403 dol_syslog(get_class($this)."::setValidate() rename dir ".$dirsource." into ".$dirdest);
1404
1405 if (@rename($dirsource, $dirdest)) {
1406 dol_syslog("Rename ok");
1407 // Rename docs starting with $oldref with $newref
1408 $listoffiles = dol_dir_list($dirdest, 'files', 1, '^'.preg_quote($oldref, '/'));
1409 foreach ($listoffiles as $fileentry) {
1410 $dirsource = $fileentry['name'];
1411 $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
1412 $dirsource = $fileentry['path'].'/'.$dirsource;
1413 $dirdest = $fileentry['path'].'/'.$dirdest;
1414 @rename($dirsource, $dirdest);
1415 }
1416 }
1417 }
1418 }
1419 }
1420
1421 // Set new ref and current status
1422 if (!$error) {
1423 $this->ref = $num;
1425 }
1426
1427 if (empty($error)) {
1428 $this->db->commit();
1429 return 1;
1430 } else {
1431 $this->db->rollback();
1432 $this->error = $this->db->error();
1433 return -2;
1434 }
1435 } else {
1436 $this->db->rollback();
1437 $this->error = $this->db->lasterror();
1438 return -1;
1439 }
1440 }
1441
1442 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1449 public function set_save_from_refuse($fuser)
1450 {
1451 // phpcs:enable
1452 // Sélection de la date de début de la NDF
1453 $sql = 'SELECT date_debut';
1454 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element;
1455 $sql .= " WHERE rowid = ".((int) $this->id);
1456
1457 $result = $this->db->query($sql);
1458
1459 $objp = $this->db->fetch_object($result);
1460
1461 $this->date_debut = $this->db->jdate($objp->date_debut);
1462
1463 if ($this->status != self::STATUS_VALIDATED) {
1464 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1465 $sql .= " SET fk_statut = ".self::STATUS_VALIDATED;
1466 $sql .= " WHERE rowid = ".((int) $this->id);
1467
1468 dol_syslog(get_class($this)."::set_save_from_refuse", LOG_DEBUG);
1469
1470 if ($this->db->query($sql)) {
1471 return 1;
1472 } else {
1473 $this->error = $this->db->lasterror();
1474 return -1;
1475 }
1476 } else {
1477 dol_syslog(get_class($this)."::set_save_from_refuse expensereport already with save status", LOG_WARNING);
1478 }
1479
1480 return 0;
1481 }
1482
1490 public function setApproved($fuser, $notrigger = 0)
1491 {
1492 $now = dol_now();
1493 $error = 0;
1494
1495 // date approval
1496 $this->date_approve = $now;
1497 if ($this->status != self::STATUS_APPROVED) {
1498 $this->db->begin();
1499
1500 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1501 $sql .= " SET ref = '".$this->db->escape($this->ref)."', fk_statut = ".self::STATUS_APPROVED.", fk_user_approve = ".((int) $fuser->id).",";
1502 $sql .= " date_approve='".$this->db->idate($this->date_approve)."'";
1503 $sql .= " WHERE rowid = ".((int) $this->id);
1504 if ($this->db->query($sql)) {
1505 if (!$notrigger) {
1506 // Call trigger
1507 $result = $this->call_trigger('EXPENSE_REPORT_APPROVE', $fuser);
1508
1509 if ($result < 0) {
1510 $error++;
1511 }
1512 // End call triggers
1513 }
1514
1515 if (empty($error)) {
1516 $this->db->commit();
1517 return 1;
1518 } else {
1519 $this->db->rollback();
1520 $this->error = $this->db->error();
1521 return -2;
1522 }
1523 } else {
1524 $this->db->rollback();
1525 $this->error = $this->db->lasterror();
1526 return -1;
1527 }
1528 } else {
1529 dol_syslog(get_class($this)."::setApproved expensereport already with approve status", LOG_WARNING);
1530 }
1531
1532 return 0;
1533 }
1534
1543 public function setDeny($fuser, $details, $notrigger = 0)
1544 {
1545 $now = dol_now();
1546 $error = 0;
1547
1548 // date de refus
1549 if ($this->status != self::STATUS_REFUSED) {
1550 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1551 $sql .= " SET ref = '".$this->db->escape($this->ref)."', fk_statut = ".self::STATUS_REFUSED.", fk_user_refuse = ".((int) $fuser->id).",";
1552 $sql .= " date_refuse='".$this->db->idate($now)."',";
1553 $sql .= " detail_refuse='".$this->db->escape($details)."',";
1554 $sql .= " fk_user_approve = NULL";
1555 $sql .= " WHERE rowid = ".((int) $this->id);
1556 if ($this->db->query($sql)) {
1557 $this->fk_statut = 99; // deprecated
1558 $this->status = 99;
1559 $this->fk_user_refuse = $fuser->id;
1560 $this->detail_refuse = $details;
1561 $this->date_refuse = $now;
1562
1563 if (!$notrigger) {
1564 // Call trigger
1565 $result = $this->call_trigger('EXPENSE_REPORT_DENY', $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->lasterror();
1584 return -1;
1585 }
1586 } else {
1587 dol_syslog(get_class($this)."::setDeny expensereport already with refuse status", LOG_WARNING);
1588 }
1589
1590 return 0;
1591 }
1592
1593 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1603 public function set_unpaid($fuser, $notrigger = 0)
1604 {
1605 // phpcs:enable
1606 dol_syslog(get_class($this)."::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
1607 return $this->setUnpaid($fuser, $notrigger);
1608 }
1609
1617 public function setUnpaid($fuser, $notrigger = 0)
1618 {
1619 $error = 0;
1620
1621 if ($this->paid) {
1622 $this->db->begin();
1623
1624 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1625 $sql .= " SET paid = 0, fk_statut = ".self::STATUS_APPROVED;
1626 $sql .= " WHERE rowid = ".((int) $this->id);
1627
1628 dol_syslog(get_class($this)."::set_unpaid", LOG_DEBUG);
1629
1630 if ($this->db->query($sql)) {
1631 if (!$notrigger) {
1632 // Call trigger
1633 $result = $this->call_trigger('EXPENSE_REPORT_UNPAID', $fuser);
1634
1635 if ($result < 0) {
1636 $error++;
1637 }
1638 // End call triggers
1639 }
1640
1641 if (empty($error)) {
1642 $this->db->commit();
1643 return 1;
1644 } else {
1645 $this->db->rollback();
1646 $this->error = $this->db->error();
1647 return -2;
1648 }
1649 } else {
1650 $this->db->rollback();
1651 $this->error = $this->db->error();
1652 return -1;
1653 }
1654 } else {
1655 dol_syslog(get_class($this)."::set_unpaid expensereport already with unpaid status", LOG_WARNING);
1656 }
1657
1658 return 0;
1659 }
1660
1661 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1670 public function set_cancel($fuser, $detail, $notrigger = 0)
1671 {
1672 // phpcs:enable
1673 $error = 0;
1674 $this->date_cancel = $this->db->idate(dol_now());
1675 if ($this->status != self::STATUS_CANCELED) {
1676 $this->db->begin();
1677
1678 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1679 $sql .= " SET fk_statut = ".self::STATUS_CANCELED.", fk_user_cancel = ".((int) $fuser->id);
1680 $sql .= ", date_cancel='".$this->db->idate($this->date_cancel)."'";
1681 $sql .= ", detail_cancel='".$this->db->escape($detail)."'";
1682 $sql .= " WHERE rowid = ".((int) $this->id);
1683
1684 dol_syslog(get_class($this)."::set_cancel", LOG_DEBUG);
1685
1686 if ($this->db->query($sql)) {
1687 if (!$notrigger) {
1688 // Call trigger
1689 $result = $this->call_trigger('EXPENSE_REPORT_CANCEL', $fuser);
1690
1691 if ($result < 0) {
1692 $error++;
1693 }
1694 // End call triggers
1695 }
1696
1697 if (empty($error)) {
1698 $this->db->commit();
1699 return 1;
1700 } else {
1701 $this->db->rollback();
1702 $this->error = $this->db->error();
1703 return -2;
1704 }
1705 } else {
1706 $this->db->rollback();
1707 $this->error = $this->db->error();
1708 return -1;
1709 }
1710 } else {
1711 dol_syslog(get_class($this)."::set_cancel expensereport already with cancel status", LOG_WARNING);
1712 }
1713 return 0;
1714 }
1715
1721 public function getNextNumRef()
1722 {
1723 global $langs, $conf;
1724 $langs->load("trips");
1725
1726 if (getDolGlobalString('EXPENSEREPORT_ADDON')) {
1727 $mybool = false;
1728
1729 $file = getDolGlobalString('EXPENSEREPORT_ADDON') . ".php";
1730 $classname = getDolGlobalString('EXPENSEREPORT_ADDON');
1731
1732 // Include file with class
1733 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
1734 foreach ($dirmodels as $reldir) {
1735 $dir = dol_buildpath($reldir."core/modules/expensereport/");
1736
1737 // Load file with numbering class (if found)
1738 $mybool = ((bool) @include_once $dir.$file) || $mybool;
1739 }
1740
1741 if (!$mybool) {
1742 dol_print_error(null, "Failed to include file ".$file);
1743 return '';
1744 }
1745
1746 $obj = new $classname();
1747 '@phan-var-force ModeleNumRefExpenseReport $obj';
1748 $numref = $obj->getNextValue($this);
1749
1750 if ($numref != "") {
1751 return $numref;
1752 } else {
1753 $this->error = $obj->error;
1754 $this->errors = $obj->errors;
1755 //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
1756 return -1;
1757 }
1758 } else {
1759 $this->error = "Error_EXPENSEREPORT_ADDON_NotDefined";
1760 return -2;
1761 }
1762 }
1763
1770 public function getTooltipContentArray($params)
1771 {
1772 global $conf, $langs;
1773
1774 $langs->load('trips');
1775
1776 $nofetch = !empty($params['nofetch']);
1777 $moretitle = $params['moretitle'] ?? '';
1778
1779 $datas = array();
1780 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("ExpenseReport").'</u>';
1781 if (isset($this->status)) {
1782 $datas['picto'] .= ' '.$this->getLibStatut(5);
1783 }
1784 if ($moretitle) {
1785 $datas['picto'] .= ' - '.$moretitle;
1786 }
1787 if (!empty($this->ref)) {
1788 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1789 }
1790 if (!empty($this->total_ht)) {
1791 $datas['total_ht'] = '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
1792 }
1793 if (!empty($this->total_tva)) {
1794 $datas['total_tva'] = '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
1795 }
1796 if (!empty($this->total_ttc)) {
1797 $datas['total_ttc'] = '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
1798 }
1799
1800 return $datas;
1801 }
1802
1815 public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1)
1816 {
1817 global $langs, $hookmanager;
1818
1819 $result = '';
1820
1821 $url = DOL_URL_ROOT.'/expensereport/card.php?id='.$this->id;
1822
1823 if ($short) {
1824 return $url;
1825 }
1826
1827 $params = [
1828 'id' => $this->id,
1829 'objecttype' => $this->element,
1830 'option' => $option,
1831 'moretitle' => $moretitle,
1832 'nofetch' => 1,
1833 ];
1834 $classfortooltip = 'classfortooltip';
1835 $dataparams = '';
1836 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1837 $classfortooltip = 'classforajaxtooltip';
1838 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1839 $label = '';
1840 } else {
1841 $label = implode($this->getTooltipContentArray($params));
1842 }
1843
1844 if ($option != 'nolink') {
1845 // Add param to save lastsearch_values or not
1846 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1847 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1848 $add_save_lastsearch_values = 1;
1849 }
1850 if ($add_save_lastsearch_values) {
1851 $url .= '&save_lastsearch_values=1';
1852 }
1853 }
1854
1855 $ref = $this->ref;
1856 if (empty($ref)) {
1857 $ref = $this->id;
1858 }
1859
1860 $linkclose = '';
1861 if (empty($notooltip)) {
1862 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1863 $label = $langs->trans("ShowExpenseReport");
1864 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1865 }
1866 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1867 $linkclose .= $dataparams.' class="'.$classfortooltip.'"';
1868 }
1869
1870 $linkstart = '<a href="'.$url.'"';
1871 $linkstart .= $linkclose.'>';
1872 $linkend = '</a>';
1873
1874 $result .= $linkstart;
1875 if ($withpicto) {
1876 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1877 }
1878 if ($withpicto != 2) {
1879 $result .= ($max ? dol_trunc($ref, $max) : $ref);
1880 }
1881 $result .= $linkend;
1882
1883 global $action;
1884 $hookmanager->initHooks(array($this->element . 'dao'));
1885 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1886 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1887 if ($reshook > 0) {
1888 $result = $hookmanager->resPrint;
1889 } else {
1890 $result .= $hookmanager->resPrint;
1891 }
1892 return $result;
1893 }
1894
1895 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1903 public function update_totaux_add($ligne_total_ht, $ligne_total_tva)
1904 {
1905 // phpcs:enable
1906 $this->total_ht += (float) $ligne_total_ht;
1907 $this->total_tva += (float) $ligne_total_tva;
1908 $this->total_ttc += $this->total_tva;
1909
1910 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
1911 $sql .= " total_ht = ".((float) $this->total_ht);
1912 $sql .= " , total_ttc = ".((float) $this->total_ttc);
1913 $sql .= " , total_tva = ".((float) $this->total_tva);
1914 $sql .= " WHERE rowid = ".((int) $this->id);
1915
1916 $result = $this->db->query($sql);
1917 if ($result) {
1918 return 1;
1919 } else {
1920 $this->error = $this->db->error();
1921 return -1;
1922 }
1923 }
1924
1940 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)
1941 {
1942 global $langs, $mysoc;
1943
1944 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);
1945
1946 if ($this->status == self::STATUS_DRAFT || $this->status == self::STATUS_REFUSED) {
1947 if (empty($qty)) {
1948 $qty = 0;
1949 }
1950 if (empty($fk_c_type_fees) || $fk_c_type_fees < 0) {
1951 $fk_c_type_fees = 0;
1952 }
1953 if (empty($fk_c_exp_tax_cat) || $fk_c_exp_tax_cat < 0) {
1954 $fk_c_exp_tax_cat = 0;
1955 }
1956 if (empty($vatrate) || $vatrate < 0) {
1957 $vatrate = 0;
1958 }
1959 if (empty($date)) {
1960 $date = '';
1961 }
1962 if (empty($fk_project)) {
1963 $fk_project = 0;
1964 }
1965
1966 $qty = (float) price2num($qty);
1967 if (!preg_match('/\s*\‍((.*)\‍)/', $vatrate)) {
1968 $vatrate = price2num($vatrate); // $txtva can have format '5.0 (XXX)' or '5'
1969 }
1970 $up = price2num($up);
1971
1972 $this->db->begin();
1973
1974 $this->line = new ExpenseReportLine($this->db);
1975
1976 // We don't know seller and buyer for expense reports
1977 $seller = $mysoc; // We use same than current company (expense report are often done in same country)
1978 $seller->tva_assuj = 1; // Most seller uses vat
1979 $buyer = new Societe($this->db);
1980
1981 $localtaxes_type = getLocalTaxesFromRate($vatrate, 0, $buyer, $seller);
1982
1983 $vat_src_code = '';
1984 $reg = array();
1985 if (preg_match('/\s*\‍((.*)\‍)/', $vatrate, $reg)) {
1986 $vat_src_code = $reg[1];
1987 $vatrate = preg_replace('/\s*\‍(.*\‍)/', '', $vatrate); // Remove code into vatrate.
1988 }
1989 $vatrate = preg_replace('/\*/', '', $vatrate);
1990
1991 $tmp = calcul_price_total($qty, $up, 0, (float) price2num($vatrate), -1, -1, 0, 'TTC', 0, $type, $seller, $localtaxes_type);
1992
1993 $this->line->value_unit = $up;
1994
1995 $this->line->vat_src_code = $vat_src_code;
1996 $this->line->vatrate = price2num($vatrate);
1997 $this->line->localtax1_tx = $localtaxes_type[1];
1998 $this->line->localtax2_tx = $localtaxes_type[3];
1999 $this->line->localtax1_type = $localtaxes_type[0];
2000 $this->line->localtax2_type = $localtaxes_type[2];
2001
2002 $this->line->total_ttc = (float) $tmp[2];
2003 $this->line->total_ht = (float) $tmp[0];
2004 $this->line->total_tva = (float) $tmp[1];
2005 $this->line->total_localtax1 = (float) $tmp[9];
2006 $this->line->total_localtax2 = (float) $tmp[10];
2007
2008 $this->line->fk_expensereport = $this->id;
2009 $this->line->qty = $qty;
2010 $this->line->date = $date;
2011 $this->line->fk_c_type_fees = $fk_c_type_fees;
2012 $this->line->fk_c_exp_tax_cat = $fk_c_exp_tax_cat;
2013 $this->line->comments = $comments;
2014 $this->line->fk_projet = $fk_project; // deprecated
2015 $this->line->fk_project = $fk_project;
2016
2017 $this->line->fk_ecm_files = $fk_ecm_files;
2018
2019 $this->applyOffset();
2020 $this->checkRules($type, $seller);
2021
2022 $result = $this->line->insert(0, true);
2023 if ($result > 0) {
2024 $result = $this->update_price(1); // This method is designed to add line from user input so total calculation must be done using 'auto' mode.
2025 if ($result > 0) {
2026 $this->db->commit();
2027 return $this->line->id;
2028 } else {
2029 $this->db->rollback();
2030 return -1;
2031 }
2032 } else {
2033 $this->error = $this->line->error;
2034 dol_syslog(get_class($this)."::addline error=".$this->error, LOG_ERR);
2035 $this->db->rollback();
2036 return -2;
2037 }
2038 } else {
2039 dol_syslog(get_class($this)."::addline status of expense report must be Draft to allow use of ->addline()", LOG_ERR);
2040 $this->error = 'ErrorExpenseNotDraftAndNotRefused';
2041 return -3;
2042 }
2043 }
2044
2052 public function checkRules($type = 0, $seller = '')
2053 {
2054 global $conf, $db, $langs, $mysoc;
2055
2056 $langs->load('trips');
2057
2058 // We don't know seller and buyer for expense reports
2059 if (!is_object($seller)) {
2060 $seller = $mysoc; // We use same than current company (expense report are often done in same country)
2061 $seller->tva_assuj = 1; // Most seller uses vat
2062 }
2063
2064 $expensereportrule = new ExpenseReportRule($db);
2065 $rulestocheck = $expensereportrule->getAllRule($this->line->fk_c_type_fees, $this->line->date, $this->fk_user_author);
2066
2067 $violation = 0;
2068 $rule_warning_message_tab = array();
2069
2070 $current_total_ttc = $this->line->total_ttc;
2071 $new_current_total_ttc = $this->line->total_ttc;
2072
2073 // check if one is violated
2074 foreach ($rulestocheck as $rule) {
2075 if (in_array($rule->code_expense_rules_type, array('EX_DAY', 'EX_MON', 'EX_YEA'))) {
2076 $amount_to_test = $this->line->getExpAmount($rule, $this->fk_user_author, $rule->code_expense_rules_type);
2077 } else {
2078 $amount_to_test = $current_total_ttc; // EX_EXP
2079 }
2080
2081 $amount_to_test = $amount_to_test - $current_total_ttc + $new_current_total_ttc; // if amount as been modified by a previous rule
2082
2083 if ($amount_to_test > $rule->amount) {
2084 $violation++;
2085
2086 if ($rule->restrictive) {
2087 $this->error = 'ExpenseReportConstraintViolationError';
2088 $this->errors[] = $this->error;
2089
2090 $new_current_total_ttc -= $amount_to_test - $rule->amount; // ex, entered 16€, limit 12€, subtracts 4€;
2091 $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));
2092 } else {
2093 $this->error = 'ExpenseReportConstraintViolationWarning';
2094 $this->errors[] = $this->error;
2095
2096 $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));
2097 }
2098
2099 // No break, we should test if another rule is violated
2100 }
2101 }
2102
2103 $this->line->rule_warning_message = implode('\n', $rule_warning_message_tab);
2104
2105 if ($violation > 0) {
2106 $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);
2107
2108 $this->line->value_unit = $tmp[5];
2109 $this->line->total_ttc = (float) $tmp[2];
2110 $this->line->total_ht = (float) $tmp[0];
2111 $this->line->total_tva = (float) $tmp[1];
2112 $this->line->total_localtax1 = (float) $tmp[9];
2113 $this->line->total_localtax2 = (float) $tmp[10];
2114
2115 return false;
2116 } else {
2117 return true;
2118 }
2119 }
2120
2128 public function applyOffset($type = 0, $seller = '')
2129 {
2130 global $mysoc;
2131
2132 if (!getDolGlobalString('MAIN_USE_EXPENSE_IK')) {
2133 return false;
2134 }
2135
2136 $userauthor = new User($this->db);
2137 if ($userauthor->fetch($this->fk_user_author) <= 0) {
2138 $this->error = 'ErrorCantFetchUser';
2139 $this->errors[] = 'ErrorCantFetchUser';
2140 return false;
2141 }
2142
2143 // We don't know seller and buyer for expense reports
2144 if (!is_object($seller)) {
2145 $seller = $mysoc; // We use same than current company (expense report are often done in same country)
2146 $seller->tva_assuj = 1; // Most seller uses vat
2147 }
2148
2149 $expenseik = new ExpenseReportIk($this->db);
2150 $range = $expenseik->getRangeByUser($userauthor, $this->line->fk_c_exp_tax_cat);
2151
2152 if (empty($range)) {
2153 $this->error = 'ErrorNoRangeAvailable';
2154 $this->errors[] = 'ErrorNoRangeAvailable';
2155 return false;
2156 }
2157
2158 if (getDolGlobalString('MAIN_EXPENSE_APPLY_ENTIRE_OFFSET')) {
2159 $ikoffset = $range->ikoffset;
2160 } else {
2161 $ikoffset = $range->ikoffset / 12; // The amount of offset is a global value for the year
2162 }
2163
2164 // Test if ikoffset has been applied for the current month
2165 if (!$this->offsetAlreadyGiven()) {
2166 $new_up = $range->coef + ($ikoffset / $this->line->qty);
2167 $tmp = calcul_price_total($this->line->qty, $new_up, 0, $this->line->vatrate, 0, 0, 0, 'TTC', 0, $type, $seller);
2168
2169 $this->line->value_unit = $tmp[5];
2170 $this->line->total_ttc = (float) $tmp[2];
2171 $this->line->total_ht = (float) $tmp[0];
2172 $this->line->total_tva = (float) $tmp[1];
2173 $this->line->total_localtax1 = (float) $tmp[9];
2174 $this->line->total_localtax2 = (float) $tmp[10];
2175
2176 return true;
2177 }
2178
2179 return false;
2180 }
2181
2187 public function offsetAlreadyGiven()
2188 {
2189 $sql = 'SELECT e.rowid FROM '.MAIN_DB_PREFIX.'expensereport e';
2190 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."expensereport_det d ON (e.rowid = d.fk_expensereport)";
2191 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."c_type_fees f ON (d.fk_c_type_fees = f.id AND f.code = 'EX_KME')";
2192 $sql .= " WHERE e.fk_user_author = ".(int) $this->fk_user_author;
2193 $sql .= " AND YEAR(d.date) = '".dol_print_date($this->line->date, '%Y')."' AND MONTH(d.date) = '".dol_print_date($this->line->date, '%m')."'";
2194 if (!empty($this->line->id)) {
2195 $sql .= ' AND d.rowid <> '.((int) $this->line->id);
2196 }
2197
2198 dol_syslog(get_class($this)."::offsetAlreadyGiven");
2199 $resql = $this->db->query($sql);
2200 if ($resql) {
2201 $num = $this->db->num_rows($resql);
2202 if ($num > 0) {
2203 return true;
2204 }
2205 } else {
2206 dol_print_error($this->db);
2207 }
2208
2209 return false;
2210 }
2211
2229 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)
2230 {
2231 global $user, $mysoc;
2232
2233 if ($this->status == self::STATUS_DRAFT || $this->status == self::STATUS_REFUSED) {
2234 $this->db->begin();
2235
2236 $error = 0;
2237 $type = 0; // TODO What if type is service ?
2238
2239 // We don't know seller and buyer for expense reports
2240 $seller = $mysoc; // We use same than current company (expense report are often done in same country)
2241 $seller->tva_assuj = 1; // Most seller uses vat
2242 $seller->localtax1_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
2243 $seller->localtax2_assuj = $mysoc->localtax1_assuj; // We don't know, we reuse the state of company
2244 $buyer = new Societe($this->db);
2245
2246 $localtaxes_type = getLocalTaxesFromRate($vatrate, 0, $buyer, $seller);
2247
2248 // Clean vat code
2249 $reg = array();
2250 $vat_src_code = '';
2251 if (preg_match('/\‍((.*)\‍)/', (string) $vatrate, $reg)) {
2252 $vat_src_code = $reg[1];
2253 $vatrate = preg_replace('/\s*\‍(.*\‍)/', '', (string) $vatrate); // Remove code into vatrate.
2254 }
2255 $vatrate = preg_replace('/\*/', '', $vatrate);
2256
2257 $tmp = calcul_price_total($qty, $value_unit, 0, (float) price2num($vatrate), -1, -1, 0, 'TTC', 0, $type, $seller, $localtaxes_type);
2258 // calcul total of line
2259 // $total_ttc = price2num($qty*$value_unit, 'MT');
2260
2261 $tx_tva = 1 + (float) $vatrate / 100;
2262
2263 $this->line = new ExpenseReportLine($this->db);
2264 $this->line->comments = $comments;
2265 $this->line->qty = $qty;
2266 $this->line->value_unit = $value_unit;
2267 $this->line->date = $date;
2268
2269 $this->line->fk_expensereport = $expensereport_id;
2270 $this->line->fk_c_type_fees = $type_fees_id;
2271 $this->line->fk_c_exp_tax_cat = $fk_c_exp_tax_cat;
2272 $this->line->fk_projet = $projet_id; // deprecated
2273 $this->line->fk_project = $projet_id;
2274
2275 $this->line->vat_src_code = $vat_src_code;
2276 $this->line->vatrate = price2num($vatrate);
2277 $this->line->localtax1_tx = $localtaxes_type[1];
2278 $this->line->localtax2_tx = $localtaxes_type[3];
2279 $this->line->localtax1_type = $localtaxes_type[0];
2280 $this->line->localtax2_type = $localtaxes_type[2];
2281
2282 $this->line->total_ttc = (float) $tmp[2];
2283 $this->line->total_ht = (float) $tmp[0];
2284 $this->line->total_tva = (float) $tmp[1];
2285 $this->line->total_localtax1 = (float) $tmp[9];
2286 $this->line->total_localtax2 = (float) $tmp[10];
2287
2288 $this->line->fk_ecm_files = $fk_ecm_files;
2289
2290 $this->line->id = ((int) $rowid);
2291
2292 // Select des infos sur le type fees
2293 $sql = "SELECT c.code as code_type_fees, c.label as label_type_fees";
2294 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees as c";
2295 $sql .= " WHERE c.id = ".((int) $type_fees_id);
2296 $resql = $this->db->query($sql);
2297 if ($resql) {
2298 $objp_fees = $this->db->fetch_object($resql);
2299 $this->line->type_fees_code = $objp_fees->code_type_fees;
2300 $this->line->type_fees_libelle = $objp_fees->label_type_fees;
2301 $this->db->free($resql);
2302 }
2303
2304 // Select des information du projet
2305 $sql = "SELECT p.ref as ref_projet, p.title as title_projet";
2306 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2307 $sql .= " WHERE p.rowid = ".((int) $projet_id);
2308 $resql = $this->db->query($sql);
2309 if ($resql) {
2310 $objp_projet = $this->db->fetch_object($resql);
2311 $this->line->projet_ref = $objp_projet->ref_projet;
2312 $this->line->projet_title = $objp_projet->title_projet;
2313 $this->db->free($resql);
2314 }
2315
2316 $this->applyOffset();
2317 $this->checkRules();
2318
2319 $result = $this->line->update($user, $notrigger);
2320 if ($result < 0) {
2321 $error++;
2322 }
2323
2324 if (!$error) {
2325 $this->db->commit();
2326 return 1;
2327 } else {
2328 $this->error = $this->line->error;
2329 $this->errors = $this->line->errors;
2330 $this->db->rollback();
2331 return -2;
2332 }
2333 }
2334
2335 return 0;
2336 }
2337
2346 public function deleteLine($rowid, $fuser = '', $notrigger = 0)
2347 {
2348 $error = 0;
2349
2350 $this->db->begin();
2351
2352 if (!$notrigger) {
2353 // Call triggers
2354 $result = $this->call_trigger('EXPENSE_REPORT_DET_DELETE', $fuser);
2355 if ($result < 0) {
2356 $error++;
2357 }
2358 // End call triggers
2359 }
2360
2361 $sql = ' DELETE FROM '.MAIN_DB_PREFIX.$this->table_element_line;
2362 $sql .= ' WHERE rowid = '.((int) $rowid);
2363
2364 dol_syslog(get_class($this)."::deleteline sql=".$sql);
2365 $result = $this->db->query($sql);
2366
2367 if (!$result || $error > 0) {
2368 $this->error = $this->db->error();
2369 dol_syslog(get_class($this)."::deleteline Error ".$this->error, LOG_ERR);
2370 $this->db->rollback();
2371 return -1;
2372 }
2373
2374 $this->update_price(1);
2375
2376 $this->db->commit();
2377
2378 return 1;
2379 }
2380
2389 public function periodExists($fuser, $date_debut, $date_fin)
2390 {
2391 global $conf;
2392
2393 $sql = "SELECT rowid, date_debut, date_fin";
2394 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
2395 $sql .= " WHERE entity = ".((int) $conf->entity); // not shared, only for the current entity
2396 $sql .= " AND fk_user_author = ".((int) $fuser->id);
2397
2398 dol_syslog(get_class($this)."::periodExists sql=".$sql);
2399 $result = $this->db->query($sql);
2400 if ($result) {
2401 $num_rows = $this->db->num_rows($result);
2402 $i = 0;
2403
2404 if ($num_rows > 0) {
2405 $date_d_form = $date_debut;
2406 $date_f_form = $date_fin;
2407
2408 while ($i < $num_rows) {
2409 $objp = $this->db->fetch_object($result);
2410
2411 $date_d_req = $this->db->jdate($objp->date_debut); // 3
2412 $date_f_req = $this->db->jdate($objp->date_fin); // 4
2413
2414 if (!($date_f_form < $date_d_req || $date_d_form > $date_f_req)) {
2415 return $objp->rowid;
2416 }
2417
2418 $i++;
2419 }
2420
2421 return 0;
2422 } else {
2423 return 0;
2424 }
2425 } else {
2426 $this->error = $this->db->lasterror();
2427 dol_syslog(get_class($this)."::periodExists Error ".$this->error, LOG_ERR);
2428 return -1;
2429 }
2430 }
2431
2432
2433 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2441 {
2442 // phpcs:enable
2443 $users_validator = array();
2444
2445 $sql = "SELECT DISTINCT ur.fk_user";
2446 $sql .= " FROM ".MAIN_DB_PREFIX."user_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd";
2447 $sql .= " WHERE ur.fk_id = rd.id and rd.module = 'expensereport' AND rd.perms = 'approve'"; // Permission 'Approve';
2448 $sql .= " UNION";
2449 $sql .= " SELECT DISTINCT ugu.fk_user";
2450 $sql .= " FROM ".MAIN_DB_PREFIX."usergroup_user as ugu, ".MAIN_DB_PREFIX."usergroup_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd";
2451 $sql .= " WHERE ugu.fk_usergroup = ur.fk_usergroup AND ur.fk_id = rd.id and rd.module = 'expensereport' AND rd.perms = 'approve'"; // Permission 'Approve';
2452 //print $sql;
2453
2454 dol_syslog(get_class($this)."::fetch_users_approver_expensereport sql=".$sql);
2455 $result = $this->db->query($sql);
2456 if ($result) {
2457 $num_rows = $this->db->num_rows($result);
2458 $i = 0;
2459 while ($i < $num_rows) {
2460 $objp = $this->db->fetch_object($result);
2461 array_push($users_validator, $objp->fk_user);
2462 $i++;
2463 }
2464 return $users_validator;
2465 } else {
2466 $this->error = $this->db->lasterror();
2467 dol_syslog(get_class($this)."::fetch_users_approver_expensereport Error ".$this->error, LOG_ERR);
2468 return -1;
2469 }
2470 }
2471
2483 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
2484 {
2485 $outputlangs->load("trips");
2486
2487 if (!dol_strlen($modele)) {
2488 if (!empty($this->model_pdf)) {
2489 $modele = $this->model_pdf;
2490 } elseif (getDolGlobalString('EXPENSEREPORT_ADDON_PDF')) {
2491 $modele = getDolGlobalString('EXPENSEREPORT_ADDON_PDF');
2492 }
2493 }
2494
2495 if (!empty($modele)) {
2496 $modelpath = "core/modules/expensereport/doc/";
2497
2498 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2499 } else {
2500 return 0;
2501 }
2502 }
2503
2510 public function listOfTypes($active = 1)
2511 {
2512 global $langs;
2513 $ret = array();
2514 $sql = "SELECT id, code, label";
2515 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees";
2516 $sql .= " WHERE active = ".((int) $active);
2517 dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
2518 $result = $this->db->query($sql);
2519 if ($result) {
2520 $num = $this->db->num_rows($result);
2521 $i = 0;
2522 while ($i < $num) {
2523 $obj = $this->db->fetch_object($result);
2524 $ret[$obj->code] = (($langs->transnoentitiesnoconv($obj->code) != $obj->code) ? $langs->transnoentitiesnoconv($obj->code) : $obj->label);
2525 $i++;
2526 }
2527 } else {
2528 dol_print_error($this->db);
2529 }
2530 return $ret;
2531 }
2532
2538 public function loadStateBoard()
2539 {
2540 global $user;
2541
2542 $this->nb = array();
2543
2544 $sql = "SELECT count(ex.rowid) as nb";
2545 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as ex";
2546 $sql .= " WHERE ex.fk_statut > 0";
2547 $sql .= " AND ex.entity IN (".getEntity('expensereport').")";
2548 if (!$user->hasRight('expensereport', 'readall')) {
2549 $userchildids = $user->getAllChildIds(1);
2550 $sql .= " AND (ex.fk_user_author IN (".$this->db->sanitize(implode(',', $userchildids)).")";
2551 $sql .= " OR ex.fk_user_validator IN (".$this->db->sanitize(implode(',', $userchildids))."))";
2552 }
2553
2554 $resql = $this->db->query($sql);
2555 if ($resql) {
2556 while ($obj = $this->db->fetch_object($resql)) {
2557 $this->nb["expensereports"] = $obj->nb;
2558 }
2559 $this->db->free($resql);
2560 return 1;
2561 } else {
2562 dol_print_error($this->db);
2563 $this->error = $this->db->error();
2564 return -1;
2565 }
2566 }
2567
2568 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2576 public function load_board($user, $option = 'topay')
2577 {
2578 // phpcs:enable
2579 global $conf, $langs;
2580
2581 if ($user->socid) {
2582 return -1; // protection pour eviter appel par utilisateur externe
2583 }
2584
2585 $now = dol_now();
2586
2587 $sql = "SELECT ex.rowid, ex.date_valid";
2588 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as ex";
2589 if ($option == 'toapprove') {
2590 $sql .= " WHERE ex.fk_statut = ".self::STATUS_VALIDATED;
2591 } else {
2592 $sql .= " WHERE ex.fk_statut = ".self::STATUS_APPROVED;
2593 }
2594 $sql .= " AND ex.entity IN (".getEntity('expensereport').")";
2595 if (!$user->hasRight('expensereport', 'readall')) {
2596 $userchildids = $user->getAllChildIds(1);
2597 $sql .= " AND (ex.fk_user_author IN (".$this->db->sanitize(implode(',', $userchildids)).")";
2598 $sql .= " OR ex.fk_user_validator IN (".$this->db->sanitize(implode(',', $userchildids))."))";
2599 }
2600
2601 $resql = $this->db->query($sql);
2602 if ($resql) {
2603 $langs->load("trips");
2604
2605 $response = new WorkboardResponse();
2606 if ($option == 'toapprove') {
2607 $response->warning_delay = $conf->expensereport->approve->warning_delay / 60 / 60 / 24;
2608 $response->label = $langs->trans("ExpenseReportsToApprove");
2609 $response->labelShort = $langs->trans("ToApprove");
2610 $response->url = DOL_URL_ROOT.'/expensereport/list.php?mainmenu=hrm&amp;statut='.self::STATUS_VALIDATED;
2611 } else {
2612 $response->warning_delay = $conf->expensereport->payment->warning_delay / 60 / 60 / 24;
2613 $response->label = $langs->trans("ExpenseReportsToPay");
2614 $response->labelShort = $langs->trans("StatusToPay");
2615 $response->url = DOL_URL_ROOT.'/expensereport/list.php?mainmenu=hrm&amp;statut='.self::STATUS_APPROVED;
2616 }
2617 $response->img = img_object('', "trip");
2618
2619 while ($obj = $this->db->fetch_object($resql)) {
2620 $response->nbtodo++;
2621
2622 if ($option == 'toapprove') {
2623 if ($this->db->jdate($obj->date_valid) < ($now - $conf->expensereport->approve->warning_delay)) {
2624 $response->nbtodolate++;
2625 }
2626 } else {
2627 if ($this->db->jdate($obj->date_valid) < ($now - $conf->expensereport->payment->warning_delay)) {
2628 $response->nbtodolate++;
2629 }
2630 }
2631 }
2632
2633 return $response;
2634 } else {
2635 dol_print_error($this->db);
2636 $this->error = $this->db->error();
2637 return -1;
2638 }
2639 }
2640
2647 public function hasDelay($option)
2648 {
2649 global $conf;
2650
2651 // Only valid expenses reports
2652 if ($option == 'toapprove' && $this->status != 2) {
2653 return false;
2654 }
2655 if ($option == 'topay' && $this->status != 5) {
2656 return false;
2657 }
2658
2659 $now = dol_now();
2660 if ($option == 'toapprove') {
2661 return (!empty($this->datevalid) ? $this->datevalid : $this->date_valid) < ($now - $conf->expensereport->approve->warning_delay);
2662 } else {
2663 return (!empty($this->datevalid) ? $this->datevalid : $this->date_valid) < ($now - $conf->expensereport->payment->warning_delay);
2664 }
2665 }
2666
2672 public function getVentilExportCompta()
2673 {
2674 $alreadydispatched = 0;
2675
2676 $type = 'expense_report';
2677
2678 $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);
2679 $resql = $this->db->query($sql);
2680 if ($resql) {
2681 $obj = $this->db->fetch_object($resql);
2682 if ($obj) {
2683 $alreadydispatched = $obj->nb;
2684 }
2685 } else {
2686 $this->error = $this->db->lasterror();
2687 return -1;
2688 }
2689
2690 if ($alreadydispatched) {
2691 return 1;
2692 }
2693 return 0;
2694 }
2695
2701 public function getSumPayments()
2702 {
2703 $table = 'payment_expensereport';
2704 $field = 'fk_expensereport';
2705
2706 $sql = 'SELECT sum(amount) as amount';
2707 $sql .= ' FROM '.MAIN_DB_PREFIX.$table;
2708 $sql .= " WHERE ".$field." = ".((int) $this->id);
2709
2710 dol_syslog(get_class($this)."::getSumPayments", LOG_DEBUG);
2711 $resql = $this->db->query($sql);
2712 if ($resql) {
2713 $obj = $this->db->fetch_object($resql);
2714 $this->db->free($resql);
2715 return (empty($obj->amount) ? 0 : $obj->amount);
2716 } else {
2717 $this->error = $this->db->lasterror();
2718 return -1;
2719 }
2720 }
2721
2730 public function computeTotalKm($fk_cat, $qty, $tva)
2731 {
2732 global $langs, $db, $conf;
2733
2734 $cumulYearQty = 0;
2735 $ranges = array();
2736 $coef = 0;
2737
2738
2739 if ($fk_cat < 0) {
2740 $this->error = $langs->trans('ErrorBadParameterCat');
2741 return -1;
2742 }
2743
2744 if ($qty <= 0) {
2745 $this->error = $langs->trans('ErrorBadParameterQty');
2746 return -1;
2747 }
2748
2749 $currentUser = new User($db);
2750 $currentUser->fetch($this->fk_user);
2751 $currentUser->loadRights('expensereport');
2752 //Clean
2753 $qty = (float) price2num($qty);
2754
2755 $sql = " SELECT r.range_ik, t.ikoffset, t.coef";
2756 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_ik t";
2757 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_exp_tax_range r ON r.rowid = t.fk_range";
2758 $sql .= " WHERE t.fk_c_exp_tax_cat = ".(int) $fk_cat;
2759 $sql .= " ORDER BY r.range_ik ASC";
2760
2761 dol_syslog("expenseReport::computeTotalkm sql=".$sql, LOG_DEBUG);
2762
2763 $result = $this->db->query($sql);
2764
2765 if ($result) {
2766 if ($conf->global->EXPENSEREPORT_CALCULATE_MILEAGE_EXPENSE_COEFFICIENT_ON_CURRENT_YEAR) {
2767 $arrayDate = dol_getdate(dol_now());
2768 $sql = " SELECT count(n.qty) as cumul FROM ".MAIN_DB_PREFIX."expensereport_det n";
2769 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport e ON e.rowid = n.fk_expensereport";
2770 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_fees tf ON tf.id = n.fk_c_type_fees";
2771 $sql .= " WHERE e.fk_user_author = ".(int) $this->fk_user_author;
2772 $sql .= " AND YEAR(n.date) = ".(int) $arrayDate['year'];
2773 $sql .= " AND tf.code = 'EX_KME' ";
2774 $sql .= " AND e.fk_statut = ".(int) ExpenseReport::STATUS_VALIDATED;
2775
2776 $resql = $this->db->query($sql);
2777
2778 if ($resql) {
2779 $obj = $this->db->fetch_object($resql);
2780 $cumulYearQty = $obj->cumul;
2781 }
2782
2783 $qty += (float) $cumulYearQty;
2784 }
2785
2786 $num = $this->db->num_rows($result);
2787
2788 if ($num) {
2789 for ($i = 0; $i < $num; $i++) {
2790 $obj = $this->db->fetch_object($result);
2791
2792 $ranges[$i] = $obj;
2793 }
2794 '@phan-var-force Object[] $ranges';
2795
2796 for ($i = 0; $i < $num; $i++) {
2797 if ($i < ($num - 1)) {
2798 // @phan-suppress-next-line PhanTypeInvalidDimOffset
2799 if ($qty > $ranges[$i]->range_ik && $qty < $ranges[$i + 1]->range_ik) {
2800 $coef = $ranges[$i]->coef;
2801 $offset = $ranges[$i]->ikoffset;
2802 }
2803 } else {
2804 if ($qty > $ranges[$i]->range_ik) {
2805 $coef = $ranges[$i]->coef;
2806 $offset = $ranges[$i]->ikoffset;
2807 }
2808 }
2809 }
2810 $total_ht = $coef;
2811 return $total_ht;
2812 } else {
2813 $this->error = $langs->trans('TaxUndefinedForThisCategory');
2814 return 0;
2815 }
2816 } else {
2817 $this->error = $this->db->error()." sql=".$sql;
2818
2819 return -1;
2820 }
2821 }
2822
2830 public function getKanbanView($option = '', $arraydata = null)
2831 {
2832 global $langs;
2833
2834 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2835
2836 $return = '<div class="box-flex-item box-flex-grow-zero">';
2837 $return .= '<div class="info-box info-box-sm">';
2838 $return .= '<span class="info-box-icon bg-infobox-action">';
2839 $return .= img_picto('', $this->picto);
2840 $return .= '</span>';
2841 $return .= '<div class="info-box-content">';
2842 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
2843 if ($selected >= 0) {
2844 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2845 }
2846 if (array_key_exists('userauthor', $arraydata) && $arraydata['userauthor'] instanceof User) {
2847 $return .= '<br><span class="info-box-label">'.$arraydata['userauthor']->getNomUrl(-1).'</span>';
2848 }
2849 if (property_exists($this, 'date_debut') && property_exists($this, 'date_fin')) {
2850 $return .= '<br><span class="info-box-label">'.dol_print_date($this->date_debut, 'day').'</span>';
2851 $return .= ' <span class="opacitymedium">'.$langs->trans("To").'</span> ';
2852 $return .= '<span class="info-box-label">'.dol_print_date($this->date_fin, 'day').'</span>';
2853 }
2854 if (method_exists($this, 'getLibStatut')) {
2855 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2856 }
2857 $return .= '</div>';
2858 $return .= '</div>';
2859 $return .= '</div>';
2860 return $return;
2861 }
2862}
$object ref
Definition info.php:89
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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...
deleteEcmFiles($mode=0)
Delete related files of object in database.
update_price($exclspec=0, $roundingadjust='auto', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
setErrorsFromObject($object)
setErrorsFromObject
deleteExtraFields()
Delete all extra fields values for the current object.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Trips and Expenses.
setPaid($id, $fuser, $notrigger=0)
Classify the expense report as paid.
loadStateBoard()
Load the indicators this->nb for the state board.
__construct($db)
Constructor.
checkRules($type=0, $seller='')
Check constraint of rules and update price if needed.
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.
getNextNumRef()
Return next reference of expense report not already used.
createFromClone(User $user, $fk_user_author)
Load an object from its id and create a new one in database.
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.
const STATUS_DRAFT
Draft status.
computeTotalKm($fk_cat, $qty, $tva)
Compute the cost of the kilometers expense based on the number of kilometers and the vehicle category...
periodExists($fuser, $date_debut, $date_fin)
periodExists
offsetAlreadyGiven()
If the sql find any rows then the ikoffset is already given (ikoffset is applied at the first expense...
listOfTypes($active=1)
List of types.
const STATUS_APPROVED
Classified approved.
set_save_from_refuse($fuser)
set_save_from_refuse
setValidate($fuser, $notrigger=0)
Set to status validate.
getSumPayments()
Return amount of payments already done.
getLibStatut($mode=0)
Returns the label status.
set_cancel($fuser, $detail, $notrigger=0)
set_cancel
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $moretitle='', $notooltip=0, $save_lastsearch_value=-1)
Return clickable name (with picto eventually)
set_unpaid($fuser, $notrigger=0)
set_unpaid
info($id)
Load information on object.
getTooltipContentArray($params)
getTooltipContentArray
hasDelay($option)
Return if an expense report is late or not.
applyOffset($type=0, $seller='')
Method to apply the offset if needed.
const STATUS_CANCELED
Classified canceled.
const STATUS_CLOSED
Classified paid.
const STATUS_REFUSED
Classified refused.
deleteLine($rowid, $fuser='', $notrigger=0)
deleteline
setDeny($fuser, $details, $notrigger=0)
setDeny
getVentilExportCompta()
Return if object was dispatched into bookkeeping.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with optional picto)
update($user, $notrigger=0, $userofexpensereport=null)
update
const STATUS_VALIDATED
Validated (need to be paid)
create($user, $notrigger=0)
Create object in database.
load_board($user, $option='topay')
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
set_paid($id, $fuser, $notrigger=0)
Classify the expense report as paid.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
initAsSpecimen()
Initialise an instance with random values.
fetch_users_approver_expensereport()
Return list of people with permission to validate expense reports.
setApproved($fuser, $notrigger=0)
Set status to approved.
LibStatut($status, $mode=0)
Returns the label of a status.
setUnpaid($fuser, $notrigger=0)
set_unpaid
fetch_line_by_project($projectid, $user)
fetch_line_by_project
update_totaux_add($ligne_total_ht, $ligne_total_tva)
Update total of an expense report when you add a line.
Class to manage inventories.
Class of expense report details lines.
Class to manage inventories.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:171
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.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:63
dol_delete_preview($object)
Delete all preview files linked to object instance.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
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.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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:90