diff --git a/app/models/contract.rb b/app/models/contract.rb index 0e65756..2db8ae4 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -36,8 +36,8 @@ class Contract < ActiveRecord::Base named_scope :by_name, {:order => "#{Contract.table_name}.name ASC"} [:status, :contract_type, - :fixed_spent, :fixed_budget, - :markup_spent, :markup_budget, + :fixed_spent, + :fixed_markup_spent, :discount_spent, :discount_budget ].each do |mthd| define_method(mthd) { "TODO in later release" } @@ -96,6 +96,16 @@ class Contract < ActiveRecord::Base end alias_method :profit_spent, :profit_left + # OPTIMIZE: N+1 + def fixed_budget + deliverables.inject(0) {|total, deliverable| total += deliverable.fixed_budget_total } + end + + # OPTIMIZE: N+1 + def fixed_markup_budget + deliverables.inject(0) {|total, deliverable| total += deliverable.fixed_markup_budget_total } + end + def after_initialize self.executed = false unless self.executed.present? end diff --git a/app/models/deliverable.rb b/app/models/deliverable.rb index 9f2f677..d9f356e 100644 --- a/app/models/deliverable.rb +++ b/app/models/deliverable.rb @@ -88,6 +88,15 @@ class Deliverable < ActiveRecord::Base issues.inject(0) {|total, issue| total += issue.spent_hours } end + def fixed_budget_total(date=nil) + fixed_budgets.sum(:budget) + end + + # OPTIMIZE: N+1 + def fixed_markup_budget_total(date=nil) + fixed_budgets.inject(0) {|total, fixed_budget| total += fixed_budget.markup_value } + end + def filter_by_date(date=nil, &block) block.call end diff --git a/app/views/contracts/show.html.erb b/app/views/contracts/show.html.erb index 55a4254..bc54681 100644 --- a/app/views/contracts/show.html.erb +++ b/app/views/contracts/show.html.erb @@ -44,7 +44,7 @@ <%= show_budget_field(resource, :labor_spent, :labor_budget, :format => :format_value_field_for_contracts, :html_options => {:class => 'contract-labor'}, :label_html_options => {:width => '46%'}) %> <%= show_budget_field(resource, :overhead_spent, :overhead_budget, :format => :format_value_field_for_contracts, :html_options => {:class => 'contract-overhead'}) %> <%= show_budget_field(resource, :fixed_spent, :fixed_budget, :format => :format_value_field_for_contracts, :html_options => {:class => 'contract-fixed'}) %> - <%= show_budget_field(resource, :markup_spent, :markup_budget, :format => :format_value_field_for_contracts, :html_options => {:class => 'contract-markup'}) %> + <%= show_budget_field(resource, :fixed_markup_spent, :fixed_markup_budget, :format => :format_value_field_for_contracts, :html_options => {:class => 'contract-markup'}) %> <%= show_budget_field(resource, :profit_spent, :profit_budget, :format => :format_value_field_for_contracts, :html_options => {:class => 'contract-profit'}) %> <%= show_budget_field(resource, :discount_spent, :discount_budget, :format => :format_value_field_for_contracts, :html_options => {:class => 'contract-discount'}) %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 64c8cad..5dd440e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -40,8 +40,8 @@ en: field_fixed_spent: "Fixed" field_labor_budget: "Labor" field_labor_spent: "Labor" - field_markup_budget: "Markup" - field_markup_spent: "Markup" + field_fixed_markup_budget: "Markup" + field_fixed_markup_spent: "Markup" field_overhead_budget: "Overhead" field_overhead_spent: "Overhead" field_profit_budget: "Profit" diff --git a/test/unit/contract_test.rb b/test/unit/contract_test.rb index db441b2..b993620 100644 --- a/test/unit/contract_test.rb +++ b/test/unit/contract_test.rb @@ -254,4 +254,38 @@ class ContractTest < ActiveSupport::TestCase end end + + context "#fixed_budget" do + should "sum all fixed budget amounts on the Deliverables" do + contract = Contract.generate! + contract.deliverables << @deliverable_1 = FixedDeliverable.generate! + FixedBudget.generate!(:deliverable => @deliverable_1, :budget => '$1,000') + contract.deliverables << @deliverable_2 = HourlyDeliverable.generate! + FixedBudget.generate!(:deliverable => @deliverable_2, :budget => '$2,000') + + assert_equal 3000, contract.fixed_budget + end + end + + context "#fixed_spent" do + should "QUESTION: how to compute the amount spent" + end + + context "#fixed_markup_budget" do + should "sum all fixed budget markup values on the Deliverables" do + contract = Contract.generate! + contract.deliverables << @deliverable_1 = FixedDeliverable.generate! + FixedBudget.generate!(:deliverable => @deliverable_1, :budget => '$1,000', :markup => '$100') + contract.deliverables << @deliverable_2 = HourlyDeliverable.generate! + FixedBudget.generate!(:deliverable => @deliverable_2, :budget => '$2,000', :markup => '200%') + + assert_equal (100) + (2.00 * 2000), contract.fixed_markup_budget + end + + end + + context "#fixed_markup_spent" do + should "QUESTION: how to compute the amount spent" + end + end