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.

69 lines
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. '''
  3. Created on 16 mai 2016
  4. @author: Thibault Francois (thibault@françois.be)
  5. '''
  6. from coda.parser import Parser
  7. from openerp import models, _
  8. class CodaBankStatementImport(models.TransientModel):
  9. _inherit = 'account.bank.statement.import'
  10. def _generate_note(self, move):
  11. notes = []
  12. if move.counterparty_name:
  13. notes.append("%s: %s" % (_('Counter Party Name'), move.counterparty_name))
  14. if move.counterparty_address:
  15. notes.append("%s: %s" % (_('Counter Party Address'), move.counterparty_address))
  16. if move.counterparty_number:
  17. notes.append("%s: %s" % (_('Counter Party Account'), move.counterparty_number))
  18. if move.communication:
  19. notes.append("%s: %s" % (_('Communication'), move.communication))
  20. return '\n'.join(notes)
  21. def _get_move_value(self, move, statement, sequence):
  22. move_data = {
  23. 'name': move.communication, #ok
  24. 'note': self._generate_note(move),
  25. 'date': move.entry_date, #ok
  26. 'amount': move.transaction_amount if move.transaction_amount_sign == '0' else - move.transaction_amount, #ok
  27. 'account_number': move.counterparty_number, #ok
  28. 'partner_name': move.counterparty_name, #ok
  29. 'ref': move.ref,
  30. 'sequence': sequence, #ok
  31. 'unique_import_id' : statement.coda_seq_number + '-' + statement.creation_date + '-' + move.ref
  32. }
  33. return move_data
  34. def _get_statement_data(self, statement):
  35. statement_data = {
  36. 'name' : statement.paper_seq_number,
  37. 'date' : statement.creation_date,
  38. 'balance_start': statement.old_balance, #ok
  39. 'balance_end_real' : statement.new_balance, #ok
  40. 'coda_note' : '',
  41. 'transactions' : []
  42. }
  43. return statement_data
  44. def _parse_file(self, data_file):
  45. parser = Parser()
  46. try:
  47. statements = parser.parse(data_file)
  48. except ValueError:
  49. return super(CodaBankStatementImport, self)._parse_file(data_file)
  50. currency_code = False
  51. account_number = False
  52. stmts_vals = []
  53. for statement in statements:
  54. account_number = statement.acc_number
  55. currency_code = statement.currency
  56. statement_data = self._get_statement_data(statement)
  57. stmts_vals.append(statement_data)
  58. for move in statement.movements:
  59. statement_data['transactions'].append(self._get_move_value(move, statement, len(statement_data['transactions']) + 1))
  60. return currency_code, account_number, stmts_vals