Private
Public Access
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package repositorios
|
|
|
|
import (
|
|
"time"
|
|
|
|
"repuestera_back/models"
|
|
|
|
"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)
|
|
ObtenerConRelaciones(id uint) (*models.Factura, error)
|
|
}
|
|
|
|
type repositorioFactura struct {
|
|
*RepositorioBase[models.Factura]
|
|
db *gorm.DB
|
|
empresaID uint
|
|
}
|
|
|
|
func NuevoRepositorioFactura(db *gorm.DB, empresaID uint) RepositorioFactura {
|
|
return &repositorioFactura{
|
|
RepositorioBase: NewRepositorioBase[models.Factura](db, empresaID),
|
|
db: dbConEmpresa(db, empresaID),
|
|
empresaID: empresaID,
|
|
}
|
|
}
|
|
|
|
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))
|
|
}
|
|
|
|
if err := query.Order("factura.created_at DESC").Find(&facturas).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return facturas, nil
|
|
}
|
|
|
|
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 {
|
|
return nil, err
|
|
}
|
|
return &factura, nil
|
|
}
|