Chris@909
|
1 # Redmine - project management software
|
Chris@909
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@909
|
3 #
|
Chris@909
|
4 # This program is free software; you can redistribute it and/or
|
Chris@909
|
5 # modify it under the terms of the GNU General Public License
|
Chris@909
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@909
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@909
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@909
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@909
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@909
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@909
|
14 # You should have received a copy of the GNU General Public License
|
Chris@909
|
15 # along with this program; if not, write to the Free Software
|
Chris@909
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@909
|
17
|
Chris@909
|
18 class TimeEntry < ActiveRecord::Base
|
Chris@909
|
19 # could have used polymorphic association
|
Chris@909
|
20 # project association here allows easy loading of time entries at project level with one database trip
|
Chris@909
|
21 belongs_to :project
|
Chris@909
|
22 belongs_to :issue
|
Chris@909
|
23 belongs_to :user
|
Chris@909
|
24 belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
|
Chris@909
|
25
|
Chris@909
|
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
|
Chris@909
|
27
|
Chris@909
|
28 acts_as_customizable
|
Chris@909
|
29 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
|
Chris@909
|
30 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
|
Chris@909
|
31 :author => :user,
|
Chris@909
|
32 :description => :comments
|
Chris@909
|
33
|
Chris@909
|
34 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
|
Chris@909
|
35 :author_key => :user_id,
|
Chris@909
|
36 :find_options => {:include => :project}
|
Chris@909
|
37
|
Chris@909
|
38 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
|
Chris@909
|
39 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
|
Chris@909
|
40 validates_length_of :comments, :maximum => 255, :allow_nil => true
|
Chris@909
|
41 before_validation :set_project_if_nil
|
Chris@909
|
42 validate :validate_time_entry
|
Chris@909
|
43
|
Chris@909
|
44 named_scope :visible, lambda {|*args| {
|
Chris@909
|
45 :include => :project,
|
Chris@909
|
46 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
|
Chris@909
|
47 }}
|
Chris@909
|
48
|
Chris@909
|
49 def after_initialize
|
Chris@909
|
50 if new_record? && self.activity.nil?
|
Chris@909
|
51 if default_activity = TimeEntryActivity.default
|
Chris@909
|
52 self.activity_id = default_activity.id
|
Chris@909
|
53 end
|
Chris@909
|
54 self.hours = nil if hours == 0
|
Chris@909
|
55 end
|
Chris@909
|
56 end
|
Chris@909
|
57
|
Chris@909
|
58 def set_project_if_nil
|
Chris@909
|
59 self.project = issue.project if issue && project.nil?
|
Chris@909
|
60 end
|
Chris@909
|
61
|
Chris@909
|
62 def validate_time_entry
|
Chris@909
|
63 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
|
Chris@909
|
64 errors.add :project_id, :invalid if project.nil?
|
Chris@909
|
65 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
|
Chris@909
|
66 end
|
Chris@909
|
67
|
Chris@909
|
68 def hours=(h)
|
Chris@909
|
69 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
|
Chris@909
|
70 end
|
Chris@909
|
71
|
Chris@909
|
72 # tyear, tmonth, tweek assigned where setting spent_on attributes
|
Chris@909
|
73 # these attributes make time aggregations easier
|
Chris@909
|
74 def spent_on=(date)
|
Chris@909
|
75 super
|
Chris@909
|
76 if spent_on.is_a?(Time)
|
Chris@909
|
77 self.spent_on = spent_on.to_date
|
Chris@909
|
78 end
|
Chris@909
|
79 self.tyear = spent_on ? spent_on.year : nil
|
Chris@909
|
80 self.tmonth = spent_on ? spent_on.month : nil
|
Chris@909
|
81 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
|
Chris@909
|
82 end
|
Chris@909
|
83
|
Chris@909
|
84 # Returns true if the time entry can be edited by usr, otherwise false
|
Chris@909
|
85 def editable_by?(usr)
|
Chris@909
|
86 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
|
Chris@909
|
87 end
|
Chris@909
|
88
|
Chris@909
|
89 def self.earilest_date_for_project(project=nil)
|
Chris@909
|
90 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
|
Chris@909
|
91 if project
|
Chris@909
|
92 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
|
Chris@909
|
93 end
|
Chris@909
|
94 TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
|
Chris@909
|
95 end
|
Chris@909
|
96
|
Chris@909
|
97 def self.latest_date_for_project(project=nil)
|
Chris@909
|
98 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
|
Chris@909
|
99 if project
|
Chris@909
|
100 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
|
Chris@909
|
101 end
|
Chris@909
|
102 TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
|
Chris@909
|
103 end
|
Chris@909
|
104 end
|