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($options) {
43 // Prepare dropdown parts.
45 if ($options['client_id'])
46 $dropdown_parts .= ' and l.client_id = '.$options['client_id'];
47 elseif ($user->isClient() && $user->client_id)
48 $dropdown_parts .= ' and l.client_id = '.$user->client_id;
49 if ($options['cf_1_option_id']) $dropdown_parts .= ' and l.id in(select log_id from tt_custom_field_log where status = 1 and option_id = '.$options['cf_1_option_id'].')';
50 if ($options['project_id']) $dropdown_parts .= ' and l.project_id = '.$options['project_id'];
51 if ($options['task_id']) $dropdown_parts .= ' and l.task_id = '.$options['task_id'];
52 if ($options['billable']=='1') $dropdown_parts .= ' and l.billable = 1';
53 if ($options['billable']=='2') $dropdown_parts .= ' and l.billable = 0';
54 if ($options['invoice']=='1') $dropdown_parts .= ' and l.invoice_id is not NULL';
55 if ($options['invoice']=='2') $dropdown_parts .= ' and l.invoice_id is NULL';
56 if ($options['paid_status']=='1') $dropdown_parts .= ' and l.paid = 1';
57 if ($options['paid_status']=='2') $dropdown_parts .= ' and l.paid = 0';
59 // Prepare sql query part for user list.
60 $userlist = $options['users'] ? $options['users'] : '-1';
61 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
62 $user_list_part = " and l.user_id in ($userlist)";
64 $user_list_part = " and l.user_id = ".$user->id;
65 $user_list_part .= " and l.group_id = ".$user->getActiveGroup();
67 // Prepare sql query part for where.
68 if ($options['period'])
69 $period = new Period($options['period'], new DateAndTime($user->date_format));
71 $period = new Period();
73 new DateAndTime($user->date_format, $options['period_start']),
74 new DateAndTime($user->date_format, $options['period_end']));
76 $where = " where l.status = 1 and l.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and l.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
77 " $user_list_part $dropdown_parts";
81 // getExpenseWhere prepares WHERE clause for expenses query in a report.
82 static function getExpenseWhere($options) {
85 // Prepare dropdown parts.
87 if ($options['client_id'])
88 $dropdown_parts .= ' and ei.client_id = '.$options['client_id'];
89 elseif ($user->isClient() && $user->client_id)
90 $dropdown_parts .= ' and ei.client_id = '.$user->client_id;
91 if ($options['project_id']) $dropdown_parts .= ' and ei.project_id = '.$options['project_id'];
92 if ($options['invoice']=='1') $dropdown_parts .= ' and ei.invoice_id is not NULL';
93 if ($options['invoice']=='2') $dropdown_parts .= ' and ei.invoice_id is NULL';
94 if ($options['paid_status']=='1') $dropdown_parts .= ' and ei.paid = 1';
95 if ($options['paid_status']=='2') $dropdown_parts .= ' and ei.paid = 0';
97 // Prepare sql query part for user list.
98 $userlist = $options['users'] ? $options['users'] : '-1';
99 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
100 $user_list_part = " and ei.user_id in ($userlist)";
102 $user_list_part = " and ei.user_id = ".$user->id;
103 $user_list_part .= " and ei.group_id = ".$user->getActiveGroup();
105 // Prepare sql query part for where.
106 if ($options['period'])
107 $period = new Period($options['period'], new DateAndTime($user->date_format));
109 $period = new Period();
111 new DateAndTime($user->date_format, $options['period_start']),
112 new DateAndTime($user->date_format, $options['period_end']));
114 $where = " where ei.status = 1 and ei.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and ei.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
115 " $user_list_part $dropdown_parts";
119 // getItems retrieves all items associated with a report.
120 // It combines tt_log and tt_expense_items in one array for presentation in one table using mysql union all.
121 // Expense items use the "note" field for item name.
122 static function getItems($options) {
124 $mdb2 = getConnection();
126 // Determine these once as they are used in multiple places in this function.
127 $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
128 $isClient = $user->isClient();
130 $group_by_option = $options['group_by'];
131 $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($options['show_start'] || $options['show_end']);
133 // Prepare a query for time items in tt_log table.
134 $fields = array(); // An array of fields for database query.
135 array_push($fields, 'l.id as id');
136 array_push($fields, '1 as type'); // Type 1 is for tt_log entries.
137 array_push($fields, 'l.date as date');
138 if($canViewReports || $isClient)
139 array_push($fields, 'u.name as user');
140 // Add client name if it is selected.
141 if ($options['show_client'] || 'client' == $group_by_option)
142 array_push($fields, 'c.name as client');
143 // Add project name if it is selected.
144 if ($options['show_project'] || 'project' == $group_by_option)
145 array_push($fields, 'p.name as project');
146 // Add task name if it is selected.
147 if ($options['show_task'] || 'task' == $group_by_option)
148 array_push($fields, 't.name as task');
150 $include_cf_1 = $options['show_custom_field_1'] || 'cf_1' == $group_by_option;
152 $custom_fields = new CustomFields($user->group_id);
153 $cf_1_type = $custom_fields->fields[0]['type'];
154 if ($cf_1_type == CustomFields::TYPE_TEXT) {
155 array_push($fields, 'cfl.value as cf_1');
156 } elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
157 array_push($fields, 'cfo.value as cf_1');
161 if ($options['show_start']) {
162 array_push($fields, "l.start as unformatted_start");
163 array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start");
166 if ($options['show_end'])
167 array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish");
169 if ($options['show_duration'])
170 array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
172 if ($options['show_work_units']) {
173 if ($user->unit_totals_only)
174 array_push($fields, "null as units");
176 array_push($fields, "if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit)) as units");
179 if ($options['show_note'])
180 array_push($fields, 'l.comment as note');
182 $includeCost = $options['show_cost'];
184 if (MODE_TIME == $user->tracking_mode)
185 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.
187 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.
188 array_push($fields, "null as expense");
191 if ($canViewReports && $options['show_paid'])
192 array_push($fields, 'l.paid as paid');
194 if ($canViewReports && $options['show_ip']) {
195 array_push($fields, 'l.created as created');
196 array_push($fields, 'l.created_ip as created_ip');
197 array_push($fields, 'l.modified as modified');
198 array_push($fields, 'l.modified_ip as modified_ip');
200 // Add invoice name if it is selected.
201 if (($canViewReports || $isClient) && $options['show_invoice'])
202 array_push($fields, 'i.name as invoice');
204 // Prepare sql query part for left joins.
206 if ($options['show_client'] || 'client' == $group_by_option)
207 $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
208 if (($canViewReports || $isClient) && $options['show_invoice'])
209 $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
210 if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
211 $left_joins .= " left join tt_users u on (u.id = l.user_id)";
212 if ($options['show_project'] || 'project' == $group_by_option)
213 $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
214 if ($options['show_task'] || 'task' == $group_by_option)
215 $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
217 if ($cf_1_type == CustomFields::TYPE_TEXT)
218 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)";
219 elseif ($cf_1_type == CustomFields::TYPE_DROPDOWN) {
220 $left_joins .= " left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1)".
221 " left join tt_custom_field_options cfo on (cfl.option_id = cfo.id)";
224 if ($includeCost && MODE_TIME != $user->tracking_mode)
225 $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
227 $where = ttReportHelper::getWhere($options);
229 // Construct sql query for tt_log items.
230 $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
231 // If we don't have expense items (such as when the Expenses plugin is desabled), the above is all sql we need,
232 // with an exception of sorting part, that is added in the end.
234 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
235 if ($options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
237 $fields = array(); // An array of fields for database query.
238 array_push($fields, 'ei.id');
239 array_push($fields, '2 as type'); // Type 2 is for tt_expense_items entries.
240 array_push($fields, 'ei.date');
241 if($canViewReports || $isClient)
242 array_push($fields, 'u.name as user');
243 // Add client name if it is selected.
244 if ($options['show_client'] || 'client' == $group_by_option)
245 array_push($fields, 'c.name as client');
246 // Add project name if it is selected.
247 if ($options['show_project'] || 'project' == $group_by_option)
248 array_push($fields, 'p.name as project');
249 if ($options['show_task'] || 'task' == $group_by_option)
250 array_push($fields, 'null'); // null for task name. We need to match column count for union.
251 if ($options['show_custom_field_1'] || 'cf_1' == $group_by_option)
252 array_push($fields, 'null'); // null for cf_1.
253 if ($options['show_start']) {
254 array_push($fields, 'null'); // null for unformatted_start.
255 array_push($fields, 'null'); // null for start.
257 if ($options['show_end'])
258 array_push($fields, 'null'); // null for finish.
259 if ($options['show_duration'])
260 array_push($fields, 'null'); // null for duration.
261 if ($options['show_work_units'])
262 array_push($fields, 'null as units'); // null for work units.
263 // Use the note field to print item name.
264 if ($options['show_note'])
265 array_push($fields, 'ei.name as note');
266 array_push($fields, 'ei.cost as cost');
267 array_push($fields, 'ei.cost as expense');
269 if ($canViewReports && $options['show_paid'])
270 array_push($fields, 'ei.paid as paid');
272 if ($canViewReports && $options['show_ip']) {
273 array_push($fields, 'ei.created as created');
274 array_push($fields, 'ei.created_ip as created_ip');
275 array_push($fields, 'ei.modified as modified');
276 array_push($fields, 'ei.modified_ip as modified_ip');
278 // Add invoice name if it is selected.
279 if (($canViewReports || $isClient) && $options['show_invoice'])
280 array_push($fields, 'i.name as invoice');
282 // Prepare sql query part for left joins.
284 if ($canViewReports || $isClient)
285 $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
286 if ($options['show_client'] || 'client' == $group_by_option)
287 $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
288 if ($options['show_project'] || 'project' == $group_by_option)
289 $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
290 if (($canViewReports || $isClient) && $options['show_invoice'])
291 $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
293 $where = ttReportHelper::getExpenseWhere($options);
295 // Construct sql query for expense items.
296 $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
298 // Construct a union.
299 $sql = "($sql) union all ($sql_for_expense_items)";
302 // Determine sort part.
303 $sort_part = ' order by ';
304 if ($group_by_option == null || 'no_grouping' == $group_by_option || 'date' == $group_by_option)
305 $sort_part .= 'date';
307 $sort_part .= $group_by_option.', date';
308 if (($canViewReports || $isClient) && $options['users'] && 'user' != $group_by_option)
309 $sort_part .= ', user, type';
310 if ($options['show_start'])
311 $sort_part .= ', unformatted_start';
312 $sort_part .= ', id';
315 // By now we are ready with sql.
317 // Obtain items for report.
318 $res = $mdb2->query($sql);
319 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
321 while ($val = $res->fetchRow()) {
322 if ($convertTo12Hour) {
323 if($val['start'] != '')
324 $val['start'] = ttTimeHelper::to12HourFormat($val['start']);
325 if($val['finish'] != '')
326 $val['finish'] = ttTimeHelper::to12HourFormat($val['finish']);
328 if (isset($val['cost'])) {
329 if ('.' != $user->decimal_mark)
330 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
332 if (isset($val['expense'])) {
333 if ('.' != $user->decimal_mark)
334 $val['expense'] = str_replace('.', $user->decimal_mark, $val['expense']);
336 if ('no_grouping' != $group_by_option) {
337 $val['grouped_by'] = $val[$group_by_option];
338 if ('date' == $group_by_option) {
339 // This is needed to get the date in user date format.
340 $o_date = new DateAndTime(DB_DATEFORMAT, $val['grouped_by']);
341 $val['grouped_by'] = $o_date->toString($user->date_format);
346 // This is needed to get the date in user date format.
347 $o_date = new DateAndTime(DB_DATEFORMAT, $val['date']);
348 $val['date'] = $o_date->toString($user->date_format);
352 $report_items[] = $row;
355 return $report_items;
358 // putInSession stores tt_log and tt_expense_items ids from a report in user session
359 // as 2 comma-separated lists.
360 static function putInSession($report_items) {
361 unset($_SESSION['report_item_ids']);
362 unset($_SESSION['report_item_expense_ids']);
364 // Iterate through records and build 2 comma-separated lists.
365 foreach($report_items as $item) {
366 if ($item['type'] == 1)
367 $report_item_ids .= ','.$item['id'];
368 else if ($item['type'] == 2)
369 $report_item_expense_ids .= ','.$item['id'];
371 $report_item_ids = trim($report_item_ids, ',');
372 $report_item_expense_ids = trim($report_item_expense_ids, ',');
374 // The lists are reqdy. Put them in session.
375 if ($report_item_ids) $_SESSION['report_item_ids'] = $report_item_ids;
376 if ($report_item_expense_ids) $_SESSION['report_item_expense_ids'] = $report_item_expense_ids;
379 // getFromSession obtains tt_log and tt_expense_items ids stored in user session.
380 static function getFromSession() {
382 $report_item_ids = $_SESSION['report_item_ids'];
383 if ($report_item_ids)
384 $items['report_item_ids'] = explode(',', $report_item_ids);
385 $report_item_expense_ids = $_SESSION['report_item_expense_ids'];
386 if ($report_item_expense_ids)
387 $items['report_item_expense_ids'] = explode(',', $report_item_expense_ids);
391 // getSubtotals calculates report items subtotals when a report is grouped by.
392 // Without expenses, it's a simple select with group by.
393 // With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
394 static function getSubtotals($bean, $options) {
397 $group_by_option = $bean->getAttribute('group_by');
398 if ('no_grouping' == $group_by_option) return null;
400 $mdb2 = getConnection();
402 // Start with sql to obtain subtotals for time items. This simple sql will be used when we have no expenses.
404 // Determine group by field and a required join.
405 switch ($group_by_option) {
407 $group_field = 'l.date';
411 $group_field = 'u.name';
412 $group_join = 'left join tt_users u on (l.user_id = u.id) ';
415 $group_field = 'c.name';
416 $group_join = 'left join tt_clients c on (l.client_id = c.id) ';
419 $group_field = 'p.name';
420 $group_join = 'left join tt_projects p on (l.project_id = p.id) ';
423 $group_field = 't.name';
424 $group_join = 'left join tt_tasks t on (l.task_id = t.id) ';
427 $group_field = 'cfo.value';
428 $custom_fields = new CustomFields($user->group_id);
429 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
430 $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) ';
431 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
432 $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) ';
436 $where = ttReportHelper::getWhere($options);
437 if ($bean->getAttribute('chcost')) {
438 if (MODE_TIME == $user->tracking_mode) {
439 if ($group_by_option != 'user')
440 $left_join = 'left join tt_users u on (l.user_id = u.id)';
441 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
442 if ($bean->getAttribute('chunits')) {
443 if ($user->unit_totals_only)
444 $sql .= ", if (sum(l.billable * time_to_sec(l.duration)/60) < $user->first_unit_threshold, 0, ceil(sum(l.billable * time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
446 $sql .= ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
448 $sql .= ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
449 null as expenses from tt_log l
450 $group_join $left_join $where group by $group_field";
452 // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
453 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
454 if ($bean->getAttribute('chunits')) {
455 if ($user->unit_totals_only)
456 $sql .= ", if (sum(l.billable * time_to_sec(l.duration)/60) < $user->first_unit_threshold, 0, ceil(sum(l.billable * time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
458 $sql .= ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
460 $sql .= ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
461 null as expenses from tt_log l
463 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";
466 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
467 if ($bean->getAttribute('chunits')) {
468 if ($user->unit_totals_only)
469 $sql .= ", if (sum(l.billable * time_to_sec(l.duration)/60) < $user->first_unit_threshold, 0, ceil(sum(l.billable * time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
471 $sql .= ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
473 $sql .= ", null as expenses from tt_log l
474 $group_join $where group by $group_field";
476 // By now we have sql for time items.
478 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
479 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
481 // Determine group by field and a required join.
483 $group_field = 'null';
484 switch ($group_by_option) {
486 $group_field = 'ei.date';
490 $group_field = 'u.name';
491 $group_join = 'left join tt_users u on (ei.user_id = u.id) ';
494 $group_field = 'c.name';
495 $group_join = 'left join tt_clients c on (ei.client_id = c.id) ';
498 $group_field = 'p.name';
499 $group_join = 'left join tt_projects p on (ei.project_id = p.id) ';
503 $where = ttReportHelper::getExpenseWhere($options);
504 $sql_for_expenses = "select $group_field as group_field, null as time";
505 if ($bean->getAttribute('chunits')) $sql_for_expenses .= ", null as units";
506 $sql_for_expenses .= ", sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei $group_join $where";
507 // Add a "group by" clause if we are grouping.
508 if ('null' != $group_field) $sql_for_expenses .= " group by $group_field";
510 // Create a combined query.
511 $combined = "select group_field, sum(time) as time";
512 if ($bean->getAttribute('chunits')) $combined .= ", sum(units) as units";
513 $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by group_field";
518 $res = $mdb2->query($sql);
519 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
521 while ($val = $res->fetchRow()) {
522 if ('date' == $group_by_option) {
523 // This is needed to get the date in user date format.
524 $o_date = new DateAndTime(DB_DATEFORMAT, $val['group_field']);
525 $val['group_field'] = $o_date->toString($user->date_format);
528 $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
529 if ($bean->getAttribute('chcost')) {
530 if ('.' != $user->decimal_mark) {
531 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
532 $val['expenses'] = str_replace('.', $user->decimal_mark, $val['expenses']);
534 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time, 'units'=> $val['units'],'cost'=>$val['cost'],'expenses'=>$val['expenses']);
536 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time, 'units'=> $val['units']);
542 // getFavSubtotals calculates report items subtotals when a favorite report is grouped by.
543 // Without expenses, it's a simple select with group by.
544 // With expenses, it becomes a select with group by from a combined set of records obtained with "union all".
545 static function getFavSubtotals($options) {
548 $group_by_option = $options['group_by'];
549 if ('no_grouping' == $group_by_option) return null;
551 $mdb2 = getConnection();
553 // Start with sql to obtain subtotals for time items. This simple sql will be used when we have no expenses.
555 // Determine group by field and a required join.
556 switch ($group_by_option) {
558 $group_field = 'l.date';
562 $group_field = 'u.name';
563 $group_join = 'left join tt_users u on (l.user_id = u.id) ';
566 $group_field = 'c.name';
567 $group_join = 'left join tt_clients c on (l.client_id = c.id) ';
570 $group_field = 'p.name';
571 $group_join = 'left join tt_projects p on (l.project_id = p.id) ';
574 $group_field = 't.name';
575 $group_join = 'left join tt_tasks t on (l.task_id = t.id) ';
578 $group_field = 'cfo.value';
579 $custom_fields = new CustomFields($user->group_id);
580 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
581 $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) ';
582 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
583 $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) ';
587 $where = ttReportHelper::getWhere($options);
588 if ($options['show_cost']) {
589 if (MODE_TIME == $user->tracking_mode) {
590 if ($group_by_option != 'user')
591 $left_join = 'left join tt_users u on (l.user_id = u.id)';
592 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
593 if ($options['show_work_units']) {
594 if ($user->unit_totals_only)
595 $sql .= ", if (sum(l.billable * time_to_sec(l.duration)/60) < $user->first_unit_threshold, 0, ceil(sum(l.billable * time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
597 $sql .= ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
599 $sql .= ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
600 $sql .= ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
601 null as expenses from tt_log l
602 $group_join $left_join $where group by $group_field";
604 // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
605 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
606 if ($options['show_work_units']) {
607 if ($user->unit_totals_only)
608 $sql .= ", if (sum(l.billable * time_to_sec(l.duration)/60) < $user->first_unit_threshold, 0, ceil(sum(l.billable * time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
610 $sql .= ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
612 $sql .= ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
613 null as expenses from tt_log l
615 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";
618 $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
619 if ($options['show_work_units']) {
620 if ($user->unit_totals_only)
621 $sql .= ", if (sum(l.billable * time_to_sec(l.duration)/60) < $user->first_unit_threshold, 0, ceil(sum(l.billable * time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
623 $sql .= ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
625 $sql .= ", null as expenses from tt_log l
626 $group_join $where group by $group_field";
628 // By now we have sql for time items.
630 // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
631 if ($options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
633 // Determine group by field and a required join.
635 $group_field = 'null';
636 switch ($group_by_option) {
638 $group_field = 'ei.date';
642 $group_field = 'u.name';
643 $group_join = 'left join tt_users u on (ei.user_id = u.id) ';
646 $group_field = 'c.name';
647 $group_join = 'left join tt_clients c on (ei.client_id = c.id) ';
650 $group_field = 'p.name';
651 $group_join = 'left join tt_projects p on (ei.project_id = p.id) ';
655 $where = ttReportHelper::getExpenseWhere($options);
656 $sql_for_expenses = "select $group_field as group_field, null as time";
657 if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
658 $sql_for_expenses .= ", sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei $group_join $where";
659 // Add a "group by" clause if we are grouping.
660 if ('null' != $group_field) $sql_for_expenses .= " group by $group_field";
662 // Create a combined query.
663 $combined = "select group_field, sum(time) as time";
664 if ($options['show_work_units']) $combined .= ", sum(units) as units";
665 $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by group_field";
670 $res = $mdb2->query($sql);
671 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
673 while ($val = $res->fetchRow()) {
674 if ('date' == $group_by_option) {
675 // This is needed to get the date in user date format.
676 $o_date = new DateAndTime(DB_DATEFORMAT, $val['group_field']);
677 $val['group_field'] = $o_date->toString($user->date_format);
680 $time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
681 if ($options['show_cost']) {
682 if ('.' != $user->decimal_mark) {
683 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
684 $val['expenses'] = str_replace('.', $user->decimal_mark, $val['expenses']);
686 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time, 'units'=> $val['units'], 'cost'=>$val['cost'],'expenses'=>$val['expenses']);
688 $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time, 'units'=> $val['units']);
694 // getTotals calculates total hours and cost for all report items.
695 static function getTotals($bean, $options)
699 $mdb2 = getConnection();
701 $where = ttReportHelper::getWhere($options);
704 $time_part = "sum(time_to_sec(l.duration)) as time";
705 if ($bean->getAttribute('chunits')) {
706 $units_part = $user->unit_totals_only ? ", null as units" : ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
708 if ($bean->getAttribute('chcost')) {
709 if (MODE_TIME == $user->tracking_mode)
710 $cost_part = ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost, null as expenses";
712 $cost_part = ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost, null as expenses";
714 $cost_part = ", null as cost, null as expenses";
716 if ($bean->getAttribute('chcost')) {
717 if (MODE_TIME == $user->tracking_mode) {
718 $left_joins = "left join tt_users u on (l.user_id = u.id)";
720 $left_joins = "left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
723 // Prepare a query for time items.
724 $sql = "select $time_part $units_part $cost_part from tt_log l $left_joins $where";
726 // If we have expenses, query becomes a bit more complex.
727 if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) {
728 $where = ttReportHelper::getExpenseWhere($options);
729 $sql_for_expenses = "select null as time";
730 if ($bean->getAttribute('chunits')) $sql_for_expenses .= ", null as units";
731 $sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
733 // Create a combined query.
734 $combined = "select sum(time) as time";
735 if ($bean->getAttribute('chunits')) $combined .= ", sum(units) as units";
736 $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
741 $res = $mdb2->query($sql);
742 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
744 $val = $res->fetchRow();
745 $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
746 if ($bean->getAttribute('chcost')) {
747 $total_cost = $val['cost'];
748 if (!$total_cost) $total_cost = '0.00';
749 if ('.' != $user->decimal_mark)
750 $total_cost = str_replace('.', $user->decimal_mark, $total_cost);
751 $total_expenses = $val['expenses'];
752 if (!$total_expenses) $total_expenses = '0.00';
753 if ('.' != $user->decimal_mark)
754 $total_expenses = str_replace('.', $user->decimal_mark, $total_expenses);
757 if ($bean->getAttribute('period'))
758 $period = new Period($bean->getAttribute('period'), new DateAndTime($user->date_format));
760 $period = new Period();
762 new DateAndTime($user->date_format, $bean->getAttribute('start_date')),
763 new DateAndTime($user->date_format, $bean->getAttribute('end_date')));
766 $totals['start_date'] = $period->getStartDate();
767 $totals['end_date'] = $period->getEndDate();
768 $totals['time'] = $total_time;
769 $totals['units'] = $val['units'];
770 $totals['cost'] = $total_cost;
771 $totals['expenses'] = $total_expenses;
776 // getFavTotals calculates total hours and cost for all favorite report items.
777 static function getFavTotals($options)
781 $mdb2 = getConnection();
783 $where = ttReportHelper::getWhere($options);
786 $time_part = "sum(time_to_sec(l.duration)) as time";
787 if ($options['show_work_units']) {
788 $units_part = $user->unit_totals_only ? ", null as units" : ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit))) as units";
790 if ($options['show_cost']) {
791 if (MODE_TIME == $user->tracking_mode)
792 $cost_part = ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost, null as expenses";
794 $cost_part = ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost, null as expenses";
796 $cost_part = ", null as cost, null as expenses";
798 if ($options['show_cost']) {
799 if (MODE_TIME == $user->tracking_mode) {
800 $left_joins = "left join tt_users u on (l.user_id = u.id)";
802 $left_joins = "left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
805 // Prepare a query for time items.
806 $sql = "select $time_part $units_part $cost_part from tt_log l $left_joins $where";
808 // If we have expenses, query becomes a bit more complex.
809 if ($options['show_cost'] && $user->isPluginEnabled('ex')) {
810 $where = ttReportHelper::getExpenseWhere($options);
811 $sql_for_expenses = "select null as time";
812 if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
813 $sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
815 // Create a combined query.
816 $combined = "select sum(time) as time";
817 if ($options['show_work_units']) $combined .= ", sum(units) as units";
818 $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
823 $res = $mdb2->query($sql);
824 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
826 $val = $res->fetchRow();
827 $total_time = $val['time'] ? sec_to_time_fmt_hm($val['time']) : null;
828 if ($options['show_cost']) {
829 $total_cost = $val['cost'];
830 if (!$total_cost) $total_cost = '0.00';
831 if ('.' != $user->decimal_mark)
832 $total_cost = str_replace('.', $user->decimal_mark, $total_cost);
833 $total_expenses = $val['expenses'];
834 if (!$total_expenses) $total_expenses = '0.00';
835 if ('.' != $user->decimal_mark)
836 $total_expenses = str_replace('.', $user->decimal_mark, $total_expenses);
839 if ($options['period'])
840 $period = new Period($options['period'], new DateAndTime($user->date_format));
842 $period = new Period();
844 new DateAndTime($user->date_format, $options['period_start']),
845 new DateAndTime($user->date_format, $options['period_end']));
848 $totals['start_date'] = $period->getStartDate();
849 $totals['end_date'] = $period->getEndDate();
850 $totals['time'] = $total_time;
851 $totals['units'] = $val['units'];
852 $totals['cost'] = $total_cost;
853 $totals['expenses'] = $total_expenses;
858 // The assignToInvoice assigns a set of records to a specific invoice.
859 static function assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids)
861 $mdb2 = getConnection();
863 $sql = "update tt_log set invoice_id = ".$mdb2->quote($invoice_id).
864 " where id in(".join(', ', $time_log_ids).")";
865 $affected = $mdb2->exec($sql);
866 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
868 if ($expense_item_ids) {
869 $sql = "update tt_expense_items set invoice_id = ".$mdb2->quote($invoice_id).
870 " where id in(".join(', ', $expense_item_ids).")";
871 $affected = $mdb2->exec($sql);
872 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
876 // The markPaid marks a set of records as either paid or unpaid.
877 static function markPaid($time_log_ids, $expense_item_ids, $paid = true)
879 $mdb2 = getConnection();
880 $paid_val = (int) $paid;
882 $sql = "update tt_log set paid = $paid_val where id in(".join(', ', $time_log_ids).")";
883 $affected = $mdb2->exec($sql);
884 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
886 if ($expense_item_ids) {
887 $sql = "update tt_expense_items set paid = $paid_val where id in(".join(', ', $expense_item_ids).")";
888 $affected = $mdb2->exec($sql);
889 if (is_a($affected, 'PEAR_Error')) die($affected->getMessage());
893 // prepareReportBody - prepares an email body for report.
894 static function prepareReportBody($bean, $comment)
899 // Determine these once as they are used in multiple places in this function.
900 $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
901 $isClient = $user->isClient();
902 $options = ttReportHelper::getReportOptions($bean);
904 $items = ttReportHelper::getItems($options);
905 $group_by = $bean->getAttribute('group_by');
906 if ($group_by && 'no_grouping' != $group_by)
907 $subtotals = ttReportHelper::getSubtotals($bean, $options);
908 $totals = ttReportHelper::getTotals($bean, $options);
910 // Use custom fields plugin if it is enabled.
911 if ($user->isPluginEnabled('cf'))
912 $custom_fields = new CustomFields($user->group_id);
914 // Define some styles to use in email.
915 $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
916 $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
917 $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
918 $rowItem = 'background-color: #ffffff;';
919 $rowItemAlt = 'background-color: #f5f5f5;';
920 $rowSubtotal = 'background-color: #e0e0e0;';
921 $cellLeftAligned = 'text-align: left; vertical-align: top;';
922 $cellRightAligned = 'text-align: right; vertical-align: top;';
923 $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
924 $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
926 // Start creating email body.
928 $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
932 $body .= '<p style="'.$style_title.'">'.$i18n->get('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
935 if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>';
937 if ($bean->getAttribute('chtotalsonly')) {
938 // Totals only report. Output subtotals.
940 // Determine group_by header.
941 if ('cf_1' == $group_by)
942 $group_by_header = htmlspecialchars($custom_fields->fields[0]['label']);
944 $key = 'label.'.$group_by;
945 $group_by_header = $i18n->get($key);
948 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
950 $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
951 if ($bean->getAttribute('chduration'))
952 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
953 if ($bean->getAttribute('chunits'))
954 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
955 if ($bean->getAttribute('chcost'))
956 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
958 foreach($subtotals as $subtotal) {
959 $body .= '<tr style="'.$rowSubtotal.'">';
960 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : ' ').'</td>';
961 if ($bean->getAttribute('chduration')) {
962 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
963 if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
966 if ($bean->getAttribute('chunits')) {
967 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
968 $body .= $subtotal['units'];
971 if ($bean->getAttribute('chcost')) {
972 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
973 $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
980 $body .= '<tr><td> </td></tr>';
981 $body .= '<tr style="'.$rowSubtotal.'">';
982 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
983 if ($bean->getAttribute('chduration')) {
984 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
985 if ($totals['time'] <> '0:00') $body .= $totals['time'];
988 if ($bean->getAttribute('chunits')) {
989 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
990 $body .= $totals['units'];
993 if ($bean->getAttribute('chcost')) {
994 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
995 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1000 $body .= '</table>';
1004 // Print table header.
1005 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1007 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.date').'</td>';
1008 if ($canViewReports || $isClient)
1009 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.user').'</td>';
1010 if ($bean->getAttribute('chclient'))
1011 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.client').'</td>';
1012 if ($bean->getAttribute('chproject'))
1013 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.project').'</td>';
1014 if ($bean->getAttribute('chtask'))
1015 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.task').'</td>';
1016 if ($bean->getAttribute('chcf_1'))
1017 $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
1018 if ($bean->getAttribute('chstart'))
1019 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.start').'</td>';
1020 if ($bean->getAttribute('chfinish'))
1021 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.finish').'</td>';
1022 if ($bean->getAttribute('chduration'))
1023 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
1024 if ($bean->getAttribute('chunits'))
1025 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
1026 if ($bean->getAttribute('chnote'))
1027 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.note').'</td>';
1028 if ($bean->getAttribute('chcost'))
1029 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
1030 if ($bean->getAttribute('chpaid'))
1031 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.paid').'</td>';
1032 if ($bean->getAttribute('chip'))
1033 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.ip').'</td>';
1034 if ($bean->getAttribute('chinvoice'))
1035 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.invoice').'</td>';
1038 // Initialize variables to print subtotals.
1039 if ($items && 'no_grouping' != $group_by) {
1040 $print_subtotals = true;
1042 $prev_grouped_by = '';
1043 $cur_grouped_by = '';
1045 // Initialize variables to alternate color of rows for different dates.
1048 $row_style = $rowItem;
1050 // Print report items.
1051 if (is_array($items)) {
1052 foreach ($items as $record) {
1053 $cur_date = $record['date'];
1054 // Print a subtotal row after a block of grouped items.
1055 if ($print_subtotals) {
1056 $cur_grouped_by = $record['grouped_by'];
1057 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
1058 $body .= '<tr style="'.$rowSubtotal.'">';
1059 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
1060 $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
1061 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1062 if ($bean->getAttribute('chclient')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1063 if ($bean->getAttribute('chproject')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1064 if ($bean->getAttribute('chtask')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1065 if ($bean->getAttribute('chcf_1')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1066 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1067 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1068 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
1069 if ($bean->getAttribute('chunits')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['units'].'</td>';
1070 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1071 if ($bean->getAttribute('chcost')) {
1072 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1073 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
1076 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1077 if ($bean->getAttribute('chip')) $body .= '<td></td>';
1078 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1080 $body .= '<tr><td> </td></tr>';
1082 $first_pass = false;
1085 // Print a regular row.
1086 if ($cur_date != $prev_date)
1087 $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
1088 $body .= '<tr style="'.$row_style.'">';
1089 $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
1090 if ($canViewReports || $isClient)
1091 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
1092 if ($bean->getAttribute('chclient'))
1093 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
1094 if ($bean->getAttribute('chproject'))
1095 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
1096 if ($bean->getAttribute('chtask'))
1097 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
1098 if ($bean->getAttribute('chcf_1'))
1099 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
1100 if ($bean->getAttribute('chstart'))
1101 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
1102 if ($bean->getAttribute('chfinish'))
1103 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
1104 if ($bean->getAttribute('chduration'))
1105 $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
1106 if ($bean->getAttribute('chunits'))
1107 $body .= '<td style="'.$cellRightAligned.'">'.$record['units'].'</td>';
1108 if ($bean->getAttribute('chnote'))
1109 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
1110 if ($bean->getAttribute('chcost'))
1111 $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
1112 if ($bean->getAttribute('chpaid')) {
1113 $body .= '<td style="'.$cellRightAligned.'">';
1114 $body .= $record['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
1117 if ($bean->getAttribute('chip')) {
1118 $body .= '<td style="'.$cellRightAligned.'">';
1119 $body .= $record['modified'] ? $record['modified_ip'].' '.$record['modified'] : $record['created_ip'].' '.$record['created'];
1122 if ($bean->getAttribute('chinvoice'))
1123 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
1126 $prev_date = $record['date'];
1127 if ($print_subtotals)
1128 $prev_grouped_by = $record['grouped_by'];
1132 // Print a terminating subtotal.
1133 if ($print_subtotals) {
1134 $body .= '<tr style="'.$rowSubtotal.'">';
1135 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
1136 $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
1137 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1138 if ($bean->getAttribute('chclient')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1139 if ($bean->getAttribute('chproject')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1140 if ($bean->getAttribute('chtask')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1141 if ($bean->getAttribute('chcf_1')) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1142 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1143 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1144 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
1145 if ($bean->getAttribute('chunits')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['units'].'</td>';
1146 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1147 if ($bean->getAttribute('chcost')) {
1148 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1149 $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
1152 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1153 if ($bean->getAttribute('chip')) $body .= '<td></td>';
1154 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1159 $body .= '<tr><td> </td></tr>';
1160 $body .= '<tr style="'.$rowSubtotal.'">';
1161 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
1162 if ($canViewReports || $isClient) $body .= '<td></td>';
1163 if ($bean->getAttribute('chclient')) $body .= '<td></td>';
1164 if ($bean->getAttribute('chproject')) $body .= '<td></td>';
1165 if ($bean->getAttribute('chtask')) $body .= '<td></td>';
1166 if ($bean->getAttribute('chcf_1')) $body .= '<td></td>';
1167 if ($bean->getAttribute('chstart')) $body .= '<td></td>';
1168 if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
1169 if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
1170 if ($bean->getAttribute('chunits')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['units'].'</td>';
1171 if ($bean->getAttribute('chnote')) $body .= '<td></td>';
1172 if ($bean->getAttribute('chcost')) {
1173 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1174 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1177 if ($bean->getAttribute('chpaid')) $body .= '<td></td>';
1178 if ($bean->getAttribute('chip')) $body .= '<td></td>';
1179 if ($bean->getAttribute('chinvoice')) $body .= '<td></td>';
1182 $body .= '</table>';
1186 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
1187 $body .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
1189 // Finish creating email body.
1190 $body .= '</body></html>';
1195 // checkFavReportCondition - checks whether it is okay to send fav report.
1196 static function checkFavReportCondition($options, $condition)
1198 $items = ttReportHelper::getItems($options);
1200 $condition = str_replace('count', '', $condition);
1201 $count_required = (int) trim(str_replace('>', '', $condition));
1203 if (count($items) > $count_required)
1204 return true; // Condition ok.
1209 // prepareFavReportBody - prepares an email body for a favorite report.
1210 static function prepareFavReportBody($options)
1215 // Determine these once as they are used in multiple places in this function.
1216 $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
1217 $isClient = $user->isClient();
1219 $items = ttReportHelper::getItems($options);
1220 $group_by = $options['group_by'];
1221 if ($group_by && 'no_grouping' != $group_by)
1222 $subtotals = ttReportHelper::getFavSubtotals($options);
1223 $totals = ttReportHelper::getFavTotals($options);
1225 // Use custom fields plugin if it is enabled.
1226 if ($user->isPluginEnabled('cf'))
1227 $custom_fields = new CustomFields($user->group_id);
1229 // Define some styles to use in email.
1230 $style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
1231 $tableHeader = 'font-weight: bold; background-color: #a6ccf7; text-align: left;';
1232 $tableHeaderCentered = 'font-weight: bold; background-color: #a6ccf7; text-align: center;';
1233 $rowItem = 'background-color: #ffffff;';
1234 $rowItemAlt = 'background-color: #f5f5f5;';
1235 $rowSubtotal = 'background-color: #e0e0e0;';
1236 $cellLeftAligned = 'text-align: left; vertical-align: top;';
1237 $cellRightAligned = 'text-align: right; vertical-align: top;';
1238 $cellLeftAlignedSubtotal = 'font-weight: bold; text-align: left; vertical-align: top;';
1239 $cellRightAlignedSubtotal = 'font-weight: bold; text-align: right; vertical-align: top;';
1241 // Start creating email body.
1243 $body .= '<head><meta http-equiv="content-type" content="text/html; charset='.CHARSET.'"></head>';
1247 $body .= '<p style="'.$style_title.'">'.$i18n->get('form.mail.report_subject').': '.$totals['start_date'].' - '.$totals['end_date'].'</p>';
1250 // if ($comment) $body .= '<p>'.htmlspecialchars($comment).'</p>'; // No comment for fav. reports.
1252 if ($options['show_totals_only']) {
1253 // Totals only report. Output subtotals.
1255 // Determine group_by header.
1256 if ('cf_1' == $group_by)
1257 $group_by_header = htmlspecialchars($custom_fields->fields[0]['label']);
1259 $key = 'label.'.$group_by;
1260 $group_by_header = $i18n->get($key);
1263 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1265 $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
1266 if ($options['show_duration'])
1267 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
1268 if ($options['show_work_units'])
1269 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
1270 if ($options['show_cost'])
1271 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
1273 foreach($subtotals as $subtotal) {
1274 $body .= '<tr style="'.$rowSubtotal.'">';
1275 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($subtotal['name'] ? htmlspecialchars($subtotal['name']) : ' ').'</td>';
1276 if ($options['show_duration']) {
1277 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1278 if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
1281 if ($options['show_work_units']) {
1282 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1283 $body .= $subtotal['units'];
1286 if ($options['show_cost']) {
1287 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1288 $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
1295 $body .= '<tr><td> </td></tr>';
1296 $body .= '<tr style="'.$rowSubtotal.'">';
1297 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
1298 if ($options['show_duration']) {
1299 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1300 if ($totals['time'] <> '0:00') $body .= $totals['time'];
1303 if ($options['show_work_units']) {
1304 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1305 $body .= $totals['units'];
1308 if ($options['show_cost']) {
1309 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1310 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1315 $body .= '</table>';
1319 // Print table header.
1320 $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
1322 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.date').'</td>';
1323 if ($canViewReports || $isClient)
1324 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.user').'</td>';
1325 if ($options['show_client'])
1326 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.client').'</td>';
1327 if ($options['show_project'])
1328 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.project').'</td>';
1329 if ($options['show_task'])
1330 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.task').'</td>';
1331 if ($options['show_custom_field_1'])
1332 $body .= '<td style="'.$tableHeader.'">'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>';
1333 if ($options['show_start'])
1334 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.start').'</td>';
1335 if ($options['show_end'])
1336 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.finish').'</td>';
1337 if ($options['show_duration'])
1338 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
1339 if ($options['show_work_units'])
1340 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
1341 if ($options['show_note'])
1342 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.note').'</td>';
1343 if ($options['show_cost'])
1344 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
1345 if ($options['show_paid'])
1346 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.paid').'</td>';
1347 if ($options['show_ip'])
1348 $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.ip').'</td>';
1349 if ($options['show_invoice'])
1350 $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.invoice').'</td>';
1353 // Initialize variables to print subtotals.
1354 if ($items && 'no_grouping' != $group_by) {
1355 $print_subtotals = true;
1357 $prev_grouped_by = '';
1358 $cur_grouped_by = '';
1360 // Initialize variables to alternate color of rows for different dates.
1363 $row_style = $rowItem;
1365 // Print report items.
1366 if (is_array($items)) {
1367 foreach ($items as $record) {
1368 $cur_date = $record['date'];
1369 // Print a subtotal row after a block of grouped items.
1370 if ($print_subtotals) {
1371 $cur_grouped_by = $record['grouped_by'];
1372 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
1373 $body .= '<tr style="'.$rowSubtotal.'">';
1374 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
1375 $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
1376 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1377 if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1378 if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1379 if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1380 if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1381 if ($options['show_start']) $body .= '<td></td>';
1382 if ($options['show_end']) $body .= '<td></td>';
1383 if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
1384 if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['units'].'</td>';
1385 if ($options['show_note']) $body .= '<td></td>';
1386 if ($options['show_cost']) {
1387 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1388 $body .= ($canViewReports || $isClient) ? $subtotals[$prev_grouped_by]['cost'] : $subtotals[$prev_grouped_by]['expenses'];
1391 if ($options['show_paid']) $body .= '<td></td>';
1392 if ($options['show_ip']) $body .= '<td></td>';
1393 if ($options['show_invoice']) $body .= '<td></td>';
1395 $body .= '<tr><td> </td></tr>';
1397 $first_pass = false;
1400 // Print a regular row.
1401 if ($cur_date != $prev_date)
1402 $row_style = ($row_style == $rowItem) ? $rowItemAlt : $rowItem;
1403 $body .= '<tr style="'.$row_style.'">';
1404 $body .= '<td style="'.$cellLeftAligned.'">'.$record['date'].'</td>';
1405 if ($canViewReports || $isClient)
1406 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['user']).'</td>';
1407 if ($options['show_client'])
1408 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['client']).'</td>';
1409 if ($options['show_project'])
1410 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['project']).'</td>';
1411 if ($options['show_task'])
1412 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['task']).'</td>';
1413 if ($options['show_custom_field_1'])
1414 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['cf_1']).'</td>';
1415 if ($options['show_start'])
1416 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['start'].'</td>';
1417 if ($options['show_end'])
1418 $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
1419 if ($options['show_duration'])
1420 $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
1421 if ($options['show_work_units'])
1422 $body .= '<td style="'.$cellRightAligned.'">'.$record['units'].'</td>';
1423 if ($options['show_note'])
1424 $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
1425 if ($options['show_cost'])
1426 $body .= '<td style="'.$cellRightAligned.'">'.$record['cost'].'</td>';
1427 if ($options['show_paid']) {
1428 $body .= '<td style="'.$cellRightAligned.'">';
1429 $body .= $record['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
1432 if ($options['show_ip']) {
1433 $body .= '<td style="'.$cellRightAligned.'">';
1434 $body .= $record['modified'] ? $record['modified_ip'].' '.$record['modified'] : $record['created_ip'].' '.$record['created'];
1437 if ($options['show_invoice'])
1438 $body .= '<td style="'.$cellRightAligned.'">'.htmlspecialchars($record['invoice']).'</td>';
1441 $prev_date = $record['date'];
1442 if ($print_subtotals)
1443 $prev_grouped_by = $record['grouped_by'];
1447 // Print a terminating subtotal.
1448 if ($print_subtotals) {
1449 $body .= '<tr style="'.$rowSubtotal.'">';
1450 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
1451 $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
1452 if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
1453 if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
1454 if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
1455 if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
1456 if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
1457 if ($options['show_start']) $body .= '<td></td>';
1458 if ($options['show_end']) $body .= '<td></td>';
1459 if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
1460 if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['units'].'</td>';
1461 if ($options['show_note']) $body .= '<td></td>';
1462 if ($options['show_cost']) {
1463 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
1464 $body .= ($canViewReports || $isClient) ? $subtotals[$cur_grouped_by]['cost'] : $subtotals[$cur_grouped_by]['expenses'];
1467 if ($options['show_paid']) $body .= '<td></td>';
1468 if ($options['show_ip']) $body .= '<td></td>';
1469 if ($options['show_invoice']) $body .= '<td></td>';
1474 $body .= '<tr><td> </td></tr>';
1475 $body .= '<tr style="'.$rowSubtotal.'">';
1476 $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.total').'</td>';
1477 if ($canViewReports || $isClient) $body .= '<td></td>';
1478 if ($options['show_client']) $body .= '<td></td>';
1479 if ($options['show_project']) $body .= '<td></td>';
1480 if ($options['show_task']) $body .= '<td></td>';
1481 if ($options['show_custom_field_1']) $body .= '<td></td>';
1482 if ($options['show_start']) $body .= '<td></td>';
1483 if ($options['show_end']) $body .= '<td></td>';
1484 if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
1485 if ($options['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['units'].'</td>';
1486 if ($options['show_note']) $body .= '<td></td>';
1487 if ($options['show_cost']) {
1488 $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
1489 $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
1492 if ($options['show_paid']) $body .= '<td></td>';
1493 if ($options['show_ip']) $body .= '<td></td>';
1494 if ($options['show_invoice']) $body .= '<td></td>';
1497 $body .= '</table>';
1501 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false))
1502 $body .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
1504 // Finish creating email body.
1505 $body .= '</body></html>';
1510 // sendFavReport - sends a favorite report to a specified email, called from cron.php
1511 static function sendFavReport($options, $subject, $email, $cc) {
1512 // We are called from cron.php, we have no $bean in session.
1513 // cron.php sets global $user and $i18n objects to match our favorite report user.
1517 // Prepare report body.
1518 $body = ttReportHelper::prepareFavReportBody($options);
1520 import('mail.Mailer');
1521 $mailer = new Mailer();
1522 $mailer->setCharSet(CHARSET);
1523 $mailer->setContentType('text/html');
1524 $mailer->setSender(SENDER);
1526 $mailer->setReceiverCC($cc);
1527 if (!empty($user->bcc_email))
1528 $mailer->setReceiverBCC($user->bcc_email);
1529 $mailer->setReceiver($email);
1530 $mailer->setMailMode(MAIL_MODE);
1531 if (empty($subject)) $subject = $options['name'];
1532 if (!$mailer->send($subject, $body))
1538 // getReportOptions - returns an array of report options constructed from session bean.
1540 // Note: similarly to ttFavReportHelper::getReportOptions, this function is a part of
1541 // refactoring to simplify maintenance of report generating functions, as we currently
1542 // have 2 sets: normal reporting (from bean), and fav report emailing (from db fields).
1543 // Using options obtained from either db or bean shall allow us to use only one set of functions.
1544 static function getReportOptions($bean) {
1547 // Prepare an array of report options.
1550 // Construct one by one.
1551 $options['name'] = null; // No name required.
1552 $options['user_id'] = $user->id; // Not sure if we need user_id here. Fav reports use it to recycle $user object in cron.php.
1553 $options['client_id'] = $bean->getAttribute('client');
1554 $options['cf_1_option_id'] = $bean->getAttribute('option');
1555 $options['project_id'] = $bean->getAttribute('project');
1556 $options['task_id'] = $bean->getAttribute('task');
1557 $options['billable'] = $bean->getAttribute('include_records');
1558 $options['invoice'] = $bean->getAttribute('invoice');
1559 $options['paid_status'] = $bean->getAttribute('paid_status');
1560 if (is_array($bean->getAttribute('users'))) $options['users'] = join(',', $bean->getAttribute('users'));
1561 $options['period'] = $bean->getAttribute('period');
1562 $options['period_start'] = $bean->getAttribute('start_date');
1563 $options['period_end'] = $bean->getAttribute('end_date');
1564 $options['show_client'] = $bean->getAttribute('chclient');
1565 $options['show_invoice'] = $bean->getAttribute('chinvoice');
1566 $options['show_paid'] = $bean->getAttribute('chpaid');
1567 $options['show_ip'] = $bean->getAttribute('chip');
1568 $options['show_project'] = $bean->getAttribute('chproject');
1569 $options['show_start'] = $bean->getAttribute('chstart');
1570 $options['show_duration'] = $bean->getAttribute('chduration');
1571 $options['show_cost'] = $bean->getAttribute('chcost');
1572 $options['show_task'] = $bean->getAttribute('chtask');
1573 $options['show_end'] = $bean->getAttribute('chfinish');
1574 $options['show_note'] = $bean->getAttribute('chnote');
1575 $options['show_custom_field_1'] = $bean->getAttribute('chcf_1');
1576 $options['show_work_units'] = $bean->getAttribute('chunits');
1577 $options['show_totals_only'] = $bean->getAttribute('chtotalsonly');
1578 $options['group_by'] = $bean->getAttribute('group_by');
1582 // verifyBean is a security function to make sure data in bean makes sense for a group.
1583 static function verifyBean($bean) {
1587 $users_in_bean = $bean->getAttribute('users');
1588 if (is_array($users_in_bean)) {
1589 $users_in_group = ttTeamHelper::getUsers();
1590 foreach ($users_in_group as $user_in_group) {
1591 $valid_ids[] = $user_in_group['id'];
1593 foreach ($users_in_bean as $user_in_bean) {
1594 if (!in_array($user_in_bean, $valid_ids)) {
1600 // TODO: add additional checks here. Perhaps do it before saving the bean for consistency.