diff --git a/account_bank_statement_import/__init__.py b/account_bank_statement_import/__init__.py new file mode 100644 index 0000000..edce853 --- /dev/null +++ b/account_bank_statement_import/__init__.py @@ -0,0 +1,5 @@ +# -*- encoding: utf-8 -*- + +import account_bank_statement_import + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/account_bank_statement_import/__openerp__.py b/account_bank_statement_import/__openerp__.py new file mode 100644 index 0000000..af12dd4 --- /dev/null +++ b/account_bank_statement_import/__openerp__.py @@ -0,0 +1,25 @@ +# -*- encoding: utf-8 -*- +{ + 'name': 'Account Bank Statement Import', + 'version': '1.0', + 'author': 'OpenERP SA', + 'depends': ['account'], + 'demo': [], + 'description' : """Generic Wizard to Import Bank Statements. + + Includes the import of files in .OFX format + + Backport from Odoo 9.0 + """, + 'data' : [ + 'account_bank_statement_import_view.xml', + ], + 'demo': [ + 'demo/fiscalyear_period.xml', + 'demo/partner_bank.xml', + ], + 'auto_install': False, + 'installable': True, +} + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py new file mode 100644 index 0000000..7866aeb --- /dev/null +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -0,0 +1,96 @@ +# -*- coding: utf-8 -*- + +from openerp.osv import fields, osv +from openerp.tools.translate import _ + +import logging +_logger = logging.getLogger(__name__) + +_IMPORT_FILE_TYPE = [('none', _('No Import Format Available'))] + +def add_file_type(selection_value): + global _IMPORT_FILE_TYPE + if _IMPORT_FILE_TYPE[0][0] == 'none': + _IMPORT_FILE_TYPE = [selection_value] + else: + _IMPORT_FILE_TYPE.append(selection_value) + +class account_bank_statement_import(osv.TransientModel): + _name = 'account.bank.statement.import' + _description = 'Import Bank Statement' + + def _get_import_file_type(self, cr, uid, context=None): + return _IMPORT_FILE_TYPE + + _columns = { + 'data_file': fields.binary('Bank Statement File', required=True, help='Get you bank statements in electronic format from your bank and select them here.'), + 'file_type': fields.selection(_get_import_file_type, 'File Type', required=True), + 'journal_id': fields.many2one('account.journal', 'Journal', required=True, help="The journal for which the bank statements will be created"), + } + + def _get_first_file_type(self, cr, uid, context=None): + return self._get_import_file_type(cr, uid, context=context)[0][0] + + def _get_default_journal(self, cr, uid, context=None): + company_id = self.pool.get('res.company')._company_default_get(cr, uid, 'account.bank.statement', context=context) + journal_ids = self.pool.get('account.journal').search(cr, uid, [('type', '=', 'bank'), ('company_id', '=', company_id)], context=context) + return journal_ids and journal_ids[0] or False + + _defaults = { + 'file_type': _get_first_file_type, + 'journal_id': _get_default_journal, + } + + def _detect_partner(self, cr, uid, identifying_string, identifying_field='acc_number', context=None): + """Try to find a bank account and its related partner for the given 'identifying_string', looking on the field 'identifying_field'. + + :param identifying_string: varchar + :param identifying_field: varchar corresponding to the name of a field of res.partner.bank + :returns: tuple(ID of the bank account found or False, ID of the partner for the bank account found or False) + """ + partner_id = False + bank_account_id = False + if identifying_string: + ids = self.pool.get('res.partner.bank').search(cr, uid, [(identifying_field, '=', identifying_string)], context=context) + if ids: + bank_account_id = ids[0] + partner_id = self.pool.get('res.partner.bank').browse(cr, uid, bank_account_id, context=context).partner_id.id + else: + #create the bank account, not linked to any partner. The reconciliation will link the partner manually + #chosen at the bank statement final confirmation time. + try: + type_model, type_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'bank_normal') + type_id = self.pool.get('res.partner.bank.type').browse(cr, uid, type_id, context=context) + bank_code = type_id.code + except ValueError: + bank_code = 'bank' + acc_number = identifying_field == 'acc_number' and identifying_string or _('Undefined') + bank_account_vals = { + 'acc_number': acc_number, + 'state': bank_code, + } + bank_account_vals[identifying_field] = identifying_string + bank_account_id = self.pool.get('res.partner.bank').create(cr, uid, bank_account_vals, context=context) + return bank_account_id, partner_id + + def import_bank_statement(self, cr, uid, bank_statement_vals=False, context=None): + """ Get a list of values to pass to the create() of account.bank.statement object, and returns a list of ID created using those values""" + statement_ids = [] + for vals in bank_statement_vals: + statement_ids.append(self.pool.get('account.bank.statement').create(cr, uid, vals, context=context)) + return statement_ids + + def process_none(self, cr, uid, data_file, journal_id=False, context=None): + raise osv.except_osv(_('Error'), _('No available format for importing bank statement. You can install one of the file format available through the module installation.')) + + def parse_file(self, cr, uid, ids, context=None): + """ Process the file chosen in the wizard and returns a list view of the imported bank statements""" + data = self.browse(cr, uid, ids[0], context=context) + vals = getattr(self, "process_%s" % data.file_type)(cr, uid, data.data_file, data.journal_id.id, context=context) + statement_ids = self.import_bank_statement(cr, uid, vals, context=context) + model, action_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'action_bank_statement_tree') + action = self.pool[model].read(cr, uid, action_id, context=context) + action['domain'] = "[('id', 'in', [" + ', '.join(map(str, statement_ids)) + "])]" + return action + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/account_bank_statement_import/account_bank_statement_import_view.xml b/account_bank_statement_import/account_bank_statement_import_view.xml new file mode 100644 index 0000000..c48371c --- /dev/null +++ b/account_bank_statement_import/account_bank_statement_import_view.xml @@ -0,0 +1,46 @@ + + + + + + Import Bank Statements + account.bank.statement.import + 1 + +
+ + + + + + + + How to import your bank statement in OpenERP. + + +
+
+
+
+
+ + + Import + ir.actions.act_window + account.bank.statement.import + form + form + new + + + + + +
+
diff --git a/account_bank_statement_import/demo/fiscalyear_period.xml b/account_bank_statement_import/demo/fiscalyear_period.xml new file mode 100644 index 0000000..41b4be3 --- /dev/null +++ b/account_bank_statement_import/demo/fiscalyear_period.xml @@ -0,0 +1,119 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/account_bank_statement_import/demo/partner_bank.xml b/account_bank_statement_import/demo/partner_bank.xml new file mode 100644 index 0000000..47e8fa7 --- /dev/null +++ b/account_bank_statement_import/demo/partner_bank.xml @@ -0,0 +1,38 @@ + + + + + + Agrolait + 00987654321 + + bank + + + + + China Export + 00987654322 + + bank + + + + + Delta PC + 10987654320 + + bank + + + + + Epic Technologies + 10987654322 + + bank + + + + + diff --git a/account_bank_statement_import_ofx/__init__.py b/account_bank_statement_import_ofx/__init__.py new file mode 100644 index 0000000..fe838c2 --- /dev/null +++ b/account_bank_statement_import_ofx/__init__.py @@ -0,0 +1,5 @@ +# -*- encoding: utf-8 -*- + +import account_bank_statement_import_ofx + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/account_bank_statement_import_ofx/__openerp__.py b/account_bank_statement_import_ofx/__openerp__.py new file mode 100644 index 0000000..8cd569b --- /dev/null +++ b/account_bank_statement_import_ofx/__openerp__.py @@ -0,0 +1,26 @@ +# -*- encoding: utf-8 -*- +{ + 'name': 'Import OFX Bank Statement', + 'version': '1.0', + 'author': 'OpenERP SA', + 'depends': ['account_bank_statement_import'], + 'demo': [], + 'description' : """ +Module to import OFX bank statements. +====================================== + +This module allows you to import the machine readable OFX Files in Odoo: they are parsed and stored in human readable format in +Accounting \ Bank and Cash \ Bank Statements. + +Bank Statements may be generated containing a subset of the OFX information (only those transaction lines that are required for the +creation of the Financial Accounting records). + +Backported from Odoo 9.0 + """, + 'data' : [], + 'demo': [], + '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 new file mode 100644 index 0000000..13966c0 --- /dev/null +++ b/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- + +import logging +import base64 +import os + +from openerp.osv import osv +from openerp.tools.translate import _ + +_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." + "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""" + 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`.")) + 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)) + except: + raise osv.except_osv(_('Import Error!'), _('Please check OFX file format is proper or not.')) + line_ids = [] + 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) + vals_line = { + 'date': transaction.date, + 'name': transaction.payee + ': ' + transaction.memo, + 'ref': transaction.id, + 'amount': transaction.amount, + 'partner_id': partner_id, + 'bank_account_id': bank_account_id, + } + total_amt += float(transaction.amount) + line_ids.append((0, 0, 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) + 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 + } + vals_bank_statement.update({'line_ids': line_ids}) + os.remove(path) + return [vals_bank_statement] + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/account_bank_statement_import_ofx/test_ofx_file/test_ofx.ofx b/account_bank_statement_import_ofx/test_ofx_file/test_ofx.ofx new file mode 100644 index 0000000..37df4d0 --- /dev/null +++ b/account_bank_statement_import_ofx/test_ofx_file/test_ofx.ofx @@ -0,0 +1,100 @@ + + + + + + + 0 + INFO + + 20130831165153.000[-8:PST] + ENG + + + + + 0 + + 0 + INFO + + + USD + + 000000123 + 123456 + CHECKING + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -80 + 219378 + Agrolait + + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -90 + 219379 + China Export + + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -100 + 219380 + Axelor Scuba + + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -90 + 219381 + China Scuba + + + + 2156.56 + 20130831165153 + + + + + + + 0 + + 0 + INFO + + + USD + + 123412341234 + + + + + -562.00 + 20130831165153 + + + + + diff --git a/account_bank_statement_import_ofx/tests/__init__.py b/account_bank_statement_import_ofx/tests/__init__.py new file mode 100644 index 0000000..8a5a8e9 --- /dev/null +++ b/account_bank_statement_import_ofx/tests/__init__.py @@ -0,0 +1,5 @@ +from . import test_import_bank_statement + +checks = [ + test_import_bank_statement +] diff --git a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py new file mode 100644 index 0000000..967d647 --- /dev/null +++ b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py @@ -0,0 +1,30 @@ +from openerp.tests.common import TransactionCase +from openerp.modules.module import get_module_resource + +class TestOfxFile(TransactionCase): + """Tests for import bank statement ofx file format (account.bank.statement.import) + """ + + def setUp(self): + super(TestOfxFile, self).setUp() + self.statement_import_model = self.registry('account.bank.statement.import') + self.bank_statement_model = self.registry('account.bank.statement') + + def test_ofx_file_import(self): + try: + from ofxparse import OfxParser as ofxparser + except ImportError: + #the Python library isn't installed on the server, the OFX import is unavailable and the test cannot be run + 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 = 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]) + 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) + self.assertEquals(bank_st_record.balance_end_real, 1796.56) diff --git a/account_bank_statement_import_qif/__init__.py b/account_bank_statement_import_qif/__init__.py new file mode 100644 index 0000000..befbd0e --- /dev/null +++ b/account_bank_statement_import_qif/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- + +import account_bank_statement_import_qif + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/account_bank_statement_import_qif/__openerp__.py b/account_bank_statement_import_qif/__openerp__.py new file mode 100644 index 0000000..3fe1eb2 --- /dev/null +++ b/account_bank_statement_import_qif/__openerp__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +{ + 'name': 'Import QIF Bank Statement', + 'version': '1.0', + 'author': 'OpenERP SA', + 'description': ''' +Module to import QIF bank statements. +====================================== + +This module allows you to import the machine readable QIF Files in Odoo: they are parsed and stored in human readable format in +Accounting \ Bank and Cash \ Bank Statements. + +Bank Statements may be generated containing a subset of the QIF information (only those transaction lines that are required for the +creation of the Financial Accounting records). + +Backported from Odoo 9.0 +''', + 'images' : [], + 'depends': ['account_bank_statement_import'], + 'demo': [], + 'data': [], + 'auto_install': False, + 'installable': True, +} + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/account_bank_statement_import_qif/account_bank_statement_import_qif.py b/account_bank_statement_import_qif/account_bank_statement_import_qif.py new file mode 100644 index 0000000..a253cb6 --- /dev/null +++ b/account_bank_statement_import_qif/account_bank_statement_import_qif.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- + +import dateutil.parser +import base64 +from tempfile import TemporaryFile + +from openerp.tools.translate import _ +from openerp.osv import osv + +from openerp.addons.account_bank_statement_import import account_bank_statement_import as ibs + +ibs.add_file_type(('qif', 'QIF')) + +class account_bank_statement_import(osv.TransientModel): + _inherit = "account.bank.statement.import" + + def process_qif(self, cr, uid, data_file, journal_id=False, context=None): + """ Import a file in the .QIF format""" + try: + fileobj = TemporaryFile('wb+') + fileobj.write(base64.b64decode(data_file)) + fileobj.seek(0) + file_data = "" + for line in fileobj.readlines(): + file_data += line + fileobj.close() + if '\r' in file_data: + data_list = file_data.split('\r') + else: + data_list = file_data.split('\n') + header = data_list[0].strip() + header = header.split(":")[1] + except: + raise osv.except_osv(_('Import Error!'), _('Please check QIF file format is proper or not.')) + line_ids = [] + vals_line = {} + total = 0 + if header == "Bank": + vals_bank_statement = {} + for line in data_list: + line = line.strip() + if not line: + continue + if line[0] == 'D': # date of transaction + vals_line['date'] = dateutil.parser.parse(line[1:], fuzzy=True).date() + if vals_line.get('date') and not vals_bank_statement.get('period_id'): + period_ids = self.pool.get('account.period').find(cr, uid, vals_line['date'], context=context) + vals_bank_statement.update({'period_id': period_ids and period_ids[0] or False}) + elif line[0] == 'T': # Total amount + total += float(line[1:].replace(',', '')) + vals_line['amount'] = float(line[1:].replace(',', '')) + elif line[0] == 'N': # Check number + vals_line['ref'] = line[1:] + elif line[0] == 'P': # Payee + bank_account_id, partner_id = self._detect_partner(cr, uid, line[1:], identifying_field='owner_name', context=context) + vals_line['partner_id'] = partner_id + vals_line['bank_account_id'] = bank_account_id + vals_line['name'] = 'name' in vals_line and line[1:] + ': ' + vals_line['name'] or line[1:] + elif line[0] == 'M': # Memo + vals_line['name'] = 'name' in vals_line and vals_line['name'] + ': ' + line[1:] or line[1:] + elif line[0] == '^': # end of item + line_ids.append((0, 0, vals_line)) + vals_line = {} + elif line[0] == '\n': + line_ids = [] + else: + pass + else: + raise osv.except_osv(_('Error!'), _('Cannot support this Format !Type:%s.') % (header,)) + vals_bank_statement.update({'balance_end_real': total, + 'line_ids': line_ids, + 'journal_id': journal_id}) + return [vals_bank_statement] + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/account_bank_statement_import_qif/test_qif_file/test_qif.qif b/account_bank_statement_import_qif/test_qif_file/test_qif.qif new file mode 100644 index 0000000..5388e6d --- /dev/null +++ b/account_bank_statement_import_qif/test_qif_file/test_qif.qif @@ -0,0 +1,21 @@ +!Type:Bank +D8/12/13 +T-1,000.00 +PDelta PC +^ +D8/15/13 +T-75.46 +PWalts Drugs +^ +D3/3/13 +T-379.00 +PEpic Technologies +^ +D3/4/13 +T-20.28 +PYOUR LOCAL SUPERMARKET +^ +D3/3/13 +T-421.35 +PSPRINGFIELD WATER UTILITY +^ diff --git a/account_bank_statement_import_qif/tests/__init__.py b/account_bank_statement_import_qif/tests/__init__.py new file mode 100644 index 0000000..05b23c4 --- /dev/null +++ b/account_bank_statement_import_qif/tests/__init__.py @@ -0,0 +1,5 @@ +from . import test_import_bank_statement +checks = [ + test_import_bank_statement +] + diff --git a/account_bank_statement_import_qif/tests/test_import_bank_statement.py b/account_bank_statement_import_qif/tests/test_import_bank_statement.py new file mode 100644 index 0000000..0f9ef4d --- /dev/null +++ b/account_bank_statement_import_qif/tests/test_import_bank_statement.py @@ -0,0 +1,27 @@ +from openerp.tests.common import TransactionCase +from openerp.modules.module import get_module_resource + +class TestQifFile(TransactionCase): + """Tests for import bank statement qif file format (account.bank.statement.import) + """ + + def setUp(self): + super(TestQifFile, self).setUp() + self.statement_import_model = self.registry('account.bank.statement.import') + self.bank_statement_model = self.registry('account.bank.statement') + self.bank_statement_line_model = self.registry('account.bank.statement.line') + + def test_qif_file_import(self): + from openerp.tools import float_compare + cr, uid = self.cr, self.uid + qif_file_path = get_module_resource('account_bank_statement_import_qif', 'test_qif_file', 'test_qif.qif') + qif_file = open(qif_file_path, 'rb').read().encode('base64') + bank_statement_id = self.statement_import_model.create(cr, uid, dict( + file_type='qif', + data_file=qif_file, + )) + self.statement_import_model.parse_file(cr, uid, [bank_statement_id]) + line_id = self.bank_statement_line_model.search(cr, uid, [('name', '=', 'YOUR LOCAL SUPERMARKET')])[0] + statement_id = self.bank_statement_line_model.browse(cr, uid, line_id).statement_id.id + bank_st_record = self.bank_statement_model.browse(cr, uid, statement_id) + assert float_compare(bank_st_record.balance_end_real, -1896.09, 2) == 0