kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.5 KiB
105 lines
3.5 KiB
13 years ago
|
#-- encoding: UTF-8
|
||
4 years ago
|
|
||
14 years ago
|
#-- copyright
|
||
5 years ago
|
# OpenProject is an open source project management software.
|
||
|
# Copyright (C) 2012-2020 the OpenProject GmbH
|
||
14 years ago
|
#
|
||
14 years ago
|
# This program is free software; you can redistribute it and/or
|
||
12 years ago
|
# modify it under the terms of the GNU General Public License version 3.
|
||
14 years ago
|
#
|
||
11 years ago
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
8 years ago
|
# Copyright (C) 2006-2017 Jean-Philippe Lang
|
||
11 years ago
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
||
|
#
|
||
|
# 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.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
#
|
||
7 years ago
|
# See docs/COPYRIGHT.rdoc for more details.
|
||
14 years ago
|
#++
|
||
|
|
||
5 years ago
|
class TimeEntry < ApplicationRecord
|
||
18 years ago
|
# could have used polymorphic association
|
||
|
# project association here allows easy loading of time entries at project level with one database trip
|
||
|
belongs_to :project
|
||
12 years ago
|
belongs_to :work_package
|
||
18 years ago
|
belongs_to :user
|
||
10 years ago
|
belongs_to :activity, class_name: 'TimeEntryActivity', foreign_key: 'activity_id'
|
||
4 years ago
|
belongs_to :rate, -> { where(type: %w[HourlyRate DefaultHourlyRate]) }, class_name: 'Rate'
|
||
14 years ago
|
|
||
17 years ago
|
acts_as_customizable
|
||
11 years ago
|
|
||
11 years ago
|
acts_as_journalized
|
||
15 years ago
|
|
||
18 years ago
|
validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
|
||
10 years ago
|
validates_numericality_of :hours, allow_nil: true, message: :invalid
|
||
|
validates_length_of :comments, maximum: 255, allow_nil: true
|
||
14 years ago
|
|
||
7 years ago
|
scope :on_work_packages, ->(work_packages) { where(work_package_id: work_packages) }
|
||
|
|
||
4 years ago
|
include ::Scopes::Scoped
|
||
|
extend ::TimeEntry::TimeEntryScopes
|
||
|
include Entry::Costs
|
||
|
include Entry::SplashedDates
|
||
12 years ago
|
|
||
4 years ago
|
scope_classes TimeEntry::Scopes::OfUserAndDay,
|
||
|
TimeEntry::Scopes::Visible
|
||
|
|
||
|
# TODO: move into service
|
||
|
before_save :update_costs
|
||
14 years ago
|
|
||
4 years ago
|
def self.update_all(updates, conditions = nil, options = {})
|
||
|
# instead of a update_all, perform an individual update during work_package#move
|
||
|
# to trigger the update of the costs based on new rates
|
||
|
if conditions.respond_to?(:keys) && conditions.keys == [:work_package_id] && updates =~ /^project_id = ([\d]+)$/
|
||
|
project_id = $1
|
||
|
time_entries = TimeEntry.where(conditions)
|
||
|
time_entries.each do |entry|
|
||
|
entry.project_id = project_id
|
||
|
entry.save!
|
||
|
end
|
||
|
else
|
||
|
super
|
||
14 years ago
|
end
|
||
4 years ago
|
end
|
||
|
|
||
|
def hours=(value)
|
||
|
write_attribute :hours, (value.is_a?(String) ? (value.to_hours || value) : value)
|
||
17 years ago
|
end
|
||
14 years ago
|
|
||
17 years ago
|
# Returns true if the time entry can be edited by usr, otherwise false
|
||
|
def editable_by?(usr)
|
||
17 years ago
|
(usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
|
||
17 years ago
|
end
|
||
14 years ago
|
|
||
4 years ago
|
def current_rate
|
||
|
user.rate_at(spent_on, project_id)
|
||
|
end
|
||
|
|
||
|
def visible_by?(usr)
|
||
|
usr.allowed_to?(:view_time_entries, project) ||
|
||
|
(user_id == usr.id && usr.allowed_to?(:view_own_time_entries, project))
|
||
14 years ago
|
end
|
||
|
|
||
4 years ago
|
def costs_visible_by?(usr)
|
||
|
usr.allowed_to?(:view_hourly_rates, project) ||
|
||
|
(user_id == usr.id && usr.allowed_to?(:view_own_hourly_rate, project))
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def cost_attribute
|
||
|
hours
|
||
14 years ago
|
end
|
||
18 years ago
|
end
|