From 7ecd065841fc466287dac4a42dfca96282475f1e Mon Sep 17 00:00:00 2001 From: Alexandre Pollet Date: Mon, 3 Dec 2018 14:55:12 +0100 Subject: [PATCH] [IMP] account_bank_statement_import_qif: Let user choose date format of .qif file import (#185) --- account_bank_statement_import_qif/README.rst | 2 + account_bank_statement_import_qif/__init__.py | 1 + .../__manifest__.py | 3 +- .../models/__init__.py | 1 + .../models/account_journal.py | 11 +++ .../{tests => test_files}/test_qif.qif | 0 .../test_files/test_qif_dmy.qif | 21 ++++++ .../test_files/test_qif_ymd.qif | 21 ++++++ .../tests/test_import_bank_statement.py | 71 ++++++++++++++++++- .../views/account_journal_view.xml | 12 ++++ .../account_bank_statement_import_qif.py | 21 +++++- 11 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 account_bank_statement_import_qif/models/__init__.py create mode 100644 account_bank_statement_import_qif/models/account_journal.py rename account_bank_statement_import_qif/{tests => test_files}/test_qif.qif (100%) create mode 100644 account_bank_statement_import_qif/test_files/test_qif_dmy.qif create mode 100644 account_bank_statement_import_qif/test_files/test_qif_ymd.qif create mode 100644 account_bank_statement_import_qif/views/account_journal_view.xml diff --git a/account_bank_statement_import_qif/README.rst b/account_bank_statement_import_qif/README.rst index 5707836..5ef4bd0 100644 --- a/account_bank_statement_import_qif/README.rst +++ b/account_bank_statement_import_qif/README.rst @@ -26,6 +26,8 @@ Usage To use this module, you need to: +#. Go to the *Account Journal* configuration. +#. Select the right date format for qif file import. #. Go to *Accounting* dashboard. #. Click on *Import statement* from any of the bank journals. #. Select a QIF file. diff --git a/account_bank_statement_import_qif/__init__.py b/account_bank_statement_import_qif/__init__.py index 0796ed1..84830dc 100644 --- a/account_bank_statement_import_qif/__init__.py +++ b/account_bank_statement_import_qif/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from . import models from . import wizards diff --git a/account_bank_statement_import_qif/__manifest__.py b/account_bank_statement_import_qif/__manifest__.py index dc67e56..7722021 100644 --- a/account_bank_statement_import_qif/__manifest__.py +++ b/account_bank_statement_import_qif/__manifest__.py @@ -8,7 +8,7 @@ { 'name': 'Import QIF Bank Statements', 'category': 'Accounting', - 'version': '10.0.1.0.1', + 'version': '10.0.1.0.2', 'author': 'OpenERP SA,' 'Tecnativa,' 'Odoo Community Association (OCA)', @@ -17,6 +17,7 @@ 'account_bank_statement_import', ], 'data': [ + 'views/account_journal_view.xml', 'wizards/account_bank_statement_import_qif_view.xml', ], 'installable': True, diff --git a/account_bank_statement_import_qif/models/__init__.py b/account_bank_statement_import_qif/models/__init__.py new file mode 100644 index 0000000..2388e11 --- /dev/null +++ b/account_bank_statement_import_qif/models/__init__.py @@ -0,0 +1 @@ +from . import account_journal diff --git a/account_bank_statement_import_qif/models/account_journal.py b/account_bank_statement_import_qif/models/account_journal.py new file mode 100644 index 0000000..c12b6d9 --- /dev/null +++ b/account_bank_statement_import_qif/models/account_journal.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +from odoo import models, fields + + +class AccountJournal(models.Model): + _inherit = 'account.journal' + + qif_date_format = fields.Selection([ + ('dmy', 'DD/MM/YYYY'), + ('mdy', 'MM/DD/YYYY'), + ('ymd', 'YYYY/MM/DD')], string='QIF Date format', default='dmy') diff --git a/account_bank_statement_import_qif/tests/test_qif.qif b/account_bank_statement_import_qif/test_files/test_qif.qif similarity index 100% rename from account_bank_statement_import_qif/tests/test_qif.qif rename to account_bank_statement_import_qif/test_files/test_qif.qif diff --git a/account_bank_statement_import_qif/test_files/test_qif_dmy.qif b/account_bank_statement_import_qif/test_files/test_qif_dmy.qif new file mode 100644 index 0000000..7509adf --- /dev/null +++ b/account_bank_statement_import_qif/test_files/test_qif_dmy.qif @@ -0,0 +1,21 @@ +!Type:Bank +D12/8/13 +T-1,000.00 +PDelta PC +^ +D15/8/13 +T-75.46 +PWalts Drugs +^ +D3/3/13 +T-379.00 +PEpic Technologies +^ +D4/3/13 +T-20.28 +PYOUR LOCAL SUPERMARKET +^ +D3/3/13 +T-421.35 +PSPRINGFIELD WATER UTILITY +^ diff --git a/account_bank_statement_import_qif/test_files/test_qif_ymd.qif b/account_bank_statement_import_qif/test_files/test_qif_ymd.qif new file mode 100644 index 0000000..eedebe4 --- /dev/null +++ b/account_bank_statement_import_qif/test_files/test_qif_ymd.qif @@ -0,0 +1,21 @@ +!Type:Bank +D13/08/12 +T-1,000.00 +PDelta PC +^ +D13/08/15 +T-75.46 +PWalts Drugs +^ +D13/3/3 +T-379.00 +PEpic Technologies +^ +D13/3/4 +T-20.28 +PYOUR LOCAL SUPERMARKET +^ +D13/3/3 +T-421.35 +PSPRINGFIELD WATER UTILITY +^ diff --git a/account_bank_statement_import_qif/tests/test_import_bank_statement.py b/account_bank_statement_import_qif/tests/test_import_bank_statement.py index 8614602..e6d8cef 100644 --- a/account_bank_statement_import_qif/tests/test_import_bank_statement.py +++ b/account_bank_statement_import_qif/tests/test_import_bank_statement.py @@ -22,6 +22,19 @@ class TestQifFile(TransactionCase): 'name': 'Test bank journal', 'code': 'TEST', 'type': 'bank', + 'qif_date_format': 'mdy' + }) + self.journal_dmy = self.env['account.journal'].create({ + 'name': 'Test bank journal DMY', + 'code': 'TEST_DMY', + 'type': 'bank', + 'qif_date_format': 'dmy' + }) + self.journal_ymd = self.env['account.journal'].create({ + 'name': 'Test bank journal YMD', + 'code': 'TEST_YMD', + 'type': 'bank', + 'qif_date_format': 'ymd' }) self.partner = self.env['res.partner'].create({ # Different case for trying insensitive case search @@ -30,7 +43,9 @@ class TestQifFile(TransactionCase): def test_qif_file_import(self): qif_file_path = get_module_resource( - 'account_bank_statement_import_qif', 'tests', 'test_qif.qif', + 'account_bank_statement_import_qif', + 'test_files', + 'test_qif.qif', ) qif_file = open(qif_file_path, 'rb').read().encode('base64') wizard = self.statement_import_model.with_context( @@ -47,3 +62,57 @@ class TestQifFile(TransactionCase): [('name', '=', 'Epic Technologies')], limit=1, ) self.assertEqual(line.partner_id, self.partner) + + def test_date_format_mdy(self): + qif_file_path = get_module_resource( + 'account_bank_statement_import_qif', + 'test_files', + 'test_qif.qif', + ) + qif_file = open(qif_file_path, 'rb').read().encode('base64') + wizard = self.statement_import_model.with_context( + journal_id=self.journal.id + ).create( + dict(data_file=qif_file) + ) + wizard.import_file() + line = self.statement_line_model.search( + [('name', '=', 'Delta PC')], limit=1, + ) + self.assertEqual(line.date, '2013-08-12') + + def test_date_format_dmy(self): + qif_file_path = get_module_resource( + 'account_bank_statement_import_qif', + 'test_files', + 'test_qif_dmy.qif', + ) + qif_file = open(qif_file_path, 'rb').read().encode('base64') + wizard = self.statement_import_model.with_context( + journal_id=self.journal_dmy.id + ).create( + dict(data_file=qif_file) + ) + wizard.import_file() + line = self.statement_line_model.search( + [('name', '=', 'Delta PC')], limit=1, + ) + self.assertEqual(line.date, '2013-08-12') + + def test_date_format_ymd(self): + qif_file_path = get_module_resource( + 'account_bank_statement_import_qif', + 'test_files', + 'test_qif_ymd.qif', + ) + qif_file = open(qif_file_path, 'rb').read().encode('base64') + wizard = self.statement_import_model.with_context( + journal_id=self.journal_ymd.id + ).create( + dict(data_file=qif_file) + ) + wizard.import_file() + line = self.statement_line_model.search( + [('name', '=', 'Delta PC')], limit=1, + ) + self.assertEqual(line.date, '2013-08-12') diff --git a/account_bank_statement_import_qif/views/account_journal_view.xml b/account_bank_statement_import_qif/views/account_journal_view.xml new file mode 100644 index 0000000..967a569 --- /dev/null +++ b/account_bank_statement_import_qif/views/account_journal_view.xml @@ -0,0 +1,12 @@ + + + + account.journal + + + + + + + + diff --git a/account_bank_statement_import_qif/wizards/account_bank_statement_import_qif.py b/account_bank_statement_import_qif/wizards/account_bank_statement_import_qif.py index 3237096..499995f 100644 --- a/account_bank_statement_import_qif/wizards/account_bank_statement_import_qif.py +++ b/account_bank_statement_import_qif/wizards/account_bank_statement_import_qif.py @@ -35,13 +35,27 @@ class AccountBankStatementImport(models.TransientModel): else: return u'utf-8' - def _parse_qif_date(self, date_str): - return dateutil.parser.parse(date_str, fuzzy=True).date() + def _parse_qif_date(self, date_str, qif_date_format): + date_args = { + 'fuzzy': True + } + if qif_date_format == 'dmy': + date_args.update({'dayfirst': True}) + elif qif_date_format == 'mdy': + date_args.update({'dayfirst': False}) + elif qif_date_format == 'ymd': + date_args.update({'yearfirst': True}) + return dateutil.parser.parse(date_str, **date_args).date() def _parse_file(self, data_file): if not self._check_qif(data_file): return super(AccountBankStatementImport, self)._parse_file( data_file) + qif_date_format = False + journal_id = self.env.context.get('journal_id') + if journal_id: + journal = self.env['account.journal'].browse(journal_id) + qif_date_format = journal.qif_date_format encoding = self._get_qif_encoding(data_file) data_file = data_file.decode(encoding) try: @@ -66,7 +80,8 @@ class AccountBankStatementImport(models.TransientModel): if not line: continue if line[0] == 'D': # date of transaction - vals_line['date'] = self._parse_qif_date(line[1:]) + vals_line['date'] = self._parse_qif_date(line[1:], + qif_date_format) elif line[0] == 'T': # Total amount total += float(line[1:].replace(',', '')) vals_line['amount'] = float(line[1:].replace(',', ''))