[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). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{ {
'name': 'Import QIF Bank Statements', "name": "Import QIF Bank Statements",
'category': 'Accounting', "category": "Accounting",
'version': '11.0.1.0.1', "version": "11.0.1.0.1",
'author': 'OpenERP SA,' "author": "OpenERP SA," "Tecnativa," "Odoo Community Association (OCA)",
'Tecnativa,' "website": "https://github.com/OCA/bank-statement-import",
'Odoo Community Association (OCA)', "depends": ["account_bank_statement_import",],
'website': 'https://github.com/OCA/bank-statement-import', "data": ["wizards/account_bank_statement_import_qif_view.xml",],
'depends': [ "installable": True,
'account_bank_statement_import', "license": "AGPL-3",
],
'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 # Copyright 2016-2017 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # 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 import base64
from odoo.modules.module import get_module_resource
from odoo.tests.common import TransactionCase
class TestQifFile(TransactionCase): class TestQifFile(TransactionCase):
"""Tests for import bank statement qif file format """Tests for import bank statement qif file format
@@ -16,34 +17,32 @@ class TestQifFile(TransactionCase):
def setUp(self): def setUp(self):
super(TestQifFile, self).setUp() super(TestQifFile, self).setUp()
self.statement_import_model = self.env['account.bank.statement.import'] self.statement_import_model = self.env["account.bank.statement.import"]
self.statement_line_model = self.env['account.bank.statement.line'] self.statement_line_model = self.env["account.bank.statement.line"]
self.journal = self.env['account.journal'].create({ self.journal = self.env["account.journal"].create(
'name': 'Test bank journal', {"name": "Test bank journal", "code": "TEST", "type": "bank",}
'code': 'TEST', )
'type': 'bank', self.partner = self.env["res.partner"].create(
}) {
self.partner = self.env['res.partner'].create({
# Different case for trying insensitive case search # Different case for trying insensitive case search
'name': 'EPIC Technologies', "name": "EPIC Technologies",
}) }
)
def test_qif_file_import(self): def test_qif_file_import(self):
qif_file_path = get_module_resource( 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( wizard = self.statement_import_model.with_context(
journal_id=self.journal.id journal_id=self.journal.id
).create( ).create(dict(data_file=qif_file))
dict(data_file=qif_file)
)
wizard.import_file() wizard.import_file()
statement = self.statement_line_model.search( statement = self.statement_line_model.search(
[('name', '=', 'YOUR LOCAL SUPERMARKET')], limit=1, [("name", "=", "YOUR LOCAL SUPERMARKET")], limit=1,
)[0].statement_id )[0].statement_id
self.assertAlmostEqual(statement.balance_end_real, -1896.09, 2) self.assertAlmostEqual(statement.balance_end_real, -1896.09, 2)
line = self.statement_line_model.search( line = self.statement_line_model.search(
[('name', '=', 'Epic Technologies')], limit=1, [("name", "=", "Epic Technologies")], limit=1,
) )
self.assertEqual(line.partner_id, self.partner) self.assertEqual(line.partner_id, self.partner)

View File

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

View File

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