Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Budget services

Oliver Günther197afad075a8
diff --git a/modules/budgets/app/contracts/budgets/base_contract.rb b/modules/budgets/app/contracts/budgets/base_contract.rb
new file mode 100644
index 00000000000..74f77163609
--- /dev/null
+++ b/modules/budgets/app/contracts/budgets/base_contract.rb
@@ -0,0 +1,66 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 Jean-Philippe Lang
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Budgets
+ class BaseContract < ::ModelContract
+ include Attachments::ValidateReplacements
+
+ def self.model
+ Budget
+ end
+
+ attribute :subject
+ attribute :description
+ attribute :fixed_date
+ attribute :project
+ attribute :author
+ attribute :new_material_budget_item_attributes,
+ readable: false
+
+ attribute :new_labor_budget_item_attributes,
+ readable: false
+
+ attribute :existing_material_budget_item_attributes,
+ readable: false
+
+ attribute :existing_labor_budget_item_attributes,
+ readable: false
+
+ validate :validate_manage_allowed
+
+ private
+
+ def validate_manage_allowed
+ unless user.allowed_to?(:edit_budgets, model.project)
+ errors.add :base, :error_unauthorized
+ end
+ end
+ end
+end
diff --git a/modules/budgets/app/contracts/budgets/create_contract.rb b/modules/budgets/app/contracts/budgets/create_contract.rb
new file mode 100644
index 00000000000..8708e9c74ca
--- /dev/null
+++ b/modules/budgets/app/contracts/budgets/create_contract.rb
@@ -0,0 +1,34 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 Jean-Philippe Lang
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Budgets
+ class CreateContract < BaseContract
+ end
+end
diff --git a/modules/budgets/app/contracts/budgets/update_contract.rb b/modules/budgets/app/contracts/budgets/update_contract.rb
new file mode 100644
index 00000000000..44a3337b219
--- /dev/null
+++ b/modules/budgets/app/contracts/budgets/update_contract.rb
@@ -0,0 +1,34 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 Jean-Philippe Lang
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Budgets
+ class UpdateContract < BaseContract
+ end
+end
diff --git a/modules/budgets/app/controllers/budgets_controller.rb b/modules/budgets/app/controllers/budgets_controller.rb
index bb26ce92206..7127ba4ced4 100644
--- a/modules/budgets/app/controllers/budgets_controller.rb
+++ b/modules/budgets/app/controllers/budgets_controller.rb
@@ -27,6 +27,8 @@
#++
class BudgetsController < ApplicationController
+ include AttachableServiceCall
+
before_action :find_budget, only: %i[show edit update copy]
before_action :find_budgets, only: :destroy
before_action :find_project, only: %i[new create update_material_budget_item update_labor_budget_item]
@@ -85,11 +87,12 @@ class BudgetsController < ApplicationController
def copy
source = Budget.find(params[:id].to_i)
- @budget = if source
- Budget.new_copy(source)
- else
- Budget.new
- end
+ @budget =
+ if source
+ Budget.new_copy(source)
+ else
+ Budget.new
+ end
@budget.fixed_date ||= Date.today
@@ -97,20 +100,11 @@ class BudgetsController < ApplicationController
end
def create
- @budget = Budget.new
- @budget.project_id = @project.id
-
- # fixed_date must be set before material_budget_items and labor_budget_items
- @budget.fixed_date = if params[:budget] && params[:budget][:fixed_date]
- params[:budget].delete(:fixed_date)
- else
- Date.today
- end
-
- @budget.attributes = permitted_params.budget
- @budget.attach_files(permitted_params.attachments.to_h)
+ call = attachable_create_call ::Budgets::CreateService,
+ args: permitted_params.budget.merge(project: @project)
+ @budget = call.result
- if @budget.save
+ if call.success?
flash[:notice] = t(:notice_successful_create)
redirect_to(params[:continue] ? { action: 'new' } : { action: 'show', id: @budget })
else
@@ -123,20 +117,15 @@ class BudgetsController < ApplicationController
end
def update
- @budget.attributes = permitted_params.budget if params[:budget]
- if params[:budget][:existing_material_budget_item_attributes].nil?
- @budget.existing_material_budget_item_attributes = ({})
- end
- if params[:budget][:existing_labor_budget_item_attributes].nil?
- @budget.existing_labor_budget_item_attributes = ({})
- end
-
- @budget.attach_files(permitted_params.attachments.to_h)
+ call = attachable_update_call ::Budgets::UpdateService,
+ model: @budget,
+ args: permitted_params.budget
- if @budget.save
+ if call.success?
flash[:notice] = t(:notice_successful_update)
redirect_to(params[:back_to] || { action: 'show', id: @budget })
else
+ @budget = call.result
render action: 'edit'
end
rescue ActiveRecord::StaleObjectError
diff --git a/modules/budgets/app/services/budgets/create_service.rb b/modules/budgets/app/services/budgets/create_service.rb
new file mode 100644
index 00000000000..2125f3fc672
--- /dev/null
+++ b/modules/budgets/app/services/budgets/create_service.rb
@@ -0,0 +1,33 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 Jean-Philippe Lang
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Budgets
+ class CreateService < ::BaseServices::Create
+ include Attachments::ReplaceAttachments
+ end
+end
diff --git a/modules/budgets/app/services/budgets/set_attributes_service.rb b/modules/budgets/app/services/budgets/set_attributes_service.rb
new file mode 100644
index 00000000000..71481e2dd65
--- /dev/null
+++ b/modules/budgets/app/services/budgets/set_attributes_service.rb
@@ -0,0 +1,65 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 Jean-Philippe Lang
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Budgets
+ class SetAttributesService < ::BaseServices::SetAttributes
+ include Attachments::SetReplacements
+
+ private
+
+ def set_attributes(params)
+ set_fixed_date(params)
+ unset_items(params)
+
+ super
+ end
+
+ def set_default_attributes(_params)
+ model.change_by_system do
+ model.author = user
+ end
+ end
+
+ # fixed_date must be set before material_budget_items and labor_budget_items
+ def set_fixed_date(params)
+ model.fixed_date = params.delete(:fixed_date) || Date.today
+ end
+
+ def unset_items(params)
+ if params[:existing_material_budget_item_attributes].nil?
+ model.existing_material_budget_item_attributes = ({})
+ end
+
+ if params[:existing_labor_budget_item_attributes].nil?
+ model.existing_labor_budget_item_attributes = ({})
+ end
+ end
+ end
+end
diff --git a/modules/budgets/app/services/budgets/update_service.rb b/modules/budgets/app/services/budgets/update_service.rb
new file mode 100644
index 00000000000..dfa73c33a9d
--- /dev/null
+++ b/modules/budgets/app/services/budgets/update_service.rb
@@ -0,0 +1,35 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 Jean-Philippe Lang
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Budgets
+ class UpdateService < ::BaseServices::Update
+ include Attachments::ReplaceAttachments
+ end
+end
diff --git a/modules/budgets/spec/services/budgets/create_service_spec.rb b/modules/budgets/spec/services/budgets/create_service_spec.rb
new file mode 100644
index 00000000000..4f4ba1949ea
--- /dev/null
+++ b/modules/budgets/spec/services/budgets/create_service_spec.rb
@@ -0,0 +1,36 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 Jean-Philippe Lang
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+require 'services/base_services/behaves_like_create_service'
+
+describe Budgets::CreateService, type: :model do
+ it_behaves_like 'BaseServices create service'
+end
diff --git a/modules/budgets/spec/services/budgets/update_service_spec.rb b/modules/budgets/spec/services/budgets/update_service_spec.rb
new file mode 100644
index 00000000000..42051f91157
--- /dev/null
+++ b/modules/budgets/spec/services/budgets/update_service_spec.rb
@@ -0,0 +1,36 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 Jean-Philippe Lang
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+require 'services/base_services/behaves_like_update_service'
+
+describe Budgets::UpdateService, type: :model do
+ it_behaves_like 'BaseServices update service'
+end