Sha256: e5a277c3fffbd95b0bc7b4f00575774d7ba5eaaac6e13d971b90f4baefefff3c

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module BrInvoicesPdf
  module Nfce
    module Parser
      module Products
        extend Util::XmlLocate

        module_function

        FIELDS = { code: 'cProd',
                   description: 'xProd',
                   quantity: 'qCom',
                   unit_label: 'uCom',
                   unit_value: 'vUnCom',
                   total_value: 'vProd' }.freeze

        def execute(xml)
          node_products = xml.locate("#{root_path(xml)}/det")
          products_params(node_products) if node_products
        end

        def products_params(node_products)
          node_products.map(&method(:product_by))
        end
        private_class_method :products_params

        def product_by(element)
          FIELDS.map do |(key, field)|
            [key, node_locate(element, field).encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
                                             .force_encoding('WINDOWS-1252').encode('UTF-8')]
          end.to_h
        end
        private_class_method :product_by
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
br_invoices_pdf-0.2.24 lib/br_invoices_pdf/nfce/parser/products.rb
br_invoices_pdf-0.2.23 lib/br_invoices_pdf/nfce/parser/products.rb
br_invoices_pdf-0.2.22 lib/br_invoices_pdf/nfce/parser/products.rb
br_invoices_pdf-0.2.21 lib/br_invoices_pdf/nfce/parser/products.rb
br_invoices_pdf-0.2.20 lib/br_invoices_pdf/nfce/parser/products.rb
br_invoices_pdf-0.2.19 lib/br_invoices_pdf/nfce/parser/products.rb
br_invoices_pdf-0.2.18 lib/br_invoices_pdf/nfce/parser/products.rb
br_invoices_pdf-0.2.17 lib/br_invoices_pdf/nfce/parser/products.rb