61 lines
1.9 KiB
Ruby
61 lines
1.9 KiB
Ruby
# app/services/analytics/planilha_entregas_xlsx.rb
|
|
#
|
|
# Gera o binário .xlsx da planilha Entregas do cliente (aba ENTREGAS) a partir
|
|
# das linhas montadas por Analytics::PlanilhaEntregas — layout A..Z idêntico ao
|
|
# modelo "Entregas SUDESTE MM.AAAA_FINAL.xlsx".
|
|
require 'caxlsx'
|
|
|
|
module Analytics
|
|
class PlanilhaEntregasXlsx
|
|
def initialize(linhas)
|
|
@linhas = linhas
|
|
end
|
|
|
|
# String binária do .xlsx.
|
|
def gerar
|
|
pkg = Axlsx::Package.new
|
|
pkg.workbook.add_worksheet(name: 'ENTREGAS') do |sheet|
|
|
negrito = sheet.workbook.styles.add_style(b: true)
|
|
sheet.add_row(PlanilhaEntregas::CABECALHO, style: negrito)
|
|
@linhas.each do |l|
|
|
valores, tipos = linha(l)
|
|
sheet.add_row(valores, types: tipos)
|
|
end
|
|
sheet.auto_filter = "A1:Z#{@linhas.size + 1}"
|
|
end
|
|
pkg.to_stream.read
|
|
end
|
|
|
|
private
|
|
|
|
# A..V direto da tabela gade (texto); W..Z do rastreio:
|
|
# W STATUS (completed/failed...), X ENTREGA (Sim/Não), Y DATA OCORRÊNCIA
|
|
# (data real do checkout), Z OCORRÊNCIA (motivo do insucesso).
|
|
def linha(l)
|
|
status_r = l['rastreio_status'].to_s
|
|
entregue = if status_r == 'completed'
|
|
'Sim'
|
|
elsif Entrega::STATUS_FALHA.include?(status_r)
|
|
'Não'
|
|
else
|
|
''
|
|
end
|
|
data_ocorrencia = data_de(l['rastreio_checkout'])
|
|
|
|
valores = PlanilhaEntregas::COLUNAS_GADE.keys.map { |c| l[c].to_s } +
|
|
[status_r, entregue, data_ocorrencia || '', l['rastreio_observation'].to_s]
|
|
tipos = Array.new(PlanilhaEntregas::COLUNAS_GADE.size + 2, :string) +
|
|
[(data_ocorrencia ? :date : :string), :string]
|
|
[valores, tipos]
|
|
end
|
|
|
|
def data_de(valor)
|
|
return nil if valor.nil?
|
|
return valor.to_date if valor.respond_to?(:to_date)
|
|
Date.parse(valor.to_s)
|
|
rescue ArgumentError, TypeError
|
|
nil
|
|
end
|
|
end
|
|
end
|