diff --git a/app/models/retainer_deliverable.rb b/app/models/retainer_deliverable.rb index d8f07ad..25fa354 100644 --- a/app/models/retainer_deliverable.rb +++ b/app/models/retainer_deliverable.rb @@ -226,10 +226,16 @@ class RetainerDeliverable < HourlyDeliverable undated_overhead_budgets.each do |template_budget| overhead_budgets.create(template_budget.attributes.merge(:year => month.year, :month => month.month)) end + + undated_fixed_budgets = fixed_budgets.all(:conditions => ["#{FixedBudget.table_name}.year IS NULL AND #{FixedBudget.table_name}.month IS NULL"]) + undated_fixed_budgets.each do |template_budget| + fixed_budgets.create(template_budget.attributes.merge(:year => month.year, :month => month.month)) + end end # Destroy origional un-dated budgets labor_budgets.all(:conditions => ["#{LaborBudget.table_name}.year IS NULL AND #{LaborBudget.table_name}.month IS NULL"]).collect(&:destroy) overhead_budgets.all(:conditions => ["#{OverheadBudget.table_name}.year IS NULL AND #{OverheadBudget.table_name}.month IS NULL"]).collect(&:destroy) + fixed_budgets.all(:conditions => ["#{FixedBudget.table_name}.year IS NULL AND #{FixedBudget.table_name}.month IS NULL"]).collect(&:destroy) end def check_for_extended_period @@ -276,6 +282,17 @@ class RetainerDeliverable < HourlyDeliverable end end + fixed_budgets.all.each do |fixed_budget| + # Purge un-dated budgets, should not be saved at all + fixed_budget.destroy unless fixed_budget.year.present? + fixed_budget.destroy unless fixed_budget.month.present? + + # Purge budgets outside the new beginning/ending range + unless (beginning_date..ending_date).to_a.include?(Date.new(fixed_budget.year, fixed_budget.month, 1)) + fixed_budget.destroy + end + end + true end @@ -285,9 +302,10 @@ class RetainerDeliverable < HourlyDeliverable old_end_date = end_date_change[0] last_labor_budgets = labor_budgets.all(:conditions => {:year => old_end_date.year, :month => old_end_date.month}) last_overhead_budgets = overhead_budgets.all(:conditions => {:year => old_end_date.year, :month => old_end_date.month}) - + last_fixed_budgets = fixed_budgets.all(:conditions => {:year => old_end_date.year, :month => old_end_date.month}) + months_after_date(old_end_date.end_of_month.to_date).each do |new_period| - create_budgets_for_new_period(new_period, last_labor_budgets, last_overhead_budgets) + create_budgets_for_new_period(new_period, last_labor_budgets, last_overhead_budgets, last_fixed_budgets) end end @@ -297,14 +315,15 @@ class RetainerDeliverable < HourlyDeliverable old_start_date = start_date_change[0] first_labor_budgets = labor_budgets.all(:conditions => {:year => old_start_date.year, :month => old_start_date.month}) first_overhead_budgets = overhead_budgets.all(:conditions => {:year => old_start_date.year, :month => old_start_date.month}) + first_fixed_budgets = fixed_budgets.all(:conditions => {:year => old_start_date.year, :month => old_start_date.month}) months_before_date(old_start_date.beginning_of_month.to_date).each do |new_period| - create_budgets_for_new_period(new_period, first_labor_budgets, first_overhead_budgets) + create_budgets_for_new_period(new_period, first_labor_budgets, first_overhead_budgets, first_fixed_budgets) end end - def create_budgets_for_new_period(new_period, labor_budgets_to_copy, overhead_budgets_to_copy) + def create_budgets_for_new_period(new_period, labor_budgets_to_copy, overhead_budgets_to_copy, fixed_budgets_to_copy) labor_budgets_to_copy.each do |labor_budget_to_copy| create_new_labor_budget_based_on_existing_budget(labor_budget_to_copy, 'year' => new_period.year, 'month' => new_period.month) end @@ -312,6 +331,10 @@ class RetainerDeliverable < HourlyDeliverable overhead_budgets_to_copy.each do |overhead_budget_to_copy| create_new_overhead_budget_based_on_existing_budget(overhead_budget_to_copy, 'year' => new_period.year, 'month' => new_period.month) end + + fixed_budgets_to_copy.each do |fixed_budget_to_copy| + create_new_fixed_budget_based_on_existing_budget(fixed_budget_to_copy, 'year' => new_period.year, 'month' => new_period.month) + end end def create_new_labor_budget_based_on_existing_budget(existing_labor_budget, attributes={}) @@ -322,6 +345,10 @@ class RetainerDeliverable < HourlyDeliverable overhead_budgets.create(existing_overhead_budget.attributes.except('id').merge(attributes)) end + def create_new_fixed_budget_based_on_existing_budget(existing_fixed_budget, attributes={}) + fixed_budgets.create(existing_fixed_budget.attributes.except('id').merge(attributes)) + end + def scope_date_status(date) if date if within_date_range?(date) diff --git a/app/views/deliverables/_finance_form.html.erb b/app/views/deliverables/_finance_form.html.erb index 6c3d44d..0942953 100644 --- a/app/views/deliverables/_finance_form.html.erb +++ b/app/views/deliverables/_finance_form.html.erb @@ -67,6 +67,9 @@ <% form.fields_for :fixed_budgets, fixed_budgets do |fixed_budget| %> + <%= fixed_budget.hidden_field(:year) %> + <%= fixed_budget.hidden_field(:month) %> +
<%= fixed_budget.text_field(:title) %> diff --git a/test/integration/deliverables_edit_test.rb b/test/integration/deliverables_edit_test.rb index c38cb74..b139622 100644 --- a/test/integration/deliverables_edit_test.rb +++ b/test/integration/deliverables_edit_test.rb @@ -27,10 +27,11 @@ class DeliverablesEditTest < ActionController::IntegrationTest assert_select "select#fixed_deliverable_type", :count => 0 # Not editable assert js("jQuery('#fixed_deliverable_total_input').is(':visible')"), "Total is hidden when it should be visible" - - fill_in "Title", :with => 'An updated title' - check "Feature Sign Off" - check "Warranty Sign Off" + within("#deliverable-details") do + fill_in "Title", :with => 'An updated title' + check "Feature Sign Off" + check "Warranty Sign Off" + end click_button "Save" assert_response :success @@ -56,9 +57,11 @@ class DeliverablesEditTest < ActionController::IntegrationTest assert_select "select#hourly_deliverable_type", :count => 0 # Not editable assert js("jQuery('#hourly_deliverable_total_input').is(':hidden')"), "Total is visible when it should be hidden" - fill_in "Title", :with => 'An updated title' - check "Feature Sign Off" - check "Warranty Sign Off" + within("#deliverable-details") do + fill_in "Title", :with => 'An updated title' + check "Feature Sign Off" + check "Warranty Sign Off" + end within("#deliverable-labor") do fill_in "hrs", :with => '20' @@ -95,6 +98,8 @@ class DeliverablesEditTest < ActionController::IntegrationTest @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.fixed_budgets << @fixed_budget = FixedBudget.spawn(:deliverable => @retainer_deliverable, :title => 'Printing supplies', :budget => 100, :markup => 0) + @retainer_deliverable.start_date = '2010-01-01' @retainer_deliverable.end_date = '2010-12-31' @retainer_deliverable.save! @@ -117,6 +122,13 @@ class DeliverablesEditTest < ActionController::IntegrationTest fill_in "hrs", :with => '100' fill_in "$", :with => '100' end + + within "#deliverable-fixed" do + fill_in "title", :with => 'Flight to NYC' + fill_in "budget", :with => '$600' + fill_in "markup", :with => '50%' + fill_in "description", :with => 'Need to fly to NYC for the week' + end end click_button "Save" @@ -157,6 +169,24 @@ class DeliverablesEditTest < ActionController::IntegrationTest end end + @fixed_budgets = @retainer_deliverable.reload.fixed_budgets + assert_equal 12, @fixed_budgets.length + @fixed_budgets.each do |fixed_budget| + if fixed_budget.year == 2010 && fixed_budget.month == 1 + + # Specific month's budget updated? + assert_equal 600, fixed_budget.budget + assert_equal '50%', fixed_budget.markup + assert_equal 300, fixed_budget.markup_value + + else + + assert_equal 100, fixed_budget.budget + assert_equal '0', fixed_budget.markup + + end + end + end should "allow extending a Retainer's start and end months" do @@ -175,6 +205,7 @@ class DeliverablesEditTest < ActionController::IntegrationTest @retainer_deliverable.labor_budgets << @labor_budget = LaborBudget.spawn(:deliverable => @retainer_deliverable, :budget => labor_budget_amount_2, :hours => labor_budget_hours_2) @retainer_deliverable.overhead_budgets << @overhead_budget = OverheadBudget.spawn(:deliverable => @retainer_deliverable, :budget => overhead_budget_amount_1, :hours => overhead_budget_hours_1) @retainer_deliverable.overhead_budgets << @overhead_budget = OverheadBudget.spawn(:deliverable => @retainer_deliverable, :budget => overhead_budget_amount_2, :hours => overhead_budget_hours_2) + @retainer_deliverable.fixed_budgets << @fixed_budget = FixedBudget.spawn(:deliverable => @retainer_deliverable, :title => 'Printing supplies', :budget => 100, :markup => 0) @retainer_deliverable.start_date = '2010-01-01' @retainer_deliverable.end_date = '2010-12-31' @retainer_deliverable.save! @@ -256,6 +287,17 @@ class DeliverablesEditTest < ActionController::IntegrationTest assert [overhead_budget_amount_1, overhead_budget_amount_2].include?(overhead_budget.budget), "Extended overhead budget dollars not matching template budget" end + @fixed_budgets = @retainer_deliverable.reload.fixed_budgets + assert_equal 36, @fixed_budgets.length # 36 months * 1 record + + @fixed_budgets_for_2009 = @fixed_budgets.select {|l| l.year == 2009 } + @fixed_budgets_for_2010 = @fixed_budgets.select {|l| l.year == 2010 } + @fixed_budgets_for_2011 = @fixed_budgets.select {|l| l.year == 2011 } + + assert_equal 12, @fixed_budgets_for_2009.length + assert_equal 12, @fixed_budgets_for_2010.length + assert_equal 12, @fixed_budgets_for_2011.length + end should "allow shrinking a Retainer's start and end months" do @@ -264,6 +306,7 @@ class DeliverablesEditTest < ActionController::IntegrationTest @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.fixed_budgets << @fixed_budget = FixedBudget.spawn(:deliverable => @retainer_deliverable, :title => 'Printing supplies', :budget => 100, :markup => 0) @retainer_deliverable.start_date = '2010-01-01' @retainer_deliverable.end_date = '2010-12-31' @retainer_deliverable.save! @@ -293,6 +336,9 @@ class DeliverablesEditTest < ActionController::IntegrationTest @overhead_budgets = @retainer_deliverable.reload.overhead_budgets assert_equal 12, @overhead_budgets.length # 6 months * 2 records + @fixed_budgets = @retainer_deliverable.reload.fixed_budgets + assert_equal 6, @fixed_budgets.length # 6 months * 1 records + end should "allow editing a Retainer's start and end months inside the current period" do @@ -346,7 +392,7 @@ class DeliverablesEditTest < ActionController::IntegrationTest assert_response :success assert_template 'deliverables/edit' - # Should show 6 inputs: + # Should show inputs: # * labor hidden year # * labor hidden month # * labor hours @@ -355,9 +401,15 @@ class DeliverablesEditTest < ActionController::IntegrationTest # * overhead hidden month # * overhead hours # * overhead amount + # * fixed hidden year + # * fixed hidden month + # * fixed title + # * fixed budget + # * fixed markup # * total (hidden) assert_select ".date-2010-01" do - assert_select "input", :count => 9 + assert_select "input", :count => 14 + assert_select "textarea.wiki-edit", :count => 1 # Fixed description end @@ -371,6 +423,14 @@ class DeliverablesEditTest < ActionController::IntegrationTest fill_in "hrs", :with => '100' fill_in "$", :with => '100' end + + within "#deliverable-fixed" do + fill_in "title", :with => 'Flight to NYC' + fill_in "budget", :with => '$600' + fill_in "markup", :with => '50%' + fill_in "description", :with => 'Need to fly to NYC for the week' + end + end click_button "Save" @@ -386,5 +446,8 @@ class DeliverablesEditTest < ActionController::IntegrationTest assert_equal 3, @retainer_deliverable.overhead_budgets.count assert_equal [100, nil, nil], @retainer_deliverable.overhead_budgets.collect(&:hours) assert_equal [100, nil, nil], @retainer_deliverable.overhead_budgets.collect(&:budget) + + assert_equal 3, @retainer_deliverable.fixed_budgets.count + assert_equal [600, nil, nil], @retainer_deliverable.fixed_budgets.collect(&:budget) end end