mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
fix[BE]: fix logic pengambilan quatity untuk chick in dan penggunaan helper common
This commit is contained in:
@@ -132,27 +132,33 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
s.Log.Errorf("Failed to get project flock: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Project Flock not found")
|
||||
}
|
||||
|
||||
var productWarehouse entity.ProductWarehouse
|
||||
var productWarehouses []entity.ProductWarehouse
|
||||
err = s.ProductWarehouseRepo.DB().
|
||||
WithContext(c.Context()).
|
||||
Joins("JOIN products ON products.id = product_warehouses.product_id").
|
||||
Joins("JOIN product_categories ON product_categories.id = products.product_category_id").
|
||||
Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", projectFlock.ProductCategory.Code, warehouse.Id).
|
||||
Order("created_at DESC").
|
||||
First(&productWarehouse).Error
|
||||
Find(&productWarehouses).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Product Warehouse not found for the given Project Flock and Warehouse")
|
||||
}
|
||||
s.Log.Errorf("Failed to get product warehouse: %+v", err)
|
||||
s.Log.Errorf("Failed to get product warehouses: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if productWarehouse.Quantity < 1 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Insufficient product quantity in warehouse")
|
||||
if len(productWarehouses) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Product Warehouse not found for the given Project Flock and Warehouse")
|
||||
}
|
||||
|
||||
// Jumlahkan semua quantity DOC
|
||||
totalQuantity := 0.0
|
||||
for _, pw := range productWarehouses {
|
||||
totalQuantity += pw.Quantity
|
||||
}
|
||||
|
||||
if totalQuantity < 1 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Insufficient quantity in Product Warehouses")
|
||||
}
|
||||
|
||||
// Buat satu chickin dengan total quantity
|
||||
chickinDate, err := utils.ParseDateString(req.ChickInDate)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to parse chickin date: %+v", err)
|
||||
@@ -161,9 +167,9 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
newChickin := &entity.ProjectChickin{
|
||||
ProjectFlockKandangId: projectflockkandang.ProjectFlockId,
|
||||
ChickInDate: chickinDate,
|
||||
Quantity: productWarehouse.Quantity,
|
||||
Quantity: totalQuantity,
|
||||
Note: "",
|
||||
CreatedBy: 1, //todo: ganti dengan
|
||||
CreatedBy: 1, //todo: ganti dengan user login
|
||||
}
|
||||
err = s.Repository.CreateOne(c.Context(), newChickin, nil)
|
||||
if err != nil {
|
||||
@@ -171,16 +177,15 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updatedQuantity := productWarehouse.Quantity - newChickin.Quantity
|
||||
if updatedQuantity < 0 {
|
||||
updatedQuantity = 0
|
||||
}
|
||||
err = s.ProductWarehouseRepo.PatchOne(c.Context(), productWarehouse.Id, map[string]any{
|
||||
"quantity": updatedQuantity,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update product warehouse quantity: %+v", err)
|
||||
return nil, err
|
||||
// Update semua product warehouse: set quantity jadi 0
|
||||
for _, pw := range productWarehouses {
|
||||
err = s.ProductWarehouseRepo.PatchOne(c.Context(), pw.Id, map[string]any{
|
||||
"quantity": 0,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update product warehouse quantity: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
existingPopulation, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), req.ProjectFlockKandangId)
|
||||
|
||||
Reference in New Issue
Block a user