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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 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 :title =>
  29. Proc.new {|o|
  30. related = o.issue if o.issue && o.issue.visible?
  31. related ||= o.project
  32. "#{l_hours(o.hours)} (#{related.event_title})"
  33. },
  34. :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
  35. :author => :user,
  36. :group => :issue,
  37. :description => :comments
  38. acts_as_activity_provider :timestamp => "#{table_name}.created_on",
  39. :author_key => :user_id,
  40. :scope => proc { joins(:project).preload(:project) }
  41. validates_presence_of :author_id, :user_id, :activity_id, :project_id, :hours, :spent_on
  42. validates_presence_of :issue_id, :if => lambda { Setting.timelog_required_fields.include?('issue_id') }
  43. validates_presence_of :comments, :if => lambda { Setting.timelog_required_fields.include?('comments') }
  44. validates_numericality_of :hours, :allow_nil => true, :message => :invalid
  45. validates_length_of :comments, :maximum => 1024, :allow_nil => true
  46. validates :spent_on, :date => true
  47. before_validation :set_project_if_nil
  48. # TODO: remove this, author should be always explicitly set
  49. before_validation :set_author_if_nil
  50. validate :validate_time_entry
  51. scope :visible, lambda {|*args|
  52. joins(:project).
  53. where(TimeEntry.visible_condition(args.shift || User.current, *args))
  54. }
  55. scope :left_join_issue, lambda {
  56. joins("LEFT OUTER JOIN #{Issue.table_name} ON #{Issue.table_name}.id = #{TimeEntry.table_name}.issue_id")
  57. }
  58. scope :on_issue, lambda {|issue|
  59. joins(:issue).
  60. where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
  61. }
  62. safe_attributes 'user_id', 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
  63. # Returns a SQL conditions string used to find all time entries visible by the specified user
  64. def self.visible_condition(user, options={})
  65. Project.allowed_to_condition(user, :view_time_entries, options) do |role, user|
  66. if role.time_entries_visibility == 'all'
  67. nil
  68. elsif role.time_entries_visibility == 'own' && user.id && user.logged?
  69. "#{table_name}.user_id = #{user.id}"
  70. else
  71. '1=0'
  72. end
  73. end
  74. end
  75. # Returns true if user or current user is allowed to view the time entry
  76. def visible?(user=nil)
  77. (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
  78. if role.time_entries_visibility == 'all'
  79. true
  80. elsif role.time_entries_visibility == 'own'
  81. self.user == user
  82. else
  83. false
  84. end
  85. end
  86. end
  87. def initialize(attributes=nil, *args)
  88. super
  89. if new_record? && self.activity.nil?
  90. if default_activity = TimeEntryActivity.default
  91. self.activity_id = default_activity.id
  92. end
  93. self.hours = nil if hours == 0
  94. end
  95. end
  96. def safe_attributes=(attrs, user=User.current)
  97. if attrs
  98. attrs = super(attrs)
  99. if issue_id_changed? && issue
  100. if issue.visible?(user) && user.allowed_to?(:log_time, issue.project)
  101. if attrs[:project_id].blank? && issue.project_id != project_id
  102. self.project_id = issue.project_id
  103. end
  104. @invalid_issue_id = nil
  105. else
  106. @invalid_issue_id = issue_id
  107. end
  108. end
  109. if user_id_changed? && user_id != author_id && !user.allowed_to?(:log_time_for_other_users, project)
  110. @invalid_user_id = user_id
  111. else
  112. @invalid_user_id = nil
  113. end
  114. end
  115. attrs
  116. end
  117. def set_project_if_nil
  118. self.project = issue.project if issue && project.nil?
  119. end
  120. def set_author_if_nil
  121. self.author = User.current if author.nil?
  122. end
  123. def validate_time_entry
  124. if hours
  125. errors.add :hours, :invalid if hours < 0
  126. errors.add :hours, :invalid if hours == 0.0 && hours_changed? && !Setting.timelog_accept_0_hours?
  127. max_hours = Setting.timelog_max_hours_per_day.to_f
  128. if hours_changed? && max_hours > 0.0
  129. logged_hours = other_hours_with_same_user_and_day
  130. if logged_hours + hours > max_hours
  131. errors.add(
  132. :base,
  133. I18n.t(:error_exceeds_maximum_hours_per_day,
  134. :logged_hours => format_hours(logged_hours),
  135. :max_hours => format_hours(max_hours)))
  136. end
  137. end
  138. end
  139. errors.add :project_id, :invalid if project.nil?
  140. if @invalid_user_id || (user_id_changed? && user_id != author_id && !self.assignable_users.map(&:id).include?(user_id))
  141. errors.add :user_id, :invalid
  142. end
  143. errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id
  144. errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity)
  145. if spent_on_changed? && user
  146. errors.add :base, I18n.t(:error_spent_on_future_date) if !Setting.timelog_accept_future_dates? && (spent_on > user.today)
  147. end
  148. end
  149. def hours=(h)
  150. write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
  151. end
  152. def hours
  153. h = read_attribute(:hours)
  154. if h.is_a?(Float)
  155. h.round(2)
  156. else
  157. h
  158. end
  159. end
  160. # tyear, tmonth, tweek assigned where setting spent_on attributes
  161. # these attributes make time aggregations easier
  162. def spent_on=(date)
  163. super
  164. self.tyear = spent_on ? spent_on.year : nil
  165. self.tmonth = spent_on ? spent_on.month : nil
  166. self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
  167. end
  168. # Returns true if the time entry can be edited by usr, otherwise false
  169. def editable_by?(usr)
  170. visible?(usr) && (
  171. (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
  172. )
  173. end
  174. # Returns the custom_field_values that can be edited by the given user
  175. def editable_custom_field_values(user=nil)
  176. visible_custom_field_values
  177. end
  178. # Returns the custom fields that can be edited by the given user
  179. def editable_custom_fields(user=nil)
  180. editable_custom_field_values(user).map(&:custom_field).uniq
  181. end
  182. def visible_custom_field_values(user = nil)
  183. user ||= User.current
  184. custom_field_values.select do |value|
  185. value.custom_field.visible_by?(project, user)
  186. end
  187. end
  188. def assignable_users
  189. users = []
  190. if project
  191. users = project.members.active.preload(:user)
  192. users = users.map(&:user).select{ |u| u.allowed_to?(:log_time, project) }
  193. end
  194. users << User.current if User.current.logged? && !users.include?(User.current)
  195. users
  196. end
  197. private
  198. # Returns the hours that were logged in other time entries for the same user and the same day
  199. def other_hours_with_same_user_and_day
  200. if user_id && spent_on
  201. TimeEntry.
  202. where(:user_id => user_id, :spent_on => spent_on).
  203. where.not(:id => id).
  204. sum(:hours).to_f
  205. else
  206. 0.0
  207. end
  208. end
  209. end