2022-12-01 13:03:19 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-07 16:26:25 +02:00
|
|
|
wb = xlsx_package.workbook
|
|
|
|
|
2022-12-01 13:03:19 +01:00
|
|
|
bold = wb.styles.add_style b: true
|
|
|
|
header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
|
|
|
|
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
|
2016-07-07 16:26:25 +02:00
|
|
|
|
|
|
|
wb.add_worksheet(name: @index.label) do |sheet|
|
|
|
|
## heading stats for the current page
|
2022-12-01 13:03:19 +01:00
|
|
|
sheet.add_row [t('export.entries'), @results['hits']['total']], style: [bold, nil], types: %i[string integer]
|
2016-07-07 16:26:25 +02:00
|
|
|
if @index.ca
|
2022-12-01 13:03:19 +01:00
|
|
|
sheet.add_row [t('export.revenue'), @results['aggregations']['total_ca']['value']], style: [bold, nil], types: %i[string float]
|
2016-07-07 16:26:25 +02:00
|
|
|
end
|
2022-12-01 13:03:19 +01:00
|
|
|
sheet.add_row [t('export.average_age'), @results['aggregations']['average_age']['value']], style: [bold, nil], types: %i[string float]
|
2016-07-07 16:26:25 +02:00
|
|
|
unless @type.simple
|
2022-12-01 13:03:19 +01:00
|
|
|
sheet.add_row ["#{t('export.total')} #{@type.label}", @results['aggregations']['total_stat']['value']],
|
|
|
|
style: [bold, nil],
|
|
|
|
types: %i[string integer]
|
2016-07-07 16:26:25 +02:00
|
|
|
end
|
|
|
|
sheet.add_row []
|
|
|
|
|
|
|
|
## data table
|
|
|
|
# heading labels
|
2022-12-01 13:03:19 +01:00
|
|
|
columns = [t('export.date'), t('export.user'), t('export.email'), t('export.phone'), t('export.gender'), t('export.age'),
|
|
|
|
t('export.type')]
|
2016-07-07 16:26:25 +02:00
|
|
|
columns.push @type.label unless @type.simple
|
|
|
|
@fields.each do |f|
|
|
|
|
columns.push f.label
|
|
|
|
end
|
|
|
|
columns.push t('export.revenue') if @index.ca
|
2022-12-01 13:03:19 +01:00
|
|
|
sheet.add_row columns, style: header
|
2016-07-07 16:26:25 +02:00
|
|
|
|
|
|
|
# data rows
|
|
|
|
@results['hits']['hits'].each do |hit|
|
|
|
|
user = get_item(@users, hit['_source']['userId'])
|
|
|
|
subtype = get_item(@subtypes, hit['_source']['subType'], 'key')
|
2022-12-01 13:03:19 +01:00
|
|
|
data, styles, types = statistics_line(hit, user, @type, subtype, date)
|
2016-07-07 16:26:25 +02:00
|
|
|
@fields.each do |f|
|
2022-12-01 13:03:19 +01:00
|
|
|
format_xlsx_cell(hit['_source'][f.key], data, styles, types, source_data_type: f.data_type, date_format: date)
|
2016-07-07 16:26:25 +02:00
|
|
|
end
|
2022-12-01 13:03:19 +01:00
|
|
|
add_ca_cell(@index, hit, data, styles, types)
|
2016-07-07 16:26:25 +02:00
|
|
|
|
2022-12-01 13:03:19 +01:00
|
|
|
sheet.add_row data, style: styles, types: types
|
2016-07-07 16:26:25 +02:00
|
|
|
end
|
2022-12-01 13:03:19 +01:00
|
|
|
end
|