Skip to content

Commit

Permalink
Add support for attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
corny committed Jan 2, 2025
1 parent 32baa37 commit 9ecb598
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
xrechnung (0.5.0)
xrechnung (0.6.0)
builder (~> 3.2)

GEM
Expand Down
22 changes: 12 additions & 10 deletions lib/xrechnung.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "xrechnung/quantity"
require "xrechnung/id"
require "xrechnung/member_container"
require "xrechnung/additional_document_reference"
require "xrechnung/contact"
require "xrechnung/party_identification"
require "xrechnung/party_legal_entity"
Expand Down Expand Up @@ -34,7 +35,7 @@ class Document

# Default customization specs
DEFAULT_CUSTOMIZATION_ID = "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0"
DEFAULT_PROFILE_ID = "urn:fdc:peppol.eu:2017:poacc:billing:01:1.0"
DEFAULT_PROFILE_ID = "urn:fdc:peppol.eu:2017:poacc:billing:01:1.0"

# Document customization identifier
#
Expand Down Expand Up @@ -187,6 +188,11 @@ class Document
# @return [Xrechnung::InvoiceDocumentReference]
member :billing_reference, type: Xrechnung::InvoiceDocumentReference, optional: true

# Additional supporting documents BG-24
# @!attribute additional_document_references
# @return [Array]
member :additional_document_references, type: Array, default: []

# @!attribute invoice_period
# @return [Xrechnung::InvoicePeriod]
member :invoice_period, type: Xrechnung::InvoicePeriod, optional: true
Expand Down Expand Up @@ -341,6 +347,8 @@ def to_xml(indent: 2, target: "")
end
end

additional_document_references.each { _1.to_xml(xml) }

xml.cac :AccountingSupplierParty do
accounting_supplier_party&.to_xml(xml)
end
Expand All @@ -359,17 +367,13 @@ def to_xml(indent: 2, target: "")
payment_means&.to_xml(xml)
end

unless self.class.members[:payee_party].optional && payee_party.nil?
payee_party&.to_xml(xml)
end
payee_party&.to_xml(xml) unless self.class.members[:payee_party].optional && payee_party.nil?

xml.cac :PaymentTerms do
xml.cbc :Note, payment_terms_note
end

allowance_charges.each do |allowance_charge|
allowance_charge&.to_xml(xml)
end
allowance_charges.each { _1.to_xml(xml) }

xml.cac :TaxTotal do
tax_total&.to_xml(xml)
Expand All @@ -379,9 +383,7 @@ def to_xml(indent: 2, target: "")
legal_monetary_total&.to_xml(xml)
end

invoice_lines.each do |invoice_line|
invoice_line&.to_xml(xml)
end
invoice_lines.each { _1.to_xml(xml) }
end

target
Expand Down
34 changes: 34 additions & 0 deletions lib/xrechnung/additional_document_reference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "xrechnung/attachment"

module Xrechnung
class AdditionalDocumentReference
include MemberContainer

# @!attribute id
# @return [String]
member :id, type: String

# @!attribute document_type
# @return [String]
member :document_type, type: String, optional: true

# @!attribute document_description
# @return [String]
member :document_description, type: String, optional: true

# @!attribute attachment
# @return [Xrechnung::Attachment]
member :attachment, type: Xrechnung::Attachment, optional: true

# noinspection RubyResolve
def to_xml(xml)
xml.cac :AdditionalDocumentReference do
xml.cbc :ID, id
xml.cbc :DocumentType, document_type
xml.cbc :DocumentDescription, document_description

attachment&.to_xml(xml)
end
end
end
end
27 changes: 27 additions & 0 deletions lib/xrechnung/attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "base64"

module Xrechnung
class Attachment
include MemberContainer

# @!attribute mime_code
# @return [String]
member :mime_code, type: String

# @!attribute filename
# @return [String]
member :filename, type: String

member :payload

# noinspection RubyResolve
def to_xml(xml)
xml.cac :Attachment do
xml.cbc :EmbeddedDocumentBinaryObject,
Base64.strict_encode64(payload),
mimeCode: mime_code,
filename: filename
end
end
end
end
2 changes: 1 addition & 1 deletion lib/xrechnung/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Xrechnung
VERSION = "0.5.0".freeze
VERSION = "0.6.0".freeze
end
12 changes: 12 additions & 0 deletions spec/fixtures/ruby/additional_document_reference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def build_additional_document_reference
Xrechnung::AdditionalDocumentReference.new(
id: "CR44123",
document_type: "Consumption report",
document_description: "Lorem Ipsum",
attachment: Xrechnung::Attachment.new(
payload: "Hello World",
mime_code: "text/plain",
filename: "attachment.txt",
),
)
end
8 changes: 8 additions & 0 deletions spec/fixtures/scraps/additional_document_reference.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<cac:AdditionalDocumentReference>
<cbc:ID>CR44123</cbc:ID>
<cbc:DocumentType>Consumption report</cbc:DocumentType>
<cbc:DocumentDescription>Lorem Ipsum</cbc:DocumentDescription>
<cac:Attachment>
<cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain" filename="attachment.txt">SGVsbG8gV29ybGQ=</cbc:EmbeddedDocumentBinaryObject>
</cac:Attachment>
</cac:AdditionalDocumentReference>
6 changes: 5 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ def to_xml(entity)
end

def expect_xml_eq_fixture(entity, fixture_base_name)
expect(to_xml(entity)).to eq File.read("spec/fixtures/scraps/#{fixture_base_name}.xml")
path = "spec/fixtures/scraps/#{fixture_base_name}.xml"
actual = to_xml(entity)

File.open(path, "w") { _1 << actual } if ENV["WRITE_FIXTURES"] == "1"
expect(actual).to eq File.read(path)
end
8 changes: 8 additions & 0 deletions spec/xrechnung/additional_document_reference_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "spec_helper"
load("spec/fixtures/ruby/additional_document_reference.rb")

RSpec.describe Xrechnung::AdditionalDocumentReference do
it "generates xml" do
expect_xml_eq_fixture(build_additional_document_reference, "additional_document_reference")
end
end

0 comments on commit 9ecb598

Please sign in to comment.