Skip to content

Commit

Permalink
Add 2 decimal places to totals, move discount before tax
Browse files Browse the repository at this point in the history
  • Loading branch information
hartraft committed Jul 1, 2024
1 parent bc382da commit d59bc5b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
golang.org/x/text v0.13.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -28,6 +29,5 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ var (
file = Invoice{}
defaultInvoice = DefaultInvoice()
)

func init() {
viper.AutomaticEnv()

Expand Down Expand Up @@ -148,7 +147,7 @@ var generateCmd = &cobra.Command{
if file.Note != "" {
writeNotes(&pdf, file.Note)
}
writeTotals(&pdf, subtotal, subtotal*file.Tax, subtotal*file.Discount)
writeTotals(&pdf, subtotal, (subtotal-(subtotal*file.Discount))*file.Tax, subtotal*file.Discount)
if file.Due != "" {
writeDueDate(&pdf, file.Due)
}
Expand All @@ -158,7 +157,6 @@ var generateCmd = &cobra.Command{
if err != nil {
return err
}

fmt.Printf("Generated %s\n", output)

return nil
Expand Down
8 changes: 4 additions & 4 deletions pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,18 @@ func writeTotals(pdf *gopdf.GoPdf, subtotal float64, tax float64, discount float
pdf.SetY(600)

writeTotal(pdf, subtotalLabel, subtotal)
if tax > 0 {
writeTotal(pdf, taxLabel, tax)
}
if discount > 0 {
writeTotal(pdf, discountLabel, discount)
}
if tax > 0 {
writeTotal(pdf, taxLabel, tax)
}
writeTotal(pdf, totalLabel, subtotal+tax-discount)
}

func writeTotal(pdf *gopdf.GoPdf, label string, total float64) {
p := message.NewPrinter(message.MatchLanguage("en"))
formattedTotal := p.Sprint(total)
formattedTotal := p.Sprintf("%.2f", total)
_ = pdf.SetFont("Inter", "", 9)
pdf.SetTextColor(75, 75, 75)
pdf.SetX(rateColumnOffset)
Expand Down

0 comments on commit d59bc5b

Please sign in to comment.