Private
Public Access
232 lines
10 KiB
Go
232 lines
10 KiB
Go
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
|
|
pagos []models.Pago
|
|
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) ConPagos(pagos []models.Pago) *ComprobanteVentaEmailBuilder {
|
|
b.pagos = pagos
|
|
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, b.pagos)).
|
|
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, pagos []models.Pago) 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 := detalle.RepuestoID // placeholder, se reemplaza abajo
|
|
_ = nombre
|
|
nombreRepuesto := fmt.Sprintf("Repuesto %d", detalle.RepuestoID)
|
|
if n := strings.TrimSpace(nombres[detalle.RepuestoID]); n != "" {
|
|
nombreRepuesto = 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;">%s</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(nombreRepuesto),
|
|
formatoCantidad(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>`)
|
|
|
|
// Sección de pagos realizados
|
|
if len(pagos) > 0 {
|
|
b.WriteString(`<h2 style="margin:24px 0 10px;font-size:14px;letter-spacing:1px;text-transform:uppercase;color:` + comprobanteColorMuted + `;">Pagos realizados</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;">Fecha</th>`)
|
|
b.WriteString(`<th align="right" style="padding:10px 8px;">Forma de pago</th>`)
|
|
b.WriteString(`<th align="right" style="padding:10px 8px;">Importe</th>`)
|
|
b.WriteString(`<th align="right" style="padding:10px 8px;">Observación</th>`)
|
|
b.WriteString(`</tr></thead><tbody>`)
|
|
for _, pago := range pagos {
|
|
forma := "-"
|
|
if pago.FormaDePago.ID != 0 {
|
|
forma = pago.FormaDePago.Descripcion
|
|
}
|
|
fechaStr := pago.Fecha.Format("02/01/2006 15:04")
|
|
obs := pago.Observacion
|
|
if obs == "" {
|
|
obs = "-"
|
|
}
|
|
fmt.Fprintf(&b,
|
|
`<tr style="border-bottom:1px solid %s;"><td style="padding:10px 8px;">%s</td><td align="right" style="padding:10px 8px;">%s</td><td align="right" style="padding:10px 8px;font-weight:600;">%s</td><td style="padding:10px 8px;color:%s;">%s</td></tr>`,
|
|
comprobanteColorBorder, fechaStr, htmlutil.EscapeString(forma), montoComprobante(pago.Importe), comprobanteColorMuted, htmlutil.EscapeString(obs),
|
|
)
|
|
}
|
|
b.WriteString(`</tbody></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()
|
|
}
|
|
|
|
// formatoCantidad devuelve la cantidad como número entero si no tiene decimales,
|
|
// o con coma decimal si los tiene (formato AR).
|
|
func formatoCantidad(cantidad float64) string {
|
|
if cantidad == float64(int64(cantidad)) {
|
|
return strconv.FormatInt(int64(cantidad), 10)
|
|
}
|
|
// Tiene decimales: mostrar con coma
|
|
centavos := int64(math.Round(cantidad * 100))
|
|
entero := centavos / 100
|
|
dec := centavos % 100
|
|
if dec == 0 {
|
|
return strconv.FormatInt(entero, 10)
|
|
}
|
|
return fmt.Sprintf("%d,%02d", entero, dec)
|
|
}
|
|
|
|
// 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)
|
|
}
|