-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidate_core_test.go
More file actions
2115 lines (1959 loc) · 60.5 KB
/
Copy pathvalidate_core_test.go
File metadata and controls
2115 lines (1959 loc) · 60.5 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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package einvoice
import (
"strings"
"testing"
"time"
"github.com/shopspring/decimal"
)
// TestBR11_BuyerCountryCodeField tests that BR-11 references the correct field BT-55
func TestBR11_BuyerCountryCodeField(t *testing.T) {
inv := Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecFacturXBasic,
InvoiceNumber: "TEST-001",
InvoiceTypeCode: 380,
InvoiceDate: time.Now(),
InvoiceCurrencyCode: "EUR",
LineTotal: decimal.NewFromInt(100),
TaxBasisTotal: decimal.NewFromInt(100),
GrandTotal: decimal.NewFromInt(119),
DuePayableAmount: decimal.NewFromInt(119),
Seller: Party{
Name: "Seller",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
Buyer: Party{
Name: "Buyer",
PostalAddress: &PostalAddress{
// Missing CountryID
},
},
InvoiceLines: []InvoiceLine{
{
LineID: "1",
ItemName: "Item",
BilledQuantity: decimal.NewFromInt(1),
NetPrice: decimal.NewFromInt(100),
Total: decimal.NewFromInt(100),
TaxCategoryCode: "S",
},
},
TradeTaxes: []TradeTax{
{
CategoryCode: "S",
Percent: decimal.NewFromInt(19),
BasisAmount: decimal.NewFromInt(100),
CalculatedAmount: decimal.NewFromInt(19),
},
},
}
_ = inv.Validate()
// Find BR-11 violation
var br11Found bool
for _, v := range inv.violations {
if v.Rule.Code == "BR-11" {
br11Found = true
// Check that it references BT-55, not BT-5
if len(v.Rule.Fields) == 0 {
t.Error("BR-11 violation should have InvFields")
}
if v.Rule.Fields[0] != "BT-55" {
t.Errorf("BR-11 should reference BT-55 (Buyer country code), got %s", v.Rule.Fields[0])
}
}
}
if !br11Found {
t.Error("Expected BR-11 violation for missing buyer country code")
}
}
// TestBR37_ChargeRuleNumber tests that charge tax category validation uses BR-37, not BR-32
func TestBR37_ChargeRuleNumber(t *testing.T) {
inv := Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecFacturXBasic,
InvoiceNumber: "TEST-002",
InvoiceTypeCode: 380,
InvoiceDate: time.Now(),
InvoiceCurrencyCode: "EUR",
LineTotal: decimal.NewFromInt(100),
TaxBasisTotal: decimal.NewFromInt(110),
GrandTotal: decimal.NewFromInt(130),
DuePayableAmount: decimal.NewFromInt(130),
Seller: Party{
Name: "Seller",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
Buyer: Party{
Name: "Buyer",
PostalAddress: &PostalAddress{
CountryID: "FR",
},
},
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: true,
ActualAmount: decimal.NewFromInt(10),
// Missing CategoryTradeTaxCategoryCode
},
},
InvoiceLines: []InvoiceLine{
{
LineID: "1",
ItemName: "Item",
BilledQuantity: decimal.NewFromInt(1),
NetPrice: decimal.NewFromInt(100),
Total: decimal.NewFromInt(100),
TaxCategoryCode: "S",
},
},
TradeTaxes: []TradeTax{
{
CategoryCode: "S",
Percent: decimal.NewFromInt(19),
BasisAmount: decimal.NewFromInt(110),
CalculatedAmount: decimal.NewFromInt(20),
},
},
}
_ = inv.Validate()
// Find BR-37 violation (not BR-32)
var br37Found bool
var br32Found bool
for _, v := range inv.violations {
if v.Rule.Code == "BR-37" {
br37Found = true
}
if v.Rule.Code == "BR-32" {
br32Found = true
}
}
if !br37Found {
t.Error("Expected BR-37 violation for missing charge tax category code")
}
if br32Found {
t.Error("Should use BR-37 for charges, not BR-32 (which is for allowances)")
}
}
// TestBRCO3_TaxPointDateMutuallyExclusive tests BR-CO-3: TaxPointDate and DueDateTypeCode are mutually exclusive
func TestBRCO3_TaxPointDateMutuallyExclusive(t *testing.T) {
inv := Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecFacturXBasic,
InvoiceNumber: "TEST-003",
InvoiceTypeCode: 380,
InvoiceDate: time.Now(),
InvoiceCurrencyCode: "EUR",
LineTotal: decimal.NewFromInt(100),
TaxBasisTotal: decimal.NewFromInt(100),
GrandTotal: decimal.NewFromInt(119),
DuePayableAmount: decimal.NewFromInt(119),
Seller: Party{
Name: "Seller",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
Buyer: Party{
Name: "Buyer",
PostalAddress: &PostalAddress{
CountryID: "FR",
},
},
InvoiceLines: []InvoiceLine{
{
LineID: "1",
ItemName: "Item",
BilledQuantity: decimal.NewFromInt(1),
NetPrice: decimal.NewFromInt(100),
Total: decimal.NewFromInt(100),
TaxCategoryCode: "S",
},
},
TradeTaxes: []TradeTax{
{
CategoryCode: "S",
Percent: decimal.NewFromInt(19),
BasisAmount: decimal.NewFromInt(100),
CalculatedAmount: decimal.NewFromInt(19),
TaxPointDate: time.Now(), // BT-7
DueDateTypeCode: "5", // BT-8 - mutually exclusive!
},
},
}
_ = inv.Validate()
// Find BR-CO-3 violation
var brco3Found bool
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-03" {
brco3Found = true
}
}
if !brco3Found {
t.Error("Expected BR-CO-3 violation when both TaxPointDate and DueDateTypeCode are set")
}
}
// TestBRCO4_InvoiceLineMustHaveVATCategory tests BR-CO-4: Each invoice line must have a VAT category code
func TestBRCO4_InvoiceLineMustHaveVATCategory(t *testing.T) {
inv := Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecFacturXBasic,
InvoiceNumber: "TEST-004",
InvoiceTypeCode: 380,
InvoiceDate: time.Now(),
InvoiceCurrencyCode: "EUR",
LineTotal: decimal.NewFromInt(100),
TaxBasisTotal: decimal.NewFromInt(100),
GrandTotal: decimal.NewFromInt(119),
DuePayableAmount: decimal.NewFromInt(119),
Seller: Party{
Name: "Seller",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
Buyer: Party{
Name: "Buyer",
PostalAddress: &PostalAddress{
CountryID: "FR",
},
},
InvoiceLines: []InvoiceLine{
{
LineID: "1",
ItemName: "Item",
BilledQuantity: decimal.NewFromInt(1),
NetPrice: decimal.NewFromInt(100),
Total: decimal.NewFromInt(100),
// Missing TaxCategoryCode
},
},
TradeTaxes: []TradeTax{
{
CategoryCode: "S",
Percent: decimal.NewFromInt(19),
BasisAmount: decimal.NewFromInt(100),
CalculatedAmount: decimal.NewFromInt(19),
},
},
}
_ = inv.Validate()
// Find BR-CO-4 violation
var brco4Found bool
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-04" {
brco4Found = true
}
}
if !brco4Found {
t.Error("Expected BR-CO-4 violation when invoice line missing VAT category code")
}
}
// TestBRCO17_VATCalculation tests BR-CO-17: VAT amount must equal basis × rate ÷ 100
func TestBRCO17_VATCalculation(t *testing.T) {
inv := Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecFacturXBasic,
InvoiceNumber: "TEST-005",
InvoiceTypeCode: 380,
InvoiceDate: time.Now(),
InvoiceCurrencyCode: "EUR",
LineTotal: decimal.NewFromInt(100),
TaxBasisTotal: decimal.NewFromInt(100),
GrandTotal: decimal.NewFromInt(120),
DuePayableAmount: decimal.NewFromInt(120),
Seller: Party{
Name: "Seller",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
Buyer: Party{
Name: "Buyer",
PostalAddress: &PostalAddress{
CountryID: "FR",
},
},
InvoiceLines: []InvoiceLine{
{
LineID: "1",
ItemName: "Item",
BilledQuantity: decimal.NewFromInt(1),
NetPrice: decimal.NewFromInt(100),
Total: decimal.NewFromInt(100),
TaxCategoryCode: "S",
},
},
TradeTaxes: []TradeTax{
{
CategoryCode: "S",
Percent: decimal.NewFromInt(19),
BasisAmount: decimal.NewFromInt(100),
CalculatedAmount: decimal.NewFromInt(20), // Wrong! Should be 19.00
},
},
}
_ = inv.Validate()
// Find BR-CO-17 violation
var brco17Found bool
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-17" {
brco17Found = true
}
}
if !brco17Found {
t.Error("Expected BR-CO-17 violation when VAT calculation is incorrect")
}
}
// TestBRCO18_AtLeastOneVATBreakdown tests BR-CO-18: Invoice should contain at least one VAT breakdown
func TestBRCO18_AtLeastOneVATBreakdown(t *testing.T) {
inv := Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecFacturXBasic,
InvoiceNumber: "TEST-006",
InvoiceTypeCode: 380,
InvoiceDate: time.Now(),
InvoiceCurrencyCode: "EUR",
LineTotal: decimal.NewFromInt(100),
TaxBasisTotal: decimal.NewFromInt(100),
GrandTotal: decimal.NewFromInt(100),
DuePayableAmount: decimal.NewFromInt(100),
Seller: Party{
Name: "Seller",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
Buyer: Party{
Name: "Buyer",
PostalAddress: &PostalAddress{
CountryID: "FR",
},
},
InvoiceLines: []InvoiceLine{
{
LineID: "1",
ItemName: "Item",
BilledQuantity: decimal.NewFromInt(1),
NetPrice: decimal.NewFromInt(100),
Total: decimal.NewFromInt(100),
TaxCategoryCode: "S",
},
},
TradeTaxes: []TradeTax{
// Missing VAT breakdown!
},
}
_ = inv.Validate()
// Find BR-CO-18 violation
var brco18Found bool
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-18" {
brco18Found = true
}
}
if !brco18Found {
t.Error("Expected BR-CO-18 violation when no VAT breakdown present")
}
}
// TestBRCO19_InvoicingPeriodRequiresDate tests BR-CO-19: Invoicing period requires start or end date
// This validation only applies to parsed XML where BG-14 element is present but has no dates.
func TestBRCO19_InvoicingPeriodRequiresDate(t *testing.T) {
// XML with BG-14 present but no dates inside
xml := `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>TEST-BRCO19</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime><udt:DateTimeString format="102">20240101</udt:DateTimeString></ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:SellerTradeParty>
<ram:Name>Seller</ram:Name>
<ram:PostalTradeAddress><ram:CountryID>DE</ram:CountryID></ram:PostalTradeAddress>
</ram:SellerTradeParty>
<ram:BuyerTradeParty>
<ram:Name>Buyer</ram:Name>
<ram:PostalTradeAddress><ram:CountryID>FR</ram:CountryID></ram:PostalTradeAddress>
</ram:BuyerTradeParty>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:BillingSpecifiedPeriod>
<!-- Element exists but has no StartDateTime or EndDateTime children -->
</ram:BillingSpecifiedPeriod>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
<ram:TaxBasisTotalAmount>100.00</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount currencyID="EUR">19.00</ram:TaxTotalAmount>
<ram:GrandTotalAmount>119.00</ram:GrandTotalAmount>
<ram:DuePayableAmount>119.00</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>`
inv, err := ParseReader(strings.NewReader(xml))
if err != nil {
t.Fatalf("Failed to parse XML: %v", err)
}
// Verify that hasBillingPeriodInXML flag was set
if !inv.hasBillingPeriodInXML {
t.Error("hasBillingPeriodInXML should be true when BG-14 element exists in XML")
}
// Verify both dates are zero
if !inv.BillingSpecifiedPeriodStart.IsZero() || !inv.BillingSpecifiedPeriodEnd.IsZero() {
t.Error("Both BillingSpecifiedPeriod dates should be zero")
}
// Run validation
_ = inv.Validate()
// Should find BR-CO-19 violation
found := false
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-19" {
found = true
break
}
}
if !found {
t.Error("Expected BR-CO-19 violation when BG-14 exists but has no dates")
}
}
// TestBRCO20_InvoiceLinePeriodRequiresDate tests BR-CO-20: Invoice line period requires start or end date
// This validation only applies to parsed XML where BG-26 element is present but has no dates.
func TestBRCO20_InvoiceLinePeriodRequiresDate(t *testing.T) {
xml := `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017#compliant#urn:factur-x.eu:1p0:basic</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>TEST-BRCO20</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime><udt:DateTimeString format="102">20240101</udt:DateTimeString></ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:IncludedSupplyChainTradeLineItem>
<ram:AssociatedDocumentLineDocument>
<ram:LineID>1</ram:LineID>
</ram:AssociatedDocumentLineDocument>
<ram:SpecifiedTradeProduct>
<ram:Name>Test Item</ram:Name>
</ram:SpecifiedTradeProduct>
<ram:SpecifiedLineTradeAgreement>
<ram:NetPriceProductTradePrice>
<ram:ChargeAmount>100.00</ram:ChargeAmount>
</ram:NetPriceProductTradePrice>
</ram:SpecifiedLineTradeAgreement>
<ram:SpecifiedLineTradeDelivery>
<ram:BilledQuantity unitCode="C62">1.00</ram:BilledQuantity>
</ram:SpecifiedLineTradeDelivery>
<ram:SpecifiedLineTradeSettlement>
<ram:BillingSpecifiedPeriod>
<!-- Element exists but has no StartDateTime or EndDateTime children -->
</ram:BillingSpecifiedPeriod>
<ram:ApplicableTradeTax>
<ram:TypeCode>VAT</ram:TypeCode>
<ram:CategoryCode>S</ram:CategoryCode>
<ram:RateApplicablePercent>19.00</ram:RateApplicablePercent>
</ram:ApplicableTradeTax>
<ram:SpecifiedTradeSettlementLineMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
</ram:SpecifiedTradeSettlementLineMonetarySummation>
</ram:SpecifiedLineTradeSettlement>
</ram:IncludedSupplyChainTradeLineItem>
<ram:ApplicableHeaderTradeAgreement>
<ram:SellerTradeParty>
<ram:Name>Seller</ram:Name>
<ram:PostalTradeAddress><ram:CountryID>DE</ram:CountryID></ram:PostalTradeAddress>
</ram:SellerTradeParty>
<ram:BuyerTradeParty>
<ram:Name>Buyer</ram:Name>
<ram:PostalTradeAddress><ram:CountryID>FR</ram:CountryID></ram:PostalTradeAddress>
</ram:BuyerTradeParty>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:ApplicableTradeTax>
<ram:CalculatedAmount>19.00</ram:CalculatedAmount>
<ram:TypeCode>VAT</ram:TypeCode>
<ram:BasisAmount>100.00</ram:BasisAmount>
<ram:CategoryCode>S</ram:CategoryCode>
<ram:RateApplicablePercent>19.00</ram:RateApplicablePercent>
</ram:ApplicableTradeTax>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
<ram:TaxBasisTotalAmount>100.00</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount currencyID="EUR">19.00</ram:TaxTotalAmount>
<ram:GrandTotalAmount>119.00</ram:GrandTotalAmount>
<ram:DuePayableAmount>119.00</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>`
inv, err := ParseReader(strings.NewReader(xml))
if err != nil {
t.Fatalf("Failed to parse XML: %v", err)
}
// Verify that linePeriodPresent flag was set
if len(inv.InvoiceLines) == 0 {
t.Fatal("No invoice lines parsed")
}
if !inv.InvoiceLines[0].linePeriodPresent {
t.Error("linePeriodPresent should be true when BG-26 element exists in XML")
}
// Verify both dates are zero
if !inv.InvoiceLines[0].BillingSpecifiedPeriodStart.IsZero() || !inv.InvoiceLines[0].BillingSpecifiedPeriodEnd.IsZero() {
t.Error("Both line BillingSpecifiedPeriod dates should be zero")
}
// Run validation
_ = inv.Validate()
// Should find BR-CO-20 violation
found := false
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-20" {
found = true
break
}
}
if !found {
t.Error("Expected BR-CO-20 violation when BG-26 exists but has no dates")
}
}
// TestCheckBRO_BR_CO_10_Valid tests that BR-CO-10 validation passes when LineTotal matches sum of invoice lines
func TestCheckBRO_BR_CO_10_Valid(t *testing.T) {
inv := &Invoice{
InvoiceLines: []InvoiceLine{
{Total: decimal.NewFromFloat(100.00)},
{Total: decimal.NewFromFloat(200.00)},
},
LineTotal: decimal.NewFromFloat(300.00),
}
inv.validateCalculations()
// Check that no BR-CO-10 violations were added
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-10" {
t.Errorf("Expected no BR-CO-10 violation, but got: %s", v.Text)
}
}
}
// TestCheckBRO_BR_CO_10_Invalid tests that BR-CO-10 violation is detected when LineTotal doesn't match
func TestCheckBRO_BR_CO_10_Invalid(t *testing.T) {
inv := &Invoice{
InvoiceLines: []InvoiceLine{
{Total: decimal.NewFromFloat(100.00)},
{Total: decimal.NewFromFloat(200.00)},
},
LineTotal: decimal.NewFromFloat(250.00), // Wrong value
}
inv.validateCalculations()
// Check that BR-CO-10 violation was added
found := false
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-10" {
found = true
if len(v.Rule.Fields) != 2 || v.Rule.Fields[0] != "BT-106" || v.Rule.Fields[1] != "BT-131" {
t.Errorf("BR-CO-10 violation has incorrect InvFields: %v", v.Rule.Fields)
}
}
}
if !found {
t.Error("Expected BR-CO-10 violation, but none was found")
}
}
// TestCheckBRO_BR_CO_13_Valid tests that BR-CO-13 validation passes when TaxBasisTotal is correct
func TestCheckBRO_BR_CO_13_Valid(t *testing.T) {
inv := &Invoice{
LineTotal: decimal.NewFromFloat(1000.00),
AllowanceTotal: decimal.NewFromFloat(150.00),
ChargeTotal: decimal.NewFromFloat(50.00),
TaxBasisTotal: decimal.NewFromFloat(900.00), // 1000 - 150 + 50
}
inv.validateCalculations()
// Check that no BR-CO-13 violations were added
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-13" {
t.Errorf("Expected no BR-CO-13 violation, but got: %s", v.Text)
}
}
}
// TestCheckBRO_BR_CO_13_Invalid tests that BR-CO-13 violation is detected when TaxBasisTotal is wrong
func TestCheckBRO_BR_CO_13_Invalid(t *testing.T) {
inv := &Invoice{
LineTotal: decimal.NewFromFloat(1000.00),
AllowanceTotal: decimal.NewFromFloat(150.00),
ChargeTotal: decimal.NewFromFloat(50.00),
TaxBasisTotal: decimal.NewFromFloat(1000.00), // Wrong: should be 900
}
inv.validateCalculations()
// Check that BR-CO-13 violation was added
found := false
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-13" {
found = true
expectedFields := []string{"BT-109", "BT-106", "BT-107", "BT-108"}
if len(v.Rule.Fields) != len(expectedFields) {
t.Errorf("BR-CO-13 violation has incorrect number of InvFields: got %v, want %v", v.Rule.Fields, expectedFields)
}
}
}
if !found {
t.Error("Expected BR-CO-13 violation, but none was found")
}
}
// TestCheckBRO_BR_CO_14_Valid tests that BR-CO-14 validation passes when TaxTotal matches sum of VAT category amounts
func TestCheckBRO_BR_CO_14_Valid(t *testing.T) {
inv := &Invoice{
TaxTotal: decimal.NewFromFloat(190.00), // 100 + 90
TradeTaxes: []TradeTax{
{CalculatedAmount: decimal.NewFromFloat(100.00)},
{CalculatedAmount: decimal.NewFromFloat(90.00)},
},
}
inv.validateCalculations()
// Check that no BR-CO-14 violations were added
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-14" {
t.Errorf("Expected no BR-CO-14 violation, but got: %s", v.Text)
}
}
}
// TestCheckBRO_BR_CO_14_Invalid tests that BR-CO-14 violation is detected when TaxTotal doesn't match
func TestCheckBRO_BR_CO_14_Invalid(t *testing.T) {
inv := &Invoice{
TaxTotal: decimal.NewFromFloat(200.00), // Wrong: should be 190
TradeTaxes: []TradeTax{
{CalculatedAmount: decimal.NewFromFloat(100.00)},
{CalculatedAmount: decimal.NewFromFloat(90.00)},
},
}
inv.validateCalculations()
// Check that BR-CO-14 violation was added
found := false
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-14" {
found = true
expectedFields := []string{"BT-110", "BT-117"}
if len(v.Rule.Fields) != len(expectedFields) {
t.Errorf("BR-CO-14 violation has incorrect number of InvFields: got %v, want %v", v.Rule.Fields, expectedFields)
}
}
}
if !found {
t.Error("Expected BR-CO-14 violation, but none was found")
}
}
// TestCheckBRO_BR_CO_14_MultipleCategories tests BR-CO-14 with multiple VAT categories
func TestCheckBRO_BR_CO_14_MultipleCategories(t *testing.T) {
inv := &Invoice{
TaxTotal: decimal.NewFromFloat(315.50), // 100 + 90.50 + 125
TradeTaxes: []TradeTax{
{CategoryCode: "S", CalculatedAmount: decimal.NewFromFloat(100.00)},
{CategoryCode: "S", CalculatedAmount: decimal.NewFromFloat(90.50)},
{CategoryCode: "E", CalculatedAmount: decimal.NewFromFloat(125.00)},
},
}
inv.validateCalculations()
// Check that no BR-CO-14 violations were added
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-14" {
t.Errorf("Expected no BR-CO-14 violation, but got: %s", v.Text)
}
}
}
// TestCheckBRO_BR_CO_14_ZeroTax tests BR-CO-14 with zero tax amounts
func TestCheckBRO_BR_CO_14_ZeroTax(t *testing.T) {
inv := &Invoice{
TaxTotal: decimal.Zero, // All categories are exempt
TradeTaxes: []TradeTax{
{CategoryCode: "E", CalculatedAmount: decimal.Zero},
{CategoryCode: "Z", CalculatedAmount: decimal.Zero},
},
}
inv.validateCalculations()
// Check that no BR-CO-14 violations were added
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-14" {
t.Errorf("Expected no BR-CO-14 violation, but got: %s", v.Text)
}
}
}
// TestCheckBRO_BR_CO_15_Valid tests that BR-CO-15 validation passes when GrandTotal is correct
func TestCheckBRO_BR_CO_15_Valid(t *testing.T) {
inv := &Invoice{
TaxBasisTotal: decimal.NewFromFloat(900.00),
TaxTotal: decimal.NewFromFloat(171.00),
GrandTotal: decimal.NewFromFloat(1071.00), // 900 + 171
}
inv.validateCalculations()
// Check that no BR-CO-15 violations were added
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-15" {
t.Errorf("Expected no BR-CO-15 violation, but got: %s", v.Text)
}
}
}
// TestCheckBRO_BR_CO_15_Invalid tests that BR-CO-15 violation is detected when GrandTotal is wrong
func TestCheckBRO_BR_CO_15_Invalid(t *testing.T) {
inv := &Invoice{
TaxBasisTotal: decimal.NewFromFloat(900.00),
TaxTotal: decimal.NewFromFloat(171.00),
GrandTotal: decimal.NewFromFloat(1000.00), // Wrong: should be 1071
}
inv.validateCalculations()
// Check that BR-CO-15 violation was added
found := false
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-15" {
found = true
expectedFields := []string{"BT-112", "BT-109", "BT-110"}
if len(v.Rule.Fields) != len(expectedFields) {
t.Errorf("BR-CO-15 violation has incorrect number of InvFields: got %v, want %v", v.Rule.Fields, expectedFields)
}
}
}
if !found {
t.Error("Expected BR-CO-15 violation, but none was found")
}
}
// TestCheckBRO_BR_CO_16_Valid tests that BR-CO-16 validation passes when DuePayableAmount is correct
func TestCheckBRO_BR_CO_16_Valid(t *testing.T) {
inv := &Invoice{
GrandTotal: decimal.NewFromFloat(1071.00),
TotalPrepaid: decimal.NewFromFloat(100.00),
RoundingAmount: decimal.NewFromFloat(0.05),
DuePayableAmount: decimal.NewFromFloat(971.05), // 1071 - 100 + 0.05
}
inv.validateCalculations()
// Check that no BR-CO-16 violations were added
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-16" {
t.Errorf("Expected no BR-CO-16 violation, but got: %s", v.Text)
}
}
}
// TestCheckBRO_BR_CO_16_Invalid tests that BR-CO-16 violation is detected when DuePayableAmount is wrong
func TestCheckBRO_BR_CO_16_Invalid(t *testing.T) {
inv := &Invoice{
GrandTotal: decimal.NewFromFloat(1071.00),
TotalPrepaid: decimal.NewFromFloat(100.00),
RoundingAmount: decimal.NewFromFloat(0.05),
DuePayableAmount: decimal.NewFromFloat(971.00), // Wrong: should be 971.05
}
inv.validateCalculations()
// Check that BR-CO-16 violation was added
found := false
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-16" {
found = true
expectedFields := []string{"BT-115", "BT-112", "BT-113", "BT-114"}
if len(v.Rule.Fields) != len(expectedFields) {
t.Errorf("BR-CO-16 violation has incorrect number of InvFields: got %v, want %v", v.Rule.Fields, expectedFields)
}
}
}
if !found {
t.Error("Expected BR-CO-16 violation, but none was found")
}
}
// TestCheckBRO_MultipleViolations tests detection of multiple violations at once
func TestCheckBRO_MultipleViolations(t *testing.T) {
inv := &Invoice{
InvoiceLines: []InvoiceLine{
{Total: decimal.NewFromFloat(100.00)},
{Total: decimal.NewFromFloat(200.00)},
},
LineTotal: decimal.NewFromFloat(250.00), // Wrong: should be 300 (BR-CO-10)
AllowanceTotal: decimal.NewFromFloat(50.00),
ChargeTotal: decimal.NewFromFloat(10.00),
TaxBasisTotal: decimal.NewFromFloat(250.00), // Wrong: should be 210 (BR-CO-13)
TaxTotal: decimal.NewFromFloat(47.50),
GrandTotal: decimal.NewFromFloat(300.00), // Wrong: should be 257.50 (BR-CO-15)
TotalPrepaid: decimal.NewFromFloat(50.00),
RoundingAmount: decimal.NewFromFloat(0.50),
DuePayableAmount: decimal.NewFromFloat(250.00), // Wrong: should be 250.50 (BR-CO-16)
}
inv.validateCalculations()
// Check that all four violations were detected
violations := make(map[string]bool)
for _, v := range inv.violations {
violations[v.Rule.Code] = true
}
expectedViolations := []string{"BR-CO-10", "BR-CO-13", "BR-CO-15", "BR-CO-16"}
for _, rule := range expectedViolations {
if !violations[rule] {
t.Errorf("Expected %s violation, but it was not found", rule)
}
}
}
// TestCheckBRO_WithNegativeRounding tests BR-CO-16 with negative rounding amount
func TestCheckBRO_BR_CO_16_NegativeRounding(t *testing.T) {
inv := &Invoice{
GrandTotal: decimal.NewFromFloat(119.00),
TotalPrepaid: decimal.NewFromFloat(50.00),
RoundingAmount: decimal.NewFromFloat(-0.14),
DuePayableAmount: decimal.NewFromFloat(68.86), // 119 - 50 + (-0.14)
}
inv.validateCalculations()
// Check that no BR-CO-16 violations were added
for _, v := range inv.violations {
if v.Rule.Code == "BR-CO-16" {
t.Errorf("Expected no BR-CO-16 violation with negative rounding, but got: %s", v.Text)
}
}
}
// TestBR45_CompositeKey tests that BR-45 validation correctly uses composite key
// of CategoryCode + Percent (Bug #5 fix) to avoid incorrectly grouping different
// tax categories with the same rate
func TestBR45_CompositeKey_DifferentCategories(t *testing.T) {
inv := &Invoice{
InvoiceLines: []InvoiceLine{
{
TaxCategoryCode: "S", // Standard rate
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(1000.00),
},
{
TaxCategoryCode: "AE", // Reverse charge
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(500.00),
},
},
TradeTaxes: []TradeTax{
{
CategoryCode: "S",
Percent: decimal.NewFromFloat(19),
BasisAmount: decimal.NewFromFloat(1000.00),
CalculatedAmount: decimal.NewFromFloat(190.00),
},
{
CategoryCode: "AE",
Percent: decimal.NewFromFloat(19),
BasisAmount: decimal.NewFromFloat(500.00),
CalculatedAmount: decimal.NewFromFloat(0),
},
},
}
inv.validateCalculations()
// Should not have any BR-45 violations because each category is matched correctly
for _, v := range inv.violations {
if v.Rule.Code == "BR-45" {
t.Errorf("Unexpected BR-45 violation: %s (categories should be matched separately)", v.Text)
}
}
}
// TestBR45_CompositeKey_SameCategory tests BR-45 with same category and rate
func TestBR45_CompositeKey_SameCategory(t *testing.T) {
inv := &Invoice{
InvoiceLines: []InvoiceLine{
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(1000.00),
},
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(500.00),
},
},
TradeTaxes: []TradeTax{
{
CategoryCode: "S",
Percent: decimal.NewFromFloat(19),
BasisAmount: decimal.NewFromFloat(1500.00), // Correct sum
CalculatedAmount: decimal.NewFromFloat(285.00),
},
},
}
inv.validateCalculations()
// Should not have BR-45 violations
for _, v := range inv.violations {
if v.Rule.Code == "BR-45" {
t.Errorf("Unexpected BR-45 violation: %s", v.Text)
}
}
}
// TestBR45_CompositeKey_WithDocumentLevelAllowances tests that BR-45 validation
// correctly handles document-level allowances in tax basis calculation
func TestBR45_CompositeKey_WithDocumentLevelAllowances(t *testing.T) {
inv := &Invoice{
InvoiceLines: []InvoiceLine{
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(1000.00),
},
},
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false, // Allowance
ActualAmount: decimal.NewFromFloat(100.00),
CategoryTradeTaxCategoryCode: "S",
CategoryTradeTaxRateApplicablePercent: decimal.NewFromFloat(19),
},
},
TradeTaxes: []TradeTax{
{
CategoryCode: "S",
Percent: decimal.NewFromFloat(19),
BasisAmount: decimal.NewFromFloat(900.00), // 1000 - 100
CalculatedAmount: decimal.NewFromFloat(171.00),
},