Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
708ce8352e | ||
|
|
177dfbc0a4 |
No files matched your search
@@ -6,6 +6,15 @@
|
||||
"runtimeExecutable": "pipenv",
|
||||
"runtimeArgs": ["run", "dev"],
|
||||
"port": 8765
|
||||
},
|
||||
{
|
||||
"name": "cashbook-scratch",
|
||||
"runtimeExecutable": "env",
|
||||
"runtimeArgs": ["CASHBOOK_DATA=/tmp/claude-1000/-home-jimmy-projects-accounts/1e7ee5e8-8915-44fb-a3a5-3f2db7ea9ee9/scratchpad/data",
|
||||
"HOME=/tmp/claude-1000/-home-jimmy-projects-accounts/1e7ee5e8-8915-44fb-a3a5-3f2db7ea9ee9/scratchpad/home",
|
||||
"AUTH_ENABLED=false", ".venv/bin/uvicorn", "app:app", "--app-dir", "src",
|
||||
"--host", "127.0.0.1", "--port", "8766"],
|
||||
"port": 8766
|
||||
}
|
||||
]
|
||||
}
|
||||
+2
-1
@@ -1,10 +1,11 @@
|
||||
FROM debian:bookworm-slim
|
||||
|
||||
# LibreOffice (headless Calc) + its Python bridge for spreadsheet export,
|
||||
# pdftotext for PDF statements
|
||||
# pdftotext for PDF statements, tesseract + Pillow to read receipt photos
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
python3 python3-venv python3-uno libreoffice-calc-nogui poppler-utils fonts-dejavu-core \
|
||||
tesseract-ocr tesseract-ocr-eng python3-pil \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
@@ -8,6 +8,7 @@ fastapi = "*"
|
||||
uvicorn = "*"
|
||||
sqlalchemy = "*"
|
||||
bcrypt = "*"
|
||||
fpdf2 = "*"
|
||||
|
||||
[dev-packages]
|
||||
pytest = "*"
|
||||
|
||||
Generated
+174
-4
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "47119175f9f01a429431ed06baae2e53be90fefd8a53c7a73a273db3ea065845"
|
||||
"sha256": "83c5a098be61a5bfa7680a062089ff53e8927dac5d618bc2f4194d97c80c7964"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {},
|
||||
@@ -116,6 +116,14 @@
|
||||
"markers": "python_version >= '3.10'",
|
||||
"version": "==8.5.0"
|
||||
},
|
||||
"defusedxml": {
|
||||
"hashes": [
|
||||
"sha256:1bb3032db185915b62d7c6209c5a8792be6a32ab2fedacc84e01b52c51aa3e69",
|
||||
"sha256:a352e7e428770286cc899e2542b6cdaedb2b4953ff269a210103ec58f6198a61"
|
||||
],
|
||||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'",
|
||||
"version": "==0.7.1"
|
||||
},
|
||||
"fastapi": {
|
||||
"hashes": [
|
||||
"sha256:3785963de305dd09912426cb81f2d230fce98ab3521cdfe97265c8d14983128c",
|
||||
@@ -125,6 +133,79 @@
|
||||
"markers": "python_version >= '3.10'",
|
||||
"version": "==0.162.0"
|
||||
},
|
||||
"fonttools": {
|
||||
"hashes": [
|
||||
"sha256:0781fe22583529e1e98bb8a3a33040632e202a4c427ed7e65412c41a21b8ebcb",
|
||||
"sha256:07a2f36b3263faadf5b7b548f62fd3cac401e490189c82b16f7139ac0df91cd4",
|
||||
"sha256:13d7507252c5a5d7941a5fa1be27d335c378ef07983ea2bb24988bf600eadd5e",
|
||||
"sha256:1671e5f368b0c136ed9fb62fef26c7e425b4ebb0bb669a1cb7ba453f5bba580b",
|
||||
"sha256:1be99c1f07fca59510d657ef3eae584b5273fa4e203aff2383b3520744e19536",
|
||||
"sha256:1f200cd2cf046a5a0b03babe84ebf8bbc12187d5d57f50bc03f24be89e7c1605",
|
||||
"sha256:2a09d33a9264a6b29efca9dc633b53969aaedb250a9c8521d60f51280cef65ca",
|
||||
"sha256:2bfab2f5d1d255dec82f4bd082a1c10e77df808e42210890f50a9c30bf91570e",
|
||||
"sha256:33ae23a531795864fcdbbab91a40c824976e22642c05efca3bd8a0b00630d0e7",
|
||||
"sha256:36f0fee56227b909c9d1392f17b23803616f1f04efbe020c176d9945cabc0be5",
|
||||
"sha256:38fc772182ebff3e2ebba7886460476eb65842b601ca0b9221a6a5826136396e",
|
||||
"sha256:3a19f6d5e1a373f2e4a5bdb9452c8ba212dd9f1e43df2fff042b896e28084e4a",
|
||||
"sha256:3b34324deb3e09ad648039a0a86d945b83f23a44fe3da74a84e6ada71fe0b650",
|
||||
"sha256:3cb57e6600ca77c0b1729cf8adc23bc0652633a37f18cfa934d9c7bc3de25519",
|
||||
"sha256:3fb95166eaebad72f9deb1d0d781f652525f47e4693e553dad3954cf68ed6e9c",
|
||||
"sha256:4304f03ed7f4ba000a8dcc941ad854bfa52e2f3b6112b8f099b6f431cf98e701",
|
||||
"sha256:451077d2fc61a2a03f5dca54d84fbb01051ad781f48ea137eff35c775a4cb025",
|
||||
"sha256:47dba566b4f475b0fb5f83129487c21b6a6a4edc41c0eec52524f969a68a3d45",
|
||||
"sha256:48696b630069e29b8aa5ea8b034e4f651a2e112073938ec16bd536dadde1debf",
|
||||
"sha256:4e2c1586b5b6588a47d02e2588170eefdc996b708f2659c44dbe169bd6fcacb5",
|
||||
"sha256:50c41e30aa2e0130b80d1a58ac0f3ea7c02a854a70dbea1ff8d88e0ce524806f",
|
||||
"sha256:5377e0e991e3e2be47fd1215414b20c2288b546e5a8c6d80b1a7cde9c72a89e1",
|
||||
"sha256:592d8f72024dea0408739a92599e4f839b960e1e887b25adc76dc87271fdac76",
|
||||
"sha256:59f44309ce78851c9621ee88e3f667ca3fbcc89dc0e8641336be3f12ba06bfd4",
|
||||
"sha256:5ad690ea5bfd8913d1a6e5d5e9825ccf4ed342716e63c2b0d7f490d50235daef",
|
||||
"sha256:5ccaa87b312219d02cf72a79f1eb2f3ce028882d6fd1b79336141005db83b84e",
|
||||
"sha256:621b3152b5d0412381b792bacfe410ac1f09c2c4f28a44bd19d26fe7160cfc96",
|
||||
"sha256:64e56d0d6a39780fee86955c758674538387b18f911ea904a4aae8f8e30fa26f",
|
||||
"sha256:690ab72d338aa9bf8e5cd9aefb86e0d3c458d8b9de4df041fb7dc2ed4703144e",
|
||||
"sha256:6c19a770a8d273371a37969003c143eaa629ab893c3db028af8b91d04c6f9a6d",
|
||||
"sha256:720bcf27727193b0fe1883c2e036dc88e37047916e977f5c3daf6ee4316e9656",
|
||||
"sha256:72d6d316dffc92eadb771f697f289ea7b60f689580931328905a267bd170f93b",
|
||||
"sha256:7343cd0ef70edf8be7f4913cb9b55b992fb4e04055b47dcfecddcc2eb045a9d2",
|
||||
"sha256:768a33bbe6ec5ba8f19979f938752f06d4e614cb554fd47abd7830f2007660e3",
|
||||
"sha256:775364ac079e2ea7a2eedb5f9172c57b059d638ff79e2bf8d4257e5805713f32",
|
||||
"sha256:77e0d4096a2ac60aebe43928b5382766df2d148577db8e8ff79b6a50879a6c06",
|
||||
"sha256:8239e2ca24878715a19f061d065b5721e87da81d145e48b3418f771a469b5a24",
|
||||
"sha256:846982e89b1861d6c9d7fcd6567aec3fa5a10ad313e7f2076045fcd339cfbd8e",
|
||||
"sha256:84a3aed005de106fb1794372dace82eca50859d52ae26da4bb6c602480a41250",
|
||||
"sha256:89ad62d116f45bb45873bb92fd69c14a720ba591cba488044731954a5565e194",
|
||||
"sha256:8c21073cfe7129aaa070d94f575c1e2a880ae4aae1dcffd5352f174b96d27d16",
|
||||
"sha256:8c58a8a9ad447bead6f91e5f50b23c0e4988538cdbd9bf2f68952b39f5900a84",
|
||||
"sha256:916836845e4b1c1447bb61390ffb3cb5f2940fd9f5d6de4685539a81806c7764",
|
||||
"sha256:952eb091689545d86d16e40f719ed7bb086dd810a07dcc9ea2ca0a81004810a3",
|
||||
"sha256:9c38fece8156cbda31b42d49c4a187858056a35932b88233b6fb31eaca5cf67f",
|
||||
"sha256:ad813967410ba6d24a52850df59b164ee17883f17b96a91b4b0ac6e9d7b5a118",
|
||||
"sha256:ad8b4f7c754a627e91908fa1a1ccc90b489cd2810c0ba16acd26ea2ff5273db7",
|
||||
"sha256:b274ed3106b8086f237b7dbb1529c28142ba10ae40b9d285be0ae6a44b2946d0",
|
||||
"sha256:b3ddf350e74508102b33dc6b32984b6dd751359a7c57732bcd39f9d7cb37d71e",
|
||||
"sha256:bd3239e5709fd4c3343db67245ede46aece610d7f7ef61afb174718122479282",
|
||||
"sha256:e0ca4c8438dd6320f5850c9bbee3b3980455ee3bac602a9a0299caf9e799a0e8",
|
||||
"sha256:e2b5d511ea012dce7bd6df12b279b7d7a5b01b019865717d03ae679f4b944fa5",
|
||||
"sha256:e8a8545cbd58bd29494ffe81e3cb35f8a29332a8e495c42bec334145ce8cd65b",
|
||||
"sha256:eb3c98cac93aac4b9f6e3ce2008325340b234cc9b0338ca6b513f31962a1e278",
|
||||
"sha256:f672398385849ff79e7dd50c0a06efe110c8ba23d8890f9b45fbb922bc2f55f6",
|
||||
"sha256:fc6b6b03aa44f504c8734e62ccc3e4dcda9f4b8213a85aa80742e4d1cc9d96ef",
|
||||
"sha256:fcb9743140419410161acfe7ec205fb0a8a703acfccb85b586becb5a97c047c9",
|
||||
"sha256:fd79e36c2968e9fc3e1b082f2ba7dc63ae88a161a3d8ceaa0746b906455f3617"
|
||||
],
|
||||
"markers": "python_version >= '3.11'",
|
||||
"version": "==4.67.0"
|
||||
},
|
||||
"fpdf2": {
|
||||
"hashes": [
|
||||
"sha256:5b0b3786f5236a2b3cc83c1fee567df17ddd314f8c4e13d820d8f09b617ab4f0",
|
||||
"sha256:6e1d94af6d6311950a23dec7fb5fc84b000203eb59aee8e76c1e701b12a14976"
|
||||
],
|
||||
"index": "pypi",
|
||||
"markers": "python_version >= '3.10'",
|
||||
"version": "==2.8.9"
|
||||
},
|
||||
"h11": {
|
||||
"hashes": [
|
||||
"sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1",
|
||||
@@ -149,6 +230,95 @@
|
||||
"markers": "python_version >= '3.10'",
|
||||
"version": "==1.45.1"
|
||||
},
|
||||
"pillow": {
|
||||
"hashes": [
|
||||
"sha256:02464da86f561f410e13b061ec37c3068a9cb557a095494157742d07b3c24a27",
|
||||
"sha256:04a63720f79e22fd54d897c233a68314c50ca0603360f54ce2bd5db8f0f43782",
|
||||
"sha256:05cd2cd39055d0b9650060e4513ef4a9a0aea870083950bd046a0d6490f93a22",
|
||||
"sha256:06903aac074d00c988e77a1fc435286dc498506d4ca8d6f69df9babddf4ecc15",
|
||||
"sha256:08b826a9bf1265c76de04a42061d61ea6b109418b7826f28ff1ecdfaa26c5502",
|
||||
"sha256:0d1a9cf7bc6917538b20f8d06b9ab2147e542f68849e96fc58eb66bdf6acc692",
|
||||
"sha256:10b5019883c98f1dd819ca48ffc6f33a91cee04948fe637c605f4345c7cafd6f",
|
||||
"sha256:12b0fa15879fe5a9d9eebe66e4e2d74128a773b1a4448010c9c714f45e04b9d8",
|
||||
"sha256:12fbb851cf323cc19aac3eea1df5ad17b57e91e9fe3cfd06100dba7bb8208c13",
|
||||
"sha256:149a1c323ba366f90b48e6f1a6db6582eb50512df374588de56fc909abc0f0f3",
|
||||
"sha256:19b66ca0a4d25cc0200b381ce824c7ea090db61427315e73cfb05dbf1b35c369",
|
||||
"sha256:1c774b9c0ba0d0b356f71530677b39028878a2cae5a93e0a13779cfd17120f88",
|
||||
"sha256:20722cd5375533ad754053363a80b8fb30c79d37930a6f7228030e237ce3bd5d",
|
||||
"sha256:20822140c9a2ba9c5e42df342b0a892ab2e102fc0e3db967a133bb9ce3f898bb",
|
||||
"sha256:21c32061a5512dc13f87cc7303e120e4d77c2f70df89a35af0e3ce246e5ac57f",
|
||||
"sha256:2310e2adfc13c6d457d7d0f2ffdd5c56a74a317650cf765887df8d515a647ac0",
|
||||
"sha256:23edf4ec4b6f4f240e8df6e4985e9ee2ced86474632433e55fb1e2548744bf40",
|
||||
"sha256:2d50500a4a3bc6469adcdb319cf05d688b08ca3bf0f22a05339f3b25b433a599",
|
||||
"sha256:2e31c4e68cffd7b1f116127626813b82fcba19edf6b940a7b0c266d6484d0055",
|
||||
"sha256:34fb3db1149c879343861831728c243c432d4a5afc8f1878ca7612ef8de2a033",
|
||||
"sha256:37290fbef0c73e2d51d594eb78fded7dc7170d1c3ad0a890269e6fd8c8c4e061",
|
||||
"sha256:405be6058e206a86fedce2f715ecc2343f94072d7925251632dffa2c05f70dca",
|
||||
"sha256:40a266371ece581e5de13b6635ef78d7487824af80561b6ca27694a88c19bc0f",
|
||||
"sha256:42d63be3af4f9b951b199b0a9d23365d37ab14687e6d77b74940432c8515c4ca",
|
||||
"sha256:44629d4498c9cb6b2051806155f22141df0d8a929c56134b166655232dfb2f8c",
|
||||
"sha256:49c12af515073e9bf511e4ef736743bf70d531a643668722279314b937742ba4",
|
||||
"sha256:4a25756244bdcce3b50580baadf320fa48297f0eeb8e114ba64139a40d4d0fc8",
|
||||
"sha256:4bd024d567eca931b668978d672dba914d4b8e2628e2c3073a2ad178de8a2620",
|
||||
"sha256:4f8db54177035a2b5f717f94475b74907e12a96dc74f6d487f3966b0a4694071",
|
||||
"sha256:51ebb529fc5e8abdd55302f6c3f8943037348ec2384329a3e55677f69ae2fdfc",
|
||||
"sha256:567f20f1c75897d7663830da6ea8af719e03d712b1d9ee7f813319f9ece42e5a",
|
||||
"sha256:5933fcea5a5ba64c686f55c886a115cbfd92f2900460784a1e79909fdbafd7b6",
|
||||
"sha256:5bdb38ba220b61b4ec31dd1046dea6b016d3c6850a23f43ed8b58332b2fc668e",
|
||||
"sha256:5f56499c1a74d2b862beb0c33b8016f99d095858455df9d21583bbbc1c15b582",
|
||||
"sha256:67871a13cf47275201553b536c025da777f997fa562a19eb798af4ad4b97c18f",
|
||||
"sha256:67c8d44ad89fa7eb88de5d6ef6199b1e03dd24974f85a67639fc8badb801fe79",
|
||||
"sha256:6a0920cb55df5a4d3f6862964908078c0bd59332bdd1bcd20d14e8c70ef27cc5",
|
||||
"sha256:6bce6a29c3898191f4c9998e4d4e8f69790caeabde46e54da4bc235ce5bde683",
|
||||
"sha256:6f38f3f885a09cafc93fd667b1173a92f379e43785d635e7983c893f0da319fb",
|
||||
"sha256:73ee25cf8cf8b75d5a5aeed7cb6588f5e5dbfbb0fde51707b630bbb23897645c",
|
||||
"sha256:7b999816b5850a35ada08443e6e03945f1f6488144a83afe2d37b240a552aafa",
|
||||
"sha256:7c88c10dd432f3b39ccdafc9ed81d0c21b8fd634502574da76d1bfa4fe2c76d0",
|
||||
"sha256:7d5e097eabd5d4915c27ac9f6e9325bb1e01a7c94d7348b5c4451d3fc30e8521",
|
||||
"sha256:7e91e97a929232cdb828889ee6ad0e48d7f40f4ec38818f4e4a8968b2b1af22e",
|
||||
"sha256:7f6299e494e46253d40c34653cac369412f44ca53b5f85d848d3f95b1ed0546c",
|
||||
"sha256:816752a5ef76a80484c8c441c85593b7ea9304f6231fc5776ad1fbff7e773058",
|
||||
"sha256:83b37abd87b590acd8f47ca8a61f70893322b11834d36e07c2298dacad4d14d2",
|
||||
"sha256:8619685cad8efc4c38c66d3b36aad854523297c43d748b3b6f0cc03c90ba3463",
|
||||
"sha256:8bf295fe459343bc6093c8e5e0df60bfa189e0fcb775a22e2c2488fdeccc6c0d",
|
||||
"sha256:8e459b613cf31f8980f584387b4fe47bf08e076e288d61aa8eba1f7fd4ddbb81",
|
||||
"sha256:8f36aa77f74b14a2103622b196402c71330e4e7cf4122ee45c854e9a01e0eecf",
|
||||
"sha256:93ff9c9c3663ce23aaa5d0b6b6abac55359e2a0c06c7e30f3771829af816be63",
|
||||
"sha256:9a635068d9793c7f0d929f39f18b58f1bd86c236e8e7b64167b86af9232ebca0",
|
||||
"sha256:9dda0788a9932776a75d8bcc020076edc308ce2508b51f6739a45e3158b3fc42",
|
||||
"sha256:a192360d512305fe92d3053e4f7627bbad1f7708f1f4ad38cccd9336acabe9ad",
|
||||
"sha256:a2a14cd73d5e6c927f644b0110ee81f59c4ef506259ec9183904747f2caacc81",
|
||||
"sha256:a625df9a2a5597fa5e5266e01aefc82604bb85d291e3547981e863560d75da6c",
|
||||
"sha256:a6aaa7871aebca081eb5c45f83e845bbd9712ae99fa9286dbfdcee835e63f4b4",
|
||||
"sha256:aa694c49787040943570d61c9b8fc01271bd2a2cfdd55f0d896f8aecdce1a981",
|
||||
"sha256:adb3c2124c68e480f4632d06cb7caa3b48a51678475f483b4edb1c6dee29ea43",
|
||||
"sha256:b84be41387fc00db28c13c02e30d42b02d8d8f6a4a741a1aee6012ca7f1f6039",
|
||||
"sha256:bf97ba1b5542050e9b0c74e632323b6d8136cc99d64e193abebee22fe6013c76",
|
||||
"sha256:c3bc8ee0271387e0b1e3ad0ff893645990a656e79bf9c42f4f734a3642b11d51",
|
||||
"sha256:c50c80e3591e5cf9a8dfa6007d4f10732f02e32263850999286e56ccd650cfb2",
|
||||
"sha256:c6fd031333dedcad7ce3e03d244093f43504f209e86925ebdb2b868fa4037630",
|
||||
"sha256:c87a04e7f152ee4db5dbe15f0bfa3fe628a1aeedbe66b81c43b87f52c813f9ac",
|
||||
"sha256:cdcd1ba258178f7356d74170c9a81ba4eab6a5d55748bc402a5a2a6fe6be3510",
|
||||
"sha256:d4737f50d23ddd11d6685faacbb9252887dde0bc7997127757247983d74399d6",
|
||||
"sha256:d7710eb676e9bb68965cb045be0d333b4c0d7f9dfbaa7fbf121385b1588683d3",
|
||||
"sha256:d9b0a2bee3f3e03e9f29057aedb807dbf1e1d96ee1b9d0f422d9db6c8583271d",
|
||||
"sha256:e65dd6e75de67b2362a4fa3e16aa627595f65054dc4ec73d64680c84d592fa61",
|
||||
"sha256:e892354a424b1e82a78ba16785822bdd40de06f19446a3d4a28047df1a4f8932",
|
||||
"sha256:e8ca8f69b7c77fa96dad99f8638c1fc4f1c0bed4acaa1baf7a3c31bdb80a0ab1",
|
||||
"sha256:e91c2b1cbeae768c98752e67e717e283d68dae45717b41f2d91eaee648e37b7d",
|
||||
"sha256:e98b4f91f99b24b1da72e10621ba181f7281922384a7403094debb4199de6c00",
|
||||
"sha256:ef5fe6b7db3f501c56000ba8fe7c7e1a35ca1d45824fbeb95a67daa85cff4dbe",
|
||||
"sha256:f11a3c1358bb3c68012a7b1f41b223c3db9f7bfde90b481df028cfe1c3363313",
|
||||
"sha256:f391dc92a015c478745192fe4f2e120c2af2aba18d9fe0616341cbf537599806",
|
||||
"sha256:f7cbb7a64e7ec664775195e4f81923bc8a93d9451b963f844e59118f63147a7a",
|
||||
"sha256:f9bd7d260d986f3553518c5d723544346fa48031f8aef3265d786aeb59845bc0",
|
||||
"sha256:fc481f742ccda20e9ed760b8f978dff50f2a4e9bc253ac167fb0a05b0783ef52",
|
||||
"sha256:ff3fe9eb621fbb9db4c4be33ab946f6bac5f98ab50ca8d3260503653656c961b",
|
||||
"sha256:ffea6b2986a797c84b6d1d27d4f43e0b83c73fc16281a930891cd237cf0d08fe"
|
||||
],
|
||||
"markers": "python_version >= '3.11'",
|
||||
"version": "==13.0.0"
|
||||
},
|
||||
"pydantic": {
|
||||
"hashes": [
|
||||
"sha256:9195d967ec791692a04438115466764fb8b9a27b31f14a760437694f40d6b454",
|
||||
@@ -381,11 +551,11 @@
|
||||
},
|
||||
"starlette": {
|
||||
"hashes": [
|
||||
"sha256:3d931de246ed870e35ce605974440816ef895e592f255d1396cfdf191d4c4318",
|
||||
"sha256:fd3f159936e882d06c1108f84688f7cda2cf85fc6a592a50edbd175e882c2ddc"
|
||||
"sha256:478c0ab3b4ced6e82719849c118fa406f0278a3499c1a898cd15cd31bdc38ce4",
|
||||
"sha256:b8399098d4c1e3522734f7235dc79b1fc967f96cf139c81e0fa30479e0685f9b"
|
||||
],
|
||||
"markers": "python_version >= '3.11'",
|
||||
"version": "==1.9.0"
|
||||
"version": "==1.10.0"
|
||||
},
|
||||
"typing-extensions": {
|
||||
"hashes": [
|
||||
|
||||
@@ -21,6 +21,10 @@ administrators manage who has access.
|
||||
- **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.
|
||||
- **Receipts and invoices:** attach photos of receipts and PDF invoices to
|
||||
transactions. The supplier, date, total, GST and line items are read from them on
|
||||
your own server (Tesseract OCR), and they're matched to bank transactions by amount
|
||||
and date (see [Receipts and invoices](#receipts-and-invoices)).
|
||||
- **Accounts:** sign-in with personal accounts. Administrators invite people, reset
|
||||
passwords and manage who's an administrator (see [Accounts](#accounts)).
|
||||
|
||||
@@ -54,7 +58,8 @@ administrator account. Stop it with `docker compose down`, and check it with
|
||||
**What the compose file sets up**
|
||||
|
||||
- **Data:** `./data` is mounted at `/data`. It holds the database (`cashbook.db`:
|
||||
books and accounts) and `statements/`, where uploads go. Back up this folder.
|
||||
books and accounts), `statements/`, where uploads go, and `attachments/`, the
|
||||
receipt and invoice files. 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);
|
||||
@@ -111,13 +116,16 @@ services:
|
||||
|
||||
### Without Docker
|
||||
|
||||
Needs Python 3, `pdftotext` for PDF statements, and LibreOffice's UNO bridge for
|
||||
spreadsheet export and migration. On Debian/Ubuntu:
|
||||
Needs Python 3, `pdftotext` for PDF statements, LibreOffice's UNO bridge for
|
||||
spreadsheet export and migration, and Tesseract and Pillow to read receipts. On
|
||||
Debian/Ubuntu:
|
||||
|
||||
```bash
|
||||
sudo apt install libreoffice-calc python3-uno poppler-utils pipenv
|
||||
sudo apt install libreoffice-calc python3-uno poppler-utils tesseract-ocr python3-pil pipenv
|
||||
```
|
||||
|
||||
Without Tesseract, receipts can still be attached, but their details have to be typed in.
|
||||
|
||||
The virtualenv needs the system site packages, so it can see LibreOffice's `uno`:
|
||||
|
||||
```bash
|
||||
@@ -251,6 +259,74 @@ 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.
|
||||
|
||||
### Receipts and invoices
|
||||
|
||||
Keep the paperwork behind each transaction, such as the tax invoices you need to claim GST.
|
||||
|
||||
- **From a transaction:** click 📎 on a row of a month's sheet and add a photo or PDF.
|
||||
It's linked to that transaction straight away.
|
||||
- **In bulk:** open **Receipts**, then drop files, choose them, or **take a photo** on a
|
||||
phone. Each one is read, then listed with the bank transactions it probably belongs to:
|
||||
the same amount, within 7 days of the receipt's date, across all books. Click **Link**
|
||||
to confirm one. Receipts can wait unlinked until their statement is imported.
|
||||
- **Reading the details:** photos are straightened, evened out and read with Tesseract.
|
||||
PDFs use their text, or are OCR'd if they're scans. The supplier, date, total, GST,
|
||||
GST number and line items are filled in. Correct any of them in place, and the
|
||||
suggested matches update.
|
||||
- **Several pages:** a long receipt can take more than one photo, and an invoice can be
|
||||
more than one PDF. Add several files at once and choose **One receipt**, or use
|
||||
**+ Add page** on a receipt later. The pages are read together and the details are
|
||||
updated, except anything you corrected by hand. *Read the details again* (under the
|
||||
text read from the pages) also replaces your corrections. Remove a page with its ×.
|
||||
- **Updating the transaction:** when a receipt is linked, the app offers to set the
|
||||
transaction to *Incl GST* if the receipt shows GST. It also suggests a category from
|
||||
earlier transactions with the same reference, or from the supplier's name. Only what
|
||||
you tick is changed.
|
||||
|
||||
JPEG, PNG, WebP and PDF files up to 20 MB are accepted. HEIC photos aren't: set an
|
||||
iPhone's camera to *Most Compatible* or share the photo as JPEG. Files are stored in
|
||||
`data/attachments`. Deleting a receipt removes its files. Deleting a transaction or book
|
||||
leaves its receipts in the list, unlinked.
|
||||
|
||||
### Sending invoices
|
||||
|
||||
**Invoices** makes the invoices you send to customers, as PDFs.
|
||||
|
||||
- **Business details:** set your name, address, GST number, bank account, logo (PNG or
|
||||
JPEG), payment terms and invoice numbering once. The logo is stored in `data/invoices`.
|
||||
- **Customers:** saved for reuse. An invoice keeps the customer's details from when it
|
||||
was made, so later edits to the customer don't change invoices already sent.
|
||||
- **Numbers:** invoices are numbered in order (`INV-0001`, ...) when they're created.
|
||||
Numbers are never reused, and a voided invoice keeps its number.
|
||||
- **GST:** prices are entered excluding GST. Untick *GST* on a line for items with no GST
|
||||
(for example costs passed on at cost). GST is 15% of the taxable lines, rounded once per
|
||||
invoice. With a GST number, invoices that charge GST are titled *Tax invoice*.
|
||||
- **Status:** a draft can be edited. Once it's marked sent, only its notes can change. Move
|
||||
it back to draft to correct it, or void it.
|
||||
- **Paid:** marking an invoice paid records the money in the book for the payment date,
|
||||
as *Sales*. If that bank statement is already imported, pick the matching deposit, and
|
||||
the bank line is used instead of adding a new one. Otherwise a new row is added, and a
|
||||
statement imported later recognises it (same date and amount) as already in the book.
|
||||
An invoice with GST and non-GST items becomes two rows, *Incl GST* and *Excl GST*, so
|
||||
the GST return is right. *Undo payment* removes the rows it added, or puts a matched bank
|
||||
line back as it was.
|
||||
|
||||
### Mileage
|
||||
|
||||
**Mileage** is a vehicle logbook. It works out the claim at IRD's kilometre rates.
|
||||
|
||||
- Add your vehicles, then log each trip: date, from and to, purpose, customer, and either
|
||||
both odometer readings or the distance. The next trip's start reading defaults to the
|
||||
last one. Readings that overlap another trip are saved with a warning.
|
||||
- Untick *Business trip* for private use, so the business-use percentage is shown.
|
||||
- Enter IRD's tier 1 and tier 2 rates for each income year (April to March). They
|
||||
change every year, so none are built in. Tier 1 covers each vehicle's first 14,000
|
||||
business km in the year, and tier 2 the rest.
|
||||
- **Export logbook (CSV)** downloads the year's trips for your records.
|
||||
|
||||
Mileage is a report only. It doesn't add anything to the cashbook or the IR10, so
|
||||
reimbursements you pay yourself from the bank aren't counted twice.
|
||||
|
||||
### Books kept in spreadsheets
|
||||
|
||||
Earlier versions kept each book as an `.ods` file in `data/`. At startup the app copies
|
||||
@@ -303,11 +379,13 @@ src/
|
||||
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
|
||||
routers/ auth, users, books, statements, attachments, invoices, mileage and page routes
|
||||
services/ accounts, statement files, receipt OCR, invoice PDFs, LibreOffice
|
||||
db/, schemas/ account tables (SQLAlchemy) and request/response models
|
||||
static/ the web pages (cashbook, sign-in, register, users)
|
||||
static/ the web pages (cashbook, sign-in, register, users), invoices.js, mileage.js
|
||||
ledger.py SQLite storage: books, transactions, GST and summaries
|
||||
invoices.py customers, invoices, numbering, GST and recording payments
|
||||
mileage.py vehicle logbook and the kilometre-rate claim
|
||||
tax.py IR10 and IR4 figures
|
||||
statements.py reads ANZ CSV and PDF statements, GST/category guesses
|
||||
export.py exports a book to .xlsx/.ods/.csv (also a CLI)
|
||||
@@ -318,7 +396,7 @@ src/
|
||||
anz_import.py imports statements (CLI)
|
||||
template.ods blank cashbook template, used for exports
|
||||
tests/ pytest suite (pipenv run test)
|
||||
data/ cashbook.db and statements (git-ignored)
|
||||
data/ cashbook.db, statements and attachments (git-ignored)
|
||||
```
|
||||
|
||||
`data/` is in `.gitignore`: books, backups and bank statements are never committed.
|
||||
|
||||
@@ -11,10 +11,14 @@ 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.attachments import router as attachments_router
|
||||
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.invoices import router as invoices_router
|
||||
from cashbook_app.routers.mileage import router as mileage_router
|
||||
from cashbook_app.routers.statements import router as statements_router
|
||||
from cashbook_app.routers.suppliers import router as suppliers_router
|
||||
from cashbook_app.routers.users_admin import router as users_router
|
||||
from cashbook_app.services import accounts, spreadsheets
|
||||
|
||||
@@ -119,6 +123,10 @@ def create_app() -> FastAPI:
|
||||
app.include_router(users_router)
|
||||
app.include_router(books_router)
|
||||
app.include_router(statements_router)
|
||||
app.include_router(attachments_router)
|
||||
app.include_router(invoices_router)
|
||||
app.include_router(mileage_router)
|
||||
app.include_router(suppliers_router)
|
||||
return app
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from urllib.parse import quote
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
from fastapi.responses import FileResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
import ledger
|
||||
from cashbook_app.deps import get_ledger, require_user
|
||||
from cashbook_app.services import receipts
|
||||
from cashbook_app.services.receipts import AttachmentError
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["attachments"], dependencies=[Depends(require_user)])
|
||||
|
||||
|
||||
class LinkRequest(BaseModel):
|
||||
row: int
|
||||
# None: just link, and return the GST/category the receipt suggests for the transaction
|
||||
apply: dict[str, str] | None = None
|
||||
|
||||
|
||||
class AttachmentUpdate(BaseModel):
|
||||
kind: str | None = None
|
||||
vendor: str | None = Field(None, max_length=200)
|
||||
doc_date: str | None = None
|
||||
total: float | None = None
|
||||
gst: float | None = None
|
||||
gst_number: str | None = Field(None, max_length=20)
|
||||
supplier_id: int | None = None
|
||||
|
||||
|
||||
def _call(fn, *args, **kwargs):
|
||||
try:
|
||||
return fn(*args, **kwargs)
|
||||
except ledger.NotFound as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
except (ValueError, AttachmentError) as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
def _with_suggestions(conn, a):
|
||||
a = {**a, "suggestions": ledger.suggest_transactions(conn, a)}
|
||||
if a["transaction_id"] is not None:
|
||||
a["proposed"] = ledger.propose_changes(conn, a, a["transaction_id"])
|
||||
return a
|
||||
|
||||
|
||||
async def _receive(request: Request, filename: str, conn):
|
||||
"""Store an uploaded photo/PDF and read it; returns (file details, OCR result)."""
|
||||
if int(request.headers.get("content-length") or 0) > receipts.MAX_UPLOAD:
|
||||
raise HTTPException(status_code=413, detail="the file is too large (20 MB at most)")
|
||||
data = await request.body()
|
||||
saved = _call(receipts.save_upload, filename, data)
|
||||
if ledger.file_exists(conn, saved["sha256"]):
|
||||
receipts.remove_file(saved["stored_name"])
|
||||
raise HTTPException(status_code=400, detail=f"{saved['filename']} has already been added")
|
||||
ocr = await run_in_threadpool(receipts.read_document, receipts.file_path(saved["stored_name"]), saved["mime"])
|
||||
return saved, ocr
|
||||
|
||||
|
||||
def _keep(saved, fn, *args, **kwargs):
|
||||
"""Record the stored file with fn, removing the file again if that fails."""
|
||||
try:
|
||||
return _call(fn, *args, **kwargs)
|
||||
except Exception:
|
||||
receipts.remove_file(saved["stored_name"])
|
||||
raise
|
||||
|
||||
|
||||
@router.post("/attachments")
|
||||
async def upload(request: Request, filename: str, kind: str = "receipt", row: int | None = None,
|
||||
conn=Depends(get_ledger)):
|
||||
saved, ocr = await _receive(request, filename, conn)
|
||||
a = _keep(saved, ledger.add_attachment, conn, file=saved, kind=kind, transaction_id=row, ocr=ocr)
|
||||
return {**_with_suggestions(conn, a), "ocr_error": ocr.get("error")}
|
||||
|
||||
|
||||
@router.post("/attachments/{attachment_id}/pages")
|
||||
async def add_page(attachment_id: int, request: Request, filename: str, conn=Depends(get_ledger)):
|
||||
"""Another photo of the same receipt (e.g. the bottom half) or another page of an invoice.
|
||||
The pages are read together, and details still missing are filled in."""
|
||||
_call(ledger.get_attachment, conn, attachment_id)
|
||||
saved, ocr = await _receive(request, filename, conn)
|
||||
_keep(saved, ledger.add_page, conn, attachment_id, saved, ocr.get("text"))
|
||||
found = receipts.parse_receipt(ledger.attachment_text(conn, attachment_id))
|
||||
a = ledger.apply_ocr(conn, attachment_id, found)
|
||||
return {**_with_suggestions(conn, a), "ocr_error": ocr.get("error")}
|
||||
|
||||
|
||||
@router.delete("/attachments/{attachment_id}/pages/{file_id}")
|
||||
def remove_page(attachment_id: int, file_id: int, conn=Depends(get_ledger)):
|
||||
receipts.remove_file(_call(ledger.remove_page, conn, attachment_id, file_id))
|
||||
found = receipts.parse_receipt(ledger.attachment_text(conn, attachment_id))
|
||||
return _with_suggestions(conn, ledger.apply_ocr(conn, attachment_id, found))
|
||||
|
||||
|
||||
@router.post("/attachments/{attachment_id}/reread")
|
||||
def reread(attachment_id: int, conn=Depends(get_ledger)):
|
||||
"""Replace the details with what's read from all the pages, undoing corrections."""
|
||||
_call(ledger.get_attachment, conn, attachment_id)
|
||||
found = receipts.parse_receipt(ledger.attachment_text(conn, attachment_id))
|
||||
return _with_suggestions(conn, ledger.apply_ocr(conn, attachment_id, found, overwrite=True))
|
||||
|
||||
|
||||
@router.get("/attachments")
|
||||
def list_attachments(unmatched: bool = False, supplier: int | None = None, conn=Depends(get_ledger)):
|
||||
return [_with_suggestions(conn, a) for a in ledger.list_attachments(conn, unmatched, supplier)]
|
||||
|
||||
|
||||
@router.get("/attachments/{attachment_id}")
|
||||
def get_attachment(attachment_id: int, conn=Depends(get_ledger)):
|
||||
return _with_suggestions(conn, _call(ledger.get_attachment, conn, attachment_id))
|
||||
|
||||
|
||||
def _send(f):
|
||||
path = _call(receipts.file_path, f["stored_name"])
|
||||
if not path.exists():
|
||||
raise HTTPException(status_code=404, detail="the file is missing from data/attachments")
|
||||
return FileResponse(path, media_type=f["mime"], headers={
|
||||
"Content-Disposition": f"inline; filename*=UTF-8''{quote(f['filename'])}"})
|
||||
|
||||
|
||||
@router.get("/attachments/{attachment_id}/file")
|
||||
def download(attachment_id: int, conn=Depends(get_ledger)):
|
||||
"""The first page."""
|
||||
return _send(_call(ledger.get_file, conn, attachment_id))
|
||||
|
||||
|
||||
@router.get("/attachments/{attachment_id}/files/{file_id}")
|
||||
def download_page(attachment_id: int, file_id: int, conn=Depends(get_ledger)):
|
||||
return _send(_call(ledger.get_file, conn, attachment_id, file_id))
|
||||
|
||||
|
||||
@router.patch("/attachments/{attachment_id}")
|
||||
def update(attachment_id: int, body: AttachmentUpdate, conn=Depends(get_ledger)):
|
||||
fields = body.model_dump(exclude_unset=True)
|
||||
for k in ("total", "gst"):
|
||||
if k in fields:
|
||||
v = fields.pop(k)
|
||||
fields[f"{k}_cents"] = ledger.cents(abs(v)) if v is not None else None
|
||||
a = _call(ledger.update_attachment, conn, attachment_id, **fields)
|
||||
return _with_suggestions(conn, a)
|
||||
|
||||
|
||||
@router.post("/attachments/{attachment_id}/link")
|
||||
def link(attachment_id: int, body: LinkRequest, conn=Depends(get_ledger)):
|
||||
apply = body.apply or {}
|
||||
if set(apply) - {"gst", "category"}:
|
||||
raise HTTPException(status_code=400, detail="only gst and category can be applied")
|
||||
a = _call(ledger.link_attachment, conn, attachment_id, body.row,
|
||||
gst=apply.get("gst"), category=apply.get("category"))
|
||||
return _with_suggestions(conn, a)
|
||||
|
||||
|
||||
@router.post("/attachments/{attachment_id}/unlink")
|
||||
def unlink(attachment_id: int, conn=Depends(get_ledger)):
|
||||
return _with_suggestions(conn, _call(ledger.unlink_attachment, conn, attachment_id))
|
||||
|
||||
|
||||
@router.delete("/attachments/{attachment_id}")
|
||||
def delete(attachment_id: int, conn=Depends(get_ledger)):
|
||||
for name in _call(ledger.delete_attachment, conn, attachment_id):
|
||||
receipts.remove_file(name)
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.get("/row/{row}/attachments")
|
||||
def row_attachments(row: int, conn=Depends(get_ledger)):
|
||||
return [_with_suggestions(conn, a) for a in _call(ledger.attachments_for, conn, row)]
|
||||
@@ -0,0 +1,192 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from urllib.parse import quote
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
||||
from fastapi.responses import FileResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
import invoices
|
||||
import ledger
|
||||
from cashbook_app.deps import get_ledger, require_user
|
||||
from cashbook_app.services import invoice_pdf
|
||||
from cashbook_app.services.invoice_pdf import LogoError
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["invoices"], dependencies=[Depends(require_user)])
|
||||
|
||||
|
||||
class BusinessUpdate(BaseModel):
|
||||
name: str | None = Field(None, max_length=200)
|
||||
address: str | None = Field(None, max_length=500)
|
||||
email: str | None = Field(None, max_length=200)
|
||||
phone: str | None = Field(None, max_length=50)
|
||||
gst_number: str | None = Field(None, max_length=20)
|
||||
bank_account: str | None = Field(None, max_length=50)
|
||||
invoice_prefix: str | None = Field(None, max_length=20)
|
||||
next_number: int | None = None
|
||||
payment_terms_days: int | None = Field(None, le=365)
|
||||
footer: str | None = Field(None, max_length=1000)
|
||||
|
||||
|
||||
class Customer(BaseModel):
|
||||
name: str = Field(min_length=1, max_length=200)
|
||||
email: str = Field("", max_length=200)
|
||||
address: str = Field("", max_length=500)
|
||||
|
||||
|
||||
class Line(BaseModel):
|
||||
description: str = Field(max_length=500)
|
||||
qty: float = 1
|
||||
unit: float
|
||||
taxable: bool = True
|
||||
|
||||
|
||||
class InvoiceCreate(BaseModel):
|
||||
customer_id: int
|
||||
issue_date: str
|
||||
due_date: str | None = None
|
||||
lines: list[Line]
|
||||
notes: str = Field("", max_length=2000)
|
||||
|
||||
|
||||
class InvoiceUpdate(BaseModel):
|
||||
customer_id: int | None = None
|
||||
issue_date: str | None = None
|
||||
due_date: str | None = None
|
||||
lines: list[Line] | None = None
|
||||
notes: str | None = Field(None, max_length=2000)
|
||||
|
||||
|
||||
class StatusChange(BaseModel):
|
||||
status: str
|
||||
|
||||
|
||||
class Payment(BaseModel):
|
||||
date: str
|
||||
row: int | None = None # an imported bank line to record it on
|
||||
|
||||
|
||||
def _call(fn, *args, **kwargs):
|
||||
try:
|
||||
return fn(*args, **kwargs)
|
||||
except ledger.NotFound as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
except (ValueError, LogoError) as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
# --- business ---------------------------------------------------------------
|
||||
|
||||
@router.get("/business")
|
||||
def get_business(conn=Depends(get_ledger)):
|
||||
return invoices.get_business(conn)
|
||||
|
||||
|
||||
@router.put("/business")
|
||||
def update_business(body: BusinessUpdate, conn=Depends(get_ledger)):
|
||||
return _call(invoices.update_business, conn, **body.model_dump(exclude_unset=True, exclude_none=True))
|
||||
|
||||
|
||||
@router.post("/business/logo")
|
||||
async def upload_logo(request: Request, conn=Depends(get_ledger)):
|
||||
if int(request.headers.get("content-length") or 0) > invoice_pdf.MAX_LOGO:
|
||||
raise HTTPException(status_code=413, detail="the logo is too large (2 MB at most)")
|
||||
name = _call(invoice_pdf.save_logo, await request.body())
|
||||
invoices.set_logo(conn, name)
|
||||
return invoices.get_business(conn)
|
||||
|
||||
|
||||
@router.get("/business/logo")
|
||||
def get_logo(conn=Depends(get_ledger)):
|
||||
path = invoice_pdf.logo_path(invoices.get_business(conn)["logo_file"])
|
||||
if path is None:
|
||||
raise HTTPException(status_code=404, detail="no logo")
|
||||
return FileResponse(path)
|
||||
|
||||
|
||||
@router.delete("/business/logo")
|
||||
def delete_logo(conn=Depends(get_ledger)):
|
||||
invoice_pdf.remove_logo(invoices.set_logo(conn, None))
|
||||
return invoices.get_business(conn)
|
||||
|
||||
|
||||
# --- customers --------------------------------------------------------------
|
||||
|
||||
@router.get("/customers")
|
||||
def list_customers(conn=Depends(get_ledger)):
|
||||
return invoices.list_customers(conn)
|
||||
|
||||
|
||||
@router.post("/customers")
|
||||
def create_customer(body: Customer, conn=Depends(get_ledger)):
|
||||
return _call(invoices.create_customer, conn, **body.model_dump())
|
||||
|
||||
|
||||
@router.put("/customers/{customer_id}")
|
||||
def update_customer(customer_id: int, body: Customer, conn=Depends(get_ledger)):
|
||||
return _call(invoices.update_customer, conn, customer_id, **body.model_dump())
|
||||
|
||||
|
||||
@router.delete("/customers/{customer_id}")
|
||||
def delete_customer(customer_id: int, conn=Depends(get_ledger)):
|
||||
_call(invoices.delete_customer, conn, customer_id)
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
# --- invoices ---------------------------------------------------------------
|
||||
|
||||
def _lines(lines):
|
||||
return None if lines is None else [l.model_dump() for l in lines]
|
||||
|
||||
|
||||
@router.get("/invoices")
|
||||
def list_invoices(status: str | None = None, customer: int | None = None, conn=Depends(get_ledger)):
|
||||
return invoices.list_invoices(conn, status, customer)
|
||||
|
||||
|
||||
@router.post("/invoices")
|
||||
def create_invoice(body: InvoiceCreate, conn=Depends(get_ledger)):
|
||||
return _call(invoices.create_invoice, conn, body.customer_id, body.issue_date, _lines(body.lines),
|
||||
due_date=body.due_date, notes=body.notes)
|
||||
|
||||
|
||||
@router.get("/invoices/{invoice_id}")
|
||||
def get_invoice(invoice_id: int, conn=Depends(get_ledger)):
|
||||
return _call(invoices.get_invoice, conn, invoice_id)
|
||||
|
||||
|
||||
@router.put("/invoices/{invoice_id}")
|
||||
def update_invoice(invoice_id: int, body: InvoiceUpdate, conn=Depends(get_ledger)):
|
||||
return _call(invoices.update_invoice, conn, invoice_id, customer_id=body.customer_id,
|
||||
issue_date=body.issue_date, due_date=body.due_date, lines=_lines(body.lines), notes=body.notes)
|
||||
|
||||
|
||||
@router.post("/invoices/{invoice_id}/status")
|
||||
def set_status(invoice_id: int, body: StatusChange, conn=Depends(get_ledger)):
|
||||
return _call(invoices.set_status, conn, invoice_id, body.status)
|
||||
|
||||
|
||||
@router.get("/invoices/{invoice_id}/matches")
|
||||
def bank_matches(invoice_id: int, date: str, conn=Depends(get_ledger)):
|
||||
return _call(invoices.bank_matches, conn, invoice_id, date)
|
||||
|
||||
|
||||
@router.post("/invoices/{invoice_id}/pay")
|
||||
def pay(invoice_id: int, body: Payment, conn=Depends(get_ledger)):
|
||||
return _call(invoices.mark_paid, conn, invoice_id, body.date, row=body.row)
|
||||
|
||||
|
||||
@router.post("/invoices/{invoice_id}/unpay")
|
||||
def unpay(invoice_id: int, conn=Depends(get_ledger)):
|
||||
return _call(invoices.unmark_paid, conn, invoice_id)
|
||||
|
||||
|
||||
@router.get("/invoices/{invoice_id}/pdf")
|
||||
def invoice_pdf_file(invoice_id: int, conn=Depends(get_ledger)):
|
||||
inv = _call(invoices.get_invoice, conn, invoice_id)
|
||||
data = invoice_pdf.render(inv, invoices.get_business(conn))
|
||||
filename = f"{inv['number']} {inv['customer_name']}.pdf"
|
||||
safe = re.sub(r"[^\w.-]", "_", filename)
|
||||
return Response(data, media_type="application/pdf", headers={
|
||||
"Content-Disposition": f"attachment; filename=\"{safe}\"; filename*=UTF-8''{quote(filename)}"})
|
||||
@@ -0,0 +1,113 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
import ledger
|
||||
import mileage
|
||||
from cashbook_app.deps import get_ledger, require_user
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["mileage"], dependencies=[Depends(require_user)])
|
||||
|
||||
|
||||
class VehicleCreate(BaseModel):
|
||||
name: str = Field(min_length=1, max_length=100)
|
||||
rego: str = Field("", max_length=20)
|
||||
|
||||
|
||||
class VehicleUpdate(BaseModel):
|
||||
name: str | None = Field(None, max_length=100)
|
||||
rego: str | None = Field(None, max_length=20)
|
||||
active: bool | None = None
|
||||
|
||||
|
||||
class Trip(BaseModel):
|
||||
vehicle_id: int
|
||||
date: str
|
||||
from_place: str = ""
|
||||
to_place: str = ""
|
||||
purpose: str = ""
|
||||
customer: str = ""
|
||||
odo_start: int | None = Field(None, ge=0)
|
||||
odo_end: int | None = Field(None, ge=0)
|
||||
km: float | None = None
|
||||
business: bool = True
|
||||
|
||||
|
||||
class Rate(BaseModel):
|
||||
tier1: float
|
||||
tier2: float
|
||||
tier1_limit_km: int = 14000
|
||||
|
||||
|
||||
def _call(fn, *args, **kwargs):
|
||||
try:
|
||||
return fn(*args, **kwargs)
|
||||
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))
|
||||
|
||||
|
||||
@router.get("/vehicles")
|
||||
def list_vehicles(conn=Depends(get_ledger)):
|
||||
return mileage.list_vehicles(conn)
|
||||
|
||||
|
||||
@router.post("/vehicles")
|
||||
def create_vehicle(body: VehicleCreate, conn=Depends(get_ledger)):
|
||||
return _call(mileage.create_vehicle, conn, body.name, body.rego)
|
||||
|
||||
|
||||
@router.put("/vehicles/{vehicle_id}")
|
||||
def update_vehicle(vehicle_id: int, body: VehicleUpdate, conn=Depends(get_ledger)):
|
||||
return _call(mileage.update_vehicle, conn, vehicle_id, **body.model_dump(exclude_unset=True))
|
||||
|
||||
|
||||
@router.delete("/vehicles/{vehicle_id}")
|
||||
def delete_vehicle(vehicle_id: int, conn=Depends(get_ledger)):
|
||||
_call(mileage.delete_vehicle, conn, vehicle_id)
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.get("/trips")
|
||||
def list_trips(year: int | None = None, vehicle: int | None = None, conn=Depends(get_ledger)):
|
||||
return mileage.list_trips(conn, year, vehicle)
|
||||
|
||||
|
||||
@router.post("/trips")
|
||||
def add_trip(body: Trip, conn=Depends(get_ledger)):
|
||||
return _call(mileage.add_trip, conn, **body.model_dump())
|
||||
|
||||
|
||||
@router.put("/trips/{trip_id}")
|
||||
def update_trip(trip_id: int, body: Trip, conn=Depends(get_ledger)):
|
||||
return _call(mileage.update_trip, conn, trip_id, **body.model_dump())
|
||||
|
||||
|
||||
@router.delete("/trips/{trip_id}")
|
||||
def delete_trip(trip_id: int, conn=Depends(get_ledger)):
|
||||
_call(mileage.delete_trip, conn, trip_id)
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.get("/mileage/rates/{year}")
|
||||
def get_rate(year: int, conn=Depends(get_ledger)):
|
||||
return mileage.get_rate(conn, year)
|
||||
|
||||
|
||||
@router.put("/mileage/rates/{year}")
|
||||
def set_rate(year: int, body: Rate, conn=Depends(get_ledger)):
|
||||
return _call(mileage.set_rate, conn, year, body.tier1, body.tier2, body.tier1_limit_km)
|
||||
|
||||
|
||||
@router.get("/mileage/report")
|
||||
def report(year: int, conn=Depends(get_ledger)):
|
||||
return mileage.report(conn, year)
|
||||
|
||||
|
||||
@router.get("/mileage/export")
|
||||
def export(year: int, conn=Depends(get_ledger)):
|
||||
filename = f"logbook-{year}-{(year + 1) % 100:02d}.csv"
|
||||
return Response(mileage.export_csv(conn, year), media_type="text/csv; charset=utf-8",
|
||||
headers={"Content-Disposition": f"attachment; filename=\"{filename}\""})
|
||||
@@ -0,0 +1,52 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
import ledger
|
||||
import suppliers
|
||||
from cashbook_app.deps import get_ledger, require_user
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["suppliers"], dependencies=[Depends(require_user)])
|
||||
|
||||
|
||||
class Supplier(BaseModel):
|
||||
name: str = Field(min_length=1, max_length=200)
|
||||
email: str = Field("", max_length=200)
|
||||
address: str = Field("", max_length=500)
|
||||
gst_number: str = Field("", max_length=20)
|
||||
|
||||
|
||||
def _call(fn, *args, **kwargs):
|
||||
try:
|
||||
return fn(*args, **kwargs)
|
||||
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))
|
||||
|
||||
|
||||
@router.get("/suppliers")
|
||||
def list_suppliers(conn=Depends(get_ledger)):
|
||||
return suppliers.list_suppliers(conn)
|
||||
|
||||
|
||||
@router.post("/suppliers")
|
||||
def create_supplier(body: Supplier, conn=Depends(get_ledger)):
|
||||
return _call(suppliers.create_supplier, conn, **body.model_dump())
|
||||
|
||||
|
||||
@router.get("/suppliers/{supplier_id}")
|
||||
def get_supplier(supplier_id: int, conn=Depends(get_ledger)):
|
||||
return _call(suppliers.get_supplier, conn, supplier_id)
|
||||
|
||||
|
||||
@router.put("/suppliers/{supplier_id}")
|
||||
def update_supplier(supplier_id: int, body: Supplier, conn=Depends(get_ledger)):
|
||||
return _call(suppliers.update_supplier, conn, supplier_id, **body.model_dump())
|
||||
|
||||
|
||||
@router.delete("/suppliers/{supplier_id}")
|
||||
def delete_supplier(supplier_id: int, conn=Depends(get_ledger)):
|
||||
_call(suppliers.delete_supplier, conn, supplier_id)
|
||||
return {"ok": True}
|
||||
@@ -0,0 +1,193 @@
|
||||
"""Invoice PDFs (fpdf2) and the logo they carry, kept in data/invoices."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
|
||||
from fpdf import FPDF
|
||||
|
||||
from cashbook_app import config
|
||||
|
||||
MAX_LOGO = 2 * 1024 * 1024
|
||||
LOGO_EXT = {"image/png": ".png", "image/jpeg": ".jpg"}
|
||||
LOGO_MAGIC = {b"\x89PNG\r\n\x1a\n": "image/png", b"\xff\xd8\xff": "image/jpeg"}
|
||||
FONT_DIR = Path("/usr/share/fonts/truetype/dejavu") # fonts-dejavu-core
|
||||
|
||||
|
||||
class LogoError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def invoices_dir() -> Path:
|
||||
path = config.data_dir() / "invoices"
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
return path
|
||||
|
||||
|
||||
def logo_path(logo_file: str | None) -> Path | None:
|
||||
if logo_file not in [f"logo{ext}" for ext in LOGO_EXT.values()]:
|
||||
return None
|
||||
path = invoices_dir() / logo_file
|
||||
return path if path.exists() else None
|
||||
|
||||
|
||||
def save_logo(data: bytes) -> str:
|
||||
"""Write the logo; returns its file name. Only PNG and JPEG, which fpdf can embed."""
|
||||
if not data:
|
||||
raise LogoError("the file is empty")
|
||||
if len(data) > MAX_LOGO:
|
||||
raise LogoError("the logo is too large (2 MB at most)")
|
||||
mime = next((m for magic, m in LOGO_MAGIC.items() if data.startswith(magic)), None)
|
||||
if mime is None:
|
||||
raise LogoError("the logo must be a PNG or JPEG image")
|
||||
name = f"logo{LOGO_EXT[mime]}"
|
||||
for ext in LOGO_EXT.values():
|
||||
(invoices_dir() / f"logo{ext}").unlink(missing_ok=True)
|
||||
(invoices_dir() / name).write_bytes(data)
|
||||
return name
|
||||
|
||||
|
||||
def remove_logo(logo_file: str | None) -> None:
|
||||
path = logo_path(logo_file)
|
||||
if path:
|
||||
path.unlink(missing_ok=True)
|
||||
|
||||
|
||||
def _money(x: float) -> str:
|
||||
return f"${x:,.2f}"
|
||||
|
||||
|
||||
def _qty(q: float) -> str:
|
||||
return f"{q:g}"
|
||||
|
||||
|
||||
def _date(iso: str) -> str:
|
||||
return date.fromisoformat(iso).strftime("%-d %B %Y")
|
||||
|
||||
|
||||
class _Pdf(FPDF):
|
||||
def __init__(self):
|
||||
super().__init__(format="A4")
|
||||
self.set_margins(18, 18, 18)
|
||||
self.set_auto_page_break(True, 20)
|
||||
if (FONT_DIR / "DejaVuSans.ttf").exists():
|
||||
self.add_font("body", "", str(FONT_DIR / "DejaVuSans.ttf"))
|
||||
self.add_font("body", "B", str(FONT_DIR / "DejaVuSans-Bold.ttf"))
|
||||
self.family = "body"
|
||||
else:
|
||||
self.family = "helvetica" # latin-1 only
|
||||
|
||||
def font(self, size, style=""):
|
||||
self.set_font(self.family, style, size)
|
||||
|
||||
|
||||
def render(invoice: dict, business: dict) -> bytes:
|
||||
pdf = _Pdf()
|
||||
pdf.add_page()
|
||||
width = pdf.epw
|
||||
grey = (110, 110, 110)
|
||||
has_gst = invoice["gst"] > 0
|
||||
title = "TAX INVOICE" if has_gst and business["gst_number"] else "INVOICE"
|
||||
|
||||
# header: logo and business on the left, title and numbers on the right
|
||||
top = pdf.get_y()
|
||||
logo = logo_path(business.get("logo_file"))
|
||||
if logo:
|
||||
pdf.image(str(logo), x=pdf.l_margin, y=top, h=20, keep_aspect_ratio=True)
|
||||
pdf.set_y(top + 23)
|
||||
pdf.font(12, "B")
|
||||
pdf.multi_cell(width * 0.55, 6, business["name"] or "Your business name", new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.font(9)
|
||||
pdf.set_text_color(*grey)
|
||||
for line in [business["address"], business["phone"], business["email"],
|
||||
f"GST number: {business['gst_number']}" if business["gst_number"] else ""]:
|
||||
if line:
|
||||
pdf.multi_cell(width * 0.55, 4.6, line, new_x="LMARGIN", new_y="NEXT")
|
||||
left_bottom = pdf.get_y()
|
||||
|
||||
pdf.set_text_color(0)
|
||||
pdf.set_xy(pdf.l_margin + width * 0.55, top)
|
||||
pdf.font(18, "B")
|
||||
pdf.cell(width * 0.45, 9, title, align="R", new_x="LEFT", new_y="NEXT")
|
||||
pdf.font(9)
|
||||
for label, value in [("Invoice number", invoice["number"]), ("Date", _date(invoice["issue_date"])),
|
||||
("Due", _date(invoice["due_date"]))]:
|
||||
pdf.set_x(pdf.l_margin + width * 0.55)
|
||||
pdf.set_text_color(*grey)
|
||||
pdf.cell(width * 0.25, 5, label, align="R")
|
||||
pdf.set_text_color(0)
|
||||
pdf.cell(width * 0.20, 5, value, align="R", new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.set_y(max(left_bottom, pdf.get_y()) + 8)
|
||||
|
||||
# bill to
|
||||
pdf.font(8, "B")
|
||||
pdf.set_text_color(*grey)
|
||||
pdf.cell(0, 5, "BILL TO", new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.set_text_color(0)
|
||||
pdf.font(10, "B")
|
||||
pdf.multi_cell(width * 0.6, 5.5, invoice["customer_name"], new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.font(9)
|
||||
for line in [invoice["customer_address"], invoice["customer_email"]]:
|
||||
if line:
|
||||
pdf.multi_cell(width * 0.6, 4.6, line, new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.ln(7)
|
||||
|
||||
# lines
|
||||
mixed = any(not l["taxable"] for l in invoice["lines"]) and has_gst
|
||||
cols = [("Description", width * 0.52, "L"), ("Qty", width * 0.10, "R"),
|
||||
("Unit price", width * 0.18, "R"), ("Amount", width * 0.20, "R")]
|
||||
pdf.set_fill_color(240, 240, 240)
|
||||
pdf.font(9, "B")
|
||||
for name, w, align in cols:
|
||||
pdf.cell(w, 7, name, align=align, fill=True)
|
||||
pdf.ln()
|
||||
pdf.font(9)
|
||||
for l in invoice["lines"]:
|
||||
desc = l["description"] + (" *" if mixed and not l["taxable"] else "")
|
||||
y = pdf.get_y()
|
||||
pdf.multi_cell(cols[0][1], 6, desc, new_x="RIGHT", new_y="TOP")
|
||||
end_y = pdf.get_y() if pdf.get_y() > y else y
|
||||
pdf.set_xy(pdf.l_margin + cols[0][1], y)
|
||||
pdf.cell(cols[1][1], 6, _qty(l["qty"]), align="R")
|
||||
pdf.cell(cols[2][1], 6, _money(l["unit"]), align="R")
|
||||
pdf.cell(cols[3][1], 6, _money(l["amount"]), align="R", new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.set_y(max(end_y, pdf.get_y()))
|
||||
pdf.set_draw_color(225, 225, 225)
|
||||
pdf.line(pdf.l_margin, pdf.get_y(), pdf.l_margin + width, pdf.get_y())
|
||||
pdf.ln(3)
|
||||
|
||||
# totals
|
||||
label_w, value_w = width * 0.30, width * 0.20
|
||||
rows = [("Subtotal (excl. GST)", invoice["subtotal"])]
|
||||
if has_gst or business["gst_number"]:
|
||||
rows.append(("GST 15%" + (f" on {_money(invoice['taxable'])}" if mixed else ""), invoice["gst"]))
|
||||
for label, value in rows:
|
||||
pdf.set_x(pdf.l_margin + width - label_w - value_w)
|
||||
pdf.cell(label_w, 6, label, align="R")
|
||||
pdf.cell(value_w, 6, _money(value), align="R", new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.set_x(pdf.l_margin + width - label_w - value_w)
|
||||
pdf.font(11, "B")
|
||||
pdf.cell(label_w, 8, "Total NZD", align="R", border="T")
|
||||
pdf.cell(value_w, 8, _money(invoice["total"]), align="R", border="T", new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.font(9)
|
||||
if mixed:
|
||||
pdf.set_text_color(*grey)
|
||||
pdf.cell(0, 6, "* No GST on this item", new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.set_text_color(0)
|
||||
pdf.ln(8)
|
||||
|
||||
# payment details and notes
|
||||
if business["bank_account"]:
|
||||
pdf.font(9, "B")
|
||||
pdf.cell(0, 5, "Payment", new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.font(9)
|
||||
pdf.multi_cell(0, 5, f"Please pay by {_date(invoice['due_date'])} to bank account "
|
||||
f"{business['bank_account']}, using {invoice['number']} as the reference.",
|
||||
new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.ln(3)
|
||||
for text in [invoice["notes"], business["footer"]]:
|
||||
if text:
|
||||
pdf.set_text_color(*grey)
|
||||
pdf.multi_cell(0, 5, text, new_x="LMARGIN", new_y="NEXT")
|
||||
pdf.ln(2)
|
||||
return bytes(pdf.output())
|
||||
@@ -0,0 +1,297 @@
|
||||
"""Receipt and invoice files: storing uploads, and reading their details with local OCR.
|
||||
|
||||
Images are cleaned up with Pillow (if installed) and read by the `tesseract` command; PDFs use
|
||||
their text layer (`pdftotext`), or are rendered with `pdftoppm` and OCR'd when they're scans.
|
||||
Nothing leaves the machine. `parse_receipt` turns the text into vendor, date, total, GST etc.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import uuid
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
|
||||
from cashbook_app import config
|
||||
|
||||
MAX_UPLOAD = 20 * 1024 * 1024
|
||||
OCR_TIMEOUT = 90 # seconds per page
|
||||
MIME = {".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png", ".webp": "image/webp",
|
||||
".pdf": "application/pdf"}
|
||||
EXT_OF = {"image/jpeg": ".jpg", "image/png": ".png", "image/webp": ".webp", "application/pdf": ".pdf"}
|
||||
|
||||
|
||||
class AttachmentError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def attachments_dir() -> Path:
|
||||
path = config.data_dir() / "attachments"
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
return path
|
||||
|
||||
|
||||
def file_path(stored_name: str) -> Path:
|
||||
# stored names are ours (hex + extension), but don't trust them blindly
|
||||
if not re.fullmatch(r"[0-9a-f-]+\.(jpg|png|webp|pdf)", stored_name):
|
||||
raise AttachmentError("bad file name")
|
||||
return attachments_dir() / stored_name
|
||||
|
||||
|
||||
def sniff(data: bytes) -> str | None:
|
||||
"""The file's real type from its first bytes."""
|
||||
if data.startswith(b"\xff\xd8\xff"):
|
||||
return "image/jpeg"
|
||||
if data.startswith(b"\x89PNG\r\n\x1a\n"):
|
||||
return "image/png"
|
||||
if data[:4] == b"RIFF" and data[8:12] == b"WEBP":
|
||||
return "image/webp"
|
||||
if data.startswith(b"%PDF"):
|
||||
return "application/pdf"
|
||||
return None
|
||||
|
||||
|
||||
def save_upload(filename: str, data: bytes) -> dict:
|
||||
"""Write an uploaded receipt/invoice into data/attachments; returns its details."""
|
||||
if not data:
|
||||
raise AttachmentError("the file is empty")
|
||||
if len(data) > MAX_UPLOAD:
|
||||
raise AttachmentError("the file is too large (20 MB at most)")
|
||||
mime = sniff(data)
|
||||
if mime is None:
|
||||
if data[4:12] in (b"ftypheic", b"ftypheix", b"ftypmif1", b"ftyphevc"):
|
||||
raise AttachmentError("HEIC photos aren't supported; set your camera to JPEG (Most Compatible) "
|
||||
"or export the photo as JPEG")
|
||||
raise AttachmentError("only JPEG, PNG or WebP photos and PDF files can be attached")
|
||||
sha = hashlib.sha256(data).hexdigest()
|
||||
stored = f"{sha[:16]}-{uuid.uuid4().hex[:8]}{EXT_OF[mime]}"
|
||||
dest = attachments_dir() / stored
|
||||
tmp = dest.with_name(f".upload-{uuid.uuid4().hex}")
|
||||
tmp.write_bytes(data)
|
||||
os.replace(tmp, dest)
|
||||
name = os.path.basename(filename.replace("\\", "/")).strip() or f"receipt{EXT_OF[mime]}"
|
||||
return {"filename": name[:200], "stored_name": stored, "mime": mime, "sha256": sha, "size": len(data)}
|
||||
|
||||
|
||||
def remove_file(stored_name: str) -> None:
|
||||
try:
|
||||
file_path(stored_name).unlink(missing_ok=True)
|
||||
except AttachmentError:
|
||||
pass
|
||||
|
||||
|
||||
# --- OCR --------------------------------------------------------------------
|
||||
|
||||
def _run(cmd: list[str], data: bytes | None = None) -> str:
|
||||
res = subprocess.run(cmd, input=data, capture_output=True, timeout=OCR_TIMEOUT,
|
||||
env={**os.environ, "OMP_THREAD_LIMIT": "1"})
|
||||
if res.returncode != 0:
|
||||
err = res.stderr.decode(errors="replace").strip().splitlines()
|
||||
raise AttachmentError(f"{cmd[0]}: {err[-1] if err else 'failed'}")
|
||||
return res.stdout.decode("utf-8", errors="replace")
|
||||
|
||||
|
||||
def prepare_image(data: bytes) -> bytes:
|
||||
"""Make a phone photo easier to read: upright, grey, a sensible size, even lighting, high contrast."""
|
||||
try:
|
||||
from PIL import Image, ImageChops, ImageFilter, ImageOps
|
||||
except ImportError:
|
||||
return data
|
||||
img = ImageOps.exif_transpose(Image.open(io.BytesIO(data))).convert("L")
|
||||
longest = max(img.size)
|
||||
if longest < 1600 or longest > 3600:
|
||||
scale = 2400 / longest
|
||||
img = img.resize((round(img.width * scale), round(img.height * scale)), Image.LANCZOS)
|
||||
# flatten shadows and uneven light: subtract the text from a blurred copy of the page
|
||||
background = img.filter(ImageFilter.GaussianBlur(radius=max(img.size) // 60))
|
||||
img = ImageOps.invert(ImageChops.subtract(background, img))
|
||||
img = ImageOps.autocontrast(img, cutoff=1).filter(ImageFilter.SHARPEN)
|
||||
out = io.BytesIO()
|
||||
img.save(out, format="PNG")
|
||||
return out.getvalue()
|
||||
|
||||
|
||||
def ocr_image(data: bytes) -> str:
|
||||
if not shutil.which("tesseract"):
|
||||
raise AttachmentError("tesseract isn't installed, so the text couldn't be read")
|
||||
# --psm 4: a single column of text of variable sizes, which suits till receipts
|
||||
return _run(["tesseract", "stdin", "stdout", "-l", "eng", "--psm", "4"], prepare_image(data))
|
||||
|
||||
|
||||
def pdf_text(path: Path) -> str:
|
||||
text = _run(["pdftotext", "-layout", str(path), "-"]) if shutil.which("pdftotext") else ""
|
||||
if len(text.strip()) >= 20:
|
||||
return text
|
||||
# a scanned PDF: render the first pages and OCR them
|
||||
if not shutil.which("pdftoppm"):
|
||||
raise AttachmentError("pdftoppm isn't installed, so the scanned PDF couldn't be read")
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
_run(["pdftoppm", "-r", "300", "-png", "-f", "1", "-l", "3", str(path), os.path.join(tmp, "p")])
|
||||
return "\n".join(ocr_image(p.read_bytes()) for p in sorted(Path(tmp).glob("p*.png")))
|
||||
|
||||
|
||||
def read_document(path: Path, mime: str) -> dict:
|
||||
"""Text and details of a stored receipt; never raises, so an upload is kept even if OCR fails."""
|
||||
try:
|
||||
text = pdf_text(path) if mime == "application/pdf" else ocr_image(path.read_bytes())
|
||||
except (AttachmentError, OSError, subprocess.TimeoutExpired) as e:
|
||||
return {"status": "failed", "error": str(e) if not isinstance(e, subprocess.TimeoutExpired)
|
||||
else "reading the text took too long"}
|
||||
except Exception as e: # e.g. Pillow can't decode the image
|
||||
return {"status": "failed", "error": f"couldn't read the file: {e}"}
|
||||
return {"status": "done", "text": text, **parse_receipt(text)}
|
||||
|
||||
|
||||
# --- parsing ----------------------------------------------------------------
|
||||
|
||||
# amounts with two decimals; OCR often reads the point as a comma
|
||||
MONEY = re.compile(r"(?<![\d.,])-?\$?\s?(\d{1,3}(?:,\d{3})+|\d+)\s?[.,]\s?(\d{2})(?![\d%])")
|
||||
MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
|
||||
MON = r"(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*\.?"
|
||||
DATES = [
|
||||
(re.compile(r"\b(\d{4})-(\d{1,2})-(\d{1,2})\b"), "ymd"),
|
||||
(re.compile(r"\b(\d{1,2})[/.-](\d{1,2})[/.-](\d{4}|\d{2})\b"), "dmy"),
|
||||
(re.compile(rf"\b(\d{{1,2}})(?:st|nd|rd|th)?[\s-]+{MON},?[\s-]+(\d{{4}}|\d{{2}})\b", re.I), "dMy"),
|
||||
(re.compile(rf"\b{MON}\s+(\d{{1,2}})(?:st|nd|rd|th)?,?\s+(\d{{4}})\b", re.I), "Mdy"),
|
||||
]
|
||||
TOTAL_WORDS = re.compile(r"\b(total|amount due|balance due|amount payable|to pay)\b", re.I)
|
||||
NOT_TOTAL = re.compile(r"sub[\s-]?total|excl|before gst|total gst|gst total|savings|items?\b|qty", re.I)
|
||||
PAID_WORDS = re.compile(r"\b(eftpos|visa|mastercard|credit|debit|card|paid|purchase)\b", re.I)
|
||||
NOT_ITEM = re.compile(r"total|gst|eftpos|visa|mastercard|card|change|cash|tender|balance|rounding|paid|due|"
|
||||
r"payment|account|auth|ref\b|invoice|receipt|date|time|phone|tel\b|savings|discount", re.I)
|
||||
# 8 or 9 digits after "GST"/"IRD"; OCR sometimes adds stray marks between the groups
|
||||
GST_NUMBER = re.compile(r"(?:gst|ird)[^\d\n]{0,25}?(\d[\d\s/.-]{6,14}\d)\b", re.I)
|
||||
HEADER_WORDS = re.compile(r"tax invoice|receipt|invoice|gst|ird|phone|ph\b|tel\b|www\.|@|\bdate\b|"
|
||||
r"welcome|thank|customer copy|merchant", re.I)
|
||||
|
||||
|
||||
def amounts(line: str) -> list[int]:
|
||||
"""Money amounts on a line, in cents."""
|
||||
out = []
|
||||
for m in MONEY.finditer(line):
|
||||
whole = int(m.group(1).replace(",", ""))
|
||||
value = whole * 100 + int(m.group(2))
|
||||
out.append(-value if m.group(0).lstrip().startswith("-") else value)
|
||||
return out
|
||||
|
||||
|
||||
def _to_date(kind: str, g: tuple) -> date | None:
|
||||
try:
|
||||
if kind == "ymd":
|
||||
y, m, d = int(g[0]), int(g[1]), int(g[2])
|
||||
elif kind == "dmy":
|
||||
d, m, y = int(g[0]), int(g[1]), int(g[2])
|
||||
elif kind == "dMy":
|
||||
d, m, y = int(g[0]), MONTHS.index(g[1].lower()[:3]) + 1, int(g[2])
|
||||
else:
|
||||
m, d, y = MONTHS.index(g[0].lower()[:3]) + 1, int(g[1]), int(g[2])
|
||||
if y < 100:
|
||||
y += 2000
|
||||
found = date(y, m, d)
|
||||
except ValueError:
|
||||
return None
|
||||
return found if date(2000, 1, 1) <= found <= date.today().replace(year=date.today().year + 1) else None
|
||||
|
||||
|
||||
def find_date(lines: list[str]) -> str | None:
|
||||
"""The document's date: one on a line saying "date" first, skipping due dates."""
|
||||
candidates = []
|
||||
for i, line in enumerate(lines):
|
||||
if re.search(r"\bdue\b|expir", line, re.I):
|
||||
continue
|
||||
for pattern, kind in DATES:
|
||||
for m in pattern.finditer(line):
|
||||
d = _to_date(kind, m.groups())
|
||||
if d:
|
||||
candidates.append((0 if re.search(r"date", line, re.I) else 1, i, d))
|
||||
return min(candidates)[2].isoformat() if candidates else None
|
||||
|
||||
|
||||
def find_total(lines: list[str]) -> int | None:
|
||||
totals, paid = [], []
|
||||
for i, line in enumerate(lines):
|
||||
nxt = lines[i + 1] if i + 1 < len(lines) else ""
|
||||
found = amounts(line) or (amounts(nxt) if not re.search(r"[a-z]{3}", nxt, re.I) else [])
|
||||
found = [a for a in found if a > 0]
|
||||
if not found:
|
||||
continue
|
||||
if TOTAL_WORDS.search(line) and not NOT_TOTAL.search(line):
|
||||
totals.append(found[-1])
|
||||
elif PAID_WORDS.search(line):
|
||||
paid.append(found[-1])
|
||||
if totals:
|
||||
return max(totals) # e.g. "Total" beats a smaller "Total excl" the filter missed
|
||||
if paid:
|
||||
return max(paid)
|
||||
every = [a for line in lines for a in amounts(line) if a > 0]
|
||||
return max(every) if every else None
|
||||
|
||||
|
||||
def find_gst(lines: list[str], total: int | None) -> int | None:
|
||||
"""The GST amount: of the amounts on lines mentioning GST, the one nearest 3/23 of the total."""
|
||||
found = []
|
||||
for line in lines:
|
||||
if re.search(r"\bg\.?s\.?t\b", line, re.I) and not find_gst_number(line):
|
||||
found += [a for a in amounts(line) if a > 0]
|
||||
if not found:
|
||||
return None
|
||||
if total:
|
||||
expected = total * 3 / 23
|
||||
plausible = [a for a in found if a <= total * 0.2]
|
||||
return min(plausible, key=lambda a: abs(a - expected)) if plausible else None
|
||||
return min(found)
|
||||
|
||||
|
||||
def find_gst_number(text: str) -> str | None:
|
||||
for m in GST_NUMBER.finditer(text):
|
||||
digits = re.sub(r"\D", "", m.group(1))
|
||||
if len(digits) in (8, 9) and not re.search(r"\d\.\d\d(?!\d)", m.group(1)): # not amounts
|
||||
digits = digits.zfill(9)
|
||||
return f"{digits[:3]}-{digits[3:6]}-{digits[6:]}"
|
||||
return None
|
||||
|
||||
|
||||
def find_vendor(lines: list[str]) -> str | None:
|
||||
for line in lines[:8]:
|
||||
clean = re.sub(r"[^\w&'.,\- ]", " ", line)
|
||||
clean = re.sub(r"\s+", " ", clean).strip(" .,-")
|
||||
letters = sum(c.isalpha() for c in clean)
|
||||
if letters < 3 or letters < len(clean.replace(" ", "")) * 0.6 or HEADER_WORDS.search(clean):
|
||||
continue
|
||||
return clean[:80]
|
||||
return None
|
||||
|
||||
|
||||
def find_items(lines: list[str]) -> list[dict]:
|
||||
items = []
|
||||
for line in lines:
|
||||
if TOTAL_WORDS.search(line) and not NOT_TOTAL.search(line):
|
||||
break # items come before the total
|
||||
found = amounts(line)
|
||||
if not found or NOT_ITEM.search(line):
|
||||
continue
|
||||
first = MONEY.search(line)
|
||||
desc = line[:first.start()].strip(" .:$-*")
|
||||
desc = re.sub(r"^\d+\s*[xX@]\s*", "", desc)
|
||||
if sum(c.isalpha() for c in desc) < 2:
|
||||
continue
|
||||
items.append({"description": re.sub(r"\s+", " ", desc)[:100], "amount": found[-1] / 100})
|
||||
return items[:50]
|
||||
|
||||
|
||||
def parse_receipt(text: str) -> dict:
|
||||
lines = [line.strip() for line in text.splitlines() if line.strip()]
|
||||
total = find_total(lines)
|
||||
return {
|
||||
"vendor": find_vendor(lines),
|
||||
"date": find_date(lines),
|
||||
"total_cents": total,
|
||||
"gst_cents": find_gst(lines, total),
|
||||
"gst_number": find_gst_number(text),
|
||||
"items": find_items(lines),
|
||||
}
|
||||
+331
@@ -0,0 +1,331 @@
|
||||
"""Invoices sent to customers: the business they come from, customers, numbering, GST and payment.
|
||||
|
||||
Line prices are excluding GST. GST is 15% of the taxable lines' total, rounded half-up to
|
||||
the cent once per invoice. Marking an invoice paid records the money in the cashbook: it is
|
||||
matched to the bank line if the statement is already imported, or added as a new row that
|
||||
the statement import will then recognise. An invoice with taxable and non-taxable lines
|
||||
becomes two rows (Incl GST and Excl GST), since a cashbook row has one GST type.
|
||||
"""
|
||||
from datetime import date, timedelta
|
||||
from decimal import ROUND_HALF_UP, Decimal
|
||||
|
||||
import ledger
|
||||
|
||||
STATUSES = ("draft", "sent", "paid", "void")
|
||||
BUSINESS_FIELDS = ("name", "address", "email", "phone", "gst_number", "bank_account",
|
||||
"invoice_prefix", "next_number", "payment_terms_days", "footer")
|
||||
CUSTOMER_FIELDS = ("name", "email", "address")
|
||||
MATCH_DAYS = 7 # how far an imported bank line's date may be from the payment date
|
||||
|
||||
|
||||
# --- business ---------------------------------------------------------------
|
||||
|
||||
def get_business(conn):
|
||||
with conn:
|
||||
conn.execute("INSERT OR IGNORE INTO business (id) VALUES (1)")
|
||||
return dict(conn.execute("SELECT * FROM business WHERE id = 1").fetchone())
|
||||
|
||||
|
||||
def update_business(conn, **fields):
|
||||
get_business(conn)
|
||||
for k, v in fields.items():
|
||||
if k not in BUSINESS_FIELDS:
|
||||
raise ValueError(f"can't change {k}")
|
||||
if k in ("next_number", "payment_terms_days"):
|
||||
v = int(v)
|
||||
if v < (1 if k == "next_number" else 0):
|
||||
raise ValueError(f"{k.replace('_', ' ')} is too small")
|
||||
if k == "next_number" and conn.execute(
|
||||
"SELECT 1 FROM invoices WHERE number = ?", (_number(get_business(conn)["invoice_prefix"], v),)).fetchone():
|
||||
raise ValueError("an invoice already has that number")
|
||||
with conn:
|
||||
conn.execute(f"UPDATE business SET {k} = ? WHERE id = 1", (v.strip() if isinstance(v, str) else v,))
|
||||
return get_business(conn)
|
||||
|
||||
|
||||
def set_logo(conn, logo_file):
|
||||
"""Record the logo's file name; returns the previous one so the caller can remove it."""
|
||||
old = get_business(conn)["logo_file"]
|
||||
with conn:
|
||||
conn.execute("UPDATE business SET logo_file = ? WHERE id = 1", (logo_file,))
|
||||
return old
|
||||
|
||||
|
||||
def _number(prefix, n):
|
||||
return f"{prefix}{n:04d}"
|
||||
|
||||
|
||||
# --- customers --------------------------------------------------------------
|
||||
|
||||
def list_customers(conn):
|
||||
return [dict(r) for r in conn.execute(
|
||||
"SELECT c.*, COUNT(i.id) AS invoices FROM customers c LEFT JOIN invoices i ON i.customer_id = c.id "
|
||||
"GROUP BY c.id ORDER BY c.name COLLATE NOCASE")]
|
||||
|
||||
|
||||
def get_customer(conn, customer_id):
|
||||
c = conn.execute("SELECT * FROM customers WHERE id = ?", (customer_id,)).fetchone()
|
||||
if c is None:
|
||||
raise ledger.NotFound("no such customer")
|
||||
return dict(c)
|
||||
|
||||
|
||||
def _customer_fields(fields):
|
||||
out = {}
|
||||
for k, v in fields.items():
|
||||
if k not in CUSTOMER_FIELDS:
|
||||
raise ValueError(f"can't change {k}")
|
||||
out[k] = (v or "").strip()
|
||||
if "name" in out and not out["name"]:
|
||||
raise ValueError("the customer needs a name")
|
||||
return out
|
||||
|
||||
|
||||
def create_customer(conn, name, email="", address=""):
|
||||
f = _customer_fields({"name": name, "email": email, "address": address})
|
||||
with conn:
|
||||
cur = conn.execute("INSERT INTO customers (name, email, address) VALUES (?, ?, ?)",
|
||||
(f["name"], f["email"], f["address"]))
|
||||
return get_customer(conn, cur.lastrowid)
|
||||
|
||||
|
||||
def update_customer(conn, customer_id, **fields):
|
||||
get_customer(conn, customer_id)
|
||||
for k, v in _customer_fields(fields).items():
|
||||
with conn:
|
||||
conn.execute(f"UPDATE customers SET {k} = ? WHERE id = ?", (v, customer_id))
|
||||
return get_customer(conn, customer_id)
|
||||
|
||||
|
||||
def delete_customer(conn, customer_id):
|
||||
get_customer(conn, customer_id)
|
||||
if conn.execute("SELECT 1 FROM invoices WHERE customer_id = ?", (customer_id,)).fetchone():
|
||||
raise ValueError("this customer has invoices, so it can't be deleted")
|
||||
with conn:
|
||||
conn.execute("DELETE FROM customers WHERE id = ?", (customer_id,))
|
||||
|
||||
|
||||
# --- amounts ----------------------------------------------------------------
|
||||
|
||||
def line_cents(qty, unit_cents):
|
||||
return int((Decimal(str(qty)) * unit_cents).quantize(Decimal(1), rounding=ROUND_HALF_UP))
|
||||
|
||||
|
||||
def gst_on(taxable_cents):
|
||||
"""15% GST on an amount excluding GST, rounded half-up to the cent."""
|
||||
return (taxable_cents * 6 + 20) // 40
|
||||
|
||||
|
||||
def totals(lines):
|
||||
"""Subtotal, GST and total in cents, from lines with qty, unit_cents and taxable."""
|
||||
amounts = [(line_cents(l["qty"], l["unit_cents"]), l["taxable"]) for l in lines]
|
||||
taxable = sum(a for a, t in amounts if t)
|
||||
exempt = sum(a for a, t in amounts if not t)
|
||||
gst = gst_on(taxable)
|
||||
return {"subtotal": taxable + exempt, "taxable": taxable, "exempt": exempt, "gst": gst,
|
||||
"total": taxable + exempt + gst}
|
||||
|
||||
|
||||
# --- invoices ---------------------------------------------------------------
|
||||
|
||||
def _check_lines(lines):
|
||||
if not lines:
|
||||
raise ValueError("the invoice needs at least one line")
|
||||
out = []
|
||||
for l in lines:
|
||||
description = (l.get("description") or "").strip()
|
||||
if not description:
|
||||
raise ValueError("every line needs a description")
|
||||
qty = float(l.get("qty", 1))
|
||||
if qty <= 0:
|
||||
raise ValueError("quantities must be more than zero")
|
||||
out.append({"description": description[:500], "qty": qty,
|
||||
"unit_cents": ledger.cents(float(l.get("unit", 0))), "taxable": bool(l.get("taxable", True))})
|
||||
if totals(out)["total"] <= 0:
|
||||
raise ValueError("the invoice total must be more than zero")
|
||||
return out
|
||||
|
||||
|
||||
def _check_dates(issue_date, due_date, terms):
|
||||
issued = date.fromisoformat(issue_date) # ValueError if it isn't a date
|
||||
due = date.fromisoformat(due_date) if due_date else issued + timedelta(days=terms)
|
||||
if due < issued:
|
||||
raise ValueError("the due date is before the invoice date")
|
||||
return issued.isoformat(), due.isoformat()
|
||||
|
||||
|
||||
def _write_lines(conn, invoice_id, lines):
|
||||
conn.execute("DELETE FROM invoice_lines WHERE invoice_id = ?", (invoice_id,))
|
||||
conn.executemany(
|
||||
"INSERT INTO invoice_lines (invoice_id, position, description, qty, unit_cents, taxable) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?)",
|
||||
[(invoice_id, i, l["description"], l["qty"], l["unit_cents"], int(l["taxable"]))
|
||||
for i, l in enumerate(lines)])
|
||||
|
||||
|
||||
def create_invoice(conn, customer_id, issue_date, lines, due_date=None, notes=""):
|
||||
business = get_business(conn)
|
||||
customer = get_customer(conn, customer_id)
|
||||
lines = _check_lines(lines)
|
||||
issued, due = _check_dates(issue_date, due_date, business["payment_terms_days"])
|
||||
with conn: # the number is taken in the same transaction, so it's never handed out twice
|
||||
n = conn.execute("SELECT next_number FROM business WHERE id = 1").fetchone()[0]
|
||||
while conn.execute("SELECT 1 FROM invoices WHERE number = ?",
|
||||
(_number(business["invoice_prefix"], n),)).fetchone():
|
||||
n += 1
|
||||
cur = conn.execute(
|
||||
"INSERT INTO invoices (number, customer_id, customer_name, customer_address, customer_email, "
|
||||
"issue_date, due_date, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(_number(business["invoice_prefix"], n), customer_id, customer["name"], customer["address"],
|
||||
customer["email"], issued, due, (notes or "").strip()))
|
||||
conn.execute("UPDATE business SET next_number = ? WHERE id = 1", (n + 1,))
|
||||
_write_lines(conn, cur.lastrowid, lines)
|
||||
return get_invoice(conn, cur.lastrowid)
|
||||
|
||||
|
||||
def _invoice(conn, inv):
|
||||
d = dict(inv)
|
||||
lines = [dict(l) for l in conn.execute(
|
||||
"SELECT description, qty, unit_cents, taxable FROM invoice_lines WHERE invoice_id = ? ORDER BY position",
|
||||
(inv["id"],))]
|
||||
t = totals(lines)
|
||||
d["lines"] = [{"description": l["description"], "qty": l["qty"], "unit": l["unit_cents"] / 100,
|
||||
"taxable": bool(l["taxable"]), "amount": line_cents(l["qty"], l["unit_cents"]) / 100}
|
||||
for l in lines]
|
||||
d.update({k: v / 100 for k, v in t.items()})
|
||||
d["overdue"] = d["status"] == "sent" and d["due_date"] < date.today().isoformat()
|
||||
d["payments"] = [ledger._brief(ledger._transaction_info(conn, p["transaction_id"])) for p in conn.execute(
|
||||
"SELECT transaction_id FROM invoice_payments WHERE invoice_id = ? ORDER BY transaction_id", (inv["id"],))]
|
||||
return d
|
||||
|
||||
|
||||
def get_invoice(conn, invoice_id):
|
||||
inv = conn.execute("SELECT * FROM invoices WHERE id = ?", (invoice_id,)).fetchone()
|
||||
if inv is None:
|
||||
raise ledger.NotFound("no such invoice")
|
||||
return _invoice(conn, inv)
|
||||
|
||||
|
||||
def list_invoices(conn, status=None, customer_id=None):
|
||||
where, args = [], []
|
||||
if status:
|
||||
where.append("status = ?")
|
||||
args.append(status)
|
||||
if customer_id is not None:
|
||||
where.append("customer_id = ?")
|
||||
args.append(customer_id)
|
||||
sql = "SELECT * FROM invoices" + (" WHERE " + " AND ".join(where) if where else "")
|
||||
return [_invoice(conn, i) for i in conn.execute(sql + " ORDER BY issue_date DESC, id DESC", args)]
|
||||
|
||||
|
||||
def update_invoice(conn, invoice_id, customer_id=None, issue_date=None, due_date=None, lines=None, notes=None):
|
||||
"""Change a draft. Once sent, only the notes can change."""
|
||||
inv = get_invoice(conn, invoice_id)
|
||||
if inv["status"] != "draft" and any(v is not None for v in (customer_id, issue_date, due_date, lines)):
|
||||
raise ValueError(f"invoice {inv['number']} has been {inv['status']}, so only its notes can change")
|
||||
with conn:
|
||||
if customer_id is not None:
|
||||
c = get_customer(conn, customer_id)
|
||||
conn.execute("UPDATE invoices SET customer_id = ?, customer_name = ?, customer_address = ?, "
|
||||
"customer_email = ? WHERE id = ?", (c["id"], c["name"], c["address"], c["email"], invoice_id))
|
||||
if issue_date is not None or due_date is not None:
|
||||
issued, due = _check_dates(issue_date or inv["issue_date"], due_date or inv["due_date"], 0)
|
||||
conn.execute("UPDATE invoices SET issue_date = ?, due_date = ? WHERE id = ?", (issued, due, invoice_id))
|
||||
if lines is not None:
|
||||
_write_lines(conn, invoice_id, _check_lines(lines))
|
||||
if notes is not None:
|
||||
conn.execute("UPDATE invoices SET notes = ? WHERE id = ?", (notes.strip(), invoice_id))
|
||||
return get_invoice(conn, invoice_id)
|
||||
|
||||
|
||||
def set_status(conn, invoice_id, status):
|
||||
"""draft -> sent, sent -> draft (to correct it), draft/sent -> void. Paid goes through mark_paid."""
|
||||
inv = get_invoice(conn, invoice_id)
|
||||
allowed = {"draft": ("sent", "void"), "sent": ("draft", "void"), "paid": (), "void": ()}[inv["status"]]
|
||||
if status not in allowed:
|
||||
hint = " (undo the payment first)" if inv["status"] == "paid" else ""
|
||||
raise ValueError(f"invoice {inv['number']} is {inv['status']} and can't be made {status}{hint}")
|
||||
with conn:
|
||||
conn.execute("UPDATE invoices SET status = ? WHERE id = ?", (status, invoice_id))
|
||||
return get_invoice(conn, invoice_id)
|
||||
|
||||
|
||||
# --- payment ----------------------------------------------------------------
|
||||
|
||||
def book_for(conn, day):
|
||||
"""The book whose financial year (1 April - 31 March) contains `day`."""
|
||||
year = day.year if day.month >= 4 else day.year - 1
|
||||
b = conn.execute("SELECT * FROM books WHERE start_year = ? ORDER BY id LIMIT 1", (year,)).fetchone()
|
||||
if b is None:
|
||||
raise ValueError(f"there's no book for the year starting 1 April {year}; create one first")
|
||||
return b
|
||||
|
||||
|
||||
def bank_matches(conn, invoice_id, paid_date):
|
||||
"""Imported money-in lines that could be this invoice's payment: same amount, within MATCH_DAYS,
|
||||
not already allocated, nearest first."""
|
||||
inv = get_invoice(conn, invoice_id)
|
||||
day = date.fromisoformat(paid_date)
|
||||
rows = conn.execute(
|
||||
"SELECT t.*, b.name AS book FROM transactions t JOIN books b ON b.id = t.book_id "
|
||||
"WHERE t.amount_cents = ? AND t.category IS NULL "
|
||||
"AND t.id NOT IN (SELECT transaction_id FROM invoice_payments)", (ledger.cents(inv["total"]),)).fetchall()
|
||||
near = [(abs((date.fromisoformat(t["date"]) - day).days), t) for t in rows]
|
||||
return [{**ledger._brief(t), "days_apart": gap}
|
||||
for gap, t in sorted(near, key=lambda x: x[0]) if gap <= MATCH_DAYS]
|
||||
|
||||
|
||||
def mark_paid(conn, invoice_id, paid_date, row=None):
|
||||
"""Record the payment: on bank line `row` if given (see bank_matches), otherwise as new cashbook rows
|
||||
dated `paid_date` in the book for that date."""
|
||||
inv = get_invoice(conn, invoice_id)
|
||||
if inv["status"] not in ("draft", "sent"):
|
||||
raise ValueError(f"invoice {inv['number']} is {inv['status']}")
|
||||
paid = date.fromisoformat(paid_date)
|
||||
taxable_cents = ledger.cents(inv["taxable"] + inv["gst"])
|
||||
exempt_cents = ledger.cents(inv["exempt"])
|
||||
parts = [(c, gst) for c, gst in ((taxable_cents, "Incl GST"), (exempt_cents, "Excl GST")) if c]
|
||||
reference = f"{inv['number']} {inv['customer_name']}"[:200]
|
||||
with conn:
|
||||
if row is not None:
|
||||
t = ledger._transaction_info(conn, row)
|
||||
if t["amount_cents"] != taxable_cents + exempt_cents:
|
||||
raise ValueError("that bank line's amount isn't the invoice total")
|
||||
if conn.execute("SELECT 1 FROM invoice_payments WHERE transaction_id = ?", (row,)).fetchone():
|
||||
raise ValueError("that bank line is already recorded as another invoice's payment")
|
||||
(first, gst), rest = parts[0], parts[1:]
|
||||
conn.execute("UPDATE transactions SET amount_cents = ?, gst = ?, category = 'Sales' WHERE id = ?",
|
||||
(first, gst, row))
|
||||
conn.execute("INSERT INTO invoice_payments VALUES (?, ?, 0, ?, ?, ?)",
|
||||
(invoice_id, row, t["amount_cents"], t["gst"], t["category"]))
|
||||
book_id, day, reference = t["book_id"], t["date"], t["reference"]
|
||||
paid_date = t["date"]
|
||||
else:
|
||||
book_id, day, rest = book_for(conn, paid)["id"], paid.isoformat(), parts
|
||||
for amount, gst in rest:
|
||||
cur = conn.execute(
|
||||
"INSERT INTO transactions (book_id, date, reference, origin, amount_cents, gst, category, source) "
|
||||
"VALUES (?, ?, ?, 'Invoice', ?, ?, 'Sales', ?)",
|
||||
(book_id, day, reference, amount, gst, f"invoice {inv['number']}"))
|
||||
conn.execute("INSERT INTO invoice_payments (invoice_id, transaction_id, created) VALUES (?, ?, 1)",
|
||||
(invoice_id, cur.lastrowid))
|
||||
conn.execute("UPDATE invoices SET status = 'paid', paid_date = ? WHERE id = ?", (paid_date, invoice_id))
|
||||
return get_invoice(conn, invoice_id)
|
||||
|
||||
|
||||
def unmark_paid(conn, invoice_id):
|
||||
"""Undo mark_paid: remove the rows it added and put a matched bank line back as it was."""
|
||||
inv = get_invoice(conn, invoice_id)
|
||||
if inv["status"] != "paid":
|
||||
raise ValueError(f"invoice {inv['number']} isn't paid")
|
||||
with conn:
|
||||
for p in conn.execute("SELECT * FROM invoice_payments WHERE invoice_id = ?", (invoice_id,)).fetchall():
|
||||
conn.execute("DELETE FROM invoice_payments WHERE invoice_id = ? AND transaction_id = ?",
|
||||
(invoice_id, p["transaction_id"]))
|
||||
if p["created"]:
|
||||
conn.execute("DELETE FROM transactions WHERE id = ?", (p["transaction_id"],))
|
||||
else:
|
||||
conn.execute("UPDATE transactions SET amount_cents = ?, gst = ?, category = ? WHERE id = ?",
|
||||
(p["orig_amount_cents"], p["orig_gst"], p["orig_category"], p["transaction_id"]))
|
||||
conn.execute("UPDATE invoices SET status = 'sent', paid_date = NULL WHERE id = ?", (invoice_id,))
|
||||
return get_invoice(conn, invoice_id)
|
||||
+475
-4
@@ -3,8 +3,11 @@
|
||||
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 json
|
||||
import re
|
||||
import sqlite3
|
||||
from collections import Counter
|
||||
from contextlib import closing
|
||||
from datetime import date
|
||||
|
||||
from statements import ORIGIN, classify, describe
|
||||
@@ -47,6 +50,128 @@ CREATE TABLE IF NOT EXISTS transactions (
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS transactions_book_date ON transactions (book_id, date);
|
||||
CREATE TABLE IF NOT EXISTS suppliers (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL DEFAULT '',
|
||||
address TEXT NOT NULL DEFAULT '',
|
||||
gst_number TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS attachments (
|
||||
id INTEGER PRIMARY KEY,
|
||||
transaction_id INTEGER REFERENCES transactions(id) ON DELETE SET NULL, -- NULL = not matched yet
|
||||
supplier_id INTEGER REFERENCES suppliers(id), -- who the receipt or bill is from
|
||||
kind TEXT NOT NULL DEFAULT 'receipt' CHECK (kind IN ('receipt', 'invoice')),
|
||||
-- read from the document's pages (OCR), and correctable by hand
|
||||
vendor TEXT,
|
||||
doc_date TEXT, -- ISO date
|
||||
total_cents INTEGER, -- positive
|
||||
gst_cents INTEGER,
|
||||
gst_number TEXT,
|
||||
items_json TEXT, -- [{"description", "amount"}]
|
||||
edited_json TEXT NOT NULL DEFAULT '[]', -- fields corrected by hand, which re-reading leaves alone
|
||||
ocr_status TEXT NOT NULL DEFAULT 'skipped' CHECK (ocr_status IN ('done', 'failed', 'skipped')),
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS attachments_transaction ON attachments (transaction_id);
|
||||
-- the photos or PDFs of an attachment, e.g. the two halves of a long receipt
|
||||
CREATE TABLE IF NOT EXISTS attachment_files (
|
||||
id INTEGER PRIMARY KEY,
|
||||
attachment_id INTEGER NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
position INTEGER NOT NULL, -- page order, from 0
|
||||
filename TEXT NOT NULL, -- name it was uploaded as
|
||||
stored_name TEXT NOT NULL UNIQUE, -- file in data/attachments
|
||||
mime TEXT NOT NULL,
|
||||
sha256 TEXT NOT NULL UNIQUE,
|
||||
size INTEGER NOT NULL,
|
||||
text TEXT, -- what OCR read from it; NULL = couldn't be read
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS attachment_files_attachment ON attachment_files (attachment_id, position);
|
||||
CREATE TABLE IF NOT EXISTS business ( -- the one business invoices are sent from
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
address TEXT NOT NULL DEFAULT '',
|
||||
email TEXT NOT NULL DEFAULT '',
|
||||
phone TEXT NOT NULL DEFAULT '',
|
||||
gst_number TEXT NOT NULL DEFAULT '',
|
||||
bank_account TEXT NOT NULL DEFAULT '',
|
||||
logo_file TEXT, -- file in data/invoices
|
||||
invoice_prefix TEXT NOT NULL DEFAULT 'INV-',
|
||||
next_number INTEGER NOT NULL DEFAULT 1,
|
||||
payment_terms_days INTEGER NOT NULL DEFAULT 14,
|
||||
footer TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS customers (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL DEFAULT '',
|
||||
address TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS invoices (
|
||||
id INTEGER PRIMARY KEY,
|
||||
number TEXT NOT NULL UNIQUE,
|
||||
customer_id INTEGER NOT NULL REFERENCES customers(id),
|
||||
-- copied from the customer when the invoice is made, so editing the customer doesn't change it
|
||||
customer_name TEXT NOT NULL,
|
||||
customer_address TEXT NOT NULL DEFAULT '',
|
||||
customer_email TEXT NOT NULL DEFAULT '',
|
||||
issue_date TEXT NOT NULL, -- ISO date
|
||||
due_date TEXT NOT NULL,
|
||||
status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'sent', 'paid', 'void')),
|
||||
notes TEXT NOT NULL DEFAULT '',
|
||||
paid_date TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS invoice_lines (
|
||||
id INTEGER PRIMARY KEY,
|
||||
invoice_id INTEGER NOT NULL REFERENCES invoices(id) ON DELETE CASCADE,
|
||||
position INTEGER NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
qty REAL NOT NULL,
|
||||
unit_cents INTEGER NOT NULL, -- excluding GST
|
||||
taxable INTEGER NOT NULL DEFAULT 1 -- 0 = no GST on this line
|
||||
);
|
||||
-- the transactions a paid invoice was recorded as (two when it has taxable and non-taxable lines)
|
||||
CREATE TABLE IF NOT EXISTS invoice_payments (
|
||||
invoice_id INTEGER NOT NULL REFERENCES invoices(id) ON DELETE CASCADE,
|
||||
transaction_id INTEGER NOT NULL REFERENCES transactions(id) ON DELETE CASCADE,
|
||||
created INTEGER NOT NULL, -- 1 = added for the invoice, 0 = an imported bank line it was matched to
|
||||
-- what a matched bank line was before, to put back if the payment is undone
|
||||
orig_amount_cents INTEGER,
|
||||
orig_gst TEXT,
|
||||
orig_category TEXT,
|
||||
PRIMARY KEY (invoice_id, transaction_id)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS vehicles (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
rego TEXT NOT NULL DEFAULT '',
|
||||
active INTEGER NOT NULL DEFAULT 1
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS trips (
|
||||
id INTEGER PRIMARY KEY,
|
||||
vehicle_id INTEGER NOT NULL REFERENCES vehicles(id),
|
||||
date TEXT NOT NULL, -- ISO date
|
||||
from_place TEXT NOT NULL DEFAULT '',
|
||||
to_place TEXT NOT NULL DEFAULT '',
|
||||
purpose TEXT NOT NULL DEFAULT '',
|
||||
customer TEXT NOT NULL DEFAULT '',
|
||||
odo_start INTEGER,
|
||||
odo_end INTEGER,
|
||||
km REAL NOT NULL CHECK (km > 0),
|
||||
business INTEGER NOT NULL DEFAULT 1, -- 0 = private
|
||||
CHECK (odo_start IS NULL OR odo_end IS NULL OR odo_end >= odo_start)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS trips_date ON trips (date);
|
||||
CREATE TABLE IF NOT EXISTS mileage_rates ( -- IRD kilometre rates, entered per income year
|
||||
year INTEGER PRIMARY KEY, -- year starting 1 April
|
||||
tier1_milli INTEGER NOT NULL, -- thousandths of a dollar per km
|
||||
tier2_milli INTEGER NOT NULL,
|
||||
tier1_limit_km INTEGER NOT NULL DEFAULT 14000
|
||||
);
|
||||
"""
|
||||
|
||||
|
||||
@@ -54,7 +179,7 @@ class NotFound(Exception):
|
||||
pass
|
||||
|
||||
|
||||
SCHEMA_VERSION = 2
|
||||
SCHEMA_VERSION = 5
|
||||
|
||||
|
||||
def connect(path):
|
||||
@@ -75,12 +200,47 @@ def _upgrade(conn):
|
||||
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")
|
||||
# keep attachments' reference pointing at "transactions", not the renamed copy
|
||||
conn.execute("PRAGMA legacy_alter_table = ON")
|
||||
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 legacy_alter_table = OFF")
|
||||
conn.execute("PRAGMA foreign_keys = ON")
|
||||
if "stored_name" in {c["name"] for c in conn.execute("PRAGMA table_info(attachments)")}:
|
||||
# version 2 -> 3: an attachment's file moves to attachment_files, so it can have several
|
||||
path = conn.execute("PRAGMA database_list").fetchone()["file"]
|
||||
if path: # keep a copy from before the change
|
||||
with closing(sqlite3.connect(path + ".v2.bak")) as dest:
|
||||
conn.backup(dest)
|
||||
conn.execute("PRAGMA foreign_keys = OFF")
|
||||
conn.execute("PRAGMA legacy_alter_table = ON")
|
||||
with conn:
|
||||
conn.execute("ALTER TABLE attachments RENAME TO attachments_v2")
|
||||
conn.execute("DROP INDEX attachments_transaction")
|
||||
conn.executescript(SCHEMA)
|
||||
conn.execute(
|
||||
"INSERT INTO attachments (id, transaction_id, kind, vendor, doc_date, total_cents, gst_cents, "
|
||||
"gst_number, items_json, ocr_status, created_at) SELECT id, transaction_id, kind, vendor, doc_date, "
|
||||
"total_cents, gst_cents, gst_number, items_json, ocr_status, created_at FROM attachments_v2")
|
||||
conn.execute(
|
||||
"INSERT INTO attachment_files (attachment_id, position, filename, stored_name, mime, sha256, size, "
|
||||
"text, created_at) SELECT id, 0, filename, stored_name, mime, sha256, size, raw_text, created_at "
|
||||
"FROM attachments_v2")
|
||||
conn.execute("DROP TABLE attachments_v2")
|
||||
conn.execute("PRAGMA legacy_alter_table = OFF")
|
||||
conn.execute("PRAGMA foreign_keys = ON")
|
||||
if "edited_json" not in {c["name"] for c in conn.execute("PRAGMA table_info(attachments)")}:
|
||||
# version 3 -> 4: remember which details were corrected by hand
|
||||
with conn:
|
||||
conn.execute("ALTER TABLE attachments ADD COLUMN edited_json TEXT NOT NULL DEFAULT '[]'")
|
||||
if "supplier_id" not in {c["name"] for c in conn.execute("PRAGMA table_info(attachments)")}:
|
||||
# version 4 -> 5: receipts and bills can be linked to a supplier
|
||||
with conn:
|
||||
conn.execute("ALTER TABLE attachments ADD COLUMN supplier_id INTEGER REFERENCES suppliers(id)")
|
||||
conn.execute("CREATE INDEX IF NOT EXISTS attachments_supplier ON attachments (supplier_id)")
|
||||
conn.execute(f"PRAGMA user_version = {SCHEMA_VERSION}")
|
||||
|
||||
|
||||
@@ -138,8 +298,13 @@ def month_range(book, sheet):
|
||||
# --- importing --------------------------------------------------------------
|
||||
|
||||
def _existing(conn, book):
|
||||
# an invoice payment split into taxable and non-taxable rows is one bank line
|
||||
return Counter((r["date"], r["amount_cents"]) for r in conn.execute(
|
||||
"SELECT date, amount_cents FROM transactions WHERE book_id = ?", (book["id"],)))
|
||||
"SELECT date, amount_cents FROM transactions WHERE book_id = ? "
|
||||
"AND id NOT IN (SELECT transaction_id FROM invoice_payments) "
|
||||
"UNION ALL SELECT MIN(t.date), SUM(t.amount_cents) FROM invoice_payments p "
|
||||
"JOIN transactions t ON t.id = p.transaction_id WHERE t.book_id = ? GROUP BY p.invoice_id",
|
||||
(book["id"], book["id"])))
|
||||
|
||||
|
||||
def _key(t):
|
||||
@@ -226,7 +391,9 @@ def add_rows(conn, book, rows):
|
||||
# --- reading ----------------------------------------------------------------
|
||||
|
||||
def transactions(conn, book, start=None, end=None):
|
||||
sql, args = "SELECT * FROM transactions WHERE book_id = ?", [book["id"]]
|
||||
sql = ("SELECT t.*, (SELECT COUNT(*) FROM attachments a WHERE a.transaction_id = t.id) AS attachments "
|
||||
"FROM transactions t WHERE book_id = ?")
|
||||
args = [book["id"]]
|
||||
if start:
|
||||
sql, args = sql + " AND date >= ?", args + [start.isoformat()]
|
||||
if end:
|
||||
@@ -245,7 +412,7 @@ def _row(t):
|
||||
"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,
|
||||
"net": net, "attachments": t["attachments"],
|
||||
}
|
||||
|
||||
|
||||
@@ -382,3 +549,307 @@ def update_row(conn, book, row_id, gst=None, category=None):
|
||||
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))
|
||||
|
||||
|
||||
# --- transactions by id -----------------------------------------------------
|
||||
|
||||
def _transaction_info(conn, row_id):
|
||||
t = conn.execute("SELECT t.*, b.name AS book FROM transactions t JOIN books b ON b.id = t.book_id "
|
||||
"WHERE t.id = ?", (row_id,)).fetchone()
|
||||
if t is None:
|
||||
raise NotFound("no such transaction")
|
||||
return t
|
||||
|
||||
|
||||
def _brief(t):
|
||||
return {"row": t["id"], "book": t["book"], "date": t["date"], "reference": t["reference"],
|
||||
"amount": t["amount_cents"] / 100, "gst": t["gst"], "category": t["category"] or "",
|
||||
"sheet": SHEETS[date.fromisoformat(t["date"]).month]}
|
||||
|
||||
|
||||
# --- attachments (receipts and invoices) ------------------------------------
|
||||
|
||||
ATTACHMENT_KINDS = ("receipt", "invoice")
|
||||
# fields read from the document that can be corrected by hand
|
||||
ATTACHMENT_FIELDS = ("kind", "vendor", "doc_date", "total_cents", "gst_cents", "gst_number", "supplier_id")
|
||||
MATCH_DAYS = 7 # how far a receipt's date may be from the bank transaction's
|
||||
|
||||
# merchant names that point to an expense category (lower case, matched as substrings)
|
||||
VENDOR_CATEGORIES = [
|
||||
("Motor vehicle", ("z energy", "bp connect", "mobil", "caltex", "gull", "waitomo", "npd", "parking",
|
||||
"repco", "supercheap", "tyre", "nzta", "waka kotahi")),
|
||||
("Telephone & Power", ("spark", "one nz", "vodafone", "2degrees", "skinny", "mercury", "genesis",
|
||||
"contact energy", "meridian", "electric kiwi", "flick", "slingshot", "orcon")),
|
||||
("Postage & Stationery", ("nz post", "warehouse stationery", "officemax", "paper plus", "whitcoulls")),
|
||||
("ACC & Insurance", ("insurance", "accident compensation", "tower", "vero")),
|
||||
]
|
||||
|
||||
|
||||
def _files(conn, attachment_id):
|
||||
return [dict(f) for f in conn.execute(
|
||||
"SELECT * FROM attachment_files WHERE attachment_id = ? ORDER BY position, id", (attachment_id,))]
|
||||
|
||||
|
||||
def _attachment(conn, a):
|
||||
d = dict(a)
|
||||
d["items"] = json.loads(d.pop("items_json") or "[]")
|
||||
d["edited"] = json.loads(d.pop("edited_json") or "[]")
|
||||
d["total"] = d["total_cents"] / 100 if d["total_cents"] is not None else None
|
||||
d["gst"] = d["gst_cents"] / 100 if d["gst_cents"] is not None else None
|
||||
files = _files(conn, a["id"])
|
||||
d["files"] = [{k: f[k] for k in ("id", "filename", "mime", "size")} for f in files]
|
||||
d["filename"] = files[0]["filename"] if files else ""
|
||||
d["raw_text"] = attachment_text(conn, a["id"], files)
|
||||
d["transaction"] = None
|
||||
d["supplier"] = None
|
||||
if d["supplier_id"] is not None:
|
||||
sup = conn.execute("SELECT id, name FROM suppliers WHERE id = ?", (d["supplier_id"],)).fetchone()
|
||||
d["supplier"] = dict(sup)
|
||||
return d
|
||||
|
||||
|
||||
def attachment_text(conn, attachment_id, files=None):
|
||||
"""What OCR read from all the pages, in order."""
|
||||
files = files if files is not None else _files(conn, attachment_id)
|
||||
return "\n".join(f["text"] for f in files if f["text"])
|
||||
|
||||
|
||||
def _with_transactions(conn, rows):
|
||||
out = [_attachment(conn, a) for a in rows]
|
||||
for a in out:
|
||||
if a["transaction_id"] is not None:
|
||||
a["transaction"] = _brief(_transaction_info(conn, a["transaction_id"]))
|
||||
return out
|
||||
|
||||
|
||||
def file_exists(conn, sha256):
|
||||
return conn.execute("SELECT 1 FROM attachment_files WHERE sha256 = ?", (sha256,)).fetchone() is not None
|
||||
|
||||
|
||||
def _insert_file(conn, attachment_id, position, file, text):
|
||||
if file_exists(conn, file["sha256"]):
|
||||
raise ValueError("that file has already been added")
|
||||
conn.execute(
|
||||
"INSERT INTO attachment_files (attachment_id, position, filename, stored_name, mime, sha256, size, text) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(attachment_id, position, file["filename"], file["stored_name"], file["mime"], file["sha256"],
|
||||
file["size"], text))
|
||||
|
||||
|
||||
def add_attachment(conn, *, file, kind="receipt", transaction_id=None, ocr=None):
|
||||
"""Record an uploaded receipt (its first page); `file` is from services.receipts.save_upload and
|
||||
`ocr` is what services.receipts.read_document read from it."""
|
||||
if kind not in ATTACHMENT_KINDS:
|
||||
raise ValueError("kind must be receipt or invoice")
|
||||
if transaction_id is not None:
|
||||
_transaction_info(conn, transaction_id)
|
||||
ocr = ocr or {}
|
||||
supplier = find_supplier(conn, ocr.get("vendor"), ocr.get("gst_number"))
|
||||
with conn:
|
||||
cur = conn.execute(
|
||||
"INSERT INTO attachments (transaction_id, supplier_id, kind, vendor, doc_date, total_cents, gst_cents, "
|
||||
"gst_number, items_json, ocr_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(transaction_id, supplier, kind, ocr.get("vendor"), ocr.get("date"), ocr.get("total_cents"),
|
||||
ocr.get("gst_cents"), ocr.get("gst_number"), json.dumps(ocr.get("items") or []),
|
||||
ocr.get("status", "skipped")))
|
||||
_insert_file(conn, cur.lastrowid, 0, file, ocr.get("text"))
|
||||
return get_attachment(conn, cur.lastrowid)
|
||||
|
||||
|
||||
def add_page(conn, attachment_id, file, text):
|
||||
"""Add another photo/PDF to an attachment, after its other pages."""
|
||||
get_attachment(conn, attachment_id)
|
||||
with conn:
|
||||
last = conn.execute("SELECT MAX(position) FROM attachment_files WHERE attachment_id = ?",
|
||||
(attachment_id,)).fetchone()[0]
|
||||
_insert_file(conn, attachment_id, (last or 0) + 1, file, text)
|
||||
return get_attachment(conn, attachment_id)
|
||||
|
||||
|
||||
def get_file(conn, attachment_id, file_id=None):
|
||||
"""One page of an attachment (the first if file_id is None)."""
|
||||
sql, args = "SELECT * FROM attachment_files WHERE attachment_id = ?", [attachment_id]
|
||||
if file_id is not None:
|
||||
sql, args = sql + " AND id = ?", args + [file_id]
|
||||
f = conn.execute(sql + " ORDER BY position, id LIMIT 1", args).fetchone()
|
||||
if f is None:
|
||||
raise NotFound("no such file")
|
||||
return f
|
||||
|
||||
|
||||
def remove_page(conn, attachment_id, file_id):
|
||||
"""Take one page off an attachment; returns its stored file name so the caller can remove the file."""
|
||||
f = get_file(conn, attachment_id, file_id)
|
||||
if len(_files(conn, attachment_id)) == 1:
|
||||
raise ValueError("that's the only page; delete the receipt instead")
|
||||
with conn:
|
||||
conn.execute("DELETE FROM attachment_files WHERE id = ?", (file_id,))
|
||||
return f["stored_name"]
|
||||
|
||||
|
||||
OCR_FIELDS = {"vendor": "vendor", "doc_date": "date", "total_cents": "total_cents",
|
||||
"gst_cents": "gst_cents", "gst_number": "gst_number"} # column: parse_receipt key
|
||||
|
||||
|
||||
def apply_ocr(conn, attachment_id, found, overwrite=False):
|
||||
"""Set an attachment's details to what was read from all its pages together
|
||||
(services.receipts.parse_receipt), except those corrected by hand, unless `overwrite`."""
|
||||
a = get_attachment(conn, attachment_id)
|
||||
keep = set() if overwrite else set(a["edited"])
|
||||
with conn:
|
||||
for col, key in OCR_FIELDS.items():
|
||||
if col not in keep:
|
||||
conn.execute(f"UPDATE attachments SET {col} = ? WHERE id = ?", (found.get(key), attachment_id))
|
||||
read = any(f["text"] for f in _files(conn, attachment_id))
|
||||
conn.execute("UPDATE attachments SET items_json = ?, ocr_status = ?, edited_json = ? WHERE id = ?",
|
||||
(json.dumps(found.get("items") or []), "done" if read else "failed",
|
||||
json.dumps(sorted(keep)), attachment_id))
|
||||
return get_attachment(conn, attachment_id)
|
||||
|
||||
|
||||
def get_attachment(conn, attachment_id):
|
||||
a = conn.execute("SELECT * FROM attachments WHERE id = ?", (attachment_id,)).fetchone()
|
||||
if a is None:
|
||||
raise NotFound("no such attachment")
|
||||
return _with_transactions(conn, [a])[0]
|
||||
|
||||
|
||||
def list_attachments(conn, unmatched=False, supplier_id=None):
|
||||
where, args = [], []
|
||||
if unmatched:
|
||||
where.append("transaction_id IS NULL")
|
||||
if supplier_id is not None:
|
||||
where.append("supplier_id = ?")
|
||||
args.append(supplier_id)
|
||||
sql = "SELECT * FROM attachments" + (" WHERE " + " AND ".join(where) if where else "")
|
||||
return _with_transactions(conn, conn.execute(sql + " ORDER BY created_at DESC, id DESC", args).fetchall())
|
||||
|
||||
|
||||
def attachments_for(conn, row_id):
|
||||
_transaction_info(conn, row_id)
|
||||
return _with_transactions(conn, conn.execute(
|
||||
"SELECT * FROM attachments WHERE transaction_id = ? ORDER BY id", (row_id,)).fetchall())
|
||||
|
||||
|
||||
def update_attachment(conn, attachment_id, **fields):
|
||||
"""Correct details by hand; corrected ones are then kept when the pages are read again."""
|
||||
a = get_attachment(conn, attachment_id)
|
||||
if fields.get("kind") is not None and fields["kind"] not in ATTACHMENT_KINDS:
|
||||
raise ValueError("kind must be receipt or invoice")
|
||||
if fields.get("doc_date"):
|
||||
date.fromisoformat(fields["doc_date"]) # ValueError if it isn't a date
|
||||
if fields.get("supplier_id") is not None and not conn.execute(
|
||||
"SELECT 1 FROM suppliers WHERE id = ?", (fields["supplier_id"],)).fetchone():
|
||||
raise NotFound("no such supplier")
|
||||
edited = set(a["edited"])
|
||||
with conn:
|
||||
for k, v in fields.items():
|
||||
if k not in ATTACHMENT_FIELDS:
|
||||
raise ValueError(f"can't change {k}")
|
||||
if k == "kind" and v is None:
|
||||
continue
|
||||
conn.execute(f"UPDATE attachments SET {k} = ? WHERE id = ?", (v if v != "" else None, attachment_id))
|
||||
if k in OCR_FIELDS:
|
||||
edited.add(k)
|
||||
conn.execute("UPDATE attachments SET edited_json = ? WHERE id = ?", (json.dumps(sorted(edited)), attachment_id))
|
||||
return get_attachment(conn, attachment_id)
|
||||
|
||||
|
||||
def delete_attachment(conn, attachment_id):
|
||||
"""Forget an attachment; returns its stored file names so the caller can remove the files."""
|
||||
get_attachment(conn, attachment_id)
|
||||
names = [f["stored_name"] for f in _files(conn, attachment_id)]
|
||||
with conn:
|
||||
conn.execute("DELETE FROM attachments WHERE id = ?", (attachment_id,)) # pages go too (cascade)
|
||||
return names
|
||||
|
||||
|
||||
def _company(name):
|
||||
"""A supplier name reduced for comparing: letters and digits only, without Ltd / Limited / NZ."""
|
||||
words = re.findall(r"[a-z0-9]+", (name or "").lower())
|
||||
return "".join(w for w in words if w not in ("ltd", "limited", "nz", "co", "the"))
|
||||
|
||||
|
||||
def find_supplier(conn, vendor, gst_number=None):
|
||||
"""The id of the supplier a document is probably from: the same GST number, else the same name."""
|
||||
digits = re.sub(r"\D", "", gst_number or "")
|
||||
rows = conn.execute("SELECT id, name, gst_number FROM suppliers ORDER BY id").fetchall()
|
||||
if digits:
|
||||
for r in rows:
|
||||
if re.sub(r"\D", "", r["gst_number"]) == digits:
|
||||
return r["id"]
|
||||
name = _company(vendor)
|
||||
if name:
|
||||
for r in rows:
|
||||
if _company(r["name"]) == name:
|
||||
return r["id"]
|
||||
return None
|
||||
|
||||
|
||||
def _words(s):
|
||||
return {w for w in re.findall(r"[a-z0-9]{3,}", (s or "").lower())}
|
||||
|
||||
|
||||
def suggest_transactions(conn, attachment, limit=5):
|
||||
"""Bank transactions this receipt is probably for: the same amount, within MATCH_DAYS of its date
|
||||
(any date if it has none), nearest first, then those whose reference shares words with the vendor."""
|
||||
total = attachment["total_cents"]
|
||||
if not total:
|
||||
return []
|
||||
rows = conn.execute(
|
||||
"SELECT t.*, b.name AS book, (SELECT COUNT(*) FROM attachments a WHERE a.transaction_id = t.id) AS n "
|
||||
"FROM transactions t JOIN books b ON b.id = t.book_id WHERE abs(t.amount_cents) = ?", (abs(total),)).fetchall()
|
||||
when = date.fromisoformat(attachment["doc_date"]) if attachment["doc_date"] else None
|
||||
vendor = _words(attachment["vendor"])
|
||||
scored = []
|
||||
for t in rows:
|
||||
if t["id"] == attachment["transaction_id"]:
|
||||
continue
|
||||
gap = abs((date.fromisoformat(t["date"]) - when).days) if when else 0
|
||||
if gap > MATCH_DAYS:
|
||||
continue
|
||||
overlap = len(vendor & _words(t["reference"]))
|
||||
scored.append(((gap, -overlap, t["n"], t["date"]), t))
|
||||
scored.sort(key=lambda s: s[0])
|
||||
return [{**_brief(t), "days_apart": key[0] if when else None, "attachments": t["n"]}
|
||||
for key, t in scored[:limit]]
|
||||
|
||||
|
||||
def propose_changes(conn, attachment, row_id):
|
||||
"""GST and category the transaction could take from this receipt; only what would change."""
|
||||
t = _transaction_info(conn, row_id)
|
||||
out = {}
|
||||
if (attachment["gst_cents"] or attachment["gst_number"]) and t["gst"] != "Incl GST":
|
||||
out["gst"] = "Incl GST"
|
||||
if not t["category"]:
|
||||
allowed = INCOME_CATEGORIES if t["amount_cents"] > 0 else EXPENSE_CATEGORIES
|
||||
same = conn.execute(
|
||||
"SELECT category, COUNT(*) AS n FROM transactions WHERE reference = ? AND category IS NOT NULL "
|
||||
"AND id != ? GROUP BY category ORDER BY n DESC LIMIT 1", (t["reference"], row_id)).fetchone()
|
||||
if same and same["category"] in allowed:
|
||||
out["category"] = same["category"]
|
||||
elif t["amount_cents"] < 0:
|
||||
text = f"{attachment['vendor'] or ''} {t['reference']}".lower()
|
||||
category = next((c for c, words in VENDOR_CATEGORIES if any(w in text for w in words)), None)
|
||||
if category:
|
||||
out["category"] = category
|
||||
return out
|
||||
|
||||
|
||||
def link_attachment(conn, attachment_id, row_id, gst=None, category=None):
|
||||
"""Attach to a transaction, optionally setting its GST and category at the same time."""
|
||||
get_attachment(conn, attachment_id)
|
||||
t = _transaction_info(conn, row_id)
|
||||
if gst is not None or category is not None:
|
||||
update_row(conn, conn.execute("SELECT * FROM books WHERE id = ?", (t["book_id"],)).fetchone(),
|
||||
row_id, gst=gst, category=category)
|
||||
with conn:
|
||||
conn.execute("UPDATE attachments SET transaction_id = ? WHERE id = ?", (row_id, attachment_id))
|
||||
return get_attachment(conn, attachment_id)
|
||||
|
||||
|
||||
def unlink_attachment(conn, attachment_id):
|
||||
get_attachment(conn, attachment_id)
|
||||
with conn:
|
||||
conn.execute("UPDATE attachments SET transaction_id = NULL WHERE id = ?", (attachment_id,))
|
||||
return get_attachment(conn, attachment_id)
|
||||
+236
@@ -0,0 +1,236 @@
|
||||
"""Vehicle logbook and the IRD kilometre-rate claim it supports.
|
||||
|
||||
Trips belong to an income year (1 April - 31 March) by date. The claim for a year is worked
|
||||
out per vehicle: business kilometres up to the tier 1 limit (14,000 km) at the tier 1 rate,
|
||||
the rest at the tier 2 rate. Rates change every year, so they're entered by hand from IRD's
|
||||
published figures. This is a report only: nothing is put in the cashbook or tax returns.
|
||||
"""
|
||||
import csv
|
||||
import io
|
||||
from datetime import date
|
||||
|
||||
import ledger
|
||||
|
||||
TRIP_FIELDS = ("vehicle_id", "date", "from_place", "to_place", "purpose", "customer",
|
||||
"odo_start", "odo_end", "km", "business")
|
||||
|
||||
|
||||
def income_year(day):
|
||||
return day.year if day.month >= 4 else day.year - 1
|
||||
|
||||
|
||||
def year_range(year):
|
||||
return date(year, 4, 1), date(year + 1, 3, 31)
|
||||
|
||||
|
||||
# --- vehicles ---------------------------------------------------------------
|
||||
|
||||
def list_vehicles(conn):
|
||||
return [dict(r) for r in conn.execute(
|
||||
"SELECT v.*, COUNT(t.id) AS trips, MAX(t.odo_end) AS last_odometer FROM vehicles v "
|
||||
"LEFT JOIN trips t ON t.vehicle_id = v.id GROUP BY v.id ORDER BY v.active DESC, v.name COLLATE NOCASE")]
|
||||
|
||||
|
||||
def get_vehicle(conn, vehicle_id):
|
||||
v = conn.execute("SELECT * FROM vehicles WHERE id = ?", (vehicle_id,)).fetchone()
|
||||
if v is None:
|
||||
raise ledger.NotFound("no such vehicle")
|
||||
return dict(v)
|
||||
|
||||
|
||||
def _vehicle_name(conn, name, vehicle_id=None):
|
||||
name = (name or "").strip()
|
||||
if not name:
|
||||
raise ValueError("the vehicle needs a name")
|
||||
if conn.execute("SELECT 1 FROM vehicles WHERE name = ? AND id IS NOT ?", (name, vehicle_id)).fetchone():
|
||||
raise ValueError(f"there's already a vehicle called {name}")
|
||||
return name
|
||||
|
||||
|
||||
def create_vehicle(conn, name, rego=""):
|
||||
name = _vehicle_name(conn, name)
|
||||
with conn:
|
||||
cur = conn.execute("INSERT INTO vehicles (name, rego) VALUES (?, ?)", (name, (rego or "").strip().upper()))
|
||||
return get_vehicle(conn, cur.lastrowid)
|
||||
|
||||
|
||||
def update_vehicle(conn, vehicle_id, name=None, rego=None, active=None):
|
||||
get_vehicle(conn, vehicle_id)
|
||||
with conn:
|
||||
if name is not None:
|
||||
conn.execute("UPDATE vehicles SET name = ? WHERE id = ?", (_vehicle_name(conn, name, vehicle_id), vehicle_id))
|
||||
if rego is not None:
|
||||
conn.execute("UPDATE vehicles SET rego = ? WHERE id = ?", (rego.strip().upper(), vehicle_id))
|
||||
if active is not None:
|
||||
conn.execute("UPDATE vehicles SET active = ? WHERE id = ?", (int(bool(active)), vehicle_id))
|
||||
return get_vehicle(conn, vehicle_id)
|
||||
|
||||
|
||||
def delete_vehicle(conn, vehicle_id):
|
||||
get_vehicle(conn, vehicle_id)
|
||||
if conn.execute("SELECT 1 FROM trips WHERE vehicle_id = ?", (vehicle_id,)).fetchone():
|
||||
raise ValueError("this vehicle has trips logged; mark it inactive instead")
|
||||
with conn:
|
||||
conn.execute("DELETE FROM vehicles WHERE id = ?", (vehicle_id,))
|
||||
|
||||
|
||||
# --- trips ------------------------------------------------------------------
|
||||
|
||||
def _trip(t):
|
||||
d = dict(t)
|
||||
d["business"] = bool(d["business"])
|
||||
return d
|
||||
|
||||
|
||||
def get_trip(conn, trip_id):
|
||||
t = conn.execute("SELECT t.*, v.name AS vehicle FROM trips t JOIN vehicles v ON v.id = t.vehicle_id "
|
||||
"WHERE t.id = ?", (trip_id,)).fetchone()
|
||||
if t is None:
|
||||
raise ledger.NotFound("no such trip")
|
||||
return _trip(t)
|
||||
|
||||
|
||||
def list_trips(conn, year=None, vehicle_id=None):
|
||||
sql, args = "SELECT t.*, v.name AS vehicle FROM trips t JOIN vehicles v ON v.id = t.vehicle_id WHERE 1", []
|
||||
if year is not None:
|
||||
start, end = year_range(year)
|
||||
sql, args = sql + " AND t.date BETWEEN ? AND ?", args + [start.isoformat(), end.isoformat()]
|
||||
if vehicle_id is not None:
|
||||
sql, args = sql + " AND t.vehicle_id = ?", args + [vehicle_id]
|
||||
return [_trip(t) for t in conn.execute(sql + " ORDER BY t.date, t.odo_start, t.id", args)]
|
||||
|
||||
|
||||
def _check_trip(conn, t):
|
||||
"""Validate and complete a trip's fields; km comes from the odometer when both readings are given."""
|
||||
get_vehicle(conn, t["vehicle_id"])
|
||||
t["date"] = date.fromisoformat(t["date"]).isoformat() # ValueError if it isn't a date
|
||||
for k in ("from_place", "to_place", "purpose", "customer"):
|
||||
t[k] = (t.get(k) or "").strip()[:200]
|
||||
start, end = t.get("odo_start"), t.get("odo_end")
|
||||
t["odo_start"] = int(start) if start not in (None, "") else None
|
||||
t["odo_end"] = int(end) if end not in (None, "") else None
|
||||
if t["odo_start"] is not None and t["odo_end"] is not None:
|
||||
if t["odo_end"] <= t["odo_start"]:
|
||||
raise ValueError("the end odometer reading must be more than the start")
|
||||
t["km"] = t["odo_end"] - t["odo_start"]
|
||||
km = t.get("km")
|
||||
if km in (None, "") or float(km) <= 0:
|
||||
raise ValueError("enter the distance, or both odometer readings")
|
||||
t["km"] = round(float(km), 1)
|
||||
t["business"] = int(bool(t.get("business", True)))
|
||||
return t
|
||||
|
||||
|
||||
def warnings(conn, trip):
|
||||
"""Odometer readings that clash with the vehicle's other trips (allowed, but probably a typo)."""
|
||||
if trip["odo_start"] is None or trip["odo_end"] is None:
|
||||
return []
|
||||
out = []
|
||||
for o in conn.execute("SELECT * FROM trips WHERE vehicle_id = ? AND id IS NOT ? AND odo_start IS NOT NULL "
|
||||
"AND odo_end IS NOT NULL", (trip["vehicle_id"], trip.get("id"))):
|
||||
if trip["odo_start"] < o["odo_end"] and trip["odo_end"] > o["odo_start"]:
|
||||
out.append(f"the odometer readings overlap the trip on {o['date']} ({o['odo_start']}-{o['odo_end']})")
|
||||
elif (o["date"] < trip["date"] and o["odo_start"] > trip["odo_end"]) or \
|
||||
(o["date"] > trip["date"] and o["odo_end"] < trip["odo_start"]):
|
||||
out.append(f"the odometer readings are out of order with the trip on {o['date']}")
|
||||
return out
|
||||
|
||||
|
||||
def add_trip(conn, **fields):
|
||||
t = _check_trip(conn, {k: fields.get(k) for k in TRIP_FIELDS})
|
||||
with conn:
|
||||
cur = conn.execute(f"INSERT INTO trips ({', '.join(TRIP_FIELDS)}) VALUES ({', '.join('?' * len(TRIP_FIELDS))})",
|
||||
[t[k] for k in TRIP_FIELDS])
|
||||
trip = get_trip(conn, cur.lastrowid)
|
||||
return {**trip, "warnings": warnings(conn, trip)}
|
||||
|
||||
|
||||
def update_trip(conn, trip_id, **fields):
|
||||
old = get_trip(conn, trip_id)
|
||||
t = _check_trip(conn, {k: fields[k] if k in fields else old[k] for k in TRIP_FIELDS})
|
||||
with conn:
|
||||
conn.execute(f"UPDATE trips SET {', '.join(f'{k} = ?' for k in TRIP_FIELDS)} WHERE id = ?",
|
||||
[t[k] for k in TRIP_FIELDS] + [trip_id])
|
||||
trip = get_trip(conn, trip_id)
|
||||
return {**trip, "warnings": warnings(conn, trip)}
|
||||
|
||||
|
||||
def delete_trip(conn, trip_id):
|
||||
get_trip(conn, trip_id)
|
||||
with conn:
|
||||
conn.execute("DELETE FROM trips WHERE id = ?", (trip_id,))
|
||||
|
||||
|
||||
# --- rates and the claim ----------------------------------------------------
|
||||
|
||||
def get_rate(conn, year):
|
||||
r = conn.execute("SELECT * FROM mileage_rates WHERE year = ?", (year,)).fetchone()
|
||||
if r is None:
|
||||
return None
|
||||
return {"year": year, "tier1": r["tier1_milli"] / 1000, "tier2": r["tier2_milli"] / 1000,
|
||||
"tier1_limit_km": r["tier1_limit_km"]}
|
||||
|
||||
|
||||
def set_rate(conn, year, tier1, tier2, tier1_limit_km=14000):
|
||||
if not (0 < tier1 < 10 and 0 <= tier2 < 10):
|
||||
raise ValueError("rates are dollars per kilometre, e.g. 1.17")
|
||||
if tier1_limit_km <= 0:
|
||||
raise ValueError("the tier 1 limit must be more than zero")
|
||||
with conn:
|
||||
conn.execute("INSERT INTO mileage_rates VALUES (?, ?, ?, ?) ON CONFLICT (year) DO UPDATE SET "
|
||||
"tier1_milli = excluded.tier1_milli, tier2_milli = excluded.tier2_milli, "
|
||||
"tier1_limit_km = excluded.tier1_limit_km",
|
||||
(year, round(tier1 * 1000), round(tier2 * 1000), int(tier1_limit_km)))
|
||||
return get_rate(conn, year)
|
||||
|
||||
|
||||
def report(conn, year):
|
||||
"""Kilometres and the claim per vehicle for an income year. claim is None while the year has no rate."""
|
||||
rate = get_rate(conn, year)
|
||||
by_vehicle = {}
|
||||
for t in list_trips(conn, year):
|
||||
v = by_vehicle.setdefault(t["vehicle_id"], {
|
||||
"vehicle_id": t["vehicle_id"], "vehicle": t["vehicle"], "trips": 0, "business_km": 0.0,
|
||||
"private_km": 0.0, "odo_first": None, "odo_last": None})
|
||||
v["trips"] += 1
|
||||
v["business_km" if t["business"] else "private_km"] += t["km"]
|
||||
for reading in (t["odo_start"], t["odo_end"]):
|
||||
if reading is not None:
|
||||
v["odo_first"] = reading if v["odo_first"] is None else min(v["odo_first"], reading)
|
||||
v["odo_last"] = reading if v["odo_last"] is None else max(v["odo_last"], reading)
|
||||
vehicles = []
|
||||
for v in by_vehicle.values():
|
||||
total = v["business_km"] + v["private_km"]
|
||||
v["business_km"], v["private_km"] = round(v["business_km"], 1), round(v["private_km"], 1)
|
||||
v["business_percent"] = round(100 * v["business_km"] / total, 1) if total else 0.0
|
||||
v["tier1_km"] = v["tier2_km"] = v["claim"] = None
|
||||
if rate:
|
||||
v["tier1_km"] = min(v["business_km"], rate["tier1_limit_km"])
|
||||
v["tier2_km"] = round(v["business_km"] - v["tier1_km"], 1)
|
||||
milli = v["tier1_km"] * rate["tier1"] * 1000 + v["tier2_km"] * rate["tier2"] * 1000
|
||||
v["claim"] = (int(round(milli)) + 5) // 10 / 100 # thousandths -> cents, half-up
|
||||
vehicles.append(v)
|
||||
vehicles.sort(key=lambda v: v["vehicle"].lower())
|
||||
start, end = year_range(year)
|
||||
claims = [v["claim"] for v in vehicles]
|
||||
return {
|
||||
"year": year, "start": start.isoformat(), "end": end.isoformat(), "rate": rate,
|
||||
"vehicles": vehicles,
|
||||
"business_km": round(sum(v["business_km"] for v in vehicles), 1),
|
||||
"private_km": round(sum(v["private_km"] for v in vehicles), 1),
|
||||
"claim": round(sum(claims), 2) if rate else None,
|
||||
}
|
||||
|
||||
|
||||
def export_csv(conn, year):
|
||||
"""The year's logbook as CSV (with a BOM, so Excel reads it as UTF-8)."""
|
||||
out = io.StringIO()
|
||||
w = csv.writer(out)
|
||||
w.writerow(["Date", "Vehicle", "From", "To", "Purpose", "Customer", "Odometer start", "Odometer end",
|
||||
"Km", "Business/private"])
|
||||
for t in list_trips(conn, year):
|
||||
w.writerow([t["date"], t["vehicle"], t["from_place"], t["to_place"], t["purpose"], t["customer"],
|
||||
t["odo_start"] if t["odo_start"] is not None else "",
|
||||
t["odo_end"] if t["odo_end"] is not None else "",
|
||||
f"{t['km']:g}", "Business" if t["business"] else "Private"])
|
||||
return ("" + out.getvalue()).encode("utf-8")
|
||||
+453
-74
@@ -27,17 +27,23 @@
|
||||
padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
|
||||
position: sticky; top: 0; z-index: 5; }
|
||||
header h1 { font-size: 17px; margin: 0 12px 0 0; letter-spacing: .2px; }
|
||||
header .spacer { flex: 1; }
|
||||
header { padding-top: 0; padding-bottom: 0; gap: 20px; }
|
||||
header h1 { margin: 0; padding: 12px 0; }
|
||||
header .tabs.primary { flex: 1; margin: 0; border: 0; align-self: stretch; }
|
||||
header .tabs.primary button { padding: 0 14px; margin-bottom: -1px; }
|
||||
.bookbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 8px 20px;
|
||||
background: var(--bg); border-bottom: 1px solid var(--line); }
|
||||
.bookbar[hidden] { display: none; }
|
||||
select, input, button { font: inherit; color: inherit; }
|
||||
select, input[type=text], input[type=number] {
|
||||
select, input[type=text], input[type=number], input[type=date] {
|
||||
background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; }
|
||||
button { border: 1px solid var(--line); background: var(--panel); border-radius: 6px;
|
||||
padding: 5px 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:disabled { opacity: .5; cursor: default; }
|
||||
.layout { display: grid; grid-template-columns: 340px 1fr; gap: 16px; padding: 16px 20px; }
|
||||
@media (max-width: 900px) { .layout { grid-template-columns: 1fr; padding: 12px 16px; } }
|
||||
.layout { max-width: 1400px; margin: 0 auto; padding: 16px 20px; }
|
||||
@media (max-width: 900px) { .layout { padding: 12px 16px; } }
|
||||
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px; min-width: 0; }
|
||||
.panel h2 { font-size: 14px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }
|
||||
.panel h2 .grow { flex: 1; }
|
||||
@@ -55,6 +61,12 @@
|
||||
.tabs button { border-radius: 999px; padding: 3px 12px; }
|
||||
.tabs button.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
|
||||
.tabs button.empty { color: var(--muted); }
|
||||
.tabs .sep { width: 1px; background: var(--line); margin: 6px 8px; }
|
||||
.tabs.primary { gap: 0; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
|
||||
.tabs.primary button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none;
|
||||
padding: 8px 16px; font-size: 14px; color: var(--muted); }
|
||||
.tabs.primary button.on { border-bottom-color: var(--accent); color: var(--accent); background: none; font-weight: 600; }
|
||||
.tabs .count { background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 0 7px; font-size: 12px; }
|
||||
.scroll { overflow-x: auto; }
|
||||
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
|
||||
th, td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
|
||||
@@ -130,11 +142,87 @@
|
||||
.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; }
|
||||
.receipts { display: grid; gap: 12px; }
|
||||
.receipt { display: grid; grid-template-columns: 120px 1fr; gap: 14px; border: 1px solid var(--line); border-radius: 10px; padding: 12px; }
|
||||
@media (max-width: 600px) { .receipt { grid-template-columns: 1fr; } }
|
||||
.receipt .thumb img { width: 120px; max-height: 170px; object-fit: cover; object-position: top; border-radius: 6px; border: 1px solid var(--line); display: block; }
|
||||
.pages { display: flex; flex-wrap: wrap; gap: 4px; width: 120px; }
|
||||
.page { position: relative; }
|
||||
.receipt .thumb .pages.many img, .pages.many .pdf-thumb { width: 58px; height: 78px; max-height: none; font-size: 11px; }
|
||||
.page .rm { position: absolute; top: 2px; right: 2px; padding: 0 5px; font-size: 11px; line-height: 16px; border-radius: 999px; opacity: .9; }
|
||||
.add-page { width: 120px; margin-top: 6px; font-size: 12px; padding: 3px 6px; }
|
||||
.pdf-thumb { display: flex; align-items: center; justify-content: center; width: 120px; height: 120px; border: 1px solid var(--line);
|
||||
border-radius: 6px; color: var(--out); font-weight: 700; text-decoration: none; }
|
||||
.receipt-head { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; margin-bottom: 8px; }
|
||||
.receipt-head .grow { flex: 1; min-width: 0; word-break: break-all; }
|
||||
.receipt-fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px 10px; }
|
||||
.receipt-fields label, dialog .receipt-fields label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 2px; margin: 0; }
|
||||
.receipt-fields input, .receipt-fields select { width: 100%; }
|
||||
.receipt-fields .wide { grid-column: span 2; }
|
||||
.receipt details { margin-top: 8px; }
|
||||
.receipt details summary { cursor: pointer; color: var(--muted); font-size: 12px; }
|
||||
.receipt pre { white-space: pre-wrap; font-size: 12px; max-height: 240px; overflow: auto; background: var(--row-hover); padding: 8px; border-radius: 6px; }
|
||||
.match { margin-top: 10px; padding: 8px 10px; border-radius: 8px; background: var(--row-hover); }
|
||||
.match ul { list-style: none; margin: 4px 0 0; padding: 0; }
|
||||
.match li { display: flex; gap: 8px; align-items: center; padding: 4px 0; flex-wrap: wrap; }
|
||||
.match li .grow { flex: 1; min-width: 160px; }
|
||||
.chip.linked { background: var(--ok-soft); color: var(--in); }
|
||||
.chip.failed { background: var(--warn-soft); color: var(--warn); }
|
||||
button.clip { padding: 1px 6px; font-size: 12px; opacity: .4; }
|
||||
button.clip.has { opacity: 1; border-color: var(--accent); color: var(--accent); }
|
||||
tr:hover button.clip { opacity: 1; }
|
||||
dialog.wide { width: min(780px, 94vw); }
|
||||
dialog .check { display: flex; gap: 8px; align-items: center; margin: 8px 0; font-size: 14px; color: var(--text); }
|
||||
dialog .check input { width: auto; }
|
||||
|
||||
@media (max-width: 700px) {
|
||||
body { font-size: 15px; }
|
||||
header { padding: 10px 12px; gap: 8px; }
|
||||
header h1 { font-size: 16px; margin-right: 4px; }
|
||||
header .spacer { display: none; }
|
||||
#bookInfo { flex-basis: 100%; order: 10; }
|
||||
header h1 { flex-basis: 100%; margin: 0; padding: 0; }
|
||||
header .tabs.primary { flex-basis: 100%; order: 5; margin: 0 -12px; padding: 0 12px; }
|
||||
header .tabs.primary button { padding: 10px 14px; }
|
||||
.bookbar { padding: 8px 12px; }
|
||||
#book { flex: 1; min-width: 120px; }
|
||||
.layout { padding: 10px 12px; gap: 12px; }
|
||||
.panel { padding: 12px; }
|
||||
/* 16px inputs stop iOS zooming on focus; larger tap targets */
|
||||
select, input[type=text], input[type=number], input[type=date], input[type=password], textarea { font-size: 16px; padding: 8px 10px; }
|
||||
button, .menu summary { padding: 8px 14px; }
|
||||
.tabs { flex-wrap: nowrap; overflow-x: auto; margin: 0 -12px 12px; padding: 0 12px 4px; scrollbar-width: none; }
|
||||
.tabs::-webkit-scrollbar { display: none; }
|
||||
.tabs button { flex: none; padding: 6px 14px; white-space: nowrap; }
|
||||
.statements { max-height: 40vh; }
|
||||
.stat { flex: 1 1 calc(50% - 10px); min-width: 0; }
|
||||
.menu-items { min-width: 0; width: max-content; max-width: calc(100vw - 24px); }
|
||||
.menu-items.right { right: 0; }
|
||||
.menu-items a { padding: 10px 12px; }
|
||||
.icon-btns { max-width: none; }
|
||||
.tax-input { width: 100px; }
|
||||
.receipt-fields .wide { grid-column: 1 / -1; }
|
||||
.row-toolbar > * { flex: 1 1 auto; }
|
||||
dialog { padding: 14px; max-height: 90vh; }
|
||||
#toast { bottom: 12px; width: calc(100vw - 24px); text-align: center; }
|
||||
.inv-lines input[type=text] { min-width: 160px; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Cashbook</h1>
|
||||
<h1>Accounts</h1>
|
||||
<nav class="tabs primary" id="nav" aria-label="Sections"></nav>
|
||||
<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="bookbar" id="bookbar">
|
||||
<select id="book" aria-label="Cashbook"></select>
|
||||
<button id="newBookBtn">New book…</button>
|
||||
<details class="menu" id="exportMenu">
|
||||
@@ -145,32 +233,11 @@
|
||||
<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>
|
||||
|
||||
<div class="layout">
|
||||
<aside class="panel">
|
||||
<h2><span class="grow">ANZ statements</span>
|
||||
<button id="manageBtn" title="Upload, view, rename and remove statements">Manage</button>
|
||||
<button id="uploadBtn" title="Add CSV or PDF statements">Add…</button>
|
||||
<input id="uploadInput" type="file" accept=".csv,.pdf" multiple hidden>
|
||||
</h2>
|
||||
<div class="muted small">Tick statements, then preview to see what will be added. You can also drop files anywhere on this page.</div>
|
||||
<ul id="statements" class="statements"></ul>
|
||||
<div class="actions">
|
||||
<button id="previewBtn" class="primary" disabled>Preview import</button>
|
||||
<button id="clearSel">Clear selection</button>
|
||||
</div>
|
||||
</aside>
|
||||
<input id="uploadInput" type="file" accept=".csv,.pdf" multiple hidden>
|
||||
|
||||
<main class="panel" id="main"></main>
|
||||
</div>
|
||||
@@ -211,6 +278,44 @@
|
||||
</form>
|
||||
</dialog>
|
||||
|
||||
<dialog id="attDialog" class="wide">
|
||||
<div class="receipt-head">
|
||||
<h2 class="grow" style="margin:0;font-size:16px" id="attTitle">Receipts and invoices</h2>
|
||||
<button id="attAdd" class="primary">Add file…</button>
|
||||
<button id="attClose">Close</button>
|
||||
</div>
|
||||
<div id="attSub" class="muted small" style="margin-bottom:10px"></div>
|
||||
<div id="attBody" class="receipts"></div>
|
||||
</dialog>
|
||||
|
||||
<dialog id="applyDialog">
|
||||
<form method="dialog">
|
||||
<h2 style="margin:0 0 4px;font-size:16px">Update the transaction?</h2>
|
||||
<div class="muted small" id="applySub"></div>
|
||||
<div id="applyBody"></div>
|
||||
<div class="actions" style="justify-content:flex-end">
|
||||
<button value="cancel" formnovalidate>Leave as is</button>
|
||||
<button value="ok" class="primary">Apply ticked</button>
|
||||
</div>
|
||||
</form>
|
||||
</dialog>
|
||||
|
||||
<dialog id="pagesDialog">
|
||||
<form method="dialog">
|
||||
<h2 style="margin:0 0 4px;font-size:16px" id="pagesTitle">Add files</h2>
|
||||
<div class="muted small">Are they photos of one receipt (e.g. the top and bottom of a long one) or pages of one invoice, or a different receipt each?</div>
|
||||
<div class="actions" style="justify-content:flex-end">
|
||||
<button value="cancel" formnovalidate>Cancel</button>
|
||||
<button value="separate" id="pagesSeparate">Separate receipts</button>
|
||||
<button value="one" class="primary" id="pagesOne">One receipt</button>
|
||||
</div>
|
||||
</form>
|
||||
</dialog>
|
||||
|
||||
<input id="receiptInput" type="file" accept="image/jpeg,image/png,image/webp,application/pdf" multiple hidden>
|
||||
<input id="pageInput" type="file" accept="image/jpeg,image/png,image/webp,application/pdf" multiple hidden>
|
||||
<input id="cameraInput" type="file" accept="image/*" capture="environment" hidden>
|
||||
|
||||
<div id="busy" class="busy"></div>
|
||||
<div id="dropOverlay">Drop ANZ statements to add them</div>
|
||||
<div id="toast"></div>
|
||||
@@ -269,7 +374,6 @@ function localStorageGet(k) { try { return localStorage.getItem(k); } catch { re
|
||||
function localStorageSet(k, v) { try { localStorage.setItem(k, v); } catch {} }
|
||||
|
||||
async function loadSummary() {
|
||||
updatePreviewBtn();
|
||||
if (!state.book) { state.summary = null; render(); return; }
|
||||
state.summary = await call(`/api/summary?book=${encodeURIComponent(state.book)}`);
|
||||
const s = state.summary;
|
||||
@@ -290,24 +394,6 @@ async function loadStatements() {
|
||||
state.statements = await call(`/api/statements${state.book ? "?book=" + encodeURIComponent(state.book) : ""}`);
|
||||
const known = new Set(state.statements.map(s => s.path));
|
||||
for (const p of [...state.selected]) if (!known.has(p)) state.selected.delete(p);
|
||||
renderStatements();
|
||||
}
|
||||
function renderStatements() {
|
||||
const ul = $("#statements");
|
||||
if (!state.statements.length) { ul.innerHTML = `<li class="muted">No statements found. Use “Add…”.</li>`; return; }
|
||||
let html = "", folder = null;
|
||||
const order = [...new Set(state.statements.map(s => s.folder))]; // server lists folders in priority order
|
||||
const sorted = [...state.statements].sort((a, b) => (order.indexOf(a.folder) - order.indexOf(b.folder)) || ((b.first || "").localeCompare(a.first || "")));
|
||||
for (const s of sorted) {
|
||||
if (s.folder !== folder) { folder = s.folder; html += `<li class="folder">${esc(folder)}</li>`; }
|
||||
const id = "st" + btoa(unescape(encodeURIComponent(s.path))).replace(/[^a-z0-9]/gi, "");
|
||||
const desc = s.error ? `<span class="err small">Can't read: ${esc(s.error)}</span>`
|
||||
: `<span class="muted small">${s.count} transactions · ${fmtDate(s.first)} – ${fmtDate(s.last)}${s.periods.length > 1 ? ` · ${s.periods.length} statements` : ""}</span>`;
|
||||
html += `<li><input type="checkbox" id="${id}" data-path="${esc(s.path)}" ${state.selected.has(s.path) ? "checked" : ""} ${s.error || !s.count ? "disabled" : ""}>
|
||||
<label for="${id}"><div class="name">${esc(s.name)}</div>${desc}<div class="status">${importChip(s)}</div></label></li>`;
|
||||
}
|
||||
ul.innerHTML = html;
|
||||
updatePreviewBtn();
|
||||
}
|
||||
function importChip(s) {
|
||||
if (s.error || s.imported == null || !state.book) return "";
|
||||
@@ -354,7 +440,6 @@ async function deleteStatement(path) {
|
||||
if (state.preview?.single && state.preview.statements[0] === path) { state.preview = null; state.view = "statements"; }
|
||||
await loadStatements(); render(); toast(`Removed ${st.name}`);
|
||||
}
|
||||
function updatePreviewBtn() { $("#previewBtn").disabled = !state.selected.size || !state.book; }
|
||||
|
||||
// ---- import ----
|
||||
async function preview() {
|
||||
@@ -379,10 +464,14 @@ async function doImport() {
|
||||
// ---- rendering ----
|
||||
function render() {
|
||||
const main = $("#main");
|
||||
if (state.view === "statements") { main.innerHTML = (state.summary ? tabsHtml() : "") + renderStatementsPage(); return; }
|
||||
if (state.view === "preview" && state.preview && !state.book) { main.innerHTML = renderPreview(); return; }
|
||||
$("#nav").innerHTML = navHtml();
|
||||
$("#bookbar").hidden = !BOOK_VIEWS.includes(state.view);
|
||||
const pages = { statements: renderStatementsPage, receipts: renderReceipts, invoices: renderInvoices,
|
||||
customers: renderCustomers, suppliers: renderSuppliers, mileage: renderMileage };
|
||||
if (pages[state.view]) { main.innerHTML = tabsHtml() + pages[state.view](); return; }
|
||||
if (state.view === "preview" && state.preview && !state.book) { main.innerHTML = tabsHtml() + renderPreview(); return; }
|
||||
if (!state.book) {
|
||||
main.innerHTML = `<div class="empty-state">No cashbook yet.<br><br><button class="primary" onclick="openNewBook()">Create a cashbook</button></div>`;
|
||||
main.innerHTML = tabsHtml() + `<div class="empty-state">No cashbook yet.<br><br><button class="primary" onclick="openNewBook()">Create a cashbook</button></div>`;
|
||||
return;
|
||||
}
|
||||
if (!state.summary) { main.innerHTML = ""; return; }
|
||||
@@ -393,15 +482,27 @@ function render() {
|
||||
else if (state.view === "month" && state.month) main.innerHTML = tabs + renderMonth();
|
||||
else main.innerHTML = tabs + renderSummary();
|
||||
}
|
||||
// the cashbook (year, GST return, income tax and the months) is one tab with its own row beneath
|
||||
const BOOK_VIEWS = ["summary", "gst", "tax", "month"];
|
||||
function navHtml() {
|
||||
const inBook = BOOK_VIEWS.includes(state.view);
|
||||
const tab = (view, label, on = state.view === view) => `<button data-view="${view}" class="${on ? "on" : ""}">${label}</button>`;
|
||||
return `${tab("summary", "Cashbook", inBook)}
|
||||
<span class="sep"></span>
|
||||
${tab("receipts", `Receipts${unmatchedCount() ? ` <span class="count">${unmatchedCount()}</span>` : ""}`)}
|
||||
${tab("invoices", "Invoices")}${tab("customers", "Customers")}${tab("suppliers", "Suppliers")}
|
||||
<span class="sep"></span>
|
||||
${tab("mileage", "Mileage")}${tab("statements", "Statements")}
|
||||
${state.preview ? tab("preview", state.preview.single ? "Statement" : "Import preview") : ""}`;
|
||||
}
|
||||
// the row under the top bar: the cashbook's own sections and months
|
||||
function tabsHtml() {
|
||||
if (!BOOK_VIEWS.includes(state.view) || !state.summary) return "";
|
||||
const tab = (view, label) => `<button data-view="${view}" class="${state.view === view ? "on" : ""}">${label}</button>`;
|
||||
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>
|
||||
${tab("summary", "Year")}${tab("gst", "GST return")}${tab("tax", "Income tax")}
|
||||
<span class="sep"></span>
|
||||
${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>
|
||||
${state.preview ? `<button data-view="preview" class="${state.view === "preview" ? "on" : ""}">${state.preview.single ? "Statement" : "Import preview"}</button>` : ""}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
@@ -605,15 +706,17 @@ function renderMonth() {
|
||||
<label class="small"><input type="checkbox" id="onlyUnalloc" ${state.onlyUnalloc ? "checked" : ""}> Only unallocated</label>
|
||||
</div>
|
||||
${m.rows.length ? `<div class="scroll"><table>
|
||||
<thead><tr><th>Date</th><th>Reference</th><th>Origin</th><th class="num">In</th><th class="num">Out</th><th>GST</th><th class="num">GST amt</th><th>Category</th></tr></thead>
|
||||
<thead><tr><th>Date</th><th>Reference</th><th>Origin</th><th class="num">In</th><th class="num">Out</th><th>GST</th><th class="num">GST amt</th><th>Category</th><th title="Receipts and invoices"></th></tr></thead>
|
||||
<tbody>${rows.map(r => `<tr>
|
||||
<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", "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>
|
||||
<td>${catSelect(r)}</td>
|
||||
<td><button class="clip ${r.attachments ? "has" : ""}" data-act="row-att" data-row="${r.row}"
|
||||
title="${r.attachments ? plural(r.attachments, "receipt/invoice") : "Attach a receipt or invoice"}">📎${r.attachments || ""}</button></td></tr>`).join("") || `<tr><td colspan="9" 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="4"></td></tr></tfoot>
|
||||
</table></div>` : `<div class="empty-state">No transactions this month.</div>`}`;
|
||||
}
|
||||
|
||||
@@ -649,6 +752,232 @@ function renderPreview() {
|
||||
</table></div>`;
|
||||
}
|
||||
|
||||
// ---- receipts and invoices ----
|
||||
const unmatchedCount = () => (state.receipts || []).filter(a => !a.transaction).length;
|
||||
async function loadReceipts(show) {
|
||||
[state.receipts, state.suppliers] = await Promise.all([call("/api/attachments"), call("/api/suppliers")]);
|
||||
if (show) state.view = "receipts";
|
||||
if (show || state.view === "receipts" || state.summary) render();
|
||||
}
|
||||
// Upload receipt files: each as its own receipt, or all as pages of one (asPages), or as more pages
|
||||
// of the existing receipt `into`. Returns the receipts as they are after the uploads.
|
||||
async function uploadReceipts(files, { row = null, asPages = false, into = null } = {}) {
|
||||
const added = [], errors = [];
|
||||
let target = into;
|
||||
for (const f of files) {
|
||||
busyCount++; $("#busy").classList.add("on");
|
||||
try {
|
||||
const name = `filename=${encodeURIComponent(f.name)}`;
|
||||
const url = target ? `/api/attachments/${target}/pages?${name}` : `/api/attachments?${name}${row ? `&row=${row}` : ""}`;
|
||||
const res = await fetch(url, { method: "POST", body: f });
|
||||
if (res.status === 401) { signInAgain(); break; }
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (res.ok) {
|
||||
if (target && added.length) added[added.length - 1] = data; else added.push(data);
|
||||
if (asPages || into) target = data.id;
|
||||
if (data.ocr_error) errors.push(`${f.name} was saved, but ${data.ocr_error}. Fill in its details by hand.`);
|
||||
} else errors.push(`${f.name}: ${errorText(data, res)}`);
|
||||
} finally { if (--busyCount === 0) $("#busy").classList.remove("on"); }
|
||||
}
|
||||
const n = files.length - errors.filter(e => !e.includes(" was saved, ")).length;
|
||||
if (errors.length) toast(errors.join(" · "), true);
|
||||
else if (into) toast(`Added ${plural(n, "page")}`);
|
||||
else if (added.length) toast((asPages ? `Added a receipt with ${plural(n, "page")}` : `Added ${plural(added.length, "file")}`) +
|
||||
(row ? "" : added.some(a => a.suggestions.length) ? ". Check the suggested matches." : ""));
|
||||
return added;
|
||||
}
|
||||
// several files at once: pages of one receipt, or a receipt each? (null = cancelled)
|
||||
function askPages(n) {
|
||||
$("#pagesTitle").textContent = `Add ${n} files`;
|
||||
$("#pagesOne").textContent = `One receipt, ${n} pages`;
|
||||
$("#pagesSeparate").textContent = `${n} separate receipts`;
|
||||
const d = $("#pagesDialog");
|
||||
return new Promise(resolve => {
|
||||
d.addEventListener("close", () => resolve(d.returnValue === "cancel" || !d.returnValue ? null : d.returnValue === "one"), { once: true });
|
||||
d.returnValue = ""; d.showModal();
|
||||
});
|
||||
}
|
||||
// after linking, offer the GST type / category the receipt suggests; returns what was applied
|
||||
function askApply(a) {
|
||||
const p = a.proposed || {}, keys = Object.keys(p);
|
||||
if (!keys.length) return Promise.resolve(null);
|
||||
const t = a.transaction, label = { gst: "GST", category: "Category" };
|
||||
$("#applySub").textContent = `${fmtDate(t.date)} · ${t.reference} · ${fmt(t.amount)}`;
|
||||
$("#applyBody").innerHTML = keys.map(k => `<label class="check"><input type="checkbox" data-apply="${k}" checked>
|
||||
${label[k]}: ${esc(t[k] || (k === "category" ? "Unallocated" : ""))} → <strong>${esc(p[k])}</strong></label>`).join("") +
|
||||
`<div class="muted small">${p.gst ? "The receipt shows GST, so it can be claimed. " : ""}You can change these later on the month's sheet.</div>`;
|
||||
const d = $("#applyDialog");
|
||||
return new Promise(resolve => {
|
||||
d.addEventListener("close", () => resolve(d.returnValue === "ok"
|
||||
? Object.fromEntries([...d.querySelectorAll("[data-apply]:checked")].map(c => [c.dataset.apply, p[c.dataset.apply]])) : null), { once: true });
|
||||
d.returnValue = ""; d.showModal();
|
||||
});
|
||||
}
|
||||
async function confirmChanges(a) {
|
||||
const apply = await askApply(a);
|
||||
if (apply && Object.keys(apply).length) {
|
||||
a = await call(`/api/attachments/${a.id}/link`, { row: a.transaction.row, apply });
|
||||
toast("Transaction updated");
|
||||
}
|
||||
return a;
|
||||
}
|
||||
async function refreshAfterChange() {
|
||||
const jobs = [loadReceipts()];
|
||||
if (state.book) jobs.push(call(`/api/summary?book=${encodeURIComponent(state.book)}`).then(s => state.summary = s));
|
||||
if (state.view === "month" && state.sheet)
|
||||
jobs.push(call(`/api/month?book=${encodeURIComponent(state.book)}&sheet=${state.sheet}`).then(m => state.month = m));
|
||||
await Promise.all(jobs);
|
||||
render();
|
||||
if ($("#attDialog").open) await renderRowAttachments();
|
||||
}
|
||||
async function gotoTransaction(book, sheet) {
|
||||
$("#attDialog").close();
|
||||
if (book !== state.book) { state.book = book; $("#book").value = book; localStorageSet("book", book); updateExportLinks(); await loadSummary(); }
|
||||
await loadMonth(sheet);
|
||||
}
|
||||
// buttons on receipt cards, in the Receipts view and the row dialog
|
||||
async function receiptAction(b) {
|
||||
const id = Number(b.dataset.id);
|
||||
if (b.dataset.act === "att-link") {
|
||||
const a = await call(`/api/attachments/${id}/link`, { row: Number(b.dataset.row) });
|
||||
toast(`Linked to ${a.transaction.reference}`);
|
||||
await confirmChanges(a);
|
||||
} else if (b.dataset.act === "att-unlink") {
|
||||
await call(`/api/attachments/${id}/unlink`, {});
|
||||
} else if (b.dataset.act === "att-delete") {
|
||||
const a = state.receipts.find(x => x.id === id) || state.rowAtt?.list.find(x => x.id === id);
|
||||
const pages = a?.files.length || 1;
|
||||
if (!confirm(`Delete ${a?.vendor || a?.filename || "this receipt"}${pages > 1 ? ` and its ${pages} pages` : ""}? ` +
|
||||
`The ${pages > 1 ? "files are" : "file is"} removed from data/attachments.`)) return true;
|
||||
await call(`/api/attachments/${id}`, undefined, "DELETE");
|
||||
} else if (b.dataset.act === "att-addpage") {
|
||||
state.pageTarget = id; $("#pageInput").click(); return true;
|
||||
} else if (b.dataset.act === "att-rmpage") {
|
||||
if (!confirm("Remove this page? Its file is removed from data/attachments.")) return true;
|
||||
await call(`/api/attachments/${id}/pages/${b.dataset.file}`, undefined, "DELETE");
|
||||
} else if (b.dataset.act === "att-reread") {
|
||||
if (!confirm("Read the details again from the pages? Anything you corrected by hand is replaced.")) return true;
|
||||
await call(`/api/attachments/${id}/reread`, {});
|
||||
} else if (b.dataset.act === "att-goto") {
|
||||
await gotoTransaction(b.dataset.book, b.dataset.sheet); return true;
|
||||
} else return false;
|
||||
await refreshAfterChange();
|
||||
return true;
|
||||
}
|
||||
async function saveReceiptField(input) {
|
||||
const f = input.dataset.attField, raw = input.value.trim();
|
||||
let value = ["total", "gst"].includes(f) ? (raw === "" ? null : Number(raw)) : (raw || null);
|
||||
if (f === "supplier_id") {
|
||||
if (raw === "new") {
|
||||
const a = (state.receipts || []).find(x => x.id === Number(input.dataset.id)) || state.rowAtt?.list.find(x => x.id === Number(input.dataset.id));
|
||||
await askNewSupplier(a);
|
||||
return refreshAfterChange();
|
||||
} else value = raw ? Number(raw) : null;
|
||||
}
|
||||
const focus = document.activeElement?.dataset;
|
||||
const where = focus?.attField ? `[data-id="${focus.id}"][data-att-field="${focus.attField}"]` : null;
|
||||
await call(`/api/attachments/${input.dataset.id}`, { [f]: value }, "PATCH");
|
||||
await refreshAfterChange();
|
||||
if (where) document.querySelector(where)?.focus(); // keep the cursor where the user tabbed to
|
||||
}
|
||||
function receiptCard(a, inDialog) {
|
||||
const t = a.transaction, many = a.files.length > 1;
|
||||
const page = (f, i) => {
|
||||
const url = `/api/attachments/${a.id}/files/${f.id}`, title = many ? `Page ${i + 1}: ${f.filename}` : f.filename;
|
||||
return `<div class="page">${f.mime.startsWith("image/")
|
||||
? `<a href="${url}" target="_blank" rel="noopener" title="${esc(title)} (open full size)"><img src="${url}" alt="${esc(title)}" loading="lazy"></a>`
|
||||
: `<a href="${url}" target="_blank" rel="noopener" class="pdf-thumb" title="${esc(title)} (open)">PDF</a>`}
|
||||
${many ? `<button class="rm" data-act="att-rmpage" data-id="${a.id}" data-file="${f.id}" title="Remove page ${i + 1}" aria-label="Remove page ${i + 1}">×</button>` : ""}</div>`;
|
||||
};
|
||||
const thumb = `<div class="pages ${many ? "many" : ""}">${a.files.map(page).join("")}</div>
|
||||
<button class="add-page" data-act="att-addpage" data-id="${a.id}" title="Another photo of this receipt, or another page of the invoice">+ Add page</button>`;
|
||||
const size = a.files.reduce((n, f) => n + f.size, 0);
|
||||
const input = (k, label, val, type = "text", cls = "") =>
|
||||
`<label class="${cls}">${label}<input type="${type}" ${type === "number" ? 'step="0.01" min="0"' : ""} data-att-field="${k}" data-id="${a.id}" value="${esc(val ?? "")}"></label>`;
|
||||
const status = a.ocr_status === "done" ? "" : a.ocr_status === "failed"
|
||||
? `<span class="chip failed" title="Fill in the details by hand">text not read</span>` : "";
|
||||
let match;
|
||||
if (t) {
|
||||
match = `<div class="match"><span class="chip linked">linked</span>
|
||||
<button class="linkish" data-act="att-goto" data-id="${a.id}" data-book="${esc(t.book)}" data-sheet="${t.sheet}">${fmtDate(t.date)} · ${esc(t.reference)}</button>
|
||||
<span class="${t.amount > 0 ? "in" : "out"}">${fmt(t.amount)}</span> <span class="muted small">${esc(t.book)} · ${esc(t.gst)}${t.category ? " · " + esc(t.category) : ""}</span>
|
||||
<button data-act="att-unlink" data-id="${a.id}" style="margin-left:8px">Unlink</button></div>`;
|
||||
} else if (a.suggestions.length) {
|
||||
match = `<div class="match"><strong class="small">Matching bank transactions</strong><ul>${a.suggestions.map(s => `<li>
|
||||
<span class="grow">${fmtDate(s.date)} · ${esc(s.reference)} <span class="${s.amount > 0 ? "in" : "out"}">${fmt(s.amount)}</span>
|
||||
<span class="muted small">${esc(s.book)}${s.days_apart != null ? ` · ${s.days_apart ? plural(s.days_apart, "day") + " apart" : "same day"}` : ""}${s.attachments ? ` · already has ${plural(s.attachments, "file")}` : ""}</span></span>
|
||||
<button class="primary" data-act="att-link" data-id="${a.id}" data-row="${s.row}">Link</button></li>`).join("")}</ul></div>`;
|
||||
} else {
|
||||
match = `<div class="match muted small">${a.total == null ? "Enter the total to find the bank transaction." :
|
||||
`No bank transaction of ${fmt(a.total)} within 7 days${a.doc_date ? " of " + fmtDate(a.doc_date) : ""}. Check the total and date, or import the statement first.`}</div>`;
|
||||
}
|
||||
return `<div class="receipt">
|
||||
<div class="thumb">${thumb}</div>
|
||||
<div style="min-width:0">
|
||||
<div class="receipt-head"><span class="grow"><strong>${esc(a.vendor || a.filename)}</strong>
|
||||
<span class="muted small">${many ? `${a.files.length} pages` : esc(a.filename)} · ${fmtSize(size)}</span> ${status}</span>
|
||||
${inDialog ? `<button data-act="att-unlink" data-id="${a.id}">Unlink</button>` : ""}
|
||||
<button data-act="att-delete" data-id="${a.id}">Delete</button></div>
|
||||
<div class="receipt-fields">
|
||||
${input("vendor", "Supplier", a.vendor, "text", "wide")}
|
||||
${input("doc_date", "Date", a.doc_date, "date")}
|
||||
${input("total", "Total (incl GST)", a.total?.toFixed(2), "number")}
|
||||
${input("gst", "GST", a.gst?.toFixed(2), "number")}
|
||||
${input("gst_number", "GST number", a.gst_number)}
|
||||
<label>Linked supplier<select data-att-field="supplier_id" data-id="${a.id}">
|
||||
<option value="">None</option>
|
||||
${(state.suppliers || []).map(x => `<option value="${x.id}" ${x.id === a.supplier_id ? "selected" : ""}>${esc(x.name)}</option>`).join("")}
|
||||
<option value="new">+ New supplier from this…</option></select></label>
|
||||
<label>Type<select data-att-field="kind" data-id="${a.id}">${["receipt", "invoice"].map(k => `<option value="${k}" ${k === a.kind ? "selected" : ""}>${k[0].toUpperCase() + k.slice(1)}</option>`).join("")}</select></label>
|
||||
</div>
|
||||
${a.items.length ? `<details><summary>${plural(a.items.length, "line item")}</summary><table><tbody>
|
||||
${a.items.map(i => `<tr><td style="white-space:normal">${esc(i.description)}</td><td class="num">${fmt(i.amount)}</td></tr>`).join("")}</tbody></table></details>` : ""}
|
||||
${a.raw_text ? `<details><summary>Text read from the ${many ? "pages" : a.files[0].mime === "application/pdf" ? "PDF" : "photo"}</summary><pre>${esc(a.raw_text)}</pre>
|
||||
<button class="linkish small" data-act="att-reread" data-id="${a.id}">Read the details again from this text</button></details>` : ""}
|
||||
${inDialog ? "" : match}
|
||||
</div></div>`;
|
||||
}
|
||||
function renderReceipts() {
|
||||
const all = state.receipts || [];
|
||||
const filter = state.receiptFilter || "unmatched";
|
||||
const sup = state.receiptSupplier || "";
|
||||
const list = (filter === "all" ? all : all.filter(a => !a.transaction)).filter(a => !sup || String(a.supplier_id) === sup);
|
||||
return `
|
||||
<div class="drop" id="receiptDrop"><strong>Drop receipt photos or invoices here</strong> or
|
||||
<button class="linkish" id="chooseReceipts">choose files</button> · <button class="linkish" id="takePhoto">take a photo</button><br>
|
||||
<span class="small">JPEG, PNG or PDF. A long receipt can be several photos: add them together, or use “+ Add page” on it later.
|
||||
The supplier, date, total and GST are read from the pages on this server, then matched to your bank transactions.</span></div>
|
||||
<div class="row-toolbar">
|
||||
<strong style="font-size:16px">Receipts and invoices</strong>
|
||||
<div class="tabs" style="margin:0">
|
||||
<button data-receipts="unmatched" class="${filter === "unmatched" ? "on" : ""}">Not linked (${unmatchedCount()})</button>
|
||||
<button data-receipts="all" class="${filter === "all" ? "on" : ""}">All (${all.length})</button>
|
||||
</div>
|
||||
<select id="receiptSupplier" title="Show one supplier's receipts"><option value="">All suppliers</option>
|
||||
${(state.suppliers || []).map(x => `<option value="${x.id}" ${String(x.id) === sup ? "selected" : ""}>${esc(x.name)}</option>`).join("")}</select>
|
||||
</div>
|
||||
${list.length ? `<div class="receipts">${list.map(a => receiptCard(a)).join("")}</div>`
|
||||
: `<div class="empty-state">${all.length ? "Every receipt is linked to a transaction." : "No receipts or invoices yet."}</div>`}`;
|
||||
}
|
||||
// the attachments of one transaction, from the 📎 button on a month's sheet
|
||||
async function openRowAttachments(row) {
|
||||
state.rowAtt = { row: state.month.rows.find(r => r.row === row), list: [] };
|
||||
await renderRowAttachments();
|
||||
if (!$("#attDialog").open) $("#attDialog").showModal();
|
||||
}
|
||||
async function renderRowAttachments() {
|
||||
const r = state.rowAtt.row;
|
||||
state.rowAtt.list = await call(`/api/row/${r.row}/attachments`);
|
||||
$("#attSub").textContent = `${fmtDate(r.date)} · ${r.reference} · ${fmt(r.income || -r.expense)}`;
|
||||
const list = state.rowAtt.list;
|
||||
const unlinked = (state.receipts || []).filter(a => !a.transaction && a.suggestions.some(s => s.row === r.row));
|
||||
$("#attBody").innerHTML = (list.length ? list.map(a => receiptCard(a, true)).join("")
|
||||
: `<div class="empty-state" style="padding:24px">Nothing attached yet. Add a photo of the receipt or the invoice PDF.</div>`) +
|
||||
(unlinked.length ? `<div class="match"><strong class="small">Unlinked receipts that match this transaction</strong><ul>${unlinked.map(a => `<li>
|
||||
<span class="grow">${esc(a.vendor || a.filename)} <span class="muted small">${a.doc_date ? fmtDate(a.doc_date) + " · " : ""}${fmt(a.total)}</span></span>
|
||||
<button class="primary" data-act="att-link" data-id="${a.id}" data-row="${r.row}">Link</button></li>`).join("")}</ul></div>` : "");
|
||||
}
|
||||
|
||||
// ---- new book ----
|
||||
function openNewBook() {
|
||||
const sel = state.statements.filter(s => state.selected.has(s.path) && s.first);
|
||||
@@ -687,14 +1016,6 @@ 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();
|
||||
});
|
||||
$("#clearSel").addEventListener("click", () => { state.selected.clear(); renderStatements(); if (state.view === "statements") render(); });
|
||||
$("#manageBtn").addEventListener("click", () => { state.view = "statements"; render(); });
|
||||
$("#previewBtn").addEventListener("click", guard(preview));
|
||||
$("#uploadBtn").addEventListener("click", () => $("#uploadInput").click());
|
||||
$("#uploadInput").addEventListener("change", guard(async e => {
|
||||
const files = [...e.target.files]; e.target.value = "";
|
||||
await uploadFiles(files);
|
||||
@@ -702,20 +1023,69 @@ $("#uploadInput").addEventListener("change", guard(async e => {
|
||||
// drag and drop anywhere on the page
|
||||
let dragDepth = 0;
|
||||
const hasFiles = e => [...(e.dataTransfer?.types || [])].includes("Files");
|
||||
window.addEventListener("dragenter", e => { if (hasFiles(e)) { dragDepth++; document.body.classList.add("dragging"); } });
|
||||
const dropsReceipts = () => state.view === "receipts" || $("#attDialog").open;
|
||||
window.addEventListener("dragenter", e => {
|
||||
if (!hasFiles(e)) return;
|
||||
dragDepth++; document.body.classList.add("dragging");
|
||||
$("#dropOverlay").textContent = dropsReceipts() ? "Drop receipts or invoices to add them" : "Drop ANZ statements to add them";
|
||||
});
|
||||
window.addEventListener("dragleave", e => { if (hasFiles(e) && --dragDepth <= 0) { dragDepth = 0; document.body.classList.remove("dragging"); } });
|
||||
window.addEventListener("dragover", e => { if (hasFiles(e)) e.preventDefault(); });
|
||||
window.addEventListener("drop", guard(async e => {
|
||||
if (!hasFiles(e)) return;
|
||||
e.preventDefault(); dragDepth = 0; document.body.classList.remove("dragging");
|
||||
if (dropsReceipts()) return addReceipts([...e.dataTransfer.files], $("#attDialog").open ? state.rowAtt.row.row : null);
|
||||
await uploadFiles([...e.dataTransfer.files]);
|
||||
}));
|
||||
$("#main").addEventListener("click", guard(async e => {
|
||||
// receipts: from the Receipts view (row = null) or a transaction's 📎 dialog
|
||||
async function addReceipts(files, row) {
|
||||
if (!files.length) return;
|
||||
const asPages = files.length > 1 ? await askPages(files.length) : false;
|
||||
if (asPages === null) return;
|
||||
const added = await uploadReceipts(files, { row, asPages });
|
||||
if (!added.length) return;
|
||||
await refreshAfterChange();
|
||||
const last = added[added.length - 1];
|
||||
if (row && await confirmChanges(last) !== last) await refreshAfterChange();
|
||||
await offerSuppliers(added);
|
||||
await refreshAfterChange();
|
||||
}
|
||||
for (const id of ["#receiptInput", "#cameraInput"])
|
||||
$(id).addEventListener("change", guard(async e => {
|
||||
const files = [...e.target.files]; e.target.value = "";
|
||||
await addReceipts(files, $("#attDialog").open ? state.rowAtt.row.row : null);
|
||||
}));
|
||||
$("#pageInput").addEventListener("change", guard(async e => {
|
||||
const files = [...e.target.files]; e.target.value = "";
|
||||
if (!files.length || !state.pageTarget) return;
|
||||
const added = await uploadReceipts(files, { into: state.pageTarget });
|
||||
if (added.length) await refreshAfterChange();
|
||||
}));
|
||||
$("#attAdd").addEventListener("click", () => $("#receiptInput").click());
|
||||
$("#attClose").addEventListener("click", () => $("#attDialog").close());
|
||||
$("#attDialog").addEventListener("click", guard(async e => {
|
||||
const b = e.target.closest("[data-act]");
|
||||
if (b && !b.disabled) await receiptAction(b);
|
||||
}));
|
||||
$("#attDialog").addEventListener("change", guard(async e => { if (e.target.dataset.attField) await saveReceiptField(e.target); }));
|
||||
const navClick = 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");
|
||||
const b = e.target.closest("[data-sheet],[data-view],[data-act],[data-receipts],[data-inv],[data-mi],#importBtn,#cancelPreview,#chooseFiles,#previewBtn2,#chooseReceipts,#takePhoto");
|
||||
if (!b || b.disabled) return;
|
||||
if (b.id === "chooseFiles") return $("#uploadInput").click();
|
||||
if (b.id === "chooseReceipts") return $("#receiptInput").click();
|
||||
if (b.id === "takePhoto") return $("#cameraInput").click();
|
||||
if (b.dataset.receipts) { state.receiptFilter = b.dataset.receipts; return render(); }
|
||||
if (b.dataset.act === "row-att") return openRowAttachments(Number(b.dataset.row));
|
||||
if (b.dataset.act?.startsWith("att-")) return receiptAction(b);
|
||||
if (b.dataset.view === "receipts") return loadReceipts(true);
|
||||
if (b.dataset.view === "customers") return loadCustomers();
|
||||
if (b.dataset.view === "suppliers") return loadSuppliers();
|
||||
if (b.dataset.inv) return invoiceAction(b);
|
||||
if (b.dataset.mi) return mileageAction(b);
|
||||
if (b.dataset.view === "invoices") return loadInvoices();
|
||||
if (b.dataset.view === "mileage") return loadMileage();
|
||||
if (b.id === "previewBtn2") return preview();
|
||||
if (b.dataset.act === "view") return viewStatement(b.dataset.path);
|
||||
if (b.dataset.act === "rename") return renameStatement(b.dataset.path);
|
||||
@@ -726,14 +1096,20 @@ $("#main").addEventListener("click", guard(async e => {
|
||||
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("click", navClick);
|
||||
$("#nav").addEventListener("click", navClick);
|
||||
$("#main").addEventListener("change", guard(async e => {
|
||||
if (e.target.id === "receiptSupplier") { state.receiptSupplier = e.target.value; return render(); }
|
||||
if (e.target.id === "invoiceCustomer") { state.invoiceCustomer = e.target.value; return render(); }
|
||||
if (e.target.id === "onlyUnalloc") { state.onlyUnalloc = e.target.checked; return render(); }
|
||||
if (e.target.dataset.taxBox) return saveTaxInput(e.target);
|
||||
if (e.target.dataset.mi) return mileageChange(e.target);
|
||||
if (e.target.dataset.attField) return saveReceiptField(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();
|
||||
return render();
|
||||
}
|
||||
const { row, field } = e.target.dataset; if (!row) return;
|
||||
state.month = await call("/api/row", { book: state.book, sheet: state.sheet, row: Number(row), [field]: e.target.value });
|
||||
@@ -777,8 +1153,11 @@ document.addEventListener("click", e => {
|
||||
guard(async () => {
|
||||
await loadMe();
|
||||
await loadBooks();
|
||||
await loadStatements();
|
||||
await Promise.all([loadStatements(), loadReceipts()]);
|
||||
})();
|
||||
</script>
|
||||
<script src="/static/invoices.js"></script>
|
||||
<script src="/static/suppliers.js"></script>
|
||||
<script src="/static/mileage.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,348 @@
|
||||
// ---- invoices: business details, customers, invoices and their payment ----
|
||||
// Uses $, state, call, esc, fmt, fmtDate, plural, toast, guard and render from index.html.
|
||||
const todayIso = () => { const d = new Date(); return new Date(d - d.getTimezoneOffset() * 6e4).toISOString().slice(0, 10); };
|
||||
const addDays = (iso, n) => { const d = new Date(iso + "T00:00:00Z"); d.setUTCDate(d.getUTCDate() + n); return d.toISOString().slice(0, 10); };
|
||||
|
||||
document.body.insertAdjacentHTML("beforeend", `
|
||||
<dialog id="invDialog" class="wide">
|
||||
<form method="dialog" id="invForm">
|
||||
<h2 style="margin:0 0 4px;font-size:16px" id="invTitle">New invoice</h2>
|
||||
<div class="receipt-fields" style="margin-top:10px">
|
||||
<label class="wide">Customer<select id="invCustomer" required></select></label>
|
||||
<label>Invoice date<input id="invIssue" type="date" required></label>
|
||||
<label>Due date<input id="invDue" type="date" required></label>
|
||||
</div>
|
||||
<div class="scroll" style="margin-top:12px"><table class="inv-lines">
|
||||
<thead><tr><th>Description</th><th class="num">Qty</th><th class="num">Unit price (excl GST)</th><th title="GST applies to this line">GST</th><th class="num">Amount</th><th></th></tr></thead>
|
||||
<tbody id="invLines"></tbody>
|
||||
</table></div>
|
||||
<button type="button" id="invAddLine" style="margin-top:6px">Add line</button>
|
||||
<div class="inv-totals" id="invTotals"></div>
|
||||
<label for="invNotes">Notes on the invoice</label>
|
||||
<input id="invNotes" type="text" maxlength="2000" placeholder="e.g. Thanks for your business">
|
||||
<div class="actions" style="justify-content:flex-end">
|
||||
<button value="cancel" formnovalidate>Cancel</button>
|
||||
<button value="ok" class="primary" id="invSave">Save draft</button>
|
||||
</div>
|
||||
</form>
|
||||
</dialog>
|
||||
<dialog id="payDialog">
|
||||
<form method="dialog">
|
||||
<h2 style="margin:0 0 4px;font-size:16px">Record payment</h2>
|
||||
<div class="muted small" id="paySub"></div>
|
||||
<label for="payDate">Date paid</label>
|
||||
<input id="payDate" type="date" required>
|
||||
<div id="payOptions" style="margin-top:8px"></div>
|
||||
<div class="actions" style="justify-content:flex-end">
|
||||
<button value="cancel" formnovalidate>Cancel</button>
|
||||
<button value="ok" class="primary">Mark paid</button>
|
||||
</div>
|
||||
</form>
|
||||
</dialog>
|
||||
<dialog id="bizDialog" class="wide">
|
||||
<form method="dialog" id="bizForm">
|
||||
<h2 style="margin:0 0 4px;font-size:16px">Business details</h2>
|
||||
<div class="muted small">Shown on every invoice. With a GST number, invoices with GST are titled “Tax invoice”.</div>
|
||||
<div class="receipt-fields" style="margin-top:10px">
|
||||
<label class="wide">Business name<input data-biz="name" type="text" maxlength="200"></label>
|
||||
<label>GST number<input data-biz="gst_number" type="text" maxlength="20" placeholder="123-456-789"></label>
|
||||
<label>Phone<input data-biz="phone" type="text" maxlength="50"></label>
|
||||
<label class="wide">Address<textarea data-biz="address" rows="2" maxlength="500"></textarea></label>
|
||||
<label class="wide">Email<input data-biz="email" type="text" maxlength="200"></label>
|
||||
<label class="wide">Bank account for payments<input data-biz="bank_account" type="text" maxlength="50" placeholder="01-0000-0000000-00"></label>
|
||||
<label>Number prefix<input data-biz="invoice_prefix" type="text" maxlength="20"></label>
|
||||
<label>Next number<input data-biz="next_number" type="number" min="1"></label>
|
||||
<label>Payment terms (days)<input data-biz="payment_terms_days" type="number" min="0" max="365"></label>
|
||||
<label class="wide">Footer<input data-biz="footer" type="text" maxlength="1000" placeholder="e.g. Company number, terms"></label>
|
||||
</div>
|
||||
<label>Logo (PNG or JPEG)</label>
|
||||
<div style="display:flex;gap:10px;align-items:center;flex-wrap:wrap">
|
||||
<img id="bizLogo" alt="Logo" style="max-height:48px;max-width:160px" hidden>
|
||||
<button type="button" id="bizLogoAdd">Choose image…</button>
|
||||
<button type="button" id="bizLogoRemove" hidden>Remove</button>
|
||||
<input id="bizLogoInput" type="file" accept="image/png,image/jpeg" hidden>
|
||||
</div>
|
||||
<div class="actions" style="justify-content:flex-end">
|
||||
<button value="cancel" formnovalidate>Cancel</button>
|
||||
<button value="ok" class="primary">Save</button>
|
||||
</div>
|
||||
</form>
|
||||
</dialog>`);
|
||||
|
||||
document.head.insertAdjacentHTML("beforeend", `<style>
|
||||
.inv-lines td { padding: 3px 4px; }
|
||||
.inv-lines input[type=text] { width: 100%; min-width: 200px; }
|
||||
.inv-lines input[type=number] { width: 90px; text-align: right; }
|
||||
.inv-totals { display: grid; grid-template-columns: auto auto; justify-content: end; gap: 2px 18px; margin: 10px 4px; font-variant-numeric: tabular-nums; }
|
||||
.inv-totals div:nth-child(even) { text-align: right; }
|
||||
.inv-totals .grand { font-weight: 700; font-size: 16px; }
|
||||
.chip.draft { background: var(--line); color: var(--muted); }
|
||||
.chip.sent { background: var(--accent-soft); color: var(--accent); }
|
||||
.chip.paid { background: var(--ok-soft); color: var(--in); }
|
||||
.chip.void { background: var(--line); color: var(--muted); text-decoration: line-through; }
|
||||
.chip.overdue { background: var(--bad-soft); color: var(--out); }
|
||||
.contacts td input, .contacts td textarea { width: 100%; min-width: 120px; }
|
||||
.contacts td { vertical-align: top; }
|
||||
dialog textarea { width: 100%; font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--line);
|
||||
border-radius: 6px; padding: 5px 8px; resize: vertical; }
|
||||
#payOptions label { display: flex; gap: 8px; align-items: baseline; margin: 6px 0; font-size: 14px; color: var(--text); }
|
||||
#payOptions input { width: auto; }
|
||||
</style>`);
|
||||
|
||||
async function loadInvoices() {
|
||||
[state.invoices, state.business, state.customers] = await Promise.all([
|
||||
call("/api/invoices"), call("/api/business"), call("/api/customers")]);
|
||||
state.view = "invoices"; render();
|
||||
}
|
||||
|
||||
function renderInvoices() {
|
||||
const all = state.invoices || [], b = state.business || {};
|
||||
const filter = state.invoiceFilter || "all";
|
||||
const unpaid = all.filter(i => i.status === "sent" || i.status === "draft");
|
||||
const cust = state.invoiceCustomer || "";
|
||||
const list = (filter === "unpaid" ? unpaid : filter === "paid" ? all.filter(i => i.status === "paid") : all)
|
||||
.filter(i => !cust || String(i.customer_id) === cust);
|
||||
const owed = all.filter(i => i.status === "sent").reduce((n, i) => n + i.total, 0);
|
||||
const overdue = all.filter(i => i.overdue).reduce((n, i) => n + i.total, 0);
|
||||
const chip = i => i.overdue ? `<span class="chip overdue">overdue</span>` : `<span class="chip ${i.status}">${i.status}</span>`;
|
||||
const btn = (act, i, label, cls = "") => `<button data-inv="${act}" data-id="${i.id}" class="${cls}">${label}</button>`;
|
||||
const actions = i => [
|
||||
`<a href="/api/invoices/${i.id}/pdf" download><button tabindex="-1">PDF</button></a>`,
|
||||
i.status === "draft" ? btn("edit", i, "Edit") + btn("sent", i, "Mark sent") : "",
|
||||
i.status === "sent" ? btn("draft", i, "Back to draft") : "",
|
||||
["draft", "sent"].includes(i.status) ? btn("pay", i, "Paid…", "primary") + btn("void", i, "Void") : "",
|
||||
i.status === "paid" ? btn("unpay", i, "Undo payment") : "",
|
||||
].join("");
|
||||
return `
|
||||
<div class="row-toolbar">
|
||||
<strong style="font-size:16px">Invoices</strong>
|
||||
<div class="tabs" style="margin:0">
|
||||
${[["all", `All (${all.length})`], ["unpaid", `Unpaid (${unpaid.length})`], ["paid", "Paid"]].map(([k, l]) =>
|
||||
`<button data-inv="filter" data-filter="${k}" class="${filter === k ? "on" : ""}">${l}</button>`).join("")}
|
||||
</div>
|
||||
<select id="invoiceCustomer" title="Show one customer's invoices"><option value="">All customers</option>
|
||||
${(state.customers || []).map(c => `<option value="${c.id}" ${String(c.id) === cust ? "selected" : ""}>${esc(c.name)}</option>`).join("")}</select>
|
||||
<span style="flex:1"></span>
|
||||
<button data-inv="business">Business details</button>
|
||||
<button data-inv="new" class="primary">New invoice</button>
|
||||
</div>
|
||||
${b.name ? "" : `<p class="small" style="color:var(--warn)">Add your <button class="linkish" data-inv="business">business details</button> (name, GST number, bank account) before sending invoices.</p>`}
|
||||
<div class="stats">
|
||||
<div class="stat"><div class="k">Owed to you</div><div class="v">${fmt(owed)}</div></div>
|
||||
<div class="stat"><div class="k">Overdue</div><div class="v ${overdue ? "out" : ""}">${fmt(overdue)}</div></div>
|
||||
</div>
|
||||
${list.length ? `<div class="scroll"><table>
|
||||
<thead><tr><th>Number</th><th>Date</th><th>Customer</th><th>Due</th><th class="num">GST</th><th class="num">Total</th><th>Status</th><th></th></tr></thead>
|
||||
<tbody>${list.map(i => `<tr style="${i.status === "void" ? "opacity:.55" : ""}">
|
||||
<td><strong>${esc(i.number)}</strong></td><td>${fmtDate(i.issue_date)}</td><td class="ref">${esc(i.customer_name)}</td>
|
||||
<td>${fmtDate(i.due_date)}</td><td class="num muted">${fmt(i.gst)}</td><td class="num">${fmt(i.total)}</td>
|
||||
<td>${chip(i)}${i.status === "paid" ? `<div class="muted small">${fmtDate(i.paid_date)}${i.payments.length ? ` · ${esc(i.payments[0].book)}` : ""}</div>` : ""}</td>
|
||||
<td><div class="icon-btns" style="max-width:none;flex-wrap:nowrap">${actions(i)}</div></td></tr>`).join("")}</tbody>
|
||||
</table></div>` : `<div class="empty-state">${all.length ? "No invoices here." : "No invoices yet."}</div>`}
|
||||
<p class="muted small">Marking an invoice paid records the money in the cashbook as Sales, in the book for the payment date. If the bank statement is already imported, pick the matching deposit so it isn't counted twice; statements imported later recognise the payment. An invoice with both GST and non-GST items is recorded as two rows (Incl GST and Excl GST).</p>`;
|
||||
}
|
||||
|
||||
// -- invoice editor --
|
||||
function lineRow(l = { description: "", qty: 1, unit: "", taxable: true }) {
|
||||
return `<tr>
|
||||
<td><input type="text" data-l="description" value="${esc(l.description)}" maxlength="500" required></td>
|
||||
<td><input type="number" data-l="qty" value="${l.qty}" step="any" min="0.0001" required></td>
|
||||
<td><input type="number" data-l="unit" value="${l.unit}" step="0.01" required></td>
|
||||
<td style="text-align:center"><input type="checkbox" data-l="taxable" ${l.taxable ? "checked" : ""} style="width:auto"></td>
|
||||
<td class="num" data-amount></td>
|
||||
<td><button type="button" data-remove title="Remove line">✕</button></td></tr>`;
|
||||
}
|
||||
function readLines() {
|
||||
return [...$("#invLines").rows].map(r => ({
|
||||
description: r.querySelector("[data-l=description]").value, qty: Number(r.querySelector("[data-l=qty]").value),
|
||||
unit: Number(r.querySelector("[data-l=unit]").value), taxable: r.querySelector("[data-l=taxable]").checked }));
|
||||
}
|
||||
function updateInvTotals() {
|
||||
const cents = l => Math.round(l.qty * Math.round(l.unit * 100));
|
||||
const lines = readLines();
|
||||
[...$("#invLines").rows].forEach((r, i) => r.querySelector("[data-amount]").textContent = fmt(cents(lines[i]) / 100));
|
||||
const taxable = lines.filter(l => l.taxable).reduce((n, l) => n + cents(l), 0);
|
||||
const exempt = lines.filter(l => !l.taxable).reduce((n, l) => n + cents(l), 0);
|
||||
const gst = Math.floor((taxable * 6 + 20) / 40); // same rounding as the server
|
||||
$("#invTotals").innerHTML = `<div>Subtotal (excl GST)</div><div>${fmt((taxable + exempt) / 100)}</div>
|
||||
<div>GST 15%${exempt && taxable ? ` on ${fmt(taxable / 100)}` : ""}</div><div>${fmt(gst / 100)}</div>
|
||||
<div class="grand">Total</div><div class="grand">${fmt((taxable + exempt + gst) / 100)}</div>`;
|
||||
}
|
||||
function customerOptions(selected) {
|
||||
return `<option value="">Choose a customer…</option>` +
|
||||
(state.customers || []).map(c => `<option value="${c.id}" ${c.id === selected ? "selected" : ""}>${esc(c.name)}</option>`).join("") +
|
||||
`<option value="new">+ New customer…</option>`;
|
||||
}
|
||||
function openInvoice(inv) {
|
||||
state.editingInvoice = inv || null;
|
||||
$("#invTitle").textContent = inv ? `Edit ${inv.number}` : "New invoice";
|
||||
$("#invCustomer").innerHTML = customerOptions(inv?.customer_id);
|
||||
$("#invIssue").value = inv?.issue_date || todayIso();
|
||||
$("#invDue").value = inv?.due_date || addDays($("#invIssue").value, state.business?.payment_terms_days ?? 14);
|
||||
$("#invLines").innerHTML = (inv?.lines || [undefined]).map(lineRow).join("");
|
||||
$("#invNotes").value = inv?.notes || "";
|
||||
updateInvTotals();
|
||||
$("#invDialog").returnValue = ""; $("#invDialog").showModal();
|
||||
}
|
||||
$("#invLines").addEventListener("input", updateInvTotals);
|
||||
$("#invLines").addEventListener("click", e => {
|
||||
if (!e.target.closest("[data-remove]")) return;
|
||||
if ($("#invLines").rows.length > 1) e.target.closest("tr").remove();
|
||||
updateInvTotals();
|
||||
});
|
||||
$("#invAddLine").addEventListener("click", () => {
|
||||
$("#invLines").insertAdjacentHTML("beforeend", lineRow());
|
||||
$("#invLines").lastElementChild.querySelector("input").focus();
|
||||
updateInvTotals();
|
||||
});
|
||||
$("#invIssue").addEventListener("change", () => {
|
||||
if (!state.editingInvoice) $("#invDue").value = addDays($("#invIssue").value, state.business?.payment_terms_days ?? 14);
|
||||
});
|
||||
$("#invCustomer").addEventListener("change", guard(async e => {
|
||||
if (e.target.value !== "new") return;
|
||||
const name = prompt("Customer name");
|
||||
if (!name?.trim()) { e.target.value = ""; return; }
|
||||
const c = await call("/api/customers", { name });
|
||||
state.customers = await call("/api/customers");
|
||||
e.target.innerHTML = customerOptions(c.id);
|
||||
toast(`Added ${c.name}. Add their address under Customers.`);
|
||||
}));
|
||||
$("#invSave").addEventListener("click", guard(async e => {
|
||||
e.preventDefault();
|
||||
if (!$("#invForm").reportValidity()) return;
|
||||
const body = { customer_id: Number($("#invCustomer").value), issue_date: $("#invIssue").value,
|
||||
due_date: $("#invDue").value, lines: readLines(), notes: $("#invNotes").value };
|
||||
const inv = state.editingInvoice
|
||||
? await call(`/api/invoices/${state.editingInvoice.id}`, body, "PUT")
|
||||
: await call("/api/invoices", body);
|
||||
$("#invDialog").close();
|
||||
toast(state.editingInvoice ? `Saved ${inv.number}` : `Created ${inv.number}. Download the PDF to send it.`);
|
||||
await loadInvoices();
|
||||
}));
|
||||
|
||||
// -- payment --
|
||||
async function showPayOptions(inv) {
|
||||
const matches = await call(`/api/invoices/${inv.id}/matches?date=${$("#payDate").value}`);
|
||||
$("#payOptions").innerHTML = (matches.length ? `<div class="small muted">Imported deposits of ${fmt(inv.total)} near that date:</div>` : "") +
|
||||
matches.map((m, i) => `<label><input type="radio" name="payRow" value="${m.row}" ${i === 0 ? "checked" : ""}>
|
||||
<span>${fmtDate(m.date)} · ${esc(m.reference)} <span class="muted small">${esc(m.book)} · ${m.days_apart ? plural(m.days_apart, "day") + " apart" : "same day"}</span></span></label>`).join("") +
|
||||
`<label><input type="radio" name="payRow" value="" ${matches.length ? "" : "checked"}>
|
||||
<span>${matches.length ? "None of these: add" : "Add"} it to the cashbook as a new entry <span class="muted small">(the bank statement isn't imported yet)</span></span></label>`;
|
||||
}
|
||||
function openPay(inv) {
|
||||
state.payingInvoice = inv;
|
||||
$("#paySub").textContent = `${inv.number} · ${inv.customer_name} · ${fmt(inv.total)}`;
|
||||
$("#payDate").value = todayIso();
|
||||
$("#payDialog").returnValue = ""; $("#payDialog").showModal();
|
||||
return showPayOptions(inv);
|
||||
}
|
||||
$("#payDate").addEventListener("change", guard(() => showPayOptions(state.payingInvoice)));
|
||||
$("#payDialog").addEventListener("close", guard(async () => {
|
||||
if ($("#payDialog").returnValue !== "ok") return;
|
||||
const row = document.querySelector("input[name=payRow]:checked")?.value;
|
||||
const inv = await call(`/api/invoices/${state.payingInvoice.id}/pay`, { date: $("#payDate").value, row: row ? Number(row) : null });
|
||||
toast(`${inv.number} paid: recorded in ${inv.payments[0]?.book || "the cashbook"}`);
|
||||
if (state.book) state.summary = await call(`/api/summary?book=${encodeURIComponent(state.book)}`);
|
||||
await loadInvoices();
|
||||
}));
|
||||
|
||||
// -- customers --
|
||||
async function loadCustomers() {
|
||||
[state.customers, state.invoices] = await Promise.all([call("/api/customers"), call("/api/invoices")]);
|
||||
state.view = "customers"; render();
|
||||
}
|
||||
function renderCustomers() {
|
||||
const list = state.customers || [];
|
||||
const owed = id => (state.invoices || []).filter(i => i.customer_id === id && i.status === "sent").reduce((n, i) => n + i.total, 0);
|
||||
return `
|
||||
<div class="row-toolbar"><strong style="font-size:16px">Customers</strong></div>
|
||||
${list.length ? `<div class="scroll"><table class="contacts"><thead><tr><th>Name</th><th>Email</th><th>Address</th><th class="num">Owing</th><th></th></tr></thead><tbody>
|
||||
${list.map(c => `<tr>${["name", "email"].map(k =>
|
||||
`<td><input type="text" data-cust="${k}" data-id="${c.id}" value="${esc(c[k])}"></td>`).join("")}
|
||||
<td><textarea rows="2" data-cust="address" data-id="${c.id}">${esc(c.address)}</textarea></td>
|
||||
<td class="num">${c.invoices ? `<button class="linkish" data-cust-show="${c.id}">${fmt(owed(c.id))}</button><div class="muted small">${plural(c.invoices, "invoice")}</div>` : ""}</td>
|
||||
<td>${c.invoices ? "" : `<button data-cust-delete="${c.id}">Delete</button>`}</td></tr>`).join("")}
|
||||
</tbody></table></div><div class="muted small">Changes are saved as you leave each box. Invoices already made keep the details they were made with.</div>`
|
||||
: `<div class="empty-state" style="padding:20px">No customers yet.</div>`}
|
||||
<form id="custAdd" class="receipt-fields" style="margin-top:16px;align-items:end">
|
||||
<label>Name<input id="custName" type="text" required maxlength="200"></label>
|
||||
<label>Email<input id="custEmail" type="text" maxlength="200"></label>
|
||||
<label class="wide">Address<textarea id="custAddress" rows="2" maxlength="500"></textarea></label>
|
||||
<button class="primary">Add customer</button>
|
||||
</form>`;
|
||||
}
|
||||
$("#main").addEventListener("change", guard(async e => {
|
||||
const k = e.target.dataset.cust; if (!k) return;
|
||||
const c = state.customers.find(x => x.id === Number(e.target.dataset.id));
|
||||
await call(`/api/customers/${c.id}`, { name: c.name, email: c.email, address: c.address, [k]: e.target.value }, "PUT");
|
||||
state.customers = await call("/api/customers");
|
||||
toast("Saved");
|
||||
}));
|
||||
$("#main").addEventListener("click", guard(async e => {
|
||||
const del = e.target.dataset.custDelete, show = e.target.dataset.custShow;
|
||||
if (show) { state.invoiceCustomer = show; state.invoiceFilter = "all"; await loadInvoices(); return; }
|
||||
if (del) { await call(`/api/customers/${del}`, undefined, "DELETE"); await loadCustomers(); }
|
||||
}));
|
||||
$("#main").addEventListener("submit", guard(async e => {
|
||||
if (e.target.id !== "custAdd") return;
|
||||
e.preventDefault();
|
||||
await call("/api/customers", { name: $("#custName").value, email: $("#custEmail").value, address: $("#custAddress").value });
|
||||
await loadCustomers();
|
||||
}));
|
||||
|
||||
// -- business details --
|
||||
function showLogo() {
|
||||
const has = !!state.business.logo_file;
|
||||
$("#bizLogo").hidden = !has; $("#bizLogoRemove").hidden = !has;
|
||||
if (has) $("#bizLogo").src = `/api/business/logo?t=${Date.now()}`;
|
||||
}
|
||||
async function openBusiness() {
|
||||
state.business = await call("/api/business");
|
||||
for (const input of document.querySelectorAll("[data-biz]")) input.value = state.business[input.dataset.biz] ?? "";
|
||||
showLogo();
|
||||
$("#bizDialog").returnValue = ""; $("#bizDialog").showModal();
|
||||
}
|
||||
$("#bizDialog").addEventListener("close", guard(async () => {
|
||||
if ($("#bizDialog").returnValue !== "ok") return;
|
||||
const body = {};
|
||||
for (const input of document.querySelectorAll("[data-biz]")) {
|
||||
const k = input.dataset.biz, v = input.type === "number" ? Number(input.value) : input.value;
|
||||
if (v !== state.business[k]) body[k] = v;
|
||||
}
|
||||
state.business = await call("/api/business", body, "PUT");
|
||||
toast("Business details saved");
|
||||
if (state.view === "invoices") render();
|
||||
}));
|
||||
$("#bizLogoAdd").addEventListener("click", () => $("#bizLogoInput").click());
|
||||
$("#bizLogoInput").addEventListener("change", guard(async e => {
|
||||
const f = e.target.files[0]; e.target.value = ""; if (!f) return;
|
||||
const res = await fetch("/api/business/logo", { method: "POST", body: f });
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) throw new Error(errorText(data, res));
|
||||
state.business = data; showLogo();
|
||||
}));
|
||||
$("#bizLogoRemove").addEventListener("click", guard(async () => {
|
||||
state.business = await call("/api/business/logo", undefined, "DELETE"); showLogo();
|
||||
}));
|
||||
|
||||
// buttons with data-inv in the Invoices view
|
||||
async function invoiceAction(b) {
|
||||
const id = Number(b.dataset.id), inv = (state.invoices || []).find(i => i.id === id);
|
||||
switch (b.dataset.inv) {
|
||||
case "filter": state.invoiceFilter = b.dataset.filter; return render();
|
||||
case "new": return openInvoice();
|
||||
case "edit": return openInvoice(inv);
|
||||
case "business": return openBusiness();
|
||||
case "pay": return openPay(inv);
|
||||
case "sent": case "draft": await call(`/api/invoices/${id}/status`, { status: b.dataset.inv }); break;
|
||||
case "void":
|
||||
if (!confirm(`Void ${inv.number}? It keeps its number but can't be used again.`)) return;
|
||||
await call(`/api/invoices/${id}/status`, { status: "void" }); break;
|
||||
case "unpay":
|
||||
if (!confirm(`Undo the payment of ${inv.number}? The cashbook entries it made are removed (an imported bank line is put back as it was).`)) return;
|
||||
await call(`/api/invoices/${id}/unpay`, {});
|
||||
if (state.book) state.summary = await call(`/api/summary?book=${encodeURIComponent(state.book)}`);
|
||||
break;
|
||||
}
|
||||
await loadInvoices();
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
// ---- mileage: vehicle logbook and the IRD kilometre-rate claim ----
|
||||
// Uses $, state, call, esc, fmt, fmtDate, plural, toast, guard, render and todayIso from index.html / invoices.js.
|
||||
const incomeYear = iso => { const [y, m] = iso.split("-").map(Number); return m >= 4 ? y : y - 1; };
|
||||
const yearLabel = y => `April ${y} – March ${y + 1}`;
|
||||
const km = n => Number(n).toLocaleString("en-NZ", { maximumFractionDigits: 1 });
|
||||
|
||||
document.body.insertAdjacentHTML("beforeend", `
|
||||
<dialog id="tripDialog">
|
||||
<form method="dialog" id="tripForm">
|
||||
<h2 style="margin:0 0 4px;font-size:16px" id="tripTitle">Add trip</h2>
|
||||
<div class="receipt-fields" style="margin-top:10px">
|
||||
<label>Vehicle<select id="tripVehicle" required></select></label>
|
||||
<label>Date<input id="tripDate" type="date" required></label>
|
||||
<label>From<input id="tripFrom" type="text" maxlength="200"></label>
|
||||
<label>To<input id="tripTo" type="text" maxlength="200"></label>
|
||||
<label class="wide">Purpose<input id="tripPurpose" type="text" maxlength="200" placeholder="e.g. Site visit, supplies pickup"></label>
|
||||
<label class="wide">Customer<input id="tripCustomer" type="text" maxlength="200" list="tripCustomers"></label>
|
||||
<label>Odometer start<input id="tripOdoStart" type="number" min="0" step="1"></label>
|
||||
<label>Odometer end<input id="tripOdoEnd" type="number" min="0" step="1"></label>
|
||||
<label>Distance (km)<input id="tripKm" type="number" min="0.1" step="0.1"></label>
|
||||
</div>
|
||||
<datalist id="tripCustomers"></datalist>
|
||||
<label class="check"><input type="checkbox" id="tripBusiness" checked> Business trip (untick for private use)</label>
|
||||
<div class="muted small">Enter both odometer readings and the distance is worked out, or just type the distance.</div>
|
||||
<div class="actions" style="justify-content:flex-end">
|
||||
<button value="cancel" formnovalidate>Cancel</button>
|
||||
<button value="ok" class="primary" id="tripSave">Save trip</button>
|
||||
</div>
|
||||
</form>
|
||||
</dialog>
|
||||
<dialog id="vehDialog" class="wide">
|
||||
<div class="receipt-head"><h2 class="grow" style="margin:0;font-size:16px">Vehicles</h2><button id="vehClose">Close</button></div>
|
||||
<div id="vehBody"></div>
|
||||
<form id="vehAdd" class="receipt-fields" style="margin-top:12px;align-items:end">
|
||||
<label class="wide">Name<input id="vehName" type="text" required maxlength="100" placeholder="e.g. Hilux"></label>
|
||||
<label>Registration<input id="vehRego" type="text" maxlength="20"></label>
|
||||
<button class="primary">Add vehicle</button>
|
||||
</form>
|
||||
</dialog>`);
|
||||
|
||||
async function loadMileage(year) {
|
||||
state.mileageYear = year ?? state.mileageYear ?? incomeYear(todayIso());
|
||||
const y = state.mileageYear;
|
||||
[state.vehicles, state.trips, state.mileage] = await Promise.all([
|
||||
call("/api/vehicles"), call(`/api/trips?year=${y}`), call(`/api/mileage/report?year=${y}`)]);
|
||||
state.view = "mileage"; render();
|
||||
}
|
||||
|
||||
function renderMileage() {
|
||||
const r = state.mileage, y = state.mileageYear, trips = state.trips || [], rate = r.rate;
|
||||
const thisYear = incomeYear(todayIso());
|
||||
const years = [...new Set([thisYear, thisYear - 1, thisYear - 2, y])].sort((a, b) => b - a);
|
||||
const vehicleCard = v => `<div class="stat" style="min-width:220px">
|
||||
<div class="k">${esc(v.vehicle)} · ${plural(v.trips, "trip")}</div>
|
||||
<div class="v">${v.claim == null ? `${km(v.business_km)} km` : fmt(v.claim)}</div>
|
||||
<div class="small muted">${km(v.business_km)} km business${v.private_km ? ` · ${km(v.private_km)} km private · ${v.business_percent}% business use` : ""}</div>
|
||||
${v.claim != null ? `<div class="small muted">${km(v.tier1_km)} km × ${fmt(rate.tier1)}${v.tier2_km ? ` + ${km(v.tier2_km)} km × ${fmt(rate.tier2)}` : ""}</div>` : ""}
|
||||
${v.odo_first != null ? `<div class="small muted">Odometer ${v.odo_first.toLocaleString()} → ${v.odo_last.toLocaleString()}</div>` : ""}
|
||||
</div>`;
|
||||
return `
|
||||
<div class="row-toolbar">
|
||||
<strong style="font-size:16px">Mileage</strong>
|
||||
<select data-mi="year" aria-label="Income year">${years.map(x => `<option value="${x}" ${x === y ? "selected" : ""}>${yearLabel(x)}</option>`).join("")}</select>
|
||||
<span style="flex:1"></span>
|
||||
<a href="/api/mileage/export?year=${y}" download style="color:inherit"><button tabindex="-1" ${trips.length ? "" : "disabled"}>Export logbook (CSV)</button></a>
|
||||
<button data-mi="vehicles">Vehicles (${(state.vehicles || []).length})</button>
|
||||
<button data-mi="add" class="primary">Add trip</button>
|
||||
</div>
|
||||
<div class="gst-card">
|
||||
<div class="gst-head">
|
||||
<div><strong>IRD kilometre rates for ${yearLabel(y)}</strong>
|
||||
<div class="muted small">IRD publishes these each year. Tier 1 is for each vehicle's first business kilometres up to the limit, tier 2 for the rest.</div></div>
|
||||
<div class="gst-result">${r.claim == null ? "" : `${fmt(r.claim)} claim`}</div>
|
||||
</div>
|
||||
<div class="receipt-fields" style="max-width:640px;align-items:end">
|
||||
<label>Tier 1 ($ per km)<input type="number" step="0.001" min="0" id="rateT1" value="${rate?.tier1 ?? ""}" placeholder="e.g. 1.17"></label>
|
||||
<label>Tier 2 ($ per km)<input type="number" step="0.001" min="0" id="rateT2" value="${rate?.tier2 ?? ""}" placeholder="e.g. 0.37"></label>
|
||||
<label>Tier 1 limit (km)<input type="number" step="1" min="1" id="rateLimit" value="${rate?.tier1_limit_km ?? 14000}"></label>
|
||||
<button data-mi="rate">${rate ? "Update rates" : "Save rates"}</button>
|
||||
</div>
|
||||
${rate ? "" : `<div class="small" style="color:var(--warn);margin-top:8px">Enter this year's rates from IRD to see the claim.</div>`}
|
||||
</div>
|
||||
${r.vehicles.length ? `<div class="stats">${r.vehicles.map(vehicleCard).join("")}</div>` : ""}
|
||||
${trips.length ? `<div class="scroll"><table>
|
||||
<thead><tr><th>Date</th><th>Vehicle</th><th>Trip</th><th>Purpose</th><th>Customer</th><th class="num">Odometer</th><th class="num">Km</th><th></th><th></th></tr></thead>
|
||||
<tbody>${trips.map(t => `<tr style="${t.business ? "" : "opacity:.7"}">
|
||||
<td>${fmtDate(t.date)}</td><td>${esc(t.vehicle)}</td>
|
||||
<td class="ref">${esc(t.from_place)}${t.from_place || t.to_place ? " → " : ""}${esc(t.to_place)}</td>
|
||||
<td class="ref">${esc(t.purpose)}</td><td>${esc(t.customer)}</td>
|
||||
<td class="num muted">${t.odo_start != null ? `${t.odo_start.toLocaleString()}–${(t.odo_end ?? "").toLocaleString()}` : ""}</td>
|
||||
<td class="num">${km(t.km)}</td>
|
||||
<td>${t.business ? "" : `<span class="chip duplicate">private</span>`}</td>
|
||||
<td><div class="icon-btns"><button data-mi="edit" data-id="${t.id}">Edit</button><button data-mi="delete" data-id="${t.id}">Delete</button></div></td></tr>`).join("")}</tbody>
|
||||
<tfoot><tr><td colspan="6">${plural(trips.length, "trip")}</td><td class="num">${km(r.business_km + r.private_km)}</td><td colspan="2"></td></tr></tfoot>
|
||||
</table></div>` : `<div class="empty-state">No trips logged for ${yearLabel(y)}.${(state.vehicles || []).length ? "" : `<br><br><button class="primary" data-mi="vehicles">Add a vehicle</button>`}</div>`}
|
||||
<p class="muted small">This is a report for your records and tax return: nothing here changes the cashbook. IRD expects a logbook kept for at least 90 days every three years to set the business-use percentage.</p>`;
|
||||
}
|
||||
|
||||
// -- trip editor --
|
||||
function lastOdometer(vehicleId) {
|
||||
return (state.vehicles || []).find(v => v.id === vehicleId)?.last_odometer ?? "";
|
||||
}
|
||||
function syncTripKm() {
|
||||
const s = $("#tripOdoStart").value, e = $("#tripOdoEnd").value;
|
||||
const both = s !== "" && e !== "";
|
||||
$("#tripKm").disabled = both;
|
||||
if (both) $("#tripKm").value = Number(e) > Number(s) ? Number(e) - Number(s) : "";
|
||||
}
|
||||
function openTrip(t) {
|
||||
const vehicles = (state.vehicles || []).filter(v => v.active || v.id === t?.vehicle_id);
|
||||
if (!vehicles.length) { toast("Add a vehicle first", true); return openVehicles(); }
|
||||
state.editingTrip = t || null;
|
||||
$("#tripTitle").textContent = t ? "Edit trip" : "Add trip";
|
||||
$("#tripVehicle").innerHTML = vehicles.map(v => `<option value="${v.id}" ${v.id === t?.vehicle_id ? "selected" : ""}>${esc(v.name)}${v.rego ? ` (${esc(v.rego)})` : ""}</option>`).join("");
|
||||
$("#tripDate").value = t?.date || todayIso();
|
||||
$("#tripFrom").value = t?.from_place || ""; $("#tripTo").value = t?.to_place || "";
|
||||
$("#tripPurpose").value = t?.purpose || ""; $("#tripCustomer").value = t?.customer || "";
|
||||
$("#tripOdoStart").value = t ? t.odo_start ?? "" : lastOdometer(Number($("#tripVehicle").value));
|
||||
$("#tripOdoEnd").value = t?.odo_end ?? "";
|
||||
$("#tripKm").value = t?.km ?? "";
|
||||
$("#tripBusiness").checked = t ? t.business : true;
|
||||
$("#tripCustomers").innerHTML = [...new Set([...(state.customers || []).map(c => c.name), ...(state.trips || []).map(x => x.customer)])]
|
||||
.filter(Boolean).map(n => `<option value="${esc(n)}">`).join("");
|
||||
syncTripKm();
|
||||
$("#tripDialog").returnValue = ""; $("#tripDialog").showModal();
|
||||
}
|
||||
$("#tripOdoStart").addEventListener("input", syncTripKm);
|
||||
$("#tripOdoEnd").addEventListener("input", syncTripKm);
|
||||
$("#tripVehicle").addEventListener("change", () => {
|
||||
if (!state.editingTrip) { $("#tripOdoStart").value = lastOdometer(Number($("#tripVehicle").value)); syncTripKm(); }
|
||||
});
|
||||
$("#tripSave").addEventListener("click", guard(async e => {
|
||||
e.preventDefault();
|
||||
if (!$("#tripForm").reportValidity()) return;
|
||||
const num = id => $(id).value === "" ? null : Number($(id).value);
|
||||
const body = { vehicle_id: Number($("#tripVehicle").value), date: $("#tripDate").value,
|
||||
from_place: $("#tripFrom").value, to_place: $("#tripTo").value, purpose: $("#tripPurpose").value,
|
||||
customer: $("#tripCustomer").value, odo_start: num("#tripOdoStart"), odo_end: num("#tripOdoEnd"),
|
||||
km: num("#tripKm"), business: $("#tripBusiness").checked };
|
||||
const t = state.editingTrip ? await call(`/api/trips/${state.editingTrip.id}`, body, "PUT") : await call("/api/trips", body);
|
||||
$("#tripDialog").close();
|
||||
if (t.warnings.length) toast(`Saved, but ${t.warnings.join("; ")}`, true);
|
||||
else toast(`Saved: ${km(t.km)} km`);
|
||||
await loadMileage(incomeYear(t.date));
|
||||
}));
|
||||
|
||||
// -- vehicles --
|
||||
function renderVehicles() {
|
||||
const list = state.vehicles || [];
|
||||
$("#vehBody").innerHTML = list.length ? `<div class="scroll"><table><thead><tr><th>Name</th><th>Registration</th><th>In use</th><th></th></tr></thead><tbody>
|
||||
${list.map(v => `<tr>
|
||||
<td><input type="text" data-veh="name" data-id="${v.id}" value="${esc(v.name)}" style="width:100%"></td>
|
||||
<td><input type="text" data-veh="rego" data-id="${v.id}" value="${esc(v.rego)}" style="width:100%"></td>
|
||||
<td><input type="checkbox" data-veh="active" data-id="${v.id}" ${v.active ? "checked" : ""} style="width:auto"></td>
|
||||
<td>${v.trips ? `<span class="muted small">${plural(v.trips, "trip")}</span>` : `<button data-veh-delete="${v.id}">Delete</button>`}</td></tr>`).join("")}
|
||||
</tbody></table></div><div class="muted small">Untick “In use” for a vehicle you no longer have; its trips are kept.</div>`
|
||||
: `<div class="empty-state" style="padding:20px">No vehicles yet.</div>`;
|
||||
}
|
||||
async function openVehicles() {
|
||||
state.vehicles = await call("/api/vehicles");
|
||||
renderVehicles(); $("#vehDialog").showModal();
|
||||
}
|
||||
$("#vehClose").addEventListener("click", guard(async () => { $("#vehDialog").close(); if (state.view === "mileage") await loadMileage(); }));
|
||||
$("#vehBody").addEventListener("change", guard(async e => {
|
||||
const k = e.target.dataset.veh; if (!k) return;
|
||||
await call(`/api/vehicles/${e.target.dataset.id}`, { [k]: k === "active" ? e.target.checked : e.target.value }, "PUT");
|
||||
state.vehicles = await call("/api/vehicles");
|
||||
toast("Saved");
|
||||
}));
|
||||
$("#vehBody").addEventListener("click", guard(async e => {
|
||||
const id = e.target.dataset.vehDelete; if (!id) return;
|
||||
await call(`/api/vehicles/${id}`, undefined, "DELETE");
|
||||
state.vehicles = await call("/api/vehicles"); renderVehicles();
|
||||
}));
|
||||
$("#vehAdd").addEventListener("submit", guard(async e => {
|
||||
e.preventDefault();
|
||||
await call("/api/vehicles", { name: $("#vehName").value, rego: $("#vehRego").value });
|
||||
$("#vehAdd").reset();
|
||||
state.vehicles = await call("/api/vehicles"); renderVehicles();
|
||||
}));
|
||||
|
||||
// buttons with data-mi in the Mileage view
|
||||
async function mileageAction(b) {
|
||||
const t = (state.trips || []).find(x => x.id === Number(b.dataset.id));
|
||||
switch (b.dataset.mi) {
|
||||
case "add": return openTrip();
|
||||
case "edit": return openTrip(t);
|
||||
case "vehicles": return openVehicles();
|
||||
case "delete":
|
||||
if (!confirm(`Delete the trip on ${fmtDate(t.date)} (${km(t.km)} km)?`)) return;
|
||||
await call(`/api/trips/${t.id}`, undefined, "DELETE"); break;
|
||||
case "rate":
|
||||
if ($("#rateT1").value === "" || $("#rateT2").value === "") throw new Error("Enter both rates");
|
||||
await call(`/api/mileage/rates/${state.mileageYear}`, { tier1: Number($("#rateT1").value),
|
||||
tier2: Number($("#rateT2").value), tier1_limit_km: Number($("#rateLimit").value) || 14000 }, "PUT");
|
||||
toast("Rates saved"); break;
|
||||
default: return;
|
||||
}
|
||||
await loadMileage();
|
||||
}
|
||||
function mileageChange(el) {
|
||||
if (el.dataset.mi === "year") return loadMileage(Number(el.value));
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// ---- suppliers: who receipts and bills are from ----
|
||||
// Uses $, state, call, esc, plural, fmt, guard and render from index.html.
|
||||
document.body.insertAdjacentHTML("beforeend", `
|
||||
<dialog id="newSupDialog">
|
||||
<form method="dialog">
|
||||
<h2 style="margin:0 0 4px;font-size:16px">New supplier?</h2>
|
||||
<div class="muted small" id="newSupSub"></div>
|
||||
<label for="newSupName">Name</label>
|
||||
<input id="newSupName" type="text" required maxlength="200">
|
||||
<label for="newSupGst">GST number</label>
|
||||
<input id="newSupGst" type="text" maxlength="20" placeholder="123-456-789">
|
||||
<div class="actions" style="justify-content:flex-end">
|
||||
<button value="cancel" formnovalidate>Not now</button>
|
||||
<button value="ok" class="primary">Create and link</button>
|
||||
</div>
|
||||
</form>
|
||||
</dialog>`);
|
||||
|
||||
// Offer to make a supplier from a receipt or invoice that matched none; links it if the user agrees.
|
||||
// Returns the updated attachment, or null if declined.
|
||||
function askNewSupplier(a) {
|
||||
$("#newSupSub").textContent = `${a.kind === "invoice" ? "Invoice" : "Receipt"} ${a.filename} isn't linked to a supplier. Add one so its receipts and bills are kept together.`;
|
||||
$("#newSupName").value = a.vendor || ""; $("#newSupGst").value = a.gst_number || "";
|
||||
const d = $("#newSupDialog");
|
||||
return new Promise(resolve => {
|
||||
d.addEventListener("close", guard(async () => {
|
||||
if (d.returnValue !== "ok") return resolve(null);
|
||||
const sup = await call("/api/suppliers", { name: $("#newSupName").value, gst_number: $("#newSupGst").value });
|
||||
const linked = await call(`/api/attachments/${a.id}`, { supplier_id: sup.id }, "PATCH");
|
||||
toast(`Added supplier ${sup.name}`);
|
||||
resolve(linked);
|
||||
}), { once: true });
|
||||
d.returnValue = ""; d.showModal();
|
||||
});
|
||||
}
|
||||
// after an upload: ask for each document with no supplier; one answer covers the others from the same vendor
|
||||
async function offerSuppliers(added) {
|
||||
const same = (x, y) => (x.gst_number && x.gst_number === y.gst_number) ||
|
||||
(x.vendor && y.vendor && x.vendor.trim().toLowerCase() === y.vendor.trim().toLowerCase());
|
||||
const done = new Set();
|
||||
for (const a of added) {
|
||||
if (a.supplier_id != null || done.has(a.id)) continue;
|
||||
const linked = await askNewSupplier(a);
|
||||
if (!linked) continue;
|
||||
for (const b of added)
|
||||
if (b.id !== a.id && b.supplier_id == null && same(a, b)) {
|
||||
await call(`/api/attachments/${b.id}`, { supplier_id: linked.supplier_id }, "PATCH"); done.add(b.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function loadSuppliers() {
|
||||
state.suppliers = await call("/api/suppliers");
|
||||
state.view = "suppliers"; render();
|
||||
}
|
||||
|
||||
function renderSuppliers() {
|
||||
const list = state.suppliers || [];
|
||||
const cell = (s, k) => `<td><input type="text" data-sup-field="${k}" data-id="${s.id}" value="${esc(s[k])}"></td>`;
|
||||
return `
|
||||
<div class="row-toolbar"><strong style="font-size:16px">Suppliers</strong></div>
|
||||
${list.length ? `<div class="scroll"><table class="contacts"><thead><tr><th>Name</th><th>GST number</th><th>Email</th><th>Address</th><th class="num">Spent</th><th></th></tr></thead><tbody>
|
||||
${list.map(s => `<tr>${["name", "gst_number", "email"].map(k => cell(s, k)).join("")}
|
||||
<td><textarea rows="2" data-sup-field="address" data-id="${s.id}">${esc(s.address)}</textarea></td>
|
||||
<td class="num">${s.documents ? `<button class="linkish" data-sup-show="${s.id}">${fmt(s.total)}</button><div class="muted small">${plural(s.documents, "receipt")}</div>` : ""}</td>
|
||||
<td>${s.documents ? "" : `<button data-sup-delete="${s.id}">Delete</button>`}</td></tr>`).join("")}
|
||||
</tbody></table></div>
|
||||
<div class="muted small">Changes are saved as you leave each box. Receipts and bills are linked to a supplier automatically when the GST number or name matches; otherwise pick one on the receipt.</div>`
|
||||
: `<div class="empty-state" style="padding:20px">No suppliers yet. Add one below, or choose “New supplier” on a receipt.</div>`}
|
||||
<form id="supAdd" class="receipt-fields" style="margin-top:16px;align-items:end">
|
||||
<label>Name<input id="supName" type="text" required maxlength="200"></label>
|
||||
<label>GST number<input id="supGst" type="text" maxlength="20" placeholder="123-456-789"></label>
|
||||
<label>Email<input id="supEmail" type="text" maxlength="200"></label>
|
||||
<label class="wide">Address<textarea id="supAddress" rows="2" maxlength="500"></textarea></label>
|
||||
<button class="primary">Add supplier</button>
|
||||
</form>`;
|
||||
}
|
||||
|
||||
$("#main").addEventListener("change", guard(async e => {
|
||||
const k = e.target.dataset.supField; if (!k) return;
|
||||
const s = state.suppliers.find(x => x.id === Number(e.target.dataset.id));
|
||||
await call(`/api/suppliers/${s.id}`, { name: s.name, email: s.email, address: s.address, gst_number: s.gst_number, [k]: e.target.value }, "PUT");
|
||||
state.suppliers = await call("/api/suppliers");
|
||||
toast("Saved");
|
||||
}));
|
||||
$("#main").addEventListener("click", guard(async e => {
|
||||
const del = e.target.dataset.supDelete, show = e.target.dataset.supShow;
|
||||
if (show) { state.receiptSupplier = show; state.receiptFilter = "all"; await loadReceipts(true); return; }
|
||||
if (del) { await call(`/api/suppliers/${del}`, undefined, "DELETE"); await loadSuppliers(); }
|
||||
}));
|
||||
$("#main").addEventListener("submit", guard(async e => {
|
||||
if (e.target.id !== "supAdd") return;
|
||||
e.preventDefault();
|
||||
await call("/api/suppliers", { name: $("#supName").value, gst_number: $("#supGst").value, email: $("#supEmail").value, address: $("#supAddress").value });
|
||||
await loadSuppliers();
|
||||
}));
|
||||
@@ -0,0 +1,60 @@
|
||||
"""Suppliers: who receipts and bills are from. A document is linked to a supplier by id
|
||||
(attachments.supplier_id); uploads are linked automatically when the document's GST number or
|
||||
name matches a supplier (see ledger.find_supplier)."""
|
||||
import ledger
|
||||
|
||||
SUPPLIER_FIELDS = ("name", "email", "address", "gst_number")
|
||||
|
||||
|
||||
def _supplier(conn, s):
|
||||
d = dict(s)
|
||||
row = conn.execute("SELECT COUNT(*) AS n, COALESCE(SUM(total_cents), 0) AS total FROM attachments "
|
||||
"WHERE supplier_id = ?", (s["id"],)).fetchone()
|
||||
d["documents"], d["total"] = row["n"], row["total"] / 100
|
||||
return d
|
||||
|
||||
|
||||
def list_suppliers(conn):
|
||||
return [_supplier(conn, s) for s in conn.execute("SELECT * FROM suppliers ORDER BY name COLLATE NOCASE")]
|
||||
|
||||
|
||||
def get_supplier(conn, supplier_id):
|
||||
s = conn.execute("SELECT * FROM suppliers WHERE id = ?", (supplier_id,)).fetchone()
|
||||
if s is None:
|
||||
raise ledger.NotFound("no such supplier")
|
||||
return _supplier(conn, s)
|
||||
|
||||
|
||||
def _fields(fields):
|
||||
out = {}
|
||||
for k, v in fields.items():
|
||||
if k not in SUPPLIER_FIELDS:
|
||||
raise ValueError(f"can't change {k}")
|
||||
out[k] = (v or "").strip()
|
||||
if "name" in out and not out["name"]:
|
||||
raise ValueError("the supplier needs a name")
|
||||
return out
|
||||
|
||||
|
||||
def create_supplier(conn, name, email="", address="", gst_number=""):
|
||||
f = _fields({"name": name, "email": email, "address": address, "gst_number": gst_number})
|
||||
with conn:
|
||||
cur = conn.execute("INSERT INTO suppliers (name, email, address, gst_number) VALUES (?, ?, ?, ?)",
|
||||
(f["name"], f["email"], f["address"], f["gst_number"]))
|
||||
return get_supplier(conn, cur.lastrowid)
|
||||
|
||||
|
||||
def update_supplier(conn, supplier_id, **fields):
|
||||
get_supplier(conn, supplier_id)
|
||||
for k, v in _fields(fields).items():
|
||||
with conn:
|
||||
conn.execute(f"UPDATE suppliers SET {k} = ? WHERE id = ?", (v, supplier_id))
|
||||
return get_supplier(conn, supplier_id)
|
||||
|
||||
|
||||
def delete_supplier(conn, supplier_id):
|
||||
get_supplier(conn, supplier_id)
|
||||
if conn.execute("SELECT 1 FROM attachments WHERE supplier_id = ?", (supplier_id,)).fetchone():
|
||||
raise ValueError("this supplier has receipts or bills, so it can't be deleted")
|
||||
with conn:
|
||||
conn.execute("DELETE FROM suppliers WHERE id = ?", (supplier_id,))
|
||||
@@ -0,0 +1,160 @@
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
from conftest import SAMPLE_CSV
|
||||
|
||||
LINES = [{"description": "Web design", "qty": 2, "unit": 50, "taxable": True},
|
||||
{"description": "Domain fee (no GST)", "qty": 1, "unit": 50, "taxable": False}]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def biz(admin):
|
||||
"""A book for 2026, a business and a customer."""
|
||||
admin.post("/api/books", json={"name": "b", "year": 2026})
|
||||
r = admin.put("/api/business", json={"name": "Jimmy Ltd", "gst_number": "123-456-789",
|
||||
"bank_account": "01-0822-0082180-00", "payment_terms_days": 20})
|
||||
assert r.status_code == 200, r.text
|
||||
admin.customer = admin.post("/api/customers", json={"name": "Client Ltd", "address": "1 Queen St\nAuckland"}).json()
|
||||
return admin
|
||||
|
||||
|
||||
def make(client, lines=LINES, **kw):
|
||||
r = client.post("/api/invoices", json={"customer_id": client.customer["id"], "issue_date": "2026-04-10",
|
||||
"lines": lines, **kw})
|
||||
assert r.status_code == 200, r.text
|
||||
return r.json()
|
||||
|
||||
|
||||
def rows(client, sheet="APR"):
|
||||
return client.get(f"/api/month?book=b&sheet={sheet}").json()["rows"]
|
||||
|
||||
|
||||
def test_totals_and_numbering(biz):
|
||||
inv = make(biz)
|
||||
assert inv["number"] == "INV-0001"
|
||||
assert (inv["subtotal"], inv["taxable"], inv["exempt"], inv["gst"], inv["total"]) == (150, 100, 50, 15, 165)
|
||||
assert inv["due_date"] == "2026-04-30" # 20 days' terms
|
||||
assert inv["status"] == "draft"
|
||||
assert [l["amount"] for l in inv["lines"]] == [100, 50]
|
||||
|
||||
# numbers carry on, and a void invoice keeps its number
|
||||
biz.post(f"/api/invoices/{inv['id']}/status", json={"status": "void"})
|
||||
assert make(biz)["number"] == "INV-0002"
|
||||
# moving the counter onto a used number is refused
|
||||
assert biz.put("/api/business", json={"next_number": 2}).status_code == 400
|
||||
biz.put("/api/business", json={"invoice_prefix": "J-", "next_number": 7})
|
||||
assert make(biz)["number"] == "J-0007"
|
||||
|
||||
|
||||
def test_rounding():
|
||||
import invoices
|
||||
# 3 x $33.33 = $99.99, GST 15% = 14.9985 -> 15.00 (half-up, once per invoice)
|
||||
t = invoices.totals([{"qty": 3, "unit_cents": 3333, "taxable": True},
|
||||
{"qty": 1.5, "unit_cents": 1001, "taxable": True}]) # 15.015 -> 15.02
|
||||
assert t["taxable"] == 9999 + 1502
|
||||
assert t["gst"] == (t["taxable"] * 3 + 10) // 20
|
||||
assert invoices.gst_on(10) == 2 and invoices.gst_on(3) == 0 # 1.5c rounds up, 0.45c down
|
||||
|
||||
|
||||
def test_validation(biz):
|
||||
c = biz.customer["id"]
|
||||
bad = [{"customer_id": c, "issue_date": "2026-04-10", "lines": []},
|
||||
{"customer_id": c, "issue_date": "nope", "lines": LINES},
|
||||
{"customer_id": c, "issue_date": "2026-04-10", "due_date": "2026-04-01", "lines": LINES},
|
||||
{"customer_id": c, "issue_date": "2026-04-10", "lines": [{"description": "", "unit": 5}]},
|
||||
{"customer_id": c, "issue_date": "2026-04-10", "lines": [{"description": "x", "qty": 0, "unit": 5}]}]
|
||||
for body in bad:
|
||||
assert biz.post("/api/invoices", json=body).status_code == 400, body
|
||||
assert biz.post("/api/invoices", json={**bad[1], "customer_id": 999, "issue_date": "2026-04-10"}).status_code == 404
|
||||
|
||||
|
||||
def test_customer_snapshot_and_locking(biz):
|
||||
inv = make(biz)
|
||||
biz.put(f"/api/customers/{biz.customer['id']}", json={"name": "Client Holdings Ltd", "address": "elsewhere"})
|
||||
assert biz.get(f"/api/invoices/{inv['id']}").json()["customer_name"] == "Client Ltd"
|
||||
# a draft can be changed; re-choosing the customer picks up the new details
|
||||
r = biz.put(f"/api/invoices/{inv['id']}", json={"customer_id": biz.customer["id"],
|
||||
"lines": [{"description": "x", "unit": 10}]})
|
||||
assert (r.json()["customer_name"], r.json()["total"]) == ("Client Holdings Ltd", 11.5)
|
||||
|
||||
biz.post(f"/api/invoices/{inv['id']}/status", json={"status": "sent"})
|
||||
assert biz.put(f"/api/invoices/{inv['id']}", json={"lines": LINES}).status_code == 400
|
||||
assert biz.put(f"/api/invoices/{inv['id']}", json={"notes": "Thanks!"}).json()["notes"] == "Thanks!"
|
||||
assert biz.delete(f"/api/customers/{biz.customer['id']}").status_code == 400
|
||||
assert biz.post(f"/api/invoices/{inv['id']}/status", json={"status": "paid"}).status_code == 400
|
||||
|
||||
|
||||
def test_pay_adds_rows_and_import_recognises_them(biz):
|
||||
inv = make(biz)
|
||||
paid = biz.post(f"/api/invoices/{inv['id']}/pay", json={"date": "2026-04-20"}).json()
|
||||
assert (paid["status"], paid["paid_date"]) == ("paid", "2026-04-20")
|
||||
added = [r for r in rows(biz) if r["reference"].startswith("INV-0001")]
|
||||
assert sorted((r["income"], r["gst"], r["category"]) for r in added) == [(50, "Excl GST", "Sales"),
|
||||
(115, "Incl GST", "Sales")]
|
||||
assert biz.get("/api/gst?book=b").json()["periods"][0]["boxes"]["8"] == 15.0
|
||||
|
||||
# the bank line for the payment ($165 on the same day) is then seen as already in the book
|
||||
csv = SAMPLE_CSV.splitlines()[0] + "\nDirect Credit,Client Ltd,INV-0001,,,165.00,20/04/2026,,\n"
|
||||
path = biz.post("/api/upload?filename=p.csv", content=csv.encode()).json()["path"]
|
||||
plan = biz.post("/api/preview", json={"book": "b", "statements": [path]}).json()
|
||||
assert [t["status"] for t in plan] == ["duplicate"]
|
||||
|
||||
unpaid = biz.post(f"/api/invoices/{inv['id']}/unpay").json()
|
||||
assert (unpaid["status"], unpaid["payments"]) == ("sent", [])
|
||||
assert not [r for r in rows(biz) if r["reference"].startswith("INV-0001")]
|
||||
|
||||
|
||||
def test_pay_on_imported_bank_line(biz):
|
||||
path = biz.post("/api/upload?filename=s.csv", content=SAMPLE_CSV.encode()).json()["path"]
|
||||
biz.post("/api/import", json={"book": "b", "statements": [path]})
|
||||
# SAMPLE_CSV has "Client Ltd" paying $1150.00 on 20 April
|
||||
inv = make(biz, lines=[{"description": "Consulting", "qty": 10, "unit": 100, "taxable": True}])
|
||||
[m] = biz.get(f"/api/invoices/{inv['id']}/matches?date=2026-04-18").json()
|
||||
assert (m["amount"], m["days_apart"]) == (1150, 2)
|
||||
|
||||
biz.post(f"/api/invoices/{inv['id']}/pay", json={"date": "2026-04-18", "row": m["row"]})
|
||||
line = next(r for r in rows(biz) if r["row"] == m["row"])
|
||||
assert (line["category"], line["gst"], line["income"]) == ("Sales", "Incl GST", 1150)
|
||||
assert biz.get(f"/api/invoices/{inv['id']}").json()["paid_date"] == "2026-04-20" # the bank's date
|
||||
|
||||
biz.post(f"/api/invoices/{inv['id']}/unpay")
|
||||
line = next(r for r in rows(biz) if r["row"] == m["row"])
|
||||
assert line["income"] == 1150 and line["category"] == "" # put back as it was imported
|
||||
|
||||
|
||||
def test_pay_needs_a_book(biz):
|
||||
inv = make(biz)
|
||||
r = biz.post(f"/api/invoices/{inv['id']}/pay", json={"date": "2025-06-01"})
|
||||
assert r.status_code == 400 and "no book" in r.json()["detail"]
|
||||
|
||||
|
||||
def test_pdf(biz):
|
||||
inv = make(biz, notes="Thanks for your business")
|
||||
r = biz.get(f"/api/invoices/{inv['id']}/pdf")
|
||||
assert r.status_code == 200 and r.content.startswith(b"%PDF")
|
||||
assert 'filename="INV-0001_Client_Ltd.pdf"' in r.headers["content-disposition"]
|
||||
if shutil.which("pdftotext"):
|
||||
text = subprocess.run(["pdftotext", "-layout", "-", "-"], input=r.content, capture_output=True).stdout.decode()
|
||||
for s in ("TAX INVOICE", "INV-0001", "Client Ltd", "$165.00", "123-456-789", "No GST on this item"):
|
||||
assert s in text, s
|
||||
|
||||
|
||||
def test_logo(biz, tmp_path):
|
||||
from PIL import Image
|
||||
import io
|
||||
buf = io.BytesIO()
|
||||
Image.new("RGB", (40, 20), "red").save(buf, "PNG")
|
||||
assert biz.post("/api/business/logo", content=b"hello").status_code == 400
|
||||
assert biz.post("/api/business/logo", content=buf.getvalue()).json()["logo_file"] == "logo.png"
|
||||
assert biz.get("/api/business/logo").content == buf.getvalue()
|
||||
assert biz.get(f"/api/invoices/{make(biz)['id']}/pdf").status_code == 200
|
||||
assert biz.delete("/api/business/logo").json()["logo_file"] is None
|
||||
assert not (tmp_path / "invoices" / "logo.png").exists()
|
||||
|
||||
|
||||
def test_needs_sign_in(client):
|
||||
assert client.get("/api/invoices").status_code == 401
|
||||
assert client.get("/api/business").status_code == 401
|
||||
assert client.post("/api/customers", json={"name": "x"}).status_code == 401
|
||||
@@ -0,0 +1,81 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cars(admin):
|
||||
admin.ute = admin.post("/api/vehicles", json={"name": "Ute", "rego": "abc123"}).json()
|
||||
admin.van = admin.post("/api/vehicles", json={"name": "Van"}).json()
|
||||
return admin
|
||||
|
||||
|
||||
def trip(client, vehicle, day, **kw):
|
||||
r = client.post("/api/trips", json={"vehicle_id": vehicle["id"], "date": day, "purpose": "Site visit", **kw})
|
||||
assert r.status_code == 200, r.text
|
||||
return r.json()
|
||||
|
||||
|
||||
def test_odometer_km(cars):
|
||||
t = trip(cars, cars.ute, "2026-05-01", odo_start=10000, odo_end=10042)
|
||||
assert (t["km"], t["vehicle"], t["warnings"]) == (42, "Ute", [])
|
||||
assert cars.ute["rego"] == "ABC123"
|
||||
bad = {"vehicle_id": cars.ute["id"], "date": "2026-05-02"}
|
||||
assert cars.post("/api/trips", json={**bad, "odo_start": 10100, "odo_end": 10050}).status_code == 400
|
||||
assert cars.post("/api/trips", json=bad).status_code == 400 # no distance at all
|
||||
assert cars.post("/api/trips", json={**bad, "km": 5, "vehicle_id": 999}).status_code == 404
|
||||
# overlapping readings are kept, with a warning
|
||||
assert trip(cars, cars.ute, "2026-05-02", odo_start=10030, odo_end=10060)["warnings"]
|
||||
# km typed directly
|
||||
assert trip(cars, cars.van, "2026-05-03", km=12.5)["km"] == 12.5
|
||||
|
||||
|
||||
def test_report_tiers_per_vehicle(cars):
|
||||
r = cars.get("/api/mileage/report?year=2026").json()
|
||||
assert (r["claim"], r["vehicles"], r["rate"]) == (None, [], None)
|
||||
|
||||
trip(cars, cars.ute, "2026-04-01", km=15000)
|
||||
trip(cars, cars.ute, "2026-06-01", km=1000, business=False)
|
||||
trip(cars, cars.van, "2027-03-31", km=10000)
|
||||
trip(cars, cars.van, "2027-04-01", km=999) # next year
|
||||
|
||||
r = cars.get("/api/mileage/report?year=2026").json()
|
||||
assert r["claim"] is None and r["vehicles"][0]["claim"] is None # no rate yet: no claim, not zero
|
||||
|
||||
assert cars.put("/api/mileage/rates/2026", json={"tier1": 0, "tier2": 0.3}).status_code == 400
|
||||
cars.put("/api/mileage/rates/2026", json={"tier1": 1.17, "tier2": 0.37})
|
||||
r = cars.get("/api/mileage/report?year=2026").json()
|
||||
ute, van = r["vehicles"]
|
||||
assert (ute["business_km"], ute["private_km"], ute["business_percent"]) == (15000, 1000, 93.8)
|
||||
assert (ute["tier1_km"], ute["tier2_km"], ute["claim"]) == (14000, 1000, 16380 + 370)
|
||||
# the 14,000 km limit is per vehicle
|
||||
assert (van["tier1_km"], van["tier2_km"], van["claim"]) == (10000, 0, 11700)
|
||||
assert r["claim"] == 16750 + 11700
|
||||
assert r["business_km"] == 25000
|
||||
|
||||
|
||||
def test_export_and_vehicle_delete(cars):
|
||||
trip(cars, cars.ute, "2026-05-01", odo_start=100, odo_end=150, to_place="Hamilton", customer="Client Ltd")
|
||||
trip(cars, cars.ute, "2026-05-02", km=7, business=False)
|
||||
r = cars.get("/api/mileage/export?year=2026")
|
||||
assert r.status_code == 200 and "logbook-2026-27.csv" in r.headers["content-disposition"]
|
||||
lines = r.content.decode("utf-8-sig").splitlines()
|
||||
assert lines[1] == "2026-05-01,Ute,,Hamilton,Site visit,Client Ltd,100,150,50,Business"
|
||||
assert lines[2].endswith(",,,7,Private")
|
||||
|
||||
assert cars.delete(f"/api/vehicles/{cars.ute['id']}").status_code == 400
|
||||
assert cars.put(f"/api/vehicles/{cars.ute['id']}", json={"active": False}).json()["active"] == 0
|
||||
assert cars.delete(f"/api/vehicles/{cars.van['id']}").status_code == 200
|
||||
assert cars.post("/api/vehicles", json={"name": "Ute"}).status_code == 400
|
||||
|
||||
|
||||
def test_edit_and_delete_trip(cars):
|
||||
t = trip(cars, cars.ute, "2026-05-01", km=10)
|
||||
r = cars.put(f"/api/trips/{t['id']}", json={"vehicle_id": cars.ute["id"], "date": "2026-05-01",
|
||||
"odo_start": 5, "odo_end": 25})
|
||||
assert r.json()["km"] == 20
|
||||
cars.delete(f"/api/trips/{t['id']}")
|
||||
assert cars.get("/api/trips?year=2026").json() == []
|
||||
|
||||
|
||||
def test_needs_sign_in(client):
|
||||
assert client.get("/api/trips").status_code == 401
|
||||
assert client.get("/api/mileage/report?year=2026").status_code == 401
|
||||
@@ -0,0 +1,321 @@
|
||||
import os
|
||||
import shutil
|
||||
import sqlite3
|
||||
|
||||
import pytest
|
||||
|
||||
import ledger
|
||||
from cashbook_app.services import receipts
|
||||
from cashbook_app.services.receipts import parse_receipt
|
||||
from conftest import SAMPLE_CSV
|
||||
|
||||
TILL_RECEIPT = """
|
||||
BUNNINGS WAREHOUSE
|
||||
Mt Wellington
|
||||
TAX INVOICE GST No: 123-456-789
|
||||
Date: 14/05/2026 Time 10:42
|
||||
2 x Timber 90x45 24.50
|
||||
Screws Galv 8g 12,99
|
||||
Paint Brush 50mm 9.90
|
||||
SUBTOTAL 47.39
|
||||
TOTAL $47.39
|
||||
GST INCLUDED 6.18
|
||||
EFTPOS $47.39
|
||||
CHANGE 0.00
|
||||
"""
|
||||
|
||||
INVOICE = """Acme Consulting Ltd
|
||||
Invoice INV-0042
|
||||
Invoice Date: 3 Jun 2026
|
||||
Due Date: 20 Jun 2026
|
||||
GST Reg 98-765-432
|
||||
Web design services 1,000.00
|
||||
Subtotal (excl GST) 1,000.00
|
||||
GST 15% 150.00
|
||||
Total NZD 1,150.00
|
||||
"""
|
||||
|
||||
|
||||
def test_parse_till_receipt():
|
||||
r = parse_receipt(TILL_RECEIPT)
|
||||
assert r["vendor"] == "BUNNINGS WAREHOUSE"
|
||||
assert r["date"] == "2026-05-14"
|
||||
assert (r["total_cents"], r["gst_cents"], r["gst_number"]) == (4739, 618, "123-456-789")
|
||||
assert r["items"] == [{"description": "Timber 90x45", "amount": 24.5},
|
||||
{"description": "Screws Galv 8g", "amount": 12.99}, # OCR read the point as a comma
|
||||
{"description": "Paint Brush 50mm", "amount": 9.9}]
|
||||
|
||||
|
||||
def test_parse_invoice():
|
||||
r = parse_receipt(INVOICE)
|
||||
assert r["vendor"] == "Acme Consulting Ltd"
|
||||
assert r["date"] == "2026-06-03" # the invoice date, not the due date
|
||||
assert (r["total_cents"], r["gst_cents"], r["gst_number"]) == (115000, 15000, "098-765-432")
|
||||
assert r["items"] == [{"description": "Web design services", "amount": 1000.0}]
|
||||
|
||||
|
||||
def test_parse_fallbacks():
|
||||
# no "total" line: the card payment is the total; GST far from 3/23 of it is ignored
|
||||
r = parse_receipt("Corner Dairy\n12 Mar 2026\nMilk 3.50\nBread 4.20\nEFTPOS 7.70\nGST 5.00\n")
|
||||
assert (r["date"], r["total_cents"], r["gst_cents"]) == ("2026-03-12", 770, None)
|
||||
assert parse_receipt("")["total_cents"] is None
|
||||
# OCR noise inside the GST number
|
||||
assert parse_receipt("TAX INVOICE GST No: 45-6/78-912\nTOTAL 10.00")["gst_number"] == "045-678-912"
|
||||
assert parse_receipt("GST 150.00 150.00\nTOTAL 1150.00")["gst_cents"] == 15000
|
||||
|
||||
|
||||
def png(seed: bytes) -> bytes:
|
||||
return b"\x89PNG\r\n\x1a\n" + seed * 20 # only the header is checked; OCR is faked below
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def books(admin, monkeypatch):
|
||||
"""A book with SAMPLE_CSV imported, and OCR replaced by whatever `fake_ocr` holds."""
|
||||
admin.post("/api/books", json={"name": "b", "year": 2026})
|
||||
path = admin.post("/api/upload?filename=s.csv", content=SAMPLE_CSV.encode()).json()["path"]
|
||||
admin.post("/api/import", json={"book": "b", "statements": [path]})
|
||||
admin.fake_ocr = {"status": "done", "text": "", "items": []}
|
||||
monkeypatch.setattr(receipts, "read_document", lambda p, mime: dict(admin.fake_ocr))
|
||||
return admin
|
||||
|
||||
|
||||
def rows(client, sheet):
|
||||
return client.get(f"/api/month?book=b&sheet={sheet}").json()["rows"]
|
||||
|
||||
|
||||
def test_upload_suggest_and_link(books, tmp_path):
|
||||
books.fake_ocr.update(vendor="Jaycar Electronics", date="2026-04-12", total_cents=11500, gst_cents=1500,
|
||||
items=[{"description": "Soldering iron", "amount": 115.0}])
|
||||
r = books.post("/api/attachments?filename=jaycar.png", content=png(b"j"))
|
||||
assert r.status_code == 200, r.text
|
||||
a = r.json()
|
||||
assert (a["vendor"], a["total"], a["gst"], a["transaction"]) == ("Jaycar Electronics", 115.0, 15.0, None)
|
||||
assert a["items"][0]["description"] == "Soldering iron"
|
||||
[s] = a["suggestions"]
|
||||
assert (s["reference"], s["amount"], s["days_apart"], s["sheet"]) == ("Jaycar Chris", -115.0, 1, "APR")
|
||||
|
||||
# the same file again is refused, and its copy isn't kept
|
||||
assert books.post("/api/attachments?filename=again.png", content=png(b"j")).status_code == 400
|
||||
assert len(os.listdir(tmp_path / "attachments")) == 1
|
||||
|
||||
unmatched = books.get("/api/attachments?unmatched=1").json()
|
||||
assert [x["id"] for x in unmatched] == [a["id"]]
|
||||
|
||||
linked = books.post(f"/api/attachments/{a['id']}/link", json={"row": s["row"]}).json()
|
||||
assert linked["transaction"]["reference"] == "Jaycar Chris"
|
||||
assert linked["proposed"] == {} # already Incl GST, and nothing to go on for a category
|
||||
assert books.get("/api/attachments?unmatched=1").json() == []
|
||||
assert next(r for r in rows(books, "APR") if r["row"] == s["row"])["attachments"] == 1
|
||||
assert [x["id"] for x in books.get(f"/api/row/{s['row']}/attachments").json()] == [a["id"]]
|
||||
|
||||
f = books.get(f"/api/attachments/{a['id']}/file")
|
||||
assert f.status_code == 200 and f.content == png(b"j") and f.headers["content-type"] == "image/png"
|
||||
|
||||
books.post(f"/api/attachments/{a['id']}/unlink")
|
||||
assert next(r for r in rows(books, "APR") if r["row"] == s["row"])["attachments"] == 0
|
||||
|
||||
assert books.delete(f"/api/attachments/{a['id']}").json() == {"ok": True}
|
||||
assert os.listdir(tmp_path / "attachments") == []
|
||||
assert books.get(f"/api/attachments/{a['id']}").status_code == 404
|
||||
|
||||
|
||||
def test_link_offers_gst_and_category(books):
|
||||
hetzner = next(r for r in rows(books, "MAY") if "Hetzner" in r["reference"])
|
||||
assert hetzner["gst"] == "Excl GST"
|
||||
# uploaded from the transaction's row: linked straight away, with changes to offer
|
||||
books.fake_ocr.update(vendor="Hetzner Online", total_cents=553, gst_cents=72, gst_number="123-456-789")
|
||||
a = books.post(f"/api/attachments?filename=h.pdf&kind=invoice&row={hetzner['row']}",
|
||||
content=b"%PDF-1.4 hetzner").json()
|
||||
assert (a["kind"], a["files"][0]["mime"], a["transaction"]["row"]) == ("invoice", "application/pdf", hetzner["row"])
|
||||
assert a["proposed"] == {"gst": "Incl GST"}
|
||||
|
||||
a = books.post(f"/api/attachments/{a['id']}/link",
|
||||
json={"row": hetzner["row"], "apply": {"gst": "Incl GST", "category": "Purchases"}}).json()
|
||||
assert a["proposed"] == {}
|
||||
hetzner = next(r for r in rows(books, "MAY") if r["row"] == hetzner["row"])
|
||||
assert (hetzner["gst"], hetzner["category"], hetzner["attachments"]) == ("Incl GST", "Purchases", 1)
|
||||
|
||||
r = books.post(f"/api/attachments/{a['id']}/link", json={"row": hetzner["row"], "apply": {"amount": "1"}})
|
||||
assert r.status_code == 400
|
||||
|
||||
|
||||
def test_category_from_vendor_and_history(books, tmp_path):
|
||||
con = sqlite3.connect(tmp_path / "cashbook.db")
|
||||
with con:
|
||||
con.execute("UPDATE transactions SET category = NULL")
|
||||
fee = next(r for r in rows(books, "APR") if r["reference"] == "Monthly A/C Fee")
|
||||
books.fake_ocr.update(vendor="Spark NZ", total_cents=850)
|
||||
a = books.post(f"/api/attachments?filename=spark.png&row={fee['row']}", content=png(b"s")).json()
|
||||
assert a["proposed"] == {"category": "Telephone & Power"}
|
||||
|
||||
|
||||
def test_edit_details_updates_suggestions(books):
|
||||
books.fake_ocr.update(status="failed", error="tesseract isn't installed")
|
||||
r = books.post("/api/attachments?filename=blurry.jpg", content=b"\xff\xd8\xff" + b"x" * 50).json()
|
||||
assert (r["ocr_status"], r["ocr_error"], r["suggestions"]) == ("failed", "tesseract isn't installed", [])
|
||||
|
||||
a = books.patch(f"/api/attachments/{r['id']}", json={"total": 1150, "doc_date": "2026-04-21",
|
||||
"vendor": "Client Ltd"}).json()
|
||||
assert (a["total_cents"], a["vendor"]) == (115000, "Client Ltd")
|
||||
assert [s["reference"] for s in a["suggestions"]] == ["Client Ltd (Inv 101)"]
|
||||
# too far from the transaction's date
|
||||
a = books.patch(f"/api/attachments/{r['id']}", json={"doc_date": "2026-05-21"}).json()
|
||||
assert a["suggestions"] == []
|
||||
assert books.patch(f"/api/attachments/{r['id']}", json={"doc_date": "21/05/2026"}).status_code == 400
|
||||
assert books.patch(f"/api/attachments/{r['id']}", json={"kind": "bill"}).status_code == 400
|
||||
|
||||
|
||||
def test_several_photos_of_one_receipt(books, tmp_path, monkeypatch):
|
||||
# a long receipt in two photos: the top has the shop and date, the bottom the total
|
||||
pages = {b"top": "JAYCAR ELECTRONICS\nDate: 12/04/2026\nSoldering Station 89.00\n",
|
||||
b"bottom": "Solder 26.00\nTOTAL $115.00\nGST INCLUDED 15.00\nGST No: 45-678-912\n"}
|
||||
def read(p, mime):
|
||||
text = pages[p.read_bytes()[8:]]
|
||||
return {"status": "done", "text": text, **parse_receipt(text)}
|
||||
monkeypatch.setattr(receipts, "read_document", read)
|
||||
|
||||
def photo(name):
|
||||
return b"\x89PNG\r\n\x1a\n" + name
|
||||
|
||||
top = books.post("/api/attachments?filename=top.png", content=photo(b"top")).json()
|
||||
# with no TOTAL line, the top alone gives a wrong guess at the total
|
||||
assert (top["total"], top["suggestions"], len(top["files"])) == (89.0, [], 1)
|
||||
books.patch(f"/api/attachments/{top['id']}", json={"vendor": "Jaycar"}) # a correction by hand
|
||||
|
||||
a = books.post(f"/api/attachments/{top['id']}/pages?filename=bottom.png", content=photo(b"bottom")).json()
|
||||
assert [f["filename"] for f in a["files"]] == ["top.png", "bottom.png"]
|
||||
# the details come from both pages read together, except the correction, which is kept
|
||||
assert (a["vendor"], a["doc_date"], a["total"], a["gst"], a["gst_number"], a["edited"]) == \
|
||||
("Jaycar", "2026-04-12", 115.0, 15.0, "045-678-912", ["vendor"])
|
||||
assert [i["description"] for i in a["items"]] == ["Soldering Station", "Solder"]
|
||||
assert a["raw_text"].startswith("JAYCAR") and "TOTAL" in a["raw_text"]
|
||||
assert [s["reference"] for s in a["suggestions"]] == ["Jaycar Chris"]
|
||||
assert len(books.get("/api/attachments").json()) == 1
|
||||
|
||||
bottom_id = a["files"][1]["id"]
|
||||
f = books.get(f"/api/attachments/{a['id']}/files/{bottom_id}")
|
||||
assert f.status_code == 200 and f.content == photo(b"bottom")
|
||||
assert books.get(f"/api/attachments/{a['id']}/file").content == photo(b"top")
|
||||
assert books.get(f"/api/attachments/999/files/{bottom_id}").status_code == 404
|
||||
|
||||
# the same photo can't go on twice, here or on another receipt
|
||||
r = books.post(f"/api/attachments/{a['id']}/pages?filename=again.png", content=photo(b"bottom"))
|
||||
assert r.status_code == 400
|
||||
assert books.post(f"/api/attachments/999/pages?filename=x.png", content=photo(b"x")).status_code == 404
|
||||
assert len(os.listdir(tmp_path / "attachments")) == 2
|
||||
|
||||
a = books.post(f"/api/attachments/{a['id']}/reread").json()
|
||||
assert (a["vendor"], a["edited"]) == ("JAYCAR ELECTRONICS", []) # re-reading replaces corrections
|
||||
|
||||
# without the bottom, it's back to what the top says
|
||||
books.patch(f"/api/attachments/{a['id']}", json={"gst": 15})
|
||||
a = books.delete(f"/api/attachments/{a['id']}/pages/{bottom_id}").json()
|
||||
assert ([f["filename"] for f in a["files"]], a["total"], a["gst"], a["gst_number"]) == \
|
||||
(["top.png"], 89.0, 15.0, None)
|
||||
assert len(os.listdir(tmp_path / "attachments")) == 1
|
||||
r = books.delete(f"/api/attachments/{a['id']}/pages/{a['files'][0]['id']}")
|
||||
assert r.status_code == 400 and "only page" in r.json()["detail"]
|
||||
|
||||
books.post(f"/api/attachments/{a['id']}/pages?filename=bottom.png", content=photo(b"bottom"))
|
||||
books.delete(f"/api/attachments/{a['id']}")
|
||||
assert os.listdir(tmp_path / "attachments") == []
|
||||
|
||||
|
||||
def test_rejects_other_files(books, tmp_path):
|
||||
r = books.post("/api/attachments?filename=a.txt", content=b"hello")
|
||||
assert r.status_code == 400 and "JPEG" in r.json()["detail"]
|
||||
r = books.post("/api/attachments?filename=a.heic", content=b"\0\0\0\x18ftypheic" + b"\0" * 20)
|
||||
assert r.status_code == 400 and "HEIC" in r.json()["detail"]
|
||||
assert books.post("/api/attachments?filename=a.png", content=b"").status_code == 400
|
||||
assert books.post("/api/attachments?filename=a.png&row=9999", content=png(b"z")).status_code == 404
|
||||
assert os.listdir(tmp_path / "attachments") == []
|
||||
|
||||
|
||||
def test_needs_sign_in(client):
|
||||
assert client.get("/api/attachments").status_code == 401
|
||||
assert client.post("/api/attachments?filename=a.png", content=png(b"a")).status_code == 401
|
||||
|
||||
|
||||
def test_upgrade_keeps_attachment_reference(tmp_path):
|
||||
"""The v1 -> v2 table rebuild mustn't leave attachments pointing at the old table."""
|
||||
path = tmp_path / "old.db"
|
||||
con = sqlite3.connect(path)
|
||||
con.executescript(ledger.SCHEMA.replace("'Zero-rated', ", ""))
|
||||
con.close()
|
||||
conn = ledger.connect(path)
|
||||
sql = conn.execute("SELECT sql FROM sqlite_master WHERE name = 'attachments'").fetchone()[0]
|
||||
assert "transactions_v1" not in sql
|
||||
assert "Zero-rated" in conn.execute("SELECT sql FROM sqlite_master WHERE name = 'transactions'").fetchone()[0]
|
||||
|
||||
|
||||
V2_ATTACHMENTS = """CREATE TABLE attachments (
|
||||
id INTEGER PRIMARY KEY,
|
||||
transaction_id INTEGER REFERENCES transactions(id) ON DELETE SET NULL,
|
||||
kind TEXT NOT NULL DEFAULT 'receipt' CHECK (kind IN ('receipt', 'invoice')),
|
||||
filename TEXT NOT NULL, stored_name TEXT NOT NULL UNIQUE, mime TEXT NOT NULL,
|
||||
sha256 TEXT NOT NULL UNIQUE, size INTEGER NOT NULL,
|
||||
vendor TEXT, doc_date TEXT, total_cents INTEGER, gst_cents INTEGER, gst_number TEXT,
|
||||
items_json TEXT, raw_text TEXT,
|
||||
ocr_status TEXT NOT NULL DEFAULT 'skipped' CHECK (ocr_status IN ('done', 'failed', 'skipped')),
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP);
|
||||
CREATE INDEX attachments_transaction ON attachments (transaction_id);"""
|
||||
|
||||
|
||||
def test_upgrade_moves_files_to_pages(tmp_path):
|
||||
"""Version 2 databases had one file per attachment, in the attachments table."""
|
||||
path = tmp_path / "v2.db"
|
||||
con = sqlite3.connect(path)
|
||||
con.executescript("""CREATE TABLE books (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE,
|
||||
start_year INTEGER NOT NULL, opening_cents INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP);
|
||||
CREATE TABLE transactions (id INTEGER PRIMARY KEY,
|
||||
book_id INTEGER NOT NULL REFERENCES books(id) ON DELETE CASCADE, date TEXT NOT NULL,
|
||||
reference TEXT NOT NULL DEFAULT '', origin TEXT NOT NULL DEFAULT '', amount_cents INTEGER NOT NULL,
|
||||
gst TEXT NOT NULL CHECK (gst IN ('Incl GST', 'Zero-rated', 'Excl GST')), category TEXT, source TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP);""" + V2_ATTACHMENTS + """
|
||||
INSERT INTO books (id, name, start_year) VALUES (1, 'b', 2026);
|
||||
INSERT INTO transactions (id, book_id, date, reference, amount_cents, gst) VALUES (5, 1, '2026-04-13', 'Jaycar', -11500, 'Incl GST');
|
||||
INSERT INTO attachments (id, transaction_id, filename, stored_name, mime, sha256, size, vendor, total_cents,
|
||||
items_json, raw_text, ocr_status) VALUES (7, 5, 'r.jpg', 'abc-1.jpg', 'image/jpeg', 'abc', 10, 'Jaycar',
|
||||
11500, '[]', 'JAYCAR TOTAL 115.00', 'done');
|
||||
PRAGMA user_version = 2;""")
|
||||
con.close()
|
||||
|
||||
conn = ledger.connect(path)
|
||||
assert conn.execute("PRAGMA user_version").fetchone()[0] == ledger.SCHEMA_VERSION
|
||||
a = ledger.get_attachment(conn, 7)
|
||||
assert a["edited"] == []
|
||||
assert (a["transaction"]["row"], a["vendor"], a["total"], a["raw_text"], a["filename"]) == \
|
||||
(5, "Jaycar", 115.0, "JAYCAR TOTAL 115.00", "r.jpg")
|
||||
assert [(f["filename"], f["mime"], f["size"]) for f in a["files"]] == [("r.jpg", "image/jpeg", 10)]
|
||||
assert ledger.get_file(conn, 7)["stored_name"] == "abc-1.jpg"
|
||||
assert conn.execute("SELECT name FROM sqlite_master WHERE name = 'attachments_transaction'").fetchone()
|
||||
assert "attachments_v2" not in conn.execute(
|
||||
"SELECT sql FROM sqlite_master WHERE name = 'attachment_files'").fetchone()[0]
|
||||
assert (tmp_path / "v2.db.v2.bak").exists()
|
||||
# pages go with their attachment, and the link still works both ways
|
||||
conn.execute("DELETE FROM transactions WHERE id = 5")
|
||||
assert ledger.get_attachment(conn, 7)["transaction_id"] is None
|
||||
ledger.delete_attachment(conn, 7)
|
||||
assert conn.execute("SELECT COUNT(*) FROM attachment_files").fetchone()[0] == 0
|
||||
conn.close()
|
||||
ledger.connect(path).close() # and connecting again changes nothing
|
||||
|
||||
|
||||
@pytest.mark.skipif(not shutil.which("tesseract"), reason="tesseract isn't installed")
|
||||
def test_ocr_round_trip():
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
img = Image.new("RGB", (900, 700), "white")
|
||||
draw = ImageDraw.Draw(img)
|
||||
try:
|
||||
font = ImageFont.truetype("DejaVuSansMono.ttf", 34)
|
||||
except OSError:
|
||||
font = ImageFont.load_default(size=34)
|
||||
for i, line in enumerate(TILL_RECEIPT.strip().splitlines()):
|
||||
draw.text((40, 30 + i * 50), line.strip(), fill="black", font=font)
|
||||
import io
|
||||
buf = io.BytesIO()
|
||||
img.save(buf, format="JPEG", quality=90)
|
||||
r = parse_receipt(receipts.ocr_image(buf.getvalue()))
|
||||
assert (r["date"], r["total_cents"], r["gst_cents"]) == ("2026-05-14", 4739, 618)
|
||||
assert "BUNNINGS" in r["vendor"]
|
||||
@@ -0,0 +1,84 @@
|
||||
import sqlite3
|
||||
|
||||
import pytest
|
||||
|
||||
import ledger
|
||||
from cashbook_app.services import receipts
|
||||
from test_receipts import png
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ocr(admin, monkeypatch):
|
||||
"""OCR replaced by whatever `fake_ocr` holds."""
|
||||
admin.fake_ocr = {"status": "done", "text": "", "items": []}
|
||||
monkeypatch.setattr(receipts, "read_document", lambda p, mime: dict(admin.fake_ocr))
|
||||
return admin
|
||||
|
||||
|
||||
def test_supplier_crud(admin):
|
||||
s = admin.post("/api/suppliers", json={"name": " Bunnings ", "gst_number": "123-456-789"}).json()
|
||||
assert (s["name"], s["gst_number"], s["documents"], s["total"]) == ("Bunnings", "123-456-789", 0, 0)
|
||||
assert admin.post("/api/suppliers", json={"name": ""}).status_code == 422
|
||||
s = admin.put(f"/api/suppliers/{s['id']}", json={"name": "Bunnings Ltd", "email": "a@b.nz"}).json()
|
||||
assert (s["name"], s["email"], s["gst_number"]) == ("Bunnings Ltd", "a@b.nz", "")
|
||||
assert [x["name"] for x in admin.get("/api/suppliers").json()] == ["Bunnings Ltd"]
|
||||
assert admin.delete(f"/api/suppliers/{s['id']}").json() == {"ok": True}
|
||||
assert admin.get(f"/api/suppliers/{s['id']}").status_code == 404
|
||||
|
||||
|
||||
def test_receipts_link_to_suppliers(ocr):
|
||||
sup = ocr.post("/api/suppliers", json={"name": "Jaycar Electronics Ltd", "gst_number": "098-765-432"}).json()
|
||||
|
||||
# the same name (ignoring Ltd and case) links on upload
|
||||
ocr.fake_ocr.update(vendor="JAYCAR ELECTRONICS", total_cents=11500)
|
||||
a = ocr.post("/api/attachments?filename=a.png", content=png(b"a")).json()
|
||||
assert a["supplier"] == {"id": sup["id"], "name": "Jaycar Electronics Ltd"}
|
||||
|
||||
# so does the GST number, whatever the name says
|
||||
ocr.fake_ocr.update(vendor="J'car", gst_number="098-765-432", total_cents=500)
|
||||
b = ocr.post("/api/attachments?filename=b.png", content=png(b"b")).json()
|
||||
assert b["supplier_id"] == sup["id"]
|
||||
|
||||
# an unknown vendor is left for the user
|
||||
ocr.fake_ocr.update(vendor="Somewhere Else", gst_number=None)
|
||||
c = ocr.post("/api/attachments?filename=c.png", content=png(b"c")).json()
|
||||
assert c["supplier"] is None
|
||||
|
||||
s = ocr.get(f"/api/suppliers/{sup['id']}").json()
|
||||
assert (s["documents"], s["total"]) == (2, 120.0)
|
||||
assert {x["id"] for x in ocr.get(f"/api/attachments?supplier={sup['id']}").json()} == {a["id"], b["id"]}
|
||||
|
||||
# link by hand, and unlink
|
||||
c = ocr.patch(f"/api/attachments/{c['id']}", json={"supplier_id": sup["id"]}).json()
|
||||
assert c["supplier"]["name"] == "Jaycar Electronics Ltd"
|
||||
assert c["vendor"] == "Somewhere Else" # what was read is kept
|
||||
assert ocr.patch(f"/api/attachments/{c['id']}", json={"supplier_id": None}).json()["supplier"] is None
|
||||
assert ocr.patch(f"/api/attachments/{c['id']}", json={"supplier_id": 999}).status_code == 404
|
||||
|
||||
# a supplier with documents can't be deleted until they're unlinked
|
||||
assert ocr.delete(f"/api/suppliers/{sup['id']}").status_code == 400
|
||||
|
||||
|
||||
def test_customer_invoices_filter(admin):
|
||||
admin.post("/api/books", json={"name": "b", "year": 2026})
|
||||
ids = [admin.post("/api/customers", json={"name": n}).json()["id"] for n in ("A", "B")]
|
||||
for cid in (ids[0], ids[1], ids[1]):
|
||||
r = admin.post("/api/invoices", json={"customer_id": cid, "issue_date": "2026-04-10",
|
||||
"lines": [{"description": "x", "unit": 10}]})
|
||||
assert r.status_code == 200, r.text
|
||||
assert len(admin.get(f"/api/invoices?customer={ids[1]}").json()) == 2
|
||||
assert len(admin.get("/api/invoices").json()) == 3
|
||||
|
||||
|
||||
def test_upgrade_adds_supplier_link(tmp_path):
|
||||
"""Databases from before suppliers get the new column and keep their receipts."""
|
||||
path = tmp_path / "old.db"
|
||||
con = sqlite3.connect(path)
|
||||
con.executescript(ledger.SCHEMA.replace(" supplier_id INTEGER REFERENCES suppliers(id), -- who the receipt or bill is from\n", ""))
|
||||
con.execute("INSERT INTO attachments (vendor) VALUES ('Old receipt')")
|
||||
con.execute("PRAGMA user_version = 4")
|
||||
con.commit()
|
||||
con.close()
|
||||
conn = ledger.connect(path)
|
||||
assert "supplier_id" in {c["name"] for c in conn.execute("PRAGMA table_info(attachments)")}
|
||||
assert ledger.list_attachments(conn)[0]["supplier"] is None
|
||||
Reference in new issue
Block a user