diff --git a/app/models/form/sales/pages/organisation.rb b/app/models/form/sales/pages/organisation.rb index 1d61b86ac..0d241c2fd 100644 --- a/app/models/form/sales/pages/organisation.rb +++ b/app/models/form/sales/pages/organisation.rb @@ -9,8 +9,4 @@ class Form::Sales::Pages::Organisation < ::Form::Page Form::Sales::Questions::OwningOrganisationId.new(nil, nil, self), ] end - - def routed_to?(_log, current_user) - !!current_user&.support? - 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 fa838f744..b34b3e2ac 100644 --- a/app/models/form/sales/questions/owning_organisation_id.rb +++ b/app/models/form/sales/questions/owning_organisation_id.rb @@ -7,24 +7,38 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question @type = "select" end - def answer_options + def answer_options(log = nil, user = nil) 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 = answer_opts.merge({ log.owning_organisation.id => log.owning_organisation.name }) + end - Organisation.select(:id, :name).each_with_object(answer_opts) do |organisation, hsh| - hsh[organisation.id] = organisation.name - hsh + if !user.support? && user.organisation.holds_own_stock? + answer_opts[user.organisation.id] = "#{user.organisation.name} (Your organisation)" end + + user_answer_options = if user.support? + Organisation.where(holds_own_stock: true) + else + user.organisation.stock_owners + user.organisation.absorbed_organisations + end.pluck(:id, :name).to_h + + answer_opts.merge(user_answer_options) end - def displayed_answer_options(_log, _user = nil) - answer_options + def displayed_answer_options(log, user = nil) + answer_options(log, user) end - def label_from_value(value, _log = nil, _user = nil) + def label_from_value(value, log = nil, user = nil) return unless value - answer_options[value] + answer_options(log, user)[value] end def hidden_in_check_answers?(_log, current_user)