diff --git a/app/models/retainer_deliverable.rb b/app/models/retainer_deliverable.rb index 543b892..19cd368 100644 --- a/app/models/retainer_deliverable.rb +++ b/app/models/retainer_deliverable.rb @@ -14,6 +14,7 @@ class RetainerDeliverable < HourlyDeliverable # Accessors # Callbacks + after_save :create_budgets_for_periods def short_type 'R' @@ -23,6 +24,39 @@ class RetainerDeliverable < HourlyDeliverable 'TODO' end + def beginning_date + start_date && start_date.beginning_of_month + end + + def ending_date + end_date && end_date.end_of_month + end + + def months + month_acc = [] + + current_date = beginning_date + while current_date < ending_date do + month_acc << current_date + current_date = current_date.advance(:months => 1) + end + + month_acc + end + + def create_budgets_for_periods + # For each month in the time span + months.each do |month| + # Iterate over all un-dated budgets, created dated versions + undated_labor_budgets = labor_budgets.all(:conditions => ["#{LaborBudget.table_name}.year IS NULL AND #{LaborBudget.table_name}.month IS NULL"]) + undated_labor_budgets.each do |template_budget| + labor_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) + end + def self.frequencies_to_select ValidFrequencies.collect {|f| [l("text_#{f}"), f]} end diff --git a/db/migrate/012_add_year_and_month_to_labor_budgets.rb b/db/migrate/012_add_year_and_month_to_labor_budgets.rb new file mode 100644 index 0000000..88f2847 --- /dev/null +++ b/db/migrate/012_add_year_and_month_to_labor_budgets.rb @@ -0,0 +1,14 @@ +class AddYearAndMonthToLaborBudgets < ActiveRecord::Migration + def self.up + add_column :labor_budgets, :year, :integer + add_index :labor_budgets, :year + + add_column :labor_budgets, :month, :integer + add_index :labor_budgets, :month + end + + def self.down + remove_column :labor_budgets, :year + remove_column :labor_budgets, :month + end +end diff --git a/db/migrate/013_add_year_and_month_to_overhead_budgets.rb b/db/migrate/013_add_year_and_month_to_overhead_budgets.rb new file mode 100644 index 0000000..8776e46 --- /dev/null +++ b/db/migrate/013_add_year_and_month_to_overhead_budgets.rb @@ -0,0 +1,14 @@ +class AddYearAndMonthToOverheadBudgets < ActiveRecord::Migration + def self.up + add_column :overhead_budgets, :year, :integer + add_index :overhead_budgets, :year + + add_column :overhead_budgets, :month, :integer + add_index :overhead_budgets, :month + end + + def self.down + remove_column :overhead_budgets, :year + remove_column :overhead_budgets, :month + end +end diff --git a/test/integration/deliverables_new_test.rb b/test/integration/deliverables_new_test.rb index 41063ce..51ced9b 100644 --- a/test/integration/deliverables_new_test.rb +++ b/test/integration/deliverables_new_test.rb @@ -127,7 +127,16 @@ class DeliverablesNewTest < ActionController::IntegrationTest fill_in "Start", :with => '2010-01-01' fill_in "End Date", :with => '2010-12-31' fill_in "Notes", :with => 'Some notes on the deliverable' - fill_in "Total", :with => '1,000.00' + + within("#deliverable-labor") do + fill_in "hrs", :with => '20' + fill_in "$", :with => '$2,000' + end + + within("#deliverable-overhead") do + fill_in "hrs", :with => '10' + fill_in "$", :with => '$1,000' + end click_button "Save" @@ -142,6 +151,39 @@ class DeliverablesNewTest < ActionController::IntegrationTest assert_equal '2010-12-31', @deliverable.end_date.to_s assert_equal @manager, @deliverable.manager assert_equal "monthly", @deliverable.frequency + + # Budget items, one per month + labor_budgets = @deliverable.labor_budgets + assert_equal 12, labor_budgets.length + + labor_budgets.each do |budget| + assert_equal 2000, budget.budget + assert_equal 20, budget.hours + end + + # Budget dates + labor_budgets.each do |budget| + assert_equal 2010, budget.year + end + (1..12).each do |month_number| + assert_equal 1, labor_budgets.select {|b| b.month == month_number}.length + end + + overhead_budgets = @deliverable.overhead_budgets + assert_equal 12, overhead_budgets.length + + overhead_budgets.each do |budget| + assert_equal 1000, budget.budget + assert_equal 10, budget.hours + end + + # Budget dates + overhead_budgets.each do |budget| + assert_equal 2010, budget.year + end + (1..12).each do |month_number| + assert_equal 1, overhead_budgets.select {|b| b.month == month_number}.length + end end should "create new budget items for the deliverables" do diff --git a/test/unit/retainer_deliverable_test.rb b/test/unit/retainer_deliverable_test.rb index e079dcb..b8c6e25 100644 --- a/test/unit/retainer_deliverable_test.rb +++ b/test/unit/retainer_deliverable_test.rb @@ -10,36 +10,31 @@ class RetainerDeliverableTest < ActiveSupport::TestCase should_not_allow_values_for(:frequency, 'anything', 'else', 'weekly') end - # TODO: Question: Fit to calendar or based on N days? - # Monthly => June-June or June 5th-July 5th - # Quarterly => Jan-March 31 or Feb-May 31 - context "#current_period" do - context "monthly frequency" do - should "be a range of the current month" + context "#create_budgets_for_periods" do + setup do + @deliverable = RetainerDeliverable.generate!(:start_date => '2010-01-10', + :end_date => '2010-10-10') + LaborBudget.generate!(:deliverable => @deliverable) end - context "quarterly frequency" do - should "be a range of the current quarter" - end - end + should "create a dated labor budget for each month" do + assert_equal 1, @deliverable.labor_budgets.count - context "#start_of_current_period" do - context "monthly frequency" do - should "QUESTION: be the first day of the month or 30 days ago" + @deliverable.reload.create_budgets_for_periods + + assert_equal 10, @deliverable.labor_budgets.count + labor_budgets = @deliverable.labor_budgets + labor_budgets.each do |budget| + assert_equal 2010, budget.year + end + + (1..10).each do |month_number| + assert_equal 1, labor_budgets.select {|b| b.month == month_number}.length + end + end - context "quarterly frequency" do - should "QUESTION: be the first day of the quarter or 365/4 days ago" - end + should "create a dated overhead budget for each month" end - context "#end_of_current_period" do - context "monthly frequency" do - should "QUESTION: be the first day of the month or 30 days ago" - end - - context "quarterly frequency" do - should "QUESTION: be the first day of the quarter or 365/4 days ago" - end - end end