mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat(BE): enhance chickin and transfer laying services with product warehouse validation and stockable support
This commit is contained in:
@@ -112,7 +112,6 @@ func (s chickinService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectChickin, e
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get chickin by id: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
return chickin, nil
|
||||
@@ -143,7 +142,9 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]enti
|
||||
|
||||
for idx, chickinReq := range req.ChickinRequests {
|
||||
|
||||
productWarehouse, err := s.ProductWarehouseRepo.GetByID(c.Context(), chickinReq.ProductWarehouseId, nil)
|
||||
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))
|
||||
}
|
||||
@@ -156,22 +157,27 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]enti
|
||||
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))
|
||||
}
|
||||
|
||||
// CRITICAL: Validate product category based on flock category
|
||||
// GROWING: Input DOC, Output PULLET
|
||||
// LAYING: Input PULLET, Output LAYER
|
||||
if productWarehouse.Product.Id != 0 {
|
||||
productCategoryCode := productWarehouse.Product.ProductCategory.Code
|
||||
var allowedCategory string
|
||||
|
||||
var requiredFlag utils.FlagType
|
||||
if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryGrowing) {
|
||||
allowedCategory = "DOC"
|
||||
requiredFlag = utils.FlagDOC
|
||||
} else if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryLaying) {
|
||||
allowedCategory = "PULLET"
|
||||
requiredFlag = utils.FlagPullet
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid flock category for chickin")
|
||||
}
|
||||
|
||||
if productCategoryCode != allowedCategory {
|
||||
return nil, fmt.Errorf("product warehouse %d cannot be used for %s chickin. Only %s products can be used as input (current: %s)", chickinReq.ProductWarehouseId, projectFlockKandang.ProjectFlock.Category, allowedCategory, productCategoryCode)
|
||||
hasRequiredFlag := false
|
||||
for _, flag := range productWarehouse.Product.Flags {
|
||||
if utils.FlagType(flag.Name) == requiredFlag {
|
||||
hasRequiredFlag = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !hasRequiredFlag {
|
||||
return nil, fmt.Errorf("product warehouse %d cannot be used for %s chickin. Product must have %s flag (product ID: %d, warehouse ID: %d)", chickinReq.ProductWarehouseId, projectFlockKandang.ProjectFlock.Category, requiredFlag, productWarehouse.Product.Id, productWarehouse.Id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,7 +197,18 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]enti
|
||||
}
|
||||
|
||||
newChikins = append(newChikins, newChickin)
|
||||
chickinQtyMap[uint(idx)] = productWarehouse.Quantity
|
||||
|
||||
totalPopulationQty, err := s.ProjectflockPopulationRepo.GetTotalQtyByProjectFlockKandangID(c.Context(), req.ProjectFlockKandangId)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get total population quantity for project_flock_kandang %d", req.ProjectFlockKandangId))
|
||||
}
|
||||
|
||||
availableQty := productWarehouse.Quantity - totalPopulationQty
|
||||
if availableQty < 0 {
|
||||
availableQty = 0
|
||||
}
|
||||
|
||||
chickinQtyMap[uint(idx)] = availableQty
|
||||
}
|
||||
|
||||
if len(newChikins) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user