From 6f1a7d3f4cae039f83152115499e6c143a6a0988 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Tue, 6 Feb 2024 09:28:09 +0000 Subject: [PATCH] VAT always required in Italy --- regimes/it/invoice_validator.go | 1 + regimes/it/invoice_validator_test.go | 15 +++++++++++++++ tax/set.go | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/regimes/it/invoice_validator.go b/regimes/it/invoice_validator.go index 59f0686f..4baa1550 100644 --- a/regimes/it/invoice_validator.go +++ b/regimes/it/invoice_validator.go @@ -128,6 +128,7 @@ func validateLine(value interface{}) error { } return validation.ValidateStruct(v, validation.Field(&v.Taxes, + tax.SetHasCategory(tax.CategoryVAT), validation.Each( validation.By(validateLineTax), validation.Skip, diff --git a/regimes/it/invoice_validator_test.go b/regimes/it/invoice_validator_test.go index 5b9cd3ec..05eb3eed 100644 --- a/regimes/it/invoice_validator_test.go +++ b/regimes/it/invoice_validator_test.go @@ -140,3 +140,18 @@ func TestRetainedTaxesValidation(t *testing.T) { require.NoError(t, inv.Calculate()) require.NoError(t, inv.Validate()) } + +func TestInvoiceLineTaxes(t *testing.T) { + inv := testInvoiceStandard(t) + inv.Lines = append(inv.Lines, &bill.Line{ + Quantity: num.MakeAmount(10, 0), + Item: &org.Item{ + Name: "Test Item", + Price: num.MakeAmount(10000, 2), + }, + // No taxes! + }) + require.NoError(t, inv.Calculate()) + err := inv.Validate() + require.EqualError(t, err, "lines: (1: (taxes: missing category VAT.).).") +} diff --git a/tax/set.go b/tax/set.go index 4f9b0c4b..3a2e04df 100644 --- a/tax/set.go +++ b/tax/set.go @@ -200,3 +200,25 @@ func (s Set) Rate(cat cbc.Code) cbc.Key { } return "" } + +type setValidation struct { + categories []cbc.Code +} + +// SetHasCategory validates that the set contains the given category. +func SetHasCategory(categories ...cbc.Code) validation.Rule { + return &setValidation{categories: categories} +} + +func (sv *setValidation) Validate(value interface{}) error { + s, ok := value.(Set) + if !ok { + return nil + } + for _, c := range sv.categories { + if s.Get(c) == nil { + return fmt.Errorf("missing category %s", c.String()) + } + } + return nil +}