diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 61cd43674..8ffe426d7 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -24,7 +24,7 @@ class OrganisationsController < ApplicationController end def schemes - organisation_schemes = Scheme.visible.where(owning_organisation: [@organisation] + @organisation.parent_organisations) + organisation_schemes = Scheme.visible.where(owning_organisation: [@organisation] + @organisation.parent_organisations + @organisation.absorbed_organisations.visible.merged_during_open_collection_period) @pagy, @schemes = pagy(filter_manager.filtered_schemes(organisation_schemes, search_term, session_filters)) @searched = search_term.presence diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 7f246b1e2..3dc642345 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -118,6 +118,10 @@ class SchemesController < ApplicationController validation_errors scheme_params if @scheme.errors.empty? && @scheme.save + if @scheme.owning_organisation.merge_date.present? + deactivation = SchemeDeactivationPeriod.new(scheme: @scheme, deactivation_date: @scheme.owning_organisation.merge_date) + deactivation.save!(validate: false) + end redirect_to scheme_primary_client_group_path(@scheme) else if @scheme.errors.any? { |error| error.attribute == :owning_organisation } diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index 3a4c337ea..99c69c636 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -192,9 +192,15 @@ module FiltersHelper end def show_scheme_managing_org_filter?(user) + return true if user.support? + org = user.organisation + stock_owners = org.stock_owners.count + recently_absorbed_with_stock = org.absorbed_organisations.visible.merged_during_open_collection_period.where(holds_own_stock: true).count + + relevant_orgs_count = stock_owners + recently_absorbed_with_stock + (org.holds_own_stock? ? 1 : 0) - user.support? || org.stock_owners.count > 1 || (org.holds_own_stock? && org.stock_owners.count.positive?) + relevant_orgs_count > 1 end def logs_for_both_needstypes_present?(organisation) diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index bcd40b082..12d86aba8 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -20,11 +20,14 @@ module SchemesHelper end def owning_organisation_options(current_user) - all_orgs = Organisation.visible.map { |org| OpenStruct.new(id: org.id, name: org.name) } - user_org = [OpenStruct.new(id: current_user.organisation_id, name: current_user.organisation.name)] - stock_owners = current_user.organisation.stock_owners.visible.map { |org| OpenStruct.new(id: org.id, name: org.name) } - merged_organisations = current_user.organisation.absorbed_organisations.visible.merged_during_open_collection_period.map { |org| OpenStruct.new(id: org.id, name: org.name) } - current_user.support? ? all_orgs : user_org + stock_owners + merged_organisations + if current_user.support? + Organisation.visible.map { |org| OpenStruct.new(id: org.id, name: org.name) } + else + user_org = [current_user.organisation] + stock_owners = current_user.organisation.stock_owners.visible.filter { |org| org.status == :active || (org.status == :merged && org.merge_date >= FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period) } + merged_organisations = current_user.organisation.absorbed_organisations.visible.merged_during_open_collection_period + (user_org + stock_owners + merged_organisations).map { |org| OpenStruct.new(id: org.id, name: org.name) } + end end def null_option @@ -81,7 +84,12 @@ module SchemesHelper when :deactivating_soon "This scheme deactivates on #{scheme.last_deactivation_date.to_formatted_s(:govuk_date)}. Any locations you add will be deactivated on the same date. Reactivate the scheme to add locations active after this date." when :deactivated - "This scheme deactivated on #{scheme.last_deactivation_date.to_formatted_s(:govuk_date)}. Any locations you add will be deactivated on the same date. Reactivate the scheme to add locations active after this date." + case scheme.owning_organisation.status + when :active + "This scheme deactivated on #{scheme.last_deactivation_date.to_formatted_s(:govuk_date)}. Any locations you add will be deactivated on the same date. Reactivate the scheme to add locations active after this date." + when :merged + "This scheme has been deactivated due to #{scheme.owning_organisation.name} merging into #{scheme.owning_organisation.absorbing_organisation.name} on #{scheme.owning_organisation.merge_date.to_formatted_s(:govuk_date)}. Any locations you add will be deactivated on the same date. Use the after merge organisation for schemes and locations active after this date." + end end end diff --git a/app/models/location.rb b/app/models/location.rb index 03af24a94..12c6f2fad 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -54,13 +54,13 @@ class Location < ApplicationRecord } scope :deactivated, lambda { |date = Time.zone.now| - deactivated_by_organisation + deactivated_by_organisation(date) .or(deactivated_directly(date)) .or(deactivated_by_scheme(date)) } - scope :deactivated_by_organisation, lambda { - merge(Organisation.filter_by_inactive) + scope :deactivated_by_organisation, lambda { |date = Time.zone.now| + merge(Organisation.filter_by_inactive.or(Organisation.where("merge_date <= ?", date))) } scope :deactivated_by_scheme, lambda { |date = Time.zone.now| @@ -206,7 +206,7 @@ class Location < ApplicationRecord def status_at(date) return :deleted if discarded_at.present? return :incomplete unless confirmed - return :deactivated if scheme.owning_organisation.status_at(date) == :deactivated || + return :deactivated if scheme.owning_organisation.status_at(date) == :deactivated || scheme.owning_organisation.status_at(date) == :merged || open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date || scheme.status_at(date) == :deactivated return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date || scheme.status_at(date) == :deactivating_soon return :activating_soon if startdate.present? && date < startdate diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 2c73acc06..33f236374 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -57,8 +57,8 @@ class Scheme < ApplicationRecord .or(deactivated_directly) } - scope :deactivated_by_organisation, lambda { - merge(Organisation.filter_by_inactive) + scope :deactivated_by_organisation, lambda { |date = Time.zone.now| + merge(Organisation.filter_by_inactive.or(Organisation.where("merge_date <= ?", date))) } scope :deactivated_directly, lambda { |date = Time.zone.now| @@ -96,7 +96,7 @@ class Scheme < ApplicationRecord scope :active, lambda { |date = Time.zone.now| where.not(id: joins(:scheme_deactivation_periods).reactivating_soon(date).pluck(:id)) .where.not(id: incomplete.pluck(:id)) - .where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id)) + .where.not(id: joins(:owning_organisation).deactivated_by_organisation(date).pluck(:id)) .where.not(id: joins(:owning_organisation).joins(:scheme_deactivation_periods).deactivated_directly(date).pluck(:id)) .where.not(id: activating_soon(date).pluck(:id)) } @@ -314,7 +314,7 @@ class Scheme < ApplicationRecord def status_at(date) return :deleted if discarded_at.present? return :incomplete unless confirmed && locations.confirmed.any? - return :deactivated if owning_organisation.status_at(date) == :deactivated || + return :deactivated if owning_organisation.status_at(date) == :deactivated || owning_organisation.status_at(date) == :merged || (open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date) return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date return :reactivating_soon if last_deactivation_before(date)&.reactivation_date.present? && date < last_deactivation_before(date).reactivation_date diff --git a/app/policies/location_policy.rb b/app/policies/location_policy.rb index 436b961c6..3b4a22131 100644 --- a/app/policies/location_policy.rb +++ b/app/policies/location_policy.rb @@ -16,14 +16,14 @@ class LocationPolicy if location == Location user.data_coordinator? else - user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner_or_recently_absorbed_org end end def update? return true if user.support? - user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner_or_recently_absorbed_org end def delete_confirmation? @@ -62,7 +62,7 @@ class LocationPolicy define_method method_name do return true if user.support? - user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner_or_recently_absorbed_org end end @@ -73,7 +73,7 @@ class LocationPolicy define_method method_name do return true if user.support? - scheme_owned_by_user_org_or_stock_owner + scheme_owned_by_user_org_or_stock_owner_or_recently_absorbed_org end end @@ -83,8 +83,11 @@ private location.scheme end - def scheme_owned_by_user_org_or_stock_owner - scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id) + def scheme_owned_by_user_org_or_stock_owner_or_recently_absorbed_org + scheme_owned_by_user_org = scheme&.owning_organisation == user.organisation + scheme_owned_by_stock_owner = user.organisation.stock_owners.exists?(scheme&.owning_organisation_id) + scheme_owned_by_recently_absorbed_org = user.organisation.absorbed_organisations.visible.merged_during_open_collection_period.exists?(scheme&.owning_organisation_id) + scheme_owned_by_user_org || scheme_owned_by_stock_owner || scheme_owned_by_recently_absorbed_org end def has_any_logs_in_editable_collection_period diff --git a/app/policies/scheme_policy.rb b/app/policies/scheme_policy.rb index 6b97a46de..54a2b9e89 100644 --- a/app/policies/scheme_policy.rb +++ b/app/policies/scheme_policy.rb @@ -12,7 +12,7 @@ class SchemePolicy if scheme == Scheme true else - scheme_owned_by_user_org_or_stock_owner + scheme_owned_by_user_org_or_stock_owner_or_recently_absorbed_org end end @@ -27,7 +27,7 @@ class SchemePolicy def update? return true if user.support? - user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner_or_recently_absorbed_org end def changes? @@ -41,7 +41,7 @@ class SchemePolicy define_method method_name do return true if user.support? - scheme_owned_by_user_org_or_stock_owner + scheme_owned_by_user_org_or_stock_owner_or_recently_absorbed_org end end @@ -61,7 +61,7 @@ class SchemePolicy define_method method_name do return true if user.support? - user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner_or_recently_absorbed_org end end @@ -78,8 +78,11 @@ class SchemePolicy private - def scheme_owned_by_user_org_or_stock_owner - scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id) + def scheme_owned_by_user_org_or_stock_owner_or_recently_absorbed_org + scheme_owned_by_user_org = scheme&.owning_organisation == user.organisation + scheme_owned_by_stock_owner = user.organisation.stock_owners.exists?(scheme&.owning_organisation_id) + scheme_owned_by_recently_absorbed_org = user.organisation.absorbed_organisations.visible.merged_during_open_collection_period.exists?(scheme&.owning_organisation_id) + scheme_owned_by_user_org || scheme_owned_by_stock_owner || scheme_owned_by_recently_absorbed_org end def has_any_logs_in_editable_collection_period diff --git a/app/views/locations/index.html.erb b/app/views/locations/index.html.erb index 64d9bf286..23550f894 100644 --- a/app/views/locations/index.html.erb +++ b/app/views/locations/index.html.erb @@ -56,14 +56,13 @@ <% end %> <% end %> - <% if status_hint_message = scheme_status_hint(@scheme) %> -