byro-leinelab/byro_shackspace/utils.py

80 lines
2.6 KiB
Python
Raw Normal View History

2018-01-11 18:54:42 +01:00
import csv
import re
2018-01-11 19:24:13 +01:00
from datetime import date, datetime, timedelta
2018-01-11 18:54:42 +01:00
from decimal import Decimal
2018-01-11 19:24:13 +01:00
from django.db import transaction
2018-01-11 20:20:05 +01:00
from django.dispatch import receiver
2018-01-11 19:24:13 +01:00
from django.utils.timezone import now
2018-01-11 18:54:42 +01:00
2018-01-11 19:24:13 +01:00
from byro.bookkeeping.models import RealTransaction, TransactionChannel
2018-01-11 20:20:05 +01:00
from byro.bookkeeping.signals import derive_virtual_transactions
2018-01-11 19:24:13 +01:00
from byro.members.models import Member
2018-01-11 18:54:42 +01:00
2018-01-11 19:24:13 +01:00
@transaction.atomic
2018-01-11 20:20:05 +01:00
def process_bank_csv(source):
reader = csv.DictReader(open(source.source_file.name, encoding='iso-8859-1'), delimiter=';', quotechar='"')
2018-01-11 19:24:13 +01:00
booking_timestamp = now()
2018-01-11 18:54:42 +01:00
2018-01-11 19:24:13 +01:00
for line in reader:
if not line:
continue
reference = ''
for key in line.keys():
if key.startswith('VWZ'):
2018-01-11 20:20:05 +01:00
reference += line[key] + ' '
2018-01-11 18:54:42 +01:00
2018-01-11 19:24:13 +01:00
RealTransaction.objects.create(
channel=TransactionChannel.BANK,
booking_datetime=booking_timestamp,
value_datetime=datetime.strptime(line.get('Buchungstag'), '%d.%m.%Y'),
amount=Decimal(line.get('Betrag').replace('.', '').replace(',', '.')),
purpose=reference,
2018-01-11 20:20:05 +01:00
originator=line.get('Auftraggeber/Empfänger', '<leer>'),
2018-01-11 19:24:13 +01:00
importer='shack_bank_csv_importer',
source=source,
data=line,
)
2018-01-11 18:54:42 +01:00
2018-01-11 19:24:13 +01:00
def match_transaction(transaction):
uid, score = reference_parser(transaction.reference)
member = None
error = None
try:
if uid:
member = Member.objects.get(member_id=uid)
except Member.DoesNotExist:
error = "Member does not exist"
2018-01-11 18:54:42 +01:00
2018-01-11 19:24:13 +01:00
def reference_parser(self, reference):
reference = reference.lower()
2018-01-11 18:54:42 +01:00
2018-01-11 19:24:13 +01:00
regexes = (
r'.*mitgliedsbeitrag\s+id\s+(?P<ID>\d{1,4})\s.*',
r'.*id\s+(?P<ID>\d{1,4})\smitgliedsbeitrag.*',
# r'.*id\s+(?P<ID>\d{1,4})\s.*',
r'.*mitgliedsbeitrag.*id\s+(?P<ID>\d{1,4})\s.*',
r'.*mitgliedsbeitrag\s+(?P<ID>\d{1,4})\s.*',
r'.*beitrag\s+mitglied\s+(?P<ID>\d{1,4})\s.*',
r'.*mitgliedsbeitrag.*\s+(?P<ID>\d{1,4})[^\d].*',
# r'.*id(?P<ID>\d{1,4})\s+zr\d+.*',
# r'.*id\s+(?P<ID>\d{1,4}),\s+zr\s+\d+.*',
r'.*mitgliedsbeitrag\s+id[.:-_](?P<ID>\d{1,4})\s.*',
)
2018-01-11 18:54:42 +01:00
2018-01-11 19:24:13 +01:00
for score, regex in enumerate(regexes, 1):
hit = re.match(regex, reference)
if hit:
return (int(hit.groupdict().get('ID')), score)
2018-01-11 18:54:42 +01:00
2018-01-11 19:24:13 +01:00
return (False, 99)
2018-01-11 18:54:42 +01:00
2018-01-11 19:24:13 +01:00
def get_debitor_by_record_token(self, reference):
reference = "".join(reference.lower().split())
for debitor in self.debitors:
if re.match(debitor['regex'], reference):
return Debitor.objects.get(pk=debitor['pk'])
return None