adjust calculate hpp filter delivery date marketing delivery

This commit is contained in:
giovanni
2026-01-26 11:44:22 +07:00
parent fdd8e3ec31
commit 7a8f813e1f
3 changed files with 56 additions and 45 deletions
@@ -20,7 +20,7 @@ type HppCostRepository interface {
GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (float64, error) GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (float64, error)
GetPulletCost(ctx context.Context, projectFlockKandangId uint) (float64, error) GetPulletCost(ctx context.Context, projectFlockKandangId uint) (float64, error)
GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, 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) GetProjectFlockIDByProjectFlockKandangID(ctx context.Context, projectFlockKandangId uint) (uint, error)
GetTransferSourceSummary(ctx context.Context, projectFlockKandangId uint) (uint, float64, 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) { func (r *HppRepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) {
if date == nil { // if date == nil {
now := time.Now() // now := time.Now()
date = &now // date = &now
} // }
var totals struct { var totals struct {
TotalPieces float64 TotalPieces float64
@@ -222,12 +222,13 @@ func (r *HppRepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandang
func (r *HppRepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds( func (r *HppRepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(
ctx context.Context, ctx context.Context,
projectFlockKandangIDs []uint, projectFlockKandangIDs []uint,
date *time.Time, startDate *time.Time,
endDate *time.Time,
) (float64, float64, error) { ) (float64, float64, error) {
if date == nil { if endDate == nil {
now := time.Now() now := time.Now()
date = &now endDate = &now
} }
type subResult struct { type subResult struct {
@@ -251,7 +252,8 @@ func (r *HppRepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangI
). ).
Joins("JOIN marketing_delivery_products mdp ON mdp.id = sa.usable_id"). Joins("JOIN marketing_delivery_products mdp ON mdp.id = sa.usable_id").
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs). 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 { var totals struct {
TotalPieces float64 TotalPieces float64
+38 -34
View File
@@ -11,10 +11,10 @@ import (
type HppService interface { type HppService interface {
CalculateHppCost(projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error) CalculateHppCost(projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error)
GetTotalDepresiasiFlockGrowing(sourceProjectFlockID uint, date *time.Time) (float64, error) GetTotalDepresiasiFlockGrowing(sourceProjectFlockID uint, date *time.Time) (float64, error)
GetTotalProductionCost(projectFlockKandangId uint, date *time.Time, totalDepresiasiGrowing float64) (float64, error) GetTotalProductionCost(projectFlockKandangId uint, endDate *time.Time, depresiasiTransfer float64) (float64, error)
GetBudgetKandangLaying(projectFlockKandangId uint, date *time.Time) (float64, error) GetBudgetKandangLaying(projectFlockKandangId uint, endDate *time.Time) (float64, error)
GetDepresiasiTransfer(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) GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, startDate *time.Time, endDate *time.Time) (*HppCostResponse, error)
} }
type HppCostResponse struct { type HppCostResponse struct {
@@ -44,17 +44,25 @@ func (s *hppService) CalculateHppCost(projectFlockKandangId uint, date *time.Tim
date = &now date = &now
} }
depresiasiTransfer, err := s.GetDepresiasiTransfer(projectFlockKandangId, date) location, err := time.LoadLocation("Asia/Jakarta")
if err != nil { if err != nil {
return nil, err 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 { if err != nil {
return nil, err 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 return docCost + budgetCost + expedisionCost + feedCost + ovkCost, nil
} }
func (s *hppService) GetTotalProductionCost(projectFlockKandangId uint, date *time.Time, depresiasiTransfer float64) (float64, error) { func (s *hppService) GetTotalProductionCost(projectFlockKandangId uint, endDate *time.Time, depresiasiTransfer float64) (float64, error) {
if date == nil { // if date == nil {
now := time.Now() // now := time.Now()
date = &now // date = &now
} // }
costPullet, err := s.hppRepo.GetPulletCost(context.Background(), projectFlockKandangId) costPullet, err := s.hppRepo.GetPulletCost(context.Background(), projectFlockKandangId)
if err != nil { if err != nil {
return 0, err 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 { if err != nil {
return 0, err 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 { if err != nil {
return 0, err return 0, err
} }
@@ -127,7 +135,7 @@ func (s *hppService) GetTotalProductionCost(projectFlockKandangId uint, date *ti
return 0, err return 0, err
} }
costBudget, err := s.GetBudgetKandangLaying(projectFlockKandangId, date) costBudget, err := s.GetBudgetKandangLaying(projectFlockKandangId, endDate)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -135,11 +143,11 @@ func (s *hppService) GetTotalProductionCost(projectFlockKandangId uint, date *ti
return depresiasiTransfer + costPullet + costFeed + costOvk + costExpedision + costBudget, nil return depresiasiTransfer + costPullet + costFeed + costOvk + costExpedision + costBudget, nil
} }
func (s *hppService) GetBudgetKandangLaying(projectFlockKandangId uint, date *time.Time) (float64, error) { func (s *hppService) GetBudgetKandangLaying(projectFlockKandangId uint, endDate *time.Time) (float64, error) {
if date == nil { // if date == nil {
now := time.Now() // now := time.Now()
date = &now // date = &now
} // }
if s.hppRepo == nil { if s.hppRepo == nil {
return 0, nil return 0, nil
@@ -155,12 +163,12 @@ func (s *hppService) GetBudgetKandangLaying(projectFlockKandangId uint, date *ti
return 0, err return 0, err
} }
eggProduksiPiecesFlock, _, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), projectFlockKandangIds, date) eggProduksiPiecesFlock, _, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), projectFlockKandangIds, endDate)
if err != nil { if err != nil {
return 0, err 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 { if err != nil {
return 0, err return 0, err
} }
@@ -177,11 +185,11 @@ func (s *hppService) GetBudgetKandangLaying(projectFlockKandangId uint, date *ti
return (totalBudgetCost * eggProduksiPiecesKandang) / eggProduksiPiecesFlock, nil return (totalBudgetCost * eggProduksiPiecesKandang) / eggProduksiPiecesFlock, nil
} }
func (s *hppService) GetDepresiasiTransfer(projectFlockKandangId uint, date *time.Time) (float64, error) { func (s *hppService) GetDepresiasiTransfer(projectFlockKandangId uint, endDate *time.Time) (float64, error) {
if date == nil { // if endDate == nil {
now := time.Now() // now := time.Now()
date = &now // endDate = &now
} // }
if s.hppRepo == nil { if s.hppRepo == nil {
return 0, nil return 0, nil
@@ -205,7 +213,7 @@ func (s *hppService) GetDepresiasiTransfer(projectFlockKandangId uint, date *tim
return 0, nil return 0, nil
} }
totalDepresiasiFlockGrowing, err := s.GetTotalDepresiasiFlockGrowing(sourceProjectFlockID, date) totalDepresiasiFlockGrowing, err := s.GetTotalDepresiasiFlockGrowing(sourceProjectFlockID, endDate)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -213,22 +221,18 @@ func (s *hppService) GetDepresiasiTransfer(projectFlockKandangId uint, date *tim
return (totalDepresiasiFlockGrowing * transferTotalQty) / totalPopulationFlockGrowing, nil return (totalDepresiasiFlockGrowing * transferTotalQty) / totalPopulationFlockGrowing, nil
} }
func (s *hppService) GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error) { func (s *hppService) GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, startDate *time.Time, endDate *time.Time) (*HppCostResponse, error) {
if date == nil {
now := time.Now()
date = &now
}
if s.hppRepo == nil { if s.hppRepo == nil {
return &HppCostResponse{}, 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 { if err != nil {
return nil, err 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 { if err != nil {
return nil, err return nil, err
} }
@@ -1617,7 +1617,7 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
var avgWeight float64 var avgWeight float64
eggHpp := 0.0 eggHpp := 0.0
if s.HppSvc != nil { if s.HppSvc != nil {
hppCost, err := s.HppSvc.CalculateHppCost(row.ProjectFlockKandangID, &endOfDay) hppCost, err := s.HppSvc.CalculateHppCost(row.ProjectFlockKandangID, &periodDate)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@@ -1626,7 +1626,9 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
eggHpp = hppCost.Estimation.HargaKg eggHpp = hppCost.Estimation.HargaKg
eggTotalPiecesFloat = hppCost.Estimation.Butir eggTotalPiecesFloat = hppCost.Estimation.Butir
eggWeightFloat = hppCost.Estimation.Kg eggWeightFloat = hppCost.Estimation.Kg
avgWeight = eggWeightFloat / eggTotalPiecesFloat if eggTotalPiecesFloat > 0 {
avgWeight = eggWeightFloat / eggTotalPiecesFloat
}
eggRemainingWeightFloatRemaining = avgWeight * eggPiecesFloatRemaining eggRemainingWeightFloatRemaining = avgWeight * eggPiecesFloatRemaining
} }
} }
@@ -1642,6 +1644,9 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
if math.IsNaN(eggWeightFloat) || math.IsInf(eggWeightFloat, 0) { if math.IsNaN(eggWeightFloat) || math.IsInf(eggWeightFloat, 0) {
eggWeightFloat = 0 eggWeightFloat = 0
} }
if math.IsNaN(avgWeight) || math.IsInf(avgWeight, 0) {
avgWeight = 0
}
if params.WeightMin != nil && avgWeight < *params.WeightMin { if params.WeightMin != nil && avgWeight < *params.WeightMin {
continue continue