+
+  // makeGroupByKey builds a combined group by key from group_by1, group_by2 and group_by3 values
+  // (passed in $options) and a row of data ($row obtained from a db query).
+  static function makeGroupByKey($options, $row) {
+    if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
+      // We have group_by1.
+      $group_by1 = $options['group_by1'];
+      $group_by1_value = $row[$group_by1];
+      //if ($group_by1 == 'date') $group_by1_value = ttDateToUserFormat($group_by1_value);
+      if (empty($group_by1_value)) $group_by1_value = 'Null'; // To match what comes out of makeConcatPart.
+      $group_by_key .= ' - '.$group_by1_value;
+    }
+    if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
+      // We have group_by2.
+      $group_by2 = $options['group_by2'];
+      $group_by2_value = $row[$group_by2];
+      //if ($group_by2 == 'date') $group_by2_value = ttDateToUserFormat($group_by2_value);
+      if (empty($group_by2_value)) $group_by2_value = 'Null'; // To match what comes out of makeConcatPart.
+      $group_by_key .= ' - '.$group_by2_value;
+    }
+    if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
+      // We have group_by3.
+      $group_by3 = $options['group_by3'];
+      $group_by3_value = $row[$group_by3];
+      //if ($group_by3 == 'date') $group_by3_value = ttDateToUserFormat($group_by3_value);
+      if (empty($group_by3_value)) $group_by3_value = 'Null'; // To match what comes out of makeConcatPart.
+      $group_by_key .= ' - '.$group_by3_value;
+    }
+    $group_by_key = trim($group_by_key, ' -');
+    return $group_by_key;
+  }
+
+  // makeGroupByPart builds a combined group by part for sql query for time items using group_by1,
+  // group_by2, and group_by3 values passed in $options.
+  static function makeGroupByPart($options) {
+    if (!ttReportHelper::grouping($options)) return null;
+
+    $group_by1 = $options['group_by1'];
+    $group_by2 = $options['group_by2'];
+    $group_by3 = $options['group_by3'];
+
+    switch ($group_by1) {
+      case 'date':
+        $group_by_parts .= ', l.date';
+        break;
+      case 'user':
+        $group_by_parts .= ', u.name';
+        break;
+      case 'client':
+        $group_by_parts .= ', c.name';
+        break;
+      case 'project':
+        $group_by_parts .= ', p.name';
+        break;
+      case 'task':
+        $group_by_parts .= ', t.name';
+        break;
+      case 'cf_1':
+        $group_by_parts .= ', cfo.value';
+        break;
+    }
+    switch ($group_by2) {
+      case 'date':
+        $group_by_parts .= ', l.date';
+        break;
+      case 'user':
+        $group_by_parts .= ', u.name';
+        break;
+      case 'client':
+        $group_by_parts .= ', c.name';
+        break;
+      case 'project':
+        $group_by_parts .= ', p.name';
+        break;
+      case 'task':
+        $group_by_parts .= ', t.name';
+        break;
+      case 'cf_1':
+        $group_by_parts .= ', cfo.value';
+        break;
+    }
+    switch ($group_by3) {
+      case 'date':
+        $group_by_parts .= ', l.date';
+        break;
+      case 'user':
+        $group_by_parts .= ', u.name';
+        break;
+      case 'client':
+        $group_by_parts .= ', c.name';
+        break;
+      case 'project':
+        $group_by_parts .= ', p.name';
+        break;
+      case 'task':
+        $group_by_parts .= ', t.name';
+        break;
+      case 'cf_1':
+        $group_by_parts .= ', cfo.value';
+        break;
+    }
+    // Remove garbage from the beginning.
+    $group_by_parts = ltrim($group_by_parts, ', ');
+    $group_by_part = "group by $group_by_parts";
+    return $group_by_part;
+  }
+
+  // makeGroupByExpensesPart builds a combined group by part for sql query for expense items using
+  // group_by1, group_by2, and group_by3 values passed in $options.
+  static function makeGroupByExpensesPart($options) {
+    $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
+      ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
+      ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
+    if ($no_grouping) return null;
+
+    $group_by1 = $options['group_by1'];
+    $group_by2 = $options['group_by2'];
+    $group_by3 = $options['group_by3'];
+
+    switch ($group_by1) {
+      case 'date':
+        $group_by_parts .= ', ei.date';
+        break;
+      case 'user':
+        $group_by_parts .= ', u.name';
+        break;
+      case 'client':
+        $group_by_parts .= ', c.name';
+        break;
+      case 'project':
+        $group_by_parts .= ', p.name';
+        break;
+    }
+    switch ($group_by2) {
+      case 'date':
+        $group_by_parts .= ', ei.date';
+        break;
+      case 'user':
+        $group_by_parts .= ', u.name';
+        break;
+      case 'client':
+        $group_by_parts .= ', c.name';
+        break;
+      case 'project':
+        $group_by_parts .= ', p.name';
+        break;
+    }
+    switch ($group_by3) {
+      case 'date':
+        $group_by_parts .= ', ei.date';
+        break;
+      case 'user':
+        $group_by_parts .= ', u.name';
+        break;
+      case 'client':
+        $group_by_parts .= ', c.name';
+        break;
+      case 'project':
+        $group_by_parts .= ', p.name';
+        break;
+    }
+    // Remove garbage from the beginning.
+    $group_by_parts = ltrim($group_by_parts, ', ');
+    if ($group_by_parts)
+      $group_by_part = "group by $group_by_parts";
+    return $group_by_part;
+  }
+
+  // makeConcatPart builds a concatenation part for getSubtotals query (for time items).
+  static function makeConcatPart($options) {
+    $group_by1 = $options['group_by1'];
+    $group_by2 = $options['group_by2'];
+    $group_by3 = $options['group_by3'];
+
+    switch ($group_by1) {
+      case 'date':
+        $what_to_concat .= ", ' - ', l.date";
+        break;
+      case 'user':
+        $what_to_concat .= ", ' - ', u.name";
+        $fields_part .= ', u.name as user';
+        break;
+      case 'client':
+        $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+        $fields_part .= ', c.name as client';
+        break;
+      case 'project':
+        $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+        $fields_part .= ', p.name as project';
+        break;
+      case 'task':
+        $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
+        $fields_part .= ', t.name as task';
+        break;
+      case 'cf_1':
+        $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
+        $fields_part .= ', cfo.value as cf_1';
+        break;
+    }
+    switch ($group_by2) {
+      case 'date':
+        $what_to_concat .= ", ' - ', l.date";
+        break;
+      case 'user':
+        $what_to_concat .= ", ' - ', u.name";
+        $fields_part .= ', u.name as user';
+        break;
+      case 'client':
+        $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+        $fields_part .= ', c.name as client';
+        break;
+      case 'project':
+        $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+        $fields_part .= ', p.name as project';
+        break;
+      case 'task':
+        $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
+        $fields_part .= ', t.name as task';
+        break;
+      case 'cf_1':
+        $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
+        $fields_part .= ', cfo.value as cf_1';
+        break;
+    }
+    switch ($group_by3) {
+      case 'date':
+        $what_to_concat .= ", ' - ', l.date";
+        break;
+      case 'user':
+        $what_to_concat .= ", ' - ', u.name";
+        $fields_part .= ', u.name as user';
+        break;
+      case 'client':
+        $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+        $fields_part .= ', c.name as client';
+        break;
+      case 'project':
+        $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+        $fields_part .= ', p.name as project';
+        break;
+      case 'task':
+        $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
+        $fields_part .= ', t.name as task';
+        break;
+      case 'cf_1':
+        $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
+        $fields_part .= ', cfo.value as cf_1';
+        break;
+    }
+    // Remove garbage from both ends.
+    $what_to_concat = trim($what_to_concat, "', -");
+    $concat_part = "concat($what_to_concat) as group_field";
+    $concat_part = trim($concat_part, ' -');
+    return "$concat_part $fields_part";
+  }
+
+  // makeConcatPart builds a concatenation part for getSubtotals query (for expense items).
+  static function makeConcatExpensesPart($options) {
+    $group_by1 = $options['group_by1'];
+    $group_by2 = $options['group_by2'];
+    $group_by3 = $options['group_by3'];
+
+    switch ($group_by1) {
+      case 'date':
+        $what_to_concat .= ", ' - ', ei.date";
+        break;
+      case 'user':
+        $what_to_concat .= ", ' - ', u.name";
+        $fields_part .= ', u.name as user';
+        break;
+      case 'client':
+        $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+        $fields_part .= ', c.name as client';
+        break;
+      case 'project':
+        $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+        $fields_part .= ', p.name as project';
+        break;
+
+      case 'task':
+        $what_to_concat .= ", ' - ', 'Null'";
+        $fields_part .= ', null as task';
+        break;
+
+      case 'cf_1':
+        $what_to_concat .= ", ' - ', 'Null'";
+        $fields_part .= ', null as cf_1';
+        break;
+    }
+    switch ($group_by2) {
+      case 'date':
+        $what_to_concat .= ", ' - ', ei.date";
+        break;
+      case 'user':
+        $what_to_concat .= ", ' - ', u.name";
+        $fields_part .= ', u.name as user';
+        break;
+      case 'client':
+        $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+        $fields_part .= ', c.name as client';
+        break;
+      case 'project':
+        $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+        $fields_part .= ', p.name as project';
+        break;
+
+      case 'task':
+        $what_to_concat .= ", ' - ', 'Null'";
+        $fields_part .= ', null as task';
+        break;
+
+      case 'cf_1':
+        $what_to_concat .= ", ' - ', 'Null'";
+        $fields_part .= ', null as cf_1';
+        break;
+    }
+    switch ($group_by3) {
+      case 'date':
+        $what_to_concat .= ", ' - ', ei.date";
+        break;
+      case 'user':
+        $what_to_concat .= ", ' - ', u.name";
+        $fields_part .= ', u.name as user';
+        break;
+      case 'client':
+        $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+        $fields_part .= ', c.name as client';
+        break;
+      case 'project':
+        $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+        $fields_part .= ', p.name as project';
+        break;
+
+      case 'task':
+        $what_to_concat .= ", ' - ', 'Null'";
+        $fields_part .= ', null as task';
+        break;
+
+      case 'cf_1':
+        $what_to_concat .= ", ' - ', 'Null'";
+        $fields_part .= ', null as cf_1';
+        break;
+    }
+    // Remove garbage from the beginning.
+    if ($what_to_concat)
+        $what_to_concat = substr($what_to_concat, 8);
+    $concat_part = "concat($what_to_concat) as group_field";
+    return "$concat_part $fields_part";
+  }
+
+  // makeCombinedSelectPart builds a list of fields for a combined select on a union for getSubtotals.
+  // This is used when we include expenses.
+  static function makeCombinedSelectPart($options) {
+    $group_by1 = $options['group_by1'];
+    $group_by2 = $options['group_by2'];
+    $group_by3 = $options['group_by3'];
+
+    $fields = "group_field";
+
+    switch ($group_by1) {
+      case 'user':
+        $fields .= ', user';
+        break;
+      case 'client':
+        $fields_part .= ', client';
+        break;
+      case 'project':
+        $fields .= ', project';
+        break;
+
+      case 'task':
+        $fields .= ', task';
+        break;
+
+      case 'cf_1':
+        $fields .= ', cf_1';
+        break;
+    }
+    switch ($group_by2) {
+      case 'user':
+        $fields .= ', user';
+        break;
+      case 'client':
+        $fields_part .= ', client';
+        break;
+      case 'project':
+        $fields .= ', project';
+        break;
+
+      case 'task':
+        $fields .= ', task';
+        break;
+
+      case 'cf_1':
+        $fields .= ', cf_1';
+        break;
+    }
+    switch ($group_by3) {
+      case 'user':
+        $fields .= ', user';
+        break;
+      case 'client':
+        $fields_part .= ', client';
+        break;
+      case 'project':
+        $fields .= ', project';
+        break;
+
+      case 'task':
+        $fields .= ', task';
+        break;
+
+      case 'cf_1':
+        $fields .= ', cf_1';
+        break;
+    }
+    return $fields;
+  }
+
+  // makeJoinPart builds a left join part for getSubtotals query (for time items).
+  static function makeJoinPart($options) {
+    global $user;
+
+    $trackingMode = $user->getTrackingMode();
+    if (ttReportHelper::groupingBy('user', $options) || MODE_TIME == $trackingMode) {
+      $join .= ' left join tt_users u on (l.user_id = u.id)';
+    }
+    if (ttReportHelper::groupingBy('client', $options)) {
+      $join .= ' left join tt_clients c on (l.client_id = c.id)';
+    }
+    if (ttReportHelper::groupingBy('project', $options)) {
+      $join .= ' left join tt_projects p on (l.project_id = p.id)';
+    }
+    if (ttReportHelper::groupingBy('task', $options)) {
+      $join .= ' left join tt_tasks t on (l.task_id = t.id)';
+    }
+    if (ttReportHelper::groupingBy('cf_1', $options)) {
+      $custom_fields = new CustomFields();
+      if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
+        $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)';
+      elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
+        $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)';
+    }
+    if ($options['show_cost'] && $trackingMode != MODE_TIME) {
+      $join .= ' left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)';
+    }
+    // Prepare inner joins.
+    $inner_joins = null;
+    if ($user->isPluginEnabled('ts') && $options['timesheet']) {
+      $timesheet_option = $options['timesheet'];
+      if ($timesheet_option == TIMESHEET_PENDING)
+        $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.submit_status = 1 and ts.approve_status is null)";
+      else if ($timesheet_option == TIMESHEET_APPROVED)
+        $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approve_status = 1)";
+      else if ($timesheet_option == TIMESHEET_NOT_APPROVED)
+        $inner_joins .= " inner join tt_timesheets ts on (l.timesheet_id = ts.id and ts.approve_status = 0)";
+    }
+    $join .= $inner_joins;
+    return $join;
+  }
+
+  // makeWorkUnitPart builds an sql part for work units for time items.
+  static function makeWorkUnitPart($options) {
+    global $user;
+
+    $workUnits = $options['show_work_units'];
+    if ($workUnits) {
+      $unitTotalsOnly = $user->getConfigOption('unit_totals_only');
+      $firstUnitThreshold = $user->getConfigInt('1st_unit_threshold', 0);
+      $minutesInUnit = $user->getConfigInt('minutes_in_unit', 15);
+      if ($unitTotalsOnly)
+        $work_unit_part = ", if (sum(l.billable * time_to_sec(l.duration)/60) < $firstUnitThreshold, 0, ceil(sum(l.billable * time_to_sec(l.duration)/60/$minutesInUnit))) as units";
+      else
+        $work_unit_part = ", sum(if(l.billable = 0 or time_to_sec(l.duration)/60 < $firstUnitThreshold, 0, ceil(time_to_sec(l.duration)/60/$minutesInUnit))) as units";
+    }
+    return $work_unit_part;
+  }
+
+  // makeCostPart builds a cost part for time items.
+  static function makeCostPart($options) {
+    global $user;
+
+    if ($options['show_cost']) {
+      if (MODE_TIME == $user->getTrackingMode())
+        $cost_part = ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost";
+      else
+        $cost_part .= ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost";
+    }
+    return $cost_part;
+  }
+
+  // makeJoinExpensesPart builds a left join part for getSubtotals query for expense items.
+  static function makeJoinExpensesPart($options) {
+    global $user;
+
+    if (ttReportHelper::groupingBy('user', $options)) {
+      $join .= ' left join tt_users u on (ei.user_id = u.id)';
+    }
+    if (ttReportHelper::groupingBy('client', $options)) {
+      $join .= ' left join tt_clients c on (ei.client_id = c.id)';
+    }
+    if (ttReportHelper::groupingBy('project', $options)) {
+      $join .= ' left join tt_projects p on (ei.project_id = p.id)';
+    }
+    return $join;
+  }
+
+  // grouping determines if we are grouping the report by either group_by1,
+  // group_by2, or group_by3 values passed in $options.
+  static function grouping($options) {
+    $grouping = ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') ||
+      ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') ||
+      ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping');
+    return $grouping;
+  }
+
+  // groupingBy determines if we are grouping a report by a value of $what
+  // ('date', 'user', 'project', etc.) by checking group_by1, group_by2,
+  // and group_by3 values passed in $options.
+  static function groupingBy($what, $options) {
+    $grouping = ($options['group_by1'] == $what) || ($options['group_by2'] == $what) || ($options['group_by3'] == $what);
+    return $grouping;
+  }
+
+  // makeGroupByHeader builds a column header for a totals-only report using group_by1,
+  // group_by2, and group_by3 values passed in $options.
+  static function makeGroupByHeader($options) {
+    global $i18n;
+    global $custom_fields;
+
+    $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
+      ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
+      ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
+    if ($no_grouping) return null;
+
+    if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
+      // We have group_by1.
+      $group_by1 = $options['group_by1'];
+      if ('cf_1' == $group_by1)
+        $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
+      else {
+        $key = 'label.'.$group_by1;
+        $group_by_header .= ' - '.$i18n->get($key);
+      }
+    }
+    if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
+      // We have group_by2.
+      $group_by2 = $options['group_by2'];
+      if ('cf_1' == $group_by2)
+        $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
+      else {
+        $key = 'label.'.$group_by2;
+        $group_by_header .= ' - '.$i18n->get($key);
+      }
+    }
+    if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
+      // We have group_by3.
+      $group_by3 = $options['group_by3'];
+      if ('cf_1' == $group_by3)
+        $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
+      else {
+        $key = 'label.'.$group_by3;
+        $group_by_header .= ' - '.$i18n->get($key);
+      }
+    }
+    $group_by_header = ltrim($group_by_header, ' -');
+    return $group_by_header;
+  }
+
+  // makeGroupByXmlTag creates an xml tag for a totals only report using group_by1,
+  // group_by2, and group_by3 values passed in $options.
+  static function makeGroupByXmlTag($options) {
+    if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
+      // We have group_by1.
+      $tag .= '_'.$options['group_by1'];
+    }
+    if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
+      // We have group_by2.
+      $tag .= '_'.$options['group_by2'];
+    }
+    if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
+      // We have group_by3.
+      $tag .= '_'.$options['group_by3'];
+    }
+    $tag = ltrim($tag, '_');
+    return $tag;
+  }
+
+  // makeGroupByLabel builds a label for one row in a "Totals only" report of grouped by items.
+  // It does one thing: if we are grouping by date, the date format is converted for user.
+  static function makeGroupByLabel($key, $options) {
+    if (!ttReportHelper::groupingBy('date', $options))
+      return $key; // No need to format.
+
+    global $user;
+    if ($user->getDateFormat() == DB_DATEFORMAT)
+      return $key; // No need to format.
+
+    $label = $key;
+    if (preg_match('/\d\d\d\d-\d\d-\d\d/', $key, $matches)) {
+      // Replace the first found match of a date in DB_DATEFORMAT.
+      // This is not entirely clean but better than nothing for a label in a row.
+      $userDate = ttDateToUserFormat($matches[0]);
+      $label = str_replace($matches[0], $userDate, $key);
+    }
+    return $label;
+  }