From 0ac40adb5afd2e04a1344246d1f50d79ec20c064 Mon Sep 17 00:00:00 2001 From: giovanni Date: Mon, 4 May 2026 11:30:53 +0700 Subject: [PATCH] adjust calculate umur ayam at recording --- .../service/common.depreciation.service.go | 2 +- ...adjust_recording_day_zero_indexed.down.sql | 14 +++++++++ ...4_adjust_recording_day_zero_indexed.up.sql | 14 +++++++++ .../dashboard_stats.repository.go | 30 +++++++++---------- .../recordings/services/recording.service.go | 6 ++-- 5 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 internal/database/migrations/20260504035824_adjust_recording_day_zero_indexed.down.sql create mode 100644 internal/database/migrations/20260504035824_adjust_recording_day_zero_indexed.up.sql diff --git a/internal/common/service/common.depreciation.service.go b/internal/common/service/common.depreciation.service.go index 6f12e077..e73e601a 100644 --- a/internal/common/service/common.depreciation.service.go +++ b/internal/common/service/common.depreciation.service.go @@ -31,7 +31,7 @@ func FlockAgeDay(originDate time.Time, periodDate time.Time) int { if period.Before(origin) { return 0 } - return int(period.Sub(origin).Hours()/24) + 1 + return int(period.Sub(origin).Hours() / 24) } func DepreciationScheduleDay(originDate time.Time, periodDate time.Time, houseType string) int { diff --git a/internal/database/migrations/20260504035824_adjust_recording_day_zero_indexed.down.sql b/internal/database/migrations/20260504035824_adjust_recording_day_zero_indexed.down.sql new file mode 100644 index 00000000..2f92644d --- /dev/null +++ b/internal/database/migrations/20260504035824_adjust_recording_day_zero_indexed.down.sql @@ -0,0 +1,14 @@ +UPDATE recordings r +SET day = ( + SELECT (r.record_datetime::date - MIN(pc.chick_in_date)::date)::int + 1 + FROM project_chickins pc + WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id + AND pc.deleted_at IS NULL +) +WHERE r.deleted_at IS NULL + AND ( + SELECT MIN(pc.chick_in_date) + FROM project_chickins pc + WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id + AND pc.deleted_at IS NULL + ) IS NOT NULL; diff --git a/internal/database/migrations/20260504035824_adjust_recording_day_zero_indexed.up.sql b/internal/database/migrations/20260504035824_adjust_recording_day_zero_indexed.up.sql new file mode 100644 index 00000000..67937750 --- /dev/null +++ b/internal/database/migrations/20260504035824_adjust_recording_day_zero_indexed.up.sql @@ -0,0 +1,14 @@ +UPDATE recordings r +SET day = ( + SELECT (r.record_datetime::date - MIN(pc.chick_in_date)::date)::int + FROM project_chickins pc + WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id + AND pc.deleted_at IS NULL +) +WHERE r.deleted_at IS NULL + AND ( + SELECT MIN(pc.chick_in_date) + FROM project_chickins pc + WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id + AND pc.deleted_at IS NULL + ) IS NOT NULL; diff --git a/internal/modules/dashboards/repositories/dashboard_stats.repository.go b/internal/modules/dashboards/repositories/dashboard_stats.repository.go index c79879fb..4197fd69 100644 --- a/internal/modules/dashboards/repositories/dashboard_stats.repository.go +++ b/internal/modules/dashboards/repositories/dashboard_stats.repository.go @@ -119,9 +119,9 @@ func (r *DashboardRepositoryImpl) GetRecordingWeeklyMetrics(ctx context.Context, var rows []RecordingWeeklyMetric weekExpr := `CASE - WHEN r.day IS NULL OR r.day <= 0 THEN 1 - WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17 - ELSE ((r.day - 1) / 7 + 1) + WHEN r.day IS NULL OR r.day < 0 THEN 1 + WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17 + ELSE (r.day / 7 + 1) END` db := r.DB().WithContext(ctx). @@ -503,9 +503,9 @@ func (r *DashboardRepositoryImpl) GetComparisonWeeklyMetrics(ctx context.Context var rows []ComparisonWeeklyMetric weekExpr := `CASE - WHEN r.day IS NULL OR r.day <= 0 THEN 1 - WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17 - ELSE ((r.day - 1) / 7 + 1) + WHEN r.day IS NULL OR r.day < 0 THEN 1 + WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17 + ELSE (r.day / 7 + 1) END` db := r.DB().WithContext(ctx). @@ -574,9 +574,9 @@ func (r *DashboardRepositoryImpl) GetEggQualityWeeklyMetrics(ctx context.Context var rows []EggQualityWeeklyMetric weekExpr := `CASE - WHEN r.day IS NULL OR r.day <= 0 THEN 1 - WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17 - ELSE ((r.day - 1) / 7 + 1) + WHEN r.day IS NULL OR r.day < 0 THEN 1 + WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17 + ELSE (r.day / 7 + 1) END` db := r.DB().WithContext(ctx). @@ -616,9 +616,9 @@ func (r *DashboardRepositoryImpl) GetEggWeightWeeklyGrams(ctx context.Context, s var rows []WeeklyEggWeightMetric weekExpr := `CASE - WHEN r.day IS NULL OR r.day <= 0 THEN 1 - WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17 - ELSE ((r.day - 1) / 7 + 1) + WHEN r.day IS NULL OR r.day < 0 THEN 1 + WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17 + ELSE (r.day / 7 + 1) END` db := r.DB().WithContext(ctx). @@ -647,9 +647,9 @@ func (r *DashboardRepositoryImpl) GetFeedUsageWeeklyByUom(ctx context.Context, s var rows []WeeklyFeedUsageMetric weekExpr := `CASE - WHEN r.day IS NULL OR r.day <= 0 THEN 1 - WHEN UPPER(pf.category) = 'LAYING' THEN ((r.day - 1) / 7 + 1) + 17 - ELSE ((r.day - 1) / 7 + 1) + WHEN r.day IS NULL OR r.day < 0 THEN 1 + WHEN UPPER(pf.category) = 'LAYING' THEN (r.day / 7 + 1) + 17 + ELSE (r.day / 7 + 1) END` db := r.DB().WithContext(ctx). diff --git a/internal/modules/production/recordings/services/recording.service.go b/internal/modules/production/recordings/services/recording.service.go index c7bfe648..5264e32a 100644 --- a/internal/modules/production/recordings/services/recording.service.go +++ b/internal/modules/production/recordings/services/recording.service.go @@ -2460,7 +2460,7 @@ func (s *recordingService) computeRecordingDay(ctx context.Context, projectFlock return 0, fiber.NewError(fiber.StatusBadRequest, "Record date tidak boleh sebelum tanggal chick in") } - return diff + 1, nil + return diff, nil } func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm.DB, recording *entity.Recording) error { @@ -2621,8 +2621,8 @@ func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm if isGrowing { week := 0 - if recording.Day != nil && *recording.Day > 0 { - week = (*recording.Day-1)/7 + 1 + if recording.Day != nil && *recording.Day >= 0 { + week = *recording.Day/7 + 1 } if week > 0 && s.Repository != nil { meanBw, ok, err := s.Repository.GetUniformityMeanBwByWeek(tx, recording.ProjectFlockKandangId, week)