Private
Public Access
98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
package dto_builders
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
dto "repuestera_back/dto/Facturas"
|
|
"repuestera_back/models"
|
|
)
|
|
|
|
// FacturaAjusteBuilder aplica sobre una factura existente los totales, el
|
|
// estado y la entrada de historial resultantes de una modificación o
|
|
// cancelación.
|
|
type FacturaAjusteBuilder struct {
|
|
factura *models.Factura
|
|
datos *dto.FacturaAjusteDto
|
|
err error
|
|
}
|
|
|
|
var _ IBuilder[models.Factura] = (*FacturaAjusteBuilder)(nil)
|
|
|
|
func NewFacturaAjusteBuilder() *FacturaAjusteBuilder {
|
|
return &FacturaAjusteBuilder{}
|
|
}
|
|
|
|
func (b *FacturaAjusteBuilder) ConFactura(factura *models.Factura) *FacturaAjusteBuilder {
|
|
if factura == nil {
|
|
AcumularError(&b.err, "la factura no puede ser nula")
|
|
return b
|
|
}
|
|
b.factura = factura
|
|
return b
|
|
}
|
|
|
|
func (b *FacturaAjusteBuilder) ConDatos(datos dto.FacturaAjusteDto) *FacturaAjusteBuilder {
|
|
b.datos = &datos
|
|
return b
|
|
}
|
|
|
|
func (b *FacturaAjusteBuilder) Build() (*models.Factura, error) {
|
|
if b.err != nil {
|
|
return nil, b.err
|
|
}
|
|
if b.factura == nil {
|
|
return nil, errors.New("falta la factura")
|
|
}
|
|
if b.datos == nil {
|
|
return nil, errors.New("faltan los datos del ajuste")
|
|
}
|
|
|
|
datos := b.datos
|
|
if datos.UsuarioID == 0 {
|
|
AcumularError(&b.err, "el usuario es obligatorio")
|
|
}
|
|
if strings.TrimSpace(datos.Estado) == "" {
|
|
AcumularError(&b.err, "el estado de la factura es obligatorio")
|
|
}
|
|
if strings.TrimSpace(datos.Motivo) == "" {
|
|
AcumularError(&b.err, "el motivo es obligatorio")
|
|
}
|
|
if b.err != nil {
|
|
return nil, b.err
|
|
}
|
|
|
|
fecha := datos.Fecha
|
|
if fecha.IsZero() {
|
|
fecha = time.Now()
|
|
}
|
|
usuarioID := datos.UsuarioID
|
|
|
|
factura := b.factura
|
|
factura.Detalles = append([]models.FacturaDetalle{}, datos.Detalles...)
|
|
factura.Subtotal = datos.Subtotal
|
|
factura.DescuentoTotal = datos.DescuentoTotal
|
|
factura.Impuestos = datos.Impuestos
|
|
factura.Total = datos.Total
|
|
factura.Saldo = datos.Saldo
|
|
factura.Estado = datos.Estado
|
|
factura.Version++
|
|
factura.EditadaPorID = &usuarioID
|
|
factura.EditadaAt = &fecha
|
|
factura.MotivoUltimaEdicion = datos.Motivo
|
|
factura.Historial = append(factura.Historial, models.FacturaCambio{
|
|
Fecha: fecha,
|
|
UsuarioID: usuarioID,
|
|
Motivo: datos.Motivo,
|
|
ItemsAnteriores: append([]models.FacturaDetalle{}, datos.ItemsAnteriores...),
|
|
TotalAnterior: datos.TotalAnterior,
|
|
ItemsNuevos: append([]models.FacturaDetalle{}, datos.ItemsNuevos...),
|
|
TotalNuevo: datos.TotalNuevo,
|
|
NotaTipo: datos.NotaTipo,
|
|
NotaID: datos.NotaID,
|
|
})
|
|
|
|
return factura, nil
|
|
}
|