Move to FastAPI with accounts, SQLite books, export and tax returns

Storage and export
- Keep books and transactions in SQLite (data/cashbook.db) instead of
  .ods files; existing .ods books are migrated once at startup
- Export a book as the cashbook spreadsheet (.xlsx/.ods, built from the
  template via LibreOffice) or as CSV
- GST rounded per transaction to match the spreadsheet

Tax
- GST return page with IRD GST101A boxes 5-15 per six-monthly,
  two-monthly or monthly period, due dates, and a Zero-rated GST type
- Income tax page with IR10 (financial statements summary) and IR4
  (company return) boxes, from the cashbook plus year-end amounts typed
  in and saved per book

FastAPI and accounts (following led-editor)
- Replace the stdlib server with a FastAPI app (src/cashbook_app) run by
  uvicorn; dependencies in Pipfile
- Cookie sessions with bcrypt passwords, first-run admin setup or
  BOOTSTRAP_ADMIN_*, invites (link or email), sign-in throttling,
  password change that signs out other sessions
- Users page for administrators: rename, reset password, promote or
  demote, delete, list and revoke invites; last admin is protected
- Replaces the shared CASHBOOK_PASSWORD basic auth; AUTH_* settings in
  .env (see .env.example)
- Docker image uses a venv with system site packages for python3-uno
- pytest suite for auth, user management, imports, GST and tax figures

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-10-24 22:43:18 +13:00
1 parent 17b48f25e8
commit 4b3e5f0932
47 files changed
+4189 -862

No files matched your search

