@ -311,6 +311,7 @@ class BulkUpload::Lettings::Year2023::RowParser
validate :validate_no_disabled_needs_conjunction , on : :after_log
validate :validate_no_disabled_needs_conjunction , on : :after_log
validate :validate_dont_know_disabled_needs_conjunction , on : :after_log
validate :validate_dont_know_disabled_needs_conjunction , on : :after_log
validate :validate_no_and_dont_know_disabled_needs_conjunction , on : :after_log
validate :validate_no_and_dont_know_disabled_needs_conjunction , on : :after_log
validate :validate_if_log_already_exists , on : :after_log , if : - > { FeatureToggle . bulk_upload_duplicate_log_check_enabled? }
validate :validate_owning_org_data_given , on : :after_log
validate :validate_owning_org_data_given , on : :after_log
validate :validate_owning_org_exists , on : :after_log
validate :validate_owning_org_exists , on : :after_log
@ -343,9 +344,11 @@ class BulkUpload::Lettings::Year2023::RowParser
end
end
def valid?
def valid?
return @valid if @valid
errors . clear
errors . clear
return true if blank_row?
return @valid = true if blank_row?
super ( :before_log )
super ( :before_log )
before_errors = errors . dup
before_errors = errors . dup
@ -365,7 +368,7 @@ class BulkUpload::Lettings::Year2023::RowParser
end
end
end
end
errors . blank?
@valid = errors . blank?
end
end
def blank_row?
def blank_row?
@ -398,6 +401,12 @@ class BulkUpload::Lettings::Year2023::RowParser
field_14
field_14
end
end
def log_already_exists?
@log_already_exists || = LettingsLog
. where ( status : %w[ not_started in_progress completed ] )
. exists? ( duplicate_check_fields . index_with { | field | log . public_send ( field ) } )
end
private
private
def validate_declaration_acceptance
def validate_declaration_acceptance
@ -448,6 +457,26 @@ private
end
end
end
end
def duplicate_check_fields
%w[
startdate
age1
sex1
ecstat1
owning_organisation
tcharge
propcode
postcode_full
location
]
end
def validate_needs_type_present
if field_4 . blank?
errors . add ( :field_4 , I18n . t ( " validations.not_answered " , question : " needs type " ) )
end
end
def start_date
def start_date
return if field_7 . blank? || field_8 . blank? || field_9 . blank?
return if field_7 . blank? || field_8 . blank? || field_9 . blank?
@ -679,6 +708,26 @@ private
log . form . setup_sections [ 0 ] . subsections [ 0 ] . questions . include? ( question )
log . form . setup_sections [ 0 ] . subsections [ 0 ] . questions . include? ( question )
end
end
def validate_if_log_already_exists
if log_already_exists?
error_message = " This is a duplicate log "
errors . add ( :field_1 , error_message ) # owning_organisation
errors . add ( :field_7 , error_message ) # startdate
errors . add ( :field_8 , error_message ) # startdate
errors . add ( :field_9 , error_message ) # startdate
errors . add ( :field_14 , error_message ) # propcode
errors . add ( :field_17 , error_message ) # location
errors . add ( :field_23 , error_message ) # postcode_full
errors . add ( :field_24 , error_message ) # postcode_full
errors . add ( :field_25 , error_message ) # postcode_full
errors . add ( :field_46 , error_message ) # age1
errors . add ( :field_47 , error_message ) # sex1
errors . add ( :field_50 , error_message ) # ecstat1
errors . add ( :field_132 , error_message ) # tcharge
end
end
def field_mapping_for_errors
def field_mapping_for_errors
{
{
lettype : [ :field_5 ] ,
lettype : [ :field_5 ] ,
@ -855,8 +904,8 @@ private
attributes [ " la " ] = field_25
attributes [ " la " ] = field_25
attributes [ " postcode_known " ] = postcode_known
attributes [ " postcode_known " ] = postcode_known
attributes [ " postcode_full " ] = postcode_full
attributes [ " postcode_full " ] = postcode_full
attributes [ " owning_organisation_id " ] = owning_organisation_id
attributes [ " owning_organisation " ] = owning_organisation
attributes [ " managing_organisation_id " ] = managing_organisation_id
attributes [ " managing_organisation " ] = managing_organisation
attributes [ " renewal " ] = renewal
attributes [ " renewal " ] = renewal
attributes [ " scheme " ] = scheme
attributes [ " scheme " ] = scheme
attributes [ " location " ] = location
attributes [ " location " ] = location
@ -1050,18 +1099,10 @@ private
Organisation . find_by_id_on_multiple_fields ( field_1 )
Organisation . find_by_id_on_multiple_fields ( field_1 )
end
end
def owning_organisation_id
owning_organisation & . id
end
def managing_organisation
def managing_organisation
Organisation . find_by_id_on_multiple_fields ( field_2 )
Organisation . find_by_id_on_multiple_fields ( field_2 )
end
end
def managing_organisation_id
managing_organisation & . id
end
def renewal
def renewal
case field_6
case field_6
when 1
when 1