From 507d6c42932c69223a9634d5dc41eea5d7196570 Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Fri, 23 Jan 2026 22:02:19 +0700 Subject: [PATCH] FEAT[BE]: implement HPP on penjualan harian --- .../repports/services/repport.service.go | 94 ++++--------------- 1 file changed, 17 insertions(+), 77 deletions(-) diff --git a/internal/modules/repports/services/repport.service.go b/internal/modules/repports/services/repport.service.go index 9c976138..2b6ab745 100644 --- a/internal/modules/repports/services/repport.service.go +++ b/internal/modules/repports/services/repport.service.go @@ -218,8 +218,23 @@ func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.Marketing projectFlockIDMap[projectFlockID] = true category := projectFlockKandang.ProjectFlock.Category - hppPerKg := s.calculateHppPricePerKg(c.Context(), projectFlockID, category) - hppMap[projectFlockID] = hppPerKg + if utils.ProjectFlockCategory(category) == utils.ProjectFlockCategoryLaying { + if s.HppSvc != nil { + hppCost, err := s.HppSvc.CalculateHppCost(projectFlockID, nil) + if err != nil { + hppMap[projectFlockID] = 0.0 + } else if hppCost != nil { + hppMap[projectFlockID] = hppCost.Real.HargaKg + } else { + hppMap[projectFlockID] = 0.0 + } + } else { + hppMap[projectFlockID] = 0.0 + } + } else { + + hppMap[projectFlockID] = 0.0 + } } } } @@ -228,81 +243,6 @@ func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.Marketing return items, total, nil } -func (s *repportService) calculateHppPricePerKg(ctx context.Context, projectFlockID uint, category string) float64 { - totalCost := s.getTotalProjectCost(ctx, projectFlockID) - if totalCost == 0 { - return 0 - } - - chickinQty, err := s.ChickinRepo.GetTotalChickinQtyByProjectFlockID(ctx, projectFlockID) - if err != nil { - s.Log.Warnf("HPP calculation: Failed to get chickin qty for project flock ID %d: %v", projectFlockID, err) - } - - depletion, err := s.RecordingRepo.GetTotalDepletionByProjectFlockID(ctx, projectFlockID) - if err != nil { - s.Log.Warnf("HPP calculation: Failed to get depletion for project flock ID %d: %v", projectFlockID, err) - } - - avgWeight, err := s.RecordingRepo.GetLatestAvgWeightByProjectFlockID(ctx, projectFlockID) - if err != nil { - s.Log.Warnf("HPP calculation: Failed to get avg weight for project flock ID %d: %v", projectFlockID, err) - } - - var totalWeight float64 - if utils.ProjectFlockCategory(category) == utils.ProjectFlockCategoryGrowing { - totalWeight = (chickinQty - depletion) * avgWeight - } else { - eggWeight, err := s.RecordingRepo.GetTotalEggProductionWeightByProjectFlockID(ctx, projectFlockID) - if err != nil { - s.Log.Warnf("HPP calculation: Failed to get egg weight for project flock ID %d: %v", projectFlockID, err) - } - totalWeight = (chickinQty-depletion)*avgWeight + eggWeight - } - - if totalWeight == 0 { - return 0 - } - - hppPricePerKg := totalCost / totalWeight - return hppPricePerKg -} - -func (s *repportService) getTotalProjectCost(ctx context.Context, projectFlockID uint) float64 { - if projectFlockID == 0 { - return 0 - } - - purchases, err := s.PurchaseRepo.GetItemsByProjectFlockID(ctx, projectFlockID) - if err != nil { - s.Log.Errorf("getTotalProjectCost: GetItemsByProjectFlockID error for project flock ID %d: %v", projectFlockID, err) - return 0 - } - - cost := float64(0) - purchaseCost := float64(0) - for _, p := range purchases { - purchaseCost += p.TotalPrice - } - cost += purchaseCost - - realizations, err := s.ExpenseRealizationRepo.GetByProjectFlockID(ctx, projectFlockID) - if err != nil { - s.Log.Warnf("getTotalProjectCost: GetByProjectFlockID error for project flock ID %d: %v", projectFlockID, err) - } - - bopCost := float64(0) - for _, r := range realizations { - if r.ExpenseNonstock != nil && r.ExpenseNonstock.Expense != nil && - r.ExpenseNonstock.Expense.Category == string(utils.ExpenseCategoryBOP) { - bopCost += r.Price * r.Qty - } - } - cost += bopCost - - return cost -} - func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.ProductionResultQuery) ([]dto.ProductionResultDTO, int64, error) { if err := s.Validate.Struct(params); err != nil { return nil, 0, err