From f972efffbff8fe72b354cedaaa7eabcc5ced964a Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Thu, 13 Oct 2011 11:07:31 -0700 Subject: [PATCH] [#6636] Add Deliverable#spent_for_user to get the billable/non-billable cost by user --- app/models/deliverable.rb | 6 ++++++ test/unit/deliverable_test.rb | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/app/models/deliverable.rb b/app/models/deliverable.rb index b76be56..d832d69 100644 --- a/app/models/deliverable.rb +++ b/app/models/deliverable.rb @@ -337,6 +337,12 @@ class Deliverable < ActiveRecord::Base end end + def spent_for_user(user, billable_time_only) + time_entries = project.time_entries.all(:conditions => ["#{TimeEntry.table_name}.issue_id IN (?) AND #{TimeEntry.table_name}.user_id IN (?)", issue_ids, user.id]) + + time_entries.select {|time| time.billable? == billable_time_only }.sum(&:cost) + end + def hours_spent_for_user(user, billable_time_only) time_entries = project.time_entries.all(:conditions => ["#{TimeEntry.table_name}.issue_id IN (?) AND #{TimeEntry.table_name}.user_id IN (?)", issue_ids, user.id]) diff --git a/test/unit/deliverable_test.rb b/test/unit/deliverable_test.rb index fd3c523..7835179 100644 --- a/test/unit/deliverable_test.rb +++ b/test/unit/deliverable_test.rb @@ -279,4 +279,43 @@ class DeliverableTest < ActiveSupport::TestCase end end + + context "#spent_for_user" do + setup do + configure_overhead_plugin + create_contract_and_deliverable + create_issue_with_time_for_deliverable(@deliverable, { + :activity => @billable_activity, + :user => @manager, + :hours => 5, + :amount => 100 + }) + create_issue_with_time_for_deliverable(@deliverable, { + :activity => @billable_activity, + :user => @manager, + :hours => 5, + :amount => 100 + }) + create_issue_with_time_for_deliverable(@deliverable, { + :activity => @non_billable_activity, + :user => @manager, + :hours => 5, + :amount => 100 + }) + + end + + context "with billable_time_only as true" do + should "return the total cost the user has logged on billable time entries" do + assert_equal 1000, @deliverable.spent_for_user(@manager, true) + end + end + + context "with billable_time_only as false" do + should "return the total cost the user has logged on non-billable time entries" do + assert_equal 500, @deliverable.spent_for_user(@manager, false) + end + end + + end end