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.

93 lines
3.2 KiB

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