from typing import List from urllib.parse import quote from core.errors import SAPServiceLayerError from core.sap import SAPServiceLayerClient def _item_key(sku: str) -> str: escaped = quote(sku.replace("'", "''"), safe="") return f"Items('{escaped}')" def item_existe(sap: SAPServiceLayerClient, sku: str) -> bool: try: sap.forward("GET", _item_key(sku)) return True except SAPServiceLayerError as exc: if exc.status_code == 404: return False raise def identificar_skus_faltantes(sap: SAPServiceLayerClient, skus: List[str]) -> List[str]: """ Revisa cada SKU contra SAP (GET Items) y devuelve los que no existen. Se usa cuando un POST de Invoice falla con el código -5009 ("Item number is missing"), que no indica cuál ItemCode es el problema. """ return [sku for sku in skus if not item_existe(sap, sku)]