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

also make cost entry assigend to an entity

Klaus Zanders8fe462a3dea5
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index f4d2c99c043..57dd294c6d4 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -59,7 +59,7 @@ class WorkPackage < ApplicationRecord
belongs_to :priority, class_name: "IssuePriority"
belongs_to :category, class_name: "Category", optional: true
- has_many :time_entries, dependent: :delete_all
+ has_many :time_entries, dependent: :delete_all, inverse_of: :entity
has_many :file_links, dependent: :delete_all, class_name: "Storages::FileLink", as: :container
diff --git a/modules/costs/app/models/cost_entry.rb b/modules/costs/app/models/cost_entry.rb
index a7ab871086b..4cb9fa21d98 100644
--- a/modules/costs/app/models/cost_entry.rb
+++ b/modules/costs/app/models/cost_entry.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -27,8 +29,12 @@
#++
class CostEntry < ApplicationRecord
+ ALLOWED_ENTITY_TYPES = %w[WorkPackage].freeze
+
+ self.ignored_columns += %w[work_package_id]
+
belongs_to :project
- belongs_to :work_package
+ belongs_to :entity, polymorphic: true
belongs_to :user
belongs_to :logged_by, class_name: "User"
include ::Costs::DeletedUserFallback
@@ -38,7 +44,7 @@ class CostEntry < ApplicationRecord
include ActiveModel::ForbiddenAttributesProtection
- validates_presence_of :work_package_id, :project_id, :user_id, :logged_by_id, :cost_type_id, :units, :spent_on
+ validates_presence_of :entity, :project_id, :user_id, :logged_by_id, :cost_type_id, :units, :spent_on
validates_numericality_of :units, allow_nil: false, message: :invalid
validates_length_of :comments, maximum: 255, allow_nil: true
@@ -47,7 +53,10 @@ class CostEntry < ApplicationRecord
after_initialize :after_initialize
validate :validate
- scope :on_work_packages, ->(work_packages) { where(work_package_id: work_packages) }
+ validates :entity_type,
+ inclusion: { in: ALLOWED_ENTITY_TYPES }
+
+ scope :on_work_packages, ->(work_packages) { where(entity: work_packages) }
extend CostEntryScopes
include Entry::Costs
@@ -73,7 +82,7 @@ class CostEntry < ApplicationRecord
errors.add :project_id, :invalid if project.nil?
errors.add :work_package_id, :invalid if work_package.nil? || (project != work_package.project)
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?
+ errors.add :user_id, :invalid if project.present? && project.users.exclude?(user) && user_id_changed?
begin
spent_on.to_date
@@ -87,6 +96,42 @@ class CostEntry < ApplicationRecord
update_costs
end
+ def work_package
+ OpenProject::Deprecation.replaced(:work_package, :entity, caller_locations)
+
+ if entity_type == "WorkPackage"
+ entity
+ end
+ end
+
+ def work_package_id
+ OpenProject::Deprecation.replaced(:work_package_id, :entity_id, caller_locations)
+
+ if entity_type == "WorkPackage"
+ entity_id
+ end
+ end
+
+ def work_package=(value)
+ OpenProject::Deprecation.replaced(:work_package=, :entity=, caller_locations)
+ self.entity = value
+ end
+
+ def work_package_id=(value)
+ OpenProject::Deprecation.replaced(:work_package_id=, :entity_id=, caller_locations)
+
+ self.entity_type = "WorkPackage"
+ self.entity_id = value
+ end
+
+ def entity=(value)
+ if value.is_a?(String) && value.starts_with?("gid://")
+ super(GlobalID::Locator.locate(value, only: ALLOWED_ENTITY_TYPES.map(&:constantize)))
+ else
+ super
+ end
+ end
+
def overwritten_costs=(costs)
write_attribute(:overwritten_costs, CostRate.parse_number_string_to_number(costs))
end
diff --git a/modules/costs/app/models/time_entry.rb b/modules/costs/app/models/time_entry.rb
index e5de0f3c379..69d02b3b6e9 100644
--- a/modules/costs/app/models/time_entry.rb
+++ b/modules/costs/app/models/time_entry.rb
@@ -31,6 +31,8 @@
class TimeEntry < ApplicationRecord
ALLOWED_ENTITY_TYPES = %w[WorkPackage Meeting].freeze
+ self.ignored_columns += %w[work_package_id]
+
# could have used polymorphic association
# project association here allows easy loading of time entries at project level with one database trip
belongs_to :project
@@ -123,11 +125,26 @@ class TimeEntry < ApplicationRecord
end
end
+ def work_package_id
+ OpenProject::Deprecation.replaced(:work_package_id, :entity_id, caller_locations)
+
+ if entity_type == "WorkPackage"
+ entity_id
+ end
+ end
+
def work_package=(value)
OpenProject::Deprecation.replaced(:work_package=, :entity=, caller_locations)
self.entity = value
end
+ def work_package_id=(value)
+ OpenProject::Deprecation.replaced(:work_package_id=, :entity_id=, caller_locations)
+
+ self.entity_type = "WorkPackage"
+ self.entity_id = value
+ end
+
def entity=(value)
if value.is_a?(String) && value.starts_with?("gid://")
super(GlobalID::Locator.locate(value, only: ALLOWED_ENTITY_TYPES.map(&:constantize)))
diff --git a/modules/costs/app/models/work_package/abstract_costs.rb b/modules/costs/app/models/work_package/abstract_costs.rb
index 7fb6fecf791..f76168ca7d8 100644
--- a/modules/costs/app/models/work_package/abstract_costs.rb
+++ b/modules/costs/app/models/work_package/abstract_costs.rb
@@ -34,9 +34,11 @@ class WorkPackage
# where(work_package: work_packages)
# However, AR (Rails 4.2) will not expand :includes + :references inside a subquery,
# which will render the query invalid. Therefore we manually extract the IDs in a separate (pluck) query.
- wp_ids = work_package_ids work_packages
+ wp_ids = work_package_ids(work_packages)
- filter_authorized(costs_model.where(work_package_id: wp_ids).joins(work_package: :project))
+ scope = costs_model.where(entity_type: "WorkPackage", entity_id: wp_ids).joins(:project)
+
+ filter_authorized(scope)
.sum(costs_value)
.to_f
end
@@ -135,7 +137,7 @@ class WorkPackage
authorization_scope = filter_authorized costs_model.all
authorization_where = authorization_scope.arel.ast.cores.last.wheres.last
- ce_table[:work_package_id].eq(wp_table_descendants[:id]).and(authorization_where)
+ ce_table[:entity_type].eq("WorkPackage").and(ce_table[:entity_id].eq(wp_table_descendants[:id]).and(authorization_where))
end
def projects_table
diff --git a/modules/costs/config/locales/en.yml b/modules/costs/config/locales/en.yml
index 3661f8e2536..0861ba1bf57 100644
--- a/modules/costs/config/locales/en.yml
+++ b/modules/costs/config/locales/en.yml
@@ -40,6 +40,7 @@ en:
spent: "Spent"
spent_on: "Date"
logged_by: "Logged by"
+ entity: Entity
cost_type:
unit: "Unit name"
unit_plural: "Pluralized unit name"
diff --git a/modules/costs/db/migrate/20250416112143_add_entity_to_cost_entry.rb b/modules/costs/db/migrate/20250416112143_add_entity_to_cost_entry.rb
new file mode 100644
index 00000000000..2be947243a0
--- /dev/null
+++ b/modules/costs/db/migrate/20250416112143_add_entity_to_cost_entry.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class AddEntityToCostEntry < ActiveRecord::Migration[8.0]
+ def up
+ add_reference :cost_entries, :entity, polymorphic: true, index: true
+ change_column_null :cost_entries, :work_package_id, true
+
+ execute <<~SQL.squish
+ UPDATE cost_entries
+ SET
+ entity_type = 'WorkPackage',
+ entity_id = work_package_id
+ WHERE
+ work_package_id IS NOT NULL;
+ SQL
+ end
+end
diff --git a/modules/costs/spec/factories/cost_entry_factory.rb b/modules/costs/spec/factories/cost_entry_factory.rb
index 4589330b983..7620571be93 100644
--- a/modules/costs/spec/factories/cost_entry_factory.rb
+++ b/modules/costs/spec/factories/cost_entry_factory.rb
@@ -30,14 +30,14 @@ FactoryBot.define do
factory :cost_entry do
project
user
- work_package
+ entity factory: :work_package
cost_type
spent_on { Date.today }
units { 1 }
comments { "" }
before(:create) do |ce|
- ce.work_package.project = ce.project
+ ce.project = ce.entity.project
unless ce.project.users.include?(ce.user)
Members::CreateService