Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Canceled watermark support #219

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ XML version | Supported?

### Options

* `logo_path`: Path of sender's logo image.
* `logo_dimensions`: Dimensions of the logo. Ex: logo_dimensions = { width: 100, height: 90 }
- `canceled`: (Boolean) If the NF-e is canceled.
- `options.logo_path`: Path of sender's logo image.
- `options.logo_dimensions`: Dimensions of the logo.
- Ex: `options.logo_dimensions = { width: 100, height: 90 }`

### CC-e - Carta de Correção Eletrônica

Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pt-BR:
infProt: "PROTOCOLO DE AUTORIZAÇÃO DE USO"
others:
has_no_fiscal_value: "SEM VALOR FISCAL"
canceled: "CANCELADA"
page: "FOLHA %{current} de %{total}"
danfe: "DOCUMENTO AUXILIAR DA NOTA FISCAL ELETRÔNICA"
sefaz: "Consulta de autenticidade no portal nacional da NF-e www.nfe.fazenda.gov.br/portal ou no site da Sefaz Autorizadora"
Expand Down
2 changes: 2 additions & 0 deletions lib/br_danfe/danfe_lib/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module BrDanfe
module DanfeLib
class Base
attr_reader :options
attr_writer :canceled

def initialize(xmls)
@xmls = xmls
@document = document
@options = BrDanfe::Logo::Config.new
@canceled = false
wilfison marked this conversation as resolved.
Show resolved Hide resolved

create_watermark
end
Expand Down
39 changes: 38 additions & 1 deletion lib/br_danfe/danfe_lib/nfce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,52 @@ def generate(footer_info)
render_no_fiscal_value(xml)
resize_page_height
end

render_canceled_watermark

@document
end

def render_no_fiscal_value(xml)
@document.stamp('has_no_fiscal_value') if BrDanfe::Helper.unauthorized?(xml)
@document.stamp('has_no_fiscal_value') if BrDanfe::Helper.unauthorized?(xml) && !@canceled
end

def resize_page_height
@document.page.dictionary.data[:MediaBox] = [0, @document.y - 10, PAGE_WIDTH, PAGE_HEIGHT]
end

def render_canceled_watermark
return unless @canceled

create_cancel_watermark

@document.page_count.times do |i|
@document.go_to_page(i + 1)
@document.canvas do
spacing = 10.cm

([email protected]).step(spacing).each do |x|
([email protected]).step(spacing).each do |y|
@document.stamp_at('canceled', [x, y])
end
end
end
end
end

def create_cancel_watermark
return unless @canceled

@document.create_stamp('canceled') do
@document.fill_color '7d7d7d'
@document.font_size 1.cm
@document.rotate(45, origin: [0, 0]) do
@document.transparent(0.3) do
@document.draw_text I18n.t('danfe.others.canceled'), at: [1.cm, 0]
end
end
end
end
end
wilfison marked this conversation as resolved.
Show resolved Hide resolved
end
end
48 changes: 39 additions & 9 deletions lib/br_danfe/danfe_lib/nfe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ def create_watermark
@document.fill_color '7d7d7d'
@document.text_box(
I18n.t('danfe.others.has_no_fiscal_value'),
size: 2.2.cm,
width: @document.bounds.width,
height: @document.bounds.height,
align: :center,
valign: :center,
at: [0, @document.bounds.height],
rotate: 45,
rotate_around: :center
default_watermark_text_config.merge(size: 2.2.cm)
)
end
end
Expand All @@ -36,6 +29,8 @@ def generate(footer_info)
@document.start_new_page unless index == last_index
end

render_canceled_watermark

@document
end

Expand Down Expand Up @@ -90,7 +85,42 @@ def render_footer_information(footer_info)
end

def render_no_fiscal_value(xml)
@document.stamp('has_no_fiscal_value') if BrDanfe::Helper.no_fiscal_value?(xml)
@document.stamp('has_no_fiscal_value') if BrDanfe::Helper.no_fiscal_value?(xml) && !@canceled
end

def render_canceled_watermark
return unless @canceled

create_canceled_watermark

