mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
[IMP] account_bank_statement_import_qif: Let user choose date format of .qif file import (#185)
This commit is contained in:
committed by
Pedro M. Baeza
parent
aaa04bc95d
commit
7ecd065841
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
1
account_bank_statement_import_qif/models/__init__.py
Normal file
1
account_bank_statement_import_qif/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import account_journal
|
||||
11
account_bank_statement_import_qif/models/account_journal.py
Normal file
11
account_bank_statement_import_qif/models/account_journal.py
Normal file
@@ -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')
|
||||
@@ -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
|
||||
^
|
||||
@@ -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
|
||||
^
|
||||
@@ -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')
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<odoo>
|
||||
<record id="view_account_journal_form" model="ir.ui.view">
|
||||
<field name="model">account.journal</field>
|
||||
<field name="inherit_id" ref="account.view_account_journal_form" />
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//field[@name='bank_statements_source']" position="after">
|
||||
<field name="qif_date_format" attrs="{'invisible': [('bank_statements_source', '=', 'manual')], 'required': [('bank_statements_source', '=', 'file_import')]}"/>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</odoo>
|
||||
@@ -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(',', ''))
|
||||
|
||||
Reference in New Issue
Block a user