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.

time_entry.rb 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class TimeEntry < ActiveRecord::Base
  19. include Redmine::SafeAttributes
  20. # could have used polymorphic association
  21. # project association here allows easy loading of time entries at project level with one database trip
  22. belongs_to :project
  23. belongs_to :issue
  24. belongs_to :user
  25. belongs_to :author, :class_name => 'User'
  26. belongs_to :activity, :class_name => 'TimeEntryActivity'
  27. acts_as_customizable
  28. acts_as_event(
  29. :title =>
  30. Proc.new do |o|
  31. related = o.issue if o.issue && o.issue.visible?
  32. related ||= o.project
  33. "#{l_hours(o.hours)} (#{related.event_title})"
  34. end,
  35. :url =>
  36. Proc.new do |o|
  37. {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}
  38. end,
  39. :author => :user,
  40. :group => :issue,
  41. :description => :comments
  42. )
  43. acts_as_activity_provider :timestamp => "#{table_name}.created_on",
  44. :author_key => :user_id,
  45. :scope => proc {joins(:project).preload(:project)}
  46. validates_presence_of :author_id, :user_id, :activity_id, :project_id, :hours, :spent_on
  47. validates_presence_of :issue_id, :if => lambda {Setting.timelog_required_fields.include?('issue_id')}
  48. validates_presence_of :comments, :if => lambda {Setting.timelog_required_fields.include?('comments')}
  49. validates_numericality_of :hours, :allow_nil => true, :message => :invalid
  50. validates_length_of :comments, :maximum => 1024, :allow_nil => true
  51. validates :spent_on, :date => true
  52. before_validation :set_project_if_nil
  53. # TODO: remove this, author should be always explicitly set
  54. before_validation :set_author_if_nil
  55. validate :validate_time_entry
  56. scope :visible, (lambda do |*args|
  57. joins(:project).
  58. where(TimeEntry.visible_condition(args.shift || User.current, *args))
  59. end)
  60. scope :left_join_issue, (lambda do
  61. joins("LEFT OUTER JOIN #{Issue.table_name} ON #{Issue.table_name}.id = #{TimeEntry.table_name}.issue_id")
  62. end)
  63. scope :on_issue, (lambda do |issue|
  64. joins(:issue).
  65. where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
  66. end)
  67. safe_attributes 'user_id', 'hours', 'comments', 'project_id',
  68. 'issue_id', 'activity_id', 'spent_on',
  69. 'custom_field_values', 'custom_fields'
  70. # Returns a SQL conditions string used to find all time entries visible by the specified user
  71. def self.visible_condition(user, options={})
  72. Project.allowed_to_condition(user, :view_time_entries, options) do |role, user|
  73. if role.time_entries_visibility == 'all'
  74. nil
  75. elsif role.time_entries_visibility == 'own' && user.id && user.logged?
  76. "#{table_name}.user_id = #{user.id}"
  77. else
  78. '1=0'
  79. end
  80. end
  81. end
  82. # Returns true if user or current user is allowed to view the time entry
  83. def visible?(user=nil)
  84. (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
  85. if role.time_entries_visibility == 'all'
  86. true
  87. elsif role.time_entries_visibility == 'own'
  88. self.user == user
  89. else
  90. false
  91. end
  92. end
  93. end
  94. def initialize(attributes=nil, *args)
  95. super
  96. if new_record? && self.activity.nil?
  97. if default_activity = TimeEntryActivity.default
  98. self.activity_id = default_activity.id
  99. end
  100. self.hours = nil if hours == 0
  101. end
  102. end
  103. def safe_attributes=(attrs, user=User.current)
  104. if attrs
  105. attrs = super(attrs)
  106. if issue_id_changed? && issue
  107. if issue.visible?(user) && user.allowed_to?(:log_time, issue.project)
  108. if attrs[:project_id].blank? && issue.project_id != project_id
  109. self.project_id = issue.project_id
  110. end
  111. @invalid_issue_id = nil
  112. elsif user.allowed_to?(:log_time, issue.project) && issue.assigned_to_id_changed? && issue.previous_assignee == User.current
  113. current_assignee = issue.assigned_to
  114. issue.assigned_to = issue.previous_assignee
  115. unless issue.visible?(user)
  116. @invalid_issue_id = issue_id
  117. end
  118. issue.assigned_to = current_assignee
  119. else
  120. @invalid_issue_id = issue_id
  121. end
  122. end
  123. if user_id_changed? && user_id != author_id && !user.allowed_to?(:log_time_for_other_users, project)
  124. @invalid_user_id = user_id
  125. else
  126. @invalid_user_id = nil
  127. end
  128. # Delete assigned custom fields not visible by the user
  129. editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
  130. self.custom_field_values.delete_if do |c|
  131. !editable_custom_field_ids.include?(c.custom_field.id.to_s)
  132. end
  133. end
  134. attrs
  135. end
  136. def set_project_if_nil
  137. self.project = issue.project if issue && project.nil?
  138. end
  139. def set_author_if_nil
  140. self.author = User.current if author.nil?
  141. end
  142. def validate_time_entry
  143. if hours
  144. errors.add :hours, :invalid if hours < 0
  145. errors.add :hours, :invalid if hours == 0.0 && hours_changed? && !Setting.timelog_accept_0_hours?
  146. max_hours = Setting.timelog_max_hours_per_day.to_f
  147. if hours_changed? && max_hours > 0.0
  148. logged_hours = other_hours_with_same_user_and_day
  149. if logged_hours + hours > max_hours
  150. errors.add(
  151. :base,
  152. I18n.t(:error_exceeds_maximum_hours_per_day,
  153. :logged_hours => format_hours(logged_hours),
  154. :max_hours => format_hours(max_hours)))
  155. end
  156. end
  157. end
  158. errors.add :project_id, :invalid if project.nil?
  159. if @invalid_user_id || (user_id_changed? && user_id != author_id && !self.assignable_users.map(&:id).include?(user_id))
  160. errors.add :user_id, :invalid
  161. end
  162. errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id
  163. errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity)
  164. if spent_on_changed? && user
  165. errors.add :base, I18n.t(:error_spent_on_future_date) if !Setting.timelog_accept_future_dates? && (spent_on > user.today)
  166. end
  167. end
  168. def hours=(h)
  169. write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
  170. end
  171. def hours
  172. h = read_attribute(:hours)
  173. if h.is_a?(Float)
  174. h.round(2)
  175. else
  176. h
  177. end
  178. end
  179. # tyear, tmonth, tweek assigned where setting spent_on attributes
  180. # these attributes make time aggregations easier
  181. def spent_on=(date)
  182. super
  183. self.tyear = spent_on ? spent_on.year : nil
  184. self.tmonth = spent_on ? spent_on.month : nil
  185. self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
  186. end
  187. # Returns true if the time entry can be edited by usr, otherwise false
  188. def editable_by?(usr)
  189. visible?(usr) && (
  190. (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
  191. )
  192. end
  193. # Returns the custom_field_values that can be edited by the given user
  194. def editable_custom_field_values(user=nil)
  195. visible_custom_field_values(user)
  196. end
  197. # Returns the custom fields that can be edited by the given user
  198. def editable_custom_fields(user=nil)
  199. editable_custom_field_values(user).map(&:custom_field).uniq
  200. end
  201. def visible_custom_field_values(user = nil)
  202. user ||= User.current
  203. custom_field_values.select do |value|
  204. value.custom_field.visible_by?(project, user)
  205. end
  206. end
  207. def assignable_users
  208. users = []
  209. if project
  210. users = project.members.active.preload(:user)
  211. users = users.map(&:user).select{|u| u.allowed_to?(:log_time, project)}
  212. end
  213. users << User.current if User.current.logged? && !users.include?(User.current)
  214. users
  215. end
  216. private
  217. # Returns the hours that were logged in other time entries for the same user and the same day
  218. def other_hours_with_same_user_and_day
  219. if user_id && spent_on
  220. TimeEntry.
  221. where(:user_id => user_id, :spent_on => spent_on).
  222. where.not(:id => id).
  223. sum(:hours).to_f
  224. else
  225. 0.0
  226. end
  227. end
  228. end