Some more refactoring.
[timetracker.git] / WEB-INF / lib / ttReportHelper.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import('ttClientHelper');
30 import('DateAndTime');
31 import('Period');
32 import('ttTimeHelper');
33
34 require_once(dirname(__FILE__).'/../../plugins/CustomFields.class.php');
35
36 // Class ttReportHelper is used for help with reports.
37 class ttReportHelper {
38
39   // getWhere prepares a WHERE clause for a report query.
40   static function getWhere($options) {
41     global $user;
42
43     $group_id = $user->getGroup();
44     $org_id = $user->org_id;
45
46     // Prepare dropdown parts.
47     $dropdown_parts = '';
48     if ($options['client_id'])
49       $dropdown_parts .= ' and l.client_id = '.$options['client_id'];
50     elseif ($user->isClient() && $user->client_id)
51       $dropdown_parts .= ' and l.client_id = '.$user->client_id;
52     if ($options['cf_1_option_id']) $dropdown_parts .= ' and l.id in(select log_id from tt_custom_field_log where status = 1 and option_id = '.$options['cf_1_option_id'].')';
53     if ($options['project_id']) $dropdown_parts .= ' and l.project_id = '.$options['project_id'];
54     if ($options['task_id']) $dropdown_parts .= ' and l.task_id = '.$options['task_id'];
55     if ($options['billable']=='1') $dropdown_parts .= ' and l.billable = 1';
56     if ($options['billable']=='2') $dropdown_parts .= ' and l.billable = 0';
57     if ($options['invoice']=='1') $dropdown_parts .= ' and l.invoice_id is not NULL';
58     if ($options['invoice']=='2') $dropdown_parts .= ' and l.invoice_id is NULL';
59     if ($options['paid_status']=='1') $dropdown_parts .= ' and l.paid = 1';
60     if ($options['paid_status']=='2') $dropdown_parts .= ' and l.paid = 0';
61
62     // Prepare sql query part for user list.
63     $userlist = $options['users'] ? $options['users'] : '-1';
64     if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
65       $user_list_part = " and l.user_id in ($userlist)";
66     else
67       $user_list_part = " and l.user_id = ".$user->getUser();
68     $user_list_part .= " and l.group_id = $group_id and l.org_id = $org_id";
69
70     // Prepare sql query part for where.
71     $dateFormat = $user->getDateFormat();
72     if ($options['period'])
73       $period = new Period($options['period'], new DateAndTime($dateFormat));
74     else {
75       $period = new Period();
76       $period->setPeriod(
77         new DateAndTime($dateFormat, $options['period_start']),
78         new DateAndTime($dateFormat, $options['period_end']));
79     }
80     $where = " where l.status = 1 and l.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and l.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
81       " $user_list_part $dropdown_parts";
82     return $where;
83   }
84
85   // getExpenseWhere prepares WHERE clause for expenses query in a report.
86   static function getExpenseWhere($options) {
87     global $user;
88
89     $group_id = $user->getGroup();
90     $org_id = $user->org_id;
91
92     // Prepare dropdown parts.
93     $dropdown_parts = '';
94     if ($options['client_id'])
95       $dropdown_parts .= ' and ei.client_id = '.$options['client_id'];
96     elseif ($user->isClient() && $user->client_id)
97       $dropdown_parts .= ' and ei.client_id = '.$user->client_id;
98     if ($options['project_id']) $dropdown_parts .= ' and ei.project_id = '.$options['project_id'];
99     if ($options['invoice']=='1') $dropdown_parts .= ' and ei.invoice_id is not NULL';
100     if ($options['invoice']=='2') $dropdown_parts .= ' and ei.invoice_id is NULL';
101     if ($options['paid_status']=='1') $dropdown_parts .= ' and ei.paid = 1';
102     if ($options['paid_status']=='2') $dropdown_parts .= ' and ei.paid = 0';
103
104     // Prepare sql query part for user list.
105     $userlist = $options['users'] ? $options['users'] : '-1';
106     if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
107       $user_list_part = " and ei.user_id in ($userlist)";
108     else
109       $user_list_part = " and ei.user_id = ".$user->getUser();
110     $user_list_part .= " and ei.group_id = $group_id and ei.org_id = $org_id";
111
112     // Prepare sql query part for where.
113     $dateFormat = $user->getDateFormat();
114     if ($options['period'])
115       $period = new Period($options['period'], new DateAndTime($dateFormat));
116     else {
117       $period = new Period();
118       $period->setPeriod(
119         new DateAndTime($dateFormat, $options['period_start']),
120         new DateAndTime($dateFormat, $options['period_end']));
121     }
122     $where = " where ei.status = 1 and ei.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and ei.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
123       " $user_list_part $dropdown_parts";
124     return $where;
125   }
126
127   // getItems retrieves all items associated with a report.
128   // It combines tt_log and tt_expense_items in one array for presentation in one table using mysql union all.
129   // Expense items use the "note" field for item name.
130   static function getItems($options) {
131     global $user;
132     $mdb2 = getConnection();
133
134     // Determine these once as they are used in multiple places in this function.
135     $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
136     $isClient = $user->isClient();
137
138     $grouping = ttReportHelper::grouping($options);
139     if ($grouping) {
140       $grouping_by_date = ttReportHelper::groupingBy('date', $options);
141       $grouping_by_client = ttReportHelper::groupingBy('client', $options);
142       $grouping_by_project = ttReportHelper::groupingBy('project', $options);
143       $grouping_by_task = ttReportHelper::groupingBy('task', $options);
144       $grouping_by_user = ttReportHelper::groupingBy('user', $options);
145       $grouping_by_cf_1 = ttReportHelper::groupingBy('cf_1', $options);
146     }
147     $convertTo12Hour = ('%I:%M %p' == $user->getTimeFormat()) && ($options['show_start'] || $options['show_end']);
148     $trackingMode = $user->getTrackingMode();
149     $decimalMark = $user->getDecimalMark();
150
151     // Prepare a query for time items in tt_log table.
152     $fields = array(); // An array of fields for database query.
153     array_push($fields, 'l.id as id');
154     array_push($fields, '1 as type'); // Type 1 is for tt_log entries.
155     array_push($fields, 'l.date as date');
156     if($canViewReports || $isClient)
157       array_push($fields, 'u.name as user');
158     // Add client name if it is selected.
159     if ($options['show_client'] || $grouping_by_client)
160       array_push($fields, 'c.name as client');
161     // Add project name if it is selected.
162     if ($options['show_project'] || $grouping_by_project)
163       array_push($fields, 'p.name as project');
164     // Add task name if it is selected.
165     if ($options['show_task'] || $grouping_by_task)
166       array_push($fields, 't.name as task');
167     // Add custom field.
168     $include_cf_1 = $options['show_custom_field_1'] || $grouping_by_cf_1;
169     if ($include_cf_1) {
170       $custom_fields = new CustomFields();
171       $cf_1_type = $custom_fields->fields[0]['type'];
172       if ($cf_1_type == CustomFields::TYPE_TEXT) {
173         array_push($fields, 'cfl.value as cf_1');
174       } elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
175         array_push($fields, 'cfo.value as cf_1');
176       }
177     }
178     // Add start time.
179     if ($options['show_start']) {
180       array_push($fields, "l.start as unformatted_start");
181       array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start");
182     }
183     // Add finish time.
184     if ($options['show_end'])
185       array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish");
186     // Add duration.
187     if ($options['show_duration'])
188       array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
189     // Add work units.
190     if ($options['show_work_units']) {
191       if ($user->getConfigOption('unit_totals_only'))
192         array_push($fields, "null as units");
193       else {
194         $firstUnitThreshold = $user->getConfigInt('1st_unit_threshold');
195         $minutesInUnit = $user->getConfigInt('minutes_in_unit');
196         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");
197       }
198     }
199     // Add note.
200     if ($options['show_note'])
201       array_push($fields, 'l.comment as note');
202     // Handle cost.
203     $includeCost = $options['show_cost'];
204     if ($includeCost) {
205       if (MODE_TIME == $trackingMode)
206         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.
207       else
208         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.
209       array_push($fields, "null as expense"); 
210     }
211     // Add paid status.
212     if ($canViewReports && $options['show_paid'])
213       array_push($fields, 'l.paid as paid');
214     // Add IP address.
215     if ($canViewReports && $options['show_ip']) {
216       array_push($fields, 'l.created as created');
217       array_push($fields, 'l.created_ip as created_ip');
218       array_push($fields, 'l.modified as modified');
219       array_push($fields, 'l.modified_ip as modified_ip');
220     }
221     // Add invoice name if it is selected.
222     if (($canViewReports || $isClient) && $options['show_invoice'])
223       array_push($fields, 'i.name as invoice');
224
225     // Prepare sql query part for left joins.
226     $left_joins = null;
227     if ($options['show_client'] || $grouping_by_client)
228       $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
229     if (($canViewReports || $isClient) && $options['show_invoice'])
230       $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
231     if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
232        $left_joins .= " left join tt_users u on (u.id = l.user_id)";
233     if ($options['show_project'] || $grouping_by_project)
234       $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
235     if ($options['show_task'] || $grouping_by_task)
236       $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
237     if ($include_cf_1) {
238       if ($cf_1_type == CustomFields::TYPE_TEXT)
239         $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)";
240       elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
241         $left_joins .=  " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)".
242           " left join tt_custom_field_options cfo on (cfl.option_id = cfo.id)";
243       }
244     }
245     if ($includeCost && MODE_TIME != $trackingMode)
246       $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
247
248     $where = ttReportHelper::getWhere($options);
249
250     // Construct sql query for tt_log items.
251     $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
252     // If we don't have expense items (such as when the Expenses plugin is disabled), the above is all sql we need,
253     // with an exception of sorting part, that is added in the end.
254
255     // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
256     if ($options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
257
258       $fields = array(); // An array of fields for database query.
259       array_push($fields, 'ei.id');
260       array_push($fields, '2 as type'); // Type 2 is for tt_expense_items entries.
261       array_push($fields, 'ei.date');
262       if($canViewReports || $isClient)
263         array_push($fields, 'u.name as user');
264       // Add client name if it is selected.
265       if ($options['show_client'] || $grouping_by_client)
266         array_push($fields, 'c.name as client');
267       // Add project name if it is selected.
268       if ($options['show_project'] || $grouping_by_project)
269         array_push($fields, 'p.name as project');
270       if ($options['show_task'] || $grouping_by_task)
271         array_push($fields, 'null'); // null for task name. We need to match column count for union.
272       if ($options['show_custom_field_1'] || $grouping_by_cf_1)
273         array_push($fields, 'null'); // null for cf_1.
274       if ($options['show_start']) {
275         array_push($fields, 'null'); // null for unformatted_start.
276         array_push($fields, 'null'); // null for start.
277       }
278       if ($options['show_end'])
279         array_push($fields, 'null'); // null for finish.
280       if ($options['show_duration'])
281         array_push($fields, 'null'); // null for duration.
282       if ($options['show_work_units'])
283         array_push($fields, 'null as units'); // null for work units.
284       // Use the note field to print item name.
285       if ($options['show_note'])
286         array_push($fields, 'ei.name as note');
287       array_push($fields, 'ei.cost as cost');
288       array_push($fields, 'ei.cost as expense');
289       // Add paid status.
290       if ($canViewReports && $options['show_paid'])
291         array_push($fields, 'ei.paid as paid');
292       // Add IP address.
293       if ($canViewReports && $options['show_ip']) {
294         array_push($fields, 'ei.created as created');
295         array_push($fields, 'ei.created_ip as created_ip');
296         array_push($fields, 'ei.modified as modified');
297         array_push($fields, 'ei.modified_ip as modified_ip');
298       }
299       // Add invoice name if it is selected.
300       if (($canViewReports || $isClient) && $options['show_invoice'])
301         array_push($fields, 'i.name as invoice');
302
303       // Prepare sql query part for left joins.
304       $left_joins = null;
305       if ($canViewReports || $isClient)
306         $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
307       if ($options['show_client'] || $grouping_by_client)
308         $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
309       if ($options['show_project'] || $grouping_by_project)
310         $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
311       if (($canViewReports || $isClient) && $options['show_invoice'])
312         $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
313
314       $where = ttReportHelper::getExpenseWhere($options);
315
316       // Construct sql query for expense items.
317       $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
318
319       // Construct a union.
320       $sql = "($sql) union all ($sql_for_expense_items)";
321     }
322
323     // Determine sort part.
324     $sort_part = ' order by ';
325     if ($grouping) {
326       $sort_part2 .= ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') ? ', '.$options['group_by1'] : '';
327       $sort_part2 .= ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') ? ', '.$options['group_by2'] : '';
328       $sort_part2 .= ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') ? ', '.$options['group_by3'] : '';
329       if (!$grouping_by_date) $sort_part2 .= ', date';
330       $sort_part .= ltrim($sort_part2, ', '); // Remove leading comma and space.
331     } else {
332       $sort_part .= 'date';
333     }
334     if (($canViewReports || $isClient) && $options['users'] && !$grouping_by_user)
335       $sort_part .= ', user, type';
336     if ($options['show_start'])
337       $sort_part .= ', unformatted_start';
338     $sort_part .= ', id';
339
340     $sql .= $sort_part;
341     // By now we are ready with sql.
342
343     // Obtain items for report.
344     $res = $mdb2->query($sql);
345     if (is_a($res, 'PEAR_Error')) die($res->getMessage());
346
347     while ($val = $res->fetchRow()) {
348       if ($convertTo12Hour) {
349         if($val['start'] != '')
350           $val['start'] = ttTimeHelper::to12HourFormat($val['start']);
351         if($val['finish'] != '')
352           $val['finish'] = ttTimeHelper::to12HourFormat($val['finish']);
353       }
354       if (isset($val['cost'])) {
355         if ('.' != $decimalMark)
356           $val['cost'] = str_replace('.', $decimalMark, $val['cost']);
357       }
358       if (isset($val['expense'])) {
359         if ('.' != $decimalMark)
360           $val['expense'] = str_replace('.', $decimalMark, $val['expense']);
361       }
362
363       if ($grouping) $val['grouped_by'] = ttReportHelper::makeGroupByKey($options, $val);
364       $val['date'] = ttDateToUserFormat($val['date']);
365
366       $report_items[] = $val;
367     }
368
369     return $report_items;
370   }
371
372   // putInSession stores tt_log and tt_expense_items ids from a report in user session
373   // as 2 comma-separated lists.
374   static function putInSession($report_items) {
375     unset($_SESSION['report_item_ids']);
376     unset($_SESSION['report_item_expense_ids']);
377
378     // Iterate through records and build 2 comma-separated lists.
379     foreach($report_items as $item) {
380       if ($item['type'] == 1)
381         $report_item_ids .= ','.$item['id'];
382       else if ($item['type'] == 2)
383          $report_item_expense_ids .= ','.$item['id'];
384     }
385     $report_item_ids = trim($report_item_ids, ',');
386     $report_item_expense_ids = trim($report_item_expense_ids, ',');
387
388     // The lists are reqdy. Put them in session.
389     if ($report_item_ids) $_SESSION['report_item_ids'] = $report_item_ids;
390     if ($report_item_expense_ids) $_SESSION['report_item_expense_ids'] = $report_item_expense_ids;
391   }
392
393   // getFromSession obtains tt_log and tt_expense_items ids stored in user session.
394   static function getFromSession() {
395     $items = array();
396     $report_item_ids = $_SESSION['report_item_ids'];
397     if ($report_item_ids)
398       $items['report_item_ids'] = explode(',', $report_item_ids);
399     $report_item_expense_ids = $_SESSION['report_item_expense_ids'];
400     if ($report_item_expense_ids)
401       $items['report_item_expense_ids'] = explode(',', $report_item_expense_ids);
402     return $items;
403   }
404
405   // getSubtotals calculates report items subtotals when a report is grouped by.
406   // Without expenses, it's a simple select with group by.
407   // With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
408   static function getSubtotals($options) {
409     global $user;
410     $mdb2 = getConnection();
411
412     $concat_part = ttReportHelper::makeConcatPart($options);
413     $join_part = ttReportHelper::makeJoinPart($options);
414
415     // TODO: Consider moving this block out into a separate function.
416     $workUnits = $options['show_work_units'];
417     if ($workUnits) {
418       $unitTotalsOnly = $user->getConfigOption('unit_totals_only');
419       $firstUnitThreshold = $user->getConfigInt('1st_unit_threshold');
420       $minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
421       if ($unitTotalsOnly)
422         $work_unit_part = ", if (sum(l.billable * time_to_sec(l.duration)/60) < $firstUnitThreshold, 0, ceil(sum(l.billable * time_to_sec(l.duration)/60/$minutesInUnit))) as units";
423       else
424         $work_unit_part = ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $firstUnitThreshold, 0, ceil(time_to_sec(l.duration)/60/$minutesInUnit))) as units";
425     }
426     // End of TODO.
427
428     $where = ttReportHelper::getWhere($options);
429     $group_by_part = ttReportHelper::makeGroupByPart($options);
430     if ($options['show_cost']) {
431       if (MODE_TIME == $user->getTrackingMode()) {
432         if (!ttReportHelper::groupingBy('user', $options))
433           $left_join = 'left join tt_users u on (l.user_id = u.id)';
434         $sql = "select $concat_part, sum(time_to_sec(l.duration)) as time".$work_unit_part;
435         $sql .= ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
436           null as expenses from tt_log l
437           $join_part $left_join $where $group_by_part";
438       } else {
439         // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
440         $sql = "select $concat_part, sum(time_to_sec(l.duration)) as time".$work_unit_part;
441         $sql .= ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
442           null as expenses from tt_log l 
443           $join_part
444           left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where $group_by_part";
445       }
446     }  else {
447       $sql = "select $concat_part, sum(time_to_sec(l.duration)) as time".$work_unit_part;
448       $sql .= ", null as expenses from tt_log l 
449         $join_part $where $group_by_part";
450     }
451     // By now we have sql for time items.
452
453     // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
454     if ($options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
455
456       $concat_part = ttReportHelper::makeConcatExpensesPart($options);
457       $join_part = ttReportHelper::makeJoinExpensesPart($options);
458       $where = ttReportHelper::getExpenseWhere($options);
459       $group_by_expenses_part = ttReportHelper::makeGroupByExpensesPart($options);
460       $sql_for_expenses = "select $concat_part, null as time";
461       if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
462       $sql_for_expenses .= ", sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei $join_part $where $group_by_expenses_part";
463
464       // Create a combined query.
465       $fields = ttReportHelper::makeCombinedSelectPart($options);
466       $combined = "select $fields, sum(time) as time";
467       if ($options['show_work_units']) $combined .= ", sum(units) as units";
468       $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by $fields";
469       $sql = $combined;
470     }
471
472     // Execute query.
473     $res = $mdb2->query($sql);
474     if (is_a($res, 'PEAR_Error')) die($res->getMessage());
475     while ($val = $res->fetchRow()) {
476       $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
477       $rowLabel = ttReportHelper::makeGroupByLabel($val['group_field'], $options);
478       if ($options['show_cost']) {
479         $decimalMark = $user->getDecimalMark();
480         if ('.' != $decimalMark) {
481           $val['cost'] = str_replace('.', $decimalMark, $val['cost']);
482           $val['expenses'] = str_replace('.', $decimalMark, $val['expenses']);
483         }
484         $subtotals[$val['group_field']] = array('name'=>$rowLabel,'user'=>$val['user'],'project'=>$val['project'],'task'=>$val['task'],'client'=>$val['client'],'cf_1'=>$val['cf_1'],'time'=>$time,'units'=> $val['units'],'cost'=>$val['cost'],'expenses'=>$val['expenses']);
485       } else
486         $subtotals[$val['group_field']] = array('name'=>$rowLabel,'user'=>$val['user'],'project'=>$val['project'],'task'=>$val['task'],'client'=>$val['client'],'cf_1'=>$val['cf_1'],'time'=>$time, 'units'=> $val['units']);
487     }
488
489     return $subtotals;
490   }
491
492   // getTotals calculates total hours and cost for all report items.
493   static function getTotals($options)
494   {
495     global $user;
496
497     $mdb2 = getConnection();
498
499     $where = ttReportHelper::getWhere($options);
500
501     // Prepare parts.
502     $time_part = "sum(time_to_sec(l.duration)) as time";
503     if ($options['show_work_units']) {
504       $units_part = $user->unit_totals_only ? ", null as units" : ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
505     }
506     if ($options['show_cost']) {
507       if (MODE_TIME == $user->tracking_mode)
508         $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";
509       else
510         $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";
511     } else {
512       $cost_part = ", null as cost, null as expenses";
513     }
514     if ($options['show_cost']) {
515       if (MODE_TIME == $user->tracking_mode) {
516         $left_joins = "left join tt_users u on (l.user_id = u.id)";
517       } else {
518         $left_joins = "left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
519       }
520     }
521     // Prepare a query for time items.
522     $sql = "select $time_part $units_part $cost_part from tt_log l $left_joins $where";
523
524     // If we have expenses, query becomes a bit more complex.
525     if ($options['show_cost'] && $user->isPluginEnabled('ex')) {
526       $where = ttReportHelper::getExpenseWhere($options);
527       $sql_for_expenses = "select null as time";
528       if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
529       $sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
530
531       // Create a combined query.
532       $combined = "select sum(time) as time";
533       if ($options['show_work_units']) $combined .= ", sum(units) as units";
534       $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
535       $sql = $combined;
536     }
537
538     // Execute query.
539     $res = $mdb2->query($sql);
540     if (is_a($res, 'PEAR_Error')) die($res->getMessage());
541
542     $val = $res->fetchRow();
543     $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
544     if ($options['show_cost']) {
545       $total_cost = $val['cost'];
546       if (!$total_cost) $total_cost = '0.00';
547       if ('.' != $user->decimal_mark)
548         $total_cost = str_replace('.', $user->decimal_mark, $total_cost);
549       $total_expenses = $val['expenses'];
550       if (!$total_expenses) $total_expenses = '0.00';
551       if ('.' != $user->decimal_mark)
552         $total_expenses = str_replace('.', $user->decimal_mark, $total_expenses);
553     }
554
555     if ($options['period'])
556       $period = new Period($options['period'], new DateAndTime($user->date_format));
557     else {
558       $period = new Period();
559       $period->setPeriod(
560         new DateAndTime($user->date_format, $options['period_start']),
561         new DateAndTime($user->date_format, $options['period_end']));
562     }
563
564     $totals['start_date'] = $period->getStartDate();
565     $totals['end_date'] = $period->getEndDate();
566     $totals['time'] = $total_time;
567     $totals['units'] = $val['units'];
568     $totals['cost'] = $total_cost;
569     $totals['expenses'] = $total_expenses;
570
571     return $totals;
572   }
573
574   // The assignToInvoice assigns a set of records to a specific invoice.
575   static function assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids) {
576     global $user;
577     $mdb2 = getConnection();
578
579     $group_id = $user->getGroup();
580     $org_id = $user->org_id;
581
582     if ($time_log_ids) {
583       $sql = "update tt_log set invoice_id = ".$mdb2->quote($invoice_id).
584         " where id in(".join(', ', $time_log_ids).") and group_id = $group_id and org_id = $org_id";
585       $affected = $mdb2->exec($sql);
586       if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
587     }
588     if ($expense_item_ids) {
589       $sql = "update tt_expense_items set invoice_id = ".$mdb2->quote($invoice_id).
590         " where id in(".join(', ', $expense_item_ids).") and group_id = $group_id and org_id = $org_id";
591       $affected = $mdb2->exec($sql);
592       if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
593     }
594   }
595
596   // The markPaid marks a set of records as either paid or unpaid.
597   static function markPaid($time_log_ids, $expense_item_ids, $paid = true) {
598     global $user;
599     $mdb2 = getConnection();
600
601     $group_id = $user->getGroup();
602     $org_id = $user->org_id;
603
604     $paid_val = (int) $paid;
605     if ($time_log_ids) {
606       $sql = "update tt_log set paid = $paid_val".
607         " where id in(".join(', ', $time_log_ids).") and group_id = $group_id and org_id = $org_id";
608       $affected = $mdb2->exec($sql);
609       if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
610     }
611     if ($expense_item_ids) {
612       $sql = "update tt_expense_items set paid = $paid_val".
613         " where id in(".join(', ', $expense_item_ids).") and group_id = $group_id and org_id = $org_id";
614       $affected = $mdb2->exec($sql);
615       if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
616     }
617   }
618
619   // prepareReportBody - prepares an email body for report.
620   static function prepareReportBody($options, $comment = null)
621   {
622     global $user;
623     global $i18n;
624
625     // Determine these once as they are used in multiple places in this function.
626     $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
627     $isClient = $user->isClient();
628
629     $items = ttReportHelper::getItems($options);
630     $grouping = ttReportHelper::grouping($options);
631     if ($grouping)
632       $subtotals = ttReportHelper::getSubtotals($options);
633     $totals = ttReportHelper::getTotals($options);
634
635     // Use custom fields plugin if it is enabled.
636     if ($user->isPluginEnabled('cf'))
637       $custom_fields = new CustomFields();
638
639     // Define some styles to use in email.
640     $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
641     $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
642     $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
643     $rowItem = 'background-color: #ffffff;';
644     $rowItemAlt = 'background-color: #f5f5f5;';
645     $rowSubtotal = 'background-color: #e0e0e0;';
646     $cellLeftAligned = 'text-align: left; vertical-align: top;';
647     $cellRightAligned = 'text-align: right; vertical-align: top;';
648     $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
649     $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
650
651     // Start creating email body.
652     $body = '<html>';
653     $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
654     $body .= '<body>';
655
656     // Output title.
657     $body .= '<p style="'.$style_title.'">'.$i18n->get('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
658
659     // Output comment.
660     if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>';
661
662     if ($options['show_totals_only']) {
663       // Totals only report. Output subtotals.
664       $group_by_header = ttReportHelper::makeGroupByHeader($options);
665
666       $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
667       $body .= '<tr>';
668       $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
669       if ($options['show_duration'])
670         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
671       if ($options['show_work_units'])
672         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
673       if ($options['show_cost'])
674         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
675       $body .= '</tr>';
676       foreach($subtotals as $subtotal) {
677         $body .= '<tr style="'.$rowSubtotal.'">';
678         $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : '&nbsp;').'</td>';
679         if ($options['show_duration']) {
680           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
681           if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
682           $body .= '</td>';
683         }
684         if ($options['show_work_units']) {
685           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
686           $body .= $subtotal['units'];
687           $body .= '</td>';
688         }
689         if ($options['show_cost']) {
690           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
691           $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
692           $body .= '</td>';
693         }
694         $body .= '</tr>';
695       }
696
697       // Print totals.
698       $body .= '<tr><td>&nbsp;</td></tr>';
699       $body .= '<tr style="'.$rowSubtotal.'">';
700       $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
701       if ($options['show_duration']) {
702         $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
703         if ($totals['time'] <> '0:00') $body .= $totals['time'];
704         $body .= '</td>';
705       }
706       if ($options['show_work_units']) {
707         $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
708         $body .= $totals['units'];
709         $body .= '</td>';
710       }
711       if ($options['show_cost']) {
712         $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
713         $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
714         $body .= '</td>';
715       }
716       $body .= '</tr>';
717
718       $body .= '</table>';
719     } else {
720       // Regular report.
721
722       // Print table header.
723       $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
724       $body .= '<tr>';
725       $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.date').'</td>';
726       if ($canViewReports || $isClient)
727         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.user').'</td>';
728       if ($options['show_client'])
729         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.client').'</td>';
730       if ($options['show_project'])
731         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.project').'</td>';
732       if ($options['show_task'])
733         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.task').'</td>';
734       if ($options['show_custom_field_1'])
735         $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
736       if ($options['show_start'])
737         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.start').'</td>';
738       if ($options['show_end'])
739         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.finish').'</td>';
740       if ($options['show_duration'])
741         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
742       if ($options['show_work_units'])
743         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
744       if ($options['show_note'])
745         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.note').'</td>';
746       if ($options['show_cost'])
747         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
748       if ($options['show_paid'])
749         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.paid').'</td>';
750       if ($options['show_ip'])
751         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.ip').'</td>';
752       if ($options['show_invoice'])
753         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.invoice').'</td>';
754       $body .= '</tr>';
755
756       // Initialize variables to print subtotals.
757       if ($items && $grouping) {
758         $print_subtotals = true;
759         $first_pass = true;
760         $prev_grouped_by = '';
761         $cur_grouped_by = '';
762       }
763       // Initialize variables to alternate color of rows for different dates.
764       $prev_date = '';
765       $cur_date = '';
766       $row_style = $rowItem;
767
768       // Print report items.
769       if (is_array($items)) {
770         foreach ($items as $record) {
771           $cur_date = $record['date'];
772           // Print a subtotal row after a block of grouped items.
773           if ($print_subtotals) {
774             $cur_grouped_by = $record['grouped_by'];
775             if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
776               $body .= '<tr style="'.$rowSubtotal.'">';
777               $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
778               $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
779               if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['user'].'</td>';
780               if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['client'].'</td>';
781               if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['project'].'</td>';
782               if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['task'].'</td>';
783               if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['cf_1'].'</td>';
784               if ($options['show_start']) $body .= '<td></td>';
785               if ($options['show_end']) $body .= '<td></td>';
786               if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
787               if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['units'].'</td>';
788               if ($options['show_note']) $body .= '<td></td>';
789               if ($options['show_cost']) {
790                 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
791                 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
792                 $body .= '</td>';
793               }
794               if ($options['show_paid']) $body .= '<td></td>';
795               if ($options['show_ip']) $body .= '<td></td>';
796               if ($options['show_invoice']) $body .= '<td></td>';
797               $body .= '</tr>';
798               $body .= '<tr><td>&nbsp;</td></tr>';
799             }
800             $first_pass = false;
801           }
802
803           // Print a regular row.
804           if ($cur_date != $prev_date)
805             $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
806           $body .= '<tr style="'.$row_style.'">';
807           $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
808           if ($canViewReports || $isClient)
809             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
810           if ($options['show_client'])
811             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
812           if ($options['show_project'])
813             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
814           if ($options['show_task'])
815             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
816           if ($options['show_custom_field_1'])
817             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
818           if ($options['show_start'])
819             $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
820           if ($options['show_end'])
821             $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
822           if ($options['show_duration'])
823             $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
824           if ($options['show_work_units'])
825             $body .= '<td style="'.$cellRightAligned.'">'.$record['units'].'</td>';
826           if ($options['show_note'])
827             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
828           if ($options['show_cost'])
829             $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
830           if ($options['show_paid']) {
831             $body .= '<td style="'.$cellRightAligned.'">';
832             $body .= $record['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
833             $body .= '</td>';
834           }
835           if ($options['show_ip']) {
836             $body .= '<td style="'.$cellRightAligned.'">';
837             $body .= $record['modified'] ? $record['modified_ip'].' '.$record['modified'] : $record['created_ip'].' '.$record['created'];
838             $body .= '</td>';
839           }
840           if ($options['show_invoice'])
841             $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
842           $body .= '</tr>';
843
844           $prev_date = $record['date'];
845           if ($print_subtotals)
846             $prev_grouped_by = $record['grouped_by'];
847         }
848       }
849
850       // Print a terminating subtotal.
851       if ($print_subtotals) {
852         $body .= '<tr style="'.$rowSubtotal.'">';
853         $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
854         $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
855         if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['user'].'</td>';
856         if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['client'].'</td>';
857         if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['project'].'</td>';
858         if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['task'].'</td>';
859         if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['cf_1'].'</td>';
860         if ($options['show_start']) $body .= '<td></td>';
861         if ($options['show_end']) $body .= '<td></td>';
862         if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
863         if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['units'].'</td>';
864         if ($options['show_note']) $body .= '<td></td>';
865         if ($options['show_cost']) {
866           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
867           $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
868           $body .= '</td>';
869         }
870         if ($options['show_paid']) $body .= '<td></td>';
871         if ($options['show_ip']) $body .= '<td></td>';
872         if ($options['show_invoice']) $body .= '<td></td>';
873         $body .= '</tr>';
874       }
875
876       // Print totals.
877       $body .= '<tr><td>&nbsp;</td></tr>';
878       $body .= '<tr style="'.$rowSubtotal.'">';
879       $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
880       if ($canViewReports || $isClient) $body .= '<td></td>';
881       if ($options['show_client']) $body .= '<td></td>';
882       if ($options['show_project']) $body .= '<td></td>';
883       if ($options['show_task']) $body .= '<td></td>';
884       if ($options['show_custom_field_1']) $body .= '<td></td>';
885       if ($options['show_start']) $body .= '<td></td>';
886       if ($options['show_end']) $body .= '<td></td>';
887       if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
888       if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['units'].'</td>';
889       if ($options['show_note']) $body .= '<td></td>';
890       if ($options['show_cost']) {
891         $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
892         $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
893         $body .= '</td>';
894       }
895       if ($options['show_paid']) $body .= '<td></td>';
896       if ($options['show_ip']) $body .= '<td></td>';
897       if ($options['show_invoice']) $body .= '<td></td>';
898       $body .= '</tr>';
899
900       $body .= '</table>';
901     }
902
903     // Output footer.
904     if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
905       $body .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
906
907     // Finish creating email body.
908     $body .= '</body></html>';
909
910     return $body;
911   }
912
913   // checkFavReportCondition - checks whether it is okay to send fav report.
914   static function checkFavReportCondition($options, $condition)
915   {
916     $items = ttReportHelper::getItems($options);
917
918     $condition = trim(str_replace('count', '', $condition));
919
920     $greater_or_equal = ttStartsWith($condition, '>=');
921     if ($greater_or_equal) $condition = trim(str_replace('>=', '', $condition));
922
923     $less_or_equal = ttStartsWith($condition, '<=');
924     if ($less_or_equal) $condition = trim(str_replace('<=', '', $condition));
925
926     $not_equal = ttStartsWith($condition, '<>');
927     if ($not_equal) $condition = trim(str_replace('<>', '', $condition));
928
929     $greater = ttStartsWith($condition, '>');
930     if ($greater) $condition = trim(str_replace('>', '', $condition));
931
932     $less = ttStartsWith($condition, '<');
933     if ($less) $condition = trim(str_replace('<', '', $condition));
934
935     $equal = ttStartsWith($condition, '=');
936     if ($equal) $condition = trim(str_replace('=', '', $condition));
937
938     $count_required = (int) $condition;
939
940     if ($greater && count($items) > $count_required) return true;
941     if ($greater_or_equal && count($items) >= $count_required) return true;
942     if ($less && count($items) < $count_required) return true;
943     if ($less_or_equal && count($items) <= $count_required) return true;
944     if ($equal && count($items) == $count_required) return true;
945     if ($not_equal && count($items) <> $count_required) return true;
946
947     return false;
948   }
949
950   // sendFavReport - sends a favorite report to a specified email, called from cron.php
951   static function sendFavReport($options, $subject, $email, $cc) {
952     // We are called from cron.php, we have no $bean in session.
953     // cron.php sets global $user and $i18n objects to match our favorite report user.
954     global $user;
955     global $i18n;
956
957     // Prepare report body.
958     $body = ttReportHelper::prepareReportBody($options);
959
960     import('mail.Mailer');
961     $mailer = new Mailer();
962     $mailer->setCharSet(CHARSET);
963     $mailer->setContentType('text/html');
964     $mailer->setSender(SENDER);
965     if (!empty($cc))
966       $mailer->setReceiverCC($cc);
967     if (!empty($user->bcc_email))
968       $mailer->setReceiverBCC($user->bcc_email);
969     $mailer->setReceiver($email);
970     $mailer->setMailMode(MAIL_MODE);
971     if (empty($subject)) $subject = $options['name'];
972     if (!$mailer->send($subject, $body))
973       return false;
974
975     return true;
976   }
977
978   // getReportOptions - returns an array of report options constructed from session bean.
979   //
980   // Note: similarly to ttFavReportHelper::getReportOptions, this function is a part of
981   // refactoring to simplify maintenance of report generating functions, as we currently
982   // have 2 sets: normal reporting (from bean), and fav report emailing (from db fields).
983   // Using options obtained from either db or bean shall allow us to use only one set of functions.
984   static function getReportOptions($bean) {
985     global $user;
986
987     // Prepare an array of report options.
988     $options = array();
989
990     // Construct one by one.
991     $options['name'] = null; // No name required.
992     $options['user_id'] = $user->id; // Not sure if we need user_id here. Fav reports use it to recycle $user object in cron.php.
993     $options['client_id'] = $bean->getAttribute('client');
994     $options['cf_1_option_id'] = $bean->getAttribute('option');
995     $options['project_id'] = $bean->getAttribute('project');
996     $options['task_id'] = $bean->getAttribute('task');
997     $options['billable'] = $bean->getAttribute('include_records');
998     $options['invoice'] = $bean->getAttribute('invoice');
999     $options['paid_status'] = $bean->getAttribute('paid_status');
1000     if (is_array($bean->getAttribute('users'))) $options['users'] = join(',', $bean->getAttribute('users'));
1001     $options['period'] = $bean->getAttribute('period');
1002     $options['period_start'] = $bean->getAttribute('start_date');
1003     $options['period_end'] = $bean->getAttribute('end_date');
1004     $options['show_client'] = $bean->getAttribute('chclient');
1005     $options['show_invoice'] = $bean->getAttribute('chinvoice');
1006     $options['show_paid'] = $bean->getAttribute('chpaid');
1007     $options['show_ip'] = $bean->getAttribute('chip');
1008     $options['show_project'] = $bean->getAttribute('chproject');
1009     $options['show_start'] = $bean->getAttribute('chstart');
1010     $options['show_duration'] = $bean->getAttribute('chduration');
1011     $options['show_cost'] = $bean->getAttribute('chcost');
1012     $options['show_task'] = $bean->getAttribute('chtask');
1013     $options['show_end'] = $bean->getAttribute('chfinish');
1014     $options['show_note'] = $bean->getAttribute('chnote');
1015     $options['show_custom_field_1'] = $bean->getAttribute('chcf_1');
1016     $options['show_work_units'] = $bean->getAttribute('chunits');
1017     $options['show_totals_only'] = $bean->getAttribute('chtotalsonly');
1018     $options['group_by1'] = $bean->getAttribute('group_by1');
1019     $options['group_by2'] = $bean->getAttribute('group_by2');
1020     $options['group_by3'] = $bean->getAttribute('group_by3');
1021     return $options;
1022   }
1023
1024   // verifyBean is a security function to make sure data in bean makes sense for a group.
1025   static function verifyBean($bean) {
1026     global $user;
1027
1028     // Check users.
1029     $users_in_bean = $bean->getAttribute('users');
1030     if (is_array($users_in_bean)) {
1031       $users_in_group = ttGroupHelper::getUsers();
1032       foreach ($users_in_group as $user_in_group) {
1033         $valid_ids[] = $user_in_group['id'];
1034       }
1035       foreach ($users_in_bean as $user_in_bean) {
1036         if (!in_array($user_in_bean, $valid_ids)) {
1037           return false;
1038         }
1039       }
1040     }
1041
1042     // TODO: add additional checks here. Perhaps do it before saving the bean for consistency.
1043     return true;
1044   }
1045
1046   // makeGroupByKey builds a combined group by key from group_by1, group_by2 and group_by3 values
1047   // (passed in $options) and a row of data ($row obtained from a db query).
1048   static function makeGroupByKey($options, $row) {
1049     if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1050       // We have group_by1.
1051       $group_by1 = $options['group_by1'];
1052       $group_by1_value = $row[$group_by1];
1053       //if ($group_by1 == 'date') $group_by1_value = ttDateToUserFormat($group_by1_value);
1054       if (empty($group_by1_value)) $group_by1_value = 'Null'; // To match what comes out of makeConcatPart.
1055       $group_by_key .= ' - '.$group_by1_value;
1056     }
1057     if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1058       // We have group_by2.
1059       $group_by2 = $options['group_by2'];
1060       $group_by2_value = $row[$group_by2];
1061       //if ($group_by2 == 'date') $group_by2_value = ttDateToUserFormat($group_by2_value);
1062       if (empty($group_by2_value)) $group_by2_value = 'Null'; // To match what comes out of makeConcatPart.
1063       $group_by_key .= ' - '.$group_by2_value;
1064     }
1065     if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1066       // We have group_by3.
1067       $group_by3 = $options['group_by3'];
1068       $group_by3_value = $row[$group_by3];
1069       //if ($group_by3 == 'date') $group_by3_value = ttDateToUserFormat($group_by3_value);
1070       if (empty($group_by3_value)) $group_by3_value = 'Null'; // To match what comes out of makeConcatPart.
1071       $group_by_key .= ' - '.$group_by3_value;
1072     }
1073     $group_by_key = trim($group_by_key, ' -');
1074     return $group_by_key;
1075   }
1076
1077   // makeGroupByPart builds a combined group by part for sql query for time items using group_by1,
1078   // group_by2, and group_by3 values passed in $options.
1079   static function makeGroupByPart($options) {
1080     if (!ttReportHelper::grouping($options)) return null;
1081
1082     $group_by1 = $options['group_by1'];
1083     $group_by2 = $options['group_by2'];
1084     $group_by3 = $options['group_by3'];
1085
1086     switch ($group_by1) {
1087       case 'date':
1088         $group_by_parts .= ', l.date';
1089         break;
1090       case 'user':
1091         $group_by_parts .= ', u.name';
1092         break;
1093       case 'client':
1094         $group_by_parts .= ', c.name';
1095         break;
1096       case 'project':
1097         $group_by_parts .= ', p.name';
1098         break;
1099       case 'task':
1100         $group_by_parts .= ', t.name';
1101         break;
1102       case 'cf_1':
1103         $group_by_parts .= ', cfo.value';
1104         break;
1105     }
1106     switch ($group_by2) {
1107       case 'date':
1108         $group_by_parts .= ', l.date';
1109         break;
1110       case 'user':
1111         $group_by_parts .= ', u.name';
1112         break;
1113       case 'client':
1114         $group_by_parts .= ', c.name';
1115         break;
1116       case 'project':
1117         $group_by_parts .= ', p.name';
1118         break;
1119       case 'task':
1120         $group_by_parts .= ', t.name';
1121         break;
1122       case 'cf_1':
1123         $group_by_parts .= ', cfo.value';
1124         break;
1125     }
1126     switch ($group_by3) {
1127       case 'date':
1128         $group_by_parts .= ', l.date';
1129         break;
1130       case 'user':
1131         $group_by_parts .= ', u.name';
1132         break;
1133       case 'client':
1134         $group_by_parts .= ', c.name';
1135         break;
1136       case 'project':
1137         $group_by_parts .= ', p.name';
1138         break;
1139       case 'task':
1140         $group_by_parts .= ', t.name';
1141         break;
1142       case 'cf_1':
1143         $group_by_parts .= ', cfo.value';
1144         break;
1145     }
1146     // Remove garbage from the beginning.
1147     $group_by_parts = ltrim($group_by_parts, ', ');
1148     $group_by_part = "group by $group_by_parts";
1149     return $group_by_part;
1150   }
1151
1152   // makeGroupByExpensesPart builds a combined group by part for sql query for expense items using
1153   // group_by1, group_by2, and group_by3 values passed in $options.
1154   static function makeGroupByExpensesPart($options) {
1155     $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
1156       ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
1157       ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
1158     if ($no_grouping) return null;
1159
1160     $group_by1 = $options['group_by1'];
1161     $group_by2 = $options['group_by2'];
1162     $group_by3 = $options['group_by3'];
1163
1164     switch ($group_by1) {
1165       case 'date':
1166         $group_by_parts .= ', ei.date';
1167         break;
1168       case 'user':
1169         $group_by_parts .= ', u.name';
1170         break;
1171       case 'client':
1172         $group_by_parts .= ', c.name';
1173         break;
1174       case 'project':
1175         $group_by_parts .= ', p.name';
1176         break;
1177     }
1178     switch ($group_by2) {
1179       case 'date':
1180         $group_by_parts .= ', ei.date';
1181         break;
1182       case 'user':
1183         $group_by_parts .= ', u.name';
1184         break;
1185       case 'client':
1186         $group_by_parts .= ', c.name';
1187         break;
1188       case 'project':
1189         $group_by_parts .= ', p.name';
1190         break;
1191     }
1192     switch ($group_by3) {
1193       case 'date':
1194         $group_by_parts .= ', ei.date';
1195         break;
1196       case 'user':
1197         $group_by_parts .= ', u.name';
1198         break;
1199       case 'client':
1200         $group_by_parts .= ', c.name';
1201         break;
1202       case 'project':
1203         $group_by_parts .= ', p.name';
1204         break;
1205     }
1206     // Remove garbage from the beginning.
1207     $group_by_parts = ltrim($group_by_parts, ', ');
1208     if ($group_by_parts)
1209       $group_by_part = "group by $group_by_parts";
1210     return $group_by_part;
1211   }
1212
1213   // makeConcatPart builds a concatenation part for getSubtotals query (for time items).
1214   static function makeConcatPart($options) {
1215     $group_by1 = $options['group_by1'];
1216     $group_by2 = $options['group_by2'];
1217     $group_by3 = $options['group_by3'];
1218
1219     switch ($group_by1) {
1220       case 'date':
1221         $what_to_concat .= ", ' - ', l.date";
1222         break;
1223       case 'user':
1224         $what_to_concat .= ", ' - ', u.name";
1225         $fields_part .= ', u.name as user';
1226         break;
1227       case 'client':
1228         $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1229         $fields_part .= ', c.name as client';
1230         break;
1231       case 'project':
1232         $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1233         $fields_part .= ', p.name as project';
1234         break;
1235       case 'task':
1236         $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1237         $fields_part .= ', t.name as task';
1238         break;
1239       case 'cf_1':
1240         $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1241         $fields_part .= ', cfo.value as cf_1';
1242         break;
1243     }
1244     switch ($group_by2) {
1245       case 'date':
1246         $what_to_concat .= ", ' - ', l.date";
1247         break;
1248       case 'user':
1249         $what_to_concat .= ", ' - ', u.name";
1250         $fields_part .= ', u.name as user';
1251         break;
1252       case 'client':
1253         $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1254         $fields_part .= ', c.name as client';
1255         break;
1256       case 'project':
1257         $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1258         $fields_part .= ', p.name as project';
1259         break;
1260       case 'task':
1261         $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1262         $fields_part .= ', t.name as task';
1263         break;
1264       case 'cf_1':
1265         $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1266         $fields_part .= ', cfo.value as cf_1';
1267         break;
1268     }
1269     switch ($group_by3) {
1270       case 'date':
1271         $what_to_concat .= ", ' - ', l.date";
1272         break;
1273       case 'user':
1274         $what_to_concat .= ", ' - ', u.name";
1275         $fields_part .= ', u.name as user';
1276         break;
1277       case 'client':
1278         $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1279         $fields_part .= ', c.name as client';
1280         break;
1281       case 'project':
1282         $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1283         $fields_part .= ', p.name as project';
1284         break;
1285       case 'task':
1286         $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1287         $fields_part .= ', t.name as task';
1288         break;
1289       case 'cf_1':
1290         $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1291         $fields_part .= ', cfo.value as cf_1';
1292         break;
1293     }
1294     // Remove garbage from both ends.
1295     $what_to_concat = trim($what_to_concat, "', -");
1296     $concat_part = "concat($what_to_concat) as group_field";
1297     $concat_part = trim($concat_part, ' -');
1298     return "$concat_part $fields_part";
1299   }
1300
1301   // makeConcatPart builds a concatenation part for getSubtotals query (for expense items).
1302   static function makeConcatExpensesPart($options) {
1303     $group_by1 = $options['group_by1'];
1304     $group_by2 = $options['group_by2'];
1305     $group_by3 = $options['group_by3'];
1306
1307     switch ($group_by1) {
1308       case 'date':
1309         $what_to_concat .= ", ' - ', ei.date";
1310         break;
1311       case 'user':
1312         $what_to_concat .= ", ' - ', u.name";
1313         $fields_part .= ', u.name as user';
1314         break;
1315       case 'client':
1316         $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1317         $fields_part .= ', c.name as client';
1318         break;
1319       case 'project':
1320         $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1321         $fields_part .= ', p.name as project';
1322         break;
1323
1324       case 'task':
1325         $what_to_concat .= ", ' - ', 'Null'";
1326         $fields_part .= ', null as task';
1327         break;
1328
1329       case 'cf_1':
1330         $what_to_concat .= ", ' - ', 'Null'";
1331         $fields_part .= ', null as cf_1';
1332         break;
1333     }
1334     switch ($group_by2) {
1335       case 'date':
1336         $what_to_concat .= ", ' - ', ei.date";
1337         break;
1338       case 'user':
1339         $what_to_concat .= ", ' - ', u.name";
1340         $fields_part .= ', u.name as user';
1341         break;
1342       case 'client':
1343         $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1344         $fields_part .= ', c.name as client';
1345         break;
1346       case 'project':
1347         $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1348         $fields_part .= ', p.name as project';
1349         break;
1350
1351       case 'task':
1352         $what_to_concat .= ", ' - ', 'Null'";
1353         $fields_part .= ', null as task';
1354         break;
1355
1356       case 'cf_1':
1357         $what_to_concat .= ", ' - ', 'Null'";
1358         $fields_part .= ', null as cf_1';
1359         break;
1360     }
1361     switch ($group_by3) {
1362       case 'date':
1363         $what_to_concat .= ", ' - ', ei.date";
1364         break;
1365       case 'user':
1366         $what_to_concat .= ", ' - ', u.name";
1367         $fields_part .= ', u.name as user';
1368         break;
1369       case 'client':
1370         $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1371         $fields_part .= ', c.name as client';
1372         break;
1373       case 'project':
1374         $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1375         $fields_part .= ', p.name as project';
1376         break;
1377
1378       case 'task':
1379         $what_to_concat .= ", ' - ', 'Null'";
1380         $fields_part .= ', null as task';
1381         break;
1382
1383       case 'cf_1':
1384         $what_to_concat .= ", ' - ', 'Null'";
1385         $fields_part .= ', null as cf_1';
1386         break;
1387     }
1388     // Remove garbage from the beginning.
1389     if ($what_to_concat)
1390         $what_to_concat = substr($what_to_concat, 8);
1391     $concat_part = "concat($what_to_concat) as group_field";
1392     return "$concat_part $fields_part";
1393   }
1394
1395   // makeCombinedSelectPart builds a list of fields for a combined select on a union for getSubtotals.
1396   // This is used when we include expenses.
1397   static function makeCombinedSelectPart($options) {
1398     $group_by1 = $options['group_by1'];
1399     $group_by2 = $options['group_by2'];
1400     $group_by3 = $options['group_by3'];
1401
1402     $fields = "group_field";
1403
1404     switch ($group_by1) {
1405       case 'user':
1406         $fields .= ', user';
1407         break;
1408       case 'client':
1409         $fields_part .= ', client';
1410         break;
1411       case 'project':
1412         $fields .= ', project';
1413         break;
1414
1415       case 'task':
1416         $fields .= ', task';
1417         break;
1418
1419       case 'cf_1':
1420         $fields .= ', cf_1';
1421         break;
1422     }
1423     switch ($group_by2) {
1424       case 'user':
1425         $fields .= ', user';
1426         break;
1427       case 'client':
1428         $fields_part .= ', client';
1429         break;
1430       case 'project':
1431         $fields .= ', project';
1432         break;
1433
1434       case 'task':
1435         $fields .= ', task';
1436         break;
1437
1438       case 'cf_1':
1439         $fields .= ', cf_1';
1440         break;
1441     }
1442     switch ($group_by3) {
1443       case 'user':
1444         $fields .= ', user';
1445         break;
1446       case 'client':
1447         $fields_part .= ', client';
1448         break;
1449       case 'project':
1450         $fields .= ', project';
1451         break;
1452
1453       case 'task':
1454         $fields .= ', task';
1455         break;
1456
1457       case 'cf_1':
1458         $fields .= ', cf_1';
1459         break;
1460     }
1461     return $fields;
1462   }
1463
1464   // makeJoinPart builds a left join part for getSubtotals query (for time items).
1465   static function makeJoinPart($options) {
1466     global $user;
1467
1468     if (ttReportHelper::groupingBy('user', $options)) {
1469       $join .= ' left join tt_users u on (l.user_id = u.id)';
1470     }
1471     if (ttReportHelper::groupingBy('client', $options)) {
1472       $join .= ' left join tt_clients c on (l.client_id = c.id)';
1473     }
1474     if (ttReportHelper::groupingBy('project', $options)) {
1475       $join .= ' left join tt_projects p on (l.project_id = p.id)';
1476     }
1477     if (ttReportHelper::groupingBy('task', $options)) {
1478       $join .= ' left join tt_tasks t on (l.task_id = t.id)';
1479     }
1480     if (ttReportHelper::groupingBy('cf_1', $options)) {
1481       $custom_fields = new CustomFields();
1482       if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
1483         $join .= ' left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1) left join tt_custom_field_options cfo on (cfl.value = cfo.id)';
1484       elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
1485         $join .= ' left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1) left join tt_custom_field_options cfo on (cfl.option_id = cfo.id)';
1486     }
1487     return $join;
1488   }
1489
1490   // makeJoinExpensesPart builds a left join part for getSubtotals query for expense items.
1491   static function makeJoinExpensesPart($options) {
1492     if (ttReportHelper::groupingBy('user', $options)) {
1493       $join .= ' left join tt_users u on (ei.user_id = u.id)';
1494     }
1495     if (ttReportHelper::groupingBy('client', $options)) {
1496       $join .= ' left join tt_clients c on (ei.client_id = c.id)';
1497     }
1498     if (ttReportHelper::groupingBy('project', $options)) {
1499       $join .= ' left join tt_projects p on (ei.project_id = p.id)';
1500     }
1501     return $join;
1502   }
1503
1504   // grouping determines if we are grouping the report by either group_by1,
1505   // group_by2, or group_by3 values passed in $options.
1506   static function grouping($options) {
1507     $grouping = ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') ||
1508       ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') ||
1509       ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping');
1510     return $grouping;
1511   }
1512
1513   // groupingBy determines if we are grouping a report by a value of $what
1514   // ('date', 'user', 'project', etc.) by checking group_by1, group_by2,
1515   // and group_by3 values passed in $options.
1516   static function groupingBy($what, $options) {
1517     $grouping = ($options['group_by1'] == $what) || ($options['group_by2'] == $what) || ($options['group_by3'] == $what);
1518     return $grouping;
1519   }
1520
1521   // makeGroupByHeader builds a column header for a totals-only report using group_by1,
1522   // group_by2, and group_by3 values passed in $options.
1523   static function makeGroupByHeader($options) {
1524     global $i18n;
1525     global $custom_fields;
1526
1527     $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
1528       ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
1529       ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
1530     if ($no_grouping) return null;
1531
1532     if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1533       // We have group_by1.
1534       $group_by1 = $options['group_by1'];
1535       if ('cf_1' == $group_by1)
1536         $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1537       else {
1538         $key = 'label.'.$group_by1;
1539         $group_by_header .= ' - '.$i18n->get($key);
1540       }
1541     }
1542     if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1543       // We have group_by2.
1544       $group_by2 = $options['group_by2'];
1545       if ('cf_1' == $group_by2)
1546         $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1547       else {
1548         $key = 'label.'.$group_by2;
1549         $group_by_header .= ' - '.$i18n->get($key);
1550       }
1551     }
1552     if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1553       // We have group_by3.
1554       $group_by3 = $options['group_by3'];
1555       if ('cf_1' == $group_by3)
1556         $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1557       else {
1558         $key = 'label.'.$group_by3;
1559         $group_by_header .= ' - '.$i18n->get($key);
1560       }
1561     }
1562     $group_by_header = ltrim($group_by_header, ' -');
1563     return $group_by_header;
1564   }
1565
1566   // makeGroupByXmlTag creates an xml tag for a totals only report using group_by1,
1567   // group_by2, and group_by3 values passed in $options.
1568   static function makeGroupByXmlTag($options) {
1569     if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1570       // We have group_by1.
1571       $tag .= '_'.$options['group_by1'];
1572     }
1573     if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1574       // We have group_by2.
1575       $tag .= '_'.$options['group_by2'];
1576     }
1577     if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1578       // We have group_by3.
1579       $tag .= '_'.$options['group_by3'];
1580     }
1581     $tag = ltrim($tag, '_');
1582     return $tag;
1583   }
1584
1585   // makeGroupByLabel builds a label for one row in a "Totals only" report of grouped by items.
1586   // It does one thing: if we are grouping by date, the date format is converted for user.
1587   static function makeGroupByLabel($key, $options) {
1588     if (!ttReportHelper::groupingBy('date', $options))
1589       return $key; // No need to format.
1590
1591     global $user;
1592     if ($user->date_format == DB_DATEFORMAT)
1593       return $key; // No need to format.
1594
1595     $label = $key;
1596     if (preg_match('/\d\d\d\d-\d\d-\d\d/', $key, $matches)) {
1597       // Replace the first found match of a date in DB_DATEFORMAT.
1598       // This is not entirely clean but better than nothing for a label in a row.
1599       $userDate = ttDateToUserFormat($matches[0]);
1600       $label = str_replace($matches[0], $userDate, $key);
1601     }
1602     return $label;
1603   }
1604 }