Files
KPK/stare server dump ucty.py
T
2026-06-23 15:20:56 +02:00

79 lines
2.3 KiB
Python

def load_ucty_for_select(
cur_pref: str,
id_kas: str | None = None,
closed: bool | None = None,
limit: int | None = None,
) -> list[dict]:
table = f"{cur_pref}_ucty"
out: list[dict] = []
where: list[str] = []
params: list = []
# --- filtr pokladny ---
if id_kas is not None:
where.append("id_kas = ?")
params.append(id_kas)
# --- otevřené / uzavřené ---
if closed is True:
where.append("closed_at IS NOT NULL AND TRIM(closed_at) <> ''")
elif closed is False:
where.append("(closed_at IS NULL OR TRIM(closed_at) = '')")
where_sql = f"WHERE {' AND '.join(where)}" if where else ""
# --- řazení + limit ---
if closed:
order_sql = "ORDER BY ucislo DESC"
if limit is None:
limit = 50
limit_sql = "LIMIT ?"
params.append(limit)
else:
order_sql = """
ORDER BY
CASE WHEN stul IS NULL THEN 1 ELSE 0 END,
stul
"""
limit_sql = ""
sql = f"""
SELECT
ucty_id,
id_kas,
ucislo,
stul,
blocked_by,
closed_at,
json_extract(data, '$.open_at') AS open_at,
json_extract(data, '$.storno') AS storno,
json_extract(data, '$.is_storno') AS is_storno,
json_extract(data, '$.origin') AS origin,
json_extract(data, '$.autor') AS autor
FROM "{table}"
{where_sql}
{order_sql}
{limit_sql}
"""
with get_db() as conn:
cur = conn.cursor()
cur.execute(sql, params)
rows = cur.fetchall()
for (
ucty_id, id_kas, ucislo, stul, blocked_by,
closed_at, open_at, storno, is_storno, origin, autor
) in rows:
out.append({
"ucty_id": ucty_id,
"id_kas": id_kas,
"ucislo": ucislo or "",
"stul": stul,
"open_at": open_at,
"closed_at": closed_at,
"blocked_by": blocked_by or "",
"closed": bool(closed_at and closed_at.strip()),
"storno": storno,
"is_storno": is_storno,
"origin": origin,
"autor": autor,
})
return out