Skip to content

Commit

Permalink
Use credit-note instead of corrective invoices
Browse files Browse the repository at this point in the history
Poland doesn't have a distinction between credit notes and
corrective invoices. Our implementation of invoice correction
is closer to what we see in credit notes in other countries.
For example we have differential values for line items.
  • Loading branch information
noplisu committed Mar 27, 2024
1 parent 47507cb commit e0ffb56
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 149 deletions.
3 changes: 2 additions & 1 deletion corrected.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/invopop/gobl/head"
)

// CorrectedInv defines the XML structure for KSeF correction invoiceą
// CorrectedInv defines the XML structure for KSeF correction invoice
type CorrectedInv struct {
IssueDate string `xml:"DataWystFaKorygowanej,omitempty"`
SequentialNumber string `xml:"NrFaKorygowanej,omitempty"`
Expand All @@ -14,6 +14,7 @@ type CorrectedInv struct {
KsefNumber string `xml:"NrKSeFFaKorygowanej,omitempty"`
}

// NewCorrectedInv gets credit note data from GOBL invoice
func NewCorrectedInv(prc *bill.Preceding) *CorrectedInv {
inv := &CorrectedInv{
SequentialNumber: invoiceNumber(prc.Series, prc.Code),
Expand Down
4 changes: 2 additions & 2 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func NewInv(inv *bill.Invoice) *Inv {
for _, prc := range inv.Preceding {
Inv.CorrectedInv = NewCorrectedInv(prc)
Inv.CorrectionReason = prc.Reason
if prc.Ext.Has(pl.ExtKeyKSEFCorrection) {
Inv.CorrectionType = prc.Ext[pl.ExtKeyKSEFCorrection].Code().String()
if prc.Ext.Has(pl.ExtKeyKSeFEffectiveDate) {

Check failure on line 98 in invoice.go

View workflow job for this annotation

GitHub Actions / Test

prc.Ext undefined (type *bill.Preceding has no field or method Ext)

Check failure on line 98 in invoice.go

View workflow job for this annotation

GitHub Actions / Test

undefined: pl.ExtKeyKSeFEffectiveDate

Check failure on line 98 in invoice.go

View workflow job for this annotation

GitHub Actions / golangci-lint

prc.Ext undefined (type *bill.Preceding has no field or method Ext) (typecheck)
Inv.CorrectionType = prc.Ext[pl.ExtKeyKSeFEffectiveDate].Code().String()

Check failure on line 99 in invoice.go

View workflow job for this annotation

GitHub Actions / Test

prc.Ext undefined (type *bill.Preceding has no field or method Ext)

Check failure on line 99 in invoice.go

View workflow job for this annotation

GitHub Actions / Test

undefined: pl.ExtKeyKSeFEffectiveDate

Check failure on line 99 in invoice.go

View workflow job for this annotation

GitHub Actions / golangci-lint

prc.Ext undefined (type *bill.Preceding has no field or method Ext) (typecheck)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestNewInv(t *testing.T) {
Preceding: []*bill.Preceding{
{
Ext: tax.Extensions{

Check failure on line 72 in invoice_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: tax.Extensions (typecheck)
pl.ExtKeyKSEFCorrection: "1",
pl.ExtKeyKSeFEffectiveDate: "1",
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions ksef_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func TestNewDocument(t *testing.T) {
assert.Equal(t, output, data)
})

t.Run("should return bytes of the corrective invoice", func(t *testing.T) {
doc, err := test.NewDocumentFrom("corrective-invoice.json")
t.Run("should return bytes of the credit-note invoice", func(t *testing.T) {
doc, err := test.NewDocumentFrom("credit-note.json")
require.NoError(t, err)

data, err := doc.Bytes()
require.NoError(t, err)

output, err := test.LoadOutputFile("corrective-invoice.xml")
output, err := test.LoadOutputFile("credit-note.xml")
require.NoError(t, err)

assert.Equal(t, output, data)
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestNewDocument(t *testing.T) {
assert.Nil(t, validation)
})

t.Run("should generate valid correction invoice", func(t *testing.T) {
t.Run("should generate valid credit-note", func(t *testing.T) {
err := xsdvalidate.Init()
require.NoError(t, err)
defer xsdvalidate.Cleanup()
Expand All @@ -84,7 +84,7 @@ func TestNewDocument(t *testing.T) {
require.NoError(t, err)
defer xsdhandler.Free()

doc, err := test.NewDocumentFrom("corrective-invoice.json")
doc, err := test.NewDocumentFrom("credit-note.json")
require.NoError(t, err)

data, err := doc.Bytes()
Expand Down
11 changes: 0 additions & 11 deletions lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,13 @@ func newLine(line *bill.Line) *Line {
Measure: string(line.Item.Unit.UNECE()),
NetUnitPrice: line.Item.Price.String(),
Quantity: line.Quantity.String(),
UnitDiscount: unitDiscount(line),
NetPriceTotal: line.Total.String(),
TaxRate: line.Taxes[0].Percent.Rescale(2).StringWithoutSymbol(),
}

return Line
}

func unitDiscount(line *bill.Line) string {
if line.Total == line.Sum {
return ""
}

discount := line.Total.Subtract(line.Sum).Divide(line.Quantity) // not sure if there should be some rescale or matchPrecision

return discount.String()
}

// NewLines generates lines for the KSeF invoice
func NewLines(lines []*bill.Line) []*Line {
var Lines []*Line
Expand Down
3 changes: 1 addition & 2 deletions payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/regimes/pl"
"github.com/invopop/gobl/tax"
)

// AdvancePayment defines the XML structure for KSeF advance payments
Expand Down Expand Up @@ -126,7 +125,7 @@ func findPaymentMeansCode(key cbc.Key) (string, error) {
return code.String(), nil
}

func findPaymentKeyDefinition(key cbc.Key) *tax.KeyDefinition {
func findPaymentKeyDefinition(key cbc.Key) *cbc.KeyDefinition {

Check failure on line 128 in payments.go

View workflow job for this annotation

GitHub Actions / Test

undefined: cbc.KeyDefinition

Check failure on line 128 in payments.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: cbc.KeyDefinition (typecheck)
for _, keyDef := range regime.PaymentMeansKeys {
if key == keyDef.Key {
return keyDef
Expand Down
2 changes: 1 addition & 1 deletion regime.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import (
"github.com/invopop/gobl/tax"
)

var regime = tax.RegimeFor(l10n.PL, l10n.CodeEmpty)
var regime = tax.RegimeFor(l10n.PL)

Check failure on line 8 in regime.go

View workflow job for this annotation

GitHub Actions / Test

not enough arguments in call to tax.RegimeFor

Check failure on line 8 in regime.go

View workflow job for this annotation

GitHub Actions / golangci-lint

not enough arguments in call to tax.RegimeFor
120 changes: 0 additions & 120 deletions test/data/corrective-invoice.json

This file was deleted.

120 changes: 120 additions & 0 deletions test/data/credit-note.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"$schema": "https://gobl.org/draft-0/envelope",
"head": {
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
"dig": {
"alg": "sha256",
"val": "2dfc5c1c3b11a69e10ba47256e5a1655f5c6dfca99ad6b0390a987588020bbb2"
},
"draft": true
},
"doc": {
"$schema": "https://gobl.org/draft-0/bill/invoice",
"type": "credit-note",
"series": "CN",
"code": "002",
"issue_date": "2023-12-21",
"currency": "PLN",
"preceding": [
{
"type": "standard",
"series": "SAMPLE",
"code": "001",
"issue_date": "2023-12-20",
"reason": "Special Discount",
"stamps": [
{
"prv": "ksef-id",
"val": "9876543210-20231220-107FDF72DB53-F7"
}
],
"ext": {
"pl-ksef-effective-date": "2"
}
}
],
"supplier": {
"name": "Provide One S.L.",
"tax_id": {
"country": "PL",
"code": "9876543210"
},
"addresses": [
{
"num": "42",
"street": "Calle Pradillo",
"locality": "Madrid",
"region": "Madrid",
"code": "00-015",
"country": "PL"
}
],
"emails": [
{
"addr": "[email protected]"
}
]
},
"customer": {
"name": "Sample Consumer",
"tax_id": {
"country": "PL",
"code": "1234567788"
},
"addresses": [
{
"num": "43",
"street": "Calle Pradillo",
"locality": "Madrid",
"region": "Madrid",
"code": "00-015",
"country": "PL"
}
]
},
"lines": [
{
"i": 1,
"quantity": "20",
"item": {
"name": "Development services",
"price": "10.00",
"unit": "h"
},
"sum": "200.00",
"taxes": [
{
"cat": "VAT",
"rate": "standard",
"percent": "23.0%"
}
],
"total": "200.00"
}
],
"totals": {
"sum": "200.00",
"total": "200.00",
"taxes": {
"categories": [
{
"code": "VAT",
"rates": [
{
"key": "standard",
"base": "200.00",
"percent": "23.0%",
"amount": "46.00"
}
],
"amount": "46.00"
}
],
"sum": "46.00"
},
"tax": "46.00",
"total_with_tax": "246.00",
"payable": "246.00"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
<Fa>
<KodWaluty>PLN</KodWaluty>
<P_1>2023-12-21</P_1>
<P_2>COR-002</P_2>
<P_13_1>-200.00</P_13_1>
<P_14_1>-46.00</P_14_1>
<P_15>-246.00</P_15>
<P_2>CN-002</P_2>
<P_13_1>200.00</P_13_1>
<P_14_1>46.00</P_14_1>
<P_15>246.00</P_15>
<Adnotacje>
<P_16>2</P_16>
<P_17>2</P_17>
Expand Down Expand Up @@ -68,8 +68,8 @@
<P_7>Development services</P_7>
<P_8A>HUR</P_8A>
<P_8B>20</P_8B>
<P_9A>-10.00</P_9A>
<P_11>-200.00</P_11>
<P_9A>10.00</P_9A>
<P_11>200.00</P_11>
<P_12>23</P_12>
</FaWiersz>
</Fa>
Expand Down

0 comments on commit e0ffb56

Please sign in to comment.