Fix transaction matching

This commit is contained in:
Tobias Kunze 2018-01-13 13:05:49 +01:00
parent 566bf433f5
commit 49eee77d87
4 changed files with 17 additions and 14 deletions

View File

@ -8,8 +8,7 @@ class ShackspacePluginConfig(AppConfig):
name = 'shackspace' name = 'shackspace'
def ready(self): def ready(self):
print('fooooo') from . import utils # noqa
from . import utils
default_app_config = 'byro_shackspace.ShackspacePluginConfig' default_app_config = 'byro_shackspace.ShackspacePluginConfig'

View File

@ -1,4 +1,5 @@
import os.path import os.path
import pytest import pytest
from byro_shackspace.utils import process_bank_csv from byro_shackspace.utils import process_bank_csv
from django.core.files.uploadedfile import InMemoryUploadedFile from django.core.files.uploadedfile import InMemoryUploadedFile

View File

@ -1,7 +1,7 @@
import os.path import os.path
def test_file_encoding(): def test_file_encoding():
filename = os.path.join(os.path.dirname(__file__), 'fixtures/transactions.csv') filename = os.path.join(os.path.dirname(__file__), 'fixtures/transactions.csv')
with open(filename, encoding='iso-8859-1') as fp: with open(filename, encoding='iso-8859-1') as fp:
assert len(fp.read()) == 1086 assert len(fp.read()) == 1086

View File

@ -1,19 +1,22 @@
import csv import csv
import re import re
from datetime import date, datetime, timedelta from datetime import datetime
from decimal import Decimal from decimal import Decimal
from django.db import transaction
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.timezone import now from django.utils.timezone import now
from byro.bookkeeping.models import RealTransaction, TransactionChannel from byro.bookkeeping.models import (
from byro.bookkeeping.signals import derive_virtual_transactions, process_bank_csv Account, AccountCategory, RealTransaction,
TransactionChannel, VirtualTransaction,
)
from byro.bookkeeping.signals import (
derive_virtual_transactions, process_csv_upload,
)
from byro.members.models import Member from byro.members.models import Member
@transaction.atomic @receiver(process_csv_upload)
@receiver(process_bank_csv)
def process_bank_csv(sender, signal, **kwargs): def process_bank_csv(sender, signal, **kwargs):
source = sender source = sender
reader = csv.DictReader(open(source.source_file.name, encoding='iso-8859-1'), delimiter=';', quotechar='"') reader = csv.DictReader(open(source.source_file.name, encoding='iso-8859-1'), delimiter=';', quotechar='"')
@ -38,13 +41,13 @@ def process_bank_csv(sender, signal, **kwargs):
source=source, source=source,
data=line, data=line,
) )
return True
@transaction.atomic
@receiver(derive_virtual_transactions) @receiver(derive_virtual_transactions)
def match_transaction(sender, signal, **kwargs): def match_transaction(sender, signal, **kwargs):
transaction = sender transaction = sender
uid, score = reference_parser(transaction.reference) uid, score = reference_parser(reference=transaction.purpose)
member = None member = None
try: try:
member = Member.objects.get(number=uid) member = Member.objects.get(number=uid)
@ -54,8 +57,8 @@ def match_transaction(sender, signal, **kwargs):
account = Account.objects.get(account_category=AccountCategory.MEMBER_FEES) account = Account.objects.get(account_category=AccountCategory.MEMBER_FEES)
data = { data = {
'amount': transaction.amount, 'amount': transaction.amount,
'destination': account, 'destination_account': account,
'value_datetime': value_datetime, 'value_datetime': transaction.value_datetime,
'member': member, 'member': member,
} }
virtual_transaction = VirtualTransaction.objects.filter(**data).first() virtual_transaction = VirtualTransaction.objects.filter(**data).first()
@ -67,7 +70,7 @@ def match_transaction(sender, signal, **kwargs):
return [virtual_transaction] return [virtual_transaction]
def reference_parser(self, reference): def reference_parser(reference):
reference = reference.lower() reference = reference.lower()
regexes = ( regexes = (