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/default_hourly_rate_observe...

106 lines
3.1 KiB

#-- copyright
# OpenProject Costs Plugin
#
# Copyright (C) 2009 - 2014 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#++
class DefaultHourlyRateObserver < ActiveRecord::Observer
class Methods
def initialize(changed_rate)
@rate = changed_rate
end
12 years ago
def order_dates(date1, date2)
# order the dates
return date1 || date2 if date1.nil? || date2.nil?
if date2 < date1
date_tmp = date2
date2 = date1
date1 = date_tmp
end
[date1, date2]
end
def orphaned_child_entries(date1, date2 = nil)
# This method returns all entries in all projects without an explicit rate
# between date1 and date2
# i.e. the ones with an assigned default rate or without a rate
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.pluck(:id)
12 years ago
if date1.nil? || date2.nil?
# we have only one date, query >=
conditions = [
'user_id = ? AND (rate_id IN (?) OR rate_id IS NULL) AND spent_on >= ?',
@rate.user_id, default_rates, date1 || date2
]
else
# we have two dates, query between
conditions = [
'user_id = ? AND (rate_id IN (?) OR rate_id IS NULL) AND spent_on BETWEEN ? AND ?',
@rate.user_id, default_rates, date1, date2 - 1
]
end
12 years ago
TimeEntry.includes(:rate).where(conditions)
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
end
12 years ago
def after_create(rate)
o = Methods.new(rate)
next_rate = rate.next
# and entries from all projects that need updating
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)
# FIXME: This might be extremly slow. Consider using an implementation like in HourlyRateObserver
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
after_destroy(rate)
after_create(rate)
end
12 years ago
def after_destroy(rate)
o = Methods.new(rate)
o.update_entries(TimeEntry.where(rate_id: rate.id))
end
12 years ago
end