en16931-formats 0.2.0

European e-invoicing formats on top of the EN 16931 semantic model: UBL 2.1 and UN/CEFACT CII in both directions, the XRechnung CIUS, and ZUGFeRD / Factur-X hybrid PDFs. Every business rule is delegated to `en16931`.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
//! Reading UBL 2.1 into the semantic model.
//!
//! # It never silently drops anything
//!
//! Every element walked is either mapped or recorded in [`Reader::unmapped`],
//! as `Parent/Child`. A reader that quietly ignored `cbc:TaxExclusiveAmount`
//! would report a clean parse and prove nothing — so the conformance suites
//! assert the unmapped set against an explicit allowlist rather than trusting
//! silence.
//!
//! # Malformed values are reported, not panicked on
//!
//! CEN's own suites carry deliberately malformed values — `<cbc:IssueDate>123`,
//! `<cbc:Amount>.00` — to exercise `BR-DEC-*` and the date rules. `en16931`'s
//! types make those unrepresentable, so the reader records them in
//! [`Reader::malformed`] and leaves the field absent. Losing the distinction
//! between "absent" and "present but unreadable" would make a `BR-03` finding
//! unexplainable, which is why the list exists at all.

use std::collections::BTreeSet;

use en16931::invoice::{
    Code, Contact, CreditTransfer, Delivery, DirectDebit, DocumentAllowanceCharge, Invoice,
    InvoiceLine, InvoiceNote, Item, ItemAttribute, LineAllowanceCharge, LineVat, Party, Payee,
    PaymentCard, PaymentInstructions, PaymentMeans, Period, PostalAddress, PrecedingInvoice,
    PriceDetails, SupportingDocument, TaxRepresentative, VatBreakdown,
};
use en16931::{
    Date, DocumentKind, DocumentReference, Identifier, InvoiceAmount, Percentage, Quantity,
};

/// Reads one UBL `Invoice` or `CreditNote` element into an [`Invoice`].
#[derive(Default)]
pub struct Reader {
    /// Element paths encountered and not mapped, as `Parent/Child`.
    pub unmapped: BTreeSet<String>,
    /// Every `cac:TaxTotal/cbc:TaxAmount` seen, with its `@currencyID`.
    ///
    /// Resolved into BT-110 / BT-111 once BT-5 and BT-6 are known — see
    /// [`Reader::tax_total`].
    tax_amounts: Vec<(Option<String>, Option<InvoiceAmount>)>,
    /// Elements whose text is present but not representable in the model —
    /// `<cbc:IssueDate>123</cbc:IssueDate>`, `<cbc:Amount>.00</cbc:Amount>`.
    ///
    /// The suite uses these to exercise rules about *syntax*: BR-03 asks whether
    /// the element is there, not whether it parses. This crate's types refuse
    /// the value at the boundary instead, so the presence question cannot be
    /// asked of the model at all. Cases that hit one are skipped and counted,
    /// never scored.
    pub malformed: Vec<String>,
}

/// Local name, namespace-independent.
fn name<'i>(n: roxmltree::Node<'_, 'i>) -> &'i str {
    n.tag_name().name()
}

/// Direct element children.
fn kids<'a, 'i>(n: roxmltree::Node<'a, 'i>) -> impl Iterator<Item = roxmltree::Node<'a, 'i>> {
    n.children().filter(roxmltree::Node::is_element)
}

/// The first direct child with this local name.
fn kid<'a, 'i>(n: roxmltree::Node<'a, 'i>, want: &str) -> Option<roxmltree::Node<'a, 'i>> {
    kids(n).find(|c| name(*c) == want)
}

/// Trimmed text content of a direct child.
fn text(n: roxmltree::Node<'_, '_>, want: &str) -> Option<String> {
    kid(n, want)
        .and_then(|c| c.text())
        .map(|t| t.trim().to_owned())
}

/// Trimmed text of this element.
fn own_text(n: roxmltree::Node<'_, '_>) -> String {
    n.text().unwrap_or_default().trim().to_owned()
}

/// An amount, or `None` when the text is not a valid EN 16931 amount.
///
/// Returning `None` rather than panicking is deliberate: several CEN test cases
/// carry a deliberately malformed amount to trigger `BR-DEC-*`, which this
/// crate's types make unrepresentable. Those cases are reported as unreadable,
/// not as failures.
fn amount(n: roxmltree::Node<'_, '_>, want: &str) -> Option<InvoiceAmount> {
    text(n, want).and_then(|t| InvoiceAmount::parse(&t).ok())
}

