"""FICHA360-B · comprobación mecánica del paquete (no es la lectura): con las reglas del PROMPT sobre los originales ya enmascarados de para_lector, ¿la tabla de cada caso cuadra? Esperado: cabeza y positivos sí; negativos no. Solo recuentos.""" import csv, json, re from pathlib import Path X = Path(__file__).resolve().parent; PL = X / "lotes/vias/para_lector" def ins(s): u = (s or "").strip().upper() return "subvencion" if u.startswith("SUBVENCI") else "prestamo" if u in ("PRÉSTAMO", "PRESTAMO") else "garantia" if u.startswith("GARANT") else "ventaja_fiscal" if u == "VENTAJA FISCAL" else "otros" mapa = {int(r["ref"]): r for r in csv.DictReader(open(X / "lotes/vias/mapa/grupos.tsv", encoding="utf-8"), delimiter="\t")} sel = {c["nif"]: c for c in json.load(open(X / "priv/sel.json"))["casos"] if c["subgrupo"] != "negativo"} sel_todos = json.load(open(X / "priv/sel.json"))["casos"] res = {"cabeza": [0, 0], "positivo": [0, 0], "negativo": [0, 0]} for ref, m in mapa.items(): d = PL / "originales" / f"{ref}_1"; nif = m["clave"] vistos, cel = set(), {} def mete(via, x, ck): a = int(str(x["fechaConcesion"])[:4]); cod = x.get(ck) if (via, cod) in vistos: return vistos.add((via, cod)); k = (a, via, ins(x.get("instrumento"))); o = cel.setdefault(k, [0, None, None]); o[0] += 1 if via != "minimis" and x.get("importe") not in (None, ""): o[1] = (o[1] or 0) + float(x["importe"]) if x.get("ayudaEquivalente") not in (None, ""): o[2] = (o[2] or 0) + float(x["ayudaEquivalente"]) for l in open(d / "bdns_concesiones.jsonl", encoding="utf-8"): x = json.loads(l) if str(x["fechaConcesion"]) >= "2022": mete("concesion", x, "codConcesion") for f, via, ck in (("bdns_ayudas_estado.jsonl", "ayuda_estado", "codConcesion"), ("bdns_minimis.jsonl", "minimis", "codigoConcesion")): for l in open(d / f, encoding="utf-8"): x = json.loads(l) if "2016" <= str(x["fechaConcesion"]) < "2022": mete(via, x, ck) caso = next(c for c in sel_todos if c["nif"] == nif and (c["grupo"], c["subgrupo"]) == (m["grupo"], m["subgrupo"])) afi = {(x["anio"], x["via"], x["instrumento"]): [x["n"], x["importe"], x["equivalente"]] for x in caso["celdas"]} ig = lambda a, b: a[0] == b[0] and all((p is None and q is None) or (p is not None and q is not None and abs(p - q) <= 0.01) for p, q in zip(a[1:], b[1:])) ok = set(afi) == set(cel) and all(ig(afi[k], cel[k]) for k in afi) prtr_f = sorted((PL / "originales" / f"{ref}_2").glob("prtr_*.csv")) if (PL / "originales" / f"{ref}_2").exists() else [] if caso.get("prtr") or prtr_f: tot = {} for f in prtr_f: lista = f.stem.split("_", 1)[1] tot[lista] = round(sum(float(r["Total (€)"]) for r in csv.DictReader(open(f, encoding="utf-8"))), 2) afp = {l["lista"]: l["importe"] for l in (caso.get("prtr") or {}).get("listas", [])} ok = ok and set(afp) == set(tot) and all(abs(afp[k] - tot[k]) <= 0.01 for k in afp) g = "cabeza" if m["grupo"] == "cabeza" else m["subgrupo"] res[g][0] += ok; res[g][1] += 1 print(res)