54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"BE-MiniERP/modules/inventory/models"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type StockMovementRepository struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
func NewStockMovementRepository(db *gorm.DB) *StockMovementRepository {
|
|
return &StockMovementRepository{DB: db}
|
|
}
|
|
|
|
func (r *StockMovementRepository) FindAll(page, limit, offset int, search string, productID, originID, destinationID uint, selectedDate time.Time) ([]models.StockMovement, error) {
|
|
var movements []models.StockMovement
|
|
query := r.DB.Model(&models.StockMovement{}).
|
|
Preload("Product").
|
|
Preload("OriginWarehouse").
|
|
Preload("DestinationWarehouse")
|
|
|
|
if search != "" {
|
|
query = query.Where("stock_type ILIKE ?", "%"+search+"%")
|
|
}
|
|
|
|
if productID != 0 {
|
|
query = query.Where("product_id = ?", productID)
|
|
}
|
|
if originID != 0 {
|
|
query = query.Where("origin_warehouse_id = ?", originID)
|
|
}
|
|
if destinationID != 0 {
|
|
query = query.Where("destination_warehouse_id = ?", destinationID)
|
|
}
|
|
|
|
if !selectedDate.IsZero() {
|
|
query = query.Where("DATE(created_at) = ?", selectedDate.Format("2006-01-02"))
|
|
}
|
|
|
|
err := query.
|
|
Order("created_at DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&movements).Error
|
|
return movements, err
|
|
}
|
|
|
|
func (r *StockMovementRepository) Create(movement *models.StockMovement) error {
|
|
return r.DB.Create(movement).Error
|
|
}
|