Browse Source

feat: wip update sales org select

pull/1815/head
natdeanlewissoftwire 3 years ago
parent
commit
453d272ae7
  1. 3
      app/models/form/lettings/pages/stock_owner.rb
  2. 2
      app/models/form/sales/pages/organisation.rb
  3. 48
      app/models/form/sales/questions/owning_organisation_id.rb

3
app/models/form/lettings/pages/stock_owner.rb

@ -17,6 +17,9 @@ class Form::Lettings::Pages::StockOwner < ::Form::Page
stock_owners = current_user.organisation.stock_owners stock_owners = current_user.organisation.stock_owners
if current_user.organisation.holds_own_stock? if current_user.organisation.holds_own_stock?
if current_user.organisation.absorbed_organisations.any?(&:holds_own_stock?)
return true
end
return true if stock_owners.count >= 1 return true if stock_owners.count >= 1
log.update!(owning_organisation: current_user.organisation) log.update!(owning_organisation: current_user.organisation)

2
app/models/form/sales/pages/organisation.rb

@ -11,6 +11,6 @@ class Form::Sales::Pages::Organisation < ::Form::Page
end end
def routed_to?(_log, current_user) def routed_to?(_log, current_user)
!!current_user&.support? !!current_user&.support? || current_user&.organisation&.absorbed_organisations.present?
end end
end end

48
app/models/form/sales/questions/owning_organisation_id.rb

@ -7,31 +7,57 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question
@type = "select" @type = "select"
end end
def answer_options 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?
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| if !user.support? && user.organisation.holds_own_stock?
hsh[organisation.id] = organisation.name answer_opts[user.organisation.id] = "#{user.organisation.name} (Your organisation)"
hsh
end end
user_answer_options = if user.support?
Organisation.where(holds_own_stock: true)
else
user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true)
end.pluck(:id, :name).to_h
answer_opts.merge(user_answer_options)
end end
def displayed_answer_options(_log, _user = nil) def displayed_answer_options(log, user = nil)
answer_options answer_options(log, user)
end end
def label_from_value(value, _log = nil, _user = nil) def label_from_value(value, log = nil, user = nil)
return unless value return unless value
answer_options[value] answer_options(log, user)[value]
end end
def hidden_in_check_answers?(_log, current_user) def derived?
!current_user.support? true
end end
def derived? def hidden_in_check_answers?(_log, user = nil)
return false if user.support?
stock_owners = user.organisation.stock_owners
if user.organisation.holds_own_stock?
stock_owners.count.zero?
else
stock_owners.count <= 1
end
end
def enabled
true true
end end

Loading…
Cancel
Save