[#4420] Retainers extended past the end date will duplicate the month's last budget items.

This commit is contained in:
Eric Davis
2010-09-01 15:09:36 -07:00
parent c12a33e25d
commit 16f8152599
2 changed files with 62 additions and 0 deletions
+22
View File
@@ -14,6 +14,7 @@ class RetainerDeliverable < HourlyDeliverable
# Accessors
# Callbacks
before_update :check_for_extended_period
def short_type
'R'
@@ -72,6 +73,27 @@ class RetainerDeliverable < HourlyDeliverable
overhead_budgets.all(:conditions => ["#{OverheadBudget.table_name}.year IS NULL AND #{OverheadBudget.table_name}.month IS NULL"]).collect(&:destroy)
end
def check_for_extended_period
# TODO: brute force. Alternative would be to check end_date_changes to see if the period actually shifted
if end_date_changed?
last_labor_budget = labor_budgets.last(:order => 'year ASC, month ASC')
last_overhead_budget = overhead_budgets.last(:order => 'year ASC, month ASC')
months.each do |new_date|
existing_labor = labor_budgets.first(:conditions => {:year => new_date.year, :month => new_date.month})
unless existing_labor
labor_budgets.create(last_labor_budget.attributes.except('id').merge('year' => new_date.year, 'month' => new_date.month))
end
existing_overhead = overhead_budgets.first(:conditions => {:year => new_date.year, :month => new_date.month})
unless existing_overhead
overhead_budgets.create(last_overhead_budget.attributes.except('id').merge('year' => new_date.year, 'month' => new_date.month))
end
end
end
end
def self.frequencies_to_select
ValidFrequencies.collect {|f| [l("text_#{f}"), f]}
end
@@ -158,4 +158,44 @@ class DeliverablesEditTest < ActionController::IntegrationTest
end
end
should "allow extending a Retainer's out to additional months" do
@retainer_deliverable = RetainerDeliverable.spawn(:contract => @contract, :manager => @manager, :title => "Retainer")
@retainer_deliverable.labor_budgets << @labor_budget = LaborBudget.spawn(:deliverable => @retainer_deliverable, :budget => 1000, :hours => 10)
@retainer_deliverable.overhead_budgets << @overhead_budget = OverheadBudget.spawn(:deliverable => @retainer_deliverable, :budget => 1000, :hours => 10)
@retainer_deliverable.start_date = '2010-01-01'
@retainer_deliverable.end_date = '2010-12-31'
@retainer_deliverable.save!
assert_equal 12, @retainer_deliverable.months.length
@last_labor_budget = @retainer_deliverable.labor_budgets.last
@last_overhead_budget = @retainer_deliverable.overhead_budgets.last
visit_contract_page(@contract)
click_link_within "#deliverable_details_#{@retainer_deliverable.id}", 'Edit'
assert_response :success
assert_template 'deliverables/edit'
# Extend the period
fill_in "End Date", :with => '2011-12-01' # 12 new months
click_button "Save"
assert_response :success
assert_template 'contracts/show'
@labor_budgets = @retainer_deliverable.reload.labor_budgets
assert_equal 24, @labor_budgets.length
@labor_budgets[-12,24].each do |labor_budget| # Last 12
assert_equal @last_labor_budget.hours, labor_budget.hours
assert_equal @last_labor_budget.budget, labor_budget.budget
end
@overhead_budgets = @retainer_deliverable.reload.overhead_budgets
assert_equal 24, @overhead_budgets.length
@overhead_budgets[-12,24].each do |overhead_budget| # Last 12
assert_equal @last_overhead_budget.hours, overhead_budget.hours
assert_equal @last_overhead_budget.budget, overhead_budget.budget
end
end
end