Skip to content
Snippets Groups Projects
Commit fc3ef913 authored by Daniele Nicolodi's avatar Daniele Nicolodi
Browse files

bestellanforderung: Add handling of new online Barkauf forms

Extract details from email message received after filling online
Barkauf form saved with attachments from Notes mail client.
parent 1aa6e13a
No related branches found
No related tags found
No related merge requests found
import bs4
import calendar
import click
import datetime
import decimal
import email
import email.policy
import email.utils
import locale
import os
import re
......@@ -122,6 +126,62 @@ def scrape_banfvorlage(filename, accounts):
data.Posting(account, amount, None, None, None, None)])
def scrape_teilbescheinigung(filename, narration, date, accounts):
"""Get information from email from online Barkauf form."""
with open(filename, 'rb') as fd:
msg = email.message_from_binary_file(fd, policy=email.policy.default)
if msg['From'] != 'Daniele Nicolodi <daniele.nicolodi@ptb.de>':
raise ValueError
meta = {'payment-request-date': email.utils.parsedate_to_datetime(msg['Date']).date()}
fields = {
# 'Firmenname': 'company',
# 'Beleg-ID (siehe Betreffzeile der Teilbescheinigung E-Mail)': 'document-id',
'Rechnungsnummer': 'invoice-number',
'Rechnungsdatum': 'invoice-date',
'Käufer-OE': 'division',
'Käufer-Name': 'name',
'Finanzposition': 'position',
'Finanzstelle': 'funds-center',
'Kostenstelle': 'cost-center',
'PSP-Element': 'psp',
# 'Inventarisierungsvermerk': 'inventory'
# 'Verwendungszweck'
# 'Sachlich richtig Name'
# 'Empfänger E-Mail-Adresse'
# 'Anmerkungen für weiteren Bearbeiter'
# 'Ausgefüllt von (Name)'
# 'Ausgefüllt von (E-Mail)'
'Rechnung hochladen': 'invoice-filename',
'ggf. Lieferschein hochladen': 'delivery-note-filename',
}
d = {}
body = msg.get_body(('html',))
content = bs4.BeautifulSoup(body.get_content(), 'lxml')
for tr in content.table.find_all('tr'):
td = tr.find_all('td')
assert len(td) == 2
name = ' '.join(td[0].b.stripped_strings)
value = ' '.join(td[1].stripped_strings)
key = fields.get(name)
if key:
d[key] = value
account = accounts.get(d.get('psp') or d.get('cost-center') or d.get('funds-center'))
if not account:
raise ValueError(f'unknown account in {filename}')
# The form does not contain amount information.
amount = data.Amount.from_string('0.00 EUR')
return data.Transaction(meta, date, '!', None, narration, EMPTY, EMPTY, [
data.Posting(account, amount, None, None, None, None)])
def scrape(folder, date, narration, accounts):
with os.scandir(folder) as it:
for entry in it:
......@@ -136,6 +196,8 @@ def scrape(folder, date, narration, accounts):
if m := re.match(r'Banfvorlage_(.*)\.xlsx$', entry.name):
return scrape_banfvorlage(filepath, accounts)
if m := re.match(r'Teilbescheinigung für Barkäufe - Beleg-ID_ (.*), Firmenname_ (.*).eml$', entry.name):
return scrape_teilbescheinigung(filepath, narration, date, accounts)
# Folder without any recognized form.
return data.Transaction({}, date, '?', None, narration, EMPTY, EMPTY, [])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment