package service import ( "context" "time" commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository" ) type HppV2Service interface { CalculateHppCost(projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error) GetCostPakan(projectFlockKandangId uint, endDate *time.Time) (float64, error) GetFeedGrowing(projectFlockKandangId uint, endDate *time.Time) (float64, error) GetFeedLaying(projectFlockKandangId uint, endDate *time.Time) (float64, error) GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, startDate *time.Time, endDate *time.Time) (*HppCostResponse, error) } type hppV2Service struct { hppRepo commonRepo.HppV2CostRepository } func NewHppV2Service(hppRepo commonRepo.HppV2CostRepository) HppV2Service { return &hppV2Service{hppRepo: hppRepo} } func (s *hppV2Service) CalculateHppCost(projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error) { if date == nil { now := time.Now() date = &now } location, err := time.LoadLocation("Asia/Jakarta") if err != nil { return nil, err } startOfDay := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, location) endOfDay := startOfDay.Add(24 * time.Hour) pakan, err := s.GetCostPakan(projectFlockKandangId, &endOfDay) if err != nil { return nil, err } result, err := s.GetHppEstimationDanRealisasi(pakan, projectFlockKandangId, &startOfDay, &endOfDay) if err != nil { return nil, err } return result, nil } func (s *hppV2Service) GetCostPakan(projectFlockKandangId uint, endDate *time.Time) (float64, error) { feedGrowing, err := s.GetFeedGrowing(projectFlockKandangId, endDate) if err != nil { return 0, err } feedLaying, err := s.GetFeedLaying(projectFlockKandangId, endDate) if err != nil { return 0, err } pakan := feedGrowing + feedLaying return pakan, nil } func (s *hppV2Service) GetFeedGrowing(projectFlockKandangId uint, endDate *time.Time) (float64, error) { if s.hppRepo == nil { return 0, nil } sourceProjectFlockID, transferTotalQty, err := s.hppRepo.GetTransferSourceSummary(context.Background(), projectFlockKandangId) if err != nil { return 0, err } if sourceProjectFlockID == 0 || transferTotalQty <= 0 { return 0, nil } kandangIDsGrowing, err := s.hppRepo.GetProjectFlockKandangIDs(context.Background(), sourceProjectFlockID) if err != nil { return 0, err } if len(kandangIDsGrowing) == 0 { return 0, nil } feedUsageCostGrowing, err := s.hppRepo.GetFeedUsageCost(context.Background(), kandangIDsGrowing, endDate) if err != nil { return 0, err } totalPopulationFlockGrowing, err := s.hppRepo.GetTotalPopulation(context.Background(), kandangIDsGrowing) if err != nil { return 0, err } if totalPopulationFlockGrowing == 0 { return 0, nil } result := feedUsageCostGrowing * (transferTotalQty / totalPopulationFlockGrowing) return result, nil } func (s *hppV2Service) GetFeedLaying(projectFlockKandangId uint, endDate *time.Time) (float64, error) { if s.hppRepo == nil { return 0, nil } result, err := s.hppRepo.GetFeedUsageCost(context.Background(), []uint{projectFlockKandangId}, endDate) if err != nil { return 0, err } return result, nil } func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, startDate *time.Time, endDate *time.Time) (*HppCostResponse, error) { if s.hppRepo == nil { return &HppCostResponse{}, nil } estimPieces, estimWeightKg, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, endDate) if err != nil { return nil, err } realPieces, realWeightKg, err := s.hppRepo.GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, startDate, endDate) if err != nil { return nil, err } estimation := HppCostDetail{ Total: totalProductionCost, Kg: estimWeightKg, Butir: estimPieces, } if estimWeightKg > 0 { estimation.HargaKg = roundToTwoDecimals(totalProductionCost / estimWeightKg) } if estimPieces > 0 { estimation.HargaButir = roundToTwoDecimals(totalProductionCost / estimPieces) } real := HppCostDetail{ Total: totalProductionCost, Kg: realWeightKg, Butir: realPieces, } if realWeightKg > 0 { real.HargaKg = roundToTwoDecimals(totalProductionCost / realWeightKg) } if realPieces > 0 { real.HargaButir = roundToTwoDecimals(totalProductionCost / realPieces) } result := &HppCostResponse{ Estimation: estimation, Real: real, } return result, nil }