br_danfe 0.14.4 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.tool-versions +1 -0
  3. data/Gemfile.lock +48 -33
  4. data/Guardfile +2 -2
  5. data/README.md +39 -0
  6. data/br_danfe.gemspec +5 -4
  7. data/config/locales/pt-BR.yml +1 -0
  8. data/lib/br_danfe.rb +1 -1
  9. data/lib/br_danfe/danfe.rb +8 -5
  10. data/lib/br_danfe/danfe_lib/base.rb +2 -2
  11. data/lib/br_danfe/danfe_lib/nfce.rb +14 -12
  12. data/lib/br_danfe/danfe_lib/nfe.rb +39 -27
  13. data/lib/br_danfe/danfe_lib/nfe_lib/dest.rb +1 -1
  14. data/lib/br_danfe/danfe_lib/nfe_lib/emit_header.rb +5 -5
  15. data/lib/br_danfe/document_builder.rb +1 -1
  16. data/lib/br_danfe/helper.rb +11 -7
  17. data/lib/br_danfe/mdfe.rb +55 -0
  18. data/lib/br_danfe/mdfe_lib/authorization_protocol.rb +24 -0
  19. data/lib/br_danfe/mdfe_lib/document.rb +26 -0
  20. data/lib/br_danfe/mdfe_lib/drivers.rb +70 -0
  21. data/lib/br_danfe/mdfe_lib/fisco_control.rb +40 -0
  22. data/lib/br_danfe/mdfe_lib/header.rb +78 -0
  23. data/lib/br_danfe/mdfe_lib/mdfe_identification.rb +69 -0
  24. data/lib/br_danfe/mdfe_lib/notes.rb +94 -0
  25. data/lib/br_danfe/mdfe_lib/totalizer.rb +57 -0
  26. data/lib/br_danfe/mdfe_lib/vehicles.rb +80 -0
  27. data/lib/br_danfe/qr_code.rb +22 -0
  28. data/lib/br_danfe/version.rb +1 -1
  29. data/spec/br_danfe/danfe_lib/nfce_spec.rb +12 -1
  30. data/spec/br_danfe/danfe_lib/nfe_lib/emit_header_spec.rb +2 -2
  31. data/spec/br_danfe/danfe_lib/nfe_lib/infadic_spec.rb +3 -3
  32. data/spec/br_danfe/danfe_lib/nfe_spec.rb +17 -1
  33. data/spec/br_danfe/danfe_spec.rb +16 -0
  34. data/spec/br_danfe/helper_spec.rb +12 -0
  35. data/spec/br_danfe/mdfe_lib/authorization_protocol_spec.rb +35 -0
  36. data/spec/br_danfe/mdfe_lib/document_spec.rb +20 -0
  37. data/spec/br_danfe/mdfe_lib/drivers_spec.rb +88 -0
  38. data/spec/br_danfe/mdfe_lib/fisco_control_spec.rb +55 -0
  39. data/spec/br_danfe/mdfe_lib/header_spec.rb +126 -0
  40. data/spec/br_danfe/mdfe_lib/mdfe_identification_spec.rb +104 -0
  41. data/spec/br_danfe/mdfe_lib/notes_spec.rb +97 -0
  42. data/spec/br_danfe/mdfe_lib/totalizer_spec.rb +63 -0
  43. data/spec/br_danfe/mdfe_lib/vehicles_spec.rb +66 -0
  44. data/spec/br_danfe/mdfe_spec.rb +46 -0
  45. data/spec/br_danfe/{danfe_lib/nfce_lib/qr_code_spec.rb → qr_code_spec.rb} +10 -5
  46. data/spec/fixtures/cce/lib/barcode#render.pdf +5 -3
  47. data/spec/fixtures/cce/v1.00/cce.fixture.pdf +15 -13
  48. data/spec/fixtures/mdfe/lib/document#render.pdf +0 -0
  49. data/spec/fixtures/mdfe/lib/document#text.align.pdf +0 -0
  50. data/spec/fixtures/mdfe/lib/document#text.pad.pdf +0 -0
  51. data/spec/fixtures/mdfe/lib/document#text.simple.pdf +0 -0
  52. data/spec/fixtures/mdfe/lib/document#text.size.pdf +0 -0
  53. data/spec/fixtures/mdfe/lib/document#text.style.pdf +0 -0
  54. data/spec/fixtures/mdfe/lib/fisco_control#barcode.pdf +0 -0
  55. data/spec/fixtures/mdfe/lib/header#render-qr-code.pdf +0 -0
  56. data/spec/fixtures/mdfe/lib/header#render-with_logo.pdf +0 -0
  57. data/spec/fixtures/mdfe/lib/header#render-without_logo.pdf +0 -0
  58. data/spec/fixtures/mdfe/lib/mdfe-with-multiple-pages.pdf +1945 -0
  59. data/spec/fixtures/mdfe/lib/mdfe.fixture.pdf +1286 -0
  60. data/spec/fixtures/mdfe/lib/notes#render-big-aditional-information.pdf +1943 -0
  61. data/spec/fixtures/mdfe/lib/xml-mdfe-multiples-pages.xml +116 -0
  62. data/spec/fixtures/mdfe/lib/xml-mdfe.xml +250 -0
  63. data/spec/fixtures/nfce/lib/document#render.pdf +17 -17
  64. data/spec/fixtures/nfce/lib/footer#render-with_additional_information.pdf +12 -12
  65. data/spec/fixtures/nfce/lib/footer#render.pdf +10 -10
  66. data/spec/fixtures/nfce/lib/header#render-homologation.pdf +0 -0
  67. data/spec/fixtures/nfce/lib/header#render-long_name_with_logo.pdf +0 -0
  68. data/spec/fixtures/nfce/lib/header#render-long_name_without_logo.pdf +16 -16
  69. data/spec/fixtures/nfce/lib/header#render-short_name_with_logo.pdf +0 -0
  70. data/spec/fixtures/nfce/lib/header#render-short_name_without_logo.pdf +16 -16
  71. data/spec/fixtures/nfce/lib/key#render.pdf +14 -14
  72. data/spec/fixtures/nfce/lib/nfce_identification#render.pdf +16 -16
  73. data/spec/fixtures/nfce/lib/product_list#render-with_many_products.pdf +43 -43
  74. data/spec/fixtures/nfce/lib/product_list#render-with_many_products_and_long_names.pdf +47 -47
  75. data/spec/fixtures/nfce/lib/product_list#render-with_single_product.pdf +25 -25
  76. data/spec/fixtures/nfce/lib/product_list#render-with_single_product_and_long_name.pdf +27 -27
  77. data/spec/fixtures/nfce/lib/qr_code#render.pdf +0 -0
  78. data/spec/fixtures/nfce/lib/recipient#render-company.pdf +13 -13
  79. data/spec/fixtures/nfce/lib/recipient#render-consumer_without_document.pdf +12 -12
  80. data/spec/fixtures/nfce/lib/recipient#render-foreign.pdf +13 -13
  81. data/spec/fixtures/nfce/lib/recipient#render-individual.pdf +13 -13
  82. data/spec/fixtures/nfce/lib/recipient#render-unidentified_consumer.pdf +11 -11
  83. data/spec/fixtures/nfce/lib/total_list#does_not_render-payment_methods.pdf +19 -19
  84. data/spec/fixtures/nfce/lib/total_list#render-grouped_payment_methods.pdf +26 -26
  85. data/spec/fixtures/nfce/lib/total_list#render-totals.pdf +19 -19
  86. data/spec/fixtures/nfce/lib/total_list#render-without_payment.pdf +28 -28
  87. data/spec/fixtures/nfce/lib/total_list#render.pdf +28 -28
  88. data/spec/fixtures/nfce/v4.00/multiples_xmls_on_the_same_pdf.pdf +1229 -0
  89. data/spec/fixtures/nfce/v4.00/nfce-unauthorized-hom.fixture.pdf +0 -0
  90. data/spec/fixtures/nfce/v4.00/nfce-unauthorized-prod.fixture.pdf +0 -0
  91. data/spec/fixtures/nfce/v4.00/rendered_nfce.fixture.pdf +0 -0
  92. data/spec/fixtures/nfce/v4.00/saved_nfce.fixture.pdf +0 -0
  93. data/spec/fixtures/nfe/lib/dest#render-v2.00.pdf +0 -0
  94. data/spec/fixtures/nfe/lib/dest#render-v3.10.pdf +0 -0
  95. data/spec/fixtures/nfe/lib/dest#render-v4.00.pdf +0 -0
  96. data/spec/fixtures/nfe/lib/dest#render-with-address-bigger.pdf +0 -0
  97. data/spec/fixtures/nfe/lib/dest#render-with_cnpj.pdf +0 -0
  98. data/spec/fixtures/nfe/lib/dest#render-with_cpf.pdf +0 -0
  99. data/spec/fixtures/nfe/lib/dest#render-with_ie.pdf +0 -0
  100. data/spec/fixtures/nfe/lib/dest#render-without-address-complement.pdf +0 -0
  101. data/spec/fixtures/nfe/lib/det_body#render-csosn.pdf +662 -662
  102. data/spec/fixtures/nfe/lib/det_body#render-cst.pdf +410 -410
  103. data/spec/fixtures/nfe/lib/det_body#render-fci.pdf +272 -272
  104. data/spec/fixtures/nfe/lib/det_body#render-icms_st.pdf +272 -272
  105. data/spec/fixtures/nfe/lib/det_body#render-quantity_with_custom_precision.pdf +270 -270
  106. data/spec/fixtures/nfe/lib/det_body#render-three_pages.pdf +6213 -6213
  107. data/spec/fixtures/nfe/lib/det_body#render-two_pages.pdf +6083 -6083
  108. data/spec/fixtures/nfe/lib/det_body#render-unit_price_with_custom_precision.pdf +270 -270
  109. data/spec/fixtures/nfe/lib/det_body#render-with_infadprod.pdf +272 -272
  110. data/spec/fixtures/nfe/lib/det_body#render-with_issqn.pdf +2859 -2859
  111. data/spec/fixtures/nfe/lib/det_body#render-without_issqn.pdf +2859 -2859
  112. data/spec/fixtures/nfe/lib/document#lcnpj-blank.pdf +0 -0
  113. data/spec/fixtures/nfe/lib/document#lcnpj-invalid.pdf +0 -0
  114. data/spec/fixtures/nfe/lib/document#lcnpj-valid.pdf +0 -0
  115. data/spec/fixtures/nfe/lib/document#lie-blank-uf-invalid.pdf +0 -0
  116. data/spec/fixtures/nfe/lib/document#lie-blank.pdf +0 -0
  117. data/spec/fixtures/nfe/lib/document#lie-invalid.pdf +0 -0
  118. data/spec/fixtures/nfe/lib/document#lie-valid.pdf +0 -0
  119. data/spec/fixtures/nfe/lib/dup#render.pdf +0 -0
  120. data/spec/fixtures/nfe/lib/emit_header#render-second_page.pdf +511 -509
  121. data/spec/fixtures/nfe/lib/emit_header#render-with_logo.pdf +510 -508
  122. data/spec/fixtures/nfe/lib/emit_header#render-without_logo.pdf +507 -505
  123. data/spec/fixtures/nfe/lib/icmstot#render.pdf +0 -0
  124. data/spec/fixtures/nfe/lib/infadic#address-shipment.pdf +0 -0
  125. data/spec/fixtures/nfe/lib/infadic#render-all_the_informations.pdf +0 -0
  126. data/spec/fixtures/nfe/lib/infadic#render-difal.pdf +0 -0
  127. data/spec/fixtures/nfe/lib/infadic#render-extra_volume.pdf +0 -0
  128. data/spec/fixtures/nfe/lib/infadic#render-with_complementary_information.pdf +0 -0
  129. data/spec/fixtures/nfe/lib/infadic#render-with_fisco_additional_information.pdf +0 -0
  130. data/spec/fixtures/nfe/lib/infadic#render-with_street_data.pdf +0 -0
  131. data/spec/fixtures/nfe/lib/infadic#render.pdf +0 -0
  132. data/spec/fixtures/nfe/lib/infadic_vol#render-extra_volume.pdf +0 -0
  133. data/spec/fixtures/nfe/lib/issqn#render-with_issqn.pdf +0 -0
  134. data/spec/fixtures/nfe/lib/issqn#render-with_one_issqn_value.pdf +0 -0
  135. data/spec/fixtures/nfe/lib/ticket#render.pdf +0 -0
  136. data/spec/fixtures/nfe/lib/transp#render-modfrete_0.pdf +0 -0
  137. data/spec/fixtures/nfe/lib/transp#render-modfrete_1.pdf +0 -0
  138. data/spec/fixtures/nfe/lib/transp#render-modfrete_2.pdf +0 -0
  139. data/spec/fixtures/nfe/lib/transp#render-modfrete_3.pdf +0 -0
  140. data/spec/fixtures/nfe/lib/transp#render-modfrete_4.pdf +0 -0
  141. data/spec/fixtures/nfe/lib/transp#render-modfrete_9.pdf +0 -0
  142. data/spec/fixtures/nfe/lib/transp#render.pdf +0 -0
  143. data/spec/fixtures/nfe/lib/vol#render-blank-boxes.pdf +0 -0
  144. data/spec/fixtures/nfe/lib/vol#render.pdf +0 -0
  145. data/spec/fixtures/nfe/v2.00/custom_options.fixture.pdf +733 -733
  146. data/spec/fixtures/nfe/v2.00/nfe_simples_nacional.xml.fixture.pdf +730 -730
  147. data/spec/fixtures/nfe/v2.00/nfe_with_extra_volumes.xml.fixture.pdf +3449 -3445
  148. data/spec/fixtures/nfe/v2.00/nfe_with_fci.xml.fixture.pdf +929 -927
  149. data/spec/fixtures/nfe/v2.00/nfe_with_ns.xml.fixture.pdf +3424 -3420
  150. data/spec/fixtures/nfe/v2.00/nfe_without_ns.xml.fixture.pdf +1178 -1176
  151. data/spec/fixtures/nfe/v3.10/multiples.xmls.on.the.same.pdf.pdf +11471 -0
  152. data/spec/fixtures/nfe/v3.10/nfe_simples_nacional.xml.fixture.pdf +1495 -1495
  153. data/spec/fixtures/nfe/v3.10/with_footer.fixture.pdf +2444 -2444
  154. data/spec/fixtures/nfe/v3.10/with_issqn.fixture.pdf +2142 -2142
  155. data/spec/fixtures/nfe/v3.10/with_three_pages.fixture.pdf +7096 -7096
  156. data/spec/fixtures/nfe/v3.10/without_issqn.fixture.pdf +2811 -2811
  157. data/spec/fixtures/qr_code/qr_code#render.pdf +0 -0
  158. metadata +90 -39
  159. data/lib/br_danfe/danfe_lib/nfce_lib/qr_code.rb +0 -25
  160. data/lib/br_danfe/danfe_lib/nfe_lib/cep.rb +0 -11
  161. data/spec/br_danfe/danfe_lib/nfe_lib/cep_spec.rb +0 -10
