diff --git a/internal/common/service/common.hpp.service.go b/internal/common/service/common.hpp.service.go index 8210fc17..1b94e791 100644 --- a/internal/common/service/common.hpp.service.go +++ b/internal/common/service/common.hpp.service.go @@ -13,6 +13,7 @@ type HppService interface { GetTotalProductionCost(projectFlockKandangId uint, date *time.Time, totalDepresiasiGrowing float64) (float64, error) GetBudgetKandangLaying(projectFlockKandangId uint, date *time.Time) (float64, error) GetDepresiasiTransfer(projectFlockKandangId uint, date *time.Time) (float64, error) + GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error) } type HppCostResponse struct { @@ -52,12 +53,8 @@ func (s *hppService) CalculateHppCost(projectFlockKandangId uint, date *time.Tim return nil, err } - _ = totalProductionCost + return s.GetHppEstimationDanRealisasi(totalProductionCost, projectFlockKandangId, date) - return &HppCostResponse{ - Estimation: HppCostDetail{}, - Real: HppCostDetail{}, - }, nil } func (s *hppService) GetTotalDepresiasiFlockGrowing(sourceProjectFlockID uint, date *time.Time) (float64, error) { @@ -214,3 +211,53 @@ func (s *hppService) GetDepresiasiTransfer(projectFlockKandangId uint, date *tim return (totalDepresiasiFlockGrowing * transferTotalQty) / totalPopulationFlockGrowing, nil } + +func (s *hppService) GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error) { + if date == nil { + now := time.Now() + date = &now + } + + if s.hppRepo == nil { + return &HppCostResponse{}, nil + } + + estimPieces, estimWeightKg, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, date) + if err != nil { + return nil, err + } + + realPieces, realWeightKg, err := s.hppRepo.GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, date) + if err != nil { + return nil, err + } + + estimation := HppCostDetail{ + Total: totalProductionCost, + Kg: estimWeightKg, + Butir: estimPieces, + } + if estimWeightKg > 0 { + estimation.HargaKg = totalProductionCost / estimWeightKg + } + if estimPieces > 0 { + estimation.HargaButir = totalProductionCost / estimPieces + } + + real := HppCostDetail{ + Total: totalProductionCost, + Kg: realWeightKg, + Butir: realPieces, + } + if realWeightKg > 0 { + real.HargaKg = totalProductionCost / realWeightKg + } + if realPieces > 0 { + real.HargaButir = totalProductionCost / realPieces + } + + return &HppCostResponse{ + Estimation: estimation, + Real: real, + }, nil +}