|
|
|
|
@ -5,11 +5,19 @@ module Csv
|
|
|
|
|
@export_type = export_type |
|
|
|
|
@year = year |
|
|
|
|
@attributes = sales_log_attributes |
|
|
|
|
@definitions = sales_log_definitions |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def prepare_csv(logs) |
|
|
|
|
CSV.generate(headers: true) do |csv| |
|
|
|
|
csv << formatted_attribute_headers |
|
|
|
|
formatted_attributes = formatted_attribute_headers |
|
|
|
|
if @year >= 2023 |
|
|
|
|
csv << formatted_attributes.map do |attribute| |
|
|
|
|
record = @definitions.find { |r| r.variable == attribute.downcase } |
|
|
|
|
record&.tap { |r| r.update!(last_accessed: Time.zone.now) }&.definition |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
csv << formatted_attributes |
|
|
|
|
|
|
|
|
|
logs.find_each do |log| |
|
|
|
|
csv << @attributes.map { |attribute| value(attribute, log) } |
|
|
|
|
@ -161,6 +169,16 @@ module Csv
|
|
|
|
|
@user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def sales_log_definitions |
|
|
|
|
CsvVariableDefinition.sales.group_by { |record| [record.variable, record.definition] } |
|
|
|
|
.map do |_, options| |
|
|
|
|
exact_match = options.find { |definition| definition.year == @year } |
|
|
|
|
next exact_match if exact_match |
|
|
|
|
|
|
|
|
|
options.max_by(&:year) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def insert_derived_and_related_attributes(ordered_questions) |
|
|
|
|
ordered_questions.flat_map do |question| |
|
|
|
|
if question.type == "checkbox" |
|
|
|
|
|