Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download a list of non-continuing ODA activities #2310

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/controllers/exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,18 @@ def continuing_activities
end
end
end

def non_continuing_activities
authorize :export, :show_continuing_activities?

respond_to do |format|
format.csv do
export = Export::ContinuingActivities.new

stream_csv_download(filename: export.non_continuing_filename, headers: export.non_continuing_headers) do |csv|
export.non_continuing_rows.each { |row| csv << row }
end
end
end
end
end
128 changes: 99 additions & 29 deletions app/services/export/continuing_activities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ def filename
"continuing_activities.csv"
end

def non_continuing_filename
"non_continuing_activities.csv"
end

def headers
[
"Partner Organisation name",
Expand All @@ -21,6 +25,22 @@ def headers
]
end

def non_continuing_headers
[
"Partner Organisation name",
"Activity title",
"Activity RODA ID",
"Fund (level A)",
"Parent level B RODA ID",
"Parent level B title",
"Transparency identifier",
"Previous identifier",
"Partner Organisation ID",
"Status",
"Level"
]
end

def rows
activities.map do |activity|
partner_organisation_name = activity.organisation.name
Expand Down Expand Up @@ -70,39 +90,89 @@ def rows
end

def activities
# active statuses, regardless of any associated actual spend
definitely_active = Activity
.joins(:organisation)
.includes(:organisation, :parent)
.where.not(level: "fund")
.where(is_oda: [nil, true])
.where.not(programme_status: ["completed", "stopped", "cancelled", "finalisation", "paused"])
.order("organisations.name, activities.roda_identifier")
@_activities ||= begin
# active statuses, regardless of any associated actual spend
definitely_active = Activity
.joins(:organisation)
.includes(:organisation, :parent)
.where.not(level: "fund")
.where(is_oda: [nil, true])
.where.not(programme_status: ["completed", "stopped", "cancelled", "finalisation", "paused"])
.order("organisations.name, activities.roda_identifier")

cut_off_quarter = FinancialQuarter.new(2022, 4)
cut_off_quarter = FinancialQuarter.new(2022, 4)

# activities that MAY need to continue, IF they have actual spend more recent than FQ4 2022-2023
potentially_active_due_to_actuals = Activity
.joins(:organisation, :actuals)
.includes(:organisation, :parent)
.where.not(level: "fund")
.where(is_oda: [nil, true])
.where(programme_status: ["completed", "stopped", "cancelled", "finalisation", "paused"])
.where("transactions.date > ?", cut_off_quarter.end_date)
.order("organisations.name, activities.roda_identifier")
# activities that MAY need to continue, IF they have actual spend more recent than FQ4 2022-2023
potentially_active_due_to_actuals = Activity
.joins(:organisation, :actuals)
.includes(:organisation, :parent)
.where.not(level: "fund")
.where(is_oda: [nil, true])
.where(programme_status: ["completed", "stopped", "cancelled", "finalisation", "paused"])
.where("transactions.date > ?", cut_off_quarter.end_date)
.order("organisations.name, activities.roda_identifier")

# activities that MAY need to continue, IF they have forecasts for quarters after FQ4 2022-2023
potentially_active_due_to_forecasts = Activity
.joins(:organisation)
.includes(:organisation, :parent)
.where.not(level: "fund")
.where(is_oda: [nil, true])
.where(programme_status: "paused")
.order("organisations.name, activities.roda_identifier")
potentially_active_due_to_forecasts = potentially_active_due_to_forecasts.select do |activity|
Forecast.unscoped.where(parent_activity_id: activity.id).where("period_start_date > ?", cut_off_quarter.end_date).any?
# activities that MAY need to continue, IF they have forecasts for quarters after FQ4 2022-2023
potentially_active_due_to_forecasts = Activity
.joins(:organisation)
.includes(:organisation, :parent)
.where.not(level: "fund")
.where(is_oda: [nil, true])
.where(programme_status: "paused")
.order("organisations.name, activities.roda_identifier")
potentially_active_due_to_forecasts = potentially_active_due_to_forecasts.select do |activity|
Forecast.unscoped.where(parent_activity_id: activity.id).where("period_start_date > ?", cut_off_quarter.end_date).any?
end

(definitely_active + potentially_active_due_to_actuals + potentially_active_due_to_forecasts).uniq
end
end

def non_continuing_rows
non_continuing_activities = Activity
.where.not(level: "fund")
.where.not(id: activities.pluck(:id))
.where(is_oda: [true, nil])
non_continuing_activities.map do |activity|
partner_organisation_name = activity.organisation.name
activity_title = activity.title || "Untitled (#{activity.id})"
roda_identifier = activity.roda_identifier
fund = activity.associated_fund.roda_identifier
parent_level_b_roda_id = case activity.level
when "programme"
""
when "project"
activity.parent&.roda_identifier
else
activity.parent&.parent&.roda_identifier
end
parent_level_b_title = case activity.level
when "programme"
""
when "project"
activity.parent&.title
else
activity.parent&.parent&.title
end
transparency_identifier = activity.transparency_identifier
previous_identifier = activity.previous_identifier
partner_organisation_identifier = activity.partner_organisation_identifier
status = activity.programme_status ? I18n.t("activity.programme_status.#{activity.programme_status}") : ""
level = I18n.t("table.body.activity.level.#{activity.level}")

(definitely_active + potentially_active_due_to_actuals + potentially_active_due_to_forecasts).uniq
[
partner_organisation_name,
activity_title,
roda_identifier,
fund,
parent_level_b_roda_id,
parent_level_b_title,
transparency_identifier,
previous_identifier,
partner_organisation_identifier,
status,
level
]
end
end
end
7 changes: 7 additions & 0 deletions app/views/exports/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@
CSV
%td.govuk-table__cell
= a11y_action_link("Download", continuing_activities_exports_path(format: "csv"))
%tr.govuk-table__row
%td.govuk-table__cell
Activities not continuing under GB-GOV-26
%td.govuk-table__cell
CSV
%td.govuk-table__cell
= a11y_action_link("Download", non_continuing_activities_exports_path(format: "csv"))

%h1.govuk-heading-m
= t("page_content.export.organisations.title")
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
get "spending_breakdown_download"
end
get "continuing_activities", on: :collection
get "non_continuing_activities", on: :collection
end

namespace :exports do
Expand Down