codex/fix: hidden product warehouse depletion and egg <= 0

This commit is contained in:
Adnan Zahir
2026-04-06 22:27:58 +07:00
committed by giovanni
parent 491fe0abef
commit e6dc658046
5 changed files with 154 additions and 10 deletions
@@ -87,6 +87,25 @@ func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository
}
}
func resolveWarehouseForProjectFlockKandang(ctx context.Context, warehouseRepo rWarehouse.WarehouseRepository, pfk *entity.ProjectFlockKandang) (*entity.Warehouse, error) {
if warehouseRepo == nil || pfk == nil {
return nil, gorm.ErrRecordNotFound
}
if pfk.KandangId == 0 {
return nil, gorm.ErrRecordNotFound
}
if pfk.ProjectFlock.LocationId != 0 {
warehouse, err := warehouseRepo.GetByKandangIDAndLocationID(ctx, pfk.KandangId, pfk.ProjectFlock.LocationId)
if err == nil {
return warehouse, nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
}
return warehouseRepo.GetByKandangID(ctx, pfk.KandangId)
}
func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
@@ -241,7 +260,7 @@ func (s projectFlockKandangService) getAvailableQuantities(c *fiber.Ctx, project
return nil, nil
}
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectFlockKandang.Kandang.Id)
warehouse, err := resolveWarehouseForProjectFlockKandang(c.Context(), s.WarehouseRepo, projectFlockKandang)
if err != nil || warehouse == nil {
return nil, nil
}
@@ -300,7 +319,7 @@ func (s projectFlockKandangService) CheckClosing(c *fiber.Ctx, id uint) (*Closin
stockRemain := make([]StockRemainingDetail, 0)
if s.WarehouseRepo != nil && s.ProductWarehouseRepo != nil {
warehouse, werr := s.WarehouseRepo.GetByKandangID(c.Context(), pfk.KandangId)
warehouse, werr := resolveWarehouseForProjectFlockKandang(c.Context(), s.WarehouseRepo, pfk)
if werr != nil {
return nil, werr
}
@@ -464,7 +483,7 @@ func (s projectFlockKandangService) Closing(c *fiber.Ctx, id uint, req *validati
}
if s.WarehouseRepo != nil && s.ProductWarehouseRepo != nil {
warehouse, werr := s.WarehouseRepo.GetByKandangID(c.Context(), pfk.KandangId)
warehouse, werr := resolveWarehouseForProjectFlockKandang(c.Context(), s.WarehouseRepo, pfk)
if werr != nil {
return nil, werr
}