|
@ -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] |