Files
Reem-Notas/app/services/pdf/relatorio_motorista_pdf.rb

94 lines
3.8 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# app/services/pdf/relatorio_motorista_pdf.rb
#
# Relatório Individual: detalhe de todas as entregas classificadas
# de um motorista em uma consolidação.
#
module Pdf
class RelatorioMotoristaPdf < BasePdf
def initialize(consolidacao:, motorista:)
super()
@consolidacao = consolidacao
@motorista = motorista
@entregas = consolidacao.consolidacao_entregas
.where(motorista_nome: motorista)
.order(:created_at)
end
private
def corpo
secao "RELATÓRIO INDIVIDUAL DE ENTREGAS"
@pdf.text "Consolidação: #{@consolidacao.nome}", size: 11, style: :bold
@pdf.text "Motorista: #{@motorista}", size: 11
@pdf.text "Período: #{@consolidacao.data_inicio.strftime('%d/%m/%Y')} a #{@consolidacao.data_fim.strftime('%d/%m/%Y')}", size: 10
@pdf.fill_color CINZA
@pdf.text "Status: #{@consolidacao.status.humanize}", size: 10
@pdf.fill_color '000000'
secao "ENTREGAS (#{@entregas.size})"
if @entregas.any?
linhas = [['#', 'Veículo', 'NF', 'Endereço', 'Tipo', 'Valor']]
@entregas.each_with_index do |e, i|
original = e.entrega_original
linhas << [
(i + 1).to_s,
original&.vehicle.presence || (e.manual? ? 'manual' : '—'),
original ? "NF #{original.numero_nf}" : (e.nf_manual.present? ? "NF #{e.nf_manual}" : '—'),
original ? original.address.to_s : (e.obs_manual.presence || '—'),
e.termo? && e.quantidade > 1 ? "#{e.tipo_cor[:label]} ×#{e.quantidade}" : e.tipo_cor[:label],
(e.desconto? ? '-' : '') + moeda(e.valor_aplicado)
]
end
# Larguras fixas para as colunas estreitas; a coluna 3 (Endereço) fica
# automática e recebe o espaço restante, quebrando em várias linhas em
# vez de truncar no meio da palavra.
@pdf.table(linhas, header: true, width: @pdf.bounds.width,
column_widths: { 0 => 22, 1 => 70, 2 => 60, 4 => 95, 5 => 60 },
cell_style: { size: 8, padding: [4, 6] }) do |t|
t.row(0).background_color = PRETO
t.row(0).text_color = 'FFFFFF'
t.row(0).font_style = :bold
t.columns(5).align = :right
t.row(1..-1).borders = [:bottom]
t.row(1..-1).border_color = 'DDDDDD'
end
else
@pdf.fill_color CINZA
@pdf.text 'Nenhuma entrega classificada.', size: 10
@pdf.fill_color '000000'
end
# Totais por tipo — mantém RESUMO + Total + Assinaturas juntos, evitando
# título órfão no pé da página e sobreposição com o rodapé.
@pdf.start_new_page if @pdf.cursor < 200
secao "RESUMO"
# reorder(nil): remove o .order(:created_at) da relação — senão o Postgres
# recusa "GROUP BY tipo ORDER BY created_at" (PG::GroupingError → 500).
# sum(:quantidade) (default 1) conta os termos lançados em lote como N entregas.
resumo = @entregas.reorder(nil).group(:tipo).sum(:quantidade)
total = @entregas.sum { |e| e.desconto? ? -e.valor_aplicado : e.valor_aplicado }
resumo_linhas = [['Tipo', 'Quantidade']]
ConsolidacaoEntrega::TIPO_CORES.each do |tipo, cfg|
resumo_linhas << [cfg[:label], (resumo[tipo] || 0).to_s]
end
@pdf.table(resumo_linhas, width: 250, cell_style: { size: 9, padding: [4, 8] }) do |t|
t.row(0).background_color = LARANJA
t.row(0).text_color = '000000'
t.row(0).font_style = :bold
end
@pdf.move_down 14
@pdf.fill_color LARANJA
@pdf.text "TOTAL A PAGAR: #{moeda(total)}", size: 16, style: :bold
@pdf.fill_color '000000'
assinaturas(motorista: @motorista)
end
end
end