feat(BE-287):adjustment purchase restrict unfinished

This commit is contained in:
ragilap
2025-12-30 14:27:50 +07:00
parent ddda696454
commit 0396aa0255
2 changed files with 43 additions and 32 deletions
@@ -310,9 +310,6 @@ func (b *expenseBridge) OnItemsReceived(c *fiber.Ctx, purchaseID uint, updates [
return err return err
} }
if cnt == 1 { if cnt == 1 {
if item.Warehouse == nil || item.Warehouse.KandangId == nil || *item.Warehouse.KandangId == 0 {
return fiber.NewError(fiber.StatusBadRequest, "Warehouse not connect to kandangs")
}
newNonstockID, err := b.findExpeditionNonstockID(ctx, supplierID) newNonstockID, err := b.findExpeditionNonstockID(ctx, supplierID)
if err != nil { if err != nil {
return err return err
@@ -332,7 +329,9 @@ func (b *expenseBridge) OnItemsReceived(c *fiber.Ctx, purchaseID uint, updates [
"price": pricePerItem, "price": pricePerItem,
"notes": note, "notes": note,
"nonstock_id": newNonstockID, "nonstock_id": newNonstockID,
"kandang_id": uint64(*item.Warehouse.KandangId), }
if item.Warehouse != nil && item.Warehouse.KandangId != nil && *item.Warehouse.KandangId != 0 {
updateBody["kandang_id"] = uint64(*item.Warehouse.KandangId)
} }
if err := b.db.WithContext(ctx). if err := b.db.WithContext(ctx).
Model(&entity.ExpenseNonstock{}). Model(&entity.ExpenseNonstock{}).
@@ -550,10 +549,9 @@ func (b *expenseBridge) createExpenseViaService(
} }
kandangID := items[0].kandangID kandangID := items[0].kandangID
if kandangID == nil || *kandangID == 0 { var locationID uint64
return nil, fiber.NewError(fiber.StatusBadRequest, "Warehouse not connect to kandangs") var expenseKandangID *uint64
} if kandangID != nil && *kandangID != 0 {
kandang, err := b.kandangRepo.GetByID(ctx, *kandangID, func(db *gorm.DB) *gorm.DB { kandang, err := b.kandangRepo.GetByID(ctx, *kandangID, func(db *gorm.DB) *gorm.DB {
return db.Select("id, location_id") return db.Select("id, location_id")
}) })
@@ -563,6 +561,16 @@ func (b *expenseBridge) createExpenseViaService(
if kandang == nil { if kandang == nil {
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Kandang not found: %d", *kandangID)) return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Kandang not found: %d", *kandangID))
} }
locationID = uint64(kandang.LocationId)
id := uint64(*kandangID)
expenseKandangID = &id
} else {
warehouse := items[0].item.Warehouse
if warehouse == nil || warehouse.LocationId == nil || *warehouse.LocationId == 0 {
return nil, fiber.NewError(fiber.StatusBadRequest, "Warehouse location is required for expense")
}
locationID = uint64(*warehouse.LocationId)
}
costItems := make([]expenseValidation.CostItem, 0, len(items)) costItems := make([]expenseValidation.CostItem, 0, len(items))
for _, gi := range items { for _, gi := range items {
@@ -584,9 +592,9 @@ func (b *expenseBridge) createExpenseViaService(
TransactionDate: utils.FormatDate(expenseDate), TransactionDate: utils.FormatDate(expenseDate),
Category: "BOP", Category: "BOP",
SupplierID: uint64(supplierID), SupplierID: uint64(supplierID),
LocationID: uint64(kandang.LocationId), LocationID: locationID,
ExpenseNonstocks: []expenseValidation.ExpenseNonstock{{ ExpenseNonstocks: []expenseValidation.ExpenseNonstock{{
KandangID: func() *uint64 { id := uint64(*kandangID); return &id }(), KandangID: expenseKandangID,
CostItems: costItems, CostItems: costItems,
}}, }},
} }
@@ -246,10 +246,12 @@ func (s *purchaseService) CreateOne(c *fiber.Ctx, req *validation.CreatePurchase
s.Log.Errorf("Failed to get warehouse %d: %+v", id, err) s.Log.Errorf("Failed to get warehouse %d: %+v", id, err)
return nil, nil, utils.Internal("Failed to get warehouse") return nil, nil, utils.Internal("Failed to get warehouse")
} }
var pfkID *uint
isKandang := strings.EqualFold(strings.TrimSpace(warehouse.Type), "KANDANG")
if isKandang {
if warehouse.KandangId == nil || *warehouse.KandangId == 0 { if warehouse.KandangId == nil || *warehouse.KandangId == 0 {
return nil, nil, utils.BadRequest(fmt.Sprintf("%s is not linked to a kandang", warehouse.Name)) return nil, nil, utils.BadRequest(fmt.Sprintf("%s is not linked to a kandang", warehouse.Name))
} }
var pfkID *uint
if s.ProjectFlockKandangRepo != nil { if s.ProjectFlockKandangRepo != nil {
if pfk, err := s.ProjectFlockKandangRepo.GetActiveByKandangID(c.Context(), uint(*warehouse.KandangId)); err == nil && pfk != nil { if pfk, err := s.ProjectFlockKandangRepo.GetActiveByKandangID(c.Context(), uint(*warehouse.KandangId)); err == nil && pfk != nil {
if pfk.ClosedAt != nil { if pfk.ClosedAt != nil {
@@ -264,6 +266,7 @@ func (s *purchaseService) CreateOne(c *fiber.Ctx, req *validation.CreatePurchase
return nil, nil, utils.Internal("Failed to validate project flock") return nil, nil, utils.Internal("Failed to validate project flock")
} }
} }
}
warehouseCache[id] = warehouse warehouseCache[id] = warehouse
return warehouse, pfkID, nil return warehouse, pfkID, nil