Some more refactoring.
[timetracker.git] / WEB-INF / lib / ttReportHelper.class.php
index b33ded8..6412827 100644 (file)
@@ -37,8 +37,7 @@ require_once(dirname(__FILE__).'/../../plugins/CustomFields.class.php');
 class ttReportHelper {
 
   // getWhere prepares a WHERE clause for a report query.
-  // Note: $options is a future replacement of $bean, which is work in progress.
-  static function getWhere($bean, $options) {
+  static function getWhere($options) {
     global $user;
 
     // Prepare dropdown parts.
@@ -57,69 +56,13 @@ class ttReportHelper {
     if ($options['paid_status']=='1') $dropdown_parts .= ' and l.paid = 1';
     if ($options['paid_status']=='2') $dropdown_parts .= ' and l.paid = 0';
 
-    // Prepare user list part.
-    $userlist = -1;
-    if (($user->can('view_reports') || $user->isClient()) && is_array($bean->getAttribute('users')))
-      $userlist = join(',', $bean->getAttribute('users'));
     // Prepare sql query part for user list.
-    $user_list_part = null;
-    if ($user->can('view_reports') || $user->isClient())
-      $user_list_part = " and l.user_id in ($userlist)";
-    else
-      $user_list_part = " and l.user_id = ".$user->id;
-
-    // Prepare sql query part for where.
-    if ($bean->getAttribute('period'))
-      $period = new Period($bean->getAttribute('period'), new DateAndTime($user->date_format));
-    else {
-      $period = new Period();
-      $period->setPeriod(
-        new DateAndTime($user->date_format, $bean->getAttribute('start_date')),
-        new DateAndTime($user->date_format, $bean->getAttribute('end_date')));
-    }
-    $where = " where l.status = 1 and l.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and l.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
-      " $user_list_part $dropdown_parts";
-    return $where;
-  }
-
-  // getFavWhere prepares a WHERE clause for a favorite report query.
-  static function getFavWhere($options) {
-    global $user;
-
-    // Prepare dropdown parts.
-    $dropdown_parts = '';
-    if ($options['client_id'])
-      $dropdown_parts .= ' and l.client_id = '.$options['client_id'];
-    elseif ($user->isClient() && $user->client_id)
-      $dropdown_parts .= ' and l.client_id = '.$user->client_id;
-    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'].')';
-    if ($options['project_id']) $dropdown_parts .= ' and l.project_id = '.$options['project_id'];
-    if ($options['task_id']) $dropdown_parts .= ' and l.task_id = '.$options['task_id'];
-    if ($options['billable']=='1') $dropdown_parts .= ' and l.billable = 1';
-    if ($options['billable']=='2') $dropdown_parts .= ' and l.billable = 0';
-    if ($options['invoice']=='1') $dropdown_parts .= ' and l.invoice_id is not NULL';
-    if ($options['invoice']=='2') $dropdown_parts .= ' and l.invoice_id is NULL';
-    if ($options['paid_status']=='1') $dropdown_parts .= ' and l.paid = 1';
-    if ($options['paid_status']=='2') $dropdown_parts .= ' and l.paid = 0';
-
-    // Prepare user list part.
-    $userlist = -1;
-    if (($user->can('view_reports') || $user->isClient())) {
-      if ($options['users'])
-        $userlist = $options['users'];
-      else {
-        $active_users = ttTeamHelper::getActiveUsers();
-        foreach ($active_users as $single_user)
-          $users[] = $single_user['id'];
-        $userlist = join(',', $users);
-      }
-    }
-    // Prepare sql query part for user list.
-    $user_list_part = null;
-    if ($user->can('view_reports') || $user->isClient())
+    $userlist = $options['users'] ? $options['users'] : '-1';
+    if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
       $user_list_part = " and l.user_id in ($userlist)";
     else
       $user_list_part = " and l.user_id = ".$user->id;
+    $user_list_part .= " and l.group_id = ".$user->getActiveGroup();
 
     // Prepare sql query part for where.
     if ($options['period'])
@@ -136,48 +79,7 @@ class ttReportHelper {
   }
 
   // getExpenseWhere prepares WHERE clause for expenses query in a report.
-  static function getExpenseWhere($bean, $options) {
-    global $user;
-
-    // Prepare dropdown parts.
-    $dropdown_parts = '';
-    if ($options['client_id'])
-      $dropdown_parts .= ' and l.client_id = '.$options['client_id'];
-    elseif ($user->isClient() && $user->client_id)
-      $dropdown_parts .= ' and ei.client_id = '.$user->client_id;
-    if ($options['project_id']) $dropdown_parts .= ' and ei.project_id = '.$options['project_id'];
-    if ($options['invoice']=='1') $dropdown_parts .= ' and ei.invoice_id is not NULL';
-    if ($options['invoice']=='2') $dropdown_parts .= ' and ei.invoice_id is NULL';
-    if ($options['paid_status']=='1') $dropdown_parts .= ' and ei.paid = 1';
-    if ($options['paid_status']=='2') $dropdown_parts .= ' and ei.paid = 0';
-
-    // Prepare user list part.
-    $userlist = -1;
-    if (($user->can('view_reports') || $user->isClient()) && is_array($bean->getAttribute('users')))
-      $userlist = join(',', $bean->getAttribute('users'));
-    // Prepare sql query part for user list.
-    $user_list_part = null;
-    if ($user->can('view_reports') || $user->isClient())
-      $user_list_part = " and ei.user_id in ($userlist)";
-    else
-      $user_list_part = " and ei.user_id = ".$user->id;
-
-    // Prepare sql query part for where.
-    if ($bean->getAttribute('period'))
-      $period = new Period($bean->getAttribute('period'), new DateAndTime($user->date_format));
-    else {
-      $period = new Period();
-      $period->setPeriod(
-        new DateAndTime($user->date_format, $bean->getAttribute('start_date')),
-        new DateAndTime($user->date_format, $bean->getAttribute('end_date')));
-    }
-    $where = " where ei.status = 1 and ei.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and ei.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
-      " $user_list_part $dropdown_parts";
-    return $where;
-  }
-
-  // getFavExpenseWhere prepares a WHERE clause for expenses query in a favorite report.
-  static function getFavExpenseWhere($options) {
+  static function getExpenseWhere($options) {
     global $user;
 
     // Prepare dropdown parts.
@@ -192,24 +94,13 @@ class ttReportHelper {
     if ($options['paid_status']=='1') $dropdown_parts .= ' and ei.paid = 1';
     if ($options['paid_status']=='2') $dropdown_parts .= ' and ei.paid = 0';
 
-    // Prepare user list part.
-    $userlist = -1;
-    if (($user->can('view_reports') || $user->isClient())) {
-      if ($options['users'])
-        $userlist = $options['users'];
-      else {
-       $active_users = ttTeamHelper::getActiveUsers();
-        foreach ($active_users as $single_user)
-          $users[] = $single_user['id'];
-        $userlist = join(',', $users);
-      }
-    }
     // Prepare sql query part for user list.
-    $user_list_part = null;
-    if ($user->can('view_reports') || $user->isClient())
+    $userlist = $options['users'] ? $options['users'] : '-1';
+    if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
       $user_list_part = " and ei.user_id in ($userlist)";
     else
       $user_list_part = " and ei.user_id = ".$user->id;
+    $user_list_part .= " and ei.group_id = ".$user->getActiveGroup();
 
     // Prepare sql query part for where.
     if ($options['period'])
@@ -233,11 +124,11 @@ class ttReportHelper {
     $mdb2 = getConnection();
 
     // Determine these once as they are used in multiple places in this function.
-    $canViewReports = $user->can('view_reports');
+    $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
     $isClient = $user->isClient();
 
-    $group_by_option = $bean->getAttribute('group_by');
-    $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($bean->getAttribute('chstart') || $bean->getAttribute('chfinish'));
+    $group_by_option = $options['group_by'];
+    $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($options['show_start'] || $options['show_end']);
 
     // Prepare a query for time items in tt_log table.
     $fields = array(); // An array of fields for database query.
@@ -247,16 +138,16 @@ class ttReportHelper {
     if($canViewReports || $isClient)
       array_push($fields, 'u.name as user');
     // Add client name if it is selected.
-    if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
+    if ($options['show_client'] || 'client' == $group_by_option)
       array_push($fields, 'c.name as client');
     // Add project name if it is selected.
-    if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
+    if ($options['show_project'] || 'project' == $group_by_option)
       array_push($fields, 'p.name as project');
     // Add task name if it is selected.
-    if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
+    if ($options['show_task'] || 'task' == $group_by_option)
       array_push($fields, 't.name as task');
     // Add custom field.
-    $include_cf_1 = $bean->getAttribute('chcf_1') || 'cf_1' == $group_by_option;
+    $include_cf_1 = $options['show_custom_field_1'] || 'cf_1' == $group_by_option;
     if ($include_cf_1) {
       $custom_fields = new CustomFields($user->group_id);
       $cf_1_type = $custom_fields->fields[0]['type'];
@@ -267,28 +158,28 @@ class ttReportHelper {
       }
     }
     // Add start time.
-    if ($bean->getAttribute('chstart')) {
+    if ($options['show_start']) {
       array_push($fields, "l.start as unformatted_start");
       array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start");
     }
     // Add finish time.
-    if ($bean->getAttribute('chfinish'))
+    if ($options['show_end'])
       array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish");
     // Add duration.
-    if ($bean->getAttribute('chduration'))
+    if ($options['show_duration'])
       array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
     // Add work units.
-    if ($bean->getAttribute('chunits')) {
+    if ($options['show_work_units']) {
       if ($user->unit_totals_only)
         array_push($fields, "null as units");
       else
-       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");
+        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");
     }
     // Add note.
-    if ($bean->getAttribute('chnote'))
+    if ($options['show_note'])
       array_push($fields, 'l.comment as note');
     // Handle cost.
-    $includeCost = $bean->getAttribute('chcost');
+    $includeCost = $options['show_cost'];
     if ($includeCost) {
       if (MODE_TIME == $user->tracking_mode)
         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.
@@ -297,31 +188,30 @@ class ttReportHelper {
       array_push($fields, "null as expense"); 
     }
     // Add paid status.
-    if ($canViewReports && $bean->getAttribute('chpaid'))
+    if ($canViewReports && $options['show_paid'])
       array_push($fields, 'l.paid as paid');
     // Add IP address.
-    if ($canViewReports && $bean->getAttribute('chip')) {
+    if ($canViewReports && $options['show_ip']) {
       array_push($fields, 'l.created as created');
       array_push($fields, 'l.created_ip as created_ip');
       array_push($fields, 'l.modified as modified');
       array_push($fields, 'l.modified_ip as modified_ip');
     }
-
     // Add invoice name if it is selected.
-    if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
+    if (($canViewReports || $isClient) && $options['show_invoice'])
       array_push($fields, 'i.name as invoice');
 
     // Prepare sql query part for left joins.
     $left_joins = null;
-    if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
+    if ($options['show_client'] || 'client' == $group_by_option)
       $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
-    if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
+    if (($canViewReports || $isClient) && $options['show_invoice'])
       $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
     if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
        $left_joins .= " left join tt_users u on (u.id = l.user_id)";
-    if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
+    if ($options['show_project'] || 'project' == $group_by_option)
       $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
-    if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
+    if ($options['show_task'] || 'task' == $group_by_option)
       $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
     if ($include_cf_1) {
       if ($cf_1_type == CustomFields::TYPE_TEXT)
@@ -334,7 +224,7 @@ class ttReportHelper {
     if ($includeCost && MODE_TIME != $user->tracking_mode)
       $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
 
-    $where = ttReportHelper::getWhere($bean, $options);
+    $where = ttReportHelper::getWhere($options);
 
     // Construct sql query for tt_log items.
     $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
@@ -342,7 +232,7 @@ class ttReportHelper {
     // with an exception of sorting part, that is added in the end.
 
     // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
-    if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
+    if ($options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
 
       $fields = array(); // An array of fields for database query.
       array_push($fields, 'ei.id');
@@ -351,58 +241,56 @@ class ttReportHelper {
       if($canViewReports || $isClient)
         array_push($fields, 'u.name as user');
       // Add client name if it is selected.
-      if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
+      if ($options['show_client'] || 'client' == $group_by_option)
         array_push($fields, 'c.name as client');
       // Add project name if it is selected.
-      if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
+      if ($options['show_project'] || 'project' == $group_by_option)
         array_push($fields, 'p.name as project');
-      if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
+      if ($options['show_task'] || 'task' == $group_by_option)
         array_push($fields, 'null'); // null for task name. We need to match column count for union.
-      if ($bean->getAttribute('chcf_1') || 'cf_1' == $group_by_option)
+      if ($options['show_custom_field_1'] || 'cf_1' == $group_by_option)
         array_push($fields, 'null'); // null for cf_1.
-      if ($bean->getAttribute('chstart')) {
+      if ($options['show_start']) {
         array_push($fields, 'null'); // null for unformatted_start.
         array_push($fields, 'null'); // null for start.
       }
-      if ($bean->getAttribute('chfinish'))
+      if ($options['show_end'])
         array_push($fields, 'null'); // null for finish.
-      if ($bean->getAttribute('chduration'))
+      if ($options['show_duration'])
         array_push($fields, 'null'); // null for duration.
-      // Add work units.
-      if ($bean->getAttribute('chunits'))
+      if ($options['show_work_units'])
         array_push($fields, 'null as units'); // null for work units.
       // Use the note field to print item name.
-      if ($bean->getAttribute('chnote'))
+      if ($options['show_note'])
         array_push($fields, 'ei.name as note');
       array_push($fields, 'ei.cost as cost');
       array_push($fields, 'ei.cost as expense');
       // Add paid status.
-      if ($canViewReports && $bean->getAttribute('chpaid'))
+      if ($canViewReports && $options['show_paid'])
         array_push($fields, 'ei.paid as paid');
       // Add IP address.
-      if ($canViewReports && $bean->getAttribute('chip')) {
+      if ($canViewReports && $options['show_ip']) {
         array_push($fields, 'ei.created as created');
         array_push($fields, 'ei.created_ip as created_ip');
         array_push($fields, 'ei.modified as modified');
         array_push($fields, 'ei.modified_ip as modified_ip');
       }
-
       // Add invoice name if it is selected.
-      if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
+      if (($canViewReports || $isClient) && $options['show_invoice'])
         array_push($fields, 'i.name as invoice');
 
       // Prepare sql query part for left joins.
       $left_joins = null;
       if ($canViewReports || $isClient)
         $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
-      if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
+      if ($options['show_client'] || 'client' == $group_by_option)
         $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
-      if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
+      if ($options['show_project'] || 'project' == $group_by_option)
         $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
-      if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
+      if (($canViewReports || $isClient) && $options['show_invoice'])
         $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
 
-      $where = ttReportHelper::getExpenseWhere($bean, $options);
+      $where = ttReportHelper::getExpenseWhere($options);
 
       // Construct sql query for expense items.
       $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
@@ -413,13 +301,13 @@ class ttReportHelper {
 
     // Determine sort part.
     $sort_part = ' order by ';
-    if ('no_grouping' == $group_by_option || 'date' == $group_by_option)
+    if ($group_by_option == null || 'no_grouping' == $group_by_option || 'date' == $group_by_option)
       $sort_part .= 'date';
     else
       $sort_part .= $group_by_option.', date';
-    if (($canViewReports || $isClient) && is_array($bean->getAttribute('users')) && 'user' != $group_by_option)
+    if (($canViewReports || $isClient) && $options['users'] && 'user' != $group_by_option)
       $sort_part .= ', user, type';
-    if ($bean->getAttribute('chstart'))
+    if ($options['show_start'])
       $sort_part .= ', unformatted_start';
     $sort_part .= ', id';
 
@@ -508,7 +396,7 @@ class ttReportHelper {
     $mdb2 = getConnection();
 
     // Determine these once as they are used in multiple places in this function.
-    $canViewReports = $user->can('view_reports');
+    $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
     $isClient = $user->isClient();
 
     $group_by_option = $options['group_by'];
@@ -559,7 +447,6 @@ class ttReportHelper {
       else
         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");
     }
-
     // Add note.
     if ($options['show_note'])
       array_push($fields, 'l.comment as note');
@@ -609,7 +496,7 @@ class ttReportHelper {
     if ($includeCost && MODE_TIME != $user->tracking_mode)
       $left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
 
-    $where = ttReportHelper::getFavWhere($options);
+    $where = ttReportHelper::getWhere($options);
 
     // Construct sql query for tt_log items.
     $sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
@@ -675,7 +562,7 @@ class ttReportHelper {
       if (($canViewReports || $isClient) && $options['show_invoice'])
         $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
 
-      $where = ttReportHelper::getFavExpenseWhere($options);
+      $where = ttReportHelper::getExpenseWhere($options);
 
       // Construct sql query for expense items.
       $sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
@@ -686,11 +573,11 @@ class ttReportHelper {
 
     // Determine sort part.
     $sort_part = ' order by ';
-    if ($group_by_option == null || 'no_grouping' == $group_by_option || 'date' == $group_by_option) // TODO: fix DB for NULL values in group_by field.
+    if ($group_by_option == null || 'no_grouping' == $group_by_option || 'date' == $group_by_option)
       $sort_part .= 'date';
     else
       $sort_part .= $group_by_option.', date';
-    if (($canViewReports || $isClient) /*&& is_array($bean->getAttribute('users'))*/ && 'user' != $group_by_option)
+    if (($canViewReports || $isClient) && $options['users'] && 'user' != $group_by_option)
       $sort_part .= ', user, type';
     if ($options['show_start'])
       $sort_part .= ', unformatted_start';
@@ -785,7 +672,7 @@ class ttReportHelper {
         break;
     }
 
-    $where = ttReportHelper::getWhere($bean, $options);
+    $where = ttReportHelper::getWhere($options);
     if ($bean->getAttribute('chcost')) {
       if (MODE_TIME == $user->tracking_mode) {
         if ($group_by_option != 'user')
@@ -852,7 +739,7 @@ class ttReportHelper {
           break;
       }
 
-      $where = ttReportHelper::getExpenseWhere($bean, $options);
+      $where = ttReportHelper::getExpenseWhere($options);
       $sql_for_expenses = "select $group_field as group_field, null as time";
       if ($bean->getAttribute('chunits')) $sql_for_expenses .= ", null as units";
       $sql_for_expenses .= ", sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei $group_join $where";
@@ -936,7 +823,7 @@ class ttReportHelper {
         break;
     }
 
-    $where = ttReportHelper::getFavWhere($options);
+    $where = ttReportHelper::getWhere($options);
     if ($options['show_cost']) {
       if (MODE_TIME == $user->tracking_mode) {
         if ($group_by_option != 'user')
@@ -1004,7 +891,7 @@ class ttReportHelper {
           break;
       }
 
-      $where = ttReportHelper::getFavExpenseWhere($options);
+      $where = ttReportHelper::getExpenseWhere($options);
       $sql_for_expenses = "select $group_field as group_field, null as time";
       if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
       $sql_for_expenses .= ", sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei $group_join $where";
@@ -1050,7 +937,7 @@ class ttReportHelper {
 
     $mdb2 = getConnection();
 
-    $where = ttReportHelper::getWhere($bean, $options);
+    $where = ttReportHelper::getWhere($options);
 
     // Prepare parts.
     $time_part = "sum(time_to_sec(l.duration)) as time";
@@ -1077,7 +964,7 @@ class ttReportHelper {
 
     // If we have expenses, query becomes a bit more complex.
     if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) {
-      $where = ttReportHelper::getExpenseWhere($bean, $options);
+      $where = ttReportHelper::getExpenseWhere($options);
       $sql_for_expenses = "select null as time";
       if ($bean->getAttribute('chunits')) $sql_for_expenses .= ", null as units";
       $sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
@@ -1132,7 +1019,7 @@ class ttReportHelper {
 
     $mdb2 = getConnection();
 
-    $where = ttReportHelper::getFavWhere($options);
+    $where = ttReportHelper::getWhere($options);
 
     // Prepare parts.
     $time_part = "sum(time_to_sec(l.duration)) as time";
@@ -1159,7 +1046,7 @@ class ttReportHelper {
 
     // If we have expenses, query becomes a bit more complex.
     if ($options['show_cost'] && $user->isPluginEnabled('ex')) {
-      $where = ttReportHelper::getFavExpenseWhere($options);
+      $where = ttReportHelper::getExpenseWhere($options);
       $sql_for_expenses = "select null as time";
       if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
       $sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
@@ -1909,32 +1796,47 @@ class ttReportHelper {
     $options['billable'] = $bean->getAttribute('include_records');
     $options['invoice'] = $bean->getAttribute('invoice');
     $options['paid_status'] = $bean->getAttribute('paid_status');
-
-/*
- * TODO: remaining fields to fill in...
-  `users` text default NULL,                             # Comma-separated list of user ids. Nothing here means "all" users.
-  `period` tinyint(4) default NULL,                      # selected period type for report
-  `period_start` date default NULL,                      # period start
-  `period_end` date default NULL,                        # period end
-  `show_client` tinyint(4) NOT NULL default 0,           # whether to show client column
-  `show_invoice` tinyint(4) NOT NULL default 0,          # whether to show invoice column
-  `show_paid` tinyint(4) NOT NULL default 0,             # whether to show paid column
-  `show_ip` tinyint(4) NOT NULL default 0,               # whether to show ip column
-  `show_project` tinyint(4) NOT NULL default 0,          # whether to show project column
-  `show_start` tinyint(4) NOT NULL default 0,            # whether to show start field
-  `show_duration` tinyint(4) NOT NULL default 0,         # whether to show duration field
-  `show_cost` tinyint(4) NOT NULL default 0,             # whether to show cost field
-  `show_task` tinyint(4) NOT NULL default 0,             # whether to show task column
-  `show_end` tinyint(4) NOT NULL default 0,              # whether to show end field
-  `show_note` tinyint(4) NOT NULL default 0,             # whether to show note column
-  `show_custom_field_1` tinyint(4) NOT NULL default 0,   # whether to show custom field 1
-  `show_work_units` tinyint(4) NOT NULL default 0,       # whether to show work units
-  `show_totals_only` tinyint(4) NOT NULL default 0,      # whether to show totals only
-  `group_by` varchar(20) default NULL,                   # group by field
-  `status` tinyint(4) default 1,                         # favorite report status
-  PRIMARY KEY (`id`)
-);
-     */
+    if (is_array($bean->getAttribute('users'))) $options['users'] = join(',', $bean->getAttribute('users'));
+    $options['period'] = $bean->getAttribute('period');
+    $options['period_start'] = $bean->getAttribute('start_date');
+    $options['period_end'] = $bean->getAttribute('end_date');
+    $options['show_client'] = $bean->getAttribute('chclient');
+    $options['show_invoice'] = $bean->getAttribute('chinvoice');
+    $options['show_paid'] = $bean->getAttribute('chpaid');
+    $options['show_ip'] = $bean->getAttribute('chip');
+    $options['show_project'] = $bean->getAttribute('chproject');
+    $options['show_start'] = $bean->getAttribute('chstart');
+    $options['show_duration'] = $bean->getAttribute('chduration');
+    $options['show_cost'] = $bean->getAttribute('chcost');
+    $options['show_task'] = $bean->getAttribute('chtask');
+    $options['show_end'] = $bean->getAttribute('chfinish');
+    $options['show_note'] = $bean->getAttribute('chnote');
+    $options['show_custom_field_1'] = $bean->getAttribute('chcf_1');
+    $options['show_work_units'] = $bean->getAttribute('chunits');
+    $options['show_totals_only'] = $bean->getAttribute('chtotalsonly');
+    $options['group_by'] = $bean->getAttribute('group_by');
     return $options;
   }
+
+  // verifyBean is a security function to make sure data in bean makes sense for a group.
+  static function verifyBean($bean) {
+    global $user;
+
+    // Check users.
+    $users_in_bean = $bean->getAttribute('users');
+    if (is_array($users_in_bean)) {
+      $users_in_group = ttTeamHelper::getUsers();
+      foreach ($users_in_group as $user_in_group) {
+        $valid_ids[] = $user_in_group['id'];
+      }
+      foreach ($users_in_bean as $user_in_bean) {
+        if (!in_array($user_in_bean, $valid_ids)) {
+          return false;
+        }
+      }
+    }
+
+    // TODO: add additional checks here. Perhaps do it before saving the bean for consistency.
+    return true;
+  }
 }