Private
Public Access
hecho para poder cargar una reserva desde el facturas venta
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
1073c69d5c
commit
ab59bd7043
20 files changed
+293
-90
No files matched your search
@@ -5,7 +5,6 @@ import (
|
||||
"strings"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/utils"
|
||||
|
||||
@@ -14,9 +13,10 @@ import (
|
||||
|
||||
func ActualizarEmpresa(c *gin.Context) {
|
||||
var req struct {
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit string `json:"cuit"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit string `json:"cuit"`
|
||||
EmailAlertaStock *[]string `json:"email_alerta_stock"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -30,10 +30,28 @@ func ActualizarEmpresa(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
input := models_seguridad.Empresa{Nombre: strings.TrimSpace(req.Nombre), Slug: strings.TrimSpace(req.Slug)}
|
||||
if req.Cuit != "" {
|
||||
if strings.TrimSpace(req.Nombre) != "" {
|
||||
empresa.Nombre = strings.TrimSpace(req.Nombre)
|
||||
}
|
||||
if strings.TrimSpace(req.Slug) != "" {
|
||||
empresa.Slug = strings.TrimSpace(req.Slug)
|
||||
}
|
||||
if strings.TrimSpace(req.Cuit) != "" {
|
||||
cuit := strings.TrimSpace(req.Cuit)
|
||||
input.Cuit = &cuit
|
||||
empresa.Cuit = &cuit
|
||||
}
|
||||
if req.EmailAlertaStock != nil {
|
||||
limpios := make([]string, 0, len(*req.EmailAlertaStock))
|
||||
vistos := make(map[string]bool)
|
||||
for _, email := range *req.EmailAlertaStock {
|
||||
email = strings.TrimSpace(email)
|
||||
if email == "" || vistos[email] {
|
||||
continue
|
||||
}
|
||||
vistos[email] = true
|
||||
limpios = append(limpios, email)
|
||||
}
|
||||
empresa.EmailAlertaStock = limpios
|
||||
}
|
||||
|
||||
if err := repo.Empresa.Actualizar(empresa, input); err != nil {
|
||||
|
||||
@@ -18,10 +18,11 @@ func ActualizarEmpresa(c *gin.Context) {
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit *string `json:"cuit"`
|
||||
Activa *bool `json:"activa"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit *string `json:"cuit"`
|
||||
EmailAlertaStock *[]string `json:"email_alerta_stock"`
|
||||
Activa *bool `json:"activa"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -49,6 +50,19 @@ func ActualizarEmpresa(c *gin.Context) {
|
||||
empresa.Cuit = &cuit
|
||||
}
|
||||
}
|
||||
if req.EmailAlertaStock != nil {
|
||||
limpios := make([]string, 0, len(*req.EmailAlertaStock))
|
||||
vistos := make(map[string]bool)
|
||||
for _, email := range *req.EmailAlertaStock {
|
||||
email = strings.TrimSpace(email)
|
||||
if email == "" || vistos[email] {
|
||||
continue
|
||||
}
|
||||
vistos[email] = true
|
||||
limpios = append(limpios, email)
|
||||
}
|
||||
empresa.EmailAlertaStock = limpios
|
||||
}
|
||||
if req.Activa != nil {
|
||||
empresa.Activa = *req.Activa
|
||||
}
|
||||
|
||||
@@ -16,12 +16,13 @@ import (
|
||||
|
||||
func CrearEmpresa(c *gin.Context) {
|
||||
var req struct {
|
||||
Nombre string `json:"nombre" binding:"required"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit string `json:"cuit"`
|
||||
OwnerEmail string `json:"owner_email" binding:"required,email"`
|
||||
OwnerName string `json:"owner_name"`
|
||||
OwnerPassword string `json:"owner_password"`
|
||||
Nombre string `json:"nombre" binding:"required"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit string `json:"cuit"`
|
||||
EmailAlertaStock []string `json:"email_alerta_stock"`
|
||||
OwnerEmail string `json:"owner_email" binding:"required,email"`
|
||||
OwnerName string `json:"owner_name"`
|
||||
OwnerPassword string `json:"owner_password"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -82,6 +83,17 @@ func CrearEmpresa(c *gin.Context) {
|
||||
cuit := strings.TrimSpace(req.Cuit)
|
||||
empresa.Cuit = &cuit
|
||||
}
|
||||
limpios := make([]string, 0, len(req.EmailAlertaStock))
|
||||
vistos := make(map[string]bool)
|
||||
for _, email := range req.EmailAlertaStock {
|
||||
email = strings.TrimSpace(email)
|
||||
if email == "" || vistos[email] {
|
||||
continue
|
||||
}
|
||||
vistos[email] = true
|
||||
limpios = append(limpios, email)
|
||||
}
|
||||
empresa.EmailAlertaStock = limpios
|
||||
|
||||
if err := repo.Empresa.Crear(&empresa, owner.ID); err != nil {
|
||||
if creado {
|
||||
|
||||
@@ -85,6 +85,12 @@ func destinatariosAlertaStock(repo *repositorios.Repositorios, empresaID, usuari
|
||||
return limpiarDestinatarios(strings.Split(raw, ","))
|
||||
}
|
||||
|
||||
if empresaID > 0 {
|
||||
if empresa, err := repo.Empresa.ObtenerPorID(empresaID); err == nil && empresa != nil && len(empresa.EmailAlertaStock) > 0 {
|
||||
return limpiarDestinatarios([]string(empresa.EmailAlertaStock))
|
||||
}
|
||||
}
|
||||
|
||||
var destinatarios []string
|
||||
if empresaID > 0 {
|
||||
miembros, _, err := repo.Empresa.ListarMiembros(empresaID, 0, 100)
|
||||
|
||||
@@ -48,12 +48,13 @@ func (b *EmpresaActualResponseBuilder) Build() (*dto.EmpresaActualResponse, erro
|
||||
}
|
||||
|
||||
return &dto.EmpresaActualResponse{
|
||||
ID: b.empresa.ID,
|
||||
Nombre: b.empresa.Nombre,
|
||||
Slug: b.empresa.Slug,
|
||||
Cuit: b.empresa.Cuit,
|
||||
Activa: b.empresa.Activa,
|
||||
Rol: string(b.rol),
|
||||
MiembrosActivos: b.miembros,
|
||||
ID: b.empresa.ID,
|
||||
Nombre: b.empresa.Nombre,
|
||||
Slug: b.empresa.Slug,
|
||||
Cuit: b.empresa.Cuit,
|
||||
EmailAlertaStock: ListaNoNil([]string(b.empresa.EmailAlertaStock)),
|
||||
Activa: b.empresa.Activa,
|
||||
Rol: string(b.rol),
|
||||
MiembrosActivos: b.miembros,
|
||||
}, nil
|
||||
}
|
||||
@@ -48,13 +48,14 @@ func (b *EmpresaAdminResponseBuilder) Build() (*dto.EmpresaAdminResponse, error)
|
||||
}
|
||||
|
||||
return &dto.EmpresaAdminResponse{
|
||||
ID: b.empresa.ID,
|
||||
Nombre: b.empresa.Nombre,
|
||||
Slug: b.empresa.Slug,
|
||||
Cuit: b.empresa.Cuit,
|
||||
Activa: b.empresa.Activa,
|
||||
Miembros: b.miembros,
|
||||
Owners: b.owners,
|
||||
CreatedAt: b.empresa.CreatedAt,
|
||||
ID: b.empresa.ID,
|
||||
Nombre: b.empresa.Nombre,
|
||||
Slug: b.empresa.Slug,
|
||||
Cuit: b.empresa.Cuit,
|
||||
EmailAlertaStock: ListaNoNil([]string(b.empresa.EmailAlertaStock)),
|
||||
Activa: b.empresa.Activa,
|
||||
Miembros: b.miembros,
|
||||
Owners: b.owners,
|
||||
CreatedAt: b.empresa.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
@@ -36,12 +36,13 @@ func (b *EmpresaResponseBuilder) Build() (*dto.EmpresaResponse, error) {
|
||||
}
|
||||
|
||||
return &dto.EmpresaResponse{
|
||||
ID: b.empresa.ID,
|
||||
Nombre: b.empresa.Nombre,
|
||||
Slug: b.empresa.Slug,
|
||||
Cuit: b.empresa.Cuit,
|
||||
Activa: b.empresa.Activa,
|
||||
CreatedAt: b.empresa.CreatedAt,
|
||||
UpdatedAt: b.empresa.UpdatedAt,
|
||||
ID: b.empresa.ID,
|
||||
Nombre: b.empresa.Nombre,
|
||||
Slug: b.empresa.Slug,
|
||||
Cuit: b.empresa.Cuit,
|
||||
EmailAlertaStock: ListaNoNil([]string(b.empresa.EmailAlertaStock)),
|
||||
Activa: b.empresa.Activa,
|
||||
CreatedAt: b.empresa.CreatedAt,
|
||||
UpdatedAt: b.empresa.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
@@ -8,6 +8,7 @@ type CrearFacturaDto struct {
|
||||
Pagos []PagoFacturaDto `json:"pagos"`
|
||||
EnviarEmail bool `json:"enviarEmail"`
|
||||
Observacion string `json:"observacion"`
|
||||
ReservaID uint `json:"reservaId"`
|
||||
}
|
||||
|
||||
type ItemFacturaDto struct {
|
||||
|
||||
@@ -4,24 +4,26 @@ import "time"
|
||||
|
||||
// EmpresaResponse es la representación pública de una empresa.
|
||||
type EmpresaResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit *string `json:"cuit,omitempty"`
|
||||
Activa bool `json:"activa"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID uint `json:"id"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit *string `json:"cuit,omitempty"`
|
||||
EmailAlertaStock []string `json:"email_alerta_stock,omitempty"`
|
||||
Activa bool `json:"activa"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// EmpresaActualResponse describe la empresa activa de la sesión.
|
||||
type EmpresaActualResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit *string `json:"cuit,omitempty"`
|
||||
Activa bool `json:"activa"`
|
||||
Rol string `json:"rol"`
|
||||
MiembrosActivos int64 `json:"miembros_activos"`
|
||||
ID uint `json:"id"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit *string `json:"cuit,omitempty"`
|
||||
EmailAlertaStock []string `json:"email_alerta_stock,omitempty"`
|
||||
Activa bool `json:"activa"`
|
||||
Rol string `json:"rol"`
|
||||
MiembrosActivos int64 `json:"miembros_activos"`
|
||||
}
|
||||
|
||||
// EmpresasResponse es el envelope de empresas del usuario.
|
||||
@@ -81,14 +83,15 @@ type InvitacionCreadaResponse struct {
|
||||
|
||||
// EmpresaAdminResponse es la vista de empresa para plataforma.
|
||||
type EmpresaAdminResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit *string `json:"cuit,omitempty"`
|
||||
Activa bool `json:"activa"`
|
||||
Miembros int64 `json:"miembros"`
|
||||
Owners int64 `json:"owners"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ID uint `json:"id"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit *string `json:"cuit,omitempty"`
|
||||
EmailAlertaStock []string `json:"email_alerta_stock,omitempty"`
|
||||
Activa bool `json:"activa"`
|
||||
Miembros int64 `json:"miembros"`
|
||||
Owners int64 `json:"owners"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// EmpresasAdminListadoResponse es el envelope de empresas de plataforma.
|
||||
|
||||
@@ -3,20 +3,22 @@ package models_seguridad
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Empresa es el tenant del sistema. Todos los datos de negocio pertenecen a
|
||||
// una empresa y se filtran por EmpresaID.
|
||||
type Empresa struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Nombre string `gorm:"type:varchar(120);not null" json:"nombre"`
|
||||
Slug string `gorm:"type:varchar(60);uniqueIndex;not null" json:"slug"`
|
||||
Cuit *string `gorm:"type:varchar(11);uniqueIndex" json:"cuit,omitempty"`
|
||||
Activa bool `gorm:"not null;default:true" json:"activa"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Nombre string `gorm:"type:varchar(120);not null" json:"nombre"`
|
||||
Slug string `gorm:"type:varchar(60);uniqueIndex;not null" json:"slug"`
|
||||
Cuit *string `gorm:"type:varchar(11);uniqueIndex" json:"cuit,omitempty"`
|
||||
EmailAlertaStock datatypes.JSONSlice[string] `gorm:"type:json" json:"email_alerta_stock,omitempty"`
|
||||
Activa bool `gorm:"not null;default:true" json:"activa"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
}
|
||||
|
||||
func (Empresa) TableName() string {
|
||||
|
||||
@@ -130,6 +130,9 @@ func (r *repositorioEmpresa) Actualizar(empresa *models_seguridad.Empresa, input
|
||||
if input.Cuit != nil {
|
||||
empresa.Cuit = input.Cuit
|
||||
}
|
||||
if input.EmailAlertaStock != nil {
|
||||
empresa.EmailAlertaStock = input.EmailAlertaStock
|
||||
}
|
||||
return r.db.Save(empresa).Error
|
||||
}
|
||||
|
||||
|
||||
@@ -97,6 +97,28 @@ func CrearFactura(
|
||||
}
|
||||
}
|
||||
|
||||
// Si se factura a partir de una reserva, validarla y consumirla al confirmar.
|
||||
var reserva *models.Reserva
|
||||
if entrada.ReservaID > 0 {
|
||||
r, err := repo.Reserva.ObtenerPorID(entrada.ReservaID)
|
||||
if err != nil || r == nil {
|
||||
return nil, fmt.Errorf("reserva %d no encontrada", entrada.ReservaID)
|
||||
}
|
||||
if r.Estado != models.ReservaActiva {
|
||||
return nil, fmt.Errorf("la reserva %d no está activa", entrada.ReservaID)
|
||||
}
|
||||
if r.AlmacenID != entrada.AlmacenID {
|
||||
return nil, errors.New("la reserva no corresponde al almacén seleccionado")
|
||||
}
|
||||
if r.ClienteID == nil || *r.ClienteID != entrada.ClienteID {
|
||||
return nil, errors.New("la reserva no corresponde al cliente seleccionado")
|
||||
}
|
||||
if len(items) != 1 || items[0].RepuestoID != uint64(r.RepuestoID) || items[0].Cantidad != r.Cantidad {
|
||||
return nil, errors.New("la reserva no corresponde a los ítems de la factura")
|
||||
}
|
||||
reserva = r
|
||||
}
|
||||
|
||||
// Calcular totales.
|
||||
var subtotal float64
|
||||
detalles := make([]models.FacturaDetalle, 0, len(items))
|
||||
@@ -198,6 +220,12 @@ func CrearFactura(
|
||||
stock := stocksPorRepuesto[item.RepuestoID]
|
||||
saldoAnterior := stock.Cantidad
|
||||
stock.Cantidad = redondearCantidad(stock.Cantidad - item.Cantidad)
|
||||
if reserva != nil && item.RepuestoID == uint64(reserva.RepuestoID) {
|
||||
stock.CantidadReservada = redondearCantidad(stock.CantidadReservada - reserva.Cantidad)
|
||||
if stock.CantidadReservada < 0 {
|
||||
stock.CantidadReservada = 0
|
||||
}
|
||||
}
|
||||
if err := repo.Stock.ActualizarCantidades(tx, stock); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -222,6 +250,18 @@ func CrearFactura(
|
||||
stocks = append(stocks, stock)
|
||||
}
|
||||
|
||||
if reserva != nil {
|
||||
resultado := tx.Model(&models.Reserva{}).
|
||||
Where("id = ? AND estado = ?", reserva.ID, models.ReservaActiva).
|
||||
Update("estado", models.ReservaConsumida)
|
||||
if resultado.Error != nil {
|
||||
return resultado.Error
|
||||
}
|
||||
if resultado.RowsAffected == 0 {
|
||||
return errors.New("la reserva ya no está activa")
|
||||
}
|
||||
}
|
||||
|
||||
if err := RegistrarAsientoVenta(tx, repo, factura, subtotal, descuentoTotal, impuestos, total); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user