-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcalculate_test.go
More file actions
1214 lines (1070 loc) · 39.2 KB
/
Copy pathcalculate_test.go
File metadata and controls
1214 lines (1070 loc) · 39.2 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 (
"bytes"
"os"
"testing"
"github.com/shopspring/decimal"
)
// TestUpdateTotals_BasicCalculation tests the basic calculation of totals from invoice lines
// according to BR-CO-10: LineTotal should be the sum of all invoice line net amounts
func TestUpdateTotals_BasicCalculation(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{Total: decimal.NewFromFloat(100.00)},
{Total: decimal.NewFromFloat(200.00)},
{Total: decimal.NewFromFloat(50.50)},
},
TradeTaxes: []TradeTax{
{CalculatedAmount: decimal.NewFromFloat(19.00)},
{CalculatedAmount: decimal.NewFromFloat(38.00)},
},
}
inv.UpdateTotals()
// BR-CO-10: LineTotal = Sum of invoice line totals
expectedLineTotal := decimal.NewFromFloat(350.50)
if !inv.LineTotal.Equal(expectedLineTotal) {
t.Errorf("LineTotal = %s, want %s (BR-CO-10)", inv.LineTotal, expectedLineTotal)
}
// TaxTotal = Sum of calculated tax amounts
expectedTaxTotal := decimal.NewFromFloat(57.00)
if !inv.TaxTotal.Equal(expectedTaxTotal) {
t.Errorf("TaxTotal = %s, want %s", inv.TaxTotal, expectedTaxTotal)
}
// BR-CO-13: TaxBasisTotal = LineTotal (no allowances/charges)
if !inv.TaxBasisTotal.Equal(expectedLineTotal) {
t.Errorf("TaxBasisTotal = %s, want %s (BR-CO-13)", inv.TaxBasisTotal, expectedLineTotal)
}
// BR-CO-15: GrandTotal = TaxBasisTotal + TaxTotal
expectedGrandTotal := decimal.NewFromFloat(407.50)
if !inv.GrandTotal.Equal(expectedGrandTotal) {
t.Errorf("GrandTotal = %s, want %s (BR-CO-15)", inv.GrandTotal, expectedGrandTotal)
}
// BR-CO-16: DuePayableAmount = GrandTotal (no prepaid/rounding)
if !inv.DuePayableAmount.Equal(expectedGrandTotal) {
t.Errorf("DuePayableAmount = %s, want %s (BR-CO-16)", inv.DuePayableAmount, expectedGrandTotal)
}
}
// TestUpdateTotals_WithAllowancesAndCharges tests that document-level allowances
// and charges are correctly applied according to BR-CO-13
func TestUpdateTotals_WithAllowancesAndCharges(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{Total: decimal.NewFromFloat(1000.00)},
},
TradeTaxes: []TradeTax{
{CalculatedAmount: decimal.NewFromFloat(171.00)}, // 19% of 900
},
// Provide actual allowances/charges instead of manually setting totals
// UpdateTotals() will calculate AllowanceTotal and ChargeTotal from these
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(100.00),
},
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(50.00),
},
{
ChargeIndicator: true,
ActualAmount: decimal.NewFromFloat(50.00),
},
},
}
inv.UpdateTotals()
// BR-CO-10: LineTotal = Sum of invoice lines
expectedLineTotal := decimal.NewFromFloat(1000.00)
if !inv.LineTotal.Equal(expectedLineTotal) {
t.Errorf("LineTotal = %s, want %s (BR-CO-10)", inv.LineTotal, expectedLineTotal)
}
// BR-CO-13: TaxBasisTotal = LineTotal - AllowanceTotal + ChargeTotal
// = 1000 - 150 + 50 = 900
expectedTaxBasisTotal := decimal.NewFromFloat(900.00)
if !inv.TaxBasisTotal.Equal(expectedTaxBasisTotal) {
t.Errorf("TaxBasisTotal = %s, want %s (BR-CO-13: LineTotal - AllowanceTotal + ChargeTotal)",
inv.TaxBasisTotal, expectedTaxBasisTotal)
}
// BR-CO-15: GrandTotal = TaxBasisTotal + TaxTotal
// = 900 + 171 = 1071
expectedGrandTotal := decimal.NewFromFloat(1071.00)
if !inv.GrandTotal.Equal(expectedGrandTotal) {
t.Errorf("GrandTotal = %s, want %s (BR-CO-15)", inv.GrandTotal, expectedGrandTotal)
}
}
// TestUpdateTotals_WithRoundingAmount tests that rounding amount is correctly
// applied according to BR-CO-16
func TestUpdateTotals_WithRoundingAmount(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{Total: decimal.NewFromFloat(100.00)},
},
TradeTaxes: []TradeTax{
{CalculatedAmount: decimal.NewFromFloat(19.00)},
},
TotalPrepaid: decimal.NewFromFloat(50.00), // BT-113
RoundingAmount: decimal.NewFromFloat(-0.14), // BT-114
}
inv.UpdateTotals()
// BR-CO-16: DuePayableAmount = GrandTotal - TotalPrepaid + RoundingAmount
// = 119.00 - 50.00 + (-0.14) = 68.86
expectedDuePayableAmount := decimal.NewFromFloat(68.86)
if !inv.DuePayableAmount.Equal(expectedDuePayableAmount) {
t.Errorf("DuePayableAmount = %s, want %s (BR-CO-16: GrandTotal - TotalPrepaid + RoundingAmount)",
inv.DuePayableAmount, expectedDuePayableAmount)
}
}
// TestUpdateTotals_Idempotent tests that calling UpdateTotals() multiple times
// produces the same result (fixes the accumulation bug)
func TestUpdateTotals_Idempotent(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{Total: decimal.NewFromFloat(100.00)},
{Total: decimal.NewFromFloat(200.00)},
},
TradeTaxes: []TradeTax{
{CalculatedAmount: decimal.NewFromFloat(57.00)},
},
}
// Call UpdateTotals() first time
inv.UpdateTotals()
firstLineTotal := inv.LineTotal
firstTaxTotal := inv.TaxTotal
firstGrandTotal := inv.GrandTotal
// Call UpdateTotals() second time
inv.UpdateTotals()
// Results should be identical
if !inv.LineTotal.Equal(firstLineTotal) {
t.Errorf("Second call: LineTotal = %s, want %s (should be idempotent)",
inv.LineTotal, firstLineTotal)
}
if !inv.TaxTotal.Equal(firstTaxTotal) {
t.Errorf("Second call: TaxTotal = %s, want %s (should be idempotent)",
inv.TaxTotal, firstTaxTotal)
}
if !inv.GrandTotal.Equal(firstGrandTotal) {
t.Errorf("Second call: GrandTotal = %s, want %s (should be idempotent)",
inv.GrandTotal, firstGrandTotal)
}
// Call UpdateTotals() third time to be sure
inv.UpdateTotals()
if !inv.LineTotal.Equal(firstLineTotal) {
t.Errorf("Third call: LineTotal = %s, want %s (should be idempotent)",
inv.LineTotal, firstLineTotal)
}
}
// TestUpdateTotals_ComprehensiveScenario tests all business rules together
// with a realistic invoice scenario
func TestUpdateTotals_ComprehensiveScenario(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{Total: decimal.NewFromFloat(500.00)}, // Line 1
{Total: decimal.NewFromFloat(300.00)}, // Line 2
{Total: decimal.NewFromFloat(200.00)}, // Line 3
},
TradeTaxes: []TradeTax{
{CalculatedAmount: decimal.NewFromFloat(152.00)}, // 19% VAT on 800 basis
},
// Provide actual allowances/charges instead of manually setting totals
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(150.00), // Document discount
},
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(100.00), // Additional discount
},
{
ChargeIndicator: true,
ActualAmount: decimal.NewFromFloat(50.00), // Shipping charge
},
},
TotalPrepaid: decimal.NewFromFloat(100.00), // Partial payment
RoundingAmount: decimal.NewFromFloat(0.05), // Rounding
}
inv.UpdateTotals()
// BR-CO-10: LineTotal = 500 + 300 + 200 = 1000
expectedLineTotal := decimal.NewFromFloat(1000.00)
if !inv.LineTotal.Equal(expectedLineTotal) {
t.Errorf("LineTotal = %s, want %s (BR-CO-10)", inv.LineTotal, expectedLineTotal)
}
// TaxTotal = 152 (from TradeTaxes)
expectedTaxTotal := decimal.NewFromFloat(152.00)
if !inv.TaxTotal.Equal(expectedTaxTotal) {
t.Errorf("TaxTotal = %s, want %s", inv.TaxTotal, expectedTaxTotal)
}
// BR-CO-13: TaxBasisTotal = 1000 - 250 + 50 = 800
expectedTaxBasisTotal := decimal.NewFromFloat(800.00)
if !inv.TaxBasisTotal.Equal(expectedTaxBasisTotal) {
t.Errorf("TaxBasisTotal = %s, want %s (BR-CO-13)", inv.TaxBasisTotal, expectedTaxBasisTotal)
}
// BR-CO-15: GrandTotal = 800 + 152 = 952
expectedGrandTotal := decimal.NewFromFloat(952.00)
if !inv.GrandTotal.Equal(expectedGrandTotal) {
t.Errorf("GrandTotal = %s, want %s (BR-CO-15)", inv.GrandTotal, expectedGrandTotal)
}
// BR-CO-16: DuePayableAmount = 952 - 100 + 0.05 = 852.05
expectedDuePayableAmount := decimal.NewFromFloat(852.05)
if !inv.DuePayableAmount.Equal(expectedDuePayableAmount) {
t.Errorf("DuePayableAmount = %s, want %s (BR-CO-16)", inv.DuePayableAmount, expectedDuePayableAmount)
}
}
// TestUpdateTotals_ZeroValues tests that UpdateTotals works correctly with zero values
func TestUpdateTotals_ZeroValues(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{},
TradeTaxes: []TradeTax{},
}
inv.UpdateTotals()
if !inv.LineTotal.IsZero() {
t.Errorf("LineTotal = %s, want 0", inv.LineTotal)
}
if !inv.TaxTotal.IsZero() {
t.Errorf("TaxTotal = %s, want 0", inv.TaxTotal)
}
if !inv.TaxBasisTotal.IsZero() {
t.Errorf("TaxBasisTotal = %s, want 0", inv.TaxBasisTotal)
}
if !inv.GrandTotal.IsZero() {
t.Errorf("GrandTotal = %s, want 0", inv.GrandTotal)
}
if !inv.DuePayableAmount.IsZero() {
t.Errorf("DuePayableAmount = %s, want 0", inv.DuePayableAmount)
}
}
// TestUpdateApplicableTradeTax_WithDocumentLevelAllowances tests that document-level
// allowances are correctly subtracted from the tax basis amount (Bug #4 fix)
func TestUpdateApplicableTradeTax_WithDocumentLevelAllowances(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
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),
},
},
}
inv.UpdateApplicableTradeTax(map[string]string{})
// Should have one tax entry for category S with 19%
if len(inv.TradeTaxes) != 1 {
t.Fatalf("Expected 1 TradeTax entry, got %d", len(inv.TradeTaxes))
}
tt := inv.TradeTaxes[0]
// BR-S-8: Basis amount should be line total minus allowance
// = 1000 - 100 = 900
expectedBasisAmount := decimal.NewFromFloat(900.00)
if !tt.BasisAmount.Equal(expectedBasisAmount) {
t.Errorf("BasisAmount = %s, want %s (should subtract allowance)", tt.BasisAmount, expectedBasisAmount)
}
// Tax should be 19% of 900 = 171
expectedTax := decimal.NewFromFloat(171.00)
if !tt.CalculatedAmount.Equal(expectedTax) {
t.Errorf("CalculatedAmount = %s, want %s", tt.CalculatedAmount, expectedTax)
}
if tt.CategoryCode != "S" {
t.Errorf("CategoryCode = %s, want S", tt.CategoryCode)
}
}
// TestUpdateApplicableTradeTax_WithDocumentLevelCharges tests that document-level
// charges are correctly added to the tax basis amount (Bug #4 fix)
func TestUpdateApplicableTradeTax_WithDocumentLevelCharges(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(1000.00),
},
},
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: true, // Charge
ActualAmount: decimal.NewFromFloat(50.00),
CategoryTradeTaxCategoryCode: "S",
CategoryTradeTaxRateApplicablePercent: decimal.NewFromFloat(19),
},
},
}
inv.UpdateApplicableTradeTax(map[string]string{})
// Should have one tax entry for category S with 19%
if len(inv.TradeTaxes) != 1 {
t.Fatalf("Expected 1 TradeTax entry, got %d", len(inv.TradeTaxes))
}
tt := inv.TradeTaxes[0]
// BR-S-8: Basis amount should be line total plus charge
// = 1000 + 50 = 1050
expectedBasisAmount := decimal.NewFromFloat(1050.00)
if !tt.BasisAmount.Equal(expectedBasisAmount) {
t.Errorf("BasisAmount = %s, want %s (should add charge)", tt.BasisAmount, expectedBasisAmount)
}
// Tax should be 19% of 1050 = 199.50
expectedTax := decimal.NewFromFloat(199.50)
if !tt.CalculatedAmount.Equal(expectedTax) {
t.Errorf("CalculatedAmount = %s, want %s", tt.CalculatedAmount, expectedTax)
}
}
// TestUpdateApplicableTradeTax_MixedAllowancesAndCharges tests a realistic scenario
// with both allowances and charges on the same tax category
func TestUpdateApplicableTradeTax_MixedAllowancesAndCharges(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(1000.00),
},
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(500.00),
},
},
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false, // Allowance
ActualAmount: decimal.NewFromFloat(150.00),
CategoryTradeTaxCategoryCode: "S",
CategoryTradeTaxRateApplicablePercent: decimal.NewFromFloat(19),
},
{
ChargeIndicator: true, // Charge (shipping)
ActualAmount: decimal.NewFromFloat(50.00),
CategoryTradeTaxCategoryCode: "S",
CategoryTradeTaxRateApplicablePercent: decimal.NewFromFloat(19),
},
},
}
inv.UpdateApplicableTradeTax(map[string]string{})
if len(inv.TradeTaxes) != 1 {
t.Fatalf("Expected 1 TradeTax entry, got %d", len(inv.TradeTaxes))
}
tt := inv.TradeTaxes[0]
// Basis = (1000 + 500) - 150 + 50 = 1400
expectedBasisAmount := decimal.NewFromFloat(1400.00)
if !tt.BasisAmount.Equal(expectedBasisAmount) {
t.Errorf("BasisAmount = %s, want %s (lines - allowance + charge)", tt.BasisAmount, expectedBasisAmount)
}
// Tax = 19% of 1400 = 266
expectedTax := decimal.NewFromFloat(266.00)
if !tt.CalculatedAmount.Equal(expectedTax) {
t.Errorf("CalculatedAmount = %s, want %s", tt.CalculatedAmount, expectedTax)
}
}
// TestUpdateApplicableTradeTax_MultipleCategories tests handling of multiple
// tax categories with document-level allowances/charges
func TestUpdateApplicableTradeTax_MultipleCategories(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(1000.00),
},
{
TaxCategoryCode: "AE", // Reverse charge
TaxRateApplicablePercent: decimal.NewFromFloat(0),
Total: decimal.NewFromFloat(500.00),
},
},
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(100.00),
CategoryTradeTaxCategoryCode: "S",
CategoryTradeTaxRateApplicablePercent: decimal.NewFromFloat(19),
},
},
}
inv.UpdateApplicableTradeTax(map[string]string{"AE": "Reverse charge"})
// Should have two tax entries
if len(inv.TradeTaxes) != 2 {
t.Fatalf("Expected 2 TradeTax entries, got %d", len(inv.TradeTaxes))
}
// Find category S and AE
var ttS, ttAE *TradeTax
for i := range inv.TradeTaxes {
switch inv.TradeTaxes[i].CategoryCode {
case "S":
ttS = &inv.TradeTaxes[i]
case "AE":
ttAE = &inv.TradeTaxes[i]
}
}
if ttS == nil {
t.Fatal("Category S not found in TradeTaxes")
}
if ttAE == nil {
t.Fatal("Category AE not found in TradeTaxes")
}
// Category S: 1000 - 100 = 900
expectedBasisS := decimal.NewFromFloat(900.00)
if !ttS.BasisAmount.Equal(expectedBasisS) {
t.Errorf("Category S BasisAmount = %s, want %s", ttS.BasisAmount, expectedBasisS)
}
// Category AE: 500 (no allowances/charges for this category)
expectedBasisAE := decimal.NewFromFloat(500.00)
if !ttAE.BasisAmount.Equal(expectedBasisAE) {
t.Errorf("Category AE BasisAmount = %s, want %s", ttAE.BasisAmount, expectedBasisAE)
}
// Check exemption reason
if ttAE.ExemptionReason != "Reverse charge" {
t.Errorf("ExemptionReason = %q, want %q", ttAE.ExemptionReason, "Reverse charge")
}
}
// TestUpdateApplicableTradeTax_AllowanceOnlyCategory tests that allowances
// can create a tax category even without invoice lines (edge case)
func TestUpdateApplicableTradeTax_AllowanceOnlyCategory(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{},
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(50.00),
CategoryTradeTaxCategoryCode: "S",
CategoryTradeTaxRateApplicablePercent: decimal.NewFromFloat(19),
},
},
}
inv.UpdateApplicableTradeTax(map[string]string{})
// Should create a tax entry for the allowance
if len(inv.TradeTaxes) != 1 {
t.Fatalf("Expected 1 TradeTax entry, got %d", len(inv.TradeTaxes))
}
tt := inv.TradeTaxes[0]
// Basis = -50 (allowance with no lines)
expectedBasis := decimal.NewFromFloat(-50.00)
if !tt.BasisAmount.Equal(expectedBasis) {
t.Errorf("BasisAmount = %s, want %s", tt.BasisAmount, expectedBasis)
}
// Tax = 19% of -50 = -9.50
expectedTax := decimal.NewFromFloat(-9.50)
if !tt.CalculatedAmount.Equal(expectedTax) {
t.Errorf("CalculatedAmount = %s, want %s", tt.CalculatedAmount, expectedTax)
}
}
// TestUpdateAllowancesAndCharges verifies the internal calculation function
func TestUpdateAllowancesAndCharges(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(100),
},
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(50),
},
{
ChargeIndicator: true,
ActualAmount: decimal.NewFromFloat(30),
},
{
ChargeIndicator: true,
ActualAmount: decimal.NewFromFloat(20),
},
},
}
inv.updateAllowancesAndCharges()
expectedAllowance := decimal.NewFromFloat(150)
expectedCharge := decimal.NewFromFloat(50)
if !inv.AllowanceTotal.Equal(expectedAllowance) {
t.Errorf("AllowanceTotal = %s, want %s", inv.AllowanceTotal, expectedAllowance)
}
if !inv.ChargeTotal.Equal(expectedCharge) {
t.Errorf("ChargeTotal = %s, want %s", inv.ChargeTotal, expectedCharge)
}
}
// TestUpdateAllowancesAndCharges_Idempotent verifies idempotent behavior
func TestUpdateAllowancesAndCharges_Idempotent(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(100),
},
},
}
inv.updateAllowancesAndCharges()
first := inv.AllowanceTotal
inv.updateAllowancesAndCharges()
second := inv.AllowanceTotal
if !first.Equal(second) {
t.Errorf("updateAllowancesAndCharges not idempotent: first=%s, second=%s", first, second)
}
}
// TestUpdateTotals_AutomaticallyCalculatesAllowancesAndCharges verifies that
// UpdateTotals() automatically calculates AllowanceTotal and ChargeTotal
// from SpecifiedTradeAllowanceCharge, making the API more intuitive
func TestUpdateTotals_AutomaticallyCalculatesAllowancesAndCharges(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{Total: decimal.NewFromFloat(1000.00)},
},
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(100.00),
Reason: "Volume discount",
},
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(50.00),
Reason: "Early payment",
},
{
ChargeIndicator: true,
ActualAmount: decimal.NewFromFloat(30.00),
Reason: "Shipping",
},
},
TradeTaxes: []TradeTax{
{CalculatedAmount: decimal.NewFromFloat(166.60)}, // 19% of 880
},
}
// User only needs to call UpdateTotals() - it handles everything
inv.UpdateTotals()
// Verify AllowanceTotal was calculated automatically
expectedAllowanceTotal := decimal.NewFromFloat(150.00) // 100 + 50
if !inv.AllowanceTotal.Equal(expectedAllowanceTotal) {
t.Errorf("AllowanceTotal = %s, want %s (should be calculated automatically)",
inv.AllowanceTotal, expectedAllowanceTotal)
}
// Verify ChargeTotal was calculated automatically
expectedChargeTotal := decimal.NewFromFloat(30.00)
if !inv.ChargeTotal.Equal(expectedChargeTotal) {
t.Errorf("ChargeTotal = %s, want %s (should be calculated automatically)",
inv.ChargeTotal, expectedChargeTotal)
}
// Verify TaxBasisTotal uses the calculated allowances/charges
// LineTotal (1000) - AllowanceTotal (150) + ChargeTotal (30) = 880
expectedTaxBasisTotal := decimal.NewFromFloat(880.00)
if !inv.TaxBasisTotal.Equal(expectedTaxBasisTotal) {
t.Errorf("TaxBasisTotal = %s, want %s (should use calculated allowances/charges)",
inv.TaxBasisTotal, expectedTaxBasisTotal)
}
// Verify GrandTotal = TaxBasisTotal + TaxTotal = 880 + 166.60 = 1046.60
expectedGrandTotal := decimal.NewFromFloat(1046.60)
if !inv.GrandTotal.Equal(expectedGrandTotal) {
t.Errorf("GrandTotal = %s, want %s", inv.GrandTotal, expectedGrandTotal)
}
}
// TestUpdateTotals_IntegrationWithCalculations tests the complete calculation flow
// that users would typically follow when building an invoice programmatically
func TestUpdateTotals_IntegrationWithCalculations(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{
Total: decimal.NewFromFloat(500.00),
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
},
{
Total: decimal.NewFromFloat(300.00),
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
},
},
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(100.00),
CategoryTradeTaxCategoryCode: "S",
CategoryTradeTaxRateApplicablePercent: decimal.NewFromFloat(19),
},
{
ChargeIndicator: true,
ActualAmount: decimal.NewFromFloat(50.00),
CategoryTradeTaxCategoryCode: "S",
CategoryTradeTaxRateApplicablePercent: decimal.NewFromFloat(19),
},
},
TotalPrepaid: decimal.NewFromFloat(200.00),
RoundingAmount: decimal.NewFromFloat(-0.25),
}
// Step 1: Calculate VAT breakdown from lines and document-level allowances/charges
inv.UpdateApplicableTradeTax(map[string]string{})
// Verify VAT was calculated correctly
// Basis: (500 + 300) - 100 + 50 = 750
// VAT: 750 × 19% = 142.50
if len(inv.TradeTaxes) != 1 {
t.Fatalf("Expected 1 TradeTax, got %d", len(inv.TradeTaxes))
}
expectedVATBasis := decimal.NewFromFloat(750.00)
if !inv.TradeTaxes[0].BasisAmount.Equal(expectedVATBasis) {
t.Errorf("VAT BasisAmount = %s, want %s", inv.TradeTaxes[0].BasisAmount, expectedVATBasis)
}
expectedVAT := decimal.NewFromFloat(142.50)
if !inv.TradeTaxes[0].CalculatedAmount.Equal(expectedVAT) {
t.Errorf("VAT CalculatedAmount = %s, want %s", inv.TradeTaxes[0].CalculatedAmount, expectedVAT)
}
// Step 2: Calculate all totals (including allowances/charges)
inv.UpdateTotals()
// Verify all totals
// LineTotal = 500 + 300 = 800
expectedLineTotal := decimal.NewFromFloat(800.00)
if !inv.LineTotal.Equal(expectedLineTotal) {
t.Errorf("LineTotal = %s, want %s", inv.LineTotal, expectedLineTotal)
}
// AllowanceTotal = 100 (calculated automatically)
expectedAllowanceTotal := decimal.NewFromFloat(100.00)
if !inv.AllowanceTotal.Equal(expectedAllowanceTotal) {
t.Errorf("AllowanceTotal = %s, want %s", inv.AllowanceTotal, expectedAllowanceTotal)
}
// ChargeTotal = 50 (calculated automatically)
expectedChargeTotal := decimal.NewFromFloat(50.00)
if !inv.ChargeTotal.Equal(expectedChargeTotal) {
t.Errorf("ChargeTotal = %s, want %s", inv.ChargeTotal, expectedChargeTotal)
}
// TaxBasisTotal = 800 - 100 + 50 = 750
expectedTaxBasisTotal := decimal.NewFromFloat(750.00)
if !inv.TaxBasisTotal.Equal(expectedTaxBasisTotal) {
t.Errorf("TaxBasisTotal = %s, want %s", inv.TaxBasisTotal, expectedTaxBasisTotal)
}
// TaxTotal = 142.50
expectedTaxTotal := decimal.NewFromFloat(142.50)
if !inv.TaxTotal.Equal(expectedTaxTotal) {
t.Errorf("TaxTotal = %s, want %s", inv.TaxTotal, expectedTaxTotal)
}
// GrandTotal = 750 + 142.50 = 892.50
expectedGrandTotal := decimal.NewFromFloat(892.50)
if !inv.GrandTotal.Equal(expectedGrandTotal) {
t.Errorf("GrandTotal = %s, want %s", inv.GrandTotal, expectedGrandTotal)
}
// DuePayableAmount = 892.50 - 200 + (-0.25) = 692.25
expectedDuePayable := decimal.NewFromFloat(692.25)
if !inv.DuePayableAmount.Equal(expectedDuePayable) {
t.Errorf("DuePayableAmount = %s, want %s", inv.DuePayableAmount, expectedDuePayable)
}
}
// TestUpdateTotals_NoAllowancesOrCharges verifies UpdateTotals works correctly
// when there are no document-level allowances or charges
func TestUpdateTotals_NoAllowancesOrCharges(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{Total: decimal.NewFromFloat(100.00)},
},
TradeTaxes: []TradeTax{
{CalculatedAmount: decimal.NewFromFloat(19.00)},
},
// No SpecifiedTradeAllowanceCharge
}
inv.UpdateTotals()
// AllowanceTotal should be zero
if !inv.AllowanceTotal.IsZero() {
t.Errorf("AllowanceTotal = %s, want 0 (no allowances provided)", inv.AllowanceTotal)
}
// ChargeTotal should be zero
if !inv.ChargeTotal.IsZero() {
t.Errorf("ChargeTotal = %s, want 0 (no charges provided)", inv.ChargeTotal)
}
// TaxBasisTotal should equal LineTotal
if !inv.TaxBasisTotal.Equal(inv.LineTotal) {
t.Errorf("TaxBasisTotal = %s, want %s (LineTotal with no adjustments)",
inv.TaxBasisTotal, inv.LineTotal)
}
}
// TestUpdateApplicableTradeTax_BasisAmountRounding tests that BasisAmount is rounded
// to 2 decimal places to comply with BR-DEC-19 (Bug #4)
// This reproduces the bug where BasisAmount can have more than 2 decimal places,
// violating BR-DEC-19 and causing validation inconsistencies
func TestUpdateApplicableTradeTax_BasisAmountRounding(t *testing.T) {
// Create invoice with line totals that sum to a value with > 2 decimals
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(100.333), // 3 decimals
},
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(200.777), // 3 decimals
},
},
}
inv.UpdateApplicableTradeTax(map[string]string{})
if len(inv.TradeTaxes) != 1 {
t.Fatalf("Expected 1 TradeTax entry, got %d", len(inv.TradeTaxes))
}
tt := inv.TradeTaxes[0]
// BUG: BasisAmount should be rounded to 2 decimals per BR-DEC-19
// Sum is 100.333 + 200.777 = 301.110 which should be rounded to 301.11
expectedBasis := decimal.NewFromFloat(301.11)
// Check if BasisAmount has at most 2 decimal places (BR-DEC-19)
if !hasMaxDecimals(tt.BasisAmount, 2) {
t.Errorf("BasisAmount has more than 2 decimal places: %s (violates BR-DEC-19)", tt.BasisAmount.String())
}
// Verify the rounded value
if !tt.BasisAmount.Equal(expectedBasis) {
t.Errorf("BasisAmount = %s, want %s (should be rounded to 2 decimals)", tt.BasisAmount.String(), expectedBasis.String())
}
// Verify CalculatedAmount is also correctly rounded
// 301.11 × 19% = 57.2109, rounded to 57.21
expectedTax := decimal.NewFromFloat(57.21)
if !tt.CalculatedAmount.Equal(expectedTax) {
t.Errorf("CalculatedAmount = %s, want %s", tt.CalculatedAmount.String(), expectedTax.String())
}
}
// TestUpdateApplicableTradeTax_ValidationConsistency tests that the BasisAmount
// calculated by UpdateApplicableTradeTax matches the BasisAmount calculated by
// BR-S-8 validation (Bug #4)
func TestUpdateApplicableTradeTax_ValidationConsistency(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(19),
Total: decimal.NewFromFloat(100.335),
},
},
SpecifiedTradeAllowanceCharge: []AllowanceCharge{
{
ChargeIndicator: false,
ActualAmount: decimal.NewFromFloat(10.225),
CategoryTradeTaxCategoryCode: "S",
CategoryTradeTaxRateApplicablePercent: decimal.NewFromFloat(19),
},
},
}
inv.UpdateApplicableTradeTax(map[string]string{})
// Simulate what BR-S-8 validation does
tt := inv.TradeTaxes[0]
calculatedBasis := decimal.Zero
for _, line := range inv.InvoiceLines {
if line.TaxCategoryCode == "S" && line.TaxRateApplicablePercent.Equal(tt.Percent) {
calculatedBasis = calculatedBasis.Add(line.Total)
}
}
for _, ac := range inv.SpecifiedTradeAllowanceCharge {
if ac.CategoryTradeTaxCategoryCode == "S" && ac.CategoryTradeTaxRateApplicablePercent.Equal(tt.Percent) {
if ac.ChargeIndicator {
calculatedBasis = calculatedBasis.Add(ac.ActualAmount)
} else {
calculatedBasis = calculatedBasis.Sub(ac.ActualAmount)
}
}
}
calculatedBasis = calculatedBasis.Round(2) // BR-S-8 rounds for comparison
// BUG: Without rounding in UpdateApplicableTradeTax, these won't match
// UpdateApplicableTradeTax gives: 100.335 - 10.225 = 90.110 (3 decimals)
// BR-S-8 validation expects: 90.11 (2 decimals)
if !tt.BasisAmount.Equal(calculatedBasis) {
t.Errorf("BasisAmount mismatch: UpdateApplicableTradeTax=%s, BR-S-8 validation=%s (should be equal)",
tt.BasisAmount.String(), calculatedBasis.String())
}
}
// TestUpdateApplicableTradeTax_RoundHalfUpVAT tests that VAT amounts are correctly
// rounded using "round half up" (commercial rounding) instead of banker's rounding.
// This test reproduces the issue from GitHub issue #51.
//
// Example: 182,631.82 EUR at 19% VAT rate
// - Calculation: 182631.82 * 0.19 = 34,700.0458
// - With banker's rounding: 34,700.04 (incorrect, rounds to even)
// - With round half up: 34,700.05 (correct, rounds up when ≥ 5)
func TestUpdateApplicableTradeTax_RoundHalfUpVAT(t *testing.T) {
tests := []struct {
name string
basisAmount string
rate float64
expectedVAT string
}{
{
name: "Issue #51: 182631.82 at 19%",
basisAmount: "182631.82",
rate: 19.0,
expectedVAT: "34700.05", // Not 34700.04 (banker's rounding would give .04)
},
{
name: "Round down case",
basisAmount: "100.00",
rate: 19.0,
expectedVAT: "19.00",
},
{
name: "Round up at .005",
basisAmount: "10.00",
rate: 19.5,
expectedVAT: "1.95", // 10 * 0.195 = 1.95 exactly
},
{
name: "Round up at .006",
basisAmount: "100.03",
rate: 19.0,
expectedVAT: "19.01", // 100.03 * 0.19 = 19.0057, rounds to 19.01
},
{
name: "Round down at .004",
basisAmount: "100.02",
rate: 19.0,
expectedVAT: "19.00", // 100.02 * 0.19 = 19.0038, rounds to 19.00
},
{
name: "Exactly at .005 boundary",
basisAmount: "52.63",
rate: 19.0,
expectedVAT: "10.00", // 52.63 * 0.19 = 9.9997 ≈ 10.00, rounds to 10.00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
basis, _ := decimal.NewFromString(tt.basisAmount)
expectedVAT, _ := decimal.NewFromString(tt.expectedVAT)
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceLines: []InvoiceLine{
{
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromFloat(tt.rate),
Total: basis,
},
},
}
inv.UpdateApplicableTradeTax(map[string]string{})
if len(inv.TradeTaxes) != 1 {
t.Fatalf("Expected 1 trade tax, got %d", len(inv.TradeTaxes))
}
actualVAT := inv.TradeTaxes[0].CalculatedAmount
if !actualVAT.Equal(expectedVAT) {
t.Errorf("VAT amount = %s, want %s (basis %s × rate %s%%)",
actualVAT.String(), expectedVAT.String(), tt.basisAmount, decimal.NewFromFloat(tt.rate).String())
}
})
}
}
// TestRoundHalfUp tests the roundHalfUp helper function directly
func TestRoundHalfUp(t *testing.T) {
tests := []struct {
name string
value string
places int32
expected string
}{
{"Round up at .005", "34700.0458", 2, "34700.05"},
{"Round down at .004", "34700.0448", 2, "34700.04"},
{"Exactly .005", "1.005", 2, "1.01"},
{"Exactly .015", "1.015", 2, "1.02"},
{"Exactly .025", "1.025", 2, "1.03"},
{"Negative round up", "-34700.0458", 2, "-34700.05"},
{"Negative round down", "-34700.0448", 2, "-34700.04"},
{"Negative exactly .005", "-1.005", 2, "-1.01"},
{"Round to 1 place", "1.25", 1, "1.3"},
{"Round to 0 places", "1.5", 0, "2"},