Repository navigation
Expand file tree
/
Copy pathvalidate_core.go
More file actions
947 lines (871 loc) · 48.4 KB
/
Copy pathvalidate_core.go
File metadata and controls
947 lines (871 loc) · 48.4 KB
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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
package einvoice
import (
"fmt"
"github.com/shopspring/decimal"
"github.com/speedata/einvoice/rules"
)
func (inv *Invoice) validateCalculations() {
var sum decimal.Decimal
// BR-CO-9 VAT identifier country prefix validation
// VAT identifiers shall have ISO 3166-1 alpha-2 country prefix (e.g., DE, FR, GB). Greece may use 'EL' or 'GR'.
validateVATIDPrefix := func(vatID string, fieldName string) {
if vatID == "" {
return // Empty VAT IDs are handled by other rules
}
if len(vatID) < 2 {
inv.addViolation(rules.BRCO9, fieldName+" must have at least 2-character country prefix")
return
}
// Extract first 2 characters as potential country code
prefix := vatID[:2]
// Check if it's uppercase letters (basic validation for country code format)
if prefix[0] < 'A' || prefix[0] > 'Z' || prefix[1] < 'A' || prefix[1] > 'Z' {
inv.addViolation(rules.BRCO9, fmt.Sprintf("%s must start with 2-letter ISO 3166-1 alpha-2 country code (got: %s)", fieldName, prefix))
}
// Note: Full validation against all valid ISO codes would require maintaining a complete list
}
validateVATIDPrefix(inv.Seller.VATaxRegistration, "Seller VAT identifier (BT-31)")
validateVATIDPrefix(inv.Buyer.VATaxRegistration, "Buyer VAT identifier (BT-48)")
if inv.SellerTaxRepresentativeTradeParty != nil {
validateVATIDPrefix(inv.SellerTaxRepresentativeTradeParty.VATaxRegistration, "Seller tax representative VAT identifier (BT-63)")
}
// BR-CO-3 Rechnung
// Umsatzsteuerdatum "Value added tax point date" (BT-7) und Code für das Umsatzsteuerdatum "Value added tax point date code" (BT-8)
// schließen sich gegenseitig aus.
for i := range inv.TradeTaxes {
if !inv.TradeTaxes[i].TaxPointDate.IsZero() && inv.TradeTaxes[i].DueDateTypeCode != "" {
inv.addViolation(rules.BRCO3, "TaxPointDate and DueDateTypeCode are mutually exclusive")
break
}
}
// BR-CO-4 Rechnungsposition
// Jede Rechnungsposition "INVOICE LINE" (BG-25) muss anhand der Umsatzsteuerkategorie des in Rechnung gestellten Postens "Invoiced item VAT
// category code" (BT-151) kategorisiert werden.
// Sub invoice line aggregation lines (GROUP / INFORMATION) may omit the VAT
// category (BR-FXEXT) and must be excluded from document-level sums.
for i := range inv.InvoiceLines {
if !inv.InvoiceLines[i].isDetailLine() {
continue
}
if inv.InvoiceLines[i].TaxCategoryCode == "" {
inv.addViolation(rules.BRCO4, fmt.Sprintf("Invoice line %s missing VAT category code", inv.InvoiceLines[i].LineID))
}
}
// BR-CO-10 Gesamtsummen auf Dokumentenebene
// Der Inhalt des Elementes "Sum of Invoice line net amount" (BT-106) entspricht der Summe aller Inhalte der Elemente "Invoice line net amount"
// (BT-131).
// Note: Only validate when invoice lines exist (Minimum profile may not have lines)
if len(inv.InvoiceLines) > 0 {
sum = decimal.Zero
detailCount := 0
for i := range inv.InvoiceLines {
if !inv.InvoiceLines[i].isDetailLine() {
continue
}
sum = sum.Add(inv.InvoiceLines[i].Total)
detailCount++
}
// In the EXTENDED profile BR-CO-10 is replaced by BR-FXEXT-CO-10, which
// allows a tolerance of 0.01 per line net amount (Factur-X 1.09).
if inv.IsExtended() {
tolerance := decimal.New(1, -2).Mul(decimal.NewFromInt(int64(detailCount)))
if inv.LineTotal.Sub(sum).Abs().GreaterThan(tolerance) {
inv.addViolation(rules.BRFXEXTCO10, fmt.Sprintf("Line total %s does not match sum of invoice lines %s (tolerance %s)", inv.LineTotal.String(), sum.String(), tolerance.String()))
}
} else if !inv.LineTotal.Equal(sum) {
inv.addViolation(rules.BRCO10, fmt.Sprintf("Line total %s does not match sum of invoice lines %s", inv.LineTotal.String(), sum.String()))
}
}
// BR-CO-11 Gesamtsummen auf Dokumentenebene
// Der Inhalt des Elementes "Sum of allowances on document level" (BT-107) entspricht der Summe aller Inhalte
// der Elemente "Document level allowance amount" (BT-92).
calculatedAllowanceTotal := decimal.Zero
for i := range inv.SpecifiedTradeAllowanceCharge {
if !inv.SpecifiedTradeAllowanceCharge[i].ChargeIndicator {
calculatedAllowanceTotal = calculatedAllowanceTotal.Add(inv.SpecifiedTradeAllowanceCharge[i].ActualAmount)
}
}
if !inv.AllowanceTotal.Equal(calculatedAllowanceTotal) {
inv.addViolation(rules.BRCO11, fmt.Sprintf("Allowance total %s does not match sum of document level allowances %s", inv.AllowanceTotal.String(), calculatedAllowanceTotal.String()))
}
// BR-CO-12 Gesamtsummen auf Dokumentenebene
// Der Inhalt des Elementes "Sum of charges on document level" (BT-108) entspricht der Summe aller Inhalte
// der Elemente "Document level charge amount" (BT-99).
calculatedChargeTotal := decimal.Zero
for i := range inv.SpecifiedTradeAllowanceCharge {
if inv.SpecifiedTradeAllowanceCharge[i].ChargeIndicator {
calculatedChargeTotal = calculatedChargeTotal.Add(inv.SpecifiedTradeAllowanceCharge[i].ActualAmount)
}
}
if !inv.ChargeTotal.Equal(calculatedChargeTotal) {
inv.addViolation(rules.BRCO12, fmt.Sprintf("Charge total %s does not match sum of document level charges %s", inv.ChargeTotal.String(), calculatedChargeTotal.String()))
}
// BR-CO-13 Gesamtsummen auf Dokumentenebene
// Der Inhalt des Elementes "Invoice total amount without VAT" (BT-109) entspricht der Summe aus "Sum of Invoice line net amount"
// (BT-106) abzüglich "Sum of allowances on document level" (BT-107) zuzüglich "Sum of charges on document level" (BT-108).
// Note: Minimum profile may not have LineTotal populated, so only validate when invoice lines exist
if len(inv.InvoiceLines) > 0 || !inv.LineTotal.IsZero() {
expectedTaxBasisTotal := inv.LineTotal.Sub(inv.AllowanceTotal).Add(inv.ChargeTotal)
if !inv.TaxBasisTotal.Equal(expectedTaxBasisTotal) {
inv.addViolation(rules.BRCO13, fmt.Sprintf("Tax basis total %s does not match LineTotal - AllowanceTotal + ChargeTotal = %s", inv.TaxBasisTotal.String(), expectedTaxBasisTotal.String()))
}
}
// BR-CO-14 Gesamtsummen auf Dokumentenebene
// Der Inhalt des Elementes "Invoice total VAT amount" (BT-110) entspricht der
// Summe aller Inhalte der Elemente "VAT category tax amount" (BT-117).
// Note: Minimum profile may not have VAT breakdown, so only validate when TradeTaxes exist
if len(inv.TradeTaxes) > 0 {
calculatedTaxTotal := decimal.Zero
for i := range inv.TradeTaxes {
calculatedTaxTotal = calculatedTaxTotal.Add(inv.TradeTaxes[i].CalculatedAmount)
}
if !inv.TaxTotal.Equal(calculatedTaxTotal) {
inv.addViolation(rules.BRCO14, fmt.Sprintf("Invoice total VAT amount %s does not match sum of VAT category amounts %s", inv.TaxTotal.String(), calculatedTaxTotal.String()))
}
}
// BR-CO-15 Gesamtsummen auf Dokumentenebene
// Der Inhalt des Elementes "Invoice total amount with VAT" (BT-112) entspricht der Summe aus "Invoice total amount without VAT"
// (BT-109) und "Invoice total VAT amount" (BT-110).
// Per EN 16931 schematron: TaxInclusiveAmount = round((TaxExclusiveAmount + TaxAmount) * 100) / 100
// This applies 2-decimal rounding to the calculated side to account for rounding during invoice generation
expectedGrandTotal := roundHalfUp(inv.TaxBasisTotal.Add(inv.TaxTotal), 2)
if !inv.GrandTotal.Equal(expectedGrandTotal) {
inv.addViolation(rules.BRCO15, fmt.Sprintf("Grand total %s does not match TaxBasisTotal + TaxTotal = %s", inv.GrandTotal.String(), expectedGrandTotal.String()))
}
// BR-CO-16 Gesamtsummen auf Dokumentenebene
// Der Inhalt des Elementes "Amount due for payment" (BT-115) entspricht der Summe aus "Invoice total amount with VAT" (BT-112)
// abzüglich "Paid amount" (BT-113) zuzüglich "Rounding amount" (BT-114).
// Per EN 16931 schematron, this rule has different cases based on which fields are present:
// 1. If PrepaidAmount exists but not RoundingAmount: round((TaxInclusiveAmount - PrepaidAmount) * 100) / 100 = PayableAmount
// 2. If neither exists: PayableAmount = TaxInclusiveAmount
// 3. If both exist: round((PayableAmount - RoundingAmount) * 100) / 100 = round((TaxInclusiveAmount - PrepaidAmount) * 100) / 100
// 4. If only RoundingAmount exists: round((PayableAmount - RoundingAmount) * 100) / 100 = TaxInclusiveAmount
hasPrepaid := !inv.TotalPrepaid.IsZero()
hasRounding := !inv.RoundingAmount.IsZero()
var valid bool
switch {
case hasPrepaid && !hasRounding:
// Case 1: round((GrandTotal - PrepaidAmount) * 100) / 100 = DuePayableAmount
expected := roundHalfUp(inv.GrandTotal.Sub(inv.TotalPrepaid), 2)
valid = inv.DuePayableAmount.Equal(expected)
case !hasPrepaid && !hasRounding:
// Case 2: DuePayableAmount = GrandTotal
valid = inv.DuePayableAmount.Equal(inv.GrandTotal)
case hasPrepaid && hasRounding:
// Case 3: round((DuePayableAmount - RoundingAmount) * 100) / 100 = round((GrandTotal - PrepaidAmount) * 100) / 100
leftSide := roundHalfUp(inv.DuePayableAmount.Sub(inv.RoundingAmount), 2)
rightSide := roundHalfUp(inv.GrandTotal.Sub(inv.TotalPrepaid), 2)
valid = leftSide.Equal(rightSide)
default:
// Case 4: !hasPrepaid && hasRounding: round((DuePayableAmount - RoundingAmount) * 100) / 100 = GrandTotal
leftSide := roundHalfUp(inv.DuePayableAmount.Sub(inv.RoundingAmount), 2)
valid = leftSide.Equal(inv.GrandTotal)
}
if !valid {
expectedDuePayableAmount := inv.GrandTotal.Sub(inv.TotalPrepaid).Add(inv.RoundingAmount)
inv.addViolation(rules.BRCO16, fmt.Sprintf("Due payable amount %s does not match GrandTotal - TotalPrepaid + RoundingAmount = %s (with rounding)", inv.DuePayableAmount.String(), expectedDuePayableAmount.String()))
}
// BR-CO-17 Umsatzsteueraufschlüsselung
// Der Inhalt des Elementes "VAT category tax amount" (BT-117) entspricht dem Inhalt des Elementes "VAT category taxable amount" (BT-116),
// multipliziert mit dem Inhalt des Elementes "VAT category rate" (BT-119) geteilt durch 100, gerundet auf zwei Dezimalstellen.
for i := range inv.TradeTaxes {
expected := roundHalfUp(inv.TradeTaxes[i].BasisAmount.Mul(inv.TradeTaxes[i].Percent).Div(decimal100), 2)
if !inv.TradeTaxes[i].CalculatedAmount.Equal(expected) {
inv.addViolation(rules.BRCO17, fmt.Sprintf("VAT category tax amount %s does not match expected %s (basis %s × rate %s ÷ 100)", inv.TradeTaxes[i].CalculatedAmount.String(), expected.String(), inv.TradeTaxes[i].BasisAmount.String(), inv.TradeTaxes[i].Percent.String()))
}
}
// BR-CO-18 Umsatzsteueraufschlüsselung
// Eine Rechnung (INVOICE) soll mindestens eine Gruppe "VAT BREAKDOWN" (BG-23) enthalten.
// Note: This rule only applies to profiles >= BasicWL (Minimum profile doesn't require VAT breakdown)
if inv.ProfileLevel() >= levelBasicWL && len(inv.TradeTaxes) < 1 {
inv.addViolation(rules.BRCO18, "Invoice should contain at least one VAT BREAKDOWN")
}
// BR-CO-19 Liefer- oder Rechnungszeitraum
// Wenn die Gruppe "INVOICING PERIOD" (BG-14) verwendet wird, müssen entweder das Element "Invoicing period start date" (BT-73) oder das
// Element "Invoicing period end date" (BT-74) oder beide gefüllt sein.
// Note: Only validates parsed XML where BG-14 element was present (tracked via hasBillingPeriodInXML flag).
if inv.isParsed && inv.hasBillingPeriodInXML {
if inv.BillingSpecifiedPeriodStart.IsZero() && inv.BillingSpecifiedPeriodEnd.IsZero() {
inv.addViolation(rules.BRCO19, "If invoicing period (BG-14) is used, either start date (BT-73) or end date (BT-74) must be filled")
}
}
// BR-CO-20 Rechnungszeitraum auf Positionsebene
// Wenn die Gruppe "INVOICE LINE PERIOD" (BG-26) verwendet wird, müssen entweder das Element "Invoice line period start date" (BT-134) oder
// das Element "Invoice line period end date" (BT-135) oder beide gefüllt sein.
// Note: Only validates parsed XML where BG-26 element was present (tracked via linePeriodPresent flag).
for i := range inv.InvoiceLines {
if inv.InvoiceLines[i].linePeriodPresent {
if inv.InvoiceLines[i].BillingSpecifiedPeriodStart.IsZero() && inv.InvoiceLines[i].BillingSpecifiedPeriodEnd.IsZero() {
inv.addViolation(rules.BRCO20, fmt.Sprintf("Invoice line %d: if line period (BG-26) is used, either start date (BT-134) or end date (BT-135) must be filled", i+1))
}
}
}
// BR-CO-25 was removed from the EN 16931 CII schematron in v1.3.16
// (anticipating the EN 16931 revision); it is no longer validated.
// BR-CO-26 Verkäufer
// In order for the buyer to automatically identify a supplier, at least one of the following shall be present:
// - Seller identifier (BT-29)
// - Seller legal registration identifier (BT-30)
// - Seller VAT identifier (BT-31)
hasSellerID := len(inv.Seller.ID) > 0 || len(inv.Seller.GlobalID) > 0
hasLegalReg := inv.Seller.SpecifiedLegalOrganization != nil && inv.Seller.SpecifiedLegalOrganization.ID != ""
hasVATID := inv.Seller.VATaxRegistration != ""
if !hasSellerID && !hasLegalReg && !hasVATID {
inv.addViolation(rules.BRCO26, "At least one seller identifier must be present: Seller ID (BT-29), Legal registration (BT-30), or VAT ID (BT-31)")
}
// Note: BR-CO-05, BR-CO-06, BR-CO-07, and BR-CO-08 are not validated here.
// These rules state that reason codes and reason text "shall indicate the same type"
// when both are present. However, implementing this check would require a lookup table
// mapping codes to text that is not part of the EN 16931 specification.
// The required validations are already covered by:
// - BR-33: Allowances must have reason (BT-97) OR code (BT-98)
// - BR-38: Charges must have reason (BT-104) OR code (BT-105)
// - BR-42: Line allowances must have reason (BT-139) OR code (BT-140)
// - BR-44: Line charges must have reason (BT-144) OR code (BT-145)
}
func (inv *Invoice) validateCore() {
// Helper function to check if this invoice allows negative amounts.
// Credit notes (381) and correction invoices (384) may have negative amounts throughout
// as per EN 16931 support for negative grand totals in correction scenarios.
// Additionally, correction invoices with type 380 that reference other invoices
// (via BillingReference/InvoiceReferencedDocument) may also have negative amounts.
allowsNegativeAmounts := func() bool {
if inv.InvoiceTypeCode == 381 || inv.InvoiceTypeCode == 384 {
return true
}
// Allow negative amounts for correction invoices (type 380) with billing references
if inv.InvoiceTypeCode == 380 && len(inv.InvoiceReferencedDocument) > 0 {
return true
}
return false
}
// BR-1
// Eine Rechnung (INVOICE) muss eine Spezifikationskennung "Specification identification" (BT-24) enthalten.
if inv.GuidelineSpecifiedDocumentContextParameter == "" {
inv.addViolation(rules.BR1, "GuidelineSpecifiedDocumentContextParameter (BT-24) is empty")
}
// BR-2 Rechnung
// Eine Rechnung (INVOICE) muss eine Rechnungsnummer "Invoice number" (BT-1) enthalten.
if inv.InvoiceNumber == "" {
inv.addViolation(rules.BR2, "No invoice number found")
}
// BR-3 Rechnung
// Eine Rechnung (INVOICE) muss ein Rechnungsdatum "Invoice issue date" (BT-2) enthalten.
if inv.InvoiceDate.IsZero() {
inv.addViolation(rules.BR3, "Date is zero")
}
// BR-4 Rechnung
// Eine Rechnung (INVOICE) muss einen Rechnungstyp-Code "Invoice type code" (BT-3) enthalten.
if inv.InvoiceTypeCode == 0 {
inv.addViolation(rules.BR4, "Invoice type code is 0")
}
// BR-5 Rechnung
// Eine Rechnung (INVOICE) muss einen Währungs-Code "Invoice currency code" (BT-5) enthalten.
if inv.InvoiceCurrencyCode == "" {
inv.addViolation(rules.BR5, "Invoice currency code is empty")
}
// BR-6 Verkäufer
// Eine Rechnung (INVOICE) muss den Verkäufernamen "Seller name" (BT-27) enthalten.
if inv.Seller.Name == "" {
inv.addViolation(rules.BR6, "Seller name is empty")
}
// BR-7 Käufer
// Eine Rechnung (INVOICE) muss den Erwerbernamen "Buyer name" (BT-44) enthalten.
if inv.Buyer.Name == "" {
inv.addViolation(rules.BR7, "Buyer name is empty")
}
// BR-8 Verkäufer
// Eine Rechnung (INVOICE) muss die postalische Anschrift des Verkäufers "SELLER POSTAL ADDRESS" (BG-5) enthalten.
if inv.Seller.PostalAddress == nil {
inv.addViolation(rules.BR8, "Seller has no postal address")
} else if inv.Seller.PostalAddress.CountryID == "" {
// BR-9 Verkäufer
// Eine postalische Anschrift des Verkäufers "SELLER POSTAL ADDRESS" (BG-5) muss einen Verkäufer-Ländercode "Seller country code" (BT-40) enthalten.
inv.addViolation(rules.BR9, "Seller country code empty")
}
if inv.ProfileLevel() > levelMinimum {
// BR-10 Käufer
// Eine Rechnung (INVOICE) muss die postalische Anschrift des Erwerbers "BUYER POSTAL ADDRESS" (BG-8) enthalten.
if inv.Buyer.PostalAddress == nil {
inv.addViolation(rules.BR10, "Buyer has no postal address")
} else if inv.Buyer.PostalAddress.CountryID == "" {
// BR-11 Käufer
// Eine postalische Anschrift des Erwerbers "BUYER POSTAL ADDRESS" (BG-8) muss einen Erwerber-Ländercode "Buyer country code" (BT-55)
// enthalten.
inv.addViolation(rules.BR11, "Buyer country code empty")
}
}
// BR-12 Gesamtsummen auf Dokumentenebene
// Eine Rechnung (INVOICE) muss die Summe der Rechnungspositionen-Nettobeträge "Sum of Invoice line net amount" (BT-106) enthalten.
// Note: EN 16931 schematron validates element presence in XML, not non-zero value.
// Zero is valid (e.g., credit notes, zero-rated items).
// This check only applies to parsed XML invoices; programmatically built invoices skip this.
// Minimum profile doesn't require LineTotal (no invoice lines), so only check for profiles >= Basic
if inv.isParsed {
if inv.ProfileLevel() >= levelBasic && !inv.hasLineTotalInXML && inv.SchemaType == CII {
inv.addViolation(rules.BR12, "LineTotalAmount element missing in XML")
}
}
// BR-13 Gesamtsummen auf Dokumentenebene
// Eine Rechnung (INVOICE) muss den Gesamtbetrag der Rechnung ohne Umsatzsteuer "Invoice total amount without VAT" (BT-109) enthalten.
// Note: EN 16931 schematron validates element presence in XML, not non-zero value.
// Zero is valid (e.g., zero-rated items).
// This check only applies to parsed XML invoices; programmatically built invoices skip this.
if inv.isParsed {
if !inv.hasTaxBasisTotalInXML && inv.SchemaType == CII {
inv.addViolation(rules.BR13, "TaxBasisTotalAmount element missing in XML")
}
}
// BR-14 Gesamtsummen auf Dokumentenebene
// Eine Rechnung (INVOICE) muss den Gesamtbetrag der Rechnung mit Umsatzsteuer "Invoice total amount with VAT" (BT-112) enthalten.
// Note: EN 16931 schematron validates element presence in XML, not non-zero value.
// Zero is valid (e.g., zero-rated items).
// This check only applies to parsed XML invoices; programmatically built invoices skip this.
if inv.isParsed {
if !inv.hasGrandTotalInXML && inv.SchemaType == CII {
inv.addViolation(rules.BR14, "GrandTotalAmount element missing in XML")
}
}
// BR-15 Gesamtsummen auf Dokumentenebene
// Eine Rechnung (INVOICE) muss den ausstehenden Betrag "Amount due for payment" (BT-115) enthalten.
// Note: EN 16931 schematron validates element presence in XML, not non-zero value.
// Zero is valid for prepaid invoices (TotalPrepaidAmount = GrandTotalAmount).
// This check only applies to parsed XML invoices; programmatically built invoices skip this.
if inv.isParsed {
if !inv.hasDuePayableAmountInXML && inv.SchemaType == CII {
inv.addViolation(rules.BR15, "DuePayableAmount element missing in XML")
}
}
// BR-16 Rechnung
// Eine Rechnung (INVOICE) muss mindestens eine Rechnungsposition "INVOICE LINE" (BG-25) enthalten.
if is(levelBasic, inv) {
if len(inv.InvoiceLines) == 0 {
inv.addViolation(rules.BR16, "Invoice lines must be at least 1")
}
}
// BR-17 Zahlungsempfänger
// Eine Rechnung (INVOICE) muss den Namen des Zahlungsempfängers "Payee name" (BT-59) enthalten, wenn sich der Zahlungsempfänger "PAYEE"
// (BG-10) vom Verkäufer "SELLER" (BG-4) unterscheidet.
if inv.PayeeTradeParty != nil {
if inv.PayeeTradeParty.Name == "" {
inv.addViolation(rules.BR17, "Payee has no name, although different from seller")
}
}
// BR-18 Steuerbevollmächtigter des Verkäufers
// Eine Rechnung (INVOICE) muss den Namen des Steuervertreters des Verkäufers "Seller tax representative name" (BT-62) enthalten, wenn der
// Verkäufer "SELLER" (BG-4) einen Steuervertreter (BG-11) hat.
if trp := inv.SellerTaxRepresentativeTradeParty; trp != nil {
if trp.Name == "" {
inv.addViolation(rules.BR18, "Tax representative has no name, although seller has specified one")
}
// BR-19 Steuerbevollmächtigter des Verkäufers
// Eine Rechnung (INVOICE) muss die postalische Anschrift des Steuervertreters "SELLER TAX REPRESENTATIVE POSTAL ADDRESS" (BG-12) enthalten,
// wenn der Verkäufer "SELLER" (BG-4) einen Steuervertreter hat.
if trp.PostalAddress == nil {
inv.addViolation(rules.BR19, "Tax representative has no postal address")
} else if trp.PostalAddress.CountryID == "" {
// BR-20 Steuerbevollmächtigter des Verkäufers
// Die postalische Anschrift des Steuervertreters des Verkäufers "SELLER TAX REPRESENTATIVE POSTAL ADDRESS" (BG-12) muss einen
// Steuervertreter-Ländercode enthalten, wenn der Verkäufer "SELLER" (BG-4) einen Steuervertreter hat.
inv.addViolation(rules.BR20, "Tax representative postal address missing country code")
}
}
// Sub invoice line aggregation lines (GROUP / INFORMATION) may omit
// quantity (BT-129), unit (BT-130) and net price (BT-146) per BR-FXEXT-*.
// In the EXTENDED profile these base rules are replaced by their BR-FXEXT-*
// counterparts (Factur-X 1.09).
br22, br23, br26 := rules.BR22, rules.BR23, rules.BR26
if inv.IsExtended() {
br22, br23, br26 = rules.BRFXEXT22, rules.BRFXEXT23, rules.BRFXEXT26
}
for i := range inv.InvoiceLines {
// BT-X-8 (EXTENDED): the invoice line subtype, when present, must be a
// known value. isDetailLine treats any unknown value as an aggregation
// line, which would silently drop the line from the totals and the VAT
// breakdown, so a typo or future subtype is flagged here instead.
if rc := inv.InvoiceLines[i].LineStatusReasonCode; rc != "" && rc != "DETAIL" && rc != "GROUP" && rc != "INFORMATION" {
inv.addViolation(rules.BRUSER06, fmt.Sprintf("Invoice line %s has unknown subtype %q (BT-X-8); expected DETAIL, GROUP or INFORMATION", inv.InvoiceLines[i].LineID, rc))
}
isContainer := !inv.InvoiceLines[i].isDetailLine()
// BR-21 Rechnungsposition
// Jede Rechnungsposition "INVOICE LINE" (BG-25) muss eine eindeutige Bezeichnung "Invoice line identifier" (BT-126) haben.
if inv.InvoiceLines[i].LineID == "" {
inv.addViolation(rules.BR21, "Line has no line ID")
}
// BR-22 Rechnungsposition
// Jede Rechnungsposition "INVOICE LINE" (BG-25) muss die Menge der in der betreffenden Position in Rechnung gestellten Waren oder
// Dienstleistungen als Einzelposten "Invoiced quantity" (BT-129) enthalten.
if !isContainer && inv.InvoiceLines[i].BilledQuantity.IsZero() {
inv.addViolation(br22, "Line has no billed quantity")
}
// BR-23 Rechnungsposition
// Jede Rechnungsposition "INVOICE LINE" (BG-25) muss eine Einheit zur Mengenangabe "Invoiced quantity unit of measure code" (BT-130)
// enthalten.
if !isContainer && inv.InvoiceLines[i].BilledQuantityUnit == "" {
inv.addViolation(br23, "Line's billed quantity has no unit")
}
// BR-24 Rechnungsposition
// Jede Rechnungsposition "INVOICE LINE" (BG-25) muss den Nettobetrag der Rechnungsposition "Invoice line net amount" (BT-131) enthalten.
// Note: EN 16931 schematron validates element presence in XML, not non-zero value.
// Zero is valid (e.g., free items, zero-rated services).
// This check only applies to parsed XML invoices; programmatically built invoices skip this.
if inv.isParsed {
if !inv.InvoiceLines[i].hasLineTotalInXML && inv.SchemaType == CII {
inv.addViolation(rules.BR24, "LineTotalAmount element missing in XML for line "+inv.InvoiceLines[i].LineID)
}
}
// BR-25 Artikelinformationen
// Jede Rechnungsposition "INVOICE LINE" (BG-25) muss den Namen des Postens "Item name" (BT-153) enthalten.
if inv.InvoiceLines[i].ItemName == "" {
inv.addViolation(rules.BR25, "Line's item name missing")
}
// BR-26 Detailinformationen zum Preis
// Jede Rechnungsposition "INVOICE LINE" (BG-25) muss den Preis des Postens, ohne Umsatzsteuer, nach Abzug des für diese Rechnungsposition
// geltenden Rabatts "Item net price" (BT-146) beinhalten.
// Note: EN 16931 schematron validates element presence in XML, not non-zero value.
// Zero is valid (e.g., free items, promotional products).
// This check only applies to parsed XML invoices; programmatically built invoices skip this.
if inv.isParsed && !isContainer {
if !inv.InvoiceLines[i].hasNetPriceInXML && inv.SchemaType == CII {
inv.addViolation(br26, "NetPrice ChargeAmount element missing in XML for line "+inv.InvoiceLines[i].LineID)
}
}
// BR-27 Nettopreis des Artikels
// Der Artikel-Nettobetrag "Item net price" (BT-146) darf nicht negativ sein.
if inv.InvoiceLines[i].NetPrice.IsNegative() {
inv.addViolation(rules.BR27, "Net price must not be negative")
}
// BR-28 Detailinformationen zum Preis
// Der Einheitspreis ohne Umsatzsteuer vor Abzug des Postenpreisrabatts einer Rechnungsposition "Item gross price" (BT-148) darf nicht negativ
// sein.
if inv.InvoiceLines[i].GrossPrice.IsNegative() {
inv.addViolation(rules.BR28, "Gross price must not be negative")
}
}
// BR-29 Rechnungszeitraum
// Wenn Start- und Enddatum des Rechnungszeitraums gegeben sind, muss das Enddatum "Invoicing period end date" (BT-74) nach dem Startdatum
// "Invoicing period start date" (BT-73) liegen oder mit diesem identisch sein.
// Only validate when BOTH dates are present (non-zero)
if !inv.BillingSpecifiedPeriodStart.IsZero() && !inv.BillingSpecifiedPeriodEnd.IsZero() {
if inv.BillingSpecifiedPeriodEnd.Before(inv.BillingSpecifiedPeriodStart) {
inv.addViolation(rules.BR29, "Billing period end must be after start")
}
}
for i := range inv.InvoiceLines {
// BR-30 Rechnungszeitraum auf Positionsebene
// Wenn Start- und Enddatum des Rechnungspositionenzeitraums gegeben sind, muss das Enddatum "Invoice line period end date" (BT-135) nach
// dem Startdatum "Invoice line period start date" (BT-134) liegen oder mit diesem identisch sein.
// Only validate when BOTH dates are present (non-zero)
if !inv.InvoiceLines[i].BillingSpecifiedPeriodStart.IsZero() && !inv.InvoiceLines[i].BillingSpecifiedPeriodEnd.IsZero() {
if inv.InvoiceLines[i].BillingSpecifiedPeriodEnd.Before(inv.InvoiceLines[i].BillingSpecifiedPeriodStart) {
inv.addViolation(rules.BR30, "Line item billing period end must be after or identical to start")
}
}
}
// Initialize applicableTradeTaxes map for BR-45 validation
// Use composite key of CategoryCode + Percent to properly group by tax category
applicableTradeTaxes := make(map[string]decimal.Decimal, len(inv.TradeTaxes))
for i := range inv.InvoiceLines {
if !inv.InvoiceLines[i].isDetailLine() {
continue
}
key := inv.InvoiceLines[i].TaxCategoryCode + "_" + inv.InvoiceLines[i].TaxRateApplicablePercent.String()
applicableTradeTaxes[key] = applicableTradeTaxes[key].Add(inv.InvoiceLines[i].Total)
}
for i := range inv.SpecifiedTradeAllowanceCharge {
// BR-66 Specified Trade Allowance Charge
// Each Specified Trade Allowance Charge shall contain a Charge Indicator.
// Note: In Go, the boolean ChargeIndicator field always has a value (true or false),
// so this rule is implicitly satisfied. This validation is kept for documentation
// and to align with the EN 16931 specification.
// Add to applicableTradeTaxes for BR-45 validation
key := inv.SpecifiedTradeAllowanceCharge[i].CategoryTradeTaxCategoryCode + "_" + inv.SpecifiedTradeAllowanceCharge[i].CategoryTradeTaxRateApplicablePercent.String()
amount := inv.SpecifiedTradeAllowanceCharge[i].ActualAmount
if !inv.SpecifiedTradeAllowanceCharge[i].ChargeIndicator {
amount = amount.Neg()
}
applicableTradeTaxes[key] = applicableTradeTaxes[key].Add(amount)
if inv.SpecifiedTradeAllowanceCharge[i].ChargeIndicator {
// BR-36 Zuschläge auf Dokumentenebene
// Jede Abgabe auf Dokumentenebene "DOCUMENT LEVEL CHARGES" (BG-21) muss einen Betrag "Document level charge amount" (BT-99)
// aufweisen.
if inv.SpecifiedTradeAllowanceCharge[i].ActualAmount.IsZero() {
inv.addViolation(rules.BR36, "Charge must not be zero")
}
// BR-37 Zuschläge auf Dokumentenebene
// Jede Abgabe auf Dokumentenebene "DOCUMENT LEVEL CHARGES" (BG-21) muss einen Umsatzsteuer-Code "Document level charge VAT
// category code" (BT-102) aufweisen.
if inv.SpecifiedTradeAllowanceCharge[i].CategoryTradeTaxCategoryCode == "" {
inv.addViolation(rules.BR37, "Charge tax category code not set")
}
// BR-38 Zuschläge auf Dokumentenebene
// Jede Abgabe auf Dokumentenebene "DOCUMENT LEVEL CHARGES" (BG-21) muss einen Abgabegrund "Document level charge reason" (BT-104)
// oder einen entsprechenden Code "Document level charge reason code" (BT-105) aufweisen.
if inv.SpecifiedTradeAllowanceCharge[i].Reason == "" && inv.SpecifiedTradeAllowanceCharge[i].ReasonCode == "" {
inv.addViolation(rules.BR38, "Charge reason empty or code unset")
}
// BR-USER-03 Zuschläge auf Dokumentenebene
// Der Betrag einer Abgabe auf Dokumentenebene "Document level charge amount" (BT-99) darf nicht negativ sein.
// Note: Credit notes (381) and correction invoices (384) may have negative amounts as per EN 16931.
if !allowsNegativeAmounts() && inv.SpecifiedTradeAllowanceCharge[i].ActualAmount.LessThan(decimal.Zero) {
inv.addViolation(rules.BRUSER03, "Document level charge amount must not be negative")
}
// BR-USER-04 Zuschläge auf Dokumentenebene
// Der Basisbetrag einer Abgabe auf Dokumentenebene "Document level charge base amount" (BT-100) darf nicht negativ sein.
// Note: Credit notes (381) and correction invoices (384) may have negative amounts as per EN 16931.
if !allowsNegativeAmounts() && inv.SpecifiedTradeAllowanceCharge[i].BasisAmount.LessThan(decimal.Zero) {
inv.addViolation(rules.BRUSER04, "Document level charge base amount must not be negative")
}
} else {
// BR-31 Abschläge auf Dokumentenebene
// Jeder Nachlass für die Rechnung als Ganzes "DOCUMENT LEVEL ALLOWANCES" (BG-20) muss einen Betrag "Document level allowance amount"
// (BT-92) aufweisen.
if inv.SpecifiedTradeAllowanceCharge[i].ActualAmount.IsZero() {
inv.addViolation(rules.BR31, "Allowance must not be zero")
}
// BR-32 Abschläge auf Dokumentenebene
// Jeder Nachlass für die Rechnung als Ganzes "DOCUMENT LEVEL ALLOWANCES" (BG-20) muss einen Umsatzsteuer-Code "Document level
// allowance VAT category code" (BT-95) aufweisen.
if inv.SpecifiedTradeAllowanceCharge[i].CategoryTradeTaxCategoryCode == "" {
inv.addViolation(rules.BR32, "Allowance tax category code not set")
}
// BR-33 Abschläge auf Dokumentenebene
// Jeder Nachlass für die Rechnung als Ganzes "DOCUMENT LEVEL ALLOWANCES" (BG-20) muss einen Nachlassgrund "Document level allowance
// reason" (BT-97) oder einen entsprechenden Code "Document level allowance reason code" (BT-98") aufweisen.
if inv.SpecifiedTradeAllowanceCharge[i].Reason == "" && inv.SpecifiedTradeAllowanceCharge[i].ReasonCode == "" {
inv.addViolation(rules.BR33, "Allowance reason empty or code unset")
}
// BR-USER-01 Abschläge auf Dokumentenebene
// Der Betrag eines Nachlasses auf Dokumentenebene "Document level allowance amount" (BT-92) darf nicht negativ sein.
// Note: Credit notes (381) and correction invoices (384) may have negative amounts as per EN 16931.
if !allowsNegativeAmounts() && inv.SpecifiedTradeAllowanceCharge[i].ActualAmount.LessThan(decimal.Zero) {
inv.addViolation(rules.BRUSER01, "Document level allowance amount must not be negative")
}
// BR-USER-02 Abschläge auf Dokumentenebene
// Der Basisbetrag eines Nachlasses auf Dokumentenebene "Document level allowance base amount" (BT-93) darf nicht negativ sein.
// Note: Credit notes (381) and correction invoices (384) may have negative amounts as per EN 16931.
if !allowsNegativeAmounts() && inv.SpecifiedTradeAllowanceCharge[i].BasisAmount.LessThan(decimal.Zero) {
inv.addViolation(rules.BRUSER02, "Document level allowance base amount must not be negative")
}
}
}
for i := range inv.InvoiceLines {
// BR-41 Abschläge auf Ebene der Rechnungsposition
// Jeder Nachlass auf der Ebene der Rechnungsposition "INVOICE LINE ALLOWANCES" (BG-27) muss einen Betrag "Invoice line allowance amount"
// (BT-136) aufweisen.
for j := range inv.InvoiceLines[i].InvoiceLineAllowances {
if inv.InvoiceLines[i].InvoiceLineAllowances[j].ActualAmount.IsZero() {
inv.addViolation(rules.BR41, "Line allowance amount zero")
}
// BR-42 Abschläge auf Ebene der Rechnungsposition
// Jeder Nachlass auf der Ebene der Rechnungsposition "INVOICE LINE ALLOWANCES" (BG-27) muss einen Nachlassgrund "Invoice line allowance
// reason" (BT-139) oder einen entsprechenden Code "Invoice line allowance reason code" (BT-140) aufweisen.
if inv.InvoiceLines[i].InvoiceLineAllowances[j].Reason == "" && inv.InvoiceLines[i].InvoiceLineAllowances[j].ReasonCode == "" {
inv.addViolation(rules.BR42, "Line allowance must have a reason")
}
}
for j := range inv.InvoiceLines[i].InvoiceLineCharges {
// BR-43 Charge ou frais sur ligne de facture
// Jede Abgabe auf der Ebene der Rechnungsposition "INVOICE LINE CHARGES" (BG-28) muss einen Betrag "Invoice line charge amount" (BT-141)
// aufweisen.
if inv.InvoiceLines[i].InvoiceLineCharges[j].ActualAmount.IsZero() {
inv.addViolation(rules.BR43, "Line charge amount zero")
}
// BR-44 Charge ou frais sur ligne de facture
// Jede Abgabe auf der Ebene der Rechnungsposition "INVOICE LINE CHARGES" (BG-28) muss einen Abgabegrund "Invoice line charge reason" (BT-
// 144) oder einen entsprechenden Code "Invoice line charge reason code" (BT-145) aufweisen.
if inv.InvoiceLines[i].InvoiceLineCharges[j].Reason == "" && inv.InvoiceLines[i].InvoiceLineCharges[j].ReasonCode == "" {
inv.addViolation(rules.BR44, "Line charge must have a reason")
}
}
}
for i := range inv.TradeTaxes {
// BR-46 Umsatzsteueraufschlüsselung
// Jede Umsatzsteueraufschlüsselung "VAT BREAKDOWN" (BG-23) muss den für
// die betreffende Umsatzsteuerkategorie zu entrichtenden Gesamtbetrag
// "VAT category tax amount" (BT-117) aufweisen.
// Note: Zero is a valid value for exempt categories (E, AE, Z, G, O, IC, IG, IP).
// Category-specific rules (BR-E-9, BR-AE-9, BR-Z-9, etc.) enforce when zero is required.
// This rule only ensures the field is present, which it always is after parsing or calculation.
// BR-48 Umsatzsteueraufschlüsselung
// Jede Umsatzsteueraufschlüsselung "VAT BREAKDOWN" (BG-23) muss einen
// Umsatzsteuersatz gemäß einer Kategorie "VAT category rate" (BT-119)
// haben. Sofern die Rechnung von der Umsatzsteuer ausgenommen ist, ist
// "0" zu übermitteln.
// Note: Zero is a valid and required value for categories E, AE, Z, G, O, IC, IG, IP.
// Category-specific rules (BR-S-5, BR-E-5, BR-AE-5, etc.) enforce the correct rate per category.
// This rule only ensures the field is present, which it always is after parsing or calculation.
// BR-45 Umsatzsteueraufschlüsselung
// Jede Umsatzsteueraufschlüsselung "VAT BREAKDOWN" (BG-23) muss die
// Summe aller nach dem jeweiligen Schlüssel zu versteuernden Beträge
// "VAT category taxable amount" (BT-116) aufweisen.
// Note: This validation only applies to profiles with line items (>= Basic, level 3).
// BasicWL profile (level 2) provides BasisAmount directly without line items.
if is(levelBasic, inv) {
key := inv.TradeTaxes[i].CategoryCode + "_" + inv.TradeTaxes[i].Percent.String()
if !applicableTradeTaxes[key].Equal(inv.TradeTaxes[i].BasisAmount) {
inv.addViolation(rules.BR45, "Applicable trade tax basis amount not equal to the sum of line total")
}
}
// BR-47 Umsatzsteueraufschlüsselung
// Jede Umsatzsteueraufschlüsselung "VAT BREAKDOWN" (BG-23) muss über
// eine codierte Bezeichnung einer Umsatzsteuerkategorie "VAT category
// code" (BT-118) definiert werden.
if inv.TradeTaxes[i].CategoryCode == "" {
inv.addViolation(rules.BR47, "CategoryCode not set for applicable trade tax")
}
}
for i := range inv.PaymentMeans {
// BR-49 Zahlungsanweisungen
// Die Zahlungsinstruktionen "PAYMENT INSTRUCTIONS" (BG-16) müssen den Zahlungsart-Code "Payment means type code" (BT-81) enthalten.
if inv.PaymentMeans[i].TypeCode == 0 {
inv.addViolation(rules.BR49, "Payment means type code must be set")
}
}
// BR-50 Kontoinformationen
// Die Kennung des Kontos, auf das die Zahlung erfolgen soll "Payment
// account identifier" (BT-84), muss angegeben werden, wenn
// Überweisungsinformationen in der Rechnung angegeben werden.
// Note: This is a weaker version of BR-61 and is generally covered by BR-61
// BR-51 Karteninformationen
// Die letzten vier bis sechs Ziffern der Kreditkartennummer "Payment card
// primary account number" (BT-87) sollen angegeben werden, wenn
// Informationen zur Kartenzahlung übermittelt werden.
// Note: This uses "sollen" (should) not "muss" (must), so it's a recommendation not requirement
// BR-52 Rechnungsbegründende Unterlagen
// Jede rechnungsbegründende Unterlage muss einen Dokumentenbezeichner
// "Supporting document reference" (BT-122) haben.
for i := range inv.AdditionalReferencedDocument {
if inv.AdditionalReferencedDocument[i].IssuerAssignedID == "" {
inv.addViolation(rules.BR52, "Supporting document must have a reference")
}
}
// BR-53 Gesamtsummen auf Dokumentenebene
// Wenn eine Währung für die Umsatzsteuerabrechnung angegeben wurde, muss
// der Umsatzsteuergesamtbetrag in der Abrechnungswährung "Invoice total VAT
// amount in accounting currency" (BT-111) angegeben werden.
if inv.TaxCurrencyCode != "" && inv.TaxTotalAccounting.IsZero() {
inv.addViolation(rules.BR53, "Tax total in accounting currency must be specified when tax currency code is provided")
}
// Validate TaxTotalAmount currency consistency
// EN 16931 specifies only BT-110 (invoice currency) and BT-111 (accounting currency) are allowed
for _, unexpectedCurrency := range inv.unexpectedTaxCurrencies {
expectedCurrencies := inv.InvoiceCurrencyCode
if inv.TaxCurrencyCode != "" {
expectedCurrencies += " or " + inv.TaxCurrencyCode
}
inv.addViolation(rules.UNEXPECTED_TAX_CURRENCY,
fmt.Sprintf("TaxTotalAmount with unexpected currency %s (expected %s)", unexpectedCurrency, expectedCurrencies))
}
// BR-54 Artikelattribute
// Jede Eigenschaft eines in Rechnung gestellten Postens "ITEM ATTRIBUTES"
// (BG-32) muss eine Bezeichnung "Item attribute name" (BT-160) und einen
// Wert "Item attribute value" (BT-161) haben.
for i := range inv.InvoiceLines {
for j := range inv.InvoiceLines[i].Characteristics {
if inv.InvoiceLines[i].Characteristics[j].Description == "" || inv.InvoiceLines[i].Characteristics[j].Value == "" {
inv.addViolation(rules.BR54, "Item attribute must have both name and value")
}
}
}
// BR-55 Referenz auf die vorausgegangene Rechnung
// Jede Bezugnahme auf eine vorausgegangene Rechnung "Preceding Invoice
// reference" (BT-25) muss die Nummer der vorausgegangenen Rechnung
// enthalten.
for _, ref := range inv.InvoiceReferencedDocument {
if ref.ID == "" {
inv.addViolation(rules.BR55, "Preceding invoice reference must contain invoice number")
}
}
// BR-56 Steuerbevollmächtigter des Verkäufers
// Jeder Steuervertreter des Verkäufers "SELLER TAX REPRESENTATIVE PARTY"
// (BG-11) muss eine Umsatzsteuer-Identifikationsnummer "Seller tax
// representative VAT identifier" (BT-63) haben.
if inv.SellerTaxRepresentativeTradeParty != nil && inv.SellerTaxRepresentativeTradeParty.VATaxRegistration == "" {
inv.addViolation(rules.BR56, "Seller tax representative must have VAT identifier")
}
// BR-57 Lieferanschrift
// Jede Lieferadresse "DELIVER TO ADDRESS" (BG-15) muss einen entsprechenden
// Ländercode "Deliver to country code" (BT-80) enthalten.
if inv.ShipTo != nil && inv.ShipTo.PostalAddress != nil && inv.ShipTo.PostalAddress.CountryID == "" {
inv.addViolation(rules.BR57, "Deliver-to address must have country code")
}
// BR-61: Payment account identifier (BT-84) required for credit transfers (codes 30, 58).
// Note: Validates element presence per EN 16931 schematron, not value. Empty elements
// like <ram:IBANID/> are valid. Only triggers when PayeePartyCreditorFinancialAccount
// exists but neither IBANID nor ProprietaryID elements are present.
if inv.isParsed {
for i := range inv.PaymentMeans {
if (inv.PaymentMeans[i].TypeCode == 30 || inv.PaymentMeans[i].TypeCode == 58) && inv.PaymentMeans[i].hasPayeeAccountInXML {
if !inv.PaymentMeans[i].hasPayeeIBANInXML && !inv.PaymentMeans[i].hasPayeeProprietaryIDInXML {
inv.addViolation(rules.BR61, "Payment account identifier required for credit transfer payment types")
}
}
}
}
// BR-62 Elektronische Adresse des Verkäufers
// Im Element "Seller electronic address" (BT-34) muss die Komponente
// "Scheme Identifier" vorhanden sein.
if inv.Seller.URIUniversalCommunication != "" && inv.Seller.URIUniversalCommunicationScheme == "" {
inv.addViolation(rules.BR62, "Seller electronic address must have scheme identifier")
}
// BR-63 Elektronische Adresse des Käufers
// Im Element "Buyer electronic address" (BT-49) muss die Komponente "Scheme
// Identifier" vorhanden sein.
if inv.Buyer.URIUniversalCommunication != "" && inv.Buyer.URIUniversalCommunicationScheme == "" {
inv.addViolation(rules.BR63, "Buyer electronic address must have scheme identifier")
}
// BR-64 Kennung eines Artikels nach registriertem Schema
// Im Element "Item standard identifier" (BT-157) muss die Komponente
// "Scheme Identifier" vorhanden sein.
for i := range inv.InvoiceLines {
if inv.InvoiceLines[i].GlobalID != "" && inv.InvoiceLines[i].GlobalIDType == "" {
inv.addViolation(rules.BR64, "Item standard identifier must have scheme identifier")
}
}
// BR-65 Kennung der Artikelklassifizierung
// Im Element "Item classification identifier" (BT-158) muss die Komponente
// "Scheme Identifier" vorhanden sein.
for i := range inv.InvoiceLines {
for j := range inv.InvoiceLines[i].ProductClassification {
if inv.InvoiceLines[i].ProductClassification[j].ClassCode != "" && inv.InvoiceLines[i].ProductClassification[j].ListID == "" {
inv.addViolation(rules.BR65, "Item classification identifier must have scheme identifier")
}
}
}
// BR-B-1 Split payment (Italian domestic invoices)
// An Invoice where the VAT category code is "Split payment" (B) shall be a domestic Italian invoice.
// This means both seller and buyer must be located in Italy (IT).
hasSplitPayment := false
for i := range inv.InvoiceLines {
if inv.InvoiceLines[i].TaxCategoryCode == "B" {
hasSplitPayment = true
break
}
}
if !hasSplitPayment {
for i := range inv.SpecifiedTradeAllowanceCharge {
if inv.SpecifiedTradeAllowanceCharge[i].CategoryTradeTaxCategoryCode == "B" {
hasSplitPayment = true
break
}
}
}
if hasSplitPayment {
// Check seller country
sellerCountry := ""
if inv.Seller.PostalAddress != nil {
sellerCountry = inv.Seller.PostalAddress.CountryID
}
// Check buyer country
buyerCountry := ""
if inv.Buyer.PostalAddress != nil {
buyerCountry = inv.Buyer.PostalAddress.CountryID
}
if sellerCountry != "IT" || buyerCountry != "IT" {
inv.addViolation(rules.BRB1, "Split payment VAT category (B) requires both seller and buyer to be in Italy (IT)")
}
}
// BR-B-2 Split payment and Standard rated exclusion
// An Invoice with Split payment (B) shall not contain Standard rated (S) VAT category.
hasStandardRated := false
for i := range inv.InvoiceLines {
if inv.InvoiceLines[i].TaxCategoryCode == "S" {
hasStandardRated = true
break
}
}
if !hasStandardRated {
for i := range inv.SpecifiedTradeAllowanceCharge {
if inv.SpecifiedTradeAllowanceCharge[i].CategoryTradeTaxCategoryCode == "S" {
hasStandardRated = true
break
}
}
}
if hasSplitPayment && hasStandardRated {
inv.addViolation(rules.BRB2, "Invoice with Split payment VAT category (B) must not contain Standard rated (S) category")
}
// VAT category validations - delegated to specialized methods
inv.validateVATStandard()
inv.validateVATReverse()
inv.validateVATExempt()
inv.validateVATZero()
inv.validateVATExport()
inv.validateVATIntracommunity()
inv.validateVATIGIC()
inv.validateVATIPSI()
inv.validateVATNotSubject()
}
// hasMaxDecimals checks if a decimal value has at most maxDecimals decimal places.
// Values that equal their rounded form are considered valid (trailing zeros are ignored).
func hasMaxDecimals(value decimal.Decimal, maxDecimals int) bool {
rounded := value.Round(int32(maxDecimals))
return value.Equal(rounded)
}
func (inv *Invoice) validateDecimals() {
// Helper function to validate decimal precision
checkDecimalPrecision := func(value decimal.Decimal, fieldName string, btCode string, rule rules.Rule) {
if !value.IsZero() && !hasMaxDecimals(value, 2) {
inv.addViolation(rule, fmt.Sprintf("%s (%s) has more than 2 decimal places: %s", fieldName, btCode, value.String()))
}
}
// BR-DEC-01: Document level allowance amount (BT-92)
// BR-DEC-02: Document level allowance base amount (BT-93)
// BR-DEC-05: Document level charge amount (BT-99)
// BR-DEC-06: Document level charge base amount (BT-100)
for i := range inv.SpecifiedTradeAllowanceCharge {
if !inv.SpecifiedTradeAllowanceCharge[i].ChargeIndicator {
// Allowance
checkDecimalPrecision(inv.SpecifiedTradeAllowanceCharge[i].ActualAmount, "Document level allowance amount", "BT-92", rules.BRDEC1)
checkDecimalPrecision(inv.SpecifiedTradeAllowanceCharge[i].BasisAmount, "Document level allowance base amount", "BT-93", rules.BRDEC2)
} else {
// Charge
checkDecimalPrecision(inv.SpecifiedTradeAllowanceCharge[i].ActualAmount, "Document level charge amount", "BT-99", rules.BRDEC5)
checkDecimalPrecision(inv.SpecifiedTradeAllowanceCharge[i].BasisAmount, "Document level charge base amount", "BT-100", rules.BRDEC6)
}
}
// BR-DEC-09: Sum of Invoice line net amount (BT-106)
checkDecimalPrecision(inv.LineTotal, "Sum of Invoice line net amount", "BT-106", rules.BRDEC9)
// BR-DEC-10: Sum of allowances on document level (BT-107)
checkDecimalPrecision(inv.AllowanceTotal, "Sum of allowances on document level", "BT-107", rules.BRDEC10)
// BR-DEC-11: Sum of charges on document level (BT-108)
checkDecimalPrecision(inv.ChargeTotal, "Sum of charges on document level", "BT-108", rules.BRDEC11)
// BR-DEC-12: Invoice total amount without VAT (BT-109)
checkDecimalPrecision(inv.TaxBasisTotal, "Invoice total amount without VAT", "BT-109", rules.BRDEC12)
// BR-DEC-13: Invoice total VAT amount (BT-110)
checkDecimalPrecision(inv.TaxTotal, "Invoice total VAT amount", "BT-110", rules.BRDEC13)
// BR-DEC-14: Invoice total amount with VAT (BT-112)
checkDecimalPrecision(inv.GrandTotal, "Invoice total amount with VAT", "BT-112", rules.BRDEC14)
// BR-DEC-15: Invoice total VAT amount in accounting currency (BT-111)
checkDecimalPrecision(inv.TaxTotalAccounting, "Invoice total VAT amount in accounting currency", "BT-111", rules.BRDEC15)
// BR-DEC-16: Paid amount (BT-113)
checkDecimalPrecision(inv.TotalPrepaid, "Paid amount", "BT-113", rules.BRDEC16)
// BR-DEC-17: Rounding amount (BT-114)
checkDecimalPrecision(inv.RoundingAmount, "Rounding amount", "BT-114", rules.BRDEC17)
// BR-DEC-18: Amount due for payment (BT-115)
checkDecimalPrecision(inv.DuePayableAmount, "Amount due for payment", "BT-115", rules.BRDEC18)
// BR-DEC-19: VAT category taxable amount (BT-116)
// BR-DEC-20: VAT category tax amount (BT-117)
for i := range inv.TradeTaxes {
checkDecimalPrecision(inv.TradeTaxes[i].BasisAmount, "VAT category taxable amount", "BT-116", rules.BRDEC19)
checkDecimalPrecision(inv.TradeTaxes[i].CalculatedAmount, "VAT category tax amount", "BT-117", rules.BRDEC20)
}
// BR-DEC-23: Invoice line net amount (BT-131)
// BR-DEC-24: Invoice line allowance amount (BT-136)
// BR-DEC-25: Invoice line allowance base amount (BT-137)
// BR-DEC-27: Invoice line charge amount (BT-141)
// BR-DEC-28: Invoice line charge base amount (BT-142)
for i := range inv.InvoiceLines {
linePrefix := fmt.Sprintf("Line %d: ", i+1)
checkDecimalPrecision(inv.InvoiceLines[i].Total, linePrefix+"Invoice line net amount", "BT-131", rules.BRDEC23)
for j := range inv.InvoiceLines[i].InvoiceLineAllowances {
checkDecimalPrecision(inv.InvoiceLines[i].InvoiceLineAllowances[j].ActualAmount, linePrefix+"Invoice line allowance amount", "BT-136", rules.BRDEC24)
checkDecimalPrecision(inv.InvoiceLines[i].InvoiceLineAllowances[j].BasisAmount, linePrefix+"Invoice line allowance base amount", "BT-137", rules.BRDEC25)
}
for j := range inv.InvoiceLines[i].InvoiceLineCharges {
checkDecimalPrecision(inv.InvoiceLines[i].InvoiceLineCharges[j].ActualAmount, linePrefix+"Invoice line charge amount", "BT-141", rules.BRDEC27)
checkDecimalPrecision(inv.InvoiceLines[i].InvoiceLineCharges[j].BasisAmount, linePrefix+"Invoice line charge base amount", "BT-142", rules.BRDEC28)
}
}
}