Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: class TimeEntry < ActiveRecord::Base Chris@1494: include Redmine::SafeAttributes Chris@1494: # could have used polymorphic association Chris@1494: # project association here allows easy loading of time entries at project level with one database trip Chris@1494: belongs_to :project Chris@1494: belongs_to :issue Chris@1494: belongs_to :user Chris@1494: belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id' Chris@1494: Chris@1494: attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek Chris@1494: Chris@1494: acts_as_customizable Chris@1494: acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"}, Chris@1494: :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}}, Chris@1494: :author => :user, Chris@1494: :group => :issue, Chris@1494: :description => :comments Chris@1494: Chris@1494: acts_as_activity_provider :timestamp => "#{table_name}.created_on", Chris@1494: :author_key => :user_id, Chris@1494: :find_options => {:include => :project} Chris@1494: Chris@1494: validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on Chris@1494: validates_numericality_of :hours, :allow_nil => true, :message => :invalid Chris@1494: validates_length_of :comments, :maximum => 255, :allow_nil => true Chris@1494: validates :spent_on, :date => true Chris@1494: before_validation :set_project_if_nil Chris@1494: validate :validate_time_entry Chris@1494: Chris@1494: scope :visible, lambda {|*args| Chris@1494: includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)) Chris@1494: } Chris@1494: scope :on_issue, lambda {|issue| Chris@1494: includes(:issue).where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}") Chris@1494: } Chris@1494: scope :on_project, lambda {|project, include_subprojects| Chris@1494: includes(:project).where(project.project_condition(include_subprojects)) Chris@1494: } Chris@1494: scope :spent_between, lambda {|from, to| Chris@1494: if from && to Chris@1494: where("#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to) Chris@1494: elsif from Chris@1494: where("#{TimeEntry.table_name}.spent_on >= ?", from) Chris@1494: elsif to Chris@1494: where("#{TimeEntry.table_name}.spent_on <= ?", to) Chris@1494: else Chris@1494: where(nil) Chris@1494: end Chris@1494: } Chris@1494: Chris@1494: safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields' Chris@1494: Chris@1494: def initialize(attributes=nil, *args) Chris@1494: super Chris@1494: if new_record? && self.activity.nil? Chris@1494: if default_activity = TimeEntryActivity.default Chris@1494: self.activity_id = default_activity.id Chris@1494: end Chris@1494: self.hours = nil if hours == 0 Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def set_project_if_nil Chris@1494: self.project = issue.project if issue && project.nil? Chris@1494: end Chris@1494: Chris@1494: def validate_time_entry Chris@1494: errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) Chris@1494: errors.add :project_id, :invalid if project.nil? Chris@1494: errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) Chris@1494: end Chris@1494: Chris@1494: def hours=(h) Chris@1494: write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) Chris@1494: end Chris@1494: Chris@1494: def hours Chris@1494: h = read_attribute(:hours) Chris@1494: if h.is_a?(Float) Chris@1494: h.round(2) Chris@1494: else Chris@1494: h Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: # tyear, tmonth, tweek assigned where setting spent_on attributes Chris@1494: # these attributes make time aggregations easier Chris@1494: def spent_on=(date) Chris@1494: super Chris@1494: if spent_on.is_a?(Time) Chris@1494: self.spent_on = spent_on.to_date Chris@1494: end Chris@1494: self.tyear = spent_on ? spent_on.year : nil Chris@1494: self.tmonth = spent_on ? spent_on.month : nil Chris@1494: self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil Chris@1494: end Chris@1494: Chris@1494: # Returns true if the time entry can be edited by usr, otherwise false Chris@1494: def editable_by?(usr) Chris@1494: (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) Chris@1494: end Chris@1494: end