add api daily checklist report

This commit is contained in:
MacBook Air M1
2026-01-07 17:39:17 +07:00
parent e545047165
commit c3f8ae5887
6 changed files with 704 additions and 47 deletions
@@ -7,6 +7,7 @@ import (
"gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/validations"
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
@@ -28,6 +29,18 @@ func (u *DailyChecklistController) GetAll(c *fiber.Ctx) error {
Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""),
}
query.DateFrom = c.Query("date_from", "")
query.DateTo = c.Query("date_to", "")
query.Status = c.Query("status", "")
if kandangParam := c.Query("kandang_id", ""); kandangParam != "" {
kandangID, err := strconv.ParseUint(kandangParam, 10, 64)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid kandang_id")
}
value := uint(kandangID)
query.KandangID = &value
}
if query.Page < 1 || query.Limit < 1 {
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
@@ -38,6 +51,40 @@ func (u *DailyChecklistController) GetAll(c *fiber.Ctx) error {
return err
}
responseData := make([]dto.DailyChecklistListDTO, len(result))
for i, item := range result {
var name string
if item.Name != nil {
name = *item.Name
}
var status string
if item.Status != nil {
status = *item.Status
}
var kandang *kandangDTO.KandangRelationDTO
if item.Kandang.Id != 0 {
mapped := kandangDTO.ToKandangRelationDTO(item.Kandang)
kandang = &mapped
}
responseData[i] = dto.DailyChecklistListDTO{
Id: item.ID,
Name: name,
Status: status,
Category: item.Category,
Date: item.Date,
Kandang: kandang,
CreatedUser: nil,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
TotalPhase: item.TotalPhase,
TotalActivity: item.TotalActivity,
Progress: item.Progress,
}
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.DailyChecklistListDTO]{
Code: fiber.StatusOK,
@@ -49,7 +96,7 @@ func (u *DailyChecklistController) GetAll(c *fiber.Ctx) error {
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToDailyChecklistListDTOs(result),
Data: responseData,
})
}
@@ -128,6 +175,121 @@ func (u *DailyChecklistController) GetSummary(c *fiber.Ctx) error {
})
}
func (u *DailyChecklistController) GetReport(c *fiber.Ctx) error {
query := &validation.ReportQuery{
Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10),
Month: c.QueryInt("bulan", 0),
Year: c.QueryInt("tahun", 0),
}
parseUintParam := func(param string) (*uint, error) {
if param == "" {
return nil, nil
}
value, err := strconv.ParseUint(param, 10, 64)
if err != nil {
return nil, err
}
u := uint(value)
return &u, nil
}
if val, err := parseUintParam(c.Query("area_id", "")); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid area_id")
} else {
query.AreaID = val
}
if val, err := parseUintParam(c.Query("location_id", "")); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid location_id")
} else {
query.LocationID = val
}
if val, err := parseUintParam(c.Query("kandang_id", "")); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid kandang_id")
} else {
query.KandangID = val
}
if val, err := parseUintParam(c.Query("employee_id", "")); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid employee_id")
} else {
query.EmployeeID = val
}
if val, err := parseUintParam(c.Query("phase_id", "")); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid phase_id")
} else {
query.PhaseID = val
}
if query.Month == 0 || query.Year == 0 {
return fiber.NewError(fiber.StatusBadRequest, "bulan and tahun are required")
}
result, totalResults, err := u.DailyChecklistService.GetReport(c, query)
withoutActivities := func(src map[string]int) map[string]int {
if src == nil {
return map[string]int{}
}
return src
}
if err != nil {
return err
}
responseData := make([]dto.DailyChecklistReportDTO, len(result))
for i, item := range result {
responseData[i] = dto.DailyChecklistReportDTO{
Area: dto.DailyChecklistReportEntityDTO{
Id: item.AreaID,
Name: item.AreaName,
},
Farm: dto.DailyChecklistReportEntityDTO{
Id: item.LocationID,
Name: item.LocationName,
},
Kandang: dto.DailyChecklistReportEntityDTO{
Id: item.KandangID,
Name: item.KandangName,
},
ABK: dto.DailyChecklistReportEntityDTO{
Id: item.EmployeeID,
Name: item.EmployeeName,
},
Phase: item.PhaseName,
DailyActivities: withoutActivities(item.DailyActivities),
Summary: dto.DailyChecklistReportSummaryDTO{
TotalChecklist: item.Summary.TotalChecklist,
JumlahHariEfektif: item.Summary.JumlahHariEfektif,
AbkPercentage: item.Summary.AbkPercentage,
KandangPercentage: item.Summary.KandangPercentage,
Kategori: dto.DailyChecklistReportCategoryDTO{
Kurang: item.Summary.Category.Kurang,
Cukup: item.Summary.Category.Cukup,
Baik: item.Summary.Category.Baik,
},
},
}
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.DailyChecklistReportDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get daily checklist report successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: responseData,
})
}
func (u *DailyChecklistController) GetOne(c *fiber.Ctx) error {
param := c.Params("idDailyChecklist")
@@ -199,7 +361,7 @@ func (u *DailyChecklistController) UpdateOne(c *fiber.Ctx) error {
}
func (u *DailyChecklistController) DeleteOne(c *fiber.Ctx) error {
param := c.Params("id")
param := c.Params("idDailyChecklist")
id, err := strconv.Atoi(param)
if err != nil {