unipdf/creator/invoice_test.go

228 lines
5.3 KiB
Go
Raw Normal View History

2018-10-30 20:51:27 +02:00
package creator
import (
"fmt"
"testing"
2018-10-30 22:14:04 +02:00
"github.com/unidoc/unipdf/v3/model"
2018-10-30 20:51:27 +02:00
)
func TestInvoiceSimple(t *testing.T) {
c := New()
c.NewPage()
logo, err := c.NewImageFromFile(testImageFile1)
if err != nil {
t.Errorf("Fail: %v\n", err)
}
invoice := c.NewInvoice()
2018-11-01 20:58:37 +02:00
// Set invoice logo.
2018-10-30 20:51:27 +02:00
invoice.SetLogo(logo)
2018-11-01 20:58:37 +02:00
// Set invoice information.
2018-10-30 20:51:27 +02:00
invoice.SetNumber("0001")
invoice.SetDate("28/07/2016")
invoice.SetDueDate("28/07/2016")
2018-10-30 22:36:13 +02:00
invoice.AddInfo("Payment terms", "Due on receipt")
invoice.AddInfo("Paid", "No")
2018-10-30 20:51:27 +02:00
2018-11-01 20:58:37 +02:00
// Set invoice addresses.
2018-10-30 20:51:27 +02:00
invoice.SetSellerAddress(&InvoiceAddress{
Name: "John Doe",
Street: "8 Elm Street",
City: "Cambridge",
Zip: "CB14DH",
Country: "United Kingdom",
Phone: "xxx-xxx-xxxx",
Email: "johndoe@email.com",
})
invoice.SetBuyerAddress(&InvoiceAddress{
Name: "Jane Doe",
Street: "9 Elm Street",
City: "London",
Zip: "LB15FH",
Country: "United Kingdom",
Phone: "xxx-xxx-xxxx",
Email: "janedoe@email.com",
})
2018-11-01 20:58:37 +02:00
// Add invoice line items.
for i := 0; i < 75; i++ {
2018-10-30 20:51:27 +02:00
invoice.AddLine(
fmt.Sprintf("Test product #%d", i+1),
"1",
"$10",
"$10",
)
}
2018-11-01 20:58:37 +02:00
// Set invoice totals.
2018-10-30 20:51:27 +02:00
invoice.SetSubtotal("$100.00")
invoice.AddTotalLine("Tax (10%)", "$10.00")
invoice.AddTotalLine("Shipping", "$5.00")
invoice.SetTotal("$115.00")
2018-11-01 20:58:37 +02:00
// Set invoice content sections.
2018-10-30 22:14:04 +02:00
invoice.SetNotes("Notes", "Thank you for your business.")
2018-10-30 20:51:27 +02:00
invoice.SetTerms("Terms and conditions", "Full refund for 60 days after purchase.")
if err = c.Draw(invoice); err != nil {
t.Fatalf("Error drawing: %v", err)
}
// Write output file.
err = c.WriteToFile(tempFile("invoice_simple.pdf"))
2018-10-30 20:51:27 +02:00
if err != nil {
t.Fatalf("Fail: %v\n", err)
}
}
2018-10-30 22:14:04 +02:00
func TestInvoiceAdvanced(t *testing.T) {
fontHelvetica := model.NewStandard14FontMustCompile(model.HelveticaName)
2018-10-30 22:14:04 +02:00
c := New()
c.NewPage()
logo, err := c.NewImageFromFile(testImageFile1)
if err != nil {
t.Errorf("Fail: %v\n", err)
}
white := ColorRGBFrom8bit(255, 255, 255)
lightBlue := ColorRGBFrom8bit(217, 240, 250)
2018-11-01 20:58:37 +02:00
blue := ColorRGBFrom8bit(2, 136, 209)
2018-10-30 22:14:04 +02:00
invoice := c.NewInvoice()
2018-11-01 20:58:37 +02:00
// Set invoice title.
invoice.SetTitle("Unidoc Invoice")
// Customize invoice title style.
titleStyle := invoice.TitleStyle()
titleStyle.Color = blue
titleStyle.Font = fontHelvetica
titleStyle.FontSize = 30
invoice.SetTitleStyle(titleStyle)
// Set invoice logo.
2018-10-30 22:14:04 +02:00
invoice.SetLogo(logo)
2018-11-01 20:58:37 +02:00
// Set invoice information.
2018-10-30 22:36:13 +02:00
invoice.SetNumber("0001")
invoice.SetDate("28/07/2016")
invoice.SetDueDate("28/07/2016")
invoice.AddInfo("Payment terms", "Due on receipt")
invoice.AddInfo("Paid", "No")
2018-10-30 22:14:04 +02:00
2018-11-01 20:58:37 +02:00
// Customize invoice information styles.
2018-10-30 22:36:13 +02:00
for _, info := range invoice.InfoLines() {
descCell, contentCell := info[0], info[1]
descCell.BackgroundColor = lightBlue
contentCell.TextStyle.Font = fontHelvetica
}
2018-10-30 22:14:04 +02:00
2018-11-01 20:58:37 +02:00
// Set invoice addresses.
2018-10-30 22:14:04 +02:00
invoice.SetSellerAddress(&InvoiceAddress{
Name: "John Doe",
Street: "8 Elm Street",
City: "Cambridge",
Zip: "CB14DH",
Country: "United Kingdom",
Phone: "xxx-xxx-xxxx",
Email: "johndoe@email.com",
})
invoice.SetBuyerAddress(&InvoiceAddress{
Name: "Jane Doe",
Street: "9 Elm Street",
City: "London",
Zip: "LB15FH",
Country: "United Kingdom",
Phone: "xxx-xxx-xxxx",
Email: "janedoe@email.com",
})
2018-11-01 20:58:37 +02:00
// Customize address styles.
addressStyle := invoice.AddressStyle()
addressStyle.Font = fontHelvetica
addressStyle.FontSize = 9
addressHeadingStyle := invoice.AddressHeadingStyle()
addressHeadingStyle.Color = blue
addressHeadingStyle.Font = fontHelvetica
addressHeadingStyle.FontSize = 16
invoice.SetAddressStyle(addressStyle)
invoice.SetAddressHeadingStyle(addressHeadingStyle)
// Insert new column.
2018-10-30 22:14:04 +02:00
col := invoice.InsertColumn(2, "Discount")
col.Alignment = CellHorizontalAlignmentRight
// Customize column styles.
for _, column := range invoice.Columns() {
column.BackgroundColor = lightBlue
column.BorderColor = lightBlue
2018-10-30 22:36:13 +02:00
column.TextStyle.FontSize = 9
2018-10-30 22:14:04 +02:00
}
for i := 0; i < 7; i++ {
cells := invoice.AddLine(
fmt.Sprintf("Test product #%d", i+1),
"1",
"0%",
"$10",
"$10",
)
for _, cell := range cells {
cell.BorderColor = white
2018-10-30 22:36:13 +02:00
cell.TextStyle.FontSize = 9
2018-10-30 22:14:04 +02:00
}
}
2018-11-01 20:58:37 +02:00
// Customize total line styles.
2018-10-30 22:14:04 +02:00
titleCell, contentCell := invoice.Total()
titleCell.BackgroundColor = lightBlue
titleCell.BorderColor = lightBlue
contentCell.BackgroundColor = lightBlue
contentCell.BorderColor = lightBlue
invoice.SetSubtotal("$100.00")
invoice.AddTotalLine("Tax (10%)", "$10.00")
invoice.AddTotalLine("Shipping", "$5.00")
invoice.SetTotal("$85.00")
2018-11-01 20:58:37 +02:00
// Set invoice content sections.
2018-10-30 22:14:04 +02:00
invoice.SetNotes("Notes", "Thank you for your business.")
invoice.SetTerms("Terms and conditions", "Full refund for 60 days after purchase.")
invoice.AddSection("Custom section", "This is a custom section.")
2018-11-01 20:58:37 +02:00
// Customize note styles.
2019-01-06 16:50:22 +01:00
noteStyle := invoice.NoteStyle()
2018-11-01 20:58:37 +02:00
noteStyle.Font = fontHelvetica
noteStyle.FontSize = 12
2019-01-06 16:50:22 +01:00
noteHeadingStyle := invoice.NoteHeadingStyle()
2018-11-01 20:58:37 +02:00
noteHeadingStyle.Color = blue
noteHeadingStyle.Font = fontHelvetica
noteHeadingStyle.FontSize = 14
invoice.SetNoteStyle(noteStyle)
invoice.SetNoteHeadingStyle(noteHeadingStyle)
2018-10-30 22:14:04 +02:00
if err = c.Draw(invoice); err != nil {
t.Fatalf("Error drawing: %v", err)
}
// Write output file.
err = c.WriteToFile(tempFile("invoice_advanced.pdf"))
2018-10-30 22:14:04 +02:00
if err != nil {
t.Fatalf("Fail: %v\n", err)
}
}