Skip to content

Commit 9388d24

Browse files
authored
Support CPF in emit_header and improve document class and its uses el… (#262)
1 parent af3540d commit 9388d24

34 files changed

+95
-39
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
br_danfe (2.0.0)
4+
br_danfe (2.1.0)
55
barby (= 0.6.9)
66
br_documents (>= 0.1.3)
77
i18n (>= 0.8.6)

config/locales/pt-BR.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ pt-BR:
4747
IM: "INSCRIÇÃO MUNICIPAL"
4848
IE: "INSCRIÇÃO ESTADUAL"
4949
IEST: "INSC.ESTADUAL DO SUBST. TRIBUTÁRIO"
50-
CNPJ: "CNPJ"
50+
CNPJ: "CNPJ/CPF"
51+
CPF: "CNPJ/CPF"
5152
dup:
5253
title: "FATURA / DUPLICATAS"
5354
nDup: "NRO"
@@ -146,8 +147,8 @@ pt-BR:
146147
document: "NF-e\nN°. %{nNF}\nSÉRIE %{serie}"
147148
entrega:
148149
title: "INFORMAÇÕES DO LOCAL DE ENTREGA"
149-
CNPJ: "CNPJ"
150-
CPF: "CPF"
150+
CNPJ: "CNPJ/CPF"
151+
CPF: "CNPJ/CPF"
151152
xNome: "NOME/RAZÃO SOCIAL"
152153
xLgr: "ENDEREÇO"
153154
nro: "NÚMERO"

lib/br_danfe/danfe_lib/nfe_lib/dest.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,8 @@ def render_line1
3434
end
3535

3636
def render_cnpj_cpf
37-
if @xml['dest/CNPJ'] == ''
38-
@pdf.i18n_lbox LINE_HEIGHT, 4.37, 12.57, @l1, 'dest.CPF', cpf
39-
else
40-
@pdf.lcnpj LINE_HEIGHT, 4.37, 12.57, @l1, @xml, 'dest/CNPJ'
41-
end
42-
end
43-
44-
def cpf
45-
cpf = BrDocuments::CnpjCpf::Cpf.new(@xml['dest/CPF'])
46-
cpf.formatted
37+
xpath = @xml['dest/CNPJ'].present? ? 'dest/CNPJ' : 'dest/CPF'
38+
@pdf.lcnpj_cpf LINE_HEIGHT, 4.37, 12.57, @l1, @xml, xpath
4739
end
4840

4941
def render_line2

lib/br_danfe/danfe_lib/nfe_lib/document.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ def ltime(h, w, x, y, i18n = '', info = '', options = {})
6464
i18n_lbox(h, w, x, y, i18n, data, options)
6565
end
6666

67-
def lcnpj(h, w, x, y, xml, xpath, options = {})
67+
def lcnpj_cpf(h, w, x, y, xml, xpath, options = {})
6868
i18n = xpath.tr('/', '.')
69-
70-
cnpj = BrDocuments::CnpjCpf::Cnpj.new(xml[xpath])
71-
data = if cnpj.valid?
72-
cnpj.formatted
69+
document_class = xpath.include?('CNPJ') ? BrDocuments::CnpjCpf::Cnpj : BrDocuments::CnpjCpf::Cpf
70+
cnpj_cpf = document_class.new(xml[xpath])
71+
data = if cnpj_cpf.valid?
72+
cnpj_cpf.formatted
7373
else
7474
''
7575
end

lib/br_danfe/danfe_lib/nfe_lib/emit_header.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ def render_emit(y_position)
108108

109109
@pdf.lie LINE_HEIGHT, 6.36, 0.75, y_second_line, @xml, 'enderEmit/UF', 'emit/IE'
110110
@pdf.lie LINE_HEIGHT, 6.86, 7.11, y_second_line, @xml, 'enderDest/UF', 'emit/IEST'
111-
@pdf.lcnpj LINE_HEIGHT, 6.34, 13.97, y_second_line, @xml, 'emit/CNPJ'
111+
@pdf.lcnpj_cpf LINE_HEIGHT, 6.34, 13.97, y_second_line, @xml, cnpj_cpf
112+
end
113+
114+
def cnpj_cpf
115+
if @xml['emit/CNPJ'].present?
116+
'emit/CNPJ'
117+
else
118+
'emit/CPF'
119+
end
112120
end
113121
end
114122
end

lib/br_danfe/danfe_lib/nfe_lib/entrega.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,8 @@ def render_line1
3939
end
4040

4141
def render_cnpj_cpf
42-
if @xml['entrega/CNPJ'] == ''
43-
@pdf.i18n_lbox LINE_HEIGHT, 4.37, 12.57, @l1, 'entrega.CPF', cpf
44-
else
45-
@pdf.lcnpj LINE_HEIGHT, 4.37, 12.57, @l1, @xml, 'entrega/CNPJ'
46-
end
47-
end
48-
49-
def cpf
50-
cpf = BrDocuments::CnpjCpf::Cpf.new(@xml['entrega/CPF'])
51-
cpf.formatted
42+
xpath = @xml['entrega/CNPJ'].present? ? 'entrega/CNPJ' : 'entrega/CPF'
43+
@pdf.lcnpj_cpf LINE_HEIGHT, 4.37, 12.57, @l1, @xml, xpath
5244
end
5345

5446
def render_line2

lib/br_danfe/danfe_lib/nfe_lib/transp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def render
2525
@pdf.lbox LINE_HEIGHT, 1.78, 12.06, @l1, @xml, 'veicTransp/RNTC'
2626
@pdf.i18n_lbox LINE_HEIGHT, 2.29, 13.84, @l1, 'veicTransp.placa', plate
2727
@pdf.lbox LINE_HEIGHT, 0.76, 16.13, @l1, @xml, 'veicTransp/UF'
28-
@pdf.lcnpj LINE_HEIGHT, 3.44, 16.89, @l1, @xml, 'transporta/CNPJ'
28+
@pdf.lcnpj_cpf LINE_HEIGHT, 3.44, 16.89, @l1, @xml, 'transporta/CNPJ'
2929
@pdf.lbox LINE_HEIGHT, 8.52, 0.75, @l2, @xml, 'transporta/xEnder'
3030
@pdf.lbox LINE_HEIGHT, 6.86, 9.27, @l2, @xml, 'transporta/xMun'
3131
@pdf.lbox LINE_HEIGHT, 0.76, 16.13, @l2, @xml, 'transporta/UF'

lib/br_danfe/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module BrDanfe
2-
VERSION = '2.0.0'.freeze
2+
VERSION = '2.1.0'.freeze
33
end

spec/br_danfe/danfe_lib/nfe_lib/document_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
end
115115
end
116116

117-
describe '#lcnpj' do
117+
describe '#lcnpj_cpf' do
118118
before do
119119
subject.render
120120
FileUtils.rm_f(output_pdf)
@@ -138,10 +138,10 @@
138138
it 'renders a box with a formated CNPJ to the pdf' do
139139
expect(File.exist?(output_pdf)).to be_falsey
140140

141-
subject.lcnpj 0.80, 3.94, 0.75, 1.85, xml, 'transporta/CNPJ'
141+
subject.lcnpj_cpf 0.80, 3.94, 0.75, 1.85, xml, 'transporta/CNPJ'
142142
subject.render_file output_pdf
143143

144-
expect("#{base_dir}document#lcnpj-valid.pdf").to have_same_content_of file: output_pdf
144+
expect("#{base_dir}document#lcnpj_cpf-valid.pdf").to have_same_content_of file: output_pdf
145145
end
146146
end
147147

@@ -163,10 +163,10 @@
163163
it 'renders a blank box to the pdf' do
164164
expect(File.exist?(output_pdf)).to be_falsey
165165

166-
subject.lcnpj 0.80, 3.94, 0.75, 1.85, xml, 'transporta/CNPJ'
166+
subject.lcnpj_cpf 0.80, 3.94, 0.75, 1.85, xml, 'transporta/CNPJ'
167167
subject.render_file output_pdf
168168

169-
expect("#{base_dir}document#lcnpj-invalid.pdf").to have_same_content_of file: output_pdf
169+
expect("#{base_dir}document#lcnpj_cpf-invalid.pdf").to have_same_content_of file: output_pdf
170170
end
171171
end
172172

@@ -187,10 +187,10 @@
187187
it 'renders a blank box to the pdf' do
188188
expect(File.exist?(output_pdf)).to be_falsey
189189

190-
subject.lcnpj 0.80, 3.94, 0.75, 1.85, xml, 'transporta/CNPJ'
190+
subject.lcnpj_cpf 0.80, 3.94, 0.75, 1.85, xml, 'transporta/CNPJ'
191191
subject.render_file output_pdf
192192

193-
expect("#{base_dir}document#lcnpj-blank.pdf").to have_same_content_of file: output_pdf
193+
expect("#{base_dir}document#lcnpj_cpf-blank.pdf").to have_same_content_of file: output_pdf
194194
end
195195
end
196196
end

spec/br_danfe/danfe_lib/nfe_lib/emit_header_spec.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,68 @@
114114
end
115115
end
116116
end
117+
118+
context 'when emitter has CPF instead of CNPJ' do
119+
let(:xml_as_string) do
120+
<<~XML
121+
<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.00">
122+
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
123+
<infNFe Id="NFe25111012345678901234550020000134151000134151" versao="2.00">
124+
<ide>
125+
<tpNF>1</tpNF>
126+
<nNF>1</nNF>
127+
<serie>1</serie>
128+
<natOp>Vendas de producao do estabelecimento</natOp>
129+
</ide>
130+
<emit>
131+
<xNome>Nome do Remetente</xNome>
132+
<CPF>86893723008</CPF>
133+
<IE>526926313553</IE>
134+
<enderEmit>
135+
<xLgr>Rua do Remetente, Casa</xLgr>
136+
<nro>123</nro>
137+
<xBairro>Bairro do Remetente</xBairro>
138+
<xMun>SAO PAULO</xMun>
139+
<UF>SP</UF>
140+
<CEP>12345678</CEP>
141+
<fone>1112345678</fone>
142+
<email>foo@bar.com</email>
143+
</enderEmit>
144+
</emit>
145+
<det nItem="1">
146+
<prod>
147+
<detExport>
148+
<exportInd>
149+
<chNFe>41211285493849000105550020000027171182910239</chNFe>
150+
</exportInd>
151+
</detExport>
152+
</prod>
153+
</det>
154+
</infNFe>
155+
</NFe>
156+
<protNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.00">
157+
<infProt Id="ID325110012866320">
158+
<chNFe>25111012345678901234550020000134151000134151</chNFe>
159+
<dhRecbto>2011-10-29T14:37:09</dhRecbto>
160+
<nProt>325110012866320</nProt>
161+
</infProt>
162+
</protNFe>
163+
</nfeProc>
164+
XML
165+
end
166+
167+
before do
168+
subject.render 1, 3.96, 1
169+
FileUtils.rm_f(output_pdf)
170+
end
171+
172+
it 'renders xml with CPF to the pdf' do
173+
expect(File.exist?(output_pdf)).to be_falsey
174+
175+
pdf.render_file output_pdf
176+
177+
expect("#{base_dir}emit_header#render-with_cpf.pdf").to have_same_content_of file: output_pdf
178+
end
179+
end
117180
end
118181
end

0 commit comments

Comments
 (0)