-
Notifications
You must be signed in to change notification settings - Fork 2
/
payments.go
102 lines (86 loc) · 2.74 KB
/
payments.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
90
91
92
93
94
95
96
97
98
99
100
101
102
package fatturapa
import (
"fmt"
"github.com/invopop/gobl/addons/it/sdi"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/pay"
)
// datiPagamento contains all data related to the payment of the document.
type datiPagamento struct {
CondizioniPagamento string
DettaglioPagamento []*dettaglioPagamento
}
// dettaglioPagamento contains data related to a single payment.
type dettaglioPagamento struct {
ModalitaPagamento string
Date string `xml:"DataRiferimentoTerminiPagamento,omitempty"`
DueDate string `xml:"DataScadenzaPagamento,omitempty"`
ImportoPagamento string
}
func newDatiPagamento(inv *bill.Invoice) (*datiPagamento, error) {
if inv.Payment == nil {
return nil, nil
}
dp, err := preparePaymentDetails(inv)
if err != nil {
return nil, err
}
return &datiPagamento{
CondizioniPagamento: determinePaymentConditions(inv),
DettaglioPagamento: dp,
}, nil
}
func preparePaymentDetails(inv *bill.Invoice) ([]*dettaglioPagamento, error) {
var dp []*dettaglioPagamento
payment := inv.Payment
if len(payment.Advances) == 0 && payment.Instructions == nil {
return nil, fmt.Errorf("missing payment advances or instructions")
}
// First deal with payment advances
for _, advance := range payment.Advances {
row := &dettaglioPagamento{
ModalitaPagamento: advance.Ext[sdi.ExtKeyPaymentMeans].String(),
ImportoPagamento: formatAmount(&advance.Amount),
}
if advance.Date != nil {
row.Date = advance.Date.String()
}
dp = append(dp, row)
}
if payment.Instructions == nil {
// No instructions, ignore anything else
return dp, nil
}
codeModalitaPagamento := payment.Instructions.Ext[sdi.ExtKeyPaymentMeans].String()
// First check if there are multiple due dates, and if so, create a
// DettaglioPagamento for each one.
if terms := payment.Terms; terms != nil {
for _, dueDate := range payment.Terms.DueDates {
dp = append(dp, &dettaglioPagamento{
ModalitaPagamento: codeModalitaPagamento,
DueDate: dueDate.Date.String(), // ISO 8601 YYYY-MM-DD format
ImportoPagamento: formatAmount(&dueDate.Amount),
})
}
}
// If there are no due dates, then a single DettaglioPagamento is created
// with the total payable amount.
if len(dp) == 0 {
dp = append(dp, &dettaglioPagamento{
ModalitaPagamento: codeModalitaPagamento,
ImportoPagamento: formatAmount(&inv.Totals.Payable),
})
}
return dp, nil
}
func determinePaymentConditions(inv *bill.Invoice) string {
p := inv.Payment
switch {
case inv.Totals.Paid() || (p.Terms != nil && p.Terms.Key == pay.TermKeyAdvanced):
return condizioniPagamentoAdvance
case len(p.Advances) > 0 || (p.Terms != nil && len(p.Terms.DueDates) > 1):
return condizioniPagamentoInstallments
default:
return condizioniPagamentoFull
}
}