Merge branch 'fix/hpp-calculate' into 'development'

[FIX][BE]: fix calculate sisa berat telur

See merge request mbugroup/lti-api!251
This commit is contained in:
Hafizh A. Y.
2026-01-24 09:34:24 +00:00
2 changed files with 20 additions and 23 deletions
@@ -23,6 +23,7 @@ type HppPerKandangRow struct {
// RemainingChickenBirds float64 // RemainingChickenBirds float64
// RemainingChickenWeight float64 // RemainingChickenWeight float64
EggProductionWeightKgRemaining float64 EggProductionWeightKgRemaining float64
// AverageWeightEggPerPiece float64
// EggProductionPiecesRemaining float64 // EggProductionPiecesRemaining float64
// EggProductionTotalWeightKg float64 // EggProductionTotalWeightKg float64
// EggProductionTotalPieces float64 // EggProductionTotalPieces float64
@@ -229,8 +230,8 @@ func (r *hppPerKandangRepository) GetWeightRemainingByProjectFlockKandangIDs(ctx
) )
type eggRow struct { type eggRow struct {
ProjectFlockKandangID uint ProjectFlockKandangID uint
EggProductionWeightKgRemaining float64 AverageWeightEggPerPiece float64
// EggProductionPiecesRemaining float64 // EggProductionPiecesRemaining float64
// EggProductionTotalWeightKg float64 // EggProductionTotalWeightKg float64
// EggProductionTotalPieces float64 // EggProductionTotalPieces float64
@@ -241,7 +242,7 @@ func (r *hppPerKandangRepository) GetWeightRemainingByProjectFlockKandangIDs(ctx
Table("recordings AS r"). Table("recordings AS r").
Select(` Select(`
r.project_flock_kandangs_id AS project_flock_kandang_id, r.project_flock_kandangs_id AS project_flock_kandang_id,
COALESCE((SUM(re.weight) / NULLIF(SUM(re.total_qty), 0)) * SUM(re.total_qty - re.total_used), 0) AS egg_production_weight_kg_remaining`). COALESCE(SUM(re.weight) / NULLIF(SUM(re.total_qty), 0), 0) AS average_weight_egg_per_piece`).
Joins("LEFT JOIN (?) AS la ON la.approvable_id = r.id", latestApproval). Joins("LEFT JOIN (?) AS la ON la.approvable_id = r.id", latestApproval).
Joins("LEFT JOIN recording_eggs AS re ON re.recording_id = r.id"). Joins("LEFT JOIN recording_eggs AS re ON re.recording_id = r.id").
Where("r.project_flock_kandangs_id IN ?", projectFlockKandangIDs). Where("r.project_flock_kandangs_id IN ?", projectFlockKandangIDs).
@@ -257,8 +258,8 @@ func (r *hppPerKandangRepository) GetWeightRemainingByProjectFlockKandangIDs(ctx
result := make(map[uint]HppPerKandangRow, len(eggRows)) result := make(map[uint]HppPerKandangRow, len(eggRows))
for _, row := range eggRows { for _, row := range eggRows {
result[row.ProjectFlockKandangID] = HppPerKandangRow{ result[row.ProjectFlockKandangID] = HppPerKandangRow{
ProjectFlockKandangID: row.ProjectFlockKandangID, ProjectFlockKandangID: row.ProjectFlockKandangID,
EggProductionWeightKgRemaining: row.EggProductionWeightKgRemaining, // AverageWeightEggPerPiece: row.AverageWeightEggPerPiece,
// EggProductionPiecesRemaining: row.EggProductionPiecesRemaining, // EggProductionPiecesRemaining: row.EggProductionPiecesRemaining,
// EggProductionTotalWeightKg: row.EggProductionTotalWeightKg, // EggProductionTotalWeightKg: row.EggProductionTotalWeightKg,
// EggProductionTotalPieces: row.EggProductionTotalPieces, // EggProductionTotalPieces: row.EggProductionTotalPieces,
@@ -1516,18 +1516,16 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
return nil, nil, err return nil, nil, err
} }
eggMap, err := s.HppPerKandangRepo.GetWeightRemainingByProjectFlockKandangIDs(ctx.Context(), startOfDay, endOfDay, validPfkIDs) // eggMap, err := s.HppPerKandangRepo.GetWeightRemainingByProjectFlockKandangIDs(ctx.Context(), startOfDay, endOfDay, validPfkIDs)
if err != nil { // if err != nil {
return nil, nil, err // return nil, nil, err
} // }
for pfkID, egg := range eggMap { // for pfkID, egg := range eggMap {
if rowIdx, ok := pfkIndex[pfkID]; ok { // if rowIdx, ok := pfkIndex[pfkID]; ok {
repoRows[rowIdx].EggProductionWeightKgRemaining = egg.EggProductionWeightKgRemaining // repoRows[rowIdx].EggProductionWeightKgRemaining = egg.EggProductionWeightKgRemaining
// repoRows[rowIdx].EggProductionPiecesRemaining = egg.EggProductionPiecesRemaining // repoRows[rowIdx].AverageWeightEggPerPiece = egg.AverageWeightEggPerPiece
// repoRows[rowIdx].EggProductionTotalWeightKg = egg.EggProductionTotalWeightKg // }
// repoRows[rowIdx].EggProductionTotalPieces = egg.EggProductionTotalPieces // }
}
}
} }
costMap := make(map[uint]HppCostAggregate, len(costRows)) costMap := make(map[uint]HppCostAggregate, len(costRows))
@@ -1613,9 +1611,10 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
} }
var eggPiecesFloatRemaining float64 var eggPiecesFloatRemaining float64
eggRemainingWeightFloatRemaining := row.EggProductionWeightKgRemaining var eggRemainingWeightFloatRemaining float64
var eggTotalPiecesFloat float64 var eggTotalPiecesFloat float64
var eggWeightFloat float64 var eggWeightFloat float64
var avgWeight float64
eggHpp := 0.0 eggHpp := 0.0
if s.HppSvc != nil { if s.HppSvc != nil {
hppCost, err := s.HppSvc.CalculateHppCost(row.ProjectFlockKandangID, &endOfDay) hppCost, err := s.HppSvc.CalculateHppCost(row.ProjectFlockKandangID, &endOfDay)
@@ -1623,11 +1622,12 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
return nil, nil, err return nil, nil, err
} }
if hppCost != nil { if hppCost != nil {
// eggRemainingWeightFloatRemaining = hppCost.Estimation.Kg - hppCost.Real.Kg
eggPiecesFloatRemaining = hppCost.Estimation.Butir - hppCost.Real.Butir eggPiecesFloatRemaining = hppCost.Estimation.Butir - hppCost.Real.Butir
eggHpp = hppCost.Estimation.HargaKg eggHpp = hppCost.Estimation.HargaKg
eggTotalPiecesFloat = hppCost.Estimation.Butir eggTotalPiecesFloat = hppCost.Estimation.Butir
eggWeightFloat = hppCost.Estimation.Kg eggWeightFloat = hppCost.Estimation.Kg
avgWeight = eggWeightFloat / eggTotalPiecesFloat
eggRemainingWeightFloatRemaining = avgWeight * eggPiecesFloatRemaining
} }
} }
if math.IsNaN(eggPiecesFloatRemaining) || math.IsInf(eggPiecesFloatRemaining, 0) { if math.IsNaN(eggPiecesFloatRemaining) || math.IsInf(eggPiecesFloatRemaining, 0) {
@@ -1643,10 +1643,6 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
eggWeightFloat = 0 eggWeightFloat = 0
} }
avgWeight := 0.0
if eggTotalPiecesFloat > 0 {
avgWeight = eggWeightFloat / eggTotalPiecesFloat
}
if params.WeightMin != nil && avgWeight < *params.WeightMin { if params.WeightMin != nil && avgWeight < *params.WeightMin {
continue continue
} }