fn amount_here(n: roxmltree::Node<'_, '_>) -> Option<InvoiceAmount> {
    InvoiceAmount::parse(&own_text(n)).ok()
}

fn decimal(n: roxmltree::Node<'_, '_>, want: &str) -> Option<rust_decimal::Decimal> {
    text(n, want).and_then(|t| t.parse().ok())
}

fn date(n: roxmltree::Node<'_, '_>, want: &str) -> Option<Date> {
    text(n, want).and_then(|t| Date::parse(&t).ok())
}

/// Whether an element is present with text the model cannot hold.
fn is_malformed<T>(
    n: roxmltree::Node<'_, '_>,
    want: &str,
    parse: impl Fn(&str) -> Option<T>,
) -> bool {
    text(n, want).is_some_and(|t| !t.is_empty() && parse(&t).is_none())
}

fn code(n: roxmltree::Node<'_, '_>, want: &str) -> Option<Code> {
    text(n, want).map(Code::new)
}

/// `<cbc:ID schemeID="0088">x</cbc:ID>` → an [`Identifier`] with its scheme.
fn identifier(n: roxmltree::Node<'_, '_>) -> Identifier {
    match n.attribute("schemeID") {
        Some(s) => Identifier::schemed(own_text(n), s),
        None => Identifier::new(own_text(n)),
    }
}

impl Reader {
    /// Note an element this reader does not map.
    fn skip(&mut self, parent: &str, n: roxmltree::Node<'_, '_>) {
        self.unmapped.insert(format!("{parent}/{}", name(n)));
    }

