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.

365 lines
16 KiB

  1. # -*- coding: utf-8 -*-
  2. """Framework for importing bank statement files."""
  3. import base64
  4. from openerp import api, models, fields
  5. from openerp.tools.translate import _
  6. from openerp.exceptions import Warning
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. class AccountBankStatementLine(models.Model):
  10. """Extend model account.bank.statement.line."""
  11. _inherit = "account.bank.statement.line"
  12. # Ensure transactions can be imported only once (if the import format
  13. # provides unique transaction ids)
  14. unique_import_id = fields.Char('Import ID', readonly=True, copy=False)
  15. _sql_constraints = [
  16. ('unique_import_id',
  17. 'unique (unique_import_id)',
  18. 'A bank account transactions can be imported only once !')
  19. ]
  20. class AccountBankStatementImport(models.TransientModel):
  21. """Extend model account.bank.statement."""
  22. _name = 'account.bank.statement.import'
  23. _description = 'Import Bank Statement'
  24. @api.model
  25. def _get_hide_journal_field(self):
  26. """ Return False if the journal_id can't be provided by the parsed
  27. file and must be provided by the wizard.
  28. See account_bank_statement_import_qif """
  29. return True
  30. journal_id = fields.Many2one(
  31. 'account.journal', string='Journal',
  32. help='Accounting journal related to the bank statement you\'re '
  33. 'importing. It has be be manually chosen for statement formats which '
  34. 'doesn\'t allow automatic journal detection (QIF for example).')
  35. hide_journal_field = fields.Boolean(
  36. string='Hide the journal field in the view',
  37. compute='_get_hide_journal_field')
  38. data_file = fields.Binary(
  39. 'Bank Statement File', required=True,
  40. help='Get you bank statements in electronic format from your bank '
  41. 'and select them here.')
  42. @api.multi
  43. def import_file(self):
  44. """ Process the file chosen in the wizard, create bank statement(s) and
  45. go to reconciliation."""
  46. self.ensure_one()
  47. data_file = base64.b64decode(self.data_file)
  48. statement_ids, notifications = self.with_context(
  49. active_id=self.id)._import_file(data_file)
  50. # dispatch to reconciliation interface
  51. action = self.env.ref(
  52. 'account.action_bank_reconcile_bank_statements')
  53. return {
  54. 'name': action.name,
  55. 'tag': action.tag,
  56. 'context': {
  57. 'statement_ids': statement_ids,
  58. 'notifications': notifications
  59. },
  60. 'type': 'ir.actions.client',
  61. }
  62. @api.model
  63. def _import_file(self, data_file):
  64. """ Create bank statement(s) from file."""
  65. # The appropriate implementation module returns the required data
  66. statement_ids = []
  67. notifications = []
  68. statements = self._parse_file(data_file)
  69. # Check raw data:
  70. self._check_parsed_data(statements)
  71. # Import all statements:
  72. for stmt_vals in statements:
  73. (statement_id, new_notifications) = (
  74. self._import_statement(stmt_vals))
  75. if statement_id:
  76. statement_ids.append(statement_id)
  77. notifications.append(new_notifications)
  78. if len(statement_ids) == 0:
  79. raise Warning(_('You have already imported that file.'))
  80. return statement_ids, notifications
  81. @api.model
  82. def _import_statement(self, stmt_vals):
  83. """Import a single bank-statement.
  84. Return ids of created statements and notifications.
  85. """
  86. currency_code = stmt_vals.pop('currency_code')
  87. account_number = stmt_vals.pop('account_number')
  88. # Try to find the bank account and currency in odoo
  89. currency_id = self._find_currency_id(currency_code)
  90. bank_account_id = self._find_bank_account_id(account_number)
  91. # Create the bank account if not already existing
  92. if not bank_account_id and account_number:
  93. journal_id = self.env.context.get('journal_id')
  94. company_id = self.env.user.company_id.id
  95. if journal_id:
  96. journal = self.env['account.journal'].browse(journal_id)
  97. company_id = journal.company_id.id
  98. bank_account_id = self._create_bank_account(
  99. account_number, company_id=company_id,
  100. currency_id=currency_id).id
  101. # Find or create the bank journal
  102. journal_id = self._get_journal(currency_id, bank_account_id)
  103. # By now journal and account_number must be known
  104. if not journal_id:
  105. raise Warning(_('Can not determine journal for import.'))
  106. # Prepare statement data to be used for bank statements creation
  107. stmt_vals = self._complete_statement(
  108. stmt_vals, journal_id, account_number)
  109. # Create the bank stmt_vals
  110. return self._create_bank_statement(stmt_vals)
  111. @api.model
  112. def _parse_file(self, data_file):
  113. """ Each module adding a file support must extends this method. It
  114. processes the file if it can, returns super otherwise, resulting in a
  115. chain of responsability.
  116. This method parses the given file and returns the data required by
  117. the bank statement import process, as specified below.
  118. - bank statements data: list of dict containing (optional
  119. items marked by o) :
  120. -o currency code: string (e.g: 'EUR')
  121. The ISO 4217 currency code, case insensitive
  122. -o account number: string (e.g: 'BE1234567890')
  123. The number of the bank account which the statement
  124. belongs to
  125. - 'name': string (e.g: '000000123')
  126. - 'date': date (e.g: 2013-06-26)
  127. -o 'balance_start': float (e.g: 8368.56)
  128. -o 'balance_end_real': float (e.g: 8888.88)
  129. - 'transactions': list of dict containing :
  130. - 'name': string
  131. (e.g: 'KBC-INVESTERINGSKREDIET 787-5562831-01')
  132. - 'date': date
  133. - 'amount': float
  134. - 'unique_import_id': string
  135. -o 'account_number': string
  136. Will be used to find/create the res.partner.bank
  137. in odoo
  138. -o 'note': string
  139. -o 'partner_name': string
  140. -o 'ref': string
  141. """
  142. raise Warning(_(
  143. 'Could not make sense of the given file.\n'
  144. 'Did you install the module to support this type of file?'
  145. ))
  146. @api.model
  147. def _check_parsed_data(self, statements):
  148. """ Basic and structural verifications """
  149. if len(statements) == 0:
  150. raise Warning(_('This file doesn\'t contain any statement.'))
  151. for stmt_vals in statements:
  152. if 'transactions' in stmt_vals and stmt_vals['transactions']:
  153. return
  154. # If we get here, no transaction was found:
  155. raise Warning(_('This file doesn\'t contain any transaction.'))
  156. @api.model
  157. def _find_currency_id(self, currency_code):
  158. """ Get res.currency ID."""
  159. if currency_code:
  160. currency_ids = self.env['res.currency'].search(
  161. [('name', '=ilike', currency_code)])
  162. if currency_ids:
  163. return currency_ids[0].id
  164. else:
  165. raise Warning(_(
  166. 'Statement has invalid currency code %s') % currency_code)
  167. # if no currency_code is provided, we'll use the company currency
  168. return self.env.user.company_id.currency_id.id
  169. @api.model
  170. def _find_bank_account_id(self, account_number):
  171. """ Get res.partner.bank ID """
  172. bank_account_id = None
  173. if account_number and len(account_number) > 4:
  174. bank_account_ids = self.env['res.partner.bank'].search(
  175. [('acc_number', '=', account_number)], limit=1)
  176. if bank_account_ids:
  177. bank_account_id = bank_account_ids[0].id
  178. return bank_account_id
  179. @api.model
  180. def _get_journal(self, currency_id, bank_account_id, account_number):
  181. """ Find the journal """
  182. bank_model = self.env['res.partner.bank']
  183. # Find the journal from context, wizard or bank account
  184. journal_id = self.env.context.get('journal_id') or self.journal_id.id
  185. if bank_account_id:
  186. bank_account = bank_model.browse(bank_account_id)
  187. if journal_id:
  188. if (bank_account.journal_id.id and
  189. bank_account.journal_id.id != journal_id):
  190. raise Warning(
  191. _('The account of this statement is linked to '
  192. 'another journal.'))
  193. if not bank_account.journal_id.id:
  194. bank_model.write({'journal_id': journal_id})
  195. else:
  196. if bank_account.journal_id.id:
  197. journal_id = bank_account.journal_id.id
  198. # If importing into an existing journal, its currency must be the same
  199. # as the bank statement. When journal has no currency, currency must
  200. # be equal to company currency.
  201. if journal_id and currency_id:
  202. journal_obj = self.env['account.journal'].browse(journal_id)
  203. if journal_obj.currency:
  204. journal_currency_id = journal_obj.currency.id
  205. if currency_id != journal_currency_id:
  206. # ALso log message with id's for technical analysis:
  207. _logger.warn(
  208. _('Statement currency id is %d,'
  209. ' but journal currency id = %d.'),
  210. currency_id,
  211. journal_currency_id
  212. )
  213. raise Warning(_(
  214. 'The currency of the bank statement is not '
  215. 'the same as the currency of the journal !'
  216. ))
  217. else:
  218. company_currency_id = self.env.user.company_id.currency_id.id
  219. if currency_id != company_currency_id:
  220. # ALso log message with id's for technical analysis:
  221. _logger.warn(
  222. _('Statement currency id is %d,'
  223. ' but company currency id = %d.'),
  224. currency_id,
  225. company_currency_id
  226. )
  227. raise Warning(_(
  228. 'The currency of the bank statement is not '
  229. 'the same as the company currency !'
  230. ))
  231. return journal_id
  232. @api.model
  233. @api.returns('res.partner.bank')
  234. def _create_bank_account(
  235. self, account_number, company_id=False, currency_id=False):
  236. """Automagically create bank account, when not yet existing."""
  237. try:
  238. bank_type = self.env.ref('base.bank_normal')
  239. bank_code = bank_type.code
  240. except ValueError:
  241. bank_code = 'bank'
  242. vals_acc = {
  243. 'acc_number': account_number,
  244. 'state': bank_code,
  245. }
  246. # Odoo users bank accounts (which we import statement from) have
  247. # company_id and journal_id set while 'counterpart' bank accounts
  248. # (from which statement transactions originate) don't.
  249. # Warning : if company_id is set, the method post_write of class
  250. # bank will create a journal
  251. if company_id:
  252. vals = self.env['res.partner.bank'].onchange_company_id(company_id)
  253. vals_acc.update(vals.get('value', {}))
  254. vals_acc['company_id'] = company_id
  255. # When the journal is created at same time of the bank account, we need
  256. # to specify the currency to use for the account.account and
  257. # account.journal
  258. return self.env['res.partner.bank'].with_context(
  259. default_currency_id=currency_id,
  260. default_currency=currency_id).create(vals_acc)
  261. @api.model
  262. def _complete_statement(self, stmt_vals, journal_id, account_number):
  263. """Complete statement from information passed."""
  264. stmt_vals['journal_id'] = journal_id
  265. for line_vals in stmt_vals['transactions']:
  266. unique_import_id = line_vals.get('unique_import_id', False)
  267. if unique_import_id:
  268. line_vals['unique_import_id'] = (
  269. account_number and account_number + '-' or '') + \
  270. unique_import_id
  271. if not line_vals.get('bank_account_id'):
  272. # Find the partner and his bank account or create the bank
  273. # account. The partner selected during the reconciliation
  274. # process will be linked to the bank when the statement is
  275. # closed.
  276. partner_id = False
  277. bank_account_id = False
  278. identifying_string = line_vals.get('account_number')
  279. if identifying_string:
  280. bank_model = self.env['res.partner.bank']
  281. banks = bank_model.search(
  282. [('acc_number', '=', identifying_string)], limit=1)
  283. if banks:
  284. bank_account_id = banks[0].id
  285. partner_id = banks[0].partner_id.id
  286. else:
  287. bank_account_id = self._create_bank_account(
  288. identifying_string).id
  289. line_vals['partner_id'] = partner_id
  290. line_vals['bank_account_id'] = bank_account_id
  291. return stmt_vals
  292. @api.model
  293. def _create_bank_statement(self, stmt_vals):
  294. """ Create bank statement from imported values, filtering out
  295. already imported transactions, and return data used by the
  296. reconciliation widget
  297. """
  298. bs_model = self.env['account.bank.statement']
  299. bsl_model = self.env['account.bank.statement.line']
  300. # Filter out already imported transactions and create statement
  301. ignored_line_ids = []
  302. filtered_st_lines = []
  303. for line_vals in stmt_vals['transactions']:
  304. unique_id = (
  305. 'unique_import_id' in line_vals and
  306. line_vals['unique_import_id']
  307. )
  308. if not unique_id or not bool(bsl_model.sudo().search(
  309. [('unique_import_id', '=', unique_id)], limit=1)):
  310. filtered_st_lines.append(line_vals)
  311. else:
  312. ignored_line_ids.append(unique_id)
  313. statement_id = False
  314. if len(filtered_st_lines) > 0:
  315. # Remove values that won't be used to create records
  316. stmt_vals.pop('transactions', None)
  317. for line_vals in filtered_st_lines:
  318. line_vals.pop('account_number', None)
  319. # Create the statement
  320. stmt_vals['line_ids'] = [
  321. [0, False, line] for line in filtered_st_lines]
  322. statement_id = bs_model.create(stmt_vals).id
  323. # Prepare import feedback
  324. notifications = []
  325. num_ignored = len(ignored_line_ids)
  326. if num_ignored > 0:
  327. notifications += [{
  328. 'type': 'warning',
  329. 'message':
  330. _("%d transactions had already been imported and "
  331. "were ignored.") % num_ignored
  332. if num_ignored > 1
  333. else _("1 transaction had already been imported and "
  334. "was ignored."),
  335. 'details': {
  336. 'name': _('Already imported items'),
  337. 'model': 'account.bank.statement.line',
  338. 'ids': bsl_model.search(
  339. [('unique_import_id', 'in', ignored_line_ids)]).ids}
  340. }]
  341. return statement_id, notifications