<%# app/views/consolidacoes/totais.html.erb — Total por motorista no período %> <% content_for :title, 'Totais por motorista' %>

<%= icone :motorista, espaco: false %> Totais por motorista

Consolidações <%= @incluir_rascunhos ? 'de qualquer status' : 'finalizadas' %> com período dentro de <%= l @inicio, format: :short %> → <%= l @fim, format: :short %>

<%= link_to rotulo(:consolidacoes, 'Consolidações', cor: nil), consolidacoes_path, class: 'text-gray-400 hover:text-white border border-[#2a2a2a] px-4 py-3 rounded-lg min-h-[48px] flex items-center transition-colors' %> <%= link_to rotulo(:baixar, 'Baixar relatório (PDF)', cor: nil), totais_pdf_consolidacoes_path(inicio: @inicio, fim: @fim, rascunhos: (@incluir_rascunhos ? '1' : nil), motorista: @motorista), data: { turbo: false }, class: 'bg-orange-500 hover:bg-orange-600 text-black font-bold px-5 py-3 rounded-lg min-h-[48px] flex items-center transition-colors' %>
<%# ── Filtros ──────────────────────────────────────────── %> <%= form_with url: totais_consolidacoes_path, method: :get, class: 'bg-[#1a1a1a] border border-[#2a2a2a] rounded-xl p-4 mb-6 flex flex-wrap items-center gap-3' do |f| %> <%# O motorista clicado viaja junto para o filtro não fechar o drill-down. %> <%= hidden_field_tag :motorista, @motorista if @motorista %> <%= f.date_field :inicio, value: @inicio.strftime('%Y-%m-%d'), class: 'flex-1 basis-36 max-w-[13rem] min-w-0 bg-[#0a0a0a] border border-[#2a2a2a] text-white rounded-lg px-3 py-2.5' %> <%= f.date_field :fim, value: @fim.strftime('%Y-%m-%d'), class: 'flex-1 basis-36 max-w-[13rem] min-w-0 bg-[#0a0a0a] border border-[#2a2a2a] text-white rounded-lg px-3 py-2.5' %>
<%= f.submit 'Filtrar', class: 'bg-orange-500 hover:bg-orange-600 text-black font-bold rounded-lg cursor-pointer px-5 py-2.5 whitespace-nowrap' %> <%= link_to 'Limpar', totais_consolidacoes_path, class: 'px-4 py-2.5 text-gray-400 hover:text-white border border-[#2a2a2a] rounded-lg whitespace-nowrap' %>
<% end %> <%# ── KPIs do período ──────────────────────────────────── %>

<%= icone :dinheiro %> Total do período

<%= moeda(@totais.total_geral) %>

<%= @totais.total_consolidacoes %> consolidação(ões) · <%= @totais.linhas.size %> motorista(s)

<%= icone :sucesso, cor: 'text-green-400' %> Pago

<%= moeda(@totais.total_pago) %>

<%= icone :parcial, cor: 'text-amber-400' %> A pagar

<%= moeda(@totais.total_pendente) %>

<%= icone :ticket %> Média por motorista

<%= moeda(@totais.linhas.any? ? @totais.total_geral / @totais.linhas.size : 0) %>

<% if @totais.vazio? %>

<%= icone :vazio, tamanho: 'w-12 h-12', cor: 'text-gray-600', espaco: false %>

Nenhuma consolidação <%= @incluir_rascunhos ? '' : 'finalizada ' %>com período dentro dessa faixa.

Amplie o período ou marque "Incluir rascunhos".

<% else %> <%# ── Tabela: total por motorista (clique abre a composição) ── %>
<% @totais.linhas.each do |l| %> <% aberto = @motorista == l[:motorista] %> <%# O nome é um LINK de verdade (e não um ): abre/fecha a composição daquele motorista e continua acessível pelo teclado. %> <% end %>
Motorista Consolidações Lançamentos Pago A pagar Total
<%= link_to totais_consolidacoes_path(inicio: @inicio, fim: @fim, rascunhos: (@incluir_rascunhos ? '1' : nil), motorista: (aberto ? nil : l[:motorista])), class: 'text-white font-semibold hover:text-orange-500 flex items-center gap-1' do %> <%= icone(aberto ? :expandir : :avancar, cor: 'text-gray-500', espaco: false) %><%= l[:motorista] %> <% end %> <%= l[:consolidacoes] %> <%= l[:lancamentos] %> <%= moeda(l[:valor_pago]) %> <%= moeda(l[:valor_pendente]) %> <%= moeda(l[:valor_total]) %>
TOTAL <%= @totais.total_consolidacoes %> <%= moeda(@totais.total_pago) %> <%= moeda(@totais.total_pendente) %> <%= moeda(@totais.total_geral) %>

<%= icone :dica %> Clique num motorista para ver as consolidações que compõem o total dele.

<%# PDF sem a composição de cada motorista — para períodos com muita gente, quando só o resumo interessa. %> <%= link_to 'Baixar só o resumo (PDF)', totais_pdf_consolidacoes_path(inicio: @inicio, fim: @fim, rascunhos: (@incluir_rascunhos ? '1' : nil), detalhar: '0'), data: { turbo: false }, class: 'text-gray-400 hover:text-orange-500 text-xs underline whitespace-nowrap' %>
<%# ── Drill-down: as consolidações que compõem o total ── %> <% if @detalhe %>

<%= icone :composicao, espaco: false %> Composição de <%= @motorista %>

<%= @detalhe.size %> consolidação(ões) no período · <%= moeda(@detalhe.sum { |cm| cm.valor_total || 0 }) %>

<%= link_to rotulo(:baixar, 'PDF deste motorista', cor: nil), totais_pdf_consolidacoes_path(inicio: @inicio, fim: @fim, rascunhos: (@incluir_rascunhos ? '1' : nil), motorista: @motorista), data: { turbo: false }, class: 'bg-orange-500 hover:bg-orange-600 text-black font-bold px-4 py-2.5 rounded-lg flex items-center' %> <%= link_to rotulo(:fechar, 'Fechar', cor: nil), totais_consolidacoes_path(inicio: @inicio, fim: @fim, rascunhos: (@incluir_rascunhos ? '1' : nil)), class: 'text-gray-400 hover:text-white border border-[#2a2a2a] px-4 py-2.5 rounded-lg flex items-center' %>
<% if @detalhe.empty? %>

Nenhuma consolidação desse motorista neste período.

<% end %>
<% @detalhe.each do |cm| %> <% c = cm.consolidacao %> <%= link_to consolidacao_path(c), class: 'block bg-[#0a0a0a] border border-[#2a2a2a] hover:border-orange-500 rounded-xl p-4 transition-colors' do %>
<%= c.nome %> <%= badge_status(c.status) %> <%= badge_pagamento(cm.pago? ? :pago : :pendente) %>

<%= icone :calendario %> <%= l c.data_inicio, format: :short %> → <%= l c.data_fim, format: :short %> <% if cm.pago? %> · <%= icone :dinheiro, cor: 'text-green-400' %> Pago em <%= l cm.pago_em.to_date, format: :short %> <% if cm.forma_pagamento.present? %>via <%= cm.forma_pagamento.humanize %><% end %> <% if cm.nota_fiscal.present? %>· NF <%= cm.nota_fiscal %><% end %> <% end %>

<%= moeda(cm.valor_total) %>

<% end %> <% end %>
<% end %> <% end %>