135 lines
4.7 KiB
Python
135 lines
4.7 KiB
Python
# -*- 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
|
|
from trytond.transaction import Transaction
|
|
from trytond.i18n import gettext
|
|
|
|
|
|
class ImportQifWizardStart(ModelView):
|
|
'Import QIF-File'
|
|
__name__ = 'cashbook_dataexchange.qif_imp_wiz.start'
|
|
|
|
company = fields.Many2One(model_name='company.company',
|
|
string="Company", required=True,
|
|
states={'invisible': True})
|
|
file_ = fields.Binary(string="QIF-File", required=True,
|
|
help='Quicken Interchange Format')
|
|
|
|
@classmethod
|
|
def default_company(cls):
|
|
return Transaction().context.get('company')
|
|
|
|
# end ImportQifWizardStart
|
|
|
|
|
|
class ImportQifWizardInfo(ModelView):
|
|
'Import QIF-File'
|
|
__name__ = 'cashbook_dataexchange.qif_imp_wiz.info'
|
|
|
|
company = fields.Many2One(model_name='company.company',
|
|
string="Company", required=True,
|
|
states={'invisible': True})
|
|
info = fields.Text(string='Information', readonly=True)
|
|
|
|
# end ImportQifWizardInfo
|
|
|
|
|
|
class ImportQifWizard(Wizard):
|
|
'Import QIF-File'
|
|
__name__ = 'cashbook_dataexchange.qif_imp_wiz'
|
|
|
|
start_state = 'start'
|
|
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'),
|
|
Button(string='Read File', state='readf', icon='tryton-forward', default=True),
|
|
])
|
|
showinfo = StateView(model_name='cashbook_dataexchange.qif_imp_wiz.info', \
|
|
view='cashbook_dataexchange.qif_imp_wiz_info_form', \
|
|
buttons=[
|
|
Button(string='Cancel', state='end', icon='tryton-cancel'),
|
|
Button(string='Import Categories', state='importf', icon='tryton-import', default=True),
|
|
])
|
|
|
|
readf = StateTransition()
|
|
importf = StateTransition()
|
|
|
|
def default_showinfo(self, fields):
|
|
""" show import-info
|
|
"""
|
|
values = {
|
|
'company': self.start.company.id,
|
|
'info': getattr(self.showinfo, 'info', None),
|
|
}
|
|
return values
|
|
|
|
def transition_readf(self):
|
|
""" read file, show number of objects
|
|
"""
|
|
pool = Pool()
|
|
QitTool = pool.get('cashbook_dataexchange.qiftool')
|
|
|
|
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':
|
|
def get_catlist(catlist, parent_name=None):
|
|
""" generate list of categories
|
|
"""
|
|
names = []
|
|
for name1 in catlist.keys():
|
|
name_lst = []
|
|
if parent_name:
|
|
name_lst.append(parent_name)
|
|
name_lst.append(name1)
|
|
current_name = '/'.join(name_lst)
|
|
names.append(current_name)
|
|
names.extend(get_catlist(catlist[name1]['childs'], current_name))
|
|
return names
|
|
|
|
# read file content, extract categories
|
|
qif_content = QitTool.split_by_type(file_content)
|
|
if 'Cat' in qif_content.keys():
|
|
categories = QitTool.qif_read_categories(qif_content['Cat'])
|
|
self.showinfo.info = gettext(
|
|
'cashbook_dataexchange.msg_wiz_categories_found',
|
|
categories = '\n'.join(
|
|
[''] +
|
|
['%s (in)' % x for x in get_catlist(categories['in'], None)]+
|
|
[''] +
|
|
['%s (out)' % x for x in get_catlist(categories['out'], None)]
|
|
)
|
|
)
|
|
else :
|
|
self.showinfo.info = gettext('cashbook_dataexchange.msg_wiz_no_categories')
|
|
|
|
return 'showinfo'
|
|
|
|
def transition_importf(self):
|
|
""" read file, show number of objects
|
|
"""
|
|
pool = Pool()
|
|
Category = pool.get('cashbook.category')
|
|
|
|
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:
|
|
records = Category.create_from_qif(file_content)
|
|
|
|
return 'end'
|
|
|
|
# end ImportQifWizard
|
|
|