Files
redmine_contracts/app/models/retainer_deliverable.rb
T

69 lines
2.2 KiB
Ruby

# A RetainerDeliverable is an HourlyDeliverable that is renewed at
# regular calendar periods. The Company bills a regular number of
# hours for a hourly rate whereby the budgets are reset over a
# regular cyclical period (often monthly).
class RetainerDeliverable < HourlyDeliverable
unloadable
# Associations
# Validations
ValidFrequencies = ["monthly", "quarterly"]
validates_inclusion_of :frequency, :in => ValidFrequencies, :allow_nil => true, :allow_blank => true
# Accessors
# Callbacks
def short_type
'R'
end
def current_period
'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
undated_overhead_budgets = overhead_budgets.all(:conditions => ["#{OverheadBudget.table_name}.year IS NULL AND #{OverheadBudget.table_name}.month IS NULL"])
undated_overhead_budgets.each do |template_budget|
overhead_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)
end
def self.frequencies_to_select
ValidFrequencies.collect {|f| [l("text_#{f}"), f]}
end
end