Files
Repuestera/back/dto/Builders/CrearFacturaBuilder.go
T
2026-11-28 14:58:08 -03:00

95 lines
2.5 KiB
Go

package dto_builders
import (
"errors"
"strings"
dto "repuestera_back/dto/Facturas"
"repuestera_back/models"
)
type CrearFacturaBuilder struct {
datos *dto.FacturaCalculada
err error
}
var _ IBuilder[models.Factura] = (*CrearFacturaBuilder)(nil)
func NewCrearFacturaBuilder() *CrearFacturaBuilder {
return &CrearFacturaBuilder{}
}
func (b *CrearFacturaBuilder) ConDatos(datos dto.FacturaCalculada) *CrearFacturaBuilder {
b.datos = &datos
return b
}
func (b *CrearFacturaBuilder) Build() (*models.Factura, error) {
if b.datos == nil {
return nil, errors.New("faltan los datos de la factura")
}
datos := b.datos
if strings.TrimSpace(datos.Numero) == "" {
AcumularError(&b.err, "el número de factura es obligatorio")
}
if datos.ClienteID == 0 {
AcumularError(&b.err, "el cliente es obligatorio")
}
if datos.AlmacenID == 0 {
AcumularError(&b.err, "el almacén es obligatorio")
}
if datos.ResponsableID == 0 {
AcumularError(&b.err, "el responsable es obligatorio")
}
if datos.FechaEmision.IsZero() {
AcumularError(&b.err, "la fecha de emisión es obligatoria")
}
if len(datos.Detalles) == 0 {
AcumularError(&b.err, "la factura debe tener al menos un detalle")
}
if datos.Subtotal < 0 {
AcumularError(&b.err, "el subtotal no puede ser negativo")
}
if datos.DescuentoTotal < 0 {
AcumularError(&b.err, "el descuento no puede ser negativo")
}
if datos.Impuestos < 0 {
AcumularError(&b.err, "los impuestos no pueden ser negativos")
}
if datos.AlicuotaIVA < 0 {
AcumularError(&b.err, "la alícuota de IVA no puede ser negativa")
}
if datos.Total < 0 {
AcumularError(&b.err, "el total no puede ser negativo")
}
if datos.Saldo < 0 {
AcumularError(&b.err, "el saldo no puede ser negativo")
}
if b.err != nil {
return nil, b.err
}
factura := &models.Factura{
Numero: strings.TrimSpace(datos.Numero),
ClienteID: datos.ClienteID,
AlmacenID: datos.AlmacenID,
FechaEmision: datos.FechaEmision,
Subtotal: datos.Subtotal,
DescuentoID: datos.DescuentoID,
DescuentoTotal: datos.DescuentoTotal,
Impuestos: datos.Impuestos,
AlicuotaIVA: datos.AlicuotaIVA,
Saldo: datos.Saldo,
Detalles: append([]models.FacturaDetalle{}, datos.Detalles...),
Observacion: strings.TrimSpace(datos.Observacion),
Version: 1,
}
factura.EmpresaID = datos.EmpresaID
factura.Estado = "EMITIDA"
factura.Total = datos.Total
factura.ResponsableID = datos.ResponsableID
return factura, nil
}