    /// Read a UBL `Invoice` or `CreditNote` root element.
    pub fn read(&mut self, root: roxmltree::Node<'_, '_>) -> Invoice {
        let mut inv = Invoice::default();
        // The root element *is* the document kind, in both syntaxes.
        inv.kind = if name(root) == "CreditNote" {
            DocumentKind::CreditNote
        } else {
            DocumentKind::Invoice
        };
        // UBL splits allowances and charges by `ChargeIndicator`, and the totals
        // by which `TaxTotal` carries subtotals. Both need a second pass.
        for c in kids(root) {
            match name(c) {
                "CustomizationID" => inv.specification_id = Some(own_text(c)),
                "ProfileID" => inv.business_process = Some(own_text(c)),
                "ID" => inv.number = Some(own_text(c)),
                "IssueDate" => {
                    if is_malformed(root, "IssueDate", |t| Date::parse(t).ok()) {
                        self.malformed.push("IssueDate".to_owned());
                    }
                    inv.issue_date = date(root, "IssueDate");
                }
                "DueDate" => inv.due_date = date(root, "DueDate"),
                "InvoiceTypeCode" | "CreditNoteTypeCode" => {
                    inv.type_code = Some(Code::new(own_text(c)));
                }
                "Note" => inv.notes.push(read_note(&own_text(c))),
                "TaxPointDate" => inv.vat_point_date = date(root, "TaxPointDate"),
                "DocumentCurrencyCode" => inv.currency = Some(Code::new(own_text(c))),
                "TaxCurrencyCode" => inv.vat_accounting_currency = Some(Code::new(own_text(c))),
                "AccountingCost" => inv.accounting_reference = Some(own_text(c)),
                "BuyerReference" => inv.buyer_reference = Some(own_text(c)),
                "InvoicePeriod" => {
                    // BT-8 rides inside `cac:InvoicePeriod` in UBL, so an
                    // `InvoicePeriod` carrying *only* a `DescriptionCode` is
                    // BT-8 and **not** BG-14. CEN's suite is explicit about it:
                    // "Verify that BT-8 can be informed even if there is no
                    // Invoice period defined." Treating the element itself as
                    // the group makes `BR-CO-19` fire on a document CEN accepts.
                    if let Some(dc) = code(c, "DescriptionCode") {
                        inv.vat_point_date_code = Some(dc);
                    }
                    let p = self.period(c);
                    if p.start.is_some() || p.end.is_some() {
                        inv.invoicing_period = Some(p);
                    }
                }
                "OrderReference" => {
                    inv.purchase_order_reference = text(c, "ID").map(DocumentReference::new);
                    inv.sales_order_reference = text(c, "SalesOrderID").map(DocumentReference::new);
                }
                "BillingReference" => {
                    if let Some(r) = kid(c, "InvoiceDocumentReference") {
                        inv.preceding_invoices.push(PrecedingInvoice {
                            reference: text(r, "ID")
                                .map_or_else(|| DocumentReference::new(""), DocumentReference::new),
                            issue_date: date(r, "IssueDate"),
                        });
                    }
                }
                "DespatchDocumentReference" => {
                    inv.despatch_advice_reference = text(c, "ID").map(DocumentReference::new);
                }
                "ReceiptDocumentReference" => {
                    inv.receiving_advice_reference = text(c, "ID").map(DocumentReference::new);
                }
                "OriginatorDocumentReference" => {
                    inv.tender_reference = text(c, "ID").map(DocumentReference::new);
                }
                "ContractDocumentReference" => {
                    inv.contract_reference = text(c, "ID").map(DocumentReference::new);
                }
                "ProjectReference" => {
                    inv.project_reference = text(c, "ID").map(DocumentReference::new);
                }
                "AdditionalDocumentReference" => self.additional_document(c, &mut inv),
                "AccountingSupplierParty" => {
                    if let Some(p) = kid(c, "Party") {
                        inv.seller = self.party(p);
                    }
                }
                "AccountingCustomerParty" => {
                    if let Some(p) = kid(c, "Party") {
                        inv.buyer = self.party(p);
                    }
                }
                "PayeeParty" => inv.payee = Some(self.payee(c)),
                "TaxRepresentativeParty" => inv.tax_representative = Some(self.tax_rep(c)),
                "Delivery" => inv.delivery = Some(self.delivery(c)),
                "PaymentMeans" => inv.payment = Some(self.payment(c)),
                // **Not trimmed.** `BR-DE-18` requires the Skonto block to end
                // with a newline, so BT-20's trailing whitespace is load-bearing
                // and trimming it makes a conforming document fail.
                "PaymentTerms" => {
                    inv.payment_terms = kid(c, "Note").and_then(|n| n.text()).map(str::to_owned);
                }
                "AllowanceCharge" => {
                    let is_charge = text(c, "ChargeIndicator").as_deref() == Some("true");
                    let ac = self.allowance(c);
                    if is_charge {
                        inv.charges.push(ac);
                    } else {
                        inv.allowances.push(ac);
                    }
                }
                "TaxTotal" => self.tax_total(c, &mut inv),
                // `cac:PrepaidPayment` is BG-DEX-09, the XRechnung Extension's
                // third-party payment. Core UBL maps BT-113 to the same element,
                // but the Extension's instances carry all three DEX terms.
                "PrepaidPayment" => {
                    inv.extensions
                        .third_party_payments
                        .push(en16931::ThirdPartyPayment {
                            payment_type: text(c, "ID"),
                            amount: amount(c, "PaidAmount"),
                            description: text(c, "InstructionID"),
                        });
                }
                "LegalMonetaryTotal" => self.totals(c, &mut inv),
                "InvoiceLine" | "CreditNoteLine" => {
                    let l = self.line(c);
                    // BG-DEX-01 hangs beneath the line, keyed by its index.
                    let subs = self.sub_lines(c);
                    if !subs.is_empty() {
                        inv.extensions
                            .sub_invoice_lines
                            .push((inv.lines.len(), subs));
                    }
                    inv.lines.push(l);
                }
                // Syntax-only: UBL bookkeeping with no business term.
                "UBLVersionID"
                | "CopyIndicator"
                | "UUID"
                | "IssueTime"
                | "LineCountNumeric"
                | "Signature"
                | "TaxExchangeRate"
                | "InvoiceDocumentReference" => {}
                _ => self.skip("Invoice", c),
            }
        }
        // BT-90 lives on the *seller* in UBL — `cac:PartyIdentification` with
        // `schemeID="SEPA"` — while the model puts it in BG-19, where the rule
        // that needs it is. `BR-DE-30` cannot be checked without this hop.
        let sepa = inv
            .seller
            .identifiers
            .iter()
            .find(|id| id.scheme() == Some("SEPA"))
            .map(|id| id.content().to_owned());
        if let Some(PaymentMeans::DirectDebit(dd)) =
            inv.payment.as_mut().and_then(|p| p.means.as_mut())
        {
            dd.creditor_identifier = sepa;
        }

        // Resolve BT-110 / BT-111 now that BT-5 and BT-6 are known.
        //
        // CEN's own `BR-53` binding is
        // `exists(//cac:TaxTotal/cbc:TaxAmount[@currencyID = $taxcurrency])`,
        // which is satisfied by the *document-currency* total whenever BT-6
        // equals BT-5. So when the two currencies coincide, one element is both
        // BT-110 and BT-111 — and a reader that assigns it to only one of them
        // makes `BR-53` fire on a document CEN publishes as an example.
        let doc_ccy = inv.currency.as_ref().map(|c| c.as_str().to_owned());
        let tax_ccy = inv
            .vat_accounting_currency
            .as_ref()
            .map(|c| c.as_str().to_owned());
        for (ccy, amount) in std::mem::take(&mut self.tax_amounts) {
            // No `@currencyID` means the document currency.
            let is_doc = ccy.is_none() || ccy == doc_ccy;
            let is_tax = tax_ccy.is_some() && ccy == tax_ccy;
            if is_doc && inv.totals.vat_total.is_none() {
                inv.totals.vat_total = amount;
            }
            if is_tax && inv.totals.vat_total_accounting.is_none() {
                inv.totals.vat_total_accounting = amount;
            }
        }
        inv
    }

