diff --git a/README.md b/README.md index 2d6ad38..04146c3 100644 --- a/README.md +++ b/README.md @@ -46,3 +46,24 @@ pip3 install fastapi uvicorn python-dotenv sqlalchemy psycopg2-binary openpyxl p - Alle Daten bleiben lokal auf Evo-X2 - Keine Datenverarbeitung in der Cloud - PyMuPDF läuft vollständig lokal + +## Wochenlauf (Cron NucBox) + +``` +0 6 * * 0 docker exec -w /app -e PYTHONPATH=/app invoice-check-backend python -m backend.grabber --send --report >> /home/c-stone-ai-lab/invoice-check/grabber.log 2>&1 +``` + +## Tools (`tools/`) + +| Datei | Zweck | Aufruf | +|-------|-------|--------| +| `qonto_abgleich.py` | Qonto-Auszüge (Text) ↔ Accountable-Export, Monatssummen + Lücken | lokal: `python3 tools/qonto_abgleich.py [stichtag]` | +| `mail_suche.py` | Postfächer (alle Ordner) nach Begriffen durchsuchen | Container, s. u. | +| `mail_pdfs.py` | PDF-Anhänge nach Absender/Betreff speichern | Container, s. u. | +| `accountable_helfer.js` | Banktx → neue Ausgabe im Accountable-Tab | Anleitung im Dateikopf | + +Container-Skripte (brauchen `backend` + IMAP-Env): +```bash +scp tools/mail_suche.py c-stone-ai-lab@nucbox:/tmp/ && ssh c-stone-ai-lab@nucbox 'docker cp /tmp/mail_suche.py invoice-check-backend:/tmp/ && docker exec -w /app -e PYTHONPATH=/app invoice-check-backend python /tmp/mail_suche.py ; rm /tmp/mail_suche.py' +``` +Danach `/tmp` im Container aufräumen. Ergebnisse (Belege, Listen) nur lokal unter `lokal/` ablegen, nie committen. diff --git a/tools/accountable_helfer.js b/tools/accountable_helfer.js new file mode 100644 index 0000000..4305030 --- /dev/null +++ b/tools/accountable_helfer.js @@ -0,0 +1,20 @@ +// Accountable-Browser-Helfer: Banktx "Zu kategorisieren" -> neue Ausgabe (Kategorie-Check, USt-Button, Speichern). +// Nutzung: im eingeloggten Accountable-Tab (Bank-Ansicht) per DevTools/javascript_tool einfügen, dann z.B. +// __bg([['suchbegriff', /regex-auf-zeilentext/, '19%']]); // vat: Button-Text, z.B. '19%', '0%', 'Ausländische USt.' +// Fortschritt: window.__log / window.__running / window.__cur. Bricht beim ersten Nicht-OK ab und verwirft das Formular. +// JS-Aufrufe < 45 s halten -> __bg läuft im Hintergrund, Status abfragen. Kontonummer der tx immer prüfen (privates Girokonto!). +window.__t=e=>(e.innerText||"").replace(/\u00AD/g,"").trim(); +window.__sleep=ms=>new Promise(r=>setTimeout(r,ms)); window.__btn=t=>[...document.querySelectorAll('button')].find(x=>__t(x)===t); +window.__kat=()=>{const l=[...document.querySelectorAll('label,span,div,p')].find(e=>__t(e)==='Kategorie'&&e.children.length===0); let n=l; for(let i=0;i<4&&n;i++){n=n.parentElement; const t=__t(n).replace('Kategorie','').replace('Kann ich das absetzen?','').trim(); if(t) return t.split('\n')[0];} return '?';}; +window.__ust=()=>{const u=[...document.querySelectorAll('div,span,p')].find(e=>/^USt\.: /.test(__t(e))&&e.children.length===0); return u?__t(u):'-';}; +window.__closeRating=()=>{const d=[...document.querySelectorAll('div')].find(x=>/Gefällt dir Accountable/.test(__t(x))&&__t(x).length<200); if(d){const b=d.querySelector('button'); b&&b.click();}}; +window.__search=async q=>{const inp=[...document.querySelectorAll('input')].find(i=>/Suche/.test((i.placeholder||'').replace(/­/g,''))); Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,'value').set.call(inp,q); inp.dispatchEvent(new Event('input',{bubbles:true})); await __sleep(2500);}; +window.__clickText=async(re)=>{const c=[...document.querySelectorAll('*')].filter(e=>re.test(__t(e))&&__t(e).length<80); if(!c.length) return false; const el=c[c.length-1]; (el.closest('button,[role=button],a')||el).click(); await __sleep(1500); return true;}; +window.__discard=async()=>{ if(__btn('Schließen')){ __btn('Schließen').click(); await __sleep(800); const ja=__btn('Ja'); if(ja){ja.click(); await __sleep(800);} } }; +window.__one=async(match,vat)=>{ const rows=[...document.querySelectorAll('tr')].filter(r=>{const t=__t(r).replace(/\s+/g,' '); return /Zu kategorisieren\s+-/.test(t)&&match.test(t.toLowerCase());}); if(!rows.length) return 'NONE'; const r=rows[0]; const desc=__t(r).replace(/\s+/g,' ').slice(0,60); const name=[...r.querySelectorAll('td')][1]||r; (name.querySelector('*')||name).click(); await __sleep(1800); + let ok=await __clickText(/^Neue Ausgabe erstellen/); if(!ok){ ok=await __clickText(/^Geschäftliche Ausgabe$/); if(ok) ok=await __clickText(/^Als neue Ausgabe hinzufügen/);} if(!ok||!__btn('Speichern')) return 'NOFORM '+desc; + const k=__kat(); if(!k||/Bitte|\?/.test(k)) return 'STOP-KAT '+k+' | '+desc; const b=__btn(vat); if(!b) return 'NOVAT '+desc; b.click(); await __sleep(700); + if(vat==='0%'){ const lab=[...document.querySelectorAll('*')].find(e=>/^Warum ist auf dieser Ausgabe keine USt/.test(__t(e))&&e.children.length===0); if(lab){ let n=lab.parentElement; const dd=n.querySelector('button,[role=combobox],[role=button]')||n.nextElementSibling; (dd.querySelector('button,[role=button]')||dd).click(); await __sleep(1000); const opt=[...document.querySelectorAll('*')].filter(e=>/^Der Artikel ist von der USt\. befreit/.test(__t(e))); if(!opt.length) return 'NOREASON '+desc; const o=opt[opt.length-1]; (o.closest('button,[role=button],label')||o.parentElement).click(); await __sleep(800);} } + const u=__ust(); if(vat==='Ausländische USt.'&&u!=='-') return 'STOP-UST '+u; __btn('Speichern').click(); await __sleep(2200); __closeRating(); if(__btn('Speichern')) return 'NOTSAVED '+desc; return 'OK '+k+' '+vat+' '+u+' | '+desc;}; +window.__bg=async(jobs)=>{window.__running=true; for(const [q,re,vat] of jobs){ window.__cur=q; await __search(q); for(let i=0;i<120;i++){ const o=await __one(re,vat); if(o==='NONE'){__log.push(q+': fertig');break;} __log.push(o); if(!o.startsWith('OK')){ await __discard(); __log.push(q+': abgebrochen'); break;} await __sleep(400);} } window.__running=false; }; +window.__log=[];"ok" \ No newline at end of file diff --git a/tools/mail_pdfs.py b/tools/mail_pdfs.py new file mode 100644 index 0000000..38e09f2 --- /dev/null +++ b/tools/mail_pdfs.py @@ -0,0 +1,32 @@ +"""PDF-Anhänge passender Mails aus Strato (INBOX + Archive) und Gmail (INBOX) speichern, nur lesend. + +Aufruf (im Backend-Container): python /tmp/mail_pdfs.py +Dateiname: YYYY-MM-DD_.pdf; gleicher Name wird nicht doppelt geschrieben. +""" +import email, email.utils, os, re, sys +from backend.routers.emails import connect_imap + +if len(sys.argv) != 4: sys.exit(__doc__) +sender, subject, out = sys.argv[1:] +os.makedirs(out, exist_ok=True) +boxes = [(os.getenv('IMAP_HOST', 'imap.strato.de'), os.getenv('IMAP_USER'), os.getenv('IMAP_PASSWORD'), ['INBOX', 'Archive', 'Archive.2026', 'Archive.2025']), + ('imap.gmail.com', os.getenv('GMAIL_IMAP_USER'), os.getenv('GMAIL_IMAP_PASSWORD'), ['INBOX'])] +got = [] +for h, u, pw, folders in boxes: + if not pw: continue + m = connect_imap(h, 993, u, pw) + for f in folders: + if m.select(f'"{f}"', readonly=True)[0] != 'OK': continue + for i in m.search(None, f'FROM "{sender}" SUBJECT "{subject}"')[1][0].split(): + msg = email.message_from_bytes(m.fetch(i, '(BODY.PEEK[])')[1][0][1]) + try: d = email.utils.parsedate_to_datetime(msg.get('Date')).date().isoformat() + except Exception: d = 'ohne-datum' + for part in msg.walk(): + fn = part.get_filename() + if not fn or not fn.lower().endswith('.pdf'): continue + safe = re.sub(r'[^\w.-]', '_', fn) + path = os.path.join(out, f'{d}_{safe}') + if os.path.exists(path): continue + open(path, 'wb').write(part.get_payload(decode=True)); got.append(path) + m.logout() +print(len(got), 'PDFs:'); print('\n'.join(got)) diff --git a/tools/mail_suche.py b/tools/mail_suche.py new file mode 100644 index 0000000..8ad2eab --- /dev/null +++ b/tools/mail_suche.py @@ -0,0 +1,29 @@ +"""Alle Ordner der Postfächer (Strato + optional Gmail) nach Begriffen durchsuchen, nur lesend. + +Aufruf (im Backend-Container): python /tmp/mail_suche.py [begriff ...] +Sucht je Begriff in FROM, SUBJECT und TEXT; zeigt die letzten 20 Treffer je Ordner/Kriterium. +""" +import email, os, sys +from backend.routers.emails import connect_imap, decode_hdr + +terms = sys.argv[1:] or sys.exit(__doc__) +boxes = [(os.getenv('IMAP_HOST', 'imap.strato.de'), int(os.getenv('IMAP_PORT', '993')), os.getenv('IMAP_USER'), os.getenv('IMAP_PASSWORD'))] +if os.getenv('GMAIL_IMAP_PASSWORD'): + boxes.append(('imap.gmail.com', 993, os.getenv('GMAIL_IMAP_USER'), os.getenv('GMAIL_IMAP_PASSWORD'))) +for h, p, u, pw in boxes: + m = connect_imap(h, p, u, pw) + for f in m.list()[1]: + name = f.decode().split(' "/" ')[-1].split(' "." ')[-1].strip() + if m.select(name if name.startswith('"') else f'"{name}"', readonly=True)[0] != 'OK': continue + for term in terms: + seen = set() + for crit in (f'FROM "{term}"', f'SUBJECT "{term}"', f'TEXT "{term}"'): + try: ids = m.search(None, crit)[1][0].split() + except Exception: continue + for i in ids[-20:]: + if i in seen: continue + seen.add(i) + hd = email.message_from_bytes(m.fetch(i, '(BODY.PEEK[HEADER.FIELDS (FROM SUBJECT DATE)])')[1][0][1]) + print(h[:12], name[:20], '|', term, '|', hd.get('Date', '')[:16], '|', + decode_hdr(hd.get('From', ''))[:40], '|', decode_hdr(hd.get('Subject', ''))[:70]) + m.logout() diff --git a/tools/qonto_abgleich.py b/tools/qonto_abgleich.py new file mode 100644 index 0000000..d50d0a9 --- /dev/null +++ b/tools/qonto_abgleich.py @@ -0,0 +1,63 @@ +"""Qonto-Kontoauszüge gegen Accountable-Export abgleichen. + +Aufruf: python tools/qonto_abgleich.py [stichtag YYYY-MM-DD] + enthält: + - YYYY-MM*.txt Qonto-Monatsauszüge als Text (pdftotext -layout) + - acc.txt Accountable-Export, eine Zeile je Banktx: + datum|betrag|name|zuordnung|kategorie||id +Schreibt /qonto.json, prüft Monatssummen, listet Lücken. +""" +import datetime as dt, glob, json, re, sys + +S = sys.argv[1] +STICHTAG = sys.argv[2] if len(sys.argv) > 2 else dt.date.today().isoformat() + +# --- Qonto-Auszüge parsen +q, summ = [], {} +for f in sorted(glob.glob(S + '/20*.txt')): + ym = f.split('/')[-1][:7]; y = ym[:4] + t = open(f).read() + e = re.search(r'Eingänge\s+\+ ([\d.]+) EUR', t); a = re.search(r'Ausgänge\s+- ([\d.]+) EUR', t) + summ[ym] = (float(e[1]), float(a[1])) + lines = t.split('\n') + for i, l in enumerate(lines): + r = re.match(r'^(\d\d)/(\d\d)\s{3,}(\S.*?)\s{2,}([+-]) ([\d.]+) EUR\s*$', l) + if r: + nxt = lines[i + 1].strip() if i + 1 < len(lines) else '' + # ponytail: Jahr = Auszugsjahr; Buchung aus Vorjahr im Januar-Auszug würde falsch datiert + q.append({'d': f'{y}-{r[2]}-{r[1]}', 'name': r[3].strip(), + 'amt': float(r[5]) * (1 if r[4] == '+' else -1), + 'info': re.sub(r'\s{2,}.*', '', nxt)[:60], 'ym': ym}) +json.dump(q, open(S + '/qonto.json', 'w'), ensure_ascii=False) + +print('--- Monatssummen (Auszug vs. geparst)') +for ym, (e, a) in summ.items(): + E = round(sum(x['amt'] for x in q if x['ym'] == ym and x['amt'] > 0), 2) + A = round(-sum(x['amt'] for x in q if x['ym'] == ym and x['amt'] < 0), 2) + ok = abs(e - E) < .01 and abs(a - A) < .01 + print(ym, 'ein', e, E, 'aus', a, A, 'OK' if ok else 'DIFF', sum(1 for x in q if x['ym'] == ym)) + +# --- Abgleich mit Accountable (Betrag exakt, Datum ±5 Tage, nächster Treffer) +acc = [] +for l in open(S + '/acc.txt'): + p = l.rstrip('\n').split('|') + acc.append({'d': p[0], 'amt': float(p[1]), 'name': p[2], 'm': p[3], 'cat': p[4], 'id': p[6], 'used': False}) +D = dt.date.fromisoformat +miss = [] +for x in q: + best = None + for a in acc: + if a['used'] or abs(a['amt'] - x['amt']) > 0.005: continue + dd = abs((D(a['d']) - D(x['d'])).days) + if dd <= 5 and (best is None or dd < best[0]): best = (dd, a) + if best: best[1]['used'] = True; x['acc'] = best[1] + else: miss.append(x) + +print('Qonto', len(q), 'gefunden', len(q) - len(miss), 'FEHLEN in Accountable:', len(miss)) +for x in miss: print(' ', x['d'], x['amt'], x['name'][:30], '|', x['info'][:40]) +extra = [a for a in acc if not a['used'] and a['d'] <= STICHTAG] +print(f'Accountable ohne Qonto-Gegenstück (bis {STICHTAG}):', len(extra)) +for a in extra: print(' ', a['d'], a['amt'], a['name'], a['m'] or '-', a['cat']) +nomatch = [x for x in q if 'acc' in x and not x['acc']['m']] +print('In Accountable, aber KEINER Einnahme/Ausgabe zugeordnet:', len(nomatch)) +for x in nomatch: print(' ', x['d'], x['amt'], x['name'][:28], '|', x['acc']['cat'] or 'unkategorisiert')