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