Skip to content

Commit 80a0696

Browse files
authored
Merge pull request #52 from toudi/feature/abstract-jpk-support
Feature/abstract jpk support
2 parents 59eb16c + ce2f39a commit 80a0696

42 files changed

Lines changed: 1143 additions & 623 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/ksef/commands/invoices/jpk/dump.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package jpk
33
import (
44
"errors"
55
"fmt"
6-
"ksef/internal/invoicesdb/jpk"
6+
"ksef/internal/invoicesdb/jpk/manager"
77
monthlyregistry "ksef/internal/invoicesdb/monthly-registry"
88
"ksef/internal/invoicesdb/shared"
99

@@ -34,9 +34,9 @@ func initJPKManagerFromInvoiceFile(cmd *cobra.Command, args []string) error {
3434
return err
3535
}
3636

37-
jpkManager, err = jpk.Manager(
37+
jpkManager, err = manager.Manager(
3838
vip,
39-
jpk.WithMonthlyRegistry(invoiceRegistry),
39+
manager.WithMonthlyRegistry(invoiceRegistry),
4040
)
4141
if err != nil {
4242
return err

cmd/ksef/commands/invoices/jpk/variables.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package jpk
22

33
import (
4-
"ksef/internal/invoicesdb/jpk"
4+
"ksef/internal/invoicesdb/jpk/manager"
55
monthlyregistry "ksef/internal/invoicesdb/monthly-registry"
66
)
77

88
var (
9-
jpkManager *jpk.JPKManager
9+
jpkManager *manager.JPKManager
1010
invoiceChecksum string
1111
invoice *monthlyregistry.Invoice
1212
xmlInvoice *monthlyregistry.XMLInvoice
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package purchase
2+
3+
import (
4+
"errors"
5+
"ksef/internal/invoicesdb/jpk/abstract/processors/vat"
6+
"ksef/internal/invoicesdb/jpk/abstract/types"
7+
"ksef/internal/invoicesdb/jpk/manager"
8+
jpkTypes "ksef/internal/invoicesdb/jpk/types"
9+
monthlyregistry "ksef/internal/invoicesdb/monthly-registry"
10+
"ksef/internal/invoicesdb/shared"
11+
"ksef/internal/logging"
12+
13+
"github.com/beevik/etree"
14+
)
15+
16+
const (
17+
xpathItem = "//Faktura/Fa/FaWiersz"
18+
)
19+
20+
var errUnableToCalculateVAT = errors.New("unable to calculate VAT for invoice item")
21+
22+
func AggregateItems(
23+
manager *manager.JPKManager,
24+
invoice *monthlyregistry.Invoice,
25+
doc *etree.Document,
26+
purchase *types.PurchaseItem,
27+
) error {
28+
items := doc.FindElements(xpathItem)
29+
30+
vatCalculator := &vat.Calculator{}
31+
32+
logger := logging.JPKLogger.With("faktura zakupowa", invoice.RefNo)
33+
34+
for _, item := range items {
35+
itemHash := itemHashFromXML(item)
36+
// check if item should be excluded from the report
37+
if manager.ItemHasRule(invoice, itemHash, func(rule shared.JPKItemRule) bool { return rule.Exclude }) {
38+
logger.Info("przedmiot faktury ma flagę wyłączenia z raportu", "hash", itemHash)
39+
continue
40+
}
41+
42+
vatInfo, err := vatCalculator.GetVat(item)
43+
if err != nil {
44+
return errors.Join(errUnableToCalculateVAT, err)
45+
}
46+
47+
// apply 50% VAT if applicable
48+
if manager.ItemHasRule(invoice, itemHash, func(rule shared.JPKItemRule) bool { return rule.Vat50Percent }) {
49+
logger.Info("przedmiot faktury ma flagę zastosowania 50% VAT", "hash", itemHash)
50+
vatInfo.VatAmount = vatInfo.VatAmount.HalveAndRoundUp()
51+
}
52+
53+
purchaseAttributes := jpkTypes.PurchaseAttributes{}
54+
vatRate, exists := jpkTypes.VatRates[vatInfo.VatRate]
55+
if !exists {
56+
return errors.New("unknown VAT rate")
57+
}
58+
59+
purchaseAttributes.VatRate = vatRate
60+
// decide if this is a fixed asset or not
61+
if manager.ItemHasRule(invoice, itemHash, func(rule shared.JPKItemRule) bool { return rule.FixedAsset }) {
62+
logger.Info("przedmiot faktury ma flagę środków trwałych", "hash", itemHash)
63+
purchaseAttributes.FixedAssets = true
64+
}
65+
66+
purchase.AddAmount(purchaseAttributes, *vatInfo)
67+
}
68+
69+
return nil
70+
}
71+
72+
const (
73+
pathName = "P_7"
74+
pathIndex = "Indeks"
75+
pathGTIN = "GTIN"
76+
pathPKWiU = "PKWiU"
77+
)
78+
79+
func itemHashFromXML(node *etree.Element) shared.ItemHash {
80+
var hash shared.ItemHash
81+
82+
if nodeName := node.FindElement(pathName); nodeName != nil {
83+
hash.Name = nodeName.Text()
84+
}
85+
if nodeIndex := node.FindElement(pathName); nodeIndex != nil {
86+
hash.Index = nodeIndex.Text()
87+
}
88+
if nodeGTIN := node.FindElement(pathGTIN); nodeGTIN != nil {
89+
hash.GTIN = nodeGTIN.Text()
90+
}
91+
if nodePKWiU := node.FindElement(pathPKWiU); nodePKWiU != nil {
92+
hash.PKWiU = nodePKWiU.Text()
93+
}
94+
95+
return hash
96+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package purchase
2+
3+
import (
4+
"ksef/internal/invoicesdb/jpk/abstract/types"
5+
"ksef/internal/invoicesdb/jpk/manager"
6+
monthlyregistry "ksef/internal/invoicesdb/monthly-registry"
7+
8+
"github.com/beevik/etree"
9+
)
10+
11+
func ExtractRefNos(
12+
manager *manager.JPKManager,
13+
invoice *monthlyregistry.Invoice,
14+
doc *etree.Document,
15+
purchase *types.PurchaseItem,
16+
) error {
17+
purchase.RefNo = invoice.RefNo
18+
purchase.KSeFRefNo = invoice.KSeFRefNo
19+
return nil
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package purchase
2+
3+
import (
4+
"ksef/internal/invoicesdb/jpk/abstract/types"
5+
"ksef/internal/invoicesdb/jpk/manager"
6+
monthlyregistry "ksef/internal/invoicesdb/monthly-registry"
7+
8+
"github.com/beevik/etree"
9+
)
10+
11+
const (
12+
xpathIssued = "//Faktura/Fa/P_1"
13+
xpathIssuerNIP = "//Faktura/Podmiot1/DaneIdentyfikacyjne/NIP"
14+
xpathIssuerName = "//Faktura/Podmiot1/DaneIdentyfikacyjne/Nazwa"
15+
)
16+
17+
func ExtractSeller(
18+
manager *manager.JPKManager,
19+
invoice *monthlyregistry.Invoice,
20+
doc *etree.Document,
21+
purchase *types.PurchaseItem,
22+
) error {
23+
purchase.Date = doc.FindElement(xpathIssued).Text()
24+
purchase.Seller.NIP = doc.FindElement(xpathIssuerNIP).Text()
25+
purchase.Seller.Name = doc.FindElement(xpathIssuerName).Text()
26+
return nil
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package sale
2+
3+
import (
4+
"errors"
5+
"ksef/internal/invoicesdb/jpk/abstract/processors/vat"
6+
"ksef/internal/invoicesdb/jpk/abstract/types"
7+
monthlyregistry "ksef/internal/invoicesdb/monthly-registry"
8+
9+
"github.com/beevik/etree"
10+
)
11+
12+
const (
13+
xpathItem = "//Faktura/Fa/FaWiersz"
14+
)
15+
16+
var (
17+
errUnableToCalculateVAT = errors.New("unable to calculate VAT for invoice item")
18+
errUnrecognizedVatRate = errors.New("unrecognizable VAT rate")
19+
)
20+
21+
func AggregateAmountsByVATRate(
22+
invoice *monthlyregistry.Invoice,
23+
doc *etree.Document,
24+
salesRow *types.SaleItem,
25+
) error {
26+
items := doc.FindElements(xpathItem)
27+
28+
vatCalculator := &vat.Calculator{}
29+
30+
for _, item := range items {
31+
vatInfo, err := vatCalculator.GetVat(item)
32+
if err != nil {
33+
return errors.Join(errUnableToCalculateVAT, err)
34+
}
35+
36+
if err := salesRow.AddAmount(vatInfo); err != nil {
37+
return err
38+
}
39+
}
40+
41+
return nil
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package sale
2+
3+
import (
4+
"ksef/internal/invoicesdb/jpk/abstract/types"
5+
monthlyregistry "ksef/internal/invoicesdb/monthly-registry"
6+
7+
"github.com/beevik/etree"
8+
)
9+
10+
const (
11+
recipientPrefix = "//Faktura/Podmiot2/"
12+
recipientNIP = recipientPrefix + "DaneIdentyfikacyjne/NIP"
13+
recipientName = recipientPrefix + "DaneIdentyfikacyjne/Nazwa"
14+
)
15+
16+
func ExtractBuyer(
17+
invoice *monthlyregistry.Invoice,
18+
doc *etree.Document,
19+
salesRow *types.SaleItem,
20+
) error {
21+
recipientID := "BRAK"
22+
recipientNIPNode := doc.FindElement(recipientNIP)
23+
24+
if recipientNIPNode != nil {
25+
recipientID = recipientNIPNode.Text()
26+
}
27+
28+
salesRow.Buyer.NIP = recipientID
29+
salesRow.Buyer.Name = doc.FindElement(recipientName).Text()
30+
31+
return nil
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package sale
2+
3+
import (
4+
"ksef/internal/invoicesdb/jpk/abstract/types"
5+
monthlyregistry "ksef/internal/invoicesdb/monthly-registry"
6+
7+
"github.com/beevik/etree"
8+
)
9+
10+
const (
11+
xpathIssued = "//Faktura/Fa/P_1"
12+
xpathSale = "//Faktura/Fa/P_6"
13+
)
14+
15+
func ExtractDates(
16+
invoice *monthlyregistry.Invoice,
17+
doc *etree.Document,
18+
salesRow *types.SaleItem,
19+
) error {
20+
salesRow.IssueDate = doc.FindElement(xpathIssued).Text()
21+
salesRow.SaleDate = doc.FindElement(xpathSale).Text()
22+
return nil
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sale
2+
3+
import (
4+
"ksef/internal/invoicesdb/jpk/abstract/types"
5+
monthlyregistry "ksef/internal/invoicesdb/monthly-registry"
6+
7+
"github.com/beevik/etree"
8+
)
9+
10+
func ExtractRefNos(
11+
invoice *monthlyregistry.Invoice,
12+
doc *etree.Document,
13+
salesRow *types.SaleItem,
14+
) error {
15+
salesRow.RefNo = invoice.RefNo
16+
salesRow.KSeFRefNo = invoice.KSeFRefNo
17+
return nil
18+
}

internal/invoicesdb/jpk/processors/vat/calculator.go renamed to internal/invoicesdb/jpk/abstract/processors/vat/calculator.go

File renamed without changes.

0 commit comments

Comments
 (0)