adjust hpp per farm query to take feed and ovk

This commit is contained in:
giovanni
2026-06-06 10:29:33 +07:00
parent 98bfdac3c5
commit aa3e655a67
5 changed files with 429 additions and 34 deletions
@@ -3256,8 +3256,19 @@ func (s *repportService) GetHppPerFarm(ctx *fiber.Ctx) (*dto.HppPerFarmResponseD
feed := codeTotals[hppPerFarmComponentPakan]
ovk := codeTotals[hppPerFarmComponentOvk]
bop := codeTotals[hppPerFarmComponentBopRegular] + codeTotals[hppPerFarmComponentBopEkspedisi]
nonDepreciation := 0.0
// BOP dihitung range-correct via engine (hindari differential rasio egg-weight yang bisa
// negatif saat share antar kandang bergeser). Keluarkan kode BOP dari codeTotals agar tidak
// ikut terjumlah dua kali di akumulasi 'nonDepreciation'/'other'.
delete(codeTotals, hppPerFarmComponentBopRegular)
delete(codeTotals, hppPerFarmComponentBopEkspedisi)
bop, err := s.hppPerFarmFlockBopRange(ctx.Context(), flockID, startBreakdownDate, endBreakdownDate)
if err != nil {
return nil, nil, err
}
nonDepreciation := bop
for _, value := range codeTotals {
nonDepreciation += value
}
@@ -3428,6 +3439,39 @@ func (s *repportService) hppPerFarmFlockCostRange(ctx context.Context, projectFl
return codeTotals, nil
}
// hppPerFarmFlockBopRange menjumlah BOP production_cost range-correct (BOP_REGULAR + BOP_EKSPEDISI)
// untuk seluruh PFK dalam flock, memakai GetBop*ProductionScopeRange di engine. Pendekatan ini
// menghitung delta expense kumulatif lalu memproratanya dengan rasio akhir-range — bukan
// men-differensiasi dua angka yang sudah diprorata berbeda — sehingga tidak pernah negatif.
func (s *repportService) hppPerFarmFlockBopRange(ctx context.Context, projectFlockID uint, startBreakdownDate, endBreakdownDate time.Time) (float64, error) {
if s.HppCostRepo == nil {
return 0, errors.New("hpp cost repository is not configured")
}
if s.HppV2Svc == nil {
return 0, errors.New("hpp v2 service is not configured")
}
pfkIDs, err := s.HppCostRepo.GetProjectFlockKandangIDs(ctx, projectFlockID)
if err != nil {
return 0, err
}
total := 0.0
for _, pfkID := range pfkIDs {
reg, err := s.HppV2Svc.GetBopRegularProductionScopeRange(pfkID, &startBreakdownDate, &endBreakdownDate)
if err != nil {
return 0, err
}
eksp, err := s.HppV2Svc.GetBopEkspedisiProductionScopeRange(pfkID, &startBreakdownDate, &endBreakdownDate)
if err != nil {
return 0, err
}
total += reg + eksp
}
return total, nil
}
// sumHppPerFarmDepreciationOverRange sums the daily depreciation_value from
// farm_depreciation_snapshots across [startDate, endDate] per project flock,
// computing (and persisting) any missing daily snapshot on demand — same lazy