kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
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.
104 lines
2.7 KiB
104 lines
2.7 KiB
15 years ago
|
require_dependency 'project'
|
||
|
|
||
|
require_dependency 'principal'
|
||
15 years ago
|
require_dependency 'user'
|
||
15 years ago
|
|
||
12 years ago
|
module OpenProject::Costs::Patches::UserPatch
|
||
15 years ago
|
def self.included(base) # :nodoc:
|
||
|
base.send(:include, InstanceMethods)
|
||
|
|
||
|
base.class_eval do
|
||
15 years ago
|
unloadable
|
||
15 years ago
|
|
||
15 years ago
|
has_many :rates, :class_name => 'HourlyRate'
|
||
15 years ago
|
has_many :default_rates, :class_name => 'DefaultHourlyRate'
|
||
14 years ago
|
|
||
15 years ago
|
before_save :save_rates
|
||
12 years ago
|
end
|
||
15 years ago
|
end
|
||
|
|
||
|
module InstanceMethods
|
||
12 years ago
|
def allowed_to_condition_with_project_id(permission, projects = nil)
|
||
|
ids = Project.all(:select => :id,
|
||
|
:conditions => Project.allowed_to_condition(self, permission, :project => projects)).map(&:id)
|
||
14 years ago
|
|
||
12 years ago
|
ids.empty? ?
|
||
|
"1=0" :
|
||
|
"(#{Project.table_name}.id in (#{ids.join(", ")}))"
|
||
15 years ago
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def current_rate(project = nil, include_default = true)
|
||
15 years ago
|
rate_at(Date.today, project, include_default)
|
||
15 years ago
|
end
|
||
14 years ago
|
|
||
13 years ago
|
# kept for backwards compatibility
|
||
15 years ago
|
def rate_at(date, project = nil, include_default = true)
|
||
13 years ago
|
::HourlyRate.at_date_for_user_in_project(date, self.id, project, include_default)
|
||
15 years ago
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def current_default_rate()
|
||
13 years ago
|
::DefaultHourlyRate.at_for_user(Date.today, self.id)
|
||
15 years ago
|
end
|
||
|
|
||
13 years ago
|
# kept for backwards compatibility
|
||
15 years ago
|
def default_rate_at(date)
|
||
13 years ago
|
::DefaultHourlyRate.at_for_user(date, self.id)
|
||
15 years ago
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def add_rates(project, rate_attributes)
|
||
15 years ago
|
# set project to nil to set the default rates
|
||
14 years ago
|
|
||
15 years ago
|
return unless rate_attributes
|
||
14 years ago
|
|
||
15 years ago
|
rate_attributes.each do |index, attributes|
|
||
|
attributes[:rate] = Rate.clean_currency(attributes[:rate])
|
||
14 years ago
|
|
||
15 years ago
|
if project.nil?
|
||
15 years ago
|
default_rates.build(attributes)
|
||
15 years ago
|
else
|
||
|
attributes[:project] = project
|
||
15 years ago
|
rates.build(attributes)
|
||
15 years ago
|
end
|
||
15 years ago
|
end
|
||
|
end
|
||
|
|
||
15 years ago
|
def set_existing_rates (project, rate_attributes)
|
||
15 years ago
|
if project.nil?
|
||
|
default_rates.reject(&:new_record?).each do |rate|
|
||
|
update_rate(rate, rate_attributes, false)
|
||
15 years ago
|
end
|
||
15 years ago
|
else
|
||
|
rates.reject{|r| r.new_record? || r.project_id != project.id}.each do |rate|
|
||
|
update_rate(rate, rate_attributes, true)
|
||
15 years ago
|
end
|
||
|
end
|
||
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def save_rates
|
||
15 years ago
|
(default_rates + rates).each do |rate|
|
||
15 years ago
|
rate.save(false)
|
||
|
end
|
||
|
end
|
||
14 years ago
|
|
||
|
|
||
15 years ago
|
private
|
||
15 years ago
|
def update_rate(rate, rate_attributes, project_rate = true)
|
||
|
attributes = rate_attributes[rate.id.to_s] if rate_attributes
|
||
14 years ago
|
|
||
15 years ago
|
has_rate = false
|
||
15 years ago
|
if attributes && attributes[:rate].present?
|
||
15 years ago
|
attributes[:rate] = Rate.clean_currency(attributes[:rate])
|
||
15 years ago
|
has_rate = true
|
||
15 years ago
|
end
|
||
|
|
||
|
if has_rate
|
||
|
rate.attributes = attributes
|
||
|
else
|
||
|
project_rate ? rates.delete(rate) : default_rates.delete(rate)
|
||
|
end
|
||
|
end
|
||
15 years ago
|
end
|
||
|
end
|
||
15 years ago
|
|
||
12 years ago
|
User.send(:include, OpenProject::Costs::Patches::UserPatch)
|