Private
Public Access
174 lines
6.3 KiB
Go
174 lines
6.3 KiB
Go
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. 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
|
|
saldoAnterior float64
|
|
destinatarios []string
|
|
usuarioID uint
|
|
err error
|
|
}
|
|
|
|
var _ IBuilder[dto_email.MensajeEmail] = (*StockBajoEmailBuilder)(nil)
|
|
|
|
func NewStockBajoEmailBuilder() *StockBajoEmailBuilder {
|
|
return &StockBajoEmailBuilder{}
|
|
}
|
|
|
|
func (b *StockBajoEmailBuilder) ConStock(stock *models.Stock) *StockBajoEmailBuilder {
|
|
if stock == nil {
|
|
AcumularError(&b.err, "el stock no puede ser nulo")
|
|
}
|
|
b.stock = stock
|
|
return b
|
|
}
|
|
|
|
func (b *StockBajoEmailBuilder) ConRepuesto(repuesto *models.Repuesto) *StockBajoEmailBuilder {
|
|
if repuesto == nil {
|
|
AcumularError(&b.err, "el repuesto no puede ser nulo")
|
|
}
|
|
b.repuesto = repuesto
|
|
return b
|
|
}
|
|
|
|
func (b *StockBajoEmailBuilder) ConSaldoAnterior(saldoAnterior float64) *StockBajoEmailBuilder {
|
|
b.saldoAnterior = saldoAnterior
|
|
return b
|
|
}
|
|
|
|
func (b *StockBajoEmailBuilder) ConDestinatarios(destinatarios []string) *StockBajoEmailBuilder {
|
|
b.destinatarios = destinatarios
|
|
return b
|
|
}
|
|
|
|
func (b *StockBajoEmailBuilder) ConUsuarioID(usuarioID uint) *StockBajoEmailBuilder {
|
|
b.usuarioID = usuarioID
|
|
return b
|
|
}
|
|
|
|
func (b *StockBajoEmailBuilder) Build() (*dto_email.MensajeEmail, error) {
|
|
if b.stock == nil && b.err == nil {
|
|
AcumularError(&b.err, "falta el stock")
|
|
}
|
|
if b.repuesto == nil && b.err == nil {
|
|
AcumularError(&b.err, "falta el repuesto")
|
|
}
|
|
if b.err != nil {
|
|
return nil, b.err
|
|
}
|
|
|
|
return NewMensajeEmailBuilder().
|
|
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)
|
|
}
|