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,72 @@
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user