[IMP] account_bank_statement_import_qif: black, isort, prettier

This commit is contained in:
Víctor Martínez
2022-10-17 09:11:17 +02:00
parent 13a266141e
commit 02d4ee0047
4 changed files with 75 additions and 73 deletions

View File

@@ -5,19 +5,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Import QIF Bank Statements',
'category': 'Accounting',
'version': '11.0.1.0.1',
'author': 'OpenERP SA,'
'Tecnativa,'
'Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/bank-statement-import',
'depends': [
'account_bank_statement_import',
],
'data': [
'wizards/account_bank_statement_import_qif_view.xml',
],
'installable': True,
'license': 'AGPL-3',
"name": "Import QIF Bank Statements",
"category": "Accounting",
"version": "11.0.1.0.1",
"author": "OpenERP SA," "Tecnativa," "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/bank-statement-import",
"depends": ["account_bank_statement_import",],
"data": ["wizards/account_bank_statement_import_qif_view.xml",],
"installable": True,
"license": "AGPL-3",
}

View File

@@ -4,10 +4,11 @@
# Copyright 2016-2017 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.tests.common import TransactionCase
from odoo.modules.module import get_module_resource
import base64
from odoo.modules.module import get_module_resource
from odoo.tests.common import TransactionCase
class TestQifFile(TransactionCase):
"""Tests for import bank statement qif file format
@@ -16,34 +17,32 @@ class TestQifFile(TransactionCase):
def setUp(self):
super(TestQifFile, self).setUp()
self.statement_import_model = self.env['account.bank.statement.import']
self.statement_line_model = self.env['account.bank.statement.line']
self.journal = self.env['account.journal'].create({
'name': 'Test bank journal',
'code': 'TEST',
'type': 'bank',
})
self.partner = self.env['res.partner'].create({
self.statement_import_model = self.env["account.bank.statement.import"]
self.statement_line_model = self.env["account.bank.statement.line"]
self.journal = self.env["account.journal"].create(
{"name": "Test bank journal", "code": "TEST", "type": "bank",}
)
self.partner = self.env["res.partner"].create(
{
# Different case for trying insensitive case search
'name': 'EPIC Technologies',
})
"name": "EPIC Technologies",
}
)
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", "tests", "test_qif.qif",
)
qif_file = base64.b64encode(open(qif_file_path, 'rb').read())
qif_file = base64.b64encode(open(qif_file_path, "rb").read())
wizard = self.statement_import_model.with_context(
journal_id=self.journal.id
).create(
dict(data_file=qif_file)
)
).create(dict(data_file=qif_file))
wizard.import_file()
statement = self.statement_line_model.search(
[('name', '=', 'YOUR LOCAL SUPERMARKET')], limit=1,
[("name", "=", "YOUR LOCAL SUPERMARKET")], limit=1,
)[0].statement_id
self.assertAlmostEqual(statement.balance_end_real, -1896.09, 2)
line = self.statement_line_model.search(
[('name', '=', 'Epic Technologies')], limit=1,
[("name", "=", "Epic Technologies")], limit=1,
)
self.assertEqual(line.partner_id, self.partner)

View File

@@ -6,9 +6,9 @@
import dateutil.parser
from odoo.tools.translate import _
from odoo import api, models
from odoo.exceptions import UserError
from odoo.tools.translate import _
class AccountBankStatementImport(models.TransientModel):
@@ -16,22 +16,21 @@ class AccountBankStatementImport(models.TransientModel):
@api.model
def _check_qif(self, data_file):
return data_file.strip().startswith(b'!Type:')
return data_file.strip().startswith(b"!Type:")
def _parse_file(self, data_file):
if not self._check_qif(data_file):
return super(AccountBankStatementImport, self)._parse_file(
data_file)
return super(AccountBankStatementImport, self)._parse_file(data_file)
try:
file_data = data_file.decode()
if '\r' in file_data:
data_list = file_data.split('\r')
if "\r" in file_data:
data_list = file_data.split("\r")
else:
data_list = file_data.split('\n')
data_list = file_data.split("\n")
header = data_list[0].strip()
header = header.split(":")[1]
except:
raise UserError(_('Could not decipher the QIF file.'))
raise UserError(_("Could not decipher the QIF file."))
transactions = []
vals_line = {}
total = 0
@@ -41,37 +40,44 @@ class AccountBankStatementImport(models.TransientModel):
line = line.strip()
if not line:
continue
if line[0] == 'D': # date of transaction
vals_line['date'] = dateutil.parser.parse(
line[1:], fuzzy=True).date()
elif line[0] == 'T': # Total amount
total += float(line[1:].replace(',', ''))
vals_line['amount'] = float(line[1:].replace(',', ''))
elif line[0] == 'N': # Check number
vals_line['ref'] = line[1:]
elif line[0] == 'P': # Payee
vals_line['name'] = (
'name' in vals_line and
line[1:] + ': ' + vals_line['name'] or line[1:]
if line[0] == "D": # date of transaction
vals_line["date"] = dateutil.parser.parse(
line[1:], fuzzy=True
).date()
elif line[0] == "T": # Total amount
total += float(line[1:].replace(",", ""))
vals_line["amount"] = float(line[1:].replace(",", ""))
elif line[0] == "N": # Check number
vals_line["ref"] = line[1:]
elif line[0] == "P": # Payee
vals_line["name"] = (
"name" in vals_line
and line[1:] + ": " + vals_line["name"]
or line[1:]
)
elif line[0] == 'M': # Memo
vals_line['name'] = ('name' in vals_line and
vals_line['name'] + ': ' + line[1:] or
line[1:])
elif line[0] == '^' and vals_line: # end of item
elif line[0] == "M": # Memo
vals_line["name"] = (
"name" in vals_line
and vals_line["name"] + ": " + line[1:]
or line[1:]
)
elif line[0] == "^" and vals_line: # end of item
transactions.append(vals_line)
vals_line = {}
elif line[0] == '\n':
elif line[0] == "\n":
transactions = []
else:
pass
else:
raise UserError(_('This file is either not a bank statement or is '
'not correctly formed.'))
vals_bank_statement.update({
'balance_end_real': total,
'transactions': transactions
})
raise UserError(
_(
"This file is either not a bank statement or is "
"not correctly formed."
)
)
vals_bank_statement.update(
{"balance_end_real": total, "transactions": transactions}
)
return None, None, [vals_bank_statement]
def _complete_stmts_vals(self, stmt_vals, journal_id, account_number):
@@ -82,12 +88,12 @@ class AccountBankStatementImport(models.TransientModel):
# Since QIF doesn't provide account numbers (normal behaviour is to
# provide 'account_number', which the generic module uses to find
# the partner), we have to find res.partner through the name
partner_obj = self.env['res.partner']
partner_obj = self.env["res.partner"]
for statement in res:
for line_vals in statement['transactions']:
if not line_vals.get('partner_id') and line_vals.get('name'):
for line_vals in statement["transactions"]:
if not line_vals.get("partner_id") and line_vals.get("name"):
partner = partner_obj.search(
[('name', 'ilike', line_vals['name'])], limit=1,
[("name", "ilike", line_vals["name"])], limit=1,
)
line_vals['partner_id'] = partner.id
line_vals["partner_id"] = partner.id
return res

View File

@@ -3,7 +3,10 @@
<record id="account_bank_statement_import_view" model="ir.ui.view">
<field name="model">account.bank.statement.import</field>
<field name="inherit_id" ref="account_bank_statement_import.account_bank_statement_import_view"/>
<field
name="inherit_id"
ref="account_bank_statement_import.account_bank_statement_import_view"
/>
<field name="arch" type="xml">
<xpath expr="//ul[@id='statement_format']" position="inside">
<li>Quicken Interchange Format (.qif)</li>