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.

95 lines
3.3 KiB

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import StringIO
  4. from odoo import api, models, _
  5. from odoo.exceptions import UserError
  6. from odoo.addons.base_iban.models.res_partner_bank import _map_iban_template
  7. from odoo.addons.base_iban.models.res_partner_bank import validate_iban
  8. _logger = logging.getLogger(__name__)
  9. try:
  10. from ofxparse import OfxParser
  11. except ImportError:
  12. _logger.debug("ofxparse not found.")
  13. OfxParser = None
  14. class AccountBankStatementImport(models.TransientModel):
  15. _inherit = 'account.bank.statement.import'
  16. def _check_journal_bank_account(self, journal, account_number):
  17. res = super(
  18. AccountBankStatementImport, self
  19. )._check_journal_bank_account(journal, account_number)
  20. if not res:
  21. e_acc_num = journal.bank_account_id.sanitized_acc_number
  22. e_acc_num = e_acc_num.replace(" ", "")
  23. validate_iban(e_acc_num)
  24. country_code = e_acc_num[:2].lower()
  25. iban_template = _map_iban_template[country_code].replace(
  26. " ", "")
  27. e_acc_num = "".join(
  28. [c for c, t in zip(e_acc_num, iban_template) if t == "C"])
  29. res = (e_acc_num == account_number)
  30. return res
  31. @api.model
  32. def _check_ofx(self, data_file):
  33. if not OfxParser:
  34. return False
  35. try:
  36. ofx = OfxParser.parse(StringIO.StringIO(data_file))
  37. except Exception as e:
  38. _logger.debug(e)
  39. return False
  40. return ofx
  41. @api.model
  42. def _prepare_ofx_transaction_line(self, transaction):
  43. # Since ofxparse doesn't provide account numbers,
  44. # we cannot provide the key 'bank_account_id',
  45. # nor the key 'account_number'
  46. # If you read odoo10/addons/account_bank_statement_import/
  47. # account_bank_statement_import.py, it's the only 2 keys
  48. # we can provide to match a partner.
  49. vals = {
  50. 'date': transaction.date,
  51. 'name': transaction.payee + (
  52. transaction.memo and ': ' + transaction.memo or ''),
  53. 'ref': transaction.id,
  54. 'amount': float(transaction.amount),
  55. 'unique_import_id': transaction.id,
  56. }
  57. return vals
  58. def _parse_file(self, data_file):
  59. ofx = self._check_ofx(data_file)
  60. if not ofx:
  61. return super(AccountBankStatementImport, self)._parse_file(
  62. data_file)
  63. transactions = []
  64. total_amt = 0.00
  65. try:
  66. for transaction in ofx.account.statement.transactions:
  67. vals = self._prepare_ofx_transaction_line(transaction)
  68. if vals:
  69. transactions.append(vals)
  70. total_amt += vals['amount']
  71. except Exception as e:
  72. raise UserError(_(
  73. "The following problem occurred during import. "
  74. "The file might not be valid.\n\n %s") % e.message)
  75. balance = float(ofx.account.statement.balance)
  76. vals_bank_statement = {
  77. 'name': ofx.account.number,
  78. 'transactions': transactions,
  79. 'balance_start': balance - total_amt,
  80. 'balance_end_real': balance,
  81. }
  82. return ofx.account.statement.currency, ofx.account.number, [
  83. vals_bank_statement]