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.
141 lines
4.0 KiB
141 lines
4.0 KiB
11 years ago
|
#-- copyright
|
||
|
# OpenProject Costs Plugin
|
||
|
#
|
||
|
# Copyright (C) 2009 - 2014 the OpenProject Foundation (OPF)
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
|
# version 3.
|
||
|
#
|
||
|
# 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.
|
||
|
#++
|
||
|
|
||
16 years ago
|
class CostEntry < ActiveRecord::Base
|
||
|
belongs_to :project
|
||
12 years ago
|
belongs_to :work_package
|
||
16 years ago
|
belongs_to :user
|
||
12 years ago
|
include ::OpenProject::Costs::DeletedUserFallback
|
||
16 years ago
|
belongs_to :cost_type
|
||
15 years ago
|
belongs_to :cost_object
|
||
9 years ago
|
belongs_to :rate, class_name: 'CostRate'
|
||
13 years ago
|
|
||
12 years ago
|
include ActiveModel::ForbiddenAttributesProtection
|
||
13 years ago
|
|
||
12 years ago
|
validates_presence_of :work_package_id, :project_id, :user_id, :cost_type_id, :units, :spent_on
|
||
9 years ago
|
validates_numericality_of :units, allow_nil: false, message: :invalid
|
||
|
validates_length_of :comments, maximum: 255, allow_nil: true
|
||
13 years ago
|
|
||
12 years ago
|
before_save :before_save
|
||
|
before_validation :before_validation
|
||
|
after_initialize :after_initialize
|
||
|
validate :validate
|
||
|
|
||
8 years ago
|
scope :on_work_packages, ->(work_packages) { where(work_package_id: work_packages) }
|
||
|
|
||
8 years ago
|
extend CostEntryScopes
|
||
9 years ago
|
|
||
16 years ago
|
def after_initialize
|
||
9 years ago
|
if new_record? && cost_type.nil?
|
||
16 years ago
|
if default_cost_type = CostType.default
|
||
|
self.cost_type_id = default_cost_type.id
|
||
|
end
|
||
|
end
|
||
|
end
|
||
13 years ago
|
|
||
16 years ago
|
def before_validation
|
||
12 years ago
|
self.project = work_package.project if work_package && project.nil?
|
||
16 years ago
|
end
|
||
13 years ago
|
|
||
16 years ago
|
def validate
|
||
12 years ago
|
errors.add :units, :invalid if units && (units < 0)
|
||
|
errors.add :project_id, :invalid if project.nil?
|
||
12 years ago
|
errors.add :work_package_id, :invalid if work_package.nil? || (project != work_package.project)
|
||
12 years ago
|
errors.add :cost_type_id, :invalid if cost_type.present? && cost_type.deleted_at.present?
|
||
|
errors.add :user_id, :invalid if project.present? && !project.users.include?(user) && user_id_changed?
|
||
14 years ago
|
|
||
15 years ago
|
begin
|
||
|
spent_on.to_date
|
||
|
rescue Exception
|
||
12 years ago
|
errors.add :spent_on, :invalid
|
||
15 years ago
|
end
|
||
16 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def before_save
|
||
15 years ago
|
self.spent_on &&= spent_on.to_date
|
||
15 years ago
|
update_costs
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def overwritten_costs=(costs)
|
||
|
write_attribute(:overwritten_costs, CostRate.clean_currency(costs))
|
||
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def units=(units)
|
||
|
write_attribute(:units, CostRate.clean_currency(units))
|
||
|
end
|
||
13 years ago
|
|
||
15 years ago
|
# tyear, tmonth, tweek assigned where setting spent_on attributes
|
||
|
# these attributes make time aggregations easier
|
||
15 years ago
|
def spent_on=(date)
|
||
|
super
|
||
|
self.tyear = spent_on ? spent_on.year : nil
|
||
|
self.tmonth = spent_on ? spent_on.month : nil
|
||
|
self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
|
||
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def real_costs
|
||
|
# This methods returns the actual assigned costs of the entry
|
||
9 years ago
|
overridden_costs || costs || calculated_costs
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def calculated_costs(rate_attr = nil)
|
||
|
rate_attr ||= current_rate
|
||
|
units * rate_attr.rate
|
||
15 years ago
|
rescue
|
||
15 years ago
|
0.0
|
||
16 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def update_costs(rate_attr = nil)
|
||
|
rate_attr ||= current_rate
|
||
15 years ago
|
if rate_attr.nil?
|
||
15 years ago
|
self.costs = 0.0
|
||
15 years ago
|
self.rate = nil
|
||
|
return
|
||
|
end
|
||
|
|
||
9 years ago
|
self.costs = calculated_costs(rate_attr)
|
||
15 years ago
|
self.rate = rate_attr
|
||
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def update_costs!(rate_attr = nil)
|
||
9 years ago
|
update_costs(rate_attr)
|
||
15 years ago
|
self.save!
|
||
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def current_rate
|
||
9 years ago
|
cost_type.rate_at(self.spent_on)
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
# Returns true if the cost entry can be edited by usr, otherwise false
|
||
16 years ago
|
def editable_by?(usr)
|
||
11 years ago
|
usr.allowed_to?(:edit_cost_entries, project) ||
|
||
|
(usr.allowed_to?(:edit_own_cost_entries, project) && user_id == usr.id)
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
12 years ago
|
def creatable_by?(usr)
|
||
11 years ago
|
usr.allowed_to?(:log_costs, project) ||
|
||
|
(usr.allowed_to?(:log_own_costs, project) && user_id == usr.id)
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def costs_visible_by?(usr)
|
||
12 years ago
|
usr.allowed_to?(:view_cost_rates, project) ||
|
||
|
(usr.id == user_id && !overridden_costs.nil?)
|
||
16 years ago
|
end
|
||
13 years ago
|
end
|