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.
151 lines
4.0 KiB
151 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.
|
||
|
#++
|
||
|
|
||
15 years ago
|
# A CostObject is an item that is created as part of the project. These items
|
||
12 years ago
|
# contain a collection of work packages.
|
||
15 years ago
|
class CostObject < ActiveRecord::Base
|
||
9 years ago
|
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
|
||
15 years ago
|
belongs_to :project
|
||
9 years ago
|
has_many :work_packages, dependent: :nullify
|
||
13 years ago
|
|
||
9 years ago
|
has_many :cost_entries, through: :work_packages
|
||
|
has_many :time_entries, through: :work_packages
|
||
13 years ago
|
|
||
12 years ago
|
include ActiveModel::ForbiddenAttributesProtection
|
||
13 years ago
|
|
||
9 years ago
|
acts_as_attachable
|
||
11 years ago
|
acts_as_journalized
|
||
|
|
||
|
acts_as_event type: 'cost-objects',
|
||
9 years ago
|
title: Proc.new { |o| "#{I18n.t(:label_cost_object)} ##{o.id}: #{o.subject}" },
|
||
9 years ago
|
url: Proc.new { |o| { controller: 'cost_objects', action: 'show', id: o.id } }
|
||
13 years ago
|
|
||
11 years ago
|
validates_presence_of :subject, :project, :author, :kind, :fixed_date
|
||
9 years ago
|
validates_length_of :subject, maximum: 255
|
||
|
validates_length_of :subject, minimum: 1
|
||
13 years ago
|
|
||
13 years ago
|
User.before_destroy do |user|
|
||
12 years ago
|
CostObject.replace_author_with_deleted_user user
|
||
13 years ago
|
end
|
||
13 years ago
|
|
||
8 years ago
|
def self.visible(user)
|
||
|
includes(:project)
|
||
|
.references(:projects)
|
||
|
.merge(Project.allowed_to(user, :view_cost_objects))
|
||
|
end
|
||
|
|
||
13 years ago
|
def initialize(attributes = nil)
|
||
|
super
|
||
|
self.author = User.current if self.new_record?
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def copy_from(arg)
|
||
12 years ago
|
if !arg.is_a?(Hash)
|
||
9 years ago
|
# turn args into an attributes hash if it is not already (which is the case when called from VariableCostObject)
|
||
12 years ago
|
arg = (arg.is_a?(CostObject) ? arg : self.class.find(arg)).attributes.dup
|
||
|
end
|
||
9 years ago
|
arg.delete('id')
|
||
|
self.type = arg.delete('type')
|
||
12 years ago
|
self.attributes = arg
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
# Wrap type column to make it usable in views (especially in a select tag)
|
||
|
def kind
|
||
|
self[:type]
|
||
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def kind=(type)
|
||
|
self[:type] = type
|
||
|
end
|
||
13 years ago
|
|
||
11 years ago
|
# Assign all the work_packages with +version_id+ to this Cost Object
|
||
|
def assign_work_packages_by_version(version_id)
|
||
15 years ago
|
version = Version.find_by_id(version_id)
|
||
11 years ago
|
return 0 if version.nil? || version.fixed_work_packages.blank?
|
||
13 years ago
|
|
||
11 years ago
|
version.fixed_work_packages.each do |work_package|
|
||
9 years ago
|
work_package.update_attribute(:cost_object_id, id)
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
9 years ago
|
version.fixed_work_packages.size
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
# Change the Cost Object type to another type. Valid types are
|
||
|
#
|
||
|
# * FixedCostObject
|
||
|
# * VariableCostObject
|
||
|
def change_type(to)
|
||
|
if [FixedCostObject.name, VariableCostObject.name].include?(to)
|
||
|
self.type = to
|
||
|
self.save!
|
||
6 years ago
|
CostObject.find(id)
|
||
15 years ago
|
else
|
||
6 years ago
|
self
|
||
15 years ago
|
end
|
||
|
end
|
||
13 years ago
|
|
||
15 years ago
|
# Amount spent. Virtual accessor that is overriden by subclasses.
|
||
|
def spent
|
||
13 years ago
|
0
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
# Budget of labor. Virtual accessor that is overriden by subclasses.
|
||
|
def labor_budget
|
||
|
0.0
|
||
|
end
|
||
13 years ago
|
|
||
15 years ago
|
# Budget of material, i.e. all costs besides labor costs. Virtual accessor that is overriden by subclasses.
|
||
|
def material_budget
|
||
|
0.0
|
||
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def budget
|
||
|
material_budget + labor_budget
|
||
|
end
|
||
13 years ago
|
|
||
15 years ago
|
# Label of the current type for display in GUI. Virtual accessor that is overriden by subclasses.
|
||
|
def type_label
|
||
9 years ago
|
I18n.t(:label_cost_object)
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
# Amount of the budget spent. Expressed as as a percentage whole number
|
||
|
def budget_ratio
|
||
9 years ago
|
return 0.0 if budget.nil? || budget == 0.0
|
||
|
((spent / budget) * 100).round
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
15 years ago
|
def css_classes
|
||
9 years ago
|
'cost_object'
|
||
15 years ago
|
end
|
||
13 years ago
|
|
||
13 years ago
|
def self.replace_author_with_deleted_user(user)
|
||
|
substitute = DeletedUser.first
|
||
|
|
||
9 years ago
|
where(author_id: user.id).update_all(author_id: substitute.id)
|
||
13 years ago
|
end
|
||
11 years ago
|
|
||
|
def to_s
|
||
|
subject
|
||
|
end
|
||
8 years ago
|
|
||
|
def name
|
||
|
subject
|
||
|
end
|
||
13 years ago
|
end
|