@@ -63,7 +63,7 @@ module BrDanfe
63
63
  end
64
64
 
65
65
  def cep
66
- Cep.format(@xml['enderDest/CEP'])
66
+ BrDanfe::Helper.format_cep(@xml['enderDest/CEP'])
67
67
  end
68
68
 
69
69
  def render_line3
@@ -9,10 +9,10 @@ module BrDanfe
9
9
  @logo_dimensions = logo_dimensions
10
10
  end
11
11
 
12
- def render(page, y_position)
12
+ def render(page, y_position, total)
13
13
  @y_position = y_position
14
14
  company_box
15
- danfe_box page
15
+ danfe_box(page, total)
16
16
  access_key_box
17
17
  sefaz_box
18
18
  render_emit y_position
@@ -50,7 +50,7 @@ module BrDanfe
50
50
  end
51
51
 
52
52
  def cep
53
- Cep.format(@xml['enderEmit/CEP'])
53
+ BrDanfe::Helper.format_cep(@xml['enderEmit/CEP'])
54
54
  end
55
55
 
56
56
  def logo
@@ -64,7 +64,7 @@ module BrDanfe
64
64
  end
65
65
  end
66
66
 
67
- def danfe_box(page)
67
+ def danfe_box(page, total)
68
68
  @pdf.ibox 3.92, 2.08, 8.21, @y_position
