cashbook_dataexchange/qif_import_wiz.py

273 lines
10 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
# This file is part of the cashbook-module from m-ds for Tryton.
# The COPYRIGHT file at the top level of this repository contains the
# full copyright notices and license terms.
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.model import ModelView, fields
from trytond.wizard import Wizard, StateTransition, StateView, Button
2022-08-29 09:05:59 +00:00
from trytond.i18n import gettext
2022-09-01 12:48:04 +00:00
from trytond.pyson import Eval, Bool
from trytond.report import Report
class ImportQifWizardStart(ModelView):
'Import QIF-File'
__name__ = 'cashbook_dataexchange.qif_imp_wiz.start'
2023-06-06 13:22:57 +00:00
company = fields.Many2One(
model_name='company.company',
string="Company", required=True,
states={'invisible': True})
2023-06-06 13:22:57 +00:00
book = fields.Many2One(
string='Cashbook', readonly=True,
2022-09-01 12:48:04 +00:00
model_name='cashbook.book',
states={
2023-12-01 11:39:05 +00:00
'invisible': ~Bool(Eval('book'))})
2023-06-06 13:22:57 +00:00
file_ = fields.Binary(
string="QIF-File", required=True,
help='Quicken Interchange Format')
@classmethod
def default_company(cls):
return Transaction().context.get('company')
# end ImportQifWizardStart
2022-08-29 09:05:59 +00:00
class ImportQifWizardInfo(ModelView):
'Import QIF-File'
__name__ = 'cashbook_dataexchange.qif_imp_wiz.info'
2023-06-06 13:22:57 +00:00
company = fields.Many2One(
model_name='company.company', string="Company",
required=True, states={'invisible': True})
book = fields.Many2One(
string='Cash Book', readonly=True, model_name='cashbook.book',
2022-09-01 12:48:04 +00:00
states={
2023-12-01 11:39:05 +00:00
'invisible': ~Bool(Eval('book'))})
2023-06-06 13:22:57 +00:00
allowimport = fields.Boolean(
string='Import Enabled',
2022-09-01 12:48:04 +00:00
states={'invisible': True})
2022-08-29 09:05:59 +00:00
info = fields.Text(string='Information', readonly=True)
# end ImportQifWizardInfo
class ImportQifWizard(Wizard):
'Import QIF-File'
__name__ = 'cashbook_dataexchange.qif_imp_wiz'
start_state = 'start'
2023-06-06 13:22:57 +00:00
start = StateView(
model_name='cashbook_dataexchange.qif_imp_wiz.start',
view='cashbook_dataexchange.qif_imp_wiz_start_form',
buttons=[
Button(string='Cancel', state='end', icon='tryton-cancel'),
2023-06-06 13:22:57 +00:00
Button(
string='Read File', state='readf',
2023-12-01 11:39:05 +00:00
icon='tryton-forward', default=True)])
2023-06-06 13:22:57 +00:00
showinfo = StateView(
model_name='cashbook_dataexchange.qif_imp_wiz.info',
view='cashbook_dataexchange.qif_imp_wiz_info_form',
2022-08-29 09:05:59 +00:00
buttons=[
Button(string='Cancel', state='end', icon='tryton-cancel'),
2023-06-06 13:22:57 +00:00
Button(
string='Import Data', state='importf',
icon='tryton-import', default=True,
2023-12-01 11:39:05 +00:00
states={'readonly': ~Eval('allowimport', False)})])
2022-08-29 09:05:59 +00:00
readf = StateTransition()
importf = StateTransition()
2022-09-01 12:48:04 +00:00
def default_start(self, fields):
""" show book, company
"""
context = Transaction().context
values = {
'company': Transaction().context.get('company'),
2023-12-01 11:39:05 +00:00
'book': None}
2022-09-01 12:48:04 +00:00
model = context.get('active_model', '')
if model == 'cashbook.book':
values['book'] = context.get('active_id', None)
return values
2022-08-29 09:05:59 +00:00
def default_showinfo(self, fields):
""" show import-info
"""
values = {
'company': self.start.company.id,
'info': getattr(self.showinfo, 'info', None),
2022-09-01 12:48:04 +00:00
'book': getattr(getattr(self.start, 'book', None), 'id', None),
'allowimport': getattr(self.showinfo, 'allowimport', False),
2022-08-29 09:05:59 +00:00
}
return values
def transition_readf(self):
""" read file, show number of objects
"""
pool = Pool()
2022-09-01 12:48:04 +00:00
QifTool = pool.get('cashbook_dataexchange.qiftool')
Category = pool.get('cashbook.category')
2022-08-29 09:05:59 +00:00
model = Transaction().context.get('active_model', '')
file_content = None
if isinstance(self.start.file_, bytes):
file_content = self.start.file_.decode('utf8')
2022-09-01 12:48:04 +00:00
self.showinfo.allowimport = False
2022-08-29 09:05:59 +00:00
if model == 'cashbook.category':
def get_catlist(record, cattype, parent_name=None):
2022-08-29 09:05:59 +00:00
""" generate list of categories
"""
names = []
if record['cattype'] != cattype:
return []
if 'parent' in record.keys():
parent_name = Category(record['parent']).rec_name
name_lst = []
if parent_name:
name_lst.append(parent_name)
name_lst.append(record['name'])
current_name = '/'.join(name_lst)
names.append(current_name)
if 'childs' in record.keys():
# record['childs']: [('create', [{}, ...]))]
for x in record['childs'][0][1]:
names.extend(get_catlist(x, cattype, current_name))
2022-08-29 09:05:59 +00:00
return names
# read file content, extract categories
2022-09-01 12:48:04 +00:00
qif_content = QifTool.split_by_type(file_content)
2022-08-29 09:05:59 +00:00
if 'Cat' in qif_content.keys():
2023-06-06 13:22:57 +00:00
to_create = QifTool.convert_categories_to_create(
QifTool.qif_read_categories(qif_content['Cat']))
in_categories = []
out_categories = []
for x in to_create:
in_categories.extend(get_catlist(x, 'in'))
out_categories.extend(get_catlist(x, 'out'))
2022-08-29 09:05:59 +00:00
self.showinfo.info = gettext(
'cashbook_dataexchange.msg_wiz_categories_found',
2023-06-06 13:22:57 +00:00
categories='\n'.join(
2022-08-30 20:49:52 +00:00
[''] +
2023-06-06 13:22:57 +00:00
['%s (in)' % x for x in in_categories] +
2022-08-30 20:49:52 +00:00
[''] +
['%s (out)' % x for x in out_categories]
2023-12-01 11:39:05 +00:00
))
if len(to_create) > 0:
self.showinfo.allowimport = True
2023-06-06 13:22:57 +00:00
else:
self.showinfo.info = gettext(
'cashbook_dataexchange.msg_wiz_no_categories')
2022-09-01 15:13:55 +00:00
elif model == 'party.party':
# read file content, extract parties
qif_content = QifTool.split_by_type(file_content)
if 'Bank' in qif_content.keys():
to_create = QifTool.convert_parties_to_create(
2023-12-01 11:39:05 +00:00
QifTool.qif_read_transactions(qif_content['Bank']))
2022-09-01 15:13:55 +00:00
self.showinfo.info = gettext(
'cashbook_dataexchange.msg_wiz_parties_found',
2023-06-06 13:22:57 +00:00
numparties=len(to_create),
2022-09-01 15:13:55 +00:00
) + '\n\n' + '\n'.join([x['name'] for x in to_create])
if len(to_create) > 0:
self.showinfo.allowimport = True
2023-06-06 13:22:57 +00:00
else:
self.showinfo.info = gettext(
'cashbook_dataexchange.msg_wiz_no_bank')
2022-09-01 12:48:04 +00:00
elif model == 'cashbook.book':
info_lst = []
2022-09-01 12:48:04 +00:00
# read file content, extract categories
qif_content = QifTool.split_by_type(file_content)
if 'Bank' in qif_content.keys():
2023-06-06 13:22:57 +00:00
(to_create, msg_list, fail_cnt) = \
QifTool.convert_transactions_to_create(
self.start.book,
2023-06-06 13:22:57 +00:00
QifTool.qif_read_transactions(qif_content['Bank']))
2022-09-01 12:48:04 +00:00
if len(msg_list) > 0:
2023-06-06 13:22:57 +00:00
info_lst.append(gettext(
'cashbook_dataexchange.msg_wiz_transactions_error'))
info_lst.append('')
2022-09-01 12:48:04 +00:00
short_lst = []
for x in msg_list:
if x not in short_lst:
short_lst.append(x)
info_lst.extend(short_lst)
info_lst.append('')
2022-09-01 12:48:04 +00:00
# count
if fail_cnt == 0:
2023-06-06 13:22:57 +00:00
debit = sum([
x['amount'] for x in to_create
if x['bookingtype'] in ['out', 'mvout', 'spout']])
credit = sum([
x['amount'] for x in to_create
if x['bookingtype'] in ['in', 'mvin', 'spin']])
2022-09-03 18:36:16 +00:00
balance = credit - debit
if len(msg_list) > 0:
msg_list.append('')
info_lst.append(gettext(
2022-09-01 12:48:04 +00:00
'cashbook_dataexchange.msg_wiz_transactions_found',
2023-06-06 13:22:57 +00:00
quantity=len(to_create),
balance=Report.format_currency(
balance, None, self.start.book.currency),
credit=Report.format_currency(
credit, None, self.start.book.currency),
debit=Report.format_currency(
debit, None, self.start.book.currency)))
2022-09-01 12:48:04 +00:00
self.showinfo.allowimport = True
2023-06-06 13:22:57 +00:00
else:
info_lst.append(gettext(
'cashbook_dataexchange.msg_wiz_no_bank'))
self.showinfo.info = '\n'.join(info_lst)
2022-08-29 09:05:59 +00:00
return 'showinfo'
def transition_importf(self):
""" read file, show number of objects
"""
pool = Pool()
Category = pool.get('cashbook.category')
2022-09-01 12:48:04 +00:00
Book = pool.get('cashbook.book')
2022-09-13 09:29:43 +00:00
Line = pool.get('cashbook.line')
2022-09-01 15:13:55 +00:00
Party = pool.get('party.party')
QifTool = pool.get('cashbook_dataexchange.qiftool')
2022-08-29 09:05:59 +00:00
model = Transaction().context.get('active_model', '')
file_content = None
if isinstance(self.start.file_, bytes):
file_content = self.start.file_.decode('utf8')
if model == 'cashbook.category':
if file_content:
2023-06-06 13:22:57 +00:00
Category.create_from_qif(file_content)
2022-09-01 12:48:04 +00:00
elif model == 'cashbook.book':
if file_content:
Book.create_from_qif(self.showinfo.book, file_content)
2022-09-13 09:29:43 +00:00
lines = Line.search([
('cashbook.id', '=', self.showinfo.book.id),
2023-12-01 11:39:05 +00:00
('state', '=', 'edit')])
2022-09-13 09:29:43 +00:00
if len(lines) > 0:
Line.wfcheck(lines)
2022-09-01 15:13:55 +00:00
elif model == 'party.party':
qif_content = QifTool.split_by_type(file_content)
if 'Bank' in qif_content.keys():
to_create = QifTool.convert_parties_to_create(
QifTool.qif_read_transactions(qif_content['Bank'])
)
Party.create(to_create)
2022-08-29 09:05:59 +00:00
return 'end'
# end ImportQifWizard