mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat(BE-74-76-78-278):adjustment project flock,recording,purchase getall
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
|
||||
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
||||
uniformityRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
|
||||
purchaseRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
||||
utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||
|
||||
@@ -308,12 +309,12 @@ func (s *projectflockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*
|
||||
}
|
||||
|
||||
createBody := &entity.ProjectFlock{
|
||||
AreaId: req.AreaId,
|
||||
Category: cat,
|
||||
FcrId: req.FcrId,
|
||||
AreaId: req.AreaId,
|
||||
Category: cat,
|
||||
FcrId: req.FcrId,
|
||||
ProductionStandardId: req.ProductionStandardId,
|
||||
LocationId: req.LocationId,
|
||||
CreatedBy: actorID,
|
||||
LocationId: req.LocationId,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
@@ -823,22 +824,7 @@ func (s projectflockService) detachKandangs(ctx context.Context, dbTransaction *
|
||||
return nil
|
||||
}
|
||||
|
||||
blocked, err := s.pivotRepoWithTx(dbTransaction).FindKandangsWithRecordings(ctx, projectFlockID, kandangIDs)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to check recordings before detaching kandangs: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to validate kandang detachment")
|
||||
}
|
||||
if len(blocked) > 0 {
|
||||
names := make([]string, 0, len(blocked))
|
||||
for _, item := range blocked {
|
||||
label := fmt.Sprintf("ID %d", item.Id)
|
||||
if strings.TrimSpace(item.Name) != "" {
|
||||
label = fmt.Sprintf("%s (%s)", label, item.Name)
|
||||
}
|
||||
names = append(names, label)
|
||||
}
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Tidak dapat melepas kandang karena sudah memiliki recording: %s", strings.Join(names, ", ")))
|
||||
}
|
||||
// NOTE: Recording constraints are enforced via FK cascade; allow detachment even if recordings exist.
|
||||
|
||||
pfkIDs, err := s.pivotRepoWithTx(dbTransaction).ListIDsByProjectAndKandang(ctx, projectFlockID, kandangIDs)
|
||||
if err != nil {
|
||||
@@ -854,6 +840,14 @@ func (s projectflockService) detachKandangs(ctx context.Context, dbTransaction *
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to remove uniformity data for project flock kandang")
|
||||
}
|
||||
|
||||
db := s.Repository.DB()
|
||||
if dbTransaction != nil {
|
||||
db = dbTransaction
|
||||
}
|
||||
purchaseRepo := purchaseRepository.NewPurchaseRepository(db)
|
||||
if err := purchaseRepo.SoftDeleteByProjectFlockKandangIDs(ctx, pfkIDs); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to soft delete purchases for project flock kandang")
|
||||
}
|
||||
pwRepo := s.ProductWarehouseRepo
|
||||
if dbTransaction != nil {
|
||||
pwRepo = productWarehouseRepository.NewProductWarehouseRepository(dbTransaction)
|
||||
@@ -906,6 +900,11 @@ func (s projectflockService) ensureProjectFlockKandangProductWarehouses(ctx cont
|
||||
return nil
|
||||
}
|
||||
|
||||
projectFlockID := records[0].ProjectFlockId
|
||||
if projectFlockID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Project flock id tidak ditemukan")
|
||||
}
|
||||
|
||||
pwRepo := s.ProductWarehouseRepo
|
||||
if dbTransaction != nil {
|
||||
pwRepo = productWarehouseRepository.NewProductWarehouseRepository(dbTransaction)
|
||||
@@ -920,24 +919,34 @@ func (s projectflockService) ensureProjectFlockKandangProductWarehouses(ctx cont
|
||||
warehouseRepo = warehouseRepository.NewWarehouseRepository(s.Repository.DB())
|
||||
}
|
||||
|
||||
flags := []utils.FlagType{
|
||||
utils.FlagAyamAfkir,
|
||||
utils.FlagAyamCulling,
|
||||
utils.FlagAyamMati,
|
||||
utils.FlagTelurPecah,
|
||||
utils.FlagTelurUtuh,
|
||||
db := s.Repository.DB()
|
||||
if dbTransaction != nil {
|
||||
db = dbTransaction
|
||||
}
|
||||
var category string
|
||||
if err := db.WithContext(ctx).
|
||||
Model(&entity.ProjectFlock{}).
|
||||
Select("category").
|
||||
Where("id = ?", projectFlockID).
|
||||
Scan(&category).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(category) == "" {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Project flock category tidak ditemukan")
|
||||
}
|
||||
|
||||
productIDs := make(map[utils.FlagType]uint, len(flags))
|
||||
for _, flag := range flags {
|
||||
product, err := pwRepo.GetFirstProductByFlag(ctx, string(flag))
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Product untuk flag %s tidak ditemukan", flag))
|
||||
}
|
||||
return err
|
||||
}
|
||||
productIDs[flag] = product.Id
|
||||
prefixes := []string{"AYAM-"}
|
||||
if strings.EqualFold(category, string(utils.ProjectFlockCategoryLaying)) {
|
||||
prefixes = append(prefixes, "TELUR")
|
||||
}
|
||||
|
||||
invisibleOnly := false
|
||||
productIDs, err := pwRepo.ListProductIDsByFlagPrefixes(ctx, prefixes, &invisibleOnly)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(productIDs) == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Product dengan flag %s tidak ditemukan", strings.Join(prefixes, ", ")))
|
||||
}
|
||||
|
||||
for _, record := range records {
|
||||
@@ -953,8 +962,7 @@ func (s projectflockService) ensureProjectFlockKandangProductWarehouses(ctx cont
|
||||
return err
|
||||
}
|
||||
|
||||
for _, flag := range flags {
|
||||
productID := productIDs[flag]
|
||||
for _, productID := range productIDs {
|
||||
if _, err := pwRepo.GetByProductWarehouseAndProjectFlockKandang(ctx, productID, warehouse.Id, record.Id); err == nil {
|
||||
continue
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
||||
Reference in New Issue
Block a user