diff --git a/app/models/contract.rb b/app/models/contract.rb index 9407b02..403ba74 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, - :fixed_spent, :fixed_budget, :total_spent, :total_budget, + :fixed_spent, :fixed_budget, :total_spent, :markup_spent, :markup_budget, :profit_spent, :profit_budget, :discount_spent, :discount_budget, :client_point_of_contact, :estimated_hour_spent @@ -48,10 +48,16 @@ class Contract < ActiveRecord::Base deliverables.inject(0) {|total, deliverable| total += deliverable.overhead_budget_total } end + # OPTIMIZE: N+1 def estimated_hour_budget deliverables.inject(0) {|total, deliverable| total += deliverable.estimated_hour_budget_total } end + # OPTIMIZE: N+1 + def total_budget + deliverables.inject(0) {|total, deliverable| total += deliverable.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 535cd40..4a2ec60 100644 --- a/test/unit/contract_test.rb +++ b/test/unit/contract_test.rb @@ -68,4 +68,16 @@ class ContractTest < ActiveSupport::TestCase assert_equal 110, contract.estimated_hour_budget end end + + context "#total_budget" do + should "sum all of the totals of the Deliverables" do + contract = Contract.generate!(:billable_rate => 100.0) + contract.deliverables << @deliverable_1 = FixedDeliverable.generate!(:total => 10_000) + contract.deliverables << @deliverable_2 = HourlyDeliverable.generate! + LaborBudget.generate!(:deliverable => @deliverable_2, :hours => 10) + OverheadBudget.generate!(:deliverable => @deliverable_2, :hours => 20) + + assert_equal 10_000 + (30 * 100), contract.total_budget + end + end end