    fn period(&mut self, n: roxmltree::Node<'_, '_>) -> Period {
        for c in kids(n) {
            if !matches!(name(c), "StartDate" | "EndDate" | "DescriptionCode") {
                self.skip("InvoicePeriod", c);
            }
        }
        Period {
            start: date(n, "StartDate"),
            end: date(n, "EndDate"),
        }
    }

    fn address(&mut self, n: roxmltree::Node<'_, '_>) -> PostalAddress {
        let mut a = PostalAddress::default();
        for c in kids(n) {
            match name(c) {
                "StreetName" => a.line1 = Some(own_text(c)),
                "AdditionalStreetName" => a.line2 = Some(own_text(c)),
                "AddressLine" => a.line3 = text(c, "Line"),
                "CityName" => a.city = Some(own_text(c)),
                "PostalZone" => a.post_code = Some(own_text(c)),
                "CountrySubentity" => a.subdivision = Some(own_text(c)),
                "Country" => a.country = code(c, "IdentificationCode"),
                _ => self.skip("PostalAddress", c),
            }
        }
        a
    }

    fn party(&mut self, n: roxmltree::Node<'_, '_>) -> Party {
        let mut p = Party::default();
        for c in kids(n) {
            match name(c) {
                "EndpointID" => p.electronic_address = Some(identifier(c)),
                "PartyIdentification" => {
                    if let Some(id) = kid(c, "ID") {
                        p.identifiers.push(identifier(id));
                    }
                }
                "PartyName" => p.trading_name = text(c, "Name"),
                "PostalAddress" => p.address = self.address(c),
                "PartyTaxScheme" => {
                    // BT-31 versus BT-32 is decided by the tax scheme, not by
                    // position: `VAT` is the VAT identifier, anything else the
                    // registration identifier.
                    let scheme = kid(c, "TaxScheme").and_then(|s| text(s, "ID"));
                    let id = text(c, "CompanyID");
                    if scheme.as_deref() == Some("VAT") {
                        p.vat_identifier = id;
                    } else {
                        p.tax_registration = id;
                    }
                }
                "PartyLegalEntity" => {
                    // BT-27 / BT-44 is the *registration* name in UBL, not
                    // `PartyName/Name` — that is BT-28 / BT-45.
                    if let Some(rn) = text(c, "RegistrationName") {
                        p.name = Some(rn);
                    }
                    if let Some(cid) = kid(c, "CompanyID") {
                        p.legal_registration = Some(identifier(cid));
                    }
                    if let Some(extra) = text(c, "CompanyLegalForm") {
                        p.additional_legal_information = Some(extra);
                    }
                }
                "Contact" => {
                    p.contact = Contact {
                        name: text(c, "Name"),
                        phone: text(c, "Telephone"),
                        email: text(c, "ElectronicMail"),
                    };
                }
                _ => self.skip("Party", c),
            }
        }
        p
    }

