mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
add paired adjustment triger depletion adjustment
This commit is contained in:
@@ -701,16 +701,17 @@ func (s *fifoStockV2Service) resolveRollbackFlagGroup(ctx context.Context, tx *g
|
||||
FlagGroupCode string `gorm:"column:flag_group_code"`
|
||||
}
|
||||
var latest row
|
||||
err := tx.WithContext(ctx).
|
||||
latestQuery := tx.WithContext(ctx).
|
||||
Table("stock_allocations").
|
||||
Select("flag_group_code").
|
||||
Where("usable_type = ? AND usable_id = ?", req.Usable.LegacyTypeKey, req.Usable.ID).
|
||||
Where("engine_version = 'v2'").
|
||||
Where("allocation_purpose = ?", defaultAllocationPurpose()).
|
||||
Where("flag_group_code IS NOT NULL AND flag_group_code <> ''").
|
||||
Order("id DESC").
|
||||
Limit(1).
|
||||
Take(&latest).Error
|
||||
Where("flag_group_code IS NOT NULL AND flag_group_code <> ''")
|
||||
if code := strings.TrimSpace(req.Usable.FunctionCode); code != "" {
|
||||
latestQuery = latestQuery.Where("function_code = ?", code)
|
||||
}
|
||||
err := latestQuery.Order("id DESC").Limit(1).Take(&latest).Error
|
||||
if err == nil && strings.TrimSpace(latest.FlagGroupCode) != "" {
|
||||
return latest.FlagGroupCode, nil
|
||||
}
|
||||
@@ -718,19 +719,56 @@ func (s *fifoStockV2Service) resolveRollbackFlagGroup(ctx context.Context, tx *g
|
||||
return "", err
|
||||
}
|
||||
|
||||
var rules []routeRule
|
||||
err = tx.WithContext(ctx).
|
||||
rulesQuery := tx.WithContext(ctx).
|
||||
Table("fifo_stock_v2_route_rules").
|
||||
Where("is_active = TRUE").
|
||||
Where("lane = ?", string(LaneUsable)).
|
||||
Where("legacy_type_key = ?", req.Usable.LegacyTypeKey).
|
||||
Find(&rules).Error
|
||||
Where("legacy_type_key = ?", req.Usable.LegacyTypeKey)
|
||||
if code := strings.TrimSpace(req.Usable.FunctionCode); code != "" {
|
||||
rulesQuery = rulesQuery.Where("function_code = ?", code)
|
||||
}
|
||||
|
||||
var rules []routeRule
|
||||
err = rulesQuery.Find(&rules).Error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(rules) == 0 {
|
||||
return "", fmt.Errorf("cannot resolve flag group for usable type %s", req.Usable.LegacyTypeKey)
|
||||
}
|
||||
if len(rules) > 1 && req.ProductWarehouseID != 0 {
|
||||
type candidateRow struct {
|
||||
FlagGroupCode string `gorm:"column:flag_group_code"`
|
||||
}
|
||||
var candidates []candidateRow
|
||||
byProductQuery := tx.WithContext(ctx).
|
||||
Table("fifo_stock_v2_route_rules rr").
|
||||
Select("DISTINCT rr.flag_group_code").
|
||||
Joins("JOIN fifo_stock_v2_flag_groups fg ON fg.code = rr.flag_group_code AND fg.is_active = TRUE").
|
||||
Where("rr.is_active = TRUE").
|
||||
Where("rr.lane = ?", string(LaneUsable)).
|
||||
Where("rr.legacy_type_key = ?", req.Usable.LegacyTypeKey).
|
||||
Where(`
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM product_warehouses pw
|
||||
JOIN flags f ON f.flagable_id = pw.product_id
|
||||
JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.is_active = TRUE
|
||||
WHERE pw.id = ?
|
||||
AND f.flagable_type = 'products'
|
||||
AND fm.flag_group_code = rr.flag_group_code
|
||||
)
|
||||
`, req.ProductWarehouseID)
|
||||
if code := strings.TrimSpace(req.Usable.FunctionCode); code != "" {
|
||||
byProductQuery = byProductQuery.Where("rr.function_code = ?", code)
|
||||
}
|
||||
if err := byProductQuery.Order("rr.flag_group_code ASC").Scan(&candidates).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(candidates) == 1 {
|
||||
return strings.TrimSpace(candidates[0].FlagGroupCode), nil
|
||||
}
|
||||
}
|
||||
if len(rules) > 1 {
|
||||
return "", fmt.Errorf("ambiguous rollback flag group for usable type %s", req.Usable.LegacyTypeKey)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user