forked from invopop/gobl.fatturapa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payments.go
109 lines (91 loc) · 2.78 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
103
104
105
106
107
108
109
package fatturapa
import (
"fmt"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/pay"
"github.com/invopop/gobl/regimes/it"
)
// 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
DataScadenzaPagamento string `xml:",omitempty"`
ImportoPagamento string
}
func newDatiPagamento(inv *bill.Invoice) (*datiPagamento, error) {
if inv.Payment == nil || inv.Payment.Instructions == nil {
return nil, nil
}
dp, err := newDettalgioPagamento(inv)
if err != nil {
return nil, err
}
return &datiPagamento{
CondizioniPagamento: determinePaymentConditions(inv.Payment),
DettaglioPagamento: dp,
}, nil
}
func newDettalgioPagamento(inv *bill.Invoice) ([]*dettaglioPagamento, error) {
var dp []*dettaglioPagamento
payment := inv.Payment
codeModalitaPagamento, err := findCodeModalitaPagamento(payment.Instructions.Key)
if err != nil {
return nil, err
}
// 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,
DataScadenzaPagamento: 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 findCodeModalitaPagamento(key cbc.Key) (string, error) {
keyDef := findPaymentKeyDefinition(key)
if keyDef == nil {
return "", fmt.Errorf("ModalitaPagamento Code not found for payment method key '%s'", key)
}
code := keyDef.Map[it.KeyFatturaPAModalitaPagamento]
if code == "" {
return "", fmt.Errorf("ModalitaPagamento Code not found for payment method key '%s'", key)
}
return code.String(), nil
}
func findPaymentKeyDefinition(key cbc.Key) *cbc.KeyDefinition {
for _, keyDef := range regime.PaymentMeansKeys {
if key == keyDef.Key {
return keyDef
}
}
return nil
}
func determinePaymentConditions(payment *bill.Payment) string {
switch {
case payment.Terms == nil:
return condizioniPagamentoFull
case len(payment.Terms.DueDates) > 1:
return condizioniPagamentoInstallments
case payment.Terms.Key == pay.TermKeyAdvanced:
return condizioniPagamentoAdvance
default:
return condizioniPagamentoFull
}
}