fix filter purchase supplier repport

This commit is contained in:
ragilap
2026-04-02 11:43:44 +07:00
parent 434ae2f246
commit 8be4b54127
6 changed files with 303 additions and 53 deletions
@@ -1,6 +1,7 @@
package controller
import (
"fmt"
"math"
"strconv"
"strings"
@@ -158,17 +159,34 @@ func (c *RepportController) GetMarketing(ctx *fiber.Ctx) error {
}
func (c *RepportController) GetPurchaseSupplier(ctx *fiber.Ctx) error {
areaIDs, err := parseCommaSeparatedInt64sWithField(ctx.Query("area_id", ""), "area_id")
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
supplierIDs, err := parseCommaSeparatedInt64sWithField(ctx.Query("supplier_id", ""), "supplier_id")
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
productIDs, err := parseCommaSeparatedInt64sWithField(ctx.Query("product_id", ""), "product_id")
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
productCategoryIDs, err := parseCommaSeparatedInt64sWithField(ctx.Query("product_category_id", ""), "product_category_id")
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
query := &validation.PurchaseSupplierQuery{
Page: ctx.QueryInt("page", 1),
Limit: ctx.QueryInt("limit", 10),
AreaId: int64(ctx.QueryInt("area_id", 0)),
SupplierId: int64(ctx.QueryInt("supplier_id", 0)),
ProductId: int64(ctx.QueryInt("product_id", 0)),
ProductCategoryId: int64(ctx.QueryInt("product_category_id", 0)),
StartDate: ctx.Query("start_date", ""),
EndDate: ctx.Query("end_date", ""),
SortBy: ctx.Query("sort_by", ""),
FilterBy: ctx.Query("filter_by", ""),
Page: ctx.QueryInt("page", 1),
Limit: ctx.QueryInt("limit", 10),
AreaIDs: areaIDs,
SupplierIDs: supplierIDs,
ProductIDs: productIDs,
ProductCategoryIDs: productCategoryIDs,
StartDate: ctx.Query("start_date", ""),
EndDate: ctx.Query("end_date", ""),
SortBy: ctx.Query("sort_by", ""),
FilterBy: ctx.Query("filter_by", ""),
}
areaScope, err := m.ResolveAreaScope(ctx, c.RepportService.DB())
@@ -189,10 +207,10 @@ func (c *RepportController) GetPurchaseSupplier(ctx *fiber.Ctx) error {
}
filters := map[string]interface{}{
"area_id": query.AreaId,
"supplier_id": query.SupplierId,
"product_id": query.ProductId,
"product_category_id": query.ProductCategoryId,
"area_id": query.AreaIDs,
"supplier_id": query.SupplierIDs,
"product_id": query.ProductIDs,
"product_category_id": query.ProductCategoryIDs,
"start_date": query.StartDate,
"end_date": query.EndDate,
"sort_by": query.SortBy,
@@ -412,6 +430,9 @@ func (c *RepportController) GetProductionResult(ctx *fiber.Ctx) error {
}
func parseCommaSeparatedInt64s(raw string) ([]int64, error) {
return parseCommaSeparatedInt64sWithField(raw, "supplier_ids")
}
func parseCommaSeparatedInt64sWithField(raw, field string) ([]int64, error) {
raw = strings.TrimSpace(raw)
if raw == "" {
return []int64{}, nil
@@ -427,7 +448,7 @@ func parseCommaSeparatedInt64s(raw string) ([]int64, error) {
id, err := strconv.ParseInt(part, 10, 64)
if err != nil {
return nil, fiber.NewError(fiber.StatusBadRequest, "supplier_ids must be comma separated integers")
return nil, fmt.Errorf("%s must be comma separated integers", field)
}
result = append(result, id)
}