feat(inventory): Qwen vehicle compatibility — store AI unmatched vehicles as text, Celery background sync, fix brand filter fallback, increase vehicle limits

This commit is contained in:
2026-05-18 19:32:35 +00:00
parent 0b1dc89faf
commit 50c0dbe7d4
6 changed files with 330 additions and 109 deletions

View File

@@ -18,8 +18,10 @@ from services.audit import log_action
from tenant_db import get_master_conn
from services.inventory_vehicle_compat import (
auto_match_vehicle_compatibility, add_compatibility, remove_compatibility,
remove_all_compatibility, get_compatibility, search_mye, get_compat_source,
remove_compatibility_by_id, remove_all_compatibility, get_compatibility,
search_mye, get_compat_source,
)
from tasks import sync_vehicle_compatibility_task
inventory_bp = Blueprint('inventory', __name__, url_prefix='/pos/api/inventory')
@@ -287,29 +289,40 @@ def create_item():
compat_source = get_compat_source(g.tenant_id)
qwen_added = 0
# TecDoc auto-match
if compat_source in ('tecdoc', 'both'):
try:
master = get_master_conn()
auto_match_vehicle_compatibility(master, conn, item_id, data['part_number'],
brand=data.get('brand'), name=data.get('name'))
master.close()
except Exception as am_err:
print(f"[auto_match] Error for item {item_id}: {am_err}")
# Offload to Celery background task if possible (QWEN can take 90s+)
try:
sync_vehicle_compatibility_task.delay(
g.tenant_id, item_id, data['part_number'],
data['name'], data.get('brand', ''), compat_source
)
compat_background = True
except Exception as celery_err:
print(f"[celery] Failed to queue compatibility task for item {item_id}: {celery_err}")
compat_background = False
# QWEN AI fitment
if compat_source in ('qwen', 'both'):
try:
from services.qwen_fitment import get_vehicle_fitment
from services.inventory_vehicle_compat import save_qwen_fitment
fitment = get_vehicle_fitment(
data['part_number'],
data['name'],
data.get('brand', '')
)
qwen_added = save_qwen_fitment(conn, item_id, fitment)
except Exception as qwen_err:
print(f"[qwen_fitment] Error for item {item_id}: {qwen_err}")
if not compat_background:
# Fallback: synchronous processing
if compat_source in ('tecdoc', 'both'):
try:
master = get_master_conn()
auto_match_vehicle_compatibility(master, conn, item_id, data['part_number'],
brand=data.get('brand'), name=data.get('name'))
master.close()
except Exception as am_err:
print(f"[auto_match] Error for item {item_id}: {am_err}")
if compat_source in ('qwen', 'both'):
try:
from services.qwen_fitment import get_vehicle_fitment
from services.inventory_vehicle_compat import save_qwen_fitment
fitment = get_vehicle_fitment(
data['part_number'],
data['name'],
data.get('brand', '')
)
qwen_added = save_qwen_fitment(conn, item_id, fitment)
except Exception as qwen_err:
print(f"[qwen_fitment] Error for item {item_id}: {qwen_err}")
conn.close()
return jsonify({
@@ -317,6 +330,7 @@ def create_item():
'barcode': barcode,
'message': 'Item created',
'vehicle_compatibilities_added': qwen_added,
'vehicle_compat_queued': compat_background,
}), 201
except Exception as e:
@@ -1482,13 +1496,16 @@ def add_item_vehicle(item_id):
conn.close()
@inventory_bp.route('/items/<int:item_id>/vehicles/<int:mye_id>', methods=['DELETE'])
@inventory_bp.route('/items/<int:item_id>/vehicles/<int:compat_id>', methods=['DELETE'])
@require_auth('inventory.edit')
def delete_item_vehicle(item_id, mye_id):
"""Remove a vehicle compatibility."""
def delete_item_vehicle(item_id, compat_id):
"""Remove a vehicle compatibility by its row id.
Works for both TecDoc-linked (mye_id present) and text-only QWEN records.
"""
conn = get_tenant_conn(g.tenant_id)
try:
deleted = remove_compatibility(conn, item_id, mye_id)
deleted = remove_compatibility_by_id(conn, compat_id)
return jsonify({'message': 'Compatibility removed', 'deleted': deleted})
finally:
conn.close()