Browse Source

[IMP] use new API

Conflicts:
	account_bank_statement_import/account_bank_statement_import.py
pull/200/head
Laurent Mignon (ACSONE) 10 years ago
committed by Benjamin
parent
commit
dca15a8f09
  1. 2
      account_bank_statement_import_ofx/__init__.py
  2. 48
      account_bank_statement_import_ofx/account_bank_statement_import_ofx.py
  3. 27
      account_bank_statement_import_ofx/tests/test_import_bank_statement.py

2
account_bank_statement_import_ofx/__init__.py

@ -1,3 +1,3 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
import account_bank_statement_import_ofx
from . import account_bank_statement_import_ofx

48
account_bank_statement_import_ofx/account_bank_statement_import_ofx.py

@ -1,11 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# noqa: This is a backport from Odoo. OCA has no control over style here.
# flake8: noqa
import logging import logging
import StringIO import StringIO
from openerp.osv import osv
from openerp import api, models
from openerp.tools.translate import _ from openerp.tools.translate import _
from openerp.exceptions import Warning from openerp.exceptions import Warning
@ -17,37 +15,46 @@ except ImportError:
_logger.warn("ofxparse not found, OFX parsing disabled.") _logger.warn("ofxparse not found, OFX parsing disabled.")
ofxparser = None ofxparser = None
class account_bank_statement_import(osv.TransientModel):
class account_bank_statement_import(models.TransientModel):
_inherit = 'account.bank.statement.import' _inherit = 'account.bank.statement.import'
def _check_ofx(self, cr, uid, file, context=None):
@api.model
def _check_ofx(self, data_file):
if ofxparser is None: if ofxparser is None:
return False return False
try: try:
ofx = ofxparser.parse(file)
ofx = ofxparser.parse(StringIO.StringIO(data_file))
except: except:
return False return False
return ofx return ofx
def _parse_file(self, cr, uid, data_file, context=None):
ofx = self._check_ofx(cr, uid, StringIO.StringIO(data_file), context=context)
@api.model
def _parse_file(self, data_file):
ofx = self._check_ofx(data_file)
if not ofx: if not ofx:
return super(account_bank_statement_import, self)._parse_file(cr, uid, data_file, context=context)
return super(account_bank_statement_import, self)._parse_file(
data_file)
transactions = [] transactions = []
total_amt = 0.00 total_amt = 0.00
try: try:
for transaction in ofx.account.statement.transactions: for transaction in ofx.account.statement.transactions:
# Since ofxparse doesn't provide account numbers, we'll have to find res.partner and res.partner.bank here
# (normal behavious is to provide 'account_number', which the generic module uses to find partner/bank)
# Since ofxparse doesn't provide account numbers, we'll have
# to find res.partner and res.partner.bank here
# (normal behavious is to provide 'account_number', which the
# generic module uses to find partner/bank)
bank_account_id = partner_id = False bank_account_id = partner_id = False
ids = self.pool.get('res.partner.bank').search(cr, uid, [('owner_name', '=', transaction.payee)], context=context)
if ids:
bank_account_id = bank_account_id = ids[0]
partner_id = self.pool.get('res.partner.bank').browse(cr, uid, bank_account_id, context=context).partner_id.id
banks = self.env['res.partner.bank'].search(
[('owner_name', '=', transaction.payee)], limit=1)
if banks:
bank_account = banks[0]
bank_account_id = bank_account.id
partner_id = bank_account.partner_id.id
vals_line = { vals_line = {
'date': transaction.date, 'date': transaction.date,
'name': transaction.payee + (transaction.memo and ': ' + transaction.memo or ''),
'name': transaction.payee + (
transaction.memo and ': ' + transaction.memo or ''),
'ref': transaction.id, 'ref': transaction.id,
'amount': transaction.amount, 'amount': transaction.amount,
'unique_import_id': transaction.id, 'unique_import_id': transaction.id,
@ -57,12 +64,15 @@ class account_bank_statement_import(osv.TransientModel):
total_amt += float(transaction.amount) total_amt += float(transaction.amount)
transactions.append(vals_line) transactions.append(vals_line)
except Exception, e: except Exception, e:
raise Warning(_("The following problem occurred during import. The file might not be valid.\n\n %s" % e.message))
raise Warning(_("The following problem occurred during import. "
"The file might not be valid.\n\n %s" % e.message))
vals_bank_statement = { vals_bank_statement = {
'name': ofx.account.routing_number, 'name': ofx.account.routing_number,
'transactions': transactions, 'transactions': transactions,
'balance_start': ofx.account.statement.balance, 'balance_start': ofx.account.statement.balance,
'balance_end_real': float(ofx.account.statement.balance) + total_amt,
'balance_end_real':
float(ofx.account.statement.balance) + total_amt,
} }
return ofx.account.statement.currency, ofx.account.number, [vals_bank_statement]
return ofx.account.statement.currency, ofx.account.number, [
vals_bank_statement]

27
account_bank_statement_import_ofx/tests/test_import_bank_statement.py

@ -4,29 +4,32 @@
from openerp.tests.common import TransactionCase from openerp.tests.common import TransactionCase
from openerp.modules.module import get_module_resource from openerp.modules.module import get_module_resource
class TestOfxFile(TransactionCase): class TestOfxFile(TransactionCase):
"""Tests for import bank statement ofx file format (account.bank.statement.import)
"""Tests for import bank statement ofx file format
(account.bank.statement.import)
""" """
def setUp(self): def setUp(self):
super(TestOfxFile, self).setUp() super(TestOfxFile, self).setUp()
self.statement_import_model = self.registry('account.bank.statement.import')
self.bank_statement_model = self.registry('account.bank.statement')
self.statement_import_model = self.env['account.bank.statement.import']
self.bank_statement_model = self.env['account.bank.statement']
def test_ofx_file_import(self): def test_ofx_file_import(self):
try: try:
from ofxparse import OfxParser as ofxparser from ofxparse import OfxParser as ofxparser
except ImportError: except ImportError:
#the Python library isn't installed on the server, the OFX import is unavailable and the test cannot be run
# the Python library isn't installed on the server, the OFX import
# is unavailable and the test cannot be run
return True return True
cr, uid = self.cr, self.uid
ofx_file_path = get_module_resource('account_bank_statement_import_ofx', 'test_ofx_file', 'test_ofx.ofx')
ofx_file_path = get_module_resource(
'account_bank_statement_import_ofx',
'test_ofx_file', 'test_ofx.ofx')
ofx_file = open(ofx_file_path, 'rb').read().encode('base64') ofx_file = open(ofx_file_path, 'rb').read().encode('base64')
bank_statement_id = self.statement_import_model.create(cr, uid, dict(
data_file=ofx_file,
))
self.statement_import_model.import_file(cr, uid, [bank_statement_id])
statement_id = self.bank_statement_model.search(cr, uid, [('name', '=', '000000123')])[0]
bank_st_record = self.bank_statement_model.browse(cr, uid, statement_id)
bank_statement = self.statement_import_model.create(
dict(data_file=ofx_file))
bank_statement.import_file()
bank_st_record = self.bank_statement_model.search(
[('name', '=', '000000123')])[0]
self.assertEquals(bank_st_record.balance_start, 2156.56) self.assertEquals(bank_st_record.balance_start, 2156.56)
self.assertEquals(bank_st_record.balance_end_real, 1796.56) self.assertEquals(bank_st_record.balance_end_real, 1796.56)
Loading…
Cancel
Save