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:
@@ -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,
|
||||
|
||||
@@ -592,132 +592,34 @@ async def run_sync(id_pol: int = None, id_sectie: int = None, run_id: str = None
|
||||
# Step 2d: Pre-validate prices for importable articles
|
||||
if id_pol and (truly_importable or already_in_roa):
|
||||
_update_progress("validation", "Validating prices...", 0, len(truly_importable))
|
||||
_log_line(run_id, "Validare preturi...")
|
||||
all_codmats = set()
|
||||
for order in (truly_importable + already_in_roa):
|
||||
for item in order.items:
|
||||
if item.sku in validation["mapped"]:
|
||||
pass
|
||||
elif item.sku in validation["direct"]:
|
||||
all_codmats.add(item.sku)
|
||||
# Get standard VAT rate from settings for PROC_TVAV metadata
|
||||
cota_tva = float(app_settings.get("discount_vat") or 21)
|
||||
|
||||
# Dual pricing policy support
|
||||
id_pol_productie = int(app_settings.get("id_pol_productie") or 0) or None
|
||||
codmat_policy_map = {}
|
||||
|
||||
if all_codmats:
|
||||
if id_pol_productie:
|
||||
# Dual-policy: classify articles by cont (sales vs production)
|
||||
codmat_policy_map = await asyncio.to_thread(
|
||||
validation_service.validate_and_ensure_prices_dual,
|
||||
all_codmats, id_pol, id_pol_productie,
|
||||
conn, validation.get("direct_id_map"),
|
||||
cota_tva=cota_tva
|
||||
)
|
||||
_log_line(run_id,
|
||||
f"Politici duale: {sum(1 for v in codmat_policy_map.values() if v == id_pol)} vanzare, "
|
||||
f"{sum(1 for v in codmat_policy_map.values() if v == id_pol_productie)} productie")
|
||||
else:
|
||||
# Single-policy (backward compatible)
|
||||
price_result = await asyncio.to_thread(
|
||||
validation_service.validate_prices, all_codmats, id_pol,
|
||||
conn, validation.get("direct_id_map")
|
||||
)
|
||||
if price_result["missing_price"]:
|
||||
logger.info(
|
||||
f"Auto-adding price 0 for {len(price_result['missing_price'])} "
|
||||
f"direct articles in policy {id_pol}"
|
||||
)
|
||||
await asyncio.to_thread(
|
||||
validation_service.ensure_prices,
|
||||
price_result["missing_price"], id_pol,
|
||||
conn, validation.get("direct_id_map"),
|
||||
cota_tva=cota_tva
|
||||
)
|
||||
pv_result = await asyncio.to_thread(
|
||||
validation_service.pre_validate_order_prices,
|
||||
truly_importable + already_in_roa,
|
||||
app_settings, conn, id_pol, id_pol_productie,
|
||||
id_gestiuni, validation,
|
||||
lambda msg: _log_line(run_id, msg),
|
||||
cota_tva,
|
||||
)
|
||||
|
||||
# Also validate mapped SKU prices (cherry-pick 1)
|
||||
mapped_skus_in_orders = set()
|
||||
for order in (truly_importable + already_in_roa):
|
||||
for item in order.items:
|
||||
if item.sku in validation["mapped"]:
|
||||
mapped_skus_in_orders.add(item.sku)
|
||||
|
||||
mapped_codmat_data = {}
|
||||
if mapped_skus_in_orders:
|
||||
mapped_codmat_data = await asyncio.to_thread(
|
||||
validation_service.resolve_mapped_codmats, mapped_skus_in_orders, conn,
|
||||
id_gestiuni=id_gestiuni
|
||||
)
|
||||
# Build id_map for mapped codmats and validate/ensure their prices
|
||||
mapped_id_map = {}
|
||||
for sku, entries in mapped_codmat_data.items():
|
||||
for entry in entries:
|
||||
mapped_id_map[entry["codmat"]] = {
|
||||
"id_articol": entry["id_articol"],
|
||||
"cont": entry.get("cont")
|
||||
}
|
||||
mapped_codmats = set(mapped_id_map.keys())
|
||||
if mapped_codmats:
|
||||
if id_pol_productie:
|
||||
mapped_policy_map = await asyncio.to_thread(
|
||||
validation_service.validate_and_ensure_prices_dual,
|
||||
mapped_codmats, id_pol, id_pol_productie,
|
||||
conn, mapped_id_map, cota_tva=cota_tva
|
||||
)
|
||||
codmat_policy_map.update(mapped_policy_map)
|
||||
# Filter truly_importable for kits with missing component prices
|
||||
kit_missing = pv_result.get("kit_missing") or {}
|
||||
if kit_missing:
|
||||
kit_skus_missing = set(kit_missing.keys())
|
||||
new_truly = []
|
||||
for order in truly_importable:
|
||||
order_skus = {item.sku for item in order.items}
|
||||
if order_skus & kit_skus_missing:
|
||||
missing_list = list(order_skus & kit_skus_missing)
|
||||
skipped.append((order, missing_list))
|
||||
else:
|
||||
mp_result = await asyncio.to_thread(
|
||||
validation_service.validate_prices,
|
||||
mapped_codmats, id_pol, conn, mapped_id_map
|
||||
)
|
||||
if mp_result["missing_price"]:
|
||||
await asyncio.to_thread(
|
||||
validation_service.ensure_prices,
|
||||
mp_result["missing_price"], id_pol,
|
||||
conn, mapped_id_map, cota_tva=cota_tva
|
||||
)
|
||||
|
||||
# Add SKU → policy entries for mapped articles (1:1 and kits)
|
||||
# codmat_policy_map has CODMAT keys, but build_articles_json
|
||||
# looks up by GoMag SKU — bridge the gap here
|
||||
if codmat_policy_map and mapped_codmat_data:
|
||||
for sku, entries in mapped_codmat_data.items():
|
||||
if len(entries) == 1:
|
||||
# 1:1 mapping: SKU inherits the CODMAT's policy
|
||||
codmat = entries[0]["codmat"]
|
||||
if codmat in codmat_policy_map:
|
||||
codmat_policy_map[sku] = codmat_policy_map[codmat]
|
||||
|
||||
# Pass codmat_policy_map to import via app_settings
|
||||
if codmat_policy_map:
|
||||
app_settings["_codmat_policy_map"] = codmat_policy_map
|
||||
|
||||
# ── Kit component price validation ──
|
||||
kit_pricing_mode = app_settings.get("kit_pricing_mode")
|
||||
if kit_pricing_mode and mapped_codmat_data:
|
||||
id_pol_prod = int(app_settings.get("id_pol_productie") or 0) or None
|
||||
kit_missing = await asyncio.to_thread(
|
||||
validation_service.validate_kit_component_prices,
|
||||
mapped_codmat_data, id_pol, id_pol_prod, conn
|
||||
)
|
||||
if kit_missing:
|
||||
kit_skus_missing = set(kit_missing.keys())
|
||||
for sku, missing_codmats in kit_missing.items():
|
||||
_log_line(run_id, f"Kit {sku}: prețuri lipsă pentru {', '.join(missing_codmats)}")
|
||||
new_truly = []
|
||||
for order in truly_importable:
|
||||
order_skus = {item.sku for item in order.items}
|
||||
if order_skus & kit_skus_missing:
|
||||
missing_list = list(order_skus & kit_skus_missing)
|
||||
skipped.append((order, missing_list))
|
||||
else:
|
||||
new_truly.append(order)
|
||||
truly_importable = new_truly
|
||||
new_truly.append(order)
|
||||
truly_importable = new_truly
|
||||
|
||||
# Mode B config validation
|
||||
if kit_pricing_mode == "separate_line":
|
||||
if app_settings.get("kit_pricing_mode") == "separate_line":
|
||||
if not app_settings.get("kit_discount_codmat"):
|
||||
_log_line(run_id, "EROARE: Kit mode 'separate_line' dar kit_discount_codmat nu e configurat!")
|
||||
finally:
|
||||
|
||||
@@ -529,6 +529,147 @@ def resolve_mapped_codmats(mapped_skus: set[str], conn,
|
||||
return result
|
||||
|
||||
|
||||
def pre_validate_order_prices(orders, app_settings: dict, conn, id_pol: int,
|
||||
id_pol_productie: int = None,
|
||||
id_gestiuni: list[int] = None,
|
||||
validation: dict = None,
|
||||
log_callback=None,
|
||||
cota_tva: float = 21) -> dict:
|
||||
"""Pre-validate prices for orders before importing them via PACK_IMPORT_COMENZI.
|
||||
|
||||
Auto-inserts PRET=0 rows in CRM_POLITICI_PRET_ART for missing CODMATs so
|
||||
PL/SQL adauga_articol_comanda doesn't raise COM-001. Mutates
|
||||
app_settings["_codmat_policy_map"] for build_articles_json routing.
|
||||
|
||||
Used by both bulk sync (sync_service.run_sync) and retry (retry_service).
|
||||
|
||||
Args:
|
||||
orders: list of orders to scan for SKUs/CODMATs
|
||||
app_settings: mutated with _codmat_policy_map (SKU/CODMAT → id_pol)
|
||||
conn: Oracle connection (caller manages lifecycle)
|
||||
id_pol: default sales price policy
|
||||
id_pol_productie: production policy for cont 341/345 (None = single-policy)
|
||||
id_gestiuni: gestiune filter for resolve_mapped_codmats
|
||||
validation: output of validate_skus; computed internally if None
|
||||
log_callback: optional Callable[[str], None] for progress messages
|
||||
cota_tva: VAT rate for PROC_TVAV metadata (default 21)
|
||||
|
||||
Returns: {"codmat_policy_map": dict, "kit_missing": dict, "validation": dict}
|
||||
- codmat_policy_map: {codmat_or_sku: id_pol}
|
||||
- kit_missing: {sku: [missing_codmats]} for kits with unprice components
|
||||
- validation: validate_skus result (for caller convenience)
|
||||
"""
|
||||
log = log_callback or (lambda _msg: None)
|
||||
|
||||
if not orders:
|
||||
return {"codmat_policy_map": {}, "kit_missing": {}, "validation": validation or {}}
|
||||
|
||||
if validation is None:
|
||||
all_skus = {item.sku for o in orders for item in o.items if item.sku}
|
||||
validation = validate_skus(all_skus, conn, id_gestiuni)
|
||||
|
||||
log("Validare preturi...")
|
||||
|
||||
# Direct CODMATs (SKU exists in NOM_ARTICOLE without ARTICOLE_TERTI mapping)
|
||||
all_codmats = set()
|
||||
for order in orders:
|
||||
for item in order.items:
|
||||
if item.sku in validation["mapped"]:
|
||||
continue
|
||||
if item.sku in validation["direct"]:
|
||||
all_codmats.add(item.sku)
|
||||
|
||||
codmat_policy_map = {}
|
||||
|
||||
if all_codmats:
|
||||
if id_pol_productie:
|
||||
codmat_policy_map = validate_and_ensure_prices_dual(
|
||||
all_codmats, id_pol, id_pol_productie,
|
||||
conn, validation.get("direct_id_map"), cota_tva=cota_tva,
|
||||
)
|
||||
log(f"Politici duale: "
|
||||
f"{sum(1 for v in codmat_policy_map.values() if v == id_pol)} vanzare, "
|
||||
f"{sum(1 for v in codmat_policy_map.values() if v == id_pol_productie)} productie")
|
||||
else:
|
||||
price_result = validate_prices(
|
||||
all_codmats, id_pol, conn, validation.get("direct_id_map"),
|
||||
)
|
||||
if price_result["missing_price"]:
|
||||
logger.info(
|
||||
f"Auto-adding price 0 for {len(price_result['missing_price'])} "
|
||||
f"direct articles in policy {id_pol}"
|
||||
)
|
||||
ensure_prices(
|
||||
price_result["missing_price"], id_pol,
|
||||
conn, validation.get("direct_id_map"), cota_tva=cota_tva,
|
||||
)
|
||||
|
||||
# Mapped SKUs (via ARTICOLE_TERTI)
|
||||
mapped_skus_in_orders = {
|
||||
item.sku for o in orders for item in o.items
|
||||
if item.sku in validation["mapped"]
|
||||
}
|
||||
|
||||
mapped_codmat_data = {}
|
||||
if mapped_skus_in_orders:
|
||||
mapped_codmat_data = resolve_mapped_codmats(
|
||||
mapped_skus_in_orders, conn, id_gestiuni=id_gestiuni,
|
||||
)
|
||||
mapped_id_map = {}
|
||||
for sku, entries in mapped_codmat_data.items():
|
||||
for entry in entries:
|
||||
mapped_id_map[entry["codmat"]] = {
|
||||
"id_articol": entry["id_articol"],
|
||||
"cont": entry.get("cont"),
|
||||
}
|
||||
mapped_codmats = set(mapped_id_map.keys())
|
||||
if mapped_codmats:
|
||||
if id_pol_productie:
|
||||
mapped_policy_map = validate_and_ensure_prices_dual(
|
||||
mapped_codmats, id_pol, id_pol_productie,
|
||||
conn, mapped_id_map, cota_tva=cota_tva,
|
||||
)
|
||||
codmat_policy_map.update(mapped_policy_map)
|
||||
else:
|
||||
mp_result = validate_prices(
|
||||
mapped_codmats, id_pol, conn, mapped_id_map,
|
||||
)
|
||||
if mp_result["missing_price"]:
|
||||
ensure_prices(
|
||||
mp_result["missing_price"], id_pol,
|
||||
conn, mapped_id_map, cota_tva=cota_tva,
|
||||
)
|
||||
|
||||
# Bridge SKU → policy via 1:1 mappings (build_articles_json reads by SKU)
|
||||
if codmat_policy_map and mapped_codmat_data:
|
||||
for sku, entries in mapped_codmat_data.items():
|
||||
if len(entries) == 1:
|
||||
codmat = entries[0]["codmat"]
|
||||
if codmat in codmat_policy_map:
|
||||
codmat_policy_map[sku] = codmat_policy_map[codmat]
|
||||
|
||||
if codmat_policy_map:
|
||||
app_settings["_codmat_policy_map"] = codmat_policy_map
|
||||
|
||||
# Kit component price gating
|
||||
kit_missing = {}
|
||||
kit_pricing_mode = app_settings.get("kit_pricing_mode")
|
||||
if kit_pricing_mode and mapped_codmat_data:
|
||||
kit_missing = validate_kit_component_prices(
|
||||
mapped_codmat_data, id_pol, id_pol_productie, conn,
|
||||
)
|
||||
if kit_missing:
|
||||
for sku, missing_codmats in kit_missing.items():
|
||||
log(f"Kit {sku}: prețuri lipsă pentru {', '.join(missing_codmats)}")
|
||||
|
||||
return {
|
||||
"codmat_policy_map": codmat_policy_map,
|
||||
"kit_missing": kit_missing,
|
||||
"validation": validation,
|
||||
"mapped_codmat_data": mapped_codmat_data,
|
||||
}
|
||||
|
||||
|
||||
def validate_kit_component_prices(mapped_codmat_data: dict, id_pol: int,
|
||||
id_pol_productie: int = None, conn=None) -> dict:
|
||||
"""Pre-validate that kit components have non-zero prices in crm_politici_pret_art.
|
||||
|
||||
Reference in New Issue
Block a user