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 7.6KB

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