splitbuchung: konvertiert transfers + test
This commit is contained in:
parent
3796c7b7bc
commit
c5b3277622
3 changed files with 101 additions and 70 deletions
157
qiftool.py
157
qiftool.py
|
@ -105,10 +105,15 @@ class QifTool(Model):
|
||||||
booking['account'] = line_txt[1:-1]
|
booking['account'] = line_txt[1:-1]
|
||||||
else :
|
else :
|
||||||
booking['category'] = line_txt
|
booking['category'] = line_txt
|
||||||
elif line.startswith('S'): # split: category
|
elif line.startswith('S'): # split: category, account
|
||||||
booking['split'].append({
|
if line_txt.startswith('[') and line_txt.endswith(']'):
|
||||||
'category': line_txt,
|
booking['split'].append({
|
||||||
})
|
'account': line_txt[1:-1],
|
||||||
|
})
|
||||||
|
else :
|
||||||
|
booking['split'].append({
|
||||||
|
'category': line_txt,
|
||||||
|
})
|
||||||
elif line.startswith('E'): # split: memo
|
elif line.startswith('E'): # split: memo
|
||||||
booking['split'][-1]['description'] = line_txt
|
booking['split'][-1]['description'] = line_txt
|
||||||
elif line.startswith('$'): # split: amount
|
elif line.startswith('$'): # split: amount
|
||||||
|
@ -367,6 +372,27 @@ class QifTool(Model):
|
||||||
else :
|
else :
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_category_account(cls, book, transaction):
|
||||||
|
""" get category or account
|
||||||
|
"""
|
||||||
|
cat_name = transaction.get('category', None)
|
||||||
|
account_name = transaction.get('account', None)
|
||||||
|
msg_list = []
|
||||||
|
fail_cnt = 0
|
||||||
|
category = account = None
|
||||||
|
if cat_name is not None:
|
||||||
|
(category, msg_txt) = cls.get_category_by_name(book.company, cat_name)
|
||||||
|
if category is None:
|
||||||
|
msg_list.append(msg_txt)
|
||||||
|
fail_cnt += 1
|
||||||
|
elif account_name is not None:
|
||||||
|
(account, msg_txt) = cls.get_account_by_name(book, account_name)
|
||||||
|
if account is None:
|
||||||
|
msg_list.append(msg_txt)
|
||||||
|
fail_cnt += 1
|
||||||
|
return (category, account, msg_list, fail_cnt)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def convert_transactions_to_create(cls, book, transactions, split2edit=True):
|
def convert_transactions_to_create(cls, book, transactions, split2edit=True):
|
||||||
""" convert read transactions to create-command
|
""" convert read transactions to create-command
|
||||||
|
@ -390,57 +416,45 @@ class QifTool(Model):
|
||||||
if 'description' in line.keys():
|
if 'description' in line.keys():
|
||||||
line['description'] = updt_description(line['description'])
|
line['description'] = updt_description(line['description'])
|
||||||
|
|
||||||
cat_name = transaction.get('category', None)
|
(category, account, msg_lst2, fail_cnt2) = cls.get_category_account(book, transaction)
|
||||||
account_name = transaction.get('account', None)
|
msg_list.extend(msg_lst2)
|
||||||
if cat_name is not None:
|
if fail_cnt2 > 0:
|
||||||
(category, msg_txt) = cls.get_category_by_name(book.company, cat_name)
|
fail_cnt += fail_cnt2
|
||||||
if category is None:
|
continue
|
||||||
msg_list.append(msg_txt)
|
|
||||||
fail_cnt += 1
|
if category:
|
||||||
continue
|
cat_type = category.cattype
|
||||||
|
line['category'] = category.id
|
||||||
|
|
||||||
|
if cat_type == 'out':
|
||||||
|
# amount of usually out-transaction are negative in QIF,
|
||||||
|
# if its a return-transaction it should be positive
|
||||||
|
line['amount'] = line['amount'].copy_negate()
|
||||||
|
|
||||||
|
line['bookingtype'] = cat_type
|
||||||
|
if len(transaction['split']) > 0:
|
||||||
|
line['bookingtype'] = {
|
||||||
|
'in': 'spin',
|
||||||
|
'out': 'spout',
|
||||||
|
}[cat_type]
|
||||||
|
elif account:
|
||||||
|
if line['amount'] < Decimal('0.0'):
|
||||||
|
line['bookingtype'] = 'mvout'
|
||||||
|
line['amount'] = line['amount'].copy_negate()
|
||||||
else :
|
else :
|
||||||
cat_type = category.cattype
|
line['bookingtype'] = 'mvin'
|
||||||
line['category'] = category.id
|
|
||||||
if msg_txt is not None:
|
|
||||||
msg_list.append(msg_txt)
|
|
||||||
|
|
||||||
if cat_type == 'out':
|
line['booktransf'] = account.id
|
||||||
# amount of usually out-transaction are negative in QIF,
|
descr_lst = [transaction.get('party', '-')]
|
||||||
# if its a return-transaction it should be positive
|
if 'description' in line.keys():
|
||||||
line['amount'] = line['amount'].copy_negate()
|
descr_lst.append(line['description'])
|
||||||
|
if 'party' in transaction.keys():
|
||||||
line['bookingtype'] = cat_type
|
del transaction['party']
|
||||||
if len(transaction['split']) > 0:
|
line['description'] = '; '.join(descr_lst)
|
||||||
line['bookingtype'] = {
|
line['state'] = 'edit'
|
||||||
'in': 'spin',
|
if cls.check_counter_transaction(book, line) == True:
|
||||||
'out': 'spout',
|
# counter-transaction already exists
|
||||||
}[cat_type]
|
|
||||||
elif account_name is not None:
|
|
||||||
(account, msg_txt) = cls.get_account_by_name(book, account_name)
|
|
||||||
if account is None:
|
|
||||||
msg_list.append(msg_txt)
|
|
||||||
fail_cnt += 1
|
|
||||||
continue
|
continue
|
||||||
else :
|
|
||||||
if msg_txt is not None:
|
|
||||||
msg_list.append(msg_txt)
|
|
||||||
if line['amount'] < Decimal('0.0'):
|
|
||||||
line['bookingtype'] = 'mvout'
|
|
||||||
line['amount'] = line['amount'].copy_negate()
|
|
||||||
else :
|
|
||||||
line['bookingtype'] = 'mvin'
|
|
||||||
|
|
||||||
line['booktransf'] = account.id
|
|
||||||
descr_lst = [transaction.get('party', '-')]
|
|
||||||
if 'description' in line.keys():
|
|
||||||
descr_lst.append(line['description'])
|
|
||||||
if 'party' in transaction.keys():
|
|
||||||
del transaction['party']
|
|
||||||
line['description'] = '; '.join(descr_lst)
|
|
||||||
line['state'] = 'edit'
|
|
||||||
if cls.check_counter_transaction(book, line) == True:
|
|
||||||
# counter-transaction already exists
|
|
||||||
continue
|
|
||||||
else :
|
else :
|
||||||
# transaction: no category, no account - ignore?
|
# transaction: no category, no account - ignore?
|
||||||
if line.get('amount', Decimal('0.0')) == Decimal('0.0'):
|
if line.get('amount', Decimal('0.0')) == Decimal('0.0'):
|
||||||
|
@ -482,31 +496,40 @@ class QifTool(Model):
|
||||||
|
|
||||||
split_lines = []
|
split_lines = []
|
||||||
for sp_line in transaction['split']:
|
for sp_line in transaction['split']:
|
||||||
(cat_obj, msg_txt) = cls.get_category_by_name(book.company, sp_line['category'])
|
(category, account, msg_lst2, fail_cnt2) = cls.get_category_account(book, sp_line)
|
||||||
|
msg_list.extend(msg_lst2)
|
||||||
|
if fail_cnt2 > 0:
|
||||||
|
fail_cnt += fail_cnt2
|
||||||
|
continue
|
||||||
|
|
||||||
if msg_txt is not None:
|
split_line = {
|
||||||
msg_list.append(msg_txt)
|
'amount': sp_line['amount'] \
|
||||||
|
if line['bookingtype'].endswith('in') else sp_line['amount'].copy_negate(),
|
||||||
|
'description': updt_description(sp_line.get('description', None)),
|
||||||
|
}
|
||||||
|
|
||||||
if cat_obj is not None:
|
if category:
|
||||||
# category match to bookingtype?
|
# category match to bookingtype?
|
||||||
if ((cat_obj.cattype == 'in') and line['bookingtype'].endswith('out')) or\
|
if ((category.cattype == 'in') and line['bookingtype'].endswith('out')) or\
|
||||||
((cat_obj.cattype == 'out') and line['bookingtype'].endswith('in')):
|
((category.cattype == 'out') and line['bookingtype'].endswith('in')):
|
||||||
msg_list.append(gettext(
|
msg_list.append(gettext(
|
||||||
'cashbook_dataexchange.mds_import_category_not_match',
|
'cashbook_dataexchange.mds_import_category_not_match',
|
||||||
catname = '%s [%s]' % (cat_obj.rec_name, cat_obj.cattype),
|
catname = '%s [%s]' % (category.rec_name, category.cattype),
|
||||||
bktype = line['bookingtype'],
|
bktype = line['bookingtype'],
|
||||||
data = str(transaction),
|
data = str(transaction),
|
||||||
))
|
))
|
||||||
fail_cnt += 1
|
fail_cnt += 1
|
||||||
|
continue
|
||||||
split_lines.append({
|
split_line['splittype'] = 'cat'
|
||||||
'amount': sp_line['amount'] \
|
split_line['category'] = category.id
|
||||||
if line['bookingtype'] in ['in', 'spin'] else sp_line['amount'].copy_negate(),
|
elif account:
|
||||||
'description': updt_description(sp_line.get('description', None)),
|
split_line['splittype'] = 'tr'
|
||||||
'category': cat_obj.id,
|
split_line['booktransf'] = account.id
|
||||||
})
|
|
||||||
else :
|
else :
|
||||||
fail_cnt += 1
|
continue
|
||||||
|
|
||||||
|
split_lines.append(split_line)
|
||||||
|
|
||||||
if len(split_lines) > 0:
|
if len(split_lines) > 0:
|
||||||
line['splitlines'] = [('create', split_lines)]
|
line['splitlines'] = [('create', split_lines)]
|
||||||
|
|
||||||
|
|
|
@ -323,7 +323,8 @@ I
|
||||||
'PFoodshop Zehlendorf\nLLebensmittel\n^\nD22.10.2020\n'+
|
'PFoodshop Zehlendorf\nLLebensmittel\n^\nD22.10.2020\n'+
|
||||||
'CX\nMLebensmittel\nT-55,84\nPreal,- Teltow\nLLebensmittel\n'+
|
'CX\nMLebensmittel\nT-55,84\nPreal,- Teltow\nLLebensmittel\n'+
|
||||||
'SLebensmittel\nELebensmittel\n$-49,36\nSKosmetik\nEKlopapier\n'+
|
'SLebensmittel\nELebensmittel\n$-49,36\nSKosmetik\nEKlopapier\n'+
|
||||||
'$-2,99\nSHaushaltschemie\nESagrotan\n$-3,49\n^\n')
|
'$-2,99\nSHaushaltschemie\nESagrotan\n$-3,49\n'+
|
||||||
|
'S[S-Giro]\nEtransfer out\n$-3,49\n^\n')
|
||||||
|
|
||||||
(to_create, msg_txt, fail_cnt) = QifTool.convert_transactions_to_create(books[0], tr_list)
|
(to_create, msg_txt, fail_cnt) = QifTool.convert_transactions_to_create(books[0], tr_list)
|
||||||
self.assertEqual(msg_txt, [])
|
self.assertEqual(msg_txt, [])
|
||||||
|
@ -360,17 +361,25 @@ I
|
||||||
'party': parties[3].id,
|
'party': parties[3].id,
|
||||||
'splitlines': [
|
'splitlines': [
|
||||||
('create', [{
|
('create', [{
|
||||||
|
'splittype': 'cat',
|
||||||
'amount': Decimal('49.36'),
|
'amount': Decimal('49.36'),
|
||||||
'description': 'Lebensmittel',
|
'description': 'Lebensmittel',
|
||||||
'category': categories[0].id,
|
'category': categories[0].id,
|
||||||
}, {
|
}, {
|
||||||
|
'splittype': 'cat',
|
||||||
'amount': Decimal('2.99'),
|
'amount': Decimal('2.99'),
|
||||||
'description': 'Klopapier',
|
'description': 'Klopapier',
|
||||||
'category': categories[2].id,
|
'category': categories[2].id,
|
||||||
}, {
|
}, {
|
||||||
|
'splittype': 'cat',
|
||||||
'amount': Decimal('3.49'),
|
'amount': Decimal('3.49'),
|
||||||
'description': 'Sagrotan',
|
'description': 'Sagrotan',
|
||||||
'category': categories[1].id,
|
'category': categories[1].id,
|
||||||
|
}, {
|
||||||
|
'splittype': 'tr',
|
||||||
|
'amount': Decimal('3.49'),
|
||||||
|
'description': 'transfer out',
|
||||||
|
'booktransf': books[1].id,
|
||||||
}],
|
}],
|
||||||
)],
|
)],
|
||||||
}])
|
}])
|
||||||
|
@ -380,7 +389,7 @@ I
|
||||||
'lines': [('create', to_create)],
|
'lines': [('create', to_create)],
|
||||||
}])
|
}])
|
||||||
self.assertEqual(len(books[0].lines), 4)
|
self.assertEqual(len(books[0].lines), 4)
|
||||||
self.assertEqual(books[0].balance, Decimal('-134.09'))
|
self.assertEqual(books[0].balance, Decimal('-137.58'))
|
||||||
|
|
||||||
@with_transaction()
|
@with_transaction()
|
||||||
def test_qiftool_read_transactions(self):
|
def test_qiftool_read_transactions(self):
|
||||||
|
|
|
@ -313,7 +313,6 @@ Number of transactions: 2""")
|
||||||
self.assertEqual(books[1].lines[0].rec_name, '12/04/2013|from|7.30 usd|Transfer to book [From Book | -57.55 usd | Open]')
|
self.assertEqual(books[1].lines[0].rec_name, '12/04/2013|from|7.30 usd|Transfer to book [From Book | -57.55 usd | Open]')
|
||||||
self.assertEqual(books[1].lines[0].state, 'check')
|
self.assertEqual(books[1].lines[0].state, 'check')
|
||||||
|
|
||||||
|
|
||||||
# run wizard again - import to 'To Book'
|
# run wizard again - import to 'To Book'
|
||||||
(sess_id, start_state, end_state) = ImportWiz.create()
|
(sess_id, start_state, end_state) = ImportWiz.create()
|
||||||
w_obj = ImportWiz(sess_id)
|
w_obj = ImportWiz(sess_id)
|
||||||
|
|
Loading…
Reference in a new issue