fix recording standar prod laying

This commit is contained in:
giovanni
2026-06-07 18:55:24 +07:00
parent edfd6ac95c
commit 2216f572c2
6 changed files with 445 additions and 11 deletions
@@ -387,35 +387,87 @@ func (s productionStandardService) EnsureWeekAvailable(ctx context.Context, stan
return nil
}
week := ((day - 1) / 7) + 1
if week <= 0 {
requestedWeek := ((day - 1) / 7) + 1
if requestedWeek <= 0 {
return nil
}
upperCategory := strings.ToUpper(category)
if upperCategory == string(utils.ProjectFlockCategoryLaying) {
detail, err := s.ProductionStandardDetailRepo.GetByStandardIDAndWeek(ctx, standardID, week)
effectiveWeek := requestedWeek
firstCommonWeek, ok, err := s.layingFirstCommonStandardWeek(ctx, standardID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", week))
}
return err
}
if detail == nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", week))
if ok && requestedWeek < firstCommonWeek {
effectiveWeek = firstCommonWeek
}
detail, err := s.ProductionStandardDetailRepo.GetByStandardIDAndWeek(ctx, standardID, effectiveWeek)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
}
growthDetail, err := s.StandardGrowthDetailRepo.GetByStandardIDAndWeek(ctx, standardID, effectiveWeek)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
}
if detail != nil && growthDetail != nil {
return nil
}
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", requestedWeek))
}
growthDetail, err := s.StandardGrowthDetailRepo.GetByStandardIDAndWeek(ctx, standardID, week)
growthDetail, err := s.StandardGrowthDetailRepo.GetByStandardIDAndWeek(ctx, standardID, requestedWeek)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", week))
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", requestedWeek))
}
return err
}
if growthDetail == nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", week))
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", requestedWeek))
}
return nil
}
func (s productionStandardService) layingFirstCommonStandardWeek(ctx context.Context, standardID uint) (int, bool, error) {
details, err := s.ProductionStandardDetailRepo.GetByProductionStandardID(ctx, standardID)
if err != nil {
return 0, false, err
}
detailWeeks := make(map[int]struct{}, len(details))
for _, detail := range details {
if detail.Week <= 0 {
continue
}
detailWeeks[detail.Week] = struct{}{}
}
growthDetails, err := s.StandardGrowthDetailRepo.GetByProductionStandardID(ctx, standardID)
if err != nil {
return 0, false, err
}
firstCommonWeek := 0
for _, detail := range growthDetails {
if detail.Week <= 0 {
continue
}
if _, ok := detailWeeks[detail.Week]; !ok {
continue
}
if firstCommonWeek == 0 || detail.Week < firstCommonWeek {
firstCommonWeek = detail.Week
}
}
return firstCommonWeek, firstCommonWeek > 0, nil
}