69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"BE-MiniERP/database"
|
|
"BE-MiniERP/modules/inventory/models"
|
|
"BE-MiniERP/modules/inventory/repository"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type StockMovementHandler struct {
|
|
Repo *repository.StockMovementRepository
|
|
}
|
|
|
|
func NewStockMovementHandler() *StockMovementHandler {
|
|
return &StockMovementHandler{
|
|
Repo: repository.NewStockMovementRepository(database.DB),
|
|
}
|
|
}
|
|
|
|
func (h *StockMovementHandler) GetAll(c *fiber.Ctx) error {
|
|
page := c.QueryInt("page", 1)
|
|
limit := c.QueryInt("limit", 10) // default 10
|
|
offset := (page - 1) * limit
|
|
search := c.Query("search", "")
|
|
productID := c.QueryInt("product_id", 0)
|
|
dateStr := c.Query("date", "")
|
|
originID := c.QueryInt("origin_id", 0)
|
|
destinationID := c.QueryInt("destination_id", 0)
|
|
|
|
var selectedDate time.Time
|
|
var err error
|
|
|
|
if dateStr != "" {
|
|
selectedDate, err = time.Parse("2006-01-02", dateStr)
|
|
if err != nil {
|
|
return c.Status(400).JSON(fiber.Map{
|
|
"error": "Invalid date format. Use YYYY-MM-DD",
|
|
})
|
|
}
|
|
}
|
|
|
|
data, err := h.Repo.FindAll(
|
|
page, limit, offset,
|
|
search,
|
|
uint(productID), uint(originID), uint(destinationID),
|
|
selectedDate,
|
|
)
|
|
|
|
if err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch stock movements"})
|
|
}
|
|
return c.JSON(data)
|
|
}
|
|
|
|
func (h *StockMovementHandler) Create(c *fiber.Ctx) error {
|
|
var input models.StockMovement
|
|
if err := c.BodyParser(&input); err != nil {
|
|
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
|
|
}
|
|
|
|
if err := h.Repo.Create(&input); err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": "Failed to create stock movement"})
|
|
}
|
|
|
|
return c.JSON(input)
|
|
}
|