Chris@441
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 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@441
|
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@441
|
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@929
|
19 include Redmine::SafeAttributes
|
Chris@0
|
20 # could have used polymorphic association
|
Chris@0
|
21 # project association here allows easy loading of time entries at project level with one database trip
|
Chris@0
|
22 belongs_to :project
|
Chris@0
|
23 belongs_to :issue
|
Chris@0
|
24 belongs_to :user
|
Chris@0
|
25 belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
|
Chris@441
|
26
|
Chris@0
|
27 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
|
Chris@0
|
28
|
Chris@0
|
29 acts_as_customizable
|
Chris@0
|
30 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
|
chris@37
|
31 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
|
Chris@0
|
32 :author => :user,
|
Chris@1464
|
33 :group => :issue,
|
Chris@0
|
34 :description => :comments
|
Chris@0
|
35
|
Chris@0
|
36 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
|
Chris@0
|
37 :author_key => :user_id,
|
Chris@441
|
38 :find_options => {:include => :project}
|
Chris@0
|
39
|
Chris@0
|
40 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
|
Chris@0
|
41 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
|
Chris@0
|
42 validates_length_of :comments, :maximum => 255, :allow_nil => true
|
Chris@1464
|
43 validates :spent_on, :date => true
|
Chris@909
|
44 before_validation :set_project_if_nil
|
Chris@909
|
45 validate :validate_time_entry
|
Chris@0
|
46
|
Chris@1464
|
47 scope :visible, lambda {|*args|
|
Chris@1464
|
48 includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args))
|
Chris@1464
|
49 }
|
Chris@1464
|
50 scope :on_issue, lambda {|issue|
|
Chris@1464
|
51 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@1464
|
52 }
|
Chris@1464
|
53 scope :on_project, lambda {|project, include_subprojects|
|
Chris@1464
|
54 includes(:project).where(project.project_condition(include_subprojects))
|
Chris@1464
|
55 }
|
Chris@1115
|
56 scope :spent_between, lambda {|from, to|
|
Chris@1115
|
57 if from && to
|
Chris@1464
|
58 where("#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to)
|
Chris@1115
|
59 elsif from
|
Chris@1464
|
60 where("#{TimeEntry.table_name}.spent_on >= ?", from)
|
Chris@1115
|
61 elsif to
|
Chris@1464
|
62 where("#{TimeEntry.table_name}.spent_on <= ?", to)
|
Chris@1115
|
63 else
|
Chris@1464
|
64 where(nil)
|
Chris@1115
|
65 end
|
Chris@1115
|
66 }
|
Chris@441
|
67
|
Chris@1115
|
68 safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
|
Chris@929
|
69
|
Chris@1115
|
70 def initialize(attributes=nil, *args)
|
Chris@1115
|
71 super
|
Chris@0
|
72 if new_record? && self.activity.nil?
|
Chris@0
|
73 if default_activity = TimeEntryActivity.default
|
Chris@0
|
74 self.activity_id = default_activity.id
|
Chris@0
|
75 end
|
Chris@0
|
76 self.hours = nil if hours == 0
|
Chris@0
|
77 end
|
Chris@0
|
78 end
|
Chris@441
|
79
|
Chris@1517
|
80 def safe_attributes=(attrs, user=User.current)
|
Chris@1517
|
81 attrs = super
|
Chris@1517
|
82 if !new_record? && issue && issue.project_id != project_id
|
Chris@1517
|
83 if user.allowed_to?(:log_time, issue.project)
|
Chris@1517
|
84 self.project_id = issue.project_id
|
Chris@1517
|
85 end
|
Chris@1517
|
86 end
|
Chris@1517
|
87 attrs
|
Chris@1517
|
88 end
|
Chris@1517
|
89
|
Chris@909
|
90 def set_project_if_nil
|
Chris@0
|
91 self.project = issue.project if issue && project.nil?
|
Chris@0
|
92 end
|
Chris@441
|
93
|
Chris@909
|
94 def validate_time_entry
|
Chris@0
|
95 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
|
Chris@0
|
96 errors.add :project_id, :invalid if project.nil?
|
Chris@0
|
97 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
|
Chris@0
|
98 end
|
Chris@441
|
99
|
Chris@0
|
100 def hours=(h)
|
Chris@0
|
101 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
|
Chris@0
|
102 end
|
Chris@441
|
103
|
Chris@1115
|
104 def hours
|
Chris@1115
|
105 h = read_attribute(:hours)
|
Chris@1115
|
106 if h.is_a?(Float)
|
Chris@1115
|
107 h.round(2)
|
Chris@1115
|
108 else
|
Chris@1115
|
109 h
|
Chris@1115
|
110 end
|
Chris@1115
|
111 end
|
Chris@1115
|
112
|
Chris@0
|
113 # tyear, tmonth, tweek assigned where setting spent_on attributes
|
Chris@0
|
114 # these attributes make time aggregations easier
|
Chris@0
|
115 def spent_on=(date)
|
Chris@0
|
116 super
|
Chris@128
|
117 if spent_on.is_a?(Time)
|
Chris@128
|
118 self.spent_on = spent_on.to_date
|
Chris@128
|
119 end
|
Chris@0
|
120 self.tyear = spent_on ? spent_on.year : nil
|
Chris@0
|
121 self.tmonth = spent_on ? spent_on.month : nil
|
Chris@0
|
122 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
|
Chris@0
|
123 end
|
Chris@441
|
124
|
Chris@0
|
125 # Returns true if the time entry can be edited by usr, otherwise false
|
Chris@0
|
126 def editable_by?(usr)
|
Chris@0
|
127 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
|
Chris@0
|
128 end
|
Chris@0
|
129 end
|