ofx: black and others...

This commit is contained in:
Alexis de Lattre
2020-05-16 01:28:57 +02:00
parent 7318fdd424
commit 7ad314d53e
6 changed files with 127 additions and 118 deletions

View File

@@ -1,24 +1,18 @@
{
'name': 'Import OFX Bank Statement',
'category': 'Banking addons',
'version': '13.0.1.0.0',
'license': 'AGPL-3',
'author': 'Odoo SA,'
'Akretion,'
'La Louve,'
'GRAP,'
'Nicolas JEUDY,'
'Le Filament,'
'Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/bank-statement-import',
'depends': [
'account_bank_statement_import',
],
'data': [
'views/view_account_bank_statement_import.xml',
],
'external_dependencies': {
'python': ['ofxparse'],
},
'installable': True,
"name": "Import OFX Bank Statement",
"category": "Banking addons",
"version": "13.0.1.0.0",
"license": "AGPL-3",
"author": "Odoo SA,"
"Akretion,"
"La Louve,"
"GRAP,"
"Nicolas JEUDY,"
"Le Filament,"
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/bank-statement-import",
"depends": ["account_bank_statement_import",],
"data": ["views/view_account_bank_statement_import.xml",],
"external_dependencies": {"python": ["ofxparse"],},
"installable": True,
}

View File

@@ -1 +1 @@
from . import account_journal
from . import account_journal

View File

@@ -8,5 +8,5 @@ class AccountJournal(models.Model):
""" Adds ofx to supported import formats.
"""
rslt = super()._get_bank_statements_available_import_formats()
rslt.append('ofx')
rslt.append("ofx")
return rslt

View File

