diff --git a/app/models/retainer_deliverable.rb b/app/models/retainer_deliverable.rb index 2898718..5b558cf 100644 --- a/app/models/retainer_deliverable.rb +++ b/app/models/retainer_deliverable.rb @@ -111,6 +111,94 @@ class RetainerDeliverable < HourlyDeliverable end end + # TODO: stolen directly from redmine_overhead but with a block option + def labor_budget_spent_with_filter(&block) + return 0.0 unless self.issues.size > 0 + total = 0.0 + + # Get all timelogs assigned + if block_given? + time_logs = block.call + else + time_logs = self.issues.collect(&:time_entries).flatten + end + + return time_logs.collect {|time_log| + if time_log.billable? + time_log.cost + else + 0.0 + end + }.sum + end + + # TODO: stolen directly from redmine_overhead but with a block option + def overhead_spent_with_filter(&block) + if block_given? + time_logs = block.call + else + time_logs = issues.collect(&:time_entries).flatten + end + + return time_logs.collect {|time_entry| + if time_entry.billable? + 0 + else + time_entry.cost + end + }.sum + end + + def labor_budget_spent(date=nil) + if date + if within_date_range?(date) + labor_budget_spent_with_filter do + issue_ids = self.issues.collect(&:id) + if issue_ids.present? + TimeEntry.all(:conditions => ["#{Issue.table_name}.id IN (:issue_ids) AND tyear = (:year) AND tmonth = (:month)", + {:issue_ids => issue_ids, + :year => date.year, + :month => date.month} + ], + :include => :issue) + else + [] + end + end + else + 0 # outside of range + end + else + labor_budget_spent_with_filter + end + + end + + def overhead_spent(date=nil) + if date + if within_date_range?(date) + overhead_spent_with_filter do + issue_ids = self.issues.collect(&:id) + if issue_ids.present? + TimeEntry.all(:conditions => ["#{Issue.table_name}.id IN (:issue_ids) AND tyear = (:year) AND tmonth = (:month)", + {:issue_ids => issue_ids, + :year => date.year, + :month => date.month} + ], + :include => :issue) + else + [] + end + end + else + 0 # outside of range + end + else + overhead_spent_with_filter + end + + end + def create_budgets_for_periods # For each month in the time span months.each do |month| diff --git a/test/unit/retainer_deliverable_test.rb b/test/unit/retainer_deliverable_test.rb index 7f12696..577f91a 100644 --- a/test/unit/retainer_deliverable_test.rb +++ b/test/unit/retainer_deliverable_test.rb @@ -81,16 +81,66 @@ class RetainerDeliverableTest < ActiveSupport::TestCase end context "#labor_budget_spent" do + setup do + @project = Project.generate! + @contract = Contract.generate!(:billable_rate => 100, :project => @project) + @deliverable = RetainerDeliverable.generate!(:start_date => '2010-01-01', :end_date => '2010-03-31', :contract => @contract) + @deliverable.labor_budgets << LaborBudget.spawn(:budget => 100, :hours => 10) + @deliverable.overhead_budgets << OverheadBudget.spawn(:budget => 100, :hours => 10) + @deliverable.save! + + @manager = User.generate! + @role = Role.generate! + User.add_to_project(@manager, @project, @role) + + configure_overhead_plugin + + @issue1 = Issue.generate_for_project!(@project) + @time_entry1 = TimeEntry.generate!(:issue => @issue1, + :project => @project, + :activity => @billable_activity, + :spent_on => Date.new(2010,1,2), + :hours => 10, + :user => @manager) + @time_entry2 = TimeEntry.generate!(:issue => @issue1, + :project => @project, + :activity => @billable_activity, + :spent_on => Date.new(2010,2,1), + :hours => 20, + :user => @manager) + + @rate = Rate.generate!(:project => @project, + :user => @manager, + :date_in_effect => Date.new(2010,1,1), + :amount => 100) + + @deliverable.issues << @issue1 + + + end + context "with a empty period" do - should "use all periods" + should "use all periods" do + assert_equal (10+20) * 100, @deliverable.labor_budget_spent(nil) + end end context "with a period out of the retainer range" do - should "use all periods" + should "filter the records periods" do + assert_equal 0, @deliverable.labor_budget_spent(Date.new(2011,1,1)) + end + end + + context "with an invalid period" do + should "return 0" do + assert_equal 0, @deliverable.labor_budget_spent('1') + end end context "with a period in the retainer range" do - should "filter the records" + should "filter the records" do + assert_equal 20 * 100, @deliverable.labor_budget_spent(Date.new(2010,2,1)) + end end end @@ -126,8 +176,69 @@ class RetainerDeliverableTest < ActiveSupport::TestCase end end - # context "#overhead_spent" - + context "#overhead_spent" do + setup do + @project = Project.generate! + @contract = Contract.generate!(:billable_rate => 100, :project => @project) + @deliverable = RetainerDeliverable.generate!(:start_date => '2010-01-01', :end_date => '2010-03-31', :contract => @contract) + @deliverable.overhead_budgets << OverheadBudget.spawn(:budget => 100, :hours => 10) + @deliverable.save! + + @manager = User.generate! + @role = Role.generate! + User.add_to_project(@manager, @project, @role) + + configure_overhead_plugin + + @issue1 = Issue.generate_for_project!(@project) + @time_entry1 = TimeEntry.generate!(:issue => @issue1, + :project => @project, + :activity => @non_billable_activity, + :spent_on => Date.new(2010,1,2), + :hours => 10, + :user => @manager) + @time_entry2 = TimeEntry.generate!(:issue => @issue1, + :project => @project, + :activity => @non_billable_activity, + :spent_on => Date.new(2010,2,1), + :hours => 20, + :user => @manager) + + @rate = Rate.generate!(:project => @project, + :user => @manager, + :date_in_effect => Date.new(2010,1,1), + :amount => 100) + + @deliverable.issues << @issue1 + + + end + + context "with a empty period" do + should "use all periods" do + assert_equal (10+20) * 100, @deliverable.overhead_spent(nil) + end + end + + context "with a period out of the retainer range" do + should "filter the records periods" do + assert_equal 0, @deliverable.overhead_spent(Date.new(2011,1,1)) + end + end + + context "with an invalid period" do + should "return 0" do + assert_equal 0, @deliverable.overhead_spent('1') + end + end + + context "with a period in the retainer range" do + should "filter the records" do + assert_equal 20 * 100, @deliverable.overhead_spent(Date.new(2010,2,1)) + end + end + end + context "#overhead_budget_total" do setup do @deliverable = RetainerDeliverable.generate!(:start_date => '2010-01-01', :end_date => '2010-03-31')