OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/app/models/rate_observer.rb

178 lines
6.1 KiB

class RateObserver < ActiveRecord::Observer
observe :hourly_rate, :cost_rate
12 years ago
class Methods
def initialize(changed_rate)
@rate = changed_rate
end
12 years ago
# order the dates
def order_dates(*dates)
dates.compact!
dates.size == 1 ? dates.first : dates.sort
end
12 years ago
def conditions_after(date, date_column = :spent_on)
if @rate.is_a?(HourlyRate)
[
"#{date_column} >= ? AND user_id = ? and project_id = ?",
date, @rate.user_id, @rate.project_id
]
else
[
"#{date_column} >= ? AND cost_type_id = ?",
date, @rate.cost_type_id
]
end
end
12 years ago
def conditions_between(date1, date2 = nil, date_column = :spent_on)
# if the second date is not given, return all entries
# with a spent_on after the given date
return conditions_after(date1 || date2, date_column) if date1.nil? || date2.nil?
12 years ago
(date1, date2) = order_dates(date1, date2)
12 years ago
# return conditions for all entries between date1 and date2 - 1 day
if @rate.is_a?(HourlyRate)
{ date_column => date1..(date2 - 1),
:user_id => @rate.user_id,
:project_id => @rate.project_id
}
else
{ date_column => date1..(date2 - 1),
:cost_type_id => @rate.cost_type_id,
}
end
end
12 years ago
def find_entries(date1, date2 = nil)
if @rate.is_a?(HourlyRate)
TimeEntry.find(:all, :conditions => conditions_between(date1, date2), :include => :rate)
else
CostEntry.find(:all, :conditions => conditions_between(date1, date2), :include => :rate)
end
end
12 years ago
def update_entries(entries, rate = @rate)
# This methods updates the given array of time or cost entries with the given rate
entries = [entries] unless entries.is_a?(Array)
ActiveRecord::Base.cache do
entries.each do |entry|
entry.update_costs!(rate)
end
end
end
12 years ago
def count_rates(date1, date2 = nil)
(@rate.class).count(:conditions => conditions_between(date1, date2, :valid_from))
end
12 years ago
def orphaned_child_entries(date1, date2 = nil)
# This method returns all entries in child projects without an explicit
# rate or with a rate id of rate_id between date1 and date2
# i.e. the ones with an assigned default rate or without a rate
return [] unless @rate.is_a?(HourlyRate)
12 years ago
(date1, date2) = order_dates(date1, date2)
12 years ago
# This gets an array of all the ids of the DefaultHourlyRates
default_rates = DefaultHourlyRate.find(:all, :select => :id).inject([]){|r,d|r<<d.id}
12 years ago
if date1.nil? || date2.nil?
# we have only one date, query >=
conditions = [
"user_id = ? AND project_id IN (?) AND (rate_id IN (?) OR rate_id IS NULL) AND spent_on >= ?",
@rate.user_id, @rate.project.descendants, default_rates, date1 || date2
]
else
# we have two dates, query between
conditions = [
"user_id = ? AND project_id IN (?) AND (rate_id IN (?) OR rate_id IS NULL) AND spent_on BETWEEN ? AND ?",
@rate.user_id, @rate.project.descendants, default_rates, date1, date2
]
end
12 years ago
TimeEntry.find(:all, :conditions => conditions, :include => :rate)
end
12 years ago
def child_entries(date1, date2 = nil)
# This method returns all entries in child projects without an explicit
# rate or with a rate id of rate_id between date1 and date2
# i.e. the ones with an assigned default rate or without a rate
return [] unless @rate.is_a?(HourlyRate)
12 years ago
(date1, date2) = order_dates(date1, date2)
if date1.nil? || date2.nil?
# we have only one date, query >=
conditions = [
"user_id = ? AND project_id IN (?) AND rate_id = ? AND spent_on >= ?",
@rate.user_id, @rate.project.descendants, @rate.id, date1 || date2
]
else
# we have two dates, query between
conditions = [
"user_id = ? AND project_id IN (?) AND rate_id = ? AND spent_on BETWEEN ? AND ?",
@rate.user_id, @rate.project.descendants, @rate.id, date1, date2
]
end
12 years ago
TimeEntry.find(:all, :conditions => conditions, :include => :rate)
end
end
12 years ago
def after_create(rate)
o = Methods.new(rate)
next_rate = rate.next
# get entries from the current project
entries = o.find_entries(rate.valid_from, (next_rate.valid_from if next_rate))
12 years ago
# and entries from subprojects that need updating (only applies to hourly_rates)
entries += o.orphaned_child_entries(rate.valid_from, (next_rate.valid_from if next_rate))
12 years ago
o.update_entries(entries)
end
12 years ago
def after_update(rate)
o = Methods.new(rate)
12 years ago
unless rate.valid_from_changed?
# We have not moved a rate, maybe just changed the rate value
12 years ago
return unless rate.rate_changed?
# Only the rate value was changed so just update the currently assigned entries
return after_create(rate)
end
12 years ago
# We have definitely moved the rate
if o.count_rates(rate.valid_from_was, rate.valid_from) > 0
# We have passed the boundary of another rate
# We do essantially the same as deleting the old rate and adding a new one
# So first assign all entries from the old a new rate
after_destroy(rate)
12 years ago
# Now update the newly assigned entries
after_create(rate)
else
# We have only moved the rate without passing other rates
# So we have to either assign some entries to our previous rate (if moved forwards)
# or assign some entries to self (if moved backwards)
12 years ago
# get entries from the current project
entries = o.find_entries(rate.valid_from_was, rate.valid_from)
# and entries from subprojects that need updating (only applies to hourly_rates)
entries += o.child_entries(rate.valid_from_was, rate.valid_from)
12 years ago
o.update_entries(entries, (rate.valid_from_was < rate.valid_from) ? rate.previous : rate)
end
end
12 years ago
def after_destroy(rate)
entry_class = rate.is_a?(HourlyRate) ? TimeEntry : CostEntry
entry_class.find(:all, :conditions => {:rate_id => rate.id}).each{|e| e.update_costs!}
end
12 years ago
end