package service import ( "context" "errors" "fmt" "math" "strings" "time" commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository" commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service" entity "gitlab.com/mbugroup/lti-api.git/internal/entities" m "gitlab.com/mbugroup/lti-api.git/internal/middleware" rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories" KandangRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories" rProduct "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/repositories" rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories" repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories" validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/validations" rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories" rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories" rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories" "gitlab.com/mbugroup/lti-api.git/internal/utils" "gitlab.com/mbugroup/lti-api.git/internal/utils/fifo" "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v2" "github.com/jackc/pgconn" pgconnv5 "github.com/jackc/pgx/v5/pgconn" "github.com/sirupsen/logrus" "gorm.io/gorm" "gorm.io/gorm/clause" ) const ( chickinDeletePopulationGuardMessage = "Chickin tidak dapat dihapus karena masih memiliki population aktif" chickinDeleteRecordingGuardMessage = "Chickin tidak dapat dihapus karena masih terkait recording. Lakukan rollback/delete recording terlebih dahulu" ) type ChickinService interface { GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectChickin, int64, error) GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectChickin, error) CreateOne(ctx *fiber.Ctx, req *validation.Create) ([]entity.ProjectChickin, error) UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error) DeleteOne(ctx *fiber.Ctx, id uint) error Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectChickin, error) EnsureChickInExists(ctx context.Context, projectFlockKandangID uint) error } type chickinService struct { Log *logrus.Logger Validate *validator.Validate Repository repository.ProjectChickinRepository KandangRepo KandangRepo.KandangRepository WarehouseRepo rWarehouse.WarehouseRepository ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository ProductRepo rProduct.ProductRepository ProjectFlockRepo rProjectFlock.ProjectflockRepository ProjectflockKandangRepo rProjectFlock.ProjectFlockKandangRepository ProjectflockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository ProjectChickinDetailRepo repository.ProjectChickinDetailRepository TransferLayingRepo rTransferLaying.TransferLayingRepository FifoStockV2Svc commonSvc.FifoStockV2Service StockLogRepo rStockLogs.StockLogRepository } func NewChickinService(repo repository.ProjectChickinRepository, kandangRepo KandangRepo.KandangRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, productRepo rProduct.ProductRepository, projectFlockRepo rProjectFlock.ProjectflockRepository, projectflockkandangRepo rProjectFlock.ProjectFlockKandangRepository, projectflockpopulationRepo rProjectFlock.ProjectFlockPopulationRepository, projectChickinDetailRepo repository.ProjectChickinDetailRepository, transferLayingRepo rTransferLaying.TransferLayingRepository, validate *validator.Validate, fifoStockV2Svc commonSvc.FifoStockV2Service) ChickinService { return &chickinService{ Log: utils.Log, Validate: validate, Repository: repo, KandangRepo: kandangRepo, WarehouseRepo: warehouseRepo, ProductWarehouseRepo: productWarehouseRepo, ProductRepo: productRepo, ProjectFlockRepo: projectFlockRepo, ProjectflockKandangRepo: projectflockkandangRepo, ProjectflockPopulationRepo: projectflockpopulationRepo, ProjectChickinDetailRepo: projectChickinDetailRepo, TransferLayingRepo: transferLayingRepo, FifoStockV2Svc: fifoStockV2Svc, StockLogRepo: rStockLogs.NewStockLogRepository(repo.DB()), } } func (s chickinService) withRelations(db *gorm.DB) *gorm.DB { return db. Preload("CreatedUser"). Preload("ProjectFlockKandang.Kandang"). Preload("ProjectFlockKandang.Kandang.Location"). Preload("ProjectFlockKandang.Kandang.Location.Area"). Preload("ProjectFlockKandang.Kandang.Pic"). Preload("ProjectFlockKandang.ProjectFlock"). Preload("ProjectFlockKandang.ProjectFlock.Area"). Preload("ProjectFlockKandang.ProjectFlock.ProductionStandard.ProductionStandardDetails"). Preload("ProjectFlockKandang.ProjectFlock.Location"). Preload("ProjectFlockKandang.ProjectFlock.Location.Area") } func (s chickinService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectChickin, int64, error) { if err := s.Validate.Struct(params); err != nil { return nil, 0, err } offset := (params.Page - 1) * params.Limit chickins, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB { db = s.withRelations(db) if params.ProjectFlockKandangId != 0 { return db.Where("project_flock_kandang_id = ?", params.ProjectFlockKandangId) } return db.Order("created_at DESC").Order("updated_at DESC") }) if err != nil { return nil, 0, err } return chickins, total, nil } func (s chickinService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectChickin, error) { chickin, err := s.Repository.GetByID(c.Context(), id, s.withRelations) if errors.Is(err, gorm.ErrRecordNotFound) { return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found") } if err != nil { return nil, err } return chickin, nil } func (s chickinService) ensureNotTransferred(ctx context.Context, projectFlockKandangID uint) error { if projectFlockKandangID == 0 || s.TransferLayingRepo == nil { return nil } // Restriction transfer->laying untuk chickin hanya berlaku pada kandang kategori growing. if s.ProjectflockKandangRepo != nil { pfk, err := s.ProjectflockKandangRepo.GetByIDLight(ctx, projectFlockKandangID) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { s.Log.Errorf("Failed to resolve project flock kandang %d: %+v", projectFlockKandangID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi transfer laying") } if err == nil && pfk != nil { category := strings.ToUpper(strings.TrimSpace(pfk.ProjectFlock.Category)) if category != strings.ToUpper(string(utils.ProjectFlockCategoryGrowing)) { return nil } } } checkExecuted := func(transfer *entity.LayingTransfer) bool { return transfer != nil && transfer.ExecutedAt != nil && !transfer.ExecutedAt.IsZero() } sourceTransfer, err := s.TransferLayingRepo.GetLatestApprovedBySourceKandang(ctx, projectFlockKandangID) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { s.Log.Errorf("Failed to resolve transfer laying by source kandang %d: %+v", projectFlockKandangID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi transfer laying") } if checkExecuted(sourceTransfer) { return fiber.NewError(fiber.StatusBadRequest, "Project flock kandang sudah dipindahkan ke laying") } return nil } func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]entity.ProjectChickin, error) { if err := s.Validate.Struct(req); err != nil { return nil, err } if err := s.ensureNotTransferred(c.Context(), req.ProjectFlockKandangId); err != nil { return nil, err } projectFlockKandang, err := s.ProjectflockKandangRepo.GetByID(c.Context(), req.ProjectFlockKandangId) if err != nil { return nil, fiber.NewError(fiber.StatusNotFound, "Project Flock Kandang not found") } warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectFlockKandang.KandangId) if err != nil { return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse for Kandang not found") } actorID, err := m.ActorIDFromContext(c) if err != nil { return nil, err } newChikins := make([]*entity.ProjectChickin, 0) chickinQtyMap := make(map[uint]float64) flockCategory := strings.ToUpper(strings.TrimSpace(projectFlockKandang.ProjectFlock.Category)) for idx, chickinReq := range req.ChickinRequests { productWarehouse, err := s.ProductWarehouseRepo.GetByID(c.Context(), chickinReq.ProductWarehouseId, func(db *gorm.DB) *gorm.DB { return db.Preload("Product.Flags") }) if err != nil { return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Product warehouse %d not found", chickinReq.ProductWarehouseId)) } if productWarehouse.WarehouseId != warehouse.Id { return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Product warehouse %d is not bound to kandang's warehouse", chickinReq.ProductWarehouseId)) } if productWarehouse.ProjectFlockKandangId != nil && *productWarehouse.ProjectFlockKandangId != req.ProjectFlockKandangId { return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Product warehouse %d belongs to different flock. Only product warehouses with project_flock_kandang_id = NULL or = %d can be used", chickinReq.ProductWarehouseId, req.ProjectFlockKandangId)) } if productWarehouse.Product.Id != 0 { if flockCategory != string(utils.ProjectFlockCategoryGrowing) && flockCategory != string(utils.ProjectFlockCategoryLaying) { return nil, fmt.Errorf("invalid flock category for chickin") } hasAyamFlag := false for _, flag := range productWarehouse.Product.Flags { if utils.CanonicalFlagType(flag.Name) == utils.FlagAyam { hasAyamFlag = true break } } if !hasAyamFlag { return nil, fmt.Errorf( "product warehouse %d cannot be used for %s chickin. Product must have AYAM flag (or legacy alias DOC/PULLET/LAYER) (product ID: %d, warehouse ID: %d)", chickinReq.ProductWarehouseId, projectFlockKandang.ProjectFlock.Category, productWarehouse.Product.Id, productWarehouse.Id, ) } } chickinDate, err := utils.ParseDateString(chickinReq.ChickInDate) if err != nil { return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid ChickInDate format for product warehouse %d", chickinReq.ProductWarehouseId)) } newChickin := &entity.ProjectChickin{ ProjectFlockKandangId: req.ProjectFlockKandangId, ChickInDate: chickinDate, UsageQty: 0, PendingUsageQty: 0, ProductWarehouseId: chickinReq.ProductWarehouseId, Notes: chickinReq.Note, CreatedBy: actorID, } newChikins = append(newChikins, newChickin) totalPopulationQty, err := s.ProjectflockPopulationRepo.GetTotalQtyByProductWarehouseID(c.Context(), chickinReq.ProductWarehouseId) if err != nil { return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get total population quantity for product warehouse %d", chickinReq.ProductWarehouseId)) } availableQty := productWarehouse.Quantity - totalPopulationQty if availableQty < 0 { availableQty = 0 } if flockCategory == strings.ToUpper(string(utils.ProjectFlockCategoryLaying)) { transferAvailable, err := s.resolveLayingTransferAvailableQty(c.Context(), nil, req.ProjectFlockKandangId, chickinReq.ProductWarehouseId) if err != nil { s.Log.Errorf("Failed to resolve laying transfer availability for pfk=%d pw=%d: %+v", req.ProjectFlockKandangId, chickinReq.ProductWarehouseId, err) return nil, fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi stok transfer laying") } if transferAvailable < 0 { transferAvailable = 0 } if transferAvailable < availableQty { availableQty = transferAvailable } } chickinQtyMap[uint(idx)] = availableQty } if len(newChikins) == 0 { return nil, fiber.NewError(fiber.StatusBadRequest, "No chickins to create") } err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error { if err := s.ensurePopulationRouteScope(c.Context(), dbTransaction); err != nil { return err } repositoryTx := repository.NewChickinRepository(dbTransaction) existingChikins, err := repositoryTx.GetByProjectFlockKandangIDForUpdate(c.Context(), req.ProjectFlockKandangId) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return fiber.NewError(fiber.StatusInternalServerError, "Failed to check existing chickins") } isFirstTime := len(existingChikins) == 0 pendingQtyMap := make(map[uint]float64) for _, existingChickin := range existingChikins { if existingChickin.PendingUsageQty > 0 { pendingQtyMap[existingChickin.ProductWarehouseId] += existingChickin.PendingUsageQty } } for idx, chickin := range newChikins { pendingQty := pendingQtyMap[chickin.ProductWarehouseId] desiredQty := chickinQtyMap[uint(idx)] availableQty := desiredQty - pendingQty if availableQty < 0 { availableQty = 0 } if availableQty <= 0 { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No available stock in product warehouse %d for chickin. Warehouse: %.0f, Pending: %.0f, Available: %.0f", chickin.ProductWarehouseId, desiredQty, pendingQty, availableQty)) } chickinQtyMap[uint(idx)] = availableQty pendingQtyMap[chickin.ProductWarehouseId] += availableQty } approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction)) if err := s.Repository.WithTx(dbTransaction).CreateMany(c.Context(), newChikins, nil); err != nil { return fiber.NewError(fiber.StatusInternalServerError, "Failed to create chickins") } for idx, chickin := range newChikins { desiredQty := chickinQtyMap[uint(idx)] if err := s.StageChickinStocks(c.Context(), dbTransaction, chickin, desiredQty, actorID); err != nil { return err } } latest, err := approvalSvcTx.LatestByTarget(c.Context(), utils.ApprovalWorkflowChickin, projectFlockKandang.Id, nil) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, "Failed to get latest approval") } var approvalAction entity.ApprovalAction if isFirstTime { approvalAction = entity.ApprovalActionCreated } else { approvalAction = entity.ApprovalActionUpdated } if latest == nil { if _, err := approvalSvcTx.CreateApproval( c.Context(), utils.ApprovalWorkflowChickin, projectFlockKandang.Id, utils.ChickinStepPengajuan, &approvalAction, actorID, nil); err != nil { if !errors.Is(err, gorm.ErrDuplicatedKey) { return fiber.NewError(fiber.StatusInternalServerError, "Failed to create approval") } } } else if latest.StepNumber != uint16(utils.ChickinStepPengajuan) { if _, err := approvalSvcTx.CreateApproval( c.Context(), utils.ApprovalWorkflowChickin, projectFlockKandang.Id, utils.ChickinStepPengajuan, &approvalAction, actorID, nil); err != nil { if !errors.Is(err, gorm.ErrDuplicatedKey) { return fiber.NewError(fiber.StatusInternalServerError, "Failed to create approval") } } } return nil }) if err != nil { if fiberErr, ok := err.(*fiber.Error); ok { return nil, fiberErr } return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to create chickins") } result := make([]entity.ProjectChickin, 0, len(newChikins)) for _, chickin := range newChikins { loaded, err := s.GetOne(c, chickin.Id) if err != nil { return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to reload chickin %d with relations: %v", chickin.Id, err)) } result = append(result, *loaded) } if len(result) == 0 { return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to load created chickins") } return result, nil } func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error) { if err := s.Validate.Struct(req); err != nil { return nil, err } chickin, err := s.Repository.GetByID(c.Context(), id, nil) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found") } return nil, err } if err := s.ensureNotTransferred(c.Context(), chickin.ProjectFlockKandangId); err != nil { return nil, err } updateBody := make(map[string]any) if req.ChickInDate != "" { updateBody["chick_in_date"] = req.ChickInDate } if req.Note != "" { updateBody["notes"] = req.Note } if len(updateBody) == 0 { return s.GetOne(c, id) } if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found") } return nil, err } updated, err := s.GetOne(c, id) if err != nil { return nil, err } if updated.UsageQty > 0 { if err := s.syncChickinTraceForProductWarehouse(c.Context(), nil, updated.ProductWarehouseId); err != nil { return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to sync chickin stock trace") } } return updated, nil } func (s chickinService) DeleteOne(c *fiber.Ctx, id uint) error { chickin, err := s.Repository.GetByID(c.Context(), id, nil) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return fiber.NewError(fiber.StatusNotFound, "Chickin not found") } return err } if err := s.ensureNotTransferred(c.Context(), chickin.ProjectFlockKandangId); err != nil { return err } if err := s.ensureNoExecutedTransferForDelete(c.Context(), chickin.ProjectFlockKandangId); err != nil { return err } actorID, err := m.ActorIDFromContext(c) if err != nil { return err } err = s.Repository.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error { if err := s.ensurePopulationRouteScope(c.Context(), tx); err != nil { return err } chickinRepoTx := repository.NewChickinRepository(tx) lockedChickin, err := chickinRepoTx.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB { return db.Clauses(clause.Locking{Strength: "UPDATE"}) }) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return fiber.NewError(fiber.StatusNotFound, "Chickin not found") } return err } consumeAllocBefore, traceAllocBefore, err := s.countActiveChickinAllocations(c.Context(), tx, lockedChickin.Id) if err != nil { return err } s.Log.Infof( "Delete chickin start id=%d usage=%.3f pending=%.3f active_consume_alloc=%d active_trace_alloc=%d", lockedChickin.Id, lockedChickin.UsageQty, lockedChickin.PendingUsageQty, consumeAllocBefore, traceAllocBefore, ) if err := s.ensureNoRelatedRecording(c.Context(), tx, lockedChickin); err != nil { return err } hasActiveConsumeAlloc, err := s.hasActiveChickinConsumeAllocations(c.Context(), tx, lockedChickin.Id) if err != nil { return err } if lockedChickin.UsageQty > 0 || lockedChickin.PendingUsageQty > 0 || hasActiveConsumeAlloc { if err := s.ReleaseChickinStocks(c.Context(), tx, lockedChickin, actorID); err != nil { return err } } if err := s.rollbackChickinPopulation(c.Context(), tx, lockedChickin.Id); err != nil { return err } if err := chickinRepoTx.DeleteOne(c.Context(), id); err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return fiber.NewError(fiber.StatusNotFound, "Chickin not found") } if isForeignKeyViolation(err) { return fiber.NewError(fiber.StatusBadRequest, chickinDeletePopulationGuardMessage) } return err } reflowAsOf := normalizeDateOnlyUTC(lockedChickin.ChickInDate) if reflowAsOf.IsZero() { reflowAsOf = time.Now().UTC() } if err := s.reflowWarehouseAfterChickinDelete(c.Context(), tx, lockedChickin.ProductWarehouseId, reflowAsOf); err != nil { return err } if err := s.syncChickinTraceForProductWarehouse(c.Context(), tx, lockedChickin.ProductWarehouseId); err != nil { return err } consumeAllocAfter, traceAllocAfter, err := s.countActiveChickinAllocations(c.Context(), tx, lockedChickin.Id) if err != nil { return err } s.Log.Infof( "Delete chickin complete id=%d active_consume_alloc=%d active_trace_alloc=%d", lockedChickin.Id, consumeAllocAfter, traceAllocAfter, ) return nil }) if err != nil { if fiberErr, ok := err.(*fiber.Error); ok { return fiberErr } return err } return nil } func (s chickinService) ensureNoExecutedTransferForDelete(ctx context.Context, projectFlockKandangID uint) error { if projectFlockKandangID == 0 || s.TransferLayingRepo == nil { return nil } // Delete guard by executed transfer hanya untuk kandang kategori growing. if s.ProjectflockKandangRepo != nil { pfk, err := s.ProjectflockKandangRepo.GetByIDLight(ctx, projectFlockKandangID) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { s.Log.Errorf("Failed to resolve project flock kandang %d for delete guard: %+v", projectFlockKandangID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi transfer laying") } if err == nil && pfk != nil { category := strings.ToUpper(strings.TrimSpace(pfk.ProjectFlock.Category)) if category != strings.ToUpper(string(utils.ProjectFlockCategoryGrowing)) { return nil } } } isExecuted := func(transfer *entity.LayingTransfer) bool { return transfer != nil && transfer.ExecutedAt != nil && !transfer.ExecutedAt.IsZero() } sourceTransfer, err := s.TransferLayingRepo.GetLatestApprovedBySourceKandang(ctx, projectFlockKandangID) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { s.Log.Errorf("Failed to resolve transfer laying by source kandang %d for delete guard: %+v", projectFlockKandangID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi transfer laying") } targetTransfer, err := s.TransferLayingRepo.GetLatestApprovedByTargetKandang(ctx, projectFlockKandangID) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { s.Log.Errorf("Failed to resolve transfer laying by target kandang %d for delete guard: %+v", projectFlockKandangID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi transfer laying") } if isExecuted(sourceTransfer) || isExecuted(targetTransfer) { return fiber.NewError( fiber.StatusBadRequest, "Chickin tidak dapat dihapus karena transfer laying sudah dieksekusi. Lakukan unexecute transfer terlebih dahulu", ) } return nil } func (s *chickinService) resolveLayingTransferAvailableQty(ctx context.Context, tx *gorm.DB, targetProjectFlockKandangID, productWarehouseID uint) (float64, error) { if targetProjectFlockKandangID == 0 || productWarehouseID == 0 { return 0, nil } db := s.Repository.DB().WithContext(ctx) if tx != nil { db = tx.WithContext(ctx) } var available float64 err := db.Table("laying_transfer_targets ltt"). Select("COALESCE(SUM(GREATEST(0, COALESCE(ltt.total_qty,0) - COALESCE(ltt.total_used,0))), 0) AS available"). Joins("JOIN laying_transfers lt ON lt.id = ltt.laying_transfer_id AND lt.deleted_at IS NULL"). Where("ltt.deleted_at IS NULL"). Where("ltt.target_project_flock_kandang_id = ?", targetProjectFlockKandangID). Where("ltt.product_warehouse_id = ?", productWarehouseID). Where("lt.executed_at IS NOT NULL"). Where(`( SELECT a.action FROM approvals a WHERE a.approvable_type = ? AND a.approvable_id = lt.id ORDER BY a.id DESC LIMIT 1 ) = ?`, string(utils.ApprovalWorkflowTransferToLaying), entity.ApprovalActionApproved). Scan(&available).Error if err != nil { return 0, err } return available, nil } func (s chickinService) ensurePopulationRouteScope(ctx context.Context, tx *gorm.DB) error { db := tx if db == nil { db = s.Repository.DB() } if db == nil { return nil } now := time.Now().UTC() result := db.WithContext(ctx). Table("fifo_stock_v2_route_rules"). Where("is_active = TRUE"). Where("lane = ?", "STOCKABLE"). Where("function_code = ?", "POPULATION_IN"). Where("source_table = ?", "project_flock_populations"). Where("(scope_sql IS NULL OR TRIM(scope_sql) = '')"). Updates(map[string]any{ "scope_sql": "deleted_at IS NULL", "updated_at": now, }) if result.Error != nil { s.Log.Errorf("Failed to enforce FIFO population route scope: %+v", result.Error) return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi konfigurasi FIFO chickin") } if result.RowsAffected > 0 { s.Log.Warnf( "Auto-fixed FIFO population route scope for chickin flow (rows=%d)", result.RowsAffected, ) } return nil } func (s *chickinService) ensureNoRelatedRecording(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin) error { if chickin == nil || chickin.ProjectFlockKandangId == 0 { return nil } db := s.Repository.DB().WithContext(ctx) if tx != nil { db = tx.WithContext(ctx) } recordDateFloor := normalizeDateOnlyUTC(chickin.ChickInDate) var earliest entity.Recording query := db.Model(&entity.Recording{}). Where("project_flock_kandangs_id = ?", chickin.ProjectFlockKandangId). Where("deleted_at IS NULL") if !recordDateFloor.IsZero() { query = query.Where("record_datetime >= ?", recordDateFloor) } if err := query.Order("record_datetime ASC, id ASC").Take(&earliest).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil } s.Log.Errorf("Failed to validate related recordings for chickin %d: %+v", chickin.Id, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi recording terkait chickin") } return fiber.NewError( fiber.StatusBadRequest, fmt.Sprintf( "%s (recording tanggal %s)", chickinDeleteRecordingGuardMessage, normalizeDateOnlyUTC(earliest.RecordDatetime).Format("2006-01-02"), ), ) } func (s *chickinService) hasActiveChickinConsumeAllocations(ctx context.Context, tx *gorm.DB, chickinID uint) (bool, error) { if tx == nil || chickinID == 0 { return false, nil } var count int64 if err := tx.WithContext(ctx). Model(&entity.StockAllocation{}). Where("usable_type = ? AND usable_id = ? AND status = ? AND allocation_purpose = ?", fifo.UsableKeyProjectChickin.String(), chickinID, entity.StockAllocationStatusActive, entity.StockAllocationPurposeConsume, ). Count(&count).Error; err != nil { return false, err } return count > 0, nil } func (s *chickinService) countActiveChickinAllocations(ctx context.Context, tx *gorm.DB, chickinID uint) (consume int64, trace int64, err error) { if tx == nil || chickinID == 0 { return 0, 0, nil } baseQuery := tx.WithContext(ctx).Model(&entity.StockAllocation{}). Where("usable_type = ? AND usable_id = ? AND status = ?", fifo.UsableKeyProjectChickin.String(), chickinID, entity.StockAllocationStatusActive, ) if err := baseQuery.Session(&gorm.Session{}). Where("allocation_purpose = ?", entity.StockAllocationPurposeConsume). Count(&consume).Error; err != nil { return 0, 0, err } if err := baseQuery.Session(&gorm.Session{}). Where("allocation_purpose = ?", entity.StockAllocationPurposeTraceChickin). Count(&trace).Error; err != nil { return 0, 0, err } return consume, trace, nil } func (s *chickinService) reflowWarehouseAfterChickinDelete(ctx context.Context, tx *gorm.DB, productWarehouseID uint, asOf time.Time) error { if tx == nil || productWarehouseID == 0 || s.FifoStockV2Svc == nil { return nil } if err := s.ensurePopulationRouteScope(ctx, tx); err != nil { return err } qtyBefore, hasQtyBefore := s.tryLoadWarehouseQty(ctx, tx, productWarehouseID) flagGroupCode, err := s.resolveFlagGroupByProductWarehouse(ctx, tx, productWarehouseID) if err != nil { s.Log.Errorf("Failed to resolve flag group for delete chickin reflow pw=%d: %+v", productWarehouseID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal sinkronisasi stok setelah delete chickin") } if strings.TrimSpace(flagGroupCode) == "" { return nil } result, err := s.FifoStockV2Svc.Reflow(ctx, commonSvc.FifoStockV2ReflowRequest{ FlagGroupCode: flagGroupCode, ProductWarehouseID: productWarehouseID, AsOf: &asOf, Tx: tx, }) if err != nil { s.Log.Errorf("Failed to reflow warehouse after delete chickin pw=%d: %+v", productWarehouseID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal sinkronisasi stok setelah delete chickin") } processedUsables := 0 rollbackQty := 0.0 allocateQty := 0.0 if result != nil { processedUsables = result.ProcessedUsables rollbackQty = result.Rollback.ReleasedQty allocateQty = result.Allocate.AllocatedQty } s.Log.Infof( "Delete chickin warehouse reflow pw=%d processed_usables=%d rollback_qty=%.3f allocate_qty=%.3f", productWarehouseID, processedUsables, rollbackQty, allocateQty, ) s.logWarehouseQtySnapshot( ctx, tx, productWarehouseID, "reflow_after_delete_chickin", 0, hasQtyBefore, qtyBefore, ) return nil } func (s *chickinService) rollbackChickinPopulation(ctx context.Context, tx *gorm.DB, chickinID uint) error { if tx == nil || chickinID == 0 { return nil } var populationIDs []uint if err := tx.WithContext(ctx). Model(&entity.ProjectFlockPopulation{}). Where("project_chickin_id = ?", chickinID). Pluck("id", &populationIDs).Error; err != nil { s.Log.Errorf("Failed to list population ids for chickin %d: %+v", chickinID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil population chickin") } if len(populationIDs) == 0 { return nil } now := time.Now().UTC() note := "delete chickin rollback population" releaseResult := tx.WithContext(ctx). Model(&entity.StockAllocation{}). Where("stockable_type = ? AND stockable_id IN ? AND status = ?", fifo.StockableKeyProjectFlockPopulation.String(), populationIDs, entity.StockAllocationStatusActive, ). Where("NOT (usable_type = ? AND usable_id = ?)", fifo.UsableKeyProjectChickin.String(), chickinID, ). Updates(map[string]any{ "status": entity.StockAllocationStatusReleased, "released_at": now, "note": note, }) if releaseResult.Error != nil { err := releaseResult.Error s.Log.Errorf("Failed to release population allocation for chickin %d: %+v", chickinID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal release alokasi population chickin") } if releaseResult.RowsAffected > 0 { s.Log.Infof( "Delete chickin rollback population id=%d released_population_alloc=%d", chickinID, releaseResult.RowsAffected, ) } deleteResult := tx.WithContext(ctx). Where("id IN ?", populationIDs). Delete(&entity.ProjectFlockPopulation{}) if deleteResult.Error != nil { err := deleteResult.Error s.Log.Errorf("Failed to delete populations for chickin %d: %+v", chickinID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal menghapus population chickin") } if deleteResult.RowsAffected > 0 { s.Log.Infof( "Delete chickin rollback population id=%d deleted_population=%d", chickinID, deleteResult.RowsAffected, ) } return nil } func isForeignKeyViolation(err error) bool { if err == nil { return false } var pgErr *pgconn.PgError if errors.As(err, &pgErr) { return pgErr.Code == "23503" } var pgErrV5 *pgconnv5.PgError if errors.As(err, &pgErrV5) { return pgErrV5.Code == "23503" } return false } func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entity.ProjectChickin, error) { if err := s.Validate.Struct(req); err != nil { return nil, err } actorID, err := m.ActorIDFromContext(c) if err != nil { return nil, err } approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(s.Repository.DB())) var action entity.ApprovalAction switch strings.ToUpper(strings.TrimSpace(req.Action)) { case string(entity.ApprovalActionRejected): action = entity.ApprovalActionRejected case string(entity.ApprovalActionApproved): action = entity.ApprovalActionApproved default: return nil, fiber.NewError(fiber.StatusBadRequest, "action must be APPROVED or REJECTED") } approvableIDs := utils.UniqueUintSlice(req.ApprovableIds) if len(approvableIDs) == 0 { return nil, fiber.NewError(fiber.StatusBadRequest, "approvable_ids must contain at least one id") } for _, id := range approvableIDs { if err := commonSvc.EnsureRelations(c.Context(), commonSvc.RelationCheck{Name: "ProjectFlockKandang", ID: &id, Exists: s.ProjectflockKandangRepo.IdExists}); err != nil { return nil, err } if err := s.ensureNotTransferred(c.Context(), id); err != nil { return nil, err } latestApproval, err := approvalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowChickin, id, nil) if err != nil { return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status") } if latestApproval == nil { return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No approval found for ProjectFlockKandang %d - chickins must be created first", id)) } if latestApproval.StepNumber != uint16(utils.ChickinStepPengajuan) { return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("ProjectFlockKandang %d cannot be approved - current status is not in PENGAJUAN stage", id)) } } step := utils.ChickinStepPengajuan if action == entity.ApprovalActionApproved { step = utils.ChickinStepDisetujui } err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error { if err := s.ensurePopulationRouteScope(c.Context(), dbTransaction); err != nil { return err } approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction)) chickinRepoTx := repository.NewChickinRepository(dbTransaction) ProjectFlockPopulationRepotx := s.ProjectflockPopulationRepo.WithTx(dbTransaction) touchedProductWarehouseIDs := make(map[uint]struct{}) for _, approvableID := range approvableIDs { // Re-check latest approval inside transaction to prevent double-approve races. var latest entity.Approval if err := dbTransaction.WithContext(c.Context()). Table("approvals"). Where("approvable_type = ? AND approvable_id = ?", utils.ApprovalWorkflowChickin.String(), approvableID). Order("id DESC"). Limit(1). Clauses(clause.Locking{Strength: "UPDATE"}). Take(&latest).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return fiber.NewError(fiber.StatusInternalServerError, "Failed to recheck approval status") } if latest.Id != 0 && latest.StepNumber != uint16(utils.ChickinStepPengajuan) { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("ProjectFlockKandang %d sudah tidak berada di tahap PENGAJUAN", approvableID)) } if _, err := approvalSvc.CreateApproval( c.Context(), utils.ApprovalWorkflowChickin, approvableID, step, &action, actorID, req.Notes, ); err != nil { return fiber.NewError(fiber.StatusInternalServerError, "Failed to create approval") } if action == entity.ApprovalActionApproved { chickins, err := chickinRepoTx.GetByProjectFlockKandangID(c.Context(), approvableID) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get chickins for approval %d", approvableID)) } kandangForApproval, err := s.ProjectflockKandangRepo.GetByID(c.Context(), approvableID) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("ProjectFlockKandang %d not found", approvableID)) } return fiber.NewError(fiber.StatusInternalServerError, "Failed to get ProjectFlockKandang") } category := strings.ToUpper(strings.TrimSpace(kandangForApproval.ProjectFlock.Category)) var targetFlag utils.FlagType if category == string(utils.ProjectFlockCategoryGrowing) { targetFlag = utils.FlagPullet } else if category == string(utils.ProjectFlockCategoryLaying) { targetFlag = utils.FlagLayer } else { continue } for _, chickin := range chickins { approvedQty := chickin.UsageQty if approvedQty <= 0 { approvedQty = chickin.PendingUsageQty } if approvedQty < 0 { approvedQty = 0 } if err := s.ConsumeChickinStocks(c.Context(), dbTransaction, &chickin, approvedQty, actorID); err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to finalize usage qty for chickin %d", chickin.Id)) } chickin.UsageQty = approvedQty chickin.PendingUsageQty = 0 touchedProductWarehouseIDs[chickin.ProductWarehouseId] = struct{}{} populationExists, err := ProjectFlockPopulationRepotx.ExistsByProjectChickinID(c.Context(), chickin.Id) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to check population for chickin %d", chickin.Id)) } if populationExists { continue } sourcePW, err := s.ProductWarehouseRepo.GetByID(c.Context(), chickin.ProductWarehouseId, func(db *gorm.DB) *gorm.DB { return db.Preload("Product.Flags") }) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get product warehouse for chickin %d", chickin.Id)) } if err := s.autoAddFlagToProduct(c.Context(), dbTransaction, sourcePW.Product.Id, targetFlag); err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to auto-add flag to product %d", sourcePW.Product.Id)) } population := &entity.ProjectFlockPopulation{ ProjectChickinId: chickin.Id, ProductWarehouseId: sourcePW.Id, TotalQty: 0, TotalUsedQty: 0, Notes: chickin.Notes, CreatedBy: actorID, } if err := ProjectFlockPopulationRepotx.CreateOne(c.Context(), population, nil); err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to create population for chickin %d", chickin.Id)) } if err := s.ReplenishChickinStocks(c.Context(), dbTransaction, &chickin, sourcePW, population, actorID); err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to replenish stock for chickin %d", chickin.Id)) } } } if action == entity.ApprovalActionRejected { chickins, err := chickinRepoTx.GetByProjectFlockKandangID(c.Context(), approvableID) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get pending chickins for rejection %d", approvableID)) } if len(chickins) == 0 { continue } for _, chickin := range chickins { populationExists, err := ProjectFlockPopulationRepotx.ExistsByProjectChickinID(c.Context(), chickin.Id) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to check population for chickin %d", chickin.Id)) } if populationExists { continue } if chickin.UsageQty <= 0 && chickin.PendingUsageQty <= 0 { continue } if err := s.ReleaseChickinStocks(c.Context(), dbTransaction, &chickin, actorID); err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to release stock for rejected chickin %d: %v", chickin.Id, err)) } touchedProductWarehouseIDs[chickin.ProductWarehouseId] = struct{}{} if err := chickinRepoTx.DeleteOne(c.Context(), chickin.Id); err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to delete rejected chickin %d", chickin.Id)) } } } } } for productWarehouseID := range touchedProductWarehouseIDs { if err := s.syncChickinTraceForProductWarehouse(c.Context(), dbTransaction, productWarehouseID); err != nil { return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to sync chickin trace for product warehouse %d", productWarehouseID)) } } return nil }) if err != nil { if fiberErr, ok := err.(*fiber.Error); ok { return nil, fiberErr } return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to record approval") } updated := make([]entity.ProjectChickin, 0) for _, kandangID := range approvableIDs { var chickins []entity.ProjectChickin if err := s.Repository.DB().WithContext(c.Context()).Where("project_flock_kandang_id = ?", kandangID).Find(&chickins).Error; err != nil { return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to load approved chickins") } updated = append(updated, chickins...) } return updated, nil } func (s *chickinService) autoAddFlagToProduct(ctx context.Context, tx *gorm.DB, productID uint, targetFlag utils.FlagType) error { if s.ProductRepo == nil { return nil } currentFlags, err := s.ProductRepo.GetFlags(ctx, productID) if err != nil { return fmt.Errorf("failed to get product flags: %w", err) } hasTargetFlag := false currentFlagNames := make([]string, 0, len(currentFlags)) for _, flag := range currentFlags { currentFlagNames = append(currentFlagNames, flag.Name) if flag.Name == string(targetFlag) { hasTargetFlag = true } } if hasTargetFlag { return nil } newFlags := append(currentFlagNames, string(targetFlag)) if err := s.ProductRepo.SyncFlags(ctx, tx, productID, newFlags); err != nil { return fmt.Errorf("failed to sync flags: %w", err) } return nil } func (s *chickinService) ConsumeChickinStocks(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin, desiredQty float64, actorID uint) error { if chickin == nil { return nil } if tx == nil { return errors.New("transaction is required") } if desiredQty < 0 { return errors.New("desired quantity must be zero or greater") } return s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, desiredQty, 0) } func (s *chickinService) StageChickinStocks(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin, desiredQty float64, actorID uint) error { if chickin == nil { return nil } if tx == nil { return errors.New("transaction is required") } if desiredQty < 0 { return errors.New("desired quantity must be zero or greater") } return s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, 0, desiredQty) } func (s *chickinService) ReplenishChickinStocks(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin, targetPW *entity.ProductWarehouse, population *entity.ProjectFlockPopulation, actorID uint) error { if chickin == nil || targetPW == nil || population == nil { return nil } if tx == nil { return errors.New("transaction is required") } if s.FifoStockV2Svc == nil { return errors.New("fifo v2 service is not available") } if err := tx.WithContext(ctx). Model(&entity.ProjectFlockPopulation{}). Where("id = ?", population.Id). Update("total_qty", chickin.UsageQty).Error; err != nil { return err } asOf := chickin.ChickInDate if asOf.IsZero() { asOf = chickin.CreatedAt } if err := s.ensurePopulationRouteScope(ctx, tx); err != nil { return err } return reflowChickinScope(ctx, s.FifoStockV2Svc, tx, targetPW.Id, &asOf) } func (s *chickinService) ReleaseChickinStocks(ctx context.Context, tx *gorm.DB, chickin *entity.ProjectChickin, actorID uint) error { if chickin == nil { return nil } if tx == nil { return errors.New("transaction is required") } if chickin.ProductWarehouseId == 0 { return s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, 0, 0) } qtyBefore, hasQtyBefore := s.tryLoadWarehouseQty(ctx, tx, chickin.ProductWarehouseId) var activeConsumeCount int64 if err := tx.WithContext(ctx). Model(&entity.StockAllocation{}). Where("usable_type = ? AND usable_id = ? AND status = ? AND allocation_purpose = ?", fifo.UsableKeyProjectChickin.String(), chickin.Id, entity.StockAllocationStatusActive, entity.StockAllocationPurposeConsume, ). Count(&activeConsumeCount).Error; err != nil { return err } if activeConsumeCount == 0 || s.FifoStockV2Svc == nil { s.Log.Infof( "Release chickin stock fallback id=%d active_consume_alloc=%d fifo_available=%t", chickin.Id, activeConsumeCount, s.FifoStockV2Svc != nil, ) if err := s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, 0, 0); err != nil { return err } s.logWarehouseQtySnapshot( ctx, tx, chickin.ProductWarehouseId, "release_chickin_fallback_no_active_alloc", chickin.Id, hasQtyBefore, qtyBefore, ) return nil } shouldRestoreWarehouseQty := true if s.ProjectflockKandangRepo != nil && chickin.ProjectFlockKandangId != 0 { pfk, err := s.ProjectflockKandangRepo.GetByIDLight(ctx, chickin.ProjectFlockKandangId) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return err } if err == nil && pfk != nil { category := strings.ToUpper(strings.TrimSpace(pfk.ProjectFlock.Category)) if category == strings.ToUpper(string(utils.ProjectFlockCategoryLaying)) { shouldRestoreWarehouseQty = false } } } if !shouldRestoreWarehouseQty { var affectedTransferTargetIDs []uint if err := tx.WithContext(ctx). Model(&entity.StockAllocation{}). Where("usable_type = ? AND usable_id = ? AND status = ? AND allocation_purpose = ? AND stockable_type = ?", fifo.UsableKeyProjectChickin.String(), chickin.Id, entity.StockAllocationStatusActive, entity.StockAllocationPurposeConsume, fifo.StockableKeyTransferToLayingIn.String(), ). Pluck("stockable_id", &affectedTransferTargetIDs).Error; err != nil { return err } now := time.Now().UTC() releaseResult := tx.WithContext(ctx). Model(&entity.StockAllocation{}). Where("usable_type = ? AND usable_id = ? AND status = ? AND allocation_purpose = ?", fifo.UsableKeyProjectChickin.String(), chickin.Id, entity.StockAllocationStatusActive, entity.StockAllocationPurposeConsume, ). Updates(map[string]any{ "status": entity.StockAllocationStatusReleased, "released_at": now, "note": "chickin rollback without qty adjust", }) if releaseResult.Error != nil { err := releaseResult.Error return err } s.Log.Infof( "Release chickin stock laying id=%d released_consume_alloc=%d transfer_targets=%d", chickin.Id, releaseResult.RowsAffected, len(affectedTransferTargetIDs), ) if err := s.resyncTransferTargetUsageFromAllocations(ctx, tx, affectedTransferTargetIDs); err != nil { return err } if err := s.Repository.UpdateUsageFields(ctx, tx, chickin.Id, 0, 0); err != nil { return err } s.logWarehouseQtySnapshot( ctx, tx, chickin.ProductWarehouseId, "release_chickin_laying_no_restore", chickin.Id, hasQtyBefore, qtyBefore, ) return nil } rollbackResult, err := s.FifoStockV2Svc.Rollback(ctx, commonSvc.FifoStockV2RollbackRequest{ ProductWarehouseID: chickin.ProductWarehouseId, Usable: commonSvc.FifoStockV2Ref{ ID: chickin.Id, LegacyTypeKey: fifo.UsableKeyProjectChickin.String(), FunctionCode: "CHICKIN_OUT", }, Reason: "delete/reject chickin rollback", Tx: tx, }) if err != nil { s.Log.Errorf("Failed to rollback FIFO v2 for chickin %d: %+v", chickin.Id, err) return err } releasedQty := 0.0 detailCount := 0 if rollbackResult != nil { releasedQty = rollbackResult.ReleasedQty detailCount = len(rollbackResult.Details) } s.Log.Infof( "Release chickin stock fifo rollback id=%d released_qty=%.3f detail_count=%d", chickin.Id, releasedQty, detailCount, ) s.logWarehouseQtySnapshot( ctx, tx, chickin.ProductWarehouseId, "release_chickin_fifo_rollback", chickin.Id, hasQtyBefore, qtyBefore, ) return nil } func (s *chickinService) tryLoadWarehouseQty(ctx context.Context, tx *gorm.DB, productWarehouseID uint) (float64, bool) { if tx == nil || productWarehouseID == 0 { return 0, false } type row struct { Qty float64 `gorm:"column:qty"` } out := row{} if err := tx.WithContext(ctx). Table("product_warehouses"). Select("COALESCE(qty, 0) AS qty"). Where("id = ?", productWarehouseID). Take(&out).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return 0, false } errText := strings.ToLower(strings.TrimSpace(err.Error())) if strings.Contains(errText, "no such column") && strings.Contains(errText, "qty") { return 0, false } if strings.Contains(errText, "column") && strings.Contains(errText, "qty") && strings.Contains(errText, "does not exist") { return 0, false } s.Log.Warnf("Failed to load warehouse qty snapshot pw=%d: %+v", productWarehouseID, err) return 0, false } return out.Qty, true } func (s *chickinService) logWarehouseQtySnapshot( ctx context.Context, tx *gorm.DB, productWarehouseID uint, stage string, chickinID uint, hasBefore bool, beforeQty float64, ) { afterQty, hasAfter := s.tryLoadWarehouseQty(ctx, tx, productWarehouseID) if !hasBefore && !hasAfter { return } if hasBefore && hasAfter { s.Log.Infof( "Warehouse qty snapshot stage=%s chickin_id=%d pw=%d before=%.3f after=%.3f delta=%.3f", stage, chickinID, productWarehouseID, beforeQty, afterQty, afterQty-beforeQty, ) return } if hasAfter { s.Log.Infof( "Warehouse qty snapshot stage=%s chickin_id=%d pw=%d after=%.3f", stage, chickinID, productWarehouseID, afterQty, ) return } s.Log.Infof( "Warehouse qty snapshot stage=%s chickin_id=%d pw=%d before=%.3f", stage, chickinID, productWarehouseID, beforeQty, ) } func (s *chickinService) resyncTransferTargetUsageFromAllocations(ctx context.Context, tx *gorm.DB, transferTargetIDs []uint) error { if tx == nil || len(transferTargetIDs) == 0 { return nil } unique := make([]uint, 0, len(transferTargetIDs)) seen := make(map[uint]struct{}, len(transferTargetIDs)) for _, id := range transferTargetIDs { if id == 0 { continue } if _, ok := seen[id]; ok { continue } seen[id] = struct{}{} unique = append(unique, id) } if len(unique) == 0 { return nil } if err := tx.WithContext(ctx). Model(&entity.LayingTransferTarget{}). Where("id IN ?", unique). Update("total_used", 0).Error; err != nil { return err } type usageRow struct { StockableID uint `gorm:"column:stockable_id"` Used float64 `gorm:"column:used"` } var usageRows []usageRow if err := tx.WithContext(ctx). Table("stock_allocations"). Select("stockable_id, COALESCE(SUM(qty), 0) AS used"). Where("stockable_type = ?", fifo.StockableKeyTransferToLayingIn.String()). Where("status = ?", entity.StockAllocationStatusActive). Where("allocation_purpose = ?", entity.StockAllocationPurposeConsume). Where("stockable_id IN ?", unique). Group("stockable_id"). Scan(&usageRows).Error; err != nil { return err } for _, row := range usageRows { if err := tx.WithContext(ctx). Model(&entity.LayingTransferTarget{}). Where("id = ?", row.StockableID). Update("total_used", row.Used).Error; err != nil { return err } } return nil } func normalizeDateOnlyUTC(value time.Time) time.Time { if value.IsZero() { return time.Time{} } return time.Date(value.UTC().Year(), value.UTC().Month(), value.UTC().Day(), 0, 0, 0, 0, time.UTC) } func (s *chickinService) syncChickinTraceForProductWarehouse(ctx context.Context, tx *gorm.DB, productWarehouseID uint) error { if productWarehouseID == 0 { return nil } if s.FifoStockV2Svc == nil { return nil } if tx == nil { return s.Repository.DB().WithContext(ctx).Transaction(func(innerTx *gorm.DB) error { return s.syncChickinTraceForProductWarehouse(ctx, innerTx, productWarehouseID) }) } if err := s.ensurePopulationRouteScope(ctx, tx); err != nil { return err } now := time.Now() if err := tx.WithContext(ctx). Table("stock_allocations"). Where("product_warehouse_id = ?", productWarehouseID). Where("usable_type = ?", fifo.UsableKeyProjectChickin.String()). Where("allocation_purpose = ?", entity.StockAllocationPurposeTraceChickin). Where("status = ?", entity.StockAllocationStatusActive). Updates(map[string]any{ "status": entity.StockAllocationStatusReleased, "released_at": now, "updated_at": now, "note": "chickin_trace_reflow_reset", }).Error; err != nil { return err } flagGroupCode, err := s.resolveFlagGroupByProductWarehouse(ctx, tx, productWarehouseID) if err != nil { return err } if strings.TrimSpace(flagGroupCode) == "" { return nil } type chickinTraceRow struct { ID uint `gorm:"column:id"` UsageQty float64 `gorm:"column:usage_qty"` ChickIn time.Time `gorm:"column:chick_in_date"` } chickins := make([]chickinTraceRow, 0) if err := tx.WithContext(ctx). Table("project_chickins"). Select("id, usage_qty, chick_in_date"). Where("product_warehouse_id = ?", productWarehouseID). Where("deleted_at IS NULL"). Where("usage_qty > 0"). Order("chick_in_date ASC, id ASC"). Scan(&chickins).Error; err != nil { return err } if len(chickins) == 0 { return nil } gatherRows, err := s.FifoStockV2Svc.Gather(ctx, commonSvc.FifoStockV2GatherRequest{ FlagGroupCode: flagGroupCode, Lane: "STOCKABLE", ProductWarehouseID: productWarehouseID, Limit: 50000, Tx: tx, }) if err != nil { return err } if len(gatherRows) == 0 { return nil } type lotKey struct { StockableType string StockableID uint } remainingByLot := make(map[lotKey]float64, len(gatherRows)) for _, row := range gatherRows { key := lotKey{StockableType: row.Ref.LegacyTypeKey, StockableID: row.Ref.ID} remainingByLot[key] = row.AvailableQuantity } lotIndex := 0 traceNow := time.Now() for _, chickin := range chickins { remaining := chickin.UsageQty for remaining > 1e-6 && lotIndex < len(gatherRows) { lot := gatherRows[lotIndex] key := lotKey{StockableType: lot.Ref.LegacyTypeKey, StockableID: lot.Ref.ID} available := remainingByLot[key] if available <= 1e-6 { lotIndex++ continue } portion := math.Min(remaining, available) if portion <= 1e-6 { lotIndex++ continue } insert := map[string]any{ "product_warehouse_id": productWarehouseID, "stockable_type": lot.Ref.LegacyTypeKey, "stockable_id": lot.Ref.ID, "usable_type": fifo.UsableKeyProjectChickin.String(), "usable_id": chickin.ID, "qty": portion, "status": entity.StockAllocationStatusActive, "allocation_purpose": entity.StockAllocationPurposeTraceChickin, "engine_version": "v2", "flag_group_code": flagGroupCode, "function_code": "CHICKIN_TRACE", "created_at": traceNow, "updated_at": traceNow, } if err := tx.WithContext(ctx).Table("stock_allocations").Create(insert).Error; err != nil { return err } remaining -= portion remainingByLot[key] = available - portion } if remaining > 1e-6 { s.Log.Warnf( "chickin trace partial allocation for product_warehouse_id=%d chickin_id=%d: remaining=%.3f", productWarehouseID, chickin.ID, remaining, ) } } return nil } func (s *chickinService) resolveFlagGroupByProductWarehouse(ctx context.Context, tx *gorm.DB, productWarehouseID uint) (string, error) { type row struct { FlagGroupCode string `gorm:"column:flag_group_code"` } selected := row{} err := tx.WithContext(ctx). Table("fifo_stock_v2_route_rules rr"). Select("rr.flag_group_code"). Joins("JOIN fifo_stock_v2_flag_groups fg ON fg.code = rr.flag_group_code AND fg.is_active = TRUE"). Where("rr.is_active = TRUE"). Where("rr.lane = 'STOCKABLE'"). Where(` EXISTS ( SELECT 1 FROM product_warehouses pw JOIN flags f ON f.flagable_id = pw.product_id JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.is_active = TRUE WHERE pw.id = ? AND f.flagable_type = ? AND fm.flag_group_code = rr.flag_group_code ) `, productWarehouseID, entity.FlagableTypeProduct). Order("fg.priority ASC, rr.id ASC"). Limit(1). Take(&selected).Error if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return "", nil } return "", err } return selected.FlagGroupCode, nil } func (s chickinService) EnsureChickInExists(ctx context.Context, projectFlockKandangID uint) error { if projectFlockKandangID == 0 { return fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid") } populations, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(ctx, projectFlockKandangID) if err != nil { s.Log.Errorf("Failed to check project flock population for project_flock_kandang_id=%d: %+v", projectFlockKandangID, err) return fiber.NewError(fiber.StatusInternalServerError, "Gagal memeriksa data chick in") } if len(populations) == 0 { return fiber.NewError(fiber.StatusBadRequest, "Project flock belum memiliki chick in yang disetujui sehingga belum dapat membuat recording") } for _, population := range populations { if population.TotalQty > 0 { return nil } } return fiber.NewError(fiber.StatusBadRequest, "Chick in project flock belum disetujui sehingga belum dapat membuat recording") }