package service import ( "context" "time" commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository" ) type HppService interface { CalculateHppCost(projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error) GetTotalDepresiasiFlockGrowing(sourceProjectFlockID uint, date *time.Time) (float64, error) 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) } type HppCostResponse struct { Estimation HppCostDetail `json:"estimation"` Real HppCostDetail `json:"real"` } type HppCostDetail struct { HargaKg float64 `json:"harga_kg"` HargaButir float64 `json:"harga_butir"` Total float64 `json:"total"` Kg float64 `json:"kg"` Butir float64 `json:"butir"` } type hppService struct { hppRepo commonRepo.HppCostRepository } func NewHppService(hppRepo commonRepo.HppCostRepository) HppService { return &hppService{hppRepo: hppRepo} } func (s *hppService) CalculateHppCost(projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error) { if date == nil { now := time.Now() date = &now } depresiasiTransfer, err := s.GetDepresiasiTransfer(projectFlockKandangId, date) if err != nil { return nil, err } totalProductionCost, err := s.GetTotalProductionCost(projectFlockKandangId, date, depresiasiTransfer) if err != nil { return nil, err } _ = totalProductionCost return &HppCostResponse{ Estimation: HppCostDetail{}, Real: HppCostDetail{}, }, nil } func (s *hppService) GetTotalDepresiasiFlockGrowing(sourceProjectFlockID uint, date *time.Time) (float64, error) { if date == nil { now := time.Now() date = &now } if s.hppRepo == nil { return 0, nil } kandangIDs, err := s.hppRepo.GetProjectFlockKandangIDs(context.Background(), sourceProjectFlockID) if err != nil { return 0, err } docCost, err := s.hppRepo.GetDocCost(context.Background(), kandangIDs) if err != nil { return 0, err } budgetCost, err := s.hppRepo.GetBudgetCostByProjectFlockId(context.Background(), sourceProjectFlockID) if err != nil { return 0, err } expedisionCost, err := s.hppRepo.GetExpedisionCost(context.Background(), kandangIDs) if err != nil { return 0, err } feedCost, err := s.hppRepo.GetFeedUsageCost(context.Background(), kandangIDs, date) if err != nil { return 0, err } ovkCost, err := s.hppRepo.GetOvkUsageCost(context.Background(), kandangIDs, date) if err != nil { return 0, err } return docCost + budgetCost + expedisionCost + feedCost + ovkCost, nil } func (s *hppService) GetTotalProductionCost(projectFlockKandangId uint, date *time.Time, depresiasiTransfer float64) (float64, error) { if date == nil { now := time.Now() date = &now } costPullet, err := s.hppRepo.GetPulletCost(context.Background(), projectFlockKandangId) if err != nil { return 0, err } costFeed, err := s.hppRepo.GetFeedUsageCost(context.Background(), []uint{projectFlockKandangId}, date) if err != nil { return 0, err } costOvk, err := s.hppRepo.GetOvkUsageCost(context.Background(), []uint{projectFlockKandangId}, date) if err != nil { return 0, err } costExpedision, err := s.hppRepo.GetExpedisionCost(context.Background(), []uint{projectFlockKandangId}) if err != nil { return 0, err } costBudget, err := s.GetBudgetKandangLaying(projectFlockKandangId, date) if err != nil { return 0, err } return depresiasiTransfer + costPullet + costFeed + costOvk + costExpedision + costBudget, nil } func (s *hppService) GetBudgetKandangLaying(projectFlockKandangId uint, date *time.Time) (float64, error) { if date == nil { now := time.Now() date = &now } if s.hppRepo == nil { return 0, nil } projectFlockId, err := s.hppRepo.GetProjectFlockIDByProjectFlockKandangID(context.Background(), projectFlockKandangId) if err != nil { return 0, err } projectFlockKandangIds, err := s.hppRepo.GetProjectFlockKandangIDs(context.Background(), projectFlockId) if err != nil { return 0, err } eggProduksiPiecesFlock, _, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), projectFlockKandangIds, date) if err != nil { return 0, err } eggProduksiPiecesKandang, _, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, date) if err != nil { return 0, err } totalBudgetCost, err := s.hppRepo.GetBudgetCostByProjectFlockId(context.Background(), projectFlockId) if err != nil { return 0, err } if eggProduksiPiecesFlock == 0 { return 0, nil } return (totalBudgetCost * eggProduksiPiecesKandang) / eggProduksiPiecesFlock, nil } func (s *hppService) GetDepresiasiTransfer(projectFlockKandangId uint, date *time.Time) (float64, error) { if date == nil { now := time.Now() date = &now } if s.hppRepo == nil { return 0, nil } sourceProjectFlockID, transferTotalQty, err := s.hppRepo.GetTransferSourceSummary(context.Background(), projectFlockKandangId) if err != nil { return 0, err } kandangIDsGrowing, err := s.hppRepo.GetProjectFlockKandangIDs(context.Background(), sourceProjectFlockID) 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 } totalDepresiasiFlockGrowing, err := s.GetTotalDepresiasiFlockGrowing(sourceProjectFlockID, date) if err != nil { return 0, err } return (totalDepresiasiFlockGrowing * transferTotalQty) / totalPopulationFlockGrowing, nil }