feat(BE): add excluded stockables support in FIFO allocation and fetching methods

This commit is contained in:
aguhh18
2026-01-07 13:54:55 +07:00
parent 76d5b6b69a
commit 9336289573
2 changed files with 33 additions and 9 deletions
+27 -4
View File
@@ -228,7 +228,13 @@ func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*St
switch { switch {
case delta > 0: case delta > 0:
allocationRes, err := s.allocateFromStock(ctx, tx, productWarehouseID, req.UsableKey, req.UsableID, delta)
var excludedStockables []fifo.StockableKey
if cfg.ExcludedStockables != nil {
excludedStockables = cfg.ExcludedStockables
}
allocationRes, err := s.allocateFromStock(ctx, tx, productWarehouseID, req.UsableKey, req.UsableID, delta, excludedStockables)
if err != nil { if err != nil {
return err return err
} }
@@ -410,8 +416,9 @@ func (s *fifoService) allocateFromStock(
usableKey fifo.UsableKey, usableKey fifo.UsableKey,
usableID uint, usableID uint,
requestQty float64, requestQty float64,
excludedStockables []fifo.StockableKey,
) (*allocationOutcome, error) { ) (*allocationOutcome, error) {
lots, err := s.fetchStockLots(ctx, tx, productWarehouseID) lots, err := s.fetchStockLots(ctx, tx, productWarehouseID, excludedStockables)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -492,14 +499,24 @@ func (s *fifoService) allocateFromStock(
}, nil }, nil
} }
func (s *fifoService) fetchStockLots(ctx context.Context, tx *gorm.DB, productWarehouseID uint) ([]stockLot, error) { func (s *fifoService) fetchStockLots(ctx context.Context, tx *gorm.DB, productWarehouseID uint, excludedStockables []fifo.StockableKey) ([]stockLot, error) {
configs := fifo.Stockables() configs := fifo.Stockables()
if len(configs) == 0 { if len(configs) == 0 {
return nil, nil return nil, nil
} }
// Create exclusion set for faster lookup
excludedSet := make(map[fifo.StockableKey]bool)
for _, key := range excludedStockables {
excludedSet[key] = true
}
var lots []stockLot var lots []stockLot
for key, cfg := range configs { for key, cfg := range configs {
// Skip excluded stockables
if excludedSet[key] {
continue
}
usesNumericTime := cfg.Columns.CreatedAt == cfg.Columns.ID usesNumericTime := cfg.Columns.CreatedAt == cfg.Columns.ID
@@ -616,7 +633,13 @@ func (s *fifoService) resolvePendingForWarehouse(ctx context.Context, tx *gorm.D
continue continue
} }
outcome, err := s.allocateFromStock(ctx, tx, productWarehouseID, candidate.UsableKey, candidate.UsableID, candidate.Pending) // Get excluded stockables from candidate usable config
var excludedStockables []fifo.StockableKey
if candidate.Config.ExcludedStockables != nil {
excludedStockables = candidate.Config.ExcludedStockables
}
outcome, err := s.allocateFromStock(ctx, tx, productWarehouseID, candidate.UsableKey, candidate.UsableID, candidate.Pending, excludedStockables)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+6 -5
View File
@@ -54,11 +54,12 @@ type StockableConfig struct {
// UsableConfig registers a table that consumes stock (recordings, adjustments, sales, etc). // UsableConfig registers a table that consumes stock (recordings, adjustments, sales, etc).
type UsableConfig struct { type UsableConfig struct {
Key UsableKey Key UsableKey
Table string Table string
Columns UsableColumns Columns UsableColumns
OrderBy []string OrderBy []string
Scope QueryScope Scope QueryScope
ExcludedStockables []StockableKey // Stockables to exclude when consuming stock
} }
var ( var (