You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
761 lines
21 KiB
761 lines
21 KiB
2 years ago
|
class LettingsLogValidator < ActiveModel::Validator
|
||
3 years ago
|
# Validations methods need to be called 'validate_' to run on model save
|
||
|
# or form page submission
|
||
3 years ago
|
include Validations::SetupValidations
|
||
3 years ago
|
include Validations::HouseholdValidations
|
||
|
include Validations::PropertyValidations
|
||
|
include Validations::FinancialValidations
|
||
|
include Validations::TenancyValidations
|
||
|
include Validations::DateValidations
|
||
3 years ago
|
include Validations::LocalAuthorityValidations
|
||
3 years ago
|
def validate(record)
|
||
3 years ago
|
validation_methods = public_methods.select { |method| method.starts_with?("validate_") }
|
||
|
validation_methods.each { |meth| public_send(meth, record) }
|
||
3 years ago
|
end
|
||
|
end
|
||
|
|
||
2 years ago
|
class LettingsLog < ApplicationRecord
|
||
3 years ago
|
include Validations::SoftValidations
|
||
2 years ago
|
include DerivedVariables::LettingsLogVariables
|
||
3 years ago
|
|
||
|
has_paper_trail
|
||
3 years ago
|
|
||
2 years ago
|
validates_with LettingsLogValidator
|
||
3 years ago
|
before_validation :recalculate_start_year!, if: :startdate_changed?
|
||
2 years ago
|
before_validation :reset_scheme_location!, if: :scheme_changed?, unless: :location_changed?
|
||
3 years ago
|
before_validation :process_postcode_changes!, if: :postcode_full_changed?
|
||
|
before_validation :process_previous_postcode_changes!, if: :ppostcode_full_changed?
|
||
3 years ago
|
before_validation :reset_invalidated_dependent_fields!
|
||
3 years ago
|
before_validation :reset_location_fields!, unless: :postcode_known?
|
||
3 years ago
|
before_validation :reset_previous_location_fields!, unless: :previous_postcode_known?
|
||
3 years ago
|
before_validation :set_derived_fields!
|
||
3 years ago
|
before_save :update_status!
|
||
3 years ago
|
|
||
3 years ago
|
belongs_to :owning_organisation, class_name: "Organisation", optional: true
|
||
|
belongs_to :managing_organisation, class_name: "Organisation", optional: true
|
||
|
belongs_to :created_by, class_name: "User", optional: true
|
||
3 years ago
|
belongs_to :scheme, optional: true
|
||
3 years ago
|
belongs_to :location, optional: true
|
||
3 years ago
|
|
||
3 years ago
|
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) }
|
||
3 years ago
|
scope :filter_by_status, ->(status, _user = nil) { where status: }
|
||
3 years ago
|
scope :filter_by_years, lambda { |years, _user = nil|
|
||
3 years ago
|
first_year = years.shift
|
||
|
query = filter_by_year(first_year)
|
||
|
years.each { |year| query = query.or(filter_by_year(year)) }
|
||
|
query.all
|
||
|
}
|
||
3 years ago
|
scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) }
|
||
3 years ago
|
|
||
3 years ago
|
scope :filter_by_user, lambda { |selected_user, user|
|
||
|
if !selected_user.include?("all") && user.present?
|
||
3 years ago
|
where(created_by: user)
|
||
3 years ago
|
end
|
||
|
}
|
||
|
|
||
3 years ago
|
scope :filter_by_id, ->(id) { where(id:) }
|
||
3 years ago
|
scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") }
|
||
3 years ago
|
scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") }
|
||
2 years ago
|
scope :filter_by_postcode, ->(postcode_full) { where("REPLACE(postcode_full, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }
|
||
2 years ago
|
scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }
|
||
3 years ago
|
scope :search_by, lambda { |param|
|
||
2 years ago
|
filter_by_location_postcode(param)
|
||
|
.or(filter_by_tenant_code(param))
|
||
|
.or(filter_by_propcode(param))
|
||
|
.or(filter_by_postcode(param))
|
||
|
.or(filter_by_id(param))
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze
|
||
3 years ago
|
OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenancycode propcode].freeze
|
||
3 years ago
|
RENT_TYPE_MAPPING_LABELS = { 1 => "Social Rent", 2 => "Affordable Rent", 3 => "Intermediate Rent" }.freeze
|
||
3 years ago
|
HAS_BENEFITS_OPTIONS = [1, 6, 8, 7].freeze
|
||
3 years ago
|
STATUS = { "not_started" => 0, "in_progress" => 1, "completed" => 2 }.freeze
|
||
3 years ago
|
NUM_OF_WEEKS_FROM_PERIOD = { 2 => 26, 3 => 13, 4 => 12, 5 => 50, 6 => 49, 7 => 48, 8 => 47, 9 => 46, 1 => 52 }.freeze
|
||
3 years ago
|
SUFFIX_FROM_PERIOD = { 2 => "every 2 weeks", 3 => "every 4 weeks", 4 => "every month" }.freeze
|
||
3 years ago
|
RETIREMENT_AGES = { "M" => 67, "F" => 60, "X" => 67 }.freeze
|
||
3 years ago
|
enum status: STATUS
|
||
3 years ago
|
|
||
3 years ago
|
def form
|
||
3 years ago
|
FormHandler.instance.get_form(form_name) || FormHandler.instance.forms.first.second
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def collection_start_year
|
||
3 years ago
|
return @start_year if @start_year
|
||
3 years ago
|
return unless startdate
|
||
|
|
||
3 years ago
|
window_end_date = Time.zone.local(startdate.year, 4, 1)
|
||
3 years ago
|
@start_year = startdate < window_end_date ? startdate.year - 1 : startdate.year
|
||
|
end
|
||
|
|
||
|
def recalculate_start_year!
|
||
|
@start_year = nil
|
||
|
collection_start_year
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def form_name
|
||
3 years ago
|
return unless startdate
|
||
3 years ago
|
|
||
3 years ago
|
"#{collection_start_year}_#{collection_start_year + 1}"
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def self.editable_fields
|
||
|
attribute_names - AUTOGENERATED_FIELDS
|
||
|
end
|
||
|
|
||
2 years ago
|
def la
|
||
|
if location
|
||
|
location.location_code
|
||
|
else
|
||
|
super
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def postcode_full
|
||
|
if location
|
||
|
location.postcode
|
||
|
else
|
||
|
super
|
||
|
end
|
||
|
end
|
||
|
|
||
2 years ago
|
def postcode_full=(postcode)
|
||
|
if postcode
|
||
|
super UKPostcode.parse(postcode).to_s
|
||
|
else
|
||
|
super nil
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def ppostcode_full=(postcode)
|
||
|
if postcode
|
||
|
super UKPostcode.parse(postcode).to_s
|
||
|
else
|
||
|
super nil
|
||
|
end
|
||
|
end
|
||
|
|
||
3 years ago
|
def completed?
|
||
|
status == "completed"
|
||
|
end
|
||
|
|
||
|
def not_started?
|
||
3 years ago
|
status == "not_started"
|
||
3 years ago
|
end
|
||
|
|
||
|
def in_progress?
|
||
3 years ago
|
status == "in_progress"
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def weekly_net_income
|
||
3 years ago
|
return unless earnings && incfreq
|
||
|
|
||
3 years ago
|
if net_income_is_weekly?
|
||
3 years ago
|
earnings
|
||
3 years ago
|
elsif net_income_is_monthly?
|
||
3 years ago
|
((earnings * 12) / 52.0).round(0)
|
||
3 years ago
|
elsif net_income_is_yearly?
|
||
3 years ago
|
(earnings / 52.0).round(0)
|
||
3 years ago
|
end
|
||
|
end
|
||
|
|
||
3 years ago
|
def weekly_value(field_value)
|
||
|
num_of_weeks = NUM_OF_WEEKS_FROM_PERIOD[period]
|
||
|
return unless field_value && num_of_weeks
|
||
|
|
||
3 years ago
|
(field_value / 52 * num_of_weeks).round(2)
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def weekly_to_value_per_period(field_value)
|
||
|
num_of_weeks = NUM_OF_WEEKS_FROM_PERIOD[period]
|
||
|
|
||
|
((field_value * 52) / num_of_weeks).round(2)
|
||
|
end
|
||
|
|
||
3 years ago
|
def applicable_income_range
|
||
3 years ago
|
return unless ecstat1
|
||
3 years ago
|
|
||
3 years ago
|
ALLOWED_INCOME_RANGES[ecstat1]
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def first_time_property_let_as_social_housing?
|
||
3 years ago
|
first_time_property_let_as_social_housing == 1
|
||
|
end
|
||
|
|
||
|
def net_income_refused?
|
||
3 years ago
|
# 2: Tenant prefers not to say
|
||
3 years ago
|
net_income_known == 2
|
||
|
end
|
||
|
|
||
|
def net_income_is_weekly?
|
||
3 years ago
|
# 1: Weekly
|
||
|
!!(incfreq && incfreq == 1)
|
||
3 years ago
|
end
|
||
|
|
||
|
def net_income_is_monthly?
|
||
3 years ago
|
# 2: Monthly
|
||
|
incfreq == 2
|
||
3 years ago
|
end
|
||
|
|
||
|
def net_income_is_yearly?
|
||
3 years ago
|
# 3: Yearly
|
||
|
incfreq == 3
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def net_income_soft_validation_triggered?
|
||
|
net_income_in_soft_min_range? || net_income_in_soft_max_range?
|
||
|
end
|
||
|
|
||
3 years ago
|
def given_reasonable_preference?
|
||
3 years ago
|
# 1: Yes
|
||
3 years ago
|
reasonpref == 1
|
||
3 years ago
|
end
|
||
|
|
||
|
def is_renewal?
|
||
3 years ago
|
# 1: Yes
|
||
3 years ago
|
renewal == 1
|
||
|
end
|
||
|
|
||
|
def is_general_needs?
|
||
3 years ago
|
# 1: General Needs
|
||
3 years ago
|
needstype == 1
|
||
|
end
|
||
|
|
||
3 years ago
|
def is_supported_housing?
|
||
3 years ago
|
# 2: Supported Housing
|
||
|
needstype == 2
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def has_hbrentshortfall?
|
||
3 years ago
|
# 1: Yes
|
||
|
hbrentshortfall == 1
|
||
3 years ago
|
end
|
||
|
|
||
|
def postcode_known?
|
||
3 years ago
|
# 1: Yes
|
||
3 years ago
|
postcode_known == 1
|
||
|
end
|
||
|
|
||
|
def previous_postcode_known?
|
||
3 years ago
|
# 1: Yes
|
||
3 years ago
|
ppcodenk == 1
|
||
3 years ago
|
end
|
||
|
|
||
|
def previous_la_known?
|
||
3 years ago
|
# 1: Yes
|
||
3 years ago
|
previous_la_known == 1
|
||
|
end
|
||
|
|
||
3 years ago
|
def tshortfall_unknown?
|
||
|
tshortfall_known == 1
|
||
|
end
|
||
|
|
||
3 years ago
|
def is_fixed_term_tenancy?
|
||
|
[4, 6].include?(tenancy)
|
||
|
end
|
||
|
|
||
3 years ago
|
def is_secure_tenancy?
|
||
3 years ago
|
return unless collection_start_year
|
||
|
|
||
3 years ago
|
# 1: Secure (including flexible)
|
||
3 years ago
|
if collection_start_year < 2022
|
||
|
tenancy == 1
|
||
|
else
|
||
|
# 6: Secure - fixed term, 7: Secure - lifetime
|
||
|
[6, 7].include?(tenancy)
|
||
|
end
|
||
3 years ago
|
end
|
||
|
|
||
|
def is_assured_shorthold_tenancy?
|
||
3 years ago
|
# 4: Assured Shorthold
|
||
|
tenancy == 4
|
||
3 years ago
|
end
|
||
|
|
||
|
def is_internal_transfer?
|
||
3 years ago
|
# 1: Internal Transfer
|
||
3 years ago
|
referral == 1
|
||
3 years ago
|
end
|
||
|
|
||
|
def is_relet_to_temp_tenant?
|
||
3 years ago
|
# 9: Re-let to tenant who occupied same property as temporary accommodation
|
||
|
rsnvac == 9
|
||
3 years ago
|
end
|
||
|
|
||
|
def is_bedsit?
|
||
3 years ago
|
# 2: Bedsit
|
||
|
unittype_gn == 2
|
||
3 years ago
|
end
|
||
|
|
||
|
def is_shared_housing?
|
||
3 years ago
|
# 4: Shared flat or maisonette
|
||
|
# 9: Shared house
|
||
|
# 10: Shared bungalow
|
||
|
[4, 9, 10].include?(unittype_gn)
|
||
3 years ago
|
end
|
||
|
|
||
|
def has_first_let_vacancy_reason?
|
||
3 years ago
|
# 15: First let of new-build property
|
||
|
# 16: First let of conversion, rehabilitation or acquired property
|
||
|
# 17: First let of leased property
|
||
3 years ago
|
[15, 16, 17].include?(rsnvac)
|
||
3 years ago
|
end
|
||
|
|
||
|
def previous_tenancy_was_temporary?
|
||
3 years ago
|
# 4: Tied housing or renting with job
|
||
|
# 6: Supported housing
|
||
3 years ago
|
# 8: Sheltered accomodation (<= 21/22)
|
||
3 years ago
|
# 24: Housed by National Asylum Support Service (prev Home Office)
|
||
|
# 25: Other
|
||
3 years ago
|
# 34: Specialist retirement housing
|
||
|
# 35: Extra care housing
|
||
|
![4, 6, 8, 24, 25, 34, 35].include?(prevten)
|
||
3 years ago
|
end
|
||
|
|
||
|
def armed_forces_regular?
|
||
3 years ago
|
# 1: Yes – the person is a current or former regular
|
||
|
!!(armedforces && armedforces == 1)
|
||
3 years ago
|
end
|
||
|
|
||
|
def armed_forces_no?
|
||
3 years ago
|
# 2: No
|
||
|
armedforces == 2
|
||
3 years ago
|
end
|
||
|
|
||
|
def armed_forces_refused?
|
||
3 years ago
|
# 3: Person prefers not to say / Refused
|
||
|
armedforces == 3
|
||
3 years ago
|
end
|
||
|
|
||
|
def has_pregnancy?
|
||
3 years ago
|
# 1: Yes
|
||
|
!!(preg_occ && preg_occ == 1)
|
||
3 years ago
|
end
|
||
|
|
||
|
def pregnancy_refused?
|
||
3 years ago
|
# 3: Tenant prefers not to say / Refused
|
||
|
preg_occ == 3
|
||
3 years ago
|
end
|
||
|
|
||
|
def is_assessed_homeless?
|
||
3 years ago
|
# 11: Assessed as homeless (or threatened with homelessness within 56 days) by a local authority and owed a homelessness duty
|
||
3 years ago
|
homeless == 11
|
||
3 years ago
|
end
|
||
|
|
||
|
def is_not_homeless?
|
||
3 years ago
|
# 1: No
|
||
3 years ago
|
homeless == 1
|
||
3 years ago
|
end
|
||
|
|
||
|
def is_london_rent?
|
||
3 years ago
|
# 2: London Affordable Rent
|
||
|
# 4: London Living Rent
|
||
3 years ago
|
rent_type == 2 || rent_type == 4
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def previous_tenancy_was_foster_care?
|
||
3 years ago
|
# 13: Children's home or foster care
|
||
3 years ago
|
prevten == 13
|
||
|
end
|
||
|
|
||
|
def previous_tenancy_was_refuge?
|
||
3 years ago
|
# 21: Refuge
|
||
3 years ago
|
prevten == 21
|
||
|
end
|
||
3 years ago
|
|
||
3 years ago
|
def is_reason_permanently_decanted?
|
||
3 years ago
|
# 1: Permanently decanted from another property owned by this landlord
|
||
3 years ago
|
reason == 1
|
||
|
end
|
||
|
|
||
3 years ago
|
def receives_housing_benefit_only?
|
||
3 years ago
|
# 1: Housing benefit
|
||
3 years ago
|
hb == 1
|
||
|
end
|
||
|
|
||
3 years ago
|
def benefits_unknown?
|
||
|
hb == 3
|
||
|
end
|
||
|
|
||
|
# Option 8 has been removed starting from 22/23
|
||
3 years ago
|
def receives_housing_benefit_and_universal_credit?
|
||
3 years ago
|
# 8: Housing benefit and Universal Credit (without housing element)
|
||
3 years ago
|
hb == 8
|
||
|
end
|
||
|
|
||
|
def receives_uc_with_housing_element_excl_housing_benefit?
|
||
3 years ago
|
# 6: Universal Credit with housing element (excluding housing benefit)
|
||
3 years ago
|
hb == 6
|
||
|
end
|
||
|
|
||
|
def receives_no_benefits?
|
||
3 years ago
|
# 9: None
|
||
3 years ago
|
hb == 9
|
||
|
end
|
||
|
|
||
3 years ago
|
def tenant_refuses_to_say_benefits?
|
||
|
hb == 10
|
||
|
end
|
||
|
|
||
|
# Option 7 has been removed starting from 22/23
|
||
3 years ago
|
def receives_universal_credit_but_no_housing_benefit?
|
||
3 years ago
|
# 7: Universal Credit (without housing element)
|
||
3 years ago
|
hb == 7
|
||
|
end
|
||
|
|
||
3 years ago
|
def ethnic_refused?
|
||
|
ethnic_group == 17
|
||
|
end
|
||
|
|
||
3 years ago
|
def receives_housing_related_benefits?
|
||
3 years ago
|
if collection_start_year <= 2021
|
||
|
receives_housing_benefit_only? || receives_uc_with_housing_element_excl_housing_benefit? ||
|
||
|
receives_housing_benefit_and_universal_credit?
|
||
|
else
|
||
|
receives_housing_benefit_only? || receives_uc_with_housing_element_excl_housing_benefit?
|
||
|
end
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def local_housing_referral?
|
||
3 years ago
|
# 3: PRP lettings only - Nominated by local housing authority
|
||
3 years ago
|
referral == 3
|
||
|
end
|
||
|
|
||
3 years ago
|
def is_prevten_la_general_needs?
|
||
3 years ago
|
# 30: Fixed term Local Authority General Needs tenancy
|
||
|
# 31: Lifetime Local Authority General Needs tenancy
|
||
3 years ago
|
[30, 31].any?(prevten)
|
||
|
end
|
||
|
|
||
2 years ago
|
def owning_organisation_name
|
||
|
owning_organisation&.name
|
||
|
end
|
||
|
|
||
|
def managing_organisation_name
|
||
|
managing_organisation&.name
|
||
|
end
|
||
|
|
||
|
def created_by_name
|
||
|
created_by&.name
|
||
|
end
|
||
|
|
||
2 years ago
|
def is_dpo
|
||
|
created_by&.is_dpo
|
||
2 years ago
|
end
|
||
|
|
||
2 years ago
|
delegate :service_name, :sensitive, :registered_under_care_act, :primary_client_group, :has_other_client_group, :secondary_client_group, :owning_organisation, :managing_organisation, :support_type, :intended_stay, :created_at, prefix: "scheme", to: :scheme, allow_nil: true
|
||
|
delegate :scheme_type, to: :scheme, allow_nil: true
|
||
|
|
||
|
def scheme_code
|
||
|
scheme&.id ? "S#{scheme.id}" : nil
|
||
|
end
|
||
|
|
||
|
def scheme_owning_organisation_name
|
||
|
scheme_owning_organisation&.name
|
||
|
end
|
||
|
|
||
|
def scheme_managing_organisation_name
|
||
|
scheme_managing_organisation&.name
|
||
|
end
|
||
|
|
||
|
delegate :postcode, :name, :units, :type_of_unit, :mobility_type, :startdate, prefix: "location", to: :location, allow_nil: true
|
||
|
delegate :location_admin_district, to: :location, allow_nil: true
|
||
|
|
||
|
# This is not the location_code in the db, location.id is just called code in the UI
|
||
|
def location_code
|
||
|
location&.id
|
||
|
end
|
||
|
|
||
2 years ago
|
def self.to_csv(user = nil)
|
||
2 years ago
|
Csv::LettingsLogCsvService.new(user).to_csv
|
||
2 years ago
|
end
|
||
|
|
||
3 years ago
|
def soft_min_for_period
|
||
|
soft_min = LaRentRange.find_by(start_year: collection_start_year, la:, beds:, lettype:).soft_min
|
||
|
"#{soft_value_for_period(soft_min)} #{SUFFIX_FROM_PERIOD[period].presence || 'every week'}"
|
||
|
end
|
||
|
|
||
|
def soft_max_for_period
|
||
|
soft_max = LaRentRange.find_by(start_year: collection_start_year, la:, beds:, lettype:).soft_max
|
||
|
"#{soft_value_for_period(soft_max)} #{SUFFIX_FROM_PERIOD[period].presence || 'every week'}"
|
||
|
end
|
||
|
|
||
3 years ago
|
def optional_fields
|
||
|
OPTIONAL_FIELDS + dynamically_not_required
|
||
|
end
|
||
|
|
||
3 years ago
|
(1..8).each do |person_num|
|
||
|
define_method("retirement_age_for_person_#{person_num}") do
|
||
|
retirement_age_for_person(person_num)
|
||
|
end
|
||
|
|
||
|
define_method("plural_gender_for_person_#{person_num}") do
|
||
|
plural_gender_for_person(person_num)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def retirement_age_for_person(person_num)
|
||
|
gender = public_send("sex#{person_num}".to_sym)
|
||
|
return unless gender
|
||
|
|
||
|
RETIREMENT_AGES[gender]
|
||
|
end
|
||
|
|
||
|
def plural_gender_for_person(person_num)
|
||
|
gender = public_send("sex#{person_num}".to_sym)
|
||
|
return unless gender
|
||
|
|
||
|
if %w[M X].include?(gender)
|
||
3 years ago
|
"male and non-binary people"
|
||
3 years ago
|
elsif gender == "F"
|
||
3 years ago
|
"females"
|
||
3 years ago
|
end
|
||
|
end
|
||
|
|
||
3 years ago
|
def age_known?(person_num)
|
||
|
return false unless person_num.is_a?(Integer)
|
||
|
|
||
|
!!public_send("age#{person_num}_known")&.zero?
|
||
|
end
|
||
|
|
||
|
def age_unknown?(person_num)
|
||
|
return false unless person_num.is_a?(Integer)
|
||
|
|
||
|
public_send("age#{person_num}_known") == 1
|
||
|
end
|
||
|
|
||
2 years ago
|
def unittype_sh
|
||
|
location.type_of_unit_before_type_cast if location
|
||
|
end
|
||
|
|
||
3 years ago
|
private
|
||
3 years ago
|
|
||
2 years ago
|
PIO = PostcodeService.new
|
||
3 years ago
|
|
||
3 years ago
|
def update_status!
|
||
3 years ago
|
self.status = if all_fields_completed? && errors.empty?
|
||
3 years ago
|
"completed"
|
||
|
elsif all_fields_nil?
|
||
|
"not_started"
|
||
|
else
|
||
|
"in_progress"
|
||
|
end
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def reset_not_routed_questions
|
||
3 years ago
|
enabled_questions = form.enabled_page_questions(self)
|
||
|
enabled_question_ids = enabled_questions.map(&:id)
|
||
|
|
||
3 years ago
|
form.invalidated_page_questions(self).each do |question|
|
||
3 years ago
|
if %w[radio checkbox].include?(question.type)
|
||
|
enabled_answer_options = enabled_question_ids.include?(question.id) ? enabled_questions.find { |q| q.id == question.id }.answer_options : {}
|
||
|
current_answer_option_valid = enabled_answer_options.present? ? enabled_answer_options.key?(public_send(question.id).to_s) : false
|
||
3 years ago
|
if !current_answer_option_valid && respond_to?(question.id.to_s)
|
||
3 years ago
|
Rails.logger.debug("Cleared #{question.id} value")
|
||
3 years ago
|
public_send("#{question.id}=", nil)
|
||
|
else
|
||
|
(question.answer_options.keys - enabled_answer_options.keys).map do |invalid_answer_option|
|
||
3 years ago
|
Rails.logger.debug("Cleared #{invalid_answer_option} value")
|
||
3 years ago
|
public_send("#{invalid_answer_option}=", nil) if respond_to?(invalid_answer_option)
|
||
|
end
|
||
|
end
|
||
3 years ago
|
else
|
||
3 years ago
|
Rails.logger.debug("Cleared #{question.id} value")
|
||
3 years ago
|
public_send("#{question.id}=", nil) unless enabled_question_ids.include?(question.id)
|
||
3 years ago
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def reset_derived_questions
|
||
3 years ago
|
dependent_questions = { waityear: [{ key: :renewal, value: 0 }],
|
||
3 years ago
|
referral: [{ key: :renewal, value: 0 }],
|
||
2 years ago
|
underoccupation_benefitcap: [{ key: :renewal, value: 0 }],
|
||
|
wchair: [{ key: :needstype, value: 1 }],
|
||
|
location_id: [{ key: :needstype, value: 1 }] }
|
||
3 years ago
|
|
||
|
dependent_questions.each do |dependent, conditions|
|
||
|
condition_key = conditions.first[:key]
|
||
|
condition_value = conditions.first[:value]
|
||
|
if public_send("#{condition_key}_changed?") && condition_value == public_send(condition_key) && !public_send("#{dependent}_changed?")
|
||
3 years ago
|
Rails.logger.debug("Cleared derived #{dependent} value")
|
||
3 years ago
|
self[dependent] = nil
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
3 years ago
|
def reset_created_by
|
||
|
return unless created_by && owning_organisation
|
||
|
|
||
|
self.created_by = nil if created_by.organisation != owning_organisation
|
||
|
end
|
||
|
|
||
2 years ago
|
def reset_scheme
|
||
|
return unless scheme && owning_organisation
|
||
|
|
||
|
self.scheme = nil if scheme.owning_organisation != owning_organisation
|
||
|
end
|
||
|
|
||
3 years ago
|
def reset_invalidated_dependent_fields!
|
||
3 years ago
|
return unless form
|
||
|
|
||
3 years ago
|
reset_created_by
|
||
2 years ago
|
reset_scheme
|
||
3 years ago
|
reset_not_routed_questions
|
||
|
reset_derived_questions
|
||
3 years ago
|
end
|
||
|
|
||
|
def dynamically_not_required
|
||
3 years ago
|
not_required = []
|
||
|
not_required << "previous_la_known" if postcode_known?
|
||
|
not_required << "tshortfall" if tshortfall_unknown?
|
||
|
not_required << "tenancylength" if tenancylength_optional?
|
||
|
|
||
|
not_required
|
||
|
end
|
||
|
|
||
|
def tenancylength_optional?
|
||
|
return false unless collection_start_year
|
||
|
return true if collection_start_year < 2022
|
||
|
|
||
|
collection_start_year >= 2022 && !is_fixed_term_tenancy?
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def age_under_16?(person_num)
|
||
|
public_send("age#{person_num}") && public_send("age#{person_num}") < 16
|
||
|
end
|
||
|
|
||
3 years ago
|
def process_postcode_changes!
|
||
3 years ago
|
self.postcode_full = upcase_and_remove_whitespace(postcode_full)
|
||
3 years ago
|
process_postcode(postcode_full, "postcode_known", "is_la_inferred", "la")
|
||
3 years ago
|
end
|
||
|
|
||
|
def process_previous_postcode_changes!
|
||
3 years ago
|
self.ppostcode_full = upcase_and_remove_whitespace(ppostcode_full)
|
||
3 years ago
|
process_postcode(ppostcode_full, "ppcodenk", "is_previous_la_inferred", "prevloc")
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def process_postcode(postcode, postcode_known_key, la_inferred_key, la_key)
|
||
3 years ago
|
return if postcode.blank?
|
||
|
|
||
3 years ago
|
self[postcode_known_key] = 1
|
||
3 years ago
|
inferred_la = get_inferred_la(postcode)
|
||
|
self[la_inferred_key] = inferred_la.present?
|
||
|
self[la_key] = inferred_la if inferred_la.present?
|
||
3 years ago
|
end
|
||
|
|
||
|
def reset_location_fields!
|
||
3 years ago
|
reset_location(is_la_inferred, "la", "is_la_inferred", "postcode_full", 1)
|
||
3 years ago
|
end
|
||
|
|
||
|
def reset_previous_location_fields!
|
||
3 years ago
|
reset_location(is_previous_la_inferred, "prevloc", "is_previous_la_inferred", "ppostcode_full", previous_la_known)
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def reset_location(is_inferred, la_key, is_inferred_key, postcode_key, is_la_known)
|
||
3 years ago
|
if is_inferred || is_la_known != 1
|
||
3 years ago
|
self[la_key] = nil
|
||
3 years ago
|
end
|
||
3 years ago
|
self[is_inferred_key] = false
|
||
|
self[postcode_key] = nil
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def get_inferred_la(postcode)
|
||
2 years ago
|
result = PIO.lookup(postcode)
|
||
|
result[:location_code] if result
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def get_has_benefits
|
||
3 years ago
|
HAS_BENEFITS_OPTIONS.include?(hb) ? 1 : 0
|
||
|
end
|
||
|
|
||
|
def get_lettype
|
||
2 years ago
|
return unless rent_type.present? && needstype.present? && owning_organisation.present? && owning_organisation[:provider_type].present?
|
||
3 years ago
|
|
||
2 years ago
|
case RENT_TYPE_MAPPING_LABELS[RENT_TYPE_MAPPING[rent_type]]
|
||
3 years ago
|
when "Social Rent"
|
||
3 years ago
|
if is_supported_housing?
|
||
3 years ago
|
owning_organisation[:provider_type] == "PRP" ? 2 : 4
|
||
3 years ago
|
elsif is_general_needs?
|
||
3 years ago
|
owning_organisation[:provider_type] == "PRP" ? 1 : 3
|
||
|
end
|
||
|
when "Affordable Rent"
|
||
3 years ago
|
if is_supported_housing?
|
||
3 years ago
|
owning_organisation[:provider_type] == "PRP" ? 6 : 8
|
||
3 years ago
|
elsif is_general_needs?
|
||
3 years ago
|
owning_organisation[:provider_type] == "PRP" ? 5 : 7
|
||
|
end
|
||
|
when "Intermediate Rent"
|
||
3 years ago
|
if is_supported_housing?
|
||
3 years ago
|
owning_organisation[:provider_type] == "PRP" ? 10 : 12
|
||
3 years ago
|
elsif is_general_needs?
|
||
3 years ago
|
owning_organisation[:provider_type] == "PRP" ? 9 : 11
|
||
|
end
|
||
|
end
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def all_fields_completed?
|
||
3 years ago
|
subsection_statuses = form.subsections.map { |subsection| subsection.status(self) }.uniq
|
||
|
subsection_statuses == [:completed]
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def all_fields_nil?
|
||
3 years ago
|
not_started_statuses = %i[not_started cannot_start_yet]
|
||
|
subsection_statuses = form.subsections.map { |subsection| subsection.status(self) }.uniq
|
||
|
subsection_statuses.all? { |status| not_started_statuses.include?(status) }
|
||
3 years ago
|
end
|
||
3 years ago
|
|
||
|
def age_refused?
|
||
|
[age1_known, age2_known, age3_known, age4_known, age5_known, age6_known, age7_known, age8_known].any?(1)
|
||
|
end
|
||
|
|
||
|
def sex_refused?
|
||
|
[sex1, sex2, sex3, sex4, sex5, sex6, sex7, sex8].any?("R")
|
||
|
end
|
||
|
|
||
|
def relat_refused?
|
||
3 years ago
|
[relat2, relat3, relat4, relat5, relat6, relat7, relat8].any?("R")
|
||
3 years ago
|
end
|
||
|
|
||
|
def ecstat_refused?
|
||
|
[ecstat1, ecstat2, ecstat3, ecstat4, ecstat5, ecstat6, ecstat7, ecstat8].any?(10)
|
||
|
end
|
||
3 years ago
|
|
||
|
def soft_value_for_period(value)
|
||
|
num_of_weeks = NUM_OF_WEEKS_FROM_PERIOD[period]
|
||
|
return "" unless value && num_of_weeks
|
||
|
|
||
|
(value * 52 / num_of_weeks).round(2)
|
||
|
end
|
||
3 years ago
|
|
||
|
def upcase_and_remove_whitespace(string)
|
||
|
string.present? ? string.upcase.gsub(/\s+/, "") : string
|
||
|
end
|
||
2 years ago
|
|
||
|
def fully_wheelchair_accessible?
|
||
|
housingneeds_type.present? && housingneeds_type.zero?
|
||
|
end
|
||
|
|
||
|
def essential_wheelchair_access?
|
||
|
housingneeds_type == 1
|
||
|
end
|
||
|
|
||
|
def level_access_housing?
|
||
|
housingneeds_type == 2
|
||
|
end
|
||
|
|
||
|
def other_housingneeds?
|
||
|
housingneeds_other == 1
|
||
|
end
|
||
|
|
||
|
def has_housingneeds?
|
||
|
housingneeds == 1
|
||
|
end
|
||
|
|
||
|
def no_housingneeds?
|
||
|
housingneeds == 2
|
||
|
end
|
||
|
|
||
|
def unknown_housingneeds?
|
||
|
housingneeds == 3
|
||
|
end
|
||
3 years ago
|
end
|