Private
Public Access
bueno es parte del plan de implementacion para el caso de uso de gestion
de stock
This commit is contained in:
1 parent
24e746b1b7
commit
6ed8805e21
40 files changed
+2748
-31
No files matched your search
@@ -0,0 +1,99 @@
|
||||
package repositorios
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"repuestera_back/models/contabilidad"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type RepositorioContabilidad interface {
|
||||
CrearAsientoConMovimientos(tx *gorm.DB, asiento *contabilidad.AsientoContable) error
|
||||
ObtenerCuentaPorCodigo(tx *gorm.DB, codigo string) (*contabilidad.CuentaContable, error)
|
||||
GuardarCuenta(tx *gorm.DB, cuenta *contabilidad.CuentaContable) error
|
||||
ListarCuentas() ([]contabilidad.CuentaContable, error)
|
||||
}
|
||||
|
||||
type repositorioContabilidad struct {
|
||||
db *gorm.DB
|
||||
empresaID uint
|
||||
}
|
||||
|
||||
func NuevoRepositorioContabilidad(db *gorm.DB, empresaID uint) RepositorioContabilidad {
|
||||
return &repositorioContabilidad{
|
||||
db: dbConEmpresa(db, empresaID),
|
||||
empresaID: empresaID,
|
||||
}
|
||||
}
|
||||
|
||||
// CrearAsientoConMovimientos persiste el asiento y cada una de sus lineas.
|
||||
func (r *repositorioContabilidad) CrearAsientoConMovimientos(tx *gorm.DB, asiento *contabilidad.AsientoContable) error {
|
||||
if asiento == nil {
|
||||
return gorm.ErrInvalidValue
|
||||
}
|
||||
if r.empresaID > 0 {
|
||||
asiento.EmpresaID = r.empresaID
|
||||
}
|
||||
|
||||
if err := tx.Omit(clause.Associations).Create(asiento).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range asiento.Movimientos {
|
||||
asiento.Movimientos[i].AsientoID = asiento.ID
|
||||
if r.empresaID > 0 {
|
||||
asiento.Movimientos[i].EmpresaID = r.empresaID
|
||||
}
|
||||
}
|
||||
if len(asiento.Movimientos) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tx.Create(&asiento.Movimientos).Error
|
||||
}
|
||||
|
||||
// ObtenerCuentaPorCodigo busca la cuenta por su nombre/codigo contable.
|
||||
func (r *repositorioContabilidad) ObtenerCuentaPorCodigo(tx *gorm.DB, codigo string) (*contabilidad.CuentaContable, error) {
|
||||
var cuenta contabilidad.CuentaContable
|
||||
query := tx.Where("nombre = ?", codigo)
|
||||
if r.empresaID > 0 {
|
||||
query = query.Where("empresa_id = ?", r.empresaID)
|
||||
}
|
||||
if err := query.First(&cuenta).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cuenta, nil
|
||||
}
|
||||
|
||||
// GuardarCuenta crea la cuenta si no existe para la empresa.
|
||||
func (r *repositorioContabilidad) GuardarCuenta(tx *gorm.DB, cuenta *contabilidad.CuentaContable) error {
|
||||
if cuenta == nil {
|
||||
return gorm.ErrInvalidValue
|
||||
}
|
||||
if r.empresaID > 0 {
|
||||
cuenta.EmpresaID = r.empresaID
|
||||
}
|
||||
|
||||
existente, err := r.ObtenerCuentaPorCodigo(tx, cuenta.Nombre)
|
||||
if err == nil {
|
||||
cuenta.ID = existente.ID
|
||||
query := tx.Model(&contabilidad.CuentaContable{}).Where("id = ?", existente.ID)
|
||||
if r.empresaID > 0 {
|
||||
query = query.Where("empresa_id = ?", r.empresaID)
|
||||
}
|
||||
return query.Update("tipo", cuenta.Tipo).Error
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
return tx.Create(cuenta).Error
|
||||
}
|
||||
|
||||
func (r *repositorioContabilidad) ListarCuentas() ([]contabilidad.CuentaContable, error) {
|
||||
var cuentas []contabilidad.CuentaContable
|
||||
if err := r.db.Order("nombre ASC").Find(&cuentas).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cuentas, nil
|
||||
}
|
||||
Reference in new issue
Block a user