    fn payee(&mut self, n: roxmltree::Node<'_, '_>) -> Payee {
        // `cac:PayeeParty` *is* a party in UBL, but CEN's suite also nests a
        // `cac:Party` inside it in a few cases. Accept both.
        let p = self.party(kid(n, "Party").unwrap_or(n));
        Payee {
            name: p.name.or(p.trading_name),
            identifier: p.identifiers.into_iter().next(),
            legal_registration: p.legal_registration,
        }
    }

    fn tax_rep(&mut self, n: roxmltree::Node<'_, '_>) -> TaxRepresentative {
        let p = self.party(n);
        TaxRepresentative {
            name: p.name.or(p.trading_name),
            vat_identifier: p.vat_identifier,
            address: p.address,
        }
    }

    fn delivery(&mut self, n: roxmltree::Node<'_, '_>) -> Delivery {
        let mut d = Delivery::default();
        for c in kids(n) {
            match name(c) {
                "ActualDeliveryDate" => d.date = date(n, "ActualDeliveryDate"),
                "DeliveryLocation" => {
                    if let Some(id) = kid(c, "ID") {
                        d.location = Some(identifier(id));
                    }
                    if let Some(a) = kid(c, "Address") {
                        d.address = Some(self.address(a));
                    }
                }
                "DeliveryParty" => d.party_name = kid(c, "PartyName").and_then(|p| text(p, "Name")),
                _ => self.skip("Delivery", c),
            }
        }
        d
    }

    fn payment(&mut self, n: roxmltree::Node<'_, '_>) -> PaymentInstructions {
        let mut p = PaymentInstructions {
            means_code: code(n, "PaymentMeansCode"),
            means_text: kid(n, "PaymentMeansCode")
                .and_then(|c| c.attribute("name").map(str::to_owned)),
            remittance_information: text(n, "PaymentID"),
            means: None,
        };
        for c in kids(n) {
            match name(c) {
                "PayeeFinancialAccount" => {
                    p.means = Some(PaymentMeans::CreditTransfer(vec![CreditTransfer {
                        account_identifier: text(c, "ID"),
                        account_name: text(c, "Name"),
                        provider_identifier: kid(c, "FinancialInstitutionBranch")
                            .and_then(|b| text(b, "ID")),
                    }]));
                }
                "CardAccount" => {
                    p.means = Some(PaymentMeans::Card(PaymentCard {
                        primary_account_number: text(c, "PrimaryAccountNumberID"),
                        holder_name: text(c, "HolderName"),
                    }));
                }
                "PaymentMandate" => {
                    p.means = Some(PaymentMeans::DirectDebit(DirectDebit {
                        mandate_reference: text(c, "ID"),
                        creditor_identifier: None,
                        debited_account: kid(c, "PayerFinancialAccount")
                            .and_then(|a| text(a, "ID")),
                    }));
                }
                "PaymentMeansCode" | "PaymentID" | "PaymentDueDate" | "InstructionNote" => {}
                _ => self.skip("PaymentMeans", c),
            }
        }
        p
    }

    fn tax_category(
        &mut self,
        n: roxmltree::Node<'_, '_>,
    ) -> (Code, Option<Percentage>, Option<String>, Option<Code>) {
        let mut reason = None;
        let mut reason_code = None;
        for c in kids(n) {
            match name(c) {
                "ID" | "Percent" | "TaxScheme" => {}
                "TaxExemptionReason" => reason = Some(own_text(c)),
                "TaxExemptionReasonCode" => reason_code = Some(Code::new(own_text(c))),
                _ => self.skip("TaxCategory", c),
            }
        }
        (
            code(n, "ID").unwrap_or_default(),
            decimal(n, "Percent").map(Percentage::new),
            reason,
            reason_code,
        )
    }

