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

Perform Cost reports XLS export in background

Oliver Günther8b33f119b42f
diff --git a/modules/xls_export/app/models/xls_export/cost_reports/export.rb b/modules/xls_export/app/models/xls_export/cost_reports/export.rb
new file mode 100644
index 00000000000..6986541d538
--- /dev/null
+++ b/modules/xls_export/app/models/xls_export/cost_reports/export.rb
@@ -0,0 +1,14 @@
+module XlsExport
+ module CostReports
+ class Export < ::Export
+ acts_as_attachable view_permission: :export_work_packages,
+ add_permission: :export_work_packages,
+ delete_permission: :export_work_packages,
+ only_user_allowed: true
+
+ def ready?
+ attachments.any?
+ end
+ end
+ end
+end
diff --git a/modules/xls_export/app/services/xls_export/cost_reports/schedule_export_service.rb b/modules/xls_export/app/services/xls_export/cost_reports/schedule_export_service.rb
new file mode 100644
index 00000000000..e12e2bf5ddb
--- /dev/null
+++ b/modules/xls_export/app/services/xls_export/cost_reports/schedule_export_service.rb
@@ -0,0 +1,57 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module XlsExport
+ module CostReports
+ class ScheduleExportService
+ attr_accessor :user
+
+ def initialize(user:)
+ self.user = user
+ end
+
+ def call(filter_params:, project:, cost_types:)
+ export_storage = XlsExport::CostReports::Export.create
+ job = schedule_export(export_storage, filter_params, project, cost_types)
+
+ ServiceResult.success result: job.job_id
+ end
+
+ private
+
+ def schedule_export(export_storage, filter_params, project, cost_types)
+ XlsExport::CostReports::ExportJob.perform_later(export: export_storage,
+ user:,
+ mime_type: :xls,
+ query: filter_params,
+ project:,
+ cost_types:)
+ end
+ end
+ end
+end
diff --git a/modules/xls_export/app/workers/xls_export/cost_reports/export_job.rb b/modules/xls_export/app/workers/xls_export/cost_reports/export_job.rb
new file mode 100644
index 00000000000..9512d1a4430
--- /dev/null
+++ b/modules/xls_export/app/workers/xls_export/cost_reports/export_job.rb
@@ -0,0 +1,62 @@
+require 'active_storage/filename'
+
+module XlsExport
+ module CostReports
+ class ExportJob < ::Exports::ExportJob
+ self.model = ::CostQuery
+
+ def title
+ I18n.t('export.cost_reports.title')
+ end
+
+ def project
+ options[:project]
+ end
+
+ def cost_types
+ options[:cost_types]
+ end
+
+ private
+
+ def prepare!
+ self.query = build_query(query)
+ end
+
+ def export!
+ # Build an xls file from a cost report.
+ # We only support extracting a simple xls table, so grouping is ignored.
+ handle_export_result(export, xls_report_result)
+ end
+
+ def xls_report_result
+ params = { query:, project:, cost_types: }
+ content = ::OpenProject::XlsExport::XlsViews::CostEntryTable.generate(params).xls
+ time = Time.now.strftime('%d-%m-%Y-T-%H-%M-%S')
+ export_title = "cost-report-#{time}.xls"
+
+ ::Exports::Result.new(format: :xls,
+ title: export_title,
+ mime_type: 'application/vnd.ms-excel',
+ content:)
+ end
+
+ def build_query(filters, groups = {})
+ query = CostQuery.new(project:)
+ query.tap do |q|
+ filters[:operators].each do |filter, operator|
+ unless filters[:values][filter] == ['<<inactive>>']
+ values = Array(filters[:values][filter]).map { |v| v == '<<null>>' ? nil : v }
+ q.filter(filter.to_sym,
+ operator:,
+ values:)
+ end
+ end
+ end
+ groups[:columns].try(:reverse_each) { |c| query.column(c) }
+ groups[:rows].try(:reverse_each) { |r| query.row(r) }
+ query
+ end
+ end
+ end
+end
diff --git a/modules/xls_export/config/locales/en.yml b/modules/xls_export/config/locales/en.yml
index ff907b35a7b..463ca2721f6 100644
--- a/modules/xls_export/config/locales/en.yml
+++ b/modules/xls_export/config/locales/en.yml
@@ -5,6 +5,8 @@ en:
different_formats: Different formats
export:
+ cost_reports:
+ title: "Your Cost Reports XLS export"
format:
xls: "XLS"
xls_with_descriptions: "XLS with descriptions"
diff --git a/modules/xls_export/lib/open_project/xls_export/patches/cost_reports_controller_patch.rb b/modules/xls_export/lib/open_project/xls_export/patches/cost_reports_controller_patch.rb
index e7e78d6329a..278b5e8176a 100644
--- a/modules/xls_export/lib/open_project/xls_export/patches/cost_reports_controller_patch.rb
+++ b/modules/xls_export/lib/open_project/xls_export/patches/cost_reports_controller_patch.rb
@@ -1,6 +1,7 @@
module OpenProject::XlsExport::Patches
module CostReportsControllerPatch
- def self.included(base) # :nodoc:
+ def self.included(base)
+ # :nodoc:
base.prepend InstanceMethods
end
@@ -15,28 +16,19 @@ module OpenProject::XlsExport::Patches
# If the index action is called, hook the xls format into the cost report
def respond_to
- if excel_export?
- super do |format|
- yield format
- format.xls do
- report = report_to_xls
- time = Time.now.strftime('%d-%m-%Y-T-%H-%M-%S')
- send_data(report, type: :xls, filename: "export-#{time}.xls") if report
- end
- end
- else
- super
- end
- end
+ return super unless excel_export?
- # Build an xls file from a cost report.
- # We only support extracting a simple xls table, so grouping is ignored.
- def report_to_xls
- export_query = build_query(filter_params)
+ super do |format|
+ yield format
+ format.xls do
+ job_id = XlsExport::CostReports::ScheduleExportService
+ .new(user: current_user)
+ .call(filter_params:, project: @project, cost_types: @cost_types)
+ .result
- options = { query: export_query, project: @project, cost_types: @cost_types }
-
- ::OpenProject::XlsExport::XlsViews::CostEntryTable.generate(options).xls
+ redirect_to job_status_path(job_id)
+ end
+ end
end
end
end