Skip to content

Commit

Permalink
Modifying keys for types
Browse files Browse the repository at this point in the history
  • Loading branch information
Menendez6 committed Oct 22, 2024
1 parent 19193e4 commit 2905228
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 63 deletions.
8 changes: 4 additions & 4 deletions data/regimes/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@
]
}
],
"identity_keys": [
"identity_types": [
{
"key": "fr-tax-number",
"value": "SPI",
"name": {
"en": "Tax Number",
"fr": "Numéro fiscal de référence"
"en": "Index Steering System",
"fr": "Système de Pilotage des Indices"
}
}
],
Expand Down
6 changes: 3 additions & 3 deletions data/regimes/gb.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@
]
}
],
"identity_keys": [
"identity_types": [
{
"key": "gb-utr",
"value": "UTR",
"name": {
"en": "Unique Taxpayer Reference"
}
},
{
"key": "gb-nino",
"value": "NINO",
"name": {
"en": "National Insurance Number"
}
Expand Down
4 changes: 2 additions & 2 deletions data/regimes/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@
]
}
],
"identity_keys": [
"identity_types": [
{
"key": "pl-tax-number",
"value": "PESEL",
"name": {
"en": "Tax Number",
"pl": "Numer podatkowy"
Expand Down
13 changes: 6 additions & 7 deletions data/schemas/tax/regime-def.json
Original file line number Diff line number Diff line change
Expand Up @@ -278,21 +278,20 @@
"title": "Extensions",
"description": "Extensions defines the keys that can be used for extended or extra data inside the regime that\nis specific to the regime and cannot be easily determined from other GOBL structures.\nTypically these are used to define local codes for suppliers, customers, products, or tax rates."
},
"tax_identity_type_keys": {
"identity_keys": {
"items": {
"$ref": "https://gobl.org/draft-0/cbc/key-definition"
},
"type": "array",
"title": "Tax Identity Type Keys",
"description": "Tax Identity types specific for the regime and may be validated\nagainst."
"title": "Identity Keys",
"description": "Identity keys used in addition to regular tax identities and specific for the\nregime that may be validated against."
},
"identity_keys": {
"identity_types": {
"items": {
"$ref": "https://gobl.org/draft-0/cbc/key-definition"
"$ref": "https://gobl.org/draft-0/cbc/value-definition"
},
"type": "array",
"title": "Identity Keys",
"description": "Identity keys used in addition to regular tax identities and specific for the\nregime that may be validated against."
"description": "Identity Types are used as an alternative to Identity Keys when there is a clear local\ndefinition of a specific code."
},
"charge_keys": {
"items": {
Expand Down
1 change: 1 addition & 0 deletions regimes/fr/identities.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func identityValidator(typ cbc.Code) validation.RuleFunc {
switch typ {
case IdentityTypeSPI:
return validation.Validate(value, validation.Match(identityTypeSPIPattern))
//TODO: Add the other types
default:
return nil

Check warning on line 70 in regimes/fr/identities.go

View check run for this annotation

Codecov / codecov/patch

regimes/fr/identities.go#L69-L70

Added lines #L69 - L70 were not covered by tests
}
Expand Down
8 changes: 4 additions & 4 deletions regimes/gb/gb.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func New() *tax.RegimeDef {
Tags: []*tax.TagSet{
common.InvoiceTags(),
},
Categories: taxCategories,
IdentityKeys: identityKeyDefinitions,
Categories: taxCategories,
IdentityTypes: identityTypeDefinitions,
Corrections: []*tax.CorrectionDefinition{
{
Schema: bill.ShortSchemaInvoice,
Expand All @@ -66,7 +66,7 @@ func Validate(doc interface{}) error {
case *tax.Identity:
return validateTaxIdentity(obj)
case *org.Identity:
return validateTaxNumber(obj)
return validateIdentity(obj)
}
return nil
}
Expand All @@ -77,6 +77,6 @@ func Normalize(doc interface{}) {
case *tax.Identity:
tax.NormalizeIdentity(obj, altCountryCodes...)
case *org.Identity:
normalizeTaxNumber(obj)
normalizeIdentity(obj)
}
}
36 changes: 18 additions & 18 deletions regimes/gb/identities.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

const (
// IdentityUTR represents the UK Unique Taxpayer Reference (UTR).

Check failure on line 16 in regimes/gb/identities.go

View workflow job for this annotation

GitHub Actions / golangci-lint

exported: comment on exported const IdentityTypeUTR should be of the form "IdentityTypeUTR ..." (revive)
IdentityUTR cbc.Key = "gb-utr"
IdentityTypeUTR cbc.Code = "UTR"
// IdentityNINO represents the UK National Insurance Number (NINO).

Check failure on line 18 in regimes/gb/identities.go

View workflow job for this annotation

GitHub Actions / golangci-lint

exported: comment on exported const IdentityTypeNINO should be of the form "IdentityTypeNINO ..." (revive)
IdentityNINO cbc.Key = "gb-nino"
IdentityTypeNINO cbc.Code = "NINO"
)

var badCharsRegexPattern = regexp.MustCompile(`[^\d]`)
Expand All @@ -26,58 +26,58 @@ var utrPattern = `^[1-9]\d{9}$`
// https://design.tax.service.gov.uk/hmrc-design-patterns/unique-taxpayer-reference/
// https://www.gov.uk/hmrc-internal-manuals/national-insurance-manual/nim39110

var identityKeyDefinitions = []*cbc.KeyDefinition{
var identityTypeDefinitions = []*cbc.ValueDefinition{
{
Key: IdentityUTR,
Value: IdentityTypeUTR.String(),
Name: i18n.String{
i18n.EN: "Unique Taxpayer Reference",
},
},
{
Key: IdentityNINO,
Value: IdentityTypeNINO.String(),
Name: i18n.String{
i18n.EN: "National Insurance Number",
},
},
}

func normalizeTaxNumber(id *org.Identity) {
if id == nil || (id.Key != IdentityUTR && id.Key != IdentityNINO) {
func normalizeIdentity(id *org.Identity) {
if id == nil || (id.Type != IdentityTypeUTR && id.Type != IdentityTypeNINO) {
return
}

Check warning on line 47 in regimes/gb/identities.go

View check run for this annotation

Codecov / codecov/patch

regimes/gb/identities.go#L46-L47

Added lines #L46 - L47 were not covered by tests

if id.Key == IdentityUTR {
if id.Type == IdentityTypeUTR {
code := id.Code.String()
code = badCharsRegexPattern.ReplaceAllString(code, "")
id.Code = cbc.Code(code)
} else if id.Key == IdentityNINO {
} else if id.Type == IdentityTypeNINO {
code := id.Code.String()
code = strings.ToUpper(code)
code = tax.IdentityCodeBadCharsRegexp.ReplaceAllString(code, "")
id.Code = cbc.Code(code)
}
}

func validateTaxNumber(id *org.Identity) error {
func validateIdentity(id *org.Identity) error {
if id == nil {
return nil
}

Check warning on line 64 in regimes/gb/identities.go

View check run for this annotation

Codecov / codecov/patch

regimes/gb/identities.go#L63-L64

Added lines #L63 - L64 were not covered by tests

if id.Key == IdentityNINO {
if id.Type == IdentityTypeNINO {
return validation.ValidateStruct(id,
validation.Field(&id.Code, validation.By(validateNinoCode)),
validation.Field(&id.Code, validation.By(validateNino)),
)
} else if id.Key == IdentityUTR {
} else if id.Type == IdentityTypeUTR {
return validation.ValidateStruct(id,
validation.Field(&id.Code, validation.By(validateUtrCode)),
validation.Field(&id.Code, validation.By(validateUtr)),
)
}

return nil

Check warning on line 76 in regimes/gb/identities.go

View check run for this annotation

Codecov / codecov/patch

regimes/gb/identities.go#L76

Added line #L76 was not covered by tests
}

// validateUtrCode validates the normalized Unique Taxpayer Reference (UTR).
func validateUtrCode(value interface{}) error {
// validateUtr validates the normalized Unique Taxpayer Reference (UTR).
func validateUtr(value interface{}) error {
code, ok := value.(cbc.Code)
if !ok || code == "" {
return nil
Expand All @@ -98,8 +98,8 @@ func validateUtrCode(value interface{}) error {
return nil
}

// validateNinoCode validates the normalized National Insurance Number (NINO).
func validateNinoCode(value interface{}) error {
// validateNino validates the normalized National Insurance Number (NINO).
func validateNino(value interface{}) error {
code, ok := value.(cbc.Code)
if !ok || code == "" {
return nil
Expand Down
18 changes: 9 additions & 9 deletions regimes/gb/identities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,56 @@ import (
func TestUKIdentifiers(t *testing.T) {
tests := []struct {
name string
idKey cbc.Key
idCode cbc.Code
initialCode string
expectedCode string
expectedError string
}{
{
name: "Normalize UTR - spaces removed",
idKey: gb.IdentityUTR,
idCode: gb.IdentityTypeUTR,
initialCode: " 1234567890 ",
expectedCode: "1234567890",
expectedError: "",
},
{
name: "Validate valid UTR",
idKey: gb.IdentityUTR,
idCode: gb.IdentityTypeUTR,
initialCode: "1234567890",
expectedCode: "1234567890",
expectedError: "",
},
{
name: "Validate invalid UTR - starts with 0",
idKey: gb.IdentityUTR,
idCode: gb.IdentityTypeUTR,
initialCode: "0234567890",
expectedCode: "0234567890",
expectedError: "code: invalid UTR format.",
},
{
name: "Normalize NINO - to uppercase",
idKey: gb.IdentityNINO,
idCode: gb.IdentityTypeNINO,
initialCode: "ab123456c",
expectedCode: "AB123456C",
expectedError: "",
},
{
name: "Validate valid NINO",
idKey: gb.IdentityNINO,
idCode: gb.IdentityTypeNINO,
initialCode: "AB123456C",
expectedCode: "AB123456C",
expectedError: "",
},
{
name: "Validate invalid NINO - disallowed prefix",
idKey: gb.IdentityNINO,
idCode: gb.IdentityTypeNINO,
initialCode: "QQ123456Z",
expectedCode: "QQ123456Z",
expectedError: "code: invalid NINO format.",
},
{
name: "Validate invalid NINO - incorrect format",
idKey: gb.IdentityNINO,
idCode: gb.IdentityTypeNINO,
initialCode: "A123456C",
expectedCode: "A123456C",
expectedError: "code: invalid NINO format.",
Expand All @@ -71,7 +71,7 @@ func TestUKIdentifiers(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
id := &org.Identity{
Key: tt.idKey,
Type: tt.idCode,
Code: cbc.Code(tt.initialCode),
}

Expand Down
10 changes: 5 additions & 5 deletions regimes/pl/identities.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (
)

const (
// IdentityKeyTaxNumber represents the Polish tax number (PESEL). It is not
// IdentityTypePESEL represents the Polish tax number (PESEL). It is not
// required for invoices, but can be included for identification purposes.
IdentityKeyTaxNumber cbc.Key = "pl-tax-number"
IdentityTypePESEL cbc.Code = "PESEL"
)

// Reference: https://en.wikipedia.org/wiki/PESEL

var identityKeyDefinitions = []*cbc.KeyDefinition{
var identityTypeDefinitions = []*cbc.ValueDefinition{
{
Key: IdentityKeyTaxNumber,
Value: IdentityTypePESEL.String(),
Name: i18n.String{
i18n.EN: "Tax Number",
i18n.PL: "Numer podatkowy",
Expand All @@ -29,7 +29,7 @@ var identityKeyDefinitions = []*cbc.KeyDefinition{
}

func validateTaxNumber(id *org.Identity) error {
if id == nil || id.Key != IdentityKeyTaxNumber {
if id == nil || id.Type != IdentityTypePESEL {
return nil
}

Expand Down
12 changes: 6 additions & 6 deletions regimes/pl/identities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,39 @@ func TestValidateTaxNumber(t *testing.T) {
{
name: "Valid PESEL",
identity: &org.Identity{
Key: pl.IdentityKeyTaxNumber,
Type: pl.IdentityTypePESEL,
Code: cbc.Code("44051401359"), // Replace with an actual valid PESEL number
},
wantErr: false,
},
{
name: "Invalid length PESEL",
identity: &org.Identity{
Key: pl.IdentityKeyTaxNumber,
Type: pl.IdentityTypePESEL,
Code: cbc.Code("1234567890"), // Invalid PESEL with less than 11 digits
},
wantErr: true,
},
{
name: "Invalid checksum PESEL",
identity: &org.Identity{
Key: pl.IdentityKeyTaxNumber,
Type: pl.IdentityTypePESEL,
Code: cbc.Code("44051401358"), // Incorrect checksum
},
wantErr: true,
},
{
name: "Empty PESEL code",
identity: &org.Identity{
Key: pl.IdentityKeyTaxNumber,
Type: pl.IdentityTypePESEL,
Code: cbc.Code(""),
},
wantErr: false,
},
{
name: "Wrong Key Identity",
name: "Wrong Code Identity",
identity: &org.Identity{
Key: cbc.Key("wrong-key"),
Type: cbc.Code("NINO"),
Code: cbc.Code("44051401359"),
},
wantErr: false,
Expand Down
10 changes: 5 additions & 5 deletions regimes/pl/pl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func New() *tax.RegimeDef {
Tags: []*tax.TagSet{
common.InvoiceTags().Merge(invoiceTags),
},
Scenarios: scenarios, // scenarios.go
IdentityKeys: identityKeyDefinitions, // identities.go
Validator: Validate,
Normalizer: Normalize,
Categories: taxCategories, // tax_categories.go
Scenarios: scenarios, // scenarios.go
IdentityTypes: identityTypeDefinitions, // identities.go
Validator: Validate,
Normalizer: Normalize,
Categories: taxCategories, // tax_categories.go
Corrections: []*tax.CorrectionDefinition{
{
Schema: bill.ShortSchemaInvoice,
Expand Down

0 comments on commit 2905228

Please sign in to comment.