forked from anuko/timetracker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathttReportHelper.class.php
More file actions
2883 lines (2652 loc) · 136 KB
/
ttReportHelper.class.php
File metadata and controls
2883 lines (2652 loc) · 136 KB
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) Anuko International Ltd. https://www.anuko.com
License: See license.txt */
import('ttClientHelper');
import('ttDate');
import('ttPeriod');
import('ttTimeHelper');
import('ttConfigHelper');
require_once(APP_PLUGINS_DIR . '/CustomFields.class.php');
// Definitions of types for timesheet dropdown.
define('TIMESHEET_ALL', 0); // Include all records.
define('TIMESHEET_NOT_ASSIGNED', 1); // Include records not assigned to timesheets.
define('TIMESHEET_ASSIGNED', 2); // Include records assigned to timesheets.
define('TIMESHEET_PENDING', 3); // Include records in submitted timesheets that are pending manager approval.
define('TIMESHEET_APPROVED', 4); // Include records in approved timesheets.
define('TIMESHEET_NOT_APPROVED', 5); // Include records in disapproved timesheets.
// Class ttReportHelper is used for help with reports.
class ttReportHelper {
// getWhere prepares a WHERE clause for a report query.
static function getWhere($options) {
global $user;
$mdb2 = getConnection();
$group_id = $user->getGroup();
$org_id = $user->org_id;
if ($user->isPluginEnabled('cf')) {
global $custom_fields;
if (!$custom_fields) $custom_fields = new CustomFields();
}
// A shortcut for timesheets.
if (isset($options['timesheet_id']) && $options['timesheet_id']) {
$where = " where l.timesheet_id = ".$options['timesheet_id']." and l.group_id = $group_id and l.org_id = $org_id";
return $where;
}
// Prepare dropdown parts.
$dropdown_parts = '';
if ($options['client_id'])
$dropdown_parts .= ' and l.client_id = '.$options['client_id'];
elseif ($user->isClient() && $user->client_id)
$dropdown_parts .= ' and l.client_id = '.$user->client_id;
if (isset($options['project_ids']))
$dropdown_parts .= ' and l.project_id in ('.$options['project_ids'].')';
// if ($options['project_id']) $dropdown_parts .= ' and l.project_id = '.$options['project_id']; // This was here for a single select.
if ($options['task_id']) $dropdown_parts .= ' and l.task_id = '.$options['task_id'];
if ($options['billable']==1) $dropdown_parts .= ' and l.billable = 1';
if ($options['billable']==2) $dropdown_parts .= ' and l.billable = 0';
if ($options['invoice']==1) $dropdown_parts .= ' and l.invoice_id is not null';
if ($options['invoice']==2) $dropdown_parts .= ' and l.invoice_id is null';
if ($options['timesheet']==TIMESHEET_NOT_ASSIGNED) $dropdown_parts .= ' and l.timesheet_id is null';
if ($options['timesheet']==TIMESHEET_ASSIGNED) $dropdown_parts .= ' and l.timesheet_id is not null';
if ($options['approved']==1) $dropdown_parts .= ' and l.approved = 1';
if ($options['approved']==2) $dropdown_parts .= ' and l.approved = 0';
if ($options['paid_status']==1) $dropdown_parts .= ' and l.paid = 1';
if ($options['paid_status']==2) $dropdown_parts .= ' and l.paid = 0';
// Add time custom fields.
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
$field_value = $options[$field_name];
if ($timeField['type'] == CustomFields::TYPE_DROPDOWN && $field_value) {
$cfoTable = 'cfo'.$timeField['id'];
$dropdown_parts .= " and $cfoTable.id = $field_value";
}
}
}
// Prepare part for text custom fields using LIKE operator.
$cf_text_parts = null;
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
$field_value = $options[$field_name];
if ($timeField['type'] == CustomFields::TYPE_TEXT && $field_value) {
$cflTableName = 'cfl'.$timeField['id'];
$cf_text_parts .= " and $cflTableName.value like ".$mdb2->quote("%$field_value%");
}
}
}
// Add user custom fields.
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$field_value = $options[$field_name];
if ($userField['type'] == CustomFields::TYPE_DROPDOWN && $field_value) {
$cfoTable = 'cfo'.$userField['id'];
$dropdown_parts .= " and $cfoTable.id = $field_value";
}
}
}
// Continue preparing part for text custom fields using LIKE operator.
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$field_value = $options[$field_name];
if ($userField['type'] == CustomFields::TYPE_TEXT && $field_value) {
$ecfTableName = 'ecf'.$userField['id'];
$cf_text_parts .= " and $ecfTableName.value like ".$mdb2->quote("%$field_value%");
}
}
}
// Add project custom fields.
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$field_value = $options[$field_name];
if ($projectField['type'] == CustomFields::TYPE_DROPDOWN && $field_value) {
$cfoTable = 'cfo'.$projectField['id'];
$dropdown_parts .= " and $cfoTable.id = $field_value";
}
}
}
// Continue preparing part for text custom fields using LIKE operator.
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$field_value = $options[$field_name];
if ($projectField['type'] == CustomFields::TYPE_TEXT && $field_value) {
$ecfTableName = 'ecf'.$projectField['id'];
$cf_text_parts .= " and $ecfTableName.value like ".$mdb2->quote("%$field_value%");
}
}
}
// Prepare sql query part for user list.
$userlist = isset($options['users']) ? $options['users'] : '-1';
if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
$user_list_part = " and l.user_id in ($userlist)";
else
$user_list_part = " and l.user_id = ".$user->getUser();
$user_list_part .= " and l.group_id = $group_id and l.org_id = $org_id";
// Prepare part for note_containing using LIKE operator.
$note_containing_part = '';
if (isset($options['note_containing']) && !empty($options['note_containing'])) {
$note_containing_part = " and l.comment like ".$mdb2->quote('%'.$options['note_containing'].'%');
}
// Prepare sql query part for where.
$dateFormat = $user->getDateFormat();
if (isset($options['period']) && $options['period'])
$period = new ttPeriod(new ttDate(), $options['period']);
else {
$period = new ttPeriod(new ttDate());
$period->setPeriod(
new ttDate($options['period_start'], $dateFormat),
new ttDate($options['period_end'], $dateFormat));
}
$where = " where l.status = 1 and l.date >= '".$period->getStartDate()."' and l.date <= '".$period->getEndDate()."'".
$user_list_part.$dropdown_parts.$cf_text_parts.$note_containing_part;
return $where;
}
// getExpenseWhere prepares WHERE clause for expenses query in a report.
static function getExpenseWhere($options) {
global $user;
$mdb2 = getConnection();
$group_id = $user->getGroup();
$org_id = $user->org_id;
if ($user->isPluginEnabled('cf')) {
global $custom_fields;
if (!isset($custom_fields)) $custom_fields = new CustomFields();
}
// Prepare dropdown parts.
$dropdown_parts = '';
if ($options['client_id'])
$dropdown_parts .= ' and ei.client_id = '.$options['client_id'];
elseif ($user->isClient() && $user->client_id)
$dropdown_parts .= ' and ei.client_id = '.$user->client_id;
if (isset($options['project_ids']))
$dropdown_parts .= ' and ei.project_id in ('.$options['project_ids'].')';
// if ($options['project_id']) $dropdown_parts .= ' and l.project_id = '.$options['project_id']; // This was here for a single select.
if ($options['invoice']==1) $dropdown_parts .= ' and ei.invoice_id is not null';
if ($options['invoice']==2) $dropdown_parts .= ' and ei.invoice_id is null';
if (isset($options['timesheet']) && ($options['timesheet']!=TIMESHEET_ALL && $options['timesheet']!=TIMESHEET_NOT_ASSIGNED)) {
$dropdown_parts .= ' and 0 = 1'; // Expense items do not have a timesheet_id.
}
if ($options['approved']==1) $dropdown_parts .= ' and ei.approved = 1';
if ($options['approved']==2) $dropdown_parts .= ' and ei.approved = 0';
if ($options['paid_status']==1) $dropdown_parts .= ' and ei.paid = 1';
if ($options['paid_status']==2) $dropdown_parts .= ' and ei.paid = 0';
// Not adding conditions for time custom fields by design because expenses are not associated with them.
// Whether or not this is proper, we'll know eventually if users complain.
// This means that filtering by time custom fields applies only to time items, not expenses.
// Add user custom fields.
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$field_value = $options[$field_name];
if ($userField['type'] == CustomFields::TYPE_DROPDOWN && $field_value) {
$cfoTable = 'cfo'.$userField['id'];
$dropdown_parts .= " and $cfoTable.id = $field_value";
}
}
}
// Prepare part for text custom fields using LIKE operator.
$cf_text_parts = null;
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$field_value = $options[$field_name];
if ($userField['type'] == CustomFields::TYPE_TEXT && $field_value) {
$ecfTableName = 'ecf'.$userField['id'];
$cf_text_parts .= " and $ecfTableName.value like ".$mdb2->quote("%$field_value%");
}
}
}
// Add project custom fields.
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$field_value = $options[$field_name];
if ($projectField['type'] == CustomFields::TYPE_DROPDOWN && $field_value) {
$cfoTable = 'cfo'.$projectField['id'];
$dropdown_parts .= " and $cfoTable.id = $field_value";
}
}
}
// Continue preparing parts for text custom fields using LIKE operator.
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$field_value = $options[$field_name];
if ($projectField['type'] == CustomFields::TYPE_TEXT && $field_value) {
$ecfTableName = 'ecf'.$projectField['id'];
$cf_text_parts .= " and $ecfTableName.value like ".$mdb2->quote("%$field_value%");
}
}
}
// Prepare sql query part for user list.
$userlist = isset($options['users']) ? $options['users'] : '-1';
if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
$user_list_part = " and ei.user_id in ($userlist)";
else
$user_list_part = " and ei.user_id = ".$user->getUser();
$user_list_part .= " and ei.group_id = $group_id and ei.org_id = $org_id";
// Prepare part for note_containing using LIKE operator.
$note_containing_part = '';
if (isset($options['note_containing']) && !empty($options['note_containing'])) {
$note_containing_part = " and ei.name like ".$mdb2->quote('%'.$options['note_containing'].'%');
}
// Prepare sql query part for where.
$dateFormat = $user->getDateFormat();
if (isset($options['period']) && $options['period'])
$period = new ttPeriod(new ttDate(), $options['period']);
else {
$period = new ttPeriod(new ttDate());
$period->setPeriod(
new ttDate($options['period_start'], $dateFormat),
new ttDate($options['period_end'], $dateFormat));
}
$where = " where ei.status = 1 and ei.date >= '".$period->getStartDate()."' and ei.date <= '".$period->getEndDate()."'".
$user_list_part.$dropdown_parts.$cf_text_parts.$note_containing_part;
return $where;
}
// getItems retrieves all items associated with a report.
// It combines tt_log and tt_expense_items in one array for presentation in one table using mysql union all.
// Expense items use the "note" field for item name.
static function getItems($options) {
global $user;
$mdb2 = getConnection();
$group_id = $user->getGroup();
$org_id = $user->org_id;
// Determine these once as they are used in multiple places in this function.
$canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
$isClient = $user->isClient();
if ($user->isPluginEnabled('cf')) {
global $custom_fields;
if (!$custom_fields) $custom_fields = new CustomFields();
}
$grouping = ttReportHelper::grouping($options);
$grouping_by_date = $grouping_by_client = $grouping_by_project = $grouping_by_task = $grouping_by_user = false;
if ($grouping) {
$grouping_by_date = ttReportHelper::groupingBy('date', $options);
$grouping_by_client = ttReportHelper::groupingBy('client', $options);
$grouping_by_project = ttReportHelper::groupingBy('project', $options);
$grouping_by_task = ttReportHelper::groupingBy('task', $options);
$grouping_by_user = ttReportHelper::groupingBy('user', $options);
}
$convertTo12Hour = ('%I:%M %p' == $user->getTimeFormat()) && ($options['show_start'] || $options['show_end']);
$trackingMode = $user->getTrackingMode();
$decimalMark = $user->getDecimalMark();
// Prepare a query for time items in tt_log table.
$fields = array(); // An array of fields for database query.
array_push($fields, 'l.id');
array_push($fields, 'l.user_id');
array_push($fields, '1 as type'); // Type 1 is for tt_log entries.
array_push($fields, 'l.date');
if($canViewReports || $isClient)
array_push($fields, 'u.name as user');
// Add user custom fields.
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$checkbox_field_name = 'show_'.$field_name;
if ($options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
if ($userField['type'] == CustomFields::TYPE_TEXT) {
$ecfTableName = 'ecf'.$userField['id'];
array_push($fields, "$ecfTableName.value as $field_name");
} elseif ($userField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTableName = 'cfo'.$userField['id'];
array_push($fields, "$cfoTableName.value as $field_name");
}
}
}
}
// Add client name if it is selected.
if ($options['show_client'] || $grouping_by_client)
array_push($fields, 'c.name as client');
// Add project name if it is selected.
if ($options['show_project'] || $grouping_by_project)
array_push($fields, 'p.name as project');
// Add task name if it is selected.
if ($options['show_task'] || $grouping_by_task)
array_push($fields, 't.name as task');
// Add time custom fields.
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
$checkbox_field_name = 'show_'.$field_name;
if ($options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
if ($timeField['type'] == CustomFields::TYPE_TEXT) {
$cflTable = 'cfl'.$timeField['id'];
array_push($fields, "$cflTable.value as $field_name");
} elseif ($timeField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTable = 'cfo'.$timeField['id'];
array_push($fields, "$cfoTable.value as $field_name");
}
}
}
}
// Add project custom fields.
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$checkbox_field_name = 'show_'.$field_name;
if ($options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
if ($projectField['type'] == CustomFields::TYPE_TEXT) {
$ecfTableName = 'ecf'.$projectField['id'];
array_push($fields, "$ecfTableName.value as $field_name");
} elseif ($projectField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTableName = 'cfo'.$projectField['id'];
array_push($fields, "$cfoTableName.value as $field_name");
}
}
}
}
// Add start time.
if ($options['show_start']) {
array_push($fields, "l.start as unformatted_start");
array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start");
}
// Add finish time.
if ($options['show_end'])
array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish");
// Add duration.
if ($options['show_duration'])
array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
// Add work units.
if ($options['show_work_units']) {
if ($user->getConfigOption('unit_totals_only'))
array_push($fields, "null as units");
else {
$firstUnitThreshold = $user->getConfigInt('1st_unit_threshold', 0);
$minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
array_push($fields, "if(l.billable = 0 or time_to_sec(l.duration)/60 < $firstUnitThreshold, 0, ceil(time_to_sec(l.duration)/60/$minutesInUnit)) as units");
}
}
// Add note.
if ($options['show_note'])
array_push($fields, 'l.comment as note');
// Handle cost.
$includeCost = $options['show_cost'];
if ($includeCost) {
$includeCostPerHour = $user->getConfigOption('report_cost_per_hour');
if (MODE_TIME == $trackingMode) {
if ($includeCostPerHour)
array_push($fields, "cast(l.billable * coalesce(u.rate, 0) as decimal(10,2)) as cost_per_hour"); // Use default user rate.
array_push($fields, "cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2)) as cost"); // Use default user rate.
} else {
if ($includeCostPerHour)
array_push($fields, "cast(l.billable * coalesce(upb.rate, 0) as decimal(10,2)) as cost_per_hour"); // Use project rate for user.
array_push($fields, "cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2)) as cost"); // Use project rate for user.
}
array_push($fields, "null as expense");
}
// Add the fields used to determine if we show an edit icon for record.
array_push($fields, 'l.approved');
array_push($fields, 'l.timesheet_id');
array_push($fields, 'l.invoice_id');
// Add paid status.
if ($canViewReports && $options['show_paid'])
array_push($fields, 'l.paid');
// Add IP address.
if ($canViewReports && $options['show_ip']) {
array_push($fields, 'l.created');
array_push($fields, 'l.created_ip');
array_push($fields, 'l.modified');
array_push($fields, 'l.modified_ip');
}
// Add invoice name if it is selected.
if (($canViewReports || $isClient) && $options['show_invoice'])
array_push($fields, 'i.name as invoice');
// Add timesheet name if it is selected.
if ($options['show_timesheet'])
array_push($fields, 'ts.name as timesheet_name');
// Add has_files.
if ($options['show_files'])
array_push($fields, 'if(Sub1.entity_id is null, 0, 1) as has_files');
// Prepare sql query part for left joins.
$left_joins = null;
// Left joins for user custom fields.
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$checkbox_field_name = 'show_'.$field_name;
$entity_type = CustomFields::ENTITY_USER;
if ($options[$field_name] || $options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
$ecfTable = 'ecf'.$userField['id'];
if ($userField['type'] == CustomFields::TYPE_TEXT) {
// Add one join for each text field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = l.user_id and $ecfTable.field_id = ".$userField['id'].")";
} elseif ($userField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTable = 'cfo'.$userField['id'];
// Add two joins for each dropdown field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = l.user_id and $ecfTable.field_id = ".$userField['id'].")";
$left_joins .= " left join tt_custom_field_options $cfoTable on ($cfoTable.field_id = $ecfTable.field_id and $cfoTable.id = $ecfTable.option_id)";
}
}
}
}
// Left joins for project custom fields.
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$checkbox_field_name = 'show_'.$field_name;
$entity_type = CustomFields::ENTITY_PROJECT;
if ($options[$field_name] || $options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
$ecfTable = 'ecf'.$projectField['id'];
if ($projectField['type'] == CustomFields::TYPE_TEXT) {
// Add one join for each text field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = l.project_id and $ecfTable.field_id = ".$projectField['id'].")";
} elseif ($projectField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTable = 'cfo'.$projectField['id'];
// Add two joins for each dropdown field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = l.project_id and $ecfTable.field_id = ".$projectField['id'].")";
$left_joins .= " left join tt_custom_field_options $cfoTable on ($cfoTable.field_id = $ecfTable.field_id and $cfoTable.id = $ecfTable.option_id)";
}
}
}
}
if ($options['show_client'] || $grouping_by_client)
$left_joins .= " left join tt_clients c on (c.id = l.client_id)";
if (($canViewReports || $isClient) && $options['show_invoice'])
$left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
$left_joins .= " left join tt_users u on (u.id = l.user_id)";
if ($options['show_project'] || $grouping_by_project)
$left_joins .= " left join tt_projects p on (p.id = l.project_id)";
if ($options['show_task'] || $grouping_by_task)
$left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
// Left joins for time custom fields.
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
$checkbox_field_name = 'show_'.$field_name;
if ($options[$field_name] || $options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
$cflTable = 'cfl'.$timeField['id'];
if ($timeField['type'] == CustomFields::TYPE_TEXT) {
// Add one join for each text field.
$left_joins .= " left join tt_custom_field_log $cflTable on ($cflTable.log_id = l.id and $cflTable.status = 1 and $cflTable.field_id = ".$timeField['id'].')';
} elseif ($timeField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTable = 'cfo'.$timeField['id'];
// Add two joins for each dropdown field.
$left_joins .= " left join tt_custom_field_log $cflTable on ($cflTable.log_id = l.id and $cflTable.status = 1 and $cflTable.field_id = ".$timeField['id'].')';
$left_joins .= " left join tt_custom_field_options $cfoTable on ($cfoTable.field_id = $cflTable.field_id and $cfoTable.id = $cflTable.option_id)";
}
}
}
}
if ($includeCost && MODE_TIME != $trackingMode)
$left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
if ($options['show_files']) {
$left_joins .= " left join (select distinct entity_id from tt_files".
" where entity_type = 'time' and group_id = $group_id and org_id = $org_id and status = 1) Sub1".
" on (l.id = Sub1.entity_id)";
}
// Prepare sql query part for inner joins.
$inner_joins = null;
if ($user->isPluginEnabled('ts')) {
$timesheet_option = $options['timesheet'];
if ($timesheet_option == TIMESHEET_PENDING)
$inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.submit_status = 1 and ts.approve_status is null)";
else if ($timesheet_option == TIMESHEET_APPROVED)
$inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approve_status = 1)";
else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
$inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approve_status = 0)";
else if ($options['show_timesheet'])
$inner_joins .= " left join tt_timesheets ts on (l.timesheet_id = ts.id)"; // Left join for timesheet nme.
}
$where = ttReportHelper::getWhere($options);
// Construct sql query for tt_log items.
$sql = "select ".join(', ', $fields)." from tt_log l $left_joins $inner_joins $where";
// If we don't have expense items (such as when the Expenses plugin is disabled), the above is all sql we need,
// with an exception of sorting part, that is added in the end.
// However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
if ($options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
$fields = array(); // An array of fields for database query.
array_push($fields, 'ei.id');
array_push($fields, 'ei.user_id');
array_push($fields, '2 as type'); // Type 2 is for tt_expense_items entries.
array_push($fields, 'ei.date');
if($canViewReports || $isClient)
array_push($fields, 'u.name as user');
// Add user custom fields.
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$checkbox_field_name = 'show_'.$field_name;
if ($options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
if ($userField['type'] == CustomFields::TYPE_TEXT) {
$ecfTableName = 'ecf'.$userField['id'];
array_push($fields, "$ecfTableName.value as $field_name");
} elseif ($userField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTableName = 'cfo'.$userField['id'];
array_push($fields, "$cfoTableName.value as $field_name");
}
}
}
}
// Add client name if it is selected.
if ($options['show_client'] || $grouping_by_client)
array_push($fields, 'c.name as client');
// Add project name if it is selected.
if ($options['show_project'] || $grouping_by_project)
array_push($fields, 'p.name as project');
if ($options['show_task'] || $grouping_by_task)
array_push($fields, 'null'); // null for task name. We need to match column count for union.
// Add null values for time custom fields.
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
$checkbox_field_name = 'show_'.$field_name;
if ($options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
array_push($fields, "null as $field_name"); // null for each time custom field.
}
}
}
// Add project custom fields.
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$checkbox_field_name = 'show_'.$field_name;
if ($options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
if ($projectField['type'] == CustomFields::TYPE_TEXT) {
$ecfTableName = 'ecf'.$projectField['id'];
array_push($fields, "$ecfTableName.value as $field_name");
} elseif ($projectField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTableName = 'cfo'.$projectField['id'];
array_push($fields, "$cfoTableName.value as $field_name");
}
}
}
}
if ($options['show_start']) {
array_push($fields, 'null'); // null for unformatted_start.
array_push($fields, 'null'); // null for start.
}
if ($options['show_end'])
array_push($fields, 'null'); // null for finish.
if ($options['show_duration'])
array_push($fields, 'null'); // null for duration.
if ($options['show_work_units'])
array_push($fields, 'null as units'); // null for work units.
// Use the note field to print item name.
if ($options['show_note'])
array_push($fields, 'ei.name as note');
if ($user->getConfigOption('report_cost_per_hour'))
array_push($fields, 'null as cost_per_hour'); // null for cost_per_hour.
array_push($fields, 'ei.cost as cost');
array_push($fields, 'ei.cost as expense');
// Add the fields used to determine if we show an edit icon for record.
array_push($fields, 'ei.approved');
array_push($fields, 'null as timesheet_id');
array_push($fields, 'ei.invoice_id');
// Add paid status.
if ($canViewReports && $options['show_paid'])
array_push($fields, 'ei.paid');
// Add IP address.
if ($canViewReports && $options['show_ip']) {
array_push($fields, 'ei.created');
array_push($fields, 'ei.created_ip');
array_push($fields, 'ei.modified');
array_push($fields, 'ei.modified_ip');
}
// Add invoice name if it is selected.
if (($canViewReports || $isClient) && $options['show_invoice'])
array_push($fields, 'i.name as invoice');
if ($options['show_timesheet'])
array_push($fields, 'null as timesheet_name');
// Add has_files.
if ($options['show_files'])
array_push($fields, 'if(Sub1.entity_id is null, 0, 1) as has_files');
// Prepare sql query part for left joins.
$left_joins = null;
// Left joins for user custom fields.
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$checkbox_field_name = 'show_'.$field_name;
$entity_type = CustomFields::ENTITY_USER;
if ($options[$field_name] || $options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
$ecfTable = 'ecf'.$userField['id'];
if ($userField['type'] == CustomFields::TYPE_TEXT) {
// Add one join for each text field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = ei.user_id and $ecfTable.field_id = ".$userField['id'].")";
} elseif ($userField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTable = 'cfo'.$userField['id'];
// Add two joins for each dropdown field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = ei.user_id and $ecfTable.field_id = ".$userField['id'].")";
$left_joins .= " left join tt_custom_field_options $cfoTable on ($cfoTable.field_id = $ecfTable.field_id and $cfoTable.id = $ecfTable.option_id)";
}
}
}
}
// Left joins for project custom fields.
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$checkbox_field_name = 'show_'.$field_name;
$entity_type = CustomFields::ENTITY_PROJECT;
if ($options[$field_name] || $options[$checkbox_field_name] || ttReportHelper::groupingBy($field_name, $options)) {
$ecfTable = 'ecf'.$projectField['id'];
if ($projectField['type'] == CustomFields::TYPE_TEXT) {
// Add one join for each text field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = ei.project_id and $ecfTable.field_id = ".$projectField['id'].")";
} elseif ($projectField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTable = 'cfo'.$projectField['id'];
// Add two joins for each dropdown field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = ei.project_id and $ecfTable.field_id = ".$projectField['id'].")";
$left_joins .= " left join tt_custom_field_options $cfoTable on ($cfoTable.field_id = $ecfTable.field_id and $cfoTable.id = $ecfTable.option_id)";
}
}
}
}
if ($canViewReports || $isClient)
$left_joins .= " left join tt_users u on (u.id = ei.user_id)";
if ($options['show_client'] || $grouping_by_client)
$left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
if ($options['show_project'] || $grouping_by_project)
$left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
if (($canViewReports || $isClient) && $options['show_invoice'])
$left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
if ($options['show_files']) {
$left_joins .= " left join (select distinct entity_id from tt_files".
" where entity_type = 'expense' and group_id = $group_id and org_id = $org_id and status = 1) Sub1".
" on (ei.id = Sub1.entity_id)";
}
$where = ttReportHelper::getExpenseWhere($options);
// Construct sql query for expense items.
$sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
// Construct a union.
$sql = "($sql) union all ($sql_for_expense_items)";
}
// Determine sort part.
$sort_part = ' order by ';
if ($grouping) {
$sort_part2 = '';
$sort_part2 .= ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') ? ', '.$options['group_by1'] : '';
$sort_part2 .= ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') ? ', '.$options['group_by2'] : '';
$sort_part2 .= ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') ? ', '.$options['group_by3'] : '';
if (!$grouping_by_date) $sort_part2 .= ', date';
$sort_part .= ltrim($sort_part2, ', '); // Remove leading comma and space.
} else {
$sort_part .= 'date';
}
if (($canViewReports || $isClient) && isset($options['users']) && !$grouping_by_user)
$sort_part .= ', user, type';
if ($options['show_start'])
$sort_part .= ', unformatted_start';
$sort_part .= ', id';
$sql .= $sort_part;
// By now we are ready with sql.
// Obtain items for report.
$res = $mdb2->query($sql);
if (is_a($res, 'PEAR_Error')) die($res->getMessage());
$report_items = array();
while ($val = $res->fetchRow()) {
if ($convertTo12Hour) {
if($val['start'] != '')
$val['start'] = ttTimeHelper::to12HourFormat($val['start']);
if($val['finish'] != '')
$val['finish'] = ttTimeHelper::to12HourFormat($val['finish']);
}
if (isset($val['cost_per_hour'])) {
if ('.' != $decimalMark)
$val['cost_per_hour'] = str_replace('.', $decimalMark, $val['cost_per_hour']);
}
if (isset($val['cost'])) {
if ('.' != $decimalMark)
$val['cost'] = str_replace('.', $decimalMark, $val['cost']);
}
if (isset($val['expense'])) {
if ('.' != $decimalMark)
$val['expense'] = str_replace('.', $decimalMark, $val['expense']);
}
if ($grouping) $val['grouped_by'] = ttReportHelper::makeGroupByKey($options, $val);
$val['date'] = ttDateToUserFormat($val['date']);
$report_items[] = $val;
}
return $report_items;
}
// putInSession stores tt_log and tt_expense_items ids from a report in user session
// as 2 comma-separated lists.
static function putInSession($report_items) {
unset($_SESSION['report_item_ids']);
unset($_SESSION['report_item_expense_ids']);
if (is_array($report_items)) {
// Iterate through records and build 2 comma-separated lists.
$report_item_ids = '';
$report_item_expense_ids = '';
foreach($report_items as $item) {
if ($item['type'] == 1)
$report_item_ids .= ','.$item['id'];
else if ($item['type'] == 2)
$report_item_expense_ids .= ','.$item['id'];
}
$report_item_ids = trim($report_item_ids, ',');
$report_item_expense_ids = trim($report_item_expense_ids, ',');
// The lists are ready. Put them in session.
if ($report_item_ids) $_SESSION['report_item_ids'] = $report_item_ids;
if ($report_item_expense_ids) $_SESSION['report_item_expense_ids'] = $report_item_expense_ids;
}
}
// getFromSession obtains tt_log and tt_expense_items ids stored in user session.
static function getFromSession() {
$items = array();
$report_item_ids = @$_SESSION['report_item_ids'];
if ($report_item_ids)
$items['report_item_ids'] = explode(',', $report_item_ids);
$report_item_expense_ids = @$_SESSION['report_item_expense_ids'];
if ($report_item_expense_ids)
$items['report_item_expense_ids'] = explode(',', $report_item_expense_ids);
return $items;
}
// getSubtotals calculates report items subtotals when a report is grouped by.
// Without expenses, it's a simple select with group by.
// With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
// getSubtotals uses helper functions such as makeConcatPart and others to get parts to assemble an sql query from.
// Query may become quite complex depending on $options, whether custom fields are used, etc.
// This is the reason for having individual functions for parts (to keep getSubtotals easier to manage).
static function getSubtotals($options) {
global $user;
$mdb2 = getConnection();
$concat_part = ttReportHelper::makeConcatPart($options);
$group_by_fields_part = ttReportHelper::makeGroupByFieldsPart($options);
$work_unit_part = ttReportHelper::makeWorkUnitPart($options);
$join_part = ttReportHelper::makeJoinPart($options);
$cost_part = ttReportHelper::makeCostPart($options);
$where = ttReportHelper::getWhere($options);
$group_by_part = ttReportHelper::makeGroupByPart($options);
$parts = $concat_part.$group_by_fields_part.", sum(time_to_sec(l.duration)) as time, null as expenses".$work_unit_part.$cost_part;
$sql = "select $parts from tt_log l $join_part $where $group_by_part";
// By now we have sql for time items.
// However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
if (isset($options['show_cost']) && $options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
$concat_part = ttReportHelper::makeConcatExpensesPart($options);
$group_by_fields_part = ttReportHelper::makeGroupByFieldsExpensesPart($options);
$join_part = ttReportHelper::makeJoinExpensesPart($options);
$where = ttReportHelper::getExpenseWhere($options);
$group_by_expenses_part = ttReportHelper::makeGroupByExpensesPart($options);
$parts = $concat_part.$group_by_fields_part.", null as time";
if ($options['show_work_units']) $parts .= ", null as units";
$parts .= ", sum(ei.cost) as cost, sum(ei.cost) as expenses";
$sql_for_expenses = "select $parts from tt_expense_items ei $join_part $where $group_by_expenses_part";
// Create a combined query.
$fields = ttReportHelper::makeCombinedSelectPart($options);
$combined = "select $fields, sum(time) as time";
if ($options['show_work_units']) $combined .= ", sum(units) as units";
$combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by $fields";
$sql = $combined;
}
// Execute query.
$res = $mdb2->query($sql);
if (is_a($res, 'PEAR_Error')) die($res->getMessage());
$subtotals = array();
while ($val = $res->fetchRow()) {
$time = ttTimeHelper::minutesToDuration($val['time'] / 60);
$rowLabel = ttReportHelper::makeGroupByLabel($val['group_field'], $options);
if (isset($options['show_cost']) && $options['show_cost']) {
$decimalMark = $user->getDecimalMark();
if ('.' != $decimalMark) {
$val['cost'] = str_replace('.', $decimalMark, $val['cost']);
$val['expenses'] = str_replace('.', $decimalMark, $val['expenses']);
}
$subtotals[$val['group_field']] = array('name'=>$rowLabel,
'user'=>isset($val['user']) ? $val['user'] : null,
'project'=>isset($val['project']) ? $val['project'] : null,
'task'=>isset($val['task']) ? $val['task'] : null,
'client'=>isset($val['client']) ? $val['client'] : null,
'time'=>$time,
'units'=>isset($val['units']) ? $val['units'] : null,
'cost'=>$val['cost'],
'expenses'=>$val['expenses']);
} else {
$subtotals[$val['group_field']] = array('name'=>$rowLabel,
'user'=>isset($val['user']) ? $val['user'] : null,
'project'=>isset($val['project']) ? $val['project'] : null,
'task'=>isset($val['task']) ? $val['task'] : null,
'client'=>isset($val['client']) ? $val['client'] : null,
'time'=>$time,
'units'=>isset($val['units']) ? $val['units'] : null);
}
}
return $subtotals;
}
// getTotals calculates total hours and cost for all report items.
static function getTotals($options)
{
global $user;
$mdb2 = getConnection();
if ($user->isPluginEnabled('cf')) {
global $custom_fields;
if (!isset($custom_fields)) $custom_fields = new CustomFields();
}
$trackingMode = $user->getTrackingMode();
$decimalMark = $user->getDecimalMark();
$where = ttReportHelper::getWhere($options);
// Prepare parts.
$time_part = $units_part = $cost_part = '';
$time_part = "sum(time_to_sec(l.duration)) as time";
if (isset($options['show_work_units']) && $options['show_work_units']) {
$unitTotalsOnly = $user->getConfigOption('unit_totals_only');
$firstUnitThreshold = $user->getConfigInt('1st_unit_threshold', 0);
$minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
$units_part = $unitTotalsOnly ? ", null as units" : ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $firstUnitThreshold, 0, ceil(time_to_sec(l.duration)/60/$minutesInUnit))) as units";
}
if (isset($options['show_cost']) && $options['show_cost']) {
if (MODE_TIME == $trackingMode)
$cost_part = ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost, null as expenses";
else
$cost_part = ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost, null as expenses";
} else {
$cost_part = ", null as cost, null as expenses";
}
// Prepare left joins.
$left_joins = null;
// Left joins for custom fields.
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
$field_value = isset($options[$field_name]) ? $options[$field_name] : false;
if ($field_value) {
$cflTable = 'cfl'.$timeField['id'];
if ($timeField['type'] == CustomFields::TYPE_TEXT) {
// Add one join for each text field.
$left_joins .= " left join tt_custom_field_log $cflTable on ($cflTable.log_id = l.id and $cflTable.status = 1)";
} elseif ($timeField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTable = 'cfo'.$timeField['id'];
// Add two joins for each dropdown field.
$left_joins .= " left join tt_custom_field_log $cflTable on ($cflTable.log_id = l.id and $cflTable.status = 1)";
$left_joins .= " left join tt_custom_field_options $cfoTable on ($cfoTable.field_id = $cflTable.field_id and $cfoTable.id = $cflTable.option_id)";
}
}
}
}
if (isset($custom_fields) && $custom_fields->userFields) {
foreach ($custom_fields->userFields as $userField) {
$field_name = 'user_field_'.$userField['id'];
$field_value = isset($options[$field_name]) ? $options[$field_name] : false;
$entity_type = CustomFields::ENTITY_USER;
if ($field_value) {
// We need to add left joins when input is not null.
$ecfTable = 'ecf'.$userField['id'];
if ($userField['type'] == CustomFields::TYPE_TEXT) {
// Add one join for each text field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = l.user_id and $ecfTable.field_id = ".$userField['id'].")";
} elseif ($userField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTable = 'cfo'.$userField['id'];
// Add two joins for each dropdown field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = l.user_id and $ecfTable.field_id = ".$userField['id'].")";
$left_joins .= " left join tt_custom_field_options $cfoTable on ($cfoTable.field_id = $ecfTable.field_id and $cfoTable.id = $ecfTable.option_id)";
}
}
}
}
if (isset($custom_fields) && $custom_fields->projectFields) {
foreach ($custom_fields->projectFields as $projectField) {
$field_name = 'project_field_'.$projectField['id'];
$field_value = isset($options[$field_name]) ? $options[$field_name] : false;
$entity_type = CustomFields::ENTITY_PROJECT;
if ($field_value) {
// We need to add left joins when input is not null.
$ecfTable = 'ecf'.$projectField['id'];
if ($projectField['type'] == CustomFields::TYPE_TEXT) {
// Add one join for each text field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = l.project_id and $ecfTable.field_id = ".$projectField['id'].")";
} elseif ($projectField['type'] == CustomFields::TYPE_DROPDOWN) {
$cfoTable = 'cfo'.$projectField['id'];
// Add two joins for each dropdown field.
$left_joins .= " left join tt_entity_custom_fields $ecfTable on ($ecfTable.entity_type = $entity_type and $ecfTable.entity_id = l.project_id and $ecfTable.field_id = ".$projectField['id'].")";
$left_joins .= " left join tt_custom_field_options $cfoTable on ($cfoTable.field_id = $ecfTable.field_id and $cfoTable.id = $ecfTable.option_id)";
}
}
}
}
if (isset($options['show_cost']) && $options['show_cost']) {
if (MODE_TIME == $trackingMode) {
$left_joins .= " left join tt_users u on (l.user_id = u.id)";
} else {
$left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
}
}
// Prepare sql query part for inner joins.
$inner_joins = null;
if ($user->isPluginEnabled('ts') && isset($options['timesheet']) && $options['timesheet']) {
$timesheet_option = $options['timesheet'];
if ($timesheet_option == TIMESHEET_PENDING)
$inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.submit_status = 1 and ts.approve_status is null)";
else if ($timesheet_option == TIMESHEET_APPROVED)
$inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approve_status = 1)";
else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
$inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approve_status = 0)";
}
// Prepare a query for time items.
$sql = "select $time_part $units_part $cost_part from tt_log l $left_joins $inner_joins $where";
// If we have expenses, query becomes a bit more complex.
if (isset($options['show_cost']) && $options['show_cost'] && $user->isPluginEnabled('ex')) {
$where = ttReportHelper::getExpenseWhere($options);
$sql_for_expenses = "select null as time";