Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(addresses): add flag to incl from and to address #21

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Generate invoices from the command line.

```bash
invoice generate --from "Dream, Inc." --to "Imagine, Inc." \
--from-address "1 main road,newyark,567 999" \
--to-address "234/4566 second-main street,washington,568 9899" \
--item "Rubber Duck" --quantity 2 --rate 25 \
--tax 0.13 --discount 0.15 \
--note "For debugging purposes."
Expand All @@ -22,8 +24,24 @@ with `--output`.
open invoice.pdf
```


#### address
To use an address which includes special separators like `,`
enclose the text using a single quote `"`


must be provided to cli as below

`--from-address '"1865 N Charlotte st",Suite32,"Nothaman, SN 02317"`

`--to-address "135 Wehawken St.,\"Raleigh, NC 34217\""`



<img width="574" alt="Example invoice" src="https://github.com/maaslalani/nap/assets/42545625/13153de2-dfa1-41e6-a18e-4d3a5cea5b74">



### Environment

Save repeated information with environment variables:
Expand Down Expand Up @@ -54,6 +72,8 @@ Or, save repeated information with JSON / YAML:
{
"logo": "/path/to/image.png",
"from": "Dream, Inc.",
"from_address": "1 main road,newyark,567 8989",
"to_address": "345/66 main-second street,washington,699-090",
"to": "Imagine, Inc.",
"tax": 0.13,
"items": ["Yellow Rubber Duck", "Special Edition Plaid Rubber Duck"],
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/signintech/gopdf v0.18.0
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -21,7 +22,6 @@ require (
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions invoice.tape
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Set Width 1200
Set Height 600

Type "invoice generate --from 'Dream, Inc.' \" Enter
Type " --from-address '1 main road,newyark,567 8989' \" Enter
Type " --to 'Imagine, Inc.' \" Enter
Type " --to-address '345/66 main-second street,washington,699-090' \" Enter
Type " --date 'June 10, 2023' \" Enter
Type " --tax 0.13 \" Enter
Type " --discount 0.15 \" Enter
Expand Down
44 changes: 25 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ 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"`
Logo string `json:"logo" yaml:"logo"`
From string `json:"from" yaml:"from"`
FromAddress []string `json:"from_address" yaml:"from_address"`
To string `json:"to" yaml:"to"`
ToAddress []string `json:"to_address" yaml:"to_address"`
Date string `json:"date" yaml:"date"`
Due string `json:"due" yaml:"due"`

Items []string `json:"items" yaml:"items"`
Quantities []int `json:"quantities" yaml:"quantities"`
Expand All @@ -42,18 +44,20 @@ type Invoice struct {

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",
Id: time.Now().Format("20060102"),
Title: "INVOICE",
Rates: []float64{25},
Quantities: []int{2},
Items: []string{"Paper Cranes"},
From: "Project Folded, Inc.",
FromAddress: []string{"1", "Main st", "Newyark", "626112"},
To: "Untitled Corporation, Inc.",
ToAddress: []string{"1/56A", "Main st", "Newyark", "626112"},
Date: time.Now().Format("Jan 02, 2006"),
Due: time.Now().AddDate(0, 0, 14).Format("Jan 02, 2006"),
Tax: 0,
Discount: 0,
Currency: "USD",
}
}

Expand All @@ -77,7 +81,9 @@ func init() {

generateCmd.Flags().StringVarP(&file.Logo, "logo", "l", defaultInvoice.Logo, "Company logo")
generateCmd.Flags().StringVarP(&file.From, "from", "f", defaultInvoice.From, "Issuing company")
generateCmd.Flags().StringSliceVarP(&file.FromAddress, "from-address", "s", defaultInvoice.FromAddress, "Issuing company address")
generateCmd.Flags().StringVarP(&file.To, "to", "t", defaultInvoice.To, "Recipient company")
generateCmd.Flags().StringSliceVarP(&file.ToAddress, "to-address", "a", defaultInvoice.ToAddress, "Receiving company address")
generateCmd.Flags().StringVar(&file.Date, "date", defaultInvoice.Date, "Date")
generateCmd.Flags().StringVar(&file.Due, "due", defaultInvoice.Due, "Payment due date")

Expand Down Expand Up @@ -126,9 +132,9 @@ var generateCmd = &cobra.Command{
return err
}

writeLogo(&pdf, file.Logo, file.From)
writeLogo(&pdf, file.Logo, file.From, file.FromAddress)
writeTitle(&pdf, file.Title, file.Id, file.Date)
writeBillTo(&pdf, file.To)
writeBillTo(&pdf, file.To, file.ToAddress)
writeHeaderRow(&pdf)
subtotal := 0.0
for i := range file.Items {
Expand Down
22 changes: 20 additions & 2 deletions pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ const (
totalLabel = "Total"
)

func writeLogo(pdf *gopdf.GoPdf, logo string, from string) {
func writeAddress(pdf *gopdf.GoPdf,
fromAddress []string) {
pdf.Br(18)
rectangle := gopdf.Rect{
W: 100.0,
H: 45.0,
}
for _, v := range fromAddress {
_ = pdf.Cell(&rectangle, v)
pdf.Br(18)
}
}

func writeLogo(pdf *gopdf.GoPdf,
logo string, from string, fromAddress []string) {
if logo != "" {
width, height := getImageDimension(logo)
scaledWidth := 100.0
Expand All @@ -34,6 +48,8 @@ func writeLogo(pdf *gopdf.GoPdf, logo string, from string) {
_ = pdf.SetFont("Inter", "", 12)
pdf.SetTextColor(55, 55, 55)
_ = pdf.Cell(nil, from)
// write address
writeAddress(pdf, fromAddress)
pdf.Br(36)
pdf.SetStrokeColor(225, 225, 225)
pdf.Line(pdf.GetX(), pdf.GetY(), 100, pdf.GetY())
Expand Down Expand Up @@ -68,14 +84,16 @@ func writeDueDate(pdf *gopdf.GoPdf, due string) {
pdf.Br(12)
}

func writeBillTo(pdf *gopdf.GoPdf, to string) {
func writeBillTo(pdf *gopdf.GoPdf,
to string, toAddress []string) {
pdf.SetTextColor(75, 75, 75)
_ = pdf.SetFont("Inter", "", 9)
_ = pdf.Cell(nil, "BILL TO")
pdf.Br(18)
pdf.SetTextColor(75, 75, 75)
_ = pdf.SetFont("Inter", "", 15)
_ = pdf.Cell(nil, to)
writeAddress(pdf, toAddress)
pdf.Br(64)
}

Expand Down