Skip to content

Commit

Permalink
Merge branch 'master' into update-ox
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagovsk committed Nov 15, 2018
2 parents 243072d + bd4c4f8 commit 58c3f03
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/br_invoices_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ module BrInvoicesPdf
def generate(type, xml, options = {})
generator = @generators[type]
raise(Errors::InvalidDocumentType, type) unless generator

generator.generate(xml, options)
end

def register(type, renderer, parser)
raise(ArgumentError, "Expected Symbol or String to type. Received #{type.class}") unless valid_type?(type)

@generators[type.to_sym] = Generator.new(renderer, parser)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/br_invoices_pdf/cfe/parser/access_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ module AccessKey
def execute(xml)
element = xml.locate('Signature/SignedInfo').first
return unless element

element = element.nodes.last
return unless element

element.attributes[:URI]
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/br_invoices_pdf/cfe/parser/fisco_obs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def execute(xml)
node = element.nodes.first
field = element.attributes[:xCampo]
next unless node && field

{
text: node.text,
field: field
Expand Down
1 change: 1 addition & 0 deletions lib/br_invoices_pdf/cfe/parser/payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def execute(xml)
node_payments = xml.locate('infCFe/pgto')

return unless node_payments

payments_by_nodes(node_payments) if node_payments.any?
end

Expand Down
32 changes: 28 additions & 4 deletions lib/br_invoices_pdf/nfce/parser/payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,40 @@ module Payments
module_function

def execute(xml)
node_payments = xml.locate("#{root_path(xml)}/pag")
xml_version = xml.locate(root_path(xml).to_s)

return payments_new_version(xml) if xml_version.any? && xml_version[0].attributes[:versao][0] == '4'

payments_old_version(xml)
end

def payments_old_version(xml)
node_payments = xml.locate("#{root_path(xml)}/pag")
node_payments.map(&method(:payment_by))
end

def payment_by(element)
def payments_new_version(xml)
node_payments = xml.locate("#{root_path(xml)}/pag/detPag")
cashback = cashback_for(xml)

node_payments.map do |payment|
payment_by(payment, cashback: cashback)
end
end

def cashback_for(xml)
locate_element(xml.locate("#{root_path(xml)}/pag")[0], 'vTroco')
end

def payment_by(element, cashback: nil)
payment = locate_element(element, 'tPag')

cashback_amount = cashback if payment == '01'

{
type: Util::Enum::PAYMENT_TYPES[locate_element(element, 'tPag')],
type: Util::Enum::PAYMENT_TYPES[payment],
amount: locate_element(element, 'vPag'),
cashback: locate_element(element, 'vTroco')
cashback: cashback_amount || locate_element(element, 'vTroco')
}
end
private_class_method :payment_by
Expand Down
1 change: 1 addition & 0 deletions lib/br_invoices_pdf/nfce/renderer/procon_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def execute(pdf, data)
procon_message = data[:additional_variables][:procon_message]

return if ['', nil].include? procon_message

box(pdf, [0, pdf.cursor], page_content_width(pdf)) do
procon_message(pdf, procon_message)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/br_invoices_pdf/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module BrInvoicesPdf
VERSION = '0.2.18'
VERSION = '0.2.20'
end
7 changes: 5 additions & 2 deletions spec/lib/br_invoices_pdf/nfce/parser/payments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
let(:type) { '03' }
let(:amount) { 99.9 }
let(:cashback) { 1.00 }
let(:version) { [double('xml', attributes: { versao: ['3'] })] }

def locate_element_mock(path, value)
base_parser_module = BrInvoicesPdf::Util::XmlLocate
Expand All @@ -17,7 +18,9 @@ def locate_element_mock(path, value)
end

before do
allow(xml).to receive(:locate).and_return([node_payments])
allow(xml).to receive(:locate).with('NFe/infNFe')
.and_return(version)
allow(xml).to receive(:locate).with('NFe/infNFe/pag').and_return([node_payments])
allow(xml).to receive(:name).and_return('protNFe')

locate_element_mock('tPag', type)
Expand All @@ -33,7 +36,7 @@ def locate_element_mock(path, value)

context 'when node_payments is empty' do
before do
allow(xml).to receive(:locate).and_return([])
allow(xml).to receive(:locate).with('NFe/infNFe/pag').and_return([])
end
it { expect(subject).to be_empty }
end
Expand Down

0 comments on commit 58c3f03

Please sign in to comment.