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.
112 lines
3.2 KiB
112 lines
3.2 KiB
15 years ago
|
require_dependency 'time_entry'
|
||
15 years ago
|
|
||
|
# Patches Redmine's Users dynamically.
|
||
12 years ago
|
module OpenProject::Costs::Patches::TimeEntryPatch
|
||
15 years ago
|
def self.included(base) # :nodoc:
|
||
|
base.extend(ClassMethods)
|
||
|
|
||
|
base.send(:include, InstanceMethods)
|
||
|
|
||
15 years ago
|
# Same as typing in the class t.update_costs
|
||
15 years ago
|
base.class_eval do
|
||
|
unloadable
|
||
15 years ago
|
|
||
15 years ago
|
belongs_to :rate, :conditions => {:type => ["HourlyRate", "DefaultHourlyRate"]}, :class_name => "Rate"
|
||
|
attr_protected :costs, :rate_id
|
||
12 years ago
|
|
||
12 years ago
|
scope :visible, lambda{|*args|
|
||
15 years ago
|
{ :include => [:project, :user],
|
||
12 years ago
|
:conditions => TimeEntry.visible_condition(args[0] || User.current, args[1])
|
||
15 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
|
def self.visible_condition(user, project)
|
||
|
%Q{ (#{Project.allowed_to_condition(user, :view_time_entries, :project => project)} OR
|
||
|
(#{Project.allowed_to_condition(user, :view_own_time_entries, :project => project)} AND #{TimeEntry.table_name}.user_id = #{user.id})) }
|
||
|
end
|
||
|
|
||
12 years ago
|
scope :visible_costs, lambda{|*args|
|
||
12 years ago
|
user = args.first || User.current
|
||
|
project = args[1]
|
||
|
|
||
|
view_hourly_rates = %Q{ (#{Project.allowed_to_condition(user, :view_hourly_rates, :project => project)} OR
|
||
|
(#{Project.allowed_to_condition(user, :view_own_hourly_rate, :project => project)} AND #{TimeEntry.table_name}.user_id = #{user.id})) }
|
||
|
view_time_entries = TimeEntry.visible_condition(user, project)
|
||
15 years ago
|
|
||
|
{ :include => [:project, :user],
|
||
|
:conditions => [view_time_entries, view_hourly_rates].join(" AND ")
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
15 years ago
|
end
|
||
|
|
||
|
end
|
||
|
|
||
|
module ClassMethods
|
||
15 years ago
|
def update_all(updates, conditions = nil, options = {})
|
||
|
# instead of a update_all, perform an individual update during issue#move
|
||
|
# to trigger the update of the costs based on new rates
|
||
13 years ago
|
if conditions.respond_to?(:keys) && conditions.keys == [:issue_id] && updates =~ /^project_id = ([\d]+)$/
|
||
15 years ago
|
project_id = $1
|
||
15 years ago
|
time_entries = TimeEntry.all(:conditions => conditions)
|
||
15 years ago
|
time_entries.each do |entry|
|
||
|
entry.project_id = project_id
|
||
|
entry.save!
|
||
|
end
|
||
|
else
|
||
|
super
|
||
|
end
|
||
|
end
|
||
15 years ago
|
end
|
||
|
|
||
|
module InstanceMethods
|
||
15 years ago
|
def before_save
|
||
15 years ago
|
update_costs
|
||
15 years ago
|
end
|
||
12 years ago
|
|
||
15 years ago
|
def real_costs
|
||
|
# This methods returns the actual assigned costs of the entry
|
||
15 years ago
|
overridden_costs || costs || calculated_costs
|
||
15 years ago
|
end
|
||
12 years ago
|
|
||
15 years ago
|
def calculated_costs(rate_attr = nil)
|
||
|
rate_attr ||= current_rate
|
||
15 years ago
|
hours * rate_attr.rate
|
||
15 years ago
|
rescue
|
||
15 years ago
|
0.0
|
||
15 years ago
|
end
|
||
12 years ago
|
|
||
15 years ago
|
def update_costs(rate_attr = nil)
|
||
|
rate_attr ||= current_rate
|
||
15 years ago
|
if rate_attr.nil?
|
||
15 years ago
|
self.costs = 0.0
|
||
15 years ago
|
self.rate = nil
|
||
|
return
|
||
|
end
|
||
12 years ago
|
|
||
15 years ago
|
self.costs = calculated_costs(rate_attr)
|
||
15 years ago
|
self.rate = rate_attr
|
||
15 years ago
|
end
|
||
12 years ago
|
|
||
15 years ago
|
def update_costs!(rate_attr = nil)
|
||
15 years ago
|
self.update_costs(rate_attr)
|
||
15 years ago
|
self.save!
|
||
|
end
|
||
|
|
||
|
def current_rate
|
||
|
self.user.rate_at(self.spent_on, self.project_id)
|
||
15 years ago
|
end
|
||
15 years ago
|
|
||
|
def visible_by?(usr)
|
||
12 years ago
|
usr.allowed_to?(:view_time_entries, project)
|
||
15 years ago
|
end
|
||
|
|
||
|
def costs_visible_by?(usr)
|
||
12 years ago
|
usr.allowed_to?(:view_hourly_rates, project) ||
|
||
|
(user_id == usr.id && usr.allowed_to?(:view_own_hourly_rate, project))
|
||
15 years ago
|
end
|
||
15 years ago
|
end
|
||
|
end
|
||
15 years ago
|
|
||
12 years ago
|
TimeEntry.send(:include, OpenProject::Costs::Patches::TimeEntryPatch)
|