395 lines
12 KiB
Go
395 lines
12 KiB
Go
package pdf
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"cotizador/internal/domain"
|
|
)
|
|
|
|
type RegistroHoraPDF struct {
|
|
Fecha time.Time
|
|
HoraInicio string
|
|
HoraFin string
|
|
Horas float64
|
|
TarifaHora float64
|
|
Descripcion string
|
|
EstadoPago string
|
|
}
|
|
|
|
type NotaHorasPDFData struct {
|
|
Titulo string
|
|
Numero string
|
|
ClienteNombre string
|
|
ClienteEmpresa string
|
|
Proyecto string
|
|
AsesorNombre string
|
|
Moneda string
|
|
IncluirIVA bool
|
|
Modo string
|
|
MostrarEstado bool
|
|
MostrarBancarios bool
|
|
Registros []RegistroHoraPDF
|
|
ConfigBancaria map[string]string
|
|
ColorPrimario string
|
|
ColorSecundario string
|
|
LogoBase64 string
|
|
LogoMime string
|
|
FechaEmision time.Time
|
|
}
|
|
|
|
type notaHorasGrupo struct {
|
|
Clave string
|
|
Etiqueta string
|
|
Count int
|
|
Horas float64
|
|
Importe float64
|
|
}
|
|
|
|
func modoAgrupacionLabel(modo string) string {
|
|
switch modo {
|
|
case "dia":
|
|
return "Por día"
|
|
case "semana":
|
|
return "Por semana"
|
|
case "mes":
|
|
return "Por mes"
|
|
default:
|
|
return "Detalle"
|
|
}
|
|
}
|
|
|
|
func etiquetaTotalNota(titulo string) string {
|
|
low := strings.ToLower(titulo)
|
|
switch {
|
|
case strings.Contains(low, "pagadas"):
|
|
return "Total pagado"
|
|
case strings.Contains(low, "por pagar"):
|
|
return "Total a pagar"
|
|
default:
|
|
return "Total"
|
|
}
|
|
}
|
|
|
|
func GenerateNotaHorasPDF(data NotaHorasPDFData) ([]byte, error) {
|
|
primary := safeHex(data.ColorPrimario, defaultPrimary)
|
|
dark := safeHex(data.ColorSecundario, defaultDark)
|
|
muted := "#6b7280"
|
|
border := "#d1d5db"
|
|
headBG := "#f3f4f6"
|
|
|
|
titulo := strings.TrimSpace(data.Titulo)
|
|
if titulo == "" {
|
|
titulo = "Nota de horas por pagar"
|
|
}
|
|
modo := strings.ToLower(strings.TrimSpace(data.Modo))
|
|
if modo != "detalle" && modo != "dia" && modo != "semana" && modo != "mes" {
|
|
modo = "detalle"
|
|
}
|
|
modoLabel := modoAgrupacionLabel(modo)
|
|
|
|
emitida := data.FechaEmision
|
|
if emitida.IsZero() {
|
|
emitida = time.Now()
|
|
}
|
|
|
|
// Copy and sort records chronologically.
|
|
regs := make([]RegistroHoraPDF, len(data.Registros))
|
|
copy(regs, data.Registros)
|
|
sort.Slice(regs, func(i, j int) bool {
|
|
if regs[i].Fecha.Equal(regs[j].Fecha) {
|
|
return regs[i].HoraInicio < regs[j].HoraInicio
|
|
}
|
|
return regs[i].Fecha.Before(regs[j].Fecha)
|
|
})
|
|
|
|
// Totals.
|
|
var totalHoras, subtotal float64
|
|
for _, r := range regs {
|
|
totalHoras += r.Horas
|
|
subtotal += r.Horas * r.TarifaHora
|
|
}
|
|
totalHoras = domain.Round2(totalHoras)
|
|
subtotal = domain.Round2(subtotal)
|
|
iva := 0.0
|
|
if data.IncluirIVA {
|
|
iva = domain.Round2(subtotal * domain.IVARate)
|
|
}
|
|
total := domain.Round2(subtotal + iva)
|
|
|
|
// Periodo text.
|
|
periodoTexto := "Periodo: todos los registros"
|
|
if len(regs) > 0 {
|
|
minF := domain.FormatFechaRegistro(regs[0].Fecha)
|
|
maxF := domain.FormatFechaRegistro(regs[len(regs)-1].Fecha)
|
|
if minF == maxF {
|
|
periodoTexto = "Periodo: " + minF
|
|
} else {
|
|
periodoTexto = fmt.Sprintf("Periodo: %s al %s", minF, maxF)
|
|
}
|
|
}
|
|
|
|
// Grouping if not detalle.
|
|
var grupos []notaHorasGrupo
|
|
if modo != "detalle" {
|
|
m := make(map[string]*notaHorasGrupo)
|
|
for _, r := range regs {
|
|
per := domain.PeriodoAgrupacionDe(r.Fecha, modo)
|
|
g, ok := m[per.Clave]
|
|
if !ok {
|
|
g = ¬aHorasGrupo{Clave: per.Clave, Etiqueta: per.Etiqueta}
|
|
m[per.Clave] = g
|
|
}
|
|
g.Count++
|
|
g.Horas += r.Horas
|
|
g.Importe += r.Horas * r.TarifaHora
|
|
}
|
|
for _, g := range m {
|
|
g.Horas = domain.Round2(g.Horas)
|
|
g.Importe = domain.Round2(g.Importe)
|
|
grupos = append(grupos, *g)
|
|
}
|
|
sort.Slice(grupos, func(i, j int) bool {
|
|
return grupos[i].Clave < grupos[j].Clave
|
|
})
|
|
}
|
|
|
|
fmtMoney := func(n float64) string {
|
|
return formatMoney(n, data.Moneda)
|
|
}
|
|
|
|
c := newCanvas(45, 55, 50, 50)
|
|
c.maxY = c.ph - c.mBottom
|
|
logo := registerLogo(c.doc, "nota_logo", data.LogoBase64, data.LogoMime)
|
|
|
|
c.doc.AddPage()
|
|
y := c.mTop
|
|
|
|
// ── ENCABEZADO ────────────────────────────────
|
|
logoBottom := y
|
|
if logo.ok {
|
|
_, lh := logo.drawFit(c.doc, c.L, y, 46, 46)
|
|
logoBottom = y + lh
|
|
}
|
|
txtX := c.L
|
|
if logo.ok {
|
|
txtX = c.L + 58
|
|
}
|
|
titleW := c.W - 170
|
|
if logo.ok {
|
|
titleW = c.W - 58 - 115
|
|
}
|
|
hTit := c.drawText(titulo, txtX, y+2, titleW, "B", 18, dark, "L")
|
|
sub := fmt.Sprintf("Cotización %s · %s", data.Numero, data.Proyecto)
|
|
hSub := c.drawText(sub, txtX, y+2+hTit+1, titleW, "", 9, muted, "L")
|
|
c.drawSingleLine("Emitida: "+domain.FormatFechaRegistro(emitida), c.L, y+2, c.W, "", 9, muted, "R")
|
|
|
|
yTextEnd := y + 2 + hTit + 1 + hSub + 6
|
|
y = y + 50
|
|
if logoBottom+6 > y {
|
|
y = logoBottom + 6
|
|
}
|
|
if yTextEnd > y {
|
|
y = yTextEnd
|
|
}
|
|
c.hLine(c.L, c.L+c.W, y, primary, 2.5)
|
|
y += 14
|
|
|
|
// ── META ──────────────────────────────────────
|
|
cliente := data.ClienteEmpresa
|
|
if strings.TrimSpace(cliente) == "" {
|
|
cliente = data.ClienteNombre
|
|
}
|
|
drawMetaLine := func(label, val string) {
|
|
c.setFont("B", 9.5)
|
|
lw := c.doc.GetStringWidth(label) + 2
|
|
c.drawSingleLine(label, c.L, y, lw, "B", 9.5, dark, "L")
|
|
h := c.drawText(val, c.L+lw, y, c.W-lw, "", 9.5, dark, "L")
|
|
y += h + 2
|
|
}
|
|
drawMetaLine("Cliente: ", cliente)
|
|
if strings.TrimSpace(data.ClienteEmpresa) != "" && strings.TrimSpace(data.ClienteNombre) != "" {
|
|
drawMetaLine("Contacto: ", data.ClienteNombre)
|
|
}
|
|
if strings.TrimSpace(data.AsesorNombre) != "" {
|
|
drawMetaLine("Asesor: ", data.AsesorNombre)
|
|
}
|
|
hPer := c.drawText(fmt.Sprintf("%s · Desglose %s", periodoTexto, strings.ToLower(modoLabel)), c.L, y, c.W, "", 8.5, muted, "L")
|
|
y += hPer + 12
|
|
|
|
// ── TABLA ─────────────────────────────────────
|
|
if modo == "detalle" {
|
|
wEstado := 0.0
|
|
if data.MostrarEstado {
|
|
wEstado = 58.0
|
|
}
|
|
cFecha := c.L
|
|
wFecha, wHorario, wHoras, wTarifa, wImporte := 62.0, 72.0, 42.0, 52.0, 58.0
|
|
cHorario := cFecha + wFecha
|
|
cHoras := cHorario + wHorario
|
|
cTarifa := cHoras + wHoras
|
|
cImporte := cTarifa + wTarifa
|
|
cDesc := cImporte + wImporte + 6
|
|
estadoPad := 0.0
|
|
if wEstado > 0 {
|
|
estadoPad = 6.0
|
|
}
|
|
wDesc := c.L + c.W - cDesc - wEstado - estadoPad
|
|
cEstado := c.L + c.W - wEstado
|
|
|
|
drawHeader := func(yy float64) float64 {
|
|
c.rectFill(c.L, yy, c.W, 16, headBG)
|
|
c.drawSingleLine("FECHA", cFecha+4, yy+4, wFecha-4, "B", 7, dark, "L")
|
|
c.drawSingleLine("HORARIO", cHorario+4, yy+4, wHorario-4, "B", 7, dark, "L")
|
|
c.drawSingleLine("HORAS", cHoras, yy+4, wHoras-2, "B", 7, dark, "R")
|
|
c.drawSingleLine("TARIFA", cTarifa, yy+4, wTarifa-2, "B", 7, dark, "R")
|
|
c.drawSingleLine("IMPORTE", cImporte, yy+4, wImporte-2, "B", 7, dark, "R")
|
|
c.drawSingleLine("DESCRIPCIÓN", cDesc, yy+4, wDesc, "B", 7, dark, "L")
|
|
if wEstado > 0 {
|
|
c.drawSingleLine("ESTADO", cEstado, yy+4, wEstado, "B", 7, dark, "L")
|
|
}
|
|
return yy + 16
|
|
}
|
|
|
|
y = drawHeader(y)
|
|
for _, r := range regs {
|
|
descH := c.textHeight(r.Descripcion, wDesc, "", 8)
|
|
rowH := descH + 8
|
|
if rowH < 16 {
|
|
rowH = 16
|
|
}
|
|
if y+rowH > c.maxY {
|
|
y = drawHeader(c.need(rowH+16, y))
|
|
}
|
|
c.drawSingleLine(domain.FormatFechaRegistro(r.Fecha), cFecha+4, y+4, wFecha-4, "", 8, dark, "L")
|
|
c.drawSingleLine(fmt.Sprintf("%s-%s", r.HoraInicio, r.HoraFin), cHorario+4, y+4, wHorario-4, "", 8, dark, "L")
|
|
c.drawSingleLine(fmt.Sprintf("%.2f", r.Horas), cHoras, y+4, wHoras-2, "", 8, dark, "R")
|
|
c.drawSingleLine(fmtMoney(r.TarifaHora), cTarifa, y+4, wTarifa-2, "", 8, dark, "R")
|
|
c.drawSingleLine(fmtMoney(r.Horas*r.TarifaHora), cImporte, y+4, wImporte-2, "", 8, dark, "R")
|
|
c.drawText(r.Descripcion, cDesc, y+4, wDesc, "", 8, dark, "L")
|
|
if wEstado > 0 {
|
|
est := "Por pagar"
|
|
if domain.EsPagada(r.EstadoPago) {
|
|
est = "Pagada"
|
|
}
|
|
c.drawSingleLine(est, cEstado, y+4, wEstado, "", 8, dark, "L")
|
|
}
|
|
c.hLine(c.L, c.L+c.W, y+rowH, border, 0.3)
|
|
y += rowH
|
|
}
|
|
} else {
|
|
wReg, wHor, wImp := 90.0, 90.0, 90.0
|
|
cPer := c.L
|
|
wPer := c.W - wReg - wHor - wImp
|
|
cReg := cPer + wPer
|
|
cHor := cReg + wReg
|
|
cImp := cHor + wHor
|
|
|
|
drawHeader := func(yy float64) float64 {
|
|
c.rectFill(c.L, yy, c.W, 16, headBG)
|
|
c.drawSingleLine(strings.ToUpper(modoLabel), cPer+4, yy+4, wPer-6, "B", 7, dark, "L")
|
|
c.drawSingleLine("REGISTROS", cReg, yy+4, wReg-2, "B", 7, dark, "R")
|
|
c.drawSingleLine("HORAS", cHor, yy+4, wHor-2, "B", 7, dark, "R")
|
|
c.drawSingleLine("IMPORTE", cImp, yy+4, wImp-2, "B", 7, dark, "R")
|
|
return yy + 16
|
|
}
|
|
|
|
y = drawHeader(y)
|
|
for _, g := range grupos {
|
|
rowH := 16.0
|
|
if y+rowH > c.maxY {
|
|
y = drawHeader(c.need(rowH+16, y))
|
|
}
|
|
c.drawSingleLine(g.Etiqueta, cPer+4, y+4, wPer-6, "", 8, dark, "L")
|
|
c.drawSingleLine(fmt.Sprintf("%d", g.Count), cReg, y+4, wReg-2, "", 8, dark, "R")
|
|
c.drawSingleLine(fmt.Sprintf("%.2f", g.Horas), cHor, y+4, wHor-2, "", 8, dark, "R")
|
|
c.drawSingleLine(fmtMoney(g.Importe), cImp, y+4, wImp-2, "", 8, dark, "R")
|
|
c.hLine(c.L, c.L+c.W, y+rowH, border, 0.3)
|
|
y += rowH
|
|
}
|
|
}
|
|
|
|
// ── TOTALES ───────────────────────────────────
|
|
y += 12
|
|
totW := 240.0
|
|
totX := c.L + c.W - totW
|
|
drawTotalLine := func(label, val string, bold, topLine bool) {
|
|
y = c.need(20, y)
|
|
if topLine {
|
|
c.hLine(totX, totX+totW, y, primary, 1.2)
|
|
y += 5
|
|
}
|
|
style := ""
|
|
size := 9.0
|
|
lblColor := muted
|
|
step := 15.0
|
|
if bold {
|
|
style = "B"
|
|
size = 12.0
|
|
lblColor = dark
|
|
step = 18.0
|
|
}
|
|
c.drawSingleLine(label, totX, y, totW-95, style, size, lblColor, "L")
|
|
c.drawSingleLine(val, totX+totW-95, y, 95, style, size, dark, "R")
|
|
y += step
|
|
}
|
|
|
|
drawTotalLine("Total de horas", fmt.Sprintf("%.2f h", totalHoras), false, false)
|
|
drawTotalLine("Subtotal", fmtMoney(subtotal), false, false)
|
|
if data.IncluirIVA {
|
|
drawTotalLine("IVA (16%)", fmtMoney(iva), false, false)
|
|
}
|
|
drawTotalLine(etiquetaTotalNota(titulo), fmtMoney(total), true, true)
|
|
|
|
// ── DATOS PARA EL PAGO ────────────────────────
|
|
if data.MostrarBancarios {
|
|
dp := extractBankInfo(data.ConfigBancaria)
|
|
boxH := 52.0
|
|
y = c.need(boxH+34, y) + 16
|
|
c.rectFill(c.L, y+1, 3, 10, primary)
|
|
c.drawSingleLine("Datos para el pago", c.L+10, y, c.W-10, "B", 10, dark, "L")
|
|
y += 16
|
|
|
|
c.rectFillStroke(c.L, y, c.W, boxH, headBG, border, 0.5)
|
|
c.drawSingleLine("Transferencia Nacional", c.L+8, y+5, c.W-16, "B", 8, dark, "L")
|
|
if dp.CuentaNacional != "" {
|
|
c.drawSingleLine("Cuenta: "+dp.CuentaNacional, c.L+8, y+18, c.W*0.48, "", 8.5, dark, "L")
|
|
}
|
|
if dp.ClabeNacional != "" {
|
|
c.drawSingleLine("CLABE: "+dp.ClabeNacional, c.L+c.W*0.5, y+18, c.W*0.48, "", 8.5, dark, "L")
|
|
}
|
|
c.drawSingleLine("Beneficiario: "+dp.RazonSocial, c.L+8, y+30, c.W*0.48, "", 8.5, dark, "L")
|
|
c.drawSingleLine("RFC: "+dp.RFC, c.L+c.W*0.5, y+30, c.W*0.48, "", 8.5, dark, "L")
|
|
if dp.BancoNacional != "" {
|
|
c.drawSingleLine("Banco: "+dp.BancoNacional, c.L+8, y+42, c.W*0.48, "", 8.5, dark, "L")
|
|
}
|
|
y += boxH + 8
|
|
|
|
if dp.ClabeInternacional != "" || dp.Swift != "" {
|
|
y = c.need(boxH, y)
|
|
c.rectFillStroke(c.L, y, c.W, boxH, headBG, border, 0.5)
|
|
c.drawSingleLine("Transferencia Internacional", c.L+8, y+5, c.W-16, "B", 8, dark, "L")
|
|
c.drawSingleLine("Beneficiario: "+dp.RazonSocial, c.L+8, y+18, c.W*0.48, "", 8.5, dark, "L")
|
|
if dp.ClabeInternacional != "" {
|
|
c.drawSingleLine("CLABE: "+dp.ClabeInternacional, c.L+c.W*0.5, y+18, c.W*0.48, "", 8.5, dark, "L")
|
|
}
|
|
c.drawSingleLine("Banco: BBVA México", c.L+8, y+30, c.W*0.48, "", 8.5, dark, "L")
|
|
if dp.Swift != "" {
|
|
c.drawSingleLine("SWIFT: "+dp.Swift, c.L+c.W*0.5, y+30, c.W*0.48, "", 8.5, dark, "L")
|
|
}
|
|
y += boxH + 8
|
|
}
|
|
}
|
|
|
|
// ── PIE ───────────────────────────────────────
|
|
y = c.need(30, y) + 14
|
|
c.drawText(
|
|
"Documento generado desde el Cotizador. Conteo de horas trabajadas para cobro del proyecto.",
|
|
c.L, y, c.W, "", 8, muted, "C",
|
|
)
|
|
|
|
return c.bytes()
|
|
}
|