79 lines
2.6 KiB
Ruby
79 lines
2.6 KiB
Ruby
# app/controllers/dashboard_controller.rb
|
|
class DashboardController < ApplicationController
|
|
def index
|
|
skip_authorization
|
|
|
|
@data_referencia = params[:data] ? Date.parse(params[:data]) : Date.today
|
|
@mes_inicio = @data_referencia.beginning_of_month
|
|
@mes_fim = @data_referencia.end_of_month
|
|
|
|
carregar_dados_dashboard
|
|
end
|
|
|
|
private
|
|
|
|
def carregar_dados_dashboard
|
|
# Entregas do mês atual (da tabela read-only)
|
|
entregas_mes = Entrega.do_mes(@data_referencia)
|
|
|
|
# Totais gerais
|
|
@total_entregas = entregas_mes.count
|
|
@entregas_pagas = entregas_mes.pagas.count
|
|
@entregas_pendentes = @total_entregas - @entregas_pagas
|
|
|
|
# Configurações de preço
|
|
config = Configuracao.mapa_de_precos
|
|
|
|
# Valor estimado total
|
|
@valor_estimado = (@entregas_pagas * config[:entrega]).round(2)
|
|
|
|
# Por motorista (top 10)
|
|
@motoristas = entregas_mes.pagas
|
|
.group(:driver)
|
|
.count
|
|
.sort_by { |_, v| -v }
|
|
.first(10)
|
|
.map do |driver, qtd|
|
|
{
|
|
nome: driver,
|
|
entregas: qtd,
|
|
valor: (qtd * config[:entrega]).round(2)
|
|
}
|
|
end
|
|
|
|
# Por operação (route_id → contact_name)
|
|
@por_operacao = entregas_mes.pagas
|
|
.group(:contact_name)
|
|
.count
|
|
.sort_by { |_, v| -v }
|
|
.first(6)
|
|
.to_h
|
|
|
|
# Evolução diária do mês (para Chart.js)
|
|
@grafico_diario = build_grafico_diario(entregas_mes, config[:entrega])
|
|
|
|
# Consolidações do mês
|
|
@consolidacoes_mes = Consolidacao.ativas.where(created_at: @mes_inicio.beginning_of_day..@mes_fim.end_of_day)
|
|
@consolidacoes_abertas = @consolidacoes_mes.where(status: :rascunho).count
|
|
@consolidacoes_fechadas = @consolidacoes_mes.where(status: :finalizada).count
|
|
|
|
# Histórico estimado mais recente
|
|
@historico_recente = HistoricoEstimado.order(created_at: :desc).limit(5)
|
|
end
|
|
|
|
def build_grafico_diario(entregas, preco_entrega)
|
|
dias = ((@mes_inicio)..[@mes_fim, Date.today].min).map(&:to_s)
|
|
|
|
contagem = entregas.pagas
|
|
.group("DATE(planned_date)")
|
|
.count
|
|
.transform_keys { |k| k.to_s }
|
|
|
|
labels = dias.map { |d| Date.parse(d).strftime('%d/%m') }
|
|
valores = dias.map { |d| ((contagem[d] || 0) * preco_entrega).round(2) }
|
|
qtds = dias.map { |d| contagem[d] || 0 }
|
|
|
|
{ labels: labels, valores: valores, qtds: qtds }
|
|
end
|
|
end
|