forked from angelodlfrtr/go-invoice-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.go
91 lines (73 loc) · 2.12 KB
/
item.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
package generator
import (
"fmt"
"github.com/jung-kurt/gofpdf"
"github.com/leekchan/accounting"
"github.com/shopspring/decimal"
)
type Item struct {
Name string `validate:"required"`
Description string
UnitCost string
Quantity string
Tax *Tax
Discount *Discount
}
func (i *Item) appendColTo(options *Options, pdf *gofpdf.Fpdf) {
ac := accounting.Accounting{
Symbol: encodeString(options.CurrencySymbol),
Precision: options.CurrencyPrecision,
Thousand: options.CurrencyThousand,
Decimal: options.CurrencyDecimal,
}
// Name
pdf.CellFormat(80, 6, encodeString(i.Name), "0", 0, "", false, 0, "")
// Unit price
pdf.SetX(90)
pdf.CellFormat(30, 6, ac.FormatMoneyDecimal(i.unitCost()), "0", 0, "", false, 0, "")
// Quantity
pdf.SetX(120)
pdf.CellFormat(15, 6, i.quantity().String(), "0", 0, "", false, 0, "")
// Total HT
pdf.SetX(135)
pdf.CellFormat(20, 6, ac.FormatMoneyDecimal(i.totalHT()), "0", 0, "", false, 0, "")
// Tax
taxType, taxAmount := i.Tax.getTax()
var taxString string
if taxType == "percent" {
taxString = fmt.Sprintf("%s %s", taxAmount, encodeString("%"))
} else {
taxString = fmt.Sprintf("%s %s", taxAmount, encodeString("€"))
}
pdf.SetX(155)
pdf.CellFormat(20, 6, taxString, "0", 0, "", false, 0, "")
// TOTAL TTC
pdf.SetX(175)
pdf.CellFormat(25, 6, ac.FormatMoneyDecimal(i.totalTTC()), "0", 0, "", false, 0, "")
}
func (i *Item) unitCost() decimal.Decimal {
unitCost, _ := decimal.NewFromString(i.UnitCost)
return unitCost
}
func (i *Item) quantity() decimal.Decimal {
quantity, _ := decimal.NewFromString(i.Quantity)
return quantity
}
func (i *Item) totalHT() decimal.Decimal {
quantity, _ := decimal.NewFromString(i.Quantity)
price, _ := decimal.NewFromString(i.UnitCost)
return price.Mul(quantity)
}
func (i *Item) totalTTC() decimal.Decimal {
totalHT := i.totalHT()
taxType, taxAmount := i.Tax.getTax()
var totalTTC decimal.Decimal
if taxType == "amount" {
totalTTC = totalHT.Add(taxAmount)
} else {
divider, _ := decimal.NewFromString("100")
tax := totalHT.Mul(taxAmount.Div(divider))
totalTTC = totalHT.Add(tax)
}
return totalTTC
}