diff --git a/app/models/form/lettings/pages/stock_owner.rb b/app/models/form/lettings/pages/stock_owner.rb index dee5d5f71..07fe01327 100644 --- a/app/models/form/lettings/pages/stock_owner.rb +++ b/app/models/form/lettings/pages/stock_owner.rb @@ -14,10 +14,14 @@ class Form::Lettings::Pages::StockOwner < ::Form::Page return false unless current_user return true if current_user.support? - stock_owners = current_user.organisation.stock_owners + current_user.organisation.absorbed_organisations.where(holds_own_stock: true) + stock_owners = if FeatureToggle.merge_organisations_enabled? + current_user.organisation.stock_owners + current_user.organisation.absorbed_organisations.where(holds_own_stock: true) + else + current_user.organisation.stock_owners + end if current_user.organisation.holds_own_stock? - if current_user.organisation.absorbed_organisations.any?(&:holds_own_stock?) + if FeatureToggle.merge_organisations_enabled? && current_user.organisation.absorbed_organisations.any?(&:holds_own_stock?) return true end return true if stock_owners.count >= 1 diff --git a/app/models/form/lettings/questions/stock_owner.rb b/app/models/form/lettings/questions/stock_owner.rb index ef01e6538..8fc42e66d 100644 --- a/app/models/form/lettings/questions/stock_owner.rb +++ b/app/models/form/lettings/questions/stock_owner.rb @@ -49,7 +49,11 @@ class Form::Lettings::Questions::StockOwner < ::Form::Question def hidden_in_check_answers?(_log, user = nil) return false if user.support? - stock_owners = user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true) + stock_owners = if FeatureToggle.merge_organisations_enabled? + user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true) + else + user.organisation.stock_owners + end if user.organisation.holds_own_stock? stock_owners.count.zero? diff --git a/app/models/form/sales/pages/organisation.rb b/app/models/form/sales/pages/organisation.rb index 405cf51c1..6940c2abc 100644 --- a/app/models/form/sales/pages/organisation.rb +++ b/app/models/form/sales/pages/organisation.rb @@ -11,18 +11,23 @@ class Form::Sales::Pages::Organisation < ::Form::Page end def routed_to?(_log, current_user) - return false unless current_user - return true if current_user.support? + if FeatureToggle.merge_organisations_enabled? - absorbed_stock_owners = current_user.organisation.absorbed_organisations.where(holds_own_stock: true) + return false unless current_user + return true if current_user.support? - if current_user.organisation.holds_own_stock? - return true if absorbed_stock_owners.count >= 1 + absorbed_stock_owners = current_user.organisation.absorbed_organisations.where(holds_own_stock: true) + + if current_user.organisation.holds_own_stock? + return true if absorbed_stock_owners.count >= 1 + else + return false if absorbed_stock_owners.count.zero? + return true if absorbed_stock_owners.count > 1 + end + + false else - return false if absorbed_stock_owners.count.zero? - return true if absorbed_stock_owners.count > 1 + !!current_user&.support? end - - false end end diff --git a/app/models/form/sales/questions/owning_organisation_id.rb b/app/models/form/sales/questions/owning_organisation_id.rb index a09fe05ed..ec7c28a85 100644 --- a/app/models/form/sales/questions/owning_organisation_id.rb +++ b/app/models/form/sales/questions/owning_organisation_id.rb @@ -11,39 +11,47 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question answer_opts = { "" => "Select an option" } return answer_opts unless ActiveRecord::Base.connected? - return answer_opts unless user - return answer_opts unless log - if log.owning_organisation_id.present? - answer_opts[log.owning_organisation.id] = log.owning_organisation.name - end + if FeatureToggle.merge_organisations_enabled? + return answer_opts unless user + return answer_opts unless log - recently_absorbed_organisations = user.organisation.absorbed_organisations.merged_during_open_collection_period - if !user.support? && user.organisation.holds_own_stock? - answer_opts[user.organisation.id] = if recently_absorbed_organisations.exists? - "#{user.organisation.name} (Your organisation, active as of #{user.organisation.created_at.to_fs(:govuk_date)})" - else - "#{user.organisation.name} (Your organisation)" - end - end + if log.owning_organisation_id.present? + answer_opts[log.owning_organisation.id] = log.owning_organisation.name + end - if user.support? - Organisation.where(holds_own_stock: true).find_each do |org| - if org.merge_date.present? - answer_opts[org.id] = "#{org.name} (inactive as of #{org.merge_date.to_fs(:govuk_date)})" if org.merge_date >= FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period - elsif org.absorbed_organisations.merged_during_open_collection_period.exists? - answer_opts[org.id] = "#{org.name} (active as of #{org.created_at.to_fs(:govuk_date)})" - else - answer_opts[org.id] = org.name + recently_absorbed_organisations = user.organisation.absorbed_organisations.merged_during_open_collection_period + if !user.support? && user.organisation.holds_own_stock? + answer_opts[user.organisation.id] = if recently_absorbed_organisations.exists? + "#{user.organisation.name} (Your organisation, active as of #{user.organisation.created_at.to_fs(:govuk_date)})" + else + "#{user.organisation.name} (Your organisation)" + end + end + + if user.support? + Organisation.where(holds_own_stock: true).find_each do |org| + if org.merge_date.present? + answer_opts[org.id] = "#{org.name} (inactive as of #{org.merge_date.to_fs(:govuk_date)})" if org.merge_date >= FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period + elsif org.absorbed_organisations.merged_during_open_collection_period.exists? + answer_opts[org.id] = "#{org.name} (active as of #{org.created_at.to_fs(:govuk_date)})" + else + answer_opts[org.id] = org.name + end + end + else + recently_absorbed_organisations.each do |absorbed_org| + answer_opts[absorbed_org.id] = merged_organisation_label(absorbed_org.name, absorbed_org.merge_date) if absorbed_org.holds_own_stock? end end + + answer_opts else - recently_absorbed_organisations.each do |absorbed_org| - answer_opts[absorbed_org.id] = merged_organisation_label(absorbed_org.name, absorbed_org.merge_date) if absorbed_org.holds_own_stock? + Organisation.select(:id, :name).each_with_object(answer_opts) do |organisation, hsh| + hsh[organisation.id] = organisation.name + hsh end end - - answer_opts end def displayed_answer_options(log, user = nil) @@ -57,18 +65,22 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question end def derived? - true + true if FeatureToggle.merge_organisations_enabled? end def hidden_in_check_answers?(_log, user = nil) - return false if user.support? + if FeatureToggle.merge_organisations_enabled? + return false if user.support? - stock_owners = user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true) + stock_owners = user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true) - if user.organisation.holds_own_stock? - stock_owners.count.zero? + if user.organisation.holds_own_stock? + stock_owners.count.zero? + else + stock_owners.count <= 1 + end else - stock_owners.count <= 1 + !current_user.support? end end @@ -79,7 +91,11 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question private def selected_answer_option_is_derived?(_log) - true + if FeatureToggle.merge_organisations_enabled? + true + else + false + end end def merged_organisation_label(name, merge_date)