Private
Public Access
commit parcial: refactor de decorator factura, añadida parcialemnte
funcionalidad de facturas tengo sueño Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
3c4bc5c008
commit
42bf987ffc
19 files changed
+525
-37
No files matched your search
@@ -11,8 +11,15 @@ type RepositorioFactura interface {
|
||||
IRepositorio[models.Factura]
|
||||
Listar(filtros decorator.FiltrosFactura) ([]models.Factura, error)
|
||||
ObtenerConRelaciones(id uint) (*models.Factura, error)
|
||||
CrearConDetalles(tx *gorm.DB, factura *models.Factura) error
|
||||
BloquearPorID(tx *gorm.DB, id uint) (*models.Factura, error)
|
||||
ActualizarEstadoSaldo(tx *gorm.DB, id uint, estado string, saldo float64) error
|
||||
ExisteNumero(numero string) (bool, error)
|
||||
}
|
||||
|
||||
// FiltrosFactura se mantiene como alias del paquete decorator.
|
||||
type FiltrosFactura = decorator.FiltrosFactura
|
||||
|
||||
type repositorioFactura struct {
|
||||
*RepositorioBase[models.Factura]
|
||||
db *gorm.DB
|
||||
@@ -27,7 +34,7 @@ func NuevoRepositorioFactura(db *gorm.DB, empresaID uint) RepositorioFactura {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *repositorioFactura) Listar(filtros FiltrosFactura) ([]models.Factura, error) {
|
||||
func (r *repositorioFactura) Listar(filtros decorator.FiltrosFactura) ([]models.Factura, error) {
|
||||
var facturas []models.Factura
|
||||
|
||||
paginar := filtros.Pagina > 0 || filtros.PorPagina > 0
|
||||
@@ -41,8 +48,47 @@ func (r *repositorioFactura) Listar(filtros FiltrosFactura) ([]models.Factura, e
|
||||
|
||||
func (r *repositorioFactura) ObtenerConRelaciones(id uint) (*models.Factura, error) {
|
||||
var factura models.Factura
|
||||
if err := r.db.Preload("Cliente").First(&factura, id).Error; err != nil {
|
||||
if err := r.db.Preload("Cliente").Preload("Almacen").First(&factura, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &factura, nil
|
||||
}
|
||||
|
||||
func (r *repositorioFactura) CrearConDetalles(tx *gorm.DB, factura *models.Factura) error {
|
||||
if r.empresaID > 0 {
|
||||
factura.EmpresaID = r.empresaID
|
||||
}
|
||||
return tx.Create(factura).Error
|
||||
}
|
||||
|
||||
func (r *repositorioFactura) BloquearPorID(tx *gorm.DB, id uint) (*models.Factura, error) {
|
||||
var factura models.Factura
|
||||
db := tx
|
||||
if r.empresaID > 0 {
|
||||
db = db.Where("empresa_id = ?", r.empresaID)
|
||||
}
|
||||
if err := db.Set("gorm:query_option", "FOR UPDATE").Preload("Cliente").First(&factura, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &factura, nil
|
||||
}
|
||||
|
||||
func (r *repositorioFactura) ActualizarEstadoSaldo(tx *gorm.DB, id uint, estado string, saldo float64) error {
|
||||
db := tx.Model(&models.Factura{})
|
||||
if r.empresaID > 0 {
|
||||
db = db.Where("empresa_id = ?", r.empresaID)
|
||||
}
|
||||
return db.Where("id = ?", id).Updates(map[string]any{
|
||||
"estado": estado,
|
||||
"saldo": saldo,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (r *repositorioFactura) ExisteNumero(numero string) (bool, error) {
|
||||
var count int64
|
||||
db := r.db.Model(&models.Factura{})
|
||||
if err := db.Where("numero = ?", numero).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
Reference in new issue
Block a user