Permalink
Browse files

fix the generation of the nf id and the digit

  • Loading branch information...
rafaelss committed Apr 13, 2012
1 parent efb1dff commit d2dc1a05118b7d2d3f783d975b830877ae2b18d6
View
@@ -1,22 +1,35 @@
module Nfe
module Template
class Ide < Base
- attr_accessor :nfe_id, :c_nf, :c_uf, :nat_op, :ind_pag, :serie, :n_nf, :d_emi, :tp_nf, :c_mun_fg, :tp_imp, :tp_emis, :tp_amb, :fin_nfe, :proc_emi
+ attr_accessor :nfe_id, :c_uf, :nat_op, :ind_pag, :serie, :n_nf, :d_emi, :tp_nf, :c_mun_fg, :tp_imp, :tp_emis, :tp_amb, :fin_nfe, :proc_emi
attr_writer :mod
def mod
@mod || 55
end
+ def c_nf
+ @c_nf ||= rand(10 ** 8).to_s
+ end
+
def c_dv
- sum = 0
- nfe_id.each_char do |c|
- [2, 3, 4, 5, 6, 7, 8, 9].each { |n| sum += c.to_i * n }
- end
+ @c_dv ||= begin
+ multipliers = [2, 3, 4, 5, 6, 7, 8, 9]
+ i = 42
+ sum = 0
+ loop do
+ break if i == 0
+ 0.upto(multipliers.size - 1) do |m|
+ sum += nfe_id[i].to_i * multipliers[m];
- rest = sum % 11
- return 0 if rest == 0 || rest == 1
- 11 - rest
+ break 2 if i == 0
+ i -= 1
+ end
+ end
+ rest = sum % 11;
+ return 0 if [0, 1].include?(rest)
+ 11 - rest
+ end
end
def ver_proc
@@ -31,9 +31,9 @@ def inf_adic
def id
@id ||= "".tap do |str|
- str << UFS[emit.ender_emit.uf].to_s
+ str << "%02d" % ide.c_uf
str << Date.parse(ide.d_emi).strftime("%y%m")
- str << emit.cnpj
+ str << emit.cnpj.gsub(/[^0-9]+/, "")
str << "55"
str << "%03d" % ide.serie
str << "%09d" % ide.n_nf
@@ -6,8 +6,12 @@
context "instance" do
subject { view }
+ before do
+ view.nfe_id = "3511025871652300011955001000000007184037270"
+ end
+
it "returns the digit based on nf's id" do
- subject.c_dv.should == 0
+ subject.c_dv.should == 6
end
it "defaults to 55 if mod is not set" do
@@ -18,6 +22,10 @@
context "rendering" do
subject { view.render }
+ before do
+ view.should_receive(:c_nf).and_return("00300000")
+ end
+
it do
xml("ide", subject).should == xml("ide")
end
@@ -8,14 +8,20 @@
end
context "instance" do
- subject { view }
+ subject do
+ view.tap do |v|
+ v.ide = ide(:d_emi => "2011-02-10", :n_nf => 7)
+ v.ide.should_receive(:c_nf).and_return("84037270")
+ v.emit = emit(:cnpj => "58.716.523.0001/19")
+ end
+ end
its(:id) do
- should == "3510115871652300011955001000000001100300000"
+ should == "3511025871652300011955001000000007184037270"
end
its(:c_dv) do
- should == "0"
+ should == "6"
end
end
View
@@ -204,9 +204,9 @@ def root
end
end
- def emit
+ def emit(attributes = {})
new_view(:emit) do |v|
- v.cnpj = "58.716.523.0001/19"
+ v.cnpj = attributes[:cnpj] || "58.716.523.0001/19"
v.x_nome = "FIMATEC TEXTIL LTDA"
v.x_fant = "FIMATEC"
v.ender_emit = ender_emit
@@ -233,17 +233,16 @@ def ender_emit
end
end
- def ide
+ def ide(attributes = {})
new_view(:ide) do |v|
v.nfe_id = "3510115871652300011955001000000001100300000"
- v.c_nf = "00300000"
v.c_uf = 35
v.nat_op = "VENDA"
v.ind_pag = 0
v.mod = 55
v.serie = 1
- v.n_nf = 1
- v.d_emi = "2010-11-02"
+ v.n_nf = attributes[:n_nf] || 1
+ v.d_emi = attributes[:d_emi] || "2010-11-02"
v.tp_nf = 1
v.c_mun_fg = 3550308
v.tp_imp = 1

0 comments on commit d2dc1a0

Please sign in to comment.