class BrDanfe::DanfeLib::NfeLib::DetBody

Attributes

y_position_with_entrega[R]

Public Class Methods

new(pdf, xml) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 7
def initialize(pdf, xml)
  @pdf = pdf
  @xml = xml

  @y_position_with_entrega = Entrega.delivery_local?(@xml) ? 3.00 : 0.00
end

Public Instance Methods

render(has_issqn) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 14
def render(has_issqn)
  table_height_on_first_page = table_height_on_first_page has_issqn
  first_table = create_table
  next_table = create_table

  fill_tables first_table, next_table, table_height_on_first_page
  render_tables first_table, next_table, table_height_on_first_page
end

Private Instance Methods

cell_number(text) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 98
def cell_number(text)
  cell_text(text, align: :right)
end
cell_text(text, options = {}) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 92
def cell_text(text, options = {})
  cell = { content: text, border_lines: [:solid], size: 6.5 }
  cell.merge!(options)
  cell
end
column_widths() click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 52
def column_widths
  {
    0 => 1.60.cm, 1 => 6.10.cm, 2 => 1.05.cm, 3 => 0.60.cm, 4 => 0.70.cm, 5 => 1.05.cm, 6 => 1.20.cm,
    7 => 1.20.cm, 8 => 1.20.cm, 9 => 1.20.cm, 10 => 1.00.cm, 11 => 1.00.cm, 12 => 0.90.cm, 13 => 0.76.cm
  }
end
create_header() click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 33
def create_header
  header = [[
    header_column('prod.cProd'), header_column('prod.xProd'), header_column('prod.NCM'),
    header_column('ICMS.CST'), header_column('prod.CFOP'), header_column('prod.uCom'),
    header_column('prod.qCom'), header_column('prod.vUnCom'), header_column('prod.vProd'),
    header_column('ICMS.vBC'), header_column('ICMS.vICMS'), header_column('IPI.vIPI'),
    header_column('ICMS.pICMS'), header_column('IPI.pIPI')
  ]]
  @pdf.make_table(header, options)
end
create_row(data) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 106
def create_row(data)
  @pdf.make_table([data], options)
end
create_table() click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 29
def create_table
  [create_header]
end
fill_tables(first_table, next_table, first_table_height) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 59
def fill_tables(first_table, next_table, first_table_height)
  total_height = first_table.first.height

  products.each do |product|
    row = create_row product
    total_height += row.height
    total_height <= first_table_height ? first_table << row : next_table << row
  end
end
header_column(title) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 44
def header_column(title)
  { content: I18n.t("danfe.det.#{title}"), align: :left, size: 6, valign: :top }
end
numerify(det, xpath) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 102
def numerify(det, xpath)
  BrDanfe::Helper.numerify(det.css(xpath.to_s).text)
end
options() click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 48
def options
  { column_widths: column_widths, cell_style: { padding: 2, border_width: 0.3 } }
end
product(det) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 73
def product(det)
  [
    cell_text(det.css('prod/cProd').text),
    cell_text(Xprod.new(det).render),
    cell_text(det.css('prod/NCM').text),
    cell_text(Cst.to_danfe(det)),
    cell_text(det.css('prod/CFOP').text),
    cell_text(det.css('prod/uCom').text),
    cell_number(numerify(det, 'prod/qCom')),
    cell_number(numerify(det, 'prod/vUnCom')),
    cell_number(numerify(det, 'prod/vProd')),
    cell_number(numerify(det, 'ICMS/*/vBC')),
    cell_number(numerify(det, 'ICMS/*/vICMS')),
    cell_number(numerify(det, 'IPI/*/vIPI')),
    cell_number(numerify(det, 'ICMS/*/pICMS')),
    cell_number(numerify(det, 'IPI/*/pIPI'))
  ]
end
products() click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 69
def products
  @xml.collect('xmlns', 'det') { |det| product(det) }
end
render_table(data, start_position, height) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 120
def render_table(data, start_position, height)
  @pdf.y = start_position
  @pdf.bounding_box [0.75.cm, @pdf.cursor], width: 19.56.cm, height: height do
    @pdf.table data.map { |item| [item] }, header: true
  end
end
render_tables(first_table, next_table, first_table_height) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 110
def render_tables(first_table, next_table, first_table_height)
  render_table first_table, table_position_on_first_page, first_table_height

  table_with_only_header = 1
  if next_table.size > table_with_only_header
    @pdf.start_new_page
    render_table next_table, table_position_on_next_pages, table_height_on_next_pages
  end
end
table_height_on_first_page(has_issqn) click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 25
def table_height_on_first_page(has_issqn)
  has_issqn ? 6.35.cm : 7.72.cm
end
table_height_on_next_pages() click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 136
def table_height_on_next_pages
  22.58.cm
end
table_position_on_first_page() click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 127
def table_position_on_first_page
  y_position = @y_position_with_entrega.cm + 18.07.cm
  Helper.invert(y_position)
end
table_position_on_next_pages() click to toggle source
# File lib/br_danfe/danfe_lib/nfe_lib/det_body.rb, line 132
def table_position_on_next_pages
  Helper.invert(6.57.cm)
end