]> wagnertech.de Git - timetracker.git/blobdiff - WEB-INF/lib/ttReportHelper.class.php
Refactoring of reports started.
[timetracker.git] / WEB-INF / lib / ttReportHelper.class.php
index daaad46c8a43dda007f3b2b38755ef6fbe111103..4da47ec07e285f872fa0867fc2385cc8cb282313 100644 (file)
@@ -276,6 +276,13 @@ class ttReportHelper {
     // Add duration.
     if ($bean->getAttribute('chduration'))
       array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
+    // Add work units.
+    if ($bean->getAttribute('chunits')) {
+      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");
+    }
     // Add note.
     if ($bean->getAttribute('chnote'))
       array_push($fields, 'l.comment as note');
@@ -303,10 +310,6 @@ class ttReportHelper {
     if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
       array_push($fields, 'i.name as invoice');
 
-    // Add work units.
-    if ($bean->getAttribute('chunits'))
-      array_push($fields, "if(time_to_sec(duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(duration)/60/$user->minutes_in_unit)) as units");
-
     // Prepare sql query part for left joins.
     $left_joins = null;
     if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
@@ -364,6 +367,9 @@ class ttReportHelper {
         array_push($fields, 'null'); // null for finish.
       if ($bean->getAttribute('chduration'))
         array_push($fields, 'null'); // null for duration.
+      // Add work units.
+      if ($bean->getAttribute('chunits'))
+        array_push($fields, 'null as units'); // null for work units.
       // Use the note field to print item name.
       if ($bean->getAttribute('chnote'))
         array_push($fields, 'ei.name as note');
@@ -384,10 +390,6 @@ class ttReportHelper {
       if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
         array_push($fields, 'i.name as invoice');
 
-      // Add work units.
-      if ($bean->getAttribute('chunits'))
-        array_push($fields, 'null'); // null for work units.
-
       // Prepare sql query part for left joins.
       $left_joins = null;
       if ($canViewReports || $isClient)
@@ -549,6 +551,14 @@ class ttReportHelper {
     // Add duration.
     if ($report['show_duration'])
       array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
+    // Add work units.
+    if ($report['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");
+    }
+
     // Add note.
     if ($report['show_note'])
       array_push($fields, 'l.comment as note');
@@ -632,6 +642,8 @@ class ttReportHelper {
         array_push($fields, 'null'); // null for finish.
       if ($report['show_duration'])
         array_push($fields, 'null'); // null for duration.
+      if ($report['show_work_units'])
+        array_push($fields, 'null as units'); // null for work units.
       // Use the note field to print item name.
       if ($report['show_note'])
         array_push($fields, 'ei.name as note');
@@ -777,24 +789,39 @@ class ttReportHelper {
       if (MODE_TIME == $user->tracking_mode) {
         if ($group_by_option != 'user')
           $left_join = 'left join tt_users u on (l.user_id = u.id)';
-        $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
-          sum(if(time_to_sec(duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(duration)/60/$user->minutes_in_unit))) as units,
-          sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
+        $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
+        if ($bean->getAttribute('chunits')) {
+          if ($user->unit_totals_only)
+            $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";
+          else
+            $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";
+        }
+        $sql .= ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
           null as expenses from tt_log l
           $group_join $left_join $where group by $group_field";
       } else {
         // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
-        $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
-          sum(if(time_to_sec(duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(duration)/60/$user->minutes_in_unit))) as units,
-          sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
+        $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
+        if ($bean->getAttribute('chunits')) {
+          if ($user->unit_totals_only)
+            $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";
+          else
+            $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";
+        }
+        $sql .= ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
           null as expenses from tt_log l
           $group_join
           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";
       }
     } else {
-      $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time,
-        sum(if(time_to_sec(duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(duration)/60/$user->minutes_in_unit))) as units,
-        null as expenses from tt_log l
+      $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
+      if ($bean->getAttribute('chunits')) {
+        if ($user->unit_totals_only)
+          $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";
+        else
+          $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";
+      }
+      $sql .= ", null as expenses from tt_log l
         $group_join $where group by $group_field";
     }
     // By now we have sql for time items.
@@ -825,13 +852,17 @@ class ttReportHelper {
       }
 
       $where = ttReportHelper::getExpenseWhere($bean);
-      $sql_for_expenses = "select $group_field as group_field, null as time, null as units, sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei 
-        $group_join $where";
+      $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";
       // Add a "group by" clause if we are grouping.
       if ('null' != $group_field) $sql_for_expenses .= " group by $group_field";
 
       // Create a combined query.
-      $sql = "select group_field, sum(time) as time, sum(units) as units, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by group_field";
+      $combined = "select group_field, sum(time) as time";
+      if ($bean->getAttribute('chunits')) $combined .= ", sum(units) as units";
+      $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by group_field";
+      $sql = $combined;
     }
 
     // Execute query.
@@ -909,21 +940,41 @@ class ttReportHelper {
       if (MODE_TIME == $user->tracking_mode) {
         if ($group_by_option != 'user')
           $left_join = 'left join tt_users u on (l.user_id = u.id)';
-        $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, 
-          sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
+          $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
+          if ($report['show_work_units']) {
+            if ($user->unit_totals_only)
+              $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";
+            else
+              $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";
+          }
+          $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";
+          $sql .= ", sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10, 2))) as cost,
           null as expenses from tt_log l
           $group_join $left_join $where group by $group_field";
       } else {
         // If we are including cost and tracking projects, our query (the same as above) needs to join the tt_user_project_binds table.
-        $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, 
-          sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
+        $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
+        if ($report['show_work_units']) {
+          if ($user->unit_totals_only)
+            $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";
+          else
+            $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";
+        }
+        $sql .= ", sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
           null as expenses from tt_log l 
           $group_join
           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";
       }
     } else {
-      $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, null as expenses from tt_log l 
-         $group_join $where group by $group_field";
+      $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time";
+      if ($report['show_work_units']) {
+        if ($user->unit_totals_only)
+          $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";
+        else
+          $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";
+      }
+      $sql .= ", null as expenses from tt_log l 
+        $group_join $where group by $group_field";
     }
     // By now we have sql for time items.
 
@@ -953,13 +1004,17 @@ class ttReportHelper {
       }
 
       $where = ttReportHelper::getFavExpenseWhere($report);
-      $sql_for_expenses = "select $group_field as group_field, null as time, sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei 
-        $group_join $where";
+      $sql_for_expenses = "select $group_field as group_field, null as time";
+      if ($report['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";
       // Add a "group by" clause if we are grouping.
       if ('null' != $group_field) $sql_for_expenses .= " group by $group_field";
 
       // Create a combined query.
-      $sql = "select group_field, sum(time) as time, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by group_field";
+      $combined = "select group_field, sum(time) as time";
+      if ($report['show_work_units']) $combined .= ", sum(units) as units";
+      $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t group by group_field";
+      $sql = $combined;
     }
 
     // Execute query.
@@ -979,9 +1034,9 @@ class ttReportHelper {
           $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
           $val['expenses'] = str_replace('.', $user->decimal_mark, $val['expenses']);
         }
-        $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time,'cost'=>$val['cost'],'expenses'=>$val['expenses']);
+        $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time, 'units'=> $val['units'], 'cost'=>$val['cost'],'expenses'=>$val['expenses']);
       } else
-        $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time);
+        $subtotals[$val['group_field']] = array('name'=>$val['group_field'],'time'=>$time, 'units'=> $val['units']);
     }
 
     return $subtotals;
@@ -996,37 +1051,41 @@ class ttReportHelper {
 
     $where = ttReportHelper::getWhere($bean);
 
-    // TODO: build query in parts so the work units inclusion is conditional.
-
-    // Start with a query for time items.
+    // Prepare parts.
+    $time_part = "sum(time_to_sec(l.duration)) as time";
+    if ($bean->getAttribute('chunits')) {
+      $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";
+    }
+    if ($bean->getAttribute('chcost')) {
+      if (MODE_TIME == $user->tracking_mode)
+        $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";
+      else
+        $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";
+    } else {
+      $cost_part = ", null as cost, null as expenses";
+    }
     if ($bean->getAttribute('chcost')) {
       if (MODE_TIME == $user->tracking_mode) {
-        $sql = "select sum(time_to_sec(l.duration)) as time,
-          sum(if(time_to_sec(duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(duration)/60/$user->minutes_in_unit))) as units,
-          sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
-          null as expenses 
-          from tt_log l
-          left join tt_users u on (l.user_id = u.id) $where";
+        $left_joins = "left join tt_users u on (l.user_id = u.id)";
       } else {
-        $sql = "select sum(time_to_sec(l.duration)) as time,
-          sum(if(time_to_sec(duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(duration)/60/$user->minutes_in_unit))) as units,
-          sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
-          null as expenses
-          from tt_log l
-          left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where";
+        $left_joins = "left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
       }
-    } else
-      $sql = "select sum(time_to_sec(l.duration)) as time,"
-            ." sum(if(time_to_sec(duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(duration)/60/$user->minutes_in_unit))) as units,"
-            ." null as cost, null as expenses from tt_log l $where";
+    }
+    // Prepare a query for time items.
+    $sql = "select $time_part $units_part $cost_part from tt_log l $left_joins $where";
 
     // If we have expenses, query becomes a bit more complex.
     if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) {
       $where = ttReportHelper::getExpenseWhere($bean);
-      $sql_for_expenses = "select null as time, null as units, sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
+      $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";
 
       // Create a combined query.
-      $sql = "select sum(time) as time, sum(units) as units, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
+      $combined = "select sum(time) as time";
+      if ($bean->getAttribute('chunits')) $combined .= ", sum(units) as units";
+      $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
+      $sql = $combined;
     }
 
     // Execute query.
@@ -1074,30 +1133,41 @@ class ttReportHelper {
 
     $where = ttReportHelper::getFavWhere($report);
 
-    // Start with a query for time items.
+    // Prepare parts.
+    $time_part = "sum(time_to_sec(l.duration)) as time";
+    if ($report['show_work_units']) {
+      $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";
+    }
+    if ($report['show_cost']) {
+      if (MODE_TIME == $user->tracking_mode)
+        $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";
+      else
+        $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";
+    } else {
+      $cost_part = ", null as cost, null as expenses";
+    }
     if ($report['show_cost']) {
       if (MODE_TIME == $user->tracking_mode) {
-        $sql = "select sum(time_to_sec(l.duration)) as time,
-          sum(cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
-          null as expenses 
-          from tt_log l
-          left join tt_users u on (l.user_id = u.id) $where";
+        $left_joins = "left join tt_users u on (l.user_id = u.id)";
       } else {
-        $sql = "select sum(time_to_sec(l.duration)) as time,
-          sum(cast(l.billable * coalesce(upb.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2))) as cost,
-          null as expenses
-          from tt_log l
-          left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id) $where";
+        $left_joins = "left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
       }
-    } else
-      $sql = "select sum(time_to_sec(l.duration)) as time, null as cost, null as expenses from tt_log l $where";
+    }
+    // Prepare a query for time items.
+    $sql = "select $time_part $units_part $cost_part from tt_log l $left_joins $where";
 
     // If we have expenses, query becomes a bit more complex.
     if ($report['show_cost'] && $user->isPluginEnabled('ex')) {
       $where = ttReportHelper::getFavExpenseWhere($report);
-      $sql_for_expenses = "select null as time, sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
+      $sql_for_expenses = "select null as time";
+      if ($report['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";
+
       // Create a combined query.
-      $sql = "select sum(time) as time, sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
+      $combined = "select sum(time) as time";
+      if ($report['show_work_units']) $combined .= ", sum(units) as units";
+      $combined .= ", sum(cost) as cost, sum(expenses) as expenses from (($sql) union all ($sql_for_expenses)) t";
+      $sql = $combined;
     }
 
     // Execute query.
@@ -1129,6 +1199,7 @@ class ttReportHelper {
     $totals['start_date'] = $period->getStartDate();
     $totals['end_date'] = $period->getEndDate();
     $totals['time'] = $total_time;
+    $totals['units'] = $val['units'];
     $totals['cost'] = $total_cost;
     $totals['expenses'] = $total_expenses;
 
@@ -1229,6 +1300,8 @@ class ttReportHelper {
       $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
       if ($bean->getAttribute('chduration'))
         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
+      if ($bean->getAttribute('chunits'))
+        $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
       if ($bean->getAttribute('chcost'))
         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
       $body .= '</tr>';
@@ -1240,6 +1313,11 @@ class ttReportHelper {
           if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
           $body .= '</td>';
         }
+        if ($bean->getAttribute('chunits')) {
+          $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
+          $body .= $subtotal['units'];
+          $body .= '</td>';
+        }
         if ($bean->getAttribute('chcost')) {
           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
           $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
@@ -1257,6 +1335,11 @@ class ttReportHelper {
         if ($totals['time'] <> '0:00') $body .= $totals['time'];
         $body .= '</td>';
       }
+      if ($bean->getAttribute('chunits')) {
+        $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
+        $body .= $totals['units'];
+        $body .= '</td>';
+      }
       if ($bean->getAttribute('chcost')) {
         $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
         $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
@@ -1288,6 +1371,8 @@ class ttReportHelper {
         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.finish').'</td>';
       if ($bean->getAttribute('chduration'))
         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
+      if ($bean->getAttribute('chunits'))
+        $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
       if ($bean->getAttribute('chnote'))
         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.note').'</td>';
       if ($bean->getAttribute('chcost'))
@@ -1331,6 +1416,7 @@ class ttReportHelper {
               if ($bean->getAttribute('chstart')) $body .= '<td></td>';
               if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
               if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
+              if ($bean->getAttribute('chunits')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['units'].'</td>';
               if ($bean->getAttribute('chnote')) $body .= '<td></td>';
               if ($bean->getAttribute('chcost')) {
                 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
@@ -1367,6 +1453,8 @@ class ttReportHelper {
             $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
           if ($bean->getAttribute('chduration'))
             $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
+          if ($bean->getAttribute('chunits'))
+            $body .= '<td style="'.$cellRightAligned.'">'.$record['units'].'</td>';
           if ($bean->getAttribute('chnote'))
             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
           if ($bean->getAttribute('chcost'))
@@ -1404,6 +1492,7 @@ class ttReportHelper {
         if ($bean->getAttribute('chstart')) $body .= '<td></td>';
         if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
         if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
+        if ($bean->getAttribute('chunits')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['units'].'</td>';
         if ($bean->getAttribute('chnote')) $body .= '<td></td>';
         if ($bean->getAttribute('chcost')) {
           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
@@ -1428,6 +1517,7 @@ class ttReportHelper {
       if ($bean->getAttribute('chstart')) $body .= '<td></td>';
       if ($bean->getAttribute('chfinish')) $body .= '<td></td>';
       if ($bean->getAttribute('chduration')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
+      if ($bean->getAttribute('chunits')) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['units'].'</td>';
       if ($bean->getAttribute('chnote')) $body .= '<td></td>';
       if ($bean->getAttribute('chcost')) {
         $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
@@ -1525,6 +1615,8 @@ class ttReportHelper {
       $body .= '<td style="'.$tableHeader.'">'.$group_by_header.'</td>';
       if ($report['show_duration'])
         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
+      if ($report['show_work_units'])
+        $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
       if ($report['show_cost'])
         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.cost').'</td>';
       $body .= '</tr>';
@@ -1536,6 +1628,11 @@ class ttReportHelper {
           if ($subtotal['time'] <> '0:00') $body .= $subtotal['time'];
           $body .= '</td>';
         }
+        if ($report['show_work_units']) {
+          $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
+          $body .= $subtotal['units'];
+          $body .= '</td>';
+        }
         if ($report['show_cost']) {
           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
           $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses'];
@@ -1553,6 +1650,11 @@ class ttReportHelper {
         if ($totals['time'] <> '0:00') $body .= $totals['time'];
         $body .= '</td>';
       }
+      if ($report['show_work_units']) {
+        $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
+        $body .= $totals['units'];
+        $body .= '</td>';
+      }
       if ($report['show_cost']) {
         $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';
         $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses'];
@@ -1584,6 +1686,8 @@ class ttReportHelper {
         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.finish').'</td>';
       if ($report['show_duration'])
         $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.duration').'</td>';
+      if ($report['show_work_units'])
+        $body .= '<td style="'.$tableHeaderCentered.'" width="5%">'.$i18n->get('label.work_units_short').'</td>';
       if ($report['show_note'])
         $body .= '<td style="'.$tableHeader.'">'.$i18n->get('label.note').'</td>';
       if ($report['show_cost'])
@@ -1627,6 +1731,7 @@ class ttReportHelper {
               if ($report['show_start']) $body .= '<td></td>';
               if ($report['show_end']) $body .= '<td></td>';
               if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
+              if ($report['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['units'].'</td>';
               if ($report['show_note']) $body .= '<td></td>';
               if ($report['show_cost']) {
                 $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
@@ -1663,6 +1768,8 @@ class ttReportHelper {
             $body .= '<td nowrap style="'.$cellRightAligned.'">'.$record['finish'].'</td>';
           if ($report['show_duration'])
             $body .= '<td style="'.$cellRightAligned.'">'.$record['duration'].'</td>';
+          if ($report['show_work_units'])
+            $body .= '<td style="'.$cellRightAligned.'">'.$record['units'].'</td>';
           if ($report['show_note'])
             $body .= '<td style="'.$cellLeftAligned.'">'.htmlspecialchars($record['note']).'</td>';
           if ($report['show_cost'])
@@ -1700,6 +1807,7 @@ class ttReportHelper {
         if ($report['show_start']) $body .= '<td></td>';
         if ($report['show_end']) $body .= '<td></td>';
         if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
+        if ($report['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['units'].'</td>';
         if ($report['show_note']) $body .= '<td></td>';
         if ($report['show_cost']) {
           $body .= '<td style="'.$cellRightAlignedSubtotal.'">';
@@ -1724,6 +1832,7 @@ class ttReportHelper {
       if ($report['show_start']) $body .= '<td></td>';
       if ($report['show_end']) $body .= '<td></td>';
       if ($report['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['time'].'</td>';
+      if ($report['show_work_units']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$totals['units'].'</td>';
       if ($report['show_note']) $body .= '<td></td>';
       if ($report['show_cost']) {
         $body .= '<td nowrap style="'.$cellRightAlignedSubtotal.'">'.htmlspecialchars($user->currency).' ';