X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=WEB-INF%2Flib%2FttReportHelper.class.php;h=0900f7f9d0e440717d5bbcfc8886b689e3373503;hb=8ec1dac1bcf71ce282ab116413d335f582b5e8f3;hp=67f1da26326a0d0668ead26aad202c3afe28ddf0;hpb=45c855269d952873285f7a835e82fc2b3eff3971;p=timetracker.git diff --git a/WEB-INF/lib/ttReportHelper.class.php b/WEB-INF/lib/ttReportHelper.class.php index 67f1da26..0900f7f9 100644 --- a/WEB-INF/lib/ttReportHelper.class.php +++ b/WEB-INF/lib/ttReportHelper.class.php @@ -276,6 +276,9 @@ 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')) + 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'); @@ -380,6 +383,10 @@ 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) @@ -541,6 +548,10 @@ 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']) + 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'); @@ -624,6 +635,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'); // null for work units. // Use the note field to print item name. if ($report['show_note']) array_push($fields, 'ei.name as note'); @@ -769,21 +782,25 @@ 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, + $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, + 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, 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, + $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, + 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, 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, + 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, + null as expenses from tt_log l + $group_join $where group by $group_field"; } // By now we have sql for time items. @@ -813,13 +830,13 @@ class ttReportHelper { } $where = ttReportHelper::getExpenseWhere($bean); - $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 + $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"; // 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"; + $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"; } // Execute query. @@ -839,9 +856,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; @@ -897,21 +914,25 @@ 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, + $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, + 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, 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, + $sql = "select $group_field as group_field, sum(time_to_sec(l.duration)) as time, + 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, 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, + 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, + null as expenses from tt_log l + $group_join $where group by $group_field"; } // By now we have sql for time items. @@ -941,13 +962,13 @@ 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 + $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"; // 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"; + $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"; } // Execute query. @@ -967,9 +988,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; @@ -984,30 +1005,37 @@ 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. if ($bean->getAttribute('chcost')) { if (MODE_TIME == $user->tracking_mode) { $sql = "select sum(time_to_sec(l.duration)) as time, + 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, 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"; } else { $sql = "select sum(time_to_sec(l.duration)) as time, + 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, 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"; } } else - $sql = "select sum(time_to_sec(l.duration)) as time, null as cost, null as expenses from tt_log l $where"; + $sql = "select sum(time_to_sec(l.duration)) as time," + ." 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," + ." null as cost, null as expenses from tt_log l $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, sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where"; + $sql_for_expenses = "select null as time, null as units, 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"; + $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"; } // Execute query. @@ -1039,6 +1067,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; @@ -1058,26 +1087,30 @@ class ttReportHelper { if ($report['show_cost']) { if (MODE_TIME == $user->tracking_mode) { $sql = "select sum(time_to_sec(l.duration)) as time, + 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, 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"; } else { $sql = "select sum(time_to_sec(l.duration)) as time, + 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, 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"; } } else - $sql = "select sum(time_to_sec(l.duration)) as time, null as cost, null as expenses from tt_log l $where"; + $sql = "select sum(time_to_sec(l.duration)) as time, + 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, + null as cost, null as expenses from tt_log l $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, null as units, 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"; + $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"; } // Execute query. @@ -1109,6 +1142,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; @@ -1209,6 +1243,8 @@ class ttReportHelper { $body .= ''.$group_by_header.''; if ($bean->getAttribute('chduration')) $body .= ''.$i18n->get('label.duration').''; + if ($bean->getAttribute('chunits')) + $body .= ''.$i18n->get('label.work_units_short').''; if ($bean->getAttribute('chcost')) $body .= ''.$i18n->get('label.cost').''; $body .= ''; @@ -1220,6 +1256,11 @@ class ttReportHelper { if ($subtotal['time'] <> '0:00') $body .= $subtotal['time']; $body .= ''; } + if ($bean->getAttribute('chunits')) { + $body .= ''; + $body .= $subtotal['units']; + $body .= ''; + } if ($bean->getAttribute('chcost')) { $body .= ''; $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses']; @@ -1237,6 +1278,11 @@ class ttReportHelper { if ($totals['time'] <> '0:00') $body .= $totals['time']; $body .= ''; } + if ($bean->getAttribute('chunits')) { + $body .= ''; + $body .= $totals['units']; + $body .= ''; + } if ($bean->getAttribute('chcost')) { $body .= ''.htmlspecialchars($user->currency).' '; $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses']; @@ -1268,6 +1314,8 @@ class ttReportHelper { $body .= ''.$i18n->get('label.finish').''; if ($bean->getAttribute('chduration')) $body .= ''.$i18n->get('label.duration').''; + if ($bean->getAttribute('chunits')) + $body .= ''.$i18n->get('label.work_units_short').''; if ($bean->getAttribute('chnote')) $body .= ''.$i18n->get('label.note').''; if ($bean->getAttribute('chcost')) @@ -1311,6 +1359,7 @@ class ttReportHelper { if ($bean->getAttribute('chstart')) $body .= ''; if ($bean->getAttribute('chfinish')) $body .= ''; if ($bean->getAttribute('chduration')) $body .= ''.$subtotals[$prev_grouped_by]['time'].''; + if ($bean->getAttribute('chunits')) $body .= ''.$subtotals[$prev_grouped_by]['units'].''; if ($bean->getAttribute('chnote')) $body .= ''; if ($bean->getAttribute('chcost')) { $body .= ''; @@ -1347,6 +1396,8 @@ class ttReportHelper { $body .= ''.$record['finish'].''; if ($bean->getAttribute('chduration')) $body .= ''.$record['duration'].''; + if ($bean->getAttribute('chunits')) + $body .= ''.$record['units'].''; if ($bean->getAttribute('chnote')) $body .= ''.htmlspecialchars($record['note']).''; if ($bean->getAttribute('chcost')) @@ -1384,6 +1435,7 @@ class ttReportHelper { if ($bean->getAttribute('chstart')) $body .= ''; if ($bean->getAttribute('chfinish')) $body .= ''; if ($bean->getAttribute('chduration')) $body .= ''.$subtotals[$cur_grouped_by]['time'].''; + if ($bean->getAttribute('chunits')) $body .= ''.$subtotals[$cur_grouped_by]['units'].''; if ($bean->getAttribute('chnote')) $body .= ''; if ($bean->getAttribute('chcost')) { $body .= ''; @@ -1408,6 +1460,7 @@ class ttReportHelper { if ($bean->getAttribute('chstart')) $body .= ''; if ($bean->getAttribute('chfinish')) $body .= ''; if ($bean->getAttribute('chduration')) $body .= ''.$totals['time'].''; + if ($bean->getAttribute('chunits')) $body .= ''.$totals['units'].''; if ($bean->getAttribute('chnote')) $body .= ''; if ($bean->getAttribute('chcost')) { $body .= ''.htmlspecialchars($user->currency).' '; @@ -1505,6 +1558,8 @@ class ttReportHelper { $body .= ''.$group_by_header.''; if ($report['show_duration']) $body .= ''.$i18n->get('label.duration').''; + if ($report['show_work_units']) + $body .= ''.$i18n->get('label.work_units_short').''; if ($report['show_cost']) $body .= ''.$i18n->get('label.cost').''; $body .= ''; @@ -1516,6 +1571,11 @@ class ttReportHelper { if ($subtotal['time'] <> '0:00') $body .= $subtotal['time']; $body .= ''; } + if ($report['show_work_units']) { + $body .= ''; + $body .= $subtotal['units']; + $body .= ''; + } if ($report['show_cost']) { $body .= ''; $body .= ($canViewReports || $isClient) ? $subtotal['cost'] : $subtotal['expenses']; @@ -1533,6 +1593,11 @@ class ttReportHelper { if ($totals['time'] <> '0:00') $body .= $totals['time']; $body .= ''; } + if ($report['show_work_units']) { + $body .= ''; + $body .= $totals['units']; + $body .= ''; + } if ($report['show_cost']) { $body .= ''.htmlspecialchars($user->currency).' '; $body .= ($canViewReports || $isClient) ? $totals['cost'] : $totals['expenses']; @@ -1564,6 +1629,8 @@ class ttReportHelper { $body .= ''.$i18n->get('label.finish').''; if ($report['show_duration']) $body .= ''.$i18n->get('label.duration').''; + if ($report['show_work_units']) + $body .= ''.$i18n->get('label.work_units_short').''; if ($report['show_note']) $body .= ''.$i18n->get('label.note').''; if ($report['show_cost']) @@ -1607,6 +1674,7 @@ class ttReportHelper { if ($report['show_start']) $body .= ''; if ($report['show_end']) $body .= ''; if ($report['show_duration']) $body .= ''.$subtotals[$prev_grouped_by]['time'].''; + if ($report['show_work_units']) $body .= ''.$subtotals[$prev_grouped_by]['units'].''; if ($report['show_note']) $body .= ''; if ($report['show_cost']) { $body .= ''; @@ -1643,6 +1711,8 @@ class ttReportHelper { $body .= ''.$record['finish'].''; if ($report['show_duration']) $body .= ''.$record['duration'].''; + if ($report['show_work_units']) + $body .= ''.$record['units'].''; if ($report['show_note']) $body .= ''.htmlspecialchars($record['note']).''; if ($report['show_cost']) @@ -1680,6 +1750,7 @@ class ttReportHelper { if ($report['show_start']) $body .= ''; if ($report['show_end']) $body .= ''; if ($report['show_duration']) $body .= ''.$subtotals[$cur_grouped_by]['time'].''; + if ($report['show_work_units']) $body .= ''.$subtotals[$cur_grouped_by]['units'].''; if ($report['show_note']) $body .= ''; if ($report['show_cost']) { $body .= ''; @@ -1704,6 +1775,7 @@ class ttReportHelper { if ($report['show_start']) $body .= ''; if ($report['show_end']) $body .= ''; if ($report['show_duration']) $body .= ''.$totals['time'].''; + if ($report['show_work_units']) $body .= ''.$totals['units'].''; if ($report['show_note']) $body .= ''; if ($report['show_cost']) { $body .= ''.htmlspecialchars($user->currency).' ';