Private
Public Access
quedaria testear los 4 tipos de email
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
0160ba90d4
commit
a9ec514687
6 files changed
+483
-106
No files matched your search
@@ -0,0 +1,178 @@
|
||||
package dto_builders
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
htmlutil "html"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
dto_email "repuestera_back/dto/Email"
|
||||
"repuestera_back/models"
|
||||
)
|
||||
|
||||
// ComprobanteVentaEmailBuilder arma el email de comprobante de venta. Acá viven los
|
||||
// detalles de presentación (tabla, estilos inline y formato de importes); el
|
||||
// director sólo decide qué builder usar.
|
||||
type ComprobanteVentaEmailBuilder struct {
|
||||
factura *models.Factura
|
||||
nombres map[uint64]string
|
||||
destinatario string
|
||||
err error
|
||||
}
|
||||
|
||||
var _ IBuilder[dto_email.MensajeEmail] = (*ComprobanteVentaEmailBuilder)(nil)
|
||||
|
||||
func NewComprobanteVentaEmailBuilder() *ComprobanteVentaEmailBuilder {
|
||||
return &ComprobanteVentaEmailBuilder{}
|
||||
}
|
||||
|
||||
func (b *ComprobanteVentaEmailBuilder) ConFactura(factura *models.Factura) *ComprobanteVentaEmailBuilder {
|
||||
if factura == nil {
|
||||
AcumularError(&b.err, "la factura no puede ser nula")
|
||||
}
|
||||
b.factura = factura
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *ComprobanteVentaEmailBuilder) ConNombresRepuestos(nombres map[uint64]string) *ComprobanteVentaEmailBuilder {
|
||||
b.nombres = nombres
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *ComprobanteVentaEmailBuilder) ConDestinatario(destinatario string) *ComprobanteVentaEmailBuilder {
|
||||
b.destinatario = strings.TrimSpace(destinatario)
|
||||
if b.destinatario == "" {
|
||||
AcumularError(&b.err, "el destinatario es obligatorio")
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *ComprobanteVentaEmailBuilder) Build() (*dto_email.MensajeEmail, error) {
|
||||
if b.factura == nil && b.err == nil {
|
||||
AcumularError(&b.err, "falta la factura")
|
||||
}
|
||||
if b.err != nil {
|
||||
return nil, b.err
|
||||
}
|
||||
|
||||
return NewMensajeEmailBuilder().
|
||||
ConTitulo("Comprobante de venta " + b.factura.Numero).
|
||||
ConDescripcion(htmlComprobanteVenta(b.factura, b.nombres)).
|
||||
ConItems([]string{}).
|
||||
ConDestinatarios([]string{b.destinatario}).
|
||||
Build()
|
||||
}
|
||||
|
||||
const (
|
||||
comprobanteColorPrimary = "#0f62fe"
|
||||
comprobanteColorText = "#161616"
|
||||
comprobanteColorMuted = "#525252"
|
||||
comprobanteColorBorder = "#e0e0e0"
|
||||
comprobanteColorSoft = "#f2f4f8"
|
||||
)
|
||||
|
||||
// htmlComprobanteVenta genera el cuerpo HTML del comprobante. Usa tablas y
|
||||
// estilos inline porque los clientes de correo suelen ignorar las hojas de
|
||||
// estilo.
|
||||
func htmlComprobanteVenta(factura *models.Factura, nombres map[uint64]string) string {
|
||||
var b strings.Builder
|
||||
|
||||
b.WriteString(`<div style="max-width:640px;margin:0 auto;font-family:Arial,Helvetica,sans-serif;color:` + comprobanteColorText + `;">`)
|
||||
b.WriteString(`<div style="background:` + comprobanteColorPrimary + `;color:#ffffff;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;">Tu comprobante de venta</h1>`)
|
||||
fmt.Fprintf(&b, `<p style="margin:0;font-size:14px;">Factura <strong>%s</strong></p>`, htmlutil.EscapeString(factura.Numero))
|
||||
b.WriteString(`</div>`)
|
||||
|
||||
b.WriteString(`<div style="border:1px solid ` + comprobanteColorBorder + `;border-top:0;padding:24px 28px;border-radius:0 0 8px 8px;background:#ffffff;">`)
|
||||
b.WriteString(`<table width="100%" cellpadding="0" cellspacing="0" style="font-size:13px;margin-bottom:24px;">`)
|
||||
cliente := htmlutil.EscapeString(factura.Cliente.Nombre)
|
||||
if strings.TrimSpace(cliente) == "" {
|
||||
cliente = "-"
|
||||
}
|
||||
fmt.Fprintf(&b, `<tr><td style="padding:4px 0;color:%s;">Cliente</td><td align="right" style="padding:4px 0;font-weight:600;">%s</td></tr>`, comprobanteColorMuted, cliente)
|
||||
if cuit := strings.TrimSpace(factura.Cliente.Cuit); cuit != "" {
|
||||
fmt.Fprintf(&b, `<tr><td style="padding:4px 0;color:%s;">CUIT</td><td align="right" style="padding:4px 0;">%s</td></tr>`, comprobanteColorMuted, htmlutil.EscapeString(cuit))
|
||||
}
|
||||
fmt.Fprintf(&b, `<tr><td style="padding:4px 0;color:%s;">Fecha de emisión</td><td align="right" style="padding:4px 0;">%s</td></tr>`, comprobanteColorMuted, factura.FechaEmision.Format("02/01/2006"))
|
||||
fmt.Fprintf(&b, `<tr><td style="padding:4px 0;color:%s;">Estado</td><td align="right" style="padding:4px 0;font-weight:600;">%s</td></tr>`, comprobanteColorMuted, htmlutil.EscapeString(factura.Estado))
|
||||
b.WriteString(`</table>`)
|
||||
|
||||
b.WriteString(`<h2 style="margin:0 0 10px;font-size:14px;letter-spacing:1px;text-transform:uppercase;color:` + comprobanteColorMuted + `;">Detalle de la compra</h2>`)
|
||||
b.WriteString(`<table width="100%" cellpadding="0" cellspacing="0" style="border-collapse:collapse;font-size:13px;">`)
|
||||
b.WriteString(`<thead><tr style="background:` + comprobanteColorSoft + `;color:` + comprobanteColorMuted + `;text-align:left;">`)
|
||||
b.WriteString(`<th style="padding:10px 8px;">Repuesto</th>`)
|
||||
b.WriteString(`<th align="right" style="padding:10px 8px;">Cant.</th>`)
|
||||
b.WriteString(`<th align="right" style="padding:10px 8px;">Precio unit.</th>`)
|
||||
b.WriteString(`<th align="right" style="padding:10px 8px;">Subtotal</th>`)
|
||||
b.WriteString(`</tr></thead><tbody>`)
|
||||
|
||||
if len(factura.Detalles) == 0 {
|
||||
b.WriteString(`<tr><td colspan="4" style="padding:12px 8px;color:` + comprobanteColorMuted + `;">Sin detalles</td></tr>`)
|
||||
}
|
||||
for _, detalle := range factura.Detalles {
|
||||
nombre := fmt.Sprintf("Repuesto %d", detalle.RepuestoID)
|
||||
if n := strings.TrimSpace(nombres[detalle.RepuestoID]); n != "" {
|
||||
nombre = n
|
||||
}
|
||||
fmt.Fprintf(&b,
|
||||
`<tr style="border-bottom:1px solid %s;"><td style="padding:10px 8px;">%s</td><td align="right" style="padding:10px 8px;">%.3f</td><td align="right" style="padding:10px 8px;">%s</td><td align="right" style="padding:10px 8px;font-weight:600;">%s</td></tr>`,
|
||||
comprobanteColorBorder, htmlutil.EscapeString(nombre), detalle.Cantidad,
|
||||
montoComprobante(detalle.PrecioUnitario), montoComprobante(detalle.Subtotal),
|
||||
)
|
||||
}
|
||||
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:4px 8px;color:%s;">Subtotal</td><td align="right" style="padding:4px 8px;">%s</td></tr>`, comprobanteColorMuted, montoComprobante(factura.Subtotal))
|
||||
if factura.DescuentoTotal > 0 {
|
||||
fmt.Fprintf(&b, `<tr><td style="padding:4px 8px;color:%s;">Descuento</td><td align="right" style="padding:4px 8px;color:#da1e28;">-%s</td></tr>`, comprobanteColorMuted, montoComprobante(factura.DescuentoTotal))
|
||||
}
|
||||
alicuota := factura.AlicuotaIVA
|
||||
if alicuota == 0 {
|
||||
alicuota = 21
|
||||
}
|
||||
fmt.Fprintf(&b, `<tr><td style="padding:4px 8px;color:%s;">IVA (%.0f%%)</td><td align="right" style="padding:4px 8px;">%s</td></tr>`, comprobanteColorMuted, alicuota, montoComprobante(factura.Impuestos))
|
||||
fmt.Fprintf(&b,
|
||||
`<tr><td style="padding:12px 8px 4px;border-top:2px solid %s;font-size:18px;font-weight:700;">Total</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>`,
|
||||
comprobanteColorBorder, comprobanteColorBorder, comprobanteColorPrimary, montoComprobante(factura.Total),
|
||||
)
|
||||
if factura.Saldo > 0 {
|
||||
fmt.Fprintf(&b, `<tr><td style="padding:4px 8px;color:%s;">Saldo a pagar</td><td align="right" style="padding:4px 8px;font-weight:600;">%s</td></tr>`, comprobanteColorMuted, montoComprobante(factura.Saldo))
|
||||
}
|
||||
b.WriteString(`</table>`)
|
||||
|
||||
if observacion := strings.TrimSpace(factura.Observacion); observacion != "" {
|
||||
fmt.Fprintf(&b, `<p style="margin:20px 0 0;font-size:12px;color:%s;"><strong>Observación:</strong> %s</p>`, comprobanteColorMuted, htmlutil.EscapeString(observacion))
|
||||
}
|
||||
|
||||
b.WriteString(`<p style="margin:24px 0 0;padding-top:16px;border-top:1px solid ` + comprobanteColorBorder + `;font-size:12px;color:` + comprobanteColorMuted + `;">Gracias por elegirnos. Si tenés consultas sobre esta factura, respondé a este correo.</p>`)
|
||||
b.WriteString(`</div></div>`)
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// montoComprobante formatea importes con separador de miles y coma decimal.
|
||||
func montoComprobante(valor float64) string {
|
||||
negativo := valor < 0
|
||||
if negativo {
|
||||
valor = -valor
|
||||
}
|
||||
centavos := int64(math.Round(valor * 100))
|
||||
digits := strconv.FormatInt(centavos/100, 10)
|
||||
|
||||
var conPuntos strings.Builder
|
||||
for i, ch := range digits {
|
||||
if i > 0 && (len(digits)-i)%3 == 0 {
|
||||
conPuntos.WriteByte('.')
|
||||
}
|
||||
conPuntos.WriteRune(ch)
|
||||
}
|
||||
|
||||
signo := ""
|
||||
if negativo {
|
||||
signo = "-"
|
||||
}
|
||||
return fmt.Sprintf("%s$ %s,%02d", signo, conPuntos.String(), centavos%100)
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package dto_builders
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
htmlutil "html"
|
||||
|
||||
dto_email "repuestera_back/dto/Email"
|
||||
"repuestera_back/models"
|
||||
)
|
||||
|
||||
// DiferenciasRemitoEmailBuilder arma el aviso de diferencias de un remito.
|
||||
// Resuelve los nombres de repuestos y formatea cada línea en este builder.
|
||||
type DiferenciasRemitoEmailBuilder struct {
|
||||
remito *models.Remito
|
||||
proveedor string
|
||||
diferencias []models.DiferenciaRemito
|
||||
repuestos map[uint64]*models.Repuesto
|
||||
destinatarios []string
|
||||
usuarioID uint
|
||||
err error
|
||||
}
|
||||
|
||||
var _ IBuilder[dto_email.MensajeEmail] = (*DiferenciasRemitoEmailBuilder)(nil)
|
||||
|
||||
func NewDiferenciasRemitoEmailBuilder() *DiferenciasRemitoEmailBuilder {
|
||||
return &DiferenciasRemitoEmailBuilder{}
|
||||
}
|
||||
|
||||
func (b *DiferenciasRemitoEmailBuilder) ConRemito(remito *models.Remito) *DiferenciasRemitoEmailBuilder {
|
||||
if remito == nil {
|
||||
AcumularError(&b.err, "el remito no puede ser nulo")
|
||||
}
|
||||
b.remito = remito
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *DiferenciasRemitoEmailBuilder) ConProveedor(proveedor string) *DiferenciasRemitoEmailBuilder {
|
||||
b.proveedor = proveedor
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *DiferenciasRemitoEmailBuilder) ConDiferencias(diferencias []models.DiferenciaRemito) *DiferenciasRemitoEmailBuilder {
|
||||
if len(diferencias) == 0 {
|
||||
AcumularError(&b.err, "no hay diferencias para notificar")
|
||||
}
|
||||
b.diferencias = append([]models.DiferenciaRemito(nil), diferencias...)
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *DiferenciasRemitoEmailBuilder) ConRepuestos(repuestos map[uint64]*models.Repuesto) *DiferenciasRemitoEmailBuilder {
|
||||
b.repuestos = repuestos
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *DiferenciasRemitoEmailBuilder) ConDestinatarios(destinatarios []string) *DiferenciasRemitoEmailBuilder {
|
||||
b.destinatarios = destinatarios
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *DiferenciasRemitoEmailBuilder) ConUsuarioID(usuarioID uint) *DiferenciasRemitoEmailBuilder {
|
||||
b.usuarioID = usuarioID
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *DiferenciasRemitoEmailBuilder) Build() (*dto_email.MensajeEmail, error) {
|
||||
if b.remito == nil && b.err == nil {
|
||||
AcumularError(&b.err, "falta el remito")
|
||||
}
|
||||
if len(b.diferencias) == 0 && b.err == nil {
|
||||
AcumularError(&b.err, "faltan las diferencias")
|
||||
}
|
||||
if b.err != nil {
|
||||
return nil, b.err
|
||||
}
|
||||
|
||||
items := make([]string, 0, len(b.diferencias))
|
||||
for _, diferencia := range b.diferencias {
|
||||
nombre := fmt.Sprintf("Repuesto %d", diferencia.RepuestoID)
|
||||
if repuesto, ok := b.repuestos[diferencia.RepuestoID]; ok && repuesto != nil {
|
||||
nombre = repuesto.Nombre
|
||||
}
|
||||
|
||||
items = append(items, fmt.Sprintf(
|
||||
"%s | Esperado: %.3f | Recibido: %.3f | Diferencia: %.3f",
|
||||
nombre, diferencia.CantidadEsperada, diferencia.CantidadRecibida, diferencia.Diferencia,
|
||||
))
|
||||
}
|
||||
|
||||
descripcion := fmt.Sprintf(
|
||||
"<p>Se detectaron <strong>%d</strong> diferencias al ingresar el remito "+
|
||||
"<strong>%s</strong> del proveedor <strong>%s</strong>.</p>"+
|
||||
"<p>Fecha de emisión: %s</p>"+
|
||||
"<p>Usuario responsable: %d</p>",
|
||||
len(b.diferencias),
|
||||
htmlutil.EscapeString(b.remito.Numero),
|
||||
htmlutil.EscapeString(b.proveedor),
|
||||
b.remito.FechaEmision.Format("02/01/2006"),
|
||||
b.usuarioID,
|
||||
)
|
||||
|
||||
return NewMensajeEmailBuilder().
|
||||
ConTitulo(fmt.Sprintf("Diferencia en remito %s", b.remito.Numero)).
|
||||
ConDescripcion(descripcion).
|
||||
ConItems(items).
|
||||
ConDestinatarios(b.destinatarios).
|
||||
Build()
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
package directors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
builders "repuestera_back/dto/Builders"
|
||||
dto_email "repuestera_back/dto/Email"
|
||||
"repuestera_back/models"
|
||||
)
|
||||
|
||||
// EmailDirector centraliza el armado de emails. Cada método elige el builder
|
||||
// correspondiente y le pasa los datos; la presentación y las validaciones
|
||||
// específicas viven en ese builder.
|
||||
type EmailDirector struct{}
|
||||
|
||||
func NewEmailDirector() *EmailDirector {
|
||||
@@ -18,47 +17,25 @@ func NewEmailDirector() *EmailDirector {
|
||||
|
||||
// ComprobanteVenta arma el email que se envía al cliente después de emitir
|
||||
// una factura.
|
||||
func (d *EmailDirector) ComprobanteVenta(factura *models.Factura, destinatario string) (*dto_email.MensajeEmail, error) {
|
||||
if factura == nil {
|
||||
return nil, errors.New("la factura no puede ser nula")
|
||||
}
|
||||
if strings.TrimSpace(destinatario) == "" {
|
||||
return nil, errors.New("el destinatario es obligatorio")
|
||||
}
|
||||
|
||||
descripcion := fmt.Sprintf("<p>Factura %s por %.2f</p>", factura.Numero, factura.Total)
|
||||
return builders.NewMensajeEmailBuilder().
|
||||
ConTitulo("Comprobante de venta " + factura.Numero).
|
||||
ConDescripcion(descripcion).
|
||||
ConItems([]string{}).
|
||||
ConDestinatarios([]string{destinatario}).
|
||||
func (d *EmailDirector) ComprobanteVenta(
|
||||
factura *models.Factura,
|
||||
nombresRepuestos map[uint64]string,
|
||||
destinatario string,
|
||||
) (*dto_email.MensajeEmail, error) {
|
||||
return builders.NewComprobanteVentaEmailBuilder().
|
||||
ConFactura(factura).
|
||||
ConNombresRepuestos(nombresRepuestos).
|
||||
ConDestinatario(destinatario).
|
||||
Build()
|
||||
}
|
||||
|
||||
// Invitacion arma el email que se envía a un usuario invitado a una empresa.
|
||||
func (d *EmailDirector) Invitacion(empresa, rol, link, destinatario string) (*dto_email.MensajeEmail, error) {
|
||||
if strings.TrimSpace(empresa) == "" {
|
||||
return nil, errors.New("la empresa es obligatoria")
|
||||
}
|
||||
if strings.TrimSpace(rol) == "" {
|
||||
return nil, errors.New("el rol es obligatorio")
|
||||
}
|
||||
if strings.TrimSpace(link) == "" {
|
||||
return nil, errors.New("el link de invitación es obligatorio")
|
||||
}
|
||||
|
||||
descripcion := fmt.Sprintf(
|
||||
"<p>Fuiste invitado a <strong>%s</strong> con el rol <strong>%s</strong>.</p>"+
|
||||
"<p><a href=\"%s\">Aceptar invitación</a></p>"+
|
||||
"<p>Si el enlace no funciona, copiá esta dirección: %s</p>",
|
||||
empresa, rol, link, link,
|
||||
)
|
||||
|
||||
return builders.NewMensajeEmailBuilder().
|
||||
ConTitulo("Invitación a " + empresa).
|
||||
ConDescripcion(descripcion).
|
||||
ConItems([]string{}).
|
||||
ConDestinatarios([]string{destinatario}).
|
||||
return builders.NewInvitacionEmailBuilder().
|
||||
ConEmpresa(empresa).
|
||||
ConRol(rol).
|
||||
ConLink(link).
|
||||
ConDestinatario(destinatario).
|
||||
Build()
|
||||
}
|
||||
|
||||
@@ -71,38 +48,12 @@ func (d *EmailDirector) StockBajo(
|
||||
destinatarios []string,
|
||||
usuarioID uint,
|
||||
) (*dto_email.MensajeEmail, error) {
|
||||
if stock == nil {
|
||||
return nil, errors.New("el stock no puede ser nulo")
|
||||
}
|
||||
if repuesto == nil {
|
||||
return nil, errors.New("el repuesto no puede ser nulo")
|
||||
}
|
||||
|
||||
almacen := fmt.Sprintf("%d", stock.AlmacenID)
|
||||
if stock.Almacen.Codigo != "" {
|
||||
almacen = stock.Almacen.Codigo
|
||||
}
|
||||
minimo := float64(repuesto.StockMinimo)
|
||||
|
||||
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>",
|
||||
repuesto.Nombre, almacen, usuarioID,
|
||||
)
|
||||
|
||||
return builders.NewMensajeEmailBuilder().
|
||||
ConTitulo(fmt.Sprintf("Stock bajo mínimo: %s", repuesto.Nombre)).
|
||||
ConDescripcion(descripcion).
|
||||
ConItems([]string{
|
||||
fmt.Sprintf("Repuesto: %s", repuesto.Nombre),
|
||||
fmt.Sprintf("Almacén: %s", almacen),
|
||||
fmt.Sprintf("Stock anterior: %.3f", saldoAnterior),
|
||||
fmt.Sprintf("Stock actual: %.3f", stock.Cantidad),
|
||||
fmt.Sprintf("Stock mínimo: %.3f", minimo),
|
||||
fmt.Sprintf("Faltante para el mínimo: %.3f", minimo-stock.Cantidad),
|
||||
}).
|
||||
return builders.NewStockBajoEmailBuilder().
|
||||
ConStock(stock).
|
||||
ConRepuesto(repuesto).
|
||||
ConSaldoAnterior(saldoAnterior).
|
||||
ConDestinatarios(destinatarios).
|
||||
ConUsuarioID(usuarioID).
|
||||
Build()
|
||||
}
|
||||
|
||||
@@ -116,39 +67,12 @@ func (d *EmailDirector) DiferenciasRemito(
|
||||
destinatarios []string,
|
||||
usuarioID uint,
|
||||
) (*dto_email.MensajeEmail, error) {
|
||||
if remito == nil {
|
||||
return nil, errors.New("el remito no puede ser nulo")
|
||||
}
|
||||
if len(diferencias) == 0 {
|
||||
return nil, errors.New("no hay diferencias para notificar")
|
||||
}
|
||||
|
||||
items := make([]string, 0, len(diferencias))
|
||||
for _, diferencia := range diferencias {
|
||||
nombre := fmt.Sprintf("Repuesto %d", diferencia.RepuestoID)
|
||||
if repuesto, ok := repuestos[diferencia.RepuestoID]; ok && repuesto != nil {
|
||||
nombre = repuesto.Nombre
|
||||
}
|
||||
|
||||
items = append(items, fmt.Sprintf(
|
||||
"%s | Esperado: %.3f | Recibido: %.3f | Diferencia: %.3f",
|
||||
nombre, diferencia.CantidadEsperada, diferencia.CantidadRecibida, diferencia.Diferencia,
|
||||
))
|
||||
}
|
||||
|
||||
descripcion := fmt.Sprintf(
|
||||
"<p>Se detectaron <strong>%d</strong> diferencias al ingresar el remito "+
|
||||
"<strong>%s</strong> del proveedor <strong>%s</strong>.</p>"+
|
||||
"<p>Fecha de emisión: %s</p>"+
|
||||
"<p>Usuario responsable: %d</p>",
|
||||
len(diferencias), remito.Numero, proveedor,
|
||||
remito.FechaEmision.Format("02/01/2006"), usuarioID,
|
||||
)
|
||||
|
||||
return builders.NewMensajeEmailBuilder().
|
||||
ConTitulo(fmt.Sprintf("Diferencia en remito %s", remito.Numero)).
|
||||
ConDescripcion(descripcion).
|
||||
ConItems(items).
|
||||
return builders.NewDiferenciasRemitoEmailBuilder().
|
||||
ConRemito(remito).
|
||||
ConProveedor(proveedor).
|
||||
ConDiferencias(diferencias).
|
||||
ConRepuestos(repuestos).
|
||||
ConDestinatarios(destinatarios).
|
||||
ConUsuarioID(usuarioID).
|
||||
Build()
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package dto_builders
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
htmlutil "html"
|
||||
|
||||
dto_email "repuestera_back/dto/Email"
|
||||
)
|
||||
|
||||
// InvitacionEmailBuilder arma el email de invitación a una empresa. Acá viven
|
||||
// el texto y las validaciones propias de este caso de uso.
|
||||
type InvitacionEmailBuilder struct {
|
||||
empresa string
|
||||
rol string
|
||||
link string
|
||||
destinatario string
|
||||
err error
|
||||
}
|
||||
|
||||
var _ IBuilder[dto_email.MensajeEmail] = (*InvitacionEmailBuilder)(nil)
|
||||
|
||||
func NewInvitacionEmailBuilder() *InvitacionEmailBuilder {
|
||||
return &InvitacionEmailBuilder{}
|
||||
}
|
||||
|
||||
func (b *InvitacionEmailBuilder) ConEmpresa(empresa string) *InvitacionEmailBuilder {
|
||||
b.empresa, _ = ValidarTexto(empresa, "empresa", &b.err)
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *InvitacionEmailBuilder) ConRol(rol string) *InvitacionEmailBuilder {
|
||||
b.rol, _ = ValidarTexto(rol, "rol", &b.err)
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *InvitacionEmailBuilder) ConLink(link string) *InvitacionEmailBuilder {
|
||||
b.link, _ = ValidarTexto(link, "link de invitación", &b.err)
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *InvitacionEmailBuilder) ConDestinatario(destinatario string) *InvitacionEmailBuilder {
|
||||
b.destinatario = destinatario
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *InvitacionEmailBuilder) Build() (*dto_email.MensajeEmail, error) {
|
||||
if b.err != nil {
|
||||
return nil, b.err
|
||||
}
|
||||
|
||||
empresa := htmlutil.EscapeString(b.empresa)
|
||||
rol := htmlutil.EscapeString(b.rol)
|
||||
link := htmlutil.EscapeString(b.link)
|
||||
descripcion := fmt.Sprintf(
|
||||
"<p>Fuiste invitado a <strong>%s</strong> con el rol <strong>%s</strong>.</p>"+
|
||||
"<p><a href=\"%s\">Aceptar invitación</a></p>"+
|
||||
"<p>Si el enlace no funciona, copiá esta dirección: %s</p>",
|
||||
empresa, rol, link, link,
|
||||
)
|
||||
|
||||
return NewMensajeEmailBuilder().
|
||||
ConTitulo("Invitación a " + b.empresa).
|
||||
ConDescripcion(descripcion).
|
||||
ConItems([]string{}).
|
||||
ConDestinatarios([]string{b.destinatario}).
|
||||
Build()
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package dto_builders
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
htmlutil "html"
|
||||
|
||||
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.
|
||||
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
|
||||
}
|
||||
|
||||
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).
|
||||
ConDestinatarios(b.destinatarios).
|
||||
Build()
|
||||
}
|
||||
@@ -63,6 +63,7 @@ func CrearFactura(
|
||||
|
||||
// Validar repuestos y cantidades, y detectar duplicados.
|
||||
vistos := make(map[uint64]bool, len(items))
|
||||
nombresRepuestos := make(map[uint64]string, len(items))
|
||||
for i := range items {
|
||||
item := &items[i]
|
||||
if item.RepuestoID == 0 {
|
||||
@@ -83,6 +84,7 @@ func CrearFactura(
|
||||
if item.PrecioUnitario <= 0 {
|
||||
item.PrecioUnitario = (*repuesto).Precio
|
||||
}
|
||||
nombresRepuestos[item.RepuestoID] = (*repuesto).Nombre
|
||||
}
|
||||
|
||||
// Validar pagos.
|
||||
@@ -266,8 +268,9 @@ func CrearFactura(
|
||||
}
|
||||
|
||||
// Email post-commit: un fallo no revierte la factura.
|
||||
facturaCreada.Cliente = *cliente
|
||||
if entrada.EnviarEmail && cliente.Email != "" {
|
||||
mensaje, err := dto_directors.NewEmailDirector().ComprobanteVenta(facturaCreada, cliente.Email)
|
||||
mensaje, err := dto_directors.NewEmailDirector().ComprobanteVenta(facturaCreada, nombresRepuestos, cliente.Email)
|
||||
if err != nil {
|
||||
applog.Printf("facturas: no se pudo construir el email de la factura %s: %v", facturaCreada.Numero, err)
|
||||
} else if EnviarEmail(*mensaje, usuarioID) {
|
||||
@@ -282,7 +285,6 @@ func CrearFactura(
|
||||
{NombreTabla: "factura", Columna: "total", ValorNuevo: fmt.Sprintf("%.2f", total)},
|
||||
})
|
||||
|
||||
facturaCreada.Cliente = *cliente
|
||||
respuestaFactura, err := dto_directors.NewFacturaDirector().DesdeFactura(facturaCreada)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in new issue
Block a user