diff --git a/internal/common/repository/common.hpp.repository.go b/internal/common/repository/common.hpp.repository.go index 2c8187fb..d1dc51f0 100644 --- a/internal/common/repository/common.hpp.repository.go +++ b/internal/common/repository/common.hpp.repository.go @@ -139,12 +139,11 @@ func (r *HppRepositoryImpl) GetOvkUsageCost(ctx context.Context, projectFlockKan Select("COALESCE(SUM(rs.usage_qty * COALESCE(pi.price, 0)), 0)"). Joins("JOIN recording_stocks AS rs ON rs.recording_id = r.id"). Joins("JOIN product_warehouses AS pw ON pw.id = rs.product_warehouse_id"). - Joins("JOIN flags AS f ON f.flagable_id = pw.product_id AND f.flagable_type = ?", entity.FlagableTypeProduct). Joins("JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = rs.id AND sa.stockable_type = ?", fifo.UsableKeyRecordingStock.String(), fifo.StockableKeyPurchaseItems.String()). Joins("JOIN purchase_items AS pi ON pi.id = sa.stockable_id"). Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs). Where("r.record_datetime <= ?", *date). - Where("f.name IN ?", flags). + Where("EXISTS (SELECT 1 FROM flags f WHERE f.flagable_id = pw.product_id AND f.flagable_type = ? AND f.name IN ?)", entity.FlagableTypeProduct, flags). Scan(&total).Error if err != nil { return 0, err diff --git a/internal/modules/closings/services/closingKeuangan.service.go b/internal/modules/closings/services/closingKeuangan.service.go index 804ca023..757d553c 100644 --- a/internal/modules/closings/services/closingKeuangan.service.go +++ b/internal/modules/closings/services/closingKeuangan.service.go @@ -294,6 +294,9 @@ func (s closingKeuanganService) calculateProductionData(c *fiber.Ctx, projectFlo func (s closingKeuanganService) buildHPPSection(c *fiber.Ctx, projectFlock *entity.ProjectFlock, projectFlockKandangs []entity.ProjectFlockKandang, costs *CostData, production *ProductionData) dto.HPPSection { actualPopulation := production.TotalPopulationIn - production.TotalDepletion + if lastPopulation, ok := s.getLastPopulationFromRecordings(c, projectFlockKandangs); ok { + actualPopulation = lastPopulation + } totalWeightProduced := production.TotalWeightProduced totalEggWeightKg := production.TotalEggWeightKg @@ -529,6 +532,35 @@ func (s closingKeuanganService) buildProfitLossSection(projectFlock *entity.Proj return dto.ToProfitLossSection(plItems, plSummary) } +func (s closingKeuanganService) getLastPopulationFromRecordings(c *fiber.Ctx, projectFlockKandangs []entity.ProjectFlockKandang) (float64, bool) { + if s.RecordingRepo == nil || len(projectFlockKandangs) == 0 { + return 0, false + } + + total := 0.0 + recordedCount := 0 + for _, kandang := range projectFlockKandangs { + latest, err := s.RecordingRepo.GetLatestByProjectFlockKandangID(c.Context(), kandang.Id) + if err != nil { + s.Log.Errorf("Failed to fetch latest recording for project_flock_kandang_id=%d: %+v", kandang.Id, err) + return 0, false + } + if latest == nil || latest.TotalChickQty == nil { + continue + } + recordedCount++ + if *latest.TotalChickQty > 0 { + total += *latest.TotalChickQty + } + } + + if recordedCount != len(projectFlockKandangs) { + return 0, false + } + + return total, true +} + func containsFlag(flags []entity.Flag, name string) bool { for _, flag := range flags { if flag.Name == name {