forked from angelodlfrtr/go-invoice-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
148 lines (129 loc) · 2.73 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
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
package generator
import (
"github.com/shopspring/decimal"
)
// Item represent a 'product' or a 'service'
type Item struct {
Name string `json:"name,omitempty" validate:"required"`
Description string `json:"description,omitempty"`
UnitCost string `json:"unit_cost,omitempty"`
Quantity string `json:"quantity,omitempty"`
Tax *Tax `json:"tax,omitempty"`
Discount *Discount `json:"discount,omitempty"`
Total string `json:"total,omitempty"`
_unitCost decimal.Decimal
_quantity decimal.Decimal
}
// Prepare convert strings to decimal
func (i *Item) Prepare() error {
// Unit cost
unitCost, err := decimal.NewFromString(i.UnitCost)
if err != nil {
return err
}
i._unitCost = unitCost
// Quantity
//quantity, err := decimal.NewFromString(i.Quantity)
//if err != nil {
// return err
//}
//i._quantity = quantity
// Tax
if i.Tax != nil {
if err := i.Tax.Prepare(); err != nil {
return err
}
}
// Discount
if i.Discount != nil {
if err := i.Discount.Prepare(); err != nil {
return err
}
}
return nil
}
// appendColTo document doc
func (i *Item) appendColTo(options *Options, doc *Document) {
// Get base Y (top of line)
baseY := doc.pdf.GetY()
// Name
doc.pdf.SetX(ItemColNameOffset)
doc.pdf.MultiCell(
ItemColUnitPriceOffset-ItemColNameOffset,
3,
doc.encodeString(i.Name),
"",
"",
false,
)
// Description
if len(i.Description) > 0 {
doc.pdf.SetX(ItemColNameOffset)
doc.pdf.SetY(doc.pdf.GetY() + 1)
doc.pdf.SetFont(doc.Options.Font, "", SmallTextFontSize)
doc.pdf.SetTextColor(
doc.Options.GreyTextColor[0],
doc.Options.GreyTextColor[1],
doc.Options.GreyTextColor[2],
)
doc.pdf.MultiCell(
ItemColUnitPriceOffset-ItemColNameOffset,
3,
doc.encodeString(i.Description),
"",
"",
false,
)
// Reset font
doc.pdf.SetFont(doc.Options.Font, "", BaseTextFontSize)
doc.pdf.SetTextColor(
doc.Options.BaseTextColor[0],
doc.Options.BaseTextColor[1],
doc.Options.BaseTextColor[2],
)
}
// Compute line height
colHeight := doc.pdf.GetY() - baseY
// Unit price
doc.pdf.SetY(baseY)
doc.pdf.SetX(ItemColUnitPriceOffset)
doc.pdf.CellFormat(
ItemColQuantityOffset-ItemColUnitPriceOffset,
colHeight,
doc.encodeString(i.UnitCost),
"0",
0,
"",
false,
0,
"",
)
// Quantity
doc.pdf.SetX(ItemColQuantityOffset)
doc.pdf.CellFormat(
ItemColTaxOffset-ItemColQuantityOffset,
colHeight,
doc.encodeString(i.Quantity),
"0",
0,
"",
false,
0,
"",
)
// Total HT
doc.pdf.SetX(ItemColTotalHTOffset)
doc.pdf.CellFormat(
ItemColTaxOffset-ItemColTotalHTOffset,
colHeight,
doc.encodeString(i.Total),
"0",
0,
"",
false,
0,
"",
)
// Set Y for next line
doc.pdf.SetY(baseY + colHeight)
}