Skip to content
12 changes: 8 additions & 4 deletions addons/eu/en16931/bill.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,23 @@ func billDiscountRules() *rules.Set {

func billLineDiscountRules() *rules.Set {
return rules.For(new(bill.LineDiscount),
// BR-41: either reason or allowance type extension required
rules.Assert("01", "either a reason or an allowance type extension is required (BR-41)",
// BR-42: either reason or allowance type extension required
rules.Assert("01", "either a reason or an allowance type extension is required (BR-42)",
is.Func("reason or allowance", billLineDiscountHasReasonOrAllowance),
),
)
}

func billChargeRules() *rules.Set {
return rules.For(new(bill.Charge),
// BR-36: either reason or charge type extension required
rules.Assert("01", "either a reason or a charge type extension is required (BR-36)",
// BR-38: either reason or charge type extension required
rules.Assert("01", "either a reason or a charge type extension is required (BR-38)",
is.Func("reason or charge", billChargeHasReasonOrExt),
),
// BR-37: taxes are required on document-level charges
rules.Field("taxes",
rules.Assert("02", "taxes are required (BR-37)", is.Present),
),
)
}

Expand Down
39 changes: 39 additions & 0 deletions addons/eu/en16931/bill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ func TestValidateBillCharge(t *testing.T) {
l := &bill.Charge{
Reason: "Product sample",
Amount: num.MakeAmount(100, 2),
Taxes: standardVATSet(),
}
err := rules.Validate(l, tax.AddonContext(en16931.V2017))
assert.NoError(t, err)
Expand All @@ -550,6 +551,7 @@ func TestValidateBillCharge(t *testing.T) {
untdid.ExtKeyCharge: "AAE",
}),
Amount: num.MakeAmount(100, 2),
Taxes: standardVATSet(),
}
err := rules.Validate(l, tax.AddonContext(en16931.V2017))
assert.NoError(t, err)
Expand All @@ -558,6 +560,7 @@ func TestValidateBillCharge(t *testing.T) {
t.Run("without reason or extension", func(t *testing.T) {
l := &bill.Charge{
Amount: num.MakeAmount(100, 2),
Taxes: standardVATSet(),
}
err := rules.Validate(l, tax.AddonContext(en16931.V2017))
assert.ErrorContains(t, err, "either a reason or a charge type extension is required")
Expand All @@ -570,10 +573,46 @@ func TestValidateBillCharge(t *testing.T) {
untdid.ExtKeyCharge: "AAE",
}),
Amount: num.MakeAmount(100, 2),
Taxes: standardVATSet(),
}
err := rules.Validate(l, tax.AddonContext(en16931.V2017))
assert.NoError(t, err)
})

t.Run("without taxes (BR-37)", func(t *testing.T) {
l := &bill.Charge{
Reason: "Freight",
Amount: num.MakeAmount(100, 2),
}
err := rules.Validate(l, tax.AddonContext(en16931.V2017))
assert.ErrorContains(t, err, "GOBL-EU-EN16931-BILL-CHARGE-02")
assert.ErrorContains(t, err, "taxes are required (BR-37)")
})

t.Run("without taxes outside EN 16931", func(t *testing.T) {
l := &bill.Charge{
Reason: "Freight",
Amount: num.MakeAmount(100, 2),
}
err := rules.Validate(l)
if err != nil {
assert.NotContains(t, err.Error(), "BR-37")
}
Comment thread
miguelalvarezd marked this conversation as resolved.
Resolved
})
}

// standardVATSet is a tax set with a single standard-rated VAT combo, as
// EN 16931 requires on document-level discounts and charges (BR-32, BR-37).
func standardVATSet() tax.Set {
return tax.Set{
{
Category: tax.CategoryVAT,
Rate: "standard",
Ext: tax.ExtensionsOf(cbc.CodeMap{
untdid.ExtKeyTaxCategory: en16931.TaxCategoryStandard,
}),
},
}
}

