br_danfe 0.0.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +26 -3
  3. data/br_danfe.gemspec +1 -0
  4. data/config/locales/pt-BR.yml +14 -0
  5. data/lib/br_danfe/cce.rb +28 -0
  6. data/lib/br_danfe/cce_lib/barcode.rb +24 -0
  7. data/lib/br_danfe/cce_lib/correction.rb +22 -0
  8. data/lib/br_danfe/cce_lib/document.rb +44 -0
  9. data/lib/br_danfe/cce_lib/footer.rb +15 -0
  10. data/lib/br_danfe/cce_lib/header.rb +15 -0
  11. data/lib/br_danfe/cce_lib/nfe_key.rb +23 -0
  12. data/lib/br_danfe/cce_lib/protocol.rb +23 -0
  13. data/lib/br_danfe/danfe.rb +16 -16
  14. data/lib/br_danfe/danfe_lib/cep.rb +9 -0
  15. data/lib/br_danfe/danfe_lib/consts.rb +6 -0
  16. data/lib/br_danfe/danfe_lib/cst.rb +30 -0
  17. data/lib/br_danfe/danfe_lib/dest.rb +86 -0
  18. data/lib/br_danfe/danfe_lib/det_body.rb +75 -0
  19. data/lib/br_danfe/danfe_lib/det_header.rb +40 -0
  20. data/lib/br_danfe/danfe_lib/document.rb +135 -0
  21. data/lib/br_danfe/danfe_lib/dup.rb +52 -0
  22. data/lib/br_danfe/danfe_lib/emit.rb +23 -0
  23. data/lib/br_danfe/danfe_lib/emit_header.rb +82 -0
  24. data/lib/br_danfe/danfe_lib/helper.rb +64 -0
  25. data/lib/br_danfe/danfe_lib/icmstot.rb +33 -0
  26. data/lib/br_danfe/danfe_lib/infadic.rb +80 -0
  27. data/lib/br_danfe/danfe_lib/issqn.rb +24 -0
  28. data/lib/br_danfe/danfe_lib/options.rb +24 -0
  29. data/lib/br_danfe/danfe_lib/phone.rb +13 -0
  30. data/lib/br_danfe/danfe_lib/plate.rb +9 -0
  31. data/lib/br_danfe/danfe_lib/ticket.rb +17 -0
  32. data/lib/br_danfe/danfe_lib/transp.rb +51 -0
  33. data/lib/br_danfe/danfe_lib/vol.rb +41 -0
  34. data/lib/br_danfe/danfe_lib/xml.rb +38 -0
  35. data/lib/br_danfe/danfe_lib/xprod.rb +48 -0
  36. data/lib/br_danfe/version.rb +1 -1
  37. data/lib/br_danfe.rb +1 -1
  38. data/spec/features/cce_spec.rb +28 -0
  39. data/spec/features/danfe_spec.rb +3 -3
  40. data/spec/fixtures/cce/lib/barcode#render.pdf +511 -0
  41. data/spec/fixtures/cce/lib/correction#render.pdf +100 -0
  42. data/spec/fixtures/cce/lib/document#box.pdf +55 -0
  43. data/spec/fixtures/cce/lib/document#box.with.block.pdf +72 -0
  44. data/spec/fixtures/cce/lib/document#text.align.pdf +70 -0
  45. data/spec/fixtures/cce/lib/document#text.pad.pdf +70 -0
  46. data/spec/fixtures/cce/lib/document#text.simple.pdf +70 -0
  47. data/spec/fixtures/cce/lib/document#text.size.pdf +70 -0
  48. data/spec/fixtures/cce/lib/document#text.style.pdf +70 -0
  49. data/spec/fixtures/cce/lib/footer#render.pdf +114 -0
  50. data/spec/fixtures/cce/lib/header#render.pdf +72 -0
  51. data/spec/fixtures/cce/lib/nfe_key#render.pdf +88 -0
  52. data/spec/fixtures/cce/lib/protocol#render.pdf +88 -0
  53. data/spec/fixtures/cce/v1.00/cce.fixture.pdf +659 -0
  54. data/spec/fixtures/cce/v1.00/cce.xml +1 -0
  55. data/spec/fixtures/{lib → nfe/lib}/dest#render-v2.00.pdf +0 -0
  56. data/spec/fixtures/{lib → nfe/lib}/dest#render-v3.10.pdf +0 -0
  57. data/spec/fixtures/{lib → nfe/lib}/dest#render-with_cnpj.pdf +0 -0
  58. data/spec/fixtures/{lib → nfe/lib}/dest#render-with_cpf.pdf +0 -0
  59. data/spec/fixtures/{lib → nfe/lib}/dest#render-with_ie.pdf +0 -0
  60. data/spec/fixtures/{lib → nfe/lib}/det_body#render-csosn.pdf +0 -0
  61. data/spec/fixtures/{lib → nfe/lib}/det_body#render-cst.pdf +0 -0
  62. data/spec/fixtures/{lib → nfe/lib}/det_body#render-fci.pdf +0 -0
  63. data/spec/fixtures/{lib → nfe/lib}/det_body#render-icms_st.pdf +0 -0
  64. data/spec/fixtures/{lib → nfe/lib}/det_header#render.pdf +0 -0
  65. data/spec/fixtures/{lib → nfe/lib}/document#lcnpj-blank.pdf +0 -0
  66. data/spec/fixtures/{lib → nfe/lib}/document#lcnpj-invalid.pdf +0 -0
  67. data/spec/fixtures/{lib → nfe/lib}/document#lcnpj-valid.pdf +0 -0
  68. data/spec/fixtures/{lib → nfe/lib}/document#lie-blank.pdf +0 -0
  69. data/spec/fixtures/{lib → nfe/lib}/document#lie-invalid.pdf +0 -0
  70. data/spec/fixtures/{lib → nfe/lib}/document#lie-valid.pdf +0 -0
  71. data/spec/fixtures/{lib → nfe/lib}/dup#render.pdf +0 -0
  72. data/spec/fixtures/{lib → nfe/lib}/emit#render.pdf +0 -0
  73. data/spec/fixtures/{lib → nfe/lib}/emit_header#render-with_logo.pdf +0 -0
  74. data/spec/fixtures/{lib → nfe/lib}/emit_header#render-without_logo.pdf +0 -0
  75. data/spec/fixtures/{lib → nfe/lib}/icmstot#render.pdf +0 -0
  76. data/spec/fixtures/{lib → nfe/lib}/infadic#render-extra_volume.pdf +0 -0
  77. data/spec/fixtures/{lib → nfe/lib}/infadic#render.pdf +0 -0
  78. data/spec/fixtures/{lib → nfe/lib}/issqn#render.pdf +0 -0
  79. data/spec/fixtures/{lib → nfe/lib}/ticket#render.pdf +0 -0
  80. data/spec/fixtures/{lib → nfe/lib}/transp#render-modfrete_0.pdf +0 -0
  81. data/spec/fixtures/{lib → nfe/lib}/transp#render-modfrete_1.pdf +0 -0
  82. data/spec/fixtures/{lib → nfe/lib}/transp#render-modfrete_2.pdf +0 -0
  83. data/spec/fixtures/{lib → nfe/lib}/transp#render-modfrete_9.pdf +0 -0
  84. data/spec/fixtures/{lib → nfe/lib}/transp#render.pdf +0 -0
  85. data/spec/fixtures/{lib → nfe/lib}/vol#render-blank-boxes.pdf +0 -0
  86. data/spec/fixtures/{lib → nfe/lib}/vol#render.pdf +0 -0
  87. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_simples_nacional.xml +0 -0
  88. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_simples_nacional.xml.fixture.pdf +0 -0
  89. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_with_extra_volumes.xml +0 -0
  90. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_with_extra_volumes.xml.fixture.pdf +0 -0
  91. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_with_fci.xml +0 -0
  92. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_with_fci.xml.fixture.pdf +0 -0
  93. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_with_logo.xml +0 -0
  94. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_with_ns.xml +0 -0
  95. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_with_ns.xml.fixture.pdf +0 -0
  96. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_without_ns.xml +0 -0
  97. data/spec/fixtures/{v2.00 → nfe/v2.00}/nfe_without_ns.xml.fixture.pdf +0 -0
  98. data/spec/fixtures/{v3.10 → nfe/v3.10}/nfe_simples_nacional.xml +0 -0
  99. data/spec/fixtures/{v3.10 → nfe/v3.10}/nfe_simples_nacional.xml.fixture.pdf +0 -0
  100. data/spec/lib/cce_lib/barcode_spec.rb +26 -0
  101. data/spec/lib/cce_lib/correction_spec.rb +26 -0
  102. data/spec/lib/cce_lib/document_spec.rb +115 -0
  103. data/spec/lib/cce_lib/footer_spec.rb +25 -0
  104. data/spec/lib/cce_lib/header_spec.rb +25 -0
  105. data/spec/lib/cce_lib/nfe_key_spec.rb +26 -0
  106. data/spec/lib/cce_lib/protocol_spec.rb +26 -0
  107. data/spec/lib/{cep_spec.rb → danfe_lib/cep_spec.rb} +2 -2
  108. data/spec/lib/{cst_spec.rb → danfe_lib/cst_spec.rb} +3 -3
  109. data/spec/lib/{dest_spec.rb → danfe_lib/dest_spec.rb} +4 -4
  110. data/spec/lib/{det_body_spec.rb → danfe_lib/det_body_spec.rb} +4 -4
  111. data/spec/lib/{det_header_spec.rb → danfe_lib/det_header_spec.rb} +4 -4
  112. data/spec/lib/{document_spec.rb → danfe_lib/document_spec.rb} +3 -3
  113. data/spec/lib/{dup_spec.rb → danfe_lib/dup_spec.rb} +4 -4
  114. data/spec/lib/{emit_header_spec.rb → danfe_lib/emit_header_spec.rb} +4 -4
  115. data/spec/lib/{emit_spec.rb → danfe_lib/emit_spec.rb} +4 -4
  116. data/spec/lib/{helper_spec.rb → danfe_lib/helper_spec.rb} +11 -11
  117. data/spec/lib/{icmstot_spec.rb → danfe_lib/icmstot_spec.rb} +4 -4
  118. data/spec/lib/{infadic_spec.rb → danfe_lib/infadic_spec.rb} +4 -4
  119. data/spec/lib/{issqn_spec.rb → danfe_lib/issqn_spec.rb} +4 -4
  120. data/spec/lib/{options_spec.rb → danfe_lib/options_spec.rb} +3 -3
  121. data/spec/lib/{phone_spec.rb → danfe_lib/phone_spec.rb} +3 -3
  122. data/spec/lib/{plate_spec.rb → danfe_lib/plate_spec.rb} +2 -2
  123. data/spec/lib/{ticket_spec.rb → danfe_lib/ticket_spec.rb} +4 -4
  124. data/spec/lib/{transp_spec.rb → danfe_lib/transp_spec.rb} +4 -4
  125. data/spec/lib/{vol_spec.rb → danfe_lib/vol_spec.rb} +4 -4
  126. data/spec/lib/{xml_spec.rb → danfe_lib/xml_spec.rb} +2 -2
  127. data/spec/lib/{xprod_spec.rb → danfe_lib/xprod_spec.rb} +5 -5
  128. metadata +242 -174
  129. data/lib/br_danfe/cep.rb +0 -7
  130. data/lib/br_danfe/consts.rb +0 -4
  131. data/lib/br_danfe/cst.rb +0 -28
  132. data/lib/br_danfe/dest.rb +0 -84
  133. data/lib/br_danfe/det_body.rb +0 -73
  134. data/lib/br_danfe/det_header.rb +0 -38
  135. data/lib/br_danfe/document.rb +0 -133
  136. data/lib/br_danfe/dup.rb +0 -50
  137. data/lib/br_danfe/emit.rb +0 -21
  138. data/lib/br_danfe/emit_header.rb +0 -80
  139. data/lib/br_danfe/helper.rb +0 -62
  140. data/lib/br_danfe/icmstot.rb +0 -31
  141. data/lib/br_danfe/infadic.rb +0 -78
  142. data/lib/br_danfe/issqn.rb +0 -22
  143. data/lib/br_danfe/options.rb +0 -22
  144. data/lib/br_danfe/phone.rb +0 -11
  145. data/lib/br_danfe/plate.rb +0 -7
  146. data/lib/br_danfe/ticket.rb +0 -15
  147. data/lib/br_danfe/transp.rb +0 -49
  148. data/lib/br_danfe/vol.rb +0 -39
  149. data/lib/br_danfe/xml.rb +0 -36
  150. data/lib/br_danfe/xprod.rb +0 -46
