Sha256: a4d00d24b5dd1567b462052cf59189dd010f10ef9317da675f5d160400fbdf20

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module BrInvoicesPdf
  module Cfe
    module Parser
      module ProductsData
        extend Util::XmlLocate

        module_function

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

        def execute(xml)
          node_products = xml.locate('infCFe/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).force_encoding('windows-1252')
                                             .force_encoding('UTF-8')
                                             .encode('UTF-8')]
          end
            .to_h
        end
        private_class_method :product_by
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
br_invoices_pdf-0.2.24 lib/br_invoices_pdf/cfe/parser/products_data.rb
br_invoices_pdf-0.2.23 lib/br_invoices_pdf/cfe/parser/products_data.rb
br_invoices_pdf-0.2.22 lib/br_invoices_pdf/cfe/parser/products_data.rb