2011-03-16 20:29:30 +03:00
|
|
|
# Redmine - project management software
|
2013-01-12 13:29:31 +04:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2007-06-24 20:07:06 +04:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-05-17 07:58:41 +04:00
|
|
|
#
|
2007-06-24 20:07:06 +04:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-05-17 07:58:41 +04:00
|
|
|
#
|
2007-06-24 20:07:06 +04:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
class TimeEntry < ActiveRecord::Base
|
2012-03-07 00:23:00 +04:00
|
|
|
include Redmine::SafeAttributes
|
2007-03-23 15:22:31 +03:00
|
|
|
# could have used polymorphic association
|
|
|
|
# project association here allows easy loading of time entries at project level with one database trip
|
|
|
|
belongs_to :project
|
|
|
|
belongs_to :issue
|
|
|
|
belongs_to :user
|
2009-05-31 03:30:36 +04:00
|
|
|
belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
|
2011-05-17 07:58:41 +04:00
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
|
2008-06-18 00:01:15 +04:00
|
|
|
|
2008-07-22 22:52:00 +04:00
|
|
|
acts_as_customizable
|
2009-09-12 14:37:49 +04:00
|
|
|
acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
|
2010-10-23 00:40:11 +04:00
|
|
|
:url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
|
2008-06-18 00:01:15 +04:00
|
|
|
:author => :user,
|
2012-12-08 12:24:01 +04:00
|
|
|
:group => :issue,
|
2008-06-18 00:01:15 +04:00
|
|
|
:description => :comments
|
2009-09-12 14:37:49 +04:00
|
|
|
|
|
|
|
acts_as_activity_provider :timestamp => "#{table_name}.created_on",
|
|
|
|
:author_key => :user_id,
|
2011-05-17 07:58:41 +04:00
|
|
|
:find_options => {:include => :project}
|
2009-09-12 14:37:49 +04:00
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
|
2009-02-21 14:04:50 +03:00
|
|
|
validates_numericality_of :hours, :allow_nil => true, :message => :invalid
|
2008-06-30 21:31:50 +04:00
|
|
|
validates_length_of :comments, :maximum => 255, :allow_nil => true
|
2013-02-24 16:28:44 +04:00
|
|
|
validates :spent_on, :date => true
|
2011-09-22 17:39:06 +04:00
|
|
|
before_validation :set_project_if_nil
|
2011-09-22 16:06:33 +04:00
|
|
|
validate :validate_time_entry
|
2011-05-17 07:58:41 +04:00
|
|
|
|
2012-12-07 21:59:20 +04:00
|
|
|
scope :visible, lambda {|*args|
|
|
|
|
includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args))
|
|
|
|
}
|
|
|
|
scope :on_issue, lambda {|issue|
|
|
|
|
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}")
|
|
|
|
}
|
|
|
|
scope :on_project, lambda {|project, include_subprojects|
|
|
|
|
includes(:project).where(project.project_condition(include_subprojects))
|
|
|
|
}
|
2012-04-27 03:51:10 +04:00
|
|
|
scope :spent_between, lambda {|from, to|
|
2011-12-05 03:01:42 +04:00
|
|
|
if from && to
|
2012-12-07 21:59:20 +04:00
|
|
|
where("#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to)
|
2011-12-05 03:01:42 +04:00
|
|
|
elsif from
|
2012-12-07 21:59:20 +04:00
|
|
|
where("#{TimeEntry.table_name}.spent_on >= ?", from)
|
2011-12-05 03:01:42 +04:00
|
|
|
elsif to
|
2012-12-07 21:59:20 +04:00
|
|
|
where("#{TimeEntry.table_name}.spent_on <= ?", to)
|
2011-12-05 03:01:42 +04:00
|
|
|
else
|
2012-12-07 21:59:20 +04:00
|
|
|
where(nil)
|
2011-12-05 03:01:42 +04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2012-06-09 11:55:26 +04:00
|
|
|
safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
|
2012-03-07 00:23:00 +04:00
|
|
|
|
2011-12-18 18:57:58 +04:00
|
|
|
def initialize(attributes=nil, *args)
|
|
|
|
super
|
2008-06-14 16:31:32 +04:00
|
|
|
if new_record? && self.activity.nil?
|
2009-05-31 03:30:36 +04:00
|
|
|
if default_activity = TimeEntryActivity.default
|
2008-06-14 16:31:32 +04:00
|
|
|
self.activity_id = default_activity.id
|
|
|
|
end
|
2009-12-23 23:05:46 +03:00
|
|
|
self.hours = nil if hours == 0
|
2008-05-26 21:12:11 +04:00
|
|
|
end
|
|
|
|
end
|
2011-05-17 07:58:41 +04:00
|
|
|
|
2011-09-22 17:39:06 +04:00
|
|
|
def set_project_if_nil
|
2007-03-23 15:22:31 +03:00
|
|
|
self.project = issue.project if issue && project.nil?
|
|
|
|
end
|
2011-05-17 07:58:41 +04:00
|
|
|
|
2011-09-22 16:06:33 +04:00
|
|
|
def validate_time_entry
|
2009-02-21 14:04:50 +03:00
|
|
|
errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
|
|
|
|
errors.add :project_id, :invalid if project.nil?
|
|
|
|
errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
|
2007-03-23 15:22:31 +03:00
|
|
|
end
|
2011-05-17 07:58:41 +04:00
|
|
|
|
2008-04-02 23:52:12 +04:00
|
|
|
def hours=(h)
|
2009-01-09 20:32:46 +03:00
|
|
|
write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
|
2008-04-02 23:52:12 +04:00
|
|
|
end
|
2011-05-17 07:58:41 +04:00
|
|
|
|
2012-02-24 22:40:22 +04:00
|
|
|
def hours
|
|
|
|
h = read_attribute(:hours)
|
|
|
|
if h.is_a?(Float)
|
|
|
|
h.round(2)
|
|
|
|
else
|
|
|
|
h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
# tyear, tmonth, tweek assigned where setting spent_on attributes
|
|
|
|
# these attributes make time aggregations easier
|
|
|
|
def spent_on=(date)
|
|
|
|
super
|
2011-01-14 21:04:16 +03:00
|
|
|
if spent_on.is_a?(Time)
|
|
|
|
self.spent_on = spent_on.to_date
|
|
|
|
end
|
2007-03-23 15:22:31 +03:00
|
|
|
self.tyear = spent_on ? spent_on.year : nil
|
|
|
|
self.tmonth = spent_on ? spent_on.month : nil
|
2007-04-25 19:06:20 +04:00
|
|
|
self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
|
2008-02-26 21:15:58 +03:00
|
|
|
end
|
2011-05-17 07:58:41 +04:00
|
|
|
|
2008-02-26 21:15:58 +03:00
|
|
|
# Returns true if the time entry can be edited by usr, otherwise false
|
|
|
|
def editable_by?(usr)
|
2008-03-15 00:17:09 +03:00
|
|
|
(usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
|
2008-02-26 21:15:58 +03:00
|
|
|
end
|
2007-03-23 15:22:31 +03:00
|
|
|
end
|