zugpferd 0.3.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/setup-schemas +186 -0
- data/lib/zugpferd/cii/mapping.rb +37 -1
- data/lib/zugpferd/cii/reader.rb +57 -10
- data/lib/zugpferd/cii/writer.rb +103 -4
- data/lib/zugpferd/model/billing_document.rb +16 -2
- data/lib/zugpferd/model/line_item.rb +4 -1
- data/lib/zugpferd/model/payment_instructions.rb +3 -2
- data/lib/zugpferd/model/postal_address.rb +6 -6
- data/lib/zugpferd/model/preceding_invoice.rb +17 -0
- data/lib/zugpferd/model/price.rb +2 -1
- data/lib/zugpferd/model/trade_party.rb +3 -1
- data/lib/zugpferd/pdf/catalog_af.ps +21 -0
- data/lib/zugpferd/pdf/embedder.rb +6 -0
- data/lib/zugpferd/pdf/extractor.rb +318 -0
- data/lib/zugpferd/pdf.rb +1 -0
- data/lib/zugpferd/ubl/mapping.rb +47 -1
- data/lib/zugpferd/ubl/reader.rb +40 -4
- data/lib/zugpferd/ubl/writer.rb +99 -14
- data/lib/zugpferd/validation/pdf_validator.rb +11 -7
- data/lib/zugpferd/validation/schema_validator.rb +4 -4
- data/lib/zugpferd.rb +5 -4
- metadata +9 -9
|
@@ -19,7 +19,12 @@ module Zugpferd
|
|
|
19
19
|
# @return [String] BT-3 Invoice type code
|
|
20
20
|
# @return [String] BT-5 Document currency code (default: "EUR")
|
|
21
21
|
# @return [Date, nil] BT-72 Actual delivery date
|
|
22
|
+
# @return [PostalAddress, nil] BG-15 Deliver to address
|
|
23
|
+
# @return [Array<PrecedingInvoice>] BG-3 Preceding invoice references
|
|
24
|
+
# @return [Date, nil] BT-73 Invoicing period start date
|
|
25
|
+
# @return [Date, nil] BT-74 Invoicing period end date
|
|
22
26
|
# @return [String, nil] BT-10 Buyer reference
|
|
27
|
+
# @return [String, nil] BT-13 Purchase order reference
|
|
23
28
|
# @return [String, nil] BT-24 Specification identifier
|
|
24
29
|
# @return [String, nil] BT-23 Business process type
|
|
25
30
|
# @return [String, nil] BT-22 Invoice note
|
|
@@ -30,11 +35,19 @@ module Zugpferd
|
|
|
30
35
|
# @return [MonetaryTotals, nil] BG-22 Document totals
|
|
31
36
|
# @return [PaymentInstructions, nil] BG-16 Payment information
|
|
32
37
|
# @return [Array<AllowanceCharge>] BG-20/BG-21 Document-level allowances and charges
|
|
38
|
+
# @return [String, nil] BT-11 Project reference
|
|
39
|
+
# @return [String, nil] BT-12 Contract reference
|
|
40
|
+
# @return [String, nil] BT-14 Sales order reference
|
|
41
|
+
# @return [String, nil] BT-19 Buyer accounting reference
|
|
33
42
|
attr_accessor :number, :issue_date, :due_date, :type_code,
|
|
34
|
-
:currency_code, :delivery_date, :
|
|
43
|
+
:currency_code, :delivery_date, :deliver_to_address,
|
|
44
|
+
:invoicing_period_start_date, :invoicing_period_end_date,
|
|
45
|
+
:buyer_reference, :purchase_order_reference,
|
|
35
46
|
:customization_id, :profile_id, :note, :seller, :buyer,
|
|
36
47
|
:line_items, :tax_breakdown, :monetary_totals,
|
|
37
|
-
:payment_instructions, :allowance_charges
|
|
48
|
+
:payment_instructions, :allowance_charges, :preceding_invoices,
|
|
49
|
+
:contract_reference, :seller_order_reference, :project_reference,
|
|
50
|
+
:buyer_accounting_reference
|
|
38
51
|
|
|
39
52
|
# @param number [String] BT-1 Invoice number
|
|
40
53
|
# @param issue_date [Date] BT-2 Issue date
|
|
@@ -49,6 +62,7 @@ module Zugpferd
|
|
|
49
62
|
@currency_code = currency_code
|
|
50
63
|
@line_items = []
|
|
51
64
|
@allowance_charges = []
|
|
65
|
+
@preceding_invoices = []
|
|
52
66
|
@tax_breakdown = nil
|
|
53
67
|
rest.each { |k, v| public_send(:"#{k}=", v) }
|
|
54
68
|
end
|
|
@@ -9,10 +9,13 @@ module Zugpferd
|
|
|
9
9
|
# @return [String] BT-130 Unit of measure code
|
|
10
10
|
# @return [BigDecimal] BT-131 Line extension amount
|
|
11
11
|
# @return [String, nil] BT-127 Invoice line note
|
|
12
|
+
# @return [Date, nil] BT-134 Invoice line period start date
|
|
13
|
+
# @return [Date, nil] BT-135 Invoice line period end date
|
|
12
14
|
# @return [Item, nil] BG-31 Item information
|
|
13
15
|
# @return [Price, nil] BG-29 Price details
|
|
14
16
|
attr_accessor :id, :invoiced_quantity, :unit_code,
|
|
15
|
-
:line_extension_amount, :note, :
|
|
17
|
+
:line_extension_amount, :note, :period_start_date,
|
|
18
|
+
:period_end_date, :item, :price
|
|
16
19
|
|
|
17
20
|
# @param id [String] BT-126 Line identifier
|
|
18
21
|
# @param invoiced_quantity [String, BigDecimal] BT-129 Quantity
|
|
@@ -6,14 +6,15 @@ module Zugpferd
|
|
|
6
6
|
# @return [String] BT-81 Payment means type code
|
|
7
7
|
# @return [String, nil] BT-83 Remittance information
|
|
8
8
|
# @return [String, nil] BT-84 Payment account identifier (IBAN)
|
|
9
|
-
# @return [String, nil] BT-
|
|
9
|
+
# @return [String, nil] BT-85 Payment account name
|
|
10
|
+
# @return [String, nil] BT-20 Payment terms
|
|
10
11
|
# @return [String, nil] BT-87 Payment card primary account number
|
|
11
12
|
# @return [String, nil] BT-88 Payment card holder name
|
|
12
13
|
# @return [String, nil] Card network ID (UBL-only, required in CardAccount)
|
|
13
14
|
# @return [String, nil] BT-89 Mandate reference identifier
|
|
14
15
|
# @return [String, nil] BT-90 Bank assigned creditor identifier
|
|
15
16
|
# @return [String, nil] BT-91 Debited account identifier
|
|
16
|
-
attr_accessor :payment_means_code, :payment_id, :account_id, :note,
|
|
17
|
+
attr_accessor :payment_means_code, :payment_id, :account_id, :account_name, :note,
|
|
17
18
|
:card_account_id, :card_holder_name, :card_network_id,
|
|
18
19
|
:mandate_reference, :creditor_reference_id,
|
|
19
20
|
:debited_account_id
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
module Zugpferd
|
|
2
2
|
module Model
|
|
3
|
-
# Postal address (BG-5 / BG-8).
|
|
3
|
+
# Postal address (BG-5 / BG-8 / BG-15).
|
|
4
4
|
class PostalAddress
|
|
5
|
-
# @return [String, nil] BT-35/BT-50 Street name
|
|
6
|
-
# @return [String, nil] BT-37/BT-52 City name
|
|
7
|
-
# @return [String, nil] BT-38/BT-53 Postal zone
|
|
8
|
-
# @return [String] BT-40/BT-55 Country code
|
|
5
|
+
# @return [String, nil] BT-35/BT-50/BT-75 Street name
|
|
6
|
+
# @return [String, nil] BT-37/BT-52/BT-77 City name
|
|
7
|
+
# @return [String, nil] BT-38/BT-53/BT-78 Postal zone
|
|
8
|
+
# @return [String] BT-40/BT-55/BT-80 Country code
|
|
9
9
|
attr_accessor :street_name, :city_name, :postal_zone, :country_code
|
|
10
10
|
|
|
11
|
-
# @param country_code [String] BT-40/BT-55 Country code (ISO 3166-1 alpha-2)
|
|
11
|
+
# @param country_code [String] BT-40/BT-55/BT-80 Country code (ISO 3166-1 alpha-2)
|
|
12
12
|
# @param rest [Hash] additional attributes set via accessors
|
|
13
13
|
def initialize(country_code:, **rest)
|
|
14
14
|
@country_code = country_code
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Zugpferd
|
|
2
|
+
module Model
|
|
3
|
+
# Reference to a preceding invoice (BG-3).
|
|
4
|
+
class PrecedingInvoice
|
|
5
|
+
# @return [String] BT-25 Preceding invoice number
|
|
6
|
+
# @return [Date, nil] BT-26 Preceding invoice issue date
|
|
7
|
+
attr_accessor :number, :issue_date
|
|
8
|
+
|
|
9
|
+
# @param number [String] BT-25 Preceding invoice number
|
|
10
|
+
# @param rest [Hash] additional attributes set via accessors
|
|
11
|
+
def initialize(number:, **rest)
|
|
12
|
+
@number = number
|
|
13
|
+
rest.each { |k, v| public_send(:"#{k}=", v) }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/zugpferd/model/price.rb
CHANGED
|
@@ -6,7 +6,8 @@ module Zugpferd
|
|
|
6
6
|
class Price
|
|
7
7
|
# @return [BigDecimal] BT-146 Item net price
|
|
8
8
|
# @return [BigDecimal, nil] BT-149 Item price base quantity
|
|
9
|
-
|
|
9
|
+
# @return [String, nil] BT-150 Item price base quantity unit of measure code
|
|
10
|
+
attr_accessor :amount, :base_quantity, :base_quantity_unit_code
|
|
10
11
|
|
|
11
12
|
# @param amount [String, BigDecimal] BT-146 Item net price
|
|
12
13
|
# @param rest [Hash] additional attributes set via accessors
|
|
@@ -10,14 +10,16 @@ module Zugpferd
|
|
|
10
10
|
# @return [String, nil] BT-28/BT-45 Trading name
|
|
11
11
|
# @return [String, nil] BT-29/BT-46 Party identifier
|
|
12
12
|
# @return [String, nil] BT-30/BT-47 Legal registration identifier
|
|
13
|
-
# @return [String, nil] BT-33 Company legal form
|
|
13
|
+
# @return [String, nil] BT-33 Company legal form (seller only)
|
|
14
14
|
# @return [String, nil] BT-31/BT-48 VAT identifier
|
|
15
|
+
# @return [String, nil] BT-32 Seller tax registration identifier (seller only)
|
|
15
16
|
# @return [String, nil] BT-34/BT-49 Electronic address
|
|
16
17
|
# @return [String, nil] BT-34-1/BT-49-1 Electronic address scheme
|
|
17
18
|
# @return [PostalAddress, nil] BG-5/BG-8 Postal address
|
|
18
19
|
# @return [Contact, nil] BG-6/BG-9 Contact information
|
|
19
20
|
attr_accessor :name, :trading_name, :identifier,
|
|
20
21
|
:legal_registration_id, :legal_form, :vat_identifier,
|
|
22
|
+
:tax_registration_identifier,
|
|
21
23
|
:electronic_address, :electronic_address_scheme,
|
|
22
24
|
:postal_address, :contact
|
|
23
25
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
%!PS
|
|
2
|
+
% Adds the Associated Files array to the Catalog for Ghostscript before 10.04.0.
|
|
3
|
+
%
|
|
4
|
+
% PDF/A-3 requires /AF in the Catalog for an embedded file, ISO 19005-3,
|
|
5
|
+
% clause 6.8. zugferd.ps no longer adds it, because Ghostscript 10.04.0 and
|
|
6
|
+
% later emit it themselves. Older versions emit nothing, so validators reject
|
|
7
|
+
% the output.
|
|
8
|
+
%
|
|
9
|
+
% Ghostscript runs this file after zugferd.ps. Named pdfmark objects live for
|
|
10
|
+
% the whole run, so {AFArray} from zugferd.ps is still defined here. The guards
|
|
11
|
+
% mirror zugferd.ps: without the XML file or the ICC profile it creates no
|
|
12
|
+
% {AFArray}.
|
|
13
|
+
/ZUGFeRDXMLFile where {
|
|
14
|
+
pop
|
|
15
|
+
/ZUGFeRDProfile where {
|
|
16
|
+
pop
|
|
17
|
+
revision 10040 lt {
|
|
18
|
+
[ {Catalog} << /AF {AFArray} >> /PUT pdfmark
|
|
19
|
+
} if
|
|
20
|
+
} if
|
|
21
|
+
} if
|
|
@@ -70,6 +70,11 @@ module Zugpferd
|
|
|
70
70
|
File.join(vendor_dir, "default_rgb.icc")
|
|
71
71
|
end
|
|
72
72
|
|
|
73
|
+
# Sets /AF in the Catalog where Ghostscript before 10.04.0 does not.
|
|
74
|
+
def catalog_af_ps_path
|
|
75
|
+
File.expand_path("catalog_af.ps", __dir__)
|
|
76
|
+
end
|
|
77
|
+
|
|
73
78
|
def write_xml_tempfile(xml)
|
|
74
79
|
tmpfile = Tempfile.new(["zugferd", ".xml"])
|
|
75
80
|
tmpfile.binmode
|
|
@@ -118,6 +123,7 @@ module Zugpferd
|
|
|
118
123
|
cmd << "-o"
|
|
119
124
|
cmd << output_path
|
|
120
125
|
cmd << ps_path
|
|
126
|
+
cmd << catalog_af_ps_path
|
|
121
127
|
cmd << pdf_path
|
|
122
128
|
cmd
|
|
123
129
|
end
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
require "nokogiri"
|
|
2
|
+
require "set"
|
|
3
|
+
require "zlib"
|
|
4
|
+
|
|
5
|
+
module Zugpferd
|
|
6
|
+
module PDF
|
|
7
|
+
# Extracts the embedded invoice XML from a ZUGFeRD / Factur-X / XRechnung
|
|
8
|
+
# hybrid PDF.
|
|
9
|
+
#
|
|
10
|
+
# Needs the pdf-reader gem, which zugpferd does not install for you:
|
|
11
|
+
#
|
|
12
|
+
# gem "pdf-reader", "~> 2.16"
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
15
|
+
# result = Zugpferd::PDF::Extractor.new.extract(pdf_path: "rechnung.pdf")
|
|
16
|
+
# result.filename # => "factur-x.xml"
|
|
17
|
+
# result.syntax # => :cii
|
|
18
|
+
# invoice = result.read
|
|
19
|
+
class Extractor
|
|
20
|
+
class PdfReaderNotFound < Zugpferd::Error; end
|
|
21
|
+
class ExtractError < Zugpferd::Error; end
|
|
22
|
+
class InvoiceNotFound < ExtractError; end
|
|
23
|
+
class AmbiguousInvoice < ExtractError; end
|
|
24
|
+
class LimitExceeded < ExtractError; end
|
|
25
|
+
class UnsupportedSyntax < ExtractError; end
|
|
26
|
+
|
|
27
|
+
# The names the specifications give the embedded invoice, so that PDFs
|
|
28
|
+
# from any producer are found, not only those of {Embedder}:
|
|
29
|
+
#
|
|
30
|
+
# - factur-x.xml: Factur-X 1.0.06 to 1.08 / ZUGFeRD 2.2 to 2.4, §6.2;
|
|
31
|
+
# ZUGFeRD 2.1.1 Technical Supplement Part A, §2.2
|
|
32
|
+
# - xrechnung.xml: the same Factur-X §6.2 for the XRECHNUNG profile;
|
|
33
|
+
# ZUGFeRD 2.1.1 Technical Supplement Reference Profiles, §3.1
|
|
34
|
+
# - zugferd-invoice.xml: ZUGFeRD 2.0 §5.2; ZUGFeRD 2.1.1 Technical
|
|
35
|
+
# Supplement Part B, §2.2
|
|
36
|
+
# - ZUGFeRD-invoice.xml: ZUGFeRD 1.0 §7.2
|
|
37
|
+
#
|
|
38
|
+
# The specifications do not say whether case matters, so names are
|
|
39
|
+
# matched exactly, as written there.
|
|
40
|
+
INVOICE_FILENAMES = %w[factur-x.xml xrechnung.xml zugferd-invoice.xml ZUGFeRD-invoice.xml].freeze
|
|
41
|
+
|
|
42
|
+
DEFAULT_MAX_XML_BYTES = 10 * 1024 * 1024
|
|
43
|
+
DEFAULT_MAX_NAME_TREE_DEPTH = 32
|
|
44
|
+
|
|
45
|
+
ROOTS = {
|
|
46
|
+
["urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100", "CrossIndustryInvoice"] => :cii,
|
|
47
|
+
["urn:oasis:names:specification:ubl:schema:xsd:Invoice-2", "Invoice"] => :ubl,
|
|
48
|
+
["urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2", "CreditNote"] => :ubl,
|
|
49
|
+
}.freeze
|
|
50
|
+
|
|
51
|
+
# The extracted attachment.
|
|
52
|
+
#
|
|
53
|
+
# @!attribute [r] xml
|
|
54
|
+
# @return [String] the embedded XML, byte for byte
|
|
55
|
+
# @!attribute [r] filename
|
|
56
|
+
# @return [String] the attachment name, e.g. "factur-x.xml"
|
|
57
|
+
# @!attribute [r] syntax
|
|
58
|
+
# @return [Symbol, nil] :cii or :ubl, nil for any other root element
|
|
59
|
+
Result = Struct.new(:xml, :filename, :syntax, keyword_init: true) do
|
|
60
|
+
# Parses the XML with {CII::Reader} or {UBL::Reader}.
|
|
61
|
+
#
|
|
62
|
+
# @return [Model::BillingDocument]
|
|
63
|
+
# @raise [UnsupportedSyntax] for any other root element, e.g. ZUGFeRD 1.0
|
|
64
|
+
def read
|
|
65
|
+
case syntax
|
|
66
|
+
when :cii then CII::Reader.new.read(xml)
|
|
67
|
+
when :ubl then UBL::Reader.new.read(xml)
|
|
68
|
+
else raise UnsupportedSyntax, unsupported_message
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
def unsupported_message
|
|
75
|
+
root = Nokogiri::XML(xml).root
|
|
76
|
+
if root.nil?
|
|
77
|
+
"#{filename} has no XML root element"
|
|
78
|
+
elsif root.name == "CrossIndustryDocument"
|
|
79
|
+
"#{filename} is a ZUGFeRD 1.0 invoice (CrossIndustryDocument), which zugpferd cannot read"
|
|
80
|
+
else
|
|
81
|
+
"#{filename} has root element #{root.name}; expected CII CrossIndustryInvoice or UBL Invoice/CreditNote"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# @param max_xml_bytes [Integer] largest decompressed attachment to accept
|
|
87
|
+
# @param max_name_tree_depth [Integer] deepest EmbeddedFiles name tree level to visit
|
|
88
|
+
def initialize(max_xml_bytes: DEFAULT_MAX_XML_BYTES, max_name_tree_depth: DEFAULT_MAX_NAME_TREE_DEPTH)
|
|
89
|
+
@max_xml_bytes = max_xml_bytes
|
|
90
|
+
@max_name_tree_depth = max_name_tree_depth
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# @param pdf_path [String]
|
|
94
|
+
# @return [Result]
|
|
95
|
+
# @raise [PdfReaderNotFound] if the pdf-reader gem is not installed
|
|
96
|
+
# @raise [InvoiceNotFound] if no attachment has an invoice file name
|
|
97
|
+
# @raise [AmbiguousInvoice] if more than one has
|
|
98
|
+
# @raise [LimitExceeded] if a limit is hit
|
|
99
|
+
# @raise [ExtractError] if the PDF or the attachment cannot be read
|
|
100
|
+
def extract(pdf_path:)
|
|
101
|
+
raise ArgumentError, "PDF file not found: #{pdf_path}" unless File.exist?(pdf_path)
|
|
102
|
+
|
|
103
|
+
load_pdf_reader!
|
|
104
|
+
filename, xml = read_invoice(pdf_path)
|
|
105
|
+
|
|
106
|
+
Result.new(xml: xml, filename: filename, syntax: detect_syntax(xml))
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
# Kept apart from #extract: the rescue clause names pdf-reader
|
|
112
|
+
# constants, which only exist once the gem is loaded.
|
|
113
|
+
def read_invoice(pdf_path)
|
|
114
|
+
Document.new(::PDF::Reader::ObjectHash.new(pdf_path), @max_xml_bytes, @max_name_tree_depth).invoice
|
|
115
|
+
rescue ::PDF::Reader::MalformedPDFError, ::PDF::Reader::UnsupportedFeatureError => e
|
|
116
|
+
raise ExtractError, "Cannot read #{pdf_path}: #{e.message}"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def load_pdf_reader!
|
|
120
|
+
require "pdf/reader"
|
|
121
|
+
rescue LoadError
|
|
122
|
+
raise PdfReaderNotFound,
|
|
123
|
+
"Zugpferd::PDF::Extractor needs the pdf-reader gem. Add this line to your Gemfile:\n\n" \
|
|
124
|
+
" gem \"pdf-reader\", \"~> 2.16\""
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def detect_syntax(xml)
|
|
128
|
+
root = Nokogiri::XML(xml).root
|
|
129
|
+
ROOTS[[root.namespace&.href, root.name]] if root
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Walks one PDF's object tree. A new one per call keeps Extractor
|
|
133
|
+
# free of per-file state.
|
|
134
|
+
class Document
|
|
135
|
+
# Compressed bytes fed to zlib per step. Deflate expands at most
|
|
136
|
+
# about 1032:1, so one step adds at most about 530 KB past the limit.
|
|
137
|
+
INFLATE_CHUNK_BYTES = 512
|
|
138
|
+
MAX_REFERENCE_HOPS = 16
|
|
139
|
+
|
|
140
|
+
def initialize(objects, max_xml_bytes, max_name_tree_depth)
|
|
141
|
+
@objects = objects
|
|
142
|
+
@max_xml_bytes = max_xml_bytes
|
|
143
|
+
@max_name_tree_depth = max_name_tree_depth
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# @return [Array(String, String)] file name and XML
|
|
147
|
+
def invoice
|
|
148
|
+
filename, spec = find_invoice
|
|
149
|
+
[filename, read_embedded_file(spec, filename)]
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
private
|
|
153
|
+
|
|
154
|
+
# PDF/A-3 lists associated files in the catalog's /AF array. Only
|
|
155
|
+
# when that holds no invoice is the EmbeddedFiles name tree searched.
|
|
156
|
+
def find_invoice
|
|
157
|
+
catalog = dictionary(@objects.trailer[:Root])
|
|
158
|
+
matches = invoice_specs(pdf_array(catalog[:AF]))
|
|
159
|
+
matches = invoice_specs(name_tree_values(dictionary(catalog[:Names])[:EmbeddedFiles])) if matches.empty?
|
|
160
|
+
|
|
161
|
+
case matches.size
|
|
162
|
+
when 0 then raise InvoiceNotFound, "No invoice attachment (#{INVOICE_FILENAMES.join(", ")}) found"
|
|
163
|
+
when 1 then matches.first
|
|
164
|
+
else raise AmbiguousInvoice, "More than one invoice attachment: #{matches.map(&:first).join(", ")}"
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def invoice_specs(refs)
|
|
169
|
+
specs = refs.map { |ref| dictionary(ref) }.uniq(&:object_id)
|
|
170
|
+
specs.filter_map do |spec|
|
|
171
|
+
name = file_spec_name(spec)
|
|
172
|
+
[name, spec] if INVOICE_FILENAMES.include?(name)
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def file_spec_name(spec)
|
|
177
|
+
name = resolve(spec[:UF]) || resolve(spec[:F])
|
|
178
|
+
decode_text_string(name) if name.is_a?(String)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# /UF is a text string: UTF-16BE or UTF-8 with a byte order mark,
|
|
182
|
+
# otherwise PDFDocEncoding, which matches ASCII for file names.
|
|
183
|
+
def decode_text_string(string)
|
|
184
|
+
bytes = string.b
|
|
185
|
+
if bytes.start_with?("\xFE\xFF".b)
|
|
186
|
+
bytes.byteslice(2..).force_encoding(Encoding::UTF_16BE).encode(Encoding::UTF_8)
|
|
187
|
+
elsif bytes.start_with?("\xEF\xBB\xBF".b)
|
|
188
|
+
bytes.byteslice(3..).force_encoding(Encoding::UTF_8)
|
|
189
|
+
else
|
|
190
|
+
bytes.force_encoding(Encoding::UTF_8)
|
|
191
|
+
end
|
|
192
|
+
rescue EncodingError
|
|
193
|
+
nil
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def name_tree_values(root)
|
|
197
|
+
values = []
|
|
198
|
+
visited = Set.new
|
|
199
|
+
pending = [[root, 0]]
|
|
200
|
+
|
|
201
|
+
until pending.empty?
|
|
202
|
+
ref, depth = pending.pop
|
|
203
|
+
next if ref.nil?
|
|
204
|
+
|
|
205
|
+
if depth > @max_name_tree_depth
|
|
206
|
+
raise LimitExceeded, "EmbeddedFiles name tree is deeper than #{@max_name_tree_depth} levels"
|
|
207
|
+
end
|
|
208
|
+
next if ref.is_a?(::PDF::Reader::Reference) && !visited.add?(ref)
|
|
209
|
+
|
|
210
|
+
node = dictionary(ref)
|
|
211
|
+
pdf_array(node[:Names]).each_slice(2) { |_key, value| values << value }
|
|
212
|
+
pdf_array(node[:Kids]).each { |kid| pending << [kid, depth + 1] }
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
values
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def read_embedded_file(spec, filename)
|
|
219
|
+
embedded = dictionary(spec[:EF])
|
|
220
|
+
stream = resolve(embedded[:UF] || embedded[:F])
|
|
221
|
+
raise ExtractError, "#{filename} has no embedded file stream" unless stream.is_a?(::PDF::Reader::Stream)
|
|
222
|
+
|
|
223
|
+
decode_stream(stream, filename)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# pdf-reader inflates a whole stream in one call, so a size limit
|
|
227
|
+
# could only be checked afterwards. The attachment is inflated here
|
|
228
|
+
# in steps instead, to stop as soon as the limit is passed.
|
|
229
|
+
def decode_stream(stream, filename)
|
|
230
|
+
filters = list(stream.hash[:Filter]).map { |f| resolve(f) }
|
|
231
|
+
|
|
232
|
+
case filters
|
|
233
|
+
when []
|
|
234
|
+
check_size!(stream.data.bytesize, filename)
|
|
235
|
+
stream.data.b
|
|
236
|
+
when [:FlateDecode]
|
|
237
|
+
if predictor?(stream.hash[:DecodeParms])
|
|
238
|
+
raise ExtractError, "#{filename} uses a Flate predictor, which is not supported"
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
inflate(stream.data, filename)
|
|
242
|
+
else
|
|
243
|
+
raise ExtractError, "#{filename} uses filter #{filters.join(", ")}; only FlateDecode is supported"
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def predictor?(decode_parms)
|
|
248
|
+
list(decode_parms).any? { |params| dictionary(params).fetch(:Predictor, 1).to_i > 1 }
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def inflate(data, filename)
|
|
252
|
+
inflate_with(Zlib::MAX_WBITS + 32, data, filename)
|
|
253
|
+
rescue Zlib::DataError
|
|
254
|
+
# Some producers write raw deflate data without the zlib header.
|
|
255
|
+
begin
|
|
256
|
+
inflate_with(-Zlib::MAX_WBITS, data, filename)
|
|
257
|
+
rescue Zlib::DataError
|
|
258
|
+
raise ExtractError, "#{filename} is not valid Flate data"
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def inflate_with(window_bits, data, filename)
|
|
263
|
+
zstream = Zlib::Inflate.new(window_bits)
|
|
264
|
+
output = +"".b
|
|
265
|
+
offset = 0
|
|
266
|
+
|
|
267
|
+
while offset < data.bytesize && !zstream.finished?
|
|
268
|
+
output << zstream.inflate(data.byteslice(offset, INFLATE_CHUNK_BYTES))
|
|
269
|
+
offset += INFLATE_CHUNK_BYTES
|
|
270
|
+
check_size!(output.bytesize, filename)
|
|
271
|
+
end
|
|
272
|
+
raise ExtractError, "#{filename} is truncated" unless zstream.finished?
|
|
273
|
+
|
|
274
|
+
output
|
|
275
|
+
ensure
|
|
276
|
+
# Closing a stream stopped early warns unless it is reset first.
|
|
277
|
+
zstream&.reset
|
|
278
|
+
zstream&.close
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def check_size!(bytesize, filename)
|
|
282
|
+
return if bytesize <= @max_xml_bytes
|
|
283
|
+
|
|
284
|
+
raise LimitExceeded, "#{filename} is larger than #{@max_xml_bytes} bytes"
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Follows a reference until it reaches a direct object. Ghostscript,
|
|
288
|
+
# for one, writes /AF entries that point to an object holding
|
|
289
|
+
# another reference.
|
|
290
|
+
def resolve(value)
|
|
291
|
+
MAX_REFERENCE_HOPS.times do
|
|
292
|
+
return value unless value.is_a?(::PDF::Reader::Reference)
|
|
293
|
+
|
|
294
|
+
value = @objects.deref(value)
|
|
295
|
+
end
|
|
296
|
+
raise LimitExceeded, "Reference chain longer than #{MAX_REFERENCE_HOPS} hops"
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def dictionary(ref)
|
|
300
|
+
value = resolve(ref)
|
|
301
|
+
value.is_a?(Hash) ? value : {}
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def pdf_array(ref)
|
|
305
|
+
value = resolve(ref)
|
|
306
|
+
value.is_a?(Array) ? value : []
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# A value that may be a single object or an array of them.
|
|
310
|
+
def list(ref)
|
|
311
|
+
value = resolve(ref)
|
|
312
|
+
value.is_a?(Array) ? value : [value].compact
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
private_constant :Document
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
end
|
data/lib/zugpferd/pdf.rb
CHANGED
data/lib/zugpferd/ubl/mapping.rb
CHANGED
|
@@ -21,14 +21,49 @@ module Zugpferd
|
|
|
21
21
|
type_code: "cbc:InvoiceTypeCode",
|
|
22
22
|
currency_code: "cbc:DocumentCurrencyCode",
|
|
23
23
|
buyer_reference: "cbc:BuyerReference",
|
|
24
|
+
buyer_accounting_reference: "cbc:AccountingCost",
|
|
24
25
|
customization_id: "cbc:CustomizationID",
|
|
25
26
|
profile_id: "cbc:ProfileID",
|
|
26
27
|
note: "cbc:Note",
|
|
27
28
|
}.freeze
|
|
28
29
|
|
|
30
|
+
# BT-9 in a credit note (it has no cbc:DueDate)
|
|
31
|
+
CREDIT_NOTE_DUE_DATE = "cac:PaymentMeans/cbc:PaymentDueDate"
|
|
32
|
+
|
|
29
33
|
# Delivery (BG-13)
|
|
30
34
|
DELIVERY = "cac:Delivery"
|
|
31
35
|
DELIVERY_DATE = "cbc:ActualDeliveryDate"
|
|
36
|
+
# Deliver to address (BG-15), fields as in ADDRESS
|
|
37
|
+
DELIVER_TO_ADDRESS = "cac:DeliveryLocation/cac:Address"
|
|
38
|
+
|
|
39
|
+
# Invoicing period (BG-14)
|
|
40
|
+
INVOICE_PERIOD = "cac:InvoicePeriod"
|
|
41
|
+
INVOICE_PERIOD_FIELDS = {
|
|
42
|
+
start_date: "cbc:StartDate",
|
|
43
|
+
end_date: "cbc:EndDate",
|
|
44
|
+
}.freeze
|
|
45
|
+
|
|
46
|
+
# Project reference (BT-11). CreditNote-2 has no cac:ProjectReference,
|
|
47
|
+
# credit notes carry it as additional document reference with type code 50
|
|
48
|
+
PROJECT_REFERENCE = "cac:ProjectReference/cbc:ID"
|
|
49
|
+
PROJECT_REFERENCE_TYPE_CODE = "50"
|
|
50
|
+
CREDIT_NOTE_PROJECT_REFERENCE =
|
|
51
|
+
"cac:AdditionalDocumentReference[cbc:DocumentTypeCode='#{PROJECT_REFERENCE_TYPE_CODE}']/cbc:ID"
|
|
52
|
+
# Contract reference (BT-12)
|
|
53
|
+
CONTRACT_REFERENCE = "cac:ContractDocumentReference/cbc:ID"
|
|
54
|
+
# Purchase order reference (BT-13)
|
|
55
|
+
ORDER_REFERENCE = "cac:OrderReference/cbc:ID"
|
|
56
|
+
# Written as BT-13 when only BT-14 is set, because cbc:ID is mandatory
|
|
57
|
+
ORDER_REFERENCE_NOT_APPLICABLE = "NA"
|
|
58
|
+
# Sales order reference (BT-14)
|
|
59
|
+
SALES_ORDER_REFERENCE = "cac:OrderReference/cbc:SalesOrderID"
|
|
60
|
+
|
|
61
|
+
# Preceding invoice reference (BG-3)
|
|
62
|
+
PRECEDING_INVOICE = "cac:BillingReference/cac:InvoiceDocumentReference"
|
|
63
|
+
PRECEDING_INVOICE_FIELDS = {
|
|
64
|
+
number: "cbc:ID",
|
|
65
|
+
issue_date: "cbc:IssueDate",
|
|
66
|
+
}.freeze
|
|
32
67
|
|
|
33
68
|
# Seller (BG-4)
|
|
34
69
|
SELLER = "cac:AccountingSupplierParty/cac:Party"
|
|
@@ -43,6 +78,7 @@ module Zugpferd
|
|
|
43
78
|
legal_registration_id: "cac:PartyLegalEntity/cbc:CompanyID",
|
|
44
79
|
legal_form: "cac:PartyLegalEntity/cbc:CompanyLegalForm",
|
|
45
80
|
vat_identifier: "cac:PartyTaxScheme[cac:TaxScheme/cbc:ID='VAT']/cbc:CompanyID",
|
|
81
|
+
tax_registration_identifier: "cac:PartyTaxScheme[cac:TaxScheme/cbc:ID!='VAT']/cbc:CompanyID",
|
|
46
82
|
electronic_address: "cbc:EndpointID",
|
|
47
83
|
}.freeze
|
|
48
84
|
|
|
@@ -69,6 +105,7 @@ module Zugpferd
|
|
|
69
105
|
payment_means_code: "cbc:PaymentMeansCode",
|
|
70
106
|
payment_id: "cbc:PaymentID",
|
|
71
107
|
account_id: "cac:PayeeFinancialAccount/cbc:ID",
|
|
108
|
+
account_name: "cac:PayeeFinancialAccount/cbc:Name",
|
|
72
109
|
card_account_id: "cac:CardAccount/cbc:PrimaryAccountNumberID",
|
|
73
110
|
card_network_id: "cac:CardAccount/cbc:NetworkID",
|
|
74
111
|
card_holder_name: "cac:CardAccount/cbc:HolderName",
|
|
@@ -125,6 +162,13 @@ module Zugpferd
|
|
|
125
162
|
note: "cbc:Note",
|
|
126
163
|
}.freeze
|
|
127
164
|
|
|
165
|
+
# Invoice line period (BG-26)
|
|
166
|
+
LINE_PERIOD = "cac:InvoicePeriod"
|
|
167
|
+
LINE_PERIOD_FIELDS = {
|
|
168
|
+
start_date: "cbc:StartDate",
|
|
169
|
+
end_date: "cbc:EndDate",
|
|
170
|
+
}.freeze
|
|
171
|
+
|
|
128
172
|
# Item (BG-31)
|
|
129
173
|
ITEM = "cac:Item"
|
|
130
174
|
ITEM_FIELDS = {
|
|
@@ -138,7 +182,9 @@ module Zugpferd
|
|
|
138
182
|
# Price (BG-29)
|
|
139
183
|
PRICE = "cac:Price"
|
|
140
184
|
PRICE_FIELDS = {
|
|
141
|
-
amount:
|
|
185
|
+
amount: "cbc:PriceAmount",
|
|
186
|
+
base_quantity: "cbc:BaseQuantity",
|
|
187
|
+
base_quantity_unit_code: "cbc:BaseQuantity/@unitCode",
|
|
142
188
|
}.freeze
|
|
143
189
|
end
|
|
144
190
|
end
|