MEGAINFRA-WEBAPP/scripts/pb-add-dn-item-units.py

66 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""
Adds qty_raw / unit_raw / unit_converted to the delivery_note_items
collection.
qty_received keeps its name but changes meaning: it becomes strictly
"in the purchase order line's unit". qty_raw and unit_raw preserve what
the slip actually said, so the audit trail survives the conversion and
the UI can show both.
Idempotent — re-running on a collection that already has the fields is
a no-op.
Credentials come from .env.local via pb_env.
"""
import sys
from pb_env import PB, http, superuser_token
def get_collection(token: str, name: str):
code, resp = http("GET", f"/api/collections/{name}", token=token)
if code != 200:
sys.exit(f"GET collection {name} failed: {code} {resp}")
return resp
def main() -> None:
token = superuser_token()
print(f"\n→ delivery_note_items on {PB}")
coll = get_collection(token, "delivery_note_items")
fields = list(coll.get("fields", []))
existing = {f.get("name") for f in fields}
wanted = [
{"name": "qty_raw", "type": "number", "required": False},
{"name": "unit_raw", "type": "text", "required": False},
{"name": "unit_converted", "type": "bool", "required": False},
]
additions = []
for want in wanted:
if want["name"] in existing:
print(f" • {want['name']:15s} already exists — skipping")
else:
additions.append(want)
if not additions:
print(" • no changes needed")
return
code, resp = http(
"PATCH",
f"/api/collections/{coll['id']}",
{"fields": fields + additions},
token,
)
if code != 200:
sys.exit(f"PATCH failed: {code} {resp}")
for a in additions:
print(f" ✓ added {a['name']}")
if __name__ == "__main__":
main()
print("\nDone.")