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.
225 lines
7.1 KiB
225 lines
7.1 KiB
11 years ago
|
#-- copyright
|
||
5 years ago
|
# OpenProject is an open source project management software.
|
||
|
# Copyright (C) 2012-2020 the OpenProject GmbH
|
||
11 years ago
|
#
|
||
5 years ago
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License version 3.
|
||
|
#
|
||
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
# Copyright (C) 2006-2017 Jean-Philippe Lang
|
||
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
||
11 years ago
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
5 years ago
|
# as published by the Free Software Foundation; either version 2
|
||
|
# of the License, or (at your option) any later version.
|
||
11 years ago
|
#
|
||
|
# 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.
|
||
5 years ago
|
#
|
||
|
# See docs/COPYRIGHT.rdoc for more details.
|
||
11 years ago
|
#++
|
||
|
|
||
4 years ago
|
class Budget < ApplicationRecord
|
||
|
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
|
||
|
belongs_to :project
|
||
|
has_many :work_packages, dependent: :nullify
|
||
9 years ago
|
has_many :material_budget_items, -> {
|
||
6 years ago
|
includes(:cost_type).order(Arel.sql('material_budget_items.id ASC'))
|
||
4 years ago
|
}, dependent: :destroy
|
||
9 years ago
|
has_many :labor_budget_items, -> {
|
||
6 years ago
|
includes(:user).order(Arel.sql('labor_budget_items.id ASC'))
|
||
4 years ago
|
}, dependent: :destroy
|
||
14 years ago
|
|
||
15 years ago
|
validates_associated :material_budget_items
|
||
|
validates_associated :labor_budget_items
|
||
14 years ago
|
|
||
15 years ago
|
after_update :save_material_budget_items
|
||
|
after_update :save_labor_budget_items
|
||
14 years ago
|
|
||
4 years ago
|
has_many :cost_entries, through: :work_packages
|
||
|
has_many :time_entries, through: :work_packages
|
||
|
|
||
|
include ActiveModel::ForbiddenAttributesProtection
|
||
|
|
||
|
acts_as_attachable
|
||
|
acts_as_journalized
|
||
|
|
||
|
acts_as_event type: 'cost-objects',
|
||
|
title: Proc.new { |o| "#{I18n.t(:label_budget)} ##{o.id}: #{o.subject}" },
|
||
|
url: Proc.new { |o| { controller: 'budgets', action: 'show', id: o.id } }
|
||
|
|
||
|
validates_presence_of :subject, :project, :author, :fixed_date
|
||
|
validates_length_of :subject, maximum: 255
|
||
|
validates_length_of :subject, minimum: 1
|
||
|
|
||
|
User.before_destroy do |user|
|
||
|
Budget.replace_author_with_deleted_user user
|
||
|
end
|
||
|
|
||
|
def self.visible(user)
|
||
|
includes(:project)
|
||
|
.references(:projects)
|
||
|
.merge(Project.allowed_to(user, :view_budgets))
|
||
|
end
|
||
|
|
||
|
def initialize(attributes = nil)
|
||
|
super
|
||
|
self.author = User.current if new_record?
|
||
13 years ago
|
end
|
||
|
|
||
15 years ago
|
def copy_from(arg)
|
||
4 years ago
|
budget = (arg.is_a?(Budget) ? arg : self.class.find(arg))
|
||
|
attrs = budget.attributes.dup
|
||
12 years ago
|
super(attrs)
|
||
4 years ago
|
self.labor_budget_items = budget.labor_budget_items.map(&:dup)
|
||
|
self.material_budget_items = budget.material_budget_items.map(&:dup)
|
||
|
end
|
||
|
|
||
|
def budget
|
||
|
material_budget + labor_budget
|
||
15 years ago
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def type_label
|
||
4 years ago
|
I18n.t(:label_budget)
|
||
|
end
|
||
|
|
||
|
# Amount of the budget spent. Expressed as as a percentage whole number
|
||
|
def budget_ratio
|
||
|
return 0.0 if budget.nil? || budget == 0.0
|
||
|
|
||
|
((spent / budget) * 100).round
|
||
|
end
|
||
|
|
||
|
def css_classes
|
||
|
'budget'
|
||
|
end
|
||
|
|
||
|
def self.replace_author_with_deleted_user(user)
|
||
|
substitute = DeletedUser.first
|
||
|
|
||
|
where(author_id: user.id).update_all(author_id: substitute.id)
|
||
|
end
|
||
|
|
||
|
def to_s
|
||
|
subject
|
||
|
end
|
||
|
|
||
|
def name
|
||
|
subject
|
||
|
end
|
||
|
|
||
|
# override acts_as_journalized method
|
||
|
def activity_type
|
||
|
self.class.plural_name
|
||
15 years ago
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def material_budget
|
||
5 years ago
|
@material_budget ||= material_budget_items.visible_costs.inject(BigDecimal('0.0000')) { |sum, i| sum += i.costs }
|
||
15 years ago
|
end
|
||
|
|
||
|
def labor_budget
|
||
5 years ago
|
@labor_budget ||= labor_budget_items.visible_costs.inject(BigDecimal('0.0000')) { |sum, i| sum += i.costs }
|
||
15 years ago
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def spent
|
||
|
spent_material + spent_labor
|
||
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def spent_material
|
||
15 years ago
|
@spent_material ||= begin
|
||
4 years ago
|
if cost_entries.blank?
|
||
|
BigDecimal('0.0000')
|
||
|
else
|
||
|
cost_entries.visible_costs(User.current, project).sum("CASE
|
||
15 years ago
|
WHEN #{CostEntry.table_name}.overridden_costs IS NULL THEN
|
||
|
#{CostEntry.table_name}.costs
|
||
|
ELSE
|
||
14 years ago
|
#{CostEntry.table_name}.overridden_costs END").to_d
|
||
4 years ago
|
end
|
||
|
end
|
||
15 years ago
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def spent_labor
|
||
15 years ago
|
@spent_labor ||= begin
|
||
4 years ago
|
if time_entries.blank?
|
||
|
BigDecimal('0.0000')
|
||
|
else
|
||
|
time_entries.visible_costs(User.current, project).sum("CASE
|
||
15 years ago
|
WHEN #{TimeEntry.table_name}.overridden_costs IS NULL THEN
|
||
|
#{TimeEntry.table_name}.costs
|
||
|
ELSE
|
||
14 years ago
|
#{TimeEntry.table_name}.overridden_costs END").to_d
|
||
4 years ago
|
end
|
||
|
end
|
||
15 years ago
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def new_material_budget_item_attributes=(material_budget_item_attributes)
|
||
9 years ago
|
material_budget_item_attributes.each do |_index, attributes|
|
||
4 years ago
|
material_budget_items.build(attributes) if attributes[:units].to_i.positive?
|
||
15 years ago
|
end
|
||
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def existing_material_budget_item_attributes=(material_budget_item_attributes)
|
||
|
material_budget_items.reject(&:new_record?).each do |material_budget_item|
|
||
|
attributes = material_budget_item_attributes[material_budget_item.id.to_s]
|
||
14 years ago
|
|
||
4 years ago
|
if User.current.allowed_to? :edit_budgets, material_budget_item.budget.project
|
||
|
if attributes && attributes[:units].to_i.positive?
|
||
|
material_budget_item.attributes = attributes.merge(amount: Rate.parse_number_string(attributes[:amount]))
|
||
13 years ago
|
else
|
||
|
material_budget_items.delete(material_budget_item)
|
||
|
end
|
||
15 years ago
|
end
|
||
|
end
|
||
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def save_material_budget_items
|
||
|
material_budget_items.each do |material_budget_item|
|
||
9 years ago
|
material_budget_item.save(validate: false)
|
||
15 years ago
|
end
|
||
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def new_labor_budget_item_attributes=(labor_budget_item_attributes)
|
||
9 years ago
|
labor_budget_item_attributes.each do |_index, attributes|
|
||
4 years ago
|
if valid_labor_budget_attributes?(attributes)
|
||
13 years ago
|
|
||
|
item = labor_budget_items.build(attributes)
|
||
4 years ago
|
item.budget = self # to please the labor_budget_item validation
|
||
13 years ago
|
end
|
||
15 years ago
|
end
|
||
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def existing_labor_budget_item_attributes=(labor_budget_item_attributes)
|
||
|
labor_budget_items.reject(&:new_record?).each do |labor_budget_item|
|
||
|
attributes = labor_budget_item_attributes[labor_budget_item.id.to_s]
|
||
4 years ago
|
if User.current.allowed_to? :edit_budgets, labor_budget_item.budget.project
|
||
|
if valid_labor_budget_attributes?(attributes)
|
||
|
labor_budget_item.attributes = attributes.merge(amount: Rate.parse_number_string(attributes[:amount]))
|
||
13 years ago
|
else
|
||
|
labor_budget_items.delete(labor_budget_item)
|
||
|
end
|
||
15 years ago
|
end
|
||
|
end
|
||
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def save_labor_budget_items
|
||
|
labor_budget_items.each do |labor_budget_item|
|
||
9 years ago
|
labor_budget_item.save(validate: false)
|
||
15 years ago
|
end
|
||
|
end
|
||
4 years ago
|
|
||
|
def valid_labor_budget_attributes?(attributes)
|
||
|
attributes &&
|
||
|
attributes[:hours].to_i.positive? &&
|
||
|
attributes[:user_id].to_i.positive? &&
|
||
|
project.possible_assignees.map(&:id).include?(attributes[:user_id].to_i)
|
||
|
end
|
||
13 years ago
|
end
|