forked from angelodlfrtr/go-invoice-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
244 lines (202 loc) · 6.91 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package generator
import (
"fmt"
"github.com/leekchan/accounting"
"github.com/shopspring/decimal"
"github.com/signintech/gopdf"
)
// 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"`
}
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) totalWithoutTax() decimal.Decimal {
quantity, _ := decimal.NewFromString(i.Quantity)
price, _ := decimal.NewFromString(i.UnitCost)
total := price.Mul(quantity)
return total
}
func (i *Item) totalWithoutTaxAndWithDiscount() decimal.Decimal {
total := i.totalWithoutTax()
// Check discount
if i.Discount != nil {
dType, dNum := i.Discount.getDiscount()
if dType == "amount" {
total = total.Sub(dNum)
} else {
// Percent
toSub := total.Mul(dNum.Div(decimal.NewFromFloat(100)))
total = total.Sub(toSub)
}
}
return total
}
func (i *Item) totalWithTaxAndDiscount() decimal.Decimal {
return i.totalWithoutTaxAndWithDiscount().Add(i.taxWithDiscount())
}
func (i *Item) taxWithDiscount() decimal.Decimal {
result := decimal.NewFromFloat(0)
if i.Tax == nil {
return result
}
totalHT := i.totalWithoutTaxAndWithDiscount()
taxType, taxAmount := i.Tax.getTax()
if taxType == "amount" {
result = taxAmount
} else {
divider := decimal.NewFromFloat(100)
result = totalHT.Mul(taxAmount.Div(divider))
}
return result
}
func (i *Item) appendColTo(options *Options, doc *Document) {
ac := accounting.Accounting{
Symbol: (options.CurrencySymbol),
Precision: options.CurrencyPrecision,
Thousand: options.CurrencyThousand,
Decimal: options.CurrencyDecimal,
}
// Get base Y (top of line)
baseY := doc.pdf.GetY()
// Name
doc.pdf.SetX(BaseMargin + itemTitleMargin)
doc.pdf.MultiCell(
&gopdf.Rect{
W: ItemColUnitPriceOffset - BaseMargin - itemTitleMargin*2,
H: itemFontSize * 3,
},
i.Name,
)
// Description
if len(i.Description) > 0 {
doc.pdf.SetX(BaseMargin + itemTitleMargin)
doc.pdf.SetFont("Ubuntu", "", SmallTextFontSize)
doc.pdf.SetTextColor(
doc.Options.GreyTextColor[0],
doc.Options.GreyTextColor[1],
doc.Options.GreyTextColor[2],
)
doc.pdf.MultiCell(
&gopdf.Rect{
W: ItemColUnitPriceOffset - BaseMargin - itemTitleMargin*2,
H: itemFontSize * 3,
},
i.Description,
)
// Reset font
doc.pdf.SetFont("Ubuntu", "", 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.Cell(&gopdf.Rect{W: ItemColQuantityOffset - ItemColUnitPriceOffset, H: colHeight}, ac.FormatMoneyDecimal(i.unitCost())) //, "0", 0, "", false, 0, "")
// Quantity
doc.pdf.SetX(ItemColQuantityOffset)
doc.pdf.Cell(&gopdf.Rect{W: ItemColTaxOffset - ItemColQuantityOffset, H: colHeight}, i.quantity().String()) //, "0", 0, "", false, 0, "")
// Total HT
doc.pdf.SetX(ItemColTotalHTOffset)
doc.pdf.Cell(&gopdf.Rect{W: ItemColTaxOffset - ItemColTotalHTOffset, H: colHeight}, ac.FormatMoneyDecimal(i.totalWithoutTax())) //, "0", 0, "", false, 0, "")
// Discount
doc.pdf.SetX(ItemColDiscountOffset)
if i.Discount == nil {
doc.pdf.Cell(&gopdf.Rect{W: ItemColTotalTTCOffset - ItemColDiscountOffset, H: colHeight}, "--")
} else {
// If discount
discountType, discountAmount := i.Discount.getDiscount()
var discountTitle string
var discountDesc string
if discountType == "percent" {
discountTitle = fmt.Sprintf("%s %s", discountAmount, "%")
// get amount from percent
dCost := i.totalWithoutTax()
dAmount := dCost.Mul(discountAmount.Div(decimal.NewFromFloat(100)))
discountDesc = fmt.Sprintf("-%s", ac.FormatMoneyDecimal(dAmount))
} else {
discountTitle = fmt.Sprintf("%s %s", discountAmount, "€")
dCost := i.totalWithoutTax()
dPerc := discountAmount.Mul(decimal.NewFromFloat(100))
dPerc = dPerc.Div(dCost)
// get percent from amount
discountDesc = fmt.Sprintf("-%s %%", dPerc.StringFixed(2))
}
// discount title
// lastY := doc.pdf.GetY()
doc.pdf.Cell(&gopdf.Rect{W: ItemColTotalTTCOffset - ItemColDiscountOffset, H: colHeight / 2}, discountTitle)
// discount desc
doc.pdf.SetX(ItemColDiscountOffset)
doc.pdf.SetY(baseY + BaseTextFontSize)
doc.pdf.SetFont("Ubuntu", "", SmallTextFontSize)
doc.pdf.SetTextColor(doc.Options.GreyTextColor[0], doc.Options.GreyTextColor[1], doc.Options.GreyTextColor[2])
doc.pdf.Cell(&gopdf.Rect{W: ItemColTotalTTCOffset - ItemColDiscountOffset, H: colHeight / 2}, discountDesc)
// reset font and y
doc.pdf.SetFont("Ubuntu", "", BaseTextFontSize)
doc.pdf.SetTextColor(
doc.Options.BaseTextColor[0],
doc.Options.BaseTextColor[1],
doc.Options.BaseTextColor[2],
)
doc.pdf.SetY(baseY)
}
// Tax
doc.pdf.SetX(ItemColTaxOffset)
if i.Tax == nil {
// If no tax
doc.pdf.Cell(&gopdf.Rect{W: ItemColDiscountOffset - ItemColTaxOffset, H: colHeight}, "--")
} else {
// If tax
taxType, taxAmount := i.Tax.getTax()
var taxTitle string
var taxDesc string
if taxType == "percent" {
taxTitle = fmt.Sprintf("%s %s", taxAmount, ("%"))
// get amount from percent
dCost := i.totalWithoutTaxAndWithDiscount()
dAmount := dCost.Mul(taxAmount.Div(decimal.NewFromFloat(100)))
taxDesc = ac.FormatMoneyDecimal(dAmount)
} else {
taxTitle = fmt.Sprintf("%s %s", taxAmount, ("€"))
dCost := i.totalWithoutTaxAndWithDiscount()
dPerc := taxAmount.Mul(decimal.NewFromFloat(100))
dPerc = dPerc.Div(dCost)
// get percent from amount
taxDesc = fmt.Sprintf("%s %%", dPerc.StringFixed(2))
}
// tax title
// lastY := doc.pdf.GetY()
doc.pdf.Cell(&gopdf.Rect{W: ItemColDiscountOffset - ItemColTaxOffset, H: colHeight / 2}, taxTitle)
// tax desc
doc.pdf.SetX(ItemColTaxOffset)
doc.pdf.SetY(baseY + BaseTextFontSize)
doc.pdf.SetFont("Ubuntu", "", SmallTextFontSize)
doc.pdf.SetTextColor(doc.Options.GreyTextColor[0], doc.Options.GreyTextColor[1], doc.Options.GreyTextColor[2])
doc.pdf.Cell(&gopdf.Rect{W: ItemColDiscountOffset - ItemColTaxOffset, H: colHeight / 2}, taxDesc)
// reset font and y
doc.pdf.SetFont("Ubuntu", "", BaseTextFontSize)
doc.pdf.SetTextColor(doc.Options.BaseTextColor[0], doc.Options.BaseTextColor[1], doc.Options.BaseTextColor[2])
doc.pdf.SetY(baseY)
}
// TOTAL TTC
doc.pdf.SetX(ItemColTotalTTCOffset)
doc.pdf.Cell(&gopdf.Rect{W: 190 - ItemColTotalTTCOffset, H: colHeight}, ac.FormatMoneyDecimal(i.totalWithTaxAndDiscount()))
// Set Y for next line
doc.pdf.SetY(baseY + colHeight)
}