fix(retry): pre-populate price list before re-importing failed orders

Production VENDING orders #485841978 and #485841895 (2026-04-28) crashed
on Retry with PL/SQL COM-001 because the retry path skipped the
CRM_POLITICI_PRET_ART pre-population step that bulk sync runs.

The price-list auto-insert (PRET=0) for missing CODMATs was only invoked
in sync_service.run_sync (lines 592-718). retry_service called
import_single_order directly, hitting pack_comenzi.adauga_articol_comanda
NO_DATA_FOUND on every CODMAT without a price entry.

Extracted the validation block into validation_service.pre_validate_order_prices
and call it from both bulk sync and retry. Single source of truth for
SKU validation, dual-policy routing (cont 341/345 → productie),
ARTICOLE_TERTI mapping resolution, and kit component price gating.

Tests: 3 unit + 3 oracle integration covering the regression scenario,
empty input, dual-policy routing, idempotency, and pre-validation
exception propagation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Claude Agent
2026-04-29 10:19:38 +00:00
parent 874ba4ca4e
commit bcd65d9fd6
4 changed files with 608 additions and 130 deletions

View File

@@ -59,6 +59,40 @@ async def _download_and_reimport(order_number: str, order_date_str: str, custome
id_gestiune = app_settings.get("id_gestiune", "")
id_gestiuni = [int(g.strip()) for g in id_gestiune.split(",") if g.strip()] if id_gestiune else None
# Pre-validate prices: auto-insert PRET=0 in CRM_POLITICI_PRET_ART for missing
# CODMATs so PL/SQL doesn't crash with COM-001. Mirrors sync_service flow.
from .. import database
validation = {"mapped": set(), "direct": set(), "missing": set(), "direct_id_map": {}}
if database.pool is not None:
conn = await asyncio.to_thread(database.get_oracle_connection)
try:
skus = {item.sku for item in target_order.items if item.sku}
if skus:
validation = await asyncio.to_thread(
validation_service.validate_skus, skus, conn, id_gestiuni,
)
if id_pol and skus:
id_pol_productie = int(app_settings.get("id_pol_productie") or 0) or None
cota_tva = float(app_settings.get("discount_vat") or 21)
await asyncio.to_thread(
validation_service.pre_validate_order_prices,
[target_order], app_settings, conn, id_pol, id_pol_productie,
id_gestiuni, validation, None, cota_tva,
)
except Exception as e:
logger.error(f"Retry pre-validation failed for {order_number}: {e}")
await sqlite_service.upsert_order(
sync_run_id="retry",
order_number=order_number,
order_date=order_date_str,
customer_name=customer_name,
status=OrderStatus.ERROR.value,
error_message=f"Retry pre-validation failed: {e}",
)
return {"success": False, "message": f"Eroare pre-validare preturi: {e}"}
finally:
await asyncio.to_thread(database.pool.release, conn)
try:
result = await asyncio.to_thread(
import_service.import_single_order,
@@ -77,17 +111,6 @@ async def _download_and_reimport(order_number: str, order_date_str: str, custome
)
return {"success": False, "message": f"Eroare import: {e}"}
# Build order_items data from fresh GoMag download (mirrors sync_service:882-891).
# Resolves ARTICOLE_TERTI mapping so UI shows mapped/direct badge.
try:
skus = {item.sku for item in target_order.items if item.sku}
validation = await asyncio.to_thread(
validation_service.validate_skus, skus, None, id_gestiuni
) if skus else {"mapped": set(), "direct": set()}
except Exception as e:
logger.warning(f"Retry: validate_skus failed for {order_number}, defaulting mapping_status=direct: {e}")
validation = {"mapped": set(), "direct": set()}
order_items_data = [
{
"sku": item.sku, "product_name": item.name,