-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoice_totals.go
More file actions
200 lines (182 loc) · 5.54 KB
/
Copy pathinvoice_totals.go
File metadata and controls
200 lines (182 loc) · 5.54 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package facturae
import (
"github.com/invopop/gobl/addons/es/facturae"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cal"
"github.com/invopop/gobl/num"
"github.com/invopop/gobl/pay"
"github.com/invopop/gobl/tax"
)
// InvoiceTotals contains the summary of the amounts in an invoice
type InvoiceTotals struct {
TotalGrossAmount string // Without included taxes
GeneralDiscounts *Discounts `xml:",omitempty"`
GeneralSurcharges *Charges `xml:",omitempty"`
TotalGeneralDiscounts string `xml:",omitempty"`
TotalGeneralSurcharges string `xml:",omitempty"`
TotalGrossAmountBeforeTaxes string
TotalTaxOutputs string
TotalTaxesWithheld string
InvoiceTotal string
Subsidies *Subsidies `xml:",omitempty"`
PaymentsOnAccount *PaymentsOnAccount `xml:",omitempty"`
ReimbursableExpenses *ReimbursableExpenses `xml:",omitempty"`
TotalOutstandingAmount string // InvoiceTotal - (Total subvenciones + TotalPaymentsOnAccount)
TotalPaymentsOnAccount string `xml:",omitempty"`
TotalExecutableAmount string
TotalReimbursableExpenses string `xml:",omitempty"`
}
// Subsidies is currently a placeholder, as we don't use it yet.
type Subsidies struct {
Subsidy []*Subsidy
}
// Subsidy is a single subsidy entry
type Subsidy struct {
SubsidyDescription string
SubsidyRate string `xml:",omitempty"`
SubsidyAmount string
}
// PaymentsOnAccount stores payments made in advance.
type PaymentsOnAccount struct {
PaymentOnAccount []*PaymentOnAccount
}
// PaymentOnAccount is a single advance payment.
type PaymentOnAccount struct {
PaymentOnAccountDate cal.Date
PaymentOnAccountAmount string
}
// ReimbursableExpenses is a group of reimbursable expenses.
type ReimbursableExpenses struct {
ReimbursableExpenses []*ReimbursableExpense
}
// ReimbursableExpense stores info about a single reimbursable expense.
type ReimbursableExpense struct {
ReimbursableExpensesSellerParty *TaxID `xml:",omitempty"`
ReimbursableExpensesBuyerParty *TaxID `xml:",omitempty"` // never used!
IssueDate *cal.Date `xml:",omitempty"`
InvoiceNumber string `xml:",omitempty"`
InvoiceSeriesCode string `xml:",omitempty"`
ReimbursableExpensesAmount string
}
func newInvoiceTotals(invoice *bill.Invoice) *InvoiceTotals {
totals := invoice.Totals
sum := totals.Sum
if totals.TaxIncluded != nil {
sum = sum.Subtract(*totals.TaxIncluded)
}
due := totals.Payable
outstanding := totals.Payable
if totals.Due != nil {
due = *totals.Due
outstanding = *totals.Due
}
/*
if totals.Outlays != nil {
outstanding = outstanding.Subtract(*totals.Outlays)
}
*/
xmlTotals := &InvoiceTotals{
TotalGrossAmount: amount(sum),
GeneralDiscounts: newDiscounts(invoice.Discounts),
GeneralSurcharges: newCharges(invoice.Charges),
TotalGrossAmountBeforeTaxes: amount(totals.Total),
InvoiceTotal: amount(totals.TotalWithTax),
TotalOutstandingAmount: amount(outstanding),
TotalExecutableAmount: amount(due),
}
if totals.Discount != nil {
xmlTotals.TotalGeneralDiscounts = amount(*totals.Discount)
}
if totals.Charge != nil {
xmlTotals.TotalGeneralSurcharges = amount(*totals.Charge)
}
if totals.Advances != nil {
xmlTotals.TotalPaymentsOnAccount = amount(*totals.Advances)
}
/*
if totals.Outlays != nil {
xmlTotals.TotalReimbursableExpenses = totals.Outlays.String()
}
*/
if invoice.Payment != nil {
xmlTotals.setAdvances(invoice.Payment.Advances)
}
// xmlTotals.setOutlays(invoice.Outlays)
xmlTotals.setTaxTotals(totals.Taxes)
return xmlTotals
}
func (it *InvoiceTotals) setTaxTotals(taxes *tax.Total) {
regular := num.MakeAmount(0, 2)
retained := num.MakeAmount(0, 2)
for _, ct := range taxes.Categories {
if ct.Retained {
retained = retained.Add(ct.Amount)
} else {
regular = regular.Add(ct.Amount)
if ct.Surcharge != nil {
regular = regular.Add(*ct.Surcharge)
}
}
}
it.TotalTaxOutputs = amount(regular)
it.TotalTaxesWithheld = amount(retained)
}
func (it *InvoiceTotals) setAdvances(advances []*pay.Record) {
regular := make([]*PaymentOnAccount, 0)
grants := make([]*Subsidy, 0)
for _, a := range advances {
if a.Ext.Get(facturae.ExtKeySubsidy) == "S" {
g := &Subsidy{
SubsidyDescription: a.Description,
SubsidyAmount: amount(a.Amount),
}
if a.Percent != nil {
g.SubsidyRate = a.Percent.String()
}
grants = append(grants, g)
} else {
na := &PaymentOnAccount{
PaymentOnAccountAmount: amount(a.Amount),
}
if a.Date != nil {
na.PaymentOnAccountDate = *a.Date
}
regular = append(regular, na)
}
}
if len(regular) > 0 {
it.PaymentsOnAccount = &PaymentsOnAccount{
PaymentOnAccount: regular,
}
}
if len(grants) > 0 {
it.Subsidies = &Subsidies{
Subsidy: grants,
}
}
}
/*
func (it *InvoiceTotals) setOutlays(outlays []*bill.Outlay) {
os := make([]*ReimbursableExpense, 0)
for _, o := range outlays {
no := &ReimbursableExpense{
IssueDate: o.Date,
InvoiceNumber: o.Code,
InvoiceSeriesCode: o.Series,
ReimbursableExpensesAmount: o.Amount.String(),
}
if o.Supplier != nil {
tid := o.Supplier.TaxID
if tid != nil {
no.ReimbursableExpensesSellerParty = NewTaxID(tid.Code, tid.Country)
}
}
os = append(os, no)
}
if len(os) > 0 {
it.ReimbursableExpenses = &ReimbursableExpenses{
ReimbursableExpenses: os,
}
}
}
*/