Browse Source

feat: ensure there is a location_code in all locations and use this (and beds = 0) for supported housing rent validations

pull/959/head
natdeanlewissoftwire 3 years ago
parent
commit
31b94f3642
  1. 10
      app/controllers/locations_controller.rb
  2. 6
      app/models/lettings_log.rb
  3. 6
      app/models/validations/financial_validations.rb
  4. 6
      app/models/validations/soft_validations.rb

10
app/controllers/locations_controller.rb

@ -61,7 +61,12 @@ class LocationsController < ApplicationController
error_message = I18n.t("validations.location_admin_district")
@location.errors.add :location_admin_district, error_message
render :edit_local_authority, status: :unprocessable_entity
elsif @location.update(location_params)
else
if page == "edit-local-authority"
la_list = FormHandler.instance.current_lettings_form.get_question("la", nil).answer_options
params[:location][:location_code] = la_list.key(params[:location][:location_admin_district])
end
if @location.update(location_params)
case page
when "edit"
if @location.location_admin_district.nil?
@ -84,6 +89,7 @@ class LocationsController < ApplicationController
render :edit, status: :unprocessable_entity
end
end
end
private
@ -123,7 +129,7 @@ private
end
def location_params
required_params = params.require(:location).permit(:postcode, :name, :units, :type_of_unit, :add_another_location, :startdate, :mobility_type, :location_admin_district).merge(scheme_id: @scheme.id)
required_params = params.require(:location).permit(:postcode, :name, :units, :type_of_unit, :add_another_location, :startdate, :mobility_type, :location_admin_district, :location_code).merge(scheme_id: @scheme.id)
required_params[:postcode] = PostcodeService.clean(required_params[:postcode]) if required_params[:postcode]
required_params
end

6
app/models/lettings_log.rb

@ -436,11 +436,17 @@ class LettingsLog < Log
end
def soft_min_for_period
beds = needstype == 2 ? 0 : beds
la = needstype == 2 ? Location.find(location_id).location_code : la
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
beds = needstype == 2 ? 0 : beds
la = needstype == 2 ? Location.find(location_id).location_code : la
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

6
app/models/validations/financial_validations.rb

@ -181,7 +181,11 @@ private
return if record.startdate.blank?
collection_year = record.collection_start_year
rent_range = LaRentRange.find_by(start_year: collection_year, la: record.la, beds: record.beds, lettype: record.lettype)
beds = record.needstype == 2 ? 0 : record.beds
la = record.needstype == 2 ? Location.find(record.location_id).location_code : record.la
rent_range = LaRentRange.find_by(start_year: collection_year, la:, beds:, lettype: record.lettype)
if rent_range.present? && !weekly_value_in_range(record, "brent", rent_range.hard_min, rent_range.hard_max) && record.brent.present? && record.period.present?
if record.weekly_value(record["brent"]) < rent_range.hard_min

6
app/models/validations/soft_validations.rb

@ -28,6 +28,9 @@ module Validations::SoftValidations
def rent_in_soft_min_range?
return unless brent && weekly_value(brent) && startdate
beds = needstype == 2 ? 0 : beds
la = needstype == 2 ? Location.find(location_id).location_code : la
rent_range = LaRentRange.find_by(start_year: collection_start_year, la:, beds:, lettype: get_lettype)
rent_range.present? && weekly_value(brent).between?(rent_range.hard_min, rent_range.soft_min)
end
@ -35,6 +38,9 @@ module Validations::SoftValidations
def rent_in_soft_max_range?
return unless brent && weekly_value(brent) && startdate
beds = needstype == 2 ? 0 : beds
la = needstype == 2 ? Location.find(location_id).location_code : la
rent_range = LaRentRange.find_by(start_year: collection_start_year, la:, beds:, lettype: get_lettype)
rent_range.present? && weekly_value(brent).between?(rent_range.soft_max, rent_range.hard_max)
end

Loading…
Cancel
Save