@@ -1,8 +1,9 @@
from odoo.tests.common import TransactionCase
from odoo.modules.module import get_module_resource
import base64
import datetime
from odoo.modules.module import get_module_resource
from odoo.tests.common import TransactionCase
class TestOfxFile(TransactionCase):
"""Tests for import bank statement ofx file format
@@ -11,90 +12,95 @@ class TestOfxFile(TransactionCase):
def setUp(self):
super(TestOfxFile, self).setUp()
self.absi_model = self.env['account.bank.statement.import']
self.abs_model = self.env['account.bank.statement']
self.j_model = self.env['account.journal']
self.absl_model = self.env['account.bank.statement.line']
self.ia_model = self.env['ir.attachment']
cur = self.env.ref('base.USD')
self.env.ref('base.main_company').currency_id = cur.id
bank = self.env['res.partner.bank'].create({
'acc_number': '123456',
'partner_id': self.env.ref('base.main_partner').id,
'company_id': self.env.ref('base.main_company').id,
'bank_id': self.env.ref('base.res_bank_1').id,
})
self.env['account.journal'].create({
'name': 'Bank Journal TEST OFX',
'code': 'BNK12',
'type': 'bank',
'bank_account_id': bank.id,
})
self.absi_model = self.env["account.bank.statement.import"]
self.abs_model = self.env["account.bank.statement"]
self.j_model = self.env["account.journal"]
self.absl_model = self.env["account.bank.statement.line"]
self.ia_model = self.env["ir.attachment"]
cur = self.env.ref("base.USD")
self.env.ref("base.main_company").currency_id = cur.id
bank = self.env["res.partner.bank"].create(
{
"acc_number": "123456",
"partner_id": self.env.ref("base.main_partner").id,
"company_id": self.env.ref("base.main_company").id,
"bank_id": self.env.ref("base.res_bank_1").id,
}
)
self.env["account.journal"].create(
{
"name": "Bank Journal TEST OFX",
"code": "BNK12",
"type": "bank",
"bank_account_id": bank.id,
}
)
bank_iban_ofx = self.env['res.partner.bank'].create({
'acc_number': 'FR7630001007941234567890185',
'partner_id': self.env.ref('base.main_partner').id,
'company_id': self.env.ref('base.main_company').id,
'bank_id': self.env.ref('base.res_bank_1').id,
})
bank_iban_ofx = self.env["res.partner.bank"].create(
{
"acc_number": "FR7630001007941234567890185",
"partner_id": self.env.ref("base.main_partner").id,
"company_id": self.env.ref("base.main_company").id,
"bank_id": self.env.ref("base.res_bank_1").id,
}
)
self.env['account.journal'].create({
'name': 'FR7630001007941234567890185',
'code': 'BNK13',
'type': 'bank',
'bank_account_id': bank_iban_ofx.id,
})
self.env["account.journal"].create(
{
"name": "FR7630001007941234567890185",
"code": "BNK13",
"type": "bank",
"bank_account_id": bank_iban_ofx.id,
}
)
def test_wrong_ofx_file_import(self):
ofx_file_path = get_module_resource(
'account_bank_statement_import_ofx',
'tests/test_ofx_file/', 'test_ofx_wrong.ofx')
ofx_file_wrong = base64.b64encode(open(ofx_file_path, 'rb').read())
attach = self.ia_model.create({
'name': 'test_ofx_wrong.ofx',
'datas': ofx_file_wrong,
})
"account_bank_statement_import_ofx",
"tests/test_ofx_file/",
"test_ofx_wrong.ofx",
)
ofx_file_wrong = base64.b64encode(open(ofx_file_path, "rb").read())
attach = self.ia_model.create(
{"name": "test_ofx_wrong.ofx", "datas": ofx_file_wrong,}
)
bank_statement = self.absi_model.create(
dict(attachment_ids=[(6, 0, [attach.id])]))
dict(attachment_ids=[(6, 0, [attach.id])])
)
self.assertFalse(bank_statement._check_ofx(data_file=ofx_file_wrong))
def test_ofx_file_import(self):
ofx_file_path = get_module_resource(
'account_bank_statement_import_ofx',
'tests/test_ofx_file/', 'test_ofx.ofx')
ofx_file = base64.b64encode(open(ofx_file_path, 'rb').read())
attach = self.ia_model.create({
'name': 'test_ofx.ofx',
'datas': ofx_file,
})
"account_bank_statement_import_ofx", "tests/test_ofx_file/", "test_ofx.ofx"
)
ofx_file = base64.b64encode(open(ofx_file_path, "rb").read())
attach = self.ia_model.create({"name": "test_ofx.ofx", "datas": ofx_file,})
bank_statement = self.absi_model.create(
dict(attachment_ids=[(6, 0, [attach.id])]))
dict(attachment_ids=[(6, 0, [attach.id])])
)
bank_statement.import_file()
bank_st_record = self.abs_model.search(
[('name', 'like', '123456')])[0]
bank_st_record = self.abs_model.search([("name", "like", "123456")])[0]
self.assertEqual(bank_st_record.balance_start, 2516.56)
self.assertEqual(bank_st_record.balance_end_real, 2156.56)
line = self.absl_model.search([
('name', '=', 'Agrolait'),
('statement_id', '=', bank_st_record.id)])[0]
self.assertEqual(line.ref, '219378')
line = self.absl_model.search(
[("name", "=", "Agrolait"), ("statement_id", "=", bank_st_record.id)]
)[0]
self.assertEqual(line.ref, "219378")
self.assertEqual(line.date, datetime.date(2013, 8, 24))
def test_check_journal_bank_account(self):
ofx_file_path = get_module_resource(
'account_bank_statement_import_ofx',
'tests/test_ofx_file/', 'test_ofx_iban.ofx')
ofx_file = base64.b64encode(open(ofx_file_path, 'rb').read())
attach = self.ia_model.create({
'name': 'test_ofx.ofx',
'datas': ofx_file,
})
bank_st = self.absi_model.create(
dict(attachment_ids=[(6, 0, [attach.id])]))
journal_iban_ofx = self.j_model.search([
('name', '=', 'FR7630001007941234567890185')])
res = bank_st._check_journal_bank_account(journal_iban_ofx,
'12345678901')
"account_bank_statement_import_ofx",
"tests/test_ofx_file/",
"test_ofx_iban.ofx",
)
ofx_file = base64.b64encode(open(ofx_file_path, "rb").read())
attach = self.ia_model.create({"name": "test_ofx.ofx", "datas": ofx_file,})
bank_st = self.absi_model.create(dict(attachment_ids=[(6, 0, [attach.id])]))
journal_iban_ofx = self.j_model.search(
[("name", "=", "FR7630001007941234567890185")]
)
res = bank_st._check_journal_bank_account(journal_iban_ofx, "12345678901")
self.assertTrue(res)
bank_st.with_context(journal_id=journal_iban_ofx.id).import_file()

View File

@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_account_bank_statement_import_form" 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>Open Financial Exchange (.OFX Money)</li>

View File

@@ -1,10 +1,13 @@
import logging
import io
import logging
from odoo import api, models, _
from odoo import _, api, models
from odoo.exceptions import UserError
from odoo.addons.base_iban.models.res_partner_bank import _map_iban_template
from odoo.addons.base_iban.models.res_partner_bank import validate_iban
from odoo.addons.base_iban.models.res_partner_bank import (
_map_iban_template,
validate_iban,
)
_logger = logging.getLogger(__name__)
@@ -16,7 +19,7 @@ except ImportError:
class AccountBankStatementImport(models.TransientModel):
_inherit = 'account.bank.statement.import'
_inherit = "account.bank.statement.import"
def _check_journal_bank_account(self, journal, account_number):
res = super()._check_journal_bank_account(journal, account_number)
@@ -25,11 +28,11 @@ class AccountBankStatementImport(models.TransientModel):
e_acc_num = e_acc_num.replace(" ", "")
validate_iban(e_acc_num)
country_code = e_acc_num[:2].lower()
iban_template = _map_iban_template[country_code].replace(
" ", "")
iban_template = _map_iban_template[country_code].replace(" ", "")
e_acc_num = "".join(
[c for c, t in zip(e_acc_num, iban_template) if t == "C"])
res = (e_acc_num == account_number)
[c for c, t in zip(e_acc_num, iban_template) if t == "C"]
)
res = e_acc_num == account_number
return res
@api.model
@@ -57,11 +60,11 @@ class AccountBankStatementImport(models.TransientModel):
if transaction.memo:
name += " : " + transaction.memo
vals = {
'date': transaction.date,
'name': name,
'ref': transaction.id,
'amount': float(transaction.amount),
'unique_import_id': transaction.id,
"date": transaction.date,
"name": name,
"ref": transaction.id,
"amount": float(transaction.amount),
"unique_import_id": transaction.id,
}
return vals
@@ -77,18 +80,21 @@ class AccountBankStatementImport(models.TransientModel):
vals = self._prepare_ofx_transaction_line(transaction)
if vals:
transactions.append(vals)
total_amt += vals['amount']
total_amt += vals["amount"]
except Exception as e:
raise UserError(_(
"The following problem occurred during import. "
"The file might not be valid.\n\n %s") % e.message)
raise UserError(
_(
"The following problem occurred during import. "
"The file might not be valid.\n\n %s"
)
% e.message
)
balance = float(ofx.account.statement.balance)
vals_bank_statement = {
'name': ofx.account.number,
'transactions': transactions,
'balance_start': balance - total_amt,
'balance_end_real': balance,
"name": ofx.account.number,
"transactions": transactions,
"balance_start": balance - total_amt,
"balance_end_real": balance,
}
return ofx.account.statement.currency, ofx.account.number, [
vals_bank_statement]
return ofx.account.statement.currency, ofx.account.number, [vals_bank_statement]