Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an importer for Swisscard Cashback cards CSV exports. #103

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/importers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,14 @@ Import mt940 from `BCGE <https://www.bcge.ch/>`__
from tariochbctools.importers.bcge import importer as bcge

CONFIG = [bcge.BCGEImporter("/\d+\.mt940", "Assets:BCGE")]

Swisscard cards
---------------

Import Swisscard's `Cashback Cards <https://www.cashback-cards.ch/>` transactions from a CSV export.__

.. code-block:: python

from tariochbctools.importers.swisscard import importer as swisscard

CONFIG = [swisscard.SwisscardImporter("swisscard/.*\.csv", "Liabilities:Cashback")]
Empty file.
54 changes: 54 additions & 0 deletions src/tariochbctools/importers/swisscard/importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import csv
from io import StringIO

from beancount.core import amount, data
from beancount.core.number import D
from beancount.ingest import importer
from beancount.ingest.importers.mixins import identifier
from dateutil.parser import parse


class SwisscardImporter(identifier.IdentifyMixin, importer.ImporterProtocol):
"""An importer for Swisscard's cashback CSV files."""

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

def name(self):
return super().name() + self.account

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

def extract(self, file, existing_entries):
entries = []
with StringIO(file.contents()) as csvfile:
reader = csv.DictReader(
csvfile,
delimiter=",",
skipinitialspace=True,
)
for row in reader:
book_date = parse(row["Transaction date"].strip(), dayfirst=True).date()
amt = amount.Amount(-D(row["Amount"]), row["Currency"])
metakv = {
"category": row["Category"],
}
meta = data.new_metadata(file.name, 0, metakv)
description = row["Description"].strip()
entry = data.Transaction(
meta,
book_date,
"*",
"",
description,
data.EMPTY_SET,
data.EMPTY_SET,
[
data.Posting(self.account, amt, None, None, None, None),
],
)
entries.append(entry)

return entries
Loading