/
expensereport.class.php
2747 lines (2370 loc) · 88.1 KB
/
expensereport.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Copyright (C) 2011 Dimitri Mouillard <dmouillard@teclib.com>
* Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2015 Alexandre Spangaro <aspangaro@zendsi.com>
* Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/expensereport/class/expensereport.class.php
* \ingroup expensereport
* \brief File to manage Expense Reports
*/
require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
require_once DOL_DOCUMENT_ROOT .'/expensereport/class/expensereport_ik.class.php';
require_once DOL_DOCUMENT_ROOT .'/expensereport/class/expensereport_rule.class.php';
/**
* Class to manage Trips and Expenses
*/
class ExpenseReport extends CommonObject
{
var $element='expensereport';
var $table_element='expensereport';
var $table_element_line = 'expensereport_det';
var $fk_element = 'fk_expensereport';
var $picto = 'trip';
var $lines=array();
public $date_debut;
public $date_fin;
var $fk_user_validator;
var $status;
var $fk_statut; // -- 0=draft, 2=validated (attente approb), 4=canceled, 5=approved, 6=payed, 99=denied
var $fk_c_paiement;
var $paid;
var $user_author_infos;
var $user_validator_infos;
var $fk_typepayment;
var $num_payment;
var $code_paiement;
var $code_statut;
// ACTIONS
// Create
var $date_create;
var $fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
// Update
var $date_modif;
var $fk_user_modif;
// Refus
var $date_refuse;
var $detail_refuse;
var $fk_user_refuse;
// Annulation
var $date_cancel;
var $detail_cancel;
var $fk_user_cancel;
// Validation
var $date_valid;
var $fk_user_valid;
var $user_valid_infos;
// Approve
var $date_approve;
var $fk_user_approve;
// Paiement
var $user_paid_infos;
/*
END ACTIONS
*/
/**
* Draft
*/
const STATUS_DRAFT = 0;
/**
* Validated (need to be paid)
*/
const STATUS_VALIDATED = 2;
/**
* Classified approved
*/
const STATUS_APPROVED = 5;
/**
* Classified refused
*/
const STATUS_REFUSED = 99;
/**
* Classified paid.
*/
const STATUS_CLOSED = 6;
/**
* Constructor
*
* @param DoliDB $db Handler acces base de donnees
*/
function __construct($db)
{
$this->db = $db;
$this->total_ht = 0;
$this->total_ttc = 0;
$this->total_tva = 0;
$this->modepaymentid = 0;
// List of language codes for status
$this->statuts_short = array(0 => 'Draft', 2 => 'Validated', 4 => 'Canceled', 5 => 'Approved', 6 => 'Paid', 99 => 'Refused');
$this->statuts = array(0 => 'Draft', 2 => 'ValidatedWaitingApproval', 4 => 'Canceled', 5 => 'Approved', 6 => 'Paid', 99 => 'Refused');
$this->statuts_logo = array(0 => 'statut0', 2 => 'statut1', 4 => 'statut5', 5 => 'statut3', 6 => 'statut6', 99 => 'statut8');
return 1;
}
/**
* Create object in database
*
* @param User $user User that create
* @param int $notrigger Disable triggers
* @return int <0 if KO, >0 if OK
*/
function create($user, $notrigger=0)
{
global $conf;
$now = dol_now();
$error = 0;
// Check parameters
if (empty($this->date_debut) || empty($this->date_fin))
{
$this->error='ErrorFieldRequired';
return -1;
}
$fuserid = $this->fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
if (empty($fuserid)) $fuserid = $user->id;
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
$sql.= "ref";
$sql.= ",total_ht";
$sql.= ",total_ttc";
$sql.= ",total_tva";
$sql.= ",date_debut";
$sql.= ",date_fin";
$sql.= ",date_create";
$sql.= ",fk_user_author";
$sql.= ",fk_user_validator";
$sql.= ",fk_user_modif";
$sql.= ",fk_statut";
$sql.= ",fk_c_paiement";
$sql.= ",paid";
$sql.= ",note_public";
$sql.= ",note_private";
$sql.= ",entity";
$sql.= ") VALUES(";
$sql.= "'(PROV)'";
$sql.= ", ".$this->total_ht;
$sql.= ", ".$this->total_ttc;
$sql.= ", ".$this->total_tva;
$sql.= ", '".$this->db->idate($this->date_debut)."'";
$sql.= ", '".$this->db->idate($this->date_fin)."'";
$sql.= ", '".$this->db->idate($now)."'";
$sql.= ", ".$fuserid;
$sql.= ", ".($this->fk_user_validator > 0 ? $this->fk_user_validator:"null");
$sql.= ", ".($this->fk_user_modif > 0 ? $this->fk_user_modif:"null");
$sql.= ", ".($this->fk_statut > 1 ? $this->fk_statut:0);
$sql.= ", ".($this->modepaymentid?$this->modepaymentid:"null");
$sql.= ", 0";
$sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
$sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
$sql.= ", ".$conf->entity;
$sql.= ")";
$result = $this->db->query($sql);
if ($result)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
$this->ref='(PROV'.$this->id.')';
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element." SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".$this->id;
$resql=$this->db->query($sql);
if (!$resql) $error++;
if (is_array($this->lines) && count($this->lines)>0)
{
foreach ($this->lines as $i => $val)
{
$newndfline=new ExpenseReportLine($this->db);
$newndfline=$this->lines[$i];
$newndfline->fk_expensereport=$this->id;
if ($result >= 0)
{
$result=$newndfline->insert();
}
if ($result < 0)
{
$error++;
break;
}
}
}
if (! $error)
{
$result=$this->insertExtraFields();
if ($result < 0) $error++;
}
if (! $error)
{
$result=$this->update_price();
if ($result > 0)
{
if (!$notrigger)
{
// Call trigger
$result=$this->call_trigger('EXPENSE_REPORT_CREATE',$user);
if ($result < 0) {
$error++;
}
// End call triggers
}
if (empty($error))
{
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -4;
}
}
else
{
$this->db->rollback();
return -3;
}
}
else
{
dol_syslog(get_class($this)."::create error ".$this->error, LOG_ERR);
$this->db->rollback();
return -2;
}
}
else
{
$this->error=$this->db->lasterror()." sql=".$sql;
$this->db->rollback();
return -1;
}
}
/**
* Load an object from its id and create a new one in database
*
* @param int $fk_user_author Id of new user
* @return int New id of clone
*/
function createFromClone($fk_user_author)
{
global $user,$hookmanager;
$error=0;
if (empty($fk_user_author)) $fk_user_author = $user->id;
$this->context['createfromclone'] = 'createfromclone';
$this->db->begin();
// get extrafields so they will be clone
//foreach($this->lines as $line)
//$line->fetch_optionals($line->rowid);
// Load source object
$objFrom = clone $this;
$this->id=0;
$this->ref = '';
$this->status=0;
$this->fk_statut=0;
// Clear fields
$this->fk_user_author = $fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
$this->fk_user_valid = '';
$this->date_create = '';
$this->date_creation = '';
$this->date_validation = '';
// Create clone
$result=$this->create($user);
if ($result < 0) $error++;
if (! $error)
{
// Hook of thirdparty module
if (is_object($hookmanager))
{
$parameters=array('objFrom'=>$objFrom);
$action='';
$reshook=$hookmanager->executeHooks('createFrom',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) $error++;
}
// Call trigger
$result=$this->call_trigger('EXPENSEREPORT_CLONE',$user);
if ($result < 0) $error++;
// End call triggers
}
unset($this->context['createfromclone']);
// End
if (! $error)
{
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -1;
}
}
/**
* update
*
* @param User $user User making change
* @param int $notrigger Disable triggers
* @param User $userofexpensereport New user we want to have the expense report on.
* @return int <0 if KO, >0 if OK
*/
function update($user, $notrigger = 0, $userofexpensereport=null)
{
global $langs;
$error = 0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
$sql.= " total_ht = ".$this->total_ht;
$sql.= " , total_ttc = ".$this->total_ttc;
$sql.= " , total_tva = ".$this->total_tva;
$sql.= " , date_debut = '".$this->db->idate($this->date_debut)."'";
$sql.= " , date_fin = '".$this->db->idate($this->date_fin)."'";
if ($userofexpensereport && is_object($userofexpensereport))
{
$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.
}
$sql.= " , fk_user_validator = ".($this->fk_user_validator > 0 ? $this->fk_user_validator:"null");
$sql.= " , fk_user_valid = ".($this->fk_user_valid > 0 ? $this->fk_user_valid:"null");
$sql.= " , fk_user_modif = ".$user->id;
$sql.= " , fk_statut = ".($this->fk_statut >= 0 ? $this->fk_statut:'0');
$sql.= " , fk_c_paiement = ".($this->fk_c_paiement > 0 ? $this->fk_c_paiement:"null");
$sql.= " , note_public = ".(!empty($this->note_public)?"'".$this->db->escape($this->note_public)."'":"''");
$sql.= " , note_private = ".(!empty($this->note_private)?"'".$this->db->escape($this->note_private)."'":"''");
$sql.= " , detail_refuse = ".(!empty($this->detail_refuse)?"'".$this->db->escape($this->detail_refuse)."'":"''");
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
if (!$notrigger)
{
// Call trigger
$result=$this->call_trigger('EXPENSE_REPORT_UPDATE',$user);
if ($result < 0) {
$error++;
}
// End call triggers
}
if (empty($error))
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
$this->error=$this->db->error();
return -2;
}
}
else
{
$this->db->rollback();
$this->error=$this->db->error();
return -1;
}
}
/**
* Load an object from database
*
* @param int $id Id {@min 1}
* @param string $ref Ref {@name ref}
* @return int <0 if KO, >0 if OK
*/
function fetch($id, $ref='')
{
global $conf;
$sql = "SELECT d.rowid, d.ref, d.note_public, d.note_private,"; // DEFAULT
$sql.= " d.detail_refuse, d.detail_cancel, d.fk_user_refuse, d.fk_user_cancel,"; // ACTIONS
$sql.= " d.date_refuse, d.date_cancel,"; // ACTIONS
$sql.= " d.total_ht, d.total_ttc, d.total_tva,"; // TOTAUX (int)
$sql.= " d.date_debut, d.date_fin, d.date_create, d.tms as date_modif, d.date_valid, d.date_approve,"; // DATES (datetime)
$sql.= " d.fk_user_author, d.fk_user_modif, d.fk_user_validator,";
$sql.= " d.fk_user_valid, d.fk_user_approve,";
$sql.= " d.fk_statut as status, d.fk_c_paiement,";
$sql.= " dp.libelle as libelle_paiement, dp.code as code_paiement"; // INNER JOIN paiement
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as d";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as dp ON d.fk_c_paiement = dp.id AND dp.entity IN (".getEntity('c_paiement').")";
if ($ref) $sql.= " WHERE d.ref = '".$this->db->escape($ref)."'";
else $sql.= " WHERE d.rowid = ".$id;
//$sql.= $restrict;
dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql) ;
if ($resql)
{
$obj = $this->db->fetch_object($resql);
if ($obj)
{
$this->id = $obj->rowid;
$this->ref = $obj->ref;
$this->total_ht = $obj->total_ht;
$this->total_tva = $obj->total_tva;
$this->total_ttc = $obj->total_ttc;
$this->note_public = $obj->note_public;
$this->note_private = $obj->note_private;
$this->detail_refuse = $obj->detail_refuse;
$this->detail_cancel = $obj->detail_cancel;
$this->date_debut = $this->db->jdate($obj->date_debut);
$this->date_fin = $this->db->jdate($obj->date_fin);
$this->date_valid = $this->db->jdate($obj->date_valid);
$this->date_approve = $this->db->jdate($obj->date_approve);
$this->date_create = $this->db->jdate($obj->date_create);
$this->date_modif = $this->db->jdate($obj->date_modif);
$this->date_refuse = $this->db->jdate($obj->date_refuse);
$this->date_cancel = $this->db->jdate($obj->date_cancel);
$this->fk_user_author = $obj->fk_user_author; // Note fk_user_author is not the 'author' but the guy the expense report is for.
$this->fk_user_modif = $obj->fk_user_modif;
$this->fk_user_validator = $obj->fk_user_validator;
$this->fk_user_valid = $obj->fk_user_valid;
$this->fk_user_refuse = $obj->fk_user_refuse;
$this->fk_user_cancel = $obj->fk_user_cancel;
$this->fk_user_approve = $obj->fk_user_approve;
$user_author = new User($this->db);
if ($this->fk_user_author > 0) $user_author->fetch($this->fk_user_author);
$this->user_author_infos = dolGetFirstLastname($user_author->firstname, $user_author->lastname);
$user_approver = new User($this->db);
if ($this->fk_user_validator > 0) $user_approver->fetch($this->fk_user_validator);
$this->user_validator_infos = dolGetFirstLastname($user_approver->firstname, $user_approver->lastname);
$this->fk_statut = $obj->status;
$this->status = $obj->status;
$this->fk_c_paiement = $obj->fk_c_paiement;
$this->paid = $obj->paid;
if ($this->fk_statut==5 || $this->fk_statut==6)
{
$user_valid = new User($this->db);
if ($this->fk_user_valid > 0) $user_valid->fetch($this->fk_user_valid);
$this->user_valid_infos = dolGetFirstLastname($user_valid->firstname, $user_valid->lastname);
}
$this->libelle_statut = $obj->libelle_statut;
$this->libelle_paiement = $obj->libelle_paiement;
$this->code_statut = $obj->code_statut;
$this->code_paiement = $obj->code_paiement;
$this->lines = array();
$result=$this->fetch_lines();
return $result;
}
else
{
return 0;
}
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Classify the expense report as paid
*
* @param int $id Id of expense report
* @param user $fuser User making change
* @param int $notrigger Disable triggers
* @return int <0 if KO, >0 if OK
*/
function set_paid($id, $fuser, $notrigger = 0)
{
$error = 0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."expensereport";
$sql.= " SET fk_statut = 6, paid=1";
$sql.= " WHERE rowid = ".$id." AND fk_statut = 5";
dol_syslog(get_class($this)."::set_paid sql=".$sql, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
if ($this->db->affected_rows($resql))
{
if (!$notrigger)
{
// Call trigger
$result=$this->call_trigger('EXPENSE_REPORT_PAID',$fuser);
if ($result < 0) {
$error++;
}
// End call triggers
}
if (empty($error))
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
$this->error=$this->db->error();
return -2;
}
}
else
{
$this->db->commit();
return 0;
}
}
else
{
$this->db->rollback();
dol_print_error($this->db);
return -1;
}
}
/**
* Returns the label status
*
* @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
* @return string Label
*/
function getLibStatut($mode=0)
{
return $this->LibStatut($this->status,$mode);
}
/**
* Returns the label of a statut
*
* @param int $status id statut
* @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
* @return string Label
*/
function LibStatut($status,$mode=0)
{
global $langs;
if ($mode == 0)
return $langs->transnoentities($this->statuts[$status]);
if ($mode == 1)
return $langs->transnoentities($this->statuts_short[$status]);
if ($mode == 2)
return img_picto($langs->transnoentities($this->statuts_short[$status]), $this->statuts_logo[$status]).' '.$langs->transnoentities($this->statuts_short[$status]);
if ($mode == 3)
return img_picto($langs->transnoentities($this->statuts_short[$status]), $this->statuts_logo[$status]);
if ($mode == 4)
return img_picto($langs->transnoentities($this->statuts_short[$status]),$this->statuts_logo[$status]).' '.$langs->transnoentities($this->statuts[$status]);
if ($mode == 5)
return '<span class="hideonsmartphone">'.$langs->transnoentities($this->statuts_short[$status]).' </span>'.img_picto($langs->transnoentities($this->statuts_short[$status]),$this->statuts_logo[$status]);
if ($mode == 6)
return $langs->transnoentities($this->statuts[$status]).' '.img_picto($langs->transnoentities($this->statuts_short[$status]),$this->statuts_logo[$status]);
}
/**
* Load information on object
*
* @param int $id Id of object
* @return void
*/
function info($id)
{
global $conf;
$sql = "SELECT f.rowid,";
$sql.= " f.date_create as datec,";
$sql.= " f.tms as date_modification,";
$sql.= " f.date_valid as datev,";
$sql.= " f.date_approve as datea,";
//$sql.= " f.fk_user_author as fk_user_creation,"; // This is not user of creation but user the expense is for.
$sql.= " f.fk_user_modif as fk_user_modification,";
$sql.= " f.fk_user_valid,";
$sql.= " f.fk_user_approve";
$sql.= " FROM ".MAIN_DB_PREFIX."expensereport as f";
$sql.= " WHERE f.rowid = ".$id;
$sql.= " AND f.entity = ".$conf->entity;
$resql = $this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql))
{
$obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid;
$this->date_creation = $this->db->jdate($obj->datec);
$this->date_modification = $this->db->jdate($obj->date_modification);
$this->date_validation = $this->db->jdate($obj->datev);
$this->date_approbation = $this->db->jdate($obj->datea);
$cuser = new User($this->db);
$cuser->fetch($obj->fk_user_author);
$this->user_creation = $cuser;
if ($obj->fk_user_creation)
{
$cuser = new User($this->db);
$cuser->fetch($obj->fk_user_creation);
$this->user_creation = $cuser;
}
if ($obj->fk_user_valid)
{
$vuser = new User($this->db);
$vuser->fetch($obj->fk_user_valid);
$this->user_validation = $vuser;
}
if ($obj->fk_user_modification)
{
$muser = new User($this->db);
$muser->fetch($obj->fk_user_modification);
$this->user_modification = $muser;
}
if ($obj->fk_user_approve)
{
$auser = new User($this->db);
$auser->fetch($obj->fk_user_approve);
$this->user_approve = $auser;
}
}
$this->db->free($resql);
}
else
{
dol_print_error($this->db);
}
}
/**
* Initialise an instance with random values.
* Used to build previews or test instances.
* id must be 0 if object instance is a specimen.
*
* @return void
*/
function initAsSpecimen()
{
global $user,$langs,$conf;
$now=dol_now();
// Initialise parametres
$this->id=0;
$this->ref = 'SPECIMEN';
$this->specimen=1;
$this->date_create = $now;
$this->date_debut = $now;
$this->date_fin = $now;
$this->date_valid = $now;
$this->date_approve = $now;
$type_fees_id = 2; // TF_TRIP
$this->status = 5;
$this->fk_statut = 5;
$this->fk_user_author = $user->id;
$this->fk_user_valid = $user->id;
$this->fk_user_approve = $user->id;
$this->fk_user_validator = $user->id;
$this->note_private='Private note';
$this->note_public='SPECIMEN';
$nbp = 5;
$xnbp = 0;
while ($xnbp < $nbp)
{
$line=new ExpenseReportLine($this->db);
$line->comments=$langs->trans("Comment")." ".$xnbp;
$line->date=($now-3600*(1+$xnbp));
$line->total_ht=100;
$line->total_tva=20;
$line->total_ttc=120;
$line->qty=1;
$line->vatrate=20;
$line->value_unit=120;
$line->fk_expensereport=0;
$line->type_fees_code='TRA';
$line->fk_c_type_fees=$type_fees_id;
$line->projet_ref = 'ABC';
$this->lines[$xnbp]=$line;
$xnbp++;
$this->total_ht+=$line->total_ht;
$this->total_tva+=$line->total_tva;
$this->total_ttc+=$line->total_ttc;
}
}
/**
* fetch_line_by_project
*
* @param int $projectid Project id
* @param User $user User
* @return int <0 if KO, >0 if OK
*/
function fetch_line_by_project($projectid,$user='')
{
global $conf,$db,$langs;
$langs->load('trips');
if ($user->rights->expensereport->lire) {
$sql = "SELECT de.fk_expensereport, de.date, de.comments, de.total_ht, de.total_ttc";
$sql.= " FROM ".MAIN_DB_PREFIX."expensereport_det as de";
$sql.= " WHERE de.fk_projet = ".$projectid;
dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
$result = $db->query($sql) ;
if ($result)
{
$num = $db->num_rows($result);
$i = 0;
$total_HT = 0;
$total_TTC = 0;
while ($i < $num)
{
$objp = $db->fetch_object($result);
$sql2 = "SELECT d.rowid, d.fk_user_author, d.ref, d.fk_statut";
$sql2.= " FROM ".MAIN_DB_PREFIX."expensereport as d";
$sql2.= " WHERE d.rowid = '".$objp->fk_expensereport."'";
$result2 = $db->query($sql2);
$obj = $db->fetch_object($result2);
$objp->fk_user_author = $obj->fk_user_author;
$objp->ref = $obj->ref;
$objp->fk_c_expensereport_status = $obj->fk_statut;
$objp->rowid = $obj->rowid;
$total_HT = $total_HT + $objp->total_ht;
$total_TTC = $total_TTC + $objp->total_ttc;
$author = new User($db);
$author->fetch($objp->fk_user_author);
print '<tr>';
print '<td><a href="'.DOL_URL_ROOT.'/expensereport/card.php?id='.$objp->rowid.'">'.$objp->ref_num.'</a></td>';
print '<td align="center">'.dol_print_date($objp->date,'day').'</td>';
print '<td>'.$author->getNomUrl(1).'</td>';
print '<td>'.$objp->comments.'</td>';
print '<td align="right">'.price($objp->total_ht).'</td>';
print '<td align="right">'.price($objp->total_ttc).'</td>';
print '<td align="right">';
switch($objp->fk_c_expensereport_status) {
case 4:
print img_picto($langs->trans('StatusOrderCanceled'),'statut5');
break;
case 1:
print $langs->trans('Draft').' '.img_picto($langs->trans('Draft'),'statut0');
break;
case 2:
print $langs->trans('TripForValid').' '.img_picto($langs->trans('TripForValid'),'statut3');
break;
case 5:
print $langs->trans('TripForPaid').' '.img_picto($langs->trans('TripForPaid'),'statut3');
break;
case 6:
print $langs->trans('TripPaid').' '.img_picto($langs->trans('TripPaid'),'statut4');
break;
}
/*
if ($status==4) return img_picto($langs->trans('StatusOrderCanceled'),'statut5');
if ($status==1) return img_picto($langs->trans('StatusOrderDraft'),'statut0');
if ($status==2) return img_picto($langs->trans('StatusOrderValidated'),'statut1');
if ($status==2) return img_picto($langs->trans('StatusOrderOnProcess'),'statut3');
if ($status==5) return img_picto($langs->trans('StatusOrderToBill'),'statut4');
if ($status==6) return img_picto($langs->trans('StatusOrderOnProcess'),'statut6');
*/
print '</td>';
print '</tr>';
$i++;
}
print '<tr class="liste_total"><td colspan="4">'.$langs->trans("Number").': '.$i.'</td>';
print '<td align="right" width="100">'.$langs->trans("TotalHT").' : '.price($total_HT).'</td>';
print '<td align="right" width="100">'.$langs->trans("TotalTTC").' : '.price($total_TTC).'</td>';
print '<td> </td>';
print '</tr>';
}
else
{
$this->error=$db->lasterror();
return -1;
}
}
}
/**
* recalculer
* TODO Replace this with call to update_price if not already done
*
* @param int $id Id of expense report
* @return int <0 if KO, >0 if OK
*/
function recalculer($id)
{
$sql = 'SELECT tt.total_ht, tt.total_ttc, tt.total_tva';
$sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as tt';
$sql.= ' WHERE tt.'.$this->fk_element.' = '.$id;
$total_ht = 0; $total_tva = 0; $total_ttc = 0;
$result = $this->db->query($sql);
if($result)
{
$num = $this->db->num_rows($result);
$i = 0;
while ($i < $num):
$objp = $this->db->fetch_object($result);
$total_ht+=$objp->total_ht;
$total_tva+=$objp->total_tva;
$i++;
endwhile;
$total_ttc = $total_ht + $total_tva;
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
$sql.= " total_ht = ".$total_ht;
$sql.= " , total_ttc = ".$total_ttc;
$sql.= " , total_tva = ".$total_tva;
$sql.= " WHERE rowid = ".$id;
$result = $this->db->query($sql);
if($result):
$this->db->free($result);
return 1;
else:
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::recalculer: Error ".$this->error,LOG_ERR);
return -3;
endif;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::recalculer: Error ".$this->error,LOG_ERR);
return -3;
}
}
/**
* fetch_lines
*
* @return int <0 if OK, >0 if KO
*/
function fetch_lines()
{
$this->lines=array();
$sql = ' SELECT de.rowid, de.comments, de.qty, de.value_unit, de.date, de.rang,';
$sql.= ' de.'.$this->fk_element.', de.fk_c_type_fees, de.fk_c_exp_tax_cat, de.fk_projet, de.tva_tx,';
$sql.= ' de.total_ht, de.total_tva, de.total_ttc,';
$sql.= ' ctf.code as code_type_fees, ctf.label as libelle_type_fees,';
$sql.= ' p.ref as ref_projet, p.title as title_projet';
$sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line.' as de';
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_type_fees as ctf ON de.fk_c_type_fees = ctf.id';
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'projet as p ON de.fk_projet = p.rowid';
$sql.= ' WHERE de.'.$this->fk_element.' = '.$this->id;
if (! empty($conf->global->EXPENSEREPORT_LINES_SORTED_BY_ROWID))
{
$sql.= ' ORDER BY de.rang ASC, de.rowid ASC';
}
else
{
$sql.= ' ORDER BY de.rang ASC, de.date ASC';
}
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$objp = $this->db->fetch_object($resql);
$deplig = new ExpenseReportLine($this->db);
$deplig->rowid = $objp->rowid;
$deplig->id = $objp->id;
$deplig->comments = $objp->comments;
$deplig->qty = $objp->qty;
$deplig->value_unit = $objp->value_unit;
$deplig->date = $objp->date;
$deplig->dates = $this->db->jdate($objp->date);
$deplig->fk_expensereport = $objp->fk_expensereport;
$deplig->fk_c_type_fees = $objp->fk_c_type_fees;
$deplig->fk_c_exp_tax_cat = $objp->fk_c_exp_tax_cat;
$deplig->fk_projet = $objp->fk_projet;
$deplig->total_ht = $objp->total_ht;
$deplig->total_tva = $objp->total_tva;
$deplig->total_ttc = $objp->total_ttc;
$deplig->type_fees_code = empty($objp->code_type_fees)?'TF_OTHER':$objp->code_type_fees;
$deplig->type_fees_libelle = $objp->libelle_type_fees;
$deplig->tva_tx = $objp->tva_tx;