You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
15 KiB

  1. # -*- coding: utf-8 -*-
  2. # noqa: This is a backport from Odoo. OCA has no control over style here.
  3. # flake8: noqa
  4. import base64
  5. from openerp import SUPERUSER_ID
  6. from openerp.osv import fields, osv
  7. from openerp.tools.translate import _
  8. from openerp.exceptions import Warning
  9. import logging
  10. _logger = logging.getLogger(__name__)
  11. class account_bank_statement_line(osv.osv):
  12. _inherit = "account.bank.statement.line"
  13. _columns = {
  14. # Ensure transactions can be imported only once (if the import format provides unique transaction ids)
  15. 'unique_import_id': fields.char('Import ID', readonly=True, copy=False),
  16. }
  17. _sql_constraints = [
  18. ('unique_import_id', 'unique (unique_import_id)', 'A bank account transactions can be imported only once !')
  19. ]
  20. class account_bank_statement_import(osv.TransientModel):
  21. _name = 'account.bank.statement.import'
  22. _description = 'Import Bank Statement'
  23. _columns = {
  24. 'data_file': fields.binary('Bank Statement File', required=True, help='Get you bank statements in electronic format from your bank and select them here.'),
  25. }
  26. def import_file(self, cr, uid, ids, context=None):
  27. """ Process the file chosen in the wizard, create bank statement(s) and go to reconciliation. """
  28. context = dict(context or {})
  29. #set the active_id in the context, so that any extension module could
  30. #reuse the fields chosen in the wizard if needed (see .QIF for example)
  31. context.update({'active_id': ids[0]})
  32. data_file = self.browse(cr, uid, ids[0], context=context).data_file
  33. # The appropriate implementation module returns the required data
  34. currency_code, account_number, stmts_vals = self._parse_file(cr, uid, base64.b64decode(data_file), context=context)
  35. # Check raw data
  36. self._check_parsed_data(cr, uid, stmts_vals, context=context)
  37. # Try to find the bank account and currency in odoo
  38. currency_id, bank_account_id = self._find_additional_data(cr, uid, currency_code, account_number, context=context)
  39. # Find or create the bank journal
  40. journal_id = self._get_journal(cr, uid, currency_id, bank_account_id, account_number, context=context)
  41. # Create the bank account if not already existing
  42. if not bank_account_id and account_number:
  43. self._create_bank_account(cr, uid, account_number, journal_id=journal_id, partner_id=uid, context=context)
  44. # Prepare statement data to be used for bank statements creation
  45. stmts_vals = self._complete_stmts_vals(cr, uid, stmts_vals, journal_id, account_number, context=context)
  46. # Create the bank statements
  47. statement_ids, notifications = self._create_bank_statements(cr, uid, stmts_vals, context=context)
  48. # Finally dispatch to reconciliation interface
  49. model, action_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'action_bank_reconcile_bank_statements')
  50. action = self.pool[model].browse(cr, uid, action_id, context=context)
  51. return {
  52. 'name': action.name,
  53. 'tag': action.tag,
  54. 'context': {
  55. 'statement_ids': statement_ids,
  56. 'notifications': notifications
  57. },
  58. 'type': 'ir.actions.client',
  59. }
  60. def _parse_file(self, cr, uid, data_file, context=None):
  61. """ 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.
  62. This method parses the given file and returns the data required by the bank statement import process, as specified below.
  63. rtype: triplet (if a value can't be retrieved, use None)
  64. - currency code: string (e.g: 'EUR')
  65. The ISO 4217 currency code, case insensitive
  66. - account number: string (e.g: 'BE1234567890')
  67. The number of the bank account which the statement belongs to
  68. - bank statements data: list of dict containing (optional items marked by o) :
  69. - 'name': string (e.g: '000000123')
  70. - 'date': date (e.g: 2013-06-26)
  71. -o 'balance_start': float (e.g: 8368.56)
  72. -o 'balance_end_real': float (e.g: 8888.88)
  73. - 'transactions': list of dict containing :
  74. - 'name': string (e.g: 'KBC-INVESTERINGSKREDIET 787-5562831-01')
  75. - 'date': date
  76. - 'amount': float
  77. - 'unique_import_id': string
  78. -o 'account_number': string
  79. Will be used to find/create the res.partner.bank in odoo
  80. -o 'note': string
  81. -o 'partner_name': string
  82. -o 'ref': string
  83. """
  84. raise Warning(_('Could not make sense of the given file.\nDid you install the module to support this type of file ?'))
  85. def _check_parsed_data(self, cr, uid, stmts_vals, context=None):
  86. """ Basic and structural verifications """
  87. if len(stmts_vals) == 0:
  88. raise Warning(_('This file doesn\'t contain any statement.'))
  89. no_st_line = True
  90. for vals in stmts_vals:
  91. if vals['transactions'] and len(vals['transactions']) > 0:
  92. no_st_line = False
  93. break
  94. if no_st_line:
  95. raise Warning(_('This file doesn\'t contain any transaction.'))
  96. def _find_additional_data(self, cr, uid, currency_code, account_number, context=None):
  97. """ Get the res.currency ID and the res.partner.bank ID """
  98. currency_id = False # So if no currency_code is provided, we'll use the company currency
  99. if currency_code:
  100. currency_ids = self.pool.get('res.currency').search(cr, uid, [('name', '=ilike', currency_code)], context=context)
  101. company_currency_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
  102. if currency_ids:
  103. if currency_ids[0] != company_currency_id:
  104. currency_id = currency_ids[0]
  105. bank_account_id = None
  106. if account_number and len(account_number) > 4:
  107. account_number = account_number.replace(' ', '').replace('-', '')
  108. cr.execute("select id from res_partner_bank where replace(replace(acc_number,' ',''),'-','') like %s and journal_id is not null", ('%' + account_number + '%',))
  109. bank_account_ids = [id[0] for id in cr.fetchall()]
  110. if bank_account_ids:
  111. bank_account_id = bank_account_ids[0]
  112. return currency_id, bank_account_id
  113. def _get_journal(self, cr, uid, currency_id, bank_account_id, account_number, context=None):
  114. """ Find or create the journal """
  115. if context is None:
  116. context = {}
  117. bank_pool = self.pool.get('res.partner.bank')
  118. # Find the journal from context or bank account
  119. journal_id = context.get('journal_id')
  120. if bank_account_id:
  121. bank_account = bank_pool.browse(cr, uid, bank_account_id, context=context)
  122. if journal_id:
  123. if bank_account.journal_id.id and bank_account.journal_id.id != journal_id:
  124. raise Warning(_('The account of this statement is linked to another journal.'))
  125. if not bank_account.journal_id.id:
  126. bank_pool.write(cr, uid, [bank_account_id], {'journal_id': journal_id}, context=context)
  127. else:
  128. if bank_account.journal_id.id:
  129. journal_id = bank_account.journal_id.id
  130. # If importing into an existing journal, its currency must be the same as the bank statement
  131. if journal_id:
  132. journal_currency_id = self.pool.get('account.journal').browse(cr, uid, journal_id, context=context).currency.id
  133. if currency_id and currency_id != journal_currency_id:
  134. raise Warning(_('The currency of the bank statement is not the same as the currency of the journal !'))
  135. # If there is no journal, create one (and its account)
  136. # I think it's too dangerous, so I disable that code by default -- Alexis de Lattre
  137. # -- Totally disabled, Ronald Portier
  138. # if context.get('allow_auto_create_journal') and not journal_id and account_number:
  139. # journal_id = self._create_journal(cr, uid, currency_id, account_number, context=context)
  140. # if bank_account_id:
  141. # bank_pool.write(cr, uid, [bank_account_id], {'journal_id': journal_id}, context=context)
  142. # If we couldn't find/create a journal, everything is lost
  143. if not journal_id:
  144. raise Warning(_('Cannot find in which journal import this statement. Please manually select a journal.'))
  145. return journal_id
  146. def _create_journal(self, cr, uid, currency_id, account_number, context=None):
  147. """ Create a journal and its account """
  148. wmca_pool = self.pool.get('wizard.multi.charts.accounts')
  149. company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
  150. vals_account = {'currency_id': currency_id, 'acc_name': account_number, 'account_type': 'bank'}
  151. vals_account = wmca_pool._prepare_bank_account(cr, uid, company, vals_account, context=context)
  152. account_id = self.pool.get('account.account').create(cr, uid, vals_account, context=context)
  153. vals_journal = {'currency_id': currency_id, 'acc_name': _('Bank') + ' ' + account_number, 'account_type': 'bank'}
  154. vals_journal = wmca_pool._prepare_bank_journal(cr, uid, company, vals_journal, account_id, context=context)
  155. return self.pool.get('account.journal').create(cr, uid, vals_journal, context=context)
  156. def _create_bank_account(self, cr, uid, account_number, journal_id=False, partner_id=False, context=None):
  157. try:
  158. type_model, type_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'bank_normal')
  159. type_id = self.pool.get('res.partner.bank.type').browse(cr, uid, type_id, context=context)
  160. bank_code = type_id.code
  161. except ValueError:
  162. bank_code = 'bank'
  163. account_number = account_number.replace(' ', '').replace('-', '')
  164. vals_acc = {
  165. 'acc_number': account_number,
  166. 'state': bank_code,
  167. }
  168. # Odoo users bank accounts (which we import statement from) have company_id and journal_id set
  169. # while 'counterpart' bank accounts (from which statement transactions originate) don't.
  170. # Warning : if company_id is set, the method post_write of class bank will create a journal
  171. if journal_id:
  172. vals_acc['partner_id'] = uid
  173. vals_acc['journal_id'] = journal_id
  174. vals_acc['company_id'] = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
  175. return self.pool.get('res.partner.bank').create(cr, uid, vals_acc, context=context)
  176. def _complete_stmts_vals(self, cr, uid, stmts_vals, journal_id, account_number, context=None):
  177. for st_vals in stmts_vals:
  178. st_vals['journal_id'] = journal_id
  179. for line_vals in st_vals['transactions']:
  180. unique_import_id = line_vals.get('unique_import_id', False)
  181. if unique_import_id:
  182. line_vals['unique_import_id'] = (account_number and account_number + '-' or '') + unique_import_id
  183. if not line_vals.get('partner_id') and not line_vals.get('bank_account_id'):
  184. # Find the partner and his bank account or create the bank account. The partner selected during the
  185. # reconciliation process will be linked to the bank when the statement is closed.
  186. partner_id = False
  187. bank_account_id = False
  188. identifying_string = line_vals.get('account_number', False)
  189. if identifying_string:
  190. identifying_string = identifying_string.replace(' ', '').replace('-', '')
  191. ids = self.pool.get('res.partner.bank').search(cr, uid, [('acc_number', '=', identifying_string)], context=context)
  192. if ids:
  193. bank_account_id = ids[0]
  194. partner_id = self.pool.get('res.partner.bank').browse(cr, uid, bank_account_id, context=context).partner_id.id
  195. else:
  196. bank_account_id = self._create_bank_account(cr, uid, identifying_string, context=context)
  197. line_vals['partner_id'] = partner_id
  198. line_vals['bank_account_id'] = bank_account_id
  199. return stmts_vals
  200. def _create_bank_statements(self, cr, uid, stmts_vals, context=None):
  201. """ Create new bank statements from imported values, filtering out already imported transactions, and returns data used by the reconciliation widget """
  202. bs_obj = self.pool.get('account.bank.statement')
  203. bsl_obj = self.pool.get('account.bank.statement.line')
  204. # Filter out already imported transactions and create statements
  205. statement_ids = []
  206. ignored_statement_lines_import_ids = []
  207. for st_vals in stmts_vals:
  208. filtered_st_lines = []
  209. for line_vals in st_vals['transactions']:
  210. if not 'unique_import_id' in line_vals \
  211. or not line_vals['unique_import_id'] \
  212. or not bool(bsl_obj.search(cr, SUPERUSER_ID, [('unique_import_id', '=', line_vals['unique_import_id'])], limit=1, context=context)):
  213. filtered_st_lines.append(line_vals)
  214. else:
  215. ignored_statement_lines_import_ids.append(line_vals['unique_import_id'])
  216. if len(filtered_st_lines) > 0:
  217. # Remove values that won't be used to create records
  218. st_vals.pop('transactions', None)
  219. for line_vals in filtered_st_lines:
  220. line_vals.pop('account_number', None)
  221. # Create the satement
  222. st_vals['line_ids'] = [[0, False, line] for line in filtered_st_lines]
  223. statement_ids.append(bs_obj.create(cr, uid, st_vals, context=context))
  224. if len(statement_ids) == 0:
  225. raise Warning(_('You have already imported that file.'))
  226. # Prepare import feedback
  227. notifications = []
  228. num_ignored = len(ignored_statement_lines_import_ids)
  229. if num_ignored > 0:
  230. notifications += [{
  231. 'type': 'warning',
  232. '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."),
  233. 'details': {
  234. 'name': _('Already imported items'),
  235. 'model': 'account.bank.statement.line',
  236. 'ids': bsl_obj.search(cr, uid, [('unique_import_id', 'in', ignored_statement_lines_import_ids)], context=context)
  237. }
  238. }]
  239. return statement_ids, notifications