def main(): from decimal import Decimal, InvalidOperation import re, json, ast # ---- Rocketbot GetVar/SetVar wrappers ---- def rb_get(name): try: return GetVar(name) except Exception: return None def rb_set(name, value): try: SetVar(name, value) except Exception: pass # ---- helpers ---- def is_empty(v): if v is None: return True if isinstance(v, str): s = v.strip().lower() return s == "" or s in ("none", "null", "nan", "undefined") return False def fmt_es_money(value, decimals=2): # self-contained (no depende de otra func global) if is_empty(value): return None if isinstance(value, (int, float, Decimal)): d = Decimal(str(value)) else: s = str(value).strip() s = re.sub(r"[^0-9,.\-]", "", s) if is_empty(s): return None # soporta "30.000,00" -> 30000.00 if "." in s and "," in s: s = s.replace(".", "").replace(",", ".") elif "," in s and "." not in s: s = s.replace(",", ".") try: d = Decimal(s) except InvalidOperation: return None q = Decimal("1." + ("0" * decimals)) d = d.quantize(q) us = f"{d:,.{decimals}f}" # 30,000.00 return us.replace(",", "X").replace(".", ",").replace("X", ".") # 30.000,00 def parse_container(raw): """ Soporta: - JSON string (dobles comillas) - Python literal string (comillas simples) - dict/list nativo (raro) Retorna (obj, kind) donde kind: 'json' | 'py' | None """ if is_empty(raw): return None, None if isinstance(raw, (dict, list)): return raw, "py" if not isinstance(raw, str): return None, None txt = raw.strip() # JSON try: obj = json.loads(txt) if isinstance(obj, (dict, list)): return obj, "json" except Exception: pass # Python literal try: obj = ast.literal_eval(txt) if isinstance(obj, (dict, list)): return obj, "py" except Exception: return None, None return None, None def serialize_container(obj, kind): if kind == "json": return json.dumps(obj, ensure_ascii=False) return repr(obj) # kind == "py" def format_amount_in_obj(obj): # Formatea amount in-place para dict o list[dict] if isinstance(obj, dict): if "amount" in obj and not is_empty(obj.get("amount")): f = fmt_es_money(obj.get("amount"), 2) if f is not None: obj["amount"] = f return if isinstance(obj, list): for it in obj: if isinstance(it, dict) and "amount" in it and not is_empty(it.get("amount")): f = fmt_es_money(it.get("amount"), 2) if f is not None: it["amount"] = f def extract_amounts(obj): # Devuelve lista de amounts (ya formateados) desde dict o list res = [] if isinstance(obj, dict): if "amount" in obj and not is_empty(obj.get("amount")): res.append(str(obj.get("amount"))) return res if isinstance(obj, list): for it in obj: if isinstance(it, dict) and "amount" in it and not is_empty(it.get("amount")): res.append(str(it.get("amount"))) return res return res # ========================= # 1) Montos directos (tus variables) # ========================= money_vars = [ "precio_cochera", "precio_baulera", "precio_departamento", "adicional_piso", "pago_sena", "pago_inicial", "valor_total_compra", "monto_saldo", "total_pagado", "saldo_a_pagar", ] for name in money_vars: val = rb_get(name) if is_empty(val): continue f = fmt_es_money(val, 2) if f is not None: rb_set(name, f) # ========================= # 2) cuotas_pre -> pre_amount # (puede venir dict o list o string) # ========================= rb_set("pre_amount", "") raw = rb_get("cuotas_pre") obj, kind = parse_container(raw) if obj is not None: format_amount_in_obj(obj) rb_set("cuotas_pre", serialize_container(obj, kind)) amts = extract_amounts(obj) rb_set("pre_amount", amts[0] if amts else "") # ========================= # 3) cuotas_post -> amount_post # ========================= rb_set("amount_post", "") raw = rb_get("cuotas_post") obj, kind = parse_container(raw) if obj is not None: format_amount_in_obj(obj) rb_set("cuotas_post", serialize_container(obj, kind)) amts = extract_amounts(obj) rb_set("amount_post", " | ".join(amts) if amts else "") # ========================= # 4) refuerzos -> amount_refuerzo # ejemplo: [{"date":"...","amount":5000}, ...] # ========================= rb_set("amount_refuerzo", "") raw = rb_get("refuerzos") obj, kind = parse_container(raw) if obj is not None: format_amount_in_obj(obj) rb_set("refuerzos", serialize_container(obj, kind)) amts = extract_amounts(obj) rb_set("amount_refuerzo", " | ".join(amts) if amts else "") # (Debug opcional para confirmar que el formatter anda) # rb_set("debug_format_test", fmt_es_money("30000.00", 2)) # => 30.000,00 main()