|
@@
-1,160
+1,160
|
|
1
|
# Redmine - project management software
|
|
1
|
# Redmine - project management software
|
|
2
|
# Copyright (C) 2006-2015 Jean-Philippe Lang
|
|
2
|
# Copyright (C) 2006-2015 Jean-Philippe Lang
|
|
3
|
#
|
|
3
|
#
|
|
4
|
# This program is free software; you can redistribute it and/or
|
|
4
|
# This program is free software; you can redistribute it and/or
|
|
5
|
# modify it under the terms of the GNU General Public License
|
|
5
|
# modify it under the terms of the GNU General Public License
|
|
6
|
# as published by the Free Software Foundation; either version 2
|
|
6
|
# as published by the Free Software Foundation; either version 2
|
|
7
|
# of the License, or (at your option) any later version.
|
|
7
|
# of the License, or (at your option) any later version.
|
|
8
|
#
|
|
8
|
#
|
|
9
|
# This program is distributed in the hope that it will be useful,
|
|
9
|
# This program is distributed in the hope that it will be useful,
|
|
10
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
# GNU General Public License for more details.
|
|
12
|
# GNU General Public License for more details.
|
|
13
|
#
|
|
13
|
#
|
|
14
|
# You should have received a copy of the GNU General Public License
|
|
14
|
# You should have received a copy of the GNU General Public License
|
|
15
|
# along with this program; if not, write to the Free Software
|
|
15
|
# along with this program; if not, write to the Free Software
|
|
16
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
16
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
17
|
|
|
17
|
|
|
18
|
class TimeEntry < ActiveRecord::Base
|
|
18
|
class TimeEntry < ActiveRecord::Base
|
|
19
|
include Redmine::SafeAttributes
|
|
19
|
include Redmine::SafeAttributes
|
|
20
|
# could have used polymorphic association
|
|
20
|
# could have used polymorphic association
|
|
21
|
# project association here allows easy loading of time entries at project level with one database trip
|
|
21
|
# project association here allows easy loading of time entries at project level with one database trip
|
|
22
|
belongs_to :project
|
|
22
|
belongs_to :project
|
|
23
|
belongs_to :issue
|
|
23
|
belongs_to :issue
|
|
24
|
belongs_to :user
|
|
24
|
belongs_to :user
|
|
25
|
belongs_to :activity, :class_name => 'TimeEntryActivity'
|
|
25
|
belongs_to :activity, :class_name => 'TimeEntryActivity'
|
|
26
|
|
|
26
|
|
|
27
|
attr_protected :user_id, :tyear, :tmonth, :tweek
|
|
27
|
attr_protected :user_id, :tyear, :tmonth, :tweek
|
|
28
|
|
|
28
|
|
|
29
|
acts_as_customizable
|
|
29
|
acts_as_customizable
|
|
30
|
acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
|
|
30
|
acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
|
|
31
|
:url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
|
|
31
|
:url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
|
|
32
|
:author => :user,
|
|
32
|
:author => :user,
|
|
33
|
:group => :issue,
|
|
33
|
:group => :issue,
|
|
34
|
:description => :comments
|
|
34
|
:description => :comments
|
|
35
|
|
|
35
|
|
|
36
|
acts_as_activity_provider :timestamp => "#{table_name}.created_on",
|
|
36
|
acts_as_activity_provider :timestamp => "#{table_name}.created_on",
|
|
37
|
:author_key => :user_id,
|
|
37
|
:author_key => :user_id,
|
|
38
|
:scope => joins(:project).preload(:project)
|
|
38
|
:scope => joins(:project).preload(:project)
|
|
39
|
|
|
39
|
|
|
40
|
validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
|
|
40
|
validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
|
|
41
|
validates_numericality_of :hours, :allow_nil => true, :message => :invalid
|
|
41
|
validates_numericality_of :hours, :allow_nil => true, :message => :invalid
|
|
42
|
validates_length_of :comments, :maximum => 255, :allow_nil => true
|
|
42
|
validates_length_of :comments, :maximum => 1024, :allow_nil => true
|
|
43
|
validates :spent_on, :date => true
|
|
43
|
validates :spent_on, :date => true
|
|
44
|
before_validation :set_project_if_nil
|
|
44
|
before_validation :set_project_if_nil
|
|
45
|
validate :validate_time_entry
|
|
45
|
validate :validate_time_entry
|
|
46
|
|
|
46
|
|
|
47
|
scope :visible, lambda {|*args|
|
|
47
|
scope :visible, lambda {|*args|
|
|
48
|
joins(:project).
|
|
48
|
joins(:project).
|
|
49
|
where(TimeEntry.visible_condition(args.shift || User.current, *args))
|
|
49
|
where(TimeEntry.visible_condition(args.shift || User.current, *args))
|
|
50
|
}
|
|
50
|
}
|
|
51
|
scope :on_issue, lambda {|issue|
|
|
51
|
scope :on_issue, lambda {|issue|
|
|
52
|
joins(:issue).
|
|
52
|
joins(:issue).
|
|
53
|
where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
|
|
53
|
where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
|
|
54
|
}
|
|
54
|
}
|
|
55
|
|
|
55
|
|
|
56
|
safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
|
|
56
|
safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
|
|
57
|
|
|
57
|
|
|
58
|
# Returns a SQL conditions string used to find all time entries visible by the specified user
|
|
58
|
# Returns a SQL conditions string used to find all time entries visible by the specified user
|
|
59
|
def self.visible_condition(user, options={})
|
|
59
|
def self.visible_condition(user, options={})
|
|
60
|
Project.allowed_to_condition(user, :view_time_entries, options) do |role, user|
|
|
60
|
Project.allowed_to_condition(user, :view_time_entries, options) do |role, user|
|
|
61
|
if role.time_entries_visibility == 'all'
|
|
61
|
if role.time_entries_visibility == 'all'
|
|
62
|
nil
|
|
62
|
nil
|
|
63
|
elsif role.time_entries_visibility == 'own' && user.id && user.logged?
|
|
63
|
elsif role.time_entries_visibility == 'own' && user.id && user.logged?
|
|
64
|
"#{table_name}.user_id = #{user.id}"
|
|
64
|
"#{table_name}.user_id = #{user.id}"
|
|
65
|
else
|
|
65
|
else
|
|
66
|
'1=0'
|
|
66
|
'1=0'
|
|
67
|
end
|
|
67
|
end
|
|
68
|
end
|
|
68
|
end
|
|
69
|
end
|
|
69
|
end
|
|
70
|
|
|
70
|
|
|
71
|
# Returns true if user or current user is allowed to view the time entry
|
|
71
|
# Returns true if user or current user is allowed to view the time entry
|
|
72
|
def visible?(user=nil)
|
|
72
|
def visible?(user=nil)
|
|
73
|
(user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
|
|
73
|
(user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
|
|
74
|
if role.time_entries_visibility == 'all'
|
|
74
|
if role.time_entries_visibility == 'all'
|
|
75
|
true
|
|
75
|
true
|
|
76
|
elsif role.time_entries_visibility == 'own'
|
|
76
|
elsif role.time_entries_visibility == 'own'
|
|
77
|
self.user == user
|
|
77
|
self.user == user
|
|
78
|
else
|
|
78
|
else
|
|
79
|
false
|
|
79
|
false
|
|
80
|
end
|
|
80
|
end
|
|
81
|
end
|
|
81
|
end
|
|
82
|
end
|
|
82
|
end
|
|
83
|
|
|
83
|
|
|
84
|
def initialize(attributes=nil, *args)
|
|
84
|
def initialize(attributes=nil, *args)
|
|
85
|
super
|
|
85
|
super
|
|
86
|
if new_record? && self.activity.nil?
|
|
86
|
if new_record? && self.activity.nil?
|
|
87
|
if default_activity = TimeEntryActivity.default
|
|
87
|
if default_activity = TimeEntryActivity.default
|
|
88
|
self.activity_id = default_activity.id
|
|
88
|
self.activity_id = default_activity.id
|
|
89
|
end
|
|
89
|
end
|
|
90
|
self.hours = nil if hours == 0
|
|
90
|
self.hours = nil if hours == 0
|
|
91
|
end
|
|
91
|
end
|
|
92
|
end
|
|
92
|
end
|
|
93
|
|
|
93
|
|
|
94
|
def safe_attributes=(attrs, user=User.current)
|
|
94
|
def safe_attributes=(attrs, user=User.current)
|
|
95
|
if attrs
|
|
95
|
if attrs
|
|
96
|
attrs = super(attrs)
|
|
96
|
attrs = super(attrs)
|
|
97
|
if issue_id_changed? && issue
|
|
97
|
if issue_id_changed? && issue
|
|
98
|
if user.allowed_to?(:log_time, issue.project)
|
|
98
|
if user.allowed_to?(:log_time, issue.project)
|
|
99
|
if attrs[:project_id].blank? && issue.project_id != project_id
|
|
99
|
if attrs[:project_id].blank? && issue.project_id != project_id
|
|
100
|
self.project_id = issue.project_id
|
|
100
|
self.project_id = issue.project_id
|
|
101
|
end
|
|
101
|
end
|
|
102
|
@invalid_issue_id = nil
|
|
102
|
@invalid_issue_id = nil
|
|
103
|
else
|
|
103
|
else
|
|
104
|
@invalid_issue_id = issue_id
|
|
104
|
@invalid_issue_id = issue_id
|
|
105
|
end
|
|
105
|
end
|
|
106
|
end
|
|
106
|
end
|
|
107
|
end
|
|
107
|
end
|
|
108
|
attrs
|
|
108
|
attrs
|
|
109
|
end
|
|
109
|
end
|
|
110
|
|
|
110
|
|
|
111
|
def set_project_if_nil
|
|
111
|
def set_project_if_nil
|
|
112
|
self.project = issue.project if issue && project.nil?
|
|
112
|
self.project = issue.project if issue && project.nil?
|
|
113
|
end
|
|
113
|
end
|
|
114
|
|
|
114
|
|
|
115
|
def validate_time_entry
|
|
115
|
def validate_time_entry
|
|
116
|
errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
|
|
116
|
errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
|
|
117
|
errors.add :project_id, :invalid if project.nil?
|
|
117
|
errors.add :project_id, :invalid if project.nil?
|
|
118
|
errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id
|
|
118
|
errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id
|
|
119
|
errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity)
|
|
119
|
errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity)
|
|
120
|
end
|
|
120
|
end
|
|
121
|
|
|
121
|
|
|
122
|
def hours=(h)
|
|
122
|
def hours=(h)
|
|
123
|
write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
|
|
123
|
write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
|
|
124
|
end
|
|
124
|
end
|
|
125
|
|
|
125
|
|
|
126
|
def hours
|
|
126
|
def hours
|
|
127
|
h = read_attribute(:hours)
|
|
127
|
h = read_attribute(:hours)
|
|
128
|
if h.is_a?(Float)
|
|
128
|
if h.is_a?(Float)
|
|
129
|
h.round(2)
|
|
129
|
h.round(2)
|
|
130
|
else
|
|
130
|
else
|
|
131
|
h
|
|
131
|
h
|
|
132
|
end
|
|
132
|
end
|
|
133
|
end
|
|
133
|
end
|
|
134
|
|
|
134
|
|
|
135
|
# tyear, tmonth, tweek assigned where setting spent_on attributes
|
|
135
|
# tyear, tmonth, tweek assigned where setting spent_on attributes
|
|
136
|
# these attributes make time aggregations easier
|
|
136
|
# these attributes make time aggregations easier
|
|
137
|
def spent_on=(date)
|
|
137
|
def spent_on=(date)
|
|
138
|
super
|
|
138
|
super
|
|
139
|
self.tyear = spent_on ? spent_on.year : nil
|
|
139
|
self.tyear = spent_on ? spent_on.year : nil
|
|
140
|
self.tmonth = spent_on ? spent_on.month : nil
|
|
140
|
self.tmonth = spent_on ? spent_on.month : nil
|
|
141
|
self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
|
|
141
|
self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
|
|
142
|
end
|
|
142
|
end
|
|
143
|
|
|
143
|
|
|
144
|
# Returns true if the time entry can be edited by usr, otherwise false
|
|
144
|
# Returns true if the time entry can be edited by usr, otherwise false
|
|
145
|
def editable_by?(usr)
|
|
145
|
def editable_by?(usr)
|
|
146
|
visible?(usr) && (
|
|
146
|
visible?(usr) && (
|
|
147
|
(usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
|
|
147
|
(usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
|
|
148
|
)
|
|
148
|
)
|
|
149
|
end
|
|
149
|
end
|
|
150
|
|
|
150
|
|
|
151
|
# Returns the custom_field_values that can be edited by the given user
|
|
151
|
# Returns the custom_field_values that can be edited by the given user
|
|
152
|
def editable_custom_field_values(user=nil)
|
|
152
|
def editable_custom_field_values(user=nil)
|
|
153
|
visible_custom_field_values
|
|
153
|
visible_custom_field_values
|
|
154
|
end
|
|
154
|
end
|
|
155
|
|
|
155
|
|
|
156
|
# Returns the custom fields that can be edited by the given user
|
|
156
|
# Returns the custom fields that can be edited by the given user
|
|
157
|
def editable_custom_fields(user=nil)
|
|
157
|
def editable_custom_fields(user=nil)
|
|
158
|
editable_custom_field_values(user).map(&:custom_field).uniq
|
|
158
|
editable_custom_field_values(user).map(&:custom_field).uniq
|
|
159
|
end
|
|
159
|
end
|
|
160
|
end
|
|
160
|
end
|