party: qif import

This commit is contained in:
Frederik Jaeckel 2022-09-01 17:13:55 +02:00
parent 0287452fe8
commit 50cbb2cc37
5 changed files with 66 additions and 0 deletions

View file

@ -14,6 +14,10 @@ msgctxt "model:ir.message,text:msg_wiz_transactions_found"
msgid "The following transactionen are now imported:\nBalance: %(balance)s\nNumber of transactions: %(quantity)s"
msgstr "Die folgenden Transaktionen werden nun importiert:\nSaldo: %(balance)s\nAnzahl Transactionen: %(quantity)s"
msgctxt "model:ir.message,text:msg_wiz_parties_found"
msgid "The following %(numparties)s parties are now imported:"
msgstr "Die folgenden %(numparties)s Parteien werden nun importiert:"
msgctxt "model:ir.message,name:msg_wiz_no_categories"
msgid "No categories were found in the file."
msgstr "In der Datei wurden keine Kategorien gefunden."

View file

@ -14,6 +14,9 @@ full copyright notices and license terms. -->
<record model="ir.message" id="msg_wiz_no_bank">
<field name="text">No transactions were found in the file.</field>
</record>
<record model="ir.message" id="msg_wiz_parties_found">
<field name="text">The following %(numparties)s parties are now imported:</field>
</record>
<record model="ir.message" id="msg_wiz_transactions_found">
<field name="text">The following transactionen are now imported:\nBalance: %(balance)s\nNumber of transactions: %(quantity)s</field>
</record>

View file

@ -148,6 +148,21 @@ class ImportQifWizard(Wizard):
self.showinfo.allowimport = True
else :
self.showinfo.info = gettext('cashbook_dataexchange.msg_wiz_no_categories')
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(
QifTool.qif_read_transactions(qif_content['Bank'])
)
self.showinfo.info = gettext(
'cashbook_dataexchange.msg_wiz_parties_found',
numparties = len(to_create),
) + '\n\n' + '\n'.join([x['name'] for x in to_create])
if len(to_create) > 0:
self.showinfo.allowimport = True
else :
self.showinfo.info = gettext('cashbook_dataexchange.msg_wiz_no_bank')
elif model == 'cashbook.book':
# read file content, extract categories
qif_content = QifTool.split_by_type(file_content)
@ -188,6 +203,8 @@ class ImportQifWizard(Wizard):
pool = Pool()
Category = pool.get('cashbook.category')
Book = pool.get('cashbook.book')
Party = pool.get('party.party')
QifTool = pool.get('cashbook_dataexchange.qiftool')
model = Transaction().context.get('active_model', '')
file_content = None
@ -200,6 +217,13 @@ class ImportQifWizard(Wizard):
elif model == 'cashbook.book':
if file_content:
Book.create_from_qif(self.showinfo.book, file_content)
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)
return 'end'
# end ImportQifWizard

View file

@ -36,5 +36,12 @@ full copyright notices and license terms. -->
<field name="action" ref="act_import_qif_wizard"/>
</record>
<!-- import parties -->
<record model="ir.action.keyword" id="act_import_qif_wizard-party-keyword">
<field name="keyword">form_action</field>
<field name="model">party.party,-1</field>
<field name="action" ref="act_import_qif_wizard"/>
</record>
</data>
</tryton>

View file

@ -149,6 +149,34 @@ class QifTool(Model):
cat_id = categories[0].id
return (cat_id, msg_txt)
@classmethod
def convert_parties_to_create(cls, transactions):
""" extract party from transaction, check if exist,
create 'to_create'
"""
Party = Pool().get('party.party')
to_create = []
party_cache = []
for transaction in transactions:
if 'party' in transaction.keys():
if transaction['party'] in party_cache:
continue
party_cache.append(transaction['party'])
if Party.search_count([
('rec_name', 'ilike', '%%%(pname)s%%' % {
'pname': transaction['party'],
})
]) == 0:
to_create.append({
'name': transaction['party'],
'addresses': [('create', [{
'street': transaction.get('address', None),
}])],
})
return to_create
@classmethod
def convert_transactions_to_create(cls, transactions, split2edit=True):
""" convert read transactions to create-command