diff --git a/internal/common/repository/common.hppv2.repository.go b/internal/common/repository/common.hppv2.repository.go index dfd9c29f..c58503d0 100644 --- a/internal/common/repository/common.hppv2.repository.go +++ b/internal/common/repository/common.hppv2.repository.go @@ -115,6 +115,7 @@ type HppV2CostRepository interface { GetFeedUsageCost(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, error) GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (float64, error) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) + GetEggProduksiBreakdownByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (recordingQty, recordingWeight, adjustmentQty, adjustmentWeight float64, err error) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, startDate *time.Time, endDate *time.Time) (float64, float64, error) GetTransferSourceSummary(ctx context.Context, projectFlockKandangId uint) (uint, float64, error) } @@ -858,24 +859,32 @@ func (r *HppV2RepositoryImpl) GetTotalPopulation(ctx context.Context, projectFlo } func (r *HppV2RepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) { + rQty, rWeight, aQty, aWeight, err := r.GetEggProduksiBreakdownByProjectFlockKandangIds(ctx, projectFlockKandangIDs, date) + if err != nil { + return 0, 0, err + } + return rQty + aQty, rWeight + aWeight, nil +} + +func (r *HppV2RepositoryImpl) GetEggProduksiBreakdownByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (recordingQty, recordingWeight, adjustmentQty, adjustmentWeight float64, err error) { if date == nil { now := time.Now() date = &now } - var totals struct { + var recordingTotals struct { TotalPieces float64 TotalWeightKg float64 } - err := r.db.WithContext(ctx). + err = r.db.WithContext(ctx). Table("recordings AS r"). Select("COALESCE(SUM(re.qty), 0) AS total_pieces, COALESCE(SUM(re.weight), 0) AS total_weight_kg"). Joins("JOIN recording_eggs AS re ON re.recording_id = r.id"). Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs). Where("r.record_datetime <= ?", *date). - Scan(&totals).Error + Scan(&recordingTotals).Error if err != nil { - return 0, 0, err + return 0, 0, 0, 0, err } var adjustmentTotals struct { @@ -891,13 +900,10 @@ func (r *HppV2RepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKanda Where("ast.created_at <= ?", *date). Scan(&adjustmentTotals).Error if err != nil { - return 0, 0, err + return 0, 0, 0, 0, err } - totals.TotalPieces += adjustmentTotals.TotalQty - totals.TotalWeightKg += adjustmentTotals.TotalWeight - - return totals.TotalPieces, totals.TotalWeightKg, nil + return recordingTotals.TotalPieces, recordingTotals.TotalWeightKg, adjustmentTotals.TotalQty, adjustmentTotals.TotalWeight, nil } func (r *HppV2RepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds( diff --git a/internal/common/service/common.hpp.service.go b/internal/common/service/common.hpp.service.go index db83d5a6..bd28c5f8 100644 --- a/internal/common/service/common.hpp.service.go +++ b/internal/common/service/common.hpp.service.go @@ -18,8 +18,9 @@ type HppService interface { } type HppCostResponse struct { - Estimation HppCostDetail `json:"estimation"` - Real HppCostDetail `json:"real"` + Estimation HppCostDetail `json:"estimation"` + Real HppCostDetail `json:"real"` + DebugValues *HppCostDebugValues `json:"debug_values,omitempty"` } type HppCostDetail struct { diff --git a/internal/common/service/common.hppv2.model.go b/internal/common/service/common.hppv2.model.go index f6f94bf9..d1b602d8 100644 --- a/internal/common/service/common.hppv2.model.go +++ b/internal/common/service/common.hppv2.model.go @@ -44,6 +44,15 @@ type HppV2Component struct { Parts []HppV2ComponentPart `json:"parts"` } +type HppCostDebugValues struct { + RecordingEggQty float64 `json:"recording_egg_qty"` + RecordingEggWeight float64 `json:"recording_egg_weight"` + AdjustmentEggQty float64 `json:"adjustment_egg_qty"` + AdjustmentEggWeight float64 `json:"adjustment_egg_weight"` + SoldEggQty float64 `json:"sold_egg_qty"` + SoldEggWeight float64 `json:"sold_egg_weight"` +} + type HppV2Breakdown struct { ProjectFlockKandangID uint `json:"project_flock_kandang_id"` ProjectFlockID uint `json:"project_flock_id"` diff --git a/internal/common/service/common.hppv2.service.go b/internal/common/service/common.hppv2.service.go index 661d23c2..a2b56073 100644 --- a/internal/common/service/common.hppv2.service.go +++ b/internal/common/service/common.hppv2.service.go @@ -1489,7 +1489,7 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64, return &HppCostResponse{}, nil } - estimPieces, estimWeightKg, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, endDate) + recordingQty, recordingWeight, adjustmentQty, adjustmentWeight, err := s.hppRepo.GetEggProduksiBreakdownByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, endDate) if err != nil { utils.Log.WithError(err).Errorf( "GetHppEstimationDanRealisasi failed to get estimation egg production: project_flock_kandang_id=%d end_date=%s", @@ -1498,6 +1498,8 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64, ) return nil, err } + estimPieces := recordingQty + adjustmentQty + estimWeightKg := recordingWeight + adjustmentWeight realPieces, realWeightKg, err := s.hppRepo.GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, startDate, endDate) if err != nil { @@ -1551,6 +1553,14 @@ func (s *hppV2Service) GetHppEstimationDanRealisasi(totalProductionCost float64, return &HppCostResponse{ Estimation: estimation, Real: real, + DebugValues: &HppCostDebugValues{ + RecordingEggQty: recordingQty, + RecordingEggWeight: recordingWeight, + AdjustmentEggQty: adjustmentQty, + AdjustmentEggWeight: adjustmentWeight, + SoldEggQty: realPieces, + SoldEggWeight: realWeightKg, + }, }, nil } diff --git a/internal/common/service/common.hppv2.service_test.go b/internal/common/service/common.hppv2.service_test.go index 8ab515c6..d75cf0f2 100644 --- a/internal/common/service/common.hppv2.service_test.go +++ b/internal/common/service/common.hppv2.service_test.go @@ -132,6 +132,17 @@ func (s *hppV2RepoStub) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds( return totalPieces, totalKg, nil } +func (s *hppV2RepoStub) GetEggProduksiBreakdownByProjectFlockKandangIds(_ context.Context, projectFlockKandangIDs []uint, _ *time.Time) (float64, float64, float64, float64, error) { + totalPieces := 0.0 + totalKg := 0.0 + for _, projectFlockKandangID := range projectFlockKandangIDs { + row := s.eggProductionByPFK[projectFlockKandangID] + totalPieces += row.pieces + totalKg += row.kg + } + return totalPieces, totalKg, 0, 0, nil +} + func (s *hppV2RepoStub) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(_ context.Context, projectFlockKandangIDs []uint, _ *time.Time, _ *time.Time) (float64, float64, error) { if len(projectFlockKandangIDs) != 1 { return 0, 0, nil