    fn allowance(&mut self, n: roxmltree::Node<'_, '_>) -> DocumentAllowanceCharge {
        let mut vat = LineVat::default();
        for c in kids(n) {
            match name(c) {
                "TaxCategory" => {
                    let (cat, pct, _, _) = self.tax_category(c);
                    vat = LineVat {
                        category: cat,
                        rate: pct,
                    };
                }
                "ChargeIndicator"
                | "Amount"
                | "BaseAmount"
                | "MultiplierFactorNumeric"
                | "AllowanceChargeReason"
                | "AllowanceChargeReasonCode"
                | "TaxScheme" => {}
                _ => self.skip("AllowanceCharge", c),
            }
        }
        DocumentAllowanceCharge {
            amount: amount(n, "Amount").unwrap_or_default(),
            base_amount: amount(n, "BaseAmount"),
            percentage: decimal(n, "MultiplierFactorNumeric").map(Percentage::new),
            vat,
            reason: text(n, "AllowanceChargeReason"),
            reason_code: code(n, "AllowanceChargeReasonCode"),
        }
    }

    fn tax_total(&mut self, n: roxmltree::Node<'_, '_>, inv: &mut Invoice) {
        // BT-110 and BT-111 are the **same element** in UBL, told apart only by
        // `@currencyID`. Record every `TaxTotal/TaxAmount` with its currency and
        // resolve after BT-5 and BT-6 are known — deciding by document order
        // instead gets it backwards whenever the accounting-currency total comes
        // first, and cannot express the case where one element is both.
        if let Some(ta) = kid(n, "TaxAmount") {
            self.tax_amounts.push((
                ta.attribute("currencyID").map(str::to_owned),
                amount_here(ta),
            ));
        }
        let subtotals: Vec<_> = kids(n).filter(|c| name(*c) == "TaxSubtotal").collect();
        if subtotals.is_empty() {
            return;
        }
        for st in subtotals {
            let mut cat = Code::default();
            let mut rate = None;
            let mut reason = None;
            let mut reason_code = None;
            for c in kids(st) {
                match name(c) {
                    "TaxCategory" => {
                        let (a, b, r, rc) = self.tax_category(c);
                        cat = a;
                        rate = b;
                        reason = r;
                        reason_code = rc;
                    }
                    "TaxableAmount" | "TaxAmount" => {}
                    _ => self.skip("TaxSubtotal", c),
                }
            }
            inv.vat_breakdown.push(VatBreakdown {
                taxable_amount: amount(st, "TaxableAmount").unwrap_or_default(),
                tax_amount: amount(st, "TaxAmount").unwrap_or_default(),
                category: cat,
                rate,
                exemption_reason: reason,
                exemption_reason_code: reason_code,
            });
        }
    }

    /// An amount, recording the value if the model cannot hold it.
    fn opt_amt(&mut self, n: roxmltree::Node<'_, '_>, what: &str) -> Option<InvoiceAmount> {
        let raw = own_text(n);
        let parsed = InvoiceAmount::parse(&raw).ok();
        if parsed.is_none() && !raw.is_empty() {
            self.malformed.push(format!("{what}={raw}"));
        }
        parsed
    }

    fn amt(&mut self, n: roxmltree::Node<'_, '_>, what: &str) -> InvoiceAmount {
        self.opt_amt(n, what).unwrap_or_default()
    }

    fn totals(&mut self, n: roxmltree::Node<'_, '_>, inv: &mut Invoice) {
        let t = &mut inv.totals;
        for c in kids(n) {
            match name(c) {
                "LineExtensionAmount" => t.line_total = self.amt(c, "LineExtensionAmount"),
                "TaxExclusiveAmount" => t.taxable_total = self.amt(c, "TaxExclusiveAmount"),
                "TaxInclusiveAmount" => t.gross_total = self.amt(c, "TaxInclusiveAmount"),
                "AllowanceTotalAmount" => {
                    t.allowance_total = self.opt_amt(c, "AllowanceTotalAmount");
                }
                "ChargeTotalAmount" => t.charge_total = self.opt_amt(c, "ChargeTotalAmount"),
                "PrepaidAmount" => t.paid = self.opt_amt(c, "PrepaidAmount"),
                "PayableRoundingAmount" => t.rounding = self.opt_amt(c, "PayableRoundingAmount"),
                "PayableAmount" => t.due = self.amt(c, "PayableAmount"),
                _ => self.skip("LegalMonetaryTotal", c),
            }
        }
    }