69
69
 
70
70
  @pdf.ibox 0.60, 2.08, 8.21, @y_position, '', 'DANFE',
@@ -83,7 +83,7 @@ module BrDanfe
83
83
  size: 8, align: :center, valign: :center, border: 0, style: :bold
84
84
 
85
85
  @pdf.ibox 1.00, 2.08, 8.21, @y_position + 3.00, '',
86
- I18n.t('danfe.others.page', current: page, total: @pdf.page_count),
86
+ I18n.t('danfe.others.page', current: page, total: total),
87
87
  size: 8, align: :center, valign: :center, border: 0, style: :bold
88
88
  end
89
89
 
@@ -7,7 +7,7 @@ module BrDanfe
7
7
  bold: "#{BrDanfe.root_path}/fonts/tinos_bold.ttf",
8
8
  italic: "#{BrDanfe.root_path}/fonts/tinos_italic.ttf",
9
9
  bold_italic: "#{BrDanfe.root_path}/fonts/tinos_bold_italic.ttf",
10
- normal: "#{BrDanfe.root_path}/fonts/tinos.ttf",
10
+ normal: "#{BrDanfe.root_path}/fonts/tinos.ttf"
11
11
  }
12
12
 
13
13
  document
@@ -23,15 +23,19 @@ module BrDanfe
23
23
  integer_part + ',' + decimal_part
