From 19ec0f6c43f06c7925306763911e7c56c13b325c Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Thu, 13 Oct 2011 11:02:23 -0700 Subject: [PATCH] [#6636] Add Deliverable#hours_spent_for_user to get the billable/non-billable hours by user --- app/models/deliverable.rb | 6 ++++++ test/unit/deliverable_test.rb | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/app/models/deliverable.rb b/app/models/deliverable.rb index 019aa73..b76be56 100644 --- a/app/models/deliverable.rb +++ b/app/models/deliverable.rb @@ -337,6 +337,12 @@ class Deliverable < ActiveRecord::Base end 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]) + + time_entries.select {|time| time.billable? == billable_time_only }.sum(&:hours) + end + def self.valid_types ['FixedDeliverable','HourlyDeliverable','RetainerDeliverable'] end diff --git a/test/unit/deliverable_test.rb b/test/unit/deliverable_test.rb index 62f08e7..fd3c523 100644 --- a/test/unit/deliverable_test.rb +++ b/test/unit/deliverable_test.rb @@ -244,4 +244,39 @@ class DeliverableTest < ActiveSupport::TestCase end end + context "#hours_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 + }) + create_issue_with_time_for_deliverable(@deliverable, { + :activity => @billable_activity, + :user => @manager, + :hours => 5 + }) + create_issue_with_time_for_deliverable(@deliverable, { + :activity => @non_billable_activity, + :user => @manager, + :hours => 5 + }) + + end + + context "with billable_time_only as true" do + should "return the total number of hours the user has logged on billable time entries" do + assert_equal 10, @deliverable.hours_spent_for_user(@manager, true) + end + end + + context "with billable_time_only as false" do + should "return the total number of hours the user has logged on non-billable time entries" do + assert_equal 5, @deliverable.hours_spent_for_user(@manager, false) + end + end + + end end