-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
main.go
174 lines (145 loc) · 4.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
164
165
166
167
168
169
170
171
172
173
174
package main
import (
_ "embed"
"flag"
"fmt"
"log"
"strings"
"time"
"github.com/signintech/gopdf"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
//go:embed "Inter/Inter Variable/Inter.ttf"
var interFont []byte
//go:embed "Inter/Inter Hinted for Windows/Desktop/Inter-Bold.ttf"
var interBoldFont []byte
type Invoice struct {
Id string `json:"id" yaml:"id"`
Title string `json:"title" yaml:"title"`
Logo string `json:"logo" yaml:"logo"`
From string `json:"from" yaml:"from"`
To string `json:"to" yaml:"to"`
Date string `json:"date" yaml:"date"`
Due string `json:"due" yaml:"due"`
Items []string `json:"items" yaml:"items"`
Quantities []int `json:"quantities" yaml:"quantities"`
Rates []float64 `json:"rates" yaml:"rates"`
Tax float64 `json:"tax" yaml:"tax"`
Discount float64 `json:"discount" yaml:"discount"`
Currency string `json:"currency" yaml:"currency"`
Note string `json:"note" yaml:"note"`
}
func DefaultInvoice() Invoice {
return Invoice{
Id: time.Now().Format("20060102"),
Title: "INVOICE",
Rates: []float64{25},
Quantities: []int{2},
Items: []string{"Paper Cranes"},
From: "Project Folded, Inc.",
To: "Untitled Corporation, Inc.",
Date: time.Now().Format("Jan 02, 2006"),
Due: time.Now().AddDate(0, 0, 14).Format("Jan 02, 2006"),
Tax: 0,
Discount: 0,
Currency: "USD",
}
}
var (
importPath string
output string
file = Invoice{}
defaultInvoice = DefaultInvoice()
)
func init() {
viper.AutomaticEnv()
generateCmd.Flags().StringVar(&importPath, "import", "", "Imported file (.json/.yaml)")
generateCmd.Flags().StringVar(&file.Id, "id", time.Now().Format("20060102"), "ID")
generateCmd.Flags().StringVar(&file.Title, "title", "INVOICE", "Title")
generateCmd.Flags().Float64SliceVarP(&file.Rates, "rate", "r", defaultInvoice.Rates, "Rates")
generateCmd.Flags().IntSliceVarP(&file.Quantities, "quantity", "q", defaultInvoice.Quantities, "Quantities")
generateCmd.Flags().StringSliceVarP(&file.Items, "item", "i", defaultInvoice.Items, "Items")
generateCmd.Flags().StringVarP(&file.Logo, "logo", "l", defaultInvoice.Logo, "Company logo")
generateCmd.Flags().StringVarP(&file.From, "from", "f", defaultInvoice.From, "Issuing company")
generateCmd.Flags().StringVarP(&file.To, "to", "t", defaultInvoice.To, "Recipient company")
generateCmd.Flags().StringVar(&file.Date, "date", defaultInvoice.Date, "Date")
generateCmd.Flags().StringVar(&file.Due, "due", defaultInvoice.Due, "Payment due date")
generateCmd.Flags().Float64Var(&file.Tax, "tax", defaultInvoice.Tax, "Tax")
generateCmd.Flags().Float64VarP(&file.Discount, "discount", "d", defaultInvoice.Discount, "Discount")
generateCmd.Flags().StringVarP(&file.Currency, "currency", "c", defaultInvoice.Currency, "Currency")
generateCmd.Flags().StringVarP(&file.Note, "note", "n", "", "Note")
generateCmd.Flags().StringVarP(&output, "output", "o", "invoice.pdf", "Output file (.pdf)")
flag.Parse()
}
var rootCmd = &cobra.Command{
Use: "invoice",
Short: "Invoice generates invoices from the command line.",
Long: `Invoice generates invoices from the command line.`,
}
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate an invoice",
Long: `Generate an invoice`,
RunE: func(cmd *cobra.Command, args []string) error {
if importPath != "" {
err := importData(importPath, &file, cmd.Flags())
if err != nil {
return err
}
}
pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{
PageSize: *gopdf.PageSizeA4,
})
pdf.SetMargins(40, 40, 40, 40)
pdf.AddPage()
err := pdf.AddTTFFontData("Inter", interFont)
if err != nil {
return err
}
err = pdf.AddTTFFontData("Inter-Bold", interBoldFont)
if err != nil {
return err
}
writeLogo(&pdf, file.Logo, file.From)
writeTitle(&pdf, file.Title, file.Id, file.Date)
writeBillTo(&pdf, file.To)
writeHeaderRow(&pdf)
subtotal := 0.0
for i := range file.Items {
q := 1
if len(file.Quantities) > i {
q = file.Quantities[i]
}
r := 0.0
if len(file.Rates) > i {
r = file.Rates[i]
}
writeRow(&pdf, file.Items[i], q, r)
subtotal += float64(q) * r
}
if file.Note != "" {
writeNotes(&pdf, file.Note)
}
writeTotals(&pdf, subtotal, subtotal*file.Tax, subtotal*file.Discount)
if file.Due != "" {
writeDueDate(&pdf, file.Due)
}
writeFooter(&pdf, file.Id)
output = strings.TrimSuffix(output, ".pdf") + ".pdf"
err = pdf.WritePdf(output)
if err != nil {
return err
}
fmt.Printf("Generated %s\n", output)
return nil
},
}
func main() {
rootCmd.AddCommand(generateCmd)
err := rootCmd.Execute()
if err != nil {
log.Fatal(err)
}
}