-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpayments.go
113 lines (97 loc) · 3.08 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
110
111
112
113
package fatturapa
import (
"fmt"
"github.com/invopop/gobl/addons/it/sdi"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/pay"
)
// paymentData contains all data related to the payment of the document.
type paymentData struct {
Conditions string `xml:"CondizioniPagamento"`
Payments []*paymentDetailRow `xml:"DettaglioPagamento,omitempty"`
}
// paymentDetailRow contains data related to a single payment.
type paymentDetailRow struct {
Beneficiary string `xml:"Beneficiario,omitempty"`
Method string `xml:"ModalitaPagamento"`
Date string `xml:"DataRiferimentoTerminiPagamento,omitempty"`
Days int64 `xml:"GiorniTerminiPagamento,omitempty"`
DueDate string `xml:"DataScadenzaPagamento,omitempty"`
Amount string `xml:"ImportoPagamento"`
IBAN string `xml:"IBAN,omitempty"`
ABI string `xml:"ABI,omitempty"`
CAB string `xml:"CAB,omitempty"`
BIC string `xml:"BIC,omitempty"`
Code string `xml:"CodicePagamento,omitempty"`
}
func newDatiPagamento(inv *bill.Invoice) (*paymentData, error) {
if inv.Payment == nil {
return nil, nil
}
dp, err := preparePaymentDetails(inv)
if err != nil {
return nil, err
}
return &paymentData{
Conditions: determinePaymentConditions(inv),
Payments: dp,
}, nil
}
func preparePaymentDetails(inv *bill.Invoice) ([]*paymentDetailRow, error) {
var dp []*paymentDetailRow
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 := &paymentDetailRow{
Method: advance.Ext[sdi.ExtKeyPaymentMeans].String(),
Amount: 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
}
br := paymentDetailRow{
Method: payment.Instructions.Ext[sdi.ExtKeyPaymentMeans].String(),
}
if len(payment.Instructions.CreditTransfer) > 0 {
ct1 := payment.Instructions.CreditTransfer[0]
br.IBAN = ct1.IBAN
br.BIC = ct1.BIC
}
// 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 {
r := br // copy
r.DueDate = dueDate.Date.String()
r.Amount = formatAmount(&dueDate.Amount)
dp = append(dp, &r)
}
}
// If there are no due dates, then a single DettaglioPagamento is created
// with the total payable amount.
if len(dp) == 0 {
br.Amount = formatAmount(&inv.Totals.Payable)
dp = append(dp, &br)
}
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
}
}