-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayments.go
More file actions
125 lines (108 loc) · 3.46 KB
/
Copy pathpayments.go
File metadata and controls
125 lines (108 loc) · 3.46 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
package facturae
import (
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/pay"
)
// PaymentDetails contains info about how the invoice should
// be paid
type PaymentDetails struct {
Installments []*Installment `xml:"Installment"`
}
// Installment contains info about each of the payment terms
type Installment struct {
InstallmentDueDate string
InstallmentAmount string
PaymentMeans string `xml:",omitempty"`
AccountToBeCredited *BankAccount `xml:",omitempty"`
DebitReconciliationReference string `xml:",omitempty"`
AccountToBeDebited *BankAccount `xml:",omitempty"`
CollectionAdditionalInformation string `xml:",omitempty"`
}
// BankAccount contains info needed to pay by transfer or direct debit
type BankAccount struct {
IBAN string `xml:",omitempty"`
AccountNumber string `xml:",omitempty"`
BranchInSpainAddress *Address `xml:",omitempty"`
OverseasBranchAddress *Address `xml:",omitempty"`
BIC string `xml:",omitempty"`
}
// TODO: move this to the GOBL project directly.
var facturaePaymentMethodCodes = map[cbc.Key]string{
pay.MeansKeyCash: "01",
pay.MeansKeyDirectDebit: "02",
pay.MeansKeyCreditTransfer: "04",
pay.MeansKeyCard: "19",
pay.MeansKeyOnline: "13",
}
func newPaymentDetails(paymentInfo *bill.PaymentDetails) *PaymentDetails {
if paymentInfo == nil {
return nil
}
terms := paymentInfo.Terms
if terms == nil {
return nil
}
if len(terms.DueDates) == 0 {
return nil
}
instructions := paymentInfo.Instructions
xmlInstallments := make([]*Installment, len(terms.DueDates))
for i, installment := range terms.DueDates {
xmlInstallment := &Installment{
InstallmentDueDate: installment.Date.String(),
InstallmentAmount: amount(installment.Amount),
PaymentMeans: facturaePaymentMethodCodes[instructions.Key],
CollectionAdditionalInformation: mergeNotes(paymentInfo.Terms.Notes, installment.Notes),
}
if instructions.Key == pay.MeansKeyCreditTransfer {
if len(instructions.CreditTransfer) > 0 {
xmlInstallment.AccountToBeCredited = newCreditBankAccount(instructions.CreditTransfer[0])
}
}
if instructions.Key == pay.MeansKeyDirectDebit {
xmlInstallment.AccountToBeDebited = newDebitBankAccount(instructions.DirectDebit)
xmlInstallment.DebitReconciliationReference = instructions.DirectDebit.Ref
}
if instructions.Key == pay.MeansKeyOnline {
if len(instructions.Online) > 0 {
if len(xmlInstallment.CollectionAdditionalInformation) > 0 {
xmlInstallment.CollectionAdditionalInformation += "\n"
}
xmlInstallment.CollectionAdditionalInformation += instructions.Online[0].URL
}
}
xmlInstallments[i] = xmlInstallment
}
return &PaymentDetails{
Installments: xmlInstallments,
}
}
func newCreditBankAccount(info *pay.CreditTransfer) *BankAccount {
if info == nil {
return nil
}
return &BankAccount{
IBAN: info.IBAN,
BIC: info.BIC,
AccountNumber: info.Number,
}
}
func newDebitBankAccount(info *pay.DirectDebit) *BankAccount {
if info == nil {
return nil
}
return &BankAccount{
AccountNumber: info.Account,
}
}
func mergeNotes(termNotes string, installmentNotes string) string {
notes := ""
if termNotes != "" {
notes = termNotes
if installmentNotes != "" {
notes += "\n"
}
}
return notes + installmentNotes
}