diff --git a/app/models/fixed_deliverable.rb b/app/models/fixed_deliverable.rb index 9eccb75..462b414 100644 --- a/app/models/fixed_deliverable.rb +++ b/app/models/fixed_deliverable.rb @@ -6,10 +6,25 @@ class FixedDeliverable < Deliverable 0 end - # Returns the amount spent. It will always be the fixed cost because - # that money has been allocated already and is managed by the user + # Returns the amount spent. It will always be the fixed cost plus logged time + # because that money has been allocated already and is managed by the user. def spent - self.fixed_cost || 0.0 + return 0.0 if self.fixed_cost.nil? + return self.fixed_cost unless self.issues.size > 0 + + total = fixed_cost.to_f + + # Get all timelogs assigned + time_logs = self.issues.collect(&:time_entries).flatten + + # Find each Member for their rate + time_logs.each do |time_log| + member = Member.find_by_user_id_and_project_id(time_log.user_id, time_log.project_id) + total += (member.rate * time_log.hours) unless member.nil? || member.rate.nil? + end + + return total + end def profit # :nodoc: diff --git a/spec/models/fixed_deliverable_spec.rb b/spec/models/fixed_deliverable_spec.rb index 4116c7a..14ee7ee 100644 --- a/spec/models/fixed_deliverable_spec.rb +++ b/spec/models/fixed_deliverable_spec.rb @@ -18,6 +18,26 @@ describe FixedDeliverable, '.spent' do @deliverable = FixedDeliverable.new({ :subject => 'test' }) @deliverable.spent.should eql(0.0) end + + it 'should always equal the sum of the fixed cost and any logged hours' do + + @project = mock_model(Project) + @user = mock_model(User) + @issue1 = mock_model(Issue) + + @issue_1_time_entry = mock_model(TimeEntry, :issue_id => @issue1.id, :user_id => @user.id, :project_id => @project.id, :hours => 1.0) + @issue1.stub!(:time_entries).and_return([@issue_1_time_entry]) + + @member = mock_model(Member, :user => @user, :project => @project, :rate => 60.0) + Member.should_receive(:find_by_user_id_and_project_id).with(@user.id, @project.id).and_return(@member) + + @deliverable = FixedDeliverable.new({ :subject => 'test' }) + @issues = [@issue1] + @deliverable.stub!(:fixed_cost).and_return(5000.0) + @deliverable.should_receive(:issues).twice.and_return(@issues) + + @deliverable.spent.should eql(5060.0) + end end describe FixedDeliverable, '.profit as a %' do