mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
Merge branch 'fix/day-record' into 'development'
[FIX][BE]: fix calculate day recording if has laying transfer See merge request mbugroup/lti-api!522
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user