diff --git a/app/models/contract.rb b/app/models/contract.rb index f8360e0..8c6a284 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -30,7 +30,7 @@ class Contract < ActiveRecord::Base attr_accessible :details [:status, :contract_type, :labor_spent, :overhead_spent, - :overhead_budget, :fixed_spent, :fixed_budget, :total_spent, :total_budget, + :fixed_spent, :fixed_budget, :total_spent, :total_budget, :markup_spent, :markup_budget, :profit_spent, :profit_budget, :discount_spent, :discount_budget, :client_point_of_contact, :estimated_hour_spent, :estimated_hour_budget @@ -42,7 +42,12 @@ class Contract < ActiveRecord::Base def labor_budget deliverables.inject(0) {|total, deliverable| total += deliverable.labor_budget_total } end - + + # OPTIMIZE: N+1 + def overhead_budget + deliverables.inject(0) {|total, deliverable| total += deliverable.overhead_budget_total } + end + PaymentTerms = { :net_0 => :text_payment_terms_net_0, :net_15 => :text_payment_terms_net_15, diff --git a/test/unit/contract_test.rb b/test/unit/contract_test.rb index 05501ef..97dd511 100644 --- a/test/unit/contract_test.rb +++ b/test/unit/contract_test.rb @@ -44,4 +44,16 @@ class ContractTest < ActiveSupport::TestCase assert_equal 200, contract.labor_budget end end + + context "#overhead_budget" do + should "sum all of the overhead budgets of the Deliverables" do + contract = Contract.generate! + contract.deliverables << @deliverable_1 = FixedDeliverable.generate! + OverheadBudget.generate!(:deliverable => @deliverable_1, :budget => 100) + contract.deliverables << @deliverable_2 = FixedDeliverable.generate! + OverheadBudget.generate!(:deliverable => @deliverable_2, :budget => 100) + + assert_equal 200, contract.overhead_budget + end + end end