    fn line(&mut self, n: roxmltree::Node<'_, '_>) -> InvoiceLine {
        let mut l = InvoiceLine {
            id: text(n, "ID").unwrap_or_default(),
            note: text(n, "Note"),
            order_line_reference: None,
            accounting_reference: text(n, "AccountingCost"),
            object_identifier: None,
            quantity: Quantity::new(
                text(n, "InvoicedQuantity")
                    .or_else(|| text(n, "CreditedQuantity"))
                    .and_then(|q| q.parse().ok())
                    .unwrap_or_default(),
            ),
            unit_code: kid(n, "InvoicedQuantity")
                .or_else(|| kid(n, "CreditedQuantity"))
                .and_then(|q| q.attribute("unitCode"))
                .map(Code::new)
                .unwrap_or_default(),
            net_amount: amount(n, "LineExtensionAmount").unwrap_or_default(),
            period: None,
            allowances: vec![],
            charges: vec![],
            price: PriceDetails::default(),
            vat: LineVat::default(),
            item: Item::default(),
        };
        for c in kids(n) {
            match name(c) {
                "InvoicePeriod" => l.period = Some(self.period(c)),
                "OrderLineReference" => {
                    l.order_line_reference = text(c, "LineID").map(DocumentReference::new);
                }
                "DocumentReference" => {
                    if text(c, "DocumentTypeCode").as_deref() == Some("130")
                        && let Some(id) = kid(c, "ID")
                    {
                        l.object_identifier = Some(identifier(id));
                    }
                }
                "AllowanceCharge" => {
                    let is_charge = text(c, "ChargeIndicator").as_deref() == Some("true");
                    let a = LineAllowanceCharge {
                        amount: amount(c, "Amount").unwrap_or_default(),
                        base_amount: amount(c, "BaseAmount"),
                        percentage: decimal(c, "MultiplierFactorNumeric").map(Percentage::new),
                        reason: text(c, "AllowanceChargeReason"),
                        reason_code: code(c, "AllowanceChargeReasonCode"),
                    };
                    if is_charge {
                        l.charges.push(a);
                    } else {
                        l.allowances.push(a);
                    }
                }
                "Price" => l.price = self.price(c),
                "Item" => {
                    let (item, vat) = self.item(c);
                    l.item = item;
                    l.vat = vat;
                }
                "ID"
                | "Note"
                | "InvoicedQuantity"
                | "CreditedQuantity"
                | "LineExtensionAmount"
                | "AccountingCost"
                | "TaxTotal"
                | "SubInvoiceLine" => {}
                _ => self.skip("InvoiceLine", c),
            }
        }
        l
    }

    /// `cac:SubInvoiceLine` children, recursively — BG-DEX-01.
    fn sub_lines(&mut self, n: roxmltree::Node<'_, '_>) -> Vec<en16931::SubInvoiceLine> {
        kids(n)
            .filter(|c| name(*c) == "SubInvoiceLine")
            .map(|c| {
                let line = self.line(c);
                // BG-DEX-06 is the sub-line's own `ClassifiedTaxCategory`, and
                // `BR-DEX-03` requires **exactly one**. The model holds nought
                // or one, so two is mapped to `None` — the rule fires either
                // way, which is what "exactly one" means.
                let categories = kid(c, "Item").map_or(0, |i| {
                    kids(i)
                        .filter(|x| name(*x) == "ClassifiedTaxCategory")
                        .count()
                });
                en16931::SubInvoiceLine {
                    vat: (categories == 1).then(|| line.vat.clone()),
                    line,
                    children: self.sub_lines(c),
                }
            })
            .collect()
    }

    fn price(&mut self, n: roxmltree::Node<'_, '_>) -> PriceDetails {
        let mut p = PriceDetails {
            net_price: text(n, "PriceAmount")
                .and_then(|t| t.parse().ok())
                .map(en16931::UnitPriceAmount::new)
                .unwrap_or_default(),
            price_discount: None,
            gross_price: None,
            base_quantity: decimal(n, "BaseQuantity").map(Quantity::new),
            base_quantity_code: kid(n, "BaseQuantity")
                .and_then(|q| q.attribute("unitCode"))
                .map(Code::new),
        };
        for c in kids(n) {
            match name(c) {
                "AllowanceCharge" => {
                    // BG-29's discount and gross price hide inside a price-level
                    // allowance in UBL: `Amount` is BT-147, `BaseAmount` BT-148.
                    p.price_discount = text(c, "Amount")
                        .and_then(|t| t.parse().ok())
                        .map(en16931::UnitPriceAmount::new);
                    p.gross_price = text(c, "BaseAmount")
                        .and_then(|t| t.parse().ok())
                        .map(en16931::UnitPriceAmount::new);
                }
                "PriceAmount" | "BaseQuantity" => {}
                _ => self.skip("Price", c),
            }
        }
        p
    }

