diff --git a/app/models/hourly_deliverable.rb b/app/models/hourly_deliverable.rb index 91afde2..e183114 100644 --- a/app/models/hourly_deliverable.rb +++ b/app/models/hourly_deliverable.rb @@ -21,6 +21,22 @@ class HourlyDeliverable < Deliverable return contract.billable_rate * labor_budgets.sum(:hours) end + + # Total amount to be billed on the deliverable, using the total time logged + # and the contract rate + def total_spent + return 0 if contract.nil? + return 0 if contract.billable_rate.blank? + return 0 unless self.issues.count > 0 + + time_logs = self.issues.collect(&:time_entries).flatten + hours = time_logs.inject(0) {|total, time_entry| + total += time_entry.hours if time_entry.billable? + total + } + + return hours * contract.billable_rate + end # Block setting the total on HourlyDeliverables def total=(v) diff --git a/test/unit/hourly_deliverable_test.rb b/test/unit/hourly_deliverable_test.rb index 2117799..d1e5ffc 100644 --- a/test/unit/hourly_deliverable_test.rb +++ b/test/unit/hourly_deliverable_test.rb @@ -29,6 +29,26 @@ class HourlyDeliverableTest < ActiveSupport::TestCase assert_equal 100.0 * 10, d.total end end + + context "#total_spent" do + should "be equal to the number of hours used multipled by the contract rate" do + configure_overhead_plugin + + contract = Contract.generate!(:billable_rate => 150.0) + @project = Project.generate! + @developer = User.generate! + @role = Role.generate! + User.add_to_project(@developer, @project, @role) + + d = HourlyDeliverable.generate!(:contract => contract) + d.issues << @issue1 = Issue.generate_for_project!(@project) + TimeEntry.generate!(:hours => 15, :issue => @issue1, :project => @project, + :activity => @billable_activity, + :user => @developer) + + assert_equal 2250, d.total_spent + end + end context "#total=" do should "not write any attributes" do