24
24
  end
25
25
 
26
- def self.format_datetime(xml_datetime)
27
- formated = ''
26
+ def self.format_datetime(xml_datetime, with_time_zone: false)
27
+ formated = with_time_zone ? '%d/%m/%Y %H:%M:%S%:z' : '%d/%m/%Y %H:%M:%S'
28
+ xml_datetime.present? ? Time.parse(xml_datetime).strftime(formated) : ''
29
+ end
30
+
31
+ def self.nfe?(xml)
32
+ nfe_code = '55'
28
33
 
29
- unless xml_datetime.empty?
30
- date = DateTime.strptime(xml_datetime, '%Y-%m-%dT%H:%M:%S')
31
- formated = date.strftime('%d/%m/%Y %H:%M:%S')
32
- end
34
+ xml['ide > mod'] == nfe_code
35
+ end
33
36
 
34
- formated
37
+ def self.format_cep(cep)
38
+ cep.sub(/(\d{2})(\d{3})(\d{3})/, '\\1.\\2-\\3')
35
39
  end
36
40
  end
37
41
  end
@@ -0,0 +1,55 @@
1
+ module BrDanfe
2
+ BLACK_COLOR = '000000'.freeze
3
+ GRAY_COLOR = 'A0A0A0'.freeze
4
+ LIGHT_GRAY_COLOR = 'ECECEC'.freeze
5
+
6
+ class Mdfe
7
+ attr_reader :logo_options
8
+
9
+ def initialize(xml)
10
+ @xml = xml
11
+ @pdf = MdfeLib::Document.new
12
+ @logo_options = BrDanfe::Logo::Config.new
13
+ end
14
+
15
+ def save_pdf(filename)
16
+ generate
17
+ @pdf.render_file(filename)
18
+ end
19
+
20
+ def render_pdf
21
+ generate
22
+ @pdf.render
23
+ end
24
+
25
+ private
26
+
27
+ def generate
28
+ generate_on_first_page
29
+ generate_on_each_page
30
+ end
31
+
32
+ def generate_on_first_page
33
+ MdfeLib::Totalizer.new(@pdf, @xml).generate
34
+ MdfeLib::AuthorizationProtocol.new(@pdf, @xml).generate
35
+ MdfeLib::FiscoControl.new(@pdf, @xml).generate
36
+ MdfeLib::Vehicles.new(@pdf, @xml).generate
37
+ MdfeLib::Drivers.new(@pdf, @xml).generate
38
+ MdfeLib::Notes.new(@pdf, @xml).generate
39
+ end
40
+
41
+ def generate_on_each_page
42
+ header = MdfeLib::Header.new(@pdf, @xml, @logo_options.logo, @logo_options.logo_dimensions)
43
+ mdfe_identification = MdfeLib::MdfeIdentification.new(@pdf, @xml)
44
+
45
+ @pdf.page_count.times do |i|
46
+ page = i + 1
47
+
48
+ @pdf.go_to_page page
49
+
50
+ header.generate
51
+ mdfe_identification.generate(page)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,24 @@
1
+ module BrDanfe
2
+ module MdfeLib
3
+ class AuthorizationProtocol
4
+ def initialize(pdf, xml)
5
+ @pdf = pdf
6
+ @xml = xml
7
+ end
8
+
9
+ def generate
10
+ authorization
11
+ end
12
+
13
+ private
14
+
15
+ def authorization
16
+ title = 'Protocolo de autorização'
17
+ protocol = @xml['infProt/nProt'] + ' - ' + Helper.format_datetime(@xml['infProt/dhRecbto'], with_time_zone: true)
18
+
19
+ @pdf.text_box(title, size: 9, align: :left, style: :bold, at: [0, 510])
20
+ @pdf.text_box(protocol, size: 11, align: :left, at: [0, 500])
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ module BrDanfe
2
+ module MdfeLib
3
+ class Document
4
+ def initialize
5
+ @document = BrDanfe::DocumentBuilder.build(
6
+ page_size: 'A4',
7
+ page_layout: :portrait
8
+ )
9
+
10
+ @document.font 'tinos'
11
+ end
12
+
13
+ def method_missing(method_name, *args, &block)
14
+ if @document.respond_to? method_name
15
+ @document.send method_name, *args, &block
16
+ else
17
+ super
18
+ end
19
+ end
20
+
21
+ def respond_to_missing?(method_name, include_private = false)
22
+ @document.respond_to?(method_name, include_private) || super
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,70 @@
1
+ module BrDanfe
2
+ module MdfeLib
3
+ class Drivers
4
+ def initialize(pdf, xml)
5
+ @pdf = pdf
6
+ @xml = xml
7
+ end
8
+
9
+ def generate
10
+ @pdf.move_cursor_to 460
11
+ title
12
+ table_titles
13
+ generate_drivers
14
+ end
15
+
16
+ private
17
+
18
+ def title
19
+ title = 'Condutor'
20
+
21
+ @pdf.text_box(title, size: 12, align: :left, style: :bold, at: [250, @pdf.cursor])
22
+ end
23
+
24
+ def table_titles
25
+ cpf = 'CPF'
26
+ name = 'Nome'
27
+
28
+ @pdf.stroke_color GRAY_COLOR
29
+ @pdf.move_down 20
30
+ @pdf.text_box(cpf, size: 9, align: :left, at: [250, @pdf.cursor])
31
+ @pdf.text_box(name, size: 9, align: :left, at: [350, @pdf.cursor])
32
+ @pdf.move_down 10
33
+ @pdf.stroke_horizontal_line(250, 528, at: @pdf.cursor)
34
+ end
35
+
36
+ def generate_drivers
37
+ @pdf.move_cursor_to 425
38
+
39
+ drivers.each_with_index do |driver, index|
40
+ @pdf.bounding_box [250, @pdf.cursor], width: 278, height: 20 do
41
+ @pdf.stroke_color GRAY_COLOR
42
+ @pdf.dash([2], phase: 6)
43
+ @pdf.stroke_horizontal_line(0, 278, at: 26) unless index.zero?
44
+ @pdf.undash
45
+ @pdf.move_down 20
46
+
47
+ driver.each_with_index do |cell, index_driver|
48
+ @pdf.text_box(cell, at: [x_position(index_driver), 20], size: 12)
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ def drivers
55
+ @xml.collect('xmlns', 'condutor') { |rodo| driver(rodo) }
56
+ end
57
+
58
+ def driver(rodo)
59
+ [
60
+ rodo.css('CPF').text,
61
+ rodo.css('xNome').text
62
+ ]
63
+ end
64
+
65
+ def x_position(index)
66
+ index.even? ? 0 : 100
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,40 @@
1
+ module BrDanfe
2
+ module MdfeLib
3
+ class FiscoControl
4
+ def initialize(pdf, xml)
5
+ @pdf = pdf
6
+ @xml = xml
7
+ end
8
+
9
+ def generate
10
+ title
11
+ barcode
12
+ generate_nfe_key
13
+ end
14
+
15
+ private
16
+
17
+ def title
18
+ title = 'CONTROLE DO FISCO'
19
+
20
+ @pdf.text_box(title, size: 9, align: :left, at: [250, 600])
21
+ end
22
+
23
+ def barcode
24
+ barcode = Barby::Code128C.new(nfe_key)
25
+ barcode.annotate_pdf(@pdf, x: 250, y: 530, height: 50)
26
+ end
27
+
28
+ def nfe_key
29
+ @xml['mdfeProc > protMDFe > infProt > chMDFe']
30
+ end
31
+
32
+ def generate_nfe_key
33
+ title = 'Chave de Acesso'
34
+
35
+ @pdf.text_box(title, size: 9, align: :left, style: :bold, at: [250, 510])
36
+ @pdf.text_box(nfe_key, size: 11, align: :left, at: [250, 500])
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,78 @@
1
+ module BrDanfe
2
+ module MdfeLib
3
+ class Header
4
+ def initialize(pdf, xml, logo, logo_dimensions)
5
+ @pdf = pdf
6
+ @xml = xml
7
+ @logo = logo
8
+ @logo_dimensions = logo_dimensions
9
+ end
10
+
11
+ def generate
12
+ generate_emit
13
+ generate_title
14
+ qr_code
15
+ end
16
+
17
+ private
18
+
19
+ def generate_emit
20
+ if @logo.present?
21
+ company(x: 90)
22
+ logo
23
+ else
24
+ company(x: 0)
25
+ end
26
+ end
27
+
28
+ def company(x:)
29
+ @pdf.bounding_box([x, @pdf.cursor], width: 280, height: 80) do
30
+ @pdf.text(@xml['emit/xNome'], size: 12, style: :bold, align: :left)
31
+ @pdf.text(address, align: :left, size: 9)
32
+ @pdf.text(company_informations, size: 9, align: :left, inline_format: true)
33
+ end
34
+ end
35
+
36
+ def address
37
+ formatted = @xml['enderEmit/xLgr'] + ', nº ' + @xml['enderEmit/nro'] + "\n"
38
+ formatted += @xml['enderEmit/xMun'] + ' - ' + @xml['enderEmit/UF'] + ' ' + 'CEP ' + cep + "\n"
39
+ formatted
40
+ end
41
+
42
+ def cep
43
+ Helper.format_cep(@xml['enderEmit/CEP'])
44
+ end
45
+
46
+ def company_informations
47
+ '<b>CNPJ: </b>' + @xml['emit/CNPJ'] + ' ' + '<b>IE: </b>' + @xml['emit/IE']
48
+ end
49
+
50
+ def logo
51
+ bounding_box_size = 80
52
+ logo_options = BrDanfe::Logo::Options.new(bounding_box_size, @logo_dimensions).options
53
+
54
+ @pdf.bounding_box([0, 770], width: bounding_box_size, height: bounding_box_size) do
55
+ @pdf.image @logo, logo_options
56
+ end
57
+ end
58
+
59
+ def generate_title
60
+ title = '<b>DAMDFE</b> - Documento Auxiliar de Manifesto Eletrônico de Documentos Fiscais'
61
+
62
+ @pdf.move_down 15
63
+ @pdf.bounding_box([0, @pdf.cursor], width: 500, height: 20) do
64
+ @pdf.text(title, size: 12, align: :left, inline_format: true)
65
+ end
66
+ end
67
+
68
+ def qr_code
69
+ box_size = 40.mm
70
+ security_margin = box_size + box_size / 10.0
71
+
72
+ @pdf.bounding_box([414, 780], width: security_margin, height: security_margin) do
73
+ BrDanfe::QrCode.new(pdf: @pdf, xml: @xml, qr_code_tag: @xml['qrCodMDFe'], box_size: box_size).render
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,69 @@
1
+ module BrDanfe
2
+ module MdfeLib
3
+ class MdfeIdentification
4
+ def initialize(pdf, xml)
5
+ @pdf = pdf
6
+ @xml = xml
7
+ end
8
+
9
+ def generate(page)
10
+ model
11
+ series
12
+ number
13
+ number_of_pages(page)
14
+ emitted_at
15
+ origin_uf
16
+ destination_uf
17
+ end
18
+
19
+ private
20
+
21
+ def model
22
+ generate_box('Modelo', @xml['ide/mod'], 0)
23
+ end
24
+
25
+ def generate_box(title, text, x_position, width = 40)
26
+ @pdf.move_cursor_to 655
27
+
28
+ @pdf.stroke do
29
+ @pdf.fill_color LIGHT_GRAY_COLOR
30
+ @pdf.fill_rectangle([x_position, @pdf.cursor], width, 35)
31
+ @pdf.fill_color BLACK_COLOR
32
+ end
33
+
34
+ @pdf.bounding_box([x_position, @pdf.cursor], width: width, height: 35) do
35
+ @pdf.move_down 5
36
+ @pdf.text_box(title, size: 10, at: [3, @pdf.cursor])
37
+ @pdf.move_down 15
38
+ @pdf.text_box(text, size: 12, at: [3, @pdf.cursor])
39
+ end
40
+ end
41
+
42
+ def series
43
+ generate_box('Série', @xml['ide/serie'], 40)
44
+ end
45
+
46
+ def number
47
+ generate_box('Número', @xml['ide/nMDF'], 80)
48
+ end
49
+
50
+ def number_of_pages(page)
51
+ text = page.to_s + '/' + @pdf.page_count.to_s
52
+ generate_box('FL', text, 125)
53
+ end
54
+
55
+ def emitted_at
56
+ date = Helper.format_datetime(@xml['dhEmi'])
57
+ generate_box('Data e hora de Emissão', date, 170, 110)
58
+ end
59
+
60
+ def origin_uf
61
+ generate_box('UF Carreg.', @xml['ide/UFIni'], 285, 70)
62
+ end
63
+
64
+ def destination_uf
65
+ generate_box('UF Descarreg.', @xml['ide/UFFim'], 345, 70)
66
+ end
67
+ end
68
+ end
69
+ end