diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 54e63d2cd..1d84850b3 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -61,27 +61,33 @@ 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) - case page - when "edit" - if @location.location_admin_district.nil? - redirect_to(location_edit_local_authority_path(id: @scheme.id, location_id: @location.id, add_another_location: location_params[:add_another_location])) - elsif location_params[:add_another_location] == "Yes" - redirect_to(new_location_path(@location.scheme)) - else - redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) - end - when "edit-name" - redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) - when "edit-local-authority" - if params[:add_another_location] == "Yes" - redirect_to(new_location_path(@location.scheme)) - else + 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? + redirect_to(location_edit_local_authority_path(id: @scheme.id, location_id: @location.id, add_another_location: location_params[:add_another_location])) + elsif location_params[:add_another_location] == "Yes" + redirect_to(new_location_path(@location.scheme)) + else + redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) + end + when "edit-name" redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) + when "edit-local-authority" + if params[:add_another_location] == "Yes" + redirect_to(new_location_path(@location.scheme)) + else + redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) + end end + else + render :edit, status: :unprocessable_entity end - else - render :edit, status: :unprocessable_entity end end @@ -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 diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 9e38138eb..7c437f046 100644 --- a/app/models/lettings_log.rb +++ b/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 diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index de7c4ab68..6c18260ae 100644 --- a/app/models/validations/financial_validations.rb +++ b/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 diff --git a/app/models/validations/soft_validations.rb b/app/models/validations/soft_validations.rb index dd3e94b2e..c932e42cb 100644 --- a/app/models/validations/soft_validations.rb +++ b/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