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.
|
'skip' ignores missing compat; 'reject' requires all compat.
|
||||||
Expected CSV columns (case-insensitive):
|
Expected CSV columns (case-insensitive):
|
||||||
sku/part_number, name, brand, price, stock, cost,
|
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
|
Optional compat columns: make, model, year, engine, engine_code
|
||||||
"""
|
"""
|
||||||
from services.qwen_fitment import get_vehicle_fitment
|
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
|
db_name = db_name_row[0] if db_name_row else None
|
||||||
mcur.close(); mconn.close()
|
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):
|
for row_num, row in enumerate(rows, start=1):
|
||||||
part_number = str(row.get('part_number', '')).strip()
|
part_number = str(row.get('part_number', '')).strip()
|
||||||
name = str(row.get('name', '')).strip()
|
name = str(row.get('name', '')).strip()
|
||||||
@@ -563,7 +567,10 @@ def bulk_import_items():
|
|||||||
cost = _to_decimal(row.get('cost'), 0)
|
cost = _to_decimal(row.get('cost'), 0)
|
||||||
location = str(row.get('location', '')).strip()
|
location = str(row.get('location', '')).strip()
|
||||||
description = str(row.get('description', '')).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)
|
# Check if item already exists (catalog is shared across branches)
|
||||||
cur.execute("SELECT id FROM inventory WHERE part_number = %s", (part_number,))
|
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,
|
price_1 = CASE WHEN %s > 0 THEN %s ELSE price_1 END,
|
||||||
location = COALESCE(NULLIF(%s,''), location),
|
location = COALESCE(NULLIF(%s,''), location),
|
||||||
description = COALESCE(NULLIF(%s,''), description),
|
description = COALESCE(NULLIF(%s,''), description),
|
||||||
category = COALESCE(NULLIF(%s,''), category)
|
category_id = COALESCE(%s, category_id)
|
||||||
WHERE id = %s
|
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
|
was_inserted = False
|
||||||
# Record stock adjustment for existing item if stock > 0
|
# Record stock adjustment for existing item if stock > 0
|
||||||
@@ -597,11 +604,11 @@ def bulk_import_items():
|
|||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO inventory
|
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)
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||||
RETURNING id
|
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]
|
item_id = cur.fetchone()[0]
|
||||||
was_inserted = True
|
was_inserted = True
|
||||||
|
|||||||
Reference in New Issue
Block a user