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.approve_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.approve_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.approve_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.approve_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.approve_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.approve_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.approve_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.approve_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.approve_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.approve_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.approve_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.approve_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 markApproved marks a set of records as either approved or unapproved.
663 static function markApproved($time_log_ids, $expense_item_ids, $approved = true) {
665 $mdb2 = getConnection();
667 $group_id = $user->getGroup();
668 $org_id = $user->org_id;
670 $approved_val = (int) $approved;
672 $sql = "update tt_log set approved = $approved_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 approved = $approved_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 // The markPaid marks a set of records as either paid or unpaid.
686 static function markPaid($time_log_ids, $expense_item_ids, $paid = true) {
688 $mdb2 = getConnection();
690 $group_id = $user->getGroup();
691 $org_id = $user->org_id;
693 $paid_val = (int) $paid;
695 $sql = "update tt_log set paid = $paid_val".
696 " where id in(".join(', ', $time_log_ids).") and group_id = $group_id and org_id = $org_id";
697 $affected = $mdb2->exec($sql);
698 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
700 if ($expense_item_ids) {
701 $sql = "update tt_expense_items set paid = $paid_val".
702 " where id in(".join(', ', $expense_item_ids).") and group_id = $group_id and org_id = $org_id";
703 $affected = $mdb2->exec($sql);
704 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
708 // prepareReportBody - prepares an email body for report.
709 static function prepareReportBody($options, $comment = null)
714 // Determine these once as they are used in multiple places in this function.
715 $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
716 $isClient = $user->isClient();
718 $items = ttReportHelper::getItems($options);
719 $grouping = ttReportHelper::grouping($options);
721 $subtotals = ttReportHelper::getSubtotals($options);
722 $totals = ttReportHelper::getTotals($options);
724 // Use custom fields plugin if it is enabled.
725 if ($user->isPluginEnabled('cf'))
726 $custom_fields = new CustomFields();
728 // Define some styles to use in email.
729 $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
730 $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
731 $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
732 $rowItem = 'background-color: #ffffff;';
733 $rowItemAlt = 'background-color: #f5f5f5;';
734 $rowSubtotal = 'background-color: #e0e0e0;';
735 $cellLeftAligned = 'text-align: left; vertical-align: top;';
736 $cellRightAligned = 'text-align: right; vertical-align: top;';
737 $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
738 $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
740 // Start creating email body.
742 $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
746 $body .= '<p style="'.$style_title.'">'.$i18n->get('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
749 if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>';
751 if ($options['show_totals_only']) {
752 // Totals only report. Output subtotals.
753 $group_by_header = ttReportHelper::makeGroupByHeader($options);
755 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
757 $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
758 if ($options['show_duration'])
759 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
760 if ($options['show_work_units'])
761 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
762 if ($options['show_cost'])
763 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
765 foreach($subtotals as $subtotal) {
766 $body .= '<tr style="'.$rowSubtotal.'">';
767 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : ' ').'</td>';
768 if ($options['show_duration']) {
769 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
770 if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
773 if ($options['show_work_units']) {
774 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
775 $body .= $subtotal['units'];
778 if ($options['show_cost']) {
779 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
780 $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
787 $body .= '<tr><td> </td></tr>';
788 $body .= '<tr style="'.$rowSubtotal.'">';
789 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
790 if ($options['show_duration']) {
791 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
792 if ($totals['time'] <> '0:00') $body .= $totals['time'];
795 if ($options['show_work_units']) {
796 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
797 $body .= $totals['units'];
800 if ($options['show_cost']) {
801 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
802 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
811 // Print table header.
812 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
814 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.date').'</td>';
815 if ($canViewReports || $isClient)
816 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.user').'</td>';
817 if ($options['show_client'])
818 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.client').'</td>';
819 if ($options['show_project'])
820 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.project').'</td>';
821 if ($options['show_task'])
822 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.task').'</td>';
823 if ($options['show_custom_field_1'])
824 $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
825 if ($options['show_start'])
826 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.start').'</td>';
827 if ($options['show_end'])
828 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.finish').'</td>';
829 if ($options['show_duration'])
830 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
831 if ($options['show_work_units'])
832 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
833 if ($options['show_note'])
834 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.note').'</td>';
835 if ($options['show_cost'])
836 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
837 if ($options['show_approved'])
838 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.approved').'</td>';
839 if ($options['show_paid'])
840 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.paid').'</td>';
841 if ($options['show_ip'])
842 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.ip').'</td>';
843 if ($options['show_invoice'])
844 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.invoice').'</td>';
845 if ($options['show_timesheet'])
846 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.timesheet').'</td>';
849 // Initialize variables to print subtotals.
850 if ($items && $grouping) {
851 $print_subtotals = true;
853 $prev_grouped_by = '';
854 $cur_grouped_by = '';
856 // Initialize variables to alternate color of rows for different dates.
859 $row_style = $rowItem;
861 // Print report items.
862 if (is_array($items)) {
863 foreach ($items as $record) {
864 $cur_date = $record['date'];
865 // Print a subtotal row after a block of grouped items.
866 if ($print_subtotals) {
867 $cur_grouped_by = $record['grouped_by'];
868 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
869 $body .= '<tr style="'.$rowSubtotal.'">';
870 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
871 $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
872 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['user'].'</td>';
873 if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['client'].'</td>';
874 if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['project'].'</td>';
875 if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['task'].'</td>';
876 if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['cf_1'].'</td>';
877 if ($options['show_start']) $body .= '<td></td>';
878 if ($options['show_end']) $body .= '<td></td>';
879 if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
880 if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['units'].'</td>';
881 if ($options['show_note']) $body .= '<td></td>';
882 if ($options['show_cost']) {
883 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
884 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
887 if ($options['show_approved']) $body .= '<td></td>';
888 if ($options['show_paid']) $body .= '<td></td>';
889 if ($options['show_ip']) $body .= '<td></td>';
890 if ($options['show_invoice']) $body .= '<td></td>';
891 if ($options['show_timesheet']) $body .= '<td></td>';
893 $body .= '<tr><td> </td></tr>';
898 // Print a regular row.
899 if ($cur_date != $prev_date)
900 $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
901 $body .= '<tr style="'.$row_style.'">';
902 $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
903 if ($canViewReports || $isClient)
904 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
905 if ($options['show_client'])
906 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
907 if ($options['show_project'])
908 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
909 if ($options['show_task'])
910 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
911 if ($options['show_custom_field_1'])
912 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
913 if ($options['show_start'])
914 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
915 if ($options['show_end'])
916 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
917 if ($options['show_duration'])
918 $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
919 if ($options['show_work_units'])
920 $body .= '<td style="'.$cellRightAligned.'">'.$record['units'].'</td>';
921 if ($options['show_note'])
922 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
923 if ($options['show_cost'])
924 $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
925 if ($options['show_approved']) {
926 $body .= '<td style="'.$cellRightAligned.'">';
927 $body .= $record['approved'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
930 if ($options['show_paid']) {
931 $body .= '<td style="'.$cellRightAligned.'">';
932 $body .= $record['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
935 if ($options['show_ip']) {
936 $body .= '<td style="'.$cellRightAligned.'">';
937 $body .= $record['modified'] ? $record['modified_ip'].' '.$record['modified'] : $record['created_ip'].' '.$record['created'];
940 if ($options['show_invoice'])
941 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
942 if ($options['show_timesheet'])
943 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['timesheet']).'</td>';
946 $prev_date = $record['date'];
947 if ($print_subtotals)
948 $prev_grouped_by = $record['grouped_by'];
952 // Print a terminating subtotal.
953 if ($print_subtotals) {
954 $body .= '<tr style="'.$rowSubtotal.'">';
955 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
956 $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
957 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['user'].'</td>';
958 if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['client'].'</td>';
959 if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['project'].'</td>';
960 if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['task'].'</td>';
961 if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['cf_1'].'</td>';
962 if ($options['show_start']) $body .= '<td></td>';
963 if ($options['show_end']) $body .= '<td></td>';
964 if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
965 if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['units'].'</td>';
966 if ($options['show_note']) $body .= '<td></td>';
967 if ($options['show_cost']) {
968 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
969 $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
972 if ($options['show_approved']) $body .= '<td></td>';
973 if ($options['show_paid']) $body .= '<td></td>';
974 if ($options['show_ip']) $body .= '<td></td>';
975 if ($options['show_invoice']) $body .= '<td></td>';
976 if ($options['show_timesheet']) $body .= '<td></td>';
981 $body .= '<tr><td> </td></tr>';
982 $body .= '<tr style="'.$rowSubtotal.'">';
983 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
984 if ($canViewReports || $isClient) $body .= '<td></td>';
985 if ($options['show_client']) $body .= '<td></td>';
986 if ($options['show_project']) $body .= '<td></td>';
987 if ($options['show_task']) $body .= '<td></td>';
988 if ($options['show_custom_field_1']) $body .= '<td></td>';
989 if ($options['show_start']) $body .= '<td></td>';
990 if ($options['show_end']) $body .= '<td></td>';
991 if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
992 if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['units'].'</td>';
993 if ($options['show_note']) $body .= '<td></td>';
994 if ($options['show_cost']) {
995 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
996 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
999 if ($options['show_approved']) $body .= '<td></td>';
1000 if ($options['show_paid']) $body .= '<td></td>';
1001 if ($options['show_ip']) $body .= '<td></td>';
1002 if ($options['show_invoice']) $body .= '<td></td>';
1003 if ($options['show_timesheet']) $body .= '<td></td>';
1006 $body .= '</table>';
1010 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
1011 $body .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
1013 // Finish creating email body.
1014 $body .= '</body></html>';
1019 // checkFavReportCondition - checks whether it is okay to send fav report.
1020 static function checkFavReportCondition($options, $condition)
1022 $items = ttReportHelper::getItems($options);
1024 $condition = trim(str_replace('count', '', $condition));
1026 $greater_or_equal = ttStartsWith($condition, '>=');
1027 if ($greater_or_equal) $condition = trim(str_replace('>=', '', $condition));
1029 $less_or_equal = ttStartsWith($condition, '<=');
1030 if ($less_or_equal) $condition = trim(str_replace('<=', '', $condition));
1032 $not_equal = ttStartsWith($condition, '<>');
1033 if ($not_equal) $condition = trim(str_replace('<>', '', $condition));
1035 $greater = ttStartsWith($condition, '>');
1036 if ($greater) $condition = trim(str_replace('>', '', $condition));
1038 $less = ttStartsWith($condition, '<');
1039 if ($less) $condition = trim(str_replace('<', '', $condition));
1041 $equal = ttStartsWith($condition, '=');
1042 if ($equal) $condition = trim(str_replace('=', '', $condition));
1044 $count_required = (int) $condition;
1046 if ($greater && count($items) > $count_required) return true;
1047 if ($greater_or_equal && count($items) >= $count_required) return true;
1048 if ($less && count($items) < $count_required) return true;
1049 if ($less_or_equal && count($items) <= $count_required) return true;
1050 if ($equal && count($items) == $count_required) return true;
1051 if ($not_equal && count($items) <> $count_required) return true;
1056 // sendFavReport - sends a favorite report to a specified email, called from cron.php
1057 static function sendFavReport($options, $subject, $email, $cc) {
1058 // We are called from cron.php, we have no $bean in session.
1059 // cron.php sets global $user and $i18n objects to match our favorite report user.
1063 // Prepare report body.
1064 $body = ttReportHelper::prepareReportBody($options);
1066 import('mail.Mailer');
1067 $mailer = new Mailer();
1068 $mailer->setCharSet(CHARSET);
1069 $mailer->setContentType('text/html');
1070 $mailer->setSender(SENDER);
1072 $mailer->setReceiverCC($cc);
1073 if (!empty($user->bcc_email))
1074 $mailer->setReceiverBCC($user->bcc_email);
1075 $mailer->setReceiver($email);
1076 $mailer->setMailMode(MAIL_MODE);
1077 if (empty($subject)) $subject = $options['name'];
1078 if (!$mailer->send($subject, $body))
1084 // getReportOptions - returns an array of report options constructed from session bean.
1086 // Note: similarly to ttFavReportHelper::getReportOptions, this function is a part of
1087 // refactoring to simplify maintenance of report generating functions, as we currently
1088 // have 2 sets: normal reporting (from bean), and fav report emailing (from db fields).
1089 // Using options obtained from either db or bean shall allow us to use only one set of functions.
1090 static function getReportOptions($bean) {
1093 // Prepare an array of report options.
1096 // Construct one by one.
1097 $options['name'] = null; // No name required.
1098 $options['user_id'] = $user->id; // Not sure if we need user_id here. Fav reports use it to recycle $user object in cron.php.
1099 $options['client_id'] = $bean->getAttribute('client');
1100 $options['cf_1_option_id'] = $bean->getAttribute('option');
1101 $options['project_id'] = $bean->getAttribute('project');
1102 $options['task_id'] = $bean->getAttribute('task');
1103 $options['billable'] = $bean->getAttribute('include_records');
1104 $options['invoice'] = $bean->getAttribute('invoice');
1105 $options['paid_status'] = $bean->getAttribute('paid_status');
1106 $options['approved'] = $bean->getAttribute('approved');
1107 if ($user->isPluginEnabled('ap') && $user->isClient() && !$user->can('view_client_unapproved'))
1108 $options['approved'] = 1; // Restrict clients to approved records only.
1109 $options['timesheet'] = $bean->getAttribute('timesheet');
1110 if (is_array($bean->getAttribute('users'))) $options['users'] = join(',', $bean->getAttribute('users'));
1111 $options['period'] = $bean->getAttribute('period');
1112 $options['period_start'] = $bean->getAttribute('start_date');
1113 $options['period_end'] = $bean->getAttribute('end_date');
1114 $options['show_client'] = $bean->getAttribute('chclient');
1115 $options['show_invoice'] = $bean->getAttribute('chinvoice');
1116 $options['show_approved'] = $bean->getAttribute('chapproved');
1117 $options['show_paid'] = $bean->getAttribute('chpaid');
1118 $options['show_ip'] = $bean->getAttribute('chip');
1119 $options['show_project'] = $bean->getAttribute('chproject');
1120 $options['show_start'] = $bean->getAttribute('chstart');
1121 $options['show_duration'] = $bean->getAttribute('chduration');
1122 $options['show_cost'] = $bean->getAttribute('chcost');
1123 $options['show_task'] = $bean->getAttribute('chtask');
1124 $options['show_end'] = $bean->getAttribute('chfinish');
1125 $options['show_note'] = $bean->getAttribute('chnote');
1126 $options['show_custom_field_1'] = $bean->getAttribute('chcf_1');
1127 $options['show_work_units'] = $bean->getAttribute('chunits');
1128 $options['show_timesheet'] = $bean->getAttribute('chtimesheet');
1129 $options['show_totals_only'] = $bean->getAttribute('chtotalsonly');
1130 $options['group_by1'] = $bean->getAttribute('group_by1');
1131 $options['group_by2'] = $bean->getAttribute('group_by2');
1132 $options['group_by3'] = $bean->getAttribute('group_by3');
1136 // verifyBean is a security function to make sure data in bean makes sense for a group.
1137 static function verifyBean($bean) {
1141 $users_in_bean = $bean->getAttribute('users');
1142 if (is_array($users_in_bean)) {
1143 $users_in_group = ttGroupHelper::getUsers();
1144 foreach ($users_in_group as $user_in_group) {
1145 $valid_ids[] = $user_in_group['id'];
1147 foreach ($users_in_bean as $user_in_bean) {
1148 if (!in_array($user_in_bean, $valid_ids)) {
1154 // TODO: add additional checks here. Perhaps do it before saving the bean for consistency.
1158 // makeGroupByKey builds a combined group by key from group_by1, group_by2 and group_by3 values
1159 // (passed in $options) and a row of data ($row obtained from a db query).
1160 static function makeGroupByKey($options, $row) {
1161 if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1162 // We have group_by1.
1163 $group_by1 = $options['group_by1'];
1164 $group_by1_value = $row[$group_by1];
1165 //if ($group_by1 == 'date') $group_by1_value = ttDateToUserFormat($group_by1_value);
1166 if (empty($group_by1_value)) $group_by1_value = 'Null'; // To match what comes out of makeConcatPart.
1167 $group_by_key .= ' - '.$group_by1_value;
1169 if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1170 // We have group_by2.
1171 $group_by2 = $options['group_by2'];
1172 $group_by2_value = $row[$group_by2];
1173 //if ($group_by2 == 'date') $group_by2_value = ttDateToUserFormat($group_by2_value);
1174 if (empty($group_by2_value)) $group_by2_value = 'Null'; // To match what comes out of makeConcatPart.
1175 $group_by_key .= ' - '.$group_by2_value;
1177 if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1178 // We have group_by3.
1179 $group_by3 = $options['group_by3'];
1180 $group_by3_value = $row[$group_by3];
1181 //if ($group_by3 == 'date') $group_by3_value = ttDateToUserFormat($group_by3_value);
1182 if (empty($group_by3_value)) $group_by3_value = 'Null'; // To match what comes out of makeConcatPart.
1183 $group_by_key .= ' - '.$group_by3_value;
1185 $group_by_key = trim($group_by_key, ' -');
1186 return $group_by_key;
1189 // makeGroupByPart builds a combined group by part for sql query for time items using group_by1,
1190 // group_by2, and group_by3 values passed in $options.
1191 static function makeGroupByPart($options) {
1192 if (!ttReportHelper::grouping($options)) return null;
1194 $group_by1 = $options['group_by1'];
1195 $group_by2 = $options['group_by2'];
1196 $group_by3 = $options['group_by3'];
1198 switch ($group_by1) {
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 switch ($group_by2) {
1220 $group_by_parts .= ', l.date';
1223 $group_by_parts .= ', u.name';
1226 $group_by_parts .= ', c.name';
1229 $group_by_parts .= ', p.name';
1232 $group_by_parts .= ', t.name';
1235 $group_by_parts .= ', cfo.value';
1238 switch ($group_by3) {
1240 $group_by_parts .= ', l.date';
1243 $group_by_parts .= ', u.name';
1246 $group_by_parts .= ', c.name';
1249 $group_by_parts .= ', p.name';
1252 $group_by_parts .= ', t.name';
1255 $group_by_parts .= ', cfo.value';
1258 // Remove garbage from the beginning.
1259 $group_by_parts = ltrim($group_by_parts, ', ');
1260 $group_by_part = "group by $group_by_parts";
1261 return $group_by_part;
1264 // makeGroupByExpensesPart builds a combined group by part for sql query for expense items using
1265 // group_by1, group_by2, and group_by3 values passed in $options.
1266 static function makeGroupByExpensesPart($options) {
1267 $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
1268 ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
1269 ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
1270 if ($no_grouping) return null;
1272 $group_by1 = $options['group_by1'];
1273 $group_by2 = $options['group_by2'];
1274 $group_by3 = $options['group_by3'];
1276 switch ($group_by1) {
1278 $group_by_parts .= ', ei.date';
1281 $group_by_parts .= ', u.name';
1284 $group_by_parts .= ', c.name';
1287 $group_by_parts .= ', p.name';
1290 switch ($group_by2) {
1292 $group_by_parts .= ', ei.date';
1295 $group_by_parts .= ', u.name';
1298 $group_by_parts .= ', c.name';
1301 $group_by_parts .= ', p.name';
1304 switch ($group_by3) {
1306 $group_by_parts .= ', ei.date';
1309 $group_by_parts .= ', u.name';
1312 $group_by_parts .= ', c.name';
1315 $group_by_parts .= ', p.name';
1318 // Remove garbage from the beginning.
1319 $group_by_parts = ltrim($group_by_parts, ', ');
1320 if ($group_by_parts)
1321 $group_by_part = "group by $group_by_parts";
1322 return $group_by_part;
1325 // makeConcatPart builds a concatenation part for getSubtotals query (for time items).
1326 static function makeConcatPart($options) {
1327 $group_by1 = $options['group_by1'];
1328 $group_by2 = $options['group_by2'];
1329 $group_by3 = $options['group_by3'];
1331 switch ($group_by1) {
1333 $what_to_concat .= ", ' - ', l.date";
1336 $what_to_concat .= ", ' - ', u.name";
1337 $fields_part .= ', u.name as user';
1340 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1341 $fields_part .= ', c.name as client';
1344 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1345 $fields_part .= ', p.name as project';
1348 $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1349 $fields_part .= ', t.name as task';
1352 $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1353 $fields_part .= ', cfo.value as cf_1';
1356 switch ($group_by2) {
1358 $what_to_concat .= ", ' - ', l.date";
1361 $what_to_concat .= ", ' - ', u.name";
1362 $fields_part .= ', u.name as user';
1365 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1366 $fields_part .= ', c.name as client';
1369 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1370 $fields_part .= ', p.name as project';
1373 $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1374 $fields_part .= ', t.name as task';
1377 $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1378 $fields_part .= ', cfo.value as cf_1';
1381 switch ($group_by3) {
1383 $what_to_concat .= ", ' - ', l.date";
1386 $what_to_concat .= ", ' - ', u.name";
1387 $fields_part .= ', u.name as user';
1390 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1391 $fields_part .= ', c.name as client';
1394 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1395 $fields_part .= ', p.name as project';
1398 $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
1399 $fields_part .= ', t.name as task';
1402 $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
1403 $fields_part .= ', cfo.value as cf_1';
1406 // Remove garbage from both ends.
1407 $what_to_concat = trim($what_to_concat, "', -");
1408 $concat_part = "concat($what_to_concat) as group_field";
1409 $concat_part = trim($concat_part, ' -');
1410 return "$concat_part $fields_part";
1413 // makeConcatPart builds a concatenation part for getSubtotals query (for expense items).
1414 static function makeConcatExpensesPart($options) {
1415 $group_by1 = $options['group_by1'];
1416 $group_by2 = $options['group_by2'];
1417 $group_by3 = $options['group_by3'];
1419 switch ($group_by1) {
1421 $what_to_concat .= ", ' - ', ei.date";
1424 $what_to_concat .= ", ' - ', u.name";
1425 $fields_part .= ', u.name as user';
1428 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1429 $fields_part .= ', c.name as client';
1432 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1433 $fields_part .= ', p.name as project';
1437 $what_to_concat .= ", ' - ', 'Null'";
1438 $fields_part .= ', null as task';
1442 $what_to_concat .= ", ' - ', 'Null'";
1443 $fields_part .= ', null as cf_1';
1446 switch ($group_by2) {
1448 $what_to_concat .= ", ' - ', ei.date";
1451 $what_to_concat .= ", ' - ', u.name";
1452 $fields_part .= ', u.name as user';
1455 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1456 $fields_part .= ', c.name as client';
1459 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1460 $fields_part .= ', p.name as project';
1464 $what_to_concat .= ", ' - ', 'Null'";
1465 $fields_part .= ', null as task';
1469 $what_to_concat .= ", ' - ', 'Null'";
1470 $fields_part .= ', null as cf_1';
1473 switch ($group_by3) {
1475 $what_to_concat .= ", ' - ', ei.date";
1478 $what_to_concat .= ", ' - ', u.name";
1479 $fields_part .= ', u.name as user';
1482 $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
1483 $fields_part .= ', c.name as client';
1486 $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
1487 $fields_part .= ', p.name as project';
1491 $what_to_concat .= ", ' - ', 'Null'";
1492 $fields_part .= ', null as task';
1496 $what_to_concat .= ", ' - ', 'Null'";
1497 $fields_part .= ', null as cf_1';
1500 // Remove garbage from the beginning.
1501 if ($what_to_concat)
1502 $what_to_concat = substr($what_to_concat, 8);
1503 $concat_part = "concat($what_to_concat) as group_field";
1504 return "$concat_part $fields_part";
1507 // makeCombinedSelectPart builds a list of fields for a combined select on a union for getSubtotals.
1508 // This is used when we include expenses.
1509 static function makeCombinedSelectPart($options) {
1510 $group_by1 = $options['group_by1'];
1511 $group_by2 = $options['group_by2'];
1512 $group_by3 = $options['group_by3'];
1514 $fields = "group_field";
1516 switch ($group_by1) {
1518 $fields .= ', user';
1521 $fields_part .= ', client';
1524 $fields .= ', project';
1528 $fields .= ', task';
1532 $fields .= ', cf_1';
1535 switch ($group_by2) {
1537 $fields .= ', user';
1540 $fields_part .= ', client';
1543 $fields .= ', project';
1547 $fields .= ', task';
1551 $fields .= ', cf_1';
1554 switch ($group_by3) {
1556 $fields .= ', user';
1559 $fields_part .= ', client';
1562 $fields .= ', project';
1566 $fields .= ', task';
1570 $fields .= ', cf_1';
1576 // makeJoinPart builds a left join part for getSubtotals query (for time items).
1577 static function makeJoinPart($options) {
1580 $trackingMode = $user->getTrackingMode();
1581 if (ttReportHelper::groupingBy('user', $options) || MODE_TIME == $trackingMode) {
1582 $join .= ' left join tt_users u on (l.user_id = u.id)';
1584 if (ttReportHelper::groupingBy('client', $options)) {
1585 $join .= ' left join tt_clients c on (l.client_id = c.id)';
1587 if (ttReportHelper::groupingBy('project', $options)) {
1588 $join .= ' left join tt_projects p on (l.project_id = p.id)';
1590 if (ttReportHelper::groupingBy('task', $options)) {
1591 $join .= ' left join tt_tasks t on (l.task_id = t.id)';
1593 if (ttReportHelper::groupingBy('cf_1', $options)) {
1594 $custom_fields = new CustomFields();
1595 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
1596 $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)';
1597 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
1598 $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)';
1600 if ($options['show_cost'] && $trackingMode != MODE_TIME) {
1601 $join .= ' left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)';
1603 // Prepare inner joins.
1604 $inner_joins = null;
1605 if ($user->isPluginEnabled('ts') && $options['timesheet']) {
1606 $timesheet_option = $options['timesheet'];
1607 if ($timesheet_option == TIMESHEET_PENDING)
1608 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.submit_status = 1 and ts.approve_status is null)";
1609 else if ($timesheet_option == TIMESHEET_APPROVED)
1610 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approve_status = 1)";
1611 else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
1612 $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approve_status = 0)";
1614 $join .= $inner_joins;
1618 // makeWorkUnitPart builds an sql part for work units for time items.
1619 static function makeWorkUnitPart($options) {
1622 $workUnits = $options['show_work_units'];
1624 $unitTotalsOnly = $user->getConfigOption('unit_totals_only');
1625 $firstUnitThreshold = $user->getConfigInt('1st_unit_threshold', 0);
1626 $minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
1627 if ($unitTotalsOnly)
1628 $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";
1630 $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";
1632 return $work_unit_part;
1635 // makeCostPart builds a cost part for time items.
1636 static function makeCostPart($options) {
1639 if ($options['show_cost']) {
1640 if (MODE_TIME == $user->getTrackingMode())
1641 $cost_part = ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost";
1643 $cost_part .= ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost";
1648 // makeJoinExpensesPart builds a left join part for getSubtotals query for expense items.
1649 static function makeJoinExpensesPart($options) {
1652 if (ttReportHelper::groupingBy('user', $options)) {
1653 $join .= ' left join tt_users u on (ei.user_id = u.id)';
1655 if (ttReportHelper::groupingBy('client', $options)) {
1656 $join .= ' left join tt_clients c on (ei.client_id = c.id)';
1658 if (ttReportHelper::groupingBy('project', $options)) {
1659 $join .= ' left join tt_projects p on (ei.project_id = p.id)';
1661 // Prepare inner joins.
1662 $inner_joins = null;
1663 if ($user->isPluginEnabled('ts') && $options['timesheet']) {
1664 $timesheet_option = $options['timesheet'];
1665 if ($timesheet_option == TIMESHEET_PENDING)
1666 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.submit_status = 1 and ts.approve_status is null)";
1667 else if ($timesheet_option == TIMESHEET_APPROVED)
1668 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.approve_status = 1)";
1669 else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
1670 $inner_joins .= " inner join tt_timesheets ts on (ei.timesheet_id = ts.id and ts.approve_status = 0)";
1672 $join .= $inner_joins;
1676 // grouping determines if we are grouping the report by either group_by1,
1677 // group_by2, or group_by3 values passed in $options.
1678 static function grouping($options) {
1679 $grouping = ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') ||
1680 ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') ||
1681 ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping');
1685 // groupingBy determines if we are grouping a report by a value of $what
1686 // ('date', 'user', 'project', etc.) by checking group_by1, group_by2,
1687 // and group_by3 values passed in $options.
1688 static function groupingBy($what, $options) {
1689 $grouping = ($options['group_by1'] == $what) || ($options['group_by2'] == $what) || ($options['group_by3'] == $what);
1693 // makeGroupByHeader builds a column header for a totals-only report using group_by1,
1694 // group_by2, and group_by3 values passed in $options.
1695 static function makeGroupByHeader($options) {
1697 global $custom_fields;
1699 $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
1700 ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
1701 ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
1702 if ($no_grouping) return null;
1704 if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1705 // We have group_by1.
1706 $group_by1 = $options['group_by1'];
1707 if ('cf_1' == $group_by1)
1708 $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1710 $key = 'label.'.$group_by1;
1711 $group_by_header .= ' - '.$i18n->get($key);
1714 if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1715 // We have group_by2.
1716 $group_by2 = $options['group_by2'];
1717 if ('cf_1' == $group_by2)
1718 $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1720 $key = 'label.'.$group_by2;
1721 $group_by_header .= ' - '.$i18n->get($key);
1724 if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1725 // We have group_by3.
1726 $group_by3 = $options['group_by3'];
1727 if ('cf_1' == $group_by3)
1728 $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
1730 $key = 'label.'.$group_by3;
1731 $group_by_header .= ' - '.$i18n->get($key);
1734 $group_by_header = ltrim($group_by_header, ' -');
1735 return $group_by_header;
1738 // makeGroupByXmlTag creates an xml tag for a totals only report using group_by1,
1739 // group_by2, and group_by3 values passed in $options.
1740 static function makeGroupByXmlTag($options) {
1741 if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
1742 // We have group_by1.
1743 $tag .= '_'.$options['group_by1'];
1745 if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
1746 // We have group_by2.
1747 $tag .= '_'.$options['group_by2'];
1749 if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
1750 // We have group_by3.
1751 $tag .= '_'.$options['group_by3'];
1753 $tag = ltrim($tag, '_');
1757 // makeGroupByLabel builds a label for one row in a "Totals only" report of grouped by items.
1758 // It does one thing: if we are grouping by date, the date format is converted for user.
1759 static function makeGroupByLabel($key, $options) {
1760 if (!ttReportHelper::groupingBy('date', $options))
1761 return $key; // No need to format.
1764 if ($user->getDateFormat() == DB_DATEFORMAT)
1765 return $key; // No need to format.
1768 if (preg_match('/\d\d\d\d-\d\d-\d\d/', $key, $matches)) {
1769 // Replace the first found match of a date in DB_DATEFORMAT.
1770 // This is not entirely clean but better than nothing for a label in a row.
1771 $userDate = ttDateToUserFormat($matches[0]);
1772 $label = str_replace($matches[0], $userDate, $key);