From 327cb95867ba497a9ceff2097aeb93faad3bb1cd Mon Sep 17 00:00:00 2001 From: qdp-odoo Date: Wed, 14 Jan 2015 18:31:01 +0100 Subject: [PATCH] [MERGE] merged the feature branch that improves the import of bank statements. PR 3718 --- addons/account/account.py | 79 ++-- addons/account/company.py | 3 + addons/account/company_view.xml | 4 + addons/account/demo/account_minimal.xml | 4 + .../account_bank_statement_reconciliation.css | 8 + ...account_bank_statement_reconciliation.scss | 14 + .../account/static/src/js/account_widgets.js | 32 ++ .../account_bank_statement_reconciliation.xml | 15 + .../__openerp__.py | 1 + .../account_bank_statement_import.py | 311 ++++++++++++---- .../account_bank_statement_import_view.xml | 22 +- .../static/description/icon_src.svg | 178 +++++++++ .../__openerp__.py | 4 +- .../account_bank_statement_import_ofx.py | 63 ++-- .../static/description/icon.png | Bin 0 -> 6282 bytes .../tests/test_import_bank_statement.py | 7 +- .../__openerp__.py | 13 +- .../account_bank_statement_import_qif.py | 73 ++-- ...account_bank_statement_import_qif_view.xml | 24 ++ .../static/description/icon.png | Bin 0 -> 6146 bytes .../tests/test_import_bank_statement.py | 10 +- addons/account_voucher/test/case1_usd_usd.yml | 10 +- .../l10n_be_coda/static/description/icon.png | Bin 0 -> 6370 bytes .../Ontvangen_CODA.2013-01-11-18.59.15.txt | 8 +- .../tests/test_import_bank_statement.py | 51 +-- .../wizard/account_coda_import.py | 93 ++--- openerp/addons/base/base_data.xml | 2 +- openerp/addons/base/res/res_currency.py | 2 +- openerp/addons/base/res/res_currency_data.xml | 348 +++++++++--------- openerp/addons/base/res/res_users.py | 3 + 30 files changed, 915 insertions(+), 467 deletions(-) create mode 100644 addons/account_bank_statement_import/static/description/icon_src.svg create mode 100644 addons/account_bank_statement_import_ofx/static/description/icon.png create mode 100644 addons/account_bank_statement_import_qif/account_bank_statement_import_qif_view.xml create mode 100644 addons/account_bank_statement_import_qif/static/description/icon.png create mode 100644 addons/l10n_be_coda/static/description/icon.png diff --git a/addons/account/account.py b/addons/account/account.py index 358518faebb..32c65830cdf 100644 --- a/addons/account/account.py +++ b/addons/account/account.py @@ -719,7 +719,6 @@ class account_account(osv.osv): self._check_moves(cr, uid, ids, "unlink", context=context) return super(account_account, self).unlink(cr, uid, ids, context=context) - class account_journal(osv.osv): _name = "account.journal" _description = "Journal" @@ -3399,7 +3398,7 @@ class wizard_multi_charts_accounts(osv.osv_memory): obj_wizard = self.browse(cr, uid, ids[0]) company_id = obj_wizard.company_id.id - self.pool.get('res.company').write(cr, uid, [company_id], {'currency_id': obj_wizard.currency_id.id}) + self.pool.get('res.company').write(cr, uid, [company_id], {'currency_id': obj_wizard.currency_id.id, 'accounts_code_digits': obj_wizard.code_digits}, context=context) # When we install the CoA of first company, set the currency to price types and pricelists if company_id==1: @@ -3429,80 +3428,90 @@ class wizard_multi_charts_accounts(osv.osv_memory): self._create_bank_journals_from_o2m(cr, uid, obj_wizard, company_id, acc_template_ref, context=context) return {} - def _prepare_bank_journal(self, cr, uid, line, current_num, default_account_id, company_id, context=None): + def _prepare_bank_journal(self, cr, uid, company, line, default_account_id, context=None): ''' This function prepares the value to use for the creation of a bank journal created through the wizard of generating COA from templates. :param line: dictionary containing the values encoded by the user related to his bank account - :param current_num: integer corresponding to a counter of the already created bank journals through this wizard. :param default_account_id: id of the default debit.credit account created before for this journal. :param company_id: id of the company for which the wizard is running :return: mapping of field names and values :rtype: dict ''' - obj_data = self.pool.get('ir.model.data') obj_journal = self.pool.get('account.journal') - - # we need to loop again to find next number for journal code - # because we can't rely on the value current_num as, - # its possible that we already have bank journals created (e.g. by the creation of res.partner.bank) - # and the next number for account code might have been already used before for journal - for num in xrange(current_num, 100): + # we need to loop to find next number for journal code + for num in xrange(1, 100): # journal_code has a maximal size of 5, hence we can enforce the boundary num < 100 journal_code = _('BNK')[:3] + str(num) - ids = obj_journal.search(cr, uid, [('code', '=', journal_code), ('company_id', '=', company_id)], context=context) + ids = obj_journal.search(cr, uid, [('code', '=', journal_code), ('company_id', '=', company.id)], context=context) if not ids: break else: raise osv.except_osv(_('Error!'), _('Cannot generate an unused journal code.')) - vals = { + return { 'name': line['acc_name'], 'code': journal_code, 'type': line['account_type'] == 'cash' and 'cash' or 'bank', - 'company_id': company_id, + 'company_id': company.id, 'analytic_journal_id': False, - 'currency': False, + 'currency': line['currency_id'] or False, 'default_credit_account_id': default_account_id, 'default_debit_account_id': default_account_id, - } - if line['currency_id']: - vals['currency'] = line['currency_id'] - - return vals + } - def _prepare_bank_account(self, cr, uid, line, new_code, acc_template_ref, ref_acc_bank, company_id, context=None): + def _prepare_bank_account(self, cr, uid, company, line, acc_template_ref=False, ref_acc_bank=False, context=None): ''' This function prepares the value to use for the creation of the default debit and credit accounts of a bank journal created through the wizard of generating COA from templates. + :param company: company for which the wizard is running :param line: dictionary containing the values encoded by the user related to his bank account - :param new_code: integer corresponding to the next available number to use as account code :param acc_template_ref: the dictionary containing the mapping between the ids of account templates and the ids of the accounts that have been generated from them. :param ref_acc_bank: browse record of the account template set as root of all bank accounts for the chosen template - :param company_id: id of the company for which the wizard is running :return: mapping of field names and values :rtype: dict ''' obj_data = self.pool.get('ir.model.data') + obj_acc = self.pool.get('account.account') + + # Seek the next available number for the account code + code_digits = company.accounts_code_digits or 0 + bank_account_code_char = company.bank_account_code_char or '' + for num in xrange(1, 100): + new_code = str(bank_account_code_char.ljust(code_digits - 1, '0')) + str(num) + ids = obj_acc.search(cr, uid, [('code', '=', new_code), ('company_id', '=', company.id)]) + if not ids: + break + else: + raise osv.except_osv(_('Error!'), _('Cannot generate an unused account code.')) # Get the id of the user types fr-or cash and bank tmp = obj_data.get_object_reference(cr, uid, 'account', 'data_account_type_cash') cash_type = tmp and tmp[1] or False tmp = obj_data.get_object_reference(cr, uid, 'account', 'data_account_type_bank') bank_type = tmp and tmp[1] or False + parent_id = False + if acc_template_ref: + parent_id = acc_template_ref[ref_acc_bank.id] + else: + tmp = self.pool.get('account.account').search(cr, uid, [('code', '=', company.bank_account_code_char)], context=context) + if tmp: + parent_id = tmp[0] + return { 'name': line['acc_name'], - 'currency_id': line['currency_id'], + 'currency_id': line['currency_id'] or False, 'code': new_code, 'type': 'liquidity', 'user_type': line['account_type'] == 'cash' and cash_type or bank_type, - 'parent_id': acc_template_ref[ref_acc_bank.id] or False, - 'company_id': company_id, + # TODO: refactor when parent_id removed from account.account + 'parent_id': parent_id, + 'company_id': company.id, } def _create_bank_journals_from_o2m(self, cr, uid, obj_wizard, company_id, acc_template_ref, context=None): @@ -3518,7 +3527,7 @@ class wizard_multi_charts_accounts(osv.osv_memory): ''' obj_acc = self.pool.get('account.account') obj_journal = self.pool.get('account.journal') - code_digits = obj_wizard.code_digits + company = self.pool.get('res.company').browse(cr, uid, company_id, context=context) # Build a list with all the data to process journal_data = [] @@ -3530,28 +3539,20 @@ class wizard_multi_charts_accounts(osv.osv_memory): 'currency_id': acc.currency_id.id, } journal_data.append(vals) + ref_acc_bank = obj_wizard.chart_template_id.bank_account_view_id if journal_data and not ref_acc_bank.code: raise osv.except_osv(_('Configuration Error!'), _('You have to set a code for the bank account defined on the selected chart of accounts.')) + self.pool.get('res.company').write(cr, uid, [company.id], {'bank_account_code_char': ref_acc_bank.code}, context=context) - current_num = 1 for line in journal_data: - # Seek the next available number for the account code - while True: - new_code = str(ref_acc_bank.code.ljust(code_digits-len(str(current_num)), '0')) + str(current_num) - ids = obj_acc.search(cr, uid, [('code', '=', new_code), ('company_id', '=', company_id)]) - if not ids: - break - else: - current_num += 1 # Create the default debit/credit accounts for this bank journal - vals = self._prepare_bank_account(cr, uid, line, new_code, acc_template_ref, ref_acc_bank, company_id, context=context) + vals = self._prepare_bank_account(cr, uid, company, line, acc_template_ref, ref_acc_bank, context=context) default_account_id = obj_acc.create(cr, uid, vals, context=context) #create the bank journal - vals_journal = self._prepare_bank_journal(cr, uid, line, current_num, default_account_id, company_id, context=context) + vals_journal = self._prepare_bank_journal(cr, uid, company, line, default_account_id, context=context) obj_journal.create(cr, uid, vals_journal) - current_num += 1 return True diff --git a/addons/account/company.py b/addons/account/company.py index 3b2940f8f4b..1a423b4e8b6 100644 --- a/addons/account/company.py +++ b/addons/account/company.py @@ -25,6 +25,8 @@ class res_company(osv.osv): _inherit = "res.company" _columns = { 'expects_chart_of_accounts': fields.boolean('Expects a Chart of Accounts'), + 'bank_account_code_char': fields.char('Code of the main bank account'), + 'accounts_code_digits': fields.integer('Number of digits in an account code'), 'tax_calculation_rounding_method': fields.selection([ ('round_per_line', 'Round per Line'), ('round_globally', 'Round Globally'), @@ -37,6 +39,7 @@ class res_company(osv.osv): _defaults = { 'expects_chart_of_accounts': True, 'tax_calculation_rounding_method': 'round_per_line', + 'accounts_code_digits': 6, 'overdue_msg': '''Dear Sir/Madam, Our records indicate that some payments on your account are still due. Please find details below. diff --git a/addons/account/company_view.xml b/addons/account/company_view.xml index d5ab6d7753a..2b2ce41c800 100644 --- a/addons/account/company_view.xml +++ b/addons/account/company_view.xml @@ -12,6 +12,10 @@ + + + + diff --git a/addons/account/demo/account_minimal.xml b/addons/account/demo/account_minimal.xml index 54ced4602c8..03048fc3173 100644 --- a/addons/account/demo/account_minimal.xml +++ b/addons/account/demo/account_minimal.xml @@ -1,6 +1,10 @@ + + X1100 + 7 + diff --git a/addons/account/static/src/css/account_bank_statement_reconciliation.css b/addons/account/static/src/css/account_bank_statement_reconciliation.css index cd217914e68..fe1000668f7 100644 --- a/addons/account/static/src/css/account_bank_statement_reconciliation.css +++ b/addons/account/static/src/css/account_bank_statement_reconciliation.css @@ -55,6 +55,14 @@ top: 2px; z-index: 10; text-shadow: -1px -1px 0 #f5f5f5, 1px -1px 0 #f5f5f5, -1px 1px 0 #f5f5f5, 1px 1px 0 #f5f5f5; } + .openerp .oe_bank_statement_reconciliation .notification_area { + margin: 0 15px; } + .openerp .oe_bank_statement_reconciliation .interface_options { + margin-bottom: 25px; + padding-left: 15px; } + .openerp .oe_bank_statement_reconciliation .interface_options .oe_radio_input { + border: none; + display: inline; } .openerp .oe_bank_statement_reconciliation .protip { margin: 0; position: absolute; diff --git a/addons/account/static/src/css/account_bank_statement_reconciliation.scss b/addons/account/static/src/css/account_bank_statement_reconciliation.scss index 1fcdeac3389..76c006d1ab5 100644 --- a/addons/account/static/src/css/account_bank_statement_reconciliation.scss +++ b/addons/account/static/src/css/account_bank_statement_reconciliation.scss @@ -91,6 +91,20 @@ $aestetic_animation_speed: 300ms; } } + .notification_area { + margin: 0 $actionColWidth; + } + + .interface_options { + margin-bottom: 25px; + padding-left: $actionColWidth; + + .oe_radio_input { + border: none; + display: inline; + } + } + .protip { margin: 0; position: absolute; diff --git a/addons/account/static/src/js/account_widgets.js b/addons/account/static/src/js/account_widgets.js index 7767ceb7a07..a1947575be3 100644 --- a/addons/account/static/src/js/account_widgets.js +++ b/addons/account/static/src/js/account_widgets.js @@ -11,6 +11,7 @@ openerp.account = function (instance) { className: 'oe_bank_statement_reconciliation', events: { + "click *[rel='do_action']": "doActionClickHandler", "click .statement_name span": "statementNameClickHandler", "keyup .change_statement_name_field": "changeStatementNameFieldHandler", "click .change_statement_name_button": "changeStatementButtonClickHandler", @@ -24,6 +25,7 @@ openerp.account = function (instance) { this.single_statement = this.statement_ids !== undefined && this.statement_ids.length === 1; this.multiple_statements = this.statement_ids !== undefined && this.statement_ids.length > 1; this.title = context.context.title || _t("Reconciliation"); + this.notifications = context.context.notifications || []; this.st_lines = []; this.last_displayed_reconciliation_index = undefined; // Flow control this.reconciled_lines = 0; // idem @@ -249,11 +251,41 @@ openerp.account = function (instance) { $.when.apply($, child_promises).then(function(){ self.$(".reconciliation_lines_container").animate({opacity: 1}, self.aestetic_animation_speed); self.getChildren()[0].set("mode", "match"); + if (self.notifications) { + self.displayNotifications(self.notifications); + } }); }); }); }, + doActionClickHandler: function(e) { + var name = e.currentTarget.dataset.action_nam; + var model = e.currentTarget.dataset.model; + var ids = e.currentTarget.dataset.ids.split(",").map(Number); + this.action_manager.do_action({ + name: name, + res_model: model, + domain: [['id', 'in', ids]], + views: [[false, 'list'], [false, 'form']], + type: 'ir.actions.act_window', + view_type: "list", + view_mode: "list" + }); + }, + + displayNotifications: function(notifications, speed) { + speed = speed === undefined ? this.aestetic_animation_speed : speed; + for (var i=0; i
+