func TestValidateBillLineDiscount(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions changes/unreleased/address-street-third.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Added

- `org`: `street_third` on addresses, for a third line of street details (EN 16931 BT-162, BT-163, BT-164 and BT-165), with `Address.LineThree()` to read it.
7 changes: 7 additions & 0 deletions changes/unreleased/en16931-charge-taxes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Changed

- `eu-en16931-v2017`: **breaking**: document-level charges must have taxes (BR-37), as discounts already must (BR-32). Charges without taxes now fail with `GOBL-EU-EN16931-BILL-CHARGE-02`. Informative charges fail too, as they can't have taxes and EN 16931 has no way to express them.

## Fixed

- `eu-en16931-v2017`: the charge and line discount reason rules cite BR-38 and BR-42, not BR-36 (charge amount) and BR-41 (line allowance amount). Fault codes are unchanged.
16 changes: 14 additions & 2 deletions data/rules/eu-en16931.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"assert": [
{
"id": "GOBL-EU-EN16931-BILL-LINEDISCOUNT-01",
"desc": "either a reason or an allowance type extension is required (BR-41)",
"desc": "either a reason or an allowance type extension is required (BR-42)",
"tests": "reason or allowance"
}
]
Expand All @@ -145,9 +145,21 @@
"assert": [
{
"id": "GOBL-EU-EN16931-BILL-CHARGE-01",
"desc": "either a reason or a charge type extension is required (BR-36)",
"desc": "either a reason or a charge type extension is required (BR-38)",
"tests": "reason or charge"
}
],
"subsets": [
{
"field": "taxes",
"assert": [
{
"id": "GOBL-EU-EN16931-BILL-CHARGE-02",
"desc": "taxes are required (BR-37)",
"tests": "present"
}
]
}
]
},
{
Expand Down
5 changes: 5 additions & 0 deletions data/schemas/org/address.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"title": "Extended Street",
"description": "Additional street address details."
},
Comment thread
miguelalvarezd marked this conversation as resolved.
Resolved
"street_third": {
"type": "string",
"title": "Third Street Line",
"description": "Third line of street details, for addresses that need more than two."
},
"locality": {
"type": "string",
"title": "Locality",
Expand Down
9 changes: 9 additions & 0 deletions org/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Address struct {
Street string `json:"street,omitempty" jsonschema:"title=Street"`
// Additional street address details.
StreetExtra string `json:"street_extra,omitempty" jsonschema:"title=Extended Street"`
Comment thread
miguelalvarezd marked this conversation as resolved.
Resolved
// Third line of street details, for addresses that need more than two.
StreetThird string `json:"street_third,omitempty" jsonschema:"title=Third Street Line"`
// Name of a village, town, district, or city, typically inside a region.
Locality string `json:"locality,omitempty" jsonschema:"title=Locality"`
// Name of a city, province, county, or state, inside a country.
Expand Down Expand Up @@ -82,6 +84,12 @@ func (a *Address) LineTwo() string {
return a.StreetExtra
}

// LineThree returns the third line of the address which for GOBL
// is the `StreetThird` field.
func (a *Address) LineThree() string {
return a.StreetThird
}

// CompleteNumber will combine all the number related details, such
// as number, block, floor, and door, into a single string separated
// by spaces.
Expand Down Expand Up @@ -116,6 +124,7 @@ func normalizeAddress(a *Address) {
a.Door = cbc.NormalizeString(a.Door)
a.Street = cbc.NormalizeString(a.Street)
a.StreetExtra = cbc.NormalizeString(a.StreetExtra)
a.StreetThird = cbc.NormalizeString(a.StreetThird)
a.Locality = cbc.NormalizeString(a.Locality)
a.Region = cbc.NormalizeString(a.Region)
a.State = cbc.NormalizeAlphanumericalCode(a.State)
Expand Down
11 changes: 11 additions & 0 deletions org/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestAddressNormalize(t *testing.T) {
Door: " 1 ",
Street: " Main St. ",
StreetExtra: " Apt. 3 ",
StreetThird: " West Wing ",
Locality: " Town ",
Region: " City ",
State: " MAD ",
Expand All @@ -40,6 +41,7 @@ func TestAddressNormalize(t *testing.T) {
assert.Equal(t, "1", a.Door)
assert.Equal(t, "Main St.", a.Street)
assert.Equal(t, "Apt. 3", a.StreetExtra)
assert.Equal(t, "West Wing", a.StreetThird)
assert.Equal(t, "Town", a.Locality)
assert.Equal(t, "City", a.Region)
assert.Equal(t, "MAD", a.State.String())
Expand Down Expand Up @@ -129,6 +131,15 @@ func TestAddressLineTwo(t *testing.T) {
})
}

func TestAddressLineThree(t *testing.T) {
a := &org.Address{
Street: "Main St.",
StreetExtra: "Apt. 3",
StreetThird: "West Wing",
}
assert.Equal(t, "West Wing", a.LineThree())
}

func TestAddressCountryCase(t *testing.T) {
t.Run("lowercase country is uppercased", func(t *testing.T) {
a := &org.Address{
Expand Down
Loading