104 lines
3.5 KiB
Ruby
104 lines
3.5 KiB
Ruby
# app/services/pdf/relatorio_financeiro_consolidacao_pdf.rb
|
|
#
|
|
# Relatório Financeiro de uma consolidação: resumo por motorista (pago x
|
|
# pendente), composição por tipo de lançamento e totais. Para o admin imprimir
|
|
# para conferência e histórico.
|
|
#
|
|
module Pdf
|
|
class RelatorioFinanceiroConsolidacaoPdf < BasePdf
|
|
VERDE = '22C55E'
|
|
|
|
def initialize(consolidacao:)
|
|
super()
|
|
@consolidacao = consolidacao
|
|
@motoristas = consolidacao.consolidacao_motoristas.order(:motorista_nome)
|
|
@entregas = consolidacao.consolidacao_entregas
|
|
end
|
|
|
|
private
|
|
|
|
def corpo
|
|
secao "RELATÓRIO FINANCEIRO — CONSOLIDAÇÃO"
|
|
|
|
@pdf.text "Consolidação: #{@consolidacao.nome}", size: 11, style: :bold
|
|
@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} · Pagamento: #{@consolidacao.status_pagamento.to_s.humanize}", size: 10
|
|
@pdf.fill_color '000000'
|
|
|
|
secao "POR MOTORISTA (#{@motoristas.size})"
|
|
tabela_motoristas
|
|
|
|
secao "COMPOSIÇÃO POR TIPO"
|
|
tabela_por_tipo
|
|
|
|
@pdf.start_new_page if @pdf.cursor < 140
|
|
secao "TOTAIS"
|
|
totais
|
|
end
|
|
|
|
def tabela_motoristas
|
|
if @motoristas.any?
|
|
linhas = [['#', 'Motorista', 'Pagamento', 'Forma', 'Valor']]
|
|
@motoristas.each_with_index do |cm, i|
|
|
pagamento = cm.pago? ? "Pago em #{cm.pago_em.strftime('%d/%m/%Y')}" : 'Pendente'
|
|
linhas << [
|
|
(i + 1).to_s,
|
|
cm.motorista_nome,
|
|
pagamento,
|
|
cm.forma_pagamento&.humanize || '—',
|
|
moeda(cm.valor_total)
|
|
]
|
|
end
|
|
|
|
@pdf.table(linhas, header: true, width: @pdf.bounds.width,
|
|
column_widths: { 0 => 22, 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..-1).borders = [:bottom]
|
|
t.row(1..-1).border_color = 'DDDDDD'
|
|
end
|
|
else
|
|
@pdf.fill_color CINZA
|
|
@pdf.text 'Nenhum motorista nesta consolidação.', size: 10
|
|
@pdf.fill_color '000000'
|
|
end
|
|
end
|
|
|
|
def tabela_por_tipo
|
|
soma = @entregas.reorder(nil).group(:tipo).sum(:valor_aplicado)
|
|
qtd = @entregas.reorder(nil).group(:tipo).count
|
|
|
|
linhas = [['Tipo', 'Quantidade', 'Valor']]
|
|
ConsolidacaoEntrega::TIPO_CORES.each do |tipo, cfg|
|
|
valor = soma[tipo].to_f
|
|
sinal = tipo == 'desconto' ? '-' : ''
|
|
linhas << [cfg[:label], (qtd[tipo] || 0).to_s, "#{sinal}#{moeda(valor)}"]
|
|
end
|
|
|
|
@pdf.table(linhas, width: 360, 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
|
|
t.columns(2).align = :right
|
|
end
|
|
end
|
|
|
|
def totais
|
|
linhas = [
|
|
['Custo total', moeda(@consolidacao.valor_total)],
|
|
['Pago', moeda(@consolidacao.valor_pago)],
|
|
['A pagar', moeda(@consolidacao.valor_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
|
|
end
|
|
end
|
|
end
|
|
end
|