Merge branch 'feat/hpp-harian' into 'development'

[FEAT][BE]: add hpp harian

See merge request mbugroup/lti-api!195
This commit is contained in:
Hafizh A. Y.
2026-01-15 10:54:29 +00:00
3 changed files with 238 additions and 100 deletions
@@ -1267,10 +1267,37 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
if err != nil {
return nil, nil, err
}
costRows, supplierRows, err := s.HppPerKandangRepo.GetFeedOvkDocCostByPeriod(ctx.Context(), startOfDay, endOfDay, params.AreaIDs, params.LocationIDs, params.KandangIDs)
if err != nil {
return nil, nil, err
validPfkIDs := make([]uint, 0, len(repoRows))
pfkIndex := make(map[uint]int, len(repoRows))
for idx := range repoRows {
row := repoRows[idx]
pfkIndex[row.ProjectFlockKandangID] = idx
if row.RecordingCount > 0 {
validPfkIDs = append(validPfkIDs, row.ProjectFlockKandangID)
}
}
costRows := make([]repportRepo.HppPerKandangCostRow, 0)
supplierRows := make([]repportRepo.HppPerKandangSupplierRow, 0)
if len(validPfkIDs) > 0 {
costRows, supplierRows, err = s.HppPerKandangRepo.GetFeedOvkDocCostByPeriod(ctx.Context(), startOfDay, endOfDay, validPfkIDs)
if err != nil {
return nil, nil, err
}
eggMap, err := s.HppPerKandangRepo.GetEggProductionByProjectFlockKandangIDs(ctx.Context(), startOfDay, endOfDay, validPfkIDs)
if err != nil {
return nil, nil, err
}
for pfkID, egg := range eggMap {
if rowIdx, ok := pfkIndex[pfkID]; ok {
repoRows[rowIdx].EggProductionWeightKg = egg.EggProductionWeightKg
repoRows[rowIdx].EggProductionPieces = egg.EggProductionPieces
}
}
}
costMap := make(map[uint]HppCostAggregate, len(costRows))
for _, row := range costRows {
costMap[row.KandangID] = HppCostAggregate{
@@ -1323,9 +1350,15 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
Max float64
}
type weightRangeAggregate struct {
Summary *dto.HppPerKandangSummaryWeightRangeDTO
EggHppSum float64
EggHppCount int
Summary *dto.HppPerKandangSummaryWeightRangeDTO
RemainingBirds int64
RemainingWeightKg float64
AvgWeightSum float64
AvgWeightCount int64
EggHppSum float64
EggHppCount int
FeedSuppliers map[int64]dto.HppPerKandangSupplierDTO
DocSuppliers map[int64]dto.HppPerKandangSupplierDTO
}
dataRows := make([]dto.HppPerKandangRowDTO, 0, len(repoRows))
@@ -1342,8 +1375,14 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
var totalDocPriceCount int
var totalEggHppSum float64
var totalEggHppCount int
var totalAvgWeightSum float64
var totalAvgWeightCount int64
for _, row := range repoRows {
if !params.ShowUnrecorded && row.RecordingCount == 0 {
continue
}
birdsFloat := row.RemainingChickenBirds
if math.IsNaN(birdsFloat) || math.IsInf(birdsFloat, 0) {
birdsFloat = 0
@@ -1362,9 +1401,16 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
}
avgWeight := 0.0
if birdsFloat > 0 {
avgWeight = weightFloat / birdsFloat
if eggPiecesFloat > 0 {
avgWeight = eggWeightFloat / eggPiecesFloat
}
if params.WeightMin != nil && avgWeight < *params.WeightMin {
continue
}
if params.WeightMax != nil && avgWeight > *params.WeightMax {
continue
}
weightMin := math.Floor(avgWeight*10) / 10
if weightMin < 0 {
weightMin = 0
@@ -1411,9 +1457,7 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
WeightMin: weightMin,
WeightMax: weightMax,
},
RemainingChickenBirds: rowBirds,
RemainingChickenWeightKg: weightFloat,
AvgWeightKg: avgWeight,
AvgWeightKg: avgWeight,
// FeedCostRp: costEntry.FeedCost,
// OvkCostRp: costEntry.OvkCost,
DocSuppliers: docSupplierMap[row.KandangID],
@@ -1421,10 +1465,10 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
EggProductionPieces: rowEggPieces,
EggProductionKg: eggWeightFloat,
AverageDocPriceRp: avgDocPrice,
HppRp: hppRp,
EggHppRpPerKg: eggHpp,
RemainingValueRp: rowRemainingValue,
EggValueRp: rowEggValue,
// HppRp: hppRp,
EggHppRpPerKg: eggHpp,
RemainingValueRp: rowRemainingValue,
EggValueRp: rowEggValue,
})
totalBirds += rowBirds
@@ -1433,6 +1477,8 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
totalEggKg += eggWeightFloat
totalRemainingValueRp += rowRemainingValue
totalEggValueRp += rowEggValue
totalAvgWeightSum += avgWeight
totalAvgWeightCount++
if weightFloat > 0 {
totalHppSum += hppRp
totalHppCount++
@@ -1456,13 +1502,27 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
},
Label: fmt.Sprintf("%.2f - %.2f", weightMin, weightMax),
},
FeedSuppliers: make(map[int64]dto.HppPerKandangSupplierDTO),
DocSuppliers: make(map[int64]dto.HppPerKandangSupplierDTO),
}
perRangeMap[rangeKey] = rangeAgg
}
rangeSummary := rangeAgg.Summary
rangeSummary.RemainingChickenBirds += rowBirds
rangeSummary.RemainingChickenWeightKg += row.RemainingChickenWeight
rangeAgg.RemainingBirds += rowBirds
rangeAgg.RemainingWeightKg += row.RemainingChickenWeight
rangeAgg.AvgWeightSum += avgWeight
rangeAgg.AvgWeightCount++
for _, supplier := range feedSupplierMap[row.KandangID] {
if _, ok := rangeAgg.FeedSuppliers[supplier.ID]; !ok {
rangeAgg.FeedSuppliers[supplier.ID] = supplier
}
}
for _, supplier := range docSupplierMap[row.KandangID] {
if _, ok := rangeAgg.DocSuppliers[supplier.ID]; !ok {
rangeAgg.DocSuppliers[supplier.ID] = supplier
}
}
rangeSummary.EggProductionPieces += rowEggPieces
rangeSummary.EggProductionKg += eggWeightFloat
rangeSummary.RemainingValueRp += rowRemainingValue
@@ -1489,31 +1549,37 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
agg := perRangeMap[key]
entry := agg.Summary
entry.ID = idx + 1
if entry.RemainingChickenBirds > 0 {
entry.AvgWeightKg = entry.RemainingChickenWeightKg / float64(entry.RemainingChickenBirds)
if agg.AvgWeightCount > 0 {
entry.AvgWeightKg = agg.AvgWeightSum / float64(agg.AvgWeightCount)
}
if agg.EggHppCount > 0 {
entry.EggHppRpPerKg = agg.EggHppSum / float64(agg.EggHppCount)
}
entry.FeedSuppliers = make([]dto.HppPerKandangSupplierDTO, 0, len(agg.FeedSuppliers))
for _, supplier := range agg.FeedSuppliers {
entry.FeedSuppliers = append(entry.FeedSuppliers, supplier)
}
entry.DocSuppliers = make([]dto.HppPerKandangSupplierDTO, 0, len(agg.DocSuppliers))
for _, supplier := range agg.DocSuppliers {
entry.DocSuppliers = append(entry.DocSuppliers, supplier)
}
perRangeSummary = append(perRangeSummary, *entry)
}
totalSummary := dto.HppPerKandangSummaryTotalDTO{
TotalRemainingChickenBirds: totalBirds,
TotalRemainingChickenWeightKg: totalWeight,
TotalEggProductionPieces: totalEggPieces,
TotalEggProductionKg: totalEggKg,
TotalRemainingValueRp: totalRemainingValueRp,
TotalEggValueRp: totalEggValueRp,
TotalEggProductionPieces: totalEggPieces,
TotalEggProductionKg: totalEggKg,
TotalEggValueRp: totalEggValueRp,
}
if totalBirds > 0 {
totalSummary.AverageWeightKg = totalWeight / float64(totalBirds)
}
if totalAvgWeightCount > 0 {
totalSummary.AverageWeightKg = totalAvgWeightSum / float64(totalAvgWeightCount)
}
if totalEggHppCount > 0 {
totalSummary.AverageEggHppRpPerKg = totalEggHppSum / float64(totalEggHppCount)
}
if totalHppCount > 0 {
totalSummary.TotalHppRp = totalHppSum / float64(totalHppCount)
}
if totalDocPriceCount > 0 {
totalSummary.TotalAverageDocPriceRp = totalDocPriceSum / float64(totalDocPriceCount)