49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
import os
|
|||
|
|
import sys
|
||
|
|
import json
|
||
|
|
import xml.etree.ElementTree as ET
|
||
|
|
|
||
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||
|
|
|
||
|
|
from sunat_schema import NAMESPACES, _find_text
|
||
|
|
|
||
|
|
ns = NAMESPACES
|
||
|
|
ns["cbc"] = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
|
||
|
|
|
||
|
|
CARPETA_BASE = os.path.join(os.path.dirname(__file__), "correos_prueba")
|
||
|
|
|
||
|
|
|
||
|
|
def explorar_notes(ruta_carpeta):
|
||
|
|
archivos = sorted(
|
||
|
|
os.path.join(ruta_carpeta, f) for f in os.listdir(ruta_carpeta)
|
||
|
|
if f.lower().endswith(".xml") and os.path.isfile(os.path.join(ruta_carpeta, f))
|
||
|
|
)
|
||
|
|
|
||
|
|
for xml_path in archivos:
|
||
|
|
tree = ET.parse(xml_path)
|
||
|
|
root = tree.getroot()
|
||
|
|
|
||
|
|
if "Invoice" not in root.tag:
|
||
|
|
continue
|
||
|
|
|
||
|
|
tipo = _find_text(root, "inv:InvoiceTypeCode", ns) or _find_text(root, "cbc:InvoiceTypeCode", ns) or ""
|
||
|
|
if tipo != "01":
|
||
|
|
continue
|
||
|
|
|
||
|
|
print(f"\n=== {os.path.basename(xml_path)} ===")
|
||
|
|
lineas = root.findall(".//cac:InvoiceLine", ns)
|
||
|
|
for i, linea in enumerate(lineas):
|
||
|
|
notes = linea.findall("cbc:Note", ns)
|
||
|
|
desc = _find_text(linea, "cac:Item/cbc:Description", ns) or ""
|
||
|
|
print(f" Línea {i}: Description={desc[:80]}...")
|
||
|
|
for note in notes:
|
||
|
|
print(f" Note[{note.attrib.get('languageLocaleID', '?')}]: {note.text}")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
carpeta = sys.argv[1] if len(sys.argv) > 1 else "001_correo"
|
||
|
|
ruta = os.path.join(CARPETA_BASE, carpeta)
|
||
|
|
if os.path.isdir(ruta):
|
||
|
|
explorar_notes(ruta)
|
||
|
|
else:
|
||
|
|
print(f"❌ Carpeta no encontrada: {ruta}")
|