2011-10-29 16:19:11 +04:00
|
|
|
#-- encoding: UTF-8
|
2011-05-30 00:11:52 +04:00
|
|
|
#-- copyright
|
|
|
|
# ChiliProject is a project management system.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2012-01-03 23:36:40 +04:00
|
|
|
# Copyright (C) 2010-2012 the ChiliProject Team
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +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-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# See doc/COPYRIGHT.rdoc for more details.
|
|
|
|
#++
|
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
class TimeEntry < ActiveRecord::Base
|
|
|
|
# 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-30 22:52:25 +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
|
2010-09-15 16:26:42 +04:00
|
|
|
acts_as_journalized :event_title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
|
2010-10-26 20:28:39 +04:00
|
|
|
:event_url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
|
2010-07-15 20:24:12 +04:00
|
|
|
:event_author => :user,
|
2010-07-15 23:10:05 +04:00
|
|
|
:event_description => :comments
|
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
|
2011-05-30 22:52:25 +04:00
|
|
|
|
|
|
|
named_scope :visible, lambda {|*args| {
|
2011-03-16 20:29:30 +03:00
|
|
|
:include => :project,
|
2011-05-30 22:52:25 +04:00
|
|
|
:conditions => Project.allowed_to_condition(args.first || User.current, :view_time_entries)
|
2011-03-16 20:29:30 +03:00
|
|
|
}}
|
2007-03-23 15:22:31 +03:00
|
|
|
|
2008-05-26 21:12:11 +04:00
|
|
|
def after_initialize
|
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-30 22:52:25 +04:00
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
def before_validation
|
|
|
|
self.project = issue.project if issue && project.nil?
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2007-03-23 15:22:31 +03:00
|
|
|
def validate
|
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-30 22:52:25 +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-30 22:52:25 +04:00
|
|
|
|
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-30 22:52:25 +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
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2011-03-16 20:29:30 +03:00
|
|
|
# TODO: remove this method in 1.3.0
|
2008-02-27 23:50:19 +03:00
|
|
|
def self.visible_by(usr)
|
2011-03-16 20:29:30 +03:00
|
|
|
ActiveSupport::Deprecation.warn "TimeEntry.visible_by is deprecated and will be removed in Redmine 1.3.0. Use the visible scope instead."
|
2008-02-27 23:50:19 +03:00
|
|
|
with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2010-09-14 23:02:20 +04:00
|
|
|
|
2010-09-14 23:02:25 +04:00
|
|
|
def self.earilest_date_for_project(project=nil)
|
|
|
|
finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
|
|
|
|
if project
|
|
|
|
finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
|
|
|
|
end
|
|
|
|
TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
|
2010-09-14 23:02:20 +04:00
|
|
|
end
|
|
|
|
|
2010-09-14 23:02:25 +04:00
|
|
|
def self.latest_date_for_project(project=nil)
|
|
|
|
finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
|
|
|
|
if project
|
|
|
|
finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
|
|
|
|
end
|
|
|
|
TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
|
2010-09-14 23:02:20 +04:00
|
|
|
end
|
2007-03-23 15:22:31 +03:00
|
|
|
end
|