Skip to content

Commit

Permalink
VAT always required in Italy
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Feb 6, 2024
1 parent ffa3ea9 commit 6f1a7d3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions regimes/it/invoice_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions regimes/it/invoice_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.).).")
}
22 changes: 22 additions & 0 deletions tax/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 6f1a7d3

Please sign in to comment.