]> source.dussan.org Git - redmine.git/commitdiff
Fixed that progress of parent should be calculated with total estimated hours of...
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 1 Sep 2016 17:03:17 +0000 (17:03 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 1 Sep 2016 17:03:17 +0000 (17:03 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@15802 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue.rb
test/unit/issue_subtasking_test.rb

index a8d5e52b704897b792d967cb7aa5857ec6863942..6cbb180336931260d47db4df7a930cb2d2978099 100644 (file)
@@ -1553,18 +1553,23 @@ class Issue < ActiveRecord::Base
       end
 
       if p.done_ratio_derived?
-        # done ratio = weighted average ratio of leaves
+        # done ratio = average ratio of children weighted with their total estimated hours
         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
+          children = p.children.to_a
+          if children.any?
+            child_with_total_estimated_hours = children.select {|c| c.total_estimated_hours.to_f > 0.0}
+            if child_with_total_estimated_hours.any?
+              average = child_with_total_estimated_hours.map(&:total_estimated_hours).sum.to_f / child_with_total_estimated_hours.count
+            else
+              average = 1.0
             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)
+            done = children.map {|c|
+              estimated = c.total_estimated_hours.to_f
+              estimated = average unless estimated > 0.0
+              ratio = c.closed? ? 100 : (c.done_ratio || 0)
+              estimated * ratio
+            }.sum
+            progress = done / (average * children.count)
             p.done_ratio = progress.round
           end
         end
index 189c6988ce22f0c08128f4b4d5e82cbac242f747..020bcfdfc727f3ca3777ba83bcbb47ebe5931372 100644 (file)
@@ -143,6 +143,26 @@ class IssueSubtaskingTest < ActiveSupport::TestCase
     end
   end
 
+  def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any_with_grandchildren
+    # parent
+    #   child 1 (2h estd, 0% done)
+    #   child 2 (no estd)
+    #     child a (2h estd, 50% done)
+    #     child b (2h estd, 50% done)
+    #
+    # => parent should have a calculated progress of 33%
+    #
+    with_settings :parent_issue_done_ratio => 'derived' do
+      parent = Issue.generate!
+      parent.generate_child!(:estimated_hours => 2, :done_ratio => 0)
+      child = parent.generate_child!
+      child.generate_child!(:estimated_hours => 2, :done_ratio => 50)
+      child.generate_child!(:estimated_hours => 2, :done_ratio => 50)
+      assert_equal 50, child.reload.done_ratio
+      assert_equal 33, parent.reload.done_ratio
+    end
+  end
+
   def test_parent_done_ratio_with_child_estimate_to_0_should_reach_100
     with_settings :parent_issue_done_ratio => 'derived' do
       parent = Issue.generate!