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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#59914] Add times for labour costs to the cost report and export
as-op2dc9b5362c49
diff --git a/modules/reporting/app/helpers/reporting_helper.rb b/modules/reporting/app/helpers/reporting_helper.rbindex 35f5ffe14e6..822fe88b68d 100644--- a/modules/reporting/app/helpers/reporting_helper.rb+++ b/modules/reporting/app/helpers/reporting_helper.rb@@ -141,6 +141,28 @@ module ReportingHelperend# rubocop:enable Metrics/AbcSize+ def spent_on_time_representation(start_timestamp, hours)+ return "" if start_timestamp.nil?++ result = format_time(start_timestamp, include_date: false)+ return result if hours.nil? || hours.zero?++ end_timestamp = start_timestamp + hours.hours+ days_between_suffix = days_between_representation(start_timestamp, end_timestamp)+ "#{result} - #{format_time(end_timestamp, include_date: false)}#{days_between_suffix}"+ end++ def days_between_representation(start_timestamp, end_timestamp)+ return "" if start_timestamp.nil? || end_timestamp.nil?++ days_between = (end_timestamp.to_date - start_timestamp.to_date).to_i+ if days_between.positive?+ " (+#{WorkPackage::Exports::Formatters::Days.new(nil)+ .format_value(days_between, nil)+ .delete(' ')})"+ end+ end+def custom_value(cf_identifier, value)cf_id = cf_identifier.gsub("custom_field", "").to_idiff --git a/modules/reporting/app/models/cost_query/result.rb b/modules/reporting/app/models/cost_query/result.rbindex 32b5d4bb38e..a04cfa067cf 100644--- a/modules/reporting/app/models/cost_query/result.rb+++ b/modules/reporting/app/models/cost_query/result.rb@@ -44,6 +44,7 @@ class CostQuery::Result < Report::Resultclass DirectResult < Report::Result::DirectResultinclude BaseAdditions+def display_costsself["display_costs"].to_iend@@ -51,10 +52,32 @@ class CostQuery::Result < Report::Resultdef real_costs(self["real_costs"] || 0).to_d if display_costs? # FIXME: default value here?end++ def start_timestamp+ return nil if self["start_time"].blank? || self["time_zone"].blank? || self["spent_on"].blank?++ timestamp(Date.parse(self["spent_on"]), self["start_time"], self["time_zone"])+ end++ def end_timestamp+ return nil if self["units"].blank?++ start = start_timestamp+ return nil if start.nil?++ start + self["units"].to_i.hours+ end++ private++ def timestamp(date, time, time_zone)+ ActiveSupport::TimeZone[time_zone].local(date.year, date.month, date.day, time / 60, time % 60)+ endendclass WrappedResult < Report::Result::WrappedResultinclude BaseAdditions+def display_costs(sum_for :display_costs) >= 1 ? 1 : 0enddiff --git a/modules/reporting/app/models/cost_query/sql_statement.rb b/modules/reporting/app/models/cost_query/sql_statement.rbindex 973eaf8d14c..d43b2dc2920 100644--- a/modules/reporting/app/models/cost_query/sql_statement.rb+++ b/modules/reporting/app/models/cost_query/sql_statement.rb@@ -74,6 +74,8 @@ class CostQuery::SqlStatement < Report::SqlStatement# cost_type_id | -1 | cost_type_id# type | "TimeEntry" | "CostEntry"# count | 1 | 1+ # start_time | start_time | nil+ # time_zone | time_zone | nil## Also: This _should_ handle joining activities and cost_types, as the logic differs for time_entries# and cost_entries.@@ -102,7 +104,7 @@ class CostQuery::SqlStatement < Report::SqlStatement## @param [CostQuery::SqlStatement] query The statement to adjustdef self.unify_time_entries(query)- query.select :activity_id, :logged_by_id, units: :hours, cost_type_id: -1+ query.select :activity_id, :logged_by_id, :start_time, :time_zone, units: :hours, cost_type_id: -1query.select cost_type: quoted_label(:caption_labor)end@@ -111,7 +113,7 @@ class CostQuery::SqlStatement < Report::SqlStatement## @param [CostQuery::SqlStatement] query The statement to adjustdef self.unify_cost_entries(query)- query.select :units, :cost_type_id, :logged_by_id, activity_id: -1+ query.select :units, :cost_type_id, :logged_by_id, activity_id: -1, start_time: nil, time_zone: nilquery.select cost_type: "cost_types.name"query.join CostTypeenddiff --git a/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rb b/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rbindex 647daa18814..a0232e3a0b8 100644--- a/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rb+++ b/modules/reporting/app/workers/cost_query/pdf/timesheet_generator.rb@@ -4,6 +4,7 @@ class CostQuery::PDF::TimesheetGeneratorinclude WorkPackage::PDFExport::Common::Logoinclude WorkPackage::PDFExport::Export::Pageinclude WorkPackage::PDFExport::Export::Style+ include ReportingHelperH1_FONT_SIZE = 26H1_MARGIN_BOTTOM = 2@@ -347,26 +348,7 @@ class CostQuery::PDF::TimesheetGeneratorenddef format_spent_on_time(entry)- start_timestamp = entry.start_timestamp- return "" if start_timestamp.nil?-- result = format_time(start_timestamp, include_date: false)- end_timestamp = entry.end_timestamp- return result if end_timestamp.nil?-- days_between_suffix = format_days_between(start_timestamp, end_timestamp)- "#{result} - #{format_time(end_timestamp, include_date: false)}#{days_between_suffix}"- end-- def format_days_between(start_timestamp, end_timestamp)- days_between = (end_timestamp.to_date - start_timestamp.to_date).to_i- if days_between.positive?- " (+#{days_formatter.format_value(days_between, nil).delete(' ')})"- end- end-- def days_formatter- @days_formatter ||= WorkPackage::Exports::Formatters::Days.new(nil)+ spent_on_time_representation(entry.start_timestamp, entry.hours)enddef with_times_column?diff --git a/modules/reporting/config/locales/en.yml b/modules/reporting/config/locales/en.ymlindex 1b56799a2c0..6141e7c3f89 100644--- a/modules/reporting/config/locales/en.yml+++ b/modules/reporting/config/locales/en.yml@@ -112,6 +112,8 @@ en:time: "Time"cost_reports:title: "Your Cost Reports XLS export"+ start_time: "Start time"+ end_time: "End time"reporting:group_by:diff --git a/modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rb b/modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rbindex 6cdd22eedb7..75fb34604ef 100644--- a/modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rb+++ b/modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rb@@ -52,22 +52,43 @@ class OpenProject::Reporting::CostEntryXlsTable < OpenProject::XlsExport::XlsVienumber_format: currency_format)end+ def cost_fields_columns(result)+ cost_entry_attributes+ .map { |field| show_field field, result.fields[field.to_s] }+ end++ def cost_main_times_columns(result)+ start_timestamp = result.start_timestamp+ end_timestamp = result.end_timestamp+ days_between_suffix = days_between_representation(start_timestamp, end_timestamp)+ [+ format_time(start_timestamp, include_date: false),+ "#{format_time(end_timestamp, include_date: false)}#{days_between_suffix}"+ ]+ end++ def cost_main_columns(result)+ main_cols = [show_field(:spent_on, result.fields[:spent_on.to_s])]+ main_cols.concat cost_main_times_columns(result) if with_times_column?+ main_cols+ end+def cost_row(result)current_cost_type_id = result.fields["cost_type_id"].to_i- cost_entry_attributes- .map { |field| show_field field, result.fields[field.to_s] }- .concat(- [- show_result(result, current_cost_type_id), # units- cost_type_label(current_cost_type_id, @cost_type), # cost type- show_result(result, 0) # costs/currency- ]+ cost_main_columns(result)+ .concat(cost_fields_columns(result))+ .push(++ show_result(result, current_cost_type_id), # units+ cost_type_label(current_cost_type_id, @cost_type), # cost type+ show_result(result, 0) # costs/currency+)enddef build_footer- footer = [""] * cost_entry_attributes.size+ footer = [""] * (cost_entry_attributes.size + main_headers.size)footer += if show_result(query, 0) == show_result(query)multiple_unit_types_footerelse@@ -84,14 +105,22 @@ class OpenProject::Reporting::CostEntryXlsTable < OpenProject::XlsExport::XlsVie["", "", show_result(query)]end+ def main_headers+ main = [label_for(:spent_on)]+ if with_times_column?+ main.push I18n.t(:"export.cost_reports.start_time"), I18n.t(:"export.cost_reports.end_time")+ end+ main+ end+def headers- cost_entry_attributes- .map { |field| label_for(field) }- .concat([CostEntry.human_attribute_name(:units), CostType.model_name.human, CostEntry.human_attribute_name(:costs)])+ main_headers+ .concat(cost_entry_attributes.map { |field| label_for(field) })+ .push(CostEntry.human_attribute_name(:units), CostType.model_name.human, CostEntry.human_attribute_name(:costs))enddef cost_entry_attributes- %i[spent_on user_id activity_id work_package_id comments project_id]+ %i[user_id activity_id work_package_id comments project_id]end# Returns the results of the query sorted by date the time was spent on and by id@@ -103,4 +132,12 @@ class OpenProject::Reporting::CostEntryXlsTable < OpenProject::XlsExport::XlsVie.sort.flat_map { |_, date_results| date_results.sort_by { |r| r.fields["id"] } }end++ def labour_query?+ @unit_id == -1+ end++ def with_times_column?+ Setting.allow_tracking_start_and_end_times && labour_query?+ endenddiff --git a/modules/reporting/lib/widget/table/entry_table.rb b/modules/reporting/lib/widget/table/entry_table.rbindex 6cab895d47e..cf5aeaa8470 100644--- a/modules/reporting/lib/widget/table/entry_table.rb+++ b/modules/reporting/lib/widget/table/entry_table.rb@@ -27,7 +27,9 @@#++class Widget::Table::EntryTable < Widget::Table- FIELDS = %i[spent_on user_id activity_id work_package_id comments logged_by_id project_id].freeze+ include ReportingHelper++ FIELDS = %i[user_id activity_id work_package_id comments logged_by_id project_id].freezedef rendercontent = content_tag :div, class: "generic-table--container -with-footer" do@@ -47,6 +49,7 @@ class Widget::Table::EntryTable < Widget::Tabledef colgroupcontent_tag :colgroup do+ concat content_tag(:col, "")FIELDS.each doconcat content_tag(:col, "")end@@ -56,33 +59,31 @@ class Widget::Table::EntryTable < Widget::Tableendend+ def head_column_field(field)+ head_column(label_for(field))+ end++ def head_column(label)+ content_tag(:th) do+ content_tag(:div, class: "generic-table--sort-header-outer") do+ content_tag(:div, class: "generic-table--sort-header") do+ content_tag(:span, label)+ end+ end+ end+ end+# rubocop:disable Metrics/AbcSizedef headcontent_tag :thead docontent_tag :tr do+ concat head_column_field(:spent_on)+ concat head_column(I18n.t("label_time")) if with_times_column?FIELDS.map do |field|- concat content_tag(:th) {- content_tag(:div, class: "generic-table--sort-header-outer") do- content_tag(:div, class: "generic-table--sort-header") do- content_tag(:span, label_for(field))- end- end- }+ concat head_column_field(field)end- concat content_tag(:th) {- content_tag(:div, class: "generic-table--sort-header-outer") do- content_tag(:div, class: "generic-table--sort-header") do- content_tag(:span, cost_type.try(:unit_plural) || I18n.t(:units))- end- end- }- concat content_tag(:th) {- content_tag(:div, class: "generic-table--sort-header-outer") do- content_tag(:div, class: "generic-table--sort-header") do- content_tag(:span, CostEntry.human_attribute_name(:costs))- end- end- }+ concat head_column(cost_type.try(:unit_plural) || I18n.t(:units))+ concat head_column(CostEntry.human_attribute_name(:costs))hit = false@subject.each_direct_result do |result|next if hit@@ -101,15 +102,16 @@ class Widget::Table::EntryTable < Widget::Tabledef footcontent_tag :tfoot docontent_tag :tr do+ main_columns = with_times_column? ? 2 : 1if show_result(@subject, 0) == show_result(@subject)- concat content_tag(:td, "", colspan: FIELDS.size + 1)+ concat content_tag(:td, "", colspan: FIELDS.size + main_columns + 1)concat content_tag(:td) {concat content_tag(:div,show_result(@subject),class: "result generic-table--footer-outer")}else- concat content_tag(:td, "", colspan: FIELDS.size)+ concat content_tag(:td, "", colspan: FIELDS.size + main_columns)concat content_tag(:td) {concat content_tag(:div,show_result(@subject),@@ -126,15 +128,25 @@ class Widget::Table::EntryTable < Widget::Tableendend+ def body_column_field(field, result)+ content_tag(:td, show_field(field, result.fields[field.to_s]),+ "raw-data": raw_field(field, result.fields[field.to_s]),+ class: "left")+ end+def bodycontent_tag :tbody dorows = "".html_safe@subject.each_direct_result do |result|rows << (content_tag(:tr) do+ concat body_column_field(:spent_on, result)+ if with_times_column?+ concat content_tag :td, spent_on_time_representation(result.start_timestamp, result["units"].to_i),+ class: "start_time right",+ "raw-data": result.start_timestamp.to_s+ endFIELDS.each do |field|- concat content_tag(:td, show_field(field, result.fields[field.to_s]),- "raw-data": raw_field(field, result.fields[field.to_s]),- class: "left")+ concat body_column_field(field, result)endconcat content_tag :td, show_result(result, result.fields["cost_type_id"].to_i),class: "units right",@@ -177,4 +189,13 @@ class Widget::Table::EntryTable < Widget::Tableend# rubocop:enable Metrics/AbcSize++ def labour_query?+ cost_type_filter = @subject.filters.detect { |f| f.is_a?(CostQuery::Filter::CostTypeId) }+ cost_type_filter&.values&.first.to_i == -1+ end++ def with_times_column?+ Setting.allow_tracking_start_and_end_times && labour_query?+ endend