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->team_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('chnote'))
281 array_push($fields, 'l.comment as note');
283 $includeCost = $bean->getAttribute('chcost');
285 if (MODE_TIME == $user->tracking_mode)
286 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.
288 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.
289 array_push($fields, "null as expense");
292 if ($canViewReports && $bean->getAttribute('chpaid'))
293 array_push($fields, 'l.paid as paid');
295 if ($canViewReports && $bean->getAttribute('chip')) {
296 array_push($fields, 'l.created as created');
297 array_push($fields, 'l.created_ip as created_ip');
298 array_push($fields, 'l.modified as modified');
299 array_push($fields, 'l.modified_ip as modified_ip');
302 // Add invoice name if it is selected.
303 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
304 array_push($fields, 'i.name as invoice');
306 // Prepare sql query part for left joins.
308 if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
309 $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
310 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
311 $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
312 if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
313 $left_joins .= " left join tt_users u on (u.id = l.user_id)";
314 if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
315 $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
316 if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
317 $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
319 if ($cf_1_type == CustomFields::TYPE_TEXT)
320 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)";
321 elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
322 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)".
323 " left join tt_custom_field_options cfo on (cfl.option_id = cfo.id)";
326 if ($includeCost && MODE_TIME != $user->tracking_mode)
327 $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
329 $where = ttReportHelper::getWhere($bean);
331 // Construct sql query for tt_log items.
332 $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
333 // If we don't have expense items (such as when the Expenses plugin is desabled), the above is all sql we need,
334 // with an exception of sorting part, that is added in the end.
336 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
337 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
339 $fields = array(); // An array of fields for database query.
340 array_push($fields, 'ei.id');
341 array_push($fields, '2 as type'); // Type 2 is for tt_expense_items entries.
342 array_push($fields, 'ei.date');
343 if($canViewReports || $isClient)
344 array_push($fields, 'u.name as user');
345 // Add client name if it is selected.
346 if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
347 array_push($fields, 'c.name as client');
348 // Add project name if it is selected.
349 if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
350 array_push($fields, 'p.name as project');
351 if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
352 array_push($fields, 'null'); // null for task name. We need to match column count for union.
353 if ($bean->getAttribute('chcf_1') || 'cf_1' == $group_by_option)
354 array_push($fields, 'null'); // null for cf_1.
355 if ($bean->getAttribute('chstart')) {
356 array_push($fields, 'null'); // null for unformatted_start.
357 array_push($fields, 'null'); // null for start.
359 if ($bean->getAttribute('chfinish'))
360 array_push($fields, 'null'); // null for finish.
361 if ($bean->getAttribute('chduration'))
362 array_push($fields, 'null'); // null for duration.
363 // Use the note field to print item name.
364 if ($bean->getAttribute('chnote'))
365 array_push($fields, 'ei.name as note');
366 array_push($fields, 'ei.cost as cost');
367 array_push($fields, 'ei.cost as expense');
369 if ($canViewReports && $bean->getAttribute('chpaid'))
370 array_push($fields, 'ei.paid as paid');
371 // Add IP address. NULL for now for expenses.
372 if ($canViewReports && $bean->getAttribute('chip')) {
373 array_push($fields, 'ei.created as created');
374 array_push($fields, 'ei.created_ip as created_ip');
375 array_push($fields, 'ei.modified as modified');
376 array_push($fields, 'ei.modified_ip as modified_ip');
379 // Add invoice name if it is selected.
380 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
381 array_push($fields, 'i.name as invoice');
383 // Prepare sql query part for left joins.
385 if ($canViewReports || $isClient)
386 $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
387 if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
388 $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
389 if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
390 $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
391 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
392 $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
394 $where = ttReportHelper::getExpenseWhere($bean);
396 // Construct sql query for expense items.
397 $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
399 // Construct a union.
400 $sql = "($sql) union all ($sql_for_expense_items)";
403 // Determine sort part.
404 $sort_part = ' order by ';
405 if ('no_grouping' == $group_by_option || 'date' == $group_by_option)
406 $sort_part .= 'date';
408 $sort_part .= $group_by_option.', date';
409 if (($canViewReports || $isClient) && is_array($bean->getAttribute('users')) && 'user' != $group_by_option)
410 $sort_part .= ', user, type';
411 if ($bean->getAttribute('chstart'))
412 $sort_part .= ', unformatted_start';
413 $sort_part .= ', id';
416 // By now we are ready with sql.
418 // Obtain items for report.
419 $res = $mdb2->query($sql);
420 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
422 while ($val = $res->fetchRow()) {
423 if ($convertTo12Hour) {
424 if($val['start'] != '')
425 $val['start'] = ttTimeHelper::to12HourFormat($val['start']);
426 if($val['finish'] != '')
427 $val['finish'] = ttTimeHelper::to12HourFormat($val['finish']);
429 if (isset($val['cost'])) {
430 if ('.' != $user->decimal_mark)
431 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
433 if (isset($val['expense'])) {
434 if ('.' != $user->decimal_mark)
435 $val['expense'] = str_replace('.', $user->decimal_mark, $val['expense']);
437 if ('no_grouping' != $group_by_option) {
438 $val['grouped_by'] = $val[$group_by_option];
439 if ('date' == $group_by_option) {
440 // This is needed to get the date in user date format.
441 $o_date = new DateAndTime(DB_DATEFORMAT, $val['grouped_by']);
442 $val['grouped_by'] = $o_date->toString($user->date_format);
447 // This is needed to get the date in user date format.
448 $o_date = new DateAndTime(DB_DATEFORMAT, $val['date']);
449 $val['date'] = $o_date->toString($user->date_format);
453 $report_items[] = $row;
456 return $report_items;
459 // putInSession stores tt_log and tt_expense_items ids from a report in user session
460 // as 2 comma-separated lists.
461 static function putInSession($report_items) {
462 unset($_SESSION['report_item_ids']);
463 unset($_SESSION['report_item_expense_ids']);
465 // Iterate through records and build 2 comma-separated lists.
466 foreach($report_items as $item) {
467 if ($item['type'] == 1)
468 $report_item_ids .= ','.$item['id'];
469 else if ($item['type'] == 2)
470 $report_item_expense_ids .= ','.$item['id'];
472 $report_item_ids = trim($report_item_ids, ',');
473 $report_item_expense_ids = trim($report_item_expense_ids, ',');
475 // The lists are reqdy. Put them in session.
476 if ($report_item_ids) $_SESSION['report_item_ids'] = $report_item_ids;
477 if ($report_item_expense_ids) $_SESSION['report_item_expense_ids'] = $report_item_expense_ids;
480 // getFromSession obtains tt_log and tt_expense_items ids stored in user session.
481 static function getFromSession() {
483 $report_item_ids = $_SESSION['report_item_ids'];
484 if ($report_item_ids)
485 $items['report_item_ids'] = explode(',', $report_item_ids);
486 $report_item_expense_ids = $_SESSION['report_item_expense_ids'];
487 if ($report_item_expense_ids)
488 $items['report_item_expense_ids'] = explode(',', $report_item_expense_ids);
492 // getFavItems retrieves all items associated with a favorite report.
493 // It combines tt_log and tt_expense_items in one array for presentation in one table using mysql union all.
494 // Expense items use the "note" field for item name.
495 static function getFavItems($report) {
497 $mdb2 = getConnection();
499 // Determine these once as they are used in multiple places in this function.
500 $canViewReports = $user->can('view_reports');
501 $isClient = $user->isClient();
503 $group_by_option = $report['group_by'];
504 $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($report['show_start'] || $report['show_end']);
506 // Prepare a query for time items in tt_log table.
507 $fields = array(); // An array of fields for database query.
508 array_push($fields, 'l.id as id');
509 array_push($fields, '1 as type'); // Type 1 is for tt_log entries.
510 array_push($fields, 'l.date as date');
511 if($canViewReports || $isClient)
512 array_push($fields, 'u.name as user');
513 // Add client name if it is selected.
514 if ($report['show_client'] || 'client' == $group_by_option)
515 array_push($fields, 'c.name as client');
516 // Add project name if it is selected.
517 if ($report['show_project'] || 'project' == $group_by_option)
518 array_push($fields, 'p.name as project');
519 // Add task name if it is selected.
520 if ($report['show_task'] || 'task' == $group_by_option)
521 array_push($fields, 't.name as task');
523 $include_cf_1 = $report['show_custom_field_1'] || 'cf_1' == $group_by_option;
525 $custom_fields = new CustomFields($user->team_id);
526 $cf_1_type = $custom_fields->fields[0]['type'];
527 if ($cf_1_type == CustomFields::TYPE_TEXT) {
528 array_push($fields, 'cfl.value as cf_1');
529 } elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
530 array_push($fields, 'cfo.value as cf_1');
534 if ($report['show_start']) {
535 array_push($fields, "l.start as unformatted_start");
536 array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start");
539 if ($report['show_end'])
540 array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish");
542 if ($report['show_duration'])
543 array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
545 if ($report['show_note'])
546 array_push($fields, 'l.comment as note');
548 $includeCost = $report['show_cost'];
550 if (MODE_TIME == $user->tracking_mode)
551 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.
553 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.
554 array_push($fields, "null as expense");
556 // Add invoice name if it is selected.
557 if (($canViewReports || $isClient) && $report['show_invoice'])
558 array_push($fields, 'i.name as invoice');
560 // Prepare sql query part for left joins.
562 if ($report['show_client'] || 'client' == $group_by_option)
563 $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
564 if (($canViewReports || $isClient) && $report['show_invoice'])
565 $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
566 if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
567 $left_joins .= " left join tt_users u on (u.id = l.user_id)";
568 if ($report['show_project'] || 'project' == $group_by_option)
569 $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
570 if ($report['show_task'] || 'task' == $group_by_option)
571 $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
573 if ($cf_1_type == CustomFields::TYPE_TEXT)
574 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)";
575 elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
576 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)".
577 " left join tt_custom_field_options cfo on (cfl.option_id = cfo.id)";
580 if ($includeCost && MODE_TIME != $user->tracking_mode)
581 $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
583 $where = ttReportHelper::getFavWhere($report);
585 // Construct sql query for tt_log items.
586 $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
587 // If we don't have expense items (such as when the Expenses plugin is desabled), the above is all sql we need,
588 // with an exception of sorting part, that is added in the end.
590 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
591 if ($report['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
593 $fields = array(); // An array of fields for database query.
594 array_push($fields, 'ei.id');
595 array_push($fields, '2 as type'); // Type 2 is for tt_expense_items entries.
596 array_push($fields, 'ei.date');
597 if($canViewReports || $isClient)
598 array_push($fields, 'u.name as user');
599 // Add client name if it is selected.
600 if ($report['show_client'] || 'client' == $group_by_option)
601 array_push($fields, 'c.name as client');
602 // Add project name if it is selected.
603 if ($report['show_project'] || 'project' == $group_by_option)
604 array_push($fields, 'p.name as project');
605 if ($report['show_task'] || 'task' == $group_by_option)
606 array_push($fields, 'null'); // null for task name. We need to match column count for union.
607 if ($report['show_custom_field_1'] || 'cf_1' == $group_by_option)
608 array_push($fields, 'null'); // null for cf_1.
609 if ($report['show_start']) {
610 array_push($fields, 'null'); // null for unformatted_start.
611 array_push($fields, 'null'); // null for start.
613 if ($report['show_end'])
614 array_push($fields, 'null'); // null for finish.
615 if ($report['show_duration'])
616 array_push($fields, 'null'); // null for duration.
617 // Use the note field to print item name.
618 if ($report['show_note'])
619 array_push($fields, 'ei.name as note');
620 array_push($fields, 'ei.cost as cost');
621 array_push($fields, 'ei.cost as expense');
622 // Add invoice name if it is selected.
623 if (($canViewReports || $isClient) && $report['show_invoice'])
624 array_push($fields, 'i.name as invoice');
626 // Prepare sql query part for left joins.
628 if ($canViewReports || $isClient)
629 $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
630 if ($report['show_client'] || 'client' == $group_by_option)
631 $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
632 if ($report['show_project'] || 'project' == $group_by_option)
633 $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
634 if (($canViewReports || $isClient) && $report['show_invoice'])
635 $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
637 $where = ttReportHelper::getFavExpenseWhere($report);
639 // Construct sql query for expense items.
640 $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
642 // Construct a union.
643 $sql = "($sql) union all ($sql_for_expense_items)";
646 // Determine sort part.
647 $sort_part = ' order by ';
648 if ($group_by_option == null || 'no_grouping' == $group_by_option || 'date' == $group_by_option) // TODO: fix DB for NULL values in group_by field.
649 $sort_part .= 'date';
651 $sort_part .= $group_by_option.', date';
652 if (($canViewReports || $isClient) /*&& is_array($bean->getAttribute('users'))*/ && 'user' != $group_by_option)
653 $sort_part .= ', user, type';
654 if ($report['show_start'])
655 $sort_part .= ', unformatted_start';
656 $sort_part .= ', id';
659 // By now we are ready with sql.
661 // Obtain items for report.
662 $res = $mdb2->query($sql);
663 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
665 while ($val = $res->fetchRow()) {
666 if ($convertTo12Hour) {
667 if($val['start'] != '')
668 $val['start'] = ttTimeHelper::to12HourFormat($val['start']);
669 if($val['finish'] != '')
670 $val['finish'] = ttTimeHelper::to12HourFormat($val['finish']);
672 if (isset($val['cost'])) {
673 if ('.' != $user->decimal_mark)
674 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
676 if (isset($val['expense'])) {
677 if ('.' != $user->decimal_mark)
678 $val['expense'] = str_replace('.', $user->decimal_mark, $val['expense']);
680 if ('no_grouping' != $group_by_option) {
681 $val['grouped_by'] = $val[$group_by_option];
682 if ('date' == $group_by_option) {
683 // This is needed to get the date in user date format.
684 $o_date = new DateAndTime(DB_DATEFORMAT, $val['grouped_by']);
685 $val['grouped_by'] = $o_date->toString($user->date_format);
690 // This is needed to get the date in user date format.
691 $o_date = new DateAndTime(DB_DATEFORMAT, $val['date']);
692 $val['date'] = $o_date->toString($user->date_format);
696 $report_items[] = $row;
699 return $report_items;
702 // getSubtotals calculates report items subtotals when a report is grouped by.
703 // Without expenses, it's a simple select with group by.
704 // With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
705 static function getSubtotals($bean) {
708 $group_by_option = $bean->getAttribute('group_by');
709 if ('no_grouping' == $group_by_option) return null;
711 $mdb2 = getConnection();
713 // Start with sql to obtain subtotals for time items. This simple sql will be used when we have no expenses.
715 // Determine group by field and a required join.
716 switch ($group_by_option) {
718 $group_field = 'l.date';
722 $group_field = 'u.name';
723 $group_join = 'left join tt_users u on (l.user_id = u.id) ';
726 $group_field = 'c.name';
727 $group_join = 'left join tt_clients c on (l.client_id = c.id) ';
730 $group_field = 'p.name';
731 $group_join = 'left join tt_projects p on (l.project_id = p.id) ';
734 $group_field = 't.name';
735 $group_join = 'left join tt_tasks t on (l.task_id = t.id) ';
738 $group_field = 'cfo.value';
739 $custom_fields = new CustomFields($user->team_id);
740 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
741 $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) ';
742 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
743 $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) ';
747 $where = ttReportHelper::getWhere($bean);
748 if ($bean->getAttribute('chcost')) {
749 if (MODE_TIME == $user->tracking_mode) {
750 if ($group_by_option != 'user')
751 $left_join = 'left join tt_users u on (l.user_id = u.id)';
752 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
753 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
754 null as expenses from tt_log l
755 $group_join $left_join $where group by $group_field";
757 // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
758 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
759 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
760 null as expenses from tt_log l
762 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";
765 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, null as expenses from tt_log l
766 $group_join $where group by $group_field";
768 // By now we have sql for time items.
770 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
771 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
773 // Determine group by field and a required join.
775 $group_field = 'null';
776 switch ($group_by_option) {
778 $group_field = 'ei.date';
782 $group_field = 'u.name';
783 $group_join = 'left join tt_users u on (ei.user_id = u.id) ';
786 $group_field = 'c.name';
787 $group_join = 'left join tt_clients c on (ei.client_id = c.id) ';
790 $group_field = 'p.name';
791 $group_join = 'left join tt_projects p on (ei.project_id = p.id) ';
795 $where = ttReportHelper::getExpenseWhere($bean);
796 $sql_for_expenses = "select $group_field as group_field, null as time, sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei
798 // Add a "group by" clause if we are grouping.
799 if ('null' != $group_field) $sql_for_expenses .= " group by $group_field";
801 // Create a combined query.
802 $sql = "select group_field, sum(time) as time, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by group_field";
806 $res = $mdb2->query($sql);
807 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
809 while ($val = $res->fetchRow()) {
810 if ('date' == $group_by_option) {
811 // This is needed to get the date in user date format.
812 $o_date = new DateAndTime(DB_DATEFORMAT, $val['group_field']);
813 $val['group_field'] = $o_date->toString($user->date_format);
816 $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
817 if ($bean->getAttribute('chcost')) {
818 if ('.' != $user->decimal_mark) {
819 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
820 $val['expenses'] = str_replace('.', $user->decimal_mark, $val['expenses']);
822 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time,'cost'=>$val['cost'],'expenses'=>$val['expenses']);
824 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time);
830 // getFavSubtotals calculates report items subtotals when a favorite report is grouped by.
831 // Without expenses, it's a simple select with group by.
832 // With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
833 static function getFavSubtotals($report) {
836 $group_by_option = $report['group_by'];
837 if ('no_grouping' == $group_by_option) return null;
839 $mdb2 = getConnection();
841 // Start with sql to obtain subtotals for time items. This simple sql will be used when we have no expenses.
843 // Determine group by field and a required join.
844 switch ($group_by_option) {
846 $group_field = 'l.date';
850 $group_field = 'u.name';
851 $group_join = 'left join tt_users u on (l.user_id = u.id) ';
854 $group_field = 'c.name';
855 $group_join = 'left join tt_clients c on (l.client_id = c.id) ';
858 $group_field = 'p.name';
859 $group_join = 'left join tt_projects p on (l.project_id = p.id) ';
862 $group_field = 't.name';
863 $group_join = 'left join tt_tasks t on (l.task_id = t.id) ';
866 $group_field = 'cfo.value';
867 $custom_fields = new CustomFields($user->team_id);
868 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
869 $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) ';
870 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
871 $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) ';
875 $where = ttReportHelper::getFavWhere($report);
876 if ($report['show_cost']) {
877 if (MODE_TIME == $user->tracking_mode) {
878 if ($group_by_option != 'user')
879 $left_join = 'left join tt_users u on (l.user_id = u.id)';
880 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
881 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
882 null as expenses from tt_log l
883 $group_join $left_join $where group by $group_field";
885 // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
886 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
887 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
888 null as expenses from tt_log l
890 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";
893 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, null as expenses from tt_log l
894 $group_join $where group by $group_field";
896 // By now we have sql for time items.
898 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
899 if ($report['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
901 // Determine group by field and a required join.
903 $group_field = 'null';
904 switch ($group_by_option) {
906 $group_field = 'ei.date';
910 $group_field = 'u.name';
911 $group_join = 'left join tt_users u on (ei.user_id = u.id) ';
914 $group_field = 'c.name';
915 $group_join = 'left join tt_clients c on (ei.client_id = c.id) ';
918 $group_field = 'p.name';
919 $group_join = 'left join tt_projects p on (ei.project_id = p.id) ';
923 $where = ttReportHelper::getFavExpenseWhere($report);
924 $sql_for_expenses = "select $group_field as group_field, null as time, sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei
926 // Add a "group by" clause if we are grouping.
927 if ('null' != $group_field) $sql_for_expenses .= " group by $group_field";
929 // Create a combined query.
930 $sql = "select group_field, sum(time) as time, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by group_field";
934 $res = $mdb2->query($sql);
935 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
937 while ($val = $res->fetchRow()) {
938 if ('date' == $group_by_option) {
939 // This is needed to get the date in user date format.
940 $o_date = new DateAndTime(DB_DATEFORMAT, $val['group_field']);
941 $val['group_field'] = $o_date->toString($user->date_format);
944 $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
945 if ($report['show_cost']) {
946 if ('.' != $user->decimal_mark) {
947 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
948 $val['expenses'] = str_replace('.', $user->decimal_mark, $val['expenses']);
950 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time,'cost'=>$val['cost'],'expenses'=>$val['expenses']);
952 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time);
958 // getTotals calculates total hours and cost for all report items.
959 static function getTotals($bean)
963 $mdb2 = getConnection();
965 $where = ttReportHelper::getWhere($bean);
967 // Start with a query for time items.
968 if ($bean->getAttribute('chcost')) {
969 if (MODE_TIME == $user->tracking_mode) {
970 $sql = "select sum(time_to_sec(l.duration)) as time,
971 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
974 left join tt_users u on (l.user_id = u.id) $where";
976 $sql = "select sum(time_to_sec(l.duration)) as time,
977 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
980 left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where";
983 $sql = "select sum(time_to_sec(l.duration)) as time, null as cost, null as expenses from tt_log l $where";
985 // If we have expenses, query becomes a bit more complex.
986 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) {
987 $where = ttReportHelper::getExpenseWhere($bean);
988 $sql_for_expenses = "select null as time, sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
989 // Create a combined query.
990 $sql = "select sum(time) as time, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
994 $res = $mdb2->query($sql);
995 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
997 $val = $res->fetchRow();
998 $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
999 if ($bean->getAttribute('chcost')) {
1000 $total_cost = $val['cost'];
1001 if (!$total_cost) $total_cost = '0.00';
1002 if ('.' != $user->decimal_mark)
1003 $total_cost = str_replace('.', $user->decimal_mark, $total_cost);
1004 $total_expenses = $val['expenses'];
1005 if (!$total_expenses) $total_expenses = '0.00';
1006 if ('.' != $user->decimal_mark)
1007 $total_expenses = str_replace('.', $user->decimal_mark, $total_expenses);
1010 if ($bean->getAttribute('period'))
1011 $period = new Period($bean->getAttribute('period'), new DateAndTime($user->date_format));
1013 $period = new Period();
1015 new DateAndTime($user->date_format, $bean->getAttribute('start_date')),
1016 new DateAndTime($user->date_format, $bean->getAttribute('end_date')));
1019 $totals['start_date'] = $period->getStartDate();
1020 $totals['end_date'] = $period->getEndDate();
1021 $totals['time'] = $total_time;
1022 $totals['cost'] = $total_cost;
1023 $totals['expenses'] = $total_expenses;
1028 // getFavTotals calculates total hours and cost for all favorite report items.
1029 static function getFavTotals($report)
1033 $mdb2 = getConnection();
1035 $where = ttReportHelper::getFavWhere($report);
1037 // Start with a query for time items.
1038 if ($report['show_cost']) {
1039 if (MODE_TIME == $user->tracking_mode) {
1040 $sql = "select sum(time_to_sec(l.duration)) as time,
1041 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
1044 left join tt_users u on (l.user_id = u.id) $where";
1046 $sql = "select sum(time_to_sec(l.duration)) as time,
1047 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
1050 left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where";
1053 $sql = "select sum(time_to_sec(l.duration)) as time, null as cost, null as expenses from tt_log l $where";
1055 // If we have expenses, query becomes a bit more complex.
1056 if ($report['show_cost'] && $user->isPluginEnabled('ex')) {
1057 $where = ttReportHelper::getFavExpenseWhere($report);
1058 $sql_for_expenses = "select null as time, sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
1059 // Create a combined query.
1060 $sql = "select sum(time) as time, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
1064 $res = $mdb2->query($sql);
1065 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
1067 $val = $res->fetchRow();
1068 $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
1069 if ($report['show_cost']) {
1070 $total_cost = $val['cost'];
1071 if (!$total_cost) $total_cost = '0.00';
1072 if ('.' != $user->decimal_mark)
1073 $total_cost = str_replace('.', $user->decimal_mark, $total_cost);
1074 $total_expenses = $val['expenses'];
1075 if (!$total_expenses) $total_expenses = '0.00';
1076 if ('.' != $user->decimal_mark)
1077 $total_expenses = str_replace('.', $user->decimal_mark, $total_expenses);
1080 if ($report['period'])
1081 $period = new Period($report['period'], new DateAndTime($user->date_format));
1083 $period = new Period();
1085 new DateAndTime($user->date_format, $report['period_start']),
1086 new DateAndTime($user->date_format, $report['period_end']));
1089 $totals['start_date'] = $period->getStartDate();
1090 $totals['end_date'] = $period->getEndDate();
1091 $totals['time'] = $total_time;
1092 $totals['cost'] = $total_cost;
1093 $totals['expenses'] = $total_expenses;
1098 // The assignToInvoice assigns a set of records to a specific invoice.
1099 static function assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids)
1101 $mdb2 = getConnection();
1102 if ($time_log_ids) {
1103 $sql = "update tt_log set invoice_id = ".$mdb2->quote($invoice_id).
1104 " where id in(".join(', ', $time_log_ids).")";
1105 $affected = $mdb2->exec($sql);
1106 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1108 if ($expense_item_ids) {
1109 $sql = "update tt_expense_items set invoice_id = ".$mdb2->quote($invoice_id).
1110 " where id in(".join(', ', $expense_item_ids).")";
1111 $affected = $mdb2->exec($sql);
1112 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1116 // The markPaid marks a set of records as either paid or unpaid.
1117 static function markPaid($time_log_ids, $expense_item_ids, $paid = true)
1119 $mdb2 = getConnection();
1120 $paid_val = (int) $paid;
1121 if ($time_log_ids) {
1122 $sql = "update tt_log set paid = $paid_val where id in(".join(', ', $time_log_ids).")";
1123 $affected = $mdb2->exec($sql);
1124 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1126 if ($expense_item_ids) {
1127 $sql = "update tt_expense_items set paid = $paid_val where id in(".join(', ', $expense_item_ids).")";
1128 $affected = $mdb2->exec($sql);
1129 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1133 // prepareReportBody - prepares an email body for report.
1134 static function prepareReportBody($bean, $comment)
1139 // Determine these once as they are used in multiple places in this function.
1140 $canViewReports = $user->can('view_reports');
1141 $isClient = $user->isClient();
1143 $items = ttReportHelper::getItems($bean);
1144 $group_by = $bean->getAttribute('group_by');
1145 if ($group_by && 'no_grouping' != $group_by)
1146 $subtotals = ttReportHelper::getSubtotals($bean);
1147 $totals = ttReportHelper::getTotals($bean);
1149 // Use custom fields plugin if it is enabled.
1150 if ($user->isPluginEnabled('cf'))
1151 $custom_fields = new CustomFields($user->team_id);
1153 // Define some styles to use in email.
1154 $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
1155 $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
1156 $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
1157 $rowItem = 'background-color: #ffffff;';
1158 $rowItemAlt = 'background-color: #f5f5f5;';
1159 $rowSubtotal = 'background-color: #e0e0e0;';
1160 $cellLeftAligned = 'text-align: left; vertical-align: top;';
1161 $cellRightAligned = 'text-align: right; vertical-align: top;';
1162 $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
1163 $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
1165 // Start creating email body.
1167 $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
1171 $body .= '<p style="'.$style_title.'">'.$i18n->getKey('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
1174 if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>';
1176 if ($bean->getAttribute('chtotalsonly')) {
1177 // Totals only report. Output subtotals.
1179 // Determine group_by header.
1180 if ('cf_1' == $group_by)
1181 $group_by_header = htmlspecialchars($custom_fields->fields[0]['label']);
1183 $key = 'label.'.$group_by;
1184 $group_by_header = $i18n->getKey($key);
1187 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1189 $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
1190 if ($bean->getAttribute('chduration'))
1191 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.duration').'</td>';
1192 if ($bean->getAttribute('chcost'))
1193 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.cost').'</td>';
1195 foreach($subtotals as $subtotal) {
1196 $body .= '<tr style="'.$rowSubtotal.'">';
1197 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : ' ').'</td>';
1198 if ($bean->getAttribute('chduration')) {
1199 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1200 if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
1203 if ($bean->getAttribute('chcost')) {
1204 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1205 $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
1212 $body .= '<tr><td> </td></tr>';
1213 $body .= '<tr style="'.$rowSubtotal.'">';
1214 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.total').'</td>';
1215 if ($bean->getAttribute('chduration')) {
1216 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1217 if ($totals['time'] <> '0:00') $body .= $totals['time'];
1220 if ($bean->getAttribute('chcost')) {
1221 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1222 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1227 $body .= '</table>';
1231 // Print table header.
1232 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1234 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.date').'</td>';
1235 if ($canViewReports || $isClient)
1236 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.user').'</td>';
1237 if ($bean->getAttribute('chclient'))
1238 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.client').'</td>';
1239 if ($bean->getAttribute('chproject'))
1240 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.project').'</td>';
1241 if ($bean->getAttribute('chtask'))
1242 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.task').'</td>';
1243 if ($bean->getAttribute('chcf_1'))
1244 $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
1245 if ($bean->getAttribute('chstart'))
1246 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.start').'</td>';
1247 if ($bean->getAttribute('chfinish'))
1248 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.finish').'</td>';
1249 if ($bean->getAttribute('chduration'))
1250 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.duration').'</td>';
1251 if ($bean->getAttribute('chnote'))
1252 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.note').'</td>';
1253 if ($bean->getAttribute('chcost'))
1254 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.cost').'</td>';
1255 if ($bean->getAttribute('chpaid'))
1256 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.paid').'</td>';
1257 if ($bean->getAttribute('chip'))
1258 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.ip').'</td>';
1259 if ($bean->getAttribute('chinvoice'))
1260 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.invoice').'</td>';
1263 // Initialize variables to print subtotals.
1264 if ($items && 'no_grouping' != $group_by) {
1265 $print_subtotals = true;
1267 $prev_grouped_by = '';
1268 $cur_grouped_by = '';
1270 // Initialize variables to alternate color of rows for different dates.
1273 $row_style = $rowItem;
1275 // Print report items.
1276 if (is_array($items)) {
1277 foreach ($items as $record) {
1278 $cur_date = $record['date'];
1279 // Print a subtotal row after a block of grouped items.
1280 if ($print_subtotals) {
1281 $cur_grouped_by = $record['grouped_by'];
1282 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
1283 $body .= '<tr style="'.$rowSubtotal.'">';
1284 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.subtotal').'</td>';
1285 $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
1286 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1287 if ($bean->getAttribute('chclient')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1288 if ($bean->getAttribute('chproject')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1289 if ($bean->getAttribute('chtask')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1290 if ($bean->getAttribute('chcf_1')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1291 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1292 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1293 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
1294 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1295 if ($bean->getAttribute('chcost')) {
1296 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1297 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
1300 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1301 if ($bean->getAttribute('chip')) $body .= '<td></td>';
1302 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1304 $body .= '<tr><td> </td></tr>';
1306 $first_pass = false;
1309 // Print a regular row.
1310 if ($cur_date != $prev_date)
1311 $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
1312 $body .= '<tr style="'.$row_style.'">';
1313 $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
1314 if ($canViewReports || $isClient)
1315 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
1316 if ($bean->getAttribute('chclient'))
1317 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
1318 if ($bean->getAttribute('chproject'))
1319 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
1320 if ($bean->getAttribute('chtask'))
1321 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
1322 if ($bean->getAttribute('chcf_1'))
1323 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
1324 if ($bean->getAttribute('chstart'))
1325 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
1326 if ($bean->getAttribute('chfinish'))
1327 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
1328 if ($bean->getAttribute('chduration'))
1329 $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
1330 if ($bean->getAttribute('chnote'))
1331 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
1332 if ($bean->getAttribute('chcost'))
1333 $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
1334 if ($bean->getAttribute('chpaid')) {
1335 $body .= '<td style="'.$cellRightAligned.'">';
1336 $body .= $record['paid'] == 1 ? $i18n->getKey('label.yes') : $i18n->getKey('label.no');
1339 if ($bean->getAttribute('chip')) {
1340 $body .= '<td style="'.$cellRightAligned.'">';
1341 $body .= $record['modified'] ? $record['modified_ip'].' '.$record['modified'] : $record['created_ip'].' '.$record['created'];
1344 if ($bean->getAttribute('chinvoice'))
1345 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
1348 $prev_date = $record['date'];
1349 if ($print_subtotals)
1350 $prev_grouped_by = $record['grouped_by'];
1354 // Print a terminating subtotal.
1355 if ($print_subtotals) {
1356 $body .= '<tr style="'.$rowSubtotal.'">';
1357 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.subtotal').'</td>';
1358 $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
1359 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1360 if ($bean->getAttribute('chclient')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1361 if ($bean->getAttribute('chproject')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1362 if ($bean->getAttribute('chtask')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1363 if ($bean->getAttribute('chcf_1')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1364 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1365 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1366 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
1367 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1368 if ($bean->getAttribute('chcost')) {
1369 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1370 $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
1373 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1374 if ($bean->getAttribute('chip')) $body .= '<td></td>';
1375 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1380 $body .= '<tr><td> </td></tr>';
1381 $body .= '<tr style="'.$rowSubtotal.'">';
1382 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.total').'</td>';
1383 if ($canViewReports || $isClient) $body .= '<td></td>';
1384 if ($bean->getAttribute('chclient')) $body .= '<td></td>';
1385 if ($bean->getAttribute('chproject')) $body .= '<td></td>';
1386 if ($bean->getAttribute('chtask')) $body .= '<td></td>';
1387 if ($bean->getAttribute('chcf_1')) $body .= '<td></td>';
1388 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1389 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1390 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
1391 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1392 if ($bean->getAttribute('chcost')) {
1393 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1394 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1397 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1398 if ($bean->getAttribute('chip')) $body .= '<td></td>';
1399 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1402 $body .= '</table>';
1406 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
1407 $body .= '<p style="text-align: center;">'.$i18n->getKey('form.mail.footer').'</p>';
1409 // Finish creating email body.
1410 $body .= '</body></html>';
1415 // checkFavReportCondition - checks whether it is okay to send fav report.
1416 static function checkFavReportCondition($report, $condition)
1418 $items = ttReportHelper::getFavItems($report);
1420 $condition = str_replace('count', '', $condition);
1421 $count_required = (int) trim(str_replace('>', '', $condition));
1423 if (count($items) > $count_required)
1424 return true; // Condition ok.
1429 // prepareFavReportBody - prepares an email body for a favorite report.
1430 static function prepareFavReportBody($report)
1435 // Determine these once as they are used in multiple places in this function.
1436 $canViewReports = $user->can('view_reports');
1437 $isClient = $user->isClient();
1439 $items = ttReportHelper::getFavItems($report);
1440 $group_by = $report['group_by'];
1441 if ($group_by && 'no_grouping' != $group_by)
1442 $subtotals = ttReportHelper::getFavSubtotals($report);
1443 $totals = ttReportHelper::getFavTotals($report);
1445 // Use custom fields plugin if it is enabled.
1446 if ($user->isPluginEnabled('cf'))
1447 $custom_fields = new CustomFields($user->team_id);
1449 // Define some styles to use in email.
1450 $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
1451 $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
1452 $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
1453 $rowItem = 'background-color: #ffffff;';
1454 $rowItemAlt = 'background-color: #f5f5f5;';
1455 $rowSubtotal = 'background-color: #e0e0e0;';
1456 $cellLeftAligned = 'text-align: left; vertical-align: top;';
1457 $cellRightAligned = 'text-align: right; vertical-align: top;';
1458 $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
1459 $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
1461 // Start creating email body.
1463 $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
1467 $body .= '<p style="'.$style_title.'">'.$i18n->getKey('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
1470 // if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>'; // No comment for fav. reports.
1472 if ($report['show_totals_only']) {
1473 // Totals only report. Output subtotals.
1475 // Determine group_by header.
1476 if ('cf_1' == $group_by)
1477 $group_by_header = htmlspecialchars($custom_fields->fields[0]['label']);
1479 $key = 'label.'.$group_by;
1480 $group_by_header = $i18n->getKey($key);
1483 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1485 $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
1486 if ($report['show_duration'])
1487 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.duration').'</td>';
1488 if ($report['show_cost'])
1489 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.cost').'</td>';
1491 foreach($subtotals as $subtotal) {
1492 $body .= '<tr style="'.$rowSubtotal.'">';
1493 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : ' ').'</td>';
1494 if ($report['show_duration']) {
1495 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1496 if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
1499 if ($report['show_cost']) {
1500 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1501 $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
1508 $body .= '<tr><td> </td></tr>';
1509 $body .= '<tr style="'.$rowSubtotal.'">';
1510 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.total').'</td>';
1511 if ($report['show_duration']) {
1512 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1513 if ($totals['time'] <> '0:00') $body .= $totals['time'];
1516 if ($report['show_cost']) {
1517 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1518 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1523 $body .= '</table>';
1527 // Print table header.
1528 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1530 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.date').'</td>';
1531 if ($canViewReports || $isClient)
1532 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.user').'</td>';
1533 if ($report['show_client'])
1534 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.client').'</td>';
1535 if ($report['show_project'])
1536 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.project').'</td>';
1537 if ($report['show_task'])
1538 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.task').'</td>';
1539 if ($report['show_custom_field_1'])
1540 $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
1541 if ($report['show_start'])
1542 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.start').'</td>';
1543 if ($report['show_end'])
1544 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.finish').'</td>';
1545 if ($report['show_duration'])
1546 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.duration').'</td>';
1547 if ($report['show_note'])
1548 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.note').'</td>';
1549 if ($report['show_cost'])
1550 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.cost').'</td>';
1551 if ($report['show_paid'])
1552 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.paid').'</td>';
1553 if ($report['show_invoice'])
1554 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.invoice').'</td>';
1557 // Initialize variables to print subtotals.
1558 if ($items && 'no_grouping' != $group_by) {
1559 $print_subtotals = true;
1561 $prev_grouped_by = '';
1562 $cur_grouped_by = '';
1564 // Initialize variables to alternate color of rows for different dates.
1567 $row_style = $rowItem;
1569 // Print report items.
1570 if (is_array($items)) {
1571 foreach ($items as $record) {
1572 $cur_date = $record['date'];
1573 // Print a subtotal row after a block of grouped items.
1574 if ($print_subtotals) {
1575 $cur_grouped_by = $record['grouped_by'];
1576 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
1577 $body .= '<tr style="'.$rowSubtotal.'">';
1578 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.subtotal').'</td>';
1579 $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
1580 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1581 if ($report['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1582 if ($report['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1583 if ($report['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1584 if ($report['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1585 if ($report['show_start']) $body .= '<td></td>';
1586 if ($report['show_end']) $body .= '<td></td>';
1587 if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
1588 if ($report['show_note']) $body .= '<td></td>';
1589 if ($report['show_cost']) {
1590 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1591 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
1594 if ($report['show_paid']) $body .= '<td></td>';
1595 if ($report['show_invoice']) $body .= '<td></td>';
1597 $body .= '<tr><td> </td></tr>';
1599 $first_pass = false;
1602 // Print a regular row.
1603 if ($cur_date != $prev_date)
1604 $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
1605 $body .= '<tr style="'.$row_style.'">';
1606 $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
1607 if ($canViewReports || $isClient)
1608 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
1609 if ($report['show_client'])
1610 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
1611 if ($report['show_project'])
1612 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
1613 if ($report['show_task'])
1614 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
1615 if ($report['show_custom_field_1'])
1616 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
1617 if ($report['show_start'])
1618 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
1619 if ($report['show_end'])
1620 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
1621 if ($report['show_duration'])
1622 $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
1623 if ($report['show_note'])
1624 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
1625 if ($report['show_cost'])
1626 $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
1627 if ($report['show_paid']) {
1628 $body .= '<td style="'.$cellRightAligned.'">';
1629 $body .= $record['paid'] == 1 ? $i18n->getKey('label.yes') : $i18n->getKey('label.no');
1632 if ($report['show_invoice'])
1633 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
1636 $prev_date = $record['date'];
1637 if ($print_subtotals)
1638 $prev_grouped_by = $record['grouped_by'];
1642 // Print a terminating subtotal.
1643 if ($print_subtotals) {
1644 $body .= '<tr style="'.$rowSubtotal.'">';
1645 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.subtotal').'</td>';
1646 $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
1647 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1648 if ($report['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1649 if ($report['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1650 if ($report['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1651 if ($report['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1652 if ($report['show_start']) $body .= '<td></td>';
1653 if ($report['show_end']) $body .= '<td></td>';
1654 if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
1655 if ($report['show_note']) $body .= '<td></td>';
1656 if ($report['show_cost']) {
1657 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1658 $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
1661 if ($report['show_paid']) $body .= '<td></td>';
1662 if ($report['show_invoice']) $body .= '<td></td>';
1667 $body .= '<tr><td> </td></tr>';
1668 $body .= '<tr style="'.$rowSubtotal.'">';
1669 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.total').'</td>';
1670 if ($canViewReports || $isClient) $body .= '<td></td>';
1671 if ($report['show_client']) $body .= '<td></td>';
1672 if ($report['show_project']) $body .= '<td></td>';
1673 if ($report['show_task']) $body .= '<td></td>';
1674 if ($report['show_custom_field_1']) $body .= '<td></td>';
1675 if ($report['show_start']) $body .= '<td></td>';
1676 if ($report['show_end']) $body .= '<td></td>';
1677 if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
1678 if ($report['show_note']) $body .= '<td></td>';
1679 if ($report['show_cost']) {
1680 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1681 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1684 if ($report['show_paid']) $body .= '<td></td>';
1685 if ($report['show_invoice']) $body .= '<td></td>';
1688 $body .= '</table>';
1692 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
1693 $body .= '<p style="text-align: center;">'.$i18n->getKey('form.mail.footer').'</p>';
1695 // Finish creating email body.
1696 $body .= '</body></html>';
1701 // sendFavReport - sends a favorite report to a specified email, called from cron.php
1702 static function sendFavReport($report, $subject, $email, $cc) {
1703 // We are called from cron.php, we have no $bean in session.
1704 // cron.php sets global $user and $i18n objects to match our favorite report user.
1708 // Prepare report body.
1709 $body = ttReportHelper::prepareFavReportBody($report);
1711 import('mail.Mailer');
1712 $mailer = new Mailer();
1713 $mailer->setCharSet(CHARSET);
1714 $mailer->setContentType('text/html');
1715 $mailer->setSender(SENDER);
1717 $mailer->setReceiverCC($cc);
1718 if (!empty($user->bcc_email))
1719 $mailer->setReceiverBCC($user->bcc_email);
1720 $mailer->setReceiver($email);
1721 $mailer->setMailMode(MAIL_MODE);
1722 if (empty($subject)) $subject = $report['name'];
1723 if (!$mailer->send($subject, $body))