diff --git a/back/config/permisos.go b/back/config/permisos.go
index 868eaab..bd9de6f 100644
--- a/back/config/permisos.go
+++ b/back/config/permisos.go
@@ -30,6 +30,7 @@ var permisosSemilla = []permisoSemilla{
{11, "Gestión de Proveedores", "proveedores", "Consultar y administrar los proveedores de la empresa."},
{12, "Gestión de Clientes", "clientes", "Consultar y administrar los clientes de la empresa."},
{14, "Generar Reporte de Stock", "stock/reportes", "Generar y consultar reportes de stock."},
+ {15, "Gestión de Ventas y Facturación", "facturas", "Consultar y operar la facturación y ventas de la empresa."},
}
// InitPermisos crea el catálogo global de permisos y garantiza que cada
@@ -86,6 +87,8 @@ func AsegurarGruposEmpresa(db *gorm.DB, empresaID uint) (uint, uint, error) {
10,
11,
12,
+ 13,
+ 14,
}
if err := asignarPermisosFaltantes(db, admin.ID, permisosAdmin); err != nil {
return 0, 0, err
@@ -98,6 +101,7 @@ func AsegurarGruposEmpresa(db *gorm.DB, empresaID uint) (uint, uint, error) {
10,
11,
12,
+ 13,
}
if err := asignarPermisosFaltantes(db, usuario.ID, permisosUsuario); err != nil {
return 0, 0, err
diff --git a/back/repositorios/Decorator/facturas/DecoradorCliente.go b/back/repositorios/Decorator/facturas/DecoradorCliente.go
new file mode 100644
index 0000000..2c921d4
--- /dev/null
+++ b/back/repositorios/Decorator/facturas/DecoradorCliente.go
@@ -0,0 +1,17 @@
+package repositorios_decorator
+
+import "gorm.io/gorm"
+
+type DecoradorCliente struct {
+ envuelto IFacturaQuery
+ cliente string
+}
+
+func (d DecoradorCliente) Aplicar(db *gorm.DB) *gorm.DB {
+ consulta := d.envuelto.Aplicar(db)
+ if d.cliente != "" {
+ consulta = consulta.Joins("JOIN clientes ON clientes.id = factura.`ClienteID`").
+ Where("clientes.nombre LIKE ?", "%"+d.cliente+"%")
+ }
+ return consulta
+}
diff --git a/back/repositorios/Decorator/facturas/DecoradorClienteID.go b/back/repositorios/Decorator/facturas/DecoradorClienteID.go
new file mode 100644
index 0000000..b261494
--- /dev/null
+++ b/back/repositorios/Decorator/facturas/DecoradorClienteID.go
@@ -0,0 +1,16 @@
+package repositorios_decorator
+
+import "gorm.io/gorm"
+
+type DecoradorClienteID struct {
+ envuelto IFacturaQuery
+ clienteID uint
+}
+
+func (d DecoradorClienteID) Aplicar(db *gorm.DB) *gorm.DB {
+ consulta := d.envuelto.Aplicar(db)
+ if d.clienteID > 0 {
+ consulta = consulta.Where("`ClienteID` = ?", d.clienteID)
+ }
+ return consulta
+}
diff --git a/back/repositorios/Decorator/facturas/DecoradorDesdeHasta.go b/back/repositorios/Decorator/facturas/DecoradorDesdeHasta.go
new file mode 100644
index 0000000..5461e87
--- /dev/null
+++ b/back/repositorios/Decorator/facturas/DecoradorDesdeHasta.go
@@ -0,0 +1,33 @@
+package repositorios_decorator
+
+import (
+ "time"
+
+ "gorm.io/gorm"
+)
+
+type DecoradorDesde struct {
+ envuelto IFacturaQuery
+ desde *time.Time
+}
+
+func (d DecoradorDesde) Aplicar(db *gorm.DB) *gorm.DB {
+ consulta := d.envuelto.Aplicar(db)
+ if d.desde != nil {
+ consulta = consulta.Where("factura.created_at >= ?", *d.desde)
+ }
+ return consulta
+}
+
+type DecoradorHasta struct {
+ envuelto IFacturaQuery
+ hasta *time.Time
+}
+
+func (d DecoradorHasta) Aplicar(db *gorm.DB) *gorm.DB {
+ consulta := d.envuelto.Aplicar(db)
+ if d.hasta != nil {
+ consulta = consulta.Where("factura.created_at < ?", d.hasta.AddDate(0, 0, 1))
+ }
+ return consulta
+}
diff --git a/back/repositorios/Decorator/facturas/DecoradorNumero.go b/back/repositorios/Decorator/facturas/DecoradorNumero.go
new file mode 100644
index 0000000..179ce44
--- /dev/null
+++ b/back/repositorios/Decorator/facturas/DecoradorNumero.go
@@ -0,0 +1,16 @@
+package repositorios_decorator
+
+import "gorm.io/gorm"
+
+type DecoradorNumero struct {
+ envuelto IFacturaQuery
+ numero string
+}
+
+func (d DecoradorNumero) Aplicar(db *gorm.DB) *gorm.DB {
+ consulta := d.envuelto.Aplicar(db)
+ if d.numero != "" {
+ consulta = consulta.Where("numero LIKE ?", "%"+d.numero+"%")
+ }
+ return consulta
+}
diff --git a/back/repositorios/Decorator/facturas/DecoradorPaginacion.go b/back/repositorios/Decorator/facturas/DecoradorPaginacion.go
new file mode 100644
index 0000000..321b6b3
--- /dev/null
+++ b/back/repositorios/Decorator/facturas/DecoradorPaginacion.go
@@ -0,0 +1,31 @@
+package repositorios_decorator
+
+import "gorm.io/gorm"
+
+type DecoradorPaginacion struct {
+ envuelto IFacturaQuery
+ pagina int
+ porPagina int
+}
+
+func (d DecoradorPaginacion) Aplicar(db *gorm.DB) *gorm.DB {
+ consulta := d.envuelto.Aplicar(db)
+ pagina, porPagina := NormalizarPagina(d.pagina, d.porPagina)
+ consulta = consulta.Order("factura.created_at DESC, factura.id DESC").
+ Offset((pagina - 1) * porPagina).
+ Limit(porPagina)
+ return consulta
+}
+
+func NormalizarPagina(pagina, porPagina int) (int, int) {
+ if pagina < 1 {
+ pagina = 1
+ }
+ if porPagina < 1 {
+ porPagina = 50
+ }
+ if porPagina > 200 {
+ porPagina = 200
+ }
+ return pagina, porPagina
+}
diff --git a/back/repositorios/Decorator/facturas/FiltrosFactura.go b/back/repositorios/Decorator/facturas/FiltrosFactura.go
new file mode 100644
index 0000000..04b074b
--- /dev/null
+++ b/back/repositorios/Decorator/facturas/FiltrosFactura.go
@@ -0,0 +1,14 @@
+package repositorios_decorator
+
+import "time"
+
+// FiltrosFactura acota la consulta del listado de facturas.
+type FiltrosFactura struct {
+ Numero string
+ ClienteID uint
+ Cliente string
+ Desde *time.Time
+ Hasta *time.Time
+ Pagina int
+ PorPagina int
+}
diff --git a/back/repositorios/Decorator/facturas/IFacturaQuery.go b/back/repositorios/Decorator/facturas/IFacturaQuery.go
new file mode 100644
index 0000000..c382aa9
--- /dev/null
+++ b/back/repositorios/Decorator/facturas/IFacturaQuery.go
@@ -0,0 +1,25 @@
+package repositorios_decorator
+
+import (
+ "repuestera_back/models"
+
+ "gorm.io/gorm"
+)
+
+// IFacturaQuery es el componente base del patrón Decorator para facturas.
+type IFacturaQuery interface {
+ Aplicar(db *gorm.DB) *gorm.DB
+}
+
+// FacturaQueryBase es la implementación base de la cadena.
+type FacturaQueryBase struct {
+ Preload bool
+}
+
+func (q FacturaQueryBase) Aplicar(db *gorm.DB) *gorm.DB {
+ consulta := db.Model(&models.Factura{})
+ if q.Preload {
+ consulta = consulta.Preload("Cliente")
+ }
+ return consulta
+}
diff --git a/back/repositorios/Decorator/facturas/NuevaQueryFacturas.go b/back/repositorios/Decorator/facturas/NuevaQueryFacturas.go
new file mode 100644
index 0000000..295250d
--- /dev/null
+++ b/back/repositorios/Decorator/facturas/NuevaQueryFacturas.go
@@ -0,0 +1,31 @@
+package repositorios_decorator
+
+import "gorm.io/gorm"
+
+// NuevaQueryFacturas arma la cadena de decoradores según los filtros.
+func NuevaQueryFacturas(filtros FiltrosFactura, preload bool, paginar bool) IFacturaQuery {
+ var actual IFacturaQuery = FacturaQueryBase{Preload: preload}
+
+ actual = DecoradorNumero{envuelto: actual, numero: filtros.Numero}
+ actual = DecoradorClienteID{envuelto: actual, clienteID: filtros.ClienteID}
+ actual = DecoradorCliente{envuelto: actual, cliente: filtros.Cliente}
+ actual = DecoradorDesde{envuelto: actual, desde: filtros.Desde}
+ actual = DecoradorHasta{envuelto: actual, hasta: filtros.Hasta}
+ if paginar {
+ actual = DecoradorPaginacion{envuelto: actual, pagina: filtros.Pagina, porPagina: filtros.PorPagina}
+ } else {
+ actual = decoradorOrdenFacturas{envuelto: actual}
+ }
+
+ return actual
+}
+
+// decoradorOrdenFacturas aplica el orden por defecto cuando no se pagina.
+type decoradorOrdenFacturas struct {
+ envuelto IFacturaQuery
+}
+
+func (d decoradorOrdenFacturas) Aplicar(db *gorm.DB) *gorm.DB {
+ consulta := d.envuelto.Aplicar(db)
+ return consulta.Order("factura.created_at DESC")
+}
diff --git a/back/repositorios/RepositorioFactura.go b/back/repositorios/RepositorioFactura.go
index c3b92cb..df909f5 100644
--- a/back/repositorios/RepositorioFactura.go
+++ b/back/repositorios/RepositorioFactura.go
@@ -1,24 +1,15 @@
package repositorios
import (
- "time"
-
"repuestera_back/models"
+ decorator "repuestera_back/repositorios/Decorator/facturas"
"gorm.io/gorm"
)
-type FiltrosFactura struct {
- Numero string
- ClienteID uint
- Cliente string
- Desde *time.Time
- Hasta *time.Time
-}
-
type RepositorioFactura interface {
IRepositorio[models.Factura]
- Listar(filtros FiltrosFactura) ([]models.Factura, error)
+ Listar(filtros decorator.FiltrosFactura) ([]models.Factura, error)
ObtenerConRelaciones(id uint) (*models.Factura, error)
}
@@ -39,25 +30,10 @@ func NuevoRepositorioFactura(db *gorm.DB, empresaID uint) RepositorioFactura {
func (r *repositorioFactura) Listar(filtros FiltrosFactura) ([]models.Factura, error) {
var facturas []models.Factura
- query := r.db.Model(&models.Factura{}).Preload("Cliente")
- if filtros.Numero != "" {
- query = query.Where("numero LIKE ?", "%"+filtros.Numero+"%")
- }
- if filtros.ClienteID > 0 {
- query = query.Where("`ClienteID` = ?", filtros.ClienteID)
- }
- if filtros.Cliente != "" {
- query = query.Joins("JOIN clientes ON clientes.id = factura.`ClienteID`").
- Where("clientes.nombre LIKE ?", "%"+filtros.Cliente+"%")
- }
- if filtros.Desde != nil {
- query = query.Where("factura.created_at >= ?", *filtros.Desde)
- }
- if filtros.Hasta != nil {
- query = query.Where("factura.created_at < ?", filtros.Hasta.AddDate(0, 0, 1))
- }
+ paginar := filtros.Pagina > 0 || filtros.PorPagina > 0
+ query := decorator.NuevaQueryFacturas(filtros, true, paginar).Aplicar(r.db)
- if err := query.Order("factura.created_at DESC").Find(&facturas).Error; err != nil {
+ if err := query.Find(&facturas).Error; err != nil {
return nil, err
}
return facturas, nil
diff --git a/back/routes/FacturaRoute.go b/back/routes/FacturaRoute.go
index e932294..568cef5 100644
--- a/back/routes/FacturaRoute.go
+++ b/back/routes/FacturaRoute.go
@@ -8,10 +8,19 @@ import (
)
func FacturaRoute(r *gin.Engine) {
+ // Lectura: accesible tanto por gestión de stock (para devoluciones) como
+ // por gestión de ventas.
grupo := r.Group("/facturas")
- grupo.Use(middleware.AuthMiddleware(9))
+ grupo.Use(middleware.AuthMiddleware(9, 15))
{
grupo.GET("/", factura.ObtenerFacturas)
grupo.GET("/:id", factura.ObtenerFactura)
}
+
+ // Escritura: solo gestión de ventas. Se agregan en CU-02, CU-05, CU-06,
+ // CU-07, CU-08 y CU-16.
+ escritura := r.Group("/facturas")
+ escritura.Use(middleware.AuthMiddleware(15))
+ {
+ }
}
diff --git a/front/src/lib/hooks/facturas/obtenerFacturas.ts b/front/src/lib/hooks/facturas/obtenerFacturas.ts
index d4afdbf..8f39fb7 100644
--- a/front/src/lib/hooks/facturas/obtenerFacturas.ts
+++ b/front/src/lib/hooks/facturas/obtenerFacturas.ts
@@ -1,10 +1,34 @@
import { API_URL } from '$lib/env';
import { getToken } from '../token';
-export async function obtenerFacturas(fetch2?: Function) {
+export interface FiltrosFactura {
+ numero?: string;
+ cliente?: string;
+ clienteId?: number;
+ estado?: string;
+ desde?: string;
+ hasta?: string;
+ pagina?: number;
+ porPagina?: number;
+}
+
+export async function obtenerFacturas(fetch2?: Function, filtros?: FiltrosFactura) {
const fetchfn = fetch2 ? fetch2 : fetch;
+ const params = new URLSearchParams();
+ if (filtros) {
+ if (filtros.numero) params.set('numero', filtros.numero);
+ if (filtros.cliente) params.set('cliente', filtros.cliente);
+ if (filtros.clienteId) params.set('clienteId', String(filtros.clienteId));
+ if (filtros.estado) params.set('estado', filtros.estado);
+ if (filtros.desde) params.set('desde', filtros.desde);
+ if (filtros.hasta) params.set('hasta', filtros.hasta);
+ if (filtros.pagina) params.set('pagina', String(filtros.pagina));
+ if (filtros.porPagina) params.set('porPagina', String(filtros.porPagina));
+ }
+ const qs = params.toString();
+ const url = `${API_URL}/facturas/${qs ? `?${qs}` : ''}`;
try {
- const req = await fetchfn(`${API_URL}/facturas/`, {
+ const req = await fetchfn(url, {
method: 'GET',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${getToken()}` }
});
diff --git a/front/src/routes/facturas/+page.svelte b/front/src/routes/facturas/+page.svelte
new file mode 100644
index 0000000..4653aa2
--- /dev/null
+++ b/front/src/routes/facturas/+page.svelte
@@ -0,0 +1,231 @@
+
+
+
Cliente: {facturaVer.cliente?.nombre ?? '-'}
+CUIT: {facturaVer.cliente?.cuit ?? '-'}
+Fecha: {formatFecha(facturaVer.CreatedAt)}
+Total: {formatMonto(facturaVer.total)}
+Estado: {facturaVer.estado}
+ {#if facturaVer.detalles && facturaVer.detalles.length > 0} +Ítems
+