Skip to content

Commit

Permalink
Fix and improve duplicates (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatCRL authored Jan 20, 2025
1 parent f5b5d12 commit 97021eb
Show file tree
Hide file tree
Showing 23 changed files with 344 additions and 29 deletions.
6 changes: 3 additions & 3 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ pt-BR:
CNPJ: "CNPJ"
dup:
title: "FATURA / DUPLICATAS"
nDup: "Núm.:"
vDup: "Valor: R$"
dVenc: "Venc.:"
nDup: "NRO"
vDup: "VALOR"
dVenc: "VENCTO"
ICMSTot:
title: "CÁLCULO DO IMPOSTO"
vBC: "BASE DE CÁLCULO DO ICMS"
Expand Down
49 changes: 31 additions & 18 deletions lib/br_danfe/danfe_lib/nfe_lib/dup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Dup
attr_reader :y_position

Y_POSITION = 12.92
DUP_MAX_QUANTITY = 12

def initialize(pdf, xml)
@pdf = pdf
Expand All @@ -16,28 +17,44 @@ def initialize(pdf, xml)
end

def render
@pdf.ititle 0.42, 10.00, 0.75, @ltitle, 'dup.title'
@pdf.ibox 0.85, 19.57, 0.75, @y_position

x = 0.75
y = @y_position
@xml.collect('xmlns', 'dup') do |det|
render_dup(det, x, y)
x += 2.30

@pdf.ititle 0.42, 10.00, x, @ltitle, 'dup.title'

render_titles_and_box(x, y)

@xml.collect('xmlns', 'dup') { _1 }[..(DUP_MAX_QUANTITY - 1)].each_with_index do |det, index|
x = 0.75 unless index != DUP_MAX_QUANTITY / 2

y = if index < DUP_MAX_QUANTITY / 2
@y_position - 0.015
else
@y_position + 0.185
end

render_dup(det, x, y + 0.3)
x += 3.261666667
end
end

private

def render_dup(det, x, y)
@pdf.ibox 0.85, 2.12, x, y, '', I18n.t('danfe.dup.nDup'), italic
@pdf.ibox 0.85, 2.12, x + 0.70, y, '', det.css('nDup').text, normal
@pdf.ibox 0.85, 2.12, x, y + 0.20, '', I18n.t('danfe.dup.dVenc'), italic

@pdf.ibox 0.85, 2.12, x + 0.70, y + 0.20, '', dtduplicata(det), normal
def render_titles_and_box(x, y)
(DUP_MAX_QUANTITY / 2).times do
@pdf.ibox 0.30, 3.261666667, x, y
@pdf.ibox 0.60, 3.261666667, x, y + 0.30
@pdf.ibox 0.85, 1.80, x, y - 0.05, '', I18n.t('danfe.dup.nDup'), normal
@pdf.ibox 0.85, 1.80, x + 0.87, y - 0.05, '', I18n.t('danfe.dup.dVenc'), normal
@pdf.ibox 0.85, 1.80, x + 2.35, y - 0.05, '', I18n.t('danfe.dup.vDup'), normal
x += 3.261666667
end
end

@pdf.ibox 0.85, 2.12, x, y + 0.40, '', I18n.t('danfe.dup.vDup'), italic
@pdf.inumeric 0.85, 1.25, x + 0.70, y + 0.40, '', det.css('vDup').text, normal
def render_dup(det, x, y)
@pdf.ibox 0.85, 2.12, x + 0.1, y, '', det.css('nDup').text, normal
@pdf.ibox 0.85, 2.12, x + 0.75, y, '', dtduplicata(det), normal
@pdf.inumeric 0.85, 2.12, x + 1.1, y, '', det.css('vDup').text, normal
end

def dtduplicata(det)
Expand All @@ -48,10 +65,6 @@ def dtduplicata(det)
def normal
{ size: 6, border: 0 }
end

def italic
normal.merge(style: :italic)
end
end
end
end
Expand Down
28 changes: 23 additions & 5 deletions lib/br_danfe/danfe_lib/nfe_lib/infadic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def render(volumes_number)
render_title
render_subtitle
render_volumes if volumes_number > 1
render_additional_data generate_y_position(volumes_number) if additional_data?
render_additional_data generate_y_position(volumes_number) if complementary? || address? || difal? || fisco? || dup_content.to_s.present?
render_reserved_fisco
end

Expand Down Expand Up @@ -44,6 +44,7 @@ def generate_additional_data
additional_data.push(address_content) if address?
additional_data.push(difal_content) if difal?
additional_data.push(fisco_content) if fisco?
additional_data.push(dup_content) if dup_content.any?
additional_data.join(' * ')
end

Expand Down Expand Up @@ -89,6 +90,27 @@ def fisco?
@xml['infAdic/infAdFisco'].to_s.present?
end

def format_dup_date(_det, dup_date)
dtduplicata = dup_date
"#{dtduplicata[8, 2]}/#{dtduplicata[5, 2]}/#{dtduplicata[0, 4]}"
end

def dup_content
value_dups = []

@xml.collect('xmlns', 'dup') { _1 }[NfeLib::Dup::DUP_MAX_QUANTITY..]&.each_with_index do |det, index|
value = "#{det.css('nDup').text} - #{format_dup_date(det, det.css('dVenc').text)} - R$ #{BrDanfe::Helper.numerify(det.css('vDup').text.to_f)}"

if index.zero?
value_dups.push("Faturas: #{value}")
elsif index.positive?
value_dups.push(value.to_s)
end
end

value_dups
end

def generate_y_position(volumes_number)
if volumes_number > 1
return Y_POSITION + 0.30 + volumes_number * 0.15 + 0.2
Expand All @@ -97,10 +119,6 @@ def generate_y_position(volumes_number)
Y_POSITION + 0.30
end

def additional_data?
complementary? || address? || difal? || fisco?
end

def render_reserved_fisco
@pdf.ibox 2.65, 7.15, 13.20, @y_position, I18n.t('danfe.infAdic.reserved')
end
Expand Down
56 changes: 53 additions & 3 deletions spec/br_danfe/danfe_lib/nfe_lib/dup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,67 @@
<infNFe Id="NFe25111012345678901234550020000134151000134151" versao="2.00">
<cobr>
<dup>
<nDup>1</nDup>
<nDup>001</nDup>
<dVenc>2015-02-13</dVenc>
<vDup>25.56</vDup>
</dup>
<dup>
<nDup>2</nDup>
<nDup>002</nDup>
<dVenc>2015-03-15</dVenc>
<vDup>25.56</vDup>
</dup>
<dup>
<nDup>3</nDup>
<nDup>003</nDup>
<dVenc>2015-04-14</dVenc>
<vDup>25.55</vDup>
</dup>
<dup>
<nDup>004</nDup>
<dVenc>2015-02-13</dVenc>
<vDup>25.56</vDup>
</dup>
<dup>
<nDup>005</nDup>
<dVenc>2015-03-15</dVenc>
<vDup>25.56</vDup>
</dup>
<dup>
<nDup>006</nDup>
<dVenc>2015-04-14</dVenc>
<vDup>25.55</vDup>
</dup>
<dup>
<nDup>007</nDup>
<dVenc>2015-02-13</dVenc>
<vDup>25.56</vDup>
</dup>
<dup>
<nDup>008</nDup>
<dVenc>2015-03-15</dVenc>
<vDup>25.56</vDup>
</dup>
<dup>
<nDup>009</nDup>
<dVenc>2015-04-14</dVenc>
<vDup>25.55</vDup>
</dup>
<dup>
<nDup>010</nDup>
<dVenc>2015-02-13</dVenc>
<vDup>25.56</vDup>
</dup>
<dup>
<nDup>011</nDup>
<dVenc>2015-03-15</dVenc>
<vDup>25.56</vDup>
</dup>
<dup>
<nDup>012</nDup>
<dVenc>2015-04-14</dVenc>
<vDup>25.55</vDup>
</dup>
<dup>
<nDup>013</nDup>
<dVenc>2015-04-14</dVenc>
<vDup>25.55</vDup>
</dup>
Expand Down
Loading

0 comments on commit 97021eb

Please sign in to comment.