Merge branch 'master' of https://github.com/odoo/odoo
[odoo/odoo.git] / addons / account_bank_statement_import / account_bank_statement_import.py
1 # -*- coding: utf-8 -*-
2
3 from openerp.osv import fields, osv
4 from openerp.tools.translate import _
5
6 import logging
7 _logger = logging.getLogger(__name__)
8
9 _IMPORT_FILE_TYPE = [('none', _('No Import Format Available'))]
10
11 def add_file_type(selection_value):
12     global _IMPORT_FILE_TYPE
13     if _IMPORT_FILE_TYPE[0][0] == 'none':
14         _IMPORT_FILE_TYPE = [selection_value]
15     else:
16         _IMPORT_FILE_TYPE.append(selection_value)
17
18 class account_bank_statement_import(osv.TransientModel):
19     _name = 'account.bank.statement.import'
20     _description = 'Import Bank Statement'
21
22     def _get_import_file_type(self, cr, uid, context=None):
23         return _IMPORT_FILE_TYPE
24
25     _columns = {
26         'data_file': fields.binary('Bank Statement File', required=True, help='Get you bank statements in electronic format from your bank and select them here.'),
27         'file_type': fields.selection(_get_import_file_type, 'File Type', required=True),
28         'journal_id': fields.many2one('account.journal', 'Journal', required=True, help="The journal for which the bank statements will be created"),
29     }
30
31     def _get_first_file_type(self, cr, uid, context=None):
32         return self._get_import_file_type(cr, uid, context=context)[0][0]
33
34     def _get_default_journal(self, cr, uid, context=None):
35         company_id = self.pool.get('res.company')._company_default_get(cr, uid, 'account.bank.statement', context=context)
36         journal_ids = self.pool.get('account.journal').search(cr, uid, [('type', '=', 'bank'), ('company_id', '=', company_id)], context=context)
37         return journal_ids and journal_ids[0] or False
38
39     _defaults = {
40         'file_type': _get_first_file_type,
41         'journal_id': _get_default_journal,
42     }
43
44     def _detect_partner(self, cr, uid, identifying_string, identifying_field='acc_number', context=None):
45         """Try to find a bank account and its related partner for the given 'identifying_string', looking on the field 'identifying_field'.
46
47            :param identifying_string: varchar
48            :param identifying_field: varchar corresponding to the name of a field of res.partner.bank
49            :returns: tuple(ID of the bank account found or False, ID of the partner for the bank account found or False)
50         """
51         partner_id = False
52         bank_account_id = False
53         if identifying_string:
54             ids = self.pool.get('res.partner.bank').search(cr, uid, [(identifying_field, '=', identifying_string)], context=context)
55             if ids:
56                 bank_account_id = ids[0]
57                 partner_id = self.pool.get('res.partner.bank').browse(cr, uid, bank_account_id, context=context).partner_id.id
58             else:
59                 #create the bank account, not linked to any partner. The reconciliation will link the partner manually
60                 #chosen at the bank statement final confirmation time.
61                 try:
62                     type_model, type_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'bank_normal')
63                     type_id = self.pool.get('res.partner.bank.type').browse(cr, uid, type_id, context=context)
64                     bank_code = type_id.code
65                 except ValueError:
66                     bank_code = 'bank'
67                 acc_number = identifying_field == 'acc_number' and identifying_string or _('Undefined')
68                 bank_account_vals = {
69                     'acc_number': acc_number,
70                     'state': bank_code,
71                 }
72                 bank_account_vals[identifying_field] = identifying_string
73                 bank_account_id = self.pool.get('res.partner.bank').create(cr, uid, bank_account_vals, context=context)
74         return bank_account_id, partner_id
75
76     def import_bank_statement(self, cr, uid, bank_statement_vals=False, context=None):
77         """ 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"""
78         statement_ids = []
79         for vals in bank_statement_vals:
80             statement_ids.append(self.pool.get('account.bank.statement').create(cr, uid, vals, context=context))
81         return statement_ids
82
83     def process_none(self, cr, uid, data_file, journal_id=False, context=None):
84         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.'))
85
86     def parse_file(self, cr, uid, ids, context=None):
87         """ Process the file chosen in the wizard and returns a list view of the imported bank statements"""
88         data = self.browse(cr, uid, ids[0], context=context)
89         vals = getattr(self, "process_%s" % data.file_type)(cr, uid, data.data_file, data.journal_id.id, context=context)
90         statement_ids = self.import_bank_statement(cr, uid, vals, context=context)
91         model, action_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'action_bank_statement_tree')
92         action = self.pool[model].read(cr, uid, action_id, context=context)
93         action['domain'] = "[('id', 'in', [" + ', '.join(map(str, statement_ids)) + "])]"
94         return action
95
96 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: