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 // Class ttReportHelper is used for help with reports.
37 class ttReportHelper {
39 // getWhere prepares a WHERE clause for a report query.
40 static function getWhere($bean) {
43 // Prepare dropdown parts.
45 if ($bean->getAttribute('client'))
46 $dropdown_parts .= ' and l.client_id = '.$bean->getAttribute('client');
47 elseif ($user->isClient() && $user->client_id)
48 $dropdown_parts .= ' and l.client_id = '.$user->client_id;
49 if ($bean->getAttribute('option')) $dropdown_parts .= ' and l.id in(select log_id from tt_custom_field_log where status = 1 and option_id = '.$bean->getAttribute('option').')';
50 if ($bean->getAttribute('project')) $dropdown_parts .= ' and l.project_id = '.$bean->getAttribute('project');
51 if ($bean->getAttribute('task')) $dropdown_parts .= ' and l.task_id = '.$bean->getAttribute('task');
52 if ($bean->getAttribute('include_records')=='1') $dropdown_parts .= ' and l.billable = 1';
53 if ($bean->getAttribute('include_records')=='2') $dropdown_parts .= ' and l.billable = 0';
54 if ($bean->getAttribute('invoice')=='1') $dropdown_parts .= ' and l.invoice_id is not NULL';
55 if ($bean->getAttribute('invoice')=='2') $dropdown_parts .= ' and l.invoice_id is NULL';
56 if ($bean->getAttribute('paid_status')=='1') $dropdown_parts .= ' and l.paid = 1';
57 if ($bean->getAttribute('paid_status')=='2') $dropdown_parts .= ' and l.paid = 0';
59 // Prepare user list part.
61 if (($user->can('view_reports') || $user->isClient()) && is_array($bean->getAttribute('users')))
62 $userlist = join(',', $bean->getAttribute('users'));
63 // Prepare sql query part for user list.
64 $user_list_part = null;
65 if ($user->can('view_reports') || $user->isClient())
66 $user_list_part = " and l.user_id in ($userlist)";
68 $user_list_part = " and l.user_id = ".$user->id;
70 // Prepare sql query part for where.
71 if ($bean->getAttribute('period'))
72 $period = new Period($bean->getAttribute('period'), new DateAndTime($user->date_format));
74 $period = new Period();
76 new DateAndTime($user->date_format, $bean->getAttribute('start_date')),
77 new DateAndTime($user->date_format, $bean->getAttribute('end_date')));
79 $where = " where l.status = 1 and l.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and l.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
80 " $user_list_part $dropdown_parts";
84 // getFavWhere prepares a WHERE clause for a favorite report query.
85 static function getFavWhere($report) {
88 // Prepare dropdown parts.
90 if ($report['client_id'])
91 $dropdown_parts .= ' and l.client_id = '.$report['client_id'];
92 elseif ($user->isClient() && $user->client_id)
93 $dropdown_parts .= ' and l.client_id = '.$user->client_id;
94 if ($report['cf_1_option_id']) $dropdown_parts .= ' and l.id in(select log_id from tt_custom_field_log where status = 1 and option_id = '.$report['cf_1_option_id'].')';
95 if ($report['project_id']) $dropdown_parts .= ' and l.project_id = '.$report['project_id'];
96 if ($report['task_id']) $dropdown_parts .= ' and l.task_id = '.$report['task_id'];
97 if ($report['billable']=='1') $dropdown_parts .= ' and l.billable = 1';
98 if ($report['billable']=='2') $dropdown_parts .= ' and l.billable = 0';
99 if ($report['invoice']=='1') $dropdown_parts .= ' and l.invoice_id is not NULL';
100 if ($report['invoice']=='2') $dropdown_parts .= ' and l.invoice_id is NULL';
101 if ($report['paid_status']=='1') $dropdown_parts .= ' and l.paid = 1';
102 if ($report['paid_status']=='2') $dropdown_parts .= ' and l.paid = 0';
104 // Prepare user list part.
106 if (($user->can('view_reports') || $user->isClient())) {
107 if ($report['users'])
108 $userlist = $report['users'];
110 $active_users = ttTeamHelper::getActiveUsers();
111 foreach ($active_users as $single_user)
112 $users[] = $single_user['id'];
113 $userlist = join(',', $users);
116 // Prepare sql query part for user list.
117 $user_list_part = null;
118 if ($user->can('view_reports') || $user->isClient())
119 $user_list_part = " and l.user_id in ($userlist)";
121 $user_list_part = " and l.user_id = ".$user->id;
123 // Prepare sql query part for where.
124 if ($report['period'])
125 $period = new Period($report['period'], new DateAndTime($user->date_format));
127 $period = new Period();
129 new DateAndTime($user->date_format, $report['period_start']),
130 new DateAndTime($user->date_format, $report['period_end']));
132 $where = " where l.status = 1 and l.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and l.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
133 " $user_list_part $dropdown_parts";
137 // getExpenseWhere prepares WHERE clause for expenses query in a report.
138 static function getExpenseWhere($bean) {
141 // Prepare dropdown parts.
142 $dropdown_parts = '';
143 if ($bean->getAttribute('client'))
144 $dropdown_parts .= ' and ei.client_id = '.$bean->getAttribute('client');
145 elseif ($user->isClient() && $user->client_id)
146 $dropdown_parts .= ' and ei.client_id = '.$user->client_id;
147 if ($bean->getAttribute('project')) $dropdown_parts .= ' and ei.project_id = '.$bean->getAttribute('project');
148 if ($bean->getAttribute('invoice')=='1') $dropdown_parts .= ' and ei.invoice_id is not NULL';
149 if ($bean->getAttribute('invoice')=='2') $dropdown_parts .= ' and ei.invoice_id is NULL';
150 if ($bean->getAttribute('paid_status')=='1') $dropdown_parts .= ' and ei.paid = 1';
151 if ($bean->getAttribute('paid_status')=='2') $dropdown_parts .= ' and ei.paid = 0';
153 // Prepare user list part.
155 if (($user->can('view_reports') || $user->isClient()) && is_array($bean->getAttribute('users')))
156 $userlist = join(',', $bean->getAttribute('users'));
157 // Prepare sql query part for user list.
158 $user_list_part = null;
159 if ($user->can('view_reports') || $user->isClient())
160 $user_list_part = " and ei.user_id in ($userlist)";
162 $user_list_part = " and ei.user_id = ".$user->id;
164 // Prepare sql query part for where.
165 if ($bean->getAttribute('period'))
166 $period = new Period($bean->getAttribute('period'), new DateAndTime($user->date_format));
168 $period = new Period();
170 new DateAndTime($user->date_format, $bean->getAttribute('start_date')),
171 new DateAndTime($user->date_format, $bean->getAttribute('end_date')));
173 $where = " where ei.status = 1 and ei.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and ei.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
174 " $user_list_part $dropdown_parts";
178 // getFavExpenseWhere prepares a WHERE clause for expenses query in a favorite report.
179 static function getFavExpenseWhere($report) {
182 // Prepare dropdown parts.
183 $dropdown_parts = '';
184 if ($report['client_id'])
185 $dropdown_parts .= ' and ei.client_id = '.$report['client_id'];
186 elseif ($user->isClient() && $user->client_id)
187 $dropdown_parts .= ' and ei.client_id = '.$user->client_id;
188 if ($report['project_id']) $dropdown_parts .= ' and ei.project_id = '.$report['project_id'];
189 if ($report['invoice']=='1') $dropdown_parts .= ' and ei.invoice_id is not NULL';
190 if ($report['invoice']=='2') $dropdown_parts .= ' and ei.invoice_id is NULL';
191 if ($report['paid_status']=='1') $dropdown_parts .= ' and ei.paid = 1';
192 if ($report['paid_status']=='2') $dropdown_parts .= ' and ei.paid = 0';
194 // Prepare user list part.
196 if (($user->can('view_reports') || $user->isClient())) {
197 if ($report['users'])
198 $userlist = $report['users'];
200 $active_users = ttTeamHelper::getActiveUsers();
201 foreach ($active_users as $single_user)
202 $users[] = $single_user['id'];
203 $userlist = join(',', $users);
206 // Prepare sql query part for user list.
207 $user_list_part = null;
208 if ($user->can('view_reports') || $user->isClient())
209 $user_list_part = " and ei.user_id in ($userlist)";
211 $user_list_part = " and ei.user_id = ".$user->id;
213 // Prepare sql query part for where.
214 if ($report['period'])
215 $period = new Period($report['period'], new DateAndTime($user->date_format));
217 $period = new Period();
219 new DateAndTime($user->date_format, $report['period_start']),
220 new DateAndTime($user->date_format, $report['period_end']));
222 $where = " where ei.status = 1 and ei.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and ei.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
223 " $user_list_part $dropdown_parts";
227 // getItems retrieves all items associated with a report.
228 // It combines tt_log and tt_expense_items in one array for presentation in one table using mysql union all.
229 // Expense items use the "note" field for item name.
230 static function getItems($bean) {
232 $mdb2 = getConnection();
234 // Determine these once as they are used in multiple places in this function.
235 $canViewReports = $user->can('view_reports');
236 $isClient = $user->isClient();
238 $group_by_option = $bean->getAttribute('group_by');
239 $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($bean->getAttribute('chstart') || $bean->getAttribute('chfinish'));
241 // Prepare a query for time items in tt_log table.
242 $fields = array(); // An array of fields for database query.
243 array_push($fields, 'l.id as id');
244 array_push($fields, '1 as type'); // Type 1 is for tt_log entries.
245 array_push($fields, 'l.date as date');
246 if($canViewReports || $isClient)
247 array_push($fields, 'u.name as user');
248 // Add client name if it is selected.
249 if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
250 array_push($fields, 'c.name as client');
251 // Add project name if it is selected.
252 if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
253 array_push($fields, 'p.name as project');
254 // Add task name if it is selected.
255 if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
256 array_push($fields, 't.name as task');
258 $include_cf_1 = $bean->getAttribute('chcf_1') || 'cf_1' == $group_by_option;
260 $custom_fields = new CustomFields($user->group_id);
261 $cf_1_type = $custom_fields->fields[0]['type'];
262 if ($cf_1_type == CustomFields::TYPE_TEXT) {
263 array_push($fields, 'cfl.value as cf_1');
264 } elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
265 array_push($fields, 'cfo.value as cf_1');
269 if ($bean->getAttribute('chstart')) {
270 array_push($fields, "l.start as unformatted_start");
271 array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start");
274 if ($bean->getAttribute('chfinish'))
275 array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish");
277 if ($bean->getAttribute('chduration'))
278 array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
280 if ($bean->getAttribute('chunits'))
281 array_push($fields, "if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit)) as units");
283 if ($bean->getAttribute('chnote'))
284 array_push($fields, 'l.comment as note');
286 $includeCost = $bean->getAttribute('chcost');
288 if (MODE_TIME == $user->tracking_mode)
289 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.
291 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.
292 array_push($fields, "null as expense");
295 if ($canViewReports && $bean->getAttribute('chpaid'))
296 array_push($fields, 'l.paid as paid');
298 if ($canViewReports && $bean->getAttribute('chip')) {
299 array_push($fields, 'l.created as created');
300 array_push($fields, 'l.created_ip as created_ip');
301 array_push($fields, 'l.modified as modified');
302 array_push($fields, 'l.modified_ip as modified_ip');
305 // Add invoice name if it is selected.
306 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
307 array_push($fields, 'i.name as invoice');
309 // Prepare sql query part for left joins.
311 if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
312 $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
313 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
314 $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
315 if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
316 $left_joins .= " left join tt_users u on (u.id = l.user_id)";
317 if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
318 $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
319 if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
320 $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
322 if ($cf_1_type == CustomFields::TYPE_TEXT)
323 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)";
324 elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
325 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)".
326 " left join tt_custom_field_options cfo on (cfl.option_id = cfo.id)";
329 if ($includeCost && MODE_TIME != $user->tracking_mode)
330 $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
332 $where = ttReportHelper::getWhere($bean);
334 // Construct sql query for tt_log items.
335 $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
336 // If we don't have expense items (such as when the Expenses plugin is desabled), the above is all sql we need,
337 // with an exception of sorting part, that is added in the end.
339 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
340 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
342 $fields = array(); // An array of fields for database query.
343 array_push($fields, 'ei.id');
344 array_push($fields, '2 as type'); // Type 2 is for tt_expense_items entries.
345 array_push($fields, 'ei.date');
346 if($canViewReports || $isClient)
347 array_push($fields, 'u.name as user');
348 // Add client name if it is selected.
349 if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
350 array_push($fields, 'c.name as client');
351 // Add project name if it is selected.
352 if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
353 array_push($fields, 'p.name as project');
354 if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
355 array_push($fields, 'null'); // null for task name. We need to match column count for union.
356 if ($bean->getAttribute('chcf_1') || 'cf_1' == $group_by_option)
357 array_push($fields, 'null'); // null for cf_1.
358 if ($bean->getAttribute('chstart')) {
359 array_push($fields, 'null'); // null for unformatted_start.
360 array_push($fields, 'null'); // null for start.
362 if ($bean->getAttribute('chfinish'))
363 array_push($fields, 'null'); // null for finish.
364 if ($bean->getAttribute('chduration'))
365 array_push($fields, 'null'); // null for duration.
366 // Use the note field to print item name.
367 if ($bean->getAttribute('chnote'))
368 array_push($fields, 'ei.name as note');
369 array_push($fields, 'ei.cost as cost');
370 array_push($fields, 'ei.cost as expense');
372 if ($canViewReports && $bean->getAttribute('chpaid'))
373 array_push($fields, 'ei.paid as paid');
375 if ($canViewReports && $bean->getAttribute('chip')) {
376 array_push($fields, 'ei.created as created');
377 array_push($fields, 'ei.created_ip as created_ip');
378 array_push($fields, 'ei.modified as modified');
379 array_push($fields, 'ei.modified_ip as modified_ip');
382 // Add invoice name if it is selected.
383 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
384 array_push($fields, 'i.name as invoice');
387 if ($bean->getAttribute('chunits'))
388 array_push($fields, 'null'); // null for work units.
390 // Prepare sql query part for left joins.
392 if ($canViewReports || $isClient)
393 $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
394 if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
395 $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
396 if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
397 $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
398 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
399 $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
401 $where = ttReportHelper::getExpenseWhere($bean);
403 // Construct sql query for expense items.
404 $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
406 // Construct a union.
407 $sql = "($sql) union all ($sql_for_expense_items)";
410 // Determine sort part.
411 $sort_part = ' order by ';
412 if ('no_grouping' == $group_by_option || 'date' == $group_by_option)
413 $sort_part .= 'date';
415 $sort_part .= $group_by_option.', date';
416 if (($canViewReports || $isClient) && is_array($bean->getAttribute('users')) && 'user' != $group_by_option)
417 $sort_part .= ', user, type';
418 if ($bean->getAttribute('chstart'))
419 $sort_part .= ', unformatted_start';
420 $sort_part .= ', id';
423 // By now we are ready with sql.
425 // Obtain items for report.
426 $res = $mdb2->query($sql);
427 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
429 while ($val = $res->fetchRow()) {
430 if ($convertTo12Hour) {
431 if($val['start'] != '')
432 $val['start'] = ttTimeHelper::to12HourFormat($val['start']);
433 if($val['finish'] != '')
434 $val['finish'] = ttTimeHelper::to12HourFormat($val['finish']);
436 if (isset($val['cost'])) {
437 if ('.' != $user->decimal_mark)
438 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
440 if (isset($val['expense'])) {
441 if ('.' != $user->decimal_mark)
442 $val['expense'] = str_replace('.', $user->decimal_mark, $val['expense']);
444 if ('no_grouping' != $group_by_option) {
445 $val['grouped_by'] = $val[$group_by_option];
446 if ('date' == $group_by_option) {
447 // This is needed to get the date in user date format.
448 $o_date = new DateAndTime(DB_DATEFORMAT, $val['grouped_by']);
449 $val['grouped_by'] = $o_date->toString($user->date_format);
454 // This is needed to get the date in user date format.
455 $o_date = new DateAndTime(DB_DATEFORMAT, $val['date']);
456 $val['date'] = $o_date->toString($user->date_format);
460 $report_items[] = $row;
463 return $report_items;
466 // putInSession stores tt_log and tt_expense_items ids from a report in user session
467 // as 2 comma-separated lists.
468 static function putInSession($report_items) {
469 unset($_SESSION['report_item_ids']);
470 unset($_SESSION['report_item_expense_ids']);
472 // Iterate through records and build 2 comma-separated lists.
473 foreach($report_items as $item) {
474 if ($item['type'] == 1)
475 $report_item_ids .= ','.$item['id'];
476 else if ($item['type'] == 2)
477 $report_item_expense_ids .= ','.$item['id'];
479 $report_item_ids = trim($report_item_ids, ',');
480 $report_item_expense_ids = trim($report_item_expense_ids, ',');
482 // The lists are reqdy. Put them in session.
483 if ($report_item_ids) $_SESSION['report_item_ids'] = $report_item_ids;
484 if ($report_item_expense_ids) $_SESSION['report_item_expense_ids'] = $report_item_expense_ids;
487 // getFromSession obtains tt_log and tt_expense_items ids stored in user session.
488 static function getFromSession() {
490 $report_item_ids = $_SESSION['report_item_ids'];
491 if ($report_item_ids)
492 $items['report_item_ids'] = explode(',', $report_item_ids);
493 $report_item_expense_ids = $_SESSION['report_item_expense_ids'];
494 if ($report_item_expense_ids)
495 $items['report_item_expense_ids'] = explode(',', $report_item_expense_ids);
499 // getFavItems retrieves all items associated with a favorite report.
500 // It combines tt_log and tt_expense_items in one array for presentation in one table using mysql union all.
501 // Expense items use the "note" field for item name.
502 static function getFavItems($report) {
504 $mdb2 = getConnection();
506 // Determine these once as they are used in multiple places in this function.
507 $canViewReports = $user->can('view_reports');
508 $isClient = $user->isClient();
510 $group_by_option = $report['group_by'];
511 $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($report['show_start'] || $report['show_end']);
513 // Prepare a query for time items in tt_log table.
514 $fields = array(); // An array of fields for database query.
515 array_push($fields, 'l.id as id');
516 array_push($fields, '1 as type'); // Type 1 is for tt_log entries.
517 array_push($fields, 'l.date as date');
518 if($canViewReports || $isClient)
519 array_push($fields, 'u.name as user');
520 // Add client name if it is selected.
521 if ($report['show_client'] || 'client' == $group_by_option)
522 array_push($fields, 'c.name as client');
523 // Add project name if it is selected.
524 if ($report['show_project'] || 'project' == $group_by_option)
525 array_push($fields, 'p.name as project');
526 // Add task name if it is selected.
527 if ($report['show_task'] || 'task' == $group_by_option)
528 array_push($fields, 't.name as task');
530 $include_cf_1 = $report['show_custom_field_1'] || 'cf_1' == $group_by_option;
532 $custom_fields = new CustomFields($user->group_id);
533 $cf_1_type = $custom_fields->fields[0]['type'];
534 if ($cf_1_type == CustomFields::TYPE_TEXT) {
535 array_push($fields, 'cfl.value as cf_1');
536 } elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
537 array_push($fields, 'cfo.value as cf_1');
541 if ($report['show_start']) {
542 array_push($fields, "l.start as unformatted_start");
543 array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start");
546 if ($report['show_end'])
547 array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish");
549 if ($report['show_duration'])
550 array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
552 if ($report['show_work_units'])
553 array_push($fields, "if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit)) as units");
556 if ($report['show_note'])
557 array_push($fields, 'l.comment as note');
559 $includeCost = $report['show_cost'];
561 if (MODE_TIME == $user->tracking_mode)
562 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.
564 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.
565 array_push($fields, "null as expense");
568 if ($canViewReports && $report['show_paid'])
569 array_push($fields, 'l.paid as paid');
571 if ($canViewReports && $report['show_ip']) {
572 array_push($fields, 'l.created as created');
573 array_push($fields, 'l.created_ip as created_ip');
574 array_push($fields, 'l.modified as modified');
575 array_push($fields, 'l.modified_ip as modified_ip');
577 // Add invoice name if it is selected.
578 if (($canViewReports || $isClient) && $report['show_invoice'])
579 array_push($fields, 'i.name as invoice');
581 // Prepare sql query part for left joins.
583 if ($report['show_client'] || 'client' == $group_by_option)
584 $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
585 if (($canViewReports || $isClient) && $report['show_invoice'])
586 $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
587 if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
588 $left_joins .= " left join tt_users u on (u.id = l.user_id)";
589 if ($report['show_project'] || 'project' == $group_by_option)
590 $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
591 if ($report['show_task'] || 'task' == $group_by_option)
592 $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
594 if ($cf_1_type == CustomFields::TYPE_TEXT)
595 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)";
596 elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
597 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)".
598 " left join tt_custom_field_options cfo on (cfl.option_id = cfo.id)";
601 if ($includeCost && MODE_TIME != $user->tracking_mode)
602 $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
604 $where = ttReportHelper::getFavWhere($report);
606 // Construct sql query for tt_log items.
607 $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
608 // If we don't have expense items (such as when the Expenses plugin is desabled), the above is all sql we need,
609 // with an exception of sorting part, that is added in the end.
611 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
612 if ($report['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
614 $fields = array(); // An array of fields for database query.
615 array_push($fields, 'ei.id');
616 array_push($fields, '2 as type'); // Type 2 is for tt_expense_items entries.
617 array_push($fields, 'ei.date');
618 if($canViewReports || $isClient)
619 array_push($fields, 'u.name as user');
620 // Add client name if it is selected.
621 if ($report['show_client'] || 'client' == $group_by_option)
622 array_push($fields, 'c.name as client');
623 // Add project name if it is selected.
624 if ($report['show_project'] || 'project' == $group_by_option)
625 array_push($fields, 'p.name as project');
626 if ($report['show_task'] || 'task' == $group_by_option)
627 array_push($fields, 'null'); // null for task name. We need to match column count for union.
628 if ($report['show_custom_field_1'] || 'cf_1' == $group_by_option)
629 array_push($fields, 'null'); // null for cf_1.
630 if ($report['show_start']) {
631 array_push($fields, 'null'); // null for unformatted_start.
632 array_push($fields, 'null'); // null for start.
634 if ($report['show_end'])
635 array_push($fields, 'null'); // null for finish.
636 if ($report['show_duration'])
637 array_push($fields, 'null'); // null for duration.
638 if ($report['show_work_units'])
639 array_push($fields, 'null'); // null for work units.
640 // Use the note field to print item name.
641 if ($report['show_note'])
642 array_push($fields, 'ei.name as note');
643 array_push($fields, 'ei.cost as cost');
644 array_push($fields, 'ei.cost as expense');
646 if ($canViewReports && $report['show_paid'])
647 array_push($fields, 'ei.paid as paid');
649 if ($canViewReports && $report['show_ip']) {
650 array_push($fields, 'ei.created as created');
651 array_push($fields, 'ei.created_ip as created_ip');
652 array_push($fields, 'ei.modified as modified');
653 array_push($fields, 'ei.modified_ip as modified_ip');
655 // Add invoice name if it is selected.
656 if (($canViewReports || $isClient) && $report['show_invoice'])
657 array_push($fields, 'i.name as invoice');
659 // Prepare sql query part for left joins.
661 if ($canViewReports || $isClient)
662 $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
663 if ($report['show_client'] || 'client' == $group_by_option)
664 $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
665 if ($report['show_project'] || 'project' == $group_by_option)
666 $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
667 if (($canViewReports || $isClient) && $report['show_invoice'])
668 $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
670 $where = ttReportHelper::getFavExpenseWhere($report);
672 // Construct sql query for expense items.
673 $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
675 // Construct a union.
676 $sql = "($sql) union all ($sql_for_expense_items)";
679 // Determine sort part.
680 $sort_part = ' order by ';
681 if ($group_by_option == null || 'no_grouping' == $group_by_option || 'date' == $group_by_option) // TODO: fix DB for NULL values in group_by field.
682 $sort_part .= 'date';
684 $sort_part .= $group_by_option.', date';
685 if (($canViewReports || $isClient) /*&& is_array($bean->getAttribute('users'))*/ && 'user' != $group_by_option)
686 $sort_part .= ', user, type';
687 if ($report['show_start'])
688 $sort_part .= ', unformatted_start';
689 $sort_part .= ', id';
692 // By now we are ready with sql.
694 // Obtain items for report.
695 $res = $mdb2->query($sql);
696 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
698 while ($val = $res->fetchRow()) {
699 if ($convertTo12Hour) {
700 if($val['start'] != '')
701 $val['start'] = ttTimeHelper::to12HourFormat($val['start']);
702 if($val['finish'] != '')
703 $val['finish'] = ttTimeHelper::to12HourFormat($val['finish']);
705 if (isset($val['cost'])) {
706 if ('.' != $user->decimal_mark)
707 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
709 if (isset($val['expense'])) {
710 if ('.' != $user->decimal_mark)
711 $val['expense'] = str_replace('.', $user->decimal_mark, $val['expense']);
713 if ('no_grouping' != $group_by_option) {
714 $val['grouped_by'] = $val[$group_by_option];
715 if ('date' == $group_by_option) {
716 // This is needed to get the date in user date format.
717 $o_date = new DateAndTime(DB_DATEFORMAT, $val['grouped_by']);
718 $val['grouped_by'] = $o_date->toString($user->date_format);
723 // This is needed to get the date in user date format.
724 $o_date = new DateAndTime(DB_DATEFORMAT, $val['date']);
725 $val['date'] = $o_date->toString($user->date_format);
729 $report_items[] = $row;
732 return $report_items;
735 // getSubtotals calculates report items subtotals when a report is grouped by.
736 // Without expenses, it's a simple select with group by.
737 // With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
738 static function getSubtotals($bean) {
741 $group_by_option = $bean->getAttribute('group_by');
742 if ('no_grouping' == $group_by_option) return null;
744 $mdb2 = getConnection();
746 // Start with sql to obtain subtotals for time items. This simple sql will be used when we have no expenses.
748 // Determine group by field and a required join.
749 switch ($group_by_option) {
751 $group_field = 'l.date';
755 $group_field = 'u.name';
756 $group_join = 'left join tt_users u on (l.user_id = u.id) ';
759 $group_field = 'c.name';
760 $group_join = 'left join tt_clients c on (l.client_id = c.id) ';
763 $group_field = 'p.name';
764 $group_join = 'left join tt_projects p on (l.project_id = p.id) ';
767 $group_field = 't.name';
768 $group_join = 'left join tt_tasks t on (l.task_id = t.id) ';
771 $group_field = 'cfo.value';
772 $custom_fields = new CustomFields($user->group_id);
773 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
774 $group_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) ';
775 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
776 $group_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) ';
780 $where = ttReportHelper::getWhere($bean);
781 if ($bean->getAttribute('chcost')) {
782 if (MODE_TIME == $user->tracking_mode) {
783 if ($group_by_option != 'user')
784 $left_join = 'left join tt_users u on (l.user_id = u.id)';
785 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
786 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
787 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
788 null as expenses from tt_log l
789 $group_join $left_join $where group by $group_field";
791 // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
792 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
793 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
794 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
795 null as expenses from tt_log l
797 left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where group by $group_field";
800 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
801 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
802 null as expenses from tt_log l
803 $group_join $where group by $group_field";
805 // By now we have sql for time items.
807 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
808 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
810 // Determine group by field and a required join.
812 $group_field = 'null';
813 switch ($group_by_option) {
815 $group_field = 'ei.date';
819 $group_field = 'u.name';
820 $group_join = 'left join tt_users u on (ei.user_id = u.id) ';
823 $group_field = 'c.name';
824 $group_join = 'left join tt_clients c on (ei.client_id = c.id) ';
827 $group_field = 'p.name';
828 $group_join = 'left join tt_projects p on (ei.project_id = p.id) ';
832 $where = ttReportHelper::getExpenseWhere($bean);
833 $sql_for_expenses = "select $group_field as group_field, null as time, null as units, sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei
835 // Add a "group by" clause if we are grouping.
836 if ('null' != $group_field) $sql_for_expenses .= " group by $group_field";
838 // Create a combined query.
839 $sql = "select group_field, sum(time) as time, sum(units) as units, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by group_field";
843 $res = $mdb2->query($sql);
844 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
846 while ($val = $res->fetchRow()) {
847 if ('date' == $group_by_option) {
848 // This is needed to get the date in user date format.
849 $o_date = new DateAndTime(DB_DATEFORMAT, $val['group_field']);
850 $val['group_field'] = $o_date->toString($user->date_format);
853 $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
854 if ($bean->getAttribute('chcost')) {
855 if ('.' != $user->decimal_mark) {
856 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
857 $val['expenses'] = str_replace('.', $user->decimal_mark, $val['expenses']);
859 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time, 'units'=> $val['units'],'cost'=>$val['cost'],'expenses'=>$val['expenses']);
861 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time, 'units'=> $val['units']);
867 // getFavSubtotals calculates report items subtotals when a favorite report is grouped by.
868 // Without expenses, it's a simple select with group by.
869 // With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
870 static function getFavSubtotals($report) {
873 $group_by_option = $report['group_by'];
874 if ('no_grouping' == $group_by_option) return null;
876 $mdb2 = getConnection();
878 // Start with sql to obtain subtotals for time items. This simple sql will be used when we have no expenses.
880 // Determine group by field and a required join.
881 switch ($group_by_option) {
883 $group_field = 'l.date';
887 $group_field = 'u.name';
888 $group_join = 'left join tt_users u on (l.user_id = u.id) ';
891 $group_field = 'c.name';
892 $group_join = 'left join tt_clients c on (l.client_id = c.id) ';
895 $group_field = 'p.name';
896 $group_join = 'left join tt_projects p on (l.project_id = p.id) ';
899 $group_field = 't.name';
900 $group_join = 'left join tt_tasks t on (l.task_id = t.id) ';
903 $group_field = 'cfo.value';
904 $custom_fields = new CustomFields($user->group_id);
905 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
906 $group_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) ';
907 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
908 $group_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) ';
912 $where = ttReportHelper::getFavWhere($report);
913 if ($report['show_cost']) {
914 if (MODE_TIME == $user->tracking_mode) {
915 if ($group_by_option != 'user')
916 $left_join = 'left join tt_users u on (l.user_id = u.id)';
917 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
918 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
919 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
920 null as expenses from tt_log l
921 $group_join $left_join $where group by $group_field";
923 // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
924 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
925 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
926 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
927 null as expenses from tt_log l
929 left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where group by $group_field";
932 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
933 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
934 null as expenses from tt_log l
935 $group_join $where group by $group_field";
937 // By now we have sql for time items.
939 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
940 if ($report['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
942 // Determine group by field and a required join.
944 $group_field = 'null';
945 switch ($group_by_option) {
947 $group_field = 'ei.date';
951 $group_field = 'u.name';
952 $group_join = 'left join tt_users u on (ei.user_id = u.id) ';
955 $group_field = 'c.name';
956 $group_join = 'left join tt_clients c on (ei.client_id = c.id) ';
959 $group_field = 'p.name';
960 $group_join = 'left join tt_projects p on (ei.project_id = p.id) ';
964 $where = ttReportHelper::getFavExpenseWhere($report);
965 $sql_for_expenses = "select $group_field as group_field, null as time, null as units, sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei
967 // Add a "group by" clause if we are grouping.
968 if ('null' != $group_field) $sql_for_expenses .= " group by $group_field";
970 // Create a combined query.
971 $sql = "select group_field, sum(time) as time, sum(units) as units, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by group_field";
975 $res = $mdb2->query($sql);
976 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
978 while ($val = $res->fetchRow()) {
979 if ('date' == $group_by_option) {
980 // This is needed to get the date in user date format.
981 $o_date = new DateAndTime(DB_DATEFORMAT, $val['group_field']);
982 $val['group_field'] = $o_date->toString($user->date_format);
985 $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
986 if ($report['show_cost']) {
987 if ('.' != $user->decimal_mark) {
988 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
989 $val['expenses'] = str_replace('.', $user->decimal_mark, $val['expenses']);
991 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time, 'units'=> $val['units'], 'cost'=>$val['cost'],'expenses'=>$val['expenses']);
993 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time, 'units'=> $val['units']);
999 // getTotals calculates total hours and cost for all report items.
1000 static function getTotals($bean)
1004 $mdb2 = getConnection();
1006 $where = ttReportHelper::getWhere($bean);
1008 // TODO: build query in parts so the work units inclusion is conditional.
1010 // Start with a query for time items.
1011 if ($bean->getAttribute('chcost')) {
1012 if (MODE_TIME == $user->tracking_mode) {
1013 $sql = "select sum(time_to_sec(l.duration)) as time,
1014 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
1015 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
1018 left join tt_users u on (l.user_id = u.id) $where";
1020 $sql = "select sum(time_to_sec(l.duration)) as time,
1021 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
1022 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
1025 left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where";
1028 $sql = "select sum(time_to_sec(l.duration)) as time,"
1029 ." sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,"
1030 ." null as cost, null as expenses from tt_log l $where";
1032 // If we have expenses, query becomes a bit more complex.
1033 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) {
1034 $where = ttReportHelper::getExpenseWhere($bean);
1035 $sql_for_expenses = "select null as time, null as units, sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
1037 // Create a combined query.
1038 $sql = "select sum(time) as time, sum(units) as units, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
1042 $res = $mdb2->query($sql);
1043 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
1045 $val = $res->fetchRow();
1046 $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
1047 if ($bean->getAttribute('chcost')) {
1048 $total_cost = $val['cost'];
1049 if (!$total_cost) $total_cost = '0.00';
1050 if ('.' != $user->decimal_mark)
1051 $total_cost = str_replace('.', $user->decimal_mark, $total_cost);
1052 $total_expenses = $val['expenses'];
1053 if (!$total_expenses) $total_expenses = '0.00';
1054 if ('.' != $user->decimal_mark)
1055 $total_expenses = str_replace('.', $user->decimal_mark, $total_expenses);
1058 if ($bean->getAttribute('period'))
1059 $period = new Period($bean->getAttribute('period'), new DateAndTime($user->date_format));
1061 $period = new Period();
1063 new DateAndTime($user->date_format, $bean->getAttribute('start_date')),
1064 new DateAndTime($user->date_format, $bean->getAttribute('end_date')));
1067 $totals['start_date'] = $period->getStartDate();
1068 $totals['end_date'] = $period->getEndDate();
1069 $totals['time'] = $total_time;
1070 $totals['units'] = $val['units'];
1071 $totals['cost'] = $total_cost;
1072 $totals['expenses'] = $total_expenses;
1077 // getFavTotals calculates total hours and cost for all favorite report items.
1078 static function getFavTotals($report)
1082 $mdb2 = getConnection();
1084 $where = ttReportHelper::getFavWhere($report);
1086 // Start with a query for time items.
1087 if ($report['show_cost']) {
1088 if (MODE_TIME == $user->tracking_mode) {
1089 $sql = "select sum(time_to_sec(l.duration)) as time,
1090 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
1091 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
1094 left join tt_users u on (l.user_id = u.id) $where";
1096 $sql = "select sum(time_to_sec(l.duration)) as time,
1097 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
1098 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
1101 left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where";
1104 $sql = "select sum(time_to_sec(l.duration)) as time,
1105 sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units,
1106 null as cost, null as expenses from tt_log l $where";
1108 // If we have expenses, query becomes a bit more complex.
1109 if ($report['show_cost'] && $user->isPluginEnabled('ex')) {
1110 $where = ttReportHelper::getFavExpenseWhere($report);
1111 $sql_for_expenses = "select null as time, null as units, sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
1112 // Create a combined query.
1113 $sql = "select sum(time) as time, sum(units) as units, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
1117 $res = $mdb2->query($sql);
1118 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
1120 $val = $res->fetchRow();
1121 $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
1122 if ($report['show_cost']) {
1123 $total_cost = $val['cost'];
1124 if (!$total_cost) $total_cost = '0.00';
1125 if ('.' != $user->decimal_mark)
1126 $total_cost = str_replace('.', $user->decimal_mark, $total_cost);
1127 $total_expenses = $val['expenses'];
1128 if (!$total_expenses) $total_expenses = '0.00';
1129 if ('.' != $user->decimal_mark)
1130 $total_expenses = str_replace('.', $user->decimal_mark, $total_expenses);
1133 if ($report['period'])
1134 $period = new Period($report['period'], new DateAndTime($user->date_format));
1136 $period = new Period();
1138 new DateAndTime($user->date_format, $report['period_start']),
1139 new DateAndTime($user->date_format, $report['period_end']));
1142 $totals['start_date'] = $period->getStartDate();
1143 $totals['end_date'] = $period->getEndDate();
1144 $totals['time'] = $total_time;
1145 $totals['units'] = $val['units'];
1146 $totals['cost'] = $total_cost;
1147 $totals['expenses'] = $total_expenses;
1152 // The assignToInvoice assigns a set of records to a specific invoice.
1153 static function assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids)
1155 $mdb2 = getConnection();
1156 if ($time_log_ids) {
1157 $sql = "update tt_log set invoice_id = ".$mdb2->quote($invoice_id).
1158 " where id in(".join(', ', $time_log_ids).")";
1159 $affected = $mdb2->exec($sql);
1160 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1162 if ($expense_item_ids) {
1163 $sql = "update tt_expense_items set invoice_id = ".$mdb2->quote($invoice_id).
1164 " where id in(".join(', ', $expense_item_ids).")";
1165 $affected = $mdb2->exec($sql);
1166 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1170 // The markPaid marks a set of records as either paid or unpaid.
1171 static function markPaid($time_log_ids, $expense_item_ids, $paid = true)
1173 $mdb2 = getConnection();
1174 $paid_val = (int) $paid;
1175 if ($time_log_ids) {
1176 $sql = "update tt_log set paid = $paid_val where id in(".join(', ', $time_log_ids).")";
1177 $affected = $mdb2->exec($sql);
1178 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1180 if ($expense_item_ids) {
1181 $sql = "update tt_expense_items set paid = $paid_val where id in(".join(', ', $expense_item_ids).")";
1182 $affected = $mdb2->exec($sql);
1183 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1187 // prepareReportBody - prepares an email body for report.
1188 static function prepareReportBody($bean, $comment)
1193 // Determine these once as they are used in multiple places in this function.
1194 $canViewReports = $user->can('view_reports');
1195 $isClient = $user->isClient();
1197 $items = ttReportHelper::getItems($bean);
1198 $group_by = $bean->getAttribute('group_by');
1199 if ($group_by && 'no_grouping' != $group_by)
1200 $subtotals = ttReportHelper::getSubtotals($bean);
1201 $totals = ttReportHelper::getTotals($bean);
1203 // Use custom fields plugin if it is enabled.
1204 if ($user->isPluginEnabled('cf'))
1205 $custom_fields = new CustomFields($user->group_id);
1207 // Define some styles to use in email.
1208 $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
1209 $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
1210 $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
1211 $rowItem = 'background-color: #ffffff;';
1212 $rowItemAlt = 'background-color: #f5f5f5;';
1213 $rowSubtotal = 'background-color: #e0e0e0;';
1214 $cellLeftAligned = 'text-align: left; vertical-align: top;';
1215 $cellRightAligned = 'text-align: right; vertical-align: top;';
1216 $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
1217 $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
1219 // Start creating email body.
1221 $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
1225 $body .= '<p style="'.$style_title.'">'.$i18n->get('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
1228 if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>';
1230 if ($bean->getAttribute('chtotalsonly')) {
1231 // Totals only report. Output subtotals.
1233 // Determine group_by header.
1234 if ('cf_1' == $group_by)
1235 $group_by_header = htmlspecialchars($custom_fields->fields[0]['label']);
1237 $key = 'label.'.$group_by;
1238 $group_by_header = $i18n->get($key);
1241 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1243 $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
1244 if ($bean->getAttribute('chduration'))
1245 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
1246 if ($bean->getAttribute('chunits'))
1247 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
1248 if ($bean->getAttribute('chcost'))
1249 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
1251 foreach($subtotals as $subtotal) {
1252 $body .= '<tr style="'.$rowSubtotal.'">';
1253 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : ' ').'</td>';
1254 if ($bean->getAttribute('chduration')) {
1255 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1256 if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
1259 if ($bean->getAttribute('chunits')) {
1260 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1261 $body .= $subtotal['units'];
1264 if ($bean->getAttribute('chcost')) {
1265 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1266 $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
1273 $body .= '<tr><td> </td></tr>';
1274 $body .= '<tr style="'.$rowSubtotal.'">';
1275 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
1276 if ($bean->getAttribute('chduration')) {
1277 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1278 if ($totals['time'] <> '0:00') $body .= $totals['time'];
1281 if ($bean->getAttribute('chunits')) {
1282 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1283 $body .= $totals['units'];
1286 if ($bean->getAttribute('chcost')) {
1287 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1288 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1293 $body .= '</table>';
1297 // Print table header.
1298 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1300 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.date').'</td>';
1301 if ($canViewReports || $isClient)
1302 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.user').'</td>';
1303 if ($bean->getAttribute('chclient'))
1304 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.client').'</td>';
1305 if ($bean->getAttribute('chproject'))
1306 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.project').'</td>';
1307 if ($bean->getAttribute('chtask'))
1308 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.task').'</td>';
1309 if ($bean->getAttribute('chcf_1'))
1310 $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
1311 if ($bean->getAttribute('chstart'))
1312 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.start').'</td>';
1313 if ($bean->getAttribute('chfinish'))
1314 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.finish').'</td>';
1315 if ($bean->getAttribute('chduration'))
1316 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
1317 if ($bean->getAttribute('chunits'))
1318 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
1319 if ($bean->getAttribute('chnote'))
1320 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.note').'</td>';
1321 if ($bean->getAttribute('chcost'))
1322 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
1323 if ($bean->getAttribute('chpaid'))
1324 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.paid').'</td>';
1325 if ($bean->getAttribute('chip'))
1326 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.ip').'</td>';
1327 if ($bean->getAttribute('chinvoice'))
1328 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.invoice').'</td>';
1331 // Initialize variables to print subtotals.
1332 if ($items && 'no_grouping' != $group_by) {
1333 $print_subtotals = true;
1335 $prev_grouped_by = '';
1336 $cur_grouped_by = '';
1338 // Initialize variables to alternate color of rows for different dates.
1341 $row_style = $rowItem;
1343 // Print report items.
1344 if (is_array($items)) {
1345 foreach ($items as $record) {
1346 $cur_date = $record['date'];
1347 // Print a subtotal row after a block of grouped items.
1348 if ($print_subtotals) {
1349 $cur_grouped_by = $record['grouped_by'];
1350 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
1351 $body .= '<tr style="'.$rowSubtotal.'">';
1352 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
1353 $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
1354 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1355 if ($bean->getAttribute('chclient')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1356 if ($bean->getAttribute('chproject')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1357 if ($bean->getAttribute('chtask')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1358 if ($bean->getAttribute('chcf_1')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1359 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1360 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1361 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
1362 if ($bean->getAttribute('chunits')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['units'].'</td>';
1363 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1364 if ($bean->getAttribute('chcost')) {
1365 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1366 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
1369 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1370 if ($bean->getAttribute('chip')) $body .= '<td></td>';
1371 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1373 $body .= '<tr><td> </td></tr>';
1375 $first_pass = false;
1378 // Print a regular row.
1379 if ($cur_date != $prev_date)
1380 $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
1381 $body .= '<tr style="'.$row_style.'">';
1382 $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
1383 if ($canViewReports || $isClient)
1384 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
1385 if ($bean->getAttribute('chclient'))
1386 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
1387 if ($bean->getAttribute('chproject'))
1388 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
1389 if ($bean->getAttribute('chtask'))
1390 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
1391 if ($bean->getAttribute('chcf_1'))
1392 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
1393 if ($bean->getAttribute('chstart'))
1394 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
1395 if ($bean->getAttribute('chfinish'))
1396 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
1397 if ($bean->getAttribute('chduration'))
1398 $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
1399 if ($bean->getAttribute('chunits'))
1400 $body .= '<td style="'.$cellRightAligned.'">'.$record['units'].'</td>';
1401 if ($bean->getAttribute('chnote'))
1402 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
1403 if ($bean->getAttribute('chcost'))
1404 $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
1405 if ($bean->getAttribute('chpaid')) {
1406 $body .= '<td style="'.$cellRightAligned.'">';
1407 $body .= $record['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
1410 if ($bean->getAttribute('chip')) {
1411 $body .= '<td style="'.$cellRightAligned.'">';
1412 $body .= $record['modified'] ? $record['modified_ip'].' '.$record['modified'] : $record['created_ip'].' '.$record['created'];
1415 if ($bean->getAttribute('chinvoice'))
1416 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
1419 $prev_date = $record['date'];
1420 if ($print_subtotals)
1421 $prev_grouped_by = $record['grouped_by'];
1425 // Print a terminating subtotal.
1426 if ($print_subtotals) {
1427 $body .= '<tr style="'.$rowSubtotal.'">';
1428 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
1429 $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
1430 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1431 if ($bean->getAttribute('chclient')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1432 if ($bean->getAttribute('chproject')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1433 if ($bean->getAttribute('chtask')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1434 if ($bean->getAttribute('chcf_1')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1435 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1436 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1437 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
1438 if ($bean->getAttribute('chunits')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['units'].'</td>';
1439 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1440 if ($bean->getAttribute('chcost')) {
1441 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1442 $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
1445 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1446 if ($bean->getAttribute('chip')) $body .= '<td></td>';
1447 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1452 $body .= '<tr><td> </td></tr>';
1453 $body .= '<tr style="'.$rowSubtotal.'">';
1454 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
1455 if ($canViewReports || $isClient) $body .= '<td></td>';
1456 if ($bean->getAttribute('chclient')) $body .= '<td></td>';
1457 if ($bean->getAttribute('chproject')) $body .= '<td></td>';
1458 if ($bean->getAttribute('chtask')) $body .= '<td></td>';
1459 if ($bean->getAttribute('chcf_1')) $body .= '<td></td>';
1460 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1461 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1462 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
1463 if ($bean->getAttribute('chunits')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['units'].'</td>';
1464 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1465 if ($bean->getAttribute('chcost')) {
1466 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1467 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1470 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1471 if ($bean->getAttribute('chip')) $body .= '<td></td>';
1472 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1475 $body .= '</table>';
1479 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
1480 $body .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
1482 // Finish creating email body.
1483 $body .= '</body></html>';
1488 // checkFavReportCondition - checks whether it is okay to send fav report.
1489 static function checkFavReportCondition($report, $condition)
1491 $items = ttReportHelper::getFavItems($report);
1493 $condition = str_replace('count', '', $condition);
1494 $count_required = (int) trim(str_replace('>', '', $condition));
1496 if (count($items) > $count_required)
1497 return true; // Condition ok.
1502 // prepareFavReportBody - prepares an email body for a favorite report.
1503 static function prepareFavReportBody($report)
1508 // Determine these once as they are used in multiple places in this function.
1509 $canViewReports = $user->can('view_reports');
1510 $isClient = $user->isClient();
1512 $items = ttReportHelper::getFavItems($report);
1513 $group_by = $report['group_by'];
1514 if ($group_by && 'no_grouping' != $group_by)
1515 $subtotals = ttReportHelper::getFavSubtotals($report);
1516 $totals = ttReportHelper::getFavTotals($report);
1518 // Use custom fields plugin if it is enabled.
1519 if ($user->isPluginEnabled('cf'))
1520 $custom_fields = new CustomFields($user->group_id);
1522 // Define some styles to use in email.
1523 $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
1524 $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
1525 $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
1526 $rowItem = 'background-color: #ffffff;';
1527 $rowItemAlt = 'background-color: #f5f5f5;';
1528 $rowSubtotal = 'background-color: #e0e0e0;';
1529 $cellLeftAligned = 'text-align: left; vertical-align: top;';
1530 $cellRightAligned = 'text-align: right; vertical-align: top;';
1531 $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
1532 $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
1534 // Start creating email body.
1536 $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
1540 $body .= '<p style="'.$style_title.'">'.$i18n->get('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
1543 // if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>'; // No comment for fav. reports.
1545 if ($report['show_totals_only']) {
1546 // Totals only report. Output subtotals.
1548 // Determine group_by header.
1549 if ('cf_1' == $group_by)
1550 $group_by_header = htmlspecialchars($custom_fields->fields[0]['label']);
1552 $key = 'label.'.$group_by;
1553 $group_by_header = $i18n->get($key);
1556 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1558 $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
1559 if ($report['show_duration'])
1560 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
1561 if ($report['show_work_units'])
1562 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
1563 if ($report['show_cost'])
1564 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
1566 foreach($subtotals as $subtotal) {
1567 $body .= '<tr style="'.$rowSubtotal.'">';
1568 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : ' ').'</td>';
1569 if ($report['show_duration']) {
1570 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1571 if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
1574 if ($report['show_work_units']) {
1575 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1576 $body .= $subtotal['units'];
1579 if ($report['show_cost']) {
1580 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1581 $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
1588 $body .= '<tr><td> </td></tr>';
1589 $body .= '<tr style="'.$rowSubtotal.'">';
1590 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
1591 if ($report['show_duration']) {
1592 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1593 if ($totals['time'] <> '0:00') $body .= $totals['time'];
1596 if ($report['show_work_units']) {
1597 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1598 $body .= $totals['units'];
1601 if ($report['show_cost']) {
1602 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1603 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1608 $body .= '</table>';
1612 // Print table header.
1613 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1615 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.date').'</td>';
1616 if ($canViewReports || $isClient)
1617 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.user').'</td>';
1618 if ($report['show_client'])
1619 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.client').'</td>';
1620 if ($report['show_project'])
1621 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.project').'</td>';
1622 if ($report['show_task'])
1623 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.task').'</td>';
1624 if ($report['show_custom_field_1'])
1625 $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
1626 if ($report['show_start'])
1627 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.start').'</td>';
1628 if ($report['show_end'])
1629 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.finish').'</td>';
1630 if ($report['show_duration'])
1631 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
1632 if ($report['show_work_units'])
1633 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
1634 if ($report['show_note'])
1635 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.note').'</td>';
1636 if ($report['show_cost'])
1637 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
1638 if ($report['show_paid'])
1639 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.paid').'</td>';
1640 if ($report['show_ip'])
1641 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.ip').'</td>';
1642 if ($report['show_invoice'])
1643 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.invoice').'</td>';
1646 // Initialize variables to print subtotals.
1647 if ($items && 'no_grouping' != $group_by) {
1648 $print_subtotals = true;
1650 $prev_grouped_by = '';
1651 $cur_grouped_by = '';
1653 // Initialize variables to alternate color of rows for different dates.
1656 $row_style = $rowItem;
1658 // Print report items.
1659 if (is_array($items)) {
1660 foreach ($items as $record) {
1661 $cur_date = $record['date'];
1662 // Print a subtotal row after a block of grouped items.
1663 if ($print_subtotals) {
1664 $cur_grouped_by = $record['grouped_by'];
1665 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
1666 $body .= '<tr style="'.$rowSubtotal.'">';
1667 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
1668 $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
1669 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1670 if ($report['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1671 if ($report['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1672 if ($report['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1673 if ($report['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1674 if ($report['show_start']) $body .= '<td></td>';
1675 if ($report['show_end']) $body .= '<td></td>';
1676 if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
1677 if ($report['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['units'].'</td>';
1678 if ($report['show_note']) $body .= '<td></td>';
1679 if ($report['show_cost']) {
1680 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1681 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
1684 if ($report['show_paid']) $body .= '<td></td>';
1685 if ($report['show_ip']) $body .= '<td></td>';
1686 if ($report['show_invoice']) $body .= '<td></td>';
1688 $body .= '<tr><td> </td></tr>';
1690 $first_pass = false;
1693 // Print a regular row.
1694 if ($cur_date != $prev_date)
1695 $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
1696 $body .= '<tr style="'.$row_style.'">';
1697 $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
1698 if ($canViewReports || $isClient)
1699 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
1700 if ($report['show_client'])
1701 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
1702 if ($report['show_project'])
1703 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
1704 if ($report['show_task'])
1705 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
1706 if ($report['show_custom_field_1'])
1707 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
1708 if ($report['show_start'])
1709 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
1710 if ($report['show_end'])
1711 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
1712 if ($report['show_duration'])
1713 $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
1714 if ($report['show_work_units'])
1715 $body .= '<td style="'.$cellRightAligned.'">'.$record['units'].'</td>';
1716 if ($report['show_note'])
1717 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
1718 if ($report['show_cost'])
1719 $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
1720 if ($report['show_paid']) {
1721 $body .= '<td style="'.$cellRightAligned.'">';
1722 $body .= $record['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
1725 if ($report['show_ip']) {
1726 $body .= '<td style="'.$cellRightAligned.'">';
1727 $body .= $record['modified'] ? $record['modified_ip'].' '.$record['modified'] : $record['created_ip'].' '.$record['created'];
1730 if ($report['show_invoice'])
1731 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
1734 $prev_date = $record['date'];
1735 if ($print_subtotals)
1736 $prev_grouped_by = $record['grouped_by'];
1740 // Print a terminating subtotal.
1741 if ($print_subtotals) {
1742 $body .= '<tr style="'.$rowSubtotal.'">';
1743 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
1744 $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
1745 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1746 if ($report['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1747 if ($report['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1748 if ($report['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1749 if ($report['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1750 if ($report['show_start']) $body .= '<td></td>';
1751 if ($report['show_end']) $body .= '<td></td>';
1752 if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
1753 if ($report['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['units'].'</td>';
1754 if ($report['show_note']) $body .= '<td></td>';
1755 if ($report['show_cost']) {
1756 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1757 $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
1760 if ($report['show_paid']) $body .= '<td></td>';
1761 if ($report['show_ip']) $body .= '<td></td>';
1762 if ($report['show_invoice']) $body .= '<td></td>';
1767 $body .= '<tr><td> </td></tr>';
1768 $body .= '<tr style="'.$rowSubtotal.'">';
1769 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
1770 if ($canViewReports || $isClient) $body .= '<td></td>';
1771 if ($report['show_client']) $body .= '<td></td>';
1772 if ($report['show_project']) $body .= '<td></td>';
1773 if ($report['show_task']) $body .= '<td></td>';
1774 if ($report['show_custom_field_1']) $body .= '<td></td>';
1775 if ($report['show_start']) $body .= '<td></td>';
1776 if ($report['show_end']) $body .= '<td></td>';
1777 if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
1778 if ($report['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['units'].'</td>';
1779 if ($report['show_note']) $body .= '<td></td>';
1780 if ($report['show_cost']) {
1781 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1782 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1785 if ($report['show_paid']) $body .= '<td></td>';
1786 if ($report['show_ip']) $body .= '<td></td>';
1787 if ($report['show_invoice']) $body .= '<td></td>';
1790 $body .= '</table>';
1794 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
1795 $body .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
1797 // Finish creating email body.
1798 $body .= '</body></html>';
1803 // sendFavReport - sends a favorite report to a specified email, called from cron.php
1804 static function sendFavReport($report, $subject, $email, $cc) {
1805 // We are called from cron.php, we have no $bean in session.
1806 // cron.php sets global $user and $i18n objects to match our favorite report user.
1810 // Prepare report body.
1811 $body = ttReportHelper::prepareFavReportBody($report);
1813 import('mail.Mailer');
1814 $mailer = new Mailer();
1815 $mailer->setCharSet(CHARSET);
1816 $mailer->setContentType('text/html');
1817 $mailer->setSender(SENDER);
1819 $mailer->setReceiverCC($cc);
1820 if (!empty($user->bcc_email))
1821 $mailer->setReceiverBCC($user->bcc_email);
1822 $mailer->setReceiver($email);
1823 $mailer->setMailMode(MAIL_MODE);
1824 if (empty($subject)) $subject = $report['name'];
1825 if (!$mailer->send($subject, $body))