-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransmission.go
87 lines (75 loc) · 2.42 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
package fatturapa
import (
"github.com/invopop/gobl"
"github.com/invopop/gobl/addons/it/sdi"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/l10n"
"github.com/invopop/gobl/org"
)
const (
formatoTrasmissioneFPA12 = "FPA12" // B2G
formatoTrasmissioneFPR12 = "FPR12" // B2B or B2C
)
// 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:"IdTrasmittente,omitempty"` // nolint:revive
ProgressivoInvio string `xml:"ProgressivoInvio,omitempty"`
FormatoTrasmissione string `xml:"FormatoTrasmissione,omitempty"`
CodiceDestinatario string `xml:"CodiceDestinatario"`
PECDestinatario string `xml:"PECDestinatario,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{
Country: c.Config.Transmitter.CountryCode,
Code: c.Config.Transmitter.TaxID,
}
dt.ProgressivoInvio = env.Head.UUID.String()[:8]
dt.FormatoTrasmissione = formatoTransmissione(inv)
}
return dt
}
func formatoTransmissione(inv *bill.Invoice) string {
if inv.Tax != nil && inv.Tax.Ext.Has(sdi.ExtKeyFormat) {
return inv.Tax.Ext[sdi.ExtKeyFormat].String()
}
// Default is always FPR12 for regular non-government invoices
return formatoTrasmissioneFPR12
}
func codiceDestinatario(cus *org.Party) string {
if cus != nil {
if cus.TaxID != nil && cus.TaxID.Country.Code() != l10n.IT {
return defaultCodiceDestinatarioForeignBusiness
}
for _, inbox := range cus.Inboxes {
if inbox.Key == sdi.KeyInboxCode {
return inbox.Code.String()
}
}
}
// 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 == sdi.KeyInboxPEC {
return inbox.Email
}
}
}
return ""
}