From f2cf84bfb60aca4f65fc715a3eb36644f90505dd Mon Sep 17 00:00:00 2001
From: Ian Ballou <ianballou67@gmail.com>
Date: Fri, 13 Oct 2023 16:47:59 +0000
Subject: [PATCH] Fixes #36826 - fix subs entitlement report for SCA * and
 rename it to Host - Products and Subscriptions

---
 .../host_-_products_and_subscriptions.erb     | 74 +++++++++++++++++++
 .../subscription_-_entitlement_report.erb     | 56 --------------
 ...13000000_rename_subs_entitlement_report.rb | 20 +++++
 3 files changed, 94 insertions(+), 56 deletions(-)
 create mode 100644 app/views/unattended/report_templates/host_-_products_and_subscriptions.erb
 delete mode 100644 app/views/unattended/report_templates/subscription_-_entitlement_report.erb
 create mode 100644 db/migrate/20231013000000_rename_subs_entitlement_report.rb

diff --git a/app/views/unattended/report_templates/host_-_products_and_subscriptions.erb b/app/views/unattended/report_templates/host_-_products_and_subscriptions.erb
new file mode 100644
index 000000000000..7ae14673fa37
--- /dev/null
+++ b/app/views/unattended/report_templates/host_-_products_and_subscriptions.erb
@@ -0,0 +1,74 @@
+<%#
+name: Host - Products and Subscriptions
+snippet: false
+model: ReportTemplate
+template_inputs:
+- name: Hosts filter
+  required: false
+  input_type: user
+  description: For organizations using SCA, limit the report only on hosts found by this search query. Keep empty
+    to report on all available hosts.
+  advanced: false
+  value_type: search
+  resource_type: Host
+require:
+- plugin: katello
+  version: 4.11.0
+-%>
+<%- report_headers 'Host Name', 'Organization', 'Lifecycle Environment', 'Content View', 'Host Collections', 'Virtual', 'Guest of Host', 'OS', 'Arch', 'Sockets', 'RAM', 'Cores', 'SLA', 'Role', 'Usage', 'Products', 'Last Checkin', 'Subscription Name', 'Subscription Type', 'Subscription Total Quantity', 'Subscription Total Consumed', 'Subscriptions Consumed by Host', 'Subscription SKU', 'Subscription Contract', 'Subscription Account', 'Subscription Start', 'Subscription End', 'Subscription Guest', 'Days Remaining' -%>
+<%- load_hosts(search: input('Hosts filter'), includes: [:operatingsystem, :architecture, :content_view_environments, :organization, :reported_data, :subscription_facet, :pools => [:subscription]]).each_record do |host| -%>
+<%-   if host.organization.simple_content_access? -%>
+<%-     report_row(
+          'Host Name': host.name,
+          'Organization': host.organization,
+          'Lifecycle Environment': host.single_lifecycle_environment,
+          'Content View': host.single_content_view,
+          'Host Collections': host_collections_names(host),
+          'Virtual': host.virtual,
+          'Guest of Host': host.hypervisor_host,
+          'OS': host.operatingsystem,
+          'Arch': host.architecture,
+          'Sockets': host.sockets,
+          'RAM': host.ram,
+          'Cores': host.cores,
+          'SLA': host_sla(host),
+          'Role': host.purpose_role_status_label,
+          'Usage': host.purpose_usage_status_label,
+          'Products': host_products_names_and_ids(host),
+          'Last Checkin': last_checkin(host).to_s,
+        ) -%>
+<%-   else -%>
+<%-     report_row(
+          'Host Name': host.name,
+          'Organization': host.organization,
+          'Lifecycle Environment': host.single_lifecycle_environment.name,
+          'Content View': host.single_content_view.name,
+          'Host Collections': host_collections_names(host),
+          'Virtual': host.virtual,
+          'Guest of Host': host.hypervisor_host,
+          'OS': host.operatingsystem,
+          'Arch': host.architecture,
+          'Sockets': host.sockets,
+          'RAM': host.ram,
+          'Cores': host.cores,
+          'SLA': host_sla(host),
+          'Role': host.purpose_role_status_label,
+          'Usage': host.purpose_usage_status_label,
+          'Products': host_products_names_and_ids(host),
+          'Last Checkin': last_checkin(host).to_s,
+          'Subscription Name': sub_name(pool),
+          'Subscription Type': pool.type,
+          'Subscription Total Quantity': pool.quantity,
+          'Subscription Total Consumed': pool.consumed,
+          'Subscriptions Consumed by Host': host.filtered_entitlement_quantity_consumed(pool),
+          'Subscription SKU': sub_sku(pool),
+          'Subscription Contract': pool.contract_number,
+          'Subscription Account': pool.account_number,
+          'Subscription Start': pool.start_date,
+          'Subscription End': pool.end_date,
+          'Subscription Guest': registered_through(host),
+          'Days Remaining': pool.days_until_expiration
+        ) -%>
+<%-   end -%>
+<%- end -%>
+<%= report_render -%>
diff --git a/app/views/unattended/report_templates/subscription_-_entitlement_report.erb b/app/views/unattended/report_templates/subscription_-_entitlement_report.erb
deleted file mode 100644
index 2b363fc67740..000000000000
--- a/app/views/unattended/report_templates/subscription_-_entitlement_report.erb
+++ /dev/null
@@ -1,56 +0,0 @@
-<%#
-name: Subscription - Entitlement Report
-snippet: false
-model: ReportTemplate
-template_inputs:
-- name: Days from Now
-  required: true
-  input_type: user
-  description: "Show subscriptions expiring in <= this many days.  Select 'no limit' to show all entitlements."
-  advanced: false
-  value_type: plain
-  options: "no limit\r\n7\r\n30\r\n60\r\n90\r\n120\r\n180"
-require:
-- plugin: katello
-  version: 4.8.0
--%>
-<%- days_from_now = input('Days from Now') -%>
-<%- days_from_now = "" if days_from_now == 'no limit' -%>
-<%- should_filter = days_from_now.present? -%>
-<%- report_headers 'Host Name', 'Organization', 'Lifecycle Environment', 'Content View', 'Host Collections', 'Virtual', 'Guest of Host', 'OS', 'Arch', 'Sockets', 'RAM', 'Cores', 'SLA', 'Products', 'Last Checkin', 'Subscription Name', 'Subscription Type', 'Subscription Total Quantity', 'Subscription Total Consumed', 'Subscriptions Consumed by Host', 'Subscription SKU', 'Subscription Contract', 'Subscription Account', 'Subscription Start', 'Subscription End', 'Subscription Guest', 'Days Remaining' -%>
-<%- load_hosts(includes: [:operatingsystem, :architecture, :content_view_environments, :organization, :reported_data, :subscription_facet, :pools => [:subscription]], -%>
-<%-   search: should_filter && "pools_expiring_in_days = #{days_from_now}" -%>
-<%- ).each_record do |host| -%>
-<%-   host.pools.expiring_in_days(days_from_now).sort_by { |p| p.end_date }.each do |pool| -%>
-<%-     report_row(
-          'Host Name': host.name,
-          'Organization': host.organization,
-          'Lifecycle Environment': host.single_lifecycle_environment.name,
-          'Content View': host.single_content_view.name,
-          'Host Collections': host_collections_names(host),
-          'Virtual': host.virtual,
-          'Guest of Host': host.hypervisor_host,
-          'OS': host.operatingsystem,
-          'Arch': host.architecture,
-          'Sockets': host.sockets,
-          'RAM': host.ram,
-          'Cores': host.cores,
-          'SLA': host_sla(host),
-          'Products': host_products_names(host),
-          'Last Checkin': last_checkin(host).to_s,
-          'Subscription Name': sub_name(pool),
-          'Subscription Type': pool.type,
-          'Subscription Total Quantity': pool.quantity,
-          'Subscription Total Consumed': pool.consumed,
-          'Subscriptions Consumed by Host': host.filtered_entitlement_quantity_consumed(pool),
-          'Subscription SKU': sub_sku(pool),
-          'Subscription Contract': pool.contract_number,
-          'Subscription Account': pool.account_number,
-          'Subscription Start': pool.start_date,
-          'Subscription End': pool.end_date,
-          'Subscription Guest': registered_through(host),
-          'Days Remaining': pool.days_until_expiration
-          ) -%>
-<%-   end -%>
-<%- end -%>
-<%= report_render -%>
diff --git a/db/migrate/20231013000000_rename_subs_entitlement_report.rb b/db/migrate/20231013000000_rename_subs_entitlement_report.rb
new file mode 100644
index 000000000000..e46c92cec45a
--- /dev/null
+++ b/db/migrate/20231013000000_rename_subs_entitlement_report.rb
@@ -0,0 +1,20 @@
+class RenameSubsEntitlementReport < ActiveRecord::Migration[6.1]
+  TEMPLATE_NAMES = {
+    "Subscription - Entitlement Report" => "Host - Products and Subcriptions"
+  }
+
+  def up
+    TEMPLATE_NAMES.each do |from, to|
+      token = SecureRandom.base64(5)
+      ReportTemplate.unscoped.find_by(name: to)&.update_columns(:name => "#{to} Backup #{token}")
+      ReportTemplate.unscoped.find_by(name: from)&.update_columns(:name => to)
+    end
+  end
+
+  def down
+    TEMPLATE_NAMES.each do |from, to|
+      ReportTemplate.unscoped.find_by(name: from)&.delete
+      ReportTemplate.unscoped.find_by(name: to)&.update_columns(:name => from)
+    end
+  end
+end