Skip to content

Commit

Permalink
Update invoice generator
Browse files Browse the repository at this point in the history
  • Loading branch information
chunvv committed Aug 10, 2021
1 parent 5f6d290 commit 9abceb4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 100 deletions.
4 changes: 3 additions & 1 deletion after_commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ Created by Trung Vu on AD 2021/07/11.
package invoice_generator

type AfterCommission struct {
Amount int `json:"amount,omitempty"`
Amount int `json:"amount,omitempty"`
ConsumptionTax int `json:"consumption_tax,omitempty"`
IsDomesticCreator bool `json:"is_domestic_creator,omitempty"`
}
130 changes: 46 additions & 84 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ package invoice_generator
import (
"fmt"
"github.com/jung-kurt/gofpdf"
"github.com/shopspring/decimal"
"strconv"
"time"
)

Expand Down Expand Up @@ -164,109 +162,73 @@ func (d *Document) appendNotes(pdf *gofpdf.Fpdf) {
}

func (d *Document) appendTotal(pdf *gofpdf.Fpdf) {
total, _ := decimal.NewFromString("0")

for _, item := range d.Items {
temp, _ := decimal.NewFromString(strconv.Itoa(item.Total))
total = total.Add(temp)
}

//// Tax
//totalTax := decimal.NewFromFloat(0)
//if d.Discount == nil {
// for _, item := range d.Items {
// totalTax = totalTax.Add(item.taxWithDiscount())
// }
//} else {
// discountType, discountAmount := d.Discount.getDiscount()
// discountPercent := discountAmount
// if discountType == "amount" {
// // Get percent from total discounted
// discountPercent = discountAmount.Mul(decimal.NewFromFloat(100)).Div(totalWithDiscount)
// }
//
// for _, item := range d.Items {
// if item.Tax != nil {
// taxType, taxAmount := item.Tax.getTax()
// if taxType == "amount" {
// // If tax type is amount, juste add amount to tax
// totalTax = totalTax.Add(taxAmount)
// } else {
// // Else, remove doc discount % from item total without tax and item discount
// itemTotal := item.totalWithoutTaxAndWithDiscount()
// toSub := discountPercent.Mul(itemTotal).Div(decimal.NewFromFloat(100))
// itemTotalDiscounted := itemTotal.Sub(toSub)
//
// // Then recompute tax on itemTotalDiscounted
// itemTaxDiscounted := taxAmount.Mul(itemTotalDiscounted).Div(decimal.NewFromFloat(100))
//
// totalTax = totalTax.Add(itemTaxDiscounted)
// }
// }
// }
//}
//
//// finalTotal
//totalWithTax := total.Add(totalTax)
//if d.Discount != nil {
// totalWithTax = totalWithDiscount.Add(totalTax)
//}

pdf.SetY(pdf.GetY() + 5)
// Draw after commission
pdf.SetX(162)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(160, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(40, 10, formatAmount(d.AfterCommission.Amount), "0", 0, "L", false, 0, "")

pdf.SetX(120)
pdf.Rect(120, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(38, 10, encodeString(d.Options.TextAfterCommissionTotal), "0", 0, "R", false, 0, "")
pdf.SetY(pdf.GetY() + 11)
if d.AfterCommission.IsDomesticCreator {
pdf.SetX(82)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(80, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(60, 10, formatAmount(d.AfterCommission.Amount)+" (内、消費税"+formatAmount(d.AfterCommission.ConsumptionTax)+")", "0", 0, "L", false, 0, "")
pdf.SetX(30)
pdf.Rect(20, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(40, 10, encodeString(d.Options.TextAfterCommissionTotal), "0", 0, "R", false, 0, "")
pdf.SetY(pdf.GetY() + 11)
} else {
pdf.SetX(82)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(80, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(60, 10, formatAmount(d.AfterCommission.Amount), "0", 0, "L", false, 0, "")
pdf.SetX(30)
pdf.Rect(20, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(40, 10, encodeString(d.Options.TextAfterCommissionTotalOversea), "0", 0, "R", false, 0, "")
pdf.SetY(pdf.GetY() + 11)
}

if d.ConsumptionTax != nil {
// Draw consumption tax
pdf.SetX(162)
pdf.SetX(82)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(160, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(40, 10, formatAmount(d.ConsumptionTax.Amount), "0", 0, "L", false, 0, "")
pdf.Rect(80, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(60, 10, formatAmount(d.ConsumptionTax.Amount), "0", 0, "L", false, 0, "")

pdf.SetX(120)
pdf.Rect(120, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(38, 10, encodeString(d.Options.TextConsumptionTaxTotal), "0", 0, "R", false, 0, "")
pdf.SetX(30)
pdf.Rect(20, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(40, 10, encodeString(d.Options.TextConsumptionTaxTotal), "0", 0, "R", false, 0, "")
pdf.SetY(pdf.GetY() + 15)
}

// Draw withholding tax
pdf.SetX(162)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(160, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(40, 10, "("+formatAmount(d.WithholdingTax.Amount)+")", "0", 0, "L", false, 0, "")
if d.WithholdingTax != nil {
pdf.SetX(82)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(80, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(60, 10, "("+formatAmount(d.WithholdingTax.Amount)+")", "0", 0, "L", false, 0, "")

pdf.SetX(120)
pdf.Rect(120, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(38, 10, encodeString(d.Options.TextWithholdingTaxTotal), "0", 0, "R", false, 0, "")
pdf.SetY(pdf.GetY() + 11)
pdf.SetX(30)
pdf.Rect(20, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(40, 10, encodeString(d.Options.TextWithholdingTaxTotal), "0", 0, "R", false, 0, "")
pdf.SetY(pdf.GetY() + 11)
}

// Draw payment free
pdf.SetX(162)
pdf.SetX(82)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(160, pdf.GetY(), 40, 10, "F")
pdf.Rect(80, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(40, 10, "("+formatAmount(d.PaymentFree.Amount)+")", "0", 0, "L", false, 0, "")

pdf.SetX(120)
pdf.Rect(120, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(38, 10, encodeString(d.Options.TextPaymentTotal), "0", 0, "R", false, 0, "")
pdf.SetX(30)
pdf.Rect(20, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(40, 10, encodeString(d.Options.TextPaymentTotal), "0", 0, "R", false, 0, "")
pdf.SetY(pdf.GetY() + 11)

// Draw paid amount
pdf.SetX(162)
pdf.SetX(82)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(160, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(40, 10, formatAmount(d.PaidAmount.Amount), "0", 0, "L", false, 0, "")
pdf.Rect(80, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(60, 10, formatAmount(d.PaidAmount.Amount), "0", 0, "L", false, 0, "")

pdf.SetX(120)
pdf.SetX(30)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(120, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(38, 10, encodeString(d.Options.TextTotalTotal), "0", 0, "R", false, 0, "")
pdf.Rect(20, pdf.GetY(), 60, 10, "F")
pdf.CellFormat(40, 10, encodeString(d.Options.TextTotalTotal), "0", 0, "R", false, 0, "")
}
4 changes: 2 additions & 2 deletions contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Contact) appendContactTODoc(x float64, y float64, fill bool, logoAlign
}

if fill {
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.SetFillColor(255, 255, 255)
} else {
pdf.SetFillColor(255, 255, 255)
}
Expand All @@ -69,8 +69,8 @@ func (c *Contact) appendContactTODoc(x float64, y float64, fill bool, logoAlign

pdf.SetFont("deja", "", 10)
pdf.SetXY(x, pdf.GetY()+10)
pdf.MultiCell(70, 5, c.Address.ToString(), "0", "L", false)
pdf.MultiCell(70, 5, c.Email, "0", "L", false)
pdf.MultiCell(70, 5, c.Address.ToString(), "0", "L", false)
pdf.SetXY(x, pdf.GetY()+10)
}

Expand Down
13 changes: 6 additions & 7 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (

func TestNew(t *testing.T) {
doc, _ := New(Invoice, &Options{
TextTypeInvoice: "使用料支払報告書",
TextTypeInvoice: "ロイヤリティ支払報告書",
AutoPrint: true,
})

doc.SetFooter(&HeaderFooter{
Text: "<center>ご不明な点がございましたら、 DKT株式会社 ([email protected]) までお問い合わせください。</center>",
Text: "<center>*ロイヤリティは所得税法上の源泉徴収の対象となる使用料の支払に該当し、所定の源泉徴収税率を乗じて算出しています。</center>\n" +
"<center>*上記を確認の上、内容に異議がある場合には2週間以内に連絡下さいますようお願い致します。</center>",
Pagination: true,
})

Expand All @@ -22,17 +23,15 @@ func TestNew(t *testing.T) {
doc.SetDate("2021/07/10")

doc.SetCompany(&Contact{
Name: "DKT株式会社",
Address: &Address{Country: "日本", City: "東京都", Address: "港区西新橋1-22-5", Address2: "新橋TSビル6階", PostalCode: "〒105-0003"},
Email: "[email protected]",
Name: "PostPrime(DKT株式会社)",
Address: &Address{Country: " ", City: " ", Address: " ", Address2: " ", PostalCode: " "},
})

doc.SetCustomer(&Contact{Name: "安部 慎之介 様"})

//doc.AppendItem(&Item{Name: "対象期間のDKTの売上", Total: 900000, Tax: &Tax{Amount: 90000}})

doc.SetAfterCommission(&AfterCommission{Amount: 630000})
doc.SetConsumptionTax(&ConsumptionTax{Amount: 63000})
doc.SetAfterCommission(&AfterCommission{Amount: 630000, ConsumptionTax: 64545, IsDomesticCreator: true})
doc.SetWithholdingTax(&WithholdingTax{Amount: 64323})
doc.SetPaymentFree(&PaymentFree{Amount: 35244})
doc.SetPaidAmount(&PaidAmount{Amount: 593433})
Expand Down
13 changes: 7 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ type Options struct {
TextTypeInvoice string `default:"請求書" json:"text_type_invoice,omitempty"`
TextTypeReceipt string `default:"領収書" json:"text_type_delivery_note,omitempty"`

TextNumberTitle string `default:"報告番号" json:"text_version_title,omitempty"`
TextNumberTitle string `default:"ロイヤリティ支払報告書No" json:"text_version_title,omitempty"`
TextDateTitle string `default:"発行日" json:"text_date_title,omitempty"`

TextItemsNameTitle string `default:"摘要" json:"text_items_name_title,omitempty"`
TextItemsTotalTTCTitle string `default:"売上金額" json:"text_items_total_ttc_title,omitempty"`
TextItemsTotalConsumptionTaxTitle string `default:"消費税額" json:"text_items_total_consumption_tax_title,omitempty"`

TextAfterCommissionTotal string `default:"使用料" json:"text_commission_total,omitempty"`
TextConsumptionTaxTotal string `default:"消費税額等" json:"text_consumption_tax_total,omitempty"`
TextPaymentTotal string `default:"決済手数料" json:"text_payment_total,omitempty"`
TextWithholdingTaxTotal string `default:"源泉所得税" json:"text_withholding_tax_total,omitempty"`
TextTotalTotal string `default:"お支払い金額" json:"text_total_total,omitempty"`
TextAfterCommissionTotal string `default:"ロイヤリティ(消費税込み)" json:"text_commission_total,omitempty"`
TextAfterCommissionTotalOversea string `default:"ロイヤリティ" json:"text_commission_total_oversea,omitempty"`
TextConsumptionTaxTotal string `default:"消費税額等" json:"text_consumption_tax_total,omitempty"`
TextPaymentTotal string `default:"決済手数料" json:"text_payment_total,omitempty"`
TextWithholdingTaxTotal string `default:"源泉所得税" json:"text_withholding_tax_total,omitempty"`
TextTotalTotal string `default:"お支払い金額" json:"text_total_total,omitempty"`
}
Binary file modified payment_details_example.pdf
Binary file not shown.

0 comments on commit 9abceb4

Please sign in to comment.