Adição da opção de baixar a planilha preenchida para o cliente
This commit is contained in:
@@ -10,6 +10,8 @@ class OperacoesDashboardController < ApplicationController
|
||||
# Linhas por página da tabela espelho (planilha da operação)
|
||||
ESPELHO_POR_PAGINA = 15
|
||||
|
||||
XLSX_MIME = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'.freeze
|
||||
|
||||
def index
|
||||
authorize :dashboard, :operacoes?
|
||||
|
||||
@@ -35,8 +37,53 @@ class OperacoesDashboardController < ApplicationController
|
||||
@operacao = Operacao.sanitizar([params[:operacao]]).first || @tabelas_validas.first
|
||||
@metricas = montar([@operacao], filtros: @filtros, data: @data_filtro) if @operacao
|
||||
end
|
||||
end
|
||||
|
||||
montar_tabela_espelho if @metricas && %w[operacao global].include?(@modo)
|
||||
# Planilha da operação (tabela espelho): página própria com busca e paginação.
|
||||
# Aceita os mesmos parâmetros do index (operacao/modo global/período/cross-
|
||||
# filters), então o botão no dashboard preserva o contexto atual.
|
||||
def planilha
|
||||
authorize :dashboard, :operacoes?
|
||||
|
||||
@periodo_inicio, @periodo_fim = periodo_selecionado
|
||||
@modo = params[:modo] == 'global' ? 'global' : 'operacao'
|
||||
@operacoes_agrupadas = Operacao.agrupadas_por_mes
|
||||
@tabelas_validas = Operacao.nomes_validos
|
||||
@filtros, @chips = cross_filtros
|
||||
@data_filtro = parse_data(params[:f_data])
|
||||
|
||||
if @modo == 'global'
|
||||
@metricas = montar(@tabelas_validas, inicio: @periodo_inicio, fim: @periodo_fim, filtros: @filtros, data: @data_filtro)
|
||||
else
|
||||
@operacao = Operacao.sanitizar([params[:operacao]]).first || @tabelas_validas.first
|
||||
@metricas = montar([@operacao], filtros: @filtros, data: @data_filtro) if @operacao
|
||||
end
|
||||
|
||||
montar_tabela_espelho if @metricas
|
||||
end
|
||||
|
||||
# GET /dashboard/operacoes/planilha/baixar?operacao=gade_entregas_...
|
||||
# Baixa a planilha Entregas (.xlsx) da operação já preenchida com o resultado
|
||||
# do rastreio — o modelo que é entregue ao cliente.
|
||||
def baixar_planilha
|
||||
authorize :dashboard, :operacoes?
|
||||
|
||||
tabela = params[:operacao].to_s
|
||||
unless Operacao.valida?(tabela)
|
||||
return redirect_to(operacoes_planilha_path, alert: 'Selecione uma operação válida.')
|
||||
end
|
||||
|
||||
planilha = Analytics::PlanilhaEntregas.new(tabela)
|
||||
linhas = planilha.linhas
|
||||
if linhas.empty?
|
||||
return redirect_to(operacoes_planilha_path(operacao: tabela),
|
||||
alert: "A operação #{planilha.label} não tem registros.")
|
||||
end
|
||||
|
||||
send_data Analytics::PlanilhaEntregasXlsx.new(linhas).gerar,
|
||||
filename: "Entregas #{planilha.label} #{Date.current.strftime('%m.%Y')}.xlsx",
|
||||
type: XLSX_MIME,
|
||||
disposition: 'attachment'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user