From 0aba7282e174260c650d2b5a48627242d02c4c98 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Fri, 23 Jan 2015 22:50:24 +0100 Subject: [PATCH] New backport from odoo/master Fix bug #5 --- .../__openerp__.py | 3 +- .../account_bank_statement_import_ofx.py | 71 ++++++++---------- .../static/description/icon.png | Bin 0 -> 6282 bytes .../tests/__init__.py | 4 - .../tests/test_import_bank_statement.py | 7 +- 5 files changed, 36 insertions(+), 49 deletions(-) create mode 100644 account_bank_statement_import_ofx/static/description/icon.png diff --git a/account_bank_statement_import_ofx/__openerp__.py b/account_bank_statement_import_ofx/__openerp__.py index 25056dd..8d2d4ee 100644 --- a/account_bank_statement_import_ofx/__openerp__.py +++ b/account_bank_statement_import_ofx/__openerp__.py @@ -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: diff --git a/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py b/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py index 6f690a7..cf0eb8a 100644 --- a/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py +++ b/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py @@ -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] diff --git a/account_bank_statement_import_ofx/static/description/icon.png b/account_bank_statement_import_ofx/static/description/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..c630877c1c5f76912c1e9197d3e688eb2f0e65d4 GIT binary patch literal 6282 zcma)BXHXMLv`(S-AiV_wN|B-@Aiac8yr=|hfJ!%nOAEbAD4}T}u|a6kRFEn`kRC+^ zF+flRK_E1x2Lur@0+0Luy_vT&=bYIyJNskjJMG&{^aWc%J{dj$03dkQ4&`)Y@&5@A z*OBfq^ZamRIKq%;FYz3$cpjgOqdhOq&Ls>0fVBK4zZQB2;Y&y;tGP==RfZq5iDxSwUT$_Da{#MTZEc(e7l}#@?XeMoW=D>q7FDHP!2kqGbq~ z$AY2^!Ry+>05`KyfPT45zsoTqlEr{9%#b$_0wz2ajsUQF?n7E?T4*j zhxFEHaCH93IDZEYs=F*Fa2b$)32js`!n$6eh zi83+*3Iinz#ut+yG$zR-2+)PatWNEJwQ%={t7I>u4?PLUFuYbJwE$dz_nzDr_mE^j zf9uNZ-zIzDZMGrH;;rm|J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