    fn item(&mut self, n: roxmltree::Node<'_, '_>) -> (Item, LineVat) {
        let mut item = Item {
            name: text(n, "Name"),
            description: text(n, "Description"),
            seller_identifier: kid(n, "SellersItemIdentification").and_then(|i| text(i, "ID")),
            buyer_identifier: kid(n, "BuyersItemIdentification").and_then(|i| text(i, "ID")),
            standard_identifier: kid(n, "StandardItemIdentification")
                .and_then(|i| kid(i, "ID"))
                .map(identifier),
            classification_identifiers: vec![],
            origin_country: kid(n, "OriginCountry").and_then(|c| code(c, "IdentificationCode")),
            attributes: vec![],
        };
        let mut vat = LineVat::default();
        for c in kids(n) {
            match name(c) {
                "ClassifiedTaxCategory" => {
                    let (cat, pct, _, _) = self.tax_category(c);
                    vat = LineVat {
                        category: cat,
                        rate: pct,
                    };
                }
                "CommodityClassification" => {
                    if let Some(icc) = kid(c, "ItemClassificationCode") {
                        // The scheme is `listID`, not `schemeID`, on this one.
                        let id = match icc.attribute("listID") {
                            Some(s) => Identifier::schemed(own_text(icc), s),
                            None => Identifier::new(own_text(icc)),
                        };
                        item.classification_identifiers.push(id);
                    }
                }
                "AdditionalItemProperty" => item.attributes.push(ItemAttribute {
                    name: text(c, "Name"),
                    value: text(c, "Value"),
                }),
                "Name"
                | "Description"
                | "SellersItemIdentification"
                | "BuyersItemIdentification"
                | "StandardItemIdentification"
                | "OriginCountry" => {}
                _ => self.skip("Item", c),
            }
        }
        (item, vat)
    }

    fn additional_document(&mut self, n: roxmltree::Node<'_, '_>, inv: &mut Invoice) {
        // Document type code `130` is BT-18, the invoiced object identifier.
        // Everything else is BG-24, a supporting document.
        if text(n, "DocumentTypeCode").as_deref() == Some("130") {
            if let Some(id) = kid(n, "ID") {
                inv.object_identifier = Some(identifier(id));
            }
            return;
        }
        // An attachment missing its mime code or filename violates §6.5.11, and
        // `Attachment::new` refuses it. The suites contain such documents on
        // purpose, so record it as unreadable rather than unwrapping.
        let attachment = kid(n, "Attachment")
            .and_then(|a| kid(a, "EmbeddedDocumentBinaryObject"))
            .and_then(|b| {
                match en16931::Attachment::new(
                    crate::xml::decode_base64(&own_text(b)),
                    b.attribute("mimeCode").unwrap_or_default(),
                    b.attribute("filename").unwrap_or_default(),
                ) {
                    Ok(a) => Some(a),
                    Err(e) => {
                        self.malformed
                            .push(format!("EmbeddedDocumentBinaryObject: {e}"));
                        None
                    }
                }
            });
        let uri = kid(n, "Attachment")
            .and_then(|a| kid(a, "ExternalReference"))
            .and_then(|e| text(e, "URI"));
        inv.attachments.push(SupportingDocument {
            reference: text(n, "ID")
                .map_or_else(|| DocumentReference::new(""), DocumentReference::new),
            description: text(n, "DocumentDescription"),
            uri,
            attachment,
        });
    }
}

/// UBL embeds BT-21 in the note text as `#AAI#the text`.
fn read_note(raw: &str) -> InvoiceNote {
    if let Some(rest) = raw.strip_prefix('#')
        && let Some((subject, body)) = rest.split_once('#')
        && subject.len() == 3
    {
        return InvoiceNote {
            subject_code: Some(Code::new(subject)),
            note: Some(body.to_owned()),
        };
    }
    InvoiceNote::new(raw)
}