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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('ttClientHelper');
30 import('DateAndTime');
32 import('ttTimeHelper');
34 require_once(dirname(__FILE__).'/../../plugins/CustomFields.class.php');
36 // Definitions of types for timesheet dropdown.
37 define('TIMESHEET_ALL', 0); // Include all records.
38 define('TIMESHEET_NOT_ASSIGNED', 1); // Include records not assigned to timesheets.
39 define('TIMESHEET_ASSIGNED', 2); // Include records assigned to timesheets.
40 define('TIMESHEET_PENDING', 3); // Include records in submitted timesheets that are pending manager approval.
41 define('TIMESHEET_APPROVED', 4); // Include records in approved timesheets.
42 define('TIMESHEET_NOT_APPROVED', 5); // Include records in disapproved timesheets.
44 // Class ttReportHelper is used for help with reports.
45 class ttReportHelper {
47 // getWhere prepares a WHERE clause for a report query.
48 static function getWhere($options) {
51 $group_id = $user->getGroup();
52 $org_id = $user->org_id;
54 // A shortcut for timesheets.
55 if ($options['timesheet_id']) {
56 $where = " where l.timesheet_id = ".$options['timesheet_id']." and l.group_id = $group_id and l.org_id = $org_id";
60 // Prepare dropdown parts.
62 if ($options['client_id'])
63 $dropdown_parts .= ' and l.client_id = '.$options['client_id'];
64 elseif ($user->isClient() && $user->client_id)
65 $dropdown_parts .= ' and l.client_id = '.$user->client_id;
66 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'].')';
67 if ($options['project_id']) $dropdown_parts .= ' and l.project_id = '.$options['project_id'];
68 if ($options['task_id']) $dropdown_parts .= ' and l.task_id = '.$options['task_id'];
69 if ($options['billable']=='1') $dropdown_parts .= ' and l.billable = 1';
70 if ($options['billable']=='2') $dropdown_parts .= ' and l.billable = 0';
71 if ($options['invoice']=='1') $dropdown_parts .= ' and l.invoice_id is not null';
72 if ($options['invoice']=='2') $dropdown_parts .= ' and l.invoice_id is null';
73 if ($options['timesheet']==TIMESHEET_NOT_ASSIGNED) $dropdown_parts .= ' and l.timesheet_id is null';
74 if ($options['timesheet']==TIMESHEET_ASSIGNED) $dropdown_parts .= ' and l.timesheet_id is not null';
75 if ($options['approved']=='1') $dropdown_parts .= ' and l.approved = 1';
76 if ($options['approved']=='2') $dropdown_parts .= ' and l.approved = 0';
77 if ($options['paid_status']=='1') $dropdown_parts .= ' and l.paid = 1';
78 if ($options['paid_status']=='2') $dropdown_parts .= ' and l.paid = 0';
80 // Prepare sql query part for user list.
81 $userlist = $options['users'] ? $options['users'] : '-1';
82 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
83 $user_list_part = " and l.user_id in ($userlist)";
85 $user_list_part = " and l.user_id = ".$user->getUser();
86 $user_list_part .= " and l.group_id = $group_id and l.org_id = $org_id";
88 // Prepare sql query part for where.
89 $dateFormat = $user->getDateFormat();
90 if ($options['period'])
91 $period = new Period($options['period'], new DateAndTime($dateFormat));
93 $period = new Period();
95 new DateAndTime($dateFormat, $options['period_start']),
96 new DateAndTime($dateFormat, $options['period_end']));
98 $where = " where l.status = 1 and l.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and l.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
99 " $user_list_part $dropdown_parts";
103 // getExpenseWhere prepares WHERE clause for expenses query in a report.
104 static function getExpenseWhere($options) {
107 $group_id = $user->getGroup();
108 $org_id = $user->org_id;
110 // A shortcut for timesheets.
111 if ($options['timesheet_id']) {
112 $where = " where ei.timesheet_id = ".$options['timesheet_id']." and ei.group_id = $group_id and ei.org_id = $org_id";
116 // Prepare dropdown parts.
117 $dropdown_parts = '';
118 if ($options['client_id'])
119 $dropdown_parts .= ' and ei.client_id = '.$options['client_id'];
120 elseif ($user->isClient() && $user->client_id)
121 $dropdown_parts .= ' and ei.client_id = '.$user->client_id;
122 if ($options['project_id']) $dropdown_parts .= ' and ei.project_id = '.$options['project_id'];
123 if ($options['invoice']=='1') $dropdown_parts .= ' and ei.invoice_id is not null';
124 if ($options['invoice']=='2') $dropdown_parts .= ' and ei.invoice_id is null';
125 if ($options['timesheet']==TIMESHEET_NOT_ASSIGNED) $dropdown_parts .= ' and ei.timesheet_id is null';
126 if ($options['timesheet']==TIMESHEET_ASSIGNED) $dropdown_parts .= ' and ei.timesheet_id is not null';
127 if ($options['approved']=='1') $dropdown_parts .= ' and ei.approved = 1';
128 if ($options['approved']=='2') $dropdown_parts .= ' and ei.approved = 0';
129 if ($options['paid_status']=='1') $dropdown_parts .= ' and ei.paid = 1';
130 if ($options['paid_status']=='2') $dropdown_parts .= ' and ei.paid = 0';
132 // Prepare sql query part for user list.
133 $userlist = $options['users'] ? $options['users'] : '-1';
134 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
135 $user_list_part = " and ei.user_id in ($userlist)";
137 $user_list_part = " and ei.user_id = ".$user->getUser();
138 $user_list_part .= " and ei.group_id = $group_id and ei.org_id = $org_id";
140 // Prepare sql query part for where.
141 $dateFormat = $user->getDateFormat();
142 if ($options['period'])
143 $period = new Period($options['period'], new DateAndTime($dateFormat));
145 $period = new Period();
147 new DateAndTime($dateFormat, $options['period_start']),
148 new DateAndTime($dateFormat, $options['period_end']));
150 $where = " where ei.status = 1 and ei.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and ei.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
151 " $user_list_part $dropdown_parts";
155 // getItems retrieves all items associated with a report.
156 // It combines tt_log and tt_expense_items in one array for presentation in one table using mysql union all.
157 // Expense items use the "note" field for item name.
158 static function getItems($options) {
160 $mdb2 = getConnection();
162 // Determine these once as they are used in multiple places in this function.
163 $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
164 $isClient = $user->isClient();
166 $grouping = ttReportHelper::grouping($options);
168 $grouping_by_date = ttReportHelper::groupingBy('date', $options);
169 $grouping_by_client = ttReportHelper::groupingBy('client', $options);
170 $grouping_by_project = ttReportHelper::groupingBy('project', $options);
171 $grouping_by_task = ttReportHelper::groupingBy('task', $options);
172 $grouping_by_user = ttReportHelper::groupingBy('user', $options);
173 $grouping_by_cf_1 = ttReportHelper::groupingBy('cf_1', $options);
175 $convertTo12Hour = ('%I:%M %p' == $user->getTimeFormat()) && ($options['show_start'] || $options['show_end']);
176 $trackingMode = $user->getTrackingMode();
177 $decimalMark = $user->getDecimalMark();
179 // Prepare a query for time items in tt_log table.
180 $fields = array(); // An array of fields for database query.
181 array_push($fields, 'l.id');
182 array_push($fields, 'l.user_id');
183 array_push($fields, '1 as type'); // Type 1 is for tt_log entries.
184 array_push($fields, 'l.date');
185 if($canViewReports || $isClient)
186 array_push($fields, 'u.name as user');
187 // Add client name if it is selected.
188 if ($options['show_client'] || $grouping_by_client)
189 array_push($fields, 'c.name as client');
190 // Add project name if it is selected.
191 if ($options['show_project'] || $grouping_by_project)
192 array_push($fields, 'p.name as project');
193 // Add task name if it is selected.
194 if ($options['show_task'] || $grouping_by_task)
195 array_push($fields, 't.name as task');
197 $include_cf_1 = $options['show_custom_field_1'] || $grouping_by_cf_1;
199 $custom_fields = new CustomFields();
200 $cf_1_type = $custom_fields->fields[0]['type'];
201 if ($cf_1_type == CustomFields::TYPE_TEXT) {
202 array_push($fields, 'cfl.value as cf_1');
203 } elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
204 array_push($fields, 'cfo.value as cf_1');
208 if ($options['show_start']) {
209 array_push($fields, "l.start as unformatted_start");
210 array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start");
213 if ($options['show_end'])
214 array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish");
216 if ($options['show_duration'])
217 array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
219 if ($options['show_work_units']) {
220 if ($user->getConfigOption('unit_totals_only'))
221 array_push($fields, "null as units");
223 $firstUnitThreshold = $user->getConfigInt('1st_unit_threshold', 0);
224 $minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
225 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");
229 if ($options['show_note'])
230 array_push($fields, 'l.comment as note');
232 $includeCost = $options['show_cost'];
234 if (MODE_TIME == $trackingMode)
235 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.
237 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.
238 array_push($fields, "null as expense");
241 if ($options['show_approved'])
242 array_push($fields, 'l.approved');
244 if ($canViewReports && $options['show_paid'])
245 array_push($fields, 'l.paid');
247 if ($canViewReports && $options['show_ip']) {
248 array_push($fields, 'l.created');
249 array_push($fields, 'l.created_ip');
250 array_push($fields, 'l.modified');
251 array_push($fields, 'l.modified_ip');
253 // Add invoice name if it is selected.
254 if (($canViewReports || $isClient) && $options['show_invoice'])
255 array_push($fields, 'i.name as invoice');
256 // Add timesheet name if it is selected.
257 if ($options['show_timesheet'])
258 array_push($fields, 'ts.name as timesheet_name');
260 // Prepare sql query part for left joins.
262 if ($options['show_client'] || $grouping_by_client)
263 $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
264 if (($canViewReports || $isClient) && $options['show_invoice'])
265 $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
266 if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
267 $left_joins .= " left join tt_users u on (u.id = l.user_id)";
268 if ($options['show_project'] || $grouping_by_project)
269 $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
270 if ($options['show_task'] || $grouping_by_task)
271 $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
273 if ($cf_1_type == CustomFields::TYPE_TEXT)
274 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)";
275 elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
276 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)".
277 " left join tt_custom_field_options cfo on (cfl.option_id = cfo.id)";
280 if ($includeCost && MODE_TIME != $trackingMode)
281 $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
283 // Prepare sql query part for inner joins.
285 if ($user->isPluginEnabled('ts')) {
286 $timesheet_option = $options['timesheet'];
287 if ($timesheet_option == TIMESHEET_PENDING)
288 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.submit_status = 1 and ts.approval_status is null)";
289 else if ($timesheet_option == TIMESHEET_APPROVED)
290 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approval_status = 1)";
291 else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
292 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approval_status = 0)";
293 else if ($options['show_timesheet'])
294 $inner_joins .= " left join tt_timesheets ts on (l.timesheet_id = ts.id)"; // Left join for timesheet nme.
297 $where = ttReportHelper::getWhere($options);
299 // Construct sql query for tt_log items.
300 $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $inner_joins $where";
301 // If we don't have expense items (such as when the Expenses plugin is disabled), the above is all sql we need,
302 // with an exception of sorting part, that is added in the end.
304 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
305 if ($options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
307 $fields = array(); // An array of fields for database query.
308 array_push($fields, 'ei.id');
309 array_push($fields, 'ei.user_id');
310 array_push($fields, '2 as type'); // Type 2 is for tt_expense_items entries.
311 array_push($fields, 'ei.date');
312 if($canViewReports || $isClient)
313 array_push($fields, 'u.name as user');
314 // Add client name if it is selected.
315 if ($options['show_client'] || $grouping_by_client)
316 array_push($fields, 'c.name as client');
317 // Add project name if it is selected.
318 if ($options['show_project'] || $grouping_by_project)
319 array_push($fields, 'p.name as project');
320 if ($options['show_task'] || $grouping_by_task)
321 array_push($fields, 'null'); // null for task name. We need to match column count for union.
322 if ($options['show_custom_field_1'] || $grouping_by_cf_1)
323 array_push($fields, 'null'); // null for cf_1.
324 if ($options['show_start']) {
325 array_push($fields, 'null'); // null for unformatted_start.
326 array_push($fields, 'null'); // null for start.
328 if ($options['show_end'])
329 array_push($fields, 'null'); // null for finish.
330 if ($options['show_duration'])
331 array_push($fields, 'null'); // null for duration.
332 if ($options['show_work_units'])
333 array_push($fields, 'null as units'); // null for work units.
334 // Use the note field to print item name.
335 if ($options['show_note'])
336 array_push($fields, 'ei.name as note');
337 array_push($fields, 'ei.cost as cost');
338 array_push($fields, 'ei.cost as expense');
340 if ($options['show_approved'])
341 array_push($fields, 'ei.approved');
343 if ($canViewReports && $options['show_paid'])
344 array_push($fields, 'ei.paid');
346 if ($canViewReports && $options['show_ip']) {
347 array_push($fields, 'ei.created');
348 array_push($fields, 'ei.created_ip');
349 array_push($fields, 'ei.modified');
350 array_push($fields, 'ei.modified_ip');
352 // Add invoice name if it is selected.
353 if (($canViewReports || $isClient) && $options['show_invoice'])
354 array_push($fields, 'i.name as invoice');
355 if ($options['show_timesheet'])
356 array_push($fields, 'ts.name as timesheet_name');
358 // Prepare sql query part for left joins.
360 if ($canViewReports || $isClient)
361 $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
362 if ($options['show_client'] || $grouping_by_client)
363 $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
364 if ($options['show_project'] || $grouping_by_project)
365 $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
366 if (($canViewReports || $isClient) && $options['show_invoice'])
367 $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
369 // Prepare sql query part for inner joins.
371 if ($user->isPluginEnabled('ts')) {
372 $timesheet_option = $options['timesheet'];
373 if ($timesheet_option == TIMESHEET_PENDING)
374 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.submit_status = 1 and ts.approval_status is null)";
375 else if ($timesheet_option == TIMESHEET_APPROVED)
376 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.approval_status = 1)";
377 else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
378 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.approval_status = 0)";
379 else if ($options['show_timesheet'])
380 $inner_joins .= " left join tt_timesheets ts on (ei.timesheet_id = ts.id)"; // Left join for timesheet name.
383 $where = ttReportHelper::getExpenseWhere($options);
385 // Construct sql query for expense items.
386 $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $inner_joins $where";
388 // Construct a union.
389 $sql = "($sql) union all ($sql_for_expense_items)";
392 // Determine sort part.
393 $sort_part = ' order by ';
395 $sort_part2 .= ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') ? ', '.$options['group_by1'] : '';
396 $sort_part2 .= ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') ? ', '.$options['group_by2'] : '';
397 $sort_part2 .= ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') ? ', '.$options['group_by3'] : '';
398 if (!$grouping_by_date) $sort_part2 .= ', date';
399 $sort_part .= ltrim($sort_part2, ', '); // Remove leading comma and space.
401 $sort_part .= 'date';
403 if (($canViewReports || $isClient) && $options['users'] && !$grouping_by_user)
404 $sort_part .= ', user, type';
405 if ($options['show_start'])
406 $sort_part .= ', unformatted_start';
407 $sort_part .= ', id';
410 // By now we are ready with sql.
412 // Obtain items for report.
413 $res = $mdb2->query($sql);
414 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
416 while ($val = $res->fetchRow()) {
417 if ($convertTo12Hour) {
418 if($val['start'] != '')
419 $val['start'] = ttTimeHelper::to12HourFormat($val['start']);
420 if($val['finish'] != '')
421 $val['finish'] = ttTimeHelper::to12HourFormat($val['finish']);
423 if (isset($val['cost'])) {
424 if ('.' != $decimalMark)
425 $val['cost'] = str_replace('.', $decimalMark, $val['cost']);
427 if (isset($val['expense'])) {
428 if ('.' != $decimalMark)
429 $val['expense'] = str_replace('.', $decimalMark, $val['expense']);
432 if ($grouping) $val['grouped_by'] = ttReportHelper::makeGroupByKey($options, $val);
433 $val['date'] = ttDateToUserFormat($val['date']);
435 $report_items[] = $val;
438 return $report_items;
441 // putInSession stores tt_log and tt_expense_items ids from a report in user session
442 // as 2 comma-separated lists.
443 static function putInSession($report_items) {
444 unset($_SESSION['report_item_ids']);
445 unset($_SESSION['report_item_expense_ids']);
447 // Iterate through records and build 2 comma-separated lists.
448 foreach($report_items as $item) {
449 if ($item['type'] == 1)
450 $report_item_ids .= ','.$item['id'];
451 else if ($item['type'] == 2)
452 $report_item_expense_ids .= ','.$item['id'];
454 $report_item_ids = trim($report_item_ids, ',');
455 $report_item_expense_ids = trim($report_item_expense_ids, ',');
457 // The lists are reqdy. Put them in session.
458 if ($report_item_ids) $_SESSION['report_item_ids'] = $report_item_ids;
459 if ($report_item_expense_ids) $_SESSION['report_item_expense_ids'] = $report_item_expense_ids;
462 // getFromSession obtains tt_log and tt_expense_items ids stored in user session.
463 static function getFromSession() {
465 $report_item_ids = $_SESSION['report_item_ids'];
466 if ($report_item_ids)
467 $items['report_item_ids'] = explode(',', $report_item_ids);
468 $report_item_expense_ids = $_SESSION['report_item_expense_ids'];
469 if ($report_item_expense_ids)
470 $items['report_item_expense_ids'] = explode(',', $report_item_expense_ids);
474 // getSubtotals calculates report items subtotals when a report is grouped by.
475 // Without expenses, it's a simple select with group by.
476 // With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
477 static function getSubtotals($options) {
479 $mdb2 = getConnection();
481 $concat_part = ttReportHelper::makeConcatPart($options);
482 $work_unit_part = ttReportHelper::makeWorkUnitPart($options);
483 $join_part = ttReportHelper::makeJoinPart($options);
484 $cost_part = ttReportHelper::makeCostPart($options);
485 $where = ttReportHelper::getWhere($options);
486 $group_by_part = ttReportHelper::makeGroupByPart($options);
488 $parts = "$concat_part, sum(time_to_sec(l.duration)) as time, null as expenses".$work_unit_part.$cost_part;
489 $sql = "select $parts from tt_log l $join_part $where $group_by_part";
490 // By now we have sql for time items.
492 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
493 if ($options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
495 $concat_part = ttReportHelper::makeConcatExpensesPart($options);
496 $join_part = ttReportHelper::makeJoinExpensesPart($options);
497 $where = ttReportHelper::getExpenseWhere($options);
498 $group_by_expenses_part = ttReportHelper::makeGroupByExpensesPart($options);
499 $sql_for_expenses = "select $concat_part, null as time";
500 if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
501 $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";
503 // Create a combined query.
504 $fields = ttReportHelper::makeCombinedSelectPart($options);
505 $combined = "select $fields, 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 group by $fields";
512 $res = $mdb2->query($sql);
513 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
514 while ($val = $res->fetchRow()) {
515 $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
516 $rowLabel = ttReportHelper::makeGroupByLabel($val['group_field'], $options);
517 if ($options['show_cost']) {
518 $decimalMark = $user->getDecimalMark();
519 if ('.' != $decimalMark) {
520 $val['cost'] = str_replace('.', $decimalMark, $val['cost']);
521 $val['expenses'] = str_replace('.', $decimalMark, $val['expenses']);
523 $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']);
525 $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']);
531 // getTotals calculates total hours and cost for all report items.
532 static function getTotals($options)
535 $mdb2 = getConnection();
537 $trackingMode = $user->getTrackingMode();
538 $decimalMark = $user->getDecimalMark();
539 $where = ttReportHelper::getWhere($options);
542 $time_part = "sum(time_to_sec(l.duration)) as time";
543 if ($options['show_work_units']) {
544 $unitTotalsOnly = $user->getConfigOption('unit_totals_only');
545 $firstUnitThreshold = $user->getConfigInt('1st_unit_threshold', 0);
546 $minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
547 $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";
549 if ($options['show_cost']) {
550 if (MODE_TIME == $trackingMode)
551 $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";
553 $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";
555 $cost_part = ", null as cost, null as expenses";
557 if ($options['show_cost']) {
558 if (MODE_TIME == $trackingMode) {
559 $left_joins = "left join tt_users u on (l.user_id = u.id)";
561 $left_joins = "left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
564 // Prepare sql query part for inner joins.
566 if ($user->isPluginEnabled('ts') && $options['timesheet']) {
567 $timesheet_option = $options['timesheet'];
568 if ($timesheet_option == TIMESHEET_PENDING)
569 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.submit_status = 1 and ts.approval_status is null)";
570 else if ($timesheet_option == TIMESHEET_APPROVED)
571 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approval_status = 1)";
572 else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
573 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approval_status = 0)";
575 // Prepare a query for time items.
576 $sql = "select $time_part $units_part $cost_part from tt_log l $left_joins $inner_joins $where";
578 // If we have expenses, query becomes a bit more complex.
579 if ($options['show_cost'] && $user->isPluginEnabled('ex')) {
580 // Prepare sql query part for inner joins.
582 if ($user->isPluginEnabled('ts') && $options['timesheet']) {
583 $timesheet_option = $options['timesheet'];
584 if ($timesheet_option == TIMESHEET_PENDING)
585 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.submit_status = 1 and ts.approval_status is null)";
586 else if ($timesheet_option == TIMESHEET_APPROVED)
587 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.approval_status = 1)";
588 else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
589 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.approval_status = 0)";
591 $where = ttReportHelper::getExpenseWhere($options);
592 $sql_for_expenses = "select null as time";
593 if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
594 $sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $inner_joins $where";
596 // Create a combined query.
597 $combined = "select sum(time) as time";
598 if ($options['show_work_units']) $combined .= ", sum(units) as units";
599 $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
604 $res = $mdb2->query($sql);
605 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
607 $val = $res->fetchRow();
608 $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
609 if ($options['show_cost']) {
610 $total_cost = $val['cost'];
611 if (!$total_cost) $total_cost = '0.00';
612 if ('.' != $decimalMark)
613 $total_cost = str_replace('.', $decimalMark, $total_cost);
614 $total_expenses = $val['expenses'];
615 if (!$total_expenses) $total_expenses = '0.00';
616 if ('.' != $decimalMark)
617 $total_expenses = str_replace('.', $decimalMark, $total_expenses);
620 $dateFormat = $user->getDateFormat();
621 if ($options['period'])
622 $period = new Period($options['period'], new DateAndTime($dateFormat));
624 $period = new Period();
626 new DateAndTime($dateFormat, $options['period_start']),
627 new DateAndTime($dateFormat, $options['period_end']));
630 $totals['start_date'] = $period->getStartDate();
631 $totals['end_date'] = $period->getEndDate();
632 $totals['time'] = $total_time;
633 $totals['units'] = $val['units'];
634 $totals['cost'] = $total_cost;
635 $totals['expenses'] = $total_expenses;
640 // The assignToInvoice assigns a set of records to a specific invoice.
641 static function assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids) {
643 $mdb2 = getConnection();
645 $group_id = $user->getGroup();
646 $org_id = $user->org_id;
649 $sql = "update tt_log set invoice_id = ".$mdb2->quote($invoice_id).
650 " where id in(".join(', ', $time_log_ids).") and group_id = $group_id and org_id = $org_id";
651 $affected = $mdb2->exec($sql);
652 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
654 if ($expense_item_ids) {
655 $sql = "update tt_expense_items set invoice_id = ".$mdb2->quote($invoice_id).
656 " where id in(".join(', ', $expense_item_ids).") and group_id = $group_id and org_id = $org_id";
657 $affected = $mdb2->exec($sql);
658 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
662 // The markPaid marks a set of records as either paid or unpaid.
663 static function markPaid($time_log_ids, $expense_item_ids, $paid = true) {
665 $mdb2 = getConnection();
667 $group_id = $user->getGroup();
668 $org_id = $user->org_id;
670 $paid_val = (int) $paid;
672 $sql = "update tt_log set paid = $paid_val".
673 " where id in(".join(', ', $time_log_ids).") and group_id = $group_id and org_id = $org_id";
674 $affected = $mdb2->exec($sql);
675 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
677 if ($expense_item_ids) {
678 $sql = "update tt_expense_items set paid = $paid_val".
679 " where id in(".join(', ', $expense_item_ids).") and group_id = $group_id and org_id = $org_id";
680 $affected = $mdb2->exec($sql);
681 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
685 // prepareReportBody - prepares an email body for report.
686 static function prepareReportBody($options, $comment = null)
691 // Determine these once as they are used in multiple places in this function.
692 $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
693 $isClient = $user->isClient();
695 $items = ttReportHelper::getItems($options);
696 $grouping = ttReportHelper::grouping($options);
698 $subtotals = ttReportHelper::getSubtotals($options);
699 $totals = ttReportHelper::getTotals($options);
701 // Use custom fields plugin if it is enabled.
702 if ($user->isPluginEnabled('cf'))
703 $custom_fields = new CustomFields();
705 // Define some styles to use in email.
706 $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
707 $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
708 $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
709 $rowItem = 'background-color: #ffffff;';
710 $rowItemAlt = 'background-color: #f5f5f5;';
711 $rowSubtotal = 'background-color: #e0e0e0;';
712 $cellLeftAligned = 'text-align: left; vertical-align: top;';
713 $cellRightAligned = 'text-align: right; vertical-align: top;';
714 $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
715 $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
717 // Start creating email body.
719 $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
723 $body .= '<p style="'.$style_title.'">'.$i18n->get('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
726 if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>';
728 if ($options['show_totals_only']) {
729 // Totals only report. Output subtotals.
730 $group_by_header = ttReportHelper::makeGroupByHeader($options);
732 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
734 $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
735 if ($options['show_duration'])
736 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
737 if ($options['show_work_units'])
738 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
739 if ($options['show_cost'])
740 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
742 foreach($subtotals as $subtotal) {
743 $body .= '<tr style="'.$rowSubtotal.'">';
744 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : ' ').'</td>';
745 if ($options['show_duration']) {
746 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
747 if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
750 if ($options['show_work_units']) {
751 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
752 $body .= $subtotal['units'];
755 if ($options['show_cost']) {
756 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
757 $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
764 $body .= '<tr><td> </td></tr>';
765 $body .= '<tr style="'.$rowSubtotal.'">';
766 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
767 if ($options['show_duration']) {
768 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
769 if ($totals['time'] <> '0:00') $body .= $totals['time'];
772 if ($options['show_work_units']) {
773 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
774 $body .= $totals['units'];
777 if ($options['show_cost']) {
778 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
779 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
788 // Print table header.
789 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
791 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.date').'</td>';
792 if ($canViewReports || $isClient)
793 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.user').'</td>';
794 if ($options['show_client'])
795 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.client').'</td>';
796 if ($options['show_project'])
797 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.project').'</td>';
798 if ($options['show_task'])
799 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.task').'</td>';
800 if ($options['show_custom_field_1'])
801 $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
802 if ($options['show_start'])
803 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.start').'</td>';
804 if ($options['show_end'])
805 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.finish').'</td>';
806 if ($options['show_duration'])
807 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
808 if ($options['show_work_units'])
809 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
810 if ($options['show_note'])
811 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.note').'</td>';
812 if ($options['show_cost'])
813 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
814 if ($options['show_paid'])
815 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.paid').'</td>';
816 if ($options['show_ip'])
817 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.ip').'</td>';
818 if ($options['show_invoice'])
819 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.invoice').'</td>';
822 // Initialize variables to print subtotals.
823 if ($items && $grouping) {
824 $print_subtotals = true;
826 $prev_grouped_by = '';
827 $cur_grouped_by = '';
829 // Initialize variables to alternate color of rows for different dates.
832 $row_style = $rowItem;
834 // Print report items.
835 if (is_array($items)) {
836 foreach ($items as $record) {
837 $cur_date = $record['date'];
838 // Print a subtotal row after a block of grouped items.
839 if ($print_subtotals) {
840 $cur_grouped_by = $record['grouped_by'];
841 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
842 $body .= '<tr style="'.$rowSubtotal.'">';
843 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
844 $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
845 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['user'].'</td>';
846 if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['client'].'</td>';
847 if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['project'].'</td>';
848 if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['task'].'</td>';
849 if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['cf_1'].'</td>';
850 if ($options['show_start']) $body .= '<td></td>';
851 if ($options['show_end']) $body .= '<td></td>';
852 if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
853 if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['units'].'</td>';
854 if ($options['show_note']) $body .= '<td></td>';
855 if ($options['show_cost']) {
856 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
857 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
860 if ($options['show_paid']) $body .= '<td></td>';
861 if ($options['show_ip']) $body .= '<td></td>';
862 if ($options['show_invoice']) $body .= '<td></td>';
864 $body .= '<tr><td> </td></tr>';
869 // Print a regular row.
870 if ($cur_date != $prev_date)
871 $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
872 $body .= '<tr style="'.$row_style.'">';
873 $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
874 if ($canViewReports || $isClient)
875 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
876 if ($options['show_client'])
877 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
878 if ($options['show_project'])
879 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
880 if ($options['show_task'])
881 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
882 if ($options['show_custom_field_1'])
883 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
884 if ($options['show_start'])
885 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
886 if ($options['show_end'])
887 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
888 if ($options['show_duration'])
889 $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
890 if ($options['show_work_units'])
891 $body .= '<td style="'.$cellRightAligned.'">'.$record['units'].'</td>';
892 if ($options['show_note'])
893 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
894 if ($options['show_cost'])
895 $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
896 if ($options['show_paid']) {
897 $body .= '<td style="'.$cellRightAligned.'">';
898 $body .= $record['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
901 if ($options['show_ip']) {
902 $body .= '<td style="'.$cellRightAligned.'">';
903 $body .= $record['modified'] ? $record['modified_ip'].' '.$record['modified'] : $record['created_ip'].' '.$record['created'];
906 if ($options['show_invoice'])
907 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
910 $prev_date = $record['date'];
911 if ($print_subtotals)
912 $prev_grouped_by = $record['grouped_by'];
916 // Print a terminating subtotal.
917 if ($print_subtotals) {
918 $body .= '<tr style="'.$rowSubtotal.'">';
919 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
920 $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
921 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['user'].'</td>';
922 if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['client'].'</td>';
923 if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['project'].'</td>';
924 if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['task'].'</td>';
925 if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['cf_1'].'</td>';
926 if ($options['show_start']) $body .= '<td></td>';
927 if ($options['show_end']) $body .= '<td></td>';
928 if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
929 if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['units'].'</td>';
930 if ($options['show_note']) $body .= '<td></td>';
931 if ($options['show_cost']) {
932 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
933 $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
936 if ($options['show_paid']) $body .= '<td></td>';
937 if ($options['show_ip']) $body .= '<td></td>';
938 if ($options['show_invoice']) $body .= '<td></td>';
943 $body .= '<tr><td> </td></tr>';
944 $body .= '<tr style="'.$rowSubtotal.'">';
945 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
946 if ($canViewReports || $isClient) $body .= '<td></td>';
947 if ($options['show_client']) $body .= '<td></td>';
948 if ($options['show_project']) $body .= '<td></td>';
949 if ($options['show_task']) $body .= '<td></td>';
950 if ($options['show_custom_field_1']) $body .= '<td></td>';
951 if ($options['show_start']) $body .= '<td></td>';
952 if ($options['show_end']) $body .= '<td></td>';
953 if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
954 if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['units'].'</td>';
955 if ($options['show_note']) $body .= '<td></td>';
956 if ($options['show_cost']) {
957 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
958 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
961 if ($options['show_paid']) $body .= '<td></td>';
962 if ($options['show_ip']) $body .= '<td></td>';
963 if ($options['show_invoice']) $body .= '<td></td>';
970 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
971 $body .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
973 // Finish creating email body.
974 $body .= '</body></html>';
979 // checkFavReportCondition - checks whether it is okay to send fav report.
980 static function checkFavReportCondition($options, $condition)
982 $items = ttReportHelper::getItems($options);
984 $condition = trim(str_replace('count', '', $condition));
986 $greater_or_equal = ttStartsWith($condition, '>=');
987 if ($greater_or_equal) $condition = trim(str_replace('>=', '', $condition));
989 $less_or_equal = ttStartsWith($condition, '<=');
990 if ($less_or_equal) $condition = trim(str_replace('<=', '', $condition));
992 $not_equal = ttStartsWith($condition, '<>');
993 if ($not_equal) $condition = trim(str_replace('<>', '', $condition));
995 $greater = ttStartsWith($condition, '>');
996 if ($greater) $condition = trim(str_replace('>', '', $condition));
998 $less = ttStartsWith($condition, '<');
999 if ($less) $condition = trim(str_replace('<', '', $condition));
1001 $equal = ttStartsWith($condition, '=');
1002 if ($equal) $condition = trim(str_replace('=', '', $condition));
1004 $count_required = (int) $condition;
1006 if ($greater && count($items) > $count_required) return true;
1007 if ($greater_or_equal && count($items) >= $count_required) return true;
1008 if ($less && count($items) < $count_required) return true;
1009 if ($less_or_equal && count($items) <= $count_required) return true;
1010 if ($equal && count($items) == $count_required) return true;
1011 if ($not_equal && count($items) <> $count_required) return true;
1016 // sendFavReport - sends a favorite report to a specified email, called from cron.php
1017 static function sendFavReport($options, $subject, $email, $cc) {
1018 // We are called from cron.php, we have no $bean in session.
1019 // cron.php sets global $user and $i18n objects to match our favorite report user.
1023 // Prepare report body.
1024 $body = ttReportHelper::prepareReportBody($options);
1026 import('mail.Mailer');
1027 $mailer = new Mailer();
1028 $mailer->setCharSet(CHARSET);
1029 $mailer->setContentType('text/html');
1030 $mailer->setSender(SENDER);
1032 $mailer->setReceiverCC($cc);
1033 if (!empty($user->bcc_email))
1034 $mailer->setReceiverBCC($user->bcc_email);
1035 $mailer->setReceiver($email);
1036 $mailer->setMailMode(MAIL_MODE);
1037 if (empty($subject)) $subject = $options['name'];
1038 if (!$mailer->send($subject, $body))
1044 // getReportOptions - returns an array of report options constructed from session bean.
1046 // Note: similarly to ttFavReportHelper::getReportOptions, this function is a part of
1047 // refactoring to simplify maintenance of report generating functions, as we currently
1048 // have 2 sets: normal reporting (from bean), and fav report emailing (from db fields).
1049 // Using options obtained from either db or bean shall allow us to use only one set of functions.
1050 static function getReportOptions($bean) {
1053 // Prepare an array of report options.
1056 // Construct one by one.
1057 $options['name'] = null; // No name required.
1058 $options['user_id'] = $user->id; // Not sure if we need user_id here. Fav reports use it to recycle $user object in cron.php.
1059 $options['client_id'] = $bean->getAttribute('client');
1060 $options['cf_1_option_id'] = $bean->getAttribute('option');
1061 $options['project_id'] = $bean->getAttribute('project');
1062 $options['task_id'] = $bean->getAttribute('task');
1063 $options['billable'] = $bean->getAttribute('include_records');
1064 $options['invoice'] = $bean->getAttribute('invoice');
1065 $options['paid_status'] = $bean->getAttribute('paid_status');
1066 $options['approved'] = $bean->getAttribute('approved');
1067 $options['timesheet'] = $bean->getAttribute('timesheet');
1068 if ($user->isPluginEnabled('ts') && $user->isClient() && !$user->can('view_client_unapproved'))
1069 $options['timesheet'] = TIMESHEET_APPROVED; // Restrict clients to approved timesheet records only.
1070 if (is_array($bean->getAttribute('users'))) $options['users'] = join(',', $bean->getAttribute('users'));
1071 $options['period'] = $bean->getAttribute('period');
1072 $options['period_start'] = $bean->getAttribute('start_date');
1073 $options['period_end'] = $bean->getAttribute('end_date');
1074 $options['show_client'] = $bean->getAttribute('chclient');
1075 $options['show_invoice'] = $bean->getAttribute('chinvoice');
1076 $options['show_approved'] = $bean->getAttribute('chapproved');
1077 $options['show_paid'] = $bean->getAttribute('chpaid');
1078 $options['show_ip'] = $bean->getAttribute('chip');
1079 $options['show_project'] = $bean->getAttribute('chproject');
1080 $options['show_start'] = $bean->getAttribute('chstart');
1081 $options['show_duration'] = $bean->getAttribute('chduration');
1082 $options['show_cost'] = $bean->getAttribute('chcost');
1083 $options['show_task'] = $bean->getAttribute('chtask');
1084 $options['show_end'] = $bean->getAttribute('chfinish');
1085 $options['show_note'] = $bean->getAttribute('chnote');
1086 $options['show_custom_field_1'] = $bean->getAttribute('chcf_1');
1087 $options['show_work_units'] = $bean->getAttribute('chunits');
1088 $options['show_timesheet'] = $bean->getAttribute('chtimesheet');
1089 $options['show_totals_only'] = $bean->getAttribute('chtotalsonly');
1090 $options['group_by1'] = $bean->getAttribute('group_by1');
1091 $options['group_by2'] = $bean->getAttribute('group_by2');
1092 $options['group_by3'] = $bean->getAttribute('group_by3');
1096 // verifyBean is a security function to make sure data in bean makes sense for a group.
1097 static function verifyBean($bean) {
1101 $users_in_bean = $bean->getAttribute('users');
1102 if (is_array($users_in_bean)) {
1103 $users_in_group = ttGroupHelper::getUsers();
1104 foreach ($users_in_group as $user_in_group) {
1105 $valid_ids[] = $user_in_group['id'];
1107 foreach ($users_in_bean as $user_in_bean) {
1108 if (!in_array($user_in_bean, $valid_ids)) {
1114 // TODO: add additional checks here. Perhaps do it before saving the bean for consistency.
1118 // makeGroupByKey builds a combined group by key from group_by1, group_by2 and group_by3 values
1119 // (passed in $options) and a row of data ($row obtained from a db query).
1120 static function makeGroupByKey($options, $row) {
1121 if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1122 // We have group_by1.
1123 $group_by1 = $options['group_by1'];
1124 $group_by1_value = $row[$group_by1];
1125 //if ($group_by1 == 'date') $group_by1_value = ttDateToUserFormat($group_by1_value);
1126 if (empty($group_by1_value)) $group_by1_value = 'Null'; // To match what comes out of makeConcatPart.
1127 $group_by_key .= ' - '.$group_by1_value;
1129 if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1130 // We have group_by2.
1131 $group_by2 = $options['group_by2'];
1132 $group_by2_value = $row[$group_by2];
1133 //if ($group_by2 == 'date') $group_by2_value = ttDateToUserFormat($group_by2_value);
1134 if (empty($group_by2_value)) $group_by2_value = 'Null'; // To match what comes out of makeConcatPart.
1135 $group_by_key .= ' - '.$group_by2_value;
1137 if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1138 // We have group_by3.
1139 $group_by3 = $options['group_by3'];
1140 $group_by3_value = $row[$group_by3];
1141 //if ($group_by3 == 'date') $group_by3_value = ttDateToUserFormat($group_by3_value);
1142 if (empty($group_by3_value)) $group_by3_value = 'Null'; // To match what comes out of makeConcatPart.
1143 $group_by_key .= ' - '.$group_by3_value;
1145 $group_by_key = trim($group_by_key, ' -');
1146 return $group_by_key;
1149 // makeGroupByPart builds a combined group by part for sql query for time items using group_by1,
1150 // group_by2, and group_by3 values passed in $options.
1151 static function makeGroupByPart($options) {
1152 if (!ttReportHelper::grouping($options)) return null;
1154 $group_by1 = $options['group_by1'];
1155 $group_by2 = $options['group_by2'];
1156 $group_by3 = $options['group_by3'];
1158 switch ($group_by1) {
1160 $group_by_parts .= ', l.date';
1163 $group_by_parts .= ', u.name';
1166 $group_by_parts .= ', c.name';
1169 $group_by_parts .= ', p.name';
1172 $group_by_parts .= ', t.name';
1175 $group_by_parts .= ', cfo.value';
1178 switch ($group_by2) {
1180 $group_by_parts .= ', l.date';
1183 $group_by_parts .= ', u.name';
1186 $group_by_parts .= ', c.name';
1189 $group_by_parts .= ', p.name';
1192 $group_by_parts .= ', t.name';
1195 $group_by_parts .= ', cfo.value';
1198 switch ($group_by3) {
1200 $group_by_parts .= ', l.date';
1203 $group_by_parts .= ', u.name';
1206 $group_by_parts .= ', c.name';
1209 $group_by_parts .= ', p.name';
1212 $group_by_parts .= ', t.name';
1215 $group_by_parts .= ', cfo.value';
1218 // Remove garbage from the beginning.
1219 $group_by_parts = ltrim($group_by_parts, ', ');
1220 $group_by_part = "group by $group_by_parts";
1221 return $group_by_part;
1224 // makeGroupByExpensesPart builds a combined group by part for sql query for expense items using
1225 // group_by1, group_by2, and group_by3 values passed in $options.
1226 static function makeGroupByExpensesPart($options) {
1227 $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
1228 ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
1229 ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
1230 if ($no_grouping) return null;
1232 $group_by1 = $options['group_by1'];
1233 $group_by2 = $options['group_by2'];
1234 $group_by3 = $options['group_by3'];
1236 switch ($group_by1) {
1238 $group_by_parts .= ', ei.date';
1241 $group_by_parts .= ', u.name';
1244 $group_by_parts .= ', c.name';
1247 $group_by_parts .= ', p.name';
1250 switch ($group_by2) {
1252 $group_by_parts .= ', ei.date';
1255 $group_by_parts .= ', u.name';
1258 $group_by_parts .= ', c.name';
1261 $group_by_parts .= ', p.name';
1264 switch ($group_by3) {
1266 $group_by_parts .= ', ei.date';
1269 $group_by_parts .= ', u.name';
1272 $group_by_parts .= ', c.name';
1275 $group_by_parts .= ', p.name';
1278 // Remove garbage from the beginning.
1279 $group_by_parts = ltrim($group_by_parts, ', ');
1280 if ($group_by_parts)
1281 $group_by_part = "group by $group_by_parts";
1282 return $group_by_part;
1285 // makeConcatPart builds a concatenation part for getSubtotals query (for time items).
1286 static function makeConcatPart($options) {
1287 $group_by1 = $options['group_by1'];
1288 $group_by2 = $options['group_by2'];
1289 $group_by3 = $options['group_by3'];
1291 switch ($group_by1) {
1293 $what_to_concat .= ", ' - ', l.date";
1296 $what_to_concat .= ", ' - ', u.name";
1297 $fields_part .= ', u.name as user';
1300 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1301 $fields_part .= ', c.name as client';
1304 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1305 $fields_part .= ', p.name as project';
1308 $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1309 $fields_part .= ', t.name as task';
1312 $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1313 $fields_part .= ', cfo.value as cf_1';
1316 switch ($group_by2) {
1318 $what_to_concat .= ", ' - ', l.date";
1321 $what_to_concat .= ", ' - ', u.name";
1322 $fields_part .= ', u.name as user';
1325 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1326 $fields_part .= ', c.name as client';
1329 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1330 $fields_part .= ', p.name as project';
1333 $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1334 $fields_part .= ', t.name as task';
1337 $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1338 $fields_part .= ', cfo.value as cf_1';
1341 switch ($group_by3) {
1343 $what_to_concat .= ", ' - ', l.date";
1346 $what_to_concat .= ", ' - ', u.name";
1347 $fields_part .= ', u.name as user';
1350 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1351 $fields_part .= ', c.name as client';
1354 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1355 $fields_part .= ', p.name as project';
1358 $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1359 $fields_part .= ', t.name as task';
1362 $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1363 $fields_part .= ', cfo.value as cf_1';
1366 // Remove garbage from both ends.
1367 $what_to_concat = trim($what_to_concat, "', -");
1368 $concat_part = "concat($what_to_concat) as group_field";
1369 $concat_part = trim($concat_part, ' -');
1370 return "$concat_part $fields_part";
1373 // makeConcatPart builds a concatenation part for getSubtotals query (for expense items).
1374 static function makeConcatExpensesPart($options) {
1375 $group_by1 = $options['group_by1'];
1376 $group_by2 = $options['group_by2'];
1377 $group_by3 = $options['group_by3'];
1379 switch ($group_by1) {
1381 $what_to_concat .= ", ' - ', ei.date";
1384 $what_to_concat .= ", ' - ', u.name";
1385 $fields_part .= ', u.name as user';
1388 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1389 $fields_part .= ', c.name as client';
1392 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1393 $fields_part .= ', p.name as project';
1397 $what_to_concat .= ", ' - ', 'Null'";
1398 $fields_part .= ', null as task';
1402 $what_to_concat .= ", ' - ', 'Null'";
1403 $fields_part .= ', null as cf_1';
1406 switch ($group_by2) {
1408 $what_to_concat .= ", ' - ', ei.date";
1411 $what_to_concat .= ", ' - ', u.name";
1412 $fields_part .= ', u.name as user';
1415 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1416 $fields_part .= ', c.name as client';
1419 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1420 $fields_part .= ', p.name as project';
1424 $what_to_concat .= ", ' - ', 'Null'";
1425 $fields_part .= ', null as task';
1429 $what_to_concat .= ", ' - ', 'Null'";
1430 $fields_part .= ', null as cf_1';
1433 switch ($group_by3) {
1435 $what_to_concat .= ", ' - ', ei.date";
1438 $what_to_concat .= ", ' - ', u.name";
1439 $fields_part .= ', u.name as user';
1442 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1443 $fields_part .= ', c.name as client';
1446 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1447 $fields_part .= ', p.name as project';
1451 $what_to_concat .= ", ' - ', 'Null'";
1452 $fields_part .= ', null as task';
1456 $what_to_concat .= ", ' - ', 'Null'";
1457 $fields_part .= ', null as cf_1';
1460 // Remove garbage from the beginning.
1461 if ($what_to_concat)
1462 $what_to_concat = substr($what_to_concat, 8);
1463 $concat_part = "concat($what_to_concat) as group_field";
1464 return "$concat_part $fields_part";
1467 // makeCombinedSelectPart builds a list of fields for a combined select on a union for getSubtotals.
1468 // This is used when we include expenses.
1469 static function makeCombinedSelectPart($options) {
1470 $group_by1 = $options['group_by1'];
1471 $group_by2 = $options['group_by2'];
1472 $group_by3 = $options['group_by3'];
1474 $fields = "group_field";
1476 switch ($group_by1) {
1478 $fields .= ', user';
1481 $fields_part .= ', client';
1484 $fields .= ', project';
1488 $fields .= ', task';
1492 $fields .= ', cf_1';
1495 switch ($group_by2) {
1497 $fields .= ', user';
1500 $fields_part .= ', client';
1503 $fields .= ', project';
1507 $fields .= ', task';
1511 $fields .= ', cf_1';
1514 switch ($group_by3) {
1516 $fields .= ', user';
1519 $fields_part .= ', client';
1522 $fields .= ', project';
1526 $fields .= ', task';
1530 $fields .= ', cf_1';
1536 // makeJoinPart builds a left join part for getSubtotals query (for time items).
1537 static function makeJoinPart($options) {
1540 $trackingMode = $user->getTrackingMode();
1541 if (ttReportHelper::groupingBy('user', $options) || MODE_TIME == $trackingMode) {
1542 $join .= ' left join tt_users u on (l.user_id = u.id)';
1544 if (ttReportHelper::groupingBy('client', $options)) {
1545 $join .= ' left join tt_clients c on (l.client_id = c.id)';
1547 if (ttReportHelper::groupingBy('project', $options)) {
1548 $join .= ' left join tt_projects p on (l.project_id = p.id)';
1550 if (ttReportHelper::groupingBy('task', $options)) {
1551 $join .= ' left join tt_tasks t on (l.task_id = t.id)';
1553 if (ttReportHelper::groupingBy('cf_1', $options)) {
1554 $custom_fields = new CustomFields();
1555 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
1556 $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)';
1557 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
1558 $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)';
1560 if ($options['show_cost'] && $trackingMode != MODE_TIME) {
1561 $join .= ' left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)';
1563 // Prepare inner joins.
1564 $inner_joins = null;
1565 if ($user->isPluginEnabled('ts') && $options['timesheet']) {
1566 $timesheet_option = $options['timesheet'];
1567 if ($timesheet_option == TIMESHEET_PENDING)
1568 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.submit_status = 1 and ts.approval_status is null)";
1569 else if ($timesheet_option == TIMESHEET_APPROVED)
1570 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approval_status = 1)";
1571 else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
1572 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approval_status = 0)";
1574 $join .= $inner_joins;
1578 // makeWorkUnitPart builds an sql part for work units for time items.
1579 static function makeWorkUnitPart($options) {
1582 $workUnits = $options['show_work_units'];
1584 $unitTotalsOnly = $user->getConfigOption('unit_totals_only');
1585 $firstUnitThreshold = $user->getConfigInt('1st_unit_threshold', 0);
1586 $minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
1587 if ($unitTotalsOnly)
1588 $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";
1590 $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";
1592 return $work_unit_part;
1595 // makeCostPart builds a cost part for time items.
1596 static function makeCostPart($options) {
1599 if ($options['show_cost']) {
1600 if (MODE_TIME == $user->getTrackingMode())
1601 $cost_part = ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost";
1603 $cost_part .= ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost";
1608 // makeJoinExpensesPart builds a left join part for getSubtotals query for expense items.
1609 static function makeJoinExpensesPart($options) {
1612 if (ttReportHelper::groupingBy('user', $options)) {
1613 $join .= ' left join tt_users u on (ei.user_id = u.id)';
1615 if (ttReportHelper::groupingBy('client', $options)) {
1616 $join .= ' left join tt_clients c on (ei.client_id = c.id)';
1618 if (ttReportHelper::groupingBy('project', $options)) {
1619 $join .= ' left join tt_projects p on (ei.project_id = p.id)';
1621 // Prepare inner joins.
1622 $inner_joins = null;
1623 if ($user->isPluginEnabled('ts') && $options['timesheet']) {
1624 $timesheet_option = $options['timesheet'];
1625 if ($timesheet_option == TIMESHEET_PENDING)
1626 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.submit_status = 1 and ts.approval_status is null)";
1627 else if ($timesheet_option == TIMESHEET_APPROVED)
1628 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.approval_status = 1)";
1629 else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
1630 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.approval_status = 0)";
1632 $join .= $inner_joins;
1636 // grouping determines if we are grouping the report by either group_by1,
1637 // group_by2, or group_by3 values passed in $options.
1638 static function grouping($options) {
1639 $grouping = ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') ||
1640 ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') ||
1641 ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping');
1645 // groupingBy determines if we are grouping a report by a value of $what
1646 // ('date', 'user', 'project', etc.) by checking group_by1, group_by2,
1647 // and group_by3 values passed in $options.
1648 static function groupingBy($what, $options) {
1649 $grouping = ($options['group_by1'] == $what) || ($options['group_by2'] == $what) || ($options['group_by3'] == $what);
1653 // makeGroupByHeader builds a column header for a totals-only report using group_by1,
1654 // group_by2, and group_by3 values passed in $options.
1655 static function makeGroupByHeader($options) {
1657 global $custom_fields;
1659 $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
1660 ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
1661 ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
1662 if ($no_grouping) return null;
1664 if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1665 // We have group_by1.
1666 $group_by1 = $options['group_by1'];
1667 if ('cf_1' == $group_by1)
1668 $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1670 $key = 'label.'.$group_by1;
1671 $group_by_header .= ' - '.$i18n->get($key);
1674 if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1675 // We have group_by2.
1676 $group_by2 = $options['group_by2'];
1677 if ('cf_1' == $group_by2)
1678 $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1680 $key = 'label.'.$group_by2;
1681 $group_by_header .= ' - '.$i18n->get($key);
1684 if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1685 // We have group_by3.
1686 $group_by3 = $options['group_by3'];
1687 if ('cf_1' == $group_by3)
1688 $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1690 $key = 'label.'.$group_by3;
1691 $group_by_header .= ' - '.$i18n->get($key);
1694 $group_by_header = ltrim($group_by_header, ' -');
1695 return $group_by_header;
1698 // makeGroupByXmlTag creates an xml tag for a totals only report using group_by1,
1699 // group_by2, and group_by3 values passed in $options.
1700 static function makeGroupByXmlTag($options) {
1701 if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1702 // We have group_by1.
1703 $tag .= '_'.$options['group_by1'];
1705 if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1706 // We have group_by2.
1707 $tag .= '_'.$options['group_by2'];
1709 if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1710 // We have group_by3.
1711 $tag .= '_'.$options['group_by3'];
1713 $tag = ltrim($tag, '_');
1717 // makeGroupByLabel builds a label for one row in a "Totals only" report of grouped by items.
1718 // It does one thing: if we are grouping by date, the date format is converted for user.
1719 static function makeGroupByLabel($key, $options) {
1720 if (!ttReportHelper::groupingBy('date', $options))
1721 return $key; // No need to format.
1724 if ($user->getDateFormat() == DB_DATEFORMAT)
1725 return $key; // No need to format.
1728 if (preg_match('/\d\d\d\d-\d\d-\d\d/', $key, $matches)) {
1729 // Replace the first found match of a date in DB_DATEFORMAT.
1730 // This is not entirely clean but better than nothing for a label in a row.
1731 $userDate = ttDateToUserFormat($matches[0]);
1732 $label = str_replace($matches[0], $userDate, $key);