@@ -0,0 +1,135 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Document
4
+ def initialize
5
+ @document = Prawn::Document.new(
6
+ page_size: "A4",
7
+ page_layout: :portrait,
8
+ left_margin: 0,
9
+ right_margin: 0,
10
+ top_margin: 0,
11
+ botton_margin: 0)
12
+
13
+ @document.font "Times-Roman"
14
+ @document.line_width = 0.3
15
+ end
16
+
17
+ def method_missing(method_name, *args, &block)
18
+ @document.send(method_name, *args, &block)
19
+ end
20
+
21
+ def ititle(h, w, x, y, i18n)
22
+ title = ""
23
+ title = I18n.t("danfe.#{i18n}") if i18n != ""
24
+
25
+ self.text_box title, size: 8, at: [x.cm, Helper.invert(y.cm) - 4], width: w.cm, height: h.cm, style: :bold
26
+ end
27
+
28
+ def ibarcode(h, w, x, y, info)
29
+ Barby::Code128C.new(info).annotate_pdf(self, x: x.cm, y: Helper.invert(y.cm), width: w.cm, height: h.cm) if info != ""
30
+ end
31
+
32
+ def ibox(h, w, x, y, title = "", info = "", options = {})
33
+ box [x.cm, Helper.invert(y.cm)], w.cm, h.cm, title, info, options
34
+ end
35
+
36
+ def lbox(h, w, x, y, xml, xpath, options = {})
37
+ i18n = xpath.gsub("/", ".");
38
+ label = I18n.t("danfe.#{i18n}")
39
+ data = xml[xpath]
40
+
41
+ ibox(h, w, x, y, label, data, options)
42
+ end
43
+
44
+ def ldate(h, w, x, y, i18n = "", info = "", options = {})
45
+ data = Helper::format_date(info)
46
+ i18n_lbox(h, w, x, y, i18n, data, options)
47
+ end
48
+
49
+ def ltime(h, w, x, y, i18n = "", info = "", options = {})
50
+ data = Helper::format_time(info)
51
+ i18n_lbox(h, w, x, y, i18n, data, options)
52
+ end
53
+
54
+ def lcnpj(h, w, x, y, xml, xpath, options = {})
55
+ i18n = xpath.gsub("/", ".");
56
+
57
+ cnpj = BrDocuments::CnpjCpf::Cnpj.new(xml[xpath])
58
+ if cnpj.valid?
59
+ data = cnpj.formatted
60
+ else
61
+ data = ""
62
+ end
63
+
64
+ i18n_lbox(h, w, x, y, i18n, data, options)
65
+ end
66
+
67
+ def lie(h, w, x, y, xml, xpath_uf, xpath_ie, options = {})
68
+ i18n = xpath_ie.gsub("/", ".");
69
+
70
+ data = ""
71
+ if xml[xpath_uf].present? #BrDocument bug - review this in the future
72
+ ie = BrDocuments::IE::Factory.create(xml[xpath_uf], xml[xpath_ie])
73
+ data = ie.formatted if ie.valid?
74
+ end
75
+
76
+ i18n_lbox(h, w, x, y, i18n, data, options)
77
+ end
78
+
79
+ def lnumeric(h, w, x, y, xml, xpath, options = {})
80
+ i18n = xpath.gsub("/", ".");
81
+ data = xml[xpath]
82
+
83
+ inumeric(h, w, x, y, i18n, data, options)
84
+ end
85
+
86
+ def inumeric(h, w, x, y, i18n = "", data = "", options = {})
87
+ label = ""
88
+ label = I18n.t("danfe.#{i18n}") if i18n != ""
89
+
90
+ numeric [x.cm, Helper.invert(y.cm)], w.cm, h.cm, label, data, options
91
+ end
92
+
93
+ def i18n_lbox(h, w, x, y, i18n = "", info = "", options = {})
94
+ label = ""
95
+ label = I18n.t("danfe.#{i18n}") if i18n != ""
96
+
97
+ ibox h, w, x, y, label, info, options
98
+ end
99
+
100
+ private
101
+ def numeric(at, w, h, title = "", info = "", options = {})
102
+ options = {
103
+ decimals: 2
104
+ }.merge(options)
105
+
106
+ info = Helper.numerify(info, options[:decimals]) if info != ""
107
+ box at, w, h, title, info, options.merge({align: :right})
108
+ end
109
+
110
+ def box(at, w, h, title = "", info = "", options = {})
111
+ options = {
112
+ align: :left,
113
+ size: 10,
114
+ style: nil,
115
+ valign: :top,
116
+ border: 1
117
+ }.merge(options)
118
+
119
+ self.stroke_rectangle at, w, h if options[:border] == 1
120
+
121
+ at[0] += 2
122
+
123
+ if title != ""
124
+ title_coord = Array.new(at)
125
+ title_coord[1] -= 2
126
+ self.text_box title, size: 6, at: title_coord, width: w - 4, height: 8
127
+ end
128
+
129
+ title_adjustment = title == "" ? 4 : 13
130
+ at[1] -= title_adjustment
131
+ self.text_box info, size: options[:size], at: at, width: w - 4, height: h - title_adjustment, align: options[:align], style: options[:style], valign: options[:valign]
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,52 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Dup
4
+ Y = 11.50
5
+
6
+ def initialize(pdf, xml)
7
+ @pdf = pdf
8
+ @xml = xml
9
+
10
+ @ltitle = Y - 0.42
11
+ end
12
+
13
+ def render
14
+ @pdf.ititle 0.42, 10.00, 0.25, @ltitle, "dup.title"
15
+ @pdf.ibox 0.85, 20.57, 0.25, Y
16
+
17
+ x = 0.25
18
+ y = Y
19
+ @xml.collect("xmlns", "dup") do |det|
20
+ render_dup(det, x, y)
21
+ x += 2.30
22
+ end
23
+ end
24
+
25
+ private
26
+ def render_dup(det, x, y)
27
+ @pdf.ibox 0.85, 2.12, x, y, "", I18n.t("danfe.dup.nDup"), italic
28
+ @pdf.ibox 0.85, 2.12, x + 0.70, y, "", det.css("nDup").text, normal
29
+ @pdf.ibox 0.85, 2.12, x, y + 0.20, "", I18n.t("danfe.dup.dVenc"), italic
30
+
31
+ @pdf.ibox 0.85, 2.12, x + 0.70, y + 0.20, "", dtduplicata(det), normal
32
+
33
+ @pdf.ibox 0.85, 2.12, x, y + 0.40, "", I18n.t("danfe.dup.vDup"), italic
34
+ @pdf.inumeric 0.85, 1.25, x + 0.70, y + 0.40, "", det.css("vDup").text, normal
35
+ end
36
+
37
+ def dtduplicata(det)
38
+ dtduplicata = det.css("dVenc").text
39
+ dtduplicata = dtduplicata[8,2] + "/" + dtduplicata[5, 2] + "/" + dtduplicata[0, 4]
40
+ dtduplicata
41
+ end
42
+
43
+ def normal
44
+ { size: 6, border: 0 }
45
+ end
46
+
47
+ def italic
48
+ normal.merge({ style: :italic })
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Emit
4
+ Y = 6.46
5
+
6
+ def initialize(pdf, xml)
7
+ @pdf = pdf
8
+ @xml = xml
9
+
10
+ @l1 = Y
11
+ @l2 = Y + LINE_HEIGHT
12
+ end
13
+
14
+ def render
15
+ @pdf.lbox LINE_HEIGHT, 10.54, 0.25, @l1, @xml, "ide/natOp"
16
+ @pdf.ibox LINE_HEIGHT, 10.02, 10.79, @l1, I18n.t("danfe.infProt"), @xml["infProt/nProt"] + " " + Helper.format_datetime(@xml["infProt/dhRecbto"]), { align: :center }
17
+ @pdf.lie LINE_HEIGHT, 6.86, 0.25, @l2, @xml, "enderEmit/UF", "emit/IE"
18
+ @pdf.lie LINE_HEIGHT, 6.86, 7.11, @l2, @xml, "enderEmit/UF", "emit/IE_ST"
19
+ @pdf.lcnpj LINE_HEIGHT, 6.84, 13.97, @l2, @xml, "emit/CNPJ"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,82 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class EmitHeader
4
+ def initialize(pdf, xml, logo_path)
5
+ @pdf = pdf
6
+ @xml = xml
7
+ @logo_path = logo_path
8
+ end
9
+
10
+ def render
11
+ address_box
12
+ danfe_box
13
+ access_key_box
14
+ sefaz_box
15
+ end
16
+
17
+ private
18
+ def address_box
19
+ @pdf.ibox 3.92, 8.46, 0.25, 2.54
20
+
21
+ @pdf.ibox 3.92, 8.46, 0.25, 2.80, "", @xml["emit/xNome"],
22
+ { size: 12, align: :center, border: 0, style: :bold }
23
+
24
+ if @logo_path.empty?
25
+ @pdf.ibox 3.92, 8.46, 0.75, 4, "", address, { align: :left, border: 0 }
26
+ else
27
+ @pdf.ibox 3.92, 8.46, 2.75, 4, "", address,
28
+ { size: 8, align: :left, border: 0 }
29
+
30
+ @pdf.image @logo_path, at: [0.5.cm, Helper.invert(4.cm)],
31
+ width: 2.cm
32
+ end
33
+ end
34
+
35
+ def address
36
+ formatted = @xml["enderEmit/xLgr"] + ", " + @xml["enderEmit/nro"] + "\n"
37
+ formatted += @xml["enderEmit/xBairro"] + " - " + cep + "\n"
38
+ formatted += @xml["enderEmit/xMun"] + "/" + @xml["enderEmit/UF"] + "\n"
39
+ formatted += phone + " " + @xml["enderEmit/email"]
40
+
41
+ formatted
42
+ end
43
+
44
+ def phone
45
+ Phone.format(@xml["enderEmit/fone"])
46
+ end
47
+
48
+ def cep
49
+ Cep.format(@xml["enderEmit/CEP"])
50
+ end
51
+
52
+ def danfe_box
53
+ @pdf.ibox 3.92, 2.08, 8.71, 2.54
54
+
55
+ @pdf.ibox 0.60, 2.08, 8.71, 2.54, "", "DANFE",
56
+ { size: 12, align: :center, border: 0, style: :bold }
57
+
58
+ @pdf.ibox 1.20, 2.08, 8.71, 3.14, "", I18n.t("danfe.others.danfe"),
59
+ { size: 8, align: :center, border: 0 }
60
+
61
+ @pdf.ibox 0.60, 2.08, 8.71, 4.34, "", "#{@xml['ide/tpNF']} - " + (@xml["ide/tpNF"] == "0" ? I18n.t("danfe.ide.tpNF.entry") : I18n.t("danfe.ide.tpNF.departure")),
62
+ { size: 8, align: :center, border: 0 }
63
+
64
+ @pdf.ibox 1.00, 2.08, 8.71, 4.94, "",
65
+ I18n.t("danfe.ide.document", nNF: @xml["ide/nNF"], serie: @xml["ide/serie"]),
66
+ { size: 8, align: :center, valign: :center, border: 0, style: :bold }
67
+ end
68
+
69
+ def access_key_box
70
+ @pdf.ibox 2.22, 10.02, 10.79, 2.54
71
+ @pdf.ibarcode 1.50, 8.00, 10.9010, 4.44, @xml["chNFe"]
72
+ @pdf.ibox 0.85, 10.02, 10.79, 4.76, I18n.t("danfe.chNFe"), @xml["chNFe"].gsub(/(\d)(?=(\d\d\d\d)+(?!\d))/, "\\1 "),
73
+ { style: :bold, align: :center }
74
+ end
75
+
76
+ def sefaz_box
77
+ @pdf.ibox 0.85, 10.02, 10.79, 5.61, "", I18n.t("danfe.others.sefaz"),
78
+ { align: :center, size: 8 }
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,64 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Helper
4
+ def self.numerify(number, decimals = 2)
5
+ return "" if !number || number == ""
6
+ int, frac = ("%.#{decimals}f" % number).split(".")
7
+ int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1\.")
8
+ int + "," + frac
9
+ end
10
+
11
+ def self.invert(y)
12
+ 28.7.cm - y
13
+ end
14
+
15
+ def self.format_datetime(xml_datetime)
16
+ formated = ""
17
+
18
+ if !xml_datetime.empty?
19
+ date = DateTime.strptime(xml_datetime, "%Y-%m-%dT%H:%M:%S")
20
+ formated = date.strftime("%d/%m/%Y %H:%M:%S")
21
+ end
22
+
23
+ formated
24
+ end
25
+
26
+ def self.format_date(xml_datetime)
27
+ formated = ""
28
+
29
+ if !xml_datetime.empty?
30
+ date = DateTime.strptime(xml_datetime, "%Y-%m-%d")
31
+ formated = date.strftime("%d/%m/%Y")
32
+ end
33
+
34
+ formated
35
+ end
36
+
37
+ def self.format_time(xml_datetime)
38
+ formated = ""
39
+
40
+ if xml_datetime.length == 8
41
+ formated = xml_datetime
42
+ elsif xml_datetime.length > 8
43
+ date = DateTime.strptime(xml_datetime, "%Y-%m-%dT%H:%M:%S %Z").to_time
44
+ formated = date.strftime("%H:%M:%S")
45
+ end
46
+
47
+ formated
48
+ end
49
+
50
+ def self.has_no_fiscal_value?(xml)
51
+ homologation?(xml) || unauthorized?(xml)
52
+ end
53
+
54
+ private
55
+ def self.homologation?(xml)
56
+ xml.css("nfeProc/NFe/infNFe/ide/tpAmb").text == "2"
57
+ end
58
+
59
+ def self.unauthorized?(xml)
60
+ xml.css("nfeProc/protNFe/infProt/dhRecbto").empty?
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,33 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Icmstot
4
+ Y = 12.35 + SPACE_BETWEEN_GROUPS
5
+
6
+ def initialize(pdf, xml)
7
+ @pdf = pdf
8
+ @xml = xml
9
+
10
+ @ltitle = Y - 0.42
11
+ @l1 = Y
12
+ @l2 = Y + LINE_HEIGHT
13
+ end
14
+
15
+ def render
16
+ @pdf.ititle 0.42, 5.60, 0.25, @ltitle, "ICMSTot.title"
17
+
18
+ @pdf.lnumeric LINE_HEIGHT, 4.06, 0.25, @l1, @xml, "ICMSTot/vBC"
19
+ @pdf.lnumeric LINE_HEIGHT, 4.06, 4.31, @l1, @xml, "ICMSTot/vICMS"
20
+ @pdf.lnumeric LINE_HEIGHT, 4.06, 8.37, @l1, @xml, "ICMSTot/vBCST"
21
+ @pdf.lnumeric LINE_HEIGHT, 4.06, 12.43, @l1, @xml, "ICMSTot/vST"
22
+ @pdf.lnumeric LINE_HEIGHT, 4.32, 16.49, @l1, @xml, "ICMSTot/vProd"
23
+
24
+ @pdf.lnumeric LINE_HEIGHT, 3.05, 0.25, @l2, @xml, "ICMSTot/vFrete"
25
+ @pdf.lnumeric LINE_HEIGHT, 3.05, 3.30, @l2, @xml, "ICMSTot/vSeg"
26
+ @pdf.lnumeric LINE_HEIGHT, 3.04, 6.35, @l2, @xml, "ICMSTot/vDesc"
27
+ @pdf.lnumeric LINE_HEIGHT, 3.04, 9.39, @l2, @xml, "ICMSTot/vOutro"
28
+ @pdf.lnumeric LINE_HEIGHT, 4.06, 12.43, @l2, @xml, "ICMSTot/vIPI"
29
+ @pdf.lnumeric LINE_HEIGHT, 4.32, 16.49, @l2, @xml, "ICMSTot/vNF", style: :bold
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,80 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Infadic
4
+ Y = 25.92 + SPACE_BETWEEN_GROUPS
5
+
6
+ def initialize(pdf, xml)
7
+ @pdf = pdf
8
+ @xml = xml
9
+
10
+ @ltitle = Y - 0.41
11
+ @l1 = Y
12
+ end
13
+
14
+ def render(nVol)
15
+ @pdf.ititle 0.42, 10.00, 0.25, @ltitle, "infAdic.title"
16
+
17
+ if nVol > 1
18
+ render_extra_volumes
19
+ else
20
+ @pdf.ibox 3.07, 12.93, 0.25, @l1, I18n.t("danfe.infAdic.infCpl"), @xml["infAdic/infCpl"], { size: 6, valign: :top }
21
+ end
22
+
23
+ @pdf.ibox 3.07, 7.62, 13.19, @l1, I18n.t("danfe.infAdic.reserved")
24
+ end
25
+
26
+ private
27
+ def render_extra_volumes
28
+ @pdf.ibox 3.07, 12.93, 0.25, @l1, I18n.t("danfe.infAdic.infCpl"), "", { size: 8, valign: :top }
29
+ @pdf.ibox 3.07, 12.93, 0.25, Y + 0.27, "", I18n.t("danfe.infAdic.vol.title"), { size: 5, valign: :top, border: 0 }
30
+
31
+ volumes = 0
32
+ y = Y + 0.34
33
+ @xml.collect("xmlns", "vol") do |det|
34
+ volumes += 1
35
+ if volumes > 1
36
+ render_extra_volume(det, y + 0.10)
37
+ y += 0.15
38
+ end
39
+ end
40
+
41
+ @pdf.ibox 2.07, 12.93, 0.25, y + 0.30, "", I18n.t("danfe.infAdic.others"), { size: 6, valign: :top, border: 0 }
42
+ @pdf.ibox 2.07, 12.93, 0.25, y + 0.50, "", @xml["infAdic/infCpl"], { size: 5, valign: :top, border: 0 }
43
+ end
44
+
45
+ def render_extra_volume(det, y)
46
+ render_field "qVol", det, 0.70, 0.25, 0.70, 0.90, y, :text
47
+ render_field "esp", det, 0.50, 1.35, 3.00, 1.75, y, :text
48
+ render_field "marca", det, 0.70, 4.15, 2.00, 4.75, y, :text
49
+ render_field "nVol", det, 1.00, 6.10, 1.00, 6.70, y, :text
50
+ render_field "pesoB", det, 1.30, 7.00, 1.30, 7.00, y, :numeric
51
+ render_field "pesoL", det, 0.90, 8.50, 1.50, 8.50, y, :numeric
52
+ end
53
+
54
+ def render_field(field, det, w1, x1, w2, x2, y, kind)
55
+ label = I18n.t("danfe.infAdic.vol.#{field}")
56
+ value = det.css(field).text
57
+
58
+ @pdf.ibox 0.35, w1, x1, y, "", label, style_normal
59
+
60
+ if kind == :numeric
61
+ @pdf.inumeric 0.35, w2, x2, y, "", value, style_decimal
62
+ else
63
+ @pdf.ibox 0.35, w2, x2, y, "", value, style_italic
64
+ end
65
+ end
66
+
67
+ def style_normal
68
+ { size: 4, border: 0 }
69
+ end
70
+
71
+ def style_italic
72
+ style_normal.merge({ style: :italic })
73
+ end
74
+
75
+ def style_decimal
76
+ style_italic.merge({ decimals: 3 })
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,24 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Issqn
4
+ Y = 24.60 + SPACE_BETWEEN_GROUPS
5
+
6
+ def initialize(pdf, xml)
7
+ @pdf = pdf
8
+ @xml = xml
9
+
10
+ @ltitle = Y - 0.42
11
+ @l1 = Y
12
+ end
13
+
14
+ def render
15
+ @pdf.ititle 0.42, 10.00, 0.25, @ltitle, "issqn.title"
16
+
17
+ @pdf.lbox LINE_HEIGHT, 5.14, 0.25, @l1, @xml, "emit/IM"
18
+ @pdf.lnumeric LINE_HEIGHT, 5.14, 5.39, @l1, @xml, "total/ISSQNtot/vServ"
19
+ @pdf.lnumeric LINE_HEIGHT, 5.14, 10.53, @l1, @xml, "total/ISSQNtot/vBC"
20
+ @pdf.lnumeric LINE_HEIGHT, 5.14, 15.67, @l1, @xml, "total/ISSQNtot/vISS"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Options < OpenStruct
4
+ DEFAULTOPTIONS = {
5
+ logo_path: ""
6
+ }
7
+
8
+ def initialize(new_options={})
9
+ options = DEFAULTOPTIONS.merge(config_yaml_load)
10
+ super options.merge(new_options)
11
+ end
12
+
13
+ private
14
+ def file
15
+ File.exists?("config/br_danfe.yml") ? File.open("config/br_danfe.yml").read : ""
16
+ end
17
+
18
+ def config_yaml_load
19
+ @file_read = YAML.load( file )
20
+ @file_read ? (@file_read["br_danfe"]||{})["options"] : {}
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Phone
4
+ def self.format(phone)
5
+ if phone.length == 10
6
+ phone.sub(/(\d{2})(\d{4})(\d{4})/, "(\\1) \\2-\\3")
7
+ else
8
+ phone.sub(/(\d{2})(\d{5})(\d{4})/, "(\\1) \\2-\\3")
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Plate
4
+ def self.format(plate)
5
+ plate.sub(/(\w{2})(\d{4})/, "\\1-\\2")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Ticket
4
+ def initialize(pdf, xml)
5
+ @pdf = pdf
6
+ @xml = xml
7
+ end
8
+
9
+ def render
10
+ @pdf.ibox 0.85, 16.10, 0.25, 0.42, I18n.t("danfe.ticket.xNome", xNome: @xml["emit/xNome"])
11
+ @pdf.ibox 0.85, 4.10, 0.25, 1.27, I18n.t("danfe.ticket.received_at")
12
+ @pdf.ibox 0.85, 12.00, 4.35, 1.27, I18n.t("danfe.ticket.receiver")
13
+ @pdf.ibox 1.70, 4.50, 16.35, 0.42, "", I18n.t("danfe.ticket.document", nNF: @xml["ide/nNF"], serie: @xml["ide/serie"]), {align: :center, valign: :center}
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,51 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Transp
4
+ Y = 14.47 + SPACE_BETWEEN_GROUPS
5
+
6
+ def initialize(pdf, xml)
7
+ @pdf = pdf
8
+ @xml = xml
9
+
10
+ @ltitle = Y - 0.42
11
+ @l1 = Y
12
+ @l2 = Y + LINE_HEIGHT
13
+ end
14
+
15
+ def render
16
+ @pdf.ititle 0.42, 10.00, 0.25, @ltitle, "transporta.title"
17
+
18
+ @pdf.lbox LINE_HEIGHT, 9.02, 0.25, @l1, @xml, "transporta/xNome"
19
+ @pdf.i18n_lbox LINE_HEIGHT, 2.79, 9.27, @l1, "transp.modFrete.title", mod_frete
20
+ @pdf.lbox LINE_HEIGHT, 1.78, 12.06, @l1, @xml, "veicTransp/RNTC"
21
+ @pdf.i18n_lbox LINE_HEIGHT, 2.29, 13.84, @l1, "veicTransp.placa", plate
22
+ @pdf.lbox LINE_HEIGHT, 0.76, 16.13, @l1, @xml, "veicTransp/UF"
23
+ @pdf.lcnpj LINE_HEIGHT, 3.94, 16.89, @l1, @xml, "transporta/CNPJ"
24
+ @pdf.lbox LINE_HEIGHT, 9.02, 0.25, @l2, @xml, "transporta/xEnder"
25
+ @pdf.lbox LINE_HEIGHT, 6.86, 9.27, @l2, @xml, "transporta/xMun"
26
+ @pdf.lbox LINE_HEIGHT, 0.76, 16.13, @l2, @xml, "transporta/UF"
27
+ @pdf.lie LINE_HEIGHT, 3.94, 16.89, @l2, @xml, "transporta/UF", "transporta/IE"
28
+ end
29
+
30
+ private
31
+ def plate
32
+ Plate.format(@xml["veicTransp/placa"])
33
+ end
34
+
35
+ def mod_frete
36
+ case @xml["transp/modFrete"]
37
+ when "0"
38
+ modality = "emitter"
39
+ when "1"
40
+ modality = "recipient"
41
+ when "2"
42
+ modality = "third_party"
43
+ when "9"
44
+ modality = "no_freight"
45
+ end
46
+
47
+ I18n.t("danfe.transp.modFrete.#{modality}")
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,41 @@
1
+ module BrDanfe
2
+ module DanfeLib
3
+ class Vol
4
+ Y = 16.59
5
+
6
+ def initialize(pdf, xml)
7
+ @pdf = pdf
8
+ @xml = xml
9
+
10
+ @l1 = Y
11
+ end
12
+
13
+ def render
14
+ nVol = 0
15
+
16
+ @xml.collect("xmlns", "vol") do |det|
17
+ nVol += 1
18
+ render_vol(det) if nVol < 2
19
+ end
20
+
21
+ render_blank_boxes if nVol == 0
22
+
23
+ nVol
24
+ end
25
+
26
+ private
27
+ def render_vol(det)
28
+ @pdf.ibox 0.85, 2.92, 0.25, @l1, I18n.t("danfe.vol.qVol"), det.css("qVol").text
29
+ @pdf.ibox 0.85, 3.05, 3.17, @l1, I18n.t("danfe.vol.esp"), det.css("esp").text
30
+ @pdf.ibox 0.85, 3.05, 6.22, @l1, I18n.t("danfe.vol.marca"), det.css("marca").text
31
+ @pdf.ibox 0.85, 4.83, 9.27, @l1, I18n.t("danfe.vol.nVol")
32
+ @pdf.inumeric 0.85, 3.43, 14.10, @l1, "vol.pesoB", det.css("pesoB").text, { decimals: 3 }
33
+ @pdf.inumeric 0.85, 3.30, 17.53, @l1, "vol.pesoL", det.css("pesoL").text, { decimals: 3 }
34
+ end
35
+
36
+ def render_blank_boxes
37
+ render_vol(@xml)
38
+ end
39
+ end
40
+ end
41
+ end