@document.page_count.times do |i|
@document.go_to_page(i + 1)
@document.stamp('canceled')
end
end

def create_canceled_watermark
@document.create_stamp('canceled') do
@document.fill_color '7d7d7d'
@document.transparent(0.5) do
@document.text_box(
I18n.t('danfe.others.canceled'),
default_watermark_text_config.merge(size: 3.4.cm)
)
end
end
end

def default_watermark_text_config
{
width: @document.bounds.width,
height: @document.bounds.height,
align: :center,
valign: :center,
at: [0, @document.bounds.height],
rotate: 45,
rotate_around: :center
}
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/br_danfe/danfe_lib/nfce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
expect("#{base_dir}saved_nfce.fixture.pdf").to have_same_content_of file: output_pdf
end

it 'saves canceled NFC-e as pdf with' do
expect(File.exist?(output_pdf)).to be_falsey
subject.canceled = true
subject.save_pdf output_pdf

expect("#{base_dir}canceled_nfce.fixture.pdf").to have_same_content_of file: output_pdf
end

context 'when nfc-e is unauthorized' do
context 'when nfc-e is in homologation environment' do
let(:xml) { BrDanfe::XML.new(File.read("#{base_dir}nfce-unauthorized-hom.xml")) }
Expand Down Expand Up @@ -64,6 +72,16 @@

expect("#{base_dir}multiples_xmls_on_the_same_pdf.pdf").to have_same_content_of file: output_pdf
end

it 'renders multiple canceled danfes on the same pdf' do
subject = described_class.new [xml, xml]

expect(File.exist?(output_pdf)).to be_falsey
subject.canceled = true
subject.save_pdf output_pdf

expect("#{base_dir}multiples_canceled_xmls_on_the_same_pdf.pdf").to have_same_content_of file: output_pdf
end
end
end
end
30 changes: 30 additions & 0 deletions spec/br_danfe/danfe_lib/nfe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
expected = IO.binread("#{base_dir}nfe_simples_nacional.xml.fixture.pdf")
expect(subject.render_pdf).to eq expected
end

it 'renders a canceled Simples Nacional NF-e using CSOSN' do
subject.canceled = true
expected = IO.binread("#{base_dir}nfe_simples_nacional_cancel.xml.fixture.pdf")
expect(subject.render_pdf).to eq expected
end
end

describe '#save_pdf' do
Expand Down Expand Up @@ -48,6 +54,14 @@
expect("#{base_dir}nfe_with_ns.xml.fixture.pdf").to have_same_content_of file: output_pdf
end

it 'renders a basic canceled NF-e with namespace' do
expect(File.exist?(output_pdf)).to be_falsey
subject.canceled = true
subject.save_pdf output_pdf

expect("#{base_dir}nfe_with_ns_canceled.fixture.pdf").to have_same_content_of file: output_pdf
end

context 'when NF-e does not have namespace' do
let(:xml_file) { File.read("#{base_dir}nfe_without_ns.xml") }

Expand Down Expand Up @@ -106,6 +120,14 @@
expect("#{base_dir}nfe_simples_nacional.xml.fixture.pdf").to have_same_content_of file: output_pdf
end

it 'renders a canceled Simples Nacional NF-e using CSOSN' do
expect(File.exist?(output_pdf)).to be_falsey
subject.canceled = true
subject.save_pdf output_pdf

expect("#{base_dir}nfe_simples_nacional_cancel.xml.fixture.pdf").to have_same_content_of file: output_pdf
end

context 'when there are more than one page' do
let(:xml_file) { File.read("#{base_dir}with_three_pages.xml") }

Expand All @@ -115,6 +137,14 @@

expect("#{base_dir}with_three_pages.fixture.pdf").to have_same_content_of file: output_pdf
end

it 'renders xml to the pdf with canceled stamp' do
expect(File.exist?(output_pdf)).to be_falsey
subject.canceled = true
subject.save_pdf output_pdf

expect("#{base_dir}with_three_pages_cancel.fixture.pdf").to have_same_content_of file: output_pdf
end
end

context 'when there is ISSQN' do
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.