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.

77 lines
3.1 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, CodaParserException
  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.old_balance_date + '-' + statement.new_balance_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 _get_acc_number(self, acc_number):
  45. #Check if we match the exact acc_number or the end of an acc number
  46. journal = self.env['account.journal'].search([('bank_acc_number', '=like', '%' + acc_number)])
  47. if not journal or len(journal) > 1: #if not found or ambiguious
  48. return acc_number
  49. return journal.bank_acc_number
  50. def _parse_file(self, data_file):
  51. parser = Parser()
  52. try:
  53. statements = parser.parse(data_file)
  54. except CodaParserException:
  55. return super(CodaBankStatementImport, self)._parse_file(data_file)
  56. currency_code = False
  57. account_number = False
  58. stmts_vals = []
  59. for statement in statements:
  60. account_number = statement.acc_number
  61. currency_code = statement.currency
  62. statement_data = self._get_statement_data(statement)
  63. stmts_vals.append(statement_data)
  64. for move in statement.movements:
  65. statement_data['transactions'].append(self._get_move_value(move, statement, len(statement_data['transactions']) + 1))
  66. return currency_code, self._get_acc_number(account_number), stmts_vals