mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
committed by
Alexis de Lattre
parent
f59386b9c6
commit
ac6bce3885
@@ -3,6 +3,7 @@
|
||||
# flake8: noqa
|
||||
{
|
||||
'name': 'Import OFX Bank Statement',
|
||||
'category' : 'Accounting & Finance',
|
||||
'version': '1.0',
|
||||
'author': 'OpenERP SA',
|
||||
'depends': ['account_bank_statement_import'],
|
||||
@@ -28,5 +29,3 @@ create periods for the year 2013.
|
||||
'auto_install': False,
|
||||
'installable': True,
|
||||
}
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
||||
@@ -3,74 +3,67 @@
|
||||
# flake8: noqa
|
||||
|
||||
import logging
|
||||
import base64
|
||||
import os
|
||||
import StringIO
|
||||
|
||||
from openerp.osv import osv
|
||||
from openerp.tools.translate import _
|
||||
from openerp.exceptions import Warning
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
from openerp.addons.account_bank_statement_import import account_bank_statement_import as ibs
|
||||
ibs.add_file_type(('ofx', 'OFX'))
|
||||
|
||||
try:
|
||||
from ofxparse import OfxParser as ofxparser
|
||||
except ImportError:
|
||||
_logger.warning("OFX parser unavailable because the `ofxparse` Python library cannot be found."
|
||||
_logger.error("OFX parser unavailable because the `ofxparse` Python library cannot be found."
|
||||
"It can be downloaded and installed from `https://pypi.python.org/pypi/ofxparse`.")
|
||||
ofxparser = None
|
||||
|
||||
class account_bank_statement_import(osv.TransientModel):
|
||||
_inherit = 'account.bank.statement.import'
|
||||
|
||||
def process_ofx(self, cr, uid, data_file, journal_id=False, context=None):
|
||||
""" Import a file in the .OFX format"""
|
||||
def _check_ofx(self, cr, uid, file, context=None):
|
||||
if ofxparser is None:
|
||||
raise osv.except_osv(_("Error"), _("OFX parser unavailable because the `ofxparse` Python library cannot be found."
|
||||
"It can be downloaded and installed from `https://pypi.python.org/pypi/ofxparse`."))
|
||||
return False
|
||||
try:
|
||||
tempfile = open("temp.ofx", "w+")
|
||||
tempfile.write(base64.decodestring(data_file))
|
||||
tempfile.read()
|
||||
pathname = os.path.dirname('temp.ofx')
|
||||
path = os.path.join(os.path.abspath(pathname), 'temp.ofx')
|
||||
ofx = ofxparser.parse(file(path))
|
||||
ofx = ofxparser.parse(file)
|
||||
except:
|
||||
raise osv.except_osv(_('Import Error!'), _('Please check OFX file format is proper or not.'))
|
||||
line_ids = []
|
||||
return False
|
||||
return ofx
|
||||
|
||||
def _parse_file(self, cr, uid, data_file, context=None):
|
||||
ofx = self._check_ofx(cr, uid, StringIO.StringIO(data_file), context=context)
|
||||
if not ofx:
|
||||
return super(account_bank_statement_import, self)._parse_file(cr, uid, data_file, context=context)
|
||||
|
||||
transactions = []
|
||||
total_amt = 0.00
|
||||
try:
|
||||
for transaction in ofx.account.statement.transactions:
|
||||
bank_account_id, partner_id = self._detect_partner(cr, uid, transaction.payee, identifying_field='owner_name', context=context)
|
||||
# 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
|
||||
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
|
||||
vals_line = {
|
||||
'date': transaction.date,
|
||||
'name': transaction.payee + ': ' + transaction.memo,
|
||||
'name': transaction.payee + (transaction.memo and ': ' + transaction.memo or ''),
|
||||
'ref': transaction.id,
|
||||
'amount': transaction.amount,
|
||||
'partner_id': partner_id,
|
||||
'unique_import_id': transaction.id,
|
||||
'bank_account_id': bank_account_id,
|
||||
'partner_id': partner_id,
|
||||
}
|
||||
total_amt += float(transaction.amount)
|
||||
line_ids.append((0, 0, vals_line))
|
||||
transactions.append(vals_line)
|
||||
except Exception, e:
|
||||
raise osv.except_osv(_('Error!'), _("Following problem has been occurred while importing your file, Please verify the file is proper or not.\n\n %s" % e.message))
|
||||
st_start_date = ofx.account.statement.start_date or False
|
||||
st_end_date = ofx.account.statement.end_date or False
|
||||
period_obj = self.pool.get('account.period')
|
||||
if st_end_date:
|
||||
period_ids = period_obj.find(cr, uid, st_end_date, context=context)
|
||||
else:
|
||||
period_ids = period_obj.find(cr, uid, st_start_date, context=context)
|
||||
raise Warning(_("The following problem occurred during import. The file might not be valid.\n\n %s" % e.message))
|
||||
|
||||
vals_bank_statement = {
|
||||
'name': ofx.account.routing_number,
|
||||
'balance_start': ofx.account.statement.balance,
|
||||
'balance_end_real': float(ofx.account.statement.balance) + total_amt,
|
||||
'period_id': period_ids and period_ids[0] or False,
|
||||
'journal_id': journal_id
|
||||
'transactions': transactions,
|
||||
'balance_start': float(ofx.account.statement.balance) - total_amt,
|
||||
'balance_end_real': float(ofx.account.statement.balance),
|
||||
}
|
||||
vals_bank_statement.update({'line_ids': line_ids})
|
||||
os.remove(path)
|
||||
return [vals_bank_statement]
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
return ofx.account.statement.currency, ofx.account.number, [vals_bank_statement]
|
||||
|
||||
BIN
account_bank_statement_import_ofx/static/description/icon.png
Normal file
BIN
account_bank_statement_import_ofx/static/description/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.1 KiB |
@@ -2,7 +2,3 @@
|
||||
# noqa: This is a backport from Odoo. OCA has no control over style here.
|
||||
# flake8: noqa
|
||||
from . import test_import_bank_statement
|
||||
|
||||
checks = [
|
||||
test_import_bank_statement
|
||||
]
|
||||
|
||||
@@ -23,10 +23,9 @@ class TestOfxFile(TransactionCase):
|
||||
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')
|
||||
bank_statement_id = self.statement_import_model.create(cr, uid, dict(
|
||||
file_type='ofx',
|
||||
data_file=ofx_file,
|
||||
))
|
||||
self.statement_import_model.parse_file(cr, uid, [bank_statement_id])
|
||||
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)
|
||||
self.assertEquals(bank_st_record.balance_start, 2156.56)
|
||||
|
||||
Reference in New Issue
Block a user