Skip to content

Commit

Permalink
Merge pull request #204 from invopop/es-it-corrections
Browse files Browse the repository at this point in the history
Refactor of correction changes and Italian addresses
  • Loading branch information
samlown authored Sep 21, 2023
2 parents e29015c + 0a6b092 commit 6972c0b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 26 deletions.
10 changes: 5 additions & 5 deletions bill/invoice_correct.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ func (inv *Invoice) CorrectionOptionsSchema() (interface{}, error) {
}
}

if len(cd.Keys) > 0 {
if len(cd.Changes) > 0 {
cos.Required = append(cos.Required, "changes")
if prop, ok := cos.Properties.Get("changes"); ok {
ps := prop.(orderedmap.OrderedMap)
items, _ := ps.Get("items")
pi := items.(orderedmap.OrderedMap)

oneOf := make([]*jsonschema.Schema, len(cd.Keys))
for i, v := range cd.Keys {
oneOf := make([]*jsonschema.Schema, len(cd.Changes))
for i, v := range cd.Changes {
oneOf[i] = &jsonschema.Schema{
Const: v.Key.String(),
Title: v.Name.String(),
Expand Down Expand Up @@ -346,12 +346,12 @@ func (inv *Invoice) validatePrecedingData(o *CorrectionOptions, cd *tax.Correcti
}
}

if len(cd.Keys) > 0 {
if len(cd.Changes) > 0 {
if len(pre.Changes) == 0 {
return errors.New("missing changes")
}
for _, k := range pre.Changes {
if !cd.HasKey(k) {
if !cd.HasChange(k) {
return fmt.Errorf("invalid change key: '%v'", k)
}
}
Expand Down
10 changes: 5 additions & 5 deletions regimes/es/corrections.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (

// correctionList contains an array of Key Definitions describing each of the acceptable
// correction keys, descriptions, and their "code" as determined by the FacturaE specifications.
var correctionList = []*tax.KeyDefinition{
var correctionChangesList = []*tax.KeyDefinition{
{
Key: CorrectionKeyCode,
Name: i18n.String{
Expand Down Expand Up @@ -259,10 +259,10 @@ var correctionMethodList = []*tax.KeyDefinition{
},
}

func correctionKeys() []interface{} {
keys := make([]interface{}, len(correctionList))
func correctionChangeKeys() []interface{} {
keys := make([]interface{}, len(correctionChangesList))
i := 0
for _, v := range correctionList {
for _, v := range correctionChangesList {
keys[i] = v.Key
i++
}
Expand All @@ -279,6 +279,6 @@ func correctionMethodKeys() []interface{} {
return keys
}

var isValidCorrectionKey = validation.In(correctionKeys()...)
var isValidCorrectionChangeKey = validation.In(correctionChangeKeys()...)

var isValidCorrectionMethodKey = validation.In(correctionMethodKeys()...)
2 changes: 1 addition & 1 deletion regimes/es/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func New() *tax.Regime {
Types: []cbc.Key{
bill.InvoiceTypeCorrective,
},
Keys: correctionList,
Methods: correctionMethodList,
Changes: correctionChangesList,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion regimes/es/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (v *invoiceValidator) preceding(value interface{}) error {
return validation.ValidateStruct(obj,
validation.Field(&obj.Changes,
validation.Required,
validation.Each(isValidCorrectionKey),
validation.Each(isValidCorrectionChangeKey),
),
validation.Field(&obj.CorrectionMethod,
validation.Required,
Expand Down
22 changes: 14 additions & 8 deletions regimes/it/invoice_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func (v *invoiceValidator) supplier(value interface{}) error {
tax.IdentityTypeIn(TaxIdentityTypeBusiness, TaxIdentityTypeGovernment),
),
validation.Field(&supplier.Addresses,
validation.Length(1, 1),
validation.By(validateAddress),
),
validation.Field(&supplier.Registration,
Expand All @@ -68,7 +67,7 @@ func (v *invoiceValidator) customer(value interface{}) error {
return validation.ValidateStruct(customer,
validation.Field(&customer.TaxID,
validation.When(
customer.TaxID.Country.In(l10n.IT),
isItalianParty(customer),
validation.Required,
tax.RequireIdentityCode,
tax.IdentityTypeIn(
Expand All @@ -79,24 +78,31 @@ func (v *invoiceValidator) customer(value interface{}) error {
),
),
validation.Field(&customer.Addresses,
validation.Length(1, 1),
validation.By(validateAddress),
validation.When(
isItalianParty(customer),
validation.By(validateAddress),
),
),
)
}

func isItalianParty(party *org.Party) bool {
if party == nil || party.TaxID == nil {
return false
}
return party.TaxID.Country.In(l10n.IT)
}

func validateAddress(value interface{}) error {
v, ok := value.([]*org.Address)
if v == nil || !ok {
return nil
}
// Post code and street in addition to the locality are required in Italian invoices.
address := v[0]
return validation.ValidateStruct(address,
validation.Field(&address.Country),
validation.Field(&address.Locality, validation.Required),
validation.Field(&address.Code, validation.Required),
validation.Field(&address.Street, validation.Required),
validation.Field(&address.Number, validation.Required),
validation.Field(&address.Code, validation.Required),
)

}
Expand Down
12 changes: 6 additions & 6 deletions tax/regime.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ type CorrectionDefinition struct {
Schema string `json:"schema" jsonschema:"title=Schema"`
// The types of sub-documents supported by the regime
Types []cbc.Key `json:"types,omitempty" jsonschema:"title=Types"`
// List of all the keys that can be used to identify a correction.
Keys []*KeyDefinition `json:"keys,omitempty" jsonschema:"title=Keys"`
// Methods describe the methods used to correct an invoice.
Methods []*KeyDefinition `json:"methods,omitempty" jsonschema:"title=Methods"`
// List of change keys that can be used to describe what has been corrected.
Changes []*KeyDefinition `json:"changes,omitempty" jsonschema:"title=Changes"`
// ReasonRequired when true implies that a reason must be provided
ReasonRequired bool `json:"reason_required,omitempty" jsonschema:"title=Reason Required"`
// Stamps that must be copied from the preceding document.
Expand Down Expand Up @@ -651,13 +651,13 @@ func (cd *CorrectionDefinition) HasType(t cbc.Key) bool {
return t.In(cd.Types...)
}

// HasKey returns true if the correction definition has the keys provided.
func (cd *CorrectionDefinition) HasKey(key cbc.Key) bool {
// HasChange returns true if the correction definition has the change key provided.
func (cd *CorrectionDefinition) HasChange(key cbc.Key) bool {
if cd == nil {
return false // no correction definitions
}

for _, kd := range cd.Keys {
for _, kd := range cd.Changes {
if kd.Key == key {
return true
}
Expand All @@ -684,8 +684,8 @@ func (cd *CorrectionDefinition) Validate() error {
validation.Field(&cd.Schema, validation.Required),
validation.Field(&cd.Types),
validation.Field(&cd.Stamps),
validation.Field(&cd.Keys),
validation.Field(&cd.Methods),
validation.Field(&cd.Changes),
)
return err
}
Expand Down

0 comments on commit 6972c0b

Please sign in to comment.