Default lot category to PART_ when missing

This commit is contained in:
Mikhail Chusavitin
2026-02-18 10:32:51 +03:00
parent 0827c501df
commit 221e414e2f
6 changed files with 129 additions and 15 deletions

View File

@@ -85,12 +85,43 @@ func ComputePricelistItemsFromStockLog(db *gorm.DB) ([]SnapshotItem, error) {
}
}
}
defaultCategory := models.DefaultLotCategoryCode
missingCategoryLots := make([]string, 0)
for i := range items {
if strings.TrimSpace(items[i].Category) == "" {
items[i].Category = defaultCategory
missingCategoryLots = append(missingCategoryLots, items[i].LotName)
}
}
if len(missingCategoryLots) > 0 {
ensureCategoryExists(db, defaultCategory)
_ = db.Model(&models.Lot{}).
Where("lot_name IN ?", missingCategoryLots).
Update("lot_category", defaultCategory).Error
}
}
sort.Slice(items, func(i, j int) bool { return items[i].LotName < items[j].LotName })
return items, nil
}
func ensureCategoryExists(db *gorm.DB, code string) {
var count int64
if err := db.Model(&models.Category{}).Where("code = ?", code).Count(&count).Error; err != nil || count > 0 {
return
}
var maxOrder int
if err := db.Model(&models.Category{}).Select("COALESCE(MAX(display_order), 0)").Scan(&maxOrder).Error; err != nil {
return
}
_ = db.Create(&models.Category{
Code: code,
Name: code,
NameRu: code,
DisplayOrder: maxOrder + 1,
}).Error
}
// LoadLotMetrics returns stock qty and partnumbers for selected lots.
// If latestOnly is true, qty/partnumbers from stock_log are calculated only for latest import date.
func LoadLotMetrics(db *gorm.DB, lotNames []string, latestOnly bool) (map[string]float64, map[string][]string, error) {