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

131 lines
4.7 KiB
Ruby
Raw Permalink 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/totais_motorista_pdf.rb
#
# Relatório "total por motorista no período" — o mesmo documento serve às duas
# telas, porque os números vêm do mesmo Analytics::TotaisPorMotorista:
#
# • admin (todos os motoristas): resumo por motorista + as consolidações que
# compõem o total de cada um;
# • motorista (um só): o total fechado dele no período + as consolidações que
# formam esse valor, com espaço de assinatura para servir de comprovante.
#
module Pdf
class TotaisMotoristaPdf < BasePdf
VERDE = '22C55E'
AMBAR = 'D97706'
# totais: Analytics::TotaisPorMotorista já montado (ele conhece o recorte).
# detalhar: inclui, para cada motorista, a lista de consolidações. Sempre
# ligado quando o relatório é de um motorista só.
def initialize(totais:, detalhar: true, titulo: nil)
super()
@t = totais
@detalhar = detalhar || @t.motorista.present?
@titulo = titulo
end
private
def corpo
secao(@titulo || (@t.motorista ? 'MEU TOTAL NO PERÍODO' : 'TOTAL POR MOTORISTA'))
@pdf.text "Período: #{@t.periodo_label}", size: 11, style: :bold
@pdf.fill_color CINZA
@pdf.text(@t.motorista ? "Motorista: #{@t.motorista}" : "Motoristas: #{@t.linhas.size}", size: 10)
@pdf.text "Consolidações no período: #{@t.total_consolidacoes}", size: 10
@pdf.fill_color '000000'
secao 'TOTAIS'
kpis
if @t.vazio?
@pdf.fill_color CINZA
@pdf.text 'Nenhuma consolidação fechada neste período.', size: 10
@pdf.fill_color '000000'
return
end
unless @t.motorista
secao 'RESUMO POR MOTORISTA'
tabela_resumo
end
return unless @detalhar
@t.linhas.each do |linha|
@pdf.start_new_page if @pdf.cursor < 160
secao "COMPOSIÇÃO — #{linha[:motorista].upcase}"
tabela_composicao(@t.consolidacoes_de(linha[:motorista]))
end
assinaturas(motorista: @t.motorista) if @t.motorista
end
def kpis
linhas = [
['Total do período', moeda(@t.total_geral)],
['Pago', moeda(@t.total_pago)],
['A receber', moeda(@t.total_pendente)]
]
@pdf.table(linhas, width: 300, cell_style: { size: 11, padding: [6, 10] }) do |t|
t.columns(0).font_style = :bold
t.columns(1).align = :right
t.row(1).text_color = VERDE
t.row(2).text_color = AMBAR
t.cells.borders = [:bottom]
t.cells.border_color = 'DDDDDD'
end
end
def tabela_resumo
linhas = [['Motorista', 'Consol.', 'Lanç.', 'Pago', 'A receber', 'Total']]
@t.linhas.each do |l|
linhas << [l[:motorista], l[:consolidacoes].to_s, l[:lancamentos].to_s,
moeda(l[:valor_pago]), moeda(l[:valor_pendente]), moeda(l[:valor_total])]
end
linhas << ['TOTAL', @t.total_consolidacoes.to_s, '',
moeda(@t.total_pago), moeda(@t.total_pendente), moeda(@t.total_geral)]
@pdf.table(linhas, header: true, width: @pdf.bounds.width,
column_widths: { 1 => 50, 2 => 50, 3 => 80, 4 => 80, 5 => 85 },
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(1..5).align = :right
t.row(-1).font_style = :bold
t.row(1..-1).borders = [:bottom]
t.row(1..-1).border_color = 'DDDDDD'
end
end
# As consolidações que compõem o valor — é o "clicar no motorista" da tela,
# em papel.
def tabela_composicao(cms)
linhas = [['Consolidação', 'Período', 'Situação', 'Pago em', 'Valor']]
cms.each do |cm|
c = cm.consolidacao
linhas << [
c.nome,
"#{c.data_inicio.strftime('%d/%m/%y')}#{c.data_fim.strftime('%d/%m/%y')}",
cm.pago? ? 'Pago' : 'A receber',
cm.pago? ? cm.pago_em.strftime('%d/%m/%Y') : '—',
moeda(cm.valor_total)
]
end
linhas << ['TOTAL', '', '', '', moeda(cms.sum { |cm| cm.valor_total || 0 })]
@pdf.table(linhas, header: true, width: @pdf.bounds.width,
column_widths: { 1 => 90, 2 => 70, 3 => 70, 4 => 80 },
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(4).align = :right
t.row(-1).font_style = :bold
t.row(1..-1).borders = [:bottom]
t.row(1..-1).border_color = 'DDDDDD'
end
end
end
end