-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoice.go
138 lines (104 loc) · 2.97 KB
/
Invoice.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
package main
import (
"bytes"
"fmt"
"github.com/permafrost06/escpos"
)
func printShopName(p *escpos.Escpos) {
p.Bold(true).Size(2, 2).Justify(escpos.JustifyCenter).Write("DK & NCK")
p.LineFeed()
}
func printAddress(p *escpos.Escpos) {
address := `Shop: 32 & 44, 4th Floor, Anexco Tower
8 Phoenix Road, Fulbaria, Shahbag
Dhaka-1000`
p.Bold(false).Size(1, 1).Justify(escpos.JustifyCenter).Write(address)
p.LineFeed()
}
func printPhone(p *escpos.Escpos) {
phone := `Phone: 01556341569, 01832775999`
p.Bold(false).Size(1, 1).Justify(escpos.JustifyCenter).Write(phone)
p.LineFeed()
}
func printInvoiceId(p *escpos.Escpos, id string) {
idString := fmt.Sprintf("Invoice No: %s", id)
p.Bold(false).Size(1, 1).Justify(escpos.JustifyCenter).Write(idString)
p.LineFeed()
}
func printDate(p *escpos.Escpos, date string) {
dateString := fmt.Sprintf("Date: %s", date)
p.Bold(false).Size(1, 1).Justify(escpos.JustifyCenter).Write(dateString)
p.LineFeed()
}
func printTableHeader(p *escpos.Escpos) {
header := `
ID Item Price Qty Total
------------------------------------------------`
p.Write(header)
p.LineFeed()
}
func trim(str string, length int) string {
if len(str) < length {
return str
}
return str[:length]
}
func printItems(p *escpos.Escpos, items []InvoiceItem) {
for _, item := range items {
row := fmt.Sprintf(
"%08s %-23s %5s %3s %5s",
trim(fmt.Sprint(item.Product_id), 8),
trim(item.Name, 23),
trim(item.Unit_price, 5),
trim(fmt.Sprint(item.Quantity), 3),
trim(fmt.Sprint(item.Total_price), 5),
)
p.Bold(false).Size(1, 1).Justify(escpos.JustifyCenter).Write(row)
p.LineFeed()
}
}
func printTableFooter(p *escpos.Escpos, subtotal string) {
footer := "------------------------------------------------"
footer += fmt.Sprintf(" Sub Total: %s", subtotal)
p.Write(footer)
p.LineFeed()
}
func printMessage(p *escpos.Escpos) {
message := `Please bring cash memo for returning products`
p.Bold(false).Size(1, 1).Justify(escpos.JustifyCenter).Write(message)
p.LineFeed()
p.Bold(true).Size(1, 1).Justify(escpos.JustifyCenter).Write("Thank you for shopping with DK & NCK")
p.LineFeed()
}
func printBottomPadding(p *escpos.Escpos) {
p.LineFeed()
p.LineFeed()
p.LineFeed()
}
func GetInvoiceBytes(req PrintRequest) []byte {
f := new(bytes.Buffer)
p := escpos.New(f)
printShopName(p)
printAddress(p)
printPhone(p)
p.LineFeed()
printInvoiceId(p, req.Invoice.ID)
printDate(p, req.Invoice.Date)
p.LineFeed()
printTableHeader(p)
printItems(p, req.Invoice.Items)
printTableFooter(p, req.Invoice.Subtotal)
p.LineFeed()
p.WriteRaw([]byte{0x1d, 0x68, 0x50})
p.WriteRaw([]byte{0x1d, 0x77, 0x04})
p.WriteRaw([]byte{0x1d, 0x66, 0x00})
p.WriteRaw([]byte{0x1d, 0x48, 0x02})
p.WriteRaw([]byte{0x1d, 0x6b, 0x04})
barcode := fmt.Sprintf("S%08s", req.Invoice.ID)
byteCode := append([]byte(barcode), 0)
p.WriteRaw(byteCode)
printMessage(p)
printBottomPadding(p)
p.PrintAndCut()
return f.Bytes()
}