[#4420] When a Retainer's period is shrunk, budgets outside the period are destroyed.

This commit is contained in:
Eric Davis
2010-09-01 18:12:40 -07:00
parent 0a33cdefbb
commit 9858b87fff
2 changed files with 70 additions and 0 deletions

View File

@@ -15,6 +15,7 @@ class RetainerDeliverable < HourlyDeliverable
# Callbacks
before_update :check_for_extended_period
before_update :check_for_shrunk_period
def short_type
'R'
@@ -95,12 +96,44 @@ class RetainerDeliverable < HourlyDeliverable
end
end
def check_for_shrunk_period
if end_date_changed? || start_date_changed?
shrink_budgets_to_new_period
end
end
def self.frequencies_to_select
ValidFrequencies.collect {|f| [l("text_#{f}"), f]}
end
private
def shrink_budgets_to_new_period
labor_budgets.all.each do |labor_budget|
# Purge un-dated budgets, should not be saved at all
labor_budget.destroy unless labor_budget.year.present?
labor_budget.destroy unless labor_budget.month.present?
# Purge budgets outside the new beginning/ending range
unless (beginning_date..ending_date).to_a.include?(Date.new(labor_budget.year, labor_budget.month, 1))
labor_budget.destroy
end
end
overhead_budgets.all.each do |overhead_budget|
# Purge un-dated budgets, should not be saved at all
overhead_budget.destroy unless overhead_budget.year.present?
overhead_budget.destroy unless overhead_budget.month.present?
# Purge budgets outside the new beginning/ending range
unless (beginning_date..ending_date).to_a.include?(Date.new(overhead_budget.year, overhead_budget.month, 1))
overhead_budget.destroy
end
end
true
end
def extend_period_to_new_end_date
old_end_date = end_date_change[0]
last_labor_budgets = labor_budgets.all(:conditions => {:year => old_end_date.year, :month => old_end_date.month})

View File

@@ -257,4 +257,41 @@ class DeliverablesEditTest < ActionController::IntegrationTest
end
end
should "allow shrinking a Retainer's start and end 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.labor_budgets << @labor_budget = LaborBudget.spawn(:deliverable => @retainer_deliverable, :budget => 2000, :hours => 20)
@retainer_deliverable.overhead_budgets << @overhead_budget = OverheadBudget.spawn(:deliverable => @retainer_deliverable, :budget => 1000, :hours => 10)
@retainer_deliverable.overhead_budgets << @overhead_budget = OverheadBudget.spawn(:deliverable => @retainer_deliverable, :budget => 2000, :hours => 20)
@retainer_deliverable.start_date = '2010-01-01'
@retainer_deliverable.end_date = '2010-12-31'
@retainer_deliverable.save!
assert_equal 12, @retainer_deliverable.months.length
assert_equal 24, @retainer_deliverable.reload.labor_budgets.count # 12 months * 2 records
assert_equal 24, @retainer_deliverable.reload.overhead_budgets.count # 12 months * 2 records
visit_contract_page(@contract)
click_link_within "#deliverable_details_#{@retainer_deliverable.id}", 'Edit'
assert_response :success
assert_template 'deliverables/edit'
# Shrink the period to only 6 months
fill_in "Start", :with => '2010-02-13'
fill_in "End Date", :with => '2010-07-01'
click_button "Save"
assert_response :success
assert_template 'contracts/show'
@retainer_deliverable.reload
assert_equal 6, @retainer_deliverable.months.length
@labor_budgets = @retainer_deliverable.reload.labor_budgets
assert_equal 12, @labor_budgets.length # 6 months * 2 records
@overhead_budgets = @retainer_deliverable.reload.overhead_budgets
assert_equal 12, @overhead_budgets.length # 6 months * 2 records
end
end