From 7a8f813e1f7d34098a9c3138f4b80bff59c388d3 Mon Sep 17 00:00:00 2001 From: giovanni Date: Mon, 26 Jan 2026 11:44:22 +0700 Subject: [PATCH] adjust calculate hpp filter delivery date marketing delivery --- .../repository/common.hpp.repository.go | 20 +++--- internal/common/service/common.hpp.service.go | 72 ++++++++++--------- .../repports/services/repport.service.go | 9 ++- 3 files changed, 56 insertions(+), 45 deletions(-) diff --git a/internal/common/repository/common.hpp.repository.go b/internal/common/repository/common.hpp.repository.go index c005e24e..da4b1908 100644 --- a/internal/common/repository/common.hpp.repository.go +++ b/internal/common/repository/common.hpp.repository.go @@ -20,7 +20,7 @@ type HppCostRepository interface { GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (float64, error) GetPulletCost(ctx context.Context, projectFlockKandangId uint) (float64, error) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) - GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) + GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, startDate *time.Time, endDate *time.Time) (float64, float64, error) GetProjectFlockIDByProjectFlockKandangID(ctx context.Context, projectFlockKandangId uint) (uint, error) GetTransferSourceSummary(ctx context.Context, projectFlockKandangId uint) (uint, float64, error) } @@ -196,10 +196,10 @@ func (r *HppRepositoryImpl) GetPulletCost(ctx context.Context, projectFlockKanda } func (r *HppRepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) { - if date == nil { - now := time.Now() - date = &now - } + // if date == nil { + // now := time.Now() + // date = &now + // } var totals struct { TotalPieces float64 @@ -222,12 +222,13 @@ func (r *HppRepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandang func (r *HppRepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds( ctx context.Context, projectFlockKandangIDs []uint, - date *time.Time, + startDate *time.Time, + endDate *time.Time, ) (float64, float64, error) { - if date == nil { + if endDate == nil { now := time.Now() - date = &now + endDate = &now } type subResult struct { @@ -251,7 +252,8 @@ func (r *HppRepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangI ). Joins("JOIN marketing_delivery_products mdp ON mdp.id = sa.usable_id"). Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs). - Where("r.record_datetime <= ?", *date) + Where("r.record_datetime <= ?", *endDate). + Where("mdp.delivery_date = ?", *startDate) var totals struct { TotalPieces float64 diff --git a/internal/common/service/common.hpp.service.go b/internal/common/service/common.hpp.service.go index 44f2dd5f..b1f1a1b1 100644 --- a/internal/common/service/common.hpp.service.go +++ b/internal/common/service/common.hpp.service.go @@ -11,10 +11,10 @@ import ( 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) + GetTotalProductionCost(projectFlockKandangId uint, endDate *time.Time, depresiasiTransfer float64) (float64, error) + GetBudgetKandangLaying(projectFlockKandangId uint, endDate *time.Time) (float64, error) GetDepresiasiTransfer(projectFlockKandangId uint, date *time.Time) (float64, error) - GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error) + GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, startDate *time.Time, endDate *time.Time) (*HppCostResponse, error) } type HppCostResponse struct { @@ -44,17 +44,25 @@ func (s *hppService) CalculateHppCost(projectFlockKandangId uint, date *time.Tim date = &now } - depresiasiTransfer, err := s.GetDepresiasiTransfer(projectFlockKandangId, date) + location, err := time.LoadLocation("Asia/Jakarta") if err != nil { return nil, err } - totalProductionCost, err := s.GetTotalProductionCost(projectFlockKandangId, date, depresiasiTransfer) + startOfDay := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, location) + endOfDay := startOfDay.Add(24 * time.Hour) + + depresiasiTransfer, err := s.GetDepresiasiTransfer(projectFlockKandangId, &endOfDay) if err != nil { return nil, err } - return s.GetHppEstimationDanRealisasi(totalProductionCost, projectFlockKandangId, date) + totalProductionCost, err := s.GetTotalProductionCost(projectFlockKandangId, &endOfDay, depresiasiTransfer) + if err != nil { + return nil, err + } + + return s.GetHppEstimationDanRealisasi(totalProductionCost, projectFlockKandangId, &startOfDay, &endOfDay) } @@ -101,23 +109,23 @@ func (s *hppService) GetTotalDepresiasiFlockGrowing(sourceProjectFlockID uint, d 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 - } +func (s *hppService) GetTotalProductionCost(projectFlockKandangId uint, endDate *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) + costFeed, err := s.hppRepo.GetFeedUsageCost(context.Background(), []uint{projectFlockKandangId}, endDate) if err != nil { return 0, err } - costOvk, err := s.hppRepo.GetOvkUsageCost(context.Background(), []uint{projectFlockKandangId}, date) + costOvk, err := s.hppRepo.GetOvkUsageCost(context.Background(), []uint{projectFlockKandangId}, endDate) if err != nil { return 0, err } @@ -127,7 +135,7 @@ func (s *hppService) GetTotalProductionCost(projectFlockKandangId uint, date *ti return 0, err } - costBudget, err := s.GetBudgetKandangLaying(projectFlockKandangId, date) + costBudget, err := s.GetBudgetKandangLaying(projectFlockKandangId, endDate) if err != nil { return 0, err } @@ -135,11 +143,11 @@ func (s *hppService) GetTotalProductionCost(projectFlockKandangId uint, date *ti 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 - } +func (s *hppService) GetBudgetKandangLaying(projectFlockKandangId uint, endDate *time.Time) (float64, error) { + // if date == nil { + // now := time.Now() + // date = &now + // } if s.hppRepo == nil { return 0, nil @@ -155,12 +163,12 @@ func (s *hppService) GetBudgetKandangLaying(projectFlockKandangId uint, date *ti return 0, err } - eggProduksiPiecesFlock, _, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), projectFlockKandangIds, date) + eggProduksiPiecesFlock, _, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), projectFlockKandangIds, endDate) if err != nil { return 0, err } - eggProduksiPiecesKandang, _, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, date) + eggProduksiPiecesKandang, _, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, endDate) if err != nil { return 0, err } @@ -177,11 +185,11 @@ func (s *hppService) GetBudgetKandangLaying(projectFlockKandangId uint, date *ti return (totalBudgetCost * eggProduksiPiecesKandang) / eggProduksiPiecesFlock, nil } -func (s *hppService) GetDepresiasiTransfer(projectFlockKandangId uint, date *time.Time) (float64, error) { - if date == nil { - now := time.Now() - date = &now - } +func (s *hppService) GetDepresiasiTransfer(projectFlockKandangId uint, endDate *time.Time) (float64, error) { + // if endDate == nil { + // now := time.Now() + // endDate = &now + // } if s.hppRepo == nil { return 0, nil @@ -205,7 +213,7 @@ func (s *hppService) GetDepresiasiTransfer(projectFlockKandangId uint, date *tim return 0, nil } - totalDepresiasiFlockGrowing, err := s.GetTotalDepresiasiFlockGrowing(sourceProjectFlockID, date) + totalDepresiasiFlockGrowing, err := s.GetTotalDepresiasiFlockGrowing(sourceProjectFlockID, endDate) if err != nil { return 0, err } @@ -213,22 +221,18 @@ 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 - } +func (s *hppService) 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}, date) + 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}, date) + realPieces, realWeightKg, err := s.hppRepo.GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, startDate, endDate) if err != nil { return nil, err } diff --git a/internal/modules/repports/services/repport.service.go b/internal/modules/repports/services/repport.service.go index 9c5b8faf..fb5d72ba 100644 --- a/internal/modules/repports/services/repport.service.go +++ b/internal/modules/repports/services/repport.service.go @@ -1617,7 +1617,7 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes var avgWeight float64 eggHpp := 0.0 if s.HppSvc != nil { - hppCost, err := s.HppSvc.CalculateHppCost(row.ProjectFlockKandangID, &endOfDay) + hppCost, err := s.HppSvc.CalculateHppCost(row.ProjectFlockKandangID, &periodDate) if err != nil { return nil, nil, err } @@ -1626,7 +1626,9 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes eggHpp = hppCost.Estimation.HargaKg eggTotalPiecesFloat = hppCost.Estimation.Butir eggWeightFloat = hppCost.Estimation.Kg - avgWeight = eggWeightFloat / eggTotalPiecesFloat + if eggTotalPiecesFloat > 0 { + avgWeight = eggWeightFloat / eggTotalPiecesFloat + } eggRemainingWeightFloatRemaining = avgWeight * eggPiecesFloatRemaining } } @@ -1642,6 +1644,9 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes if math.IsNaN(eggWeightFloat) || math.IsInf(eggWeightFloat, 0) { eggWeightFloat = 0 } + if math.IsNaN(avgWeight) || math.IsInf(avgWeight, 0) { + avgWeight = 0 + } if params.WeightMin != nil && avgWeight < *params.WeightMin { continue