diff --git a/internal/modules/repports/controllers/repport.controller.go b/internal/modules/repports/controllers/repport.controller.go index aff0a718..5d85a53e 100644 --- a/internal/modules/repports/controllers/repport.controller.go +++ b/internal/modules/repports/controllers/repport.controller.go @@ -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,10 @@ 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 +449,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) } diff --git a/internal/modules/repports/repositories/purchase_supplier.repository.go b/internal/modules/repports/repositories/purchase_supplier.repository.go index 3206eaa5..af98a573 100644 --- a/internal/modules/repports/repositories/purchase_supplier.repository.go +++ b/internal/modules/repports/repositories/purchase_supplier.repository.go @@ -60,24 +60,24 @@ func (r *purchaseSupplierRepositoryImpl) baseSupplierQuery(ctx context.Context, Where("(la.action IS NULL OR la.action != ?)", string(entity.ApprovalActionRejected)). Where("purchase_items.received_date IS NOT NULL") - if filters.SupplierId > 0 { - db = db.Where("suppliers.id = ?", filters.SupplierId) + if len(filters.SupplierIDs) > 0 { + db = db.Where("suppliers.id IN ?", filters.SupplierIDs) } - if filters.ProductId > 0 { - db = db.Where("purchase_items.product_id = ?", filters.ProductId) + if len(filters.ProductIDs) > 0 { + db = db.Where("purchase_items.product_id IN ?", filters.ProductIDs) } - if filters.ProductCategoryId > 0 { + if len(filters.ProductCategoryIDs) > 0 { db = db. Joins("JOIN products ON products.id = purchase_items.product_id"). - Where("products.product_category_id = ?", filters.ProductCategoryId) + Where("products.product_category_id IN ?", filters.ProductCategoryIDs) } - if filters.AreaId > 0 || filters.AllowedAreaIDs != nil { + if len(filters.AreaIDs) > 0 || filters.AllowedAreaIDs != nil { db = db.Joins("JOIN warehouses ON warehouses.id = purchase_items.warehouse_id") - if filters.AreaId > 0 { - db = db.Where("warehouses.area_id = ?", filters.AreaId) + if len(filters.AreaIDs) > 0 { + db = db.Where("warehouses.area_id IN ?", filters.AreaIDs) } if filters.AllowedAreaIDs != nil { if len(filters.AllowedAreaIDs) == 0 { @@ -187,20 +187,20 @@ func (r *purchaseSupplierRepositoryImpl) GetItemsBySuppliers(ctx context.Context Where("(la.action IS NULL OR la.action != ?)", string(entity.ApprovalActionRejected)). Where("purchase_items.received_date IS NOT NULL") - if filters.ProductId > 0 { - db = db.Where("purchase_items.product_id = ?", filters.ProductId) + if len(filters.ProductIDs) > 0 { + db = db.Where("purchase_items.product_id IN ?", filters.ProductIDs) } - if filters.ProductCategoryId > 0 { + if len(filters.ProductCategoryIDs) > 0 { db = db. Joins("JOIN products ON products.id = purchase_items.product_id"). - Where("products.product_category_id = ?", filters.ProductCategoryId) + Where("products.product_category_id IN ?", filters.ProductCategoryIDs) } - if filters.AreaId > 0 || filters.AllowedAreaIDs != nil { + if len(filters.AreaIDs) > 0 || filters.AllowedAreaIDs != nil { db = db.Joins("JOIN warehouses ON warehouses.id = purchase_items.warehouse_id") - if filters.AreaId > 0 { - db = db.Where("warehouses.area_id = ?", filters.AreaId) + if len(filters.AreaIDs) > 0 { + db = db.Where("warehouses.area_id IN ?", filters.AreaIDs) } if filters.AllowedAreaIDs != nil { if len(filters.AllowedAreaIDs) == 0 { diff --git a/internal/modules/repports/validations/repport.validation.go b/internal/modules/repports/validations/repport.validation.go index 97ea60fa..d248c779 100644 --- a/internal/modules/repports/validations/repport.validation.go +++ b/internal/modules/repports/validations/repport.validation.go @@ -38,17 +38,17 @@ type MarketingQuery struct { } type PurchaseSupplierQuery struct { - Page int `query:"page" validate:"omitempty,min=1,gt=0"` - Limit int `query:"limit" validate:"omitempty,min=1,gt=0"` - AreaId int64 `query:"area_id" validate:"omitempty"` - SupplierId int64 `query:"supplier_id" validate:"omitempty"` - ProductId int64 `query:"product_id" validate:"omitempty"` - ProductCategoryId int64 `query:"product_category_id" validate:"omitempty"` - StartDate string `query:"start_date" validate:"omitempty"` - EndDate string `query:"end_date" validate:"omitempty"` - SortBy string `query:"sort_by" validate:"omitempty"` - FilterBy string `query:"filter_by" validate:"omitempty"` - AllowedAreaIDs []int64 `query:"-"` + Page int `query:"page" validate:"omitempty,min=1,gt=0"` + Limit int `query:"limit" validate:"omitempty,min=1,gt=0"` + AreaIDs []int64 `query:"-" validate:"omitempty,dive,gt=0"` + SupplierIDs []int64 `query:"-" validate:"omitempty,dive,gt=0"` + ProductIDs []int64 `query:"-" validate:"omitempty,dive,gt=0"` + ProductCategoryIDs []int64 `query:"-" validate:"omitempty,dive,gt=0"` + StartDate string `query:"start_date" validate:"omitempty"` + EndDate string `query:"end_date" validate:"omitempty"` + SortBy string `query:"sort_by" validate:"omitempty"` + FilterBy string `query:"filter_by" validate:"omitempty"` + AllowedAreaIDs []int64 `query:"-"` } type DebtSupplierQuery struct {