Update StockBajoEmailBuilder.go

Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
fede committed 2026-12-01 17:53:38 -03:00
1 parent a6112ca23e
commit 442140e412
1 file changed
+102 -28
+102 -28
View File
@@ -3,13 +3,17 @@ package dto_builders
import (
"fmt"
htmlutil "html"
"math"
"strconv"
"strings"
dto_email "repuestera_back/dto/Email"
"repuestera_back/models"
)
// StockBajoEmailBuilder arma el aviso de stock por debajo del mínimo. El
// cálculo del faltante y el armado de los ítems son propios de este email.
// StockBajoEmailBuilder arma el aviso de stock por debajo del mínimo. Acá viven
// los detalles de presentación (tabla, estilos inline y formato de cantidades);
// el director sólo decide qué builder usar.
type StockBajoEmailBuilder struct {
stock *models.Stock
repuesto *models.Repuesto
@@ -67,33 +71,103 @@ func (b *StockBajoEmailBuilder) Build() (*dto_email.MensajeEmail, error) {
return nil, b.err
}
almacen := fmt.Sprintf("%d", b.stock.AlmacenID)
if b.stock.Almacen.Codigo != "" {
almacen = b.stock.Almacen.Codigo
}
minimo := float64(b.repuesto.StockMinimo)
nombre := htmlutil.EscapeString(b.repuesto.Nombre)
descripcion := fmt.Sprintf(
"<p>El ajuste de stock dejó <strong>%s</strong> por debajo del mínimo "+
"configurado en el almacén <strong>%s</strong>.</p>"+
"<p>Usuario responsable: %d</p>",
nombre, htmlutil.EscapeString(almacen), b.usuarioID,
)
items := []string{
fmt.Sprintf("Repuesto: %s", b.repuesto.Nombre),
fmt.Sprintf("Almacén: %s", almacen),
fmt.Sprintf("Stock anterior: %.3f", b.saldoAnterior),
fmt.Sprintf("Stock actual: %.3f", b.stock.Cantidad),
fmt.Sprintf("Stock mínimo: %.3f", minimo),
fmt.Sprintf("Faltante para el mínimo: %.3f", minimo-b.stock.Cantidad),
}
return NewMensajeEmailBuilder().
ConTitulo(fmt.Sprintf("Stock bajo mínimo: %s", b.repuesto.Nombre)).
ConDescripcion(descripcion).
ConItems(items).
ConTitulo("Stock bajo mínimo: " + b.repuesto.Nombre).
ConDescripcion(htmlStockBajo(b.stock, b.repuesto, b.saldoAnterior, b.usuarioID)).
ConItems([]string{}).
ConDestinatarios(b.destinatarios).
Build()
}
const (
stockBajoColorPrimary = "#f1c21b"
stockBajoColorOnPrim = "#161616"
stockBajoColorAccent = "#8e6a00"
stockBajoColorText = "#161616"
stockBajoColorMuted = "#525252"
stockBajoColorBorder = "#e0e0e0"
stockBajoColorSoft = "#fcf4d6"
)
// htmlStockBajo genera el cuerpo HTML del aviso. Usa tablas y estilos inline
// porque los clientes de correo suelen ignorar las hojas de estilo.
func htmlStockBajo(stock *models.Stock, repuesto *models.Repuesto, saldoAnterior float64, usuarioID uint) string {
var b strings.Builder
almacen := fmt.Sprintf("%d", stock.AlmacenID)
if stock.Almacen.Codigo != "" {
almacen = stock.Almacen.Codigo
}
minimo := float64(repuesto.StockMinimo)
faltante := minimo - stock.Cantidad
b.WriteString(`<div style="max-width:640px;margin:0 auto;font-family:Arial,Helvetica,sans-serif;color:` + stockBajoColorText + `;">`)
b.WriteString(`<div style="background:` + stockBajoColorPrimary + `;color:` + stockBajoColorOnPrim + `;padding:24px 28px;border-radius:8px 8px 0 0;">`)
b.WriteString(`<p style="margin:0;font-size:11px;letter-spacing:2px;text-transform:uppercase;opacity:.85;">Repuestera</p>`)
b.WriteString(`<h1 style="margin:8px 0 4px;font-size:24px;">Stock bajo mínimo</h1>`)
fmt.Fprintf(&b, `<p style="margin:0;font-size:14px;"><strong>%s</strong> en el almacén <strong>%s</strong></p>`,
htmlutil.EscapeString(repuesto.Nombre), htmlutil.EscapeString(almacen))
b.WriteString(`</div>`)
b.WriteString(`<div style="border:1px solid ` + stockBajoColorBorder + `;border-top:0;padding:24px 28px;border-radius:0 0 8px 8px;background:#ffffff;">`)
fmt.Fprintf(&b, `<p style="margin:0 0 20px;font-size:14px;line-height:1.5;"><strong>%s</strong> quedó por debajo del mínimo configurado. Conviene reponerlo.</p>`,
htmlutil.EscapeString(repuesto.Nombre))
b.WriteString(`<h2 style="margin:0 0 10px;font-size:14px;letter-spacing:1px;text-transform:uppercase;color:` + stockBajoColorMuted + `;">Detalle del stock</h2>`)
b.WriteString(`<table width="100%" cellpadding="0" cellspacing="0" style="border-collapse:collapse;font-size:13px;">`)
b.WriteString(`<thead><tr style="background:` + stockBajoColorSoft + `;color:` + stockBajoColorMuted + `;text-align:left;">`)
b.WriteString(`<th style="padding:10px 8px;">Concepto</th>`)
b.WriteString(`<th align="right" style="padding:10px 8px;">Valor</th>`)
b.WriteString(`</tr></thead><tbody>`)
filas := []struct {
etiqueta string
valor string
destaca bool
}{
{"Repuesto", htmlutil.EscapeString(repuesto.Nombre), false},
{"Almacén", htmlutil.EscapeString(almacen), false},
{"Stock anterior", formatCantidad(saldoAnterior), false},
{"Stock actual", formatCantidad(stock.Cantidad), true},
{"Stock mínimo", formatCantidad(minimo), false},
}
for _, fila := range filas {
peso := "400"
if fila.destaca {
peso = "600"
}
fmt.Fprintf(&b,
`<tr style="border-bottom:1px solid %s;"><td style="padding:10px 8px;color:%s;">%s</td><td align="right" style="padding:10px 8px;font-weight:%s;">%s</td></tr>`,
stockBajoColorBorder, stockBajoColorMuted, fila.etiqueta, peso, fila.valor,
)
}
b.WriteString(`</tbody></table>`)
b.WriteString(`<table width="100%" cellpadding="0" cellspacing="0" style="margin-top:18px;font-size:13px;">`)
fmt.Fprintf(&b,
`<tr><td style="padding:12px 8px 4px;border-top:2px solid %s;font-size:18px;font-weight:700;">Faltante para el mínimo</td><td align="right" style="padding:12px 8px 4px;border-top:2px solid %s;font-size:18px;font-weight:700;color:%s;">%s</td></tr>`,
stockBajoColorPrimary, stockBajoColorPrimary, stockBajoColorAccent, formatCantidad(faltante),
)
b.WriteString(`</table>`)
fmt.Fprintf(&b, `<p style="margin:24px 0 0;padding-top:16px;border-top:1px solid %s;font-size:12px;color:%s;">Usuario responsable: %d</p>`,
stockBajoColorBorder, stockBajoColorMuted, usuarioID)
b.WriteString(`</div></div>`)
return b.String()
}
// formatCantidad formatea cantidades sin ceros sobrantes. Las unidades
// enteras se muestran sin decimales; las que llevan parte decimal conservan
// solo los dígitos necesarios.
func formatCantidad(valor float64) string {
if math.IsNaN(valor) || math.IsInf(valor, 0) {
return fmt.Sprintf("%v", valor)
}
redondeado := math.Round(valor*1000) / 1000
if redondeado == math.Trunc(redondeado) {
return strconv.FormatInt(int64(redondeado), 10)
}
return strconv.FormatFloat(redondeado, 'f', -1, 64)
}