From c328b9a880b0ed7db213765cd5e10b01753e05cd Mon Sep 17 00:00:00 2001 From: giovanni Date: Fri, 8 May 2026 13:55:48 +0700 Subject: [PATCH] fix calculate day recording if has laying transfer --- .../recordings/services/recording.service.go | 74 ++++++++++++++++--- 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/internal/modules/production/recordings/services/recording.service.go b/internal/modules/production/recordings/services/recording.service.go index 3f0e7946..7ca84d95 100644 --- a/internal/modules/production/recordings/services/recording.service.go +++ b/internal/modules/production/recordings/services/recording.service.go @@ -2434,21 +2434,24 @@ func (s *recordingService) computeRecordingDay(ctx context.Context, projectFlock return 0, fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid") } - populations, err := s.ProjectFlockPopulationRepo.GetByProjectFlockKandangID(ctx, projectFlockKandangID) - if err != nil { - s.Log.Errorf("Failed to fetch populations for project_flock_kandang_id=%d: %+v", projectFlockKandangID, err) - return 0, fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil data populasi") - } + // If this PFK is a laying transfer target, use source growing PFK's chick_in_date + sourcePFKIDs := s.getLayingTransferSourcePFKIDs(ctx, projectFlockKandangID) var chickinDate time.Time - for _, pop := range populations { - if pop.ProjectChickin == nil || pop.ProjectChickin.ChickInDate.IsZero() { - continue - } - if chickinDate.IsZero() || pop.ProjectChickin.ChickInDate.Before(chickinDate) { - chickinDate = pop.ProjectChickin.ChickInDate + if len(sourcePFKIDs) > 0 { + for _, pfkID := range sourcePFKIDs { + cd := s.getEarliestChickInDate(ctx, pfkID) + if !cd.IsZero() && (chickinDate.IsZero() || cd.Before(chickinDate)) { + chickinDate = cd + } } } + + // Fallback: use current PFK's own chick_in_date (cut-over or non-laying) + if chickinDate.IsZero() { + chickinDate = s.getEarliestChickInDate(ctx, projectFlockKandangID) + } + if chickinDate.IsZero() { return 0, fiber.NewError(fiber.StatusBadRequest, "Tanggal chick in tidak ditemukan") } @@ -2463,6 +2466,55 @@ func (s *recordingService) computeRecordingDay(ctx context.Context, projectFlock return diff, nil } +func (s *recordingService) getLayingTransferSourcePFKIDs(ctx context.Context, targetPFKID uint) []uint { + transfer, err := s.TransferLayingRepo.GetLatestApprovedByTargetKandang(ctx, targetPFKID) + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + s.Log.Errorf("Failed to check laying transfer for pfk=%d: %+v", targetPFKID, err) + } + return nil + } + if transfer == nil { + return nil + } + + ids := make([]uint, 0) + if transfer.SourceProjectFlockKandangId != nil { + ids = append(ids, *transfer.SourceProjectFlockKandangId) + } + + // Check multi-source transfers + var sources []entity.LayingTransferSource + if err := s.Repository.DB().WithContext(ctx). + Where("laying_transfer_id = ?", transfer.Id). + Find(&sources).Error; err == nil { + for _, src := range sources { + ids = append(ids, src.SourceProjectFlockKandangId) + } + } + + return ids +} + +func (s *recordingService) getEarliestChickInDate(ctx context.Context, pfkID uint) time.Time { + populations, err := s.ProjectFlockPopulationRepo.GetByProjectFlockKandangID(ctx, pfkID) + if err != nil { + s.Log.Errorf("Failed to fetch populations for pfk=%d: %+v", pfkID, err) + return time.Time{} + } + + var chickinDate time.Time + for _, pop := range populations { + if pop.ProjectChickin == nil || pop.ProjectChickin.ChickInDate.IsZero() { + continue + } + if chickinDate.IsZero() || pop.ProjectChickin.ChickInDate.Before(chickinDate) { + chickinDate = pop.ProjectChickin.ChickInDate + } + } + return chickinDate +} + func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm.DB, recording *entity.Recording) error { day := 0 if recording.Day != nil {