diff --git a/app/models/issue.rb b/app/models/issue.rb index 9cf2953..f35016e 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -1549,14 +1549,38 @@ class Issue < ActiveRecord::Base unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio child_count = p.children.count if child_count > 0 - average = p.children.where("estimated_hours > 0").average(:estimated_hours).to_f - if average == 0 - average = 1 + # Calculate the average hours for all child tasks using the childrens total_estimated_hours. + sum = 0 + p.children.each do |child| + sum += child.total_estimated_hours end - done = p.children.joins(:status). - sum("COALESCE(CASE WHEN estimated_hours > 0 THEN estimated_hours ELSE NULL END, #{average}) " + - "* (CASE WHEN is_closed = #{self.class.connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)").to_f - progress = done / (average * child_count) + if sum == 0 + average_hours = 1 + else + average_hours = sum / child_count + end + + # Calculate the total work done for the children + # Each child contribute to the total work with its total_estimate_hours multiplied + # by its done_ratio or 100 if the child is closed. + # If the childs total_estiamted_hours is zero then use the average_hours for + # that child. + done = 0 + p.children.each do |child| + child_hours = child.total_estimated_hours + if child_hours == 0 then + child_hours = average_hours + end + # calculate the contribution to done for the child + if child.closed? then + done += child_hours * 100 + else + done += child_hours * child.done_ratio + end + end + + # Calculate the progress + progress = done / (average_hours * child_count) p.done_ratio = progress.round end end