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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 668190b295d1286b2f8efd2f2896054e9c102c48da549d70486ef13677d042ad
4
- data.tar.gz: '0274834d75a8c12690e178b5e075a8bed9792180b627c19a1fd3313534d9914c'
3
+ metadata.gz: 8a81cafc9a97815b530265d94e7335922f49e342e9993768f0d1e062cbe2c210
4
+ data.tar.gz: 87593306b6c7a678fc81c398aab52069bd5235133e008803df21bf7f9f993ea3
5
5
  SHA512:
6
- metadata.gz: 74be6147cd6ef40e90a23548e0e15c0b65e7c5e402535c2575f1dc41926b66b981f7c1e555d297f006ae69988ff9470c4d8b2eb2e8a4c7c83aa8791cc8bde1f5
7
- data.tar.gz: 2cb11d5ef99e1d3341006fd37d2fa8e9ba3703fbebc7c642577f269aaa9aae3ddf666c2ed892a6ca5e92df17945d1d2b916b9b542c16f283b909ec862ddd8cdd
6
+ metadata.gz: a768c2a3c9690853bd836d6a50d966f6b0241b8f5892d4ff90241a77d75f272fe011d9f76bfb6589aa3b10996b5cb830b7ee923608d687b3d9c7370ae3bdfea4
7
+ data.tar.gz: 839733c3e4d20e121b2aae12229942f4220839eadaea33be03d20d8061d1b5a74002300773e142e16bcaafe1cc3888c812c63753e51c071be7faeca89c3ff611
data/bin/setup-schemas ADDED
@@ -0,0 +1,186 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ VENDOR_DIR="$(pwd)/vendor"
5
+ SCHEMAS_DIR="$VENDOR_DIR/schemas"
6
+ TMP_DIR=$(mktemp -d)
7
+ trap 'rm -rf "$TMP_DIR"' EXIT
8
+
9
+ CURL_UA="Mozilla/5.0"
10
+ ZUGPFERD_REPO="https://github.com/alexzeitler/zugpferd"
11
+
12
+ # --- Saxon HE (for Schematron validation) ---
13
+
14
+ echo "==> Downloading Saxon HE..."
15
+ SAXON_DIR="$SCHEMAS_DIR/saxon"
16
+ mkdir -p "$SAXON_DIR"
17
+
18
+ SAXON_VERSION="12.5"
19
+ XMLRESOLVER_VERSION="5.2.2"
20
+ MAVEN="https://repo1.maven.org/maven2"
21
+
22
+ if [ ! -f "$SAXON_DIR/saxon-he-$SAXON_VERSION.jar" ]; then
23
+ echo " Downloading saxon-he-$SAXON_VERSION.jar..."
24
+ curl -fsSL -A "$CURL_UA" "$MAVEN/net/sf/saxon/Saxon-HE/$SAXON_VERSION/Saxon-HE-$SAXON_VERSION.jar" \
25
+ -o "$SAXON_DIR/saxon-he-$SAXON_VERSION.jar"
26
+ else
27
+ echo " saxon-he-$SAXON_VERSION.jar already present"
28
+ fi
29
+
30
+ if [ ! -f "$SAXON_DIR/xmlresolver-$XMLRESOLVER_VERSION.jar" ]; then
31
+ echo " Downloading xmlresolver-$XMLRESOLVER_VERSION.jar..."
32
+ curl -fsSL -A "$CURL_UA" "$MAVEN/org/xmlresolver/xmlresolver/$XMLRESOLVER_VERSION/xmlresolver-$XMLRESOLVER_VERSION.jar" \
33
+ -o "$SAXON_DIR/xmlresolver-$XMLRESOLVER_VERSION.jar"
34
+ else
35
+ echo " xmlresolver-$XMLRESOLVER_VERSION.jar already present"
36
+ fi
37
+ echo " OK"
38
+
39
+ # --- UBL 2.1 XSD ---
40
+
41
+ echo ""
42
+ echo "==> Downloading UBL 2.1 XSD schemas..."
43
+ UBL_DIR="$SCHEMAS_DIR/ubl"
44
+
45
+ if [ ! -d "$UBL_DIR/xsd/maindoc" ]; then
46
+ echo " Downloading UBL-2.1.zip..."
47
+ curl -fsSL -A "$CURL_UA" "http://docs.oasis-open.org/ubl/os-UBL-2.1/UBL-2.1.zip" \
48
+ -o "$TMP_DIR/UBL-2.1.zip"
49
+ mkdir -p "$UBL_DIR"
50
+ unzip -qo "$TMP_DIR/UBL-2.1.zip" "xsd/*" -d "$UBL_DIR"
51
+ else
52
+ echo " UBL XSD already present"
53
+ fi
54
+ echo " OK"
55
+
56
+ # --- CII D16B XSD ---
57
+
58
+ echo ""
59
+ echo "==> Downloading CII D16B XSD schemas..."
60
+ CII_DIR="$SCHEMAS_DIR/cii"
61
+
62
+ if [ ! -f "$CII_DIR/CrossIndustryInvoice_100pD16B.xsd" ]; then
63
+ echo " Downloading D16B SCRDM (Subset) CII..."
64
+ CII_URL="$ZUGPFERD_REPO/releases/download/v0.3.3/D16B_SCRDM__Subset__CII.zip"
65
+ curl -fsSL -A "$CURL_UA" "$CII_URL" -o "$TMP_DIR/cii-outer.zip"
66
+
67
+ mkdir -p "$CII_DIR"
68
+ unzip -qo "$TMP_DIR/cii-outer.zip" -d "$TMP_DIR/cii-outer"
69
+ # The outer ZIP contains nested ZIPs — extract the uncoupled one
70
+ UNCOUPLED_ZIP=$(find "$TMP_DIR/cii-outer" -name "*uncoupled.zip" -type f | head -1)
71
+ if [ -n "$UNCOUPLED_ZIP" ]; then
72
+ unzip -qo "$UNCOUPLED_ZIP" -d "$TMP_DIR/cii-uncoupled"
73
+ # XSDs are nested deep — move them flat into CII_DIR
74
+ XSD_DIR=$(find "$TMP_DIR/cii-uncoupled" -name "CrossIndustryInvoice_100pD16B.xsd" -type f -exec dirname {} \; | head -1)
75
+ if [ -n "$XSD_DIR" ]; then
76
+ cp -r "$XSD_DIR"/* "$CII_DIR/"
77
+ # Copy codelist XSDs (relative imports from main XSD)
78
+ CODELIST_DIR=$(find "$TMP_DIR/cii-uncoupled" -type d -name "codelist" | head -1)
79
+ if [ -n "$CODELIST_DIR" ]; then
80
+ cp -r "$CODELIST_DIR" "$CII_DIR/"
81
+ fi
82
+ else
83
+ echo " ERROR: Could not find CrossIndustryInvoice_100pD16B.xsd in extracted archive"
84
+ exit 1
85
+ fi
86
+ else
87
+ echo " ERROR: Could not find uncoupled ZIP inside CII archive"
88
+ exit 1
89
+ fi
90
+ else
91
+ echo " CII XSD already present"
92
+ fi
93
+ echo " OK"
94
+
95
+ # --- CEN Schematron EN16931 ---
96
+
97
+ echo ""
98
+ echo "==> Downloading CEN Schematron EN16931..."
99
+ CEN_VERSION="1.3.15"
100
+ CEN_BASE="https://github.com/ConnectingEurope/eInvoicing-EN16931/releases/download/validation-$CEN_VERSION"
101
+
102
+ CEN_UBL_DIR="$SCHEMAS_DIR/schematron/cen/ubl"
103
+ if [ ! -f "$CEN_UBL_DIR/xslt/EN16931-UBL-validation.xslt" ]; then
104
+ echo " Downloading en16931-ubl-$CEN_VERSION.zip..."
105
+ curl -fsSL -A "$CURL_UA" "$CEN_BASE/en16931-ubl-$CEN_VERSION.zip" -o "$TMP_DIR/cen-ubl.zip"
106
+ mkdir -p "$CEN_UBL_DIR"
107
+ unzip -qo "$TMP_DIR/cen-ubl.zip" -d "$CEN_UBL_DIR"
108
+ else
109
+ echo " CEN UBL Schematron already present"
110
+ fi
111
+
112
+ CEN_CII_DIR="$SCHEMAS_DIR/schematron/cen/cii"
113
+ if [ ! -f "$CEN_CII_DIR/xslt/EN16931-CII-validation.xslt" ]; then
114
+ echo " Downloading en16931-cii-$CEN_VERSION.zip..."
115
+ curl -fsSL -A "$CURL_UA" "$CEN_BASE/en16931-cii-$CEN_VERSION.zip" -o "$TMP_DIR/cen-cii.zip"
116
+ mkdir -p "$CEN_CII_DIR"
117
+ unzip -qo "$TMP_DIR/cen-cii.zip" -d "$CEN_CII_DIR"
118
+ else
119
+ echo " CEN CII Schematron already present"
120
+ fi
121
+ echo " OK"
122
+
123
+ # --- XRechnung Schematron ---
124
+
125
+ echo ""
126
+ echo "==> Downloading XRechnung Schematron..."
127
+ XRECHNUNG_SCHEMATRON_VERSION="2.5.0"
128
+ XRECHNUNG_SPEC_VERSION="3.0.2"
129
+ XR_DIR="$SCHEMAS_DIR/schematron/xrechnung"
130
+
131
+ if [ ! -f "$XR_DIR/schematron/ubl/XRechnung-UBL-validation.xsl" ]; then
132
+ echo " Downloading xrechnung-$XRECHNUNG_SPEC_VERSION-schematron-$XRECHNUNG_SCHEMATRON_VERSION.zip..."
133
+ curl -fsSL -A "$CURL_UA" "https://github.com/itplr-kosit/xrechnung-schematron/releases/download/v$XRECHNUNG_SCHEMATRON_VERSION/xrechnung-$XRECHNUNG_SPEC_VERSION-schematron-$XRECHNUNG_SCHEMATRON_VERSION.zip" \
134
+ -o "$TMP_DIR/xrechnung-schematron.zip"
135
+ mkdir -p "$XR_DIR"
136
+ unzip -qo "$TMP_DIR/xrechnung-schematron.zip" -d "$XR_DIR"
137
+ else
138
+ echo " XRechnung Schematron already present"
139
+ fi
140
+ echo " OK"
141
+
142
+ # --- XRechnung Testsuite ---
143
+
144
+ echo ""
145
+ echo "==> Downloading XRechnung test suite..."
146
+ if [ ! -d "$VENDOR_DIR/testsuite" ]; then
147
+ git clone --depth 1 \
148
+ https://github.com/itplr-kosit/xrechnung-testsuite.git \
149
+ "$VENDOR_DIR/testsuite"
150
+ else
151
+ cd "$VENDOR_DIR/testsuite" && git pull --ff-only
152
+ fi
153
+ echo " OK"
154
+
155
+ # --- ZUGFeRD PDF artifacts ---
156
+
157
+ echo ""
158
+ echo "==> Downloading ZUGFeRD PDF artifacts..."
159
+ ZUGFERD_DIR="$VENDOR_DIR/zugferd"
160
+ mkdir -p "$ZUGFERD_DIR"
161
+
162
+ GS_REF="master"
163
+ GS_RAW="https://raw.githubusercontent.com/ArtifexSoftware/ghostpdl/$GS_REF"
164
+
165
+ if [ ! -f "$ZUGFERD_DIR/zugferd.ps" ]; then
166
+ echo " Downloading zugferd.ps..."
167
+ curl -fsSL -A "$CURL_UA" "$GS_RAW/lib/zugferd.ps" -o "$ZUGFERD_DIR/zugferd.ps"
168
+ else
169
+ echo " zugferd.ps already present"
170
+ fi
171
+
172
+ if [ ! -f "$ZUGFERD_DIR/default_rgb.icc" ]; then
173
+ echo " Downloading default_rgb.icc..."
174
+ curl -fsSL -A "$CURL_UA" "$GS_RAW/iccprofiles/default_rgb.icc" -o "$ZUGFERD_DIR/default_rgb.icc"
175
+ else
176
+ echo " default_rgb.icc already present"
177
+ fi
178
+ echo " OK"
179
+
180
+ echo ""
181
+ echo "All artifacts ready in: $VENDOR_DIR"
182
+ echo ""
183
+ echo "Optional dependencies:"
184
+ echo " - Java (for Schematron validation with Saxon)"
185
+ echo " - Ghostscript (for PDF/A-3 embedding)"
186
+ echo " - Docker (for veraPDF / Mustangproject validation)"
@@ -26,16 +26,40 @@ module Zugpferd
26
26
  # Delivery (BG-13)
27
27
  DELIVERY = "#{TRANSACTION}/ram:ApplicableHeaderTradeDelivery"
28
28
  DELIVERY_DATE = "ram:ActualDeliverySupplyChainEvent/ram:OccurrenceDateTime/udt:DateTimeString"
29
+ # Deliver to address (BG-15), fields as in ADDRESS
30
+ DELIVER_TO_ADDRESS = "ram:ShipToTradeParty/ram:PostalTradeAddress"
29
31
 
30
32
  # Settlement (contains currency, payment, tax, totals)
31
33
  SETTLEMENT = "#{TRANSACTION}/ram:ApplicableHeaderTradeSettlement"
32
34
  AGREEMENT = "#{TRANSACTION}/ram:ApplicableHeaderTradeAgreement"
33
35
 
36
+ # Invoicing period (BG-14)
37
+ BILLING_PERIOD = "ram:BillingSpecifiedPeriod"
38
+ BILLING_PERIOD_FIELDS = {
39
+ start_date: "ram:StartDateTime/udt:DateTimeString",
40
+ end_date: "ram:EndDateTime/udt:DateTimeString",
41
+ }.freeze
42
+
43
+ # Preceding invoice reference (BG-3), relative to the settlement node
44
+ PRECEDING_INVOICE = "ram:InvoiceReferencedDocument"
45
+ PRECEDING_INVOICE_FIELDS = {
46
+ number: "ram:IssuerAssignedID",
47
+ issue_date: "ram:FormattedIssueDateTime/qdt:DateTimeString",
48
+ }.freeze
49
+
34
50
  INVOICE_SETTLEMENT = {
35
51
  currency_code: "#{SETTLEMENT}/ram:InvoiceCurrencyCode",
36
52
  buyer_reference: "#{AGREEMENT}/ram:BuyerReference",
53
+ purchase_order_reference: "#{AGREEMENT}/ram:BuyerOrderReferencedDocument/ram:IssuerAssignedID",
54
+ seller_order_reference: "#{AGREEMENT}/ram:SellerOrderReferencedDocument/ram:IssuerAssignedID",
55
+ contract_reference: "#{AGREEMENT}/ram:ContractReferencedDocument/ram:IssuerAssignedID",
56
+ project_reference: "#{AGREEMENT}/ram:SpecifiedProcuringProject/ram:ID",
57
+ buyer_accounting_reference: "#{SETTLEMENT}/ram:ReceivableSpecifiedTradeAccountingAccount/ram:ID",
37
58
  }.freeze
38
59
 
60
+ # Mandatory ram:Name of ram:SpecifiedProcuringProject, no business term in EN 16931
61
+ PROJECT_NAME = "Project reference"
62
+
39
63
  # Seller (BG-4)
40
64
  SELLER = "#{AGREEMENT}/ram:SellerTradeParty"
41
65
  # Buyer (BG-7)
@@ -49,6 +73,7 @@ module Zugpferd
49
73
  legal_registration_id: "ram:SpecifiedLegalOrganization/ram:ID",
50
74
  legal_form: "ram:Description",
51
75
  vat_identifier: "ram:SpecifiedTaxRegistration/ram:ID[@schemeID='VA']",
76
+ tax_registration_identifier: "ram:SpecifiedTaxRegistration/ram:ID[@schemeID='FC']",
52
77
  electronic_address: "ram:URIUniversalCommunication/ram:URIID",
53
78
  }.freeze
54
79
 
@@ -74,6 +99,7 @@ module Zugpferd
74
99
  PAYMENT = {
75
100
  payment_means_code: "ram:TypeCode",
76
101
  account_id: "ram:PayeePartyCreditorFinancialAccount/ram:IBANID",
102
+ account_name: "ram:PayeePartyCreditorFinancialAccount/ram:AccountName",
77
103
  card_account_id: "ram:ApplicableTradeSettlementFinancialCard/ram:ID",
78
104
  card_holder_name: "ram:ApplicableTradeSettlementFinancialCard/ram:CardholderName",
79
105
  debited_account_id: "ram:PayerPartyDebtorFinancialAccount/ram:IBANID",
@@ -86,6 +112,7 @@ module Zugpferd
86
112
 
87
113
  # TaxTotal (BG-23)
88
114
  TAX_SUBTOTAL = "ram:ApplicableTradeTax"
115
+ TAX_FALLBACK_CURRENCY = "ram:InvoiceCurrencyCode"
89
116
  TAX = {
90
117
  taxable_amount: "ram:BasisAmount",
91
118
  tax_amount: "ram:CalculatedAmount",
@@ -132,6 +159,13 @@ module Zugpferd
132
159
  line_extension_amount: "ram:SpecifiedLineTradeSettlement/ram:SpecifiedTradeSettlementLineMonetarySummation/ram:LineTotalAmount",
133
160
  }.freeze
134
161
 
162
+ # Invoice line period (BG-26)
163
+ LINE_PERIOD = "ram:SpecifiedLineTradeSettlement/ram:BillingSpecifiedPeriod"
164
+ LINE_PERIOD_FIELDS = {
165
+ start_date: "ram:StartDateTime/udt:DateTimeString",
166
+ end_date: "ram:EndDateTime/udt:DateTimeString",
167
+ }.freeze
168
+
135
169
  # Item (BG-31)
136
170
  ITEM = "ram:SpecifiedTradeProduct"
137
171
  ITEM_FIELDS = {
@@ -150,7 +184,9 @@ module Zugpferd
150
184
  # Price (BG-29)
151
185
  PRICE = "ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice"
152
186
  PRICE_FIELDS = {
153
- amount: "ram:ChargeAmount",
187
+ amount: "ram:ChargeAmount",
188
+ base_quantity: "ram:BasisQuantity",
189
+ base_quantity_unit_code: "ram:BasisQuantity/@unitCode",
154
190
  }.freeze
155
191
  end
156
192
  end
@@ -12,6 +12,9 @@ module Zugpferd
12
12
  class Reader
13
13
  include Mapping
14
14
 
15
+ # Raised when BT-110 is missing and the VAT breakdown (BG-23) does not sum to 0.
16
+ class MissingTaxTotalError < Zugpferd::Error; end
17
+
15
18
  TYPE_CODE_MAP = {
16
19
  "381" => Model::CreditNote,
17
20
  "384" => Model::CorrectedInvoice,
@@ -25,6 +28,7 @@ module Zugpferd
25
28
  # @param xml_string [String] valid CII D16B XML
26
29
  # @return [Model::BillingDocument]
27
30
  # @raise [Nokogiri::XML::SyntaxError] if the XML is malformed
31
+ # @raise [MissingTaxTotalError] if BT-110 is missing and the VAT breakdown does not sum to 0
28
32
  def read(xml_string)
29
33
  doc = Nokogiri::XML(xml_string) { |config| config.strict }
30
34
  root = doc.root
@@ -36,6 +40,7 @@ module Zugpferd
36
40
  def build_invoice(root)
37
41
  settlement = root.at_xpath(SETTLEMENT, NS)
38
42
  delivery = root.at_xpath(DELIVERY, NS)
43
+ period = settlement&.at_xpath(BILLING_PERIOD, NS)
39
44
  type_code = text(root, INVOICE[:type_code])
40
45
  model_class = TYPE_CODE_MAP.fetch(type_code, Model::Invoice)
41
46
 
@@ -44,23 +49,33 @@ module Zugpferd
44
49
  issue_date: parse_cii_date(text(root, INVOICE[:issue_date])),
45
50
  due_date: parse_cii_date(settlement ? text(settlement, PAYMENT_TERMS_DUE_DATE) : nil),
46
51
  delivery_date: parse_cii_date(delivery ? text(delivery, DELIVERY_DATE) : nil),
52
+ deliver_to_address: build_deliver_to_address(delivery),
53
+ invoicing_period_start_date: parse_cii_date(period ? text(period, BILLING_PERIOD_FIELDS[:start_date]) : nil),
54
+ invoicing_period_end_date: parse_cii_date(period ? text(period, BILLING_PERIOD_FIELDS[:end_date]) : nil),
47
55
  type_code: text(root, INVOICE[:type_code]),
48
56
  currency_code: text(root, INVOICE_SETTLEMENT[:currency_code]),
49
57
  buyer_reference: text(root, INVOICE_SETTLEMENT[:buyer_reference]),
58
+ purchase_order_reference: text(root, INVOICE_SETTLEMENT[:purchase_order_reference]),
59
+ seller_order_reference: text(root, INVOICE_SETTLEMENT[:seller_order_reference]),
60
+ contract_reference: text(root, INVOICE_SETTLEMENT[:contract_reference]),
61
+ project_reference: text(root, INVOICE_SETTLEMENT[:project_reference]),
62
+ buyer_accounting_reference: text(root, INVOICE_SETTLEMENT[:buyer_accounting_reference]),
50
63
  customization_id: text(root, INVOICE[:customization_id]),
51
64
  profile_id: text(root, INVOICE[:profile_id]),
52
65
  note: text(root, INVOICE[:note]),
53
- seller: build_party(root.at_xpath(SELLER, NS)),
66
+ seller: build_party(root.at_xpath(SELLER, NS), seller: true),
54
67
  buyer: build_party(root.at_xpath(BUYER, NS)),
55
68
  line_items: root.xpath(INVOICE_LINE, NS).map { |n| build_line_item(n) },
56
69
  allowance_charges: settlement ? build_allowance_charges(settlement) : [],
70
+ preceding_invoices: settlement ? build_preceding_invoices(settlement) : [],
57
71
  tax_breakdown: build_tax_breakdown(settlement),
58
72
  monetary_totals: build_monetary_totals(settlement&.at_xpath(MONETARY_TOTAL, NS)),
59
73
  payment_instructions: build_payment_instructions(settlement),
60
74
  )
61
75
  end
62
76
 
63
- def build_party(node)
77
+ # Seller-only terms are read only for the seller
78
+ def build_party(node, seller: false)
64
79
  return nil unless node
65
80
 
66
81
  party = Model::TradeParty.new(
@@ -68,8 +83,9 @@ module Zugpferd
68
83
  trading_name: text(node, PARTY[:trading_name]),
69
84
  identifier: text(node, PARTY[:identifier]),
70
85
  legal_registration_id: text(node, PARTY[:legal_registration_id]),
71
- legal_form: text(node, PARTY[:legal_form]),
86
+ legal_form: (text(node, PARTY[:legal_form]) if seller),
72
87
  vat_identifier: text(node, PARTY[:vat_identifier]),
88
+ tax_registration_identifier: (text(node, PARTY[:tax_registration_identifier]) if seller),
73
89
  electronic_address: text(node, PARTY[:electronic_address]),
74
90
  )
75
91
 
@@ -94,6 +110,11 @@ module Zugpferd
94
110
  )
95
111
  end
96
112
 
113
+ def build_deliver_to_address(delivery_node)
114
+ addr_node = delivery_node&.at_xpath(DELIVER_TO_ADDRESS, NS)
115
+ build_postal_address(addr_node) if addr_node
116
+ end
117
+
97
118
  def build_contact(node)
98
119
  Model::Contact.new(
99
120
  name: text(node, CONTACT_FIELDS[:name]),
@@ -112,6 +133,7 @@ module Zugpferd
112
133
  payment_means_code: text(means_node, PAYMENT[:payment_means_code]),
113
134
  payment_id: text(settlement_node, PAYMENT_REFERENCE),
114
135
  account_id: text(means_node, PAYMENT[:account_id]),
136
+ account_name: text(means_node, PAYMENT[:account_name]),
115
137
  card_account_id: text(means_node, PAYMENT[:card_account_id]),
116
138
  card_holder_name: text(means_node, PAYMENT[:card_holder_name]),
117
139
  debited_account_id: text(means_node, PAYMENT[:debited_account_id]),
@@ -126,14 +148,9 @@ module Zugpferd
126
148
 
127
149
  totals_node = settlement_node.at_xpath(MONETARY_TOTAL, NS)
128
150
  tax_total_node = totals_node&.at_xpath(TOTALS[:tax_total_amount], NS)
129
- currency = tax_total_node&.[]("currencyID")
151
+ currency = tax_total_node ? tax_total_node["currencyID"] : text(settlement_node, TAX_FALLBACK_CURRENCY)
130
152
 
131
- breakdown = Model::TaxBreakdown.new(
132
- tax_amount: tax_total_node&.text,
133
- currency_code: currency,
134
- )
135
-
136
- breakdown.subtotals = settlement_node.xpath(TAX_SUBTOTAL, NS).map do |sub|
153
+ subtotals = settlement_node.xpath(TAX_SUBTOTAL, NS).map do |sub|
137
154
  Model::TaxSubtotal.new(
138
155
  taxable_amount: text(sub, TAX[:taxable_amount]),
139
156
  tax_amount: text(sub, TAX[:tax_amount]),
@@ -145,9 +162,25 @@ module Zugpferd
145
162
  )
146
163
  end
147
164
 
165
+ breakdown = Model::TaxBreakdown.new(
166
+ tax_amount: tax_total_node ? tax_total_node.text : zero_tax_total(subtotals),
167
+ currency_code: currency,
168
+ )
169
+ breakdown.subtotals = subtotals
148
170
  breakdown
149
171
  end
150
172
 
173
+ # BR-CO-15 lets CII omit BT-110 on invoices without VAT. A breakdown
174
+ # that shows VAT contradicts that, even where the Schematron passes it.
175
+ def zero_tax_total(subtotals)
176
+ sum = subtotals.sum(BigDecimal("0"), &:tax_amount)
177
+ return sum if subtotals.any? && sum.zero?
178
+
179
+ raise MissingTaxTotalError,
180
+ "Invoice total VAT amount (BT-110) is missing and the VAT breakdown (BG-23) " \
181
+ "#{subtotals.empty? ? "is empty" : "sums to #{sum.to_s("F")}"}"
182
+ end
183
+
151
184
  def build_monetary_totals(node)
152
185
  return nil unless node
153
186
 
@@ -167,6 +200,7 @@ module Zugpferd
167
200
  item_node = node.at_xpath(ITEM, NS)
168
201
  price_node = node.at_xpath(PRICE, NS)
169
202
  tax_node = node.at_xpath(ITEM_TAX, NS)
203
+ period_node = node.at_xpath(LINE_PERIOD, NS)
170
204
 
171
205
  Model::LineItem.new(
172
206
  id: text(node, LINE[:id]),
@@ -174,6 +208,8 @@ module Zugpferd
174
208
  unit_code: node.at_xpath(LINE[:unit_code], NS)&.text,
175
209
  line_extension_amount: text(node, LINE[:line_extension_amount]),
176
210
  note: text(node, LINE[:note]),
211
+ period_start_date: parse_cii_date(period_node ? text(period_node, LINE_PERIOD_FIELDS[:start_date]) : nil),
212
+ period_end_date: parse_cii_date(period_node ? text(period_node, LINE_PERIOD_FIELDS[:end_date]) : nil),
177
213
  item: build_item(item_node, tax_node),
178
214
  price: build_price(price_node),
179
215
  )
@@ -196,9 +232,20 @@ module Zugpferd
196
232
 
197
233
  Model::Price.new(
198
234
  amount: text(node, PRICE_FIELDS[:amount]),
235
+ base_quantity: parse_decimal(text(node, PRICE_FIELDS[:base_quantity])),
236
+ base_quantity_unit_code: node.at_xpath(PRICE_FIELDS[:base_quantity_unit_code], NS)&.text,
199
237
  )
200
238
  end
201
239
 
240
+ def build_preceding_invoices(settlement_node)
241
+ settlement_node.xpath(PRECEDING_INVOICE, NS).map do |node|
242
+ Model::PrecedingInvoice.new(
243
+ number: text(node, PRECEDING_INVOICE_FIELDS[:number]),
244
+ issue_date: parse_cii_date(text(node, PRECEDING_INVOICE_FIELDS[:issue_date])),
245
+ )
246
+ end
247
+ end
248
+
202
249
  def build_allowance_charges(settlement_node)
203
250
  settlement_node.xpath(ALLOWANCE_CHARGE, NS).map do |node|
204
251
  Model::AllowanceCharge.new(
@@ -74,12 +74,40 @@ module Zugpferd
74
74
  def build_agreement(xml, doc)
75
75
  xml["ram"].ApplicableHeaderTradeAgreement do
76
76
  xml["ram"].BuyerReference doc.buyer_reference if doc.buyer_reference
77
- build_party(xml, "SellerTradeParty", doc.seller) if doc.seller
77
+ build_party(xml, "SellerTradeParty", doc.seller, seller: true) if doc.seller
78
78
  build_party(xml, "BuyerTradeParty", doc.buyer) if doc.buyer
79
+
80
+ if doc.seller_order_reference
81
+ xml["ram"].SellerOrderReferencedDocument do
82
+ xml["ram"].IssuerAssignedID doc.seller_order_reference
83
+ end
84
+ end
85
+
86
+ build_buyer_order_reference(xml, doc) if doc.purchase_order_reference
87
+
88
+ if doc.contract_reference
89
+ xml["ram"].ContractReferencedDocument do
90
+ xml["ram"].IssuerAssignedID doc.contract_reference
91
+ end
92
+ end
93
+
94
+ if doc.project_reference
95
+ xml["ram"].SpecifiedProcuringProject do
96
+ xml["ram"].ID doc.project_reference
97
+ xml["ram"].Name PROJECT_NAME
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ def build_buyer_order_reference(xml, doc)
104
+ xml["ram"].BuyerOrderReferencedDocument do
105
+ xml["ram"].IssuerAssignedID doc.purchase_order_reference
79
106
  end
80
107
  end
81
108
 
82
- def build_party(xml, element_name, party)
109
+ # Seller-only terms are written only for the seller
110
+ def build_party(xml, element_name, party, seller: false)
83
111
  xml["ram"].send(element_name) do
84
112
  if party.identifier
85
113
  xml["ram"].ID party.identifier
@@ -87,7 +115,7 @@ module Zugpferd
87
115
 
88
116
  xml["ram"].Name party.name
89
117
 
90
- xml["ram"].Description party.legal_form if party.legal_form
118
+ xml["ram"].Description party.legal_form if seller && party.legal_form
91
119
 
92
120
  if party.legal_registration_id || party.trading_name
93
121
  xml["ram"].SpecifiedLegalOrganization do
@@ -113,6 +141,12 @@ module Zugpferd
113
141
  xml["ram"].ID(party.vat_identifier, schemeID: "VA")
114
142
  end
115
143
  end
144
+
145
+ if seller && party.tax_registration_identifier
146
+ xml["ram"].SpecifiedTaxRegistration do
147
+ xml["ram"].ID(party.tax_registration_identifier, schemeID: "FC")
148
+ end
149
+ end
116
150
  end
117
151
  end
118
152
 
@@ -143,6 +177,11 @@ module Zugpferd
143
177
 
144
178
  def build_delivery(xml, doc)
145
179
  xml["ram"].ApplicableHeaderTradeDelivery do
180
+ if doc.deliver_to_address
181
+ xml["ram"].ShipToTradeParty do
182
+ build_postal_address(xml, doc.deliver_to_address)
183
+ end
184
+ end
146
185
  if doc.delivery_date
147
186
  xml["ram"].ActualDeliverySupplyChainEvent do
148
187
  xml["ram"].OccurrenceDateTime do
@@ -173,6 +212,8 @@ module Zugpferd
173
212
  end
174
213
  end
175
214
 
215
+ build_billing_period(xml, doc) if doc.invoicing_period_start_date || doc.invoicing_period_end_date
216
+
176
217
  doc.allowance_charges.each { |ac| build_allowance_charge(xml, ac) }
177
218
 
178
219
  if doc.payment_instructions&.note || doc.due_date || doc.payment_instructions&.mandate_reference
@@ -188,6 +229,41 @@ module Zugpferd
188
229
  end
189
230
 
190
231
  build_monetary_total(xml, doc.monetary_totals, doc.tax_breakdown) if doc.monetary_totals
232
+
233
+ # CII allows a single InvoiceReferencedDocument, so only the first BG-3 is written
234
+ build_preceding_invoice(xml, doc.preceding_invoices.first) if doc.preceding_invoices.any?
235
+
236
+ if doc.buyer_accounting_reference
237
+ xml["ram"].ReceivableSpecifiedTradeAccountingAccount do
238
+ xml["ram"].ID doc.buyer_accounting_reference
239
+ end
240
+ end
241
+ end
242
+ end
243
+
244
+ def build_preceding_invoice(xml, preceding)
245
+ xml["ram"].InvoiceReferencedDocument do
246
+ xml["ram"].IssuerAssignedID preceding.number
247
+ if preceding.issue_date
248
+ xml["ram"].FormattedIssueDateTime do
249
+ xml["qdt"].DateTimeString(format_cii_date(preceding.issue_date), format: "102")
250
+ end
251
+ end
252
+ end
253
+ end
254
+
255
+ def build_billing_period(xml, doc)
256
+ xml["ram"].BillingSpecifiedPeriod do
257
+ if doc.invoicing_period_start_date
258
+ xml["ram"].StartDateTime do
259
+ xml["udt"].DateTimeString(format_cii_date(doc.invoicing_period_start_date), format: "102")
260
+ end
261
+ end
262
+ if doc.invoicing_period_end_date
263
+ xml["ram"].EndDateTime do
264
+ xml["udt"].DateTimeString(format_cii_date(doc.invoicing_period_end_date), format: "102")
265
+ end
266
+ end
191
267
  end
192
268
  end
193
269
 
@@ -208,6 +284,7 @@ module Zugpferd
208
284
  if payment.account_id
209
285
  xml["ram"].PayeePartyCreditorFinancialAccount do
210
286
  xml["ram"].IBANID payment.account_id
287
+ xml["ram"].AccountName payment.account_name if payment.account_name
211
288
  end
212
289
  end
213
290
  end
@@ -235,8 +312,8 @@ module Zugpferd
235
312
  xml["ram"].TaxTotalAmount(format_decimal(tax_breakdown.tax_amount),
236
313
  currencyID: tax_breakdown.currency_code)
237
314
  end
238
- xml["ram"].GrandTotalAmount format_decimal(totals.tax_inclusive_amount)
239
315
  xml["ram"].RoundingAmount format_decimal(totals.payable_rounding_amount) if totals.payable_rounding_amount
316
+ xml["ram"].GrandTotalAmount format_decimal(totals.tax_inclusive_amount)
240
317
  xml["ram"].TotalPrepaidAmount format_decimal(totals.prepaid_amount) if totals.prepaid_amount
241
318
  xml["ram"].DuePayableAmount format_decimal(totals.payable_amount)
242
319
  end
@@ -279,6 +356,11 @@ module Zugpferd
279
356
  if line.price
280
357
  xml["ram"].NetPriceProductTradePrice do
281
358
  xml["ram"].ChargeAmount format_decimal(line.price.amount)
359
+ if line.price.base_quantity
360
+ attrs = {}
361
+ attrs[:unitCode] = line.price.base_quantity_unit_code if line.price.base_quantity_unit_code
362
+ xml["ram"].BasisQuantity(format_decimal(line.price.base_quantity), attrs)
363
+ end
282
364
  end
283
365
  end
284
366
  end
@@ -297,6 +379,8 @@ module Zugpferd
297
379
  end
298
380
  end
299
381
 
382
+ build_line_period(xml, line) if line.period_start_date || line.period_end_date
383
+
300
384
  xml["ram"].SpecifiedTradeSettlementLineMonetarySummation do
301
385
  xml["ram"].LineTotalAmount format_decimal(line.line_extension_amount)
302
386
  end
@@ -304,6 +388,21 @@ module Zugpferd
304
388
  end
305
389
  end
306
390
 
391
+ def build_line_period(xml, line)
392
+ xml["ram"].BillingSpecifiedPeriod do
393
+ if line.period_start_date
394
+ xml["ram"].StartDateTime do
395
+ xml["udt"].DateTimeString(format_cii_date(line.period_start_date), format: "102")
396
+ end
397
+ end
398
+ if line.period_end_date
399
+ xml["ram"].EndDateTime do
400
+ xml["udt"].DateTimeString(format_cii_date(line.period_end_date), format: "102")
401
+ end
402
+ end
403
+ end
404
+ end
405
+
307
406
  def build_item(xml, item)
308
407
  xml["ram"].SpecifiedTradeProduct do
309
408
  xml["ram"].SellerAssignedID item.sellers_identifier if item.sellers_identifier