+2 -2
View File
@@ -3,8 +3,8 @@
"configurations": [
{
"name": "cashbook",
"runtimeExecutable": "python3",
"runtimeArgs": ["src/webapp.py", "--port", "8765"],
"runtimeExecutable": "pipenv",
"runtimeArgs": ["run", "dev"],
"port": 8765
}
]
+2
View File
@@ -1,3 +1,5 @@
*
!src/
!Pipfile
!Pipfile.lock
**/__pycache__
+28
View File
@@ -0,0 +1,28 @@
# Copy to `.env` next to compose.yaml (it's git-ignored) and adjust.
# --- Sign-in ---
# AUTH_ENABLED=true # require sign-in (set false only on a trusted machine)
# AUTH_REGISTER_OPEN=true # allow creating accounts at /register
# AUTH_INVITE_ONLY=true # ...but only with an invite link from an administrator
# AUTH_SESSION_DAYS=14 # how long a sign-in lasts
# First run: create this administrator if there are no accounts yet.
# Leave unset to create the first account in the browser instead (it becomes the admin).
# BOOTSTRAP_ADMIN_USERNAME=admin
# BOOTSTRAP_ADMIN_PASSWORD=change-me
# --- Invites by email (optional) ---
# PUBLIC_BASE_URL=https://cashbook.example.com # used in invite links
# SMTP_HOST=smtp.example.com
# SMTP_PORT=587
# SMTP_USER=mailer@example.com
# SMTP_PASSWORD=app-password
# SMTP_FROM=Cashbook <mailer@example.com>
# SMTP_TLS=true
# --- Docker ---
# CASHBOOK_USER=1000:1000 # uid:gid owning ./data; use 0:0 with rootless Docker
# CASHBOOK_BIND=127.0.0.1 # host address to publish on
# CASHBOOK_PORT=8765
# CASHBOOK_ALLOWED_HOSTS= # extra host names, if a proxy doesn't pass Host through
# FORWARDED_ALLOW_IPS=127.0.0.1 # proxies trusted for X-Forwarded-Proto
+2
View File
@@ -6,3 +6,5 @@ __pycache__/
data/
.env
compose.override.yaml
.venv/
.pytest_cache/
+22 -8
View File
@@ -1,16 +1,30 @@
FROM debian:bookworm-slim
# LibreOffice (headless Calc) + its Python bridge, and pdftotext for PDF statements
# LibreOffice (headless Calc) + its Python bridge for spreadsheet export,
# pdftotext for PDF statements
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
python3 python3-uno libreoffice-calc-nogui poppler-utils fonts-dejavu-core \
python3 python3-venv python3-uno libreoffice-calc-nogui poppler-utils fonts-dejavu-core \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# a venv that can still see the system python3-uno
COPY Pipfile Pipfile.lock ./
RUN python3 -m venv --system-site-packages /opt/venv \
&& /opt/venv/bin/pip install --no-cache-dir pipenv \
&& /opt/venv/bin/pipenv requirements > /tmp/requirements.txt \
&& /opt/venv/bin/pip install --no-cache-dir -r /tmp/requirements.txt \
&& /opt/venv/bin/pip uninstall -y pipenv \
&& rm /tmp/requirements.txt
COPY src ./src
# LibreOffice needs a writable home; /tmp is the only writable place besides /data
ENV HOME=/tmp \
ENV PATH=/opt/venv/bin:$PATH \
HOME=/tmp \
CASHBOOK_DATA=/data \
CASHBOOK_STATEMENTS=/statements \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
@@ -18,9 +32,9 @@ ENV HOME=/tmp \
USER 1000:1000
EXPOSE 8765
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
CMD ["python3", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8765/healthz', timeout=3)"]
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8765/healthz', timeout=3)"]
# /data holds the cashbooks and statements/;
# /statements can hold a read-only folder of ANZ exports
CMD ["python3", "src/webapp.py", "--host", "0.0.0.0", "--data", "/data", "--statements", "/statements"]
# --proxy-headers: trust X-Forwarded-Proto from the hosts in FORWARDED_ALLOW_IPS
# (so cookies are marked Secure behind an HTTPS proxy)
CMD ["uvicorn", "app:app", "--app-dir", "src", "--host", "0.0.0.0", "--port", "8765", "--proxy-headers"]
+18
View File
@@ -0,0 +1,18 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
fastapi = "*"
uvicorn = "*"
sqlalchemy = "*"
bcrypt = "*"
[dev-packages]
pytest = "*"
httpx = "*"
[scripts]
dev = "uvicorn app:app --app-dir src --reload --host 127.0.0.1 --port 8765"
test = "pytest tests"
Generated
+516
View File
@@ -0,0 +1,516 @@
{
"_meta": {
"hash": {
"sha256": "47119175f9f01a429431ed06baae2e53be90fefd8a53c7a73a273db3ea065845"
},
"pipfile-spec": 6,
"requires": {},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"annotated-doc": {
"hashes": [
"sha256:117bac03a25ede5df5440e855b32d556049ca169ead221505badf432fed4b101",
"sha256:c7e58ce09192557605d8bbd92836d7e1d520ac9580096042c0bfd197efacf1bb"
],
"markers": "python_version >= '3.9'",
"version": "==0.0.5"
},
"annotated-types": {
"hashes": [
"sha256:13b2beaad985e05e2d6407ee4c4f35590b11f8d693a258a561055cac8f64cab7",
"sha256:f072f4d804ea359e4eaf198b1af7a8b0943881a87f31bb764f8bf219bb9419e0"
],
"markers": "python_version >= '3.10'",
"version": "==0.8.0"
},
"anyio": {
"hashes": [
"sha256:6152fdbbf9a77fdec97731721bebf7c4c44f7c29b424b0065826173efc7ed101",
"sha256:9f28306018cbd6d329e64a36d58256edff76dd996fe423bc957326e578b82a94"
],
"markers": "python_version >= '3.10'",
"version": "==4.15.1"
},
"bcrypt": {
"hashes": [
"sha256:046ad6db88edb3c5ece4369af997938fb1c19d6a699b9c1b27b0db432faae4c4",
"sha256:0c418ca99fd47e9c59a301744d63328f17798b5947b0f791e9af3c1c499c2d0a",
"sha256:0c8e093ea2532601a6f686edbc2c6b2ec24131ff5c52f7610dd64fa4553b5464",
"sha256:0cae4cb350934dfd74c020525eeae0a5f79257e8a201c0c176f4b84fdbf2a4b4",
"sha256:137c5156524328a24b9fac1cb5db0ba618bc97d11970b39184c1d87dc4bf1746",
"sha256:200af71bc25f22006f4069060c88ed36f8aa4ff7f53e67ff04d2ab3f1e79a5b2",
"sha256:212139484ab3207b1f0c00633d3be92fef3c5f0af17cad155679d03ff2ee1e41",
"sha256:2b732e7d388fa22d48920baa267ba5d97cca38070b69c0e2d37087b381c681fd",
"sha256:35a77ec55b541e5e583eb3436ffbbf53b0ffa1fa16ca6782279daf95d146dcd9",
"sha256:38cac74101777a6a7d3b3e3cfefa57089b5ada650dce2baf0cbdd9d65db22a9e",
"sha256:3abeb543874b2c0524ff40c57a4e14e5d3a66ff33fb423529c88f180fd756538",
"sha256:3ca8a166b1140436e058298a34d88032ab62f15aae1c598580333dc21d27ef10",
"sha256:3cf67a804fc66fc217e6914a5635000259fbbbb12e78a99488e4d5ba445a71eb",
"sha256:4870a52610537037adb382444fefd3706d96d663ac44cbb2f37e3919dca3d7ef",
"sha256:48f753100931605686f74e27a7b49238122aa761a9aefe9373265b8b7aa43ea4",
"sha256:4bfd2a34de661f34d0bda43c3e4e79df586e4716ef401fe31ea39d69d581ef23",
"sha256:560ddb6ec730386e7b3b26b8b4c88197aaed924430e7b74666a586ac997249ef",
"sha256:5b1589f4839a0899c146e8892efe320c0fa096568abd9b95593efac50a87cb75",
"sha256:5feebf85a9cefda32966d8171f5db7e3ba964b77fdfe31919622256f80f9cf42",
"sha256:611f0a17aa4a25a69362dcc299fda5c8a3d4f160e2abb3831041feb77393a14a",
"sha256:61afc381250c3182d9078551e3ac3a41da14154fbff647ddf52a769f588c4172",
"sha256:64d7ce196203e468c457c37ec22390f1a61c85c6f0b8160fd752940ccfb3a683",
"sha256:64ee8434b0da054d830fa8e89e1c8bf30061d539044a39524ff7dec90481e5c2",
"sha256:6b8f520b61e8781efee73cba14e3e8c9556ccfb375623f4f97429544734545b4",
"sha256:741449132f64b3524e95cd30e5cd3343006ce146088f074f31ab26b94e6c75ba",
"sha256:744d3c6b164caa658adcb72cb8cc9ad9b4b75c7db507ab4bc2480474a51989da",
"sha256:79cfa161eda8d2ddf29acad370356b47f02387153b11d46042e93a0a95127493",
"sha256:7aeef54b60ceddb6f30ee3db090351ecf0d40ec6e2abf41430997407a46d2254",
"sha256:7edda91d5ab52b15636d9c30da87d2cc84f426c72b9dba7a9b4fe142ba11f534",
"sha256:7f277a4b3390ab4bebe597800a90da0edae882c6196d3038a73adf446c4f969f",
"sha256:7f4c94dec1b5ab5d522750cb059bb9409ea8872d4494fd152b53cca99f1ddd8c",
"sha256:801cad5ccb6b87d1b430f183269b94c24f248dddbbc5c1f78b6ed231743e001c",
"sha256:83e787d7a84dbbfba6f250dd7a5efd689e935f03dd83b0f919d39349e1f23f83",
"sha256:89042e61b5e808b67daf24a434d89bab164d4de1746b37a8d173b6b14f3db9ff",
"sha256:92864f54fb48b4c718fc92a32825d0e42265a627f956bc0361fe869f1adc3e7d",
"sha256:9d52ed507c2488eddd6a95bccee4e808d3234fa78dd370e24bac65a21212b861",
"sha256:9fffdb387abe6aa775af36ef16f55e318dcda4194ddbf82007a6f21da29de8f5",
"sha256:a28bc05039bdf3289d757f49d616ab3efe8cf40d8e8001ccdd621cd4f98f4fc9",
"sha256:a5393eae5722bcef046a990b84dff02b954904c36a194f6cfc817d7dca6c6f0b",
"sha256:a71f70ee269671460b37a449f5ff26982a6f2ba493b3eabdd687b4bf35f875ac",
"sha256:b17366316c654e1ad0306a6858e189fc835eca39f7eb2cafd6aaca8ce0c40a2e",
"sha256:baade0a5657654c2984468efb7d6c110db87ea63ef5a4b54732e7e337253e44f",
"sha256:c2388ca94ffee269b6038d48747f4ce8df0ffbea43f31abfa18ac72f0218effb",
"sha256:c58b56cdfb03202b3bcc9fd8daee8e8e9b6d7e3163aa97c631dfcfcc24d36c86",
"sha256:cde08734f12c6a4e28dc6755cd11d3bdfea608d93d958fffbe95a7026ebe4980",
"sha256:d79e5c65dcc9af213594d6f7f1fa2c98ad3fc10431e7aa53c176b441943efbdd",
"sha256:d8d65b564ec849643d9f7ea05c6d9f0cd7ca23bdd4ac0c2dbef1104ab504543d",
"sha256:db99dca3b1fdc3db87d7c57eac0c82281242d1eabf19dcb8a6b10eb29a2e72d1",
"sha256:dcd58e2b3a908b5ecc9b9df2f0085592506ac2d5110786018ee5e160f28e0911",
"sha256:dd19cf5184a90c873009244586396a6a884d591a5323f0e8a5922560718d4993",
"sha256:ddb4e1500f6efdd402218ffe34d040a1196c072e07929b9820f363a1fd1f4191",
"sha256:e3cf5b2560c7b5a142286f69bde914494b6d8f901aaa71e453078388a50881c4",
"sha256:ed2e1365e31fc73f1825fa830f1c8f8917ca1b3ca6185773b349c20fd606cec2",
"sha256:edfcdcedd0d0f05850c52ba3127b1fce70b9f89e0fe5ff16517df7e81fa3cbb8",
"sha256:f0ce778135f60799d89c9693b9b398819d15f1921ba15fe719acb3178215a7db",
"sha256:f2347d3534e76bf50bca5500989d6c1d05ed64b440408057a37673282c654927",
"sha256:f3c08197f3039bec79cee59a606d62b96b16669cff3949f21e74796b6e3cd2be",
"sha256:f632fd56fc4e61564f78b46a2269153122db34988e78b6be8b32d28507b7eaeb",
"sha256:f6984a24db30548fd39a44360532898c33528b74aedf81c26cf29c51ee47057e",
"sha256:f70aadb7a809305226daedf75d90379c397b094755a710d7014b8b117df1ebbf",
"sha256:f748f7c2d6fd375cc93d3fba7ef4a9e3a092421b8dbf34d8d4dc06be9492dfdd",
"sha256:f8429e1c410b4073944f03bd778a9e066e7fad723564a52ff91841d278dfc822",
"sha256:fc746432b951e92b58317af8e0ca746efe93e66555f1b40888865ef5bf56446b"
],
"index": "pypi",
"markers": "python_version >= '3.8'",
"version": "==5.0.0"
},
"click": {
"hashes": [
"sha256:255bc9599cf7748b4b1a446ccc735421bd08a2ae529a8b88597d3de5664ee360",
"sha256:ba0d2089de75ea0310e2dde03160e6ca10009947fb95a182f9b54021bb272e34"
],
"markers": "python_version >= '3.10'",
"version": "==8.5.0"
},
"fastapi": {
"hashes": [
"sha256:3785963de305dd09912426cb81f2d230fce98ab3521cdfe97265c8d14983128c",
"sha256:6a22c1e2e6e32e0633ee7e170f00009a29b8228b16feb219ee42f32fd8d88a8f"
],
"index": "pypi",
"markers": "python_version >= '3.10'",
"version": "==0.162.0"
},
"h11": {
"hashes": [
"sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1",
"sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86"
],
"markers": "python_version >= '3.8'",
"version": "==0.16.0"
},
"idna": {
"hashes": [
"sha256:a7db850025b95ded1eae8a46181a1a6c56c92c96f0e2b005d9ff8dc0210cab44",
"sha256:ab7ae7122974553370f0bdb919e1a960b2cd1bc1ef0276416d896db81c14582c"
],
"markers": "python_version >= '3.9'",
"version": "==3.20"
},
"opentelemetry-api": {
"hashes": [
"sha256:aa38ed19bcc084ba42782a73255b3582283eced7ad6dddbd6695189e69adfb75",
"sha256:b31553efa588ae44bc306f863c785c5333a9ecc091248c6ee68b4b6c87fdedfb"
],
"markers": "python_version >= '3.10'",
"version": "==1.45.1"
},
"pydantic": {
"hashes": [
"sha256:9195d967ec791692a04438115466764fb8b9a27b31f14a760437694f40d6b454",
"sha256:94f478203dd03404682a1ada216965651dd74b1d2d5ffd62e00e0837caab5c26"
],
"markers": "python_version >= '3.10'",
"version": "==2.14.1"
},
"pydantic-core": {
"hashes": [
"sha256:0036473f5583e6a60e50b8b21651511564277a3f05cc5dab8cf579f552cd5f6c",
"sha256:0048b6dddc8ef4b64fccaad878bd143b0c3882ea9936279dc11d613f6b7dd1bc",
"sha256:009634b83993777ddcd69cad0ffcace43dabde692109528e35f0fde91e386a8b",
"sha256:028e2f212273d4a39b1ec1e0de8166b1165a65fc0f1111452a9d94fc7c625c63",
"sha256:062e891facce5ca296a1c37098e5e466780457f86413894b399f0cf22934f769",
"sha256:06e01fbbfdb9be777b316a71b6c49efaf4a08b615d0a98d678cda3023f79d019",
"sha256:06ead20d39ffd6f2f6f2a8f8a6de67ff8bb1b4f14a8a30e058502514ee2ac685",
"sha256:0b3a6f334c6a2345ca15318ff894502a90012536404b37c844a976c76c846e0b",
"sha256:0c003c3b7f49debb893d2d85ae099ac5959c9839e2f330fadb1fcdf7a6594482",
"sha256:131059670f1d2444269b8585cb888963994871932447c08b39ac6a51fcfef658",
"sha256:132529c83901437ff642f585216831bf5fd7a91df66829907e155192ead62498",
"sha256:17e722e156d0444ecaefbe640bdb60928752bf2013e2b7a11cdb099aaae19bec",
"sha256:1a9006395dece0e32e704c315eff8a00bede494f6108546cfc5539c89fef4f9a",
"sha256:1c8632d4ac04e6f91128fca584b3a8a507d81604c24eeaaad00d4be42765c32b",
"sha256:1c96fd793b73d1b92e65570132505498fe7b21eaef73cdf74e67e5dfba7ac9e4",
"sha256:1cf41f1ae3fa155cf167a72689ad044bcc1e3c97e064123677149bdfb5dafc4a",
"sha256:1deeacb112d14d3f4fcb16b165f7dbaf76c70ba6e82f37ba042bdab51970a0b8",
"sha256:1ef800dd7d85bcdadf4c3076e4c94e43939493558a3b69a1ea830c706d4617bb",
"sha256:1fa4c8bc12c1354c5550c0c35c1852c8c1901e89e06561724e03f8d0342e1f87",
"sha256:2005207aafe1231315718bf6ed5d064a7300fb4772754af35ee72fc68159492e",
"sha256:23923ab9292c40da026330b1ecf4dc2618c8e86e0422e5d1fbf50d94d64ca4f8",
"sha256:23edad659e8dbd8ca7e4e877fe6c81573abbdf215bd25a68b53e1272f58b80c7",
"sha256:2ab756b72bd5054e4c7ef3ded331b35786cbd3cf931531a508f79a9537517064",
"sha256:2cbd1b75b09e976ed0d6b6ca297675632ca35df86130088457cdc60ef36970ae",
"sha256:2cf91809d0721ab81592ba67bea7694821679c10b1a2e3c3460082b286c1918a",
"sha256:2df1ff41884de2bc4b307bafd7c40a691094fad2ff8e767e5b45a319257bcf4e",
"sha256:2eb75304506894a281d346220a4f7481a1b8729577c5ed2a05395991966a8396",
"sha256:2eedf82ee4753cdab8e50044c6bd569577eebc3859b11fecf4eb9223761ff966",
"sha256:30ddf019d082c117b5d309e5b86710c2a78909907ec1a9381feec3eec02eca0b",
"sha256:325c23f3e35cfbf0fe3486fa5f7260d1e45885173002d30a28ca019994124255",
"sha256:32fad3a91e51b6d2039c572db04a5a873260b399f6bd62c3552671fa7a4a2899",
"sha256:36c426eac0af8d1529ff8467e612b933346caec1fdc0d774f78f67a1a11e16c1",
"sha256:3a5fce22f1e87d181e924e12da7d81cfe031fb3881a5ddf26ad28f141756ca43",
"sha256:3aa9de446b793de2beb6fa2d9d0961803126c4e2a99c2f25ab59b9fd6ea125c0",
"sha256:3e46a9eb0a0901dd6275e6b06ac3a464885ef350ec4121fe486869de8053e4bb",
"sha256:3fde4fdc6487a58d944ca87cf5adc95d5f266e872c19599f5f4c0a8a1b1f9f9f",
"sha256:409e0ea40ec30d9158f33574fd758e689f6045a0f2596701828c27816ca9687d",
"sha256:40f523349960fa30f3ea51404308ff50f9997a90df639590f47a057c1f32b415",
"sha256:41bc8237121bd8dc8d888dfd6279fc166ffc88c1f1bf3a8bf00869680533ca4c",
"sha256:42b54c2c90ad348b5e3a85e03e715d572c1fde357ef104cdfe3b03b697a404ea",
"sha256:455a773617b5913bf5c20d0692e5787b119e52c4d40ea644ca31f5758fd31be2",
"sha256:45b11cac094aa25725581d9304eee93c9028516b9ea80dd9e175e13a5a2c840e",
"sha256:45c6266d071c241f2a168d45bf8c54344f0effce35e7e6b73afdec11f3687568",
"sha256:46b3301d3b5c886f77de7546e47274a5842c622ea2020b8c6524c6b66913b4a6",
"sha256:476f6ed8e43cd1e0b460920e23571700872b284e77331cb30c4faf459cf48a4b",
"sha256:48569b0ade9edfbe065cad1d700175546592aebbb42f02adcebcc26e75b896fe",
"sha256:49c2cbb2397fe4d0987e84606e691af6cb87bc0ee1bd3e7b737f7e10b4c142f9",
"sha256:4a53d13cdfbedbfa87f08b83c1a0a5efcc767d785a4b41934fa9cb672670493a",
"sha256:4be846f55c9477f5f3ddde8f2ce941137e16862a56d018ed885d422bb6ae02f2",
"sha256:4df197990c15b5a37c5a277d131d9f2c67de6133f2e5dafd80d9bba4b99f46f9",
"sha256:4e834f6a8e4ff772dcc34f58ef5504147a3ea5b0f4eeb13b0f8eb2ca75ac57f1",
"sha256:57f51b31ff826e2859120cf4737c5a758a48d96f3e97da40ccee1796d58078ff",
"sha256:5958c72adb417c39b12ac87525ac60b0d73315fcdc59e21f44ee4a5e2512c9ef",
"sha256:5dfe41f232befddb9c4377f6cfc702b51595e2d78ed082672adf8758d2c4619f",
"sha256:5f3cae32fc46121f787cb2486de9cf95a8bf72aec5cc78f64c606fa1735a6ef5",
"sha256:64f6047f62a6c5ae08d0a6afb035667aa2d97c3d20d69762e034c5ea144d92a5",
"sha256:6a733778df2f7087ec1100ed0b41533e4f3001976e99570fa34f57c66e7f8e3e",
"sha256:6b20a4bffabdad0db2927ac034ae3b8a681b1f7a0182f3e60b479ad2fde21ebb",
"sha256:6dbcbee53bf17196a7f745aa9bf5a9603953a1e365b1f020be3207c676a3e7c4",
"sha256:6ed4f3cef55164b026fefb41341b7754cc6b624c75dfe7142d2ecceb5ad21c87",
"sha256:704075d10b74f2f3c6e15407c696d88701df35fc8953f434a431add0d0074db0",
"sha256:739dc730e6be3bd5ec2f4ab5cfc7eb047cc45fc1497b3bafec74ff2ed07df597",
"sha256:7456d699b13954e9c0164dcb267250a10ae0dfb03e6e26d6796ab0d46e189c84",
"sha256:756d669f04e62ec4148ecfe22be6a4484d9b1181a6ef32e205ebfd200540858b",
"sha256:7689580e72a642ab5ec64d5f55b2e33636fa43b4ebe63c0c2c965ef307c7d1aa",
"sha256:76e2e83fa6ec8cdc972d438dafc2522b3a47bee4ec0ae668b29cfb1977ab5242",
"sha256:7816e98acc08119dc0f340ab167048ecc54126316330c1f0caf7c6756c88e28f",
"sha256:79490e33c4c0fcb933bbbcfc3a62184d8803b99f535863dfbb925e1bcb6945ad",
"sha256:7f476456ac2bb0d937f75191494a09c83a30765fea4f70f3b404942fe25f6cdf",
"sha256:844b869f118e22a41a091bdcedda8a71bc1b0f62c38d1a0c3211cece47e1d8fc",
"sha256:84bc765b282a9d5b7fe0348b8648904f25a6a04b2139da52b1dd30c8ac3a2c8f",
"sha256:84f34323a61a365b4e9295de6028474754829aaddd59c7bf1a040e7487ef8f3c",
"sha256:8812592c85d0edf423f10eadcef42716d71e8219085ad9e85b775057b7306133",
"sha256:88e492e8b9d0312e7dc13667c30222abf284dc3b79b5302b3607b41a5784ce61",
"sha256:8a6791afa2245e6c6b180122d105941644f5bd410bb18623b408808cc41a3102",
"sha256:8b4c3df25bd323bf1d36a648d563cf1fc69d717451569927151bdad7cad07a77",
"sha256:8daa7ee75245d43ad7d747e5c9ecc1b1d06552f72b14887e9276f787d57375f4",
"sha256:93ba4e9d8210d941c200431a56b2c0400b131865947903937ed3ec5404307d2e",
"sha256:94845ff54dc5193f228cab81b2662a04bfbb892e95bdc15edf7399000ce57d54",
"sha256:94be440c03fede26969a5ce75468e0e6a9927a1b46d9b679ee8adc1b057b0350",
"sha256:9572c1369e9c9da2d64a7b7992c786d90ff295abc93964cfe3125e4290768070",
"sha256:983a662de2571cb2502fc8ff47b6770b03d025d2eb314c92f77b3f07c74720ed",
"sha256:992c3514ec891fa7858099183e4d64e6bd5a5d4ff452fae29df22faa77a006bb",
"sha256:99ba9bc2b8062ea0c326a990f7f00e6530c23579de66dd246e72c4cafef950a5",
"sha256:9d1bed94af6a63835461f3cf7502058eb166c58c4778e11d0f433cfb1bd69e19",
"sha256:9e4472072de0137ee0d8e72d6620e85939c271d2f90f6bbb4b15c24638b79f92",
"sha256:a27c09d86600f1bf2fe3f37e1ae697faf3143931c09322cd799da94deee923b5",
"sha256:a29a061fec0b4e2d714f277e70a3a18125ecff803f2fea6eade2f2e53711d112",
"sha256:a3cda0e538208e5d722bbf3698b24f19c0a7d05bc8d5f8a7f9b121ea7fa243d9",
"sha256:a44101320cfe99432db74237545a63057dc7a88dfe792cbcad0647f2af56cb81",
"sha256:a4aaaa791bdae1c972a7e81765f4f3571c926b8e0b9b6e47346499fb80079665",
"sha256:a51eee75939cf811ac09b278745a6cee7dc873ccfbc8b9af3cc88fe4b7ce25b5",
"sha256:a7c58106de36ac6a56314182958de20db8d3a29dfd5db527192cc754e4f8e7fb",
"sha256:aa8224f10880d9bf1b5993988ba153d42a8b4f3f4f511f93b1f09c93ff613c72",
"sha256:acbf31f37c53a5ac0c34706c80b4f5107ba20b05fdd3816124bf236ef0c57dd2",
"sha256:adc06d218a1cadfd2ec4628424d7d79ce4eba69c2965e7e7b55106f0da5208c8",
"sha256:ae28183297fb0d2b8dc46a1f01d51f5e45825fc5afe76a835a6cb7fb34821295",
"sha256:b0135bcdcaa0f23573f286e4cb5e0fd2962700964ed13df085b85f2b97aeab9e",
"sha256:b087b1c5be7ac687cf22eabfe4b6b608d40df23610651e93611e1f49118baf84",
"sha256:b0d955195bbbe489ad343fcc956eacea9357b79cb22192c66cacdefcbc14b32f",
"sha256:b281a3b0f0822618fe5e3e0d8a2048b6356b14388505dc9374ccffeb69989713",
"sha256:b6d0c2183008c188e19f4906d426b293bdc4f67ab17df8e180fe16cda208fa71",
"sha256:bbce99252ba3167b2b6277f1829d5bf4b43b754524bddf7f944707c3db7d2253",
"sha256:bc1f08f68dac9f9e83845a8039880aba2ab553eb9b2259c3243a313182c253fe",
"sha256:bc94f474417604bd383d2cd445d071b07dd55fedceed3ce33407bf1fcc107290",
"sha256:bed5163e03b98bc1fa2eb05d74c63d9c5c95d8ed6254985481640fbf5e237dea",
"sha256:c17799a62c142d61b8a3c51752a7cbc87fe2ad4ccfab10e628a77b405075c662",
"sha256:c18db21573bd2c6489f9a544b7499f0df2853958c568e5e783536ee1f690af41",
"sha256:c3ede305158e75510be50869b319550ab072008c13d64d4ab1e094fb286b6f44",
"sha256:c516cc5367ca3448995d42cb994bf3f4c9002d2a7c22eac9622551269ad1b807",
"sha256:c531166c42ea7bdfecc8c50049581f05dd1993b09cc7c52bb36a14e96deaec7d",
"sha256:c73622ef819328873b53109ee4f77ceb598bffedd02daf916102be3228866b78",
"sha256:c8dce1f1e0e5358b682a6ad3fa5e31b31d4560997b8e61417e9217c8d60f8a0c",
"sha256:cb57f304525a5e3c13333b772bf9a473f36326e9c821b2e8e1b2fd36f80ae2c3",
"sha256:ce8c25ca38cc0e3d7753ba180808de2c0c8cb24eae0df64491e40921454e9831",
"sha256:ceff0acc940be2715bd6ad17b24c0e5304abf44f6efd0f81ee8499e640f9dc86",
"sha256:cf356f70551d40374eaffb1aa63f1eb6d2006681cbd7a9faea173ce0f4dd7cd2",
"sha256:d2d82aa62521c55ddfb000ae70f88cdd8de974078f6024e821dfe5addd0c818f",
"sha256:d32f3acc081cc3923386d88f422cde8892335e95f034e0104bb4cf9310d9915f",
"sha256:d4193206b6587047437f6f11d7e776df23e1c1e23af2a54d9347275614791e10",
"sha256:d5c0e32fdbce7f1e8ef4d11f655694bf5f4175c757a9f1dc2be09b8864e5bcf5",
"sha256:d5e062c01286d861fd6a1c4ff6e063547b3e713067f2df033c0ff97ac2ca006b",
"sha256:d8f9e8a6c4ab04b78d61f78627370d834eb004b2869dcb28cfffa647b4ea1980",
"sha256:d939de9c82e2126f7f48a7e658f8a85ed46d57662d53f44c49b8895fe94a3eb7",
"sha256:de531ce1e2a3364e8767878b58f4ff728a434b4fde089781fe30b1e08e2396e0",
"sha256:e50d7b94baac6c7d09927fa5ca5800a0c7ee5015c7fcff65beb3a1931b5a6e09",
"sha256:e5faeaee74a57d32b3ab3aebad2e348f06d3ba946fc5d28c1728455f00a3d13a",
"sha256:e6f0cc1bb9900dc558960894adeb30b0c083366fc1d69b856209fb2ca5c36fe5",
"sha256:e8e1d6ce820aa23317e8209a86bd65a540973c12dc7552b48a4f6c8e9926815e",
"sha256:ed1e728b39a383c81035b2459cfcb35d99dfb01f7d6ebe3a913bc1cc5b81e459",
"sha256:ee6db2fbed51a7991302e8fac498cd67e336246026d0dfa84cf5166ce1412760",
"sha256:efbecf43d321f7b9281441f1f213f7c21c66988b0e06c2730ba13ed47a46bb08",
"sha256:f2c634642694e6a0dad2ab1d375589fa671fd442edd5caf7d9737b8f6ca22906",
"sha256:f3377c8c2b3ce898423c5e5dd94c7982e30aa7717a7e6ab2470b9de364963709",
"sha256:f5187624823423e1d1b82b1072ac41dc837389e18d3d0572cc19bbee46cd550a",
"sha256:f77ac30b19221cd9bd3fcfa3d4614eff93140d0572ab730cded17b64adca05f3",
"sha256:fe90228920fd8ff2be62622b6bb8a2b11acd65046d50c6b130614b5879605a20"
],
"markers": "python_version >= '3.10'",
"version": "==2.50.1"
},
"sqlalchemy": {
"hashes": [
"sha256:015bd7b0687b5feaecb9988bc2f36ecb39daca83bd806b094b43b19ea503b0a9",
"sha256:048d1beb9bfd7ca1afbcfdb8da9519712827df6cccc80a225715ce387d37bb3b",
"sha256:0534d3f41295a74d9fe2e0838e06ce6329a0855b2965df5f5d1c4d3f6f1406d0",
"sha256:0bada6bd29ae58a0581bf6977c0a43dfd67388fd09129f1475c37eca370cccd8",
"sha256:0d0424a4189f95886c78b48bb500a1dd7ae6dbae76deb552b15813dc298d9c8f",
"sha256:106878cf3795cd3d357e2e858609f9e44f20f19b2d8e48a3532bef3f90b03fbe",
"sha256:10a4fe106adbb46c9452f750d8a5d4013d9a7e9752f63631c3b255c062b748d0",
"sha256:17bc5f508f5e5cee2722a5a14af0a48768b40e1fc5d1bfc92abf6ac587636bd7",
"sha256:1927eabf26265ed962627e7a64626d9058da29cc0faea341091302ec108ce305",
"sha256:1b5885ad1226c8eeea6aeabb4c4036e43202e1bd70cc7a50ece98bcd6ebe5566",
"sha256:1e12763cb2a400879c6dfe34995a032f707de635c8cad14d6ba777a1250be90b",
"sha256:1ff897d13de613768123dee68474956a51fe641c42bba7af6cb7776716a8dbcb",
"sha256:231d0fd85866f847c34c915116c386c83e677ab6f79a94c835b7ae7313b86095",
"sha256:35348ffcbf686055ea7fd02ba4eb76060ed76761648720c63170c5c8bccd85de",
"sha256:3974e33fc322b645eedeac8a3dea93022466fee41a6385c744a6656aba293cd4",
"sha256:39ca3a128260fb3aa1c33554f1993bc5ddf3f82375697028c49e7b411ab80b84",
"sha256:3d877b2c46298825e8f32059e8d912d0d100af0e9fefe31fb2abb52192817afb",
"sha256:499eae5c9051efe96cb9a73ec2fbde8ffa4186f102b9094ebab8fcc9d3b99f52",
"sha256:5a9b4cbc5f5cda3c9429e1ac503a57f3b95c59886f26af6ffb44e2e7628b7df8",
"sha256:5f9400b1234555b9ae78450123dec7aaf045d8d978a1e94967437ebe561ae120",
"sha256:61dc3fa488208a2f03e0e25ad32950dc024545291acc7348f2e550ea1bc5abb4",
"sha256:641a3742dac4b8f325667b585097de0f1d20eb3498506c92d5f04c20b3a6c823",
"sha256:650f97996a4c529f4930969f3ba11bcdb57ff160ed1cb87ea0174393bf4f5f73",
"sha256:654192c9f192459ee5f63e6606cc6dd8c50681c28e0689d2a494fc9522b357c1",
"sha256:69eeb530ce74be5e202a98ce35aa58b0776c24e64923c4873c25fa56f8e423bc",
"sha256:70b435f719d7156623f49ac6539f30dd8471500b5acf399f3b318c1c17f6fa7f",
"sha256:73aa8d2f911a54d1310e8258a752207e2d7624f4db46e2320d734fe7218320a1",
"sha256:79c1c20dffd4d8d7e669f096c88f261da8ba3646939cd3eb6faeccb92bae99c3",
"sha256:7ea80b33e57f918509eb91cd074324bff0f9aae40d238121cdc369f449c20649",
"sha256:80009748a5ce0c51a9947542935f9709a1c48b65c69f4a1e56d3072f99e750f2",
"sha256:84033cab9fec2a515a62d76a7b4acafa83339917db9ea911b65b017928ae7dd8",
"sha256:84b359a458611d40061f536b1363733b0af7a71425e4b17d0abb7e543d00e979",
"sha256:89d05a1ef531a1fee86d68e731c428b773bd37aabf743bf63dc3709c86f7254d",
"sha256:8de444335e3723959913f6635d579276a3517b8c19cbdfaefb04e73b5480e88b",
"sha256:8f9d12a8be271c7a83ded1cb7d85921f1b7f65c655c8dae972130a190211bb57",
"sha256:9245bcbcc3056d440f82e43fb34f94c15361c86b62e14cd9d7880c8e0cf370f6",
"sha256:96ace18758136905f8d5588ec35d6ae4fdc29ce4e4902518263ca33abf10de04",
"sha256:96f5fcdfb7e89b6d895e2ddc165f67b3126a585dfdf2a95b0c1f3fcf3380d7d4",
"sha256:991fa7c6756505e3dc5aad678db5fb0110d9816c2185bc3e2c520de46cfc8c5d",
"sha256:9b197f5d2bcd61b939d2f04b77e86ef24bda266518f3e1526bf666deb20d3ddd",
"sha256:9b6b5fdcdb6891025117f76f48d7d0f89245d66fab6519f3b856294bab317387",
"sha256:9bd573f8da863781ace89950d57a959397d5048b1cf69931c29b2b4ce1405bd7",
"sha256:9db8bacb2a69b4b5388bc1439463170af28f4cc710933085b81d1658d931d2c2",
"sha256:a053a9d9c9ccc9b72d90ecdcbabcbe57446defc7e8c52fc9978188f78742ac19",
"sha256:a4e4782514c16753f8bce3dd925cd37d26c847bead06e179af6fa4993ce83e88",
"sha256:a909a8eb82ce5a2f1566ebd685a79f3cfd9e049d57e3a0d3e24d56da04913e73",
"sha256:b16a8960ad9cdc923b90dbf431a4739fc81a07b53374ff74b7bdad3be31ce189",
"sha256:b25199a1b499da643de52ab73e972eb07e55e2a95bb99eaa491846098fa2214c",
"sha256:b7ddb305126d34fda64c3fe4632a43fb9b724da6cc7d1dedd8434e6c783b285b",
"sha256:b9f0b92b4a88b630f1e67df816b43cb017bb583479917dd100541d4f490a9517",
"sha256:bc495d00f235c3df6f376187a4ef48a56e162fdcc0a113ee34518332aa1aa2df",
"sha256:bc8b805fb04472e0f9cb3bc6aad90f24b202afc274b7b8bbd48042130e82997a",
"sha256:bf883ae772971bc6155cb64cdfb9e31c50a2099f3b7c8b51b0401bf608a9fe8e",
"sha256:bf937348d4052eac6cfa93c809ad214be75b1f057e3fc0dd2e374a814a23a16b",
"sha256:c314bf9026d865c2b2f67f3b853b4e2edcbe7ed46b67ef11b4a40f0af6aa17b8",
"sha256:c390f76f2fe408aa1a3121f3ca3d5c15698e14d913197417355fe3dc016f787d",
"sha256:c757e13b63a25d59a2566c9b2e77adf14d969207a375ca82a9b4fa1a04b37845",
"sha256:cc60fffea328f38187a4fce43f99c6bbf930de2b87b78251f43f2ba64a1af622",
"sha256:d106c2191a3a0dea6873c96ab909f31c13978913714a25023f492e9ccc8ffcc7",
"sha256:d5e4b4dcf85d7f289def0e3167f15aaad101b7744a065fb2019476edf306712c",
"sha256:d6e1a9ae71366164800942c477e663b31f3accef55d2e039cb7919fdb54b0086",
"sha256:d8c34c200a124a1a5769671613b3fc05a21d0a50f7ad942cf0ee12d4390a112c",
"sha256:d96186af967d7ae3f76d1e9a8fb70b5beb0f0dbdacfd3b418a946fffbb8781ff",
"sha256:da21b36d60f47dd04042e9372ffb3d1ededf5c60a76d43f8c3592221fb2d41cd",
"sha256:e269c68ec200538ae3fa7f9e8d8ed2b4b34d974c25945286ccc2a488438acf32",
"sha256:e4e4a43cde0dcdff974304ddb48246d6a094e618b6ee9642827ddbfe9fbebadb",
"sha256:ea5f3dd7450669acbfee7813d3a2a13876806b1d9e4c1626a7bf587c33c68472",
"sha256:eafb738fbcba39d12543730eacb65e2d5d627965959b3cc6bd6f797b2d52cc0f",
"sha256:ed6596fb63c574fd3dc4d9ea58a7b9c6e7235dc3d74a0bd3791559d5a618e947",
"sha256:f0f22627d6436bd120c6dde0936b12fe75498d1e2e9214ec9863be17f9b80997",
"sha256:f1e78019147ff7e08afaffb88fc3a0aa795171b147eacfc7ff873ce7a0d37dff",
"sha256:fd1154c6394892e727ff234273f5eb2b4319e5714917b016602435d77add8dcb"
],
"index": "pypi",
"markers": "python_version >= '3.11'",
"version": "==2.1.6"
},
"starlette": {
"hashes": [
"sha256:3d931de246ed870e35ce605974440816ef895e592f255d1396cfdf191d4c4318",
"sha256:fd3f159936e882d06c1108f84688f7cda2cf85fc6a592a50edbd175e882c2ddc"
],
"markers": "python_version >= '3.11'",
"version": "==1.9.0"
},
"typing-extensions": {
"hashes": [
"sha256:481caa481374e813c1b176ada14e97f1f67a4539ce9cfeb3f350d78d6370c2e8",
"sha256:dc983d19a509c94dba722ee6abd33940f7c05a89e243c47e907eb4db6f1a43e5"
],
"markers": "python_version >= '3.9'",
"version": "==4.16.0"
},
"typing-inspection": {
"hashes": [
"sha256:547274fa6b0a561ccf549cc9524b999a578e737d015d8709d021f9d0d13bea47",
"sha256:65b8397ba37ccbce054456aaccddfc91e6e3083c92824df348d96ca832f3f147"
],
"markers": "python_version >= '3.10'",
"version": "==0.4.4"
},
"uvicorn": {
"hashes": [
"sha256:505bdb0f318731d45f1f712071fc781a8981f6847a31c902c9f5e652d4f67faf",
"sha256:a2e33cbfaa0306f8e6b0c13e0cb89d7d7a2da3e62b90c66e18c33d9807b28620"
],
"index": "pypi",
"markers": "python_version >= '3.10'",
"version": "==0.54.0"
}
},
"develop": {
"anyio": {
"hashes": [
"sha256:6152fdbbf9a77fdec97731721bebf7c4c44f7c29b424b0065826173efc7ed101",
"sha256:9f28306018cbd6d329e64a36d58256edff76dd996fe423bc957326e578b82a94"
],
"markers": "python_version >= '3.10'",
"version": "==4.15.1"
},
"certifi": {
"hashes": [
"sha256:a9895e6e2758b371e0545e321a3a8fd1fcfd9829ada8824a936dfb16aaaf36cd",
"sha256:e9b54de76e530bb6cf0ccd036787103acb58fd42ba930ea8bf948ddbaa2f61d1"
],
"markers": "python_version >= '3.7'",
"version": "==2026.10.23"
},
"h11": {
"hashes": [
"sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1",
"sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86"
],
"markers": "python_version >= '3.8'",
"version": "==0.16.0"
},
"httpcore": {
"hashes": [
"sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55",
"sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8"
],
"markers": "python_version >= '3.8'",
"version": "==1.0.9"
},
"httpx": {
"hashes": [
"sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc",
"sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad"
],
"index": "pypi",
"markers": "python_version >= '3.8'",
"version": "==0.28.1"
},
"idna": {
"hashes": [
"sha256:a7db850025b95ded1eae8a46181a1a6c56c92c96f0e2b005d9ff8dc0210cab44",
"sha256:ab7ae7122974553370f0bdb919e1a960b2cd1bc1ef0276416d896db81c14582c"
],
"markers": "python_version >= '3.9'",
"version": "==3.20"
},
"iniconfig": {
"hashes": [
"sha256:67f4b9c50da0dedf52af349e7749a80a9057a5031199791b906c3bb3ae878960",
"sha256:9121e2c1fdb355232495be3194c8dfe87ccc2d5dee45947b78e68f499790d7a7"
],
"markers": "python_version >= '3.10'",
"version": "==2.3.1"
},
"packaging": {
"hashes": [
"sha256:94edc256424af38762eb31306eed28beb9f0efc50a8837492c9d6fd6004aed79",
"sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c"
],
"markers": "python_version >= '3.9'",
"version": "==26.3"
},
"pluggy": {
"hashes": [
"sha256:7dd7b0d8832ba3cb632c306926ded123429211b83641b35dc5c41ad2d34f9bec",
"sha256:d1eaa46ebb595891b860ab086b4d09c8588af65ebd4361b8e8f4bb8920b90ba8"
],
"markers": "python_version >= '3.10'",
"version": "==1.7.0"
},
"pygments": {
"hashes": [
"sha256:2363c69b61c4a97c838da3b130dcd6468f4848992b21a82f2a63ec34377137d9",
"sha256:610ca751c9bc2492b38eb9a38a7fbc93edbbb2d7182edaf34e66ae493dee5c8c"
],
"markers": "python_version >= '3.9'",
"version": "==2.21.0"
},
"pytest": {
"hashes": [
"sha256:1088fbde8f2b49d95a549a195707afa7a76a3ce9bcadc26b6d71f0ffda5fe313",
"sha256:37a86b45efb9a47a61a36449063e8e18d0cab3161329fc099eb21783169c4f0c"
],
"index": "pypi",
"markers": "python_version >= '3.10'",
"version": "==9.1.1"
},
"typing-extensions": {
"hashes": [
"sha256:481caa481374e813c1b176ada14e97f1f67a4539ce9cfeb3f350d78d6370c2e8",
"sha256:dc983d19a509c94dba722ee6abd33940f7c05a89e243c47e907eb4db6f1a43e5"
],
"markers": "python_version >= '3.9'",
"version": "==4.16.0"
}
}
}
+200 -49
View File
@@ -1,9 +1,10 @@
# Accounts
A small local web app for keeping a New Zealand business cashbook from ANZ bank
statements. It fills in a LibreOffice/Excel cashbook spreadsheet (one sheet per
month, April–March), so the spreadsheet stays the record and can still be opened
and edited directly.
A small web app (FastAPI) for keeping a New Zealand business cashbook from ANZ bank
statements. Books are kept in a SQLite database, and can be exported to the standard
cashbook spreadsheet layout (one sheet per month, April–March) as Excel or
LibreOffice files, or as CSV. People sign in with their own accounts, and
administrators manage who has access.
- Import ANZ statements: **CSV exports** from internet banking or **PDF statements**
(including a PDF holding several monthly statements).
@@ -11,10 +12,17 @@ and edited directly.
Each one shows how much of it is already in the selected book.
- Preview an import before it happens. Transactions already in the book are skipped,
matched on date and amount, so a CSV and a PDF of the same period don't double up.
- Review each month and set GST (incl/excl) and the income or expense category per
transaction.
- See the year at a glance, with a six-monthly GST summary (Apr–Sep, Oct–Mar) and
IRD due dates.
- Review each month and set the GST type (Incl GST, Zero-rated, Excl GST) and the
income or expense category per transaction.
- See the year at a glance, and get the **GST return** figures (IRD GST101A boxes
5–15) for each filing period, with due dates.
- **Income tax:** company return (IR4) and financial statements summary (IR10)
figures for the year, from the cashbook plus year-end amounts you type in.
- **Export** a book as an Excel (.xlsx) or LibreOffice (.ods) cashbook spreadsheet,
with formulas, GST and category columns and the summary sheet, or as a CSV of all
transactions.
- **Accounts:** sign-in with personal accounts. Administrators invite people, reset
passwords and manage who's an administrator (see [Accounts](#accounts)).
Everything runs on your machine; nothing is sent anywhere.
@@ -22,15 +30,15 @@ Everything runs on your machine; nothing is sent anywhere.
### Docker Compose (recommended)
One-time setup, as the user who should own the books (uid 1000 by default).
Create the data folder, then put a login password in `.env` (optional, see below):
One-time setup: create the data folder as the user who should own the books (uid
1000 by default), and copy the example settings.
```bash
mkdir -p data
```
```bash
(umask 077; echo "CASHBOOK_PASSWORD=$(openssl rand -base64 18)" >> .env)
cp .env.example .env
```
Then start it:
@@ -39,17 +47,14 @@ Then start it:
docker compose up -d --build
```
Open <http://127.0.0.1:8765> and log in as `cashbook` with the password from
`.env`. With no `CASHBOOK_PASSWORD` set, there's no login: anyone who can reach the
port can see and change your books, so only do that on a network you trust. Stop it with `docker compose down`, and check it with
Open <http://127.0.0.1:8765>. The first time, it asks you to create the
administrator account. Stop it with `docker compose down`, and check it with
`docker compose ps` (it reports `healthy`) or `docker compose logs`.
**What the compose file sets up**
- **Data:** `./data` is mounted at `/data` and holds the cashbooks and `statements/`
(uploads go there). Back up this folder.
- **Login:** HTTP Basic auth, with the password from the `CASHBOOK_PASSWORD`
environment variable. Leave it unset to turn the login off.
- **Data:** `./data` is mounted at `/data`. It holds the database (`cashbook.db`:
books and accounts) and `statements/`, where uploads go. Back up this folder.
- **Network:** published on `127.0.0.1:8765` only.
- **Lockdown:**
- read-only container filesystem, with scratch space only in `/tmp` (tmpfs);
@@ -58,23 +63,30 @@ port can see and change your books, so only do that on a network you trust. Stop
- **Operations:** a health check on `/healthz`, rotated logs (3 × 10 MB) and
`restart: unless-stopped`.
**Settings** go in a `.env` file next to `compose.yaml`. Keep it private
(`chmod 600 .env`); it's git-ignored.
**Settings** go in `.env` next to `compose.yaml` (see `.env.example`). Keep it
private (`chmod 600 .env`); it's git-ignored.
| Variable | Default | Purpose |
|---|---|---|
| `CASHBOOK_PASSWORD` | *(none)* | login password; unset means no login |
| `AUTH_ENABLED` | `true` | require sign-in; `false` lets anyone who can reach the port in |
| `AUTH_REGISTER_OPEN` | `true` | allow creating accounts at `/register` |
| `AUTH_INVITE_ONLY` | `true` | …but only with an invite link |
| `AUTH_SESSION_DAYS` | `14` | how long a sign-in lasts |
| `BOOTSTRAP_ADMIN_USERNAME` / `_PASSWORD` | | create this administrator on first start, instead of in the browser |
| `PUBLIC_BASE_URL`, `SMTP_*` | | email invites (see `.env.example`) |
| `CASHBOOK_USER` | `1000:1000` | uid:gid to run as; it should own `data/`. Use `0:0` with rootless Docker, which maps container root to you. |
| `CASHBOOK_USERNAME` | `cashbook` | login name |
| `CASHBOOK_BIND` | `127.0.0.1` | host address to publish on |
| `CASHBOOK_PORT` | `8765` | host port |
| `CASHBOOK_ALLOWED_HOSTS` | | extra host names you open the app by, comma-separated; see below |
| `FORWARDED_ALLOW_IPS` | `127.0.0.1` | proxies trusted to say the request came over HTTPS |
**Reaching it from other machines:** keep it on `127.0.0.1` and put a reverse proxy
with HTTPS in front (Caddy, nginx, Traefik). Basic auth sends the password with every
request, so don't expose it over plain HTTP.
with HTTPS in front (Caddy, nginx, Traefik). Set `FORWARDED_ALLOW_IPS` to the proxy's
address, so sign-in cookies are marked secure. On a trusted network you can instead
set `CASHBOOK_BIND=0.0.0.0` and open `http://server-ip:8765`. Passwords then cross the
network unencrypted.
To block other websites from making changes through your login, the app refuses
To block other websites from making changes through your sign-in, the app refuses
changes whose page address doesn't match the server's host name. It checks the
`Host` and `X-Forwarded-Host` headers. Caddy passes the original host through. With
nginx, add `proxy_set_header Host $host;`. Otherwise, list the name you use in
@@ -99,35 +111,156 @@ services:
### Without Docker
Needs Python 3 with LibreOffice's UNO bridge and `pdftotext`. On Debian/Ubuntu:
Needs Python 3, `pdftotext` for PDF statements, and LibreOffice's UNO bridge for
spreadsheet export and migration. On Debian/Ubuntu:
```bash
sudo apt install libreoffice-calc python3-uno poppler-utils
sudo apt install libreoffice-calc python3-uno poppler-utils pipenv
```
The virtualenv needs the system site packages, so it can see LibreOffice's `uno`:
```bash
pipenv --site-packages --python /usr/bin/python3 install --dev
```
```bash
python3 src/webapp.py
pipenv run dev
```
Options: `--port 8765`, `--host 127.0.0.1`, `--data DIR` (default `./data`), and
`--statements DIR` (repeatable) for extra folders to list statements from.
Set `CASHBOOK_PASSWORD` (or `CASHBOOK_PASSWORD_FILE`) to require a login; without
one, anyone who can reach the port can see your books.
That serves <http://127.0.0.1:8765> and reloads on code changes. Settings come from
`.env` in the same way. `CASHBOOK_DATA` sets the data folder (default `./data`), and
`CASHBOOK_STATEMENTS` adds statement folders, separated by `:`.
Run the tests with:
```bash
pipenv run test
```
## Accounts
With `AUTH_ENABLED=true` (the default), everyone signs in with their own username
and password. Everyone signed in can see and change the books. Administrators can
also manage people from **Users** in the menu at the top right.
- **First run:** with no accounts yet, the sign-in page offers to create the
administrator account. Alternatively, set `BOOTSTRAP_ADMIN_USERNAME` and
`BOOTSTRAP_ADMIN_PASSWORD` and it's created on first start.
- **Inviting people:** on the Users page, create an invite link and send it. Each
link works once and expires after 1, 7 or 30 days. It can make the new person an
administrator. With `SMTP_*` set and an email address given, the link is emailed.
Pending invites can be revoked. Set `AUTH_INVITE_ONLY=false` to let anyone create
an account, or `AUTH_REGISTER_OPEN=false` to stop new accounts entirely.
- **Managing people:** rename, reset a password, make or remove an administrator,
or delete an account. You can't delete or demote yourself, and there's always at
least one administrator.
- **Passwords:** at least 8 characters, stored as bcrypt hashes. Use **Change
password** in the menu. Changing or resetting a password signs that account out
on every other device.
- **Sessions:** a sign-in lasts `AUTH_SESSION_DAYS` days, in an `httponly`,
`SameSite=Lax` cookie that's marked secure over HTTPS. **Sign out** ends it.
- **Brute force:** after 5 wrong passwords for a username or from an address within
15 minutes, sign-in is refused for a while.
## Using it
1. **New book…** creates `data/cashbook-YYYY-YY.ods` from the template for a
financial year. If you've ticked a PDF statement covering 31 March, the opening
balance is filled in from it.
1. **New book…** creates a book for a financial year (April to March). If you've
ticked a PDF statement covering 31 March, the opening balance is filled in from it.
2. Add statements (drop them on the page, or **Add…**), tick them, and **Preview import**.
3. Check the preview: new rows, rows already in the book, and rows outside the book's
year. Then import. The book is backed up to `<book>.ods.bak` first.
year (these are skipped). Then import. The database is backed up to
`data/cashbook.db.bak` first.
4. Go through each month: fix GST settings and allocate categories. **Only unallocated**
filters to what still needs doing.
5. Check the GST table on the Year page before filing.
5. Open **GST return**, pick your filing frequency, and enter the boxes into myIR
(see below).
6. **Export** gives you the book as a spreadsheet for your accountant or IRD records.
Close a book in LibreOffice before changing it in the app; the app refuses to save
while LibreOffice has it open.
GST on the Year and month pages is 3/23 of each GST-inclusive amount, rounded to the
cent per transaction, the same as the spreadsheet. Exported spreadsheets show
zero-rated rows as Excl GST, since the template only has those two types.
### GST return
The **GST return** tab shows, for each filing period, the figures for boxes 5–15 of
the GST return (GST101A). These use the payments basis, with periods lined up to a
31 March balance date: six-monthly (Apr–Sep, Oct–Mar), two-monthly (Apr–May …
Feb–Mar) or monthly. It also shows each period's due date: the 28th of the following
month, except 7 May for periods ending in March and 15 January for November. Click a
figure to copy it.
What goes where depends on each transaction's GST type:
| GST type | Use for | On the return |
|---|---|---|
| Incl GST | sales and purchases at 15% GST | money in → Box 5; money out → Box 11 |
| Zero-rated | 0% GST supplies, such as services exported to overseas clients | money in → Box 5 and Box 6 |
| Excl GST | no GST: transfers, drawings, wages, interest, overseas purchases | left out |
Boxes 8 and 12 are 3/23 of the totals, as myIR calculates them, so they can differ by
a cent from the per-transaction GST on the Year page. Adjustments (boxes 9 and 13)
aren't tracked, so add any from your calculation sheet. The page lists anything to
check before filing: transactions not yet allocated, and money in or out left off the
return. Refunds from suppliers come in as Excl GST money in. If you claimed GST on
the original purchase, that GST needs a debit adjustment in Box 9.
These figures are worked out from your records and aren't tax advice. Check them
before you file.
### Income tax (IR4 and IR10)
The **Income tax** tab works out the company income tax return (IR4) and the
financial statements summary (IR10) for the book's year, box by box. Box numbers
follow IRD's IR10 (March 2026) and IR4 (2026) forms. Check them against the form
for the year you're filing.
**IR10.** The cashbook fills these boxes from allocated transactions, excluding GST:
| Cashbook category | IR10 box |
|---|---|
| Sales | 2 Sales and/or services |
| Interest | 7 Interest received |
| Grants, Sundry (income) | 10 Other income |
| Purchases | 4 Purchases |
| ACC & Insurance | 14 Insurance (move ACC levies to box 24) |
| Rent | 18 Rental, lease and licence payments |
| Wages | 22 Salaries and wages |
| Motor vehicle, Bank fees, Telephone & Power, Postage & Stationery, Sundry | 24 Other expenses |
| Drawings | 57 Drawings |
| Funds Intr., Loan repmt | not income or expenses |
| Closing bank balance | 31 Cash and deposits |
Anything the cashbook can't know you type in: stock, depreciation, debtors and
creditors, fixed assets, loans, tax adjustments and the other information boxes. For
boxes the cashbook fills, what you type is added to its figure, so you can move or
correct amounts. Totals (gross profit, net profit, total assets, owners' equity …)
are calculated. Unallocated transactions are left out, and the page says how much
there is.
The cashbook is on a cash basis, and bank interest arrives after RWT, so add the RWT
to box 7 to make it the gross interest.
**IR4.** Business profit (box 20B) comes from IR10 box 29, less the income reported
separately (interest, dividends, schedular and overseas income). You enter RWT and
other tax credits, donations, losses brought forward and provisional tax paid. Tax is
28% of taxable income in whole dollars, and the page shows the refund or tax to pay.
It assumes a New Zealand resident company with no residential property, Māori
authority, partnership or trust income, and no taxable property sales.
Entered amounts are saved per book in the database. These figures are worked out
from your records and aren't tax advice.
### Books kept in spreadsheets
Earlier versions kept each book as an `.ods` file in `data/`. At startup the app copies
any such spreadsheet into the database as a book named after the file, once, and
leaves the file alone. Rows split across several categories come in as unallocated,
and the log says which ones. To migrate files by hand:
```bash
python3 src/migrate.py data/cashbook-2026-27.ods
```
### How transactions are classified
@@ -146,28 +279,46 @@ won't count in the GST summary.
## Command line
```bash
python3 src/new_book.py data/cashbook-2026-27.ods --year 2026 --opening-balance 74.70
python3 src/new_book.py cashbook-2026-27 --year 2026 --opening-balance 74.70
```
```bash
python3 src/anz_import.py data/cashbook-2026-27.ods data/statements/statement.pdf
python3 src/anz_import.py cashbook-2026-27 data/statements/statement.pdf
```
`anz_import.py` refuses transactions outside the book's year unless you pass `--force`.
```bash
python3 src/export.py cashbook-2026-27 cashbook-2026-27.xlsx
```
The export format comes from the file extension: `.xlsx`, `.ods` or `.csv`.
Transactions outside a book's year are skipped on import. All of these take
`--data DIR` (default `./data`).
## Layout
```
src/
webapp.py web server and JSON API (stdlib only)
static/ the web page
app.py entry point (uvicorn app:app --app-dir src)
cashbook_app/ the FastAPI app
main.py app setup, security middleware, startup
config.py settings from the environment / .env
deps.py current user, admin check, database connections
routers/ auth, users, books, statements and page routes
services/ accounts, statement files, LibreOffice
db/, schemas/ account tables (SQLAlchemy) and request/response models
static/ the web pages (cashbook, sign-in, register, users)
ledger.py SQLite storage: books, transactions, GST and summaries
tax.py IR10 and IR4 figures
statements.py reads ANZ CSV and PDF statements, GST/category guesses
cashbook.py reads and writes the spreadsheet
office.py runs a private headless LibreOffice
new_book.py creates a book from the template (also a CLI)
export.py exports a book to .xlsx/.ods/.csv (also a CLI)
cashbook.py writes and reads the spreadsheet layout
migrate.py copies .ods cashbooks into the database (also a CLI)
office.py runs a private headless LibreOffice (for spreadsheets)
new_book.py creates a book (CLI)
anz_import.py imports statements (CLI)
template.ods blank cashbook template
data/ your books and statements (git-ignored)
template.ods blank cashbook template, used for exports
tests/ pytest suite (pipenv run test)
data/ cashbook.db and statements (git-ignored)
```
`data/` is in `.gitignore`: books, backups and bank statements are never committed.
+20 -9
View File
@@ -6,25 +6,36 @@ services:
restart: unless-stopped
stop_grace_period: 20s
# Run as the owner of ./data so saved books stay owned by them on the host.
# Run as the owner of ./data so saved files stay owned by them on the host.
# Rootless Docker maps container root to you instead: set CASHBOOK_USER=0:0.
user: "${CASHBOOK_USER:-1000:1000}"
# Only published on localhost by default; put a TLS reverse proxy in front
# to reach it from elsewhere (set CASHBOOK_BIND=0.0.0.0 only behind a firewall).
# Only published on localhost by default; put an HTTPS reverse proxy in front
# to reach it from elsewhere (set CASHBOOK_BIND=0.0.0.0 only on a trusted network).
ports:
- "${CASHBOOK_BIND:-127.0.0.1}:${CASHBOOK_PORT:-8765}:8765"
# Browser login (HTTP Basic): set CASHBOOK_PASSWORD in .env. Leave it unset to turn
# the login off; then anyone who can reach the port can see and change your books.
# Settings come from .env next to this file; see .env.example
environment:
CASHBOOK_USERNAME: "${CASHBOOK_USERNAME:-cashbook}"
CASHBOOK_PASSWORD: "${CASHBOOK_PASSWORD:-}"
# other host names you reach it by, if the proxy doesn't pass Host through
AUTH_ENABLED: "${AUTH_ENABLED:-true}"
AUTH_REGISTER_OPEN: "${AUTH_REGISTER_OPEN:-true}"
AUTH_INVITE_ONLY: "${AUTH_INVITE_ONLY:-true}"
AUTH_SESSION_DAYS: "${AUTH_SESSION_DAYS:-14}"
BOOTSTRAP_ADMIN_USERNAME: "${BOOTSTRAP_ADMIN_USERNAME:-}"
BOOTSTRAP_ADMIN_PASSWORD: "${BOOTSTRAP_ADMIN_PASSWORD:-}"
PUBLIC_BASE_URL: "${PUBLIC_BASE_URL:-}"
SMTP_HOST: "${SMTP_HOST:-}"
SMTP_PORT: "${SMTP_PORT:-587}"
SMTP_USER: "${SMTP_USER:-}"
SMTP_PASSWORD: "${SMTP_PASSWORD:-}"
SMTP_FROM: "${SMTP_FROM:-}"
SMTP_TLS: "${SMTP_TLS:-true}"
CASHBOOK_ALLOWED_HOSTS: "${CASHBOOK_ALLOWED_HOSTS:-}"
# proxies trusted for X-Forwarded-Proto (e.g. your reverse proxy's address)
FORWARDED_ALLOW_IPS: "${FORWARDED_ALLOW_IPS:-127.0.0.1}"
volumes:
# cashbooks and statements/ (uploads go there too)
# cashbook.db (books and accounts) and statements/ (uploads go there too)
- ./data:/data
# optional: a folder of ANZ exports to pick from, read-only
# - /path/to/anz:/statements:ro
+6
View File
@@ -0,0 +1,6 @@
[pytest]
pythonpath = src
testpaths = tests
filterwarnings =
ignore::DeprecationWarning
ignore:Using `httpx`
+18 -25
View File
@@ -1,48 +1,41 @@
#!/usr/bin/env python3
"""Import ANZ (NZ) statements (CSV exports or PDF statements) into a cashbook.
Each transaction is written to the month sheet (APR..MAR) matching its date,
appended after any rows already filled in. Rows that are already present
(same date, reference and amount) are skipped, so re-importing is safe.
Transactions already in the book (same date and amount) are skipped, so
re-importing is safe, and ones outside the book's financial year are left out.
Usage:
anz_import.py BOOK.ods STATEMENT [STATEMENT ...] [--force]
Needs LibreOffice (uses its bundled Python UNO bridge); the book must not be
open in LibreOffice while this runs.
anz_import.py BOOK STATEMENT [STATEMENT ...] [--data DIR]
"""
import argparse
import os
import shutil
import sys
from cashbook import YearMismatch, import_transactions
from office import BookLocked, open_book
import ledger
from statements import read_statement
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
ap.add_argument("book", help="cashbook .ods to fill in")
ap.add_argument("book", help="book name")
ap.add_argument("statements", nargs="+", help="ANZ statement(s): .csv export or .pdf")
ap.add_argument("--force", action="store_true",
help="import even if the transaction year doesn't match the sheet's year")
ap.add_argument("--data", default=os.path.join(ROOT, "data"))
args = ap.parse_args()
txns = [t for path in args.statements for t in read_statement(path)]
book = os.path.abspath(args.book)
shutil.copy2(book, book + ".bak")
conn = ledger.connect(os.path.join(args.data, "cashbook.db"))
try:
with open_book(book) as doc:
result = import_transactions(doc, txns, args.force)
doc.store()
except YearMismatch as e:
sys.exit(f"{e}; set the start year on 'Summary report' or use --force")
except BookLocked as e:
book = ledger.get_book(conn, args.book)
except ledger.NotFound as e:
sys.exit(str(e))
for name, (added, total) in result.items():
print(f"{name}: added {added} of {total} transactions")
print(f"Saved {book} (backup at {book}.bak)")
txns = [t for path in args.statements for t in read_statement(path)]
for sheet, (added, total) in ledger.import_transactions(conn, book, txns).items():
print(f"{sheet}: added {added} of {total} transactions")
start, end = ledger.fy_range(book)
outside = sum(not start <= t["date"] <= end for t in txns)
if outside:
print(f"Skipped {outside} transactions outside {start} – {end}")
if __name__ == "__main__":
+7
View File
@@ -0,0 +1,7 @@
from cashbook_app.main import app
if __name__ == "__main__": # pragma: no cover
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8765)
+69 -230
View File
@@ -1,22 +1,20 @@
"""Read and write the cashbook spreadsheet (template.ods layout) via UNO."""
from collections import Counter
"""Read and write the cashbook spreadsheet (template.ods layout) via UNO.
Used to export a book from the database to the spreadsheet, and to migrate
books kept in spreadsheets into the database.
"""
from datetime import date, timedelta
import uno # noqa: F401 (enables the com.sun.star imports)
from com.sun.star.lang import Locale
from statements import ORIGIN, classify, describe
SHEETS = {1: "JAN", 2: "FEB", 3: "MAR", 4: "APR", 5: "MAY", 6: "JUN",
7: "JUL", 8: "AUG", 9: "SEPT", 10: "OCT", 11: "NOV", 12: "DEC"}
FY_ORDER = ["APR", "MAY", "JUN", "JUL", "AUG", "SEPT", "OCT", "NOV", "DEC", "JAN", "FEB", "MAR"]
from ledger import FY_ORDER
# Month sheet layout (0-based columns / rows)
HEADER_ROW = 13 # row 14: column headings
TOTALS_ROW = 11 # row 12: totals for month
FIRST_ROW, LAST_ROW = 14, 503 # rows 15..504 are summed in the totals row
COL_REC, COL_DATE, COL_REF, COL_GST, COL_ORIGIN = 2, 3, 4, 5, 6 # C..G
COL_INCOME, COL_GST_IN, COL_EXPENSE, COL_GST_OUT = 7, 8, 9, 10 # H..K
COL_DATE, COL_REF, COL_GST, COL_ORIGIN = 3, 4, 5, 6 # D..G
COL_INCOME, COL_EXPENSE = 7, 9 # H, J
COL_UNALLOC_IN, COL_UNALLOC_OUT = 12, 19 # M, T
COL_INCOME_CATS = range(13, 18) # N..R
COL_EXPENSE_CATS = range(20, 32) # U..AF
@@ -25,10 +23,6 @@ LAST_COL = 31
EPOCH = date(1899, 12, 30)
class YearMismatch(Exception):
pass
def serial(d):
return (d - EPOCH).days
@@ -37,10 +31,6 @@ def from_serial(n):
return EPOCH + timedelta(days=int(n))
def col_name(c):
return chr(65 + c) if c < 26 else "A" + chr(65 + c - 26)
def _grid(sheet, values=True):
rng = sheet.getCellRangeByPosition(0, FIRST_ROW, LAST_COL, LAST_ROW)
return rng.DataArray if values else rng.FormulaArray
@@ -57,138 +47,6 @@ def categories(sheet):
{clean(c): c for c in COL_EXPENSE_CATS if clean(c)})
def sheet_year(sheet):
return int(sheet.getCellByPosition(4, 6).Value) # E7
# --- importing ------------------------------------------------------------
def _existing(sheet):
"""Count of rows already entered per (date, amount), and the first free row."""
keys, next_row = Counter(), FIRST_ROW
for i, (v, f) in enumerate(zip(_grid(sheet), _grid(sheet, values=False))):
if _is_entry(f):
next_row = FIRST_ROW + i + 1
amt = (v[COL_INCOME] or 0) or -(v[COL_EXPENSE] or 0)
day = int(v[COL_DATE]) if isinstance(v[COL_DATE], float) else 0
keys[(day, round(amt, 2))] += 1
return keys, next_row
def _key(t):
return serial(t["date"]), round(t["amount"], 2)
def find_new(doc, txns):
"""Split transactions into those not yet in the book, and those already there.
Matching is on date and amount, so a CSV export and a PDF statement of the
same period are recognised as the same transactions. Each statement is
matched against the book plus the statements before it; repeats within one
statement (e.g. two equal transfers on a day) are kept.
"""
existing = {}
for name in {SHEETS[t["date"].month] for t in txns}:
existing[name] = _existing(doc.Sheets.getByName(name))[0]
new, dups = [], []
by_source = {}
for t in txns:
by_source.setdefault(t.get("source"), []).append(t)
for group in by_source.values():
seen = Counter()
for t in sorted(group, key=lambda t: t["date"]):
k, have = _key(t), existing[SHEETS[t["date"].month]]
seen[k] += 1
(dups if seen[k] <= have[k] else new).append(t)
for t in group: # the book now effectively holds this statement too
have = existing[SHEETS[t["date"].month]]
have[_key(t)] = max(have[_key(t)], seen[_key(t)])
return new, dups
def count_imported(doc, txns):
"""How many of one statement's transactions are already in the book."""
return len(find_new(doc, txns)[1])
def plan_import(doc, txns):
"""Work out where each transaction would go without changing anything."""
new, _ = find_new(doc, txns)
new_ids = {id(t) for t in new}
years = {}
out = []
for t in sorted(txns, key=lambda t: t["date"]):
name = SHEETS[t["date"].month]
if name not in years:
years[name] = sheet_year(doc.Sheets.getByName(name))
gst, category = classify(t)
status = "duplicate" if id(t) not in new_ids else \
"wrong year" if t["date"].year != years[name] else "new"
out.append({"date": t["date"].isoformat(), "sheet": name, "reference": describe(t),
"amount": t["amount"], "gst": gst, "category": category or "",
"origin": ORIGIN.get(t["Type"], ""), "status": status})
return out
def import_transactions(doc, txns, force=False):
"""Append new transactions to their month sheets. Returns {sheet: (added, total)}."""
by_sheet = {}
for t in txns:
by_sheet.setdefault(SHEETS[t["date"].month], []).append(t)
new, _ = find_new(doc, txns)
if not force:
for name in {SHEETS[t["date"].month] for t in new}:
year = sheet_year(doc.Sheets.getByName(name))
wrong = sorted({t["date"].year for t in new if SHEETS[t["date"].month] == name} - {year})
if wrong:
raise YearMismatch(f"{name} sheet is for {year} but statement has {wrong}")
fmts = doc.NumberFormats
date_fmt = fmts.queryKey("DD/MM/YYYY", Locale(), False)
if date_fmt == -1:
date_fmt = fmts.addNew("DD/MM/YYYY", Locale())
result = {}
for name in sorted(by_sheet, key=FY_ORDER.index):
sheet = doc.Sheets.getByName(name)
to_add = [t for t in new if SHEETS[t["date"].month] == name]
_fill_sheet(sheet, to_add, date_fmt)
result[name] = (len(to_add), len(by_sheet[name]))
doc.calculateAll()
return result
def _fill_sheet(sheet, txns, date_fmt):
next_row = _existing(sheet)[1]
if next_row + len(txns) - 1 > LAST_ROW:
raise RuntimeError(f"{sheet.Name}: not enough empty rows left")
income_cats, expense_cats = categories(sheet)
for r, t in enumerate(sorted(txns, key=lambda t: t["date"]), start=next_row):
gst, category = classify(t)
d = sheet.getCellByPosition(COL_DATE, r)
d.Value = serial(t["date"])
d.NumberFormat = date_fmt
sheet.getCellByPosition(COL_REF, r).String = describe(t)
sheet.getCellByPosition(COL_GST, r).String = gst
sheet.getCellByPosition(COL_ORIGIN, r).String = ORIGIN.get(t["Type"], "")
amount_col = COL_INCOME if t["amount"] > 0 else COL_EXPENSE
sheet.getCellByPosition(amount_col, r).Value = abs(t["amount"])
if category:
_allocate(sheet, r, t["amount"] > 0, category, income_cats, expense_cats)
def _allocate(sheet, r, income, category, income_cats, expense_cats):
"""Put the whole net amount of row r in one category column ("" = unallocated)."""
cats = income_cats if income else expense_cats
for c in cats.values():
sheet.getCellByPosition(c, r).setString("")
if category in cats:
net = "=H{0}-I{0}" if income else "=J{0}-K{0}"
sheet.getCellByPosition(cats[category], r).Formula = net.format(r + 1)
# --- new books ------------------------------------------------------------
def setup_book(doc, year, opening_balance=None):
summary = doc.Sheets.getByName("Summary report")
summary.getCellRangeByName("D6").String = "April"
@@ -200,88 +58,69 @@ def setup_book(doc, year, opening_balance=None):
doc.calculateAll()
# --- reading / editing ----------------------------------------------------
# --- export -------------------------------------------------------------------
def read_summary(doc):
def write_book(doc, year, opening_balance, months):
"""Fill a fresh copy of the template. `months` maps sheet name to rows from ledger.read_month."""
setup_book(doc, year, opening_balance)
fmts = doc.NumberFormats
date_fmt = fmts.queryKey("DD/MM/YYYY", Locale(), False)
if date_fmt == -1:
date_fmt = fmts.addNew("DD/MM/YYYY", Locale())
for name, rows in months.items():
sheet = doc.Sheets.getByName(name)
if len(rows) > LAST_ROW - FIRST_ROW + 1:
raise ValueError(f"{name} has {len(rows)} transactions; the spreadsheet holds "
f"{LAST_ROW - FIRST_ROW + 1} a month")
income_cats, expense_cats = categories(sheet)
for r, row in enumerate(rows, start=FIRST_ROW):
d = sheet.getCellByPosition(COL_DATE, r)
d.Value = serial(date.fromisoformat(row["date"]))
d.NumberFormat = date_fmt
sheet.getCellByPosition(COL_REF, r).String = row["reference"]
# the template only knows Incl/Excl; zero-rated has no GST, like Excl
sheet.getCellByPosition(COL_GST, r).String = "Incl GST" if row["gst"] == "Incl GST" else "Excl GST"
sheet.getCellByPosition(COL_ORIGIN, r).String = row["origin"]
income = bool(row["income"])
amount_col = COL_INCOME if income else COL_EXPENSE
sheet.getCellByPosition(amount_col, r).Value = row["income"] or row["expense"]
cats = income_cats if income else expense_cats
if row["category"] in cats:
net = "=H{0}-I{0}" if income else "=J{0}-K{0}"
sheet.getCellByPosition(cats[row["category"]], r).Formula = net.format(r + 1)
doc.calculateAll()
# --- migration ----------------------------------------------------------------
def read_book(doc):
"""Read a filled-in cashbook: (start year, opening balance, rows, warnings)."""
summary = doc.Sheets.getByName("Summary report")
income_cats, expense_cats = categories(doc.Sheets.getByName("APR"))
months = []
year = int(summary.getCellRangeByName("D7").Value)
opening = summary.getCellRangeByName("G6").Value
rows, warnings = [], []
for name in FY_ORDER:
sheet = doc.Sheets.getByName(name)
tot = sheet.getCellRangeByPosition(0, TOTALS_ROW, LAST_COL, TOTALS_ROW).DataArray[0]
entries = [v for v, f in zip(_grid(sheet), _grid(sheet, values=False)) if _is_entry(f)]
incl = [v for v in entries if v[COL_GST] == "Incl GST"]
months.append({
"sheet": name,
"label": f"{sheet.getCellByPosition(3, 6).String} {sheet_year(sheet)}",
"opening": sheet.getCellByPosition(6, 5).Value,
"closing": sheet.getCellByPosition(6, 6).Value,
"count": len(entries),
# GST-inclusive amounts, as entered on a GST return
"sales_incl": sum(v[COL_INCOME] or 0 for v in incl),
"purchases_incl": sum(v[COL_EXPENSE] or 0 for v in incl),
"income": tot[COL_INCOME], "gst_in": tot[COL_GST_IN],
"expenses": tot[COL_EXPENSE], "gst_out": tot[COL_GST_OUT],
"unallocated_in": tot[COL_UNALLOC_IN], "unallocated_out": tot[COL_UNALLOC_OUT],
"income_cats": {k: tot[c] for k, c in income_cats.items()},
"expense_cats": {k: tot[c] for k, c in expense_cats.items()},
})
return {
"year": int(summary.getCellRangeByName("D7").Value),
"opening": summary.getCellRangeByName("G6").Value,
"closing": summary.getCellRangeByName("G7").Value,
"income_categories": list(income_cats),
"expense_categories": list(expense_cats),
"months": months,
}
def read_month(doc, name):
sheet = doc.Sheets.getByName(name)
income_cats, expense_cats = categories(sheet)
rows = []
for i, (v, f) in enumerate(zip(_grid(sheet), _grid(sheet, values=False))):
if not _is_entry(f):
continue
income = bool(v[COL_INCOME])
cats = income_cats if income else expense_cats
used = [k for k, c in cats.items() if isinstance(v[c], float) and abs(v[c]) > 0.004]
unalloc = v[COL_UNALLOC_IN] if income else v[COL_UNALLOC_OUT]
rows.append({
"row": FIRST_ROW + i + 1,
"date": from_serial(v[COL_DATE]).isoformat() if isinstance(v[COL_DATE], float) and v[COL_DATE] else str(v[COL_DATE]),
"reference": f[COL_REF], "gst": v[COL_GST], "origin": v[COL_ORIGIN],
"income": v[COL_INCOME] or 0, "gst_in": v[COL_GST_IN] or 0,
"expense": v[COL_EXPENSE] or 0, "gst_out": v[COL_GST_OUT] or 0,
"category": used[0] if len(used) == 1 and abs(unalloc) < 0.005 else
"split" if used else "",
"unallocated": unalloc or 0,
})
return {
"sheet": name,
"label": f"{sheet.getCellByPosition(3, 6).String} {sheet_year(sheet)}",
"opening": sheet.getCellByPosition(6, 5).Value,
"closing": sheet.getCellByPosition(6, 6).Value,
"income_categories": list(income_cats),
"expense_categories": list(expense_cats),
"rows": rows,
}
def update_row(doc, name, row, gst=None, category=None):
"""Change a row's GST setting and/or allocate it to a category (1-based row)."""
sheet = doc.Sheets.getByName(name)
r = row - 1
if not FIRST_ROW <= r <= LAST_ROW:
raise ValueError("row out of range")
if gst is not None:
if gst not in ("Incl GST", "Excl GST"):
raise ValueError("gst must be 'Incl GST' or 'Excl GST'")
sheet.getCellByPosition(COL_GST, r).String = gst
if category is not None:
income_cats, expense_cats = categories(sheet)
income = bool(sheet.getCellByPosition(COL_INCOME, r).Value)
if category and category not in (income_cats if income else expense_cats):
raise ValueError(f"unknown category {category!r}")
_allocate(sheet, r, income, category, income_cats, expense_cats)
doc.calculateAll()
for i, (v, f) in enumerate(zip(_grid(sheet), _grid(sheet, values=False))):
if not _is_entry(f):
continue
income = bool(v[COL_INCOME])
amount = (v[COL_INCOME] or 0) if income else -(v[COL_EXPENSE] or 0)
if not isinstance(v[COL_DATE], float) or not v[COL_DATE]:
warnings.append(f"{name} row {FIRST_ROW + i + 1}: no date, skipped")
continue
cats = income_cats if income else expense_cats
used = [k for k, c in cats.items() if isinstance(v[c], float) and abs(v[c]) > 0.004]
unalloc = v[COL_UNALLOC_IN] if income else v[COL_UNALLOC_OUT]
category = used[0] if len(used) == 1 and abs(unalloc or 0) < 0.005 else None
if len(used) > 1 or (used and category is None):
warnings.append(f"{name} row {FIRST_ROW + i + 1}: split across categories, "
"imported as unallocated")
rows.append({
"date": from_serial(v[COL_DATE]).isoformat(), "reference": f[COL_REF],
"origin": v[COL_ORIGIN] or "", "amount": amount,
"gst": v[COL_GST] if v[COL_GST] in ("Incl GST", "Excl GST") else "Excl GST",
"category": category,
})
return year, opening, rows, warnings
View File
Whitespace-only changes.
+64
View File
@@ -0,0 +1,64 @@
"""Settings, read from the environment (and a `.env` file at the repo root).
Values are read on each call rather than at import, so tests can change them.
"""
import os
from pathlib import Path
# Application code lives under `src/`; the repo root is one level up.
BASE_DIR = Path(__file__).resolve().parent.parent
PROJECT_ROOT = BASE_DIR.parent
STATIC_DIR = BASE_DIR / "static"
def load_env_file(env_path: Path) -> None:
"""Load KEY=VALUE entries from a .env file (doesn't override os.environ)."""
if not env_path.exists():
return
for line in env_path.read_text(encoding="utf-8").splitlines():
stripped = line.strip()
if not stripped or stripped.startswith("#") or "=" not in stripped:
continue
key, value = stripped.split("=", 1)
key = key.strip()
value = value.strip().strip('"').strip("'")
if key and key not in os.environ:
os.environ[key] = value
load_env_file(PROJECT_ROOT / ".env")
def env_flag(name: str, default: bool) -> bool:
"""True/false setting; unset or empty means the default (so AUTH_ENABLED= doesn't turn auth off)."""
value = os.environ.get(name, "").strip().lower()
if not value:
return default
return value in ("1", "true", "yes", "on")
def data_dir() -> Path:
"""Folder holding cashbook.db and statements/."""
path = Path(os.environ.get("CASHBOOK_DATA") or PROJECT_ROOT / "data").resolve()
path.mkdir(parents=True, exist_ok=True)
return path
def database_path() -> Path:
return data_dir() / "cashbook.db"
def uploads_dir() -> Path:
return data_dir() / "statements"
def statement_dirs() -> list[Path]:
"""Folders listed for statements: uploads, the data folder, ~/Downloads/anz, and any
in CASHBOOK_STATEMENTS (separated by ':')."""
extra = [Path(p).expanduser().resolve() for p in os.environ.get("CASHBOOK_STATEMENTS", "").split(":") if p]
return [uploads_dir(), data_dir(), Path("~/Downloads/anz").expanduser()] + extra
def allowed_hosts() -> set[str]:
"""Extra host names the app is reached by, e.g. behind a proxy that rewrites Host."""
return {h.strip().lower() for h in os.environ.get("CASHBOOK_ALLOWED_HOSTS", "").split(",") if h.strip()}
View File
Whitespace-only changes.
+59
View File
@@ -0,0 +1,59 @@
from __future__ import annotations
import datetime as dt
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
def utc_now() -> dt.datetime:
return dt.datetime.now(dt.UTC).replace(tzinfo=None)
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
username: Mapped[str] = mapped_column(String(64), unique=True, index=True)
password_hash: Mapped[str] = mapped_column(String(256))
is_superuser: Mapped[bool] = mapped_column(Boolean, default=False)
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=utc_now)
last_login_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
sessions: Mapped[list[AuthSession]] = relationship(
"AuthSession", back_populates="user", cascade="all, delete-orphan"
)
class AuthSession(Base):
__tablename__ = "auth_sessions"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
token: Mapped[str] = mapped_column(String(128), unique=True, index=True)
expires_at: Mapped[dt.datetime] = mapped_column(DateTime)
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=utc_now)
user: Mapped[User] = relationship("User", back_populates="sessions")
class InviteToken(Base):
__tablename__ = "invite_tokens"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
email: Mapped[str] = mapped_column(String(320), index=True)
token: Mapped[str] = mapped_column(String(128), unique=True, index=True)
expires_at: Mapped[dt.datetime] = mapped_column(DateTime)
used_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
invited_by_user_id: Mapped[int | None] = mapped_column(
ForeignKey("users.id", ondelete="SET NULL"), nullable=True, index=True
)
consumed_by_user_id: Mapped[int | None] = mapped_column(
ForeignKey("users.id", ondelete="SET NULL"), nullable=True, index=True
)
grants_superuser: Mapped[bool] = mapped_column(Boolean, default=False)
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=utc_now)
+55
View File
@@ -0,0 +1,55 @@
from __future__ import annotations
from collections.abc import Generator
from sqlalchemy import create_engine, event
from sqlalchemy.orm import Session, sessionmaker
from cashbook_app import config
_engine = None
_SessionLocal: sessionmaker[Session] | None = None
def reset_engine() -> None:
"""Test helper: drop the cached engine after the data folder changes."""
global _engine, _SessionLocal
if _engine is not None:
_engine.dispose()
_engine = None
_SessionLocal = None
def get_engine():
global _engine, _SessionLocal
if _engine is None:
# accounts live in the same SQLite file as the books
_engine = create_engine(
f"sqlite:///{config.database_path()}",
connect_args={"check_same_thread": False, "timeout": 30},
pool_pre_ping=True,
)
@event.listens_for(_engine, "connect")
def _sqlite_pragmas(dbapi_conn, _record):
cur = dbapi_conn.cursor()
cur.execute("PRAGMA foreign_keys = ON")
cur.execute("PRAGMA journal_mode = WAL")
cur.close()
_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=_engine)
return _engine
def get_session_factory() -> sessionmaker[Session]:
get_engine()
assert _SessionLocal is not None
return _SessionLocal
def get_db() -> Generator[Session, None, None]:
db = get_session_factory()()
try:
yield db
finally:
db.close()
+51
View File
@@ -0,0 +1,51 @@
from __future__ import annotations
from collections.abc import Generator
from fastapi import Cookie, Depends, HTTPException
from sqlalchemy.orm import Session
import ledger
from cashbook_app import config
from cashbook_app.db.models import User
from cashbook_app.db.session import get_db
from cashbook_app.services import accounts
def get_ledger() -> Generator:
"""Connection to the books (same SQLite file as the accounts)."""
conn = ledger.connect(config.database_path())
try:
yield conn
finally:
conn.close()
async def current_user_optional(
db: Session = Depends(get_db),
cashbook_session: str | None = Cookie(None),
) -> User | None:
if not accounts.auth_enabled():
return None
return accounts.get_session_user(db, cashbook_session)
async def require_user(
user: User | None = Depends(current_user_optional),
) -> User | None:
"""Signed-in user; with AUTH_ENABLED=false everyone gets in (returns None)."""
if accounts.auth_enabled() and user is None:
raise HTTPException(status_code=401, detail="Not signed in")
return user
async def require_superuser(
user: User | None = Depends(current_user_optional),
) -> User:
if not accounts.auth_enabled():
raise HTTPException(status_code=400, detail="User management needs AUTH_ENABLED=true")
if user is None:
raise HTTPException(status_code=401, detail="Not signed in")
if not user.is_superuser:
raise HTTPException(status_code=403, detail="Administrators only")
return user
+125
View File
@@ -0,0 +1,125 @@
import logging
import os
from contextlib import asynccontextmanager
from urllib.parse import urlparse
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
import ledger
from cashbook_app import config
from cashbook_app.db.models import Base
from cashbook_app.db.session import get_engine, get_session_factory
from cashbook_app.routers.auth_routes import router as auth_router
from cashbook_app.routers.books import router as books_router
from cashbook_app.routers.frontend import router as frontend_router
from cashbook_app.routers.statements import router as statements_router
from cashbook_app.routers.users_admin import router as users_router
from cashbook_app.services import accounts, spreadsheets
log = logging.getLogger("cashbook")
class _SkipHealthChecks(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
return "/healthz" not in record.getMessage()
logging.getLogger("uvicorn.access").addFilter(_SkipHealthChecks())
SECURITY_HEADERS = {
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"Referrer-Policy": "no-referrer",
"Content-Security-Policy": "default-src 'self'; script-src 'self' 'unsafe-inline'; "
"style-src 'self' 'unsafe-inline'; img-src 'self' data:; frame-ancestors 'none'",
}
def _bootstrap_admin() -> None:
"""First run: create an admin from BOOTSTRAP_ADMIN_USERNAME/PASSWORD if there are no users."""
username = os.environ.get("BOOTSTRAP_ADMIN_USERNAME", "").strip()
password = os.environ.get("BOOTSTRAP_ADMIN_PASSWORD", "").strip()
if not (accounts.auth_enabled() and username and password):
return
db = get_session_factory()()
try:
if accounts.count_users(db) == 0:
accounts.create_user(db, username, password, is_superuser=True)
log.warning("Created administrator %r from BOOTSTRAP_ADMIN_USERNAME", username)
finally:
db.close()
def _migrate_spreadsheets() -> None:
"""Copy any .ods cashbooks left in the data folder into the database (once each)."""
if not any(f.endswith(".ods") and f != "template.ods" for f in os.listdir(config.data_dir())):
return
from migrate import migrate_folder
conn = ledger.connect(config.database_path())
try:
for message in migrate_folder(conn, str(config.data_dir()), spreadsheets.office().open):
log.warning(message)
finally:
conn.close()
@asynccontextmanager
async def lifespan(_app: FastAPI):
Base.metadata.create_all(bind=get_engine())
ledger.connect(config.database_path()).close() # create / upgrade the books tables
_bootstrap_admin()
_migrate_spreadsheets()
if not accounts.auth_enabled():
log.warning("AUTH_ENABLED=false: anyone who can reach this server can see and change the books")
yield
spreadsheets.stop()
def _same_origin(request: Request) -> bool:
"""Refuse state-changing requests made from other websites.
Compares the Origin's host name (ports differ behind Docker and proxies) with Host,
X-Forwarded-Host and CASHBOOK_ALLOWED_HOSTS.
"""
origin = request.headers.get("origin")
if not origin:
return True
host_of = lambda h: urlparse("//" + h.strip()).hostname
allowed = {"localhost", "127.0.0.1", "::1"} | config.allowed_hosts()
for h in [request.headers.get("host", "")] + request.headers.get("x-forwarded-host", "").split(","):
if h.strip():
allowed.add(host_of(h))
if urlparse(origin).hostname in allowed:
return True
log.warning("Refused %s %s: Origin %s is not one of %s; add it to CASHBOOK_ALLOWED_HOSTS if it's yours",
request.method, request.url.path, origin, sorted(allowed))
return False
def create_app() -> FastAPI:
app = FastAPI(title="Cashbook", lifespan=lifespan, docs_url=None, redoc_url=None)
@app.middleware("http")
async def security(request: Request, call_next):
if request.method not in ("GET", "HEAD", "OPTIONS") and not _same_origin(request):
return JSONResponse({"detail": "Refused: this page's address doesn't match the server's. "
"See the server log, or set CASHBOOK_ALLOWED_HOSTS."}, status_code=403)
response = await call_next(request)
for k, v in SECURITY_HEADERS.items():
response.headers.setdefault(k, v)
if request.url.path.startswith("/api/"):
response.headers.setdefault("Cache-Control", "no-store")
return response
app.mount("/static", StaticFiles(directory=str(config.STATIC_DIR)), name="static")
app.include_router(frontend_router)
app.include_router(auth_router)
app.include_router(users_router)
app.include_router(books_router)
app.include_router(statements_router)
return app
app = create_app()
Whitespace-only changes.
+111
View File
@@ -0,0 +1,111 @@
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException, Request, Response
from sqlalchemy.orm import Session
from cashbook_app.db.models import User
from cashbook_app.db.session import get_db
from cashbook_app.deps import require_user
from cashbook_app.schemas.users import (AuthStatusResponse, LoginRequest, PasswordChange,
RegisterRequest, UserPublic)
from cashbook_app.services import accounts
router = APIRouter(prefix="/api/auth", tags=["auth"])
def _secure(request: Request) -> bool:
return request.url.scheme == "https"
def _set_session_cookie(response: Response, request: Request, token: str) -> None:
response.set_cookie(
key=accounts.SESSION_COOKIE_NAME, value=token,
max_age=accounts.session_ttl_days() * 86400,
httponly=True, samesite="lax", secure=_secure(request), path="/",
)
def _require_auth_enabled() -> None:
if not accounts.auth_enabled():
raise HTTPException(status_code=400, detail="Accounts are turned off (AUTH_ENABLED=false)")
@router.get("/status", response_model=AuthStatusResponse)
def auth_status(db: Session = Depends(get_db)) -> AuthStatusResponse:
enabled = accounts.auth_enabled()
return AuthStatusResponse(
auth_enabled=enabled,
register_open=accounts.register_open(),
invite_required=accounts.invite_required(),
setup_required=enabled and accounts.count_users(db) == 0,
)
@router.get("/me")
def auth_me(request: Request, db: Session = Depends(get_db)) -> dict:
if not accounts.auth_enabled():
return {"auth_enabled": False, "user": None}
user = accounts.get_session_user(db, request.cookies.get(accounts.SESSION_COOKIE_NAME))
if user is None:
raise HTTPException(status_code=401, detail="Not signed in")
return {"auth_enabled": True, "user": UserPublic.model_validate(user).model_dump(mode="json")}
@router.post("/register", response_model=UserPublic)
def register(body: RegisterRequest, request: Request, response: Response,
db: Session = Depends(get_db)) -> UserPublic:
_require_auth_enabled()
first = accounts.count_users(db) == 0
try:
if body.invite_token:
user = accounts.register_user_with_invite(db, body.username, body.password, body.invite_token)
elif first:
# first-run setup: the first account is the administrator
user = accounts.register_user(db, body.username, body.password)
elif not accounts.register_open():
raise HTTPException(status_code=403, detail="Registration is closed")
elif accounts.invite_required():
raise HTTPException(status_code=403, detail="You need an invite to create an account")
else:
user = accounts.register_user(db, body.username, body.password)
except ValueError as exc:
raise HTTPException(status_code=409, detail=str(exc)) from exc
# sign straight in
_set_session_cookie(response, request, accounts.create_session(db, user).token)
return UserPublic.model_validate(user)
@router.post("/login", response_model=UserPublic)
def login(body: LoginRequest, request: Request, response: Response,
db: Session = Depends(get_db)) -> UserPublic:
_require_auth_enabled()
keys = (f"user:{body.username.strip().lower()}", f"ip:{request.client.host if request.client else ''}")
if accounts.login_blocked(*keys):
raise HTTPException(status_code=429, detail="Too many failed sign-ins; try again in 15 minutes")
user = accounts.authenticate(db, body.username, body.password)
if user is None:
accounts.record_failure(*keys)
raise HTTPException(status_code=401, detail="Wrong username or password")
accounts.clear_failures(keys[0])
_set_session_cookie(response, request, accounts.create_session(db, user).token)
return UserPublic.model_validate(user)
@router.post("/logout")
def logout(request: Request, response: Response, db: Session = Depends(get_db)) -> dict:
accounts.delete_session(db, request.cookies.get(accounts.SESSION_COOKIE_NAME))
response.delete_cookie(accounts.SESSION_COOKIE_NAME, path="/", samesite="lax", secure=_secure(request))
return {"message": "Signed out"}
@router.post("/password")
def change_password(body: PasswordChange, request: Request, user: User | None = Depends(require_user),
db: Session = Depends(get_db)) -> dict:
_require_auth_enabled()
user = db.merge(user)
try:
accounts.change_password(db, user, body.current_password, body.new_password,
keep_token=request.cookies.get(accounts.SESSION_COOKIE_NAME))
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
return {"message": "Password changed; other devices have been signed out"}
+129
View File
@@ -0,0 +1,129 @@
from __future__ import annotations
import re
from urllib.parse import quote
from fastapi import APIRouter, Depends, HTTPException, Query, Response
from pydantic import BaseModel, Field
import ledger
import tax
from cashbook_app.deps import get_ledger, require_user
from cashbook_app.services import spreadsheets
from export import FORMATS, export
router = APIRouter(prefix="/api", tags=["books"], dependencies=[Depends(require_user)])
class BookCreate(BaseModel):
name: str = Field(min_length=1, max_length=100)
year: int = Field(ge=2000, le=2100)
opening_balance: float | str | None = None
class TaxInput(BaseModel):
book: str
form: str
box: str
value: float | None = None
class RowUpdate(BaseModel):
book: str
sheet: str
row: int
gst: str | None = None
category: str | None = None
def get_book(conn, name: str | None):
try:
return ledger.get_book(conn, name or "")
except ledger.NotFound as e:
raise HTTPException(status_code=404, detail=str(e))
def check_sheet(sheet: str) -> str:
if sheet not in ledger.FY_ORDER:
raise HTTPException(status_code=400, detail="invalid sheet")
return sheet
@router.get("/books")
def list_books(conn=Depends(get_ledger)):
return ledger.list_books(conn)
@router.post("/books")
def create_book(body: BookCreate, conn=Depends(get_ledger)):
name = re.sub(r"\.ods$", "", body.name.strip())
balance = body.opening_balance
try:
balance = float(balance) if balance not in (None, "") else 0.0
book = ledger.create_book(conn, name, body.year, balance)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
return {"name": book["name"]}
@router.get("/summary")
def summary(book: str, conn=Depends(get_ledger)):
return ledger.read_summary(conn, get_book(conn, book))
@router.get("/month")
def month(book: str, sheet: str, conn=Depends(get_ledger)):
return ledger.read_month(conn, get_book(conn, book), check_sheet(sheet))
@router.get("/gst")
def gst(book: str, frequency: str = "six", conn=Depends(get_ledger)):
if frequency not in ledger.FREQUENCIES:
raise HTTPException(status_code=400, detail="frequency must be six, two or one")
return ledger.gst_return(conn, get_book(conn, book), frequency)
def _tax_returns(conn, book):
ir10 = tax.ir10(conn, book)
return {"ir10": ir10, "ir4": tax.ir4(conn, book, ir10["values"])}
@router.get("/tax")
def tax_returns(book: str, conn=Depends(get_ledger)):
return _tax_returns(conn, get_book(conn, book))
@router.post("/tax")
def set_tax_input(body: TaxInput, conn=Depends(get_ledger)):
book = get_book(conn, body.book)
try:
tax.set_input(conn, book, body.form, body.box, body.value)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
return _tax_returns(conn, book)
@router.post("/row")
def update_row(body: RowUpdate, conn=Depends(get_ledger)):
book = get_book(conn, body.book)
check_sheet(body.sheet)
try:
ledger.update_row(conn, book, body.row, gst=body.gst, category=body.category)
except ledger.NotFound as e:
raise HTTPException(status_code=404, detail=str(e))
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
return ledger.read_month(conn, book, body.sheet)
@router.get("/export")
def export_book(book: str, format: str = Query("xlsx"), conn=Depends(get_ledger)):
if format not in FORMATS:
raise HTTPException(status_code=400, detail="format must be ods, xlsx or csv")
b = get_book(conn, book)
open_fn = None if format == "csv" else spreadsheets.office().open
data = export(conn, b, format, open_fn)
filename = f"{b['name']}.{format}"
safe = re.sub(r"[^\w.-]", "_", filename)
return Response(data, media_type=FORMATS[format][0], headers={
"Content-Disposition": f"attachment; filename=\"{safe}\"; filename*=UTF-8''{quote(filename)}"})
+39
View File
@@ -0,0 +1,39 @@
from fastapi import APIRouter, Depends
from fastapi.responses import FileResponse, RedirectResponse
from cashbook_app.config import STATIC_DIR
from cashbook_app.db.models import User
from cashbook_app.deps import current_user_optional
from cashbook_app.services import accounts
router = APIRouter(include_in_schema=False)
@router.get("/healthz")
def healthz():
return {"ok": True}
@router.get("/")
def home(user: User | None = Depends(current_user_optional)):
if accounts.auth_enabled() and user is None:
return RedirectResponse("/login")
return FileResponse(STATIC_DIR / "index.html")
@router.get("/login")
def login_page():
return FileResponse(STATIC_DIR / "login.html")
@router.get("/register")
def register_page():
return FileResponse(STATIC_DIR / "register.html")
@router.get("/users")
def users_page(user: User | None = Depends(current_user_optional)):
"""Admin page; the /api/users endpoints it calls enforce admin rights."""
if accounts.auth_enabled() and user is None:
return RedirectResponse("/login?next=/users")
return FileResponse(STATIC_DIR / "users.html")
+125
View File
@@ -0,0 +1,125 @@
from __future__ import annotations
import os
import sqlite3
from contextlib import closing
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.concurrency import run_in_threadpool
from fastapi.responses import FileResponse
from pydantic import BaseModel
import ledger
from cashbook_app import config
from cashbook_app.deps import get_ledger, require_user
from cashbook_app.routers.books import get_book
from cashbook_app.services import statement_files as files
from cashbook_app.services.statement_files import StatementError
from statements import describe, read_statement
router = APIRouter(prefix="/api", tags=["statements"], dependencies=[Depends(require_user)])
class StatementRef(BaseModel):
path: str
class StatementRename(BaseModel):
path: str
name: str
class ImportRequest(BaseModel):
book: str
statements: list[str]
def _bad(e: Exception):
return HTTPException(status_code=400, detail=str(e))
@router.get("/statements")
def list_statements(book: str | None = None, conn=Depends(get_ledger)):
return files.list_statements(conn, get_book(conn, book) if book else None)
@router.get("/statement")
def view_statement(path: str, book: str | None = None, conn=Depends(get_ledger)):
try:
p = files.statement_path(path)
except StatementError as e:
raise _bad(e)
parsed = files.parse(p)
if not parsed["txns"]:
raise HTTPException(status_code=400, detail=parsed["info"].get("error") or "no transactions")
if book:
return {"info": parsed["info"],
"transactions": ledger.plan_import(conn, get_book(conn, book), parsed["txns"])}
return {"info": parsed["info"], "transactions": [
{"date": t["date"].isoformat(), "reference": describe(t), "amount": t["amount"]}
for t in sorted(parsed["txns"], key=lambda t: t["date"])]}
@router.get("/statement/file")
def download_statement(path: str):
try:
p = files.statement_path(path)
except StatementError as e:
raise _bad(e)
return FileResponse(p, filename=os.path.basename(p),
media_type="application/pdf" if p.lower().endswith(".pdf") else "text/csv")
@router.post("/statement/rename")
def rename_statement(body: StatementRename):
try:
return {"path": files.rename_statement(body.path, body.name)}
except StatementError as e:
raise _bad(e)
@router.post("/statement/delete")
def delete_statement(body: StatementRef):
try:
files.delete_statement(body.path)
except StatementError as e:
raise _bad(e)
return {"ok": True}
@router.post("/upload")
async def upload(request: Request, filename: str):
length = int(request.headers.get("content-length") or 0)
if length > files.MAX_UPLOAD:
raise HTTPException(status_code=413, detail="file is too large for a statement")
data = await request.body()
try:
path = await run_in_threadpool(files.save_upload, filename, data)
except StatementError as e:
raise _bad(e)
return {"path": path}
def _read(paths: list[str]):
try:
paths = files.statement_paths(paths)
except StatementError as e:
raise _bad(e)
return [t for p in paths for t in read_statement(p)]
@router.post("/preview")
def preview(body: ImportRequest, conn=Depends(get_ledger)):
txns = _read(body.statements)
return ledger.plan_import(conn, get_book(conn, body.book), txns)
@router.post("/import")
def import_statements(body: ImportRequest, conn=Depends(get_ledger)):
txns = _read(body.statements)
book = get_book(conn, body.book)
# keep a copy of the database from before the import
with closing(sqlite3.connect(str(config.database_path()) + ".bak")) as dest:
conn.backup(dest)
result = ledger.import_transactions(conn, book, txns)
return [{"sheet": k, "added": a, "total": t} for k, (a, t) in result.items()]
+75
View File
@@ -0,0 +1,75 @@
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException, Request
from sqlalchemy.orm import Session
from cashbook_app.db.models import User
from cashbook_app.db.session import get_db
from cashbook_app.deps import require_superuser
from cashbook_app.schemas.users import (InviteCreateRequest, InviteCreateResponse, InvitePublic,
UserPublic, UserUpdateAdmin)
from cashbook_app.services import accounts
router = APIRouter(prefix="/api/users", tags=["users"])
@router.get("", response_model=list[UserPublic])
def list_users(_admin: User = Depends(require_superuser), db: Session = Depends(get_db)):
return [UserPublic.model_validate(u) for u in accounts.list_users(db)]
@router.patch("/{user_id}", response_model=UserPublic)
def update_user(user_id: int, body: UserUpdateAdmin, admin: User = Depends(require_superuser),
db: Session = Depends(get_db)) -> UserPublic:
if user_id == admin.id and body.is_superuser is False:
raise HTTPException(status_code=400, detail="You can't remove your own admin rights")
try:
updated = accounts.update_user(db, user_id, username=body.username,
password=body.password, is_superuser=body.is_superuser)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
if updated is None:
raise HTTPException(status_code=404, detail="User not found")
return UserPublic.model_validate(updated)
@router.delete("/{user_id}")
def delete_user(user_id: int, admin: User = Depends(require_superuser),
db: Session = Depends(get_db)) -> dict:
if user_id == admin.id:
raise HTTPException(status_code=400, detail="You can't delete your own account")
try:
if not accounts.delete_user(db, user_id):
raise HTTPException(status_code=404, detail="User not found")
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
return {"message": "User deleted"}
@router.get("/invites", response_model=list[InvitePublic])
def list_invites(_admin: User = Depends(require_superuser), db: Session = Depends(get_db)):
return [InvitePublic.model_validate(i) for i in accounts.list_pending_invites(db)]
@router.post("/invites", response_model=InviteCreateResponse)
def create_invite(body: InviteCreateRequest, request: Request, admin: User = Depends(require_superuser),
db: Session = Depends(get_db)) -> InviteCreateResponse:
email = (body.email or "").strip().lower()
invite = accounts.create_invite(db, email, invited_by_user_id=admin.id,
expires_days=body.expires_days, grants_superuser=body.grants_superuser)
invite_url = accounts.build_invite_url(invite.token, str(request.base_url))
delivered = False
if email:
try:
delivered = accounts.send_invite_email(email, invite_url)
except Exception:
delivered = False
return InviteCreateResponse(id=invite.id, email=invite.email, invite_url=invite_url, delivered=delivered)
@router.delete("/invites/{invite_id}")
def revoke_invite(invite_id: int, _admin: User = Depends(require_superuser),
db: Session = Depends(get_db)) -> dict:
if not accounts.revoke_invite(db, invite_id):
raise HTTPException(status_code=404, detail="Invite not found")
return {"message": "Invite revoked"}
Whitespace-only changes.
+114
View File
@@ -0,0 +1,114 @@
import datetime as dt
from pydantic import BaseModel, Field, field_validator, model_validator
def _clean_username(v: str) -> str:
s = v.strip()
if not s.replace("_", "").replace(".", "").replace("-", "").isalnum():
raise ValueError("Username may only contain letters, numbers, '.', '-' and '_'")
return s
class RegisterRequest(BaseModel):
username: str = Field(min_length=3, max_length=64)
password: str = Field(min_length=8, max_length=128)
invite_token: str | None = Field(default=None, max_length=256)
@field_validator("invite_token", mode="before")
@classmethod
def normalize_invite_token(cls, v: object) -> object:
if isinstance(v, str):
return v.strip() or None
return v
@field_validator("username")
@classmethod
def username_chars(cls, v: str) -> str:
return _clean_username(v)
class LoginRequest(BaseModel):
username: str
password: str
class PasswordChange(BaseModel):
current_password: str
new_password: str = Field(min_length=8, max_length=128)
class UserPublic(BaseModel):
id: int
username: str
is_superuser: bool
created_at: dt.datetime | None = None
last_login_at: dt.datetime | None = None
model_config = {"from_attributes": True}
class UserUpdateAdmin(BaseModel):
username: str | None = Field(default=None, min_length=3, max_length=64)
password: str | None = Field(default=None, min_length=8, max_length=128)
is_superuser: bool | None = None
@field_validator("username", "password", mode="before")
@classmethod
def empty_str_to_none(cls, v: object) -> object:
if isinstance(v, str) and not v.strip():
return None
return v
@field_validator("username")
@classmethod
def username_chars(cls, v: str | None) -> str | None:
return None if v is None else _clean_username(v)
@model_validator(mode="after")
def something_to_change(self):
if self.username is None and self.password is None and self.is_superuser is None:
raise ValueError("Provide a username, password and/or admin change")
return self
class AuthStatusResponse(BaseModel):
auth_enabled: bool
register_open: bool
invite_required: bool = False
setup_required: bool = False # no accounts yet: the first one becomes the admin
class InviteCreateRequest(BaseModel):
email: str | None = Field(default=None, max_length=320)
expires_days: int = Field(default=7, ge=1, le=30)
grants_superuser: bool = False
@field_validator("email")
@classmethod
def email_sane(cls, v: str | None) -> str | None:
if v is None:
return None
s = v.strip().lower()
if not s:
return None
if "@" not in s or "." not in s.split("@")[-1]:
raise ValueError("Invalid email address")
return s
class InviteCreateResponse(BaseModel):
id: int
email: str
invite_url: str
delivered: bool
class InvitePublic(BaseModel):
id: int
email: str
expires_at: dt.datetime
grants_superuser: bool
created_at: dt.datetime
model_config = {"from_attributes": True}
Whitespace-only changes.
+296
View File
@@ -0,0 +1,296 @@
"""User accounts: passwords, sessions and invites (same approach as led-editor)."""
from __future__ import annotations
import datetime as dt
import os
import secrets
import smtplib
import threading
import time
from email.message import EmailMessage
import bcrypt
from sqlalchemy import delete, func, select
from sqlalchemy.orm import Session
from cashbook_app.config import env_flag
from cashbook_app.db.models import AuthSession, InviteToken, User, utc_now
SESSION_COOKIE_NAME = "cashbook_session"
SESSION_DAYS_DEFAULT = 14
def auth_enabled() -> bool:
return env_flag("AUTH_ENABLED", True)
def register_open() -> bool:
return env_flag("AUTH_REGISTER_OPEN", True)
def invite_required() -> bool:
return env_flag("AUTH_INVITE_ONLY", True)
def session_ttl_days() -> int:
try:
return max(1, min(365, int(os.environ.get("AUTH_SESSION_DAYS", str(SESSION_DAYS_DEFAULT)))))
except ValueError:
return SESSION_DAYS_DEFAULT
# --- passwords ----------------------------------------------------------------
def hash_password(password: str) -> str:
return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("ascii")
def verify_password(plain: str, hashed: str) -> bool:
try:
return bcrypt.checkpw(plain.encode("utf-8"), hashed.encode("ascii"))
except (ValueError, TypeError):
return False
# a real hash to check against when the user doesn't exist, so timing doesn't reveal it
_DUMMY_HASH = hash_password(secrets.token_urlsafe(16))
# --- failed sign-in throttle ----------------------------------------------------
MAX_FAILURES = 5
FAILURE_WINDOW = 15 * 60 # seconds
_failures: dict[str, list[float]] = {}
_failures_lock = threading.Lock()
def _recent(key: str) -> list[float]:
cutoff = time.monotonic() - FAILURE_WINDOW
times = [t for t in _failures.get(key, []) if t > cutoff]
_failures[key] = times
return times
def login_blocked(*keys: str) -> bool:
with _failures_lock:
return any(len(_recent(k)) >= MAX_FAILURES for k in keys)
def record_failure(*keys: str) -> None:
with _failures_lock:
for k in keys:
_recent(k).append(time.monotonic())
def clear_failures(*keys: str) -> None:
with _failures_lock:
for k in keys:
_failures.pop(k, None)
# --- users ----------------------------------------------------------------------
def get_user_by_username(db: Session, username: str) -> User | None:
return db.scalars(select(User).where(func.lower(User.username) == username.strip().lower())).one_or_none()
def get_user_by_id(db: Session, user_id: int) -> User | None:
return db.get(User, user_id)
def count_users(db: Session) -> int:
return int(db.scalar(select(func.count()).select_from(User)) or 0)
def list_users(db: Session) -> list[User]:
return list(db.scalars(select(User).order_by(User.username)).all())
def create_user(db: Session, username: str, password: str, *, is_superuser: bool = False) -> User:
if get_user_by_username(db, username):
raise ValueError("Username already taken")
user = User(username=username.strip(), password_hash=hash_password(password), is_superuser=is_superuser)
db.add(user)
db.commit()
db.refresh(user)
return user
def register_user(db: Session, username: str, password: str) -> User:
"""Open registration. The very first account becomes the admin."""
return create_user(db, username, password, is_superuser=count_users(db) == 0)
def register_user_with_invite(db: Session, username: str, password: str, invite_token: str) -> User:
invite = get_valid_invite(db, invite_token)
if invite is None:
raise ValueError("Invite is invalid or expired")
user = create_user(db, username, password, is_superuser=bool(invite.grants_superuser))
invite.used_at = utc_now()
invite.consumed_by_user_id = user.id
db.commit()
return user
def authenticate(db: Session, username: str, password: str) -> User | None:
user = get_user_by_username(db, username)
if user is None:
verify_password(password, _DUMMY_HASH)
return None
if not verify_password(password, user.password_hash):
return None
user.last_login_at = utc_now()
db.commit()
return user
def _other_admins(db: Session, user_id: int) -> int:
return int(db.scalar(select(func.count()).select_from(User)
.where(User.is_superuser.is_(True), User.id != user_id)) or 0)
def update_user(db: Session, user_id: int, *, username: str | None = None,
password: str | None = None, is_superuser: bool | None = None) -> User | None:
user = get_user_by_id(db, user_id)
if user is None:
return None
if username is not None and username.strip() != user.username:
dup = get_user_by_username(db, username)
if dup is not None and dup.id != user_id:
raise ValueError("Username already taken")
user.username = username.strip()
if password is not None:
user.password_hash = hash_password(password)
delete_sessions(db, user.id) # sign them out everywhere
if is_superuser is not None:
if user.is_superuser and not is_superuser and not _other_admins(db, user_id):
raise ValueError("Cannot remove the last administrator")
user.is_superuser = is_superuser
db.commit()
db.refresh(user)
return user
def change_password(db: Session, user: User, current: str, new: str, keep_token: str | None) -> None:
if not verify_password(current, user.password_hash):
raise ValueError("Current password is wrong")
user.password_hash = hash_password(new)
delete_sessions(db, user.id, except_token=keep_token) # other devices sign in again
db.commit()
def delete_user(db: Session, user_id: int) -> bool:
user = get_user_by_id(db, user_id)
if user is None:
return False
if user.is_superuser and not _other_admins(db, user_id):
raise ValueError("Cannot delete the last administrator")
db.delete(user)
db.commit()
return True
# --- sessions ---------------------------------------------------------------------
def create_session(db: Session, user: User) -> AuthSession:
row = AuthSession(user_id=user.id, token=secrets.token_urlsafe(48),
expires_at=utc_now() + dt.timedelta(days=session_ttl_days()))
db.add(row)
# tidy up expired sessions while we're here
db.execute(delete(AuthSession).where(AuthSession.expires_at < utc_now()))
db.commit()
db.refresh(row)
return row
def get_session_user(db: Session, token: str | None) -> User | None:
if not token:
return None
row = db.scalars(select(AuthSession).where(AuthSession.token == token)).one_or_none()
if row is None or row.expires_at < utc_now():
return None
return get_user_by_id(db, row.user_id)
def delete_session(db: Session, token: str | None) -> None:
if token:
db.execute(delete(AuthSession).where(AuthSession.token == token))
db.commit()
def delete_sessions(db: Session, user_id: int, except_token: str | None = None) -> None:
stmt = delete(AuthSession).where(AuthSession.user_id == user_id)
if except_token:
stmt = stmt.where(AuthSession.token != except_token)
db.execute(stmt)
# --- invites ----------------------------------------------------------------------
def create_invite(db: Session, email: str, invited_by_user_id: int | None = None,
expires_days: int = 7, grants_superuser: bool = False) -> InviteToken:
row = InviteToken(
email=email.strip().lower(), token=secrets.token_urlsafe(36),
expires_at=utc_now() + dt.timedelta(days=max(1, min(30, int(expires_days)))),
invited_by_user_id=invited_by_user_id, grants_superuser=grants_superuser,
)
db.add(row)
db.commit()
db.refresh(row)
return row
def get_valid_invite(db: Session, token: str | None) -> InviteToken | None:
if not token:
return None
row = db.scalars(select(InviteToken).where(InviteToken.token == token.strip())).one_or_none()
if row is None or row.used_at is not None or row.expires_at < utc_now():
return None
return row
def list_pending_invites(db: Session) -> list[InviteToken]:
return list(db.scalars(select(InviteToken)
.where(InviteToken.used_at.is_(None), InviteToken.expires_at >= utc_now())
.order_by(InviteToken.created_at.desc())).all())
def revoke_invite(db: Session, invite_id: int) -> bool:
row = db.get(InviteToken, invite_id)
if row is None or row.used_at is not None:
return False
db.delete(row)
db.commit()
return True
def build_invite_url(token: str, base_url: str) -> str:
base = (os.environ.get("PUBLIC_BASE_URL") or base_url).rstrip("/")
return f"{base}/register?invite={token}"
def send_invite_email(email: str, invite_url: str) -> bool:
host = (os.environ.get("SMTP_HOST") or "").strip()
if not host:
return False
port = int((os.environ.get("SMTP_PORT") or "587").strip())
user = (os.environ.get("SMTP_USER") or "").strip()
password = os.environ.get("SMTP_PASSWORD") or ""
sender = (os.environ.get("SMTP_FROM") or user or "noreply@cashbook.local").strip()
msg = EmailMessage()
msg["Subject"] = "You're invited to the Cashbook"
msg["From"] = sender
msg["To"] = email
msg.set_content(
"You've been invited to the Cashbook.\n\n"
f"Use this link to create your account:\n{invite_url}\n\n"
"If you didn't expect this invite, you can ignore this message.\n"
)
with smtplib.SMTP(host, port, timeout=10) as smtp:
if env_flag("SMTP_TLS", True):
smtp.starttls()
if user:
smtp.login(user, password)
smtp.send_message(msg)
return True
+19
View File
@@ -0,0 +1,19 @@
"""The shared headless LibreOffice, used for spreadsheet export and migration."""
from __future__ import annotations
_office = None
def office():
global _office
if _office is None:
from office import Office # needs LibreOffice's Python bridge
_office = Office()
return _office
def stop() -> None:
global _office
if _office is not None:
_office.stop()
_office = None
@@ -0,0 +1,164 @@
"""ANZ statement files on disk: listing, uploads, renaming and removal."""
from __future__ import annotations
import os
import re
import shutil
import threading
import uuid
from pathlib import Path
import ledger
from cashbook_app import config
from statements import read_statement, statement_info
MAX_UPLOAD = 20 * 1024 * 1024
_cache: dict = {}
_cache_lock = threading.Lock()
class StatementError(ValueError):
pass
def parse(path: str) -> dict:
"""Statement info and transactions, cached until the file changes."""
key = (path, os.path.getmtime(path))
with _cache_lock:
if key in _cache:
return _cache[key]
try:
parsed = {"info": statement_info(path), "txns": read_statement(path)}
except Exception as e: # not an ANZ statement, or unreadable
parsed = {"info": {"error": str(e).splitlines()[0][:200]}, "txns": []}
with _cache_lock:
_cache[key] = parsed
return parsed
def _within(path: Path, folder: Path) -> bool:
try:
path.resolve().relative_to(folder.resolve())
return True
except ValueError:
return False
def folder_label(d: Path) -> str:
data = config.data_dir()
if _within(d, data):
rel = d.resolve().relative_to(data)
return "data" if str(rel) == "." else f"data/{rel}"
return str(d).replace(os.path.expanduser("~"), "~")
def list_statements(conn=None, book=None) -> list[dict]:
"""All statement files; with a ledger connection and book, also how many are imported."""
out, seen = [], set()
data = config.data_dir()
for d in config.statement_dirs():
if not d.is_dir():
continue
for entry in sorted(d.iterdir()):
path = str(entry.resolve())
name = entry.name
if path in seen or name.startswith(".") or not name.lower().endswith((".csv", ".pdf")) \
or not entry.is_file():
continue
seen.add(path)
st = entry.stat()
out.append({"path": path, "name": name, "folder": folder_label(d),
"managed": _within(entry, data), "size": st.st_size, "modified": st.st_mtime,
"kind": name.rsplit(".", 1)[1].lower(), **parse(path)["info"]})
# flag files that are byte-for-byte copies of one listed earlier
first: dict[str, str] = {}
for s in out:
if s.get("sha256"):
s["copy_of"] = first.get(s["sha256"])
first.setdefault(s["sha256"], s["name"])
if conn is not None and book is not None:
for s in out:
txns = parse(s["path"])["txns"]
s["imported"] = ledger.count_imported(conn, book, txns) if txns else 0
return out
def statement_path(path: str | None, must_manage: bool = False) -> str:
for s in list_statements():
if s["path"] == path:
if must_manage and not s["managed"]:
raise StatementError(f"{s['name']} is in {s['folder']}, which is read-only here")
return path
raise StatementError(f"unknown statement {path}")
def statement_paths(paths: list[str] | None) -> list[str]:
if not paths:
raise StatementError("select at least one statement")
allowed = {s["path"] for s in list_statements()}
for p in paths:
if p not in allowed:
raise StatementError(f"unknown statement {p}")
return paths
def safe_name(filename: str) -> str:
name = re.sub(r"[^\w.\-]+", "_", os.path.basename(filename)).strip("._")
if not name.lower().endswith((".csv", ".pdf")):
raise StatementError("only .csv and .pdf statements")
return name
def unique_path(folder: Path, name: str) -> Path:
base, ext = os.path.splitext(name)
path, n = folder / name, 2
while path.exists():
path, n = folder / f"{base}-{n}{ext}", n + 1
return path
def save_upload(filename: str, data: bytes) -> str:
name = safe_name(filename)
if len(data) > MAX_UPLOAD:
raise StatementError("file is too large for a statement")
uploads = config.uploads_dir()
uploads.mkdir(parents=True, exist_ok=True)
tmp = uploads / f".upload-{uuid.uuid4().hex}{os.path.splitext(name)[1]}"
tmp.write_bytes(data)
try:
info = statement_info(str(tmp))
except Exception as e:
tmp.unlink()
raise StatementError(f"{filename} doesn't look like an ANZ statement ({str(e).splitlines()[0][:120]})")
if not info["count"]:
tmp.unlink()
raise StatementError(f"{filename} has no transactions")
for s in list_statements():
if s.get("sha256") == info["sha256"]:
tmp.unlink()
raise StatementError(f"{filename} is already here as {s['name']}")
path = unique_path(uploads, name)
os.replace(tmp, path)
return str(path)
def rename_statement(path: str | None, new_name: str) -> str:
path = statement_path(path, must_manage=True)
name = safe_name(new_name)
if os.path.splitext(name)[1].lower() != os.path.splitext(path)[1].lower():
raise StatementError("keep the same file extension")
target = os.path.join(os.path.dirname(path), name)
if target != path and os.path.exists(target):
raise StatementError(f"{name} already exists")
os.rename(path, target)
return target
def delete_statement(path: str | None) -> str:
"""Move a statement into statements/.trash rather than deleting it outright."""
path = statement_path(path, must_manage=True)
trash = config.uploads_dir() / ".trash"
trash.mkdir(parents=True, exist_ok=True)
target = unique_path(trash, os.path.basename(path))
shutil.move(path, target)
return str(target)
+92
View File
@@ -0,0 +1,92 @@
#!/usr/bin/env python3
"""Export a cashbook from the database as a spreadsheet (.ods / .xlsx) or CSV.
The spreadsheet uses the cashbook template's layout: one sheet per month with
GST and category columns, plus the summary sheet.
Usage:
export.py BOOK OUT.{ods,xlsx,csv} [--data DIR]
"""
import argparse
import csv
import io
import os
import shutil
import tempfile
import ledger
HERE = os.path.dirname(os.path.abspath(__file__))
TEMPLATE = os.path.join(HERE, "template.ods")
FORMATS = {
"ods": ("application/vnd.oasis.opendocument.spreadsheet", None),
"xlsx": ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Calc MS Excel 2007 XML"),
"csv": ("text/csv", None),
}
def export_csv(conn, book):
out = io.StringIO()
w = csv.writer(out)
w.writerow(["Date", "Month", "Reference", "Payment origin", "In", "Out", "GST type",
"GST", "Net (excl GST)", "Category"])
for sheet in ledger.FY_ORDER:
m = ledger.read_month(conn, book, sheet)
for r in m["rows"]:
w.writerow([r["date"], m["label"], r["reference"], r["origin"],
f"{r['income']:.2f}" if r["income"] else "",
f"{r['expense']:.2f}" if r["expense"] else "",
r["gst"], f"{r['gst_in'] or r['gst_out']:.2f}", f"{r['net']:.2f}",
r["category"] or "Unallocated"])
return out.getvalue().encode("utf-8-sig") # BOM so Excel reads UTF-8
def export_spreadsheet(conn, book, fmt, open_fn):
"""Fill a copy of the template and return its bytes. `open_fn` opens a document (office.Office.open)."""
import cashbook # needs LibreOffice's Python bridge
from office import prop
months = {s: ledger.read_month(conn, book, s)["rows"] for s in ledger.FY_ORDER}
tmp = tempfile.mkdtemp(prefix="cashbook_export_")
try:
path = os.path.join(tmp, "book.ods")
shutil.copy(TEMPLATE, path)
with open_fn(path) as doc:
cashbook.write_book(doc, book["start_year"], book["opening_cents"] / 100, months)
if fmt == "ods":
doc.store()
else:
import uno
path = os.path.join(tmp, f"book.{fmt}")
doc.storeToURL(uno.systemPathToFileUrl(path), (prop("FilterName", FORMATS[fmt][1]),))
with open(path, "rb") as f:
return f.read()
finally:
shutil.rmtree(tmp, ignore_errors=True)
def export(conn, book, fmt, open_fn=None):
if fmt not in FORMATS:
raise ValueError(f"unknown format {fmt!r}; use ods, xlsx or csv")
if fmt == "csv":
return export_csv(conn, book)
if open_fn is None:
from office import open_book as open_fn
return export_spreadsheet(conn, book, fmt, open_fn)
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
ap.add_argument("book", help="book name")
ap.add_argument("out", help="file to write; the extension picks the format")
ap.add_argument("--data", default=os.path.join(os.path.dirname(HERE), "data"))
args = ap.parse_args()
conn = ledger.connect(os.path.join(args.data, "cashbook.db"))
fmt = os.path.splitext(args.out)[1].lstrip(".").lower()
data = export(conn, ledger.get_book(conn, args.book), fmt)
with open(args.out, "wb") as f:
f.write(data)
print(f"Wrote {args.out}")
if __name__ == "__main__":
main()
+384
View File
@@ -0,0 +1,384 @@
"""Cashbooks stored in SQLite: books, their transactions, and the figures derived from them.
Amounts are stored as signed integer cents (positive = money in). GST is worked out
the way the cashbook spreadsheet does it: 3/23 of the amount for "Incl GST" rows.
"""
import sqlite3
from collections import Counter
from datetime import date
from statements import ORIGIN, classify, describe
SHEETS = {1: "JAN", 2: "FEB", 3: "MAR", 4: "APR", 5: "MAY", 6: "JUN",
7: "JUL", 8: "AUG", 9: "SEPT", 10: "OCT", 11: "NOV", 12: "DEC"}
FY_ORDER = ["APR", "MAY", "JUN", "JUL", "AUG", "SEPT", "OCT", "NOV", "DEC", "JAN", "FEB", "MAR"]
MONTH_OF = {v: k for k, v in SHEETS.items()}
MONTH_NAMES = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"]
# The cashbook template's category columns, in order
INCOME_CATEGORIES = ["Sales", "Grants", "Funds Intr.", "Interest", "Sundry"]
EXPENSE_CATEGORIES = ["Purchases", "Wages", "Rent", "Motor vehicle", "Loan repmt", "Drawings",
"Bank fees", "Telephone & Power", "ACC & Insurance",
"Postage & Stationery", "Bank Fees", "Sundry"]
# Incl GST: taxable at 15%, amount includes GST. Zero-rated: taxable at 0% (e.g. exported
# services) - no GST, but counts as sales on the GST return. Excl GST: outside GST
# (wages, drawings, transfers, interest, overseas purchases).
GST_TYPES = ("Incl GST", "Zero-rated", "Excl GST")
SCHEMA = """
CREATE TABLE IF NOT EXISTS books (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
start_year INTEGER NOT NULL, -- financial year runs 1 April start_year to 31 March
opening_cents INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY,
book_id INTEGER NOT NULL REFERENCES books(id) ON DELETE CASCADE,
date TEXT NOT NULL, -- ISO date
reference TEXT NOT NULL DEFAULT '',
origin TEXT NOT NULL DEFAULT '', -- payment origin, e.g. Eftpos
amount_cents INTEGER NOT NULL, -- positive in, negative out
gst TEXT NOT NULL CHECK (gst IN ('Incl GST', 'Zero-rated', 'Excl GST')),
category TEXT, -- NULL = unallocated
source TEXT, -- statement file it came from
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS transactions_book_date ON transactions (book_id, date);
"""
class NotFound(Exception):
pass
SCHEMA_VERSION = 2
def connect(path):
conn = sqlite3.connect(path, timeout=30, check_same_thread=False)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA foreign_keys = ON")
conn.execute("PRAGMA journal_mode = WAL")
conn.executescript(SCHEMA)
_upgrade(conn)
return conn
def _upgrade(conn):
version = conn.execute("PRAGMA user_version").fetchone()[0]
if version >= SCHEMA_VERSION:
return
sql = conn.execute("SELECT sql FROM sqlite_master WHERE name = 'transactions'").fetchone()[0]
if "Zero-rated" not in sql:
# version 1 -> 2: allow 'Zero-rated'. SQLite can't change a CHECK, so rebuild the table.
conn.execute("PRAGMA foreign_keys = OFF")
with conn:
conn.execute("ALTER TABLE transactions RENAME TO transactions_v1")
conn.executescript(SCHEMA)
conn.execute("INSERT INTO transactions SELECT * FROM transactions_v1")
conn.execute("DROP TABLE transactions_v1")
conn.execute("PRAGMA foreign_keys = ON")
conn.execute(f"PRAGMA user_version = {SCHEMA_VERSION}")
def cents(amount):
return int(round(amount * 100))
def gst_of(amount_cents, gst):
"""GST component in dollars: 3/23 of a GST-inclusive amount, rounded half-up to the
cent per transaction, as the cashbook spreadsheet does ("precision as shown")."""
if gst != "Incl GST":
return 0.0
return (abs(amount_cents) * 6 + 23) // 46 / 100
# --- books ------------------------------------------------------------------
def list_books(conn):
return [dict(r) for r in conn.execute(
"SELECT b.name, b.start_year, b.opening_cents / 100.0 AS opening, COUNT(t.id) AS count "
"FROM books b LEFT JOIN transactions t ON t.book_id = b.id GROUP BY b.id ORDER BY b.name")]
def get_book(conn, name):
row = conn.execute("SELECT * FROM books WHERE name = ?", (name,)).fetchone()
if row is None:
raise NotFound(f"no book called {name!r}")
return row
def create_book(conn, name, year, opening_balance=0.0):
name = name.strip()
if not name:
raise ValueError("give the book a name")
if conn.execute("SELECT 1 FROM books WHERE name = ?", (name,)).fetchone():
raise ValueError(f"a book called {name!r} already exists")
with conn:
conn.execute("INSERT INTO books (name, start_year, opening_cents) VALUES (?, ?, ?)",
(name, int(year), cents(opening_balance or 0)))
return get_book(conn, name)
def fy_range(book):
y = book["start_year"]
return date(y, 4, 1), date(y + 1, 3, 31)
def month_range(book, sheet):
m = MONTH_OF[sheet]
y = book["start_year"] + (1 if m < 4 else 0)
end = date(y + (m == 12), m % 12 + 1, 1)
return date(y, m, 1), end # end is exclusive
# --- importing --------------------------------------------------------------
def _existing(conn, book):
return Counter((r["date"], r["amount_cents"]) for r in conn.execute(
"SELECT date, amount_cents FROM transactions WHERE book_id = ?", (book["id"],)))
def _key(t):
return t["date"].isoformat(), cents(t["amount"])
def find_new(conn, book, txns):
"""Split statement transactions into those not yet in the book, and those already there.
Matching is on date and amount, so a CSV export and a PDF statement of the same
period are recognised as the same transactions. Each statement is matched against
the book plus the statements before it; repeats within one statement (e.g. two equal
transfers on a day) are kept.
"""
have = _existing(conn, book)
new, dups, by_source = [], [], {}
for t in txns:
by_source.setdefault(t.get("source"), []).append(t)
for group in by_source.values():
seen = Counter()
for t in sorted(group, key=lambda t: t["date"]):
seen[_key(t)] += 1
(dups if seen[_key(t)] <= have[_key(t)] else new).append(t)
for k, n in seen.items(): # the book now effectively holds this statement too
have[k] = max(have[k], n)
return new, dups
def count_imported(conn, book, txns):
"""How many of one statement's transactions are already in the book."""
return len(find_new(conn, book, txns)[1])
def plan_import(conn, book, txns):
"""What importing would do, without changing anything."""
new_ids = {id(t) for t in find_new(conn, book, txns)[0]}
start, end = fy_range(book)
out = []
for t in sorted(txns, key=lambda t: t["date"]):
gst, category = classify(t)
status = "duplicate" if id(t) not in new_ids else \
"wrong year" if not start <= t["date"] <= end else "new"
out.append({"date": t["date"].isoformat(), "sheet": SHEETS[t["date"].month],
"reference": describe(t), "amount": t["amount"], "gst": gst,
"category": category or "", "origin": ORIGIN.get(t["Type"], ""),
"status": status})
return out
def import_transactions(conn, book, txns):
"""Add the new transactions that fall in the book's year.
Returns {sheet: (added, total)}; transactions from other years are left out.
"""
start, end = fy_range(book)
new = {id(t) for t in find_new(conn, book, txns)[0]}
result = {}
with conn:
for t in sorted(txns, key=lambda t: t["date"]):
sheet = SHEETS[t["date"].month]
added, total = result.get(sheet, (0, 0))
if id(t) in new and start <= t["date"] <= end:
gst, category = classify(t)
conn.execute(
"INSERT INTO transactions (book_id, date, reference, origin, amount_cents, gst, category, source) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
(book["id"], t["date"].isoformat(), describe(t), ORIGIN.get(t["Type"], ""),
cents(t["amount"]), gst, category, t.get("source")))
added += 1
result[sheet] = (added, total + 1)
return dict(sorted(result.items(), key=lambda kv: FY_ORDER.index(kv[0])))
def add_rows(conn, book, rows):
"""Insert already-classified rows (dicts with date, reference, origin, amount, gst, category)."""
with conn:
conn.executemany(
"INSERT INTO transactions (book_id, date, reference, origin, amount_cents, gst, category, source) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
[(book["id"], r["date"], r["reference"], r.get("origin", ""), cents(r["amount"]),
r["gst"], r.get("category") or None, r.get("source")) for r in rows])
# --- reading ----------------------------------------------------------------
def transactions(conn, book, start=None, end=None):
sql, args = "SELECT * FROM transactions WHERE book_id = ?", [book["id"]]
if start:
sql, args = sql + " AND date >= ?", args + [start.isoformat()]
if end:
sql, args = sql + " AND date < ?", args + [end.isoformat()]
return conn.execute(sql + " ORDER BY date, id", args).fetchall()
def _row(t):
amount = t["amount_cents"]
gst = gst_of(amount, t["gst"])
income = amount > 0
net = abs(amount) / 100 - gst
return {
"row": t["id"], "date": t["date"], "reference": t["reference"], "gst": t["gst"],
"origin": t["origin"],
"income": amount / 100 if income else 0, "gst_in": gst if income else 0,
"expense": -amount / 100 if not income else 0, "gst_out": gst if not income else 0,
"category": t["category"] or "", "unallocated": 0 if t["category"] else net,
"net": net,
}
def _balance_before(conn, book, day):
total = conn.execute("SELECT COALESCE(SUM(amount_cents), 0) FROM transactions "
"WHERE book_id = ? AND date < ?", (book["id"], day.isoformat())).fetchone()[0]
return (book["opening_cents"] + total) / 100
def read_month(conn, book, sheet):
start, end = month_range(book, sheet)
rows = [_row(t) for t in transactions(conn, book, start, end)]
opening = _balance_before(conn, book, start)
return {
"sheet": sheet,
"label": f"{MONTH_NAMES[start.month - 1]} {start.year}",
"opening": opening,
"closing": opening + sum(r["income"] - r["expense"] for r in rows),
"income_categories": INCOME_CATEGORIES,
"expense_categories": EXPENSE_CATEGORIES,
"rows": rows,
}
def read_summary(conn, book):
months = []
for sheet in FY_ORDER:
m = read_month(conn, book, sheet)
rows = m["rows"]
total = lambda k: sum(r[k] for r in rows)
months.append({
"sheet": sheet, "label": m["label"], "opening": m["opening"], "closing": m["closing"],
"count": len(rows),
# GST-inclusive amounts, as entered on a GST return
"sales_incl": sum(r["income"] for r in rows if r["gst"] == "Incl GST"),
"purchases_incl": sum(r["expense"] for r in rows if r["gst"] == "Incl GST"),
"income": total("income"), "gst_in": total("gst_in"),
"expenses": total("expense"), "gst_out": total("gst_out"),
"unallocated_in": sum(r["unallocated"] for r in rows if r["income"]),
"unallocated_out": sum(r["unallocated"] for r in rows if r["expense"]),
"income_cats": {c: sum(r["net"] for r in rows if r["income"] and r["category"] == c)
for c in INCOME_CATEGORIES},
"expense_cats": {c: sum(r["net"] for r in rows if r["expense"] and r["category"] == c)
for c in EXPENSE_CATEGORIES},
})
return {
"name": book["name"],
"year": book["start_year"],
"opening": book["opening_cents"] / 100,
"closing": months[-1]["closing"],
"income_categories": INCOME_CATEGORIES,
"expense_categories": EXPENSE_CATEGORIES,
"months": months,
}
# --- GST returns ------------------------------------------------------------
FREQUENCIES = {"six": 6, "two": 2, "one": 1} # months per GST period
def _add_months(d, n):
m = d.month - 1 + n
return date(d.year + m // 12, m % 12 + 1, 1)
def due_date(period_end):
"""IRD due date: the 28th of the next month; periods ending 31 March are due 7 May,
and those ending 30 November are due 15 January."""
if period_end.month == 3:
return date(period_end.year, 5, 7)
if period_end.month == 11:
return date(period_end.year + 1, 1, 15)
nxt = _add_months(period_end, 1)
return date(nxt.year, nxt.month, 28)
def _gst_on(total):
"""GST in a GST-inclusive total, to the cent (the return's 3/23 calculation)."""
return (cents(total) * 6 + 23) // 46 / 100
def gst_return(conn, book, frequency="six"):
"""The GST101A boxes for each filing period in the book's year (payments basis).
Periods line up with a 31 March balance date: six-monthly Apr-Sep and Oct-Mar,
two-monthly Apr-May ... Feb-Mar, or monthly.
"""
months = FREQUENCIES[frequency]
fy_start, _ = fy_range(book)
periods = []
for i in range(12 // months):
start = _add_months(fy_start, i * months)
end = _add_months(start, months) # exclusive
rows = [_row(t) for t in transactions(conn, book, start, end)]
box5 = sum(r["income"] for r in rows if r["gst"] in ("Incl GST", "Zero-rated"))
box6 = sum(r["income"] for r in rows if r["gst"] == "Zero-rated")
box7 = box5 - box6
box8 = _gst_on(box7)
box11 = sum(r["expense"] for r in rows if r["gst"] == "Incl GST")
box12 = _gst_on(box11)
box10, box14 = box8, box12 # no adjustments (boxes 9 and 13) are tracked
last_day = date.fromordinal(end.toordinal() - 1)
periods.append({
"start": start.isoformat(), "end": last_day.isoformat(),
"due": due_date(last_day).isoformat(),
"count": len(rows),
"boxes": {"5": round(box5, 2), "6": round(box6, 2), "7": round(box7, 2), "8": box8,
"9": 0.0, "10": box10, "11": round(box11, 2), "12": box12, "13": 0.0,
"14": box14, "15": round(box10 - box14, 2)},
# money in that isn't on the return, so it can be checked
"excluded_income": round(sum(r["income"] for r in rows if r["gst"] == "Excl GST"), 2),
"excluded_expenses": round(sum(r["expense"] for r in rows if r["gst"] != "Incl GST"), 2),
"unallocated": sum(1 for r in rows if not r["category"]),
})
return {"name": book["name"], "year": book["start_year"], "frequency": frequency,
"periods": periods}
# --- editing ----------------------------------------------------------------
def update_row(conn, book, row_id, gst=None, category=None):
t = conn.execute("SELECT * FROM transactions WHERE id = ? AND book_id = ?",
(row_id, book["id"])).fetchone()
if t is None:
raise NotFound("no such transaction")
with conn:
if gst is not None:
if gst not in GST_TYPES:
raise ValueError(f"gst must be one of {', '.join(GST_TYPES)}")
conn.execute("UPDATE transactions SET gst = ? WHERE id = ?", (gst, row_id))
if category is not None:
allowed = INCOME_CATEGORIES if t["amount_cents"] > 0 else EXPENSE_CATEGORIES
if category and category not in allowed:
raise ValueError(f"unknown category {category!r}")
conn.execute("UPDATE transactions SET category = ? WHERE id = ?", (category or None, row_id))
+68
View File
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
"""Copy cashbooks kept in .ods spreadsheets into the database.
Each spreadsheet becomes a book named after the file (without .ods). Books that
already exist are skipped, and the spreadsheets are left as they are.
Usage:
migrate.py BOOK.ods [BOOK.ods ...] [--data DIR]
"""
import argparse
import os
import ledger
def migrate_ods(conn, path, open_fn):
"""Add one spreadsheet as a book. Returns (book name, rows added, warnings), or None if it exists."""
import cashbook # needs LibreOffice's Python bridge
name = os.path.splitext(os.path.basename(path))[0]
if conn.execute("SELECT 1 FROM books WHERE name = ?", (name,)).fetchone():
return None
with open_fn(path, read_only=True) as doc:
year, opening, rows, warnings = cashbook.read_book(doc)
book = ledger.create_book(conn, name, year, opening)
for r in rows:
r["source"] = os.path.basename(path)
ledger.add_rows(conn, book, rows)
return name, len(rows), warnings
def migrate_folder(conn, folder, open_fn):
"""Migrate every .ods in a folder that isn't a book yet. Returns a list of messages."""
messages = []
for f in sorted(os.listdir(folder)):
if not f.endswith(".ods") or f == "template.ods":
continue
try:
result = migrate_ods(conn, os.path.join(folder, f), open_fn)
except Exception as e:
messages.append(f"Couldn't migrate {f}: {e}")
continue
if result:
name, n, warnings = result
messages.append(f"Migrated {f} into book {name!r} ({n} transactions)")
messages.extend(f" {w}" for w in warnings)
return messages
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
ap.add_argument("files", nargs="+")
ap.add_argument("--data", default=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "data"))
args = ap.parse_args()
from office import open_book
conn = ledger.connect(os.path.join(args.data, "cashbook.db"))
for path in args.files:
result = migrate_ods(conn, path, open_book)
if result is None:
print(f"{path}: a book with that name already exists, skipped")
else:
name, n, warnings = result
print(f"{path}: added book {name!r} with {n} transactions")
for w in warnings:
print(f" {w}")
if __name__ == "__main__":
main()
+10 -27
View File
@@ -1,24 +1,18 @@
#!/usr/bin/env python3
"""Create a new cashbook for a financial year by copying src/template.ods.
Sets the starting year (April) and, optionally, the opening bank balance on
the 'Summary report' sheet so the month sheets pick them up.
"""Create a new cashbook (in the database) for a financial year, April to March.
Usage:
new_book.py OUT.ods [--year 2025] [--opening-balance 1234.56]
new_book.py NAME [--year 2025] [--opening-balance 1234.56] [--data DIR]
"""
import argparse
import os
import shutil
import sys
from datetime import date
from cashbook import setup_book
from office import open_book
import ledger
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
TEMPLATE = os.path.join(HERE, "template.ods")
def current_fy_start():
@@ -26,33 +20,22 @@ def current_fy_start():
return today.year if today.month >= 4 else today.year - 1
def create_book(out, year, opening_balance=None, open_fn=open_book, template=TEMPLATE):
if os.path.exists(out):
raise FileExistsError(f"{out} already exists")
shutil.copy2(template, out)
try:
with open_fn(out) as doc:
setup_book(doc, year, opening_balance)
doc.store()
except Exception:
os.remove(out)
raise
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
ap.add_argument("out", help="new cashbook .ods to create")
ap.add_argument("name", help="name for the book, e.g. cashbook-2025-26")
ap.add_argument("--year", type=int, default=current_fy_start(),
help="calendar year the financial year starts in (default: current)")
ap.add_argument("--opening-balance", type=float,
ap.add_argument("--opening-balance", type=float, default=0.0,
help="bank balance at 1 April (default: 0)")
ap.add_argument("--data", default=os.path.join(ROOT, "data"))
args = ap.parse_args()
conn = ledger.connect(os.path.join(args.data, "cashbook.db"))
try:
create_book(args.out, args.year, args.opening_balance)
except FileExistsError as e:
ledger.create_book(conn, args.name, args.year, args.opening_balance)
except ValueError as e:
sys.exit(str(e))
print(f"Created {args.out} for April {args.year} - March {args.year + 1}")
print(f"Created {args.name} for April {args.year} - March {args.year + 1}")
if __name__ == "__main__":
+67
View File
@@ -0,0 +1,67 @@
:root {
--bg: #f6f7f9; --panel: #ffffff; --text: #1d2330; --muted: #6a7282; --line: #e3e6eb;
--accent: #0b6bcb; --accent-ink: #ffffff; --accent-soft: #e6f0fb;
--ok: #1a7f4b; --ok-soft: #e4f4ea; --bad: #b4372f; --bad-soft: #fbe7e5; --warn: #a15c00; --warn-soft: #fdf1df;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #12151b; --panel: #1a1f27; --text: #e6e9ef; --muted: #98a1b0; --line: #2b323d;
--accent: #5aa4ef; --accent-ink: #0b1220; --accent-soft: #1d2d42;
--ok: #5fcf91; --ok-soft: #173326; --bad: #f08a80; --bad-soft: #3b1f1d; --warn: #f0b660; --warn-soft: #3a2c15;
}
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text);
font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); }
input, select, button { font: inherit; color: inherit; }
input[type=text], input[type=password], input[type=email], input[type=number], select {
width: 100%; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; }
input:focus, select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
label { display: block; margin: 12px 0 4px; font-size: 12px; color: var(--muted); }
label.inline { display: flex; align-items: center; gap: 6px; margin: 10px 0 0; color: var(--text); font-size: 14px; }
button { border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 6px 12px; cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--bad); }
button:disabled { opacity: .55; cursor: default; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--bad); min-height: 1.3em; margin-top: 8px; font-size: 13px; }
.notice { background: var(--accent-soft); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; font-size: 13px; }
/* sign-in / register card */
.center { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.card { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 24px; }
.card h1 { font-size: 20px; margin: 0 0 4px; }
.card button.primary { width: 100%; margin-top: 16px; padding: 9px; }
.card .links { margin-top: 14px; font-size: 13px; }
/* admin page */
header { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; padding: 12px 20px; background: var(--panel);
border-bottom: 1px solid var(--line); }
header h1 { font-size: 17px; margin: 0; }
header .spacer { flex: 1; }
main { max-width: 980px; margin: 0 auto; padding: 16px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 16px; margin-bottom: 16px; }
.panel h2 { font-size: 15px; margin: 0 0 10px; }
.scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 7px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; }
td.actions { white-space: nowrap; text-align: right; }
td.actions button { padding: 3px 9px; font-size: 12px; }
.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; background: var(--line); color: var(--muted); }
.chip.admin { background: var(--accent-soft); color: var(--accent); }
.chip.you { background: var(--ok-soft); color: var(--ok); }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > div { flex: 1; min-width: 160px; }
.row > div.shrink { flex: 0 0 auto; min-width: 0; }
.invite-link { display: flex; gap: 8px; margin-top: 12px; }
.invite-link input { font-family: ui-monospace, monospace; font-size: 12px; }
#toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
padding: 8px 16px; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: 90vw; }
#toast.show { opacity: 1; } #toast.error { background: var(--bad); color: #fff; }
dialog { border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--text); padding: 18px; width: min(400px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog .buttons { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
+260 -55
View File
@@ -106,6 +106,29 @@
.icon-btns button { padding: 2px 8px; font-size: 12px; }
.chip.partial { background: var(--warn-soft); color: var(--warn); }
.statements .status { margin-top: 2px; }
.menu { position: relative; }
.menu summary { list-style: none; border: 1px solid var(--line); background: var(--panel); border-radius: 6px;
padding: 5px 12px; cursor: pointer; }
.menu summary::-webkit-details-marker { display: none; }
.menu summary::after { content: " ▾"; color: var(--muted); }
.menu[open] summary, .menu summary:hover { border-color: var(--accent); }
.menu-items { position: absolute; top: calc(100% + 4px); left: 0; z-index: 8; min-width: 240px; background: var(--panel);
border: 1px solid var(--line); border-radius: 8px; padding: 4px; box-shadow: 0 6px 20px rgba(0,0,0,.15); }
.menu-items a { display: block; padding: 7px 10px; border-radius: 6px; color: var(--text); text-decoration: none; }
.menu-items a:hover { background: var(--accent-soft); }
.menu-items.right { left: auto; right: 0; min-width: 180px; }
dialog input[type=password] { width: 100%; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; }
.gst-card { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin-bottom: 14px; }
.gst-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.gst-result { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.gst-boxes td { white-space: normal; }
.gst-boxes td.box { width: 2.5em; font-weight: 700; color: var(--accent); }
.gst-boxes tr.total td { font-weight: 600; }
.gst-boxes .copy { border: 0; background: none; padding: 2px 6px; font: inherit; font-variant-numeric: tabular-nums; cursor: copy; border-radius: 4px; }
.gst-boxes .copy:hover { background: var(--accent-soft); }
.gst-notes div { margin-top: 6px; }
.tax-table tr.tax-head td { font-weight: 700; padding-top: 14px; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
.tax-input { width: 120px; text-align: right; padding: 3px 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; }
.row-toolbar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
</style>
</head>
@@ -114,9 +137,24 @@
<h1>Cashbook</h1>
<select id="book" aria-label="Cashbook"></select>
<button id="newBookBtn">New book…</button>
<button id="openBtn" title="Open this book in LibreOffice">Open in LibreOffice</button>
<details class="menu" id="exportMenu">
<summary>Export</summary>
<div class="menu-items">
<a data-format="xlsx" download>Excel spreadsheet (.xlsx)</a>
<a data-format="ods" download>LibreOffice spreadsheet (.ods)</a>
<a data-format="csv" download>CSV (all transactions)</a>
</div>
</details>
<span class="spacer"></span>
<span id="bookInfo" class="muted small"></span>
<details class="menu" id="userMenu" hidden>
<summary id="userName"></summary>
<div class="menu-items right">
<a href="/users" id="usersLink" hidden>Users</a>
<a href="#" id="passwordLink">Change password</a>
<a href="#" id="logoutLink">Sign out</a>
</div>
</details>
</header>
<div class="layout">
@@ -137,13 +175,31 @@
<main class="panel" id="main"></main>
</div>
<dialog id="passwordDialog">
<form method="dialog" id="passwordForm">
<h2 style="margin:0 0 4px;font-size:16px">Change password</h2>
<div class="muted small">Your other devices will be signed out.</div>
<label for="pwCurrent">Current password</label>
<input id="pwCurrent" type="password" autocomplete="current-password" required>
<label for="pwNew">New password (at least 8 characters)</label>
<input id="pwNew" type="password" autocomplete="new-password" minlength="8" required>
<label for="pwConfirm">New password again</label>
<input id="pwConfirm" type="password" autocomplete="new-password" minlength="8" required>
<div id="pwError" class="small" style="color:var(--out);min-height:1.2em;margin-top:6px"></div>
<div class="actions" style="justify-content:flex-end">
<button value="cancel" formnovalidate>Cancel</button>
<button value="ok" class="primary" id="pwSubmit">Change password</button>
</div>
</form>
</dialog>
<dialog id="newBook">
<form method="dialog" id="newBookForm">
<h2 style="margin:0 0 4px;font-size:16px">New cashbook</h2>
<div class="muted small">Copies template.ods for a financial year (April to March).</div>
<div class="muted small">A book covers one financial year, April to March.</div>
<label for="nbYear">Year starting April</label>
<input id="nbYear" type="number" required min="2000" max="2100">
<label for="nbName">File name</label>
<label for="nbName">Name</label>
<input id="nbName" type="text" required>
<label for="nbBalance">Opening bank balance (1 April)</label>
<input id="nbBalance" type="number" step="0.01" placeholder="e.g. 74.70">
@@ -171,13 +227,21 @@ const esc = s => String(s ?? "").replace(/[&<>"']/g, c => ({ "&": "&amp;", "<":
const fmtDate = iso => { if (!/^\d{4}-\d\d-\d\d$/.test(iso)) return esc(iso); const [y, m, d] = iso.split("-"); return `${d}/${m}/${y}`; };
let busyCount = 0;
async function call(path, body) {
// FastAPI errors come back as {detail: "..."} or, for validation errors, {detail: [{msg}, ...]}
function errorText(data, res) {
const d = data?.detail ?? data?.error;
if (Array.isArray(d)) return d.map(e => e.msg.replace(/^Value error, /, "")).join("; ");
return d || res.statusText || "Request failed";
}
function signInAgain() { location.href = `/login?next=${encodeURIComponent(location.pathname)}`; }
async function call(path, body, method) {
busyCount++; $("#busy").classList.add("on");
try {
const res = await fetch(path, body === undefined ? {} :
{ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
const data = await res.json();
if (!res.ok) throw new Error(data.error || res.statusText);
const res = await fetch(path, body === undefined && !method ? {} :
{ method: method || "POST", headers: { "Content-Type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
if (res.status === 401) { signInAgain(); throw new Error("Please sign in again"); }
const data = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(errorText(data, res));
return data;
} finally {
if (--busyCount === 0) $("#busy").classList.remove("on");
@@ -193,12 +257,12 @@ const guard = fn => async (...a) => { try { await fn(...a); } catch (e) { toast(
async function loadBooks(select) {
const books = await call("/api/books");
const sel = $("#book");
sel.innerHTML = books.length ? books.map(b => `<option value="${esc(b.name)}">${esc(b.name)}${b.open_elsewhere ? " (open in LibreOffice)" : ""}</option>`).join("")
sel.innerHTML = books.length ? books.map(b => `<option value="${esc(b.name)}">${esc(b.name)}</option>`).join("")
: `<option value="">No books yet</option>`;
const saved = select || localStorageGet("book");
state.book = books.find(b => b.name === saved) ? saved : books[0]?.name || null;
if (state.book) sel.value = state.book;
$("#openBtn").disabled = !state.book;
updateExportLinks();
await loadSummary();
}
function localStorageGet(k) { try { return localStorage.getItem(k); } catch { return null; } }
@@ -210,7 +274,10 @@ async function loadSummary() {
state.summary = await call(`/api/summary?book=${encodeURIComponent(state.book)}`);
const s = state.summary;
$("#bookInfo").textContent = `April ${s.year} – March ${s.year + 1}`;
if (state.view === "month" && state.sheet) await loadMonth(state.sheet); else render();
if (state.view === "month" && state.sheet) await loadMonth(state.sheet);
else if (state.view === "gst") await loadGst();
else if (state.view === "tax") await loadTax();
else render();
}
async function loadMonth(sheet) {
state.view = "month"; state.sheet = sheet;
@@ -254,8 +321,9 @@ async function uploadFiles(files) {
busyCount++; $("#busy").classList.add("on");
try {
const res = await fetch(`/api/upload?filename=${encodeURIComponent(f.name)}`, { method: "POST", body: f });
const data = await res.json();
if (res.ok) { added.push(f.name); state.selected.add(data.path); } else errors.push(data.error);
if (res.status === 401) return signInAgain();
const data = await res.json().catch(() => ({}));
if (res.ok) { added.push(f.name); state.selected.add(data.path); } else errors.push(errorText(data, res));
} finally { if (--busyCount === 0) $("#busy").classList.remove("on"); }
}
if (added.length) await loadStatements();
@@ -295,10 +363,15 @@ async function preview() {
state.preview = { statements, rows, title: plural(statements.length, "statement") };
state.view = "preview"; render();
}
async function doImport(force) {
const res = await call("/api/import", { book: state.book, statements: state.preview.statements, force });
function updateExportLinks() {
$("#exportMenu").hidden = !state.book;
for (const a of document.querySelectorAll("#exportMenu a"))
a.href = state.book ? `/api/export?book=${encodeURIComponent(state.book)}&format=${a.dataset.format}` : "#";
}
async function doImport() {
const res = await call("/api/import", { book: state.book, statements: state.preview.statements });
const added = res.reduce((n, r) => n + r.added, 0);
toast(`Added ${added} transaction${added === 1 ? "" : "s"} (backup saved as ${state.book}.bak)`);
toast(`Added ${added} transaction${added === 1 ? "" : "s"} (database backed up to cashbook.db.bak first)`);
state.preview = null; state.view = "summary";
await Promise.all([loadSummary(), loadStatements()]);
}
@@ -315,12 +388,16 @@ function render() {
if (!state.summary) { main.innerHTML = ""; return; }
const tabs = tabsHtml();
if (state.view === "preview" && state.preview) main.innerHTML = tabs + renderPreview();
else if (state.view === "gst" && state.gst) main.innerHTML = tabs + renderGstReturn();
else if (state.view === "tax" && state.tax) main.innerHTML = tabs + renderTax();
else if (state.view === "month" && state.month) main.innerHTML = tabs + renderMonth();
else main.innerHTML = tabs + renderSummary();
}
function tabsHtml() {
return `<div class="tabs">
<button data-view="summary" class="${state.view === "summary" ? "on" : ""}">Year</button>
<button data-view="gst" class="${state.view === "gst" ? "on" : ""}">GST return</button>
<button data-view="tax" class="${state.view === "tax" ? "on" : ""}">Income tax</button>
${state.summary.months.map(m => `<button data-sheet="${m.sheet}" class="${state.view === "month" && state.sheet === m.sheet ? "on" : ""} ${m.count ? "" : "empty"}">${m.label.split(" ")[0].slice(0, 3)}</button>`).join("")}
<span style="flex:1"></span>
<button data-view="statements" class="${state.view === "statements" ? "on" : ""}">Statements</button>
@@ -361,8 +438,8 @@ function renderSummary() {
const totalUnalloc = ms.reduce((n, m) => n + unalloc(m), 0);
const count = sum("count");
const catTotals = (list, key) => list.map(c => [c, ms.reduce((n, m) => n + (m[key][c] || 0), 0)]).filter(([, v]) => Math.abs(v) > 0.004);
const catTable = (title, rows, cls) => rows.length ? `<h2 style="margin-top:18px">${title}</h2><table><tbody>
${rows.map(([c, v]) => `<tr><td>${esc(c)}</td><td class="num ${cls}">${fmt(v)}</td></tr>`).join("")}</tbody></table>` : "";
const catTable = (title, rows, cls) => rows.length ? `<div><h2 style="margin-top:18px">${title}</h2><table><tbody>
${rows.map(([c, v]) => `<tr><td>${esc(c)}</td><td class="num ${cls}">${fmt(v)}</td></tr>`).join("")}</tbody></table></div>` : "";
return `
<div class="stats">
<div class="stat"><div class="k">Opening balance</div><div class="v">${fmt(s.opening)}</div></div>
@@ -384,37 +461,126 @@ function renderSummary() {
<tfoot><tr><td>Total</td><td class="num">${count}</td><td class="num in">${fmt(sum("income"))}</td><td class="num">${fmt(sum("gst_in"))}</td>
<td class="num out">${fmt(sum("expenses"))}</td><td class="num">${fmt(sum("gst_out"))}</td><td class="num">${fmt(totalUnalloc)}</td><td class="num">${fmt(s.closing)}</td></tr></tfoot>
</table></div>
${renderGst(s)}
<p class="small" style="margin-top:14px">GST return figures by filing period are on the <button class="linkish" data-view="gst">GST return</button> tab.</p>
<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(260px,1fr));gap:0 24px">
${catTable("Income by category (excl GST)", catTotals(s.income_categories, "income_cats"), "in")}
${catTable("Expenses by category (excl GST)", catTotals(s.expense_categories, "expense_cats"), "out")}
</div>`;
}
// Six-monthly GST returns for a 31 March balance date
function renderGst(s) {
const periods = [
{ label: `1 Apr ${s.year} – 30 Sep ${s.year}`, months: s.months.slice(0, 6), due: `28 Oct ${s.year}` },
{ label: `1 Oct ${s.year} – 31 Mar ${s.year + 1}`, months: s.months.slice(6), due: `7 May ${s.year + 1}` },
];
const total = (ms, k) => ms.reduce((n, m) => n + (m[k] || 0), 0);
const rows = periods.map(p => {
const net = total(p.months, "gst_in") - total(p.months, "gst_out");
const unalloc = p.months.reduce((n, m) => n + Math.abs(m.unallocated_in || 0) + Math.abs(m.unallocated_out || 0), 0);
return `<tr>
<td>${p.label}</td>
<td class="num">${fmt(total(p.months, "sales_incl"))}</td><td class="num">${fmt(total(p.months, "gst_in"))}</td>
<td class="num">${fmt(total(p.months, "purchases_incl"))}</td><td class="num">${fmt(total(p.months, "gst_out"))}</td>
<td class="num">${Math.abs(net) < 0.005 ? "–" : `<strong class="${net > 0 ? "out" : "in"}">${fmt(Math.abs(net))}</strong> <span class="muted small">${net > 0 ? "to pay" : "refund"}</span>`}</td>
<td class="muted">${p.due}</td>
<td>${p.months.some(m => m.count) ? (unalloc > 0.004 ? `<span class="chip wrong" title="Check the GST setting on unallocated rows before filing">${fmt(unalloc)} unallocated</span>` : `<span class="chip new">all allocated</span>`) : ""}</td></tr>`;
// ---- GST return (IRD GST101A boxes) ----
const FREQ_LABELS = { six: "Six-monthly", two: "Two-monthly", one: "Monthly" };
const BOXES = [
["5", "Total sales and income for the period (including GST and any zero-rated supplies)"],
["6", "Zero-rated supplies included in Box 5"],
["7", "Subtract Box 6 from Box 5"],
["8", "Multiply Box 7 by three and divide by twenty-three"],
["9", "Adjustments from your calculation sheet"],
["10", "Total GST collected on sales and income (Box 8 + Box 9)"],
["11", "Total purchases and expenses (including GST) for which tax invoicing requirements have been met"],
["12", "Multiply Box 11 by three and divide by twenty-three"],
["13", "Credit adjustments from your calculation sheet"],
["14", "Total GST credit for purchases and expenses (Box 12 + Box 13)"],
["15", "Difference between Box 10 and Box 14"],
];
async function loadGst() {
state.gstFreq = state.gstFreq || localStorageGet("gstFreq") || "six";
state.gst = await call(`/api/gst?book=${encodeURIComponent(state.book)}&frequency=${state.gstFreq}`);
state.view = "gst"; render();
}
const fmtLong = iso => new Date(iso + "T00:00:00").toLocaleDateString("en-NZ", { day: "numeric", month: "long", year: "numeric" });
function renderGstReturn() {
const g = state.gst;
const today = new Date().toISOString().slice(0, 10);
const card = p => {
const b = p.boxes, net = b["15"];
const result = !p.count ? "" : Math.abs(net) < 0.005 ? "Nothing to pay" : net > 0 ? `${fmt(net)} to pay` : `${fmt(-net)} refund`;
return `<div class="gst-card">
<div class="gst-head">
<div><strong>${fmtLong(p.start)} – ${fmtLong(p.end)}</strong>
<div class="muted small">Due ${fmtLong(p.due)}${p.due < today && p.count ? " · <span style='color:var(--warn)'>past due</span>" : ""} · ${plural(p.count, "transaction")}</div></div>
<div class="gst-result ${net > 0.004 ? "out" : "in"}">${result}</div>
</div>
${p.count ? `<table class="gst-boxes"><tbody>${BOXES.map(([n, label]) => `<tr class="${["10", "14", "15"].includes(n) ? "total" : ""}">
<td class="box">${n}</td><td>${label}</td>
<td class="num"><button class="copy" data-copy="${Math.abs(b[n]).toFixed(2)}" title="Copy">${fmt(n === "15" ? Math.abs(b[n]) : b[n])}</button></td></tr>`).join("")}
</tbody></table>
<div class="muted small gst-notes">
${p.unallocated ? `<div>⚠ ${plural(p.unallocated, "transaction")} not allocated to a category yet. Check their GST setting before filing.</div>` : ""}
${p.excluded_income ? `<div>${fmt(p.excluded_income)} of money in is marked Excl GST and left out of Box 5 (e.g. transfers, interest, refunds). If any of it is a sale, mark it Incl GST, or Zero-rated for exports.</div>` : ""}
${p.excluded_expenses ? `<div>${fmt(p.excluded_expenses)} of money out has no GST to claim (Excl GST or zero-rated) and is left out of Box 11.</div>` : ""}
</div>` : `<div class="muted small">No transactions in this period yet.</div>`}
</div>`;
};
return `
<div class="row-toolbar">
<strong style="font-size:16px">GST return</strong>
<label class="small">Filing frequency
<select id="gstFreq">${Object.entries(FREQ_LABELS).map(([k, v]) => `<option value="${k}" ${k === g.frequency ? "selected" : ""}>${v}</option>`).join("")}</select></label>
</div>
<p class="muted small" style="margin-top:0">The figures to enter in each box of your GST return (GST101A) in myIR, worked out on a payments basis from this book.
Boxes 9 and 13 (adjustments) aren't tracked here; add any from your calculation sheet. Click a figure to copy it.</p>
${g.periods.map(card).join("")}`;
}
// ---- income tax: IR10 and IR4 ----
async function loadTax() {
state.tax = await call(`/api/tax?book=${encodeURIComponent(state.book)}`);
state.view = "tax"; render();
}
const moneyOrDash = v => v == null ? "" : fmt(v);
function taxRows(form, rows, editable) {
return rows.map(r => {
if (r.kind === "heading") return `<tr class="tax-head"><td colspan="${editable ? 5 : 4}">${esc(r.label)}</td></tr>`;
const input = r.kind === "in"
? `<input type="number" step="0.01" class="tax-input" data-tax-form="${form}" data-tax-box="${r.box}"
value="${(form === "IR10" ? r.adjustment : r.value) || ""}" placeholder="0.00" aria-label="${form} box ${r.box}">` : "";
return `<tr class="${r.kind === "total" ? "total" : ""}">
<td class="box">${r.box}</td><td>${esc(r.label)}</td>
${editable ? `<td class="num muted">${moneyOrDash(r.cashbook)}</td>` : ""}
<td class="num">${input}</td>
<td class="num"><button class="copy" data-copy="${Math.abs(r.value || 0).toFixed(2)}" data-v="${form}-${r.box}" title="Copy">${fmt(r.value || 0)}</button></td></tr>`;
}).join("");
return `<h2 style="margin-top:18px">GST – six-monthly</h2>
<div class="scroll"><table>
<thead><tr><th>Period</th><th class="num">Sales incl GST</th><th class="num">GST on sales</th>
<th class="num">Purchases incl GST</th><th class="num">GST on purchases</th><th class="num">GST to pay / refund</th><th>Due</th><th></th></tr></thead>
<tbody>${rows}</tbody></table></div>
<div class="muted small" style="margin-top:6px">Only rows marked “Incl GST” count. Zero-rated sales (e.g. exports) are not included in sales.</div>`;
}
function renderTax() {
const { ir10, ir4 } = state.tax, res = ir4.result;
const resultText = res.kind === "nil" ? "Nothing to pay" : `${fmt(res.amount)} ${res.kind === "refund" ? "refund" : "to pay"}`;
return `
<div class="row-toolbar"><strong style="font-size:16px">Income tax: year ended 31 March ${ir4.year}</strong></div>
<p class="muted small" style="margin-top:0">Company income tax return (IR4) and financial statements summary (IR10),
filled in from this book where it can. Type in anything the cashbook can't know, such as stock, depreciation,
debtors and fixed assets. Each amount is saved as you leave the box. Click a figure to copy it.</p>
<div class="gst-card">
<div class="gst-head"><div><strong>IR10 Financial statements summary</strong>
<div class="muted small">For IR10 boxes the cashbook fills, what you type is added to its figure.</div></div></div>
<div class="scroll"><table class="gst-boxes tax-table">
<thead><tr><th>Box</th><th></th><th class="num">From cashbook</th><th class="num">You add / enter</th><th class="num">Amount</th></tr></thead>
<tbody>${taxRows("IR10", ir10.rows, true)}</tbody></table></div>
<div class="muted small gst-notes" id="ir10Notes">${ir10.notes.map(n => `<div>${esc(n)}</div>`).join("")}</div>
</div>
<div class="gst-card">
<div class="gst-head"><div><strong>IR4 Company income tax return</strong>
<div class="muted small">Business profit comes from IR10 box 29.</div></div>
<div class="gst-result ${res.kind === "to pay" ? "out" : "in"}" id="ir4Result">${resultText}</div></div>
<div class="scroll"><table class="gst-boxes tax-table">
<thead><tr><th>Box</th><th></th><th class="num">You enter</th><th class="num">Amount</th></tr></thead>
<tbody>${taxRows("IR4", ir4.rows, false)}</tbody></table></div>
<div class="muted small gst-notes">${ir4.notes.map(n => `<div>${esc(n)}</div>`).join("")}</div>
</div>`;
}
async function saveTaxInput(input) {
const value = input.value === "" ? null : Number(input.value);
state.tax = await call("/api/tax", { book: state.book, form: input.dataset.taxForm, box: input.dataset.taxBox, value });
// update figures in place so focus stays where the user tabbed to
for (const [form, data] of [["IR10", state.tax.ir10], ["IR4", state.tax.ir4]])
for (const r of data.rows) {
const cell = document.querySelector(`[data-v="${form}-${r.box}"]`);
if (cell) { cell.textContent = fmt(r.value || 0); cell.dataset.copy = Math.abs(r.value || 0).toFixed(2); }
}
const res = state.tax.ir4.result, out = $("#ir4Result");
out.textContent = res.kind === "nil" ? "Nothing to pay" : `${fmt(res.amount)} ${res.kind === "refund" ? "refund" : "to pay"}`;
out.className = `gst-result ${res.kind === "to pay" ? "out" : "in"}`;
$("#ir10Notes").innerHTML = state.tax.ir10.notes.map(n => `<div>${esc(n)}</div>`).join("");
}
function renderMonth() {
@@ -444,7 +610,7 @@ function renderMonth() {
<td>${fmtDate(r.date)}</td><td class="ref">${esc(r.reference)}</td><td class="muted">${esc(r.origin)}</td>
<td class="num in">${money(r.income)}</td><td class="num out">${money(r.expense)}</td>
<td><select data-row="${r.row}" data-field="gst">
${["Incl GST", "Excl GST"].map(g => `<option ${g === r.gst ? "selected" : ""}>${g}</option>`).join("")}</select></td>
${["Incl GST", "Zero-rated", "Excl GST"].map(g => `<option ${g === r.gst ? "selected" : ""}>${g}</option>`).join("")}</select></td>
<td class="num muted">${money(r.gst_in || r.gst_out)}</td>
<td>${catSelect(r)}</td></tr>`).join("") || `<tr><td colspan="8" class="muted">Everything this month is allocated.</td></tr>`}</tbody>
<tfoot><tr><td colspan="3">${m.rows.length} transactions</td><td class="num in">${fmt(income)}</td><td class="num out">${fmt(expense)}</td><td colspan="3"></td></tr></tfoot>
@@ -459,7 +625,7 @@ function renderPreview() {
}
const n = st => p.filter(r => r.status === st).length;
const wrong = n("wrong year"), fresh = n("new"), dup = n("duplicate");
const chip = st => `<span class="chip ${st === "wrong year" ? "wrong" : st}">${st === "duplicate" ? "in book" : st}</span>`;
const chip = st => `<span class="chip ${st === "wrong year" ? "wrong" : st}">${st === "duplicate" ? "in book" : st === "wrong year" ? "other year" : st}</span>`;
const info = pv.info;
return `
<h2>${esc(pv.title)}${info?.account ? ` <span class="muted small">· ${esc(info.account)}</span>` : ""}</h2>
@@ -469,10 +635,9 @@ function renderPreview() {
<div class="stat"><div class="k">Already in book</div><div class="v">${dup}</div></div>
<div class="stat"><div class="k">Outside this book's year</div><div class="v ${wrong ? "unalloc" : ""}">${wrong}</div></div>
</div>
${wrong ? `<p class="small" style="color:var(--warn)">Some transactions fall outside April ${state.summary.year} – March ${state.summary.year + 1}. They'd go into the sheet for their month regardless of year; pick another book or create one for that year.</p>` : ""}
${wrong ? `<p class="small" style="color:var(--warn)">${plural(wrong, "transaction")} fall${wrong === 1 ? "s" : ""} outside April ${state.summary.year} – March ${state.summary.year + 1} and will be skipped. Import ${wrong === 1 ? "it" : "them"} into the book for that year.</p>` : ""}
<div class="actions" style="margin:0 0 12px">
<button class="primary" id="importBtn" ${fresh || wrong ? "" : "disabled"}>Import ${fresh} new transaction${fresh === 1 ? "" : "s"}${wrong ? " (+" + wrong + " other year)" : ""}</button>
${wrong ? `<label class="small"><input type="checkbox" id="force"> import anyway</label>` : ""}
<button class="primary" id="importBtn" ${fresh ? "" : "disabled"}>Import ${plural(fresh, "new transaction")}</button>
<button id="cancelPreview">${pv.single ? "Back to statements" : "Cancel"}</button>
</div>
<div class="scroll"><table>
@@ -496,7 +661,7 @@ function openNewBook() {
}
function fillNewBookDefaults() {
const year = Number($("#nbYear").value);
$("#nbName").value = `cashbook-${year}-${String(year + 1).slice(2)}.ods`;
$("#nbName").value = `cashbook-${year}-${String(year + 1).slice(2)}`;
// a PDF statement starting at the end of March gives the 1 April opening balance
const period = state.statements.flatMap(s => s.periods || []).find(p => p.start === `${year}-03-31` && p.opening != null);
$("#nbBalance").value = period ? period.opening.toFixed(2) : "";
@@ -505,7 +670,7 @@ function fillNewBookDefaults() {
// ---- events ----
$("#book").addEventListener("change", guard(async e => {
state.book = e.target.value; localStorageSet("book", state.book);
state.book = e.target.value; localStorageSet("book", state.book); updateExportLinks();
state.preview = null; if (state.view !== "statements") state.view = "summary";
await Promise.all([loadSummary(), loadStatements()]);
}));
@@ -517,9 +682,11 @@ $("#newBook").addEventListener("close", guard(async () => {
toast(`Created ${res.name}`); localStorageSet("book", res.name);
state.view = "summary"; await loadBooks(res.name); await loadStatements();
}));
$("#openBtn").addEventListener("click", guard(async () => {
await call("/api/open", { book: state.book }); toast("Opening in LibreOffice… close it there before editing here.");
}));
// close the export menu after choosing, or when clicking elsewhere
document.addEventListener("click", e => {
const menu = $("#exportMenu");
if (menu.open && (!menu.contains(e.target) || e.target.closest("a"))) menu.open = false;
});
$("#statements").addEventListener("change", e => {
const p = e.target.dataset.path; if (!p) return;
e.target.checked ? state.selected.add(p) : state.selected.delete(p); updatePreviewBtn();
@@ -544,6 +711,8 @@ window.addEventListener("drop", guard(async e => {
await uploadFiles([...e.dataTransfer.files]);
}));
$("#main").addEventListener("click", guard(async e => {
const copy = e.target.closest("[data-copy]");
if (copy) { await navigator.clipboard?.writeText(copy.dataset.copy).catch(() => {}); return toast(`Copied ${copy.dataset.copy}`); }
const b = e.target.closest("[data-sheet],[data-view],[data-act],#importBtn,#cancelPreview,#chooseFiles,#previewBtn2");
if (!b || b.disabled) return;
if (b.id === "chooseFiles") return $("#uploadInput").click();
@@ -551,13 +720,17 @@ $("#main").addEventListener("click", guard(async e => {
if (b.dataset.act === "view") return viewStatement(b.dataset.path);
if (b.dataset.act === "rename") return renameStatement(b.dataset.path);
if (b.dataset.act === "delete") return deleteStatement(b.dataset.path);
if (b.id === "importBtn") return doImport(!!$("#force")?.checked);
if (b.id === "importBtn") return doImport();
if (b.id === "cancelPreview") { const back = state.preview?.single ? "statements" : "summary"; state.preview = null; state.view = back; return render(); }
if (b.dataset.view === "gst") return loadGst();
if (b.dataset.view === "tax") return loadTax();
if (b.dataset.view) { state.view = b.dataset.view; return render(); }
if (b.dataset.sheet) return loadMonth(b.dataset.sheet);
}));
$("#main").addEventListener("change", guard(async e => {
if (e.target.id === "onlyUnalloc") { state.onlyUnalloc = e.target.checked; return render(); }
if (e.target.dataset.taxBox) return saveTaxInput(e.target);
if (e.target.id === "gstFreq") { state.gstFreq = e.target.value; localStorageSet("gstFreq", state.gstFreq); return loadGst(); }
if (e.target.dataset.select) {
e.target.checked ? state.selected.add(e.target.dataset.select) : state.selected.delete(e.target.dataset.select);
renderStatements(); return render();
@@ -568,9 +741,41 @@ $("#main").addEventListener("change", guard(async e => {
state.summary = await call(`/api/summary?book=${encodeURIComponent(state.book)}`);
}));
// ---- account ----
async function loadMe() {
const me = await call("/api/auth/me");
state.user = me.user;
if (!me.auth_enabled || !me.user) return;
$("#userMenu").hidden = false;
$("#userName").textContent = me.user.username;
$("#usersLink").hidden = !me.user.is_superuser;
}
$("#logoutLink").addEventListener("click", guard(async e => {
e.preventDefault();
await call("/api/auth/logout", {});
location.href = "/login";
}));
$("#passwordLink").addEventListener("click", e => {
e.preventDefault(); $("#userMenu").open = false;
$("#passwordForm").reset(); $("#pwError").textContent = "";
$("#passwordDialog").showModal();
});
$("#pwSubmit").addEventListener("click", guard(async e => {
e.preventDefault();
if (!$("#passwordForm").reportValidity()) return;
if ($("#pwNew").value !== $("#pwConfirm").value) { $("#pwError").textContent = "The new passwords don't match"; return; }
try {
const res = await call("/api/auth/password", { current_password: $("#pwCurrent").value, new_password: $("#pwNew").value });
$("#passwordDialog").close(); toast(res.message);
} catch (err) { $("#pwError").textContent = err.message; }
}));
document.addEventListener("click", e => {
const menu = $("#userMenu");
if (menu.open && (!menu.contains(e.target) || e.target.closest("a"))) menu.open = false;
});
guard(async () => {
const config = await call("/api/config");
if (!config.can_open) $("#openBtn").hidden = true;
await loadMe();
await loadBooks();
await loadStatements();
})();
+65
View File
@@ -0,0 +1,65 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sign in · Cashbook</title>
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='7' fill='%230b6bcb'/%3E%3Ctext x='16' y='23' font-size='19' font-family='sans-serif' font-weight='700' text-anchor='middle' fill='white'%3E$%3C/text%3E%3C/svg%3E">
<link rel="stylesheet" href="/static/auth.css">
</head>
<body>
<div class="center">
<main class="card">
<h1>Cashbook</h1>
<div class="muted" style="margin-bottom:12px">Sign in to continue.</div>
<div id="setup" class="notice" hidden>
There are no accounts yet. <a href="/register">Create the administrator account</a> to get started.
</div>
<form id="form">
<label for="username">Username</label>
<input id="username" type="text" autocomplete="username" autocapitalize="none" required autofocus>
<label for="password">Password</label>
<input id="password" type="password" autocomplete="current-password" required>
<div id="err" class="error" role="alert"></div>
<button type="submit" class="primary" id="submit">Sign in</button>
</form>
<div class="links muted" id="registerLinks" hidden>Have an invite? Open the link you were sent.</div>
</main>
</div>
<script>
const next = new URLSearchParams(location.search).get("next");
const target = next && next.startsWith("/") && !next.startsWith("//") ? next : "/";
const errorText = j => Array.isArray(j.detail) ? j.detail.map(e => e.msg).join("; ") : (j.detail || "Sign-in failed");
(async () => {
const s = await fetch("/api/auth/status").then(r => r.json()).catch(() => null);
if (!s) return;
if (!s.auth_enabled) { location.href = "/"; return; }
if (s.setup_required) { document.getElementById("setup").hidden = false; document.getElementById("form").hidden = true; }
else document.getElementById("registerLinks").hidden = !s.register_open;
// already signed in?
const me = await fetch("/api/auth/me");
if (me.ok) location.href = target;
})();
document.getElementById("form").addEventListener("submit", async e => {
e.preventDefault();
const err = document.getElementById("err"), btn = document.getElementById("submit");
err.textContent = ""; btn.disabled = true;
try {
const res = await fetch("/api/auth/login", {
method: "POST", headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: document.getElementById("username").value.trim(),
password: document.getElementById("password").value }),
});
if (!res.ok) { err.textContent = errorText(await res.json().catch(() => ({}))); return; }
location.href = target;
} catch (ex) {
err.textContent = ex.message;
} finally {
btn.disabled = false;
}
});
</script>
</body>
</html>
+69
View File
@@ -0,0 +1,69 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Create account · Cashbook</title>
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='7' fill='%230b6bcb'/%3E%3Ctext x='16' y='23' font-size='19' font-family='sans-serif' font-weight='700' text-anchor='middle' fill='white'%3E$%3C/text%3E%3C/svg%3E">
<link rel="stylesheet" href="/static/auth.css">
</head>
<body>
<div class="center">
<main class="card">
<h1 id="title">Create account</h1>
<div id="intro" class="muted" style="margin-bottom:12px"></div>
<div id="closed" class="notice" hidden>You need an invite link to create an account. Ask an administrator.</div>
<form id="form" hidden>
<label for="username">Username</label>
<input id="username" type="text" autocomplete="username" autocapitalize="none" minlength="3" maxlength="64" required autofocus>
<label for="password">Password (at least 8 characters)</label>
<input id="password" type="password" autocomplete="new-password" minlength="8" required>
<label for="confirm">Password again</label>
<input id="confirm" type="password" autocomplete="new-password" minlength="8" required>
<div id="err" class="error" role="alert"></div>
<button type="submit" class="primary" id="submit">Create account</button>
</form>
<div class="links"><a href="/login">Back to sign in</a></div>
</main>
</div>
<script>
const invite = new URLSearchParams(location.search).get("invite");
const errorText = j => Array.isArray(j.detail) ? j.detail.map(e => e.msg.replace(/^Value error, /, "")).join("; ") : (j.detail || "Couldn't create the account");
const $ = id => document.getElementById(id);
(async () => {
const s = await fetch("/api/auth/status").then(r => r.json()).catch(() => null);
if (!s) return;
if (!s.auth_enabled) { location.href = "/"; return; }
if (s.setup_required) {
$("title").textContent = "Set up the Cashbook";
$("intro").textContent = "Create the administrator account. You can invite other people afterwards.";
} else if (invite) {
$("intro").textContent = "You've been invited. Choose a username and password.";
} else if (!s.register_open || s.invite_required) {
$("closed").hidden = false; return;
}
$("form").hidden = false;
})();
$("form").addEventListener("submit", async e => {
e.preventDefault();
$("err").textContent = "";
if ($("password").value !== $("confirm").value) { $("err").textContent = "The passwords don't match"; return; }
$("submit").disabled = true;
try {
const res = await fetch("/api/auth/register", {
method: "POST", headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: $("username").value.trim(), password: $("password").value, invite_token: invite }),
});
if (!res.ok) { $("err").textContent = errorText(await res.json().catch(() => ({}))); return; }
location.href = "/";
} catch (ex) {
$("err").textContent = ex.message;
} finally {
$("submit").disabled = false;
}
});
</script>
</body>
</html>
+171
View File
@@ -0,0 +1,171 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Users · Cashbook</title>
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='7' fill='%230b6bcb'/%3E%3Ctext x='16' y='23' font-size='19' font-family='sans-serif' font-weight='700' text-anchor='middle' fill='white'%3E$%3C/text%3E%3C/svg%3E">
<link rel="stylesheet" href="/static/auth.css">
</head>
<body>
<header>
<h1>Users</h1>
<a href="/">← Back to the cashbook</a>
<span class="spacer"></span>
<span id="me" class="muted small"></span>
</header>
<main>
<div id="denied" class="panel" hidden>Only administrators can manage users. <a href="/">Back to the cashbook</a></div>
<section class="panel" id="usersPanel" hidden>
<h2>Accounts</h2>
<div class="scroll"><table>
<thead><tr><th>Username</th><th>Role</th><th>Created</th><th>Last sign-in</th><th></th></tr></thead>
<tbody id="users"></tbody>
</table></div>
<div class="muted small" style="margin-top:8px">Administrators can manage users. Everyone signed in can see and change the books.
Resetting a password signs that person out everywhere.</div>
</section>
<section class="panel" id="invitePanel" hidden>
<h2>Invite someone</h2>
<form id="inviteForm" class="row">
<div><label for="inviteEmail">Email (optional: sends the link if email is set up)</label>
<input id="inviteEmail" type="email" placeholder="name@example.com"></div>
<div class="shrink"><label for="inviteDays">Expires after</label>
<select id="inviteDays"><option value="1">1 day</option><option value="7" selected>7 days</option><option value="30">30 days</option></select></div>
<div class="shrink"><label class="inline"><input type="checkbox" id="inviteAdmin"> Administrator</label></div>
<div class="shrink"><button class="primary" type="submit">Create invite link</button></div>
</form>
<div id="inviteResult" hidden>
<div class="invite-link"><input id="inviteUrl" type="text" readonly><button id="copyInvite" type="button">Copy</button></div>
<div id="inviteNote" class="muted small" style="margin-top:6px"></div>
</div>
<h2 style="margin-top:18px">Pending invites</h2>
<div class="scroll"><table>
<thead><tr><th>Email</th><th>Role</th><th>Expires</th><th></th></tr></thead>
<tbody id="invites"></tbody>
</table></div>
</section>
</main>
<dialog id="editDialog">
<form id="editForm">
<h2 style="margin:0 0 6px;font-size:16px" id="editTitle"></h2>
<div id="editFields"></div>
<div id="editErr" class="error"></div>
<div class="buttons"><button type="button" id="editCancel">Cancel</button><button class="primary" type="submit">Save</button></div>
</form>
</dialog>
<div id="toast"></div>
<script>
const $ = id => document.getElementById(id);
const esc = s => String(s ?? "").replace(/[&<>"']/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]));
const when = iso => iso ? new Date(iso + "Z").toLocaleString("en-NZ", { dateStyle: "medium", timeStyle: "short" }) : "—";
let me = null;
function toast(msg, error) {
const t = $("toast"); t.textContent = msg; t.className = "show" + (error ? " error" : "");
clearTimeout(toast.timer); toast.timer = setTimeout(() => t.className = "", error ? 6000 : 3000);
}
async function api(path, method = "GET", body) {
const res = await fetch(path, { method, headers: body ? { "Content-Type": "application/json" } : {}, body: body ? JSON.stringify(body) : undefined });
if (res.status === 401) { location.href = "/login?next=/users"; throw new Error("Please sign in again"); }
const data = await res.json().catch(() => ({}));
if (!res.ok) {
const d = data.detail;
throw new Error(Array.isArray(d) ? d.map(e => e.msg.replace(/^Value error, /, "")).join("; ") : (d || res.statusText));
}
return data;
}
async function load() {
const [users, invites] = await Promise.all([api("/api/users"), api("/api/users/invites")]);
$("users").innerHTML = users.map(u => `<tr>
<td>${esc(u.username)} ${u.id === me.id ? '<span class="chip you">you</span>' : ""}</td>
<td>${u.is_superuser ? '<span class="chip admin">Administrator</span>' : '<span class="chip">User</span>'}</td>
<td class="muted">${when(u.created_at)}</td><td class="muted">${when(u.last_login_at)}</td>
<td class="actions">
<button data-act="rename" data-id="${u.id}" data-name="${esc(u.username)}">Rename</button>
<button data-act="password" data-id="${u.id}" data-name="${esc(u.username)}">Reset password</button>
${u.id === me.id ? "" : `<button data-act="role" data-id="${u.id}" data-admin="${u.is_superuser}">${u.is_superuser ? "Remove admin" : "Make admin"}</button>
<button class="danger" data-act="delete" data-id="${u.id}" data-name="${esc(u.username)}">Delete</button>`}
</td></tr>`).join("");
$("invites").innerHTML = invites.length ? invites.map(i => `<tr>
<td>${esc(i.email) || '<span class="muted">link only</span>'}</td>
<td>${i.grants_superuser ? '<span class="chip admin">Administrator</span>' : '<span class="chip">User</span>'}</td>
<td class="muted">${when(i.expires_at)}</td>
<td class="actions"><button class="danger" data-act="revoke" data-id="${i.id}">Revoke</button></td></tr>`).join("")
: `<tr><td colspan="4" class="muted">No pending invites.</td></tr>`;
}
let editSubmit = null;
function edit(title, fieldsHtml, onSubmit) {
$("editTitle").textContent = title; $("editFields").innerHTML = fieldsHtml; $("editErr").textContent = "";
editSubmit = onSubmit; $("editDialog").showModal();
$("editFields").querySelector("input")?.focus();
}
$("editCancel").addEventListener("click", () => $("editDialog").close());
$("editForm").addEventListener("submit", async e => {
e.preventDefault();
try { await editSubmit(); $("editDialog").close(); await load(); }
catch (err) { $("editErr").textContent = err.message; }
});
$("users").addEventListener("click", async e => {
const b = e.target.closest("button[data-act]"); if (!b) return;
const id = Number(b.dataset.id);
try {
if (b.dataset.act === "rename") {
edit(`Rename ${b.dataset.name}`, `<label for="f1">New username</label><input id="f1" type="text" value="${esc(b.dataset.name)}" minlength="3" maxlength="64" required>`,
async () => { await api(`/api/users/${id}`, "PATCH", { username: $("f1").value }); toast("Renamed"); });
} else if (b.dataset.act === "password") {
edit(`Reset password for ${b.dataset.name}`, `<label for="f1">New password (at least 8 characters)</label><input id="f1" type="password" autocomplete="new-password" minlength="8" required>
<div class="muted small" style="margin-top:6px">They'll be signed out everywhere and need the new password.</div>`,
async () => { await api(`/api/users/${id}`, "PATCH", { password: $("f1").value }); toast(id === me.id ? "Password changed" : "Password reset"); if (id === me.id) location.href = "/login?next=/users"; });
} else if (b.dataset.act === "role") {
const makeAdmin = b.dataset.admin !== "true";
await api(`/api/users/${id}`, "PATCH", { is_superuser: makeAdmin });
toast(makeAdmin ? "Now an administrator" : "No longer an administrator"); await load();
} else if (b.dataset.act === "delete") {
if (!confirm(`Delete ${b.dataset.name}? They won't be able to sign in. The books aren't affected.`)) return;
await api(`/api/users/${id}`, "DELETE"); toast("User deleted"); await load();
}
} catch (err) { toast(err.message, true); }
});
$("invites").addEventListener("click", async e => {
const b = e.target.closest("button[data-act=revoke]"); if (!b) return;
try { await api(`/api/users/invites/${b.dataset.id}`, "DELETE"); toast("Invite revoked"); await load(); }
catch (err) { toast(err.message, true); }
});
$("inviteForm").addEventListener("submit", async e => {
e.preventDefault();
try {
const res = await api("/api/users/invites", "POST", {
email: $("inviteEmail").value || null, expires_days: Number($("inviteDays").value), grants_superuser: $("inviteAdmin").checked });
$("inviteResult").hidden = false; $("inviteUrl").value = res.invite_url;
$("inviteNote").textContent = res.delivered ? `Emailed to ${res.email}.` : "Send this link to the person you're inviting. It works once.";
$("inviteForm").reset(); await load();
} catch (err) { toast(err.message, true); }
});
$("copyInvite").addEventListener("click", async () => {
$("inviteUrl").select();
try { await navigator.clipboard.writeText($("inviteUrl").value); toast("Copied"); } catch { document.execCommand("copy"); toast("Copied"); }
});
(async () => {
try {
const res = await api("/api/auth/me");
if (!res.auth_enabled) { $("denied").hidden = false; $("denied").textContent = "Accounts are turned off (AUTH_ENABLED=false)."; return; }
me = res.user; $("me").textContent = `Signed in as ${me.username}`;
if (!me.is_superuser) { $("denied").hidden = false; return; }
$("usersPanel").hidden = false; $("invitePanel").hidden = false;
await load();
} catch (err) { toast(err.message, true); }
})();
</script>
</body>
</html>
+262
View File
@@ -0,0 +1,262 @@
"""Company income tax figures: IR10 (financial statements summary) and IR4 (company return).
Box numbers follow IRD's IR10 (March 2026) and IR4 (2026) forms. Figures come from the
cashbook where it can know them (allocated categories, excluding GST) plus amounts typed
in for the rest (stock, depreciation, debtors, RWT, provisional tax...), which are kept
per book in the `tax_inputs` table.
"""
import ledger
SCHEMA = """
CREATE TABLE IF NOT EXISTS tax_inputs (
book_id INTEGER NOT NULL REFERENCES books(id) ON DELETE CASCADE,
form TEXT NOT NULL CHECK (form IN ('IR10', 'IR4')),
box TEXT NOT NULL,
cents INTEGER NOT NULL,
PRIMARY KEY (book_id, form, box)
);
"""
COMPANY_TAX_RATE = 0.28
# Cashbook categories -> IR10 boxes. Categories mapped to None aren't income or expenses.
INCOME_BOXES = {"Sales": "2", "Grants": "10", "Interest": "7", "Sundry": "10", "Funds Intr.": None}
EXPENSE_BOXES = {
"Purchases": "4", "Wages": "22", "Rent": "18", "Motor vehicle": "24", "Loan repmt": None,
"Drawings": None, "Bank fees": "24", "Telephone & Power": "24", "ACC & Insurance": "14",
"Postage & Stationery": "24", "Bank Fees": "24", "Sundry": "24",
}
# (box, label, kind) - kind: "in" entered/adjustable, "total" calculated, "heading"
IR10 = [
("", "Profit and loss statement", "heading"),
("2", "Gross income from sales and/or services", "in"),
("3", "Opening stock (include work in progress)", "in"),
("4", "Purchases", "in"),
("5", "Closing stock (include work in progress)", "in"),
("6", "Gross profit", "total"),
("7", "Interest received", "in"),
("8", "Dividends received", "in"),
("9", "Rental, lease and licence income", "in"),
("10", "Other income", "in"),
("11", "Total income (boxes 6 to 10)", "total"),
("12", "Bad debts", "in"),
("13", "Accounting depreciation and amortisation", "in"),
("14", "Insurance (exclude ACC levies)", "in"),
("15", "Interest expense", "in"),
("16", "Professional and consulting fees", "in"),
("17", "Rates", "in"),
("18", "Rental, lease and licence payments", "in"),
("19", "Repairs and maintenance", "in"),
("20", "Research and development", "in"),
("21", "Associated persons' remuneration", "in"),
("22", "Salaries and wages paid to employees", "in"),
("23", "Contractor and sub-contractor payments", "in"),
("24", "Other expenses", "in"),
("25", "Total expenses (boxes 12 to 24)", "total"),
("26", "Exceptional items", "in"),
("27", "Net profit/loss before tax", "total"),
("28", "Tax adjustments", "in"),
("29", "Current year taxable profit/loss", "total"),
("", "Balance sheet items (at balance date)", "heading"),
("30", "Accounts receivable (debtors)", "in"),
("31", "Cash and deposits", "in"),
("32", "Other current assets", "in"),
("33", "Vehicles", "in"),
("34", "Plant and machinery", "in"),
("35", "Furniture and fittings", "in"),
("36", "Land", "in"),
("37", "Buildings", "in"),
("38", "Other fixed assets", "in"),
("39", "Intangibles", "in"),
("40", "Shares/ownership interests", "in"),
("41", "Term deposits", "in"),
("42", "Other non-current assets", "in"),
("43", "Total assets (boxes 30 to 42)", "total"),
("44", "Provisions", "in"),
("45", "Accounts payable (creditors)", "in"),
("46", "Current loans", "in"),
("47", "Other current liabilities", "in"),
("48", "Total current liabilities (boxes 44 to 47)", "total"),
("49", "Non-current liabilities", "in"),
("50", "Total liabilities (boxes 48 and 49)", "total"),
("51", "Owners' equity (total assets less total liabilities)", "total"),
("", "Other information", "heading"),
("52", "Tax depreciation", "in"),
("53", "Untaxed realised gains/receipts", "in"),
("54", "Additions to fixed assets", "in"),
("55", "Disposals of fixed assets", "in"),
("56", "Dividends paid", "in"),
("57", "Drawings", "in"),
("58", "Current account year-end balances", "in"),
("59", "Tax-deductible loss on disposal of fixed assets", "in"),
("60", "Total value of Investment Boost asset(s) being claimed", "in"),
]
IR4 = [
("", "Income", "heading"),
("12A", "Schedular payments: total tax deducted", "in"),
("12B", "Schedular payments: total gross income", "in"),
("13A", "NZ interest: total RWT and/or NRWT", "in"),
("13B", "NZ interest: total gross interest (IR10 box 7 plus RWT)", "total"),
("14", "Dividends: total imputation credits", "in"),
("14A", "Dividends: total dividend RWT credits", "in"),
("14B", "Dividends: total gross dividends (IR10 box 8)", "total"),
("17A", "Total tax credits (12A, 13A, 14A)", "total"),
("18A", "Overseas income: total overseas tax paid", "in"),
("18B", "Overseas income: total overseas income", "in"),
("20B", "Net profit from business (IR10 box 29 less income at boxes 7, 8, 12B and 18B)", "total"),
("22B", "Other income", "in"),
("23", "Total income before donations", "total"),
("24B", "Donations to donee organisations", "in"),
("25", "Total income or net loss", "total"),
("26A", "Net losses brought forward", "in"),
("26B", "Net losses claimed this year", "in"),
("27", "Total income after net losses brought forward", "total"),
("28", "Net losses to/from other companies (minus if received)", "in"),
("28A", "Subvention payments (minus if made)", "in"),
("29", "Taxable income", "total"),
("", "Tax calculation", "heading"),
("30A", "Taxable income (whole dollars)", "total"),
("30B", "Tax at 28 cents in the dollar", "total"),
("30C", "Overseas tax paid (box 18A)", "total"),
("30D", "Box 30B less 30C", "total"),
("30E", "Foreign investor tax credit", "in"),
("30F", "Box 30D less 30E", "total"),
("30G", "Imputation credits (box 14)", "total"),
("30H", "Box 30F less 30G", "total"),
("30I", "Total tax credits (box 17A)", "total"),
("30J", "Residual income tax (minus = credit)", "total"),
("30K", "2026 provisional tax paid", "in"),
("30L", "Refund (minus) or tax to pay", "total"),
]
def ensure_schema(conn):
conn.executescript(SCHEMA)
def _inputs(conn, book, form):
ensure_schema(conn)
return {r["box"]: r["cents"] / 100 for r in conn.execute(
"SELECT box, cents FROM tax_inputs WHERE book_id = ? AND form = ?", (book["id"], form))}
def set_input(conn, book, form, box, value):
"""Save an entered amount (or an adjustment, for IR10 boxes the cashbook fills)."""
layout = {"IR10": IR10, "IR4": IR4}.get(form, [])
if {b: kind for b, _, kind in layout}.get(box) != "in":
raise ValueError(f"{form} box {box} can't be entered")
ensure_schema(conn)
with conn:
if value in (None, "", 0, 0.0):
conn.execute("DELETE FROM tax_inputs WHERE book_id = ? AND form = ? AND box = ?",
(book["id"], form, box))
else:
conn.execute("INSERT INTO tax_inputs (book_id, form, box, cents) VALUES (?, ?, ?, ?) "
"ON CONFLICT (book_id, form, box) DO UPDATE SET cents = excluded.cents",
(book["id"], form, box, ledger.cents(float(value))))
def _r(x):
return round(x + 0.0, 2)
def _from_cashbook(summary):
"""IR10 amounts the cashbook knows, by box, and what it couldn't place."""
vals, unalloc_in, unalloc_out, funds_in = {}, 0.0, 0.0, 0.0
for m in summary["months"]:
for cat, amount in m["income_cats"].items():
box = INCOME_BOXES.get(cat, "10")
if cat == "Funds Intr.":
funds_in += amount
elif box:
vals[box] = vals.get(box, 0) + amount
for cat, amount in m["expense_cats"].items():
box = EXPENSE_BOXES.get(cat, "24")
if box:
vals[box] = vals.get(box, 0) + amount
unalloc_in += m["unallocated_in"]
unalloc_out += m["unallocated_out"]
drawings = sum(m["expense_cats"].get("Drawings", 0) for m in summary["months"])
vals["31"] = summary["closing"] # bank balance at 31 March
vals["57"] = drawings
return {k: _r(v) for k, v in vals.items()}, _r(unalloc_in), _r(unalloc_out), _r(funds_in)
def ir10(conn, book):
summary = ledger.read_summary(conn, book)
cash, unalloc_in, unalloc_out, funds_in = _from_cashbook(summary)
adj = _inputs(conn, book, "IR10")
v = {b: _r(cash.get(b, 0) + adj.get(b, 0)) for b, _, kind in IR10 if kind == "in"}
s = lambda *boxes: _r(sum(v[b] for b in boxes))
v["6"] = _r(v["2"] - (v["3"] + v["4"] - v["5"]))
v["11"] = s("6", "7", "8", "9", "10")
v["25"] = s(*[str(n) for n in range(12, 25)])
v["27"] = _r(v["11"] - v["25"] + v["26"])
v["29"] = _r(v["27"] + v["28"])
v["43"] = s(*[str(n) for n in range(30, 43)])
v["48"] = s("44", "45", "46", "47")
v["50"] = _r(v["48"] + v["49"])
v["51"] = _r(v["43"] - v["50"])
notes = []
if unalloc_in or unalloc_out:
notes.append(f"{unalloc_in:,.2f} of money in and {unalloc_out:,.2f} of money out isn't allocated to a "
"category, so it's not in these figures. Allocate it in the month views first.")
notes.append("Amounts are from the cashbook (cash basis, excluding GST), plus your adjustments. "
"Interest from the bank arrives after RWT: add the RWT to box 7 so it's the gross amount.")
if funds_in:
notes.append(f"{funds_in:,.2f} of funds introduced and the drawings in box 57 aren't income or "
"expenses; they go through the shareholder current account (box 58).")
notes.append("ACC & Insurance is put in box 14: move ACC levies to other expenses (box 24) with adjustments.")
return {
"form": "IR10", "year_end": f"{book['start_year'] + 1}-03-31",
"rows": [{"box": b, "label": label, "kind": kind,
"cashbook": cash.get(b) if kind == "in" else None,
"adjustment": adj.get(b, 0) if kind == "in" else None,
"value": v.get(b)} for b, label, kind in IR10],
"values": v, "notes": notes,
}
def ir4(conn, book, ir10_values=None):
v10 = ir10_values or ir10(conn, book)["values"]
i = _inputs(conn, book, "IR4")
get = lambda b: i.get(b, 0.0)
v = {b: _r(get(b)) for b, _, kind in IR4 if kind == "in"}
v["13B"] = _r(v10["7"]) # IR10 box 7 should already be gross (add RWT there)
v["14B"] = _r(v10["8"])
v["17A"] = _r(v["12A"] + v["13A"] + v["14A"])
v["20B"] = _r(v10["29"] - v10["7"] - v10["8"] - v["12B"] - v["18B"])
v["23"] = _r(v["12B"] + v["13B"] + v["14B"] + v["18B"] + v["20B"] + v["22B"])
v["25"] = _r(v["23"] - v["24B"])
v["27"] = _r(v["25"] - v["26B"])
v["29"] = _r(v["27"] + v["28"] + v["28A"])
v["30A"] = float(max(0, int(v["29"]))) # whole dollars, no tax on a loss
v["30B"] = _r(v["30A"] * COMPANY_TAX_RATE)
v["30C"] = v["18A"]
v["30D"] = _r(max(0.0, v["30B"] - v["30C"]))
v["30F"] = _r(v["30D"] - v["30E"])
v["30G"] = v["14"]
v["30H"] = _r(max(0.0, v["30F"] - v["30G"]))
v["30I"] = v["17A"]
v["30J"] = _r(v["30H"] - v["30I"])
v["30L"] = _r(v["30J"] - v["30K"])
year = book["start_year"] + 1
result = v["30L"]
return {
"form": "IR4", "year": year,
"rows": [{"box": b, "label": label.replace("2026", str(year)), "kind": kind,
"value": v.get(b)} for b, label, kind in IR4],
"values": v,
"result": {"amount": abs(result), "kind": "refund" if result < 0 else "to pay" if result > 0 else "nil"},
"notes": [
f"Return for 1 April {year - 1} to 31 March {year}. Box numbers are from IRD's 2026 IR4; check them "
f"against the {year} form.",
"Assumes a New Zealand resident company with no residential property, Māori authority, partnership "
"or trust income and no taxable property sales (questions 15, 16, 19 and 21).",
f"Tax at {COMPANY_TAX_RATE:.0%} on taxable income in whole dollars. These figures are worked out "
"from your records and aren't tax advice.",
],
}
-457
View File
@@ -1,457 +0,0 @@
#!/usr/bin/env python3
"""Local web app for the cashbook: pick a book, import ANZ statements, review months.
Usage:
webapp.py [--host 127.0.0.1] [--port 8765] [--data DIR] [--statements DIR ...]
Books are the .ods files in --data (default: ../data); new ones are copied
from src/template.ods. Statements are .csv/.pdf files in DATA/statements
(where uploads go), DATA, ~/Downloads/anz and any --statements folders.
"""
import argparse
import base64
import hmac
import json
import os
import re
import shutil
import signal
import subprocess
import sys
import threading
import traceback
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import parse_qs, urlparse
import cashbook
from new_book import ROOT, create_book
from office import BookLocked, Office, lock_file
from statements import describe, read_statement, statement_info
HERE = os.path.dirname(os.path.abspath(__file__))
BOOKS = os.path.join(ROOT, "data")
UPLOADS = os.path.join(BOOKS, "statements")
STATEMENT_DIRS = [UPLOADS, BOOKS, os.path.expanduser("~/Downloads/anz")]
office = Office()
_info_cache = {}
class BadRequest(Exception):
pass
# --- books ------------------------------------------------------------------
def list_books():
books = []
for name in sorted(os.listdir(BOOKS)):
if name.endswith(".ods") and name != "template.ods":
path = os.path.join(BOOKS, name)
books.append({"name": name, "modified": os.path.getmtime(path),
"open_elsewhere": os.path.exists(lock_file(path))})
return books
def book_path(name):
if not name or os.path.basename(name) != name or not name.endswith(".ods") \
or name == "template.ods":
raise BadRequest("invalid book name")
path = os.path.join(BOOKS, name)
if not os.path.exists(path):
raise BadRequest(f"{name} not found")
return path
def backup(path):
shutil.copy2(path, path + ".bak")
# --- statements ---------------------------------------------------------------
MAX_UPLOAD = 20 * 1024 * 1024
def _parse(path):
"""Statement info and transactions, cached until the file changes."""
key = (path, os.path.getmtime(path))
if key not in _info_cache:
try:
_info_cache[key] = {"info": statement_info(path), "txns": read_statement(path)}
except Exception as e: # not an ANZ statement, or unreadable
_info_cache[key] = {"info": {"error": str(e).splitlines()[0][:200]}, "txns": []}
return _info_cache[key]
def managed(path):
"""Statements inside the data folder can be renamed and deleted."""
return os.path.commonpath([path, os.path.realpath(BOOKS)]) == os.path.realpath(BOOKS)
def folder_label(d):
data = os.path.realpath(BOOKS)
d = os.path.realpath(d)
if os.path.commonpath([d, data]) == data:
rel = os.path.relpath(d, data)
return "data" if rel == "." else f"data/{rel}"
return d.replace(os.path.expanduser("~"), "~")
def list_statements(book=None):
out, seen = [], set()
for d in STATEMENT_DIRS:
if not os.path.isdir(d):
continue
for name in sorted(os.listdir(d)):
path = os.path.realpath(os.path.join(d, name))
if path in seen or name.startswith(".") or not name.lower().endswith((".csv", ".pdf")) \
or not os.path.isfile(path):
continue
seen.add(path)
st = os.stat(path)
out.append({"path": path, "name": name, "folder": folder_label(d),
"managed": managed(path), "size": st.st_size, "modified": st.st_mtime,
"kind": name.rsplit(".", 1)[1].lower(), **_parse(path)["info"]})
# flag files that are byte-for-byte copies of one listed earlier
first = {}
for s in out:
if s.get("sha256"):
s["copy_of"] = first.get(s["sha256"])
first.setdefault(s["sha256"], s["name"])
if book:
with office.open(book_path(book), read_only=True) as doc:
for s in out:
txns = _parse(s["path"])["txns"]
s["imported"] = cashbook.count_imported(doc, txns) if txns else 0
return out
def statement_path(path, must_manage=False):
for s in list_statements():
if s["path"] == path:
if must_manage and not s["managed"]:
raise BadRequest(f"{s['name']} is in {s['folder']}, which is read-only here")
return path
raise BadRequest(f"unknown statement {path}")
def statement_paths(paths):
if not paths:
raise BadRequest("select at least one statement")
allowed = {s["path"] for s in list_statements()}
for p in paths:
if p not in allowed:
raise BadRequest(f"unknown statement {p}")
return paths
def safe_name(filename):
name = re.sub(r"[^\w.\-]+", "_", os.path.basename(filename)).strip("._")
if not name.lower().endswith((".csv", ".pdf")):
raise BadRequest("only .csv and .pdf statements")
return name
def unique_path(folder, name):
base, ext = os.path.splitext(name)
path, n = os.path.join(folder, name), 2
while os.path.exists(path):
path, n = os.path.join(folder, f"{base}-{n}{ext}"), n + 1
return path
def save_upload(filename, data):
name = safe_name(filename)
if len(data) > MAX_UPLOAD:
raise BadRequest("file is too large for a statement")
os.makedirs(UPLOADS, exist_ok=True)
tmp = os.path.join(UPLOADS, f".upload-{os.getpid()}-{threading.get_ident()}{os.path.splitext(name)[1]}")
with open(tmp, "wb") as f:
f.write(data)
try:
info = statement_info(tmp)
except Exception as e:
os.remove(tmp)
raise BadRequest(f"{filename} doesn't look like an ANZ statement ({str(e).splitlines()[0][:120]})")
if not info["count"]:
os.remove(tmp)
raise BadRequest(f"{filename} has no transactions")
for s in list_statements():
if s.get("sha256") == info["sha256"]:
os.remove(tmp)
raise BadRequest(f"{filename} is already here as {s['name']}")
path = unique_path(UPLOADS, name)
os.replace(tmp, path)
return path
def rename_statement(path, new_name):
path = statement_path(path, must_manage=True)
name = safe_name(new_name)
if os.path.splitext(name)[1].lower() != os.path.splitext(path)[1].lower():
raise BadRequest("keep the same file extension")
target = os.path.join(os.path.dirname(path), name)
if target != path and os.path.exists(target):
raise BadRequest(f"{name} already exists")
os.rename(path, target)
return target
def delete_statement(path):
"""Move a statement into statements/.trash rather than deleting it outright."""
path = statement_path(path, must_manage=True)
trash = os.path.join(UPLOADS, ".trash")
os.makedirs(trash, exist_ok=True)
target = unique_path(trash, os.path.basename(path))
shutil.move(path, target)
return target
# --- API ----------------------------------------------------------------------
def api(method, path, query, body):
q = lambda k: (query.get(k) or [None])[0]
if method == "GET" and path == "/api/config":
return {"can_open": shutil.which("xdg-open") is not None}
if method == "GET" and path == "/api/books":
return list_books()
if method == "POST" and path == "/api/books":
name = body.get("name", "").strip()
if not name.endswith(".ods"):
name += ".ods"
if os.path.basename(name) != name or name == "template.ods":
raise BadRequest("invalid book name")
balance = body.get("opening_balance")
try:
create_book(os.path.join(BOOKS, name), int(body["year"]),
float(balance) if balance not in (None, "") else None,
open_fn=office.open)
except FileExistsError as e:
raise BadRequest(str(e))
return {"name": name}
if method == "GET" and path == "/api/summary":
with office.open(book_path(q("book")), read_only=True) as doc:
return cashbook.read_summary(doc)
if method == "GET" and path == "/api/month":
sheet = q("sheet")
if sheet not in cashbook.FY_ORDER:
raise BadRequest("invalid sheet")
with office.open(book_path(q("book")), read_only=True) as doc:
return cashbook.read_month(doc, sheet)
if method == "POST" and path == "/api/row":
if body.get("sheet") not in cashbook.FY_ORDER:
raise BadRequest("invalid sheet")
book = book_path(body.get("book"))
with office.open(book) as doc:
try:
cashbook.update_row(doc, body["sheet"], int(body["row"]),
gst=body.get("gst"), category=body.get("category"))
except ValueError as e:
raise BadRequest(str(e))
doc.store()
return cashbook.read_month(doc, body["sheet"])
if method == "POST" and path == "/api/open":
subprocess.Popen(["xdg-open", book_path(body.get("book"))],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return {"ok": True}
if method == "GET" and path == "/api/statements":
return list_statements(q("book") or None)
if method == "GET" and path == "/api/statement":
p = statement_path(q("path"))
parsed = _parse(p)
if not parsed["txns"]:
raise BadRequest(parsed["info"].get("error") or "no transactions")
if q("book"):
with office.open(book_path(q("book")), read_only=True) as doc:
return {"info": parsed["info"], "transactions": cashbook.plan_import(doc, parsed["txns"])}
return {"info": parsed["info"], "transactions": [
{"date": t["date"].isoformat(), "reference": describe(t), "amount": t["amount"]}
for t in sorted(parsed["txns"], key=lambda t: t["date"])]}
if method == "POST" and path == "/api/statement/rename":
return {"path": rename_statement(body.get("path"), body.get("name", ""))}
if method == "POST" and path == "/api/statement/delete":
delete_statement(body.get("path"))
return {"ok": True}
if method == "POST" and path == "/api/preview":
paths = statement_paths(body.get("statements"))
txns = [t for p in paths for t in read_statement(p)]
with office.open(book_path(body.get("book")), read_only=True) as doc:
return cashbook.plan_import(doc, txns)
if method == "POST" and path == "/api/import":
paths = statement_paths(body.get("statements"))
txns = [t for p in paths for t in read_statement(p)]
book = book_path(body.get("book"))
with office.open(book) as doc:
try:
result = cashbook.import_transactions(doc, txns, bool(body.get("force")))
except cashbook.YearMismatch as e:
raise BadRequest(f"{e}. Tick 'import anyway' to import regardless.")
backup(book)
doc.store()
return [{"sheet": k, "added": a, "total": t} for k, (a, t) in result.items()]
return None
# Optional HTTP Basic auth: set CASHBOOK_PASSWORD, or CASHBOOK_PASSWORD_FILE (e.g. a
# Docker secret). The user name is CASHBOOK_USERNAME (default "cashbook").
AUTH = None
# extra host names the app is reached by (comma-separated), e.g. behind a proxy
ALLOWED_HOSTS = {h.strip().lower() for h in os.environ.get("CASHBOOK_ALLOWED_HOSTS", "").split(",") if h.strip()}
SECURITY_HEADERS = {
"Cache-Control": "no-store",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"Referrer-Policy": "no-referrer",
"Content-Security-Policy": "default-src 'self'; script-src 'self' 'unsafe-inline'; "
"style-src 'self' 'unsafe-inline'; frame-ancestors 'none'",
}
def load_auth():
password = os.environ.get("CASHBOOK_PASSWORD")
path = os.environ.get("CASHBOOK_PASSWORD_FILE")
if path:
with open(path) as f:
password = f.read().strip()
if not password:
return None
user = os.environ.get("CASHBOOK_USERNAME", "cashbook")
return base64.b64encode(f"{user}:{password}".encode()).decode()
class Handler(BaseHTTPRequestHandler):
server_version = "cashbook"
sys_version = ""
def log_request(self, code="-", size="-"):
if not self.path.startswith("/healthz"): # health checks would flood the log
super().log_request(code, size)
def _send(self, status, data, ctype="application/json", headers=None):
payload = data if isinstance(data, bytes) else json.dumps(data).encode()
self.send_response(status)
self.send_header("Content-Type", ctype)
self.send_header("Content-Length", str(len(payload)))
for k, v in {**SECURITY_HEADERS, **(headers or {})}.items():
self.send_header(k, v)
self.end_headers()
self.wfile.write(payload)
def _authorised(self):
if AUTH is None:
return True
given = self.headers.get("Authorization", "")
return given.startswith("Basic ") and hmac.compare_digest(given[6:].strip(), AUTH)
def _same_origin(self):
"""Browsers send Origin on POST; refuse ones from other sites.
Compares host names only (ports differ behind Docker port maps and proxies),
against Host, X-Forwarded-Host and CASHBOOK_ALLOWED_HOSTS.
"""
origin = self.headers.get("Origin")
if not origin:
return True
host_of = lambda h: urlparse("//" + h.strip()).hostname
allowed = {"localhost", "127.0.0.1", "::1"} | ALLOWED_HOSTS
for h in [self.headers.get("Host", "")] + self.headers.get("X-Forwarded-Host", "").split(","):
if h.strip():
allowed.add(host_of(h))
if urlparse(origin).hostname in allowed:
return True
print(f"Refused {self.command} {self.path}: Origin {origin} is not one of {sorted(allowed)}. "
"If that's your address, add it to CASHBOOK_ALLOWED_HOSTS.", file=sys.stderr, flush=True)
return False
def _handle(self, method):
url = urlparse(self.path)
if url.path == "/healthz":
return self._send(200, {"ok": True})
if not self._authorised():
return self._send(401, {"error": "login required"},
headers={"WWW-Authenticate": 'Basic realm="Cashbook", charset="UTF-8"'})
if method == "POST" and not self._same_origin():
return self._send(403, {"error": "Refused: this page's address doesn't match the server's. "
"See the server log, or set CASHBOOK_ALLOWED_HOSTS."})
if method == "GET" and url.path in ("/", "/index.html"):
with open(os.path.join(HERE, "static", "index.html"), "rb") as f:
return self._send(200, f.read(), "text/html; charset=utf-8")
try:
length = int(self.headers.get("Content-Length") or 0)
except ValueError:
return self._send(400, {"error": "bad Content-Length"})
if length > MAX_UPLOAD:
self.close_connection = True
return self._send(413, {"error": "request too large"})
raw = self.rfile.read(length) if length else b""
try:
if method == "GET" and url.path == "/api/statement/file":
p = statement_path(parse_qs(url.query).get("path", [""])[0])
with open(p, "rb") as f:
data = f.read()
return self._send(200, data, "application/pdf" if p.lower().endswith(".pdf") else "text/csv",
{"Content-Disposition": f'attachment; filename="{os.path.basename(p)}"'})
if method == "POST" and url.path == "/api/upload":
name = parse_qs(url.query).get("filename", [""])[0]
path = save_upload(name, raw)
return self._send(200, {"path": path})
body = json.loads(raw or b"{}") if method == "POST" else {}
result = api(method, url.path, parse_qs(url.query), body)
if result is None:
return self._send(404, {"error": "not found"})
self._send(200, result)
except (BadRequest, BookLocked, KeyError, ValueError) as e:
self._send(400, {"error": str(e)})
except Exception:
traceback.print_exc()
self._send(500, {"error": "something went wrong; see the server log"})
def do_GET(self):
self._handle("GET")
def do_POST(self):
self._handle("POST")
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
ap.add_argument("--host", default="127.0.0.1",
help="address to listen on (0.0.0.0 inside Docker)")
ap.add_argument("--port", type=int, default=8765)
ap.add_argument("--data", default=os.path.join(ROOT, "data"),
help="folder holding the cashbooks and statements/")
ap.add_argument("--statements", action="append", default=[],
help="extra folder to look for statements in")
args = ap.parse_args()
global BOOKS, UPLOADS
BOOKS = os.path.abspath(os.path.expanduser(args.data))
os.makedirs(BOOKS, exist_ok=True)
UPLOADS = os.path.join(BOOKS, "statements")
STATEMENT_DIRS[:] = [UPLOADS, BOOKS, os.path.expanduser("~/Downloads/anz")]
STATEMENT_DIRS.extend(os.path.abspath(os.path.expanduser(d)) for d in args.statements)
global AUTH
AUTH = load_auth()
if AUTH is None and args.host not in ("127.0.0.1", "localhost", "::1"):
print("Warning: no CASHBOOK_PASSWORD set; anyone who can reach this port can see your books",
file=sys.stderr, flush=True)
# stop LibreOffice too when the server is killed
signal.signal(signal.SIGTERM, lambda *_: sys.exit(0))
server = ThreadingHTTPServer((args.host, args.port), Handler)
print(f"Cashbook running at http://{args.host}:{args.port}", flush=True)
try:
server.serve_forever()
except KeyboardInterrupt:
pass
finally:
office.stop()
if __name__ == "__main__":
main()
+50
View File
@@ -0,0 +1,50 @@
import pytest
from fastapi.testclient import TestClient
CSV_HEADER = "Type,Details,Particulars,Code,Reference,Amount,Date,ForeignCurrencyAmount,ConversionCharge\n"
SAMPLE_CSV = CSV_HEADER + (
"Bank Fee,Monthly A/C Fee,,,,-8.50,30/04/2026,,\n"
"Visa Purchase,4835-****-****-7294 Df,,Jaycar Chris,,-115.00,13/04/2026,,\n"
"Direct Credit,Client Ltd,Inv 101,,,1150.00,20/04/2026,,\n"
"Transfer,01-0822-0082180-30,Credit,Transfer,123,500.00,02/05/2026,,\n"
"Visa Purchase,4835-****-****-7294 If, 0.58,Hetzner.Com, 0.07,-5.53,06/05/2026,EUR 3.20 converted at 0.58,\n"
)
def make_client(tmp_path, monkeypatch, **env):
"""A TestClient for a fresh app using tmp_path as the data folder."""
defaults = {"AUTH_ENABLED": "true", "AUTH_REGISTER_OPEN": "true", "AUTH_INVITE_ONLY": "true"}
for k in ("BOOTSTRAP_ADMIN_USERNAME", "BOOTSTRAP_ADMIN_PASSWORD", "CASHBOOK_STATEMENTS",
"CASHBOOK_ALLOWED_HOSTS", "PUBLIC_BASE_URL", "SMTP_HOST"):
monkeypatch.delenv(k, raising=False)
monkeypatch.setenv("CASHBOOK_DATA", str(tmp_path))
monkeypatch.setenv("HOME", str(tmp_path / "home")) # so ~/Downloads/anz isn't listed
for k, v in {**defaults, **env}.items():
monkeypatch.setenv(k, v)
from cashbook_app.db import session
from cashbook_app.main import create_app
from cashbook_app.services import accounts
session.reset_engine()
accounts._failures.clear()
return TestClient(create_app())
@pytest.fixture
def client(tmp_path, monkeypatch):
with make_client(tmp_path, monkeypatch) as c:
yield c
@pytest.fixture
def admin(client):
"""client signed in as the first (admin) account."""
r = client.post("/api/auth/register", json={"username": "admin", "password": "admin-pass-1"})
assert r.status_code == 200, r.text
return client
def invite(client, **body):
r = client.post("/api/users/invites", json=body)
assert r.status_code == 200, r.text
return r.json()["invite_url"].split("invite=")[1]
+167
View File
@@ -0,0 +1,167 @@
from fastapi.testclient import TestClient
from conftest import invite, make_client
def test_status_and_first_run_setup(client):
s = client.get("/api/auth/status").json()
assert s == {"auth_enabled": True, "register_open": True, "invite_required": True, "setup_required": True}
# the first account needs no invite and becomes the administrator, signed straight in
r = client.post("/api/auth/register", json={"username": "owner", "password": "password-1"})
assert r.status_code == 200
assert r.json()["is_superuser"] is True
assert client.get("/api/auth/me").json()["user"]["username"] == "owner"
assert client.get("/api/auth/status").json()["setup_required"] is False
def test_api_and_pages_need_sign_in(client):
for path in ("/api/books", "/api/statements", "/api/summary?book=x", "/api/users", "/api/export?book=x"):
assert client.get(path).status_code == 401, path
assert client.post("/api/books", json={"name": "x", "year": 2026}).status_code == 401
assert client.get("/", follow_redirects=False).headers["location"] == "/login"
assert client.get("/users", follow_redirects=False).status_code == 307
# public bits
assert client.get("/healthz").json() == {"ok": True}
assert client.get("/login").status_code == 200
def test_registration_needs_invite_after_setup(admin, tmp_path, monkeypatch):
other = TestClient(admin.app)
r = other.post("/api/auth/register", json={"username": "stranger", "password": "password-1"})
assert r.status_code == 403
token = invite(admin, email="bob@example.com")
r = other.post("/api/auth/register", json={"username": "bob", "password": "password-1", "invite_token": token})
assert r.status_code == 200
assert r.json()["is_superuser"] is False
# invites work once
r = other.post("/api/auth/register", json={"username": "bob2", "password": "password-1", "invite_token": token})
assert r.status_code == 409
def test_admin_invite_grants_admin(admin):
token = invite(admin, grants_superuser=True)
other = TestClient(admin.app)
r = other.post("/api/auth/register", json={"username": "second", "password": "password-1", "invite_token": token})
assert r.json()["is_superuser"] is True
def test_login_logout_and_wrong_password(admin):
c = TestClient(admin.app)
assert c.post("/api/auth/login", json={"username": "admin", "password": "nope-nope"}).status_code == 401
assert c.post("/api/auth/login", json={"username": "ghost", "password": "nope-nope"}).status_code == 401
r = c.post("/api/auth/login", json={"username": "ADMIN", "password": "admin-pass-1"}) # case-insensitive
assert r.status_code == 200
assert c.get("/api/books").status_code == 200
c.post("/api/auth/logout")
assert c.get("/api/books").status_code == 401
def test_failed_sign_ins_are_throttled(admin):
c = TestClient(admin.app)
for _ in range(5):
assert c.post("/api/auth/login", json={"username": "admin", "password": "wrong-pass"}).status_code == 401
r = c.post("/api/auth/login", json={"username": "admin", "password": "admin-pass-1"})
assert r.status_code == 429
def test_change_password_signs_out_other_sessions(admin):
laptop = TestClient(admin.app)
laptop.post("/api/auth/login", json={"username": "admin", "password": "admin-pass-1"})
assert laptop.get("/api/books").status_code == 200
r = admin.post("/api/auth/password", json={"current_password": "wrong", "new_password": "new-pass-123"})
assert r.status_code == 400
r = admin.post("/api/auth/password", json={"current_password": "admin-pass-1", "new_password": "new-pass-123"})
assert r.status_code == 200
assert admin.get("/api/books").status_code == 200 # this session stays
assert laptop.get("/api/books").status_code == 401 # other devices are signed out
assert laptop.post("/api/auth/login", json={"username": "admin", "password": "new-pass-123"}).status_code == 200
def test_user_management(admin):
token = invite(admin)
bob = TestClient(admin.app)
bob_id = bob.post("/api/auth/register", json={"username": "bob", "password": "password-1",
"invite_token": token}).json()["id"]
# regular users can't manage users
assert bob.get("/api/users").status_code == 403
assert bob.post("/api/users/invites", json={}).status_code == 403
assert bob.get("/api/books").status_code == 200
users = admin.get("/api/users").json()
assert [u["username"] for u in users] == ["admin", "bob"]
assert admin.patch(f"/api/users/{bob_id}", json={"username": "robert"}).json()["username"] == "robert"
assert admin.patch(f"/api/users/{bob_id}", json={"username": "admin"}).status_code == 400
assert admin.patch(f"/api/users/{bob_id}", json={"is_superuser": True}).json()["is_superuser"] is True
# resetting a password signs them out
assert admin.patch(f"/api/users/{bob_id}", json={"password": "reset-pass-1"}).status_code == 200
assert bob.get("/api/books").status_code == 401
admin_id = users[0]["id"]
assert admin.delete(f"/api/users/{admin_id}").status_code == 400 # not yourself
assert admin.patch(f"/api/users/{admin_id}", json={"is_superuser": False}).status_code == 400
assert admin.delete(f"/api/users/{bob_id}").status_code == 200
assert admin.delete(f"/api/users/{bob_id}").status_code == 404
def test_last_admin_is_protected(admin):
token = invite(admin)
bob = TestClient(admin.app)
bob_id = bob.post("/api/auth/register", json={"username": "bob", "password": "password-1",
"invite_token": token}).json()["id"]
admin.patch(f"/api/users/{bob_id}", json={"is_superuser": True})
admin_id = next(u["id"] for u in admin.get("/api/users").json() if u["username"] == "admin")
# bob demotes admin, then can't demote or delete himself as the last admin
bob.post("/api/auth/login", json={"username": "bob", "password": "password-1"})
assert bob.patch(f"/api/users/{admin_id}", json={"is_superuser": False}).status_code == 200
assert bob.patch(f"/api/users/{bob_id}", json={"is_superuser": False}).status_code == 400
def test_invites_list_and_revoke(admin):
invite(admin, email="a@example.com")
pending = admin.get("/api/users/invites").json()
assert [i["email"] for i in pending] == ["a@example.com"]
assert admin.delete(f"/api/users/invites/{pending[0]['id']}").status_code == 200
assert admin.get("/api/users/invites").json() == []
assert admin.post("/api/users/invites", json={"email": "not-an-email"}).status_code == 422
def test_cross_site_changes_are_refused(admin):
r = admin.post("/api/books", json={"name": "x", "year": 2026}, headers={"Origin": "https://evil.example"})
assert r.status_code == 403
r = admin.post("/api/books", json={"name": "x", "year": 2026}, headers={"Origin": "http://testserver"})
assert r.status_code == 200
def test_auth_disabled_is_open(tmp_path, monkeypatch):
with make_client(tmp_path, monkeypatch, AUTH_ENABLED="false") as c:
assert c.get("/api/books").status_code == 200
assert c.get("/", follow_redirects=False).status_code == 200
assert c.get("/api/auth/me").json() == {"auth_enabled": False, "user": None}
assert c.get("/api/users").status_code == 400
def test_empty_auth_setting_keeps_auth_on(tmp_path, monkeypatch):
with make_client(tmp_path, monkeypatch, AUTH_ENABLED="") as c:
assert c.get("/api/books").status_code == 401
def test_bootstrap_admin(tmp_path, monkeypatch):
with make_client(tmp_path, monkeypatch, BOOTSTRAP_ADMIN_USERNAME="boss",
BOOTSTRAP_ADMIN_PASSWORD="boss-pass-1") as c:
assert c.get("/api/auth/status").json()["setup_required"] is False
r = c.post("/api/auth/login", json={"username": "boss", "password": "boss-pass-1"})
assert r.json()["is_superuser"] is True
def test_open_registration_without_invites(tmp_path, monkeypatch):
with make_client(tmp_path, monkeypatch, AUTH_INVITE_ONLY="false") as c:
c.post("/api/auth/register", json={"username": "first", "password": "password-1"})
other = TestClient(c.app)
r = other.post("/api/auth/register", json={"username": "second", "password": "password-1"})
assert r.status_code == 200 and r.json()["is_superuser"] is False
+126
View File
@@ -0,0 +1,126 @@
from conftest import CSV_HEADER, SAMPLE_CSV
def upload(client, name, text):
return client.post(f"/api/upload?filename={name}", content=text.encode())
def test_book_import_and_figures(admin):
assert admin.post("/api/books", json={"name": "cashbook-2026-27", "year": 2026,
"opening_balance": "100"}).status_code == 200
assert admin.post("/api/books", json={"name": "cashbook-2026-27", "year": 2026}).status_code == 400
r = upload(admin, "april.csv", SAMPLE_CSV)
assert r.status_code == 200
path = r.json()["path"]
stmts = admin.get("/api/statements?book=cashbook-2026-27").json()
assert [(s["name"], s["count"], s["imported"]) for s in stmts] == [("april.csv", 5, 0)]
preview = admin.post("/api/preview", json={"book": "cashbook-2026-27", "statements": [path]}).json()
assert [p["status"] for p in preview] == ["new"] * 5
fee = next(p for p in preview if p["reference"] == "Monthly A/C Fee")
assert (fee["gst"], fee["category"]) == ("Excl GST", "Bank fees")
result = admin.post("/api/import", json={"book": "cashbook-2026-27", "statements": [path]}).json()
assert sum(r["added"] for r in result) == 5
# importing again adds nothing
result = admin.post("/api/import", json={"book": "cashbook-2026-27", "statements": [path]}).json()
assert sum(r["added"] for r in result) == 0
s = admin.get("/api/summary?book=cashbook-2026-27").json()
apr, may = s["months"][0], s["months"][1]
assert (apr["count"], may["count"]) == (3, 2)
assert round(apr["closing"], 2) == 100 - 8.50 - 115 + 1150
assert round(s["closing"], 2) == round(100 - 8.50 - 115 + 1150 + 500 - 5.53, 2)
assert may["income_cats"]["Funds Intr."] == 500
def test_edit_rows_and_gst_return(admin):
admin.post("/api/books", json={"name": "b", "year": 2026})
path = upload(admin, "april.csv", SAMPLE_CSV).json()["path"]
admin.post("/api/import", json={"book": "b", "statements": [path]})
month = admin.get("/api/month?book=b&sheet=APR").json()
sale = next(r for r in month["rows"] if r["income"] == 1150)
jaycar = next(r for r in month["rows"] if r["expense"] == 115)
assert jaycar["gst"] == "Incl GST" and jaycar["gst_out"] == 15.0
# mark the sale as GST-inclusive and allocate it
r = admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": sale["row"], "gst": "Incl GST", "category": "Sales"})
updated = next(x for x in r.json()["rows"] if x["row"] == sale["row"])
assert (updated["gst_in"], updated["category"], updated["unallocated"]) == (150.0, "Sales", 0)
assert admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": sale["row"],
"category": "Purchases"}).status_code == 400
assert admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": sale["row"],
"gst": "Maybe"}).status_code == 400
p = admin.get("/api/gst?book=b&frequency=six").json()["periods"][0]
assert p["due"] == "2026-10-28"
b = p["boxes"]
assert (b["5"], b["6"], b["8"], b["11"], b["12"], b["15"]) == (1150, 0, 150.0, 115, 15.0, 135.0)
# zero-rated sales count in boxes 5 and 6 but carry no GST
admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": sale["row"], "gst": "Zero-rated"})
b = admin.get("/api/gst?book=b").json()["periods"][0]["boxes"]
assert (b["5"], b["6"], b["7"], b["8"], b["15"]) == (1150, 1150, 0, 0.0, -15.0)
two = admin.get("/api/gst?book=b&frequency=two").json()["periods"]
assert [(x["end"], x["due"]) for x in two][:1] == [("2026-05-31", "2026-06-28")]
assert two[-1]["due"] == "2027-05-07"
def test_other_years_are_skipped(admin):
admin.post("/api/books", json={"name": "old", "year": 2020})
path = upload(admin, "april.csv", SAMPLE_CSV).json()["path"]
preview = admin.post("/api/preview", json={"book": "old", "statements": [path]}).json()
assert {p["status"] for p in preview} == {"wrong year"}
result = admin.post("/api/import", json={"book": "old", "statements": [path]}).json()
assert sum(r["added"] for r in result) == 0
def test_csv_export(admin):
admin.post("/api/books", json={"name": "b", "year": 2026})
path = upload(admin, "april.csv", SAMPLE_CSV).json()["path"]
admin.post("/api/import", json={"book": "b", "statements": [path]})
r = admin.get("/api/export?book=b&format=csv")
assert r.status_code == 200
assert 'filename="b.csv"' in r.headers["content-disposition"]
lines = r.content.decode("utf-8-sig").splitlines()
assert lines[0].startswith("Date,Month,Reference")
assert len(lines) == 6
assert admin.get("/api/export?book=b&format=pdf").status_code == 400
assert admin.get("/api/export?book=nope&format=csv").status_code == 404
def test_statement_management(admin):
r = upload(admin, "notes.csv", "a,b\n1,2\n")
assert r.status_code == 400 and "ANZ" in r.json()["detail"]
assert upload(admin, "evil.sh", "x").status_code == 400
path = upload(admin, "april.csv", SAMPLE_CSV).json()["path"]
r = upload(admin, "again.csv", SAMPLE_CSV)
assert r.status_code == 400 and "already here" in r.json()["detail"]
view = admin.get("/api/statement", params={"path": path}).json()
assert len(view["transactions"]) == 5
assert admin.get("/api/statement/file", params={"path": path}).text.startswith("Type,Details")
assert admin.get("/api/statement/file", params={"path": "/etc/passwd"}).status_code == 400
new_path = admin.post("/api/statement/rename", json={"path": path, "name": "april-2026.csv"}).json()["path"]
assert new_path.endswith("april-2026.csv")
assert admin.post("/api/statement/rename", json={"path": new_path, "name": "x.pdf"}).status_code == 400
assert admin.post("/api/statement/delete", json={"path": new_path}).status_code == 200
assert admin.get("/api/statements").json() == []
def test_csv_and_pdf_of_same_period_dont_double_up(admin):
"""Matching is on date and amount, so differently-worded copies are recognised."""
admin.post("/api/books", json={"name": "b", "year": 2026})
first = upload(admin, "a.csv", SAMPLE_CSV).json()["path"]
shouty = CSV_HEADER + "\n".join(line.upper() for line in SAMPLE_CSV.splitlines()[1:]) + "\n"
second = upload(admin, "b.csv", shouty).json()["path"]
result = admin.post("/api/import", json={"book": "b", "statements": [first, second]}).json()
assert sum(r["added"] for r in result) == 5
+62
View File
@@ -0,0 +1,62 @@
from conftest import SAMPLE_CSV
def setup_book(admin):
admin.post("/api/books", json={"name": "b", "year": 2026, "opening_balance": "100"})
path = admin.post("/api/upload?filename=april.csv", content=SAMPLE_CSV.encode()).json()["path"]
admin.post("/api/import", json={"book": "b", "statements": [path]})
rows = admin.get("/api/month?book=b&sheet=APR").json()["rows"]
sale = next(r for r in rows if r["income"] == 1150)
jaycar = next(r for r in rows if r["expense"] == 115)
admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": sale["row"], "gst": "Incl GST", "category": "Sales"})
admin.post("/api/row", json={"book": "b", "sheet": "APR", "row": jaycar["row"], "category": "Purchases"})
def values(data, form):
return {r["box"]: r["value"] for r in data[form.lower()]["rows"] if r["box"]}
def test_ir10_from_cashbook(admin):
setup_book(admin)
data = admin.get("/api/tax?book=b").json()
v = values(data, "IR10")
assert v["2"] == 1000.0 # sales excluding GST
assert v["4"] == 100.0 # purchases excluding GST
assert v["6"] == 900.0
assert v["24"] == 8.5 # bank fees
assert v["27"] == 891.5
assert v["31"] == round(100 - 8.5 - 115 + 1150 + 500 - 5.53, 2) # bank balance at year end
assert v["57"] == 0.0
assert any("isn't allocated" in n for n in data["ir10"]["notes"]) # the Hetzner payment
assert any("funds introduced" in n for n in data["ir10"]["notes"])
def test_adjustments_and_ir4(admin):
setup_book(admin)
# depreciation, closing stock and RWT; tax to pay at 28%
admin.post("/api/tax", json={"book": "b", "form": "IR10", "box": "13", "value": 91.5})
admin.post("/api/tax", json={"book": "b", "form": "IR10", "box": "5", "value": 50})
data = admin.post("/api/tax", json={"book": "b", "form": "IR4", "box": "30K", "value": 100}).json()
v10, v4 = values(data, "IR10"), values(data, "IR4")
assert (v10["6"], v10["25"], v10["29"]) == (950.0, 100.0, 850.0)
assert (v4["20B"], v4["29"], v4["30A"], v4["30B"]) == (850.0, 850.0, 850.0, 238.0)
assert v4["30L"] == 138.0
assert data["ir4"]["result"] == {"amount": 138.0, "kind": "to pay"}
# adjustments add to the cashbook figure, and clearing one removes it
data = admin.post("/api/tax", json={"book": "b", "form": "IR10", "box": "2", "value": -1000}).json()
assert values(data, "IR10")["2"] == 0.0
assert data["ir4"]["result"]["kind"] == "refund" # a loss: no tax, provisional tax comes back
data = admin.post("/api/tax", json={"book": "b", "form": "IR10", "box": "2", "value": None}).json()
assert values(data, "IR10")["2"] == 1000.0
def test_tax_inputs_are_checked(admin):
setup_book(admin)
assert admin.post("/api/tax", json={"book": "b", "form": "IR10", "box": "29", "value": 1}).status_code == 400
assert admin.post("/api/tax", json={"book": "b", "form": "IR3", "box": "1", "value": 1}).status_code == 400
assert admin.get("/api/tax?book=nope").status_code == 404
def test_tax_needs_sign_in(client):
assert client.get("/api/tax?book=b").status_code == 401