|
|
|
@ -10,23 +10,27 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question |
|
|
|
def answer_options(log = nil, user = nil) |
|
|
|
def answer_options(log = nil, user = nil) |
|
|
|
answer_opts = { "" => "Select an option" } |
|
|
|
answer_opts = { "" => "Select an option" } |
|
|
|
|
|
|
|
|
|
|
|
return answer_opts unless ActiveRecord::Base.connected? |
|
|
|
return answer_opts unless ActiveRecord::Base.connected? && user && log |
|
|
|
return answer_opts unless user |
|
|
|
|
|
|
|
return answer_opts unless log |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if log.owning_organisation_id.present? |
|
|
|
if log.owning_organisation_id.present? |
|
|
|
answer_opts = answer_opts.merge({ log.owning_organisation.id => log.owning_organisation.name }) |
|
|
|
answer_opts[log.owning_organisation.id] = log.owning_organisation.name |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
if !user.support? && user.organisation.holds_own_stock? |
|
|
|
if !user.support? && user.organisation.holds_own_stock? |
|
|
|
answer_opts[user.organisation.id] = "#{user.organisation.name} (Your organisation)" |
|
|
|
answer_opts[user.organisation.id] = organisation_label(user.organisation) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
user_answer_options = if user.support? |
|
|
|
user_organisation_options = user.support? ? Organisation.where(holds_own_stock: true) : user.organisation.stock_owners |
|
|
|
Organisation.where(holds_own_stock: true) |
|
|
|
user_answer_options = user_organisation_options.pluck(:id, :name).to_h |
|
|
|
else |
|
|
|
|
|
|
|
user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true) |
|
|
|
unless user.support? |
|
|
|
end.pluck(:id, :name).to_h |
|
|
|
user.organisation.absorbed_organisations.where(holds_own_stock: true).find_each do |absorbed_org| |
|
|
|
|
|
|
|
answer_opts[absorbed_org.id] = merged_organisation_label(absorbed_org.name, absorbed_org.merge_date) |
|
|
|
|
|
|
|
absorbed_org.stock_owners.each do |stock_owner| |
|
|
|
|
|
|
|
user_answer_options[stock_owner.id] = merged_organisation_label(stock_owner.name, absorbed_org.merge_date) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
answer_opts.merge(user_answer_options) |
|
|
|
answer_opts.merge(user_answer_options) |
|
|
|
end |
|
|
|
end |
|
|
|
@ -66,4 +70,16 @@ private |
|
|
|
def selected_answer_option_is_derived?(_log) |
|
|
|
def selected_answer_option_is_derived?(_log) |
|
|
|
true |
|
|
|
true |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def organisation_label(organisation) |
|
|
|
|
|
|
|
if organisation.absorbed_organisations.exists? |
|
|
|
|
|
|
|
"#{organisation.name} (Your organisation, active as of #{organisation.created_at.to_fs(:govuk_date)})" |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
"#{organisation.name} (Your organisation)" |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def merged_organisation_label(name, merge_date) |
|
|
|
|
|
|
|
"#{name} (inactive as of #{merge_date.to_fs(:govuk_date)})" |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|