A fix for work units in totals only report.
[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', 0);
195         $minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
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     $work_unit_part = ttReportHelper::makeWorkUnitPart($options);
414     $join_part = ttReportHelper::makeJoinPart($options);
415     $cost_part = ttReportHelper::makeCostPart($options);
416     $where = ttReportHelper::getWhere($options);
417     $group_by_part = ttReportHelper::makeGroupByPart($options);
418
419     $parts = "$concat_part, sum(time_to_sec(l.duration)) as time, null as expenses".$work_unit_part.$cost_part;
420     $sql = "select $parts from tt_log l $join_part $where $group_by_part";
421     // By now we have sql for time items.
422
423     // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
424     if ($options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
425
426       $concat_part = ttReportHelper::makeConcatExpensesPart($options);
427       $join_part = ttReportHelper::makeJoinExpensesPart($options);
428       $where = ttReportHelper::getExpenseWhere($options);
429       $group_by_expenses_part = ttReportHelper::makeGroupByExpensesPart($options);
430       $sql_for_expenses = "select $concat_part, null as time";
431       if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
432       $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";
433
434       // Create a combined query.
435       $fields = ttReportHelper::makeCombinedSelectPart($options);
436       $combined = "select $fields, sum(time) as time";
437       if ($options['show_work_units']) $combined .= ", sum(units) as units";
438       $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by $fields";
439       $sql = $combined;
440     }
441
442     // Execute query.
443     $res = $mdb2->query($sql);
444     if (is_a($res, 'PEAR_Error')) die($res->getMessage());
445     while ($val = $res->fetchRow()) {
446       $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
447       $rowLabel = ttReportHelper::makeGroupByLabel($val['group_field'], $options);
448       if ($options['show_cost']) {
449         $decimalMark = $user->getDecimalMark();
450         if ('.' != $decimalMark) {
451           $val['cost'] = str_replace('.', $decimalMark, $val['cost']);
452           $val['expenses'] = str_replace('.', $decimalMark, $val['expenses']);
453         }
454         $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']);
455       } else
456         $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']);
457     }
458
459     return $subtotals;
460   }
461
462   // getTotals calculates total hours and cost for all report items.
463   static function getTotals($options)
464   {
465     global $user;
466
467     $mdb2 = getConnection();
468
469     $where = ttReportHelper::getWhere($options);
470
471     // Prepare parts.
472     $time_part = "sum(time_to_sec(l.duration)) as time";
473     if ($options['show_work_units']) {
474       $unitTotalsOnly = $user->getConfigOption('unit_totals_only');
475       $firstUnitThreshold = $user->getConfigInt('1st_unit_threshold', 0);
476       $minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
477       $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";
478     }
479     if ($options['show_cost']) {
480       if (MODE_TIME == $user->tracking_mode)
481         $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";
482       else
483         $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";
484     } else {
485       $cost_part = ", null as cost, null as expenses";
486     }
487     if ($options['show_cost']) {
488       if (MODE_TIME == $user->tracking_mode) {
489         $left_joins = "left join tt_users u on (l.user_id = u.id)";
490       } else {
491         $left_joins = "left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
492       }
493     }
494     // Prepare a query for time items.
495     $sql = "select $time_part $units_part $cost_part from tt_log l $left_joins $where";
496
497     // If we have expenses, query becomes a bit more complex.
498     if ($options['show_cost'] && $user->isPluginEnabled('ex')) {
499       $where = ttReportHelper::getExpenseWhere($options);
500       $sql_for_expenses = "select null as time";
501       if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
502       $sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
503
504       // Create a combined query.
505       $combined = "select sum(time) as time";
506       if ($options['show_work_units']) $combined .= ", sum(units) as units";
507       $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
508       $sql = $combined;
509     }
510
511     // Execute query.
512     $res = $mdb2->query($sql);
513     if (is_a($res, 'PEAR_Error')) die($res->getMessage());
514
515     $val = $res->fetchRow();
516     $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
517     if ($options['show_cost']) {
518       $total_cost = $val['cost'];
519       if (!$total_cost) $total_cost = '0.00';
520       if ('.' != $user->decimal_mark)
521         $total_cost = str_replace('.', $user->decimal_mark, $total_cost);
522       $total_expenses = $val['expenses'];
523       if (!$total_expenses) $total_expenses = '0.00';
524       if ('.' != $user->decimal_mark)
525         $total_expenses = str_replace('.', $user->decimal_mark, $total_expenses);
526     }
527
528     if ($options['period'])
529       $period = new Period($options['period'], new DateAndTime($user->date_format));
530     else {
531       $period = new Period();
532       $period->setPeriod(
533         new DateAndTime($user->date_format, $options['period_start']),
534         new DateAndTime($user->date_format, $options['period_end']));
535     }
536
537     $totals['start_date'] = $period->getStartDate();
538     $totals['end_date'] = $period->getEndDate();
539     $totals['time'] = $total_time;
540     $totals['units'] = $val['units'];
541     $totals['cost'] = $total_cost;
542     $totals['expenses'] = $total_expenses;
543
544     return $totals;
545   }
546
547   // The assignToInvoice assigns a set of records to a specific invoice.
548   static function assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids) {
549     global $user;
550     $mdb2 = getConnection();
551
552     $group_id = $user->getGroup();
553     $org_id = $user->org_id;
554
555     if ($time_log_ids) {
556       $sql = "update tt_log set invoice_id = ".$mdb2->quote($invoice_id).
557         " where id in(".join(', ', $time_log_ids).") and group_id = $group_id and org_id = $org_id";
558       $affected = $mdb2->exec($sql);
559       if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
560     }
561     if ($expense_item_ids) {
562       $sql = "update tt_expense_items set invoice_id = ".$mdb2->quote($invoice_id).
563         " where id in(".join(', ', $expense_item_ids).") and group_id = $group_id and org_id = $org_id";
564       $affected = $mdb2->exec($sql);
565       if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
566     }
567   }
568
569   // The markPaid marks a set of records as either paid or unpaid.
570   static function markPaid($time_log_ids, $expense_item_ids, $paid = true) {
571     global $user;
572     $mdb2 = getConnection();
573
574     $group_id = $user->getGroup();
575     $org_id = $user->org_id;
576
577     $paid_val = (int) $paid;
578     if ($time_log_ids) {
579       $sql = "update tt_log set paid = $paid_val".
580         " where id in(".join(', ', $time_log_ids).") and group_id = $group_id and org_id = $org_id";
581       $affected = $mdb2->exec($sql);
582       if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
583     }
584     if ($expense_item_ids) {
585       $sql = "update tt_expense_items set paid = $paid_val".
586         " where id in(".join(', ', $expense_item_ids).") and group_id = $group_id and org_id = $org_id";
587       $affected = $mdb2->exec($sql);
588       if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
589     }
590   }
591
592   // prepareReportBody - prepares an email body for report.
593   static function prepareReportBody($options, $comment = null)
594   {
595     global $user;
596     global $i18n;
597
598     // Determine these once as they are used in multiple places in this function.
599     $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
600     $isClient = $user->isClient();
601
602     $items = ttReportHelper::getItems($options);
603     $grouping = ttReportHelper::grouping($options);
604     if ($grouping)
605       $subtotals = ttReportHelper::getSubtotals($options);
606     $totals = ttReportHelper::getTotals($options);
607
608     // Use custom fields plugin if it is enabled.
609     if ($user->isPluginEnabled('cf'))
610       $custom_fields = new CustomFields();
611
612     // Define some styles to use in email.
613     $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
614     $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
615     $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
616     $rowItem = 'background-color: #ffffff;';
617     $rowItemAlt = 'background-color: #f5f5f5;';
618     $rowSubtotal = 'background-color: #e0e0e0;';
619     $cellLeftAligned = 'text-align: left; vertical-align: top;';
620     $cellRightAligned = 'text-align: right; vertical-align: top;';
621     $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
622     $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
623
624     // Start creating email body.
625     $body = '<html>';
626     $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
627     $body .= '<body>';
628
629     // Output title.
630     $body .= '<p style="'.$style_title.'">'.$i18n->get('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
631
632     // Output comment.
633     if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>';
634
635     if ($options['show_totals_only']) {
636       // Totals only report. Output subtotals.
637       $group_by_header = ttReportHelper::makeGroupByHeader($options);
638
639       $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
640       $body .= '<tr>';
641       $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
642       if ($options['show_duration'])
643         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
644       if ($options['show_work_units'])
645         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
646       if ($options['show_cost'])
647         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
648       $body .= '</tr>';
649       foreach($subtotals as $subtotal) {
650         $body .= '<tr style="'.$rowSubtotal.'">';
651         $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : '&nbsp;').'</td>';
652         if ($options['show_duration']) {
653           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
654           if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
655           $body .= '</td>';
656         }
657         if ($options['show_work_units']) {
658           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
659           $body .= $subtotal['units'];
660           $body .= '</td>';
661         }
662         if ($options['show_cost']) {
663           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
664           $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
665           $body .= '</td>';
666         }
667         $body .= '</tr>';
668       }
669
670       // Print totals.
671       $body .= '<tr><td>&nbsp;</td></tr>';
672       $body .= '<tr style="'.$rowSubtotal.'">';
673       $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
674       if ($options['show_duration']) {
675         $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
676         if ($totals['time'] <> '0:00') $body .= $totals['time'];
677         $body .= '</td>';
678       }
679       if ($options['show_work_units']) {
680         $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
681         $body .= $totals['units'];
682         $body .= '</td>';
683       }
684       if ($options['show_cost']) {
685         $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
686         $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
687         $body .= '</td>';
688       }
689       $body .= '</tr>';
690
691       $body .= '</table>';
692     } else {
693       // Regular report.
694
695       // Print table header.
696       $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
697       $body .= '<tr>';
698       $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.date').'</td>';
699       if ($canViewReports || $isClient)
700         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.user').'</td>';
701       if ($options['show_client'])
702         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.client').'</td>';
703       if ($options['show_project'])
704         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.project').'</td>';
705       if ($options['show_task'])
706         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.task').'</td>';
707       if ($options['show_custom_field_1'])
708         $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
709       if ($options['show_start'])
710         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.start').'</td>';
711       if ($options['show_end'])
712         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.finish').'</td>';
713       if ($options['show_duration'])
714         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
715       if ($options['show_work_units'])
716         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
717       if ($options['show_note'])
718         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.note').'</td>';
719       if ($options['show_cost'])
720         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
721       if ($options['show_paid'])
722         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.paid').'</td>';
723       if ($options['show_ip'])
724         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.ip').'</td>';
725       if ($options['show_invoice'])
726         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.invoice').'</td>';
727       $body .= '</tr>';
728
729       // Initialize variables to print subtotals.
730       if ($items && $grouping) {
731         $print_subtotals = true;
732         $first_pass = true;
733         $prev_grouped_by = '';
734         $cur_grouped_by = '';
735       }
736       // Initialize variables to alternate color of rows for different dates.
737       $prev_date = '';
738       $cur_date = '';
739       $row_style = $rowItem;
740
741       // Print report items.
742       if (is_array($items)) {
743         foreach ($items as $record) {
744           $cur_date = $record['date'];
745           // Print a subtotal row after a block of grouped items.
746           if ($print_subtotals) {
747             $cur_grouped_by = $record['grouped_by'];
748             if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
749               $body .= '<tr style="'.$rowSubtotal.'">';
750               $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
751               $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
752               if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['user'].'</td>';
753               if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['client'].'</td>';
754               if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['project'].'</td>';
755               if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['task'].'</td>';
756               if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['cf_1'].'</td>';
757               if ($options['show_start']) $body .= '<td></td>';
758               if ($options['show_end']) $body .= '<td></td>';
759               if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
760               if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['units'].'</td>';
761               if ($options['show_note']) $body .= '<td></td>';
762               if ($options['show_cost']) {
763                 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
764                 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
765                 $body .= '</td>';
766               }
767               if ($options['show_paid']) $body .= '<td></td>';
768               if ($options['show_ip']) $body .= '<td></td>';
769               if ($options['show_invoice']) $body .= '<td></td>';
770               $body .= '</tr>';
771               $body .= '<tr><td>&nbsp;</td></tr>';
772             }
773             $first_pass = false;
774           }
775
776           // Print a regular row.
777           if ($cur_date != $prev_date)
778             $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
779           $body .= '<tr style="'.$row_style.'">';
780           $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
781           if ($canViewReports || $isClient)
782             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
783           if ($options['show_client'])
784             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
785           if ($options['show_project'])
786             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
787           if ($options['show_task'])
788             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
789           if ($options['show_custom_field_1'])
790             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
791           if ($options['show_start'])
792             $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
793           if ($options['show_end'])
794             $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
795           if ($options['show_duration'])
796             $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
797           if ($options['show_work_units'])
798             $body .= '<td style="'.$cellRightAligned.'">'.$record['units'].'</td>';
799           if ($options['show_note'])
800             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
801           if ($options['show_cost'])
802             $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
803           if ($options['show_paid']) {
804             $body .= '<td style="'.$cellRightAligned.'">';
805             $body .= $record['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
806             $body .= '</td>';
807           }
808           if ($options['show_ip']) {
809             $body .= '<td style="'.$cellRightAligned.'">';
810             $body .= $record['modified'] ? $record['modified_ip'].' '.$record['modified'] : $record['created_ip'].' '.$record['created'];
811             $body .= '</td>';
812           }
813           if ($options['show_invoice'])
814             $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
815           $body .= '</tr>';
816
817           $prev_date = $record['date'];
818           if ($print_subtotals)
819             $prev_grouped_by = $record['grouped_by'];
820         }
821       }
822
823       // Print a terminating subtotal.
824       if ($print_subtotals) {
825         $body .= '<tr style="'.$rowSubtotal.'">';
826         $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
827         $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
828         if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['user'].'</td>';
829         if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['client'].'</td>';
830         if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['project'].'</td>';
831         if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['task'].'</td>';
832         if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['cf_1'].'</td>';
833         if ($options['show_start']) $body .= '<td></td>';
834         if ($options['show_end']) $body .= '<td></td>';
835         if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
836         if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['units'].'</td>';
837         if ($options['show_note']) $body .= '<td></td>';
838         if ($options['show_cost']) {
839           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
840           $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
841           $body .= '</td>';
842         }
843         if ($options['show_paid']) $body .= '<td></td>';
844         if ($options['show_ip']) $body .= '<td></td>';
845         if ($options['show_invoice']) $body .= '<td></td>';
846         $body .= '</tr>';
847       }
848
849       // Print totals.
850       $body .= '<tr><td>&nbsp;</td></tr>';
851       $body .= '<tr style="'.$rowSubtotal.'">';
852       $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
853       if ($canViewReports || $isClient) $body .= '<td></td>';
854       if ($options['show_client']) $body .= '<td></td>';
855       if ($options['show_project']) $body .= '<td></td>';
856       if ($options['show_task']) $body .= '<td></td>';
857       if ($options['show_custom_field_1']) $body .= '<td></td>';
858       if ($options['show_start']) $body .= '<td></td>';
859       if ($options['show_end']) $body .= '<td></td>';
860       if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
861       if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['units'].'</td>';
862       if ($options['show_note']) $body .= '<td></td>';
863       if ($options['show_cost']) {
864         $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
865         $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
866         $body .= '</td>';
867       }
868       if ($options['show_paid']) $body .= '<td></td>';
869       if ($options['show_ip']) $body .= '<td></td>';
870       if ($options['show_invoice']) $body .= '<td></td>';
871       $body .= '</tr>';
872
873       $body .= '</table>';
874     }
875
876     // Output footer.
877     if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
878       $body .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
879
880     // Finish creating email body.
881     $body .= '</body></html>';
882
883     return $body;
884   }
885
886   // checkFavReportCondition - checks whether it is okay to send fav report.
887   static function checkFavReportCondition($options, $condition)
888   {
889     $items = ttReportHelper::getItems($options);
890
891     $condition = trim(str_replace('count', '', $condition));
892
893     $greater_or_equal = ttStartsWith($condition, '>=');
894     if ($greater_or_equal) $condition = trim(str_replace('>=', '', $condition));
895
896     $less_or_equal = ttStartsWith($condition, '<=');
897     if ($less_or_equal) $condition = trim(str_replace('<=', '', $condition));
898
899     $not_equal = ttStartsWith($condition, '<>');
900     if ($not_equal) $condition = trim(str_replace('<>', '', $condition));
901
902     $greater = ttStartsWith($condition, '>');
903     if ($greater) $condition = trim(str_replace('>', '', $condition));
904
905     $less = ttStartsWith($condition, '<');
906     if ($less) $condition = trim(str_replace('<', '', $condition));
907
908     $equal = ttStartsWith($condition, '=');
909     if ($equal) $condition = trim(str_replace('=', '', $condition));
910
911     $count_required = (int) $condition;
912
913     if ($greater && count($items) > $count_required) return true;
914     if ($greater_or_equal && count($items) >= $count_required) return true;
915     if ($less && count($items) < $count_required) return true;
916     if ($less_or_equal && count($items) <= $count_required) return true;
917     if ($equal && count($items) == $count_required) return true;
918     if ($not_equal && count($items) <> $count_required) return true;
919
920     return false;
921   }
922
923   // sendFavReport - sends a favorite report to a specified email, called from cron.php
924   static function sendFavReport($options, $subject, $email, $cc) {
925     // We are called from cron.php, we have no $bean in session.
926     // cron.php sets global $user and $i18n objects to match our favorite report user.
927     global $user;
928     global $i18n;
929
930     // Prepare report body.
931     $body = ttReportHelper::prepareReportBody($options);
932
933     import('mail.Mailer');
934     $mailer = new Mailer();
935     $mailer->setCharSet(CHARSET);
936     $mailer->setContentType('text/html');
937     $mailer->setSender(SENDER);
938     if (!empty($cc))
939       $mailer->setReceiverCC($cc);
940     if (!empty($user->bcc_email))
941       $mailer->setReceiverBCC($user->bcc_email);
942     $mailer->setReceiver($email);
943     $mailer->setMailMode(MAIL_MODE);
944     if (empty($subject)) $subject = $options['name'];
945     if (!$mailer->send($subject, $body))
946       return false;
947
948     return true;
949   }
950
951   // getReportOptions - returns an array of report options constructed from session bean.
952   //
953   // Note: similarly to ttFavReportHelper::getReportOptions, this function is a part of
954   // refactoring to simplify maintenance of report generating functions, as we currently
955   // have 2 sets: normal reporting (from bean), and fav report emailing (from db fields).
956   // Using options obtained from either db or bean shall allow us to use only one set of functions.
957   static function getReportOptions($bean) {
958     global $user;
959
960     // Prepare an array of report options.
961     $options = array();
962
963     // Construct one by one.
964     $options['name'] = null; // No name required.
965     $options['user_id'] = $user->id; // Not sure if we need user_id here. Fav reports use it to recycle $user object in cron.php.
966     $options['client_id'] = $bean->getAttribute('client');
967     $options['cf_1_option_id'] = $bean->getAttribute('option');
968     $options['project_id'] = $bean->getAttribute('project');
969     $options['task_id'] = $bean->getAttribute('task');
970     $options['billable'] = $bean->getAttribute('include_records');
971     $options['invoice'] = $bean->getAttribute('invoice');
972     $options['paid_status'] = $bean->getAttribute('paid_status');
973     if (is_array($bean->getAttribute('users'))) $options['users'] = join(',', $bean->getAttribute('users'));
974     $options['period'] = $bean->getAttribute('period');
975     $options['period_start'] = $bean->getAttribute('start_date');
976     $options['period_end'] = $bean->getAttribute('end_date');
977     $options['show_client'] = $bean->getAttribute('chclient');
978     $options['show_invoice'] = $bean->getAttribute('chinvoice');
979     $options['show_paid'] = $bean->getAttribute('chpaid');
980     $options['show_ip'] = $bean->getAttribute('chip');
981     $options['show_project'] = $bean->getAttribute('chproject');
982     $options['show_start'] = $bean->getAttribute('chstart');
983     $options['show_duration'] = $bean->getAttribute('chduration');
984     $options['show_cost'] = $bean->getAttribute('chcost');
985     $options['show_task'] = $bean->getAttribute('chtask');
986     $options['show_end'] = $bean->getAttribute('chfinish');
987     $options['show_note'] = $bean->getAttribute('chnote');
988     $options['show_custom_field_1'] = $bean->getAttribute('chcf_1');
989     $options['show_work_units'] = $bean->getAttribute('chunits');
990     $options['show_totals_only'] = $bean->getAttribute('chtotalsonly');
991     $options['group_by1'] = $bean->getAttribute('group_by1');
992     $options['group_by2'] = $bean->getAttribute('group_by2');
993     $options['group_by3'] = $bean->getAttribute('group_by3');
994     return $options;
995   }
996
997   // verifyBean is a security function to make sure data in bean makes sense for a group.
998   static function verifyBean($bean) {
999     global $user;
1000
1001     // Check users.
1002     $users_in_bean = $bean->getAttribute('users');
1003     if (is_array($users_in_bean)) {
1004       $users_in_group = ttGroupHelper::getUsers();
1005       foreach ($users_in_group as $user_in_group) {
1006         $valid_ids[] = $user_in_group['id'];
1007       }
1008       foreach ($users_in_bean as $user_in_bean) {
1009         if (!in_array($user_in_bean, $valid_ids)) {
1010           return false;
1011         }
1012       }
1013     }
1014
1015     // TODO: add additional checks here. Perhaps do it before saving the bean for consistency.
1016     return true;
1017   }
1018
1019   // makeGroupByKey builds a combined group by key from group_by1, group_by2 and group_by3 values
1020   // (passed in $options) and a row of data ($row obtained from a db query).
1021   static function makeGroupByKey($options, $row) {
1022     if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1023       // We have group_by1.
1024       $group_by1 = $options['group_by1'];
1025       $group_by1_value = $row[$group_by1];
1026       //if ($group_by1 == 'date') $group_by1_value = ttDateToUserFormat($group_by1_value);
1027       if (empty($group_by1_value)) $group_by1_value = 'Null'; // To match what comes out of makeConcatPart.
1028       $group_by_key .= ' - '.$group_by1_value;
1029     }
1030     if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1031       // We have group_by2.
1032       $group_by2 = $options['group_by2'];
1033       $group_by2_value = $row[$group_by2];
1034       //if ($group_by2 == 'date') $group_by2_value = ttDateToUserFormat($group_by2_value);
1035       if (empty($group_by2_value)) $group_by2_value = 'Null'; // To match what comes out of makeConcatPart.
1036       $group_by_key .= ' - '.$group_by2_value;
1037     }
1038     if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1039       // We have group_by3.
1040       $group_by3 = $options['group_by3'];
1041       $group_by3_value = $row[$group_by3];
1042       //if ($group_by3 == 'date') $group_by3_value = ttDateToUserFormat($group_by3_value);
1043       if (empty($group_by3_value)) $group_by3_value = 'Null'; // To match what comes out of makeConcatPart.
1044       $group_by_key .= ' - '.$group_by3_value;
1045     }
1046     $group_by_key = trim($group_by_key, ' -');
1047     return $group_by_key;
1048   }
1049
1050   // makeGroupByPart builds a combined group by part for sql query for time items using group_by1,
1051   // group_by2, and group_by3 values passed in $options.
1052   static function makeGroupByPart($options) {
1053     if (!ttReportHelper::grouping($options)) return null;
1054
1055     $group_by1 = $options['group_by1'];
1056     $group_by2 = $options['group_by2'];
1057     $group_by3 = $options['group_by3'];
1058
1059     switch ($group_by1) {
1060       case 'date':
1061         $group_by_parts .= ', l.date';
1062         break;
1063       case 'user':
1064         $group_by_parts .= ', u.name';
1065         break;
1066       case 'client':
1067         $group_by_parts .= ', c.name';
1068         break;
1069       case 'project':
1070         $group_by_parts .= ', p.name';
1071         break;
1072       case 'task':
1073         $group_by_parts .= ', t.name';
1074         break;
1075       case 'cf_1':
1076         $group_by_parts .= ', cfo.value';
1077         break;
1078     }
1079     switch ($group_by2) {
1080       case 'date':
1081         $group_by_parts .= ', l.date';
1082         break;
1083       case 'user':
1084         $group_by_parts .= ', u.name';
1085         break;
1086       case 'client':
1087         $group_by_parts .= ', c.name';
1088         break;
1089       case 'project':
1090         $group_by_parts .= ', p.name';
1091         break;
1092       case 'task':
1093         $group_by_parts .= ', t.name';
1094         break;
1095       case 'cf_1':
1096         $group_by_parts .= ', cfo.value';
1097         break;
1098     }
1099     switch ($group_by3) {
1100       case 'date':
1101         $group_by_parts .= ', l.date';
1102         break;
1103       case 'user':
1104         $group_by_parts .= ', u.name';
1105         break;
1106       case 'client':
1107         $group_by_parts .= ', c.name';
1108         break;
1109       case 'project':
1110         $group_by_parts .= ', p.name';
1111         break;
1112       case 'task':
1113         $group_by_parts .= ', t.name';
1114         break;
1115       case 'cf_1':
1116         $group_by_parts .= ', cfo.value';
1117         break;
1118     }
1119     // Remove garbage from the beginning.
1120     $group_by_parts = ltrim($group_by_parts, ', ');
1121     $group_by_part = "group by $group_by_parts";
1122     return $group_by_part;
1123   }
1124
1125   // makeGroupByExpensesPart builds a combined group by part for sql query for expense items using
1126   // group_by1, group_by2, and group_by3 values passed in $options.
1127   static function makeGroupByExpensesPart($options) {
1128     $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
1129       ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
1130       ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
1131     if ($no_grouping) return null;
1132
1133     $group_by1 = $options['group_by1'];
1134     $group_by2 = $options['group_by2'];
1135     $group_by3 = $options['group_by3'];
1136
1137     switch ($group_by1) {
1138       case 'date':
1139         $group_by_parts .= ', ei.date';
1140         break;
1141       case 'user':
1142         $group_by_parts .= ', u.name';
1143         break;
1144       case 'client':
1145         $group_by_parts .= ', c.name';
1146         break;
1147       case 'project':
1148         $group_by_parts .= ', p.name';
1149         break;
1150     }
1151     switch ($group_by2) {
1152       case 'date':
1153         $group_by_parts .= ', ei.date';
1154         break;
1155       case 'user':
1156         $group_by_parts .= ', u.name';
1157         break;
1158       case 'client':
1159         $group_by_parts .= ', c.name';
1160         break;
1161       case 'project':
1162         $group_by_parts .= ', p.name';
1163         break;
1164     }
1165     switch ($group_by3) {
1166       case 'date':
1167         $group_by_parts .= ', ei.date';
1168         break;
1169       case 'user':
1170         $group_by_parts .= ', u.name';
1171         break;
1172       case 'client':
1173         $group_by_parts .= ', c.name';
1174         break;
1175       case 'project':
1176         $group_by_parts .= ', p.name';
1177         break;
1178     }
1179     // Remove garbage from the beginning.
1180     $group_by_parts = ltrim($group_by_parts, ', ');
1181     if ($group_by_parts)
1182       $group_by_part = "group by $group_by_parts";
1183     return $group_by_part;
1184   }
1185
1186   // makeConcatPart builds a concatenation part for getSubtotals query (for time items).
1187   static function makeConcatPart($options) {
1188     $group_by1 = $options['group_by1'];
1189     $group_by2 = $options['group_by2'];
1190     $group_by3 = $options['group_by3'];
1191
1192     switch ($group_by1) {
1193       case 'date':
1194         $what_to_concat .= ", ' - ', l.date";
1195         break;
1196       case 'user':
1197         $what_to_concat .= ", ' - ', u.name";
1198         $fields_part .= ', u.name as user';
1199         break;
1200       case 'client':
1201         $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1202         $fields_part .= ', c.name as client';
1203         break;
1204       case 'project':
1205         $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1206         $fields_part .= ', p.name as project';
1207         break;
1208       case 'task':
1209         $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1210         $fields_part .= ', t.name as task';
1211         break;
1212       case 'cf_1':
1213         $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1214         $fields_part .= ', cfo.value as cf_1';
1215         break;
1216     }
1217     switch ($group_by2) {
1218       case 'date':
1219         $what_to_concat .= ", ' - ', l.date";
1220         break;
1221       case 'user':
1222         $what_to_concat .= ", ' - ', u.name";
1223         $fields_part .= ', u.name as user';
1224         break;
1225       case 'client':
1226         $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1227         $fields_part .= ', c.name as client';
1228         break;
1229       case 'project':
1230         $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1231         $fields_part .= ', p.name as project';
1232         break;
1233       case 'task':
1234         $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1235         $fields_part .= ', t.name as task';
1236         break;
1237       case 'cf_1':
1238         $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1239         $fields_part .= ', cfo.value as cf_1';
1240         break;
1241     }
1242     switch ($group_by3) {
1243       case 'date':
1244         $what_to_concat .= ", ' - ', l.date";
1245         break;
1246       case 'user':
1247         $what_to_concat .= ", ' - ', u.name";
1248         $fields_part .= ', u.name as user';
1249         break;
1250       case 'client':
1251         $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1252         $fields_part .= ', c.name as client';
1253         break;
1254       case 'project':
1255         $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1256         $fields_part .= ', p.name as project';
1257         break;
1258       case 'task':
1259         $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1260         $fields_part .= ', t.name as task';
1261         break;
1262       case 'cf_1':
1263         $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1264         $fields_part .= ', cfo.value as cf_1';
1265         break;
1266     }
1267     // Remove garbage from both ends.
1268     $what_to_concat = trim($what_to_concat, "', -");
1269     $concat_part = "concat($what_to_concat) as group_field";
1270     $concat_part = trim($concat_part, ' -');
1271     return "$concat_part $fields_part";
1272   }
1273
1274   // makeConcatPart builds a concatenation part for getSubtotals query (for expense items).
1275   static function makeConcatExpensesPart($options) {
1276     $group_by1 = $options['group_by1'];
1277     $group_by2 = $options['group_by2'];
1278     $group_by3 = $options['group_by3'];
1279
1280     switch ($group_by1) {
1281       case 'date':
1282         $what_to_concat .= ", ' - ', ei.date";
1283         break;
1284       case 'user':
1285         $what_to_concat .= ", ' - ', u.name";
1286         $fields_part .= ', u.name as user';
1287         break;
1288       case 'client':
1289         $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1290         $fields_part .= ', c.name as client';
1291         break;
1292       case 'project':
1293         $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1294         $fields_part .= ', p.name as project';
1295         break;
1296
1297       case 'task':
1298         $what_to_concat .= ", ' - ', 'Null'";
1299         $fields_part .= ', null as task';
1300         break;
1301
1302       case 'cf_1':
1303         $what_to_concat .= ", ' - ', 'Null'";
1304         $fields_part .= ', null as cf_1';
1305         break;
1306     }
1307     switch ($group_by2) {
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_by3) {
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     // Remove garbage from the beginning.
1362     if ($what_to_concat)
1363         $what_to_concat = substr($what_to_concat, 8);
1364     $concat_part = "concat($what_to_concat) as group_field";
1365     return "$concat_part $fields_part";
1366   }
1367
1368   // makeCombinedSelectPart builds a list of fields for a combined select on a union for getSubtotals.
1369   // This is used when we include expenses.
1370   static function makeCombinedSelectPart($options) {
1371     $group_by1 = $options['group_by1'];
1372     $group_by2 = $options['group_by2'];
1373     $group_by3 = $options['group_by3'];
1374
1375     $fields = "group_field";
1376
1377     switch ($group_by1) {
1378       case 'user':
1379         $fields .= ', user';
1380         break;
1381       case 'client':
1382         $fields_part .= ', client';
1383         break;
1384       case 'project':
1385         $fields .= ', project';
1386         break;
1387
1388       case 'task':
1389         $fields .= ', task';
1390         break;
1391
1392       case 'cf_1':
1393         $fields .= ', cf_1';
1394         break;
1395     }
1396     switch ($group_by2) {
1397       case 'user':
1398         $fields .= ', user';
1399         break;
1400       case 'client':
1401         $fields_part .= ', client';
1402         break;
1403       case 'project':
1404         $fields .= ', project';
1405         break;
1406
1407       case 'task':
1408         $fields .= ', task';
1409         break;
1410
1411       case 'cf_1':
1412         $fields .= ', cf_1';
1413         break;
1414     }
1415     switch ($group_by3) {
1416       case 'user':
1417         $fields .= ', user';
1418         break;
1419       case 'client':
1420         $fields_part .= ', client';
1421         break;
1422       case 'project':
1423         $fields .= ', project';
1424         break;
1425
1426       case 'task':
1427         $fields .= ', task';
1428         break;
1429
1430       case 'cf_1':
1431         $fields .= ', cf_1';
1432         break;
1433     }
1434     return $fields;
1435   }
1436
1437   // makeJoinPart builds a left join part for getSubtotals query (for time items).
1438   static function makeJoinPart($options) {
1439     global $user;
1440
1441     $trackingMode = $user->getTrackingMode();
1442     if (ttReportHelper::groupingBy('user', $options) || MODE_TIME == $trackingMode) {
1443       $join .= ' left join tt_users u on (l.user_id = u.id)';
1444     }
1445     if (ttReportHelper::groupingBy('client', $options)) {
1446       $join .= ' left join tt_clients c on (l.client_id = c.id)';
1447     }
1448     if (ttReportHelper::groupingBy('project', $options)) {
1449       $join .= ' left join tt_projects p on (l.project_id = p.id)';
1450     }
1451     if (ttReportHelper::groupingBy('task', $options)) {
1452       $join .= ' left join tt_tasks t on (l.task_id = t.id)';
1453     }
1454     if (ttReportHelper::groupingBy('cf_1', $options)) {
1455       $custom_fields = new CustomFields();
1456       if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
1457         $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)';
1458       elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
1459         $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)';
1460     }
1461     if ($options['show_cost'] && $trackingMode != MODE_TIME) {
1462       $join .= ' left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)';
1463     }
1464     return $join;
1465   }
1466
1467   // makeWorkUnitPart builds an sql part for work units for time items.
1468   static function makeWorkUnitPart($options) {
1469     global $user;
1470
1471     $workUnits = $options['show_work_units'];
1472     if ($workUnits) {
1473       $unitTotalsOnly = $user->getConfigOption('unit_totals_only');
1474       $firstUnitThreshold = $user->getConfigInt('1st_unit_threshold', 0);
1475       $minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
1476       if ($unitTotalsOnly)
1477         $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";
1478       else
1479         $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";
1480     }
1481     return $work_unit_part;
1482   }
1483
1484   // makeCostPart builds a cost part for time items.
1485   static function makeCostPart($options) {
1486     global $user;
1487
1488     if ($options['show_cost']) {
1489       if (MODE_TIME == $user->getTrackingMode())
1490         $cost_part = ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost";
1491       else
1492         $cost_part .= ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost";
1493     }
1494     return $cost_part;
1495   }
1496
1497   // makeJoinExpensesPart builds a left join part for getSubtotals query for expense items.
1498   static function makeJoinExpensesPart($options) {
1499     if (ttReportHelper::groupingBy('user', $options)) {
1500       $join .= ' left join tt_users u on (ei.user_id = u.id)';
1501     }
1502     if (ttReportHelper::groupingBy('client', $options)) {
1503       $join .= ' left join tt_clients c on (ei.client_id = c.id)';
1504     }
1505     if (ttReportHelper::groupingBy('project', $options)) {
1506       $join .= ' left join tt_projects p on (ei.project_id = p.id)';
1507     }
1508     return $join;
1509   }
1510
1511   // grouping determines if we are grouping the report by either group_by1,
1512   // group_by2, or group_by3 values passed in $options.
1513   static function grouping($options) {
1514     $grouping = ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') ||
1515       ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') ||
1516       ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping');
1517     return $grouping;
1518   }
1519
1520   // groupingBy determines if we are grouping a report by a value of $what
1521   // ('date', 'user', 'project', etc.) by checking group_by1, group_by2,
1522   // and group_by3 values passed in $options.
1523   static function groupingBy($what, $options) {
1524     $grouping = ($options['group_by1'] == $what) || ($options['group_by2'] == $what) || ($options['group_by3'] == $what);
1525     return $grouping;
1526   }
1527
1528   // makeGroupByHeader builds a column header for a totals-only report using group_by1,
1529   // group_by2, and group_by3 values passed in $options.
1530   static function makeGroupByHeader($options) {
1531     global $i18n;
1532     global $custom_fields;
1533
1534     $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
1535       ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
1536       ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
1537     if ($no_grouping) return null;
1538
1539     if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1540       // We have group_by1.
1541       $group_by1 = $options['group_by1'];
1542       if ('cf_1' == $group_by1)
1543         $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1544       else {
1545         $key = 'label.'.$group_by1;
1546         $group_by_header .= ' - '.$i18n->get($key);
1547       }
1548     }
1549     if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1550       // We have group_by2.
1551       $group_by2 = $options['group_by2'];
1552       if ('cf_1' == $group_by2)
1553         $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1554       else {
1555         $key = 'label.'.$group_by2;
1556         $group_by_header .= ' - '.$i18n->get($key);
1557       }
1558     }
1559     if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1560       // We have group_by3.
1561       $group_by3 = $options['group_by3'];
1562       if ('cf_1' == $group_by3)
1563         $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1564       else {
1565         $key = 'label.'.$group_by3;
1566         $group_by_header .= ' - '.$i18n->get($key);
1567       }
1568     }
1569     $group_by_header = ltrim($group_by_header, ' -');
1570     return $group_by_header;
1571   }
1572
1573   // makeGroupByXmlTag creates an xml tag for a totals only report using group_by1,
1574   // group_by2, and group_by3 values passed in $options.
1575   static function makeGroupByXmlTag($options) {
1576     if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1577       // We have group_by1.
1578       $tag .= '_'.$options['group_by1'];
1579     }
1580     if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1581       // We have group_by2.
1582       $tag .= '_'.$options['group_by2'];
1583     }
1584     if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1585       // We have group_by3.
1586       $tag .= '_'.$options['group_by3'];
1587     }
1588     $tag = ltrim($tag, '_');
1589     return $tag;
1590   }
1591
1592   // makeGroupByLabel builds a label for one row in a "Totals only" report of grouped by items.
1593   // It does one thing: if we are grouping by date, the date format is converted for user.
1594   static function makeGroupByLabel($key, $options) {
1595     if (!ttReportHelper::groupingBy('date', $options))
1596       return $key; // No need to format.
1597
1598     global $user;
1599     if ($user->date_format == DB_DATEFORMAT)
1600       return $key; // No need to format.
1601
1602     $label = $key;
1603     if (preg_match('/\d\d\d\d-\d\d-\d\d/', $key, $matches)) {
1604       // Replace the first found match of a date in DB_DATEFORMAT.
1605       // This is not entirely clean but better than nothing for a label in a row.
1606       $userDate = ttDateToUserFormat($matches[0]);
1607       $label = str_replace($matches[0], $userDate, $key);
1608     }
1609     return $label;
1610   }
1611 }