forked from invopop/gobl.fatturapa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransmission.go
89 lines (77 loc) · 2.3 KB
/
transmission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package fatturapa
import (
"github.com/invopop/gobl"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/l10n"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/regimes/it"
)
const (
formatoTrasmissioneFPA12 = "FPA12"
formatoTrasmissioneFPR12 = "FPR12"
)
// Invoices sent to Italian individuals or businesses can use 0000000 as the
// codice destinatario when it is not indicated explicitly.
// When the recipient is foreign, XXXXXXX is used.
const (
defaultCodiceDestinatarioItalianBusiness = "0000000"
defaultCodiceDestinatarioForeignBusiness = "XXXXXXX"
)
// Data related to the transmission of the invoice
type datiTrasmissione struct {
IdTrasmittente *taxID `xml:",omitempty"` // nolint:revive
ProgressivoInvio string `xml:",omitempty"`
FormatoTrasmissione string `xml:",omitempty"`
CodiceDestinatario string
PECDestinatario string `xml:",omitempty"`
}
func (c *Converter) newDatiTrasmissione(inv *bill.Invoice, env *gobl.Envelope) *datiTrasmissione {
dt := &datiTrasmissione{
CodiceDestinatario: codiceDestinatario(inv.Customer),
PECDestinatario: pecDestinatario(inv.Customer),
}
// Do we need to add the transmitter info?
if c.Config.Transmitter != nil {
dt.IdTrasmittente = &taxID{
IdPaese: c.Config.Transmitter.CountryCode,
IdCodice: c.Config.Transmitter.TaxID,
}
dt.ProgressivoInvio = env.Head.UUID.String()[:8]
dt.FormatoTrasmissione = formatoTransmissione(inv.Customer)
}
return dt
}
func formatoTransmissione(cus *org.Party) string {
if cus != nil {
taxID := cus.TaxID
if taxID != nil && taxID.Country == l10n.IT && taxID.Type == it.TaxIdentityTypeGovernment {
return formatoTrasmissioneFPA12
}
}
return formatoTrasmissioneFPR12
}
func codiceDestinatario(cus *org.Party) string {
if cus != nil {
if cus.TaxID != nil && cus.TaxID.Country != l10n.IT {
return defaultCodiceDestinatarioForeignBusiness
}
for _, inbox := range cus.Inboxes {
if inbox.Key == it.KeyInboxSDICode {
return inbox.Code
}
}
}
// When this is returned, we'll assume there is a PEC.
// This is also valid for individuals.
return defaultCodiceDestinatarioItalianBusiness
}
func pecDestinatario(cus *org.Party) string {
if cus != nil {
for _, inbox := range cus.Inboxes {
if inbox.Key == it.KeyInboxSDIPEC {
return inbox.Code
}
}
}
return ""
}