Files
Repuestera/back/repositorios/RepositorioComprobanteSecuencia.go
T
2026-11-28 14:58:08 -03:00

50 lines
1.2 KiB
Go

package repositorios
import (
"repuestera_back/models"
"gorm.io/gorm"
)
type RepositorioComprobanteSecuencia interface {
Siguiente(tx *gorm.DB, empresaID uint, tipo string) (uint, error)
}
type repositorioComprobanteSecuencia struct {
db *gorm.DB
empresaID uint
}
func NuevoRepositorioComprobanteSecuencia(db *gorm.DB, empresaID uint) RepositorioComprobanteSecuencia {
return &repositorioComprobanteSecuencia{
db: dbConEmpresa(db, empresaID),
empresaID: empresaID,
}
}
func (r *repositorioComprobanteSecuencia) Siguiente(tx *gorm.DB, empresaID uint, tipo string) (uint, error) {
var seq models.ComprobanteSecuencia
db := tx
if empresaID > 0 {
db = db.Where("empresa_id = ?", empresaID)
}
if err := db.Where("tipo = ?", tipo).First(&seq).Error; err != nil {
if err != gorm.ErrRecordNotFound {
return 0, err
}
seq = models.ComprobanteSecuencia{
EmpresaID: empresaID,
Tipo: tipo,
UltimoNumero: 0,
}
if err := tx.Create(&seq).Error; err != nil {
return 0, err
}
}
seq.UltimoNumero++
if err := tx.Model(&seq).Update("ultimo_numero", seq.UltimoNumero).Error; err != nil {
return 0, err
}
return seq.UltimoNumero, nil
}