Skip to content

Commit 3e55b11

Browse files
committed
Merge branch 'development_invoices_pdf' of github.com:codescalers/cloud4students into development_audit
2 parents e2919d0 + aebc353 commit 3e55b11

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

server/internal/pdf_generator.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal
22

33
import (
44
"fmt"
5+
"strconv"
56
"time"
67

78
"github.com/codescalers/cloud4students/models"
@@ -235,18 +236,18 @@ func (in *InvoicePDF) invoiceSection() error {
235236
return err
236237
}
237238

238-
textWidth, err = in.pdf.MeasureTextWidth(in.invoice.CreatedAt.Format("June 1, 2020"))
239+
textWidth, err = in.pdf.MeasureTextWidth(in.invoice.CreatedAt.Format("January 2, 2006"))
239240
if err != nil {
240241
return err
241242
}
242243

243244
in.pdf.SetXY(in.startX+540-textWidth, in.startY+35)
244-
if err := in.pdf.Cell(nil, in.invoice.CreatedAt.Format("June 1, 2020")); err != nil {
245+
if err := in.pdf.Cell(nil, in.invoice.CreatedAt.Format("January 2, 2006")); err != nil {
245246
return err
246247
}
247248

248249
in.pdf.SetXY(in.startX+540-textWidth, in.startY+50)
249-
return in.pdf.Cell(nil, in.invoice.CreatedAt.Format("June 1, 2020"))
250+
return in.pdf.Cell(nil, in.invoice.CreatedAt.Format("January 2, 2006"))
250251
}
251252

252253
func (in *InvoicePDF) userDetails() error {
@@ -297,14 +298,14 @@ func (in *InvoicePDF) summary() error {
297298
return err
298299
}
299300

300-
totalText := fmt.Sprintf("$%v", in.invoice.Total)
301+
totalText := formatFloat(in.invoice.Total)
301302
totalTextWidth, err := in.pdf.MeasureTextWidth(totalText)
302303
if err != nil {
303304
return err
304305
}
305306

306307
in.pdf.SetXY(in.startX+540-totalTextWidth, in.startY+35)
307-
return in.pdf.Cell(nil, fmt.Sprintf("$%v", in.invoice.Total))
308+
return in.pdf.Cell(nil, formatFloat(in.invoice.Total))
308309
}
309310

310311
func (in *InvoicePDF) totalDue() error {
@@ -317,14 +318,14 @@ func (in *InvoicePDF) totalDue() error {
317318
return err
318319
}
319320

320-
totalText := fmt.Sprintf("$%v", in.invoice.Total)
321+
totalText := formatFloat(in.invoice.Total)
321322
totalTextWidth, err := in.pdf.MeasureTextWidth(totalText)
322323
if err != nil {
323324
return err
324325
}
325326

326327
in.pdf.SetXY(in.startX+540-totalTextWidth, in.startY)
327-
if err := in.pdf.Cell(nil, fmt.Sprintf("$%v", in.invoice.Total)); err != nil {
328+
if err := in.pdf.Cell(nil, formatFloat(in.invoice.Total)); err != nil {
328329
return err
329330
}
330331

@@ -385,14 +386,14 @@ func (in *InvoicePDF) tableHeader() error {
385386
return err
386387
}
387388

388-
totalText := fmt.Sprintf("$%v", in.invoice.Total)
389+
totalText := formatFloat(in.invoice.Total)
389390
totalTextWidth, err := in.pdf.MeasureTextWidth(totalText)
390391
if err != nil {
391392
return err
392393
}
393394

394395
in.pdf.SetXY(in.startX+540-totalTextWidth, in.startY)
395-
if err := in.pdf.Cell(nil, fmt.Sprintf("$%v", in.invoice.Total)); err != nil {
396+
if err := in.pdf.Cell(nil, formatFloat(in.invoice.Total)); err != nil {
396397
return err
397398
}
398399

@@ -429,13 +430,13 @@ func (in *InvoicePDF) tableContent() error {
429430
return err
430431
}
431432

432-
costTextWidth, err := in.pdf.MeasureTextWidth(fmt.Sprintf("$%v", d.Cost))
433+
costTextWidth, err := in.pdf.MeasureTextWidth(formatFloat(d.Cost))
433434
if err != nil {
434435
return err
435436
}
436437

437438
in.pdf.SetXY(in.startX+540-costTextWidth, y)
438-
if err := in.pdf.Cell(nil, fmt.Sprintf("$%v", d.Cost)); err != nil {
439+
if err := in.pdf.Cell(nil, formatFloat(d.Cost)); err != nil {
439440
return err
440441
}
441442

@@ -460,3 +461,12 @@ func (in *InvoicePDF) tableContent() error {
460461

461462
return nil
462463
}
464+
465+
func formatFloat(f float64) string {
466+
// Check if the number has a fractional part
467+
if f == float64(int(f)) {
468+
return strconv.Itoa(int(f))
469+
}
470+
471+
return fmt.Sprintf("%.2f", f)
472+
}

0 commit comments

Comments
 (0)