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 // Add invoice name if it is selected.
296 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
297 array_push($fields, 'i.name as invoice');
299 // Prepare sql query part for left joins.
301 if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
302 $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
303 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
304 $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
305 if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
306 $left_joins .= " left join tt_users u on (u.id = l.user_id)";
307 if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
308 $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
309 if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
310 $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
312 if ($cf_1_type == CustomFields::TYPE_TEXT)
313 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)";
314 elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
315 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)".
316 " left join tt_custom_field_options cfo on (cfl.option_id = cfo.id)";
319 if ($includeCost && MODE_TIME != $user->tracking_mode)
320 $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
322 $where = ttReportHelper::getWhere($bean);
324 // Construct sql query for tt_log items.
325 $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
326 // If we don't have expense items (such as when the Expenses plugin is desabled), the above is all sql we need,
327 // with an exception of sorting part, that is added in the end.
329 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
330 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
332 $fields = array(); // An array of fields for database query.
333 array_push($fields, 'ei.id');
334 array_push($fields, '2 as type'); // Type 2 is for tt_expense_items entries.
335 array_push($fields, 'ei.date');
336 if($canViewReports || $isClient)
337 array_push($fields, 'u.name as user');
338 // Add client name if it is selected.
339 if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
340 array_push($fields, 'c.name as client');
341 // Add project name if it is selected.
342 if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
343 array_push($fields, 'p.name as project');
344 if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
345 array_push($fields, 'null'); // null for task name. We need to match column count for union.
346 if ($bean->getAttribute('chcf_1') || 'cf_1' == $group_by_option)
347 array_push($fields, 'null'); // null for cf_1.
348 if ($bean->getAttribute('chstart')) {
349 array_push($fields, 'null'); // null for unformatted_start.
350 array_push($fields, 'null'); // null for start.
352 if ($bean->getAttribute('chfinish'))
353 array_push($fields, 'null'); // null for finish.
354 if ($bean->getAttribute('chduration'))
355 array_push($fields, 'null'); // null for duration.
356 // Use the note field to print item name.
357 if ($bean->getAttribute('chnote'))
358 array_push($fields, 'ei.name as note');
359 array_push($fields, 'ei.cost as cost');
360 array_push($fields, 'ei.cost as expense');
362 if ($canViewReports && $bean->getAttribute('chpaid'))
363 array_push($fields, 'ei.paid as paid');
364 // Add invoice name if it is selected.
365 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
366 array_push($fields, 'i.name as invoice');
368 // Prepare sql query part for left joins.
370 if ($canViewReports || $isClient)
371 $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
372 if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
373 $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
374 if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
375 $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
376 if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
377 $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
379 $where = ttReportHelper::getExpenseWhere($bean);
381 // Construct sql query for expense items.
382 $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
384 // Construct a union.
385 $sql = "($sql) union all ($sql_for_expense_items)";
388 // Determine sort part.
389 $sort_part = ' order by ';
390 if ('no_grouping' == $group_by_option || 'date' == $group_by_option)
391 $sort_part .= 'date';
393 $sort_part .= $group_by_option.', date';
394 if (($canViewReports || $isClient) && is_array($bean->getAttribute('users')) && 'user' != $group_by_option)
395 $sort_part .= ', user, type';
396 if ($bean->getAttribute('chstart'))
397 $sort_part .= ', unformatted_start';
398 $sort_part .= ', id';
401 // By now we are ready with sql.
403 // Obtain items for report.
404 $res = $mdb2->query($sql);
405 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
407 while ($val = $res->fetchRow()) {
408 if ($convertTo12Hour) {
409 if($val['start'] != '')
410 $val['start'] = ttTimeHelper::to12HourFormat($val['start']);
411 if($val['finish'] != '')
412 $val['finish'] = ttTimeHelper::to12HourFormat($val['finish']);
414 if (isset($val['cost'])) {
415 if ('.' != $user->decimal_mark)
416 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
418 if (isset($val['expense'])) {
419 if ('.' != $user->decimal_mark)
420 $val['expense'] = str_replace('.', $user->decimal_mark, $val['expense']);
422 if ('no_grouping' != $group_by_option) {
423 $val['grouped_by'] = $val[$group_by_option];
424 if ('date' == $group_by_option) {
425 // This is needed to get the date in user date format.
426 $o_date = new DateAndTime(DB_DATEFORMAT, $val['grouped_by']);
427 $val['grouped_by'] = $o_date->toString($user->date_format);
432 // This is needed to get the date in user date format.
433 $o_date = new DateAndTime(DB_DATEFORMAT, $val['date']);
434 $val['date'] = $o_date->toString($user->date_format);
438 $report_items[] = $row;
441 return $report_items;
444 // putInSession stores tt_log and tt_expense_items ids from a report in user session
445 // as 2 comma-separated lists.
446 static function putInSession($report_items) {
447 unset($_SESSION['report_item_ids']);
448 unset($_SESSION['report_item_expense_ids']);
450 // Iterate through records and build 2 comma-separated lists.
451 foreach($report_items as $item) {
452 if ($item['type'] == 1)
453 $report_item_ids .= ','.$item['id'];
454 else if ($item['type'] == 2)
455 $report_item_expense_ids .= ','.$item['id'];
457 $report_item_ids = trim($report_item_ids, ',');
458 $report_item_expense_ids = trim($report_item_expense_ids, ',');
460 // The lists are reqdy. Put them in session.
461 if ($report_item_ids) $_SESSION['report_item_ids'] = $report_item_ids;
462 if ($report_item_expense_ids) $_SESSION['report_item_expense_ids'] = $report_item_expense_ids;
465 // getFromSession obtains tt_log and tt_expense_items ids stored in user session.
466 static function getFromSession() {
468 $report_item_ids = $_SESSION['report_item_ids'];
469 if ($report_item_ids)
470 $items['report_item_ids'] = explode(',', $report_item_ids);
471 $report_item_expense_ids = $_SESSION['report_item_expense_ids'];
472 if ($report_item_expense_ids)
473 $items['report_item_expense_ids'] = explode(',', $report_item_expense_ids);
477 // getFavItems retrieves all items associated with a favorite report.
478 // It combines tt_log and tt_expense_items in one array for presentation in one table using mysql union all.
479 // Expense items use the "note" field for item name.
480 static function getFavItems($report) {
482 $mdb2 = getConnection();
484 // Determine these once as they are used in multiple places in this function.
485 $canViewReports = $user->can('view_reports');
486 $isClient = $user->isClient();
488 $group_by_option = $report['group_by'];
489 $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($report['show_start'] || $report['show_end']);
491 // Prepare a query for time items in tt_log table.
492 $fields = array(); // An array of fields for database query.
493 array_push($fields, 'l.id as id');
494 array_push($fields, '1 as type'); // Type 1 is for tt_log entries.
495 array_push($fields, 'l.date as date');
496 if($canViewReports || $isClient)
497 array_push($fields, 'u.name as user');
498 // Add client name if it is selected.
499 if ($report['show_client'] || 'client' == $group_by_option)
500 array_push($fields, 'c.name as client');
501 // Add project name if it is selected.
502 if ($report['show_project'] || 'project' == $group_by_option)
503 array_push($fields, 'p.name as project');
504 // Add task name if it is selected.
505 if ($report['show_task'] || 'task' == $group_by_option)
506 array_push($fields, 't.name as task');
508 $include_cf_1 = $report['show_custom_field_1'] || 'cf_1' == $group_by_option;
510 $custom_fields = new CustomFields($user->team_id);
511 $cf_1_type = $custom_fields->fields[0]['type'];
512 if ($cf_1_type == CustomFields::TYPE_TEXT) {
513 array_push($fields, 'cfl.value as cf_1');
514 } elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
515 array_push($fields, 'cfo.value as cf_1');
519 if ($report['show_start']) {
520 array_push($fields, "l.start as unformatted_start");
521 array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start");
524 if ($report['show_end'])
525 array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish");
527 if ($report['show_duration'])
528 array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
530 if ($report['show_note'])
531 array_push($fields, 'l.comment as note');
533 $includeCost = $report['show_cost'];
535 if (MODE_TIME == $user->tracking_mode)
536 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.
538 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.
539 array_push($fields, "null as expense");
541 // Add invoice name if it is selected.
542 if (($canViewReports || $isClient) && $report['show_invoice'])
543 array_push($fields, 'i.name as invoice');
545 // Prepare sql query part for left joins.
547 if ($report['show_client'] || 'client' == $group_by_option)
548 $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
549 if (($canViewReports || $isClient) && $report['show_invoice'])
550 $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
551 if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
552 $left_joins .= " left join tt_users u on (u.id = l.user_id)";
553 if ($report['show_project'] || 'project' == $group_by_option)
554 $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
555 if ($report['show_task'] || 'task' == $group_by_option)
556 $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
558 if ($cf_1_type == CustomFields::TYPE_TEXT)
559 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)";
560 elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
561 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)".
562 " left join tt_custom_field_options cfo on (cfl.option_id = cfo.id)";
565 if ($includeCost && MODE_TIME != $user->tracking_mode)
566 $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
568 $where = ttReportHelper::getFavWhere($report);
570 // Construct sql query for tt_log items.
571 $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
572 // If we don't have expense items (such as when the Expenses plugin is desabled), the above is all sql we need,
573 // with an exception of sorting part, that is added in the end.
575 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
576 if ($report['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
578 $fields = array(); // An array of fields for database query.
579 array_push($fields, 'ei.id');
580 array_push($fields, '2 as type'); // Type 2 is for tt_expense_items entries.
581 array_push($fields, 'ei.date');
582 if($canViewReports || $isClient)
583 array_push($fields, 'u.name as user');
584 // Add client name if it is selected.
585 if ($report['show_client'] || 'client' == $group_by_option)
586 array_push($fields, 'c.name as client');
587 // Add project name if it is selected.
588 if ($report['show_project'] || 'project' == $group_by_option)
589 array_push($fields, 'p.name as project');
590 if ($report['show_task'] || 'task' == $group_by_option)
591 array_push($fields, 'null'); // null for task name. We need to match column count for union.
592 if ($report['show_custom_field_1'] || 'cf_1' == $group_by_option)
593 array_push($fields, 'null'); // null for cf_1.
594 if ($report['show_start']) {
595 array_push($fields, 'null'); // null for unformatted_start.
596 array_push($fields, 'null'); // null for start.
598 if ($report['show_end'])
599 array_push($fields, 'null'); // null for finish.
600 if ($report['show_duration'])
601 array_push($fields, 'null'); // null for duration.
602 // Use the note field to print item name.
603 if ($report['show_note'])
604 array_push($fields, 'ei.name as note');
605 array_push($fields, 'ei.cost as cost');
606 array_push($fields, 'ei.cost as expense');
607 // Add invoice name if it is selected.
608 if (($canViewReports || $isClient) && $report['show_invoice'])
609 array_push($fields, 'i.name as invoice');
611 // Prepare sql query part for left joins.
613 if ($canViewReports || $isClient)
614 $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
615 if ($report['show_client'] || 'client' == $group_by_option)
616 $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
617 if ($report['show_project'] || 'project' == $group_by_option)
618 $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
619 if (($canViewReports || $isClient) && $report['show_invoice'])
620 $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
622 $where = ttReportHelper::getFavExpenseWhere($report);
624 // Construct sql query for expense items.
625 $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
627 // Construct a union.
628 $sql = "($sql) union all ($sql_for_expense_items)";
631 // Determine sort part.
632 $sort_part = ' order by ';
633 if ($group_by_option == null || 'no_grouping' == $group_by_option || 'date' == $group_by_option) // TODO: fix DB for NULL values in group_by field.
634 $sort_part .= 'date';
636 $sort_part .= $group_by_option.', date';
637 if (($canViewReports || $isClient) /*&& is_array($bean->getAttribute('users'))*/ && 'user' != $group_by_option)
638 $sort_part .= ', user, type';
639 if ($report['show_start'])
640 $sort_part .= ', unformatted_start';
641 $sort_part .= ', id';
644 // By now we are ready with sql.
646 // Obtain items for report.
647 $res = $mdb2->query($sql);
648 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
650 while ($val = $res->fetchRow()) {
651 if ($convertTo12Hour) {
652 if($val['start'] != '')
653 $val['start'] = ttTimeHelper::to12HourFormat($val['start']);
654 if($val['finish'] != '')
655 $val['finish'] = ttTimeHelper::to12HourFormat($val['finish']);
657 if (isset($val['cost'])) {
658 if ('.' != $user->decimal_mark)
659 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
661 if (isset($val['expense'])) {
662 if ('.' != $user->decimal_mark)
663 $val['expense'] = str_replace('.', $user->decimal_mark, $val['expense']);
665 if ('no_grouping' != $group_by_option) {
666 $val['grouped_by'] = $val[$group_by_option];
667 if ('date' == $group_by_option) {
668 // This is needed to get the date in user date format.
669 $o_date = new DateAndTime(DB_DATEFORMAT, $val['grouped_by']);
670 $val['grouped_by'] = $o_date->toString($user->date_format);
675 // This is needed to get the date in user date format.
676 $o_date = new DateAndTime(DB_DATEFORMAT, $val['date']);
677 $val['date'] = $o_date->toString($user->date_format);
681 $report_items[] = $row;
684 return $report_items;
687 // getSubtotals calculates report items subtotals when a report is grouped by.
688 // Without expenses, it's a simple select with group by.
689 // With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
690 static function getSubtotals($bean) {
693 $group_by_option = $bean->getAttribute('group_by');
694 if ('no_grouping' == $group_by_option) return null;
696 $mdb2 = getConnection();
698 // Start with sql to obtain subtotals for time items. This simple sql will be used when we have no expenses.
700 // Determine group by field and a required join.
701 switch ($group_by_option) {
703 $group_field = 'l.date';
707 $group_field = 'u.name';
708 $group_join = 'left join tt_users u on (l.user_id = u.id) ';
711 $group_field = 'c.name';
712 $group_join = 'left join tt_clients c on (l.client_id = c.id) ';
715 $group_field = 'p.name';
716 $group_join = 'left join tt_projects p on (l.project_id = p.id) ';
719 $group_field = 't.name';
720 $group_join = 'left join tt_tasks t on (l.task_id = t.id) ';
723 $group_field = 'cfo.value';
724 $custom_fields = new CustomFields($user->team_id);
725 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
726 $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) ';
727 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
728 $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) ';
732 $where = ttReportHelper::getWhere($bean);
733 if ($bean->getAttribute('chcost')) {
734 if (MODE_TIME == $user->tracking_mode) {
735 if ($group_by_option != 'user')
736 $left_join = 'left join tt_users u on (l.user_id = u.id)';
737 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
738 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
739 null as expenses from tt_log l
740 $group_join $left_join $where group by $group_field";
742 // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
743 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
744 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
745 null as expenses from tt_log l
747 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";
750 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, null as expenses from tt_log l
751 $group_join $where group by $group_field";
753 // By now we have sql for time items.
755 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
756 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
758 // Determine group by field and a required join.
760 $group_field = 'null';
761 switch ($group_by_option) {
763 $group_field = 'ei.date';
767 $group_field = 'u.name';
768 $group_join = 'left join tt_users u on (ei.user_id = u.id) ';
771 $group_field = 'c.name';
772 $group_join = 'left join tt_clients c on (ei.client_id = c.id) ';
775 $group_field = 'p.name';
776 $group_join = 'left join tt_projects p on (ei.project_id = p.id) ';
780 $where = ttReportHelper::getExpenseWhere($bean);
781 $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
783 // Add a "group by" clause if we are grouping.
784 if ('null' != $group_field) $sql_for_expenses .= " group by $group_field";
786 // Create a combined query.
787 $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";
791 $res = $mdb2->query($sql);
792 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
794 while ($val = $res->fetchRow()) {
795 if ('date' == $group_by_option) {
796 // This is needed to get the date in user date format.
797 $o_date = new DateAndTime(DB_DATEFORMAT, $val['group_field']);
798 $val['group_field'] = $o_date->toString($user->date_format);
801 $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
802 if ($bean->getAttribute('chcost')) {
803 if ('.' != $user->decimal_mark) {
804 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
805 $val['expenses'] = str_replace('.', $user->decimal_mark, $val['expenses']);
807 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time,'cost'=>$val['cost'],'expenses'=>$val['expenses']);
809 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time);
815 // getFavSubtotals calculates report items subtotals when a favorite report is grouped by.
816 // Without expenses, it's a simple select with group by.
817 // With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
818 static function getFavSubtotals($report) {
821 $group_by_option = $report['group_by'];
822 if ('no_grouping' == $group_by_option) return null;
824 $mdb2 = getConnection();
826 // Start with sql to obtain subtotals for time items. This simple sql will be used when we have no expenses.
828 // Determine group by field and a required join.
829 switch ($group_by_option) {
831 $group_field = 'l.date';
835 $group_field = 'u.name';
836 $group_join = 'left join tt_users u on (l.user_id = u.id) ';
839 $group_field = 'c.name';
840 $group_join = 'left join tt_clients c on (l.client_id = c.id) ';
843 $group_field = 'p.name';
844 $group_join = 'left join tt_projects p on (l.project_id = p.id) ';
847 $group_field = 't.name';
848 $group_join = 'left join tt_tasks t on (l.task_id = t.id) ';
851 $group_field = 'cfo.value';
852 $custom_fields = new CustomFields($user->team_id);
853 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
854 $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) ';
855 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
856 $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) ';
860 $where = ttReportHelper::getFavWhere($report);
861 if ($report['show_cost']) {
862 if (MODE_TIME == $user->tracking_mode) {
863 if ($group_by_option != 'user')
864 $left_join = 'left join tt_users u on (l.user_id = u.id)';
865 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
866 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
867 null as expenses from tt_log l
868 $group_join $left_join $where group by $group_field";
870 // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
871 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
872 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
873 null as expenses from tt_log l
875 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";
878 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, null as expenses from tt_log l
879 $group_join $where group by $group_field";
881 // By now we have sql for time items.
883 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
884 if ($report['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
886 // Determine group by field and a required join.
888 $group_field = 'null';
889 switch ($group_by_option) {
891 $group_field = 'ei.date';
895 $group_field = 'u.name';
896 $group_join = 'left join tt_users u on (ei.user_id = u.id) ';
899 $group_field = 'c.name';
900 $group_join = 'left join tt_clients c on (ei.client_id = c.id) ';
903 $group_field = 'p.name';
904 $group_join = 'left join tt_projects p on (ei.project_id = p.id) ';
908 $where = ttReportHelper::getFavExpenseWhere($report);
909 $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
911 // Add a "group by" clause if we are grouping.
912 if ('null' != $group_field) $sql_for_expenses .= " group by $group_field";
914 // Create a combined query.
915 $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";
919 $res = $mdb2->query($sql);
920 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
922 while ($val = $res->fetchRow()) {
923 if ('date' == $group_by_option) {
924 // This is needed to get the date in user date format.
925 $o_date = new DateAndTime(DB_DATEFORMAT, $val['group_field']);
926 $val['group_field'] = $o_date->toString($user->date_format);
929 $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
930 if ($report['show_cost']) {
931 if ('.' != $user->decimal_mark) {
932 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
933 $val['expenses'] = str_replace('.', $user->decimal_mark, $val['expenses']);
935 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time,'cost'=>$val['cost'],'expenses'=>$val['expenses']);
937 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time);
943 // getTotals calculates total hours and cost for all report items.
944 static function getTotals($bean)
948 $mdb2 = getConnection();
950 $where = ttReportHelper::getWhere($bean);
952 // Start with a query for time items.
953 if ($bean->getAttribute('chcost')) {
954 if (MODE_TIME == $user->tracking_mode) {
955 $sql = "select sum(time_to_sec(l.duration)) as time,
956 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
959 left join tt_users u on (l.user_id = u.id) $where";
961 $sql = "select sum(time_to_sec(l.duration)) as time,
962 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
965 left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where";
968 $sql = "select sum(time_to_sec(l.duration)) as time, null as cost, null as expenses from tt_log l $where";
970 // If we have expenses, query becomes a bit more complex.
971 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) {
972 $where = ttReportHelper::getExpenseWhere($bean);
973 $sql_for_expenses = "select null as time, sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
974 // Create a combined query.
975 $sql = "select sum(time) as time, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
979 $res = $mdb2->query($sql);
980 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
982 $val = $res->fetchRow();
983 $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
984 if ($bean->getAttribute('chcost')) {
985 $total_cost = $val['cost'];
986 if (!$total_cost) $total_cost = '0.00';
987 if ('.' != $user->decimal_mark)
988 $total_cost = str_replace('.', $user->decimal_mark, $total_cost);
989 $total_expenses = $val['expenses'];
990 if (!$total_expenses) $total_expenses = '0.00';
991 if ('.' != $user->decimal_mark)
992 $total_expenses = str_replace('.', $user->decimal_mark, $total_expenses);
995 if ($bean->getAttribute('period'))
996 $period = new Period($bean->getAttribute('period'), new DateAndTime($user->date_format));
998 $period = new Period();
1000 new DateAndTime($user->date_format, $bean->getAttribute('start_date')),
1001 new DateAndTime($user->date_format, $bean->getAttribute('end_date')));
1004 $totals['start_date'] = $period->getStartDate();
1005 $totals['end_date'] = $period->getEndDate();
1006 $totals['time'] = $total_time;
1007 $totals['cost'] = $total_cost;
1008 $totals['expenses'] = $total_expenses;
1013 // getFavTotals calculates total hours and cost for all favorite report items.
1014 static function getFavTotals($report)
1018 $mdb2 = getConnection();
1020 $where = ttReportHelper::getFavWhere($report);
1022 // Start with a query for time items.
1023 if ($report['show_cost']) {
1024 if (MODE_TIME == $user->tracking_mode) {
1025 $sql = "select sum(time_to_sec(l.duration)) as time,
1026 sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
1029 left join tt_users u on (l.user_id = u.id) $where";
1031 $sql = "select sum(time_to_sec(l.duration)) as time,
1032 sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
1035 left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where";
1038 $sql = "select sum(time_to_sec(l.duration)) as time, null as cost, null as expenses from tt_log l $where";
1040 // If we have expenses, query becomes a bit more complex.
1041 if ($report['show_cost'] && $user->isPluginEnabled('ex')) {
1042 $where = ttReportHelper::getFavExpenseWhere($report);
1043 $sql_for_expenses = "select null as time, sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
1044 // Create a combined query.
1045 $sql = "select sum(time) as time, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
1049 $res = $mdb2->query($sql);
1050 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
1052 $val = $res->fetchRow();
1053 $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
1054 if ($report['show_cost']) {
1055 $total_cost = $val['cost'];
1056 if (!$total_cost) $total_cost = '0.00';
1057 if ('.' != $user->decimal_mark)
1058 $total_cost = str_replace('.', $user->decimal_mark, $total_cost);
1059 $total_expenses = $val['expenses'];
1060 if (!$total_expenses) $total_expenses = '0.00';
1061 if ('.' != $user->decimal_mark)
1062 $total_expenses = str_replace('.', $user->decimal_mark, $total_expenses);
1065 if ($report['period'])
1066 $period = new Period($report['period'], new DateAndTime($user->date_format));
1068 $period = new Period();
1070 new DateAndTime($user->date_format, $report['period_start']),
1071 new DateAndTime($user->date_format, $report['period_end']));
1074 $totals['start_date'] = $period->getStartDate();
1075 $totals['end_date'] = $period->getEndDate();
1076 $totals['time'] = $total_time;
1077 $totals['cost'] = $total_cost;
1078 $totals['expenses'] = $total_expenses;
1083 // The assignToInvoice assigns a set of records to a specific invoice.
1084 static function assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids)
1086 $mdb2 = getConnection();
1087 if ($time_log_ids) {
1088 $sql = "update tt_log set invoice_id = ".$mdb2->quote($invoice_id).
1089 " where id in(".join(', ', $time_log_ids).")";
1090 $affected = $mdb2->exec($sql);
1091 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1093 if ($expense_item_ids) {
1094 $sql = "update tt_expense_items set invoice_id = ".$mdb2->quote($invoice_id).
1095 " where id in(".join(', ', $expense_item_ids).")";
1096 $affected = $mdb2->exec($sql);
1097 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1101 // The markPaid marks a set of records as either paid or unpaid.
1102 static function markPaid($time_log_ids, $expense_item_ids, $paid = true)
1104 $mdb2 = getConnection();
1105 $paid_val = (int) $paid;
1106 if ($time_log_ids) {
1107 $sql = "update tt_log set paid = $paid_val where id in(".join(', ', $time_log_ids).")";
1108 $affected = $mdb2->exec($sql);
1109 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1111 if ($expense_item_ids) {
1112 $sql = "update tt_expense_items set paid = $paid_val where id in(".join(', ', $expense_item_ids).")";
1113 $affected = $mdb2->exec($sql);
1114 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
1118 // prepareReportBody - prepares an email body for report.
1119 static function prepareReportBody($bean, $comment)
1124 // Determine these once as they are used in multiple places in this function.
1125 $canViewReports = $user->can('view_reports');
1126 $isClient = $user->isClient();
1128 $items = ttReportHelper::getItems($bean);
1129 $group_by = $bean->getAttribute('group_by');
1130 if ($group_by && 'no_grouping' != $group_by)
1131 $subtotals = ttReportHelper::getSubtotals($bean);
1132 $totals = ttReportHelper::getTotals($bean);
1134 // Use custom fields plugin if it is enabled.
1135 if ($user->isPluginEnabled('cf'))
1136 $custom_fields = new CustomFields($user->team_id);
1138 // Define some styles to use in email.
1139 $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
1140 $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
1141 $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
1142 $rowItem = 'background-color: #ffffff;';
1143 $rowItemAlt = 'background-color: #f5f5f5;';
1144 $rowSubtotal = 'background-color: #e0e0e0;';
1145 $cellLeftAligned = 'text-align: left; vertical-align: top;';
1146 $cellRightAligned = 'text-align: right; vertical-align: top;';
1147 $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
1148 $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
1150 // Start creating email body.
1152 $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
1156 $body .= '<p style="'.$style_title.'">'.$i18n->getKey('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
1159 if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>';
1161 if ($bean->getAttribute('chtotalsonly')) {
1162 // Totals only report. Output subtotals.
1164 // Determine group_by header.
1165 if ('cf_1' == $group_by)
1166 $group_by_header = htmlspecialchars($custom_fields->fields[0]['label']);
1168 $key = 'label.'.$group_by;
1169 $group_by_header = $i18n->getKey($key);
1172 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1174 $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
1175 if ($bean->getAttribute('chduration'))
1176 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.duration').'</td>';
1177 if ($bean->getAttribute('chcost'))
1178 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.cost').'</td>';
1180 foreach($subtotals as $subtotal) {
1181 $body .= '<tr style="'.$rowSubtotal.'">';
1182 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : ' ').'</td>';
1183 if ($bean->getAttribute('chduration')) {
1184 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1185 if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
1188 if ($bean->getAttribute('chcost')) {
1189 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1190 $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
1197 $body .= '<tr><td> </td></tr>';
1198 $body .= '<tr style="'.$rowSubtotal.'">';
1199 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.total').'</td>';
1200 if ($bean->getAttribute('chduration')) {
1201 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1202 if ($totals['time'] <> '0:00') $body .= $totals['time'];
1205 if ($bean->getAttribute('chcost')) {
1206 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1207 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1212 $body .= '</table>';
1216 // Print table header.
1217 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1219 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.date').'</td>';
1220 if ($canViewReports || $isClient)
1221 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.user').'</td>';
1222 if ($bean->getAttribute('chclient'))
1223 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.client').'</td>';
1224 if ($bean->getAttribute('chproject'))
1225 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.project').'</td>';
1226 if ($bean->getAttribute('chtask'))
1227 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.task').'</td>';
1228 if ($bean->getAttribute('chcf_1'))
1229 $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
1230 if ($bean->getAttribute('chstart'))
1231 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.start').'</td>';
1232 if ($bean->getAttribute('chfinish'))
1233 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.finish').'</td>';
1234 if ($bean->getAttribute('chduration'))
1235 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.duration').'</td>';
1236 if ($bean->getAttribute('chnote'))
1237 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.note').'</td>';
1238 if ($bean->getAttribute('chcost'))
1239 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.cost').'</td>';
1240 if ($bean->getAttribute('chpaid'))
1241 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.paid').'</td>';
1242 if ($bean->getAttribute('chinvoice'))
1243 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.invoice').'</td>';
1246 // Initialize variables to print subtotals.
1247 if ($items && 'no_grouping' != $group_by) {
1248 $print_subtotals = true;
1250 $prev_grouped_by = '';
1251 $cur_grouped_by = '';
1253 // Initialize variables to alternate color of rows for different dates.
1256 $row_style = $rowItem;
1258 // Print report items.
1259 if (is_array($items)) {
1260 foreach ($items as $record) {
1261 $cur_date = $record['date'];
1262 // Print a subtotal row after a block of grouped items.
1263 if ($print_subtotals) {
1264 $cur_grouped_by = $record['grouped_by'];
1265 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
1266 $body .= '<tr style="'.$rowSubtotal.'">';
1267 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.subtotal').'</td>';
1268 $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
1269 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1270 if ($bean->getAttribute('chclient')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1271 if ($bean->getAttribute('chproject')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1272 if ($bean->getAttribute('chtask')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1273 if ($bean->getAttribute('chcf_1')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1274 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1275 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1276 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
1277 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1278 if ($bean->getAttribute('chcost')) {
1279 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1280 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
1283 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1284 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1286 $body .= '<tr><td> </td></tr>';
1288 $first_pass = false;
1291 // Print a regular row.
1292 if ($cur_date != $prev_date)
1293 $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
1294 $body .= '<tr style="'.$row_style.'">';
1295 $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
1296 if ($canViewReports || $isClient)
1297 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
1298 if ($bean->getAttribute('chclient'))
1299 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
1300 if ($bean->getAttribute('chproject'))
1301 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
1302 if ($bean->getAttribute('chtask'))
1303 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
1304 if ($bean->getAttribute('chcf_1'))
1305 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
1306 if ($bean->getAttribute('chstart'))
1307 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
1308 if ($bean->getAttribute('chfinish'))
1309 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
1310 if ($bean->getAttribute('chduration'))
1311 $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
1312 if ($bean->getAttribute('chnote'))
1313 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
1314 if ($bean->getAttribute('chcost'))
1315 $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
1316 if ($bean->getAttribute('chpaid')) {
1317 $body .= '<td style="'.$cellRightAligned.'">';
1318 $body .= $record['paid'] == 1 ? $i18n->getKey('label.yes') : $i18n->getKey('label.no');
1321 if ($bean->getAttribute('chinvoice'))
1322 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
1325 $prev_date = $record['date'];
1326 if ($print_subtotals)
1327 $prev_grouped_by = $record['grouped_by'];
1331 // Print a terminating subtotal.
1332 if ($print_subtotals) {
1333 $body .= '<tr style="'.$rowSubtotal.'">';
1334 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.subtotal').'</td>';
1335 $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
1336 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1337 if ($bean->getAttribute('chclient')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1338 if ($bean->getAttribute('chproject')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1339 if ($bean->getAttribute('chtask')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1340 if ($bean->getAttribute('chcf_1')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1341 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1342 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1343 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
1344 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1345 if ($bean->getAttribute('chcost')) {
1346 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1347 $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
1350 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1351 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1356 $body .= '<tr><td> </td></tr>';
1357 $body .= '<tr style="'.$rowSubtotal.'">';
1358 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.total').'</td>';
1359 if ($canViewReports || $isClient) $body .= '<td></td>';
1360 if ($bean->getAttribute('chclient')) $body .= '<td></td>';
1361 if ($bean->getAttribute('chproject')) $body .= '<td></td>';
1362 if ($bean->getAttribute('chtask')) $body .= '<td></td>';
1363 if ($bean->getAttribute('chcf_1')) $body .= '<td></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.'">'.$totals['time'].'</td>';
1367 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1368 if ($bean->getAttribute('chcost')) {
1369 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1370 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1373 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1374 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1377 $body .= '</table>';
1381 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
1382 $body .= '<p style="text-align: center;">'.$i18n->getKey('form.mail.footer').'</p>';
1384 // Finish creating email body.
1385 $body .= '</body></html>';
1390 // checkFavReportCondition - checks whether it is okay to send fav report.
1391 static function checkFavReportCondition($report, $condition)
1393 $items = ttReportHelper::getFavItems($report);
1395 $condition = str_replace('count', '', $condition);
1396 $count_required = (int) trim(str_replace('>', '', $condition));
1398 if (count($items) > $count_required)
1399 return true; // Condition ok.
1404 // prepareFavReportBody - prepares an email body for a favorite report.
1405 static function prepareFavReportBody($report)
1410 // Determine these once as they are used in multiple places in this function.
1411 $canViewReports = $user->can('view_reports');
1412 $isClient = $user->isClient();
1414 $items = ttReportHelper::getFavItems($report);
1415 $group_by = $report['group_by'];
1416 if ($group_by && 'no_grouping' != $group_by)
1417 $subtotals = ttReportHelper::getFavSubtotals($report);
1418 $totals = ttReportHelper::getFavTotals($report);
1420 // Use custom fields plugin if it is enabled.
1421 if ($user->isPluginEnabled('cf'))
1422 $custom_fields = new CustomFields($user->team_id);
1424 // Define some styles to use in email.
1425 $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
1426 $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
1427 $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
1428 $rowItem = 'background-color: #ffffff;';
1429 $rowItemAlt = 'background-color: #f5f5f5;';
1430 $rowSubtotal = 'background-color: #e0e0e0;';
1431 $cellLeftAligned = 'text-align: left; vertical-align: top;';
1432 $cellRightAligned = 'text-align: right; vertical-align: top;';
1433 $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
1434 $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
1436 // Start creating email body.
1438 $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
1442 $body .= '<p style="'.$style_title.'">'.$i18n->getKey('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
1445 // if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>'; // No comment for fav. reports.
1447 if ($report['show_totals_only']) {
1448 // Totals only report. Output subtotals.
1450 // Determine group_by header.
1451 if ('cf_1' == $group_by)
1452 $group_by_header = htmlspecialchars($custom_fields->fields[0]['label']);
1454 $key = 'label.'.$group_by;
1455 $group_by_header = $i18n->getKey($key);
1458 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1460 $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
1461 if ($report['show_duration'])
1462 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.duration').'</td>';
1463 if ($report['show_cost'])
1464 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.cost').'</td>';
1466 foreach($subtotals as $subtotal) {
1467 $body .= '<tr style="'.$rowSubtotal.'">';
1468 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : ' ').'</td>';
1469 if ($report['show_duration']) {
1470 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1471 if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
1474 if ($report['show_cost']) {
1475 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1476 $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
1483 $body .= '<tr><td> </td></tr>';
1484 $body .= '<tr style="'.$rowSubtotal.'">';
1485 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.total').'</td>';
1486 if ($report['show_duration']) {
1487 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1488 if ($totals['time'] <> '0:00') $body .= $totals['time'];
1491 if ($report['show_cost']) {
1492 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1493 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1498 $body .= '</table>';
1502 // Print table header.
1503 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1505 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.date').'</td>';
1506 if ($canViewReports || $isClient)
1507 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.user').'</td>';
1508 if ($report['show_client'])
1509 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.client').'</td>';
1510 if ($report['show_project'])
1511 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.project').'</td>';
1512 if ($report['show_task'])
1513 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.task').'</td>';
1514 if ($report['show_custom_field_1'])
1515 $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
1516 if ($report['show_start'])
1517 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.start').'</td>';
1518 if ($report['show_end'])
1519 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.finish').'</td>';
1520 if ($report['show_duration'])
1521 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.duration').'</td>';
1522 if ($report['show_note'])
1523 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.note').'</td>';
1524 if ($report['show_cost'])
1525 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.cost').'</td>';
1526 if ($report['show_paid'])
1527 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->getKey('label.paid').'</td>';
1528 if ($report['show_invoice'])
1529 $body .= '<td style="'.$tableHeader.'">'.$i18n->getKey('label.invoice').'</td>';
1532 // Initialize variables to print subtotals.
1533 if ($items && 'no_grouping' != $group_by) {
1534 $print_subtotals = true;
1536 $prev_grouped_by = '';
1537 $cur_grouped_by = '';
1539 // Initialize variables to alternate color of rows for different dates.
1542 $row_style = $rowItem;
1544 // Print report items.
1545 if (is_array($items)) {
1546 foreach ($items as $record) {
1547 $cur_date = $record['date'];
1548 // Print a subtotal row after a block of grouped items.
1549 if ($print_subtotals) {
1550 $cur_grouped_by = $record['grouped_by'];
1551 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
1552 $body .= '<tr style="'.$rowSubtotal.'">';
1553 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.subtotal').'</td>';
1554 $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
1555 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1556 if ($report['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1557 if ($report['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1558 if ($report['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1559 if ($report['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1560 if ($report['show_start']) $body .= '<td></td>';
1561 if ($report['show_end']) $body .= '<td></td>';
1562 if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
1563 if ($report['show_note']) $body .= '<td></td>';
1564 if ($report['show_cost']) {
1565 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1566 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
1569 if ($report['show_paid']) $body .= '<td></td>';
1570 if ($report['show_invoice']) $body .= '<td></td>';
1572 $body .= '<tr><td> </td></tr>';
1574 $first_pass = false;
1577 // Print a regular row.
1578 if ($cur_date != $prev_date)
1579 $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
1580 $body .= '<tr style="'.$row_style.'">';
1581 $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
1582 if ($canViewReports || $isClient)
1583 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
1584 if ($report['show_client'])
1585 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
1586 if ($report['show_project'])
1587 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
1588 if ($report['show_task'])
1589 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
1590 if ($report['show_custom_field_1'])
1591 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
1592 if ($report['show_start'])
1593 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
1594 if ($report['show_end'])
1595 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
1596 if ($report['show_duration'])
1597 $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
1598 if ($report['show_note'])
1599 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
1600 if ($report['show_cost'])
1601 $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
1602 if ($report['show_paid']) {
1603 $body .= '<td style="'.$cellRightAligned.'">';
1604 $body .= $record['paid'] == 1 ? $i18n->getKey('label.yes') : $i18n->getKey('label.no');
1607 if ($report['show_invoice'])
1608 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
1611 $prev_date = $record['date'];
1612 if ($print_subtotals)
1613 $prev_grouped_by = $record['grouped_by'];
1617 // Print a terminating subtotal.
1618 if ($print_subtotals) {
1619 $body .= '<tr style="'.$rowSubtotal.'">';
1620 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.subtotal').'</td>';
1621 $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
1622 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1623 if ($report['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1624 if ($report['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1625 if ($report['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1626 if ($report['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1627 if ($report['show_start']) $body .= '<td></td>';
1628 if ($report['show_end']) $body .= '<td></td>';
1629 if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
1630 if ($report['show_note']) $body .= '<td></td>';
1631 if ($report['show_cost']) {
1632 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1633 $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
1636 if ($report['show_paid']) $body .= '<td></td>';
1637 if ($report['show_invoice']) $body .= '<td></td>';
1642 $body .= '<tr><td> </td></tr>';
1643 $body .= '<tr style="'.$rowSubtotal.'">';
1644 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->getKey('label.total').'</td>';
1645 if ($canViewReports || $isClient) $body .= '<td></td>';
1646 if ($report['show_client']) $body .= '<td></td>';
1647 if ($report['show_project']) $body .= '<td></td>';
1648 if ($report['show_task']) $body .= '<td></td>';
1649 if ($report['show_custom_field_1']) $body .= '<td></td>';
1650 if ($report['show_start']) $body .= '<td></td>';
1651 if ($report['show_end']) $body .= '<td></td>';
1652 if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
1653 if ($report['show_note']) $body .= '<td></td>';
1654 if ($report['show_cost']) {
1655 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1656 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1659 if ($report['show_paid']) $body .= '<td></td>';
1660 if ($report['show_invoice']) $body .= '<td></td>';
1663 $body .= '</table>';
1667 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
1668 $body .= '<p style="text-align: center;">'.$i18n->getKey('form.mail.footer').'</p>';
1670 // Finish creating email body.
1671 $body .= '</body></html>';
1676 // sendFavReport - sends a favorite report to a specified email, called from cron.php
1677 static function sendFavReport($report, $subject, $email, $cc) {
1678 // We are called from cron.php, we have no $bean in session.
1679 // cron.php sets global $user and $i18n objects to match our favorite report user.
1683 // Prepare report body.
1684 $body = ttReportHelper::prepareFavReportBody($report);
1686 import('mail.Mailer');
1687 $mailer = new Mailer();
1688 $mailer->setCharSet(CHARSET);
1689 $mailer->setContentType('text/html');
1690 $mailer->setSender(SENDER);
1692 $mailer->setReceiverCC($cc);
1693 if (!empty($user->bcc_email))
1694 $mailer->setReceiverBCC($user->bcc_email);
1695 $mailer->setReceiver($email);
1696 $mailer->setMailMode(MAIL_MODE);
1697 if (empty($subject)) $subject = $report['name'];
1698 if (!$mailer->send($subject, $body))