fix(inventory): use category_id in bulk import and resolve category names
This commit is contained in:
@@ -426,7 +426,7 @@ def bulk_import_items():
|
||||
'skip' ignores missing compat; 'reject' requires all compat.
|
||||
Expected CSV columns (case-insensitive):
|
||||
sku/part_number, name, brand, price, stock, cost,
|
||||
location, description, category, make, model, year, engine, engine_code
|
||||
sku_secondary, description, category, make, model, year, engine, engine_code
|
||||
Optional compat columns: make, model, year, engine, engine_code
|
||||
"""
|
||||
from services.qwen_fitment import get_vehicle_fitment
|
||||
@@ -535,6 +535,10 @@ def bulk_import_items():
|
||||
db_name = db_name_row[0] if db_name_row else None
|
||||
mcur.close(); mconn.close()
|
||||
|
||||
# Pre-fetch category name -> id mapping
|
||||
cur.execute("SELECT id, name FROM categories")
|
||||
category_map = {str(r[1]).strip().lower(): r[0] for r in cur.fetchall()}
|
||||
|
||||
for row_num, row in enumerate(rows, start=1):
|
||||
part_number = str(row.get('part_number', '')).strip()
|
||||
name = str(row.get('name', '')).strip()
|
||||
@@ -563,7 +567,10 @@ def bulk_import_items():
|
||||
cost = _to_decimal(row.get('cost'), 0)
|
||||
location = str(row.get('location', '')).strip()
|
||||
description = str(row.get('description', '')).strip()
|
||||
category = str(row.get('category', '')).strip()
|
||||
category_name = str(row.get('category', '')).strip()
|
||||
category_id = category_map.get(category_name.lower()) if category_name else None
|
||||
if category_name and category_id is None:
|
||||
warnings.append(f'Row {row_num}: categoria "{category_name}" no encontrada')
|
||||
|
||||
# Check if item already exists (catalog is shared across branches)
|
||||
cur.execute("SELECT id FROM inventory WHERE part_number = %s", (part_number,))
|
||||
@@ -581,10 +588,10 @@ def bulk_import_items():
|
||||
price_1 = CASE WHEN %s > 0 THEN %s ELSE price_1 END,
|
||||
location = COALESCE(NULLIF(%s,''), location),
|
||||
description = COALESCE(NULLIF(%s,''), description),
|
||||
category = COALESCE(NULLIF(%s,''), category)
|
||||
category_id = COALESCE(%s, category_id)
|
||||
WHERE id = %s
|
||||
""",
|
||||
(name, brand, cost, cost, price_1, price_1, location, description, category, item_id)
|
||||
(name, brand, cost, cost, price_1, price_1, location, description, category_id, item_id)
|
||||
)
|
||||
was_inserted = False
|
||||
# Record stock adjustment for existing item if stock > 0
|
||||
@@ -597,11 +604,11 @@ def bulk_import_items():
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO inventory
|
||||
(part_number, barcode, name, brand, cost, price_1, location, description, category, unit)
|
||||
(part_number, barcode, name, brand, cost, price_1, location, description, category_id, unit)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING id
|
||||
""",
|
||||
(part_number, barcode, name, brand, cost, price_1, location, description, category, 'PZA')
|
||||
(part_number, barcode, name, brand, cost, price_1, location, description, category_id, 'PZA')
|
||||
)
|
||||
item_id = cur.fetchone()[0]
|
||||
was_inserted = True
|
||||
|
||||
Reference in New Issue
Block a user