Good Job! Bank statements are fully reconciled.

This page displays all the bank transactions that are to be reconciled and provides with a neat interface to do so.

@@ -31,6 +32,7 @@
+

Tip : Hit ctrl-enter to validate the whole sheet.

@@ -57,6 +59,19 @@ + +
+ + + + / + +
+
+ diff --git a/addons/account_bank_statement_import/__openerp__.py b/addons/account_bank_statement_import/__openerp__.py index 4c9e5ccb69d..d074ea16901 100644 --- a/addons/account_bank_statement_import/__openerp__.py +++ b/addons/account_bank_statement_import/__openerp__.py @@ -1,6 +1,7 @@ # -*- encoding: utf-8 -*- { 'name': 'Account Bank Statement Import', + 'category' : 'Accounting & Finance', 'version': '1.0', 'author': 'OpenERP SA', 'depends': ['account'], diff --git a/addons/account_bank_statement_import/account_bank_statement_import.py b/addons/account_bank_statement_import/account_bank_statement_import.py index 2278ac2cd7a..f3e281ac7aa 100644 --- a/addons/account_bank_statement_import/account_bank_statement_import.py +++ b/addons/account_bank_statement_import/account_bank_statement_import.py @@ -1,94 +1,271 @@ # -*- coding: utf-8 -*- +import base64 + +from openerp import SUPERUSER_ID 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_line(osv.osv): + _inherit = "account.bank.statement.line" + + _columns = { + # Ensure transactions can be imported only once (if the import format provides unique transaction ids) + 'unique_import_id': fields.char('Import ID', readonly=True, copy=False), + } + + _sql_constraints = [ + ('unique_import_id', 'unique (unique_import_id)', 'A bank account transactions can be imported only once !') + ] + 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 import_file(self, cr, uid, ids, context=None): + """ Process the file chosen in the wizard, create bank statement(s) and go to reconciliation. """ + if context is None: + context = {} + #set the active_id in the context, so that any extension module could + #reuse the fields chosen in the wizard if needed (see .QIF for example) + context.update({'active_id': ids[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 + data_file = self.browse(cr, uid, ids[0], context=context).data_file - _defaults = { - 'file_type': _get_first_file_type, - 'journal_id': _get_default_journal, - } + # The appropriate implementation module returns the required data + currency_code, account_number, stmts_vals = self._parse_file(cr, uid, base64.b64decode(data_file), context=context) + # Check raw data + self._check_parsed_data(cr, uid, stmts_vals, context=context) + # Try to find the bank account and currency in odoo + currency_id, bank_account_id = self._find_additional_data(cr, uid, currency_code, account_number, context=context) + # Find or create the bank journal + journal_id = self._get_journal(cr, uid, currency_id, bank_account_id, account_number, context=context) + # Create the bank account if not already existing + if not bank_account_id and account_number: + self._create_bank_account(cr, uid, account_number, journal_id=journal_id, partner_id=uid, context=context) + # Prepare statement data to be used for bank statements creation + stmts_vals = self._complete_stmts_vals(cr, uid, stmts_vals, journal_id, account_number, context=context) + # Create the bank statements + statement_ids, notifications = self._create_bank_statements(cr, uid, stmts_vals, context=context) - 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'. + # Finally dispatch to reconciliation interface + model, action_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'action_bank_reconcile_bank_statements') + action = self.pool[model].browse(cr, uid, action_id, context=context) + return { + 'name': action.name, + 'tag': action.tag, + 'context': { + 'statement_ids': statement_ids, + 'notifications': notifications + }, + 'type': 'ir.actions.client', + } - :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) + def _parse_file(self, cr, uid, data_file=None, context=None): + """ Each module adding a file support must extends this method. It processes the file if it can, returns super otherwise, resulting in a chain of responsability. + This method parses the given file and returns the data required by the bank statement import process, as specified below. + rtype: triplet (if a value can't be retrieved, use None) + - currency code: string (e.g: 'EUR') + The ISO 4217 currency code, case insensitive + - account number: string (e.g: 'BE1234567890') + The number of the bank account which the statement belongs to + - bank statements data: list of dict containing (optional items marked by o) : + - 'name': string (e.g: '000000123') + - 'date': date (e.g: 2013-06-26) + -o 'balance_start': float (e.g: 8368.56) + -o 'balance_end_real': float (e.g: 8888.88) + - 'transactions': list of dict containing : + - 'name': string (e.g: 'KBC-INVESTERINGSKREDIET 787-5562831-01') + - 'date': date + - 'amount': float + - 'unique_import_id': string + -o 'account_number': string + Will be used to find/create the res.partner.bank in odoo + -o 'note': string + -o 'partner_name': string + -o 'ref': string """ - 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 + raise osv.except_osv(_('Error'), _('Could not make sense of the given file.\nDid you install the module to support this type of file ?')) + + def _check_parsed_data(self, cr, uid, stmts_vals, context=None): + """ Basic and structural verifications """ + if len(stmts_vals) == 0: + raise osv.except_osv(_('Error'), _('This file doesn\'t contain any statement.')) + + no_st_line = True + for vals in stmts_vals: + if vals['transactions'] and len(vals['transactions']) > 0: + no_st_line = False + break + if no_st_line: + raise osv.except_osv(_('Error'), _('This file doesn\'t contain any transaction.')) + + def _find_additional_data(self, cr, uid, currency_code, account_number, context=None): + """ Get the res.currency ID and the res.partner.bank ID """ + currency_id = False # So if no currency_code is provided, we'll use the company currency + if currency_code: + currency_ids = self.pool.get('res.currency').search(cr, uid, [('name', '=ilike', currency_code)], context=context) + company_currency_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id + if currency_ids: + if currency_ids[0] != company_currency_id: + currency_id = currency_ids[0] + + bank_account_id = None + if account_number and len(account_number) > 4: + account_number = account_number.replace(' ', '').replace('-', '') + cr.execute("select id from res_partner_bank where replace(replace(acc_number,' ',''),'-','') = %s", (account_number,)) + bank_account_ids = [id[0] for id in cr.fetchall()] + bank_account_ids = self.pool.get('res.partner.bank').search(cr, uid, [('id', 'in', bank_account_ids)], context=context) + if bank_account_ids: + bank_account_id = bank_account_ids[0] + + return currency_id, bank_account_id + + def _get_journal(self, cr, uid, currency_id, bank_account_id, account_number, context=None): + """ Find or create the journal """ + bank_pool = self.pool.get('res.partner.bank') + + # Find the journal from context or bank account + journal_id = context.get('journal_id') + if bank_account_id: + bank_account = bank_pool.browse(cr, uid, bank_account_id, context=context) + if journal_id: + if bank_account.journal_id.id and bank_account.journal_id.id != journal_id: + raise osv.except_osv(_('Error'), _('The account of this statement is linked to another journal.')) + if not bank_account.journal_id.id: + bank_pool.write(cr, uid, [bank_account_id], {'journal_id': journal_id}, context=context) 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 + if bank_account.journal_id.id: + journal_id = bank_account.journal_id.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""" + # If importing into an existing journal, its currency must be the same as the bank statement + if journal_id: + journal_currency_id = self.pool.get('account.journal').browse(cr, uid, journal_id, context=context).currency.id + if currency_id and currency_id != journal_currency_id: + raise osv.except_osv(_('Error'), _('The currency of the bank statement is not the same as the currency of the journal !')) + + # If there is no journal, create one (and its account) + if not journal_id and account_number: + journal_id = self._create_journal(cr, uid, currency_id, account_number, context=context) + if bank_account_id: + bank_pool.write(cr, uid, [bank_account_id], {'journal_id': journal_id}, context=context) + + # If we couldn't find/create a journal, everything is lost + if not journal_id: + raise osv.except_osv(_('Error'), _('Cannot find in which journal import this statement. Please manually select a journal.')) + + return journal_id + + def _create_journal(self, cr, uid, currency_id, account_number, context=None): + """ Create a journal and its account """ + wmca_pool = self.pool.get('wizard.multi.charts.accounts') + company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id + + vals_account = {'currency_id': currency_id, 'acc_name': account_number, 'account_type': 'bank', 'currency_id': currency_id} + vals_account = wmca_pool._prepare_bank_account(cr, uid, company, vals_account, context=context) + account_id = self.pool.get('account.account').create(cr, uid, vals_account, context=context) + + vals_journal = {'currency_id': currency_id, 'acc_name': _('Bank') + ' ' + account_number, 'account_type': 'bank'} + vals_journal = wmca_pool._prepare_bank_journal(cr, uid, company, vals_journal, account_id, context=context) + return self.pool.get('account.journal').create(cr, uid, vals_journal, context=context) + + def _create_bank_account(self, cr, uid, account_number, journal_id=False, partner_id=False, context=None): + 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' + account_number = account_number.replace(' ', '').replace('-', '') + vals_acc = { + 'acc_number': account_number, + 'state': bank_code, + } + # Odoo users bank accounts (which we import statement from) have company_id and journal_id set + # while 'counterpart' bank accounts (from which statement transactions originate) don't. + # Warning : if company_id is set, the method post_write of class bank will create a journal + if journal_id: + vals_acc['partner_id'] = uid + vals_acc['journal_id'] = journal_id + vals_acc['company_id'] = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id + + return self.pool.get('res.partner.bank').create(cr, uid, vals_acc, context=context) + + def _complete_stmts_vals(self, cr, uid, stmts_vals, journal_id, account_number, context=None): + for st_vals in stmts_vals: + st_vals['journal_id'] = journal_id + + for line_vals in st_vals['transactions']: + unique_import_id = line_vals.get('unique_import_id', False) + if unique_import_id: + line_vals['unique_import_id'] = (account_number and account_number + '-' or '') + unique_import_id + + if not 'bank_account_id' in line_vals or not line_vals['bank_account_id']: + # Find the partner and his bank account or create the bank account. The partner selected during the + # reconciliation process will be linked to the bank when the statement is closed. + partner_id = False + bank_account_id = False + identifying_string = line_vals.get('account_number', False) + if identifying_string: + ids = self.pool.get('res.partner.bank').search(cr, uid, [('acc_number', '=', 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: + bank_account_id = self._create_bank_account(cr, uid, identifying_string, context=context) + line_vals['partner_id'] = partner_id + line_vals['bank_account_id'] = bank_account_id + + return stmts_vals + + def _create_bank_statements(self, cr, uid, stmts_vals, context=None): + """ Create new bank statements from imported values, filtering out already imported transactions, and returns data used by the reconciliation widget """ + bs_obj = self.pool.get('account.bank.statement') + bsl_obj = self.pool.get('account.bank.statement.line') + + # Filter out already imported transactions and create statements 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 + ignored_statement_lines_import_ids = [] + for st_vals in stmts_vals: + filtered_st_lines = [] + for line_vals in st_vals['transactions']: + if not 'unique_import_id' in line_vals \ + or not line_vals['unique_import_id'] \ + or not bool(bsl_obj.search(cr, SUPERUSER_ID, [('unique_import_id', '=', line_vals['unique_import_id'])], limit=1, context=context)): + filtered_st_lines.append(line_vals) + else: + ignored_statement_lines_import_ids.append(line_vals['unique_import_id']) + if len(filtered_st_lines) > 0: + # Remove values that won't be used to create records + st_vals.pop('transactions', None) + for line_vals in filtered_st_lines: + line_vals.pop('account_number', None) + # Create the satement + st_vals['line_ids'] = [[0, False, line] for line in filtered_st_lines] + statement_ids.append(bs_obj.create(cr, uid, st_vals, context=context)) + if len(statement_ids) == 0: + raise osv.except_osv(_('Error'), _('You have already imported that file.')) - 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.')) + # Prepare import feedback + notifications = [] + num_ignored = len(ignored_statement_lines_import_ids) + if num_ignored > 0: + notifications += [{ + 'type': 'warning', + 'message': _("%d transactions had already been imported and were ignored.") % num_ignored if num_ignored > 1 else _("1 transaction had already been imported and was ignored."), + 'details': { + 'name': _('Already imported items'), + 'model': 'account.bank.statement.line', + 'ids': bsl_obj.search(cr, uid, [('unique_import_id', 'in', ignored_statement_lines_import_ids)], context=context) + } + }] - 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 + return statement_ids, notifications diff --git a/addons/account_bank_statement_import/account_bank_statement_import_view.xml b/addons/account_bank_statement_import/account_bank_statement_import_view.xml index c48371c7046..62ffc086d41 100644 --- a/addons/account_bank_statement_import/account_bank_statement_import_view.xml +++ b/addons/account_bank_statement_import/account_bank_statement_import_view.xml @@ -7,22 +7,14 @@ account.bank.statement.import1 -
- - - - - - - - How to import your bank statement in OpenERP. - - + + +

How to import your bank statement : +