203 lines
6.0 KiB
Go
203 lines
6.0 KiB
Go
package pdf
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"time"
|
|
|
|
"lektorenplan/domain"
|
|
)
|
|
|
|
// DispatchAssignment is one entry in a recipient's personal overview.
|
|
type DispatchAssignment struct {
|
|
Date time.Time
|
|
TimeMinutes int
|
|
Church string
|
|
Title string
|
|
Role domain.Role
|
|
}
|
|
|
|
// CoverData describes the personal cover sheet of a dispatch packet.
|
|
type CoverData struct {
|
|
TenantName string
|
|
Diocese string
|
|
Range domain.DayRange
|
|
Person domain.Person
|
|
Intro string
|
|
Greeting string
|
|
Assignments []DispatchAssignment
|
|
GeneratedAt time.Time
|
|
}
|
|
|
|
// AssignmentLines returns the personal assignments as readable lines.
|
|
func AssignmentLines(assignments []DispatchAssignment) []string {
|
|
lines := make([]string, 0, len(assignments))
|
|
for _, assignment := range assignments {
|
|
lines = append(lines, fmt.Sprintf("%s · %s · %s · %s (%s)",
|
|
domain.ShortWeekdayName(assignment.Date),
|
|
domain.MediumString(assignment.Date),
|
|
domain.TimeString(assignment.TimeMinutes),
|
|
assignment.Church,
|
|
assignment.Role.Title()))
|
|
}
|
|
return lines
|
|
}
|
|
|
|
func coverOptions(data CoverData) Options {
|
|
return Options{TenantName: data.TenantName, Diocese: data.Diocese, GeneratedAt: data.GeneratedAt}
|
|
}
|
|
|
|
// Cover writes an A4 portrait cover letter with the personal assignment overview.
|
|
func Cover(w io.Writer, data CoverData) error {
|
|
pdf := newPDF("P")
|
|
tr := translator(pdf)
|
|
opts := coverOptions(data)
|
|
|
|
const (
|
|
pageWidth = 210.0
|
|
margin = 18.0
|
|
contentW = pageWidth - margin*2
|
|
)
|
|
|
|
pdf.AddPage()
|
|
|
|
// Header ---------------------------------------------------------------
|
|
setColor(pdf, colors.accent)
|
|
pdf.SetFont("Helvetica", "B", 7)
|
|
pdf.SetXY(margin, margin)
|
|
pdf.CellFormat(contentW, 4, tr("LEKTORENPLAN · "+strings.ToUpper(opts.tenant())), "", 0, "L", false, 0, "")
|
|
|
|
setColor(pdf, colors.ink)
|
|
pdf.SetFont("Times", "B", 20)
|
|
pdf.SetXY(margin, margin+6)
|
|
pdf.CellFormat(contentW, 9, tr("Gottesdienstplan"), "", 0, "L", false, 0, "")
|
|
|
|
setColor(pdf, colors.secondary)
|
|
pdf.SetFont("Helvetica", "", 10)
|
|
pdf.SetXY(margin, margin+16)
|
|
pdf.CellFormat(contentW, 5, tr(data.Range.LongTitle()), "", 0, "L", false, 0, "")
|
|
|
|
setDraw(pdf, colors.line)
|
|
pdf.SetLineWidth(0.3)
|
|
pdf.Line(margin, margin+24, pageWidth-margin, margin+24)
|
|
|
|
// Recipient ------------------------------------------------------------
|
|
y := margin + 33
|
|
setColor(pdf, colors.tertiary)
|
|
pdf.SetFont("Helvetica", "B", 6.5)
|
|
pdf.SetXY(margin, y)
|
|
pdf.CellFormat(contentW, 3.5, tr("EMPFÄNGER"), "", 0, "L", false, 0, "")
|
|
setColor(pdf, colors.ink)
|
|
pdf.SetFont("Times", "B", 13)
|
|
pdf.SetXY(margin, y+4.5)
|
|
pdf.CellFormat(contentW, 6, tr(data.Person.FullName()), "", 0, "L", false, 0, "")
|
|
if strings.TrimSpace(data.Person.Email) != "" {
|
|
setColor(pdf, colors.secondary)
|
|
pdf.SetFont("Helvetica", "", 8)
|
|
pdf.SetXY(margin, y+10.5)
|
|
pdf.CellFormat(contentW, 4, tr(data.Person.Email), "", 0, "L", false, 0, "")
|
|
}
|
|
|
|
y += 22
|
|
setColor(pdf, colors.tertiary)
|
|
pdf.SetFont("Helvetica", "", 7)
|
|
pdf.SetXY(margin, y)
|
|
pdf.CellFormat(contentW, 4, tr("Erstellt am "+domain.MediumString(opts.generated())), "", 0, "R", false, 0, "")
|
|
|
|
// Letter text ----------------------------------------------------------
|
|
y += 10
|
|
setColor(pdf, colors.ink)
|
|
pdf.SetFont("Times", "", 11.5)
|
|
pdf.SetXY(margin, y)
|
|
pdf.MultiCell(contentW, 5.6, tr(strings.TrimSpace(data.Intro)), "", "L", false)
|
|
y = pdf.GetY() + 8
|
|
|
|
// Assignment table -----------------------------------------------------
|
|
setColor(pdf, colors.ink)
|
|
pdf.SetFont("Times", "B", 12.5)
|
|
pdf.SetXY(margin, y)
|
|
pdf.CellFormat(contentW, 6, tr("Ihre Einsätze im Planungszeitraum"), "", 0, "L", false, 0, "")
|
|
y += 9
|
|
|
|
if len(data.Assignments) == 0 {
|
|
setColor(pdf, colors.secondary)
|
|
pdf.SetFont("Times", "", 10.5)
|
|
pdf.SetXY(margin, y)
|
|
pdf.MultiCell(contentW, 5.2, tr("Für Sie sind in diesem Zeitraum keine Einsätze eingetragen."), "", "L", false)
|
|
y = pdf.GetY() + 8
|
|
} else {
|
|
columns := []struct {
|
|
Label string
|
|
Width float64
|
|
}{
|
|
{"Tag", 50}, {"Zeit", 20}, {"Ort", 44}, {"Dienst", contentW - 114},
|
|
}
|
|
setFill(pdf, colors.headerWash)
|
|
pdf.Rect(margin, y, contentW, 7.5, "F")
|
|
x := margin + 2
|
|
setColor(pdf, colors.tertiary)
|
|
pdf.SetFont("Helvetica", "B", 6.3)
|
|
for _, column := range columns {
|
|
pdf.SetXY(x, y+2.2)
|
|
pdf.CellFormat(column.Width-2, 3.4, tr(strings.ToUpper(column.Label)), "", 0, "L", false, 0, "")
|
|
x += column.Width
|
|
}
|
|
y += 7.5
|
|
setDraw(pdf, colors.line)
|
|
pdf.Line(margin, y, pageWidth-margin, y)
|
|
|
|
for _, assignment := range data.Assignments {
|
|
if y > 252 {
|
|
pdf.AddPage()
|
|
y = margin
|
|
}
|
|
rowHeight := 8.2
|
|
setColor(pdf, colors.ink)
|
|
pdf.SetFont("Helvetica", "", 7.6)
|
|
x = margin + 2
|
|
dayLabel := fmt.Sprintf("%s, %d.%d.%d", domain.ShortWeekdayName(assignment.Date),
|
|
domain.DayNumber(assignment.Date), domain.Month(assignment.Date), domain.Year(assignment.Date))
|
|
values := []string{
|
|
dayLabel,
|
|
domain.TimeString(assignment.TimeMinutes),
|
|
assignment.Church,
|
|
assignment.Role.Title() + " · " + assignment.Title,
|
|
}
|
|
for index, column := range columns {
|
|
pdf.SetXY(x, y+2.1)
|
|
pdf.CellFormat(column.Width-2, 3.8, tr(fit(pdf, values[index], column.Width-2)), "", 0, "L", false, 0, "")
|
|
x += column.Width
|
|
}
|
|
y += rowHeight
|
|
setDraw(pdf, colors.line)
|
|
pdf.SetLineWidth(0.15)
|
|
pdf.Line(margin, y, pageWidth-margin, y)
|
|
}
|
|
y += 8
|
|
}
|
|
|
|
// Greeting -------------------------------------------------------------
|
|
if strings.TrimSpace(data.Greeting) != "" {
|
|
if y > 245 {
|
|
pdf.AddPage()
|
|
y = margin
|
|
}
|
|
setColor(pdf, colors.ink)
|
|
pdf.SetFont("Times", "", 11.5)
|
|
pdf.SetXY(margin, y)
|
|
pdf.MultiCell(contentW, 5.6, tr(strings.TrimSpace(data.Greeting)), "", "L", false)
|
|
}
|
|
|
|
// Footer ---------------------------------------------------------------
|
|
setColor(pdf, colors.tertiary)
|
|
pdf.SetFont("Helvetica", "", 6.5)
|
|
pdf.SetXY(margin, 284)
|
|
pdf.CellFormat(contentW, 3.4, tr("LektorenPlan · "+opts.tenant()+" · "+data.Range.ShortTitle()), "", 0, "L", false, 0, "")
|
|
|
|
if err := pdf.Output(w); err != nil {
|
|
return err
|
|
}
|
|
return pdf.Error()
|
|
}
|