generated from NezuChan/golang-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (124 loc) · 6.64 KB
/
main.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
package main
import (
"bytes"
"fmt"
"log"
"github.com/fogleman/gg"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/golang/freetype/truetype"
"github.com/kagchi/invoice-creator/structs"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
const (
width = 800 // Width of A4 paper in points (21 cm)
height = 841 // Height of A4 paper in points (29.7 cm)
margin = 20.0 // Margin in points
)
func drawString(dc *gg.Context, fontSize float64, s string, x float64, y float64, ax float64, ay float64, width float64, align gg.Align) {
font, _ := truetype.Parse(goregular.TTF)
face := truetype.NewFace(font, &truetype.Options{
Size: fontSize,
})
dc.SetFontFace(face)
dc.DrawStringWrapped(s, x, y, ax, ay, width, fontSize, align)
}
func formatNumberToLocaleString(num int, locale string) string {
tags, _, err := language.ParseAcceptLanguage(locale)
if err != nil {
return fmt.Sprintf("%d", num)
}
var tag language.Tag
if len(tags) > 0 {
tag = tags[0]
} else {
tag = language.English
}
p := message.NewPrinter(tag)
return p.Sprintf("%d", num)
}
func main() {
app := fiber.New()
app.Use(logger.New(logger.Config{
Format: "[${ip}]:${port} [${latency}] ${status} - ${method} ${path}\n",
}))
app.Post("/", func(c *fiber.Ctx) error {
b := new(structs.Payload)
if err := c.BodyParser(b); err != nil {
return err
}
dc := gg.NewContext(width, height)
dc.SetRGB(1, 1, 1)
dc.Clear()
dc.SetRGB(0, 0, 0)
drawString(dc, 28.0, "INVOICE", margin, margin, 0, 0, float64(width)-4*margin, gg.AlignRight) // Title
drawString(dc, 16.0, b.Company.Name, margin, 4*margin, 0, 0, float64(width)-4*margin, gg.AlignRight) // Company Name
drawString(dc, 16.0, b.Company.Address.Detail, margin, 5*margin, 0, 0, float64(width)-4*margin, gg.AlignRight) // Address
drawString(dc, 16.0, b.Company.Address.Zip, margin, 6*margin, 0, 0, float64(width)-4*margin, gg.AlignRight) // Zip Address
drawString(dc, 16.0, b.Company.Address.City, margin, 7*margin, 0, 0, float64(width)-4*margin, gg.AlignRight) // Country
dc.SetRGB(0, 0, 0)
lineY := margin + 20.0 + 10
dc.DrawLine(margin+20, lineY*4, width-(margin+20), lineY*4)
dc.SetLineWidth(1.0)
dc.Stroke()
drawString(dc, 16.0, b.Client.Name, margin + 20, 11*margin, 0, 0, float64(width)-4*margin, gg.AlignLeft) // Client Name
drawString(dc, 16.0, b.Client.Address.Detail, margin + 20, 12 * margin, 0, 0, float64(width)-4*margin, gg.AlignLeft) // Client Address
drawString(dc, 16.0, b.Client.Address.Zip, margin + 20, 13 * margin, 0, 0, float64(width)-4*margin, gg.AlignLeft) // Client Zip Address
drawString(dc, 16.0, b.Client.Address.Country, margin + 20, 14 * margin, 0, 0, float64(width)-4*margin, gg.AlignLeft) // Client Country
drawString(dc, 16.0, fmt.Sprintf("Number : %s", b.Invoice.Number), margin + 20, 11 * margin, 0, 0, float64(width) - 4 * margin, gg.AlignRight) // Invoice Number
drawString(dc, 16.0, fmt.Sprintf("Date : %s", b.Invoice.Date), (margin + 20) -87, 12 * margin, 0, 0, float64(width) - 4 * margin, gg.AlignRight) // Invoice Date
drawString(dc, 16.0, fmt.Sprintf("Due Date : %s", b.Invoice.Due), (margin + 20)-87, 13 * margin, 0, 0, float64(width) - 4 * margin, gg.AlignRight) // Invoice Due Date
drawString(dc, 16.0, "Products", margin + 20, 18.5 * margin, 0, 0, float64(width) -4 * margin, gg.AlignLeft)
drawString(dc, 16.0, "Quantity", margin - 210, 18.5 * margin, 0, 0, float64(width) -4 * margin, gg.AlignRight)
drawString(dc, 16.0, "Price", margin - 105, 18.5 * margin, 0, 0, float64(width) - 4 * margin, gg.AlignRight)
drawString(dc, 16.0, "Total", margin + 8, 18.5 * margin, 0, 0, float64(width) - 4 * margin, gg.AlignRight)
dc.DrawLine(margin+20, lineY*8, width-(margin+20), lineY*8)
dc.SetLineWidth(1.0)
dc.Stroke()
// TODO: use for const of
currentMargin := 21.0
currentPrice := 0
for _, product := range b.Products {
drawString(dc, 16.0, product.Name, margin+20, currentMargin * margin, 0, 0, float64(width)-4*margin, gg.AlignLeft)
quantityStr := fmt.Sprintf("%d", product.Quantity)
quantityLen := 0.0
if len(quantityStr) <= 3 {
quantityLen += float64(len(quantityStr)) * 2.7
} else {
quantityLen += float64(len(quantityStr)) * 3.5
}
drawString(dc, 16.0, quantityStr, margin-(237.5 - quantityLen), currentMargin * margin, 0, 0, float64(width) - 4 * margin, gg.AlignRight) // Quantity
priceStr := fmt.Sprintf("%d", product.Price)
priceLen := 0.0
if len(priceStr) <= 1 {
priceLen += float64(len(quantityStr)) * 14.0
} else if len(priceStr) > 1 {
priceLen -= float64(len(quantityStr)) * 10
priceLen -= float64(len(quantityStr)) * (float64(len(quantityStr)) * 10)
}
drawString(dc, 16.0, fmt.Sprintf("Rp %s", formatNumberToLocaleString(product.Price, b.Setting.Locale)), margin-(107 + priceLen), currentMargin * margin, 0, 0, float64(width) - 4 * margin, gg.AlignRight) // Quantity
drawString(dc, 16.0, fmt.Sprintf("Rp %s", formatNumberToLocaleString(product.Price * product.Quantity, b.Setting.Locale)), margin + 20, currentMargin * margin, 0, 0, float64(width) - 4 * margin, gg.AlignRight) // Price
currentPrice += product.Price * product.Quantity
currentMargin += 1.0
}
lineY = currentMargin + 20.0 + 10.0 + float64(len(b.Products)) - 2.0
dc.DrawLine(margin + 20, lineY * 9.5, width - (margin + 20), lineY * 9.5)
dc.SetLineWidth(1.0)
dc.Stroke()
drawString(dc, 16.0, fmt.Sprintf("Sub Total : %s %s", b.Setting.Currency, formatNumberToLocaleString(currentPrice, b.Setting.Locale)), margin+20, (currentMargin + 6.5) * margin, 0, 0, float64(width) - 4 * margin, gg.AlignRight) // Subtotal\
drawString(dc, 16.0, fmt.Sprintf("Vat %.2f%% : %s %s", b.Setting.Vat, b.Setting.Currency, formatNumberToLocaleString(int(float64((currentPrice)) * (b.Setting.Vat / 100)), b.Setting.Locale)), margin + 20, (currentMargin + 7.5) * margin, 0, 0, float64(width)-4*margin, gg.AlignRight) // Subtotal
currentMargin += 1.5
lineY = currentMargin + 20.0 + 10.0 + (float64(len(b.Products)) - 2.0) + 12.5
dc.DrawLine(margin + 440, lineY * 9.5, width - (margin + 20), lineY * 9.5)
dc.SetLineWidth(1.0)
dc.Stroke()
drawString(dc, 16.0, fmt.Sprintf("Total : %s %s", b.Setting.Currency, formatNumberToLocaleString(int(float64(currentPrice) * (b.Setting.Vat / 100)) + currentPrice, b.Setting.Locale)), margin + 20, (currentMargin + 7.5) * margin, 0, 0, float64(width) - 4 * margin, gg.AlignRight) // Subtotal
var buf bytes.Buffer
_ = dc.EncodePNG(&buf)
c.Set("Content-Type", "image/png")
return c.Send(buf.Bytes())
})
log.Fatal(app.Listen(":3000"))
}