28 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
30 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport_ik.class.php';
31 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport_rule.class.php';
41 public $element =
'expensereport';
46 public $table_element =
'expensereport';
51 public $table_element_line =
'expensereport_det';
56 public $fk_element =
'fk_expensereport';
61 public $picto =
'trip';
66 public $lines = array();
92 public $fk_c_paiement;
95 public $user_author_infos;
96 public $user_validator_infos;
98 public $rule_warning_message;
108 public $fk_user_creat;
113 public $fk_user_author;
117 public $fk_user_modif;
121 public $detail_refuse;
122 public $fk_user_refuse;
126 public $detail_cancel;
131 public $fk_user_cancel;
136 public $fk_user_validator;
155 public $fk_user_valid;
156 public $user_valid_infos;
159 public $date_approve;
160 public $fk_user_approve;
163 public $user_paid_infos;
168 public $statuts = array();
169 public $statuts_short = array();
170 public $statuts_logo;
176 public $fk_multicurrency;
181 public $multicurrency_code;
182 public $multicurrency_tx;
183 public $multicurrency_total_ht;
184 public $multicurrency_total_tva;
185 public $multicurrency_total_ttc;
218 public $fields = array(
219 'rowid' =>array(
'type'=>
'integer',
'label'=>
'ID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
220 'ref' =>array(
'type'=>
'varchar(50)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'showoncombobox'=>1,
'position'=>15),
221 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>20),
222 'ref_number_int' =>array(
'type'=>
'integer',
'label'=>
'Ref number int',
'enabled'=>1,
'visible'=>-1,
'position'=>25),
223 'ref_ext' =>array(
'type'=>
'integer',
'label'=>
'Ref ext',
'enabled'=>1,
'visible'=>-1,
'position'=>30),
224 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'Total ht',
'enabled'=>1,
'visible'=>-1,
'position'=>35),
225 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'Total tva',
'enabled'=>1,
'visible'=>-1,
'position'=>40),
226 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax1',
'enabled'=>1,
'visible'=>-1,
'position'=>45),
227 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax2',
'enabled'=>1,
'visible'=>-1,
'position'=>50),
228 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'Total ttc',
'enabled'=>1,
'visible'=>-1,
'position'=>55),
229 'date_debut' =>array(
'type'=>
'date',
'label'=>
'Date debut',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>60),
230 'date_fin' =>array(
'type'=>
'date',
'label'=>
'Date fin',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>65),
231 'date_valid' =>array(
'type'=>
'datetime',
'label'=>
'Date valid',
'enabled'=>1,
'visible'=>-1,
'position'=>75),
232 'date_approve' =>array(
'type'=>
'datetime',
'label'=>
'Date approve',
'enabled'=>1,
'visible'=>-1,
'position'=>80),
233 'date_refuse' =>array(
'type'=>
'datetime',
'label'=>
'Date refuse',
'enabled'=>1,
'visible'=>-1,
'position'=>85),
234 'date_cancel' =>array(
'type'=>
'datetime',
'label'=>
'Date cancel',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
235 'fk_user_author' =>array(
'type'=>
'integer',
'label'=>
'Fk user author',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>100),
236 'fk_user_modif' =>array(
'type'=>
'integer',
'label'=>
'Fk user modif',
'enabled'=>1,
'visible'=>-1,
'position'=>105),
237 'fk_user_valid' =>array(
'type'=>
'integer',
'label'=>
'Fk user valid',
'enabled'=>1,
'visible'=>-1,
'position'=>110),
238 'fk_user_validator' =>array(
'type'=>
'integer',
'label'=>
'Fk user validator',
'enabled'=>1,
'visible'=>-1,
'position'=>115),
239 'fk_user_approve' =>array(
'type'=>
'integer',
'label'=>
'Fk user approve',
'enabled'=>1,
'visible'=>-1,
'position'=>120),
240 'fk_user_refuse' =>array(
'type'=>
'integer',
'label'=>
'Fk user refuse',
'enabled'=>1,
'visible'=>-1,
'position'=>125),
241 'fk_user_cancel' =>array(
'type'=>
'integer',
'label'=>
'Fk user cancel',
'enabled'=>1,
'visible'=>-1,
'position'=>130),
242 'fk_c_paiement' =>array(
'type'=>
'integer',
'label'=>
'Fk c paiement',
'enabled'=>1,
'visible'=>-1,
'position'=>140),
243 'paid' =>array(
'type'=>
'integer',
'label'=>
'Paid',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>145),
244 'note_public' =>array(
'type'=>
'html',
'label'=>
'Note public',
'enabled'=>1,
'visible'=>0,
'position'=>150),
245 'note_private' =>array(
'type'=>
'html',
'label'=>
'Note private',
'enabled'=>1,
'visible'=>0,
'position'=>155),
246 'detail_refuse' =>array(
'type'=>
'varchar(255)',
'label'=>
'Detail refuse',
'enabled'=>1,
'visible'=>-1,
'position'=>160),
247 'detail_cancel' =>array(
'type'=>
'varchar(255)',
'label'=>
'Detail cancel',
'enabled'=>1,
'visible'=>-1,
'position'=>165),
248 'integration_compta' =>array(
'type'=>
'integer',
'label'=>
'Integration compta',
'enabled'=>1,
'visible'=>-1,
'position'=>170),
249 'fk_bank_account' =>array(
'type'=>
'integer',
'label'=>
'Fk bank account',
'enabled'=>1,
'visible'=>-1,
'position'=>175),
250 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
251 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'Multicurrency code',
'enabled'=>1,
'visible'=>-1,
'position'=>190),
252 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency tx',
'enabled'=>1,
'visible'=>-1,
'position'=>195),
253 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency total ht',
'enabled'=>1,
'visible'=>-1,
'position'=>200),
254 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency total tva',
'enabled'=>1,
'visible'=>-1,
'position'=>205),
255 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency total ttc',
'enabled'=>1,
'visible'=>-1,
'position'=>210),
256 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>220),
257 'date_create' =>array(
'type'=>
'datetime',
'label'=>
'Date create',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>300),
258 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'Tms',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>305),
259 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-1,
'position'=>1000),
260 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Model pdf',
'enabled'=>1,
'visible'=>0,
'position'=>1010),
261 'fk_statut' =>array(
'type'=>
'integer',
'label'=>
'Fk statut',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>500),
273 $this->total_ttc = 0;
274 $this->total_tva = 0;
275 $this->total_localtax1 = 0;
276 $this->total_localtax2 = 0;
277 $this->localtax1 = 0;
278 $this->localtax2 = 0;
279 $this->modepaymentid = 0;
282 $this->statuts_short = array(0 =>
'Draft', 2 =>
'Validated', 4 =>
'Canceled', 5 =>
'Approved', 6 =>
'Paid', 99 =>
'Refused');
283 $this->statuts = array(0 =>
'Draft', 2 =>
'ValidatedWaitingApproval', 4 =>
'Canceled', 5 =>
'Approved', 6 =>
'Paid', 99 =>
'Refused');
284 $this->statuts_logo = array(0 =>
'status0', 2 =>
'status1', 4 =>
'status6', 5 =>
'status4', 6 =>
'status6', 99 =>
'status5');
294 public function create($user, $notrigger = 0)
296 global $conf, $langs;
303 if (empty($this->date_debut) || empty($this->date_fin)) {
304 $this->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
'Date'));
308 $fuserid = $this->fk_user_author;
309 if (empty($fuserid)) {
310 $fuserid = $user->id;
315 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (";
318 $sql .=
",total_ttc";
319 $sql .=
",total_tva";
320 $sql .=
",date_debut";
322 $sql .=
",date_create";
323 $sql .=
",fk_user_creat";
324 $sql .=
",fk_user_author";
325 $sql .=
",fk_user_validator";
326 $sql .=
",fk_user_approve";
327 $sql .=
",fk_user_modif";
328 $sql .=
",fk_statut";
329 $sql .=
",fk_c_paiement";
331 $sql .=
",note_public";
332 $sql .=
",note_private";
336 $sql .=
", ".price2num($this->total_ht,
'MT');
337 $sql .=
", ".price2num($this->total_ttc,
'MT');
338 $sql .=
", ".price2num($this->total_tva,
'MT');
339 $sql .=
", '".$this->db->idate($this->date_debut).
"'";
340 $sql .=
", '".$this->db->idate($this->date_fin).
"'";
341 $sql .=
", '".$this->db->idate($now).
"'";
342 $sql .=
", ".((int) $user->id);
343 $sql .=
", ".((int) $fuserid);
344 $sql .=
", ".($this->fk_user_validator > 0 ? ((int) $this->fk_user_validator) :
"null");
345 $sql .=
", ".($this->fk_user_approve > 0 ? ((int) $this->fk_user_approve) :
"null");
346 $sql .=
", ".($this->fk_user_modif > 0 ? ((int) $this->fk_user_modif) :
"null");
347 $sql .=
", ".($this->fk_statut > 1 ? ((int) $this->fk_statut) : 0);
348 $sql .=
", ".($this->modepaymentid ? ((int) $this->modepaymentid) :
"null");
350 $sql .=
", ".($this->note_public ?
"'".$this->db->escape($this->note_public).
"'" :
"null");
351 $sql .=
", ".($this->note_private ?
"'".$this->db->escape($this->note_private).
"'" :
"null");
352 $sql .=
", ".((int) $conf->entity);
355 $result = $this->db->query(
$sql);
357 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
358 $this->
ref =
'(PROV'.$this->id.
')';
360 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
" SET ref='".$this->db->escape($this->
ref).
"' WHERE rowid=".((int) $this->
id);
361 $resql = $this->db->query(
$sql);
363 $this->error = $this->db->lasterror();
368 if (is_array($this->lines) && count($this->lines) > 0) {
369 foreach ($this->lines as $line) {
372 if (!is_object($line)) {
373 $line = (object) $line;
375 $newndfline->fk_expensereport = $line->fk_expensereport;
376 $newndfline->fk_c_type_fees = $line->fk_c_type_fees;
377 $newndfline->fk_project = $line->fk_project;
378 $newndfline->vatrate = $line->vatrate;
379 $newndfline->vat_src_code = $line->vat_src_code;
380 $newndfline->localtax1_tx = $line->localtax1_tx;
381 $newndfline->localtax2_tx = $line->localtax2_tx;
382 $newndfline->localtax1_type = $line->localtax1_type;
383 $newndfline->localtax2_type = $line->localtax2_type;
384 $newndfline->comments = $line->comments;
385 $newndfline->qty = $line->qty;
386 $newndfline->value_unit = $line->value_unit;
387 $newndfline->total_ht = $line->total_ht;
388 $newndfline->total_ttc = $line->total_ttc;
389 $newndfline->total_tva = $line->total_tva;
390 $newndfline->total_localtax1 = $line->total_localtax1;
391 $newndfline->total_localtax2 = $line->total_localtax2;
392 $newndfline->date = $line->date;
393 $newndfline->rule_warning_message = $line->rule_warning_message;
394 $newndfline->fk_c_exp_tax_cat = $line->fk_c_exp_tax_cat;
395 $newndfline->fk_ecm_files = $line->fk_ecm_files;
400 $newndfline->fk_expensereport = $this->id;
401 $result = $newndfline->insert();
403 $this->error = $newndfline->error;
404 $this->errors = $newndfline->errors;
424 $result = $this->
call_trigger(
'EXPENSE_REPORT_CREATE', $user);
436 $this->db->rollback();
440 $this->db->rollback();
444 dol_syslog(get_class($this).
"::create error ".$this->error, LOG_ERR);
445 $this->db->rollback();
449 $this->error = $this->db->lasterror().
" sql=".
$sql;
450 $this->db->rollback();
468 if (empty($fk_user_author)) {
469 $fk_user_author = $user->id;
479 $objFrom = clone $this;
484 $this->fk_statut = 0;
487 $this->fk_user_creat = $user->id;
488 $this->fk_user_author = $fk_user_author;
489 $this->fk_user_valid =
'';
490 $this->date_create =
'';
491 $this->date_creation =
'';
492 $this->date_validation =
'';
495 if (is_array($this->lines) && count($this->lines) > 0) {
496 foreach ($this->lines as $key => $line) {
497 $this->lines[$key]->fk_ecm_files = 0;
502 $this->context[
'createfromclone'] =
'createfromclone';
503 $result = $this->
create($user);
510 if (is_object($hookmanager)) {
511 $parameters = array(
'objFrom'=>$objFrom);
513 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
521 unset($this->context[
'createfromclone']);
528 $this->db->rollback();
542 public function update($user, $notrigger = 0, $userofexpensereport =
null)
549 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
550 $sql .=
" total_ht = ".$this->total_ht;
551 $sql .=
" , total_ttc = ".$this->total_ttc;
552 $sql .=
" , total_tva = ".$this->total_tva;
553 $sql .=
" , date_debut = '".$this->db->idate($this->date_debut).
"'";
554 $sql .=
" , date_fin = '".$this->db->idate($this->date_fin).
"'";
555 if ($userofexpensereport && is_object($userofexpensereport)) {
556 $sql .=
" , fk_user_author = ".($userofexpensereport->id > 0 ? $userofexpensereport->id :
"null");
558 $sql .=
" , fk_user_validator = ".($this->fk_user_validator > 0 ? $this->fk_user_validator :
"null");
559 $sql .=
" , fk_user_valid = ".($this->fk_user_valid > 0 ? $this->fk_user_valid :
"null");
560 $sql .=
" , fk_user_approve = ".($this->fk_user_approve > 0 ? $this->fk_user_approve :
"null");
561 $sql .=
" , fk_user_modif = ".$user->id;
562 $sql .=
" , fk_statut = ".($this->fk_statut >= 0 ? $this->fk_statut :
'0');
563 $sql .=
" , fk_c_paiement = ".($this->fk_c_paiement > 0 ? $this->fk_c_paiement :
"null");
564 $sql .=
" , note_public = ".(!empty($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"''");
565 $sql .=
" , note_private = ".(!empty($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"''");
566 $sql .=
" , detail_refuse = ".(!empty($this->detail_refuse) ?
"'".$this->db->escape($this->detail_refuse).
"'" :
"''");
567 $sql .=
" WHERE rowid = ".((int) $this->
id);
569 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
570 $result = $this->db->query(
$sql);
574 $result = $this->
call_trigger(
'EXPENSE_REPORT_MODIFY', $user);
586 $this->db->rollback();
587 $this->error = $this->db->error();
591 $this->db->rollback();
592 $this->error = $this->db->error();
604 public function fetch($id, $ref =
'')
608 $sql =
"SELECT d.rowid, d.entity, d.ref, d.note_public, d.note_private,";
609 $sql .=
" d.detail_refuse, d.detail_cancel, d.fk_user_refuse, d.fk_user_cancel,";
610 $sql .=
" d.date_refuse, d.date_cancel,";
611 $sql .=
" d.total_ht, d.total_ttc, d.total_tva,";
612 $sql .=
" d.localtax1 as total_localtax1, d.localtax2 as total_localtax2,";
613 $sql .=
" d.date_debut, d.date_fin, d.date_create, d.tms as date_modif, d.date_valid, d.date_approve,";
614 $sql .=
" d.fk_user_creat, d.fk_user_author, d.fk_user_modif, d.fk_user_validator,";
615 $sql .=
" d.fk_user_valid, d.fk_user_approve,";
616 $sql .=
" d.fk_statut as status, d.fk_c_paiement, d.paid";
617 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as d";
619 $sql .=
" WHERE d.ref = '".$this->db->escape($ref).
"'";
621 $sql .=
" WHERE d.rowid = ".((int) $id);
625 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
626 $resql = $this->db->query(
$sql);
628 $obj = $this->db->fetch_object($resql);
630 $this->
id = $obj->rowid;
631 $this->
ref = $obj->ref;
633 $this->entity = $obj->entity;
635 $this->total_ht = $obj->total_ht;
636 $this->total_tva = $obj->total_tva;
637 $this->total_ttc = $obj->total_ttc;
638 $this->localtax1 = $obj->total_localtax1;
639 $this->localtax2 = $obj->total_localtax2;
640 $this->total_localtax1 = $obj->total_localtax1;
641 $this->total_localtax2 = $obj->total_localtax2;
643 $this->note_public = $obj->note_public;
644 $this->note_private = $obj->note_private;
645 $this->detail_refuse = $obj->detail_refuse;
646 $this->detail_cancel = $obj->detail_cancel;
648 $this->date_debut = $this->db->jdate($obj->date_debut);
649 $this->date_fin = $this->db->jdate($obj->date_fin);
650 $this->date_valid = $this->db->jdate($obj->date_valid);
651 $this->date_approve = $this->db->jdate($obj->date_approve);
652 $this->date_create = $this->db->jdate($obj->date_create);
653 $this->date_modif = $this->db->jdate($obj->date_modif);
654 $this->date_refuse = $this->db->jdate($obj->date_refuse);
655 $this->date_cancel = $this->db->jdate($obj->date_cancel);
657 $this->fk_user_creat = $obj->fk_user_creat;
658 $this->fk_user_author = $obj->fk_user_author;
659 $this->fk_user_modif = $obj->fk_user_modif;
660 $this->fk_user_validator = $obj->fk_user_validator;
661 $this->fk_user_valid = $obj->fk_user_valid;
662 $this->fk_user_refuse = $obj->fk_user_refuse;
663 $this->fk_user_cancel = $obj->fk_user_cancel;
664 $this->fk_user_approve = $obj->fk_user_approve;
666 $user_author =
new User($this->db);
667 if ($this->fk_user_author > 0) {
668 $user_author->fetch($this->fk_user_author);
671 $this->user_author_infos =
dolGetFirstLastname($user_author->firstname, $user_author->lastname);
673 $user_approver =
new User($this->db);
674 if ($this->fk_user_approve > 0) {
675 $user_approver->fetch($this->fk_user_approve);
676 } elseif ($this->fk_user_validator > 0) {
677 $user_approver->fetch($this->fk_user_validator);
679 $this->user_validator_infos =
dolGetFirstLastname($user_approver->firstname, $user_approver->lastname);
681 $this->fk_statut = $obj->status;
682 $this->status = $obj->status;
683 $this->fk_c_paiement = $obj->fk_c_paiement;
684 $this->paid = $obj->paid;
686 if ($this->status == self::STATUS_APPROVED || $this->status == self::STATUS_CLOSED) {
687 $user_valid =
new User($this->db);
688 if ($this->fk_user_valid > 0) {
689 $user_valid->fetch($this->fk_user_valid);
691 $this->user_valid_infos =
dolGetFirstLastname($user_valid->firstname, $user_valid->lastname);
703 $this->error = $this->db->lasterror();
719 public function set_paid($id, $fuser, $notrigger = 0)
722 dol_syslog(get_class($this).
"::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
723 return $this->
setPaid($id, $fuser, $notrigger);
734 public function setPaid($id, $fuser, $notrigger = 0)
739 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expensereport";
740 $sql .=
" SET fk_statut = ".self::STATUS_CLOSED.
", paid=1";
743 dol_syslog(get_class($this).
"::set_paid", LOG_DEBUG);
744 $resql = $this->db->query(
$sql);
746 if ($this->db->affected_rows($resql)) {
749 $result = $this->
call_trigger(
'EXPENSE_REPORT_PAID', $fuser);
761 $this->db->rollback();
762 $this->error = $this->db->error();
770 $this->db->rollback();
784 return $this->
LibStatut($this->status, $mode);
800 $labelStatus = $langs->transnoentitiesnoconv($this->statuts[$status]);
801 $labelStatusShort = $langs->transnoentitiesnoconv($this->statuts_short[$status]);
803 $statusType = $this->statuts_logo[$status];
805 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
819 $sql =
"SELECT f.rowid,";
820 $sql .=
" f.date_create as datec,";
821 $sql .=
" f.tms as date_modification,";
822 $sql .=
" f.date_valid as datev,";
823 $sql .=
" f.date_approve as datea,";
824 $sql .=
" f.fk_user_creat as fk_user_creation,";
825 $sql .=
" f.fk_user_author as fk_user_author,";
826 $sql .=
" f.fk_user_modif as fk_user_modification,";
827 $sql .=
" f.fk_user_valid,";
828 $sql .=
" f.fk_user_approve";
829 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport as f";
830 $sql .=
" WHERE f.rowid = ".((int) $id);
831 $sql .=
" AND f.entity = ".$conf->entity;
833 $resql = $this->db->query(
$sql);
835 if ($this->db->num_rows($resql)) {
836 $obj = $this->db->fetch_object($resql);
838 $this->
id = $obj->rowid;
840 $this->date_creation = $this->db->jdate($obj->datec);
841 $this->date_modification = $this->db->jdate($obj->date_modification);
842 $this->date_validation = $this->db->jdate($obj->datev);
843 $this->date_approbation = $this->db->jdate($obj->datea);
845 $cuser =
new User($this->db);
846 $cuser->fetch($obj->fk_user_author);
847 $this->user_creation = $cuser;
849 if ($obj->fk_user_creation) {
850 $cuser =
new User($this->db);
851 $cuser->fetch($obj->fk_user_creation);
852 $this->user_creation = $cuser;
854 if ($obj->fk_user_valid) {
855 $vuser =
new User($this->db);
856 $vuser->fetch($obj->fk_user_valid);
857 $this->user_validation = $vuser;
859 if ($obj->fk_user_modification) {
860 $muser =
new User($this->db);
861 $muser->fetch($obj->fk_user_modification);
862 $this->user_modification = $muser;
864 if ($obj->fk_user_approve) {
865 $auser =
new User($this->db);
866 $auser->fetch($obj->fk_user_approve);
867 $this->user_approve = $auser;
870 $this->db->free($resql);
887 global $user, $langs, $conf;
893 $this->
ref =
'SPECIMEN';
896 $this->date_create = $now;
897 $this->date_debut = $now;
898 $this->date_fin = $now;
899 $this->date_valid = $now;
900 $this->date_approve = $now;
905 $this->fk_statut = 5;
907 $this->fk_user_author = $user->id;
908 $this->fk_user_validator = $user->id;
909 $this->fk_user_valid = $user->id;
910 $this->fk_user_approve = $user->id;
912 $this->note_private =
'Private note';
913 $this->note_public =
'SPECIMEN';
916 while ($xnbp < $nbp) {
918 $line->comments = $langs->trans(
"Comment").
" ".$xnbp;
919 $line->date = ($now - 3600 * (1 + $xnbp));
920 $line->total_ht = 100;
921 $line->total_tva = 20;
922 $line->total_ttc = 120;
925 $line->value_unit = 120;
926 $line->fk_expensereport = 0;
927 $line->type_fees_code =
'TRA';
928 $line->fk_c_type_fees = $type_fees_id;
930 $line->projet_ref =
'ABC';
932 $this->lines[$xnbp] = $line;
935 $this->total_ht += $line->total_ht;
936 $this->total_tva += $line->total_tva;
937 $this->total_ttc += $line->total_ttc;
952 global $conf, $db, $langs;
954 $langs->load(
'trips');
956 if ($user->rights->expensereport->lire) {
957 $sql =
"SELECT de.fk_expensereport, de.date, de.comments, de.total_ht, de.total_ttc";
958 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport_det as de";
959 $sql .=
" WHERE de.fk_projet = ".((int) $projectid);
961 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
962 $result = $this->db->query(
$sql);
964 $num = $this->db->num_rows($result);
970 $objp = $this->db->fetch_object($result);
972 $sql2 =
"SELECT d.rowid, d.fk_user_author, d.ref, d.fk_statut as status";
973 $sql2 .=
" FROM ".MAIN_DB_PREFIX.
"expensereport as d";
974 $sql2 .=
" WHERE d.rowid = ".((int) $objp->fk_expensereport);
976 $result2 = $this->db->query($sql2);
977 $obj = $this->db->fetch_object($result2);
979 $objp->fk_user_author = $obj->fk_user_author;
980 $objp->ref = $obj->ref;
981 $objp->fk_c_expensereport_status = $obj->status;
982 $objp->rowid = $obj->rowid;
984 $total_HT = $total_HT + $objp->total_ht;
985 $total_TTC = $total_TTC + $objp->total_ttc;
986 $author =
new User($this->db);
987 $author->fetch($objp->fk_user_author);
990 print
'<td><a href="'.DOL_URL_ROOT.
'/expensereport/card.php?id='.$objp->rowid.
'">'.$objp->ref_num.
'</a></td>';
991 print
'<td class="center">'.dol_print_date($objp->date,
'day').
'</td>';
992 print
'<td>'.$author->getNomUrl(1).
'</td>';
993 print
'<td>'.$objp->comments.
'</td>';
994 print
'<td class="right">'.price($objp->total_ht).
'</td>';
995 print
'<td class="right">'.price($objp->total_ttc).
'</td>';
996 print
'<td class="right">';
998 switch ($objp->fk_c_expensereport_status) {
1000 print
img_picto($langs->trans(
'StatusOrderCanceled'),
'statut5');
1003 print $langs->trans(
'Draft').
' '.
img_picto($langs->trans(
'Draft'),
'statut0');
1006 print $langs->trans(
'TripForValid').
' '.
img_picto($langs->trans(
'TripForValid'),
'statut3');
1009 print $langs->trans(
'TripForPaid').
' '.
img_picto($langs->trans(
'TripForPaid'),
'statut3');
1012 print $langs->trans(
'TripPaid').
' '.
img_picto($langs->trans(
'TripPaid'),
'statut4');
1029 print
'<tr class="liste_total"><td colspan="4">'.$langs->trans(
"Number").
': '.$i.
'</td>';
1030 print
'<td class="right" width="100">'.$langs->trans(
"TotalHT").
' : '.
price($total_HT).
'</td>';
1031 print
'<td class="right" width="100">'.$langs->trans(
"TotalTTC").
' : '.
price($total_TTC).
'</td>';
1032 print
'<td> </td>';
1035 $this->error = $this->db->lasterror();
1054 $this->lines = array();
1056 $sql =
' SELECT de.rowid, de.comments, de.qty, de.value_unit, de.date, de.rang,';
1057 $sql .=
" de.".$this->fk_element.
", de.fk_c_type_fees, de.fk_c_exp_tax_cat, de.fk_projet as fk_project,";
1058 $sql .=
' de.tva_tx, de.vat_src_code,';
1059 $sql .=
' de.localtax1_tx, de.localtax2_tx, de.localtax1_type, de.localtax2_type,';
1060 $sql .=
' de.fk_ecm_files,';
1061 $sql .=
' de.total_ht, de.total_tva, de.total_ttc,';
1062 $sql .=
' de.total_localtax1, de.total_localtax2, de.rule_warning_message,';
1063 $sql .=
' ctf.code as code_type_fees, ctf.label as libelle_type_fees, ctf.accountancy_code as accountancy_code_type_fees,';
1064 $sql .=
' p.ref as ref_projet, p.title as title_projet';
1065 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element_line.
' as de';
1066 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_type_fees as ctf ON de.fk_c_type_fees = ctf.id';
1067 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'projet as p ON de.fk_projet = p.rowid';
1068 $sql .=
" WHERE de.".$this->fk_element.
" = ".((int) $this->
id);
1069 if (!empty($conf->global->EXPENSEREPORT_LINES_SORTED_BY_ROWID)) {
1070 $sql .=
' ORDER BY de.rang ASC, de.rowid ASC';
1072 $sql .=
' ORDER BY de.rang ASC, de.date ASC';
1075 $resql = $this->db->query(
$sql);
1077 $num = $this->db->num_rows($resql);
1080 $objp = $this->db->fetch_object($resql);
1084 $deplig->rowid = $objp->rowid;
1085 $deplig->id = $objp->rowid;
1086 $deplig->comments = $objp->comments;
1087 $deplig->qty = $objp->qty;
1088 $deplig->value_unit = $objp->value_unit;
1089 $deplig->date = $objp->date;
1090 $deplig->dates = $this->db->jdate($objp->date);
1092 $deplig->fk_expensereport = $objp->fk_expensereport;
1093 $deplig->fk_c_type_fees = $objp->fk_c_type_fees;
1094 $deplig->fk_c_exp_tax_cat = $objp->fk_c_exp_tax_cat;
1095 $deplig->fk_projet = $objp->fk_project;
1096 $deplig->fk_project = $objp->fk_project;
1097 $deplig->fk_ecm_files = $objp->fk_ecm_files;
1099 $deplig->total_ht = $objp->total_ht;
1100 $deplig->total_tva = $objp->total_tva;
1101 $deplig->total_ttc = $objp->total_ttc;
1102 $deplig->total_localtax1 = $objp->total_localtax1;
1103 $deplig->total_localtax2 = $objp->total_localtax2;
1105 $deplig->type_fees_code = empty($objp->code_type_fees) ?
'TF_OTHER' : $objp->code_type_fees;
1106 $deplig->type_fees_libelle = $objp->libelle_type_fees;
1107 $deplig->type_fees_accountancy_code = $objp->accountancy_code_type_fees;
1109 $deplig->tva_tx = $objp->tva_tx;
1110 $deplig->vatrate = $objp->tva_tx;
1111 $deplig->vat_src_code = $objp->vat_src_code;
1112 $deplig->localtax1_tx = $objp->localtax1_tx;
1113 $deplig->localtax2_tx = $objp->localtax2_tx;
1114 $deplig->localtax1_type = $objp->localtax1_type;
1115 $deplig->localtax2_type = $objp->localtax2_type;
1117 $deplig->projet_ref = $objp->ref_projet;
1118 $deplig->projet_title = $objp->title_projet;
1120 $deplig->rule_warning_message = $objp->rule_warning_message;
1122 $deplig->rang = $objp->rang;
1124 $this->lines[$i] = $deplig;
1128 $this->db->free($resql);
1131 $this->error = $this->db->lasterror();
1132 dol_syslog(get_class($this).
"::fetch_lines: Error ".$this->error, LOG_ERR);
1145 public function delete(
User $user =
null, $notrigger =
false)
1148 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1156 $result = $this->
call_trigger(
'EXPENSE_REPORT_DELETE', $user);
1164 if (!$error && !empty($this->table_element_line)) {
1165 $tabletodelete = $this->table_element_line;
1167 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
1168 if (!$this->db->query(
$sql)) {
1170 $this->error = $this->db->lasterror();
1171 $this->errors[] = $this->error;
1172 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
1197 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
1203 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
1204 $res = $this->db->query(
$sql);
1207 $this->error = $this->db->lasterror();
1208 $this->errors[] = $this->error;
1209 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
1224 if ($conf->expensereport->multidir_output[$this->entity] && !empty($this->
ref)) {
1225 $dir = $conf->expensereport->multidir_output[$this->entity].
"/".$ref;
1226 $file = $dir.
"/".$ref.
".pdf";
1227 if (file_exists($file)) {
1231 $this->error =
'ErrorFailToDeleteFile';
1232 $this->errors[] = $this->error;
1233 $this->db->rollback();
1237 if (file_exists($dir)) {
1240 $this->error =
'ErrorFailToDeleteDir';
1241 $this->errors[] = $this->error;
1242 $this->db->rollback();
1250 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
1251 $this->db->commit();
1254 $this->db->rollback();
1268 global $conf, $langs, $user;
1274 if ($this->status == self::STATUS_VALIDATED) {
1275 dol_syslog(get_class($this).
"::valid action abandonned: already validated", LOG_WARNING);
1279 $this->date_valid = $now;
1282 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
1287 if (empty($num) || $num < 0) {
1296 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
1297 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
1298 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
1299 $sql .=
" date_valid='".$this->db->idate($this->date_valid).
"',";
1300 $sql .=
" fk_user_valid = ".$user->id;
1301 $sql .=
" WHERE rowid = ".((int) $this->
id);
1303 $resql = $this->db->query(
$sql);
1305 if (!$error && !$notrigger) {
1307 $result = $this->
call_trigger(
'EXPENSE_REPORT_VALIDATE', $fuser);
1315 $this->oldref = $this->ref;
1318 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
1319 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1322 $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).
"'";
1323 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'expensereport/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
1324 $resql = $this->db->query(
$sql);
1326 $error++; $this->error = $this->db->lasterror();
1332 $dirsource = $conf->expensereport->dir_output.
'/'.$oldref;
1333 $dirdest = $conf->expensereport->dir_output.
'/'.$newref;
1334 if (!$error && file_exists($dirsource)) {
1335 dol_syslog(get_class($this).
"::setValidate() rename dir ".$dirsource.
" into ".$dirdest);
1337 if (@rename($dirsource, $dirdest)) {
1340 $listoffiles =
dol_dir_list($conf->expensereport->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
1341 foreach ($listoffiles as $fileentry) {
1342 $dirsource = $fileentry[
'name'];
1343 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
1344 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
1345 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
1346 @rename($dirsource, $dirdest);
1359 if (empty($error)) {
1360 $this->db->commit();
1363 $this->db->rollback();
1364 $this->error = $this->db->error();
1368 $this->db->rollback();
1369 $this->error = $this->db->lasterror();
1385 $sql =
'SELECT date_debut';
1386 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element;
1387 $sql .=
" WHERE rowid = ".((int) $this->
id);
1389 $result = $this->db->query(
$sql);
1391 $objp = $this->db->fetch_object($result);
1393 $this->date_debut = $this->db->jdate($objp->date_debut);
1395 if ($this->status != self::STATUS_VALIDATED) {
1396 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1397 $sql .=
" SET fk_statut = ".self::STATUS_VALIDATED;
1398 $sql .=
" WHERE rowid = ".((int) $this->
id);
1400 dol_syslog(get_class($this).
"::set_save_from_refuse", LOG_DEBUG);
1402 if ($this->db->query(
$sql)) {
1405 $this->error = $this->db->lasterror();
1409 dol_syslog(get_class($this).
"::set_save_from_refuse expensereport already with save status", LOG_WARNING);
1428 $this->date_approve = $now;
1429 if ($this->status != self::STATUS_APPROVED) {
1432 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1433 $sql .=
" SET ref = '".$this->db->escape($this->
ref).
"', fk_statut = ".self::STATUS_APPROVED.
", fk_user_approve = ".((int) $fuser->id).
",";
1434 $sql .=
" date_approve='".$this->db->idate($this->date_approve).
"'";
1435 $sql .=
" WHERE rowid = ".((int) $this->
id);
1436 if ($this->db->query(
$sql)) {
1439 $result = $this->
call_trigger(
'EXPENSE_REPORT_APPROVE', $fuser);
1447 if (empty($error)) {
1448 $this->db->commit();
1451 $this->db->rollback();
1452 $this->error = $this->db->error();
1456 $this->db->rollback();
1457 $this->error = $this->db->lasterror();
1461 dol_syslog(get_class($this).
"::setApproved expensereport already with approve status", LOG_WARNING);
1475 public function setDeny($fuser, $details, $notrigger = 0)
1481 if ($this->status != self::STATUS_REFUSED) {
1482 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1483 $sql .=
" SET ref = '".$this->db->escape($this->
ref).
"', fk_statut = ".self::STATUS_REFUSED.
", fk_user_refuse = ".((int) $fuser->id).
",";
1484 $sql .=
" date_refuse='".$this->db->idate($now).
"',";
1485 $sql .=
" detail_refuse='".$this->db->escape($details).
"',";
1486 $sql .=
" fk_user_approve = NULL";
1487 $sql .=
" WHERE rowid = ".((int) $this->
id);
1488 if ($this->db->query(
$sql)) {
1489 $this->fk_statut = 99;
1491 $this->fk_user_refuse = $fuser->id;
1492 $this->detail_refuse = $details;
1493 $this->date_refuse = $now;
1497 $result = $this->
call_trigger(
'EXPENSE_REPORT_DENY', $fuser);
1505 if (empty($error)) {
1506 $this->db->commit();
1509 $this->db->rollback();
1510 $this->error = $this->db->error();
1514 $this->db->rollback();
1515 $this->error = $this->db->lasterror();
1519 dol_syslog(get_class($this).
"::setDeny expensereport already with refuse status", LOG_WARNING);
1538 dol_syslog(get_class($this).
"::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
1539 return $this->
setUnpaid($fuser, $notrigger);
1556 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1557 $sql .=
" SET paid = 0, fk_statut = ".self::STATUS_APPROVED;
1558 $sql .=
" WHERE rowid = ".((int) $this->
id);
1560 dol_syslog(get_class($this).
"::set_unpaid", LOG_DEBUG);
1562 if ($this->db->query(
$sql)) {
1565 $result = $this->
call_trigger(
'EXPENSE_REPORT_UNPAID', $fuser);
1573 if (empty($error)) {
1574 $this->db->commit();
1577 $this->db->rollback();
1578 $this->error = $this->db->error();
1582 $this->db->rollback();
1583 $this->error = $this->db->error();
1587 dol_syslog(get_class($this).
"::set_unpaid expensereport already with unpaid status", LOG_WARNING);
1606 $this->date_cancel = $this->db->idate(
dol_now());
1607 if ($this->status != self::STATUS_CANCELED) {
1610 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1611 $sql .=
" SET fk_statut = ".self::STATUS_CANCELED.
", fk_user_cancel = ".((int) $fuser->id);
1612 $sql .=
", date_cancel='".$this->db->idate($this->date_cancel).
"'";
1613 $sql .=
", detail_cancel='".$this->db->escape($detail).
"'";
1614 $sql .=
" WHERE rowid = ".((int) $this->
id);
1616 dol_syslog(get_class($this).
"::set_cancel", LOG_DEBUG);
1618 if ($this->db->query(
$sql)) {
1621 $result = $this->
call_trigger(
'EXPENSE_REPORT_CANCEL', $fuser);
1629 if (empty($error)) {
1630 $this->db->commit();
1633 $this->db->rollback();
1634 $this->error = $this->db->error();
1638 $this->db->rollback();
1639 $this->error = $this->db->error();
1643 dol_syslog(get_class($this).
"::set_cancel expensereport already with cancel status", LOG_WARNING);
1655 global $langs, $conf;
1656 $langs->load(
"trips");
1658 if (!empty($conf->global->EXPENSEREPORT_ADDON)) {
1661 $file = $conf->global->EXPENSEREPORT_ADDON.
".php";
1662 $classname = $conf->global->EXPENSEREPORT_ADDON;
1665 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
1666 foreach ($dirmodels as $reldir) {
1667 $dir =
dol_buildpath($reldir.
"core/modules/expensereport/");
1670 $mybool |= @include_once $dir.$file;
1673 if ($mybool ===
false) {
1678 $obj =
new $classname();
1679 $numref = $obj->getNextValue($this);
1681 if ($numref !=
"") {
1684 $this->error = $obj->error;
1685 $this->errors = $obj->errors;
1690 $this->error =
"Error_EXPENSEREPORT_ADDON_NotDefined";
1704 global $conf, $langs;
1706 $langs->load(
'expensereport');
1708 $nofetch = !empty($params[
'nofetch']);
1709 $moretitle = $params[
'moretitle'] ??
'';
1712 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"ExpenseReport").
'</u>';
1713 if (isset($this->status)) {
1714 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
1717 $datas[
'picto'] .=
' - '.$moretitle;
1719 if (!empty($this->
ref)) {
1720 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1722 if (!empty($this->total_ht)) {
1723 $datas[
'total_ht'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
1725 if (!empty($this->total_tva)) {
1726 $datas[
'total_tva'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
1728 if (!empty($this->total_ttc)) {
1729 $datas[
'total_ttc'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
1747 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1)
1749 global $langs, $conf, $hookmanager;
1753 $url = DOL_URL_ROOT.
'/expensereport/card.php?id='.$this->id;
1761 'objecttype' => $this->element,
1762 'option' => $option,
1763 'moretitle' => $moretitle,
1766 $classfortooltip =
'classfortooltip';
1769 $classfortooltip =
'classforajaxtooltip';
1770 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
1776 if ($option !=
'nolink') {
1778 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1779 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1780 $add_save_lastsearch_values = 1;
1782 if ($add_save_lastsearch_values) {
1783 $url .=
'&save_lastsearch_values=1';
1793 if (empty($notooltip)) {
1794 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1795 $label = $langs->trans(
"ShowExpenseReport");
1796 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1798 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
1799 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
1802 $linkstart =
'<a href="'.$url.
'"';
1803 $linkstart .= $linkclose.
'>';
1806 $result .= $linkstart;
1808 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') : $dataparams.
' class="'.(($withpicto != 2) ?
'paddingright ' :
'').$classfortooltip.
'"'), 0, 0, $notooltip ? 0 : 1);
1810 if ($withpicto != 2) {
1811 $result .= ($max ?
dol_trunc($ref, $max) : $ref);
1813 $result .= $linkend;
1816 $hookmanager->initHooks(array($this->element .
'dao'));
1817 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1818 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1820 $result = $hookmanager->resPrint;
1822 $result .= $hookmanager->resPrint;
1838 $this->total_ht = $this->total_ht + $ligne_total_ht;
1839 $this->total_tva = $this->total_tva + $ligne_total_tva;
1840 $this->total_ttc = $this->total_ht + $this->total_tva;
1842 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
1843 $sql .=
" total_ht = ".$this->total_ht;
1844 $sql .=
" , total_ttc = ".$this->total_ttc;
1845 $sql .=
" , total_tva = ".$this->total_tva;
1846 $sql .=
" WHERE rowid = ".((int) $this->
id);
1848 $result = $this->db->query(
$sql);
1852 $this->error = $this->db->error();
1872 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)
1874 global $conf, $langs, $mysoc;
1876 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);
1878 if ($this->status == self::STATUS_DRAFT) {
1882 if (empty($fk_c_type_fees) || $fk_c_type_fees < 0) {
1883 $fk_c_type_fees = 0;
1885 if (empty($fk_c_exp_tax_cat) || $fk_c_exp_tax_cat < 0) {
1886 $fk_c_exp_tax_cat = 0;
1888 if (empty($vatrate) || $vatrate < 0) {
1894 if (empty($fk_project)) {
1899 if (!preg_match(
'/\s*\((.*)\)/', $vatrate)) {
1910 $seller->tva_assuj = 1;
1911 $buyer =
new Societe($this->db);
1917 if (preg_match(
'/\s*\((.*)\)/', $vatrate, $reg)) {
1918 $vat_src_code = $reg[1];
1919 $vatrate = preg_replace(
'/\s*\(.*\)/',
'', $vatrate);
1921 $vatrate = preg_replace(
'/\*/',
'', $vatrate);
1923 $tmp =
calcul_price_total($qty, $up, 0, $vatrate, -1, -1, 0,
'TTC', 0, $type, $seller, $localtaxes_type);
1925 $this->line->value_unit = $up;
1927 $this->line->vat_src_code = $vat_src_code;
1928 $this->line->vatrate =
price2num($vatrate);
1929 $this->line->localtax1_tx = $localtaxes_type[1];
1930 $this->line->localtax2_tx = $localtaxes_type[3];
1931 $this->line->localtax1_type = $localtaxes_type[0];
1932 $this->line->localtax2_type = $localtaxes_type[2];
1934 $this->line->total_ttc = $tmp[2];
1935 $this->line->total_ht = $tmp[0];
1936 $this->line->total_tva = $tmp[1];
1937 $this->line->total_localtax1 = $tmp[9];
1938 $this->line->total_localtax2 = $tmp[10];
1940 $this->line->fk_expensereport = $this->id;
1941 $this->line->qty = $qty;
1942 $this->line->date = $date;
1943 $this->line->fk_c_type_fees = $fk_c_type_fees;
1944 $this->line->fk_c_exp_tax_cat = $fk_c_exp_tax_cat;
1945 $this->line->comments = $comments;
1946 $this->line->fk_projet = $fk_project;
1947 $this->line->fk_project = $fk_project;
1949 $this->line->fk_ecm_files = $fk_ecm_files;
1954 $result = $this->line->insert(0,
true);
1958 $this->db->commit();
1959 return $this->line->id;
1961 $this->db->rollback();
1965 $this->error = $this->line->error;
1966 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
1967 $this->db->rollback();
1971 dol_syslog(get_class($this).
"::addline status of expense report must be Draft to allow use of ->addline()", LOG_ERR);
1972 $this->error =
'ErrorExpenseNotDraft';
1986 global $user, $conf, $db, $langs, $mysoc;
1988 $langs->load(
'trips');
1991 if (!is_object($seller)) {
1993 $seller->tva_assuj = 1;
1997 $rulestocheck = $expensereportrule->getAllRule($this->line->fk_c_type_fees, $this->line->date, $this->fk_user_author);
2000 $rule_warning_message_tab = array();
2002 $current_total_ttc = $this->line->total_ttc;
2003 $new_current_total_ttc = $this->line->total_ttc;
2006 foreach ($rulestocheck as $rule) {
2007 if (in_array($rule->code_expense_rules_type, array(
'EX_DAY',
'EX_MON',
'EX_YEA'))) {
2008 $amount_to_test = $this->line->getExpAmount($rule, $this->fk_user_author, $rule->code_expense_rules_type);
2010 $amount_to_test = $current_total_ttc;
2013 $amount_to_test = $amount_to_test - $current_total_ttc + $new_current_total_ttc;
2015 if ($amount_to_test > $rule->amount) {
2018 if ($rule->restrictive) {
2019 $this->error =
'ExpenseReportConstraintViolationError';
2020 $this->errors[] = $this->error;
2022 $new_current_total_ttc -= $amount_to_test - $rule->amount;
2023 $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));
2025 $this->error =
'ExpenseReportConstraintViolationWarning';
2026 $this->errors[] = $this->error;
2028 $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));
2035 $this->line->rule_warning_message = implode(
'\n', $rule_warning_message_tab);
2037 if ($violation > 0) {
2038 $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);
2040 $this->line->value_unit = $tmp[5];
2041 $this->line->total_ttc = $tmp[2];
2042 $this->line->total_ht = $tmp[0];
2043 $this->line->total_tva = $tmp[1];
2044 $this->line->total_localtax1 = $tmp[9];
2045 $this->line->total_localtax2 = $tmp[10];
2062 global $conf, $mysoc;
2064 if (empty($conf->global->MAIN_USE_EXPENSE_IK)) {
2068 $userauthor =
new User($this->db);
2069 if ($userauthor->fetch($this->fk_user_author) <= 0) {
2070 $this->error =
'ErrorCantFetchUser';
2071 $this->errors[] =
'ErrorCantFetchUser';
2076 if (!is_object($seller)) {
2078 $seller->tva_assuj = 1;
2082 $range = $expenseik->getRangeByUser($userauthor, $this->line->fk_c_exp_tax_cat);
2084 if (empty($range)) {
2085 $this->error =
'ErrorNoRangeAvailable';
2086 $this->errors[] =
'ErrorNoRangeAvailable';
2090 if (!empty($conf->global->MAIN_EXPENSE_APPLY_ENTIRE_OFFSET)) {
2091 $ikoffset = $range->ikoffset;
2093 $ikoffset = $range->ikoffset / 12;
2098 $new_up = $range->coef + ($ikoffset / $this->line->qty);
2099 $tmp =
calcul_price_total($this->line->qty, $new_up, 0, $this->line->vatrate, 0, 0, 0,
'TTC', 0, $type, $seller);
2101 $this->line->value_unit = $tmp[5];
2102 $this->line->total_ttc = $tmp[2];
2103 $this->line->total_ht = $tmp[0];
2104 $this->line->total_tva = $tmp[1];
2105 $this->line->total_localtax1 = $tmp[9];
2106 $this->line->total_localtax2 = $tmp[10];
2121 $sql =
'SELECT e.rowid FROM '.MAIN_DB_PREFIX.
'expensereport e';
2122 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"expensereport_det d ON (e.rowid = d.fk_expensereport)";
2123 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"c_type_fees f ON (d.fk_c_type_fees = f.id AND f.code = 'EX_KME')";
2124 $sql .=
" WHERE e.fk_user_author = ".(int) $this->fk_user_author;
2125 $sql .=
" AND YEAR(d.date) = '".dol_print_date($this->line->date,
'%Y').
"' AND MONTH(d.date) = '".
dol_print_date($this->line->date,
'%m').
"'";
2126 if (!empty($this->line->id)) {
2127 $sql .=
' AND d.rowid <> '.((int) $this->line->id);
2130 dol_syslog(get_class($this).
"::offsetAlreadyGiven");
2131 $resql = $this->db->query(
$sql);
2133 $num = $this->db->num_rows($resql);
2161 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)
2163 global $user, $mysoc;
2165 if ($this->status == self::STATUS_DRAFT || $this->status == self::STATUS_REFUSED) {
2173 $seller->tva_assuj = 1;
2174 $seller->localtax1_assuj = $mysoc->localtax1_assuj;
2175 $seller->localtax2_assuj = $mysoc->localtax1_assuj;
2176 $buyer =
new Societe($this->db);
2183 if (preg_match(
'/\((.*)\)/', $vatrate, $reg)) {
2184 $vat_src_code = $reg[1];
2185 $vatrate = preg_replace(
'/\s*\(.*\)/',
'', $vatrate);
2187 $vatrate = preg_replace(
'/\*/',
'', $vatrate);
2189 $tmp =
calcul_price_total($qty, $value_unit, 0, $vatrate, -1, -1, 0,
'TTC', 0, $type, $seller, $localtaxes_type);
2194 $tx_tva = $vatrate / 100;
2195 $tx_tva = $tx_tva + 1;
2198 $this->line->comments = $comments;
2199 $this->line->qty = $qty;
2200 $this->line->value_unit = $value_unit;
2201 $this->line->date = $date;
2203 $this->line->fk_expensereport = $expensereport_id;
2204 $this->line->fk_c_type_fees = $type_fees_id;
2205 $this->line->fk_c_exp_tax_cat = $fk_c_exp_tax_cat;
2206 $this->line->fk_projet = $projet_id;
2207 $this->line->fk_project = $projet_id;
2209 $this->line->vat_src_code = $vat_src_code;
2210 $this->line->vatrate =
price2num($vatrate);
2211 $this->line->localtax1_tx = $localtaxes_type[1];
2212 $this->line->localtax2_tx = $localtaxes_type[3];
2213 $this->line->localtax1_type = $localtaxes_type[0];
2214 $this->line->localtax2_type = $localtaxes_type[2];
2216 $this->line->total_ttc = $tmp[2];
2217 $this->line->total_ht = $tmp[0];
2218 $this->line->total_tva = $tmp[1];
2219 $this->line->total_localtax1 = $tmp[9];
2220 $this->line->total_localtax2 = $tmp[10];
2222 $this->line->fk_ecm_files = $fk_ecm_files;
2224 $this->line->id = ((int) $rowid);
2227 $sql =
"SELECT c.code as code_type_fees, c.label as libelle_type_fees";
2228 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_type_fees as c";
2229 $sql .=
" WHERE c.id = ".((int) $type_fees_id);
2230 $resql = $this->db->query(
$sql);
2232 $objp_fees = $this->db->fetch_object($resql);
2233 $this->line->type_fees_code = $objp_fees->code_type_fees;
2234 $this->line->type_fees_libelle = $objp_fees->libelle_type_fees;
2235 $this->db->free($resql);
2239 $sql =
"SELECT p.ref as ref_projet, p.title as title_projet";
2240 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as p";
2241 $sql .=
" WHERE p.rowid = ".((int) $projet_id);
2242 $resql = $this->db->query(
$sql);
2244 $objp_projet = $this->db->fetch_object($resql);
2245 $this->line->projet_ref = $objp_projet->ref_projet;
2246 $this->line->projet_title = $objp_projet->title_projet;
2247 $this->db->free($resql);
2253 $result = $this->line->update($user);
2258 if (!$error && !$notrigger) {
2260 $result = $this->
call_trigger(
'EXPENSE_REPORT_DET_MODIFY', $user);
2268 $this->db->commit();
2271 $this->error = $this->line->error;
2272 $this->errors = $this->line->errors;
2273 $this->db->rollback();
2297 $result = $this->
call_trigger(
'EXPENSE_REPORT_DET_DELETE', $fuser);
2304 $sql =
' DELETE FROM '.MAIN_DB_PREFIX.$this->table_element_line;
2305 $sql .=
' WHERE rowid = '.((int) $rowid);
2308 $result = $this->db->query(
$sql);
2310 if (!$result || $error > 0 ) {
2311 $this->error = $this->db->error();
2312 dol_syslog(get_class($this).
"::deleteline Error ".$this->error, LOG_ERR);
2313 $this->db->rollback();
2319 $this->db->commit();
2336 $sql =
"SELECT rowid, date_debut, date_fin";
2337 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
2338 $sql .=
" WHERE fk_user_author = ".((int) $fuser->id);
2341 $result = $this->db->query(
$sql);
2343 $num_rows = $this->db->num_rows($result); $i = 0;
2345 if ($num_rows > 0) {
2346 $date_d_form = $date_debut;
2347 $date_f_form = $date_fin;
2349 while ($i < $num_rows) {
2350 $objp = $this->db->fetch_object($result);
2352 $date_d_req = $this->db->jdate($objp->date_debut);
2353 $date_f_req = $this->db->jdate($objp->date_fin);
2355 if (!($date_f_form < $date_d_req || $date_d_form > $date_f_req)) {
2356 return $objp->rowid;
2367 $this->error = $this->db->lasterror();
2368 dol_syslog(get_class($this).
"::periode_existe Error ".$this->error, LOG_ERR);
2384 $users_validator = array();
2386 $sql =
"SELECT DISTINCT ur.fk_user";
2387 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user_rights as ur, ".MAIN_DB_PREFIX.
"rights_def as rd";
2388 $sql .=
" WHERE ur.fk_id = rd.id and rd.module = 'expensereport' AND rd.perms = 'approve'";
2390 $sql .=
" SELECT DISTINCT ugu.fk_user";
2391 $sql .=
" FROM ".MAIN_DB_PREFIX.
"usergroup_user as ugu, ".MAIN_DB_PREFIX.
"usergroup_rights as ur, ".MAIN_DB_PREFIX.
"rights_def as rd";
2392 $sql .=
" WHERE ugu.fk_usergroup = ur.fk_usergroup AND ur.fk_id = rd.id and rd.module = 'expensereport' AND rd.perms = 'approve'";
2395 dol_syslog(get_class($this).
"::fetch_users_approver_expensereport sql=".
$sql);
2396 $result = $this->db->query(
$sql);
2398 $num_rows = $this->db->num_rows($result); $i = 0;
2399 while ($i < $num_rows) {
2400 $objp = $this->db->fetch_object($result);
2401 array_push($users_validator, $objp->fk_user);
2404 return $users_validator;
2406 $this->error = $this->db->lasterror();
2407 dol_syslog(get_class($this).
"::fetch_users_approver_expensereport Error ".$this->error, LOG_ERR);
2423 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
2427 $outputlangs->load(
"trips");
2430 if (!empty($this->model_pdf)) {
2431 $modele = $this->model_pdf;
2432 } elseif (!empty($this->modelpdf)) {
2433 $modele = $this->modelpdf;
2434 } elseif (!empty($conf->global->EXPENSEREPORT_ADDON_PDF)) {
2435 $modele = $conf->global->EXPENSEREPORT_ADDON_PDF;
2439 if (!empty($modele)) {
2440 $modelpath =
"core/modules/expensereport/doc/";
2442 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2458 $sql =
"SELECT id, code, label";
2459 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_type_fees";
2460 $sql .=
" WHERE active = ".((int) $active);
2461 dol_syslog(get_class($this).
"::listOfTypes", LOG_DEBUG);
2462 $result = $this->db->query(
$sql);
2464 $num = $this->db->num_rows($result);
2467 $obj = $this->db->fetch_object($result);
2468 $ret[$obj->code] = (($langs->transnoentitiesnoconv($obj->code) != $obj->code) ? $langs->transnoentitiesnoconv($obj->code) : $obj->label);
2486 global $conf, $user;
2488 $this->nb = array();
2490 $sql =
"SELECT count(ex.rowid) as nb";
2491 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport as ex";
2492 $sql .=
" WHERE ex.fk_statut > 0";
2493 $sql .=
" AND ex.entity IN (".getEntity(
'expensereport').
")";
2494 if (empty($user->rights->expensereport->readall)) {
2495 $userchildids = $user->getAllChildIds(1);
2496 $sql .=
" AND (ex.fk_user_author IN (".$this->db->sanitize(join(
',', $userchildids)).
")";
2497 $sql .=
" OR ex.fk_user_validator IN (".$this->db->sanitize(join(
',', $userchildids)).
"))";
2500 $resql = $this->db->query(
$sql);
2502 while ($obj = $this->db->fetch_object($resql)) {
2503 $this->nb[
"expensereports"] = $obj->nb;
2505 $this->db->free($resql);
2509 $this->error = $this->db->error();
2525 global $conf, $langs;
2533 $sql =
"SELECT ex.rowid, ex.date_valid";
2534 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport as ex";
2535 if ($option ==
'toapprove') {
2536 $sql .=
" WHERE ex.fk_statut = ".self::STATUS_VALIDATED;
2538 $sql .=
" WHERE ex.fk_statut = ".self::STATUS_APPROVED;
2540 $sql .=
" AND ex.entity IN (".getEntity(
'expensereport').
")";
2541 if (empty($user->rights->expensereport->readall)) {
2542 $userchildids = $user->getAllChildIds(1);
2543 $sql .=
" AND (ex.fk_user_author IN (".$this->db->sanitize(join(
',', $userchildids)).
")";
2544 $sql .=
" OR ex.fk_user_validator IN (".$this->db->sanitize(join(
',', $userchildids)).
"))";
2547 $resql = $this->db->query(
$sql);
2549 $langs->load(
"trips");
2552 if ($option ==
'toapprove') {
2553 $response->warning_delay = $conf->expensereport->approve->warning_delay / 60 / 60 / 24;
2554 $response->label = $langs->trans(
"ExpenseReportsToApprove");
2555 $response->labelShort = $langs->trans(
"ToApprove");
2558 $response->warning_delay = $conf->expensereport->payment->warning_delay / 60 / 60 / 24;
2559 $response->label = $langs->trans(
"ExpenseReportsToPay");
2560 $response->labelShort = $langs->trans(
"StatusToPay");
2561 $response->url = DOL_URL_ROOT.
'/expensereport/list.php?mainmenu=hrm&statut='.
self::STATUS_APPROVED;
2565 while ($obj = $this->db->fetch_object($resql)) {
2566 $response->nbtodo++;
2568 if ($option ==
'toapprove') {
2569 if ($this->db->jdate($obj->date_valid) < ($now - $conf->expensereport->approve->warning_delay)) {
2570 $response->nbtodolate++;
2573 if ($this->db->jdate($obj->date_valid) < ($now - $conf->expensereport->payment->warning_delay)) {
2574 $response->nbtodolate++;
2582 $this->error = $this->db->error();
2598 if ($option ==
'toapprove' && $this->status != 2) {
2601 if ($option ==
'topay' && $this->status != 5) {
2606 if ($option ==
'toapprove') {
2607 return (!empty($this->datevalid) ? $this->datevalid : $this->date_valid) < ($now - $conf->expensereport->approve->warning_delay);
2609 return (!empty($this->datevalid) ? $this->datevalid : $this->date_valid) < ($now - $conf->expensereport->payment->warning_delay);
2620 $alreadydispatched = 0;
2622 $type =
'expense_report';
2624 $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);
2625 $resql = $this->db->query(
$sql);
2627 $obj = $this->db->fetch_object($resql);
2629 $alreadydispatched = $obj->nb;
2632 $this->error = $this->db->lasterror();
2636 if ($alreadydispatched) {
2649 $table =
'payment_expensereport';
2650 $field =
'fk_expensereport';
2652 $sql =
'SELECT sum(amount) as amount';
2653 $sql .=
' FROM '.MAIN_DB_PREFIX.$table;
2654 $sql .=
" WHERE ".$field.
" = ".((int) $this->
id);
2656 dol_syslog(get_class($this).
"::getSumPayments", LOG_DEBUG);
2657 $resql = $this->db->query(
$sql);
2659 $obj = $this->db->fetch_object($resql);
2660 $this->db->free($resql);
2661 return (empty($obj->amount) ? 0 : $obj->amount);
2663 $this->error = $this->db->lasterror();
2678 global $langs, $db, $conf;
2686 $this->error = $langs->trans(
'ErrorBadParameterCat');
2691 $this->error = $langs->trans(
'ErrorBadParameterQty');
2695 $currentUser =
new User($db);
2696 $currentUser->fetch($this->fk_user);
2697 $currentUser->getrights(
'expensereport');
2701 $sql =
" SELECT r.range_ik, t.ikoffset as offset, t.coef";
2702 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport_ik t";
2703 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_exp_tax_range r ON r.rowid = t.fk_range";
2704 $sql .=
" WHERE t.fk_c_exp_tax_cat = ".(int) $fk_cat;
2705 $sql .=
" ORDER BY r.range_ik ASC";
2707 dol_syslog(
"expenseReport::computeTotalkm sql=".
$sql, LOG_DEBUG);
2709 $result = $this->db->query(
$sql);
2712 if ($conf->global->EXPENSEREPORT_CALCULATE_MILEAGE_EXPENSE_COEFFICIENT_ON_CURRENT_YEAR) {
2714 $sql =
" SELECT count(n.qty) as cumul FROM ".MAIN_DB_PREFIX.
"expensereport_det n";
2715 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expensereport e ON e.rowid = n.fk_expensereport";
2716 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_type_fees tf ON tf.id = n.fk_c_type_fees";
2717 $sql.=
" WHERE e.fk_user_author = ".(int) $this->fk_user_author;
2718 $sql.=
" AND YEAR(n.date) = ".(int) $arrayDate[
'year'];
2719 $sql.=
" AND tf.code = 'EX_KME' ";
2722 $resql = $this->db->query(
$sql);
2725 $obj = $this->db->fetch_object($resql);
2726 $cumulYearQty = $obj->cumul;
2728 $qty = $cumulYearQty + $qty;
2731 $num = $this->db->num_rows($result);
2734 for ($i = 0; $i < $num; $i++) {
2735 $obj = $this->db->fetch_object($result);
2741 for ($i = 0; $i < $num; $i++) {
2742 if ($i < ($num - 1)) {
2743 if ($qty > $ranges[$i]->range_ik && $qty < $ranges[$i+1]->range_ik) {
2744 $coef = $ranges[$i]->coef;
2745 $offset = $ranges[$i]->offset;
2748 if ($qty > $ranges[$i]->range_ik) {
2749 $coef = $ranges[$i]->coef;
2750 $offset = $ranges[$i]->offset;
2757 $this->error = $langs->trans(
'TaxUndefinedForThisCategory');
2761 $this->error = $this->db->error().
" sql=".
$sql;
2778 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
2780 $return =
'<div class="box-flex-item box-flex-grow-zero">';
2781 $return .=
'<div class="info-box info-box-sm">';
2782 $return .=
'<span class="info-box-icon bg-infobox-action">';
2784 $return .=
'</span>';
2785 $return .=
'<div class="info-box-content">';
2786 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl(1) : $this->ref).
'</span>';
2787 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
2788 if (property_exists($this,
'fk_user_author') && !empty($this->
id)) {
2789 $return .=
'<br><span class="info-box-label">'.$this->fk_user_author.
'</span>';
2791 if (property_exists($this,
'date_debut') && property_exists($this,
'date_fin')) {
2792 $return .=
'<br><span class="info-box-label">'.dol_print_date($this->date_debut,
'day').
'</span>';
2793 $return .=
' <span class="opacitymedium">'.$langs->trans(
"To").
'</span> ';
2794 $return .=
'<span class="info-box-label">'.dol_print_date($this->date_fin,
'day').
'</span>';
2796 if (method_exists($this,
'getLibStatut')) {
2797 $return .=
'<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).
'</div>';
2799 $return .=
'</div>';
2800 $return .=
'</div>';
2801 $return .=
'</div>';
2820 public $table_element =
'expensereport_det';
2840 public $fk_c_type_fees;
2845 public $fk_c_exp_tax_cat;
2855 public $fk_expensereport;
2857 public $type_fees_code;
2858 public $type_fees_libelle;
2859 public $type_fees_accountancy_code;
2862 public $projet_title;
2866 public $vat_src_code;
2868 public $localtax1_tx;
2869 public $localtax2_tx;
2870 public $localtax1_type;
2871 public $localtax2_type;
2876 public $total_localtax1;
2877 public $total_localtax2;
2883 public $fk_multicurrency;
2888 public $multicurrency_code;
2889 public $multicurrency_tx;
2890 public $multicurrency_total_ht;
2891 public $multicurrency_total_tva;
2892 public $multicurrency_total_ttc;
2897 public $fk_ecm_files;
2899 public $rule_warning_message;
2920 $sql =
'SELECT fde.rowid, fde.fk_expensereport, fde.fk_c_type_fees, fde.fk_c_exp_tax_cat, fde.fk_projet as fk_project, fde.date,';
2921 $sql .=
' fde.tva_tx as vatrate, fde.vat_src_code, fde.comments, fde.qty, fde.value_unit, fde.total_ht, fde.total_tva, fde.total_ttc, fde.fk_ecm_files,';
2922 $sql .=
' fde.localtax1_tx, fde.localtax2_tx, fde.localtax1_type, fde.localtax2_type, fde.total_localtax1, fde.total_localtax2, fde.rule_warning_message,';
2923 $sql .=
' ctf.code as type_fees_code, ctf.label as type_fees_libelle,';
2924 $sql .=
' pjt.rowid as projet_id, pjt.title as projet_title, pjt.ref as projet_ref';
2925 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expensereport_det as fde';
2926 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_type_fees as ctf ON fde.fk_c_type_fees=ctf.id';
2927 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'projet as pjt ON fde.fk_projet=pjt.rowid';
2928 $sql .=
' WHERE fde.rowid = '.((int) $rowid);
2930 $result = $this->db->query(
$sql);
2933 $objp = $this->db->fetch_object($result);
2935 $this->
rowid = $objp->rowid;
2936 $this->
id = $objp->rowid;
2937 $this->
ref = $objp->ref;
2938 $this->fk_expensereport = $objp->fk_expensereport;
2939 $this->comments = $objp->comments;
2940 $this->qty = $objp->qty;
2941 $this->date = $objp->date;
2942 $this->dates = $this->db->jdate($objp->date);
2943 $this->value_unit = $objp->value_unit;
2944 $this->fk_c_type_fees = $objp->fk_c_type_fees;
2945 $this->fk_c_exp_tax_cat = $objp->fk_c_exp_tax_cat;
2946 $this->fk_projet = $objp->fk_project;
2947 $this->fk_project = $objp->fk_project;
2948 $this->type_fees_code = $objp->type_fees_code;
2949 $this->type_fees_libelle = $objp->type_fees_libelle;
2950 $this->projet_ref = $objp->projet_ref;
2951 $this->projet_title = $objp->projet_title;
2953 $this->
vatrate = $objp->vatrate;
2954 $this->vat_src_code = $objp->vat_src_code;
2955 $this->localtax1_tx = $objp->localtax1_tx;
2956 $this->localtax2_tx = $objp->localtax2_tx;
2957 $this->localtax1_type = $objp->localtax1_type;
2958 $this->localtax2_type = $objp->localtax2_type;
2960 $this->total_ht = $objp->total_ht;
2961 $this->total_tva = $objp->total_tva;
2962 $this->total_ttc = $objp->total_ttc;
2963 $this->total_localtax1 = $objp->total_localtax1;
2964 $this->total_localtax2 = $objp->total_localtax2;
2966 $this->fk_ecm_files = $objp->fk_ecm_files;
2968 $this->rule_warning_message = $objp->rule_warning_message;
2970 $this->db->free($result);
2986 public function insert($notrigger = 0, $fromaddline =
false)
2988 global $user, $conf;
2992 dol_syslog(
"ExpenseReportLine::Insert", LOG_DEBUG);
2995 $this->comments = trim($this->comments);
2996 if (empty($this->value_unit)) {
2997 $this->value_unit = 0;
3001 if (empty($this->fk_c_exp_tax_cat)) {
3002 $this->fk_c_exp_tax_cat = 0;
3007 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'expensereport_det';
3008 $sql .=
' (fk_expensereport, fk_c_type_fees, fk_projet,';
3009 $sql .=
' tva_tx, vat_src_code,';
3010 $sql .=
' localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
3011 $sql .=
' comments, qty, value_unit,';
3012 $sql .=
' total_ht, total_tva, total_ttc,';
3013 $sql .=
' total_localtax1, total_localtax2,';
3014 $sql .=
' date, rule_warning_message, fk_c_exp_tax_cat, fk_ecm_files)';
3015 $sql .=
" VALUES (".$this->db->escape($this->fk_expensereport).
",";
3016 $sql .=
" ".((int) $this->fk_c_type_fees).
",";
3017 $sql .=
" ".((int) (!empty($this->fk_project) && $this->fk_project > 0) ? $this->fk_project : ((!empty($this->fk_projet) && $this->fk_projet > 0) ? $this->fk_projet :
'null')).
",";
3019 $sql .=
" '".$this->db->escape(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"',";
3022 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
3023 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
3024 $sql .=
" '".$this->db->escape($this->comments).
"',";
3025 $sql .=
" ".((float) $this->qty).
",";
3026 $sql .=
" ".((float) $this->value_unit).
",";
3030 $sql .=
" ".((float)
price2num($this->total_localtax1)).
",";
3031 $sql .=
" ".((float)
price2num($this->total_localtax2)).
",";
3032 $sql .=
" '".$this->db->idate($this->date).
"',";
3033 $sql .=
" ".(empty($this->rule_warning_message) ?
'null' :
"'".$this->db->escape($this->rule_warning_message).
"'").
",";
3034 $sql .=
" ".((int) $this->fk_c_exp_tax_cat).
",";
3035 $sql .=
" ".($this->fk_ecm_files > 0 ? ((int) $this->fk_ecm_files) :
'null');
3038 $resql = $this->db->query(
$sql);
3040 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'expensereport_det');
3043 if (!$error && !$notrigger) {
3045 $result = $this->
call_trigger(
'EXPENSE_REPORT_DET_CREATE', $user);
3053 if (!$fromaddline) {
3055 $tmpparent->fetch($this->fk_expensereport);
3056 $result = $tmpparent->update_price(1);
3059 $this->error = $tmpparent->error;
3060 $this->errors = $tmpparent->errors;
3068 $this->db->commit();
3071 $this->error = $this->db->lasterror();
3072 dol_syslog(
"ExpenseReportLine::insert Error ".$this->error, LOG_ERR);
3073 $this->db->rollback();
3090 $sql =
'SELECT SUM(d.total_ttc) as total_amount';
3091 $sql .=
' FROM '.MAIN_DB_PREFIX.
'expensereport_det d';
3092 $sql .=
' INNER JOIN '.MAIN_DB_PREFIX.
'expensereport e ON (d.fk_expensereport = e.rowid)';
3093 $sql .=
' WHERE e.fk_user_author = '.((int) $fk_user);
3094 if (!empty($this->
id)) {
3095 $sql .=
' AND d.rowid <> '.((int) $this->
id);
3097 $sql .=
' AND d.fk_c_type_fees = '.((int) $rule->fk_c_type_fees);
3098 if ($mode ==
'day' || $mode ==
'EX_DAY') {
3099 $sql .=
" AND d.date = '".dol_print_date($this->date,
'%Y-%m-%d').
"'";
3100 } elseif ($mode ==
'mon' || $mode ==
'EX_MON') {
3101 $sql .=
" AND DATE_FORMAT(d.date, '%Y-%m') = '".dol_print_date($this->date,
'%Y-%m').
"'";
3102 } elseif ($mode ==
'year' || $mode ==
'EX_YEA') {
3103 $sql .=
" AND DATE_FORMAT(d.date, '%Y') = '".dol_print_date($this->date,
'%Y').
"'";
3106 dol_syslog(
'ExpenseReportLine::getExpAmount');
3108 $resql = $this->db->query(
$sql);
3110 $num = $this->db->num_rows($resql);
3112 $obj = $this->db->fetch_object($resql);
3113 $amount = (double) $obj->total_amount;
3119 return $amount + $this->total_ttc;
3130 global $langs, $conf;
3135 $this->comments = trim($this->comments);
3137 $this->value_unit =
price2num($this->value_unit);
3138 if (empty($this->fk_c_exp_tax_cat)) {
3139 $this->fk_c_exp_tax_cat = 0;
3145 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expensereport_det SET";
3146 $sql .=
" comments='".$this->db->escape($this->comments).
"'";
3147 $sql .=
", value_unit = ".((float) $this->value_unit);
3148 $sql .=
", qty=".((float) $this->qty);
3149 $sql .=
", date='".$this->db->idate($this->date).
"'";
3150 $sql .=
", total_ht=".((float)
price2num($this->total_ht,
'MT'));
3151 $sql .=
", total_tva=".((float)
price2num($this->total_tva,
'MT'));
3152 $sql .=
", total_ttc=".((float)
price2num($this->total_ttc,
'MT'));
3153 $sql .=
", total_localtax1=".((float)
price2num($this->total_localtax1,
'MT'));
3154 $sql .=
", total_localtax2=".((float)
price2num($this->total_localtax2,
'MT'));
3156 $sql .=
", vat_src_code='".$this->db->escape($this->vat_src_code).
"'";
3157 $sql .=
", localtax1_tx=".((float) $this->localtax1_tx);
3158 $sql .=
", localtax2_tx=".((float) $this->localtax2_tx);
3159 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
3160 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
3161 $sql .=
", rule_warning_message='".$this->db->escape($this->rule_warning_message).
"'";
3162 $sql .=
", fk_c_exp_tax_cat=".$this->db->escape($this->fk_c_exp_tax_cat);
3163 $sql .=
", fk_ecm_files=".($this->fk_ecm_files > 0 ? ((int) $this->fk_ecm_files) :
'null');
3164 if ($this->fk_c_type_fees) {
3165 $sql .=
", fk_c_type_fees = ".((int) $this->fk_c_type_fees);
3167 $sql .=
", fk_c_type_fees=null";
3169 if ($this->fk_project > 0) {
3170 $sql .=
", fk_projet=".((int) $this->fk_project);
3172 $sql .=
", fk_projet=null";
3174 $sql .=
" WHERE rowid = ".((int) ($this->
rowid ? $this->
rowid : $this->
id));
3178 $resql = $this->db->query(
$sql);
3181 $result = $tmpparent->fetch($this->fk_expensereport);
3183 $result = $tmpparent->update_price(1);
3186 $this->error = $tmpparent->error;
3187 $this->errors = $tmpparent->errors;
3191 $this->error = $tmpparent->error;
3192 $this->errors = $tmpparent->errors;
3200 $this->db->commit();
3203 $this->error = $this->db->lasterror();
3204 dol_syslog(
"ExpenseReportLine::update Error ".$this->error, LOG_ERR);
3205 $this->db->rollback();