From edecfde51bd9b7356ac6d45e90ef853fd25688c0 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:17:07 +0100 Subject: [PATCH] CLDC-2967 Infer lar and irproduct values (#2005) * Infer lar and irproduct * Add recalculate lar values task * Create irproduct task * Tests * typo --- .../lettings_log_variables.rb | 13 ++ lib/tasks/recalculate_irproduct_values.rake | 18 +++ lib/tasks/recalculate_lar_values.rake | 13 ++ spec/fixtures/exports/general_needs_log.xml | 2 +- .../exports/general_needs_log_23_24.xml | 2 +- .../exports/supported_housing_logs.xml | 2 +- .../files/lettings_log_csv_export_codes.csv | 2 +- .../files/lettings_log_csv_export_labels.csv | 2 +- ...tings_log_csv_export_non_support_codes.csv | 2 +- ...ings_log_csv_export_non_support_labels.csv | 2 +- .../recalculate_irproduct_values_spec.rb | 132 ++++++++++++++++++ spec/lib/tasks/recalculate_lar_values_spec.rb | 100 +++++++++++++ spec/models/lettings_log_spec.rb | 52 +++++++ 13 files changed, 335 insertions(+), 7 deletions(-) create mode 100644 lib/tasks/recalculate_irproduct_values.rake create mode 100644 lib/tasks/recalculate_lar_values.rake create mode 100644 spec/lib/tasks/recalculate_irproduct_values_spec.rb create mode 100644 spec/lib/tasks/recalculate_lar_values_spec.rb diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index 9679a9839..7d1274540 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -45,6 +45,8 @@ module DerivedVariables::LettingsLogVariables end self.renttype = RENT_TYPE_MAPPING[rent_type] self.lettype = get_lettype + self.lar = get_lar + self.irproduct = get_irproduct self.totchild = get_totchild self.totelder = get_totelder self.totadult = get_totadult @@ -307,4 +309,15 @@ private self.town_or_city = nil self.county = nil end + + def get_lar + return 1 if rent_type == 2 + return 2 if rent_type == 1 + end + + def get_irproduct + return 1 if rent_type == 3 + return 2 if rent_type == 4 + return 3 if rent_type == 5 + end end diff --git a/lib/tasks/recalculate_irproduct_values.rake b/lib/tasks/recalculate_irproduct_values.rake new file mode 100644 index 000000000..751eae432 --- /dev/null +++ b/lib/tasks/recalculate_irproduct_values.rake @@ -0,0 +1,18 @@ +desc "Forces to recalculate irproduct values" +task recalculate_irproduct_values: :environment do + LettingsLog.exportable.where(rent_type: [3, 4, 5]).where(irproduct: nil).each do |log| # irproduct was never set + Rails.logger.info("Could not update irproduct for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) + end + LettingsLog.exportable.where(rent_type: 3).where.not(irproduct: 1).each do |log| # irproduct was set wrong + Rails.logger.info("Could not update irproduct for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) + end + LettingsLog.exportable.where(rent_type: 4).where.not(irproduct: 2).each do |log| # irproduct was set wrong + Rails.logger.info("Could not update irproduct for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) + end + LettingsLog.exportable.where(rent_type: 5).where.not(irproduct: 3).each do |log| # irproduct was set wrong + Rails.logger.info("Could not update irproduct for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) + end + LettingsLog.exportable.where.not(rent_type: [3, 4, 5]).where.not(irproduct: nil).each do |log| # irproduct was set when it should have been nil + Rails.logger.info("Could not update irproduct for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) + end +end diff --git a/lib/tasks/recalculate_lar_values.rake b/lib/tasks/recalculate_lar_values.rake new file mode 100644 index 000000000..f08a38726 --- /dev/null +++ b/lib/tasks/recalculate_lar_values.rake @@ -0,0 +1,13 @@ +desc "Forces to recalculate lar values for affordable rent types and clears irrelevant lar values" +task recalculate_lar_values: :environment do + LettingsLog.exportable.where(rent_type: [1, 2], lar: nil).each do |log| # lar was never set + Rails.logger.info("Could not update lar for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) + end + LettingsLog.exportable.where(rent_type: 1).where.not(lar: 2).each do |log| # lar was set wrong + Rails.logger.info("Could not update lar for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) + end + LettingsLog.exportable.where(rent_type: 2).where.not(lar: 1).each do |log| # lar was set wrong + Rails.logger.info("Could not update lar for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) + end + LettingsLog.exportable.where.not(rent_type: [1, 2]).where.not(lar: nil).update_all(lar: nil) # lar was set to 2 but should never have been set +end diff --git a/spec/fixtures/exports/general_needs_log.xml b/spec/fixtures/exports/general_needs_log.xml index 27a995304..fe7985668 100644 --- a/spec/fixtures/exports/general_needs_log.xml +++ b/spec/fixtures/exports/general_needs_log.xml @@ -129,7 +129,7 @@ - + 2 4 diff --git a/spec/fixtures/exports/general_needs_log_23_24.xml b/spec/fixtures/exports/general_needs_log_23_24.xml index aa3ca758a..ab4871a94 100644 --- a/spec/fixtures/exports/general_needs_log_23_24.xml +++ b/spec/fixtures/exports/general_needs_log_23_24.xml @@ -129,7 +129,7 @@ - + 2 3 diff --git a/spec/fixtures/exports/supported_housing_logs.xml b/spec/fixtures/exports/supported_housing_logs.xml index 0f7065b62..c05f2c76a 100644 --- a/spec/fixtures/exports/supported_housing_logs.xml +++ b/spec/fixtures/exports/supported_housing_logs.xml @@ -128,7 +128,7 @@ - + 2 1 4 diff --git a/spec/fixtures/files/lettings_log_csv_export_codes.csv b/spec/fixtures/files/lettings_log_csv_export_codes.csv index 1a6292a3b..54ac02412 100644 --- a/spec/fixtures/files/lettings_log_csv_export_codes.csv +++ b/spec/fixtures/files/lettings_log_csv_export_codes.csv @@ -1,2 +1,2 @@ id,status,created_by,is_dpo,created_at,updated_by,updated_at,creation_method,old_id,old_form_id,collection_start_year,owning_organisation_name,managing_organisation_name,needstype,lettype,renewal,startdate,renttype,rent_type_detail,irproduct,irproduct_other,lar,tenancycode,propcode,uprn_known,uprn,uprn_confirmed,address_line1,address_line2,town_or_city,county,postcode_full,is_la_inferred,la_label,la,first_time_property_let_as_social_housing,unitletas,rsnvac,newprop,offered,unittype_gn,builtype,wchair,beds,voiddate,vacdays,void_date_value_check,majorrepairs,mrcdate,major_repairs_date_value_check,joint,startertenancy,tenancy,tenancyother,tenancylength,sheltered,declaration,hhmemb,pregnancy_value_check,refused,hhtype,totchild,totelder,totadult,age1,retirement_value_check,sex1,ethnic_group,ethnic,national,ecstat1,details_known_2,relat2,age2,sex2,ecstat2,details_known_3,relat3,age3,sex3,ecstat3,details_known_4,relat4,age4,sex4,ecstat4,details_known_5,relat5,age5,sex5,ecstat5,details_known_6,relat6,age6,sex6,ecstat6,details_known_7,relat7,age7,sex7,ecstat7,details_known_8,relat8,age8,sex8,ecstat8,armedforces,leftreg,reservist,preg_occ,housingneeds,housingneeds_type,housingneeds_a,housingneeds_b,housingneeds_c,housingneeds_f,housingneeds_g,housingneeds_h,housingneeds_other,illness,illness_type_4,illness_type_5,illness_type_2,illness_type_6,illness_type_7,illness_type_3,illness_type_9,illness_type_8,illness_type_1,illness_type_10,layear,waityear,reason,reasonother,prevten,new_old,homeless,ppcodenk,ppostcode_full,previous_la_known,is_previous_la_inferred,prevloc_label,prevloc,reasonpref,rp_homeless,rp_insan_unsat,rp_medwel,rp_hardship,rp_dontknow,cbl,cap,chr,letting_allocation_unknown,referral,referral_value_check,net_income_known,incref,earnings,incfreq,net_income_value_check,hb,has_benefits,benefits,household_charge,nocharge,period,is_carehome,chcharge,wchchrg,carehome_charges_value_check,brent,wrent,rent_value_check,scharge,wscharge,pscharge,wpschrge,supcharg,wsupchrg,tcharge,wtcharge,scharge_value_check,pscharge_value_check,supcharg_value_check,hbrentshortfall,tshortfall_known,tshortfall,wtshortfall,scheme_code,scheme_service_name,scheme_sensitive,SCHTYPE,scheme_registered_under_care_act,scheme_owning_organisation_name,scheme_primary_client_group,scheme_has_other_client_group,scheme_secondary_client_group,scheme_support_type,scheme_intended_stay,scheme_created_at,location_code,location_postcode,location_name,location_units,location_type_of_unit,location_mobility_type,location_admin_district,location_startdate -,completed,s.port@jeemayle.com,false,2023-06-26T00:00:00+01:00,,2023-06-26T00:00:00+01:00,1,,,2023,DLUHC,DLUHC,1,7,0,2023-06-26,2,1,,,,HIJKLMN,ABCDEFG,0,,,fake address,,London,,NW9 5LL,false,Barnet,E09000003,0,2,6,2,2,7,1,1,3,2023-06-24,,,1,2023-06-25,,3,1,4,,2,,1,4,,1,4,0,0,2,35,,F,0,2,13,0,0,P,32,M,6,1,R,-9,R,10,0,R,-9,R,10,,,,,,,,,,,,,,,,,,,,,1,4,1,2,1,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,2,7,4,,6,2,1,0,TN23 6LZ,1,false,Ashford,E07000105,1,0,1,0,0,0,0,0,1,,2,,0,0,68,1,,6,1,1,,0,2,,,,,200.0,100.0,,50.0,25.0,40.0,20.0,35.0,17.5,325.0,162.5,,,,1,0,12.0,6.0,,,,,,,,,,,,,,,,,,,, +,completed,s.port@jeemayle.com,false,2023-06-26T00:00:00+01:00,,2023-06-26T00:00:00+01:00,1,,,2023,DLUHC,DLUHC,1,7,0,2023-06-26,2,1,,,2,HIJKLMN,ABCDEFG,0,,,fake address,,London,,NW9 5LL,false,Barnet,E09000003,0,2,6,2,2,7,1,1,3,2023-06-24,,,1,2023-06-25,,3,1,4,,2,,1,4,,1,4,0,0,2,35,,F,0,2,13,0,0,P,32,M,6,1,R,-9,R,10,0,R,-9,R,10,,,,,,,,,,,,,,,,,,,,,1,4,1,2,1,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,2,7,4,,6,2,1,0,TN23 6LZ,1,false,Ashford,E07000105,1,0,1,0,0,0,0,0,1,,2,,0,0,68,1,,6,1,1,,0,2,,,,,200.0,100.0,,50.0,25.0,40.0,20.0,35.0,17.5,325.0,162.5,,,,1,0,12.0,6.0,,,,,,,,,,,,,,,,,,,, diff --git a/spec/fixtures/files/lettings_log_csv_export_labels.csv b/spec/fixtures/files/lettings_log_csv_export_labels.csv index fd93b4015..8d9a2d240 100644 --- a/spec/fixtures/files/lettings_log_csv_export_labels.csv +++ b/spec/fixtures/files/lettings_log_csv_export_labels.csv @@ -1,2 +1,2 @@ id,status,created_by,is_dpo,created_at,updated_by,updated_at,creation_method,old_id,old_form_id,collection_start_year,owning_organisation_name,managing_organisation_name,needstype,lettype,renewal,startdate,renttype,rent_type_detail,irproduct,irproduct_other,lar,tenancycode,propcode,uprn_known,uprn,uprn_confirmed,address_line1,address_line2,town_or_city,county,postcode_full,is_la_inferred,la_label,la,first_time_property_let_as_social_housing,unitletas,rsnvac,newprop,offered,unittype_gn,builtype,wchair,beds,voiddate,vacdays,void_date_value_check,majorrepairs,mrcdate,major_repairs_date_value_check,joint,startertenancy,tenancy,tenancyother,tenancylength,sheltered,declaration,hhmemb,pregnancy_value_check,refused,hhtype,totchild,totelder,totadult,age1,retirement_value_check,sex1,ethnic_group,ethnic,national,ecstat1,details_known_2,relat2,age2,sex2,ecstat2,details_known_3,relat3,age3,sex3,ecstat3,details_known_4,relat4,age4,sex4,ecstat4,details_known_5,relat5,age5,sex5,ecstat5,details_known_6,relat6,age6,sex6,ecstat6,details_known_7,relat7,age7,sex7,ecstat7,details_known_8,relat8,age8,sex8,ecstat8,armedforces,leftreg,reservist,preg_occ,housingneeds,housingneeds_type,housingneeds_a,housingneeds_b,housingneeds_c,housingneeds_f,housingneeds_g,housingneeds_h,housingneeds_other,illness,illness_type_4,illness_type_5,illness_type_2,illness_type_6,illness_type_7,illness_type_3,illness_type_9,illness_type_8,illness_type_1,illness_type_10,layear,waityear,reason,reasonother,prevten,new_old,homeless,ppcodenk,ppostcode_full,previous_la_known,is_previous_la_inferred,prevloc_label,prevloc,reasonpref,rp_homeless,rp_insan_unsat,rp_medwel,rp_hardship,rp_dontknow,cbl,cap,chr,letting_allocation_unknown,referral,referral_value_check,net_income_known,incref,earnings,incfreq,net_income_value_check,hb,has_benefits,benefits,household_charge,nocharge,period,is_carehome,chcharge,wchchrg,carehome_charges_value_check,brent,wrent,rent_value_check,scharge,wscharge,pscharge,wpschrge,supcharg,wsupchrg,tcharge,wtcharge,scharge_value_check,pscharge_value_check,supcharg_value_check,hbrentshortfall,tshortfall_known,tshortfall,wtshortfall,scheme_code,scheme_service_name,scheme_sensitive,SCHTYPE,scheme_registered_under_care_act,scheme_owning_organisation_name,scheme_primary_client_group,scheme_has_other_client_group,scheme_secondary_client_group,scheme_support_type,scheme_intended_stay,scheme_created_at,location_code,location_postcode,location_name,location_units,location_type_of_unit,location_mobility_type,location_admin_district,location_startdate -,completed,s.port@jeemayle.com,false,2023-06-26T00:00:00+01:00,,2023-06-26T00:00:00+01:00,single log,,,2023,DLUHC,DLUHC,General needs,7,No,2023-06-26,2,Affordable Rent,,,,HIJKLMN,ABCDEFG,No,,,fake address,,London,,NW9 5LL,No,Barnet,E09000003,No,Affordable rent basis,Tenant abandoned property,2,2,House,Purpose built,Yes,3,2023-06-24,,,Yes,2023-06-25,,Don’t know,Yes,Assured Shorthold Tenancy (AST) – Fixed term,,2,,1,4,,1,4,0,0,2,35,,Female,White,Irish,Tenant prefers not to say,Other,Yes,Partner,32,Male,Not seeking work,No,Prefers not to say,Not known,Prefers not to say,Prefers not to say,Yes,Person prefers not to say,Not known,Person prefers not to say,Person prefers not to say,,,,,,,,,,,,,,,,,,,,,Yes – the person is a current or former regular,No – they left up to and including 5 years ago,Yes,No,Yes,Fully wheelchair accessible housing,1,0,0,0,0,0,No,Yes,0,0,1,0,0,0,0,0,0,0,Less than 1 year,1 year but under 2 years,Loss of tied accommodation,,Other supported housing,2,No,Yes,TN23 6LZ,Yes,No,Ashford,E07000105,Yes,0,1,0,0,0,0,0,1,,Tenant applied directly (no referral or nomination),,Yes,0,68,Weekly,,Universal Credit housing element,1,All,,0,Every 2 weeks,,,,,200.0,100.0,,50.0,25.0,40.0,20.0,35.0,17.5,325.0,162.5,,,,Yes,Yes,12.0,6.0,,,,,,,,,,,,,,,,,,,, +,completed,s.port@jeemayle.com,false,2023-06-26T00:00:00+01:00,,2023-06-26T00:00:00+01:00,single log,,,2023,DLUHC,DLUHC,General needs,7,No,2023-06-26,2,Affordable Rent,,,2,HIJKLMN,ABCDEFG,No,,,fake address,,London,,NW9 5LL,No,Barnet,E09000003,No,Affordable rent basis,Tenant abandoned property,2,2,House,Purpose built,Yes,3,2023-06-24,,,Yes,2023-06-25,,Don’t know,Yes,Assured Shorthold Tenancy (AST) – Fixed term,,2,,1,4,,1,4,0,0,2,35,,Female,White,Irish,Tenant prefers not to say,Other,Yes,Partner,32,Male,Not seeking work,No,Prefers not to say,Not known,Prefers not to say,Prefers not to say,Yes,Person prefers not to say,Not known,Person prefers not to say,Person prefers not to say,,,,,,,,,,,,,,,,,,,,,Yes – the person is a current or former regular,No – they left up to and including 5 years ago,Yes,No,Yes,Fully wheelchair accessible housing,1,0,0,0,0,0,No,Yes,0,0,1,0,0,0,0,0,0,0,Less than 1 year,1 year but under 2 years,Loss of tied accommodation,,Other supported housing,2,No,Yes,TN23 6LZ,Yes,No,Ashford,E07000105,Yes,0,1,0,0,0,0,0,1,,Tenant applied directly (no referral or nomination),,Yes,0,68,Weekly,,Universal Credit housing element,1,All,,0,Every 2 weeks,,,,,200.0,100.0,,50.0,25.0,40.0,20.0,35.0,17.5,325.0,162.5,,,,Yes,Yes,12.0,6.0,,,,,,,,,,,,,,,,,,,, diff --git a/spec/fixtures/files/lettings_log_csv_export_non_support_codes.csv b/spec/fixtures/files/lettings_log_csv_export_non_support_codes.csv index 20629ded7..5e47f5fe8 100644 --- a/spec/fixtures/files/lettings_log_csv_export_non_support_codes.csv +++ b/spec/fixtures/files/lettings_log_csv_export_non_support_codes.csv @@ -1,2 +1,2 @@ id,status,created_by,is_dpo,created_at,updated_by,updated_at,creation_method,collection_start_year,owning_organisation_name,managing_organisation_name,lettype,renewal,startdate,irproduct,irproduct_other,lar,tenancycode,propcode,uprn_known,uprn,address_line1,address_line2,town_or_city,county,postcode_full,la_label,unitletas,rsnvac,newprop,offered,unittype_gn,builtype,wchair,beds,voiddate,vacdays,void_date_value_check,majorrepairs,mrcdate,major_repairs_date_value_check,joint,startertenancy,tenancy,tenancyother,tenancylength,sheltered,declaration,refused,age1,sex1,ethnic_group,ethnic,national,ecstat1,relat2,age2,sex2,ecstat2,relat3,age3,sex3,ecstat3,relat4,age4,sex4,ecstat4,relat5,age5,sex5,ecstat5,relat6,age6,sex6,ecstat6,relat7,age7,sex7,ecstat7,relat8,age8,sex8,ecstat8,armedforces,leftreg,reservist,preg_occ,housingneeds,housingneeds_type,housingneeds_a,housingneeds_b,housingneeds_c,housingneeds_f,housingneeds_g,housingneeds_h,housingneeds_other,illness,illness_type_4,illness_type_5,illness_type_2,illness_type_6,illness_type_7,illness_type_3,illness_type_9,illness_type_8,illness_type_1,illness_type_10,layear,waityear,reason,reasonother,prevten,homeless,ppcodenk,ppostcode_full,prevloc_label,reasonpref,rp_homeless,rp_insan_unsat,rp_medwel,rp_hardship,rp_dontknow,cbl,cap,chr,referral,referral_value_check,incref,earnings,incfreq,hb,has_benefits,benefits,household_charge,nocharge,period,chcharge,wchchrg,carehome_charges_value_check,brent,scharge,pscharge,supcharg,tcharge,scharge_value_check,pscharge_value_check,supcharg_value_check,hbrentshortfall,tshortfall,scheme_code,scheme_service_name,scheme_sensitive,SCHTYPE,scheme_registered_under_care_act,scheme_owning_organisation_name,scheme_primary_client_group,scheme_has_other_client_group,scheme_secondary_client_group,scheme_support_type,scheme_intended_stay,scheme_created_at,location_code,location_postcode,location_name,location_units,location_type_of_unit,location_mobility_type,location_admin_district,location_startdate -,completed,choreographer@owtluk.com,false,2023-06-26T00:00:00+01:00,,2023-06-26T00:00:00+01:00,1,2023,DLUHC,DLUHC,7,0,2023-06-26,,,,HIJKLMN,ABCDEFG,0,,fake address,,London,,NW9 5LL,Barnet,2,6,2,2,7,1,1,3,2023-06-24,1,,1,2023-06-25,,3,1,4,,2,,1,1,35,F,0,2,13,0,P,32,M,6,R,-9,R,10,R,-9,R,10,,,,,,,,,,,,,,,,,1,4,1,2,1,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,2,7,4,,6,1,0,TN23 6LZ,Ashford,1,0,1,0,0,0,0,0,1,2,,0,68,1,6,1,1,,0,2,,,,200.0,50.0,40.0,35.0,325.0,,,,1,12.0,,,,,,,,,,,,,,,,,,,, +,completed,choreographer@owtluk.com,false,2023-06-26T00:00:00+01:00,,2023-06-26T00:00:00+01:00,1,2023,DLUHC,DLUHC,7,0,2023-06-26,,,2,HIJKLMN,ABCDEFG,0,,fake address,,London,,NW9 5LL,Barnet,2,6,2,2,7,1,1,3,2023-06-24,1,,1,2023-06-25,,3,1,4,,2,,1,1,35,F,0,2,13,0,P,32,M,6,R,-9,R,10,R,-9,R,10,,,,,,,,,,,,,,,,,1,4,1,2,1,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,2,7,4,,6,1,0,TN23 6LZ,Ashford,1,0,1,0,0,0,0,0,1,2,,0,68,1,6,1,1,,0,2,,,,200.0,50.0,40.0,35.0,325.0,,,,1,12.0,,,,,,,,,,,,,,,,,,,, diff --git a/spec/fixtures/files/lettings_log_csv_export_non_support_labels.csv b/spec/fixtures/files/lettings_log_csv_export_non_support_labels.csv index 5fd2c3cf1..57e395cd1 100644 --- a/spec/fixtures/files/lettings_log_csv_export_non_support_labels.csv +++ b/spec/fixtures/files/lettings_log_csv_export_non_support_labels.csv @@ -1,2 +1,2 @@ id,status,created_by,is_dpo,created_at,updated_by,updated_at,creation_method,collection_start_year,owning_organisation_name,managing_organisation_name,lettype,renewal,startdate,irproduct,irproduct_other,lar,tenancycode,propcode,uprn_known,uprn,address_line1,address_line2,town_or_city,county,postcode_full,la_label,unitletas,rsnvac,newprop,offered,unittype_gn,builtype,wchair,beds,voiddate,vacdays,void_date_value_check,majorrepairs,mrcdate,major_repairs_date_value_check,joint,startertenancy,tenancy,tenancyother,tenancylength,sheltered,declaration,refused,age1,sex1,ethnic_group,ethnic,national,ecstat1,relat2,age2,sex2,ecstat2,relat3,age3,sex3,ecstat3,relat4,age4,sex4,ecstat4,relat5,age5,sex5,ecstat5,relat6,age6,sex6,ecstat6,relat7,age7,sex7,ecstat7,relat8,age8,sex8,ecstat8,armedforces,leftreg,reservist,preg_occ,housingneeds,housingneeds_type,housingneeds_a,housingneeds_b,housingneeds_c,housingneeds_f,housingneeds_g,housingneeds_h,housingneeds_other,illness,illness_type_4,illness_type_5,illness_type_2,illness_type_6,illness_type_7,illness_type_3,illness_type_9,illness_type_8,illness_type_1,illness_type_10,layear,waityear,reason,reasonother,prevten,homeless,ppcodenk,ppostcode_full,prevloc_label,reasonpref,rp_homeless,rp_insan_unsat,rp_medwel,rp_hardship,rp_dontknow,cbl,cap,chr,referral,referral_value_check,incref,earnings,incfreq,hb,has_benefits,benefits,household_charge,nocharge,period,chcharge,wchchrg,carehome_charges_value_check,brent,scharge,pscharge,supcharg,tcharge,scharge_value_check,pscharge_value_check,supcharg_value_check,hbrentshortfall,tshortfall,scheme_code,scheme_service_name,scheme_sensitive,SCHTYPE,scheme_registered_under_care_act,scheme_owning_organisation_name,scheme_primary_client_group,scheme_has_other_client_group,scheme_secondary_client_group,scheme_support_type,scheme_intended_stay,scheme_created_at,location_code,location_postcode,location_name,location_units,location_type_of_unit,location_mobility_type,location_admin_district,location_startdate -,completed,choreographer@owtluk.com,false,2023-06-26T00:00:00+01:00,,2023-06-26T00:00:00+01:00,single log,2023,DLUHC,DLUHC,7,No,2023-06-26,,,,HIJKLMN,ABCDEFG,No,,fake address,,London,,NW9 5LL,Barnet,Affordable rent basis,Tenant abandoned property,2,2,House,Purpose built,Yes,3,2023-06-24,1,,Yes,2023-06-25,,Don’t know,Yes,Assured Shorthold Tenancy (AST) – Fixed term,,2,,1,1,35,Female,White,Irish,Tenant prefers not to say,Other,Partner,32,Male,Not seeking work,Prefers not to say,Not known,Prefers not to say,Prefers not to say,Person prefers not to say,Not known,Person prefers not to say,Person prefers not to say,,,,,,,,,,,,,,,,,Yes – the person is a current or former regular,No – they left up to and including 5 years ago,Yes,No,Yes,Fully wheelchair accessible housing,1,0,0,0,0,0,No,Yes,0,0,1,0,0,0,0,0,0,0,Less than 1 year,1 year but under 2 years,Loss of tied accommodation,,Other supported housing,No,Yes,TN23 6LZ,Ashford,Yes,0,1,0,0,0,0,0,1,Tenant applied directly (no referral or nomination),,0,68,Weekly,Universal Credit housing element,1,All,,0,Every 2 weeks,,,,200.0,50.0,40.0,35.0,325.0,,,,Yes,12.0,,,,,,,,,,,,,,,,,,,, +,completed,choreographer@owtluk.com,false,2023-06-26T00:00:00+01:00,,2023-06-26T00:00:00+01:00,single log,2023,DLUHC,DLUHC,7,No,2023-06-26,,,2,HIJKLMN,ABCDEFG,No,,fake address,,London,,NW9 5LL,Barnet,Affordable rent basis,Tenant abandoned property,2,2,House,Purpose built,Yes,3,2023-06-24,1,,Yes,2023-06-25,,Don’t know,Yes,Assured Shorthold Tenancy (AST) – Fixed term,,2,,1,1,35,Female,White,Irish,Tenant prefers not to say,Other,Partner,32,Male,Not seeking work,Prefers not to say,Not known,Prefers not to say,Prefers not to say,Person prefers not to say,Not known,Person prefers not to say,Person prefers not to say,,,,,,,,,,,,,,,,,Yes – the person is a current or former regular,No – they left up to and including 5 years ago,Yes,No,Yes,Fully wheelchair accessible housing,1,0,0,0,0,0,No,Yes,0,0,1,0,0,0,0,0,0,0,Less than 1 year,1 year but under 2 years,Loss of tied accommodation,,Other supported housing,No,Yes,TN23 6LZ,Ashford,Yes,0,1,0,0,0,0,0,1,Tenant applied directly (no referral or nomination),,0,68,Weekly,Universal Credit housing element,1,All,,0,Every 2 weeks,,,,200.0,50.0,40.0,35.0,325.0,,,,Yes,12.0,,,,,,,,,,,,,,,,,,,, diff --git a/spec/lib/tasks/recalculate_irproduct_values_spec.rb b/spec/lib/tasks/recalculate_irproduct_values_spec.rb new file mode 100644 index 000000000..957496fa0 --- /dev/null +++ b/spec/lib/tasks/recalculate_irproduct_values_spec.rb @@ -0,0 +1,132 @@ +require "rails_helper" +require "rake" + +RSpec.describe "recalculate_irproduct_values" do + describe ":recalculate_irproduct_values", type: :task do + subject(:task) { Rake::Task["recalculate_irproduct_values"] } + + before do + Rake.application.rake_require("tasks/recalculate_irproduct_values") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + let!(:lettings_log) { create(:lettings_log, :completed, values_updated_at: nil) } + + it "updates irproduct to nil if it's set to 1 but rent type is not 3, 4 or 5" do + lettings_log.irproduct = 1 + lettings_log.rent_type = 2 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(nil) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates irproduct to nil if it's set to 2 but rent type is not 3, 4 or 5" do + lettings_log.irproduct = 2 + lettings_log.rent_type = 2 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(nil) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates irproduct to nil if it's set to 3 but rent type is not 3, 4 or 5" do + lettings_log.irproduct = 3 + lettings_log.rent_type = 2 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(nil) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates irproduct to 1 if it's set to nil but rent type is 3" do + lettings_log.irproduct = nil + lettings_log.rent_type = 3 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(1) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates irproduct to 1 if it's set to something else but rent type is 3" do + lettings_log.irproduct = 2 + lettings_log.rent_type = 3 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(1) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates irproduct to 2 if it's set to nil but rent type is 4" do + lettings_log.irproduct = nil + lettings_log.rent_type = 4 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(2) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates irproduct to 2 if it's set to something else but rent type is 4" do + lettings_log.irproduct = 1 + lettings_log.rent_type = 4 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(2) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates irproduct to 3 if it's set to nil but rent type is 5" do + lettings_log.irproduct = nil + lettings_log.rent_type = 5 + lettings_log.irproduct_other = "other" + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(3) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates irproduct to 3 if it's set to something else but rent type is 5" do + lettings_log.irproduct = 2 + lettings_log.rent_type = 5 + lettings_log.irproduct_other = "other" + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(3) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "does not update irproduct if rent_type is not 3, 4 or 5 and irproduct is nil" do + lettings_log.irproduct = nil + lettings_log.rent_type = 2 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(nil) + expect(lettings_log.values_updated_at).to be_nil + end + + it "does not update irproduct if a different validation is triggering" do + lettings_log.irproduct = 2 + lettings_log.rent_type = 5 + lettings_log.postcode_full = "invalid" + lettings_log.save!(validate: false) + expect(Rails.logger).to receive(:info).with("Could not update irproduct for LettingsLog #{lettings_log.id}") + task.invoke + lettings_log.reload + expect(lettings_log.irproduct).to eq(2) + expect(lettings_log.values_updated_at).to be_nil + end + end + end +end diff --git a/spec/lib/tasks/recalculate_lar_values_spec.rb b/spec/lib/tasks/recalculate_lar_values_spec.rb new file mode 100644 index 000000000..7831e34d2 --- /dev/null +++ b/spec/lib/tasks/recalculate_lar_values_spec.rb @@ -0,0 +1,100 @@ +require "rails_helper" +require "rake" + +RSpec.describe "recalculate_lar_values" do + describe ":recalculate_lar_values", type: :task do + subject(:task) { Rake::Task["recalculate_lar_values"] } + + before do + Rake.application.rake_require("tasks/recalculate_lar_values") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + let!(:lettings_log) { create(:lettings_log, :completed, values_updated_at: nil) } + + it "updates lar to nil if it's not afordable rent or london afordable rent and lar is 1 but does not set it to export" do + lettings_log.lar = 1 + lettings_log.rent_type = 3 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.lar).to eq(nil) + expect(lettings_log.values_updated_at).to be_nil + end + + it "updates lar to nil if it's not afordable rent or london afordable rent and lar is 2 but does not set it to export" do + lettings_log.lar = 2 + lettings_log.rent_type = 4 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.lar).to eq(nil) + expect(lettings_log.values_updated_at).to be_nil + end + + it "does not update lar if it's not london afordable rent or affordable rent and lar is nil" do + lettings_log.lar = nil + lettings_log.rent_type = 3 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.lar).to eq(nil) + expect(lettings_log.values_updated_at).to be_nil + end + + it "updates lar to 1 if it's london afordable rent and lar is currently nil" do + lettings_log.lar = nil + lettings_log.rent_type = 2 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.lar).to eq(1) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates lar to 1 if it's london afordable rent and lar is currently 2" do + lettings_log.lar = 2 + lettings_log.rent_type = 2 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.lar).to eq(1) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates lar to 2 if it's afordable rent and lar is currently nil" do + lettings_log.lar = nil + lettings_log.rent_type = 1 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.lar).to eq(2) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "updates lar to 2 if it's afordable rent and lar is currently 1" do + lettings_log.lar = 1 + lettings_log.rent_type = 1 + lettings_log.save!(validate: false) + task.invoke + lettings_log.reload + expect(lettings_log.lar).to eq(2) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "does not update lar if a different validation is triggering" do + lettings_log.lar = 1 + lettings_log.rent_type = 1 + lettings_log.postcode_full = "invalid" + lettings_log.save!(validate: false) + expect(Rails.logger).to receive(:info).with("Could not update lar for LettingsLog #{lettings_log.id}") + task.invoke + lettings_log.reload + expect(lettings_log.lar).to eq(1) + expect(lettings_log.values_updated_at).to be_nil + end + end + end +end diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 82cc254cb..6c4d9b1e0 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -2270,6 +2270,58 @@ RSpec.describe LettingsLog do expect(lettings_log["housingneeds_g"]).to eq(1) end end + + context "when saving rent_type" do + it "derives lar as yes (1) if rent_type is london affordable rent" do + lettings_log.update!({ rent_type: 2 }) + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["lar"]).to eq(1) + end + + it "derives lar as no (2) if rent_type is affordable rent" do + lettings_log.update!({ rent_type: 1 }) + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["lar"]).to eq(2) + end + + it "clears previously set lar if rent_type is not affordable rent" do + lettings_log.update!({ rent_type: 2 }) + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["lar"]).to eq(1) + + lettings_log.update!({ rent_type: 3 }) + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["lar"]).to eq(nil) + end + + it "derives irproduct as rent_to_buy (1) if rent_type is rent_to_buy (3)" do + lettings_log.update!({ rent_type: 3 }) + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["irproduct"]).to eq(1) + end + + it "derives irproduct as london_living_rent (2) if rent_type is london_living_rent (4)" do + lettings_log.update!({ rent_type: 4 }) + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["irproduct"]).to eq(2) + end + + it "derives irproduct as other_intermediate_rent_product (3) if rent_type is other_intermediate_rent_product (5)" do + lettings_log.update!({ rent_type: 5, irproduct_other: "other" }) + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["irproduct"]).to eq(3) + end + + it "clears previously set irproduct if rent_type is intermediate rent" do + lettings_log.update!({ rent_type: 4 }) + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["irproduct"]).to eq(2) + + lettings_log.update!({ rent_type: 2 }) + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["irproduct"]).to eq(nil) + end + end end describe "optional fields" do