From b4a9ca3db8d391bc20bfaf6b0abe095db8777e6f Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Wed, 19 Apr 2023 17:18:13 +0100 Subject: [PATCH] create the SalesLogCsvService create a necessary method on the log to enable submission method to be included on the csv derive values for the two halves of previous postcode for export --- .../derived_variables/sales_log_variables.rb | 1 + app/models/log.rb | 10 +++ app/services/csv/sales_log_csv_service.rb | 87 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 app/services/csv/sales_log_csv_service.rb diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index c30c62b9a..4af864744 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -18,6 +18,7 @@ module DerivedVariables::SalesLogVariables end self.deposit = value if outright_sale? && mortgage_not_used? self.pcode1, self.pcode2 = postcode_full.split(" ") if postcode_full.present? + self.ppostc1, self.ppostc2 = postcode_full.split(" ") if ppostcode_full.present? self.totchild = total_child self.totadult = total_adult + total_elder self.hhmemb = number_of_household_members diff --git a/app/models/log.rb b/app/models/log.rb index 4ee748eda..a657d73f0 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -136,6 +136,16 @@ class Log < ApplicationRecord format_as_currency(field_value) end + def creation_method + if bulk_upload_id.present? + "bulk upload" + elsif old_id.present? + "migrated" + else + "single log" + end + end + private # Handle logs that are older than previous collection start date diff --git a/app/services/csv/sales_log_csv_service.rb b/app/services/csv/sales_log_csv_service.rb new file mode 100644 index 000000000..977121b7d --- /dev/null +++ b/app/services/csv/sales_log_csv_service.rb @@ -0,0 +1,87 @@ +module Csv + class SalesLogCsvService + def initialize(export_type:) + @export_type = export_type + @attributes = sales_log_attributes + end + + def prepare_csv(logs) + CSV.generate(headers: true) do |csv| + csv << @attributes + + logs.find_each do |log| + csv << @attributes.map { |attribute| value(attribute, log) } + end + end + end + + private + + ATTRIBUTES_OF_RELATED_OBJECTS = { + created_by_name: %i[created_by name], + is_dpo: %i[created_by is_dpo], + owning_organisation_name: %i[owning_organisation name], + } + + # can this be made so general it can also be extracted? quite possibly + def value(attribute, log) + if ATTRIBUTES_OF_RELATED_OBJECTS.key? attribute.to_sym + call_chain = ATTRIBUTES_OF_RELATED_OBJECTS[attribute.to_sym] + call_chain.reduce(log) { |object, next_call| object&.public_send(next_call) } + elsif %w[la prevloc].include? attribute # for all exports we output both the codes and labels for these location attributes + log.send(attribute) + elsif %w[la_label prevloc_label].include? attribute # as above + attribute = attribute.remove("_label") + field_value = log.send(attribute) + get_label(field_value, attribute, log) + else + value = log.public_send(attribute) + case @export_type + when "codes" + value + when "labels" + answer_label = get_label(value, attribute, log) + answer_label || label_if_boolean_value(value) || value + end + end + end + + # extract - common to lettings log csv service + def get_label(value, attribute, log) + log.form + .get_question(attribute, log) + &.label_from_value(value) + end + + # also extract + def label_if_boolean_value(value) + return "Yes" if value == true + return "No" if value == false + end + + ATTRIBUTE_MAPPINGS = { + "exdate" => %w[exday exmonth exyear], + "hodate" => %w[hoday homonth hoyear], + "postcode_full" => %w[pcode1 pcode2], + "ppostcode_full" => %w[ppostc1 ppostc2], + "la" => %w[la la_label], + "prevloc" => %w[prevloc prevloc_label], + } + + def sales_log_attributes + ordered_questions = FormHandler.instance.ordered_sales_questions_for_all_years + ordered_questions.reject! { |q| q.id.match?(/((?