askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
fringe_export.py · 2.8 KB · 65 lines
| 1 | """action_fringe_export -- the fringe-benefit register, as a payroll provider takes it. |
| 2 | |
| 3 | GET /objects/action_fringe_export?from=2025-01-01&to=2025-12-31[&format=csv] |
| 4 | |
| 5 | Hands over what finance recorded, row for row, for the benefits whose |
| 6 | period overlaps [from, to]: employee, benefit, period, value in cents, |
| 7 | taxable yes/no, how it was valued and from what. It computes nothing -- |
| 8 | no withholding, no grossing-up, no valuation. Those are the payroll |
| 9 | provider's and the accountant's, and a number this box made up would be |
| 10 | handed on looking exactly like one finance determined. |
| 11 | |
| 12 | Who may run it: role:finance and admins (the execute rule), and the object |
| 13 | asks again before reading, because it reads storage directly and is the |
| 14 | only gate in front of pay information about named people: the caller must |
| 15 | be an admin or read every row of the register whole |
| 16 | (object_access.Access.reads_every_row) -- an own-rows grant is not enough |
| 17 | for an export that is everybody's rows. |
| 18 | |
| 19 | CSV cells that a spreadsheet would run as a formula are prefixed with a |
| 20 | quote (object_expense_rules.fringe_csv): payroll staff open these. |
| 21 | """ |
| 22 | |
| 23 | import os |
| 24 | import re |
| 25 | |
| 26 | import object_access |
| 27 | import object_collections |
| 28 | import object_expense_rules as rules |
| 29 | import object_records |
| 30 | |
| 31 | _DATE = re.compile(r"^\d{4}-\d{2}-\d{2}$") |
| 32 | |
| 33 | |
| 34 | def _base_dir(): |
| 35 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 36 | |
| 37 | |
| 38 | def GET(request): |
| 39 | base = _base_dir() |
| 40 | access = object_access.Access(request.get("_identity"), base_dir=base) |
| 41 | if not (access.is_admin or access.reads_every_row("fringe_benefits")): |
| 42 | return {"status": 403, "error": "The fringe-benefit register is for finance and admins."} |
| 43 | start = rules.text(request.get("from")) |
| 44 | end = rules.text(request.get("to")) |
| 45 | for name, value in (("from", start), ("to", end)): |
| 46 | if value and not _DATE.match(value): |
| 47 | return {"status": 400, "error": f"{name} must be YYYY-MM-DD"} |
| 48 | try: |
| 49 | rows = object_records.read_collection_records("fringe_benefits", base_dir=base) |
| 50 | except object_collections.CollectionNotFoundError: |
| 51 | rows = [] |
| 52 | picked = rules.fringe_export_rows(rows, start=start, end=end) |
| 53 | if rules.text(request.get("format")).lower() == "csv": |
| 54 | return {"status": 200, "content_type": "text/csv; charset=utf-8", |
| 55 | "body": rules.fringe_csv(picked)} |
| 56 | return {"ok": True, "from": start, "to": end, "count": len(picked), |
| 57 | "total_value_cents": sum(int(row["value_cents"] or 0) for row in picked), |
| 58 | "taxable_value_cents": sum(int(row["value_cents"] or 0) for row in picked |
| 59 | if row["taxable"] == "yes"), |
| 60 | "columns": list(rules.FRINGE_EXPORT_COLUMNS), "rows": picked, |
| 61 | "note": ("Values are as finance recorded them; nothing here computes payroll or " |
| 62 | "withholding. Check with your accountant.")} |
| 63 | |
| 64 | |
| 65 | POST = GET |