Fix datetime parsing
This commit is contained in:
parent
49efd5700a
commit
5db2b81042
@ -1,8 +1,9 @@
|
|||||||
import json
|
import json
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta, time
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
|
import pytz
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.utils.dateparse import parse_date
|
from django.utils.dateparse import parse_date
|
||||||
@ -10,6 +11,13 @@ from django.utils.dateparse import parse_date
|
|||||||
from byro.bookkeeping.models import TransactionChannel, RealTransaction, VirtualTransaction, Account, AccountCategory
|
from byro.bookkeeping.models import TransactionChannel, RealTransaction, VirtualTransaction, Account, AccountCategory
|
||||||
from byro.members.models import Member, Membership
|
from byro.members.models import Member, Membership
|
||||||
|
|
||||||
|
TIMEZONE = pytz.timezone('Europe/Berlin')
|
||||||
|
|
||||||
|
|
||||||
|
def localize(date):
|
||||||
|
if date:
|
||||||
|
return TIMEZONE.localize(datetime.combine(date, time.min))
|
||||||
|
|
||||||
|
|
||||||
def _import_sepa(member_data, member):
|
def _import_sepa(member_data, member):
|
||||||
sepa_keys = [
|
sepa_keys = [
|
||||||
@ -46,7 +54,7 @@ def _import_real_transactions(real_transactions):
|
|||||||
for real_transaction in real_transactions:
|
for real_transaction in real_transactions:
|
||||||
transactions.append(RealTransaction(
|
transactions.append(RealTransaction(
|
||||||
channel=TransactionChannel.BANK,
|
channel=TransactionChannel.BANK,
|
||||||
value_datetime=parse_date(real_transaction['booking_date']),
|
value_datetime=localize(parse_date(real_transaction['booking_date'] or real_transaction['due_date'])),
|
||||||
amount=real_transaction['amount'],
|
amount=real_transaction['amount'],
|
||||||
purpose=real_transaction['reference'],
|
purpose=real_transaction['reference'],
|
||||||
originator=real_transaction.get('transaction_owner') or 'imported',
|
originator=real_transaction.get('transaction_owner') or 'imported',
|
||||||
@ -71,7 +79,7 @@ def _import_fee_claims(member, virtual_transactions):
|
|||||||
destination_account=liability_account,
|
destination_account=liability_account,
|
||||||
member=member,
|
member=member,
|
||||||
amount=abs(Decimal(claim['amount'])),
|
amount=abs(Decimal(claim['amount'])),
|
||||||
value_datetime=claim['due_date'],
|
value_datetime=localize(parse_date(claim['due_date'])),
|
||||||
))
|
))
|
||||||
|
|
||||||
VirtualTransaction.objects.bulk_create(transactions)
|
VirtualTransaction.objects.bulk_create(transactions)
|
||||||
@ -84,22 +92,26 @@ def _import_inflows(member, virtual_transactions, real_transactions):
|
|||||||
|
|
||||||
for inflow in inflows:
|
for inflow in inflows:
|
||||||
account = fee_account if inflow['transaction_type'] == 'membership fee' else donation_account
|
account = fee_account if inflow['transaction_type'] == 'membership fee' else donation_account
|
||||||
try:
|
possible_real_transaction = real_transactions.filter(
|
||||||
real_transaction = real_transactions.get(
|
virtual_transactions__isnull=True,
|
||||||
virtual_transactions__isnull=True,
|
amount=abs(Decimal(inflow['amount'])),
|
||||||
amount=abs(Decimal(inflow['amount'])),
|
value_datetime=localize(parse_date(inflow['due_date'])),
|
||||||
value_datetime=inflow['due_date'],
|
purpose=inflow['payment_reference'],
|
||||||
purpose=inflow['payment_reference'],
|
)
|
||||||
)
|
if not possible_real_transaction.exists():
|
||||||
except RealTransaction.DoesNotExist:
|
|
||||||
real_transaction = None
|
real_transaction = None
|
||||||
|
elif possible_real_transaction.count() == 1:
|
||||||
|
real_transaction = possible_real_transaction.first()
|
||||||
|
else:
|
||||||
|
real_transaction = possible_real_transaction.first()
|
||||||
|
print(f'Found more than one transactions matching our query: {possible_real_transaction.values_list("pk", flat=True)}')
|
||||||
|
|
||||||
VirtualTransaction.objects.create(
|
VirtualTransaction.objects.create(
|
||||||
destination_account=account,
|
destination_account=account,
|
||||||
source_account=liability_account,
|
source_account=liability_account,
|
||||||
member=member,
|
member=member,
|
||||||
amount=abs(Decimal(inflow['amount'])),
|
amount=abs(Decimal(inflow['amount'])),
|
||||||
value_datetime=inflow['due_date'],
|
value_datetime=localize(parse_date(inflow['due_date'])),
|
||||||
real_transaction=real_transaction,
|
real_transaction=real_transaction,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user