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