Module: BrInvoicesPdf::Util::BaseRenderer

Constant Summary collapse

PAYMENTS_TABLE_BASE_DATA =
[['FORMA DE PAGAMENTO', 'VALOR']].freeze
CNPJ_FORMAT =
'%02d.%03d.%03d/%04d-%02d'
CPF_FORMAT =
'%03d.%03d.%03d-%02d'

Class Method Summary collapse

Class Method Details

.box(pdf, position, width) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/br_invoices_pdf/util/base_renderer.rb', line 8

def box(pdf, position, width)
  pdf.bounding_box(position, width: width) do
    pdf.pad(2) do
      pdf.indent(2, 2) do
        yield
      end
    end

    pdf.stroke_bounds
  end
end

.format_cnpj(cnpj) ⇒ Object

:reek:FeatureEnvy



85
86
87
88
# File 'lib/br_invoices_pdf/util/base_renderer.rb', line 85

def format_cnpj(cnpj)
  format(CNPJ_FORMAT, cnpj[0, 2].to_i, cnpj[2, 3].to_i, cnpj[5, 3].to_i,
         cnpj[8, 4].to_i, cnpj[12, 2].to_i)
end

.format_cpf(cpf) ⇒ Object

:reek:FeatureEnvy



92
93
94
# File 'lib/br_invoices_pdf/util/base_renderer.rb', line 92

def format_cpf(cpf)
  format(CPF_FORMAT, cpf[0, 3].to_i, cpf[3, 3].to_i, cpf[6, 3].to_i, cpf[9, 2].to_i)
end

.format_currency(number_string) ⇒ Object

:reek:FeatureEnvy



97
98
99
100
# File 'lib/br_invoices_pdf/util/base_renderer.rb', line 97

def format_currency(number_string)
  number = BigDecimal(number_string)
  format('%.2f', number.truncate(2)).tr('.', ',')
end

.format_number(number_string, prec: 4) ⇒ Object

:reek:FeatureEnvy



103
104
105
106
# File 'lib/br_invoices_pdf/util/base_renderer.rb', line 103

def format_number(number_string, prec: 4)
  number = BigDecimal(number_string)
  format("%.#{prec}f", number.truncate(prec)).tr('.', ',')
end

.page_content_width(pdf) ⇒ Object

:reek:FeatureEnvy



114
115
116
117
118
# File 'lib/br_invoices_pdf/util/base_renderer.rb', line 114

def page_content_width(pdf)
  page = pdf.page
  margins = page.margins
  page_paper_width(page.size) - margins[:left] - margins[:right]
end

.page_paper_width(name) ⇒ Object

:reek:FeatureEnvy



109
110
111
# File 'lib/br_invoices_pdf/util/base_renderer.rb', line 109

def page_paper_width(name)
  (name.is_a?(Array) ? name : PDF::Core::PageGeometry::SIZES[name]).first
end

.pdf_setup(pdf) ⇒ Object

:reek:FeatureEnvy



45
46
47
48
49
50
51
52
53
54
# File 'lib/br_invoices_pdf/util/base_renderer.rb', line 45

def pdf_setup(pdf)
  pdf.bounding_box([0, pdf.cursor], width: page_content_width(pdf)) do
    pdf.pad(10) do
      pdf.indent(10, 10) do
        yield
      end
    end
    pdf.stroke_bounds
  end
end