mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +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")
|
return 0, fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid")
|
||||||
}
|
}
|
||||||
|
|
||||||
populations, err := s.ProjectFlockPopulationRepo.GetByProjectFlockKandangID(ctx, projectFlockKandangID)
|
// If this PFK is a laying transfer target, use source growing PFK's chick_in_date
|
||||||
if err != nil {
|
sourcePFKIDs := s.getLayingTransferSourcePFKIDs(ctx, projectFlockKandangID)
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
var chickinDate time.Time
|
var chickinDate time.Time
|
||||||
for _, pop := range populations {
|
if len(sourcePFKIDs) > 0 {
|
||||||
if pop.ProjectChickin == nil || pop.ProjectChickin.ChickInDate.IsZero() {
|
for _, pfkID := range sourcePFKIDs {
|
||||||
continue
|
cd := s.getEarliestChickInDate(ctx, pfkID)
|
||||||
}
|
if !cd.IsZero() && (chickinDate.IsZero() || cd.Before(chickinDate)) {
|
||||||
if chickinDate.IsZero() || pop.ProjectChickin.ChickInDate.Before(chickinDate) {
|
chickinDate = cd
|
||||||
chickinDate = pop.ProjectChickin.ChickInDate
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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() {
|
if chickinDate.IsZero() {
|
||||||
return 0, fiber.NewError(fiber.StatusBadRequest, "Tanggal chick in tidak ditemukan")
|
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
|
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 {
|
func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm.DB, recording *entity.Recording) error {
|
||||||
day := 0
|
day := 0
|
||||||
if recording.Day != nil {
|
if recording.Day != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user