Skip to content

Commit

Permalink
adding viseca importer
Browse files Browse the repository at this point in the history
  • Loading branch information
tarioch committed Aug 17, 2022
1 parent 0035a42 commit 69f08b9
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/importers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,15 @@ Import CSV from `Neon <https://www.neon-free.ch/>`__
from tariochbctools.importers.neon import importer as neonimp
CONFIG = [neonimp.Importer("\d\d\d\d_account_statements\.csv", "Assets:Neon:CHF")]
Viseca One
----------

Import PDF from `Viseca One <https://one-digitalservice.ch/>`__

.. code-block:: python
from tariochbctools.importers.viseca import importer as visecaimp
CONFIG = [visecaimp.Importer(r"Kontoauszug.*\.pdf", "Assets:Viseca:CHF")]
Empty file.
95 changes: 95 additions & 0 deletions src/tariochbctools/importers/viseca/importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import re
from datetime import datetime

import camelot
from beancount.core import amount, data
from beancount.core.number import D
from beancount.ingest import importer
from beancount.ingest.importers.mixins import identifier


class Importer(identifier.IdentifyMixin, importer.ImporterProtocol):
"""An importer for Viseca One Card Statement PDF files."""

def __init__(self, regexps, account):
identifier.IdentifyMixin.__init__(self, matchers=[("filename", regexps)])
self.account = account
self.currency = "CHF"

def file_account(self, file):
return self.account

def createEntry(self, file, date, amt, text):
meta = data.new_metadata(file.name, 0)
return data.Transaction(
meta,
date,
"*",
"",
text,
data.EMPTY_SET,
data.EMPTY_SET,
[
data.Posting(self.account, amt, None, None, None, None),
],
)

def extract(self, file, existing_entries):
entries = []

p = re.compile(
r"^(?P<bookingDate>\d\d\.\d\d\.\d\d) (?P<valueDate>\d\d\.\d\d\.\d\d) (?P<detail>.*)$"
)

tables = camelot.read_pdf(
file.name, flavor="stream", table_areas=["65,450,575,100"]
)
for table in tables:
df = table.df

# skip incompatible tables
if df.columns.size != 4:
continue

lastTrxDate = None
lastAmount = None
lastDetails = ""
for _, row in df.iterrows():
dateValutaDetails, currency, amountCcy, amountChf = tuple(row)

if (
"XX XXXX" in dateValutaDetails
or "Kartenlimite" in dateValutaDetails
):
continue

trxDate = None
detail = ""
m = p.match(dateValutaDetails)
if m:
trxDate = m.group("valueDate")
detail = m.group("detail").strip() + " "
else:
detail = dateValutaDetails.strip() + " "

if amountChf:
if lastTrxDate:
amt = None
if "-" in lastAmount:
amt = -amount.Amount(D(lastAmount.strip(" -")), "CHF")
else:
amt = amount.Amount(D(lastAmount), "CHF")

book_date = datetime.strptime(lastTrxDate, "%d.%m.%y").date()

entries.append(
self.createEntry(file, book_date, amt, lastDetails.strip())
)

lastTrxDate = trxDate
lastAmount = amountChf
lastDetails = ""

lastDetails += detail + " "

return entries

0 comments on commit 69f08b9

Please sign in to comment.