Skip to content

Commit

Permalink
Update Tax ID validations in CO
Browse files Browse the repository at this point in the history
  • Loading branch information
cavalle committed Jan 26, 2024
1 parent d6c558e commit a6f1444
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
22 changes: 10 additions & 12 deletions regimes/co/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,17 @@ func (v *invoiceValidator) validate() error {

func (v *invoiceValidator) validParty(value interface{}) error {
obj, _ := value.(*org.Party)
if obj == nil || obj.TaxID == nil {
if obj == nil {
return nil
}
return validation.ValidateStruct(obj,
validation.Field(&obj.TaxID,
validation.Required,
validation.When(
obj.TaxID.Country.In(l10n.CO),
tax.RequireIdentityCode,
validation.By(v.validTaxIdentity),
),
validation.By(v.validTaxIdentity),
),
validation.Field(&obj.Addresses,
validation.When(
obj.TaxID.Country.In(l10n.CO),
obj.TaxID != nil && obj.TaxID.Country.In(l10n.CO),
validation.Length(1, 0),
),
),
Expand All @@ -87,11 +83,13 @@ func (v *invoiceValidator) validTaxIdentity(value interface{}) error {
return nil
}
return validation.ValidateStruct(obj,
validation.Field(&obj.Zone,
tax.ZoneIn(zones),
validation.When(
obj.Type.In(TaxIdentityTypeTIN),
validation.Required,
validation.Field(&obj.Code, validation.Required),
validation.Field(&obj.Type,
validation.Required,
isValidTaxIdentityTypeKey,
validation.When(!obj.Country.In(l10n.CO),
// Certain types are exclusive of CO identities
validation.NotIn(TaxIdentityTypeTIN, TaxIdentityTypeCitizen),
),
),
)
Expand Down
11 changes: 10 additions & 1 deletion regimes/co/invoices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,16 @@ func TestBasicInvoiceValidation(t *testing.T) {

inv = baseInvoice()
inv.Customer.TaxID.Type = co.TaxIdentityTypeCitizen
inv.Customer.TaxID.Code = "100100100"
inv.Customer.TaxID.Code = co.TaxCodeFinalCustomer
inv.Customer.TaxID.Zone = ""
require.NoError(t, inv.Calculate())
err = inv.Validate()
assert.NoError(t, err)

inv = baseInvoice()
inv.Customer.TaxID.Country = l10n.ES
inv.Customer.TaxID.Code = "A13180492"
inv.Customer.TaxID.Type = co.TaxIdentityTypeForeign
inv.Customer.TaxID.Zone = ""
require.NoError(t, inv.Calculate())
err = inv.Validate()
Expand Down
23 changes: 22 additions & 1 deletion regimes/co/tax_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const (
TaxIdentityTypeForeigner cbc.Key = "foreigner"
TaxIdentityTypePEP cbc.Key = "pep"
TaxIdentityTypeNUIP cbc.Key = "nuip"

TaxCodeFinalCustomer cbc.Code = "222222222222"
)

var (
Expand Down Expand Up @@ -177,14 +179,21 @@ func validateTaxIdentity(tID *tax.Identity) error {
),
),
validation.Field(&tID.Zone,
validation.When(tID.Type.In(TaxIdentityTypeTIN),
validation.When(!isFinalCustomer(tID),
validation.Required,
tax.ZoneIn(zones),
),
),
)
}

func isFinalCustomer(tID *tax.Identity) bool {
if tID == nil {
return false
}
return tID.Type == TaxIdentityTypeCitizen && tID.Code == TaxCodeFinalCustomer
}

// normalizeTaxIdentity will remove any whitespace or separation characters from
// the tax code and also make sure the default type is set.
func normalizeTaxIdentity(tID *tax.Identity) error {
Expand Down Expand Up @@ -265,3 +274,15 @@ func validateDigits(code, check cbc.Code) error {

return nil
}

func taxIdentityTypeKeys() []interface{} {
keys := make([]interface{}, len(taxIdentityTypeDefs))
i := 0
for _, v := range taxIdentityTypeDefs {
keys[i] = v.Key
i++
}
return keys
}

var isValidTaxIdentityTypeKey = validation.In(taxIdentityTypeKeys()...)
4 changes: 2 additions & 2 deletions regimes/co/tax_identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ func TestValidateTaxIdentity(t *testing.T) {
err: "code: cannot be blank",
},
{
name: "missing zone for citizen",
name: "missing zone for final customer",
typ: co.TaxIdentityTypeCitizen,
code: "100100100",
code: co.TaxCodeFinalCustomer,
zone: "",
err: "",
},
Expand Down

0 comments on commit a6f1444

Please sign in to comment.