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.

112 lines
4.7 KiB

  1. # -*- encoding: utf-8 -*-
  2. """Classes and definitions used in parsing bank statements."""
  3. ##############################################################################
  4. #
  5. # Copyright (C) 2015 Therp BV <http://therp.nl>.
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. def convert_transaction(transaction):
  22. """Convert transaction object to values for create."""
  23. vals_line = {
  24. 'date': transaction.value_date,
  25. 'name': (
  26. transaction.message or transaction.eref or
  27. transaction.remote_owner or ''), # name is required
  28. 'ref': transaction.eref,
  29. 'amount': transaction.transferred_amount,
  30. 'partner_name': transaction.remote_owner,
  31. 'account_number': transaction.remote_account,
  32. 'unique_import_id': transaction.transaction_id,
  33. }
  34. return vals_line
  35. def convert_statements(statements):
  36. """Convert statement object to values for create."""
  37. vals_statements = []
  38. for statement in statements:
  39. # Set statement_data
  40. vals_statement = {
  41. 'currency_code': statement.local_currency,
  42. 'account_number': statement.local_account,
  43. 'name': statement.statement_id,
  44. 'date': statement.date.strftime('%Y-%m-%d'),
  45. 'balance_start': statement.start_balance,
  46. 'balance_end_real': statement.end_balance,
  47. 'balance_end': statement.end_balance,
  48. 'state': 'draft',
  49. }
  50. statement_transactions = []
  51. subno = 0
  52. for transaction in statement.transactions:
  53. subno += 1
  54. if not transaction.transaction_id:
  55. transaction.transaction_id = (
  56. statement.statement_id + str(subno).zfill(4))
  57. statement_transactions.append(convert_transaction(transaction))
  58. vals_statement['transactions'] = statement_transactions
  59. vals_statements.append(vals_statement)
  60. return vals_statements
  61. class BankStatement(object):
  62. """A bank statement groups data about several bank transactions."""
  63. def __init__(self):
  64. self.statement_id = ''
  65. self.local_account = ''
  66. self.local_currency = ''
  67. self.start_balance = 0.0
  68. self.end_balance = 0.0
  69. self.date = ''
  70. self.transactions = []
  71. class BankTransaction(object):
  72. """Single transaction that is part of a bank statement."""
  73. def __init__(self):
  74. """Define and initialize attributes.
  75. Does not include attributes that belong to statement.
  76. """
  77. self.transaction_id = False # Message id
  78. self.transfer_type = False # Action type that initiated this message
  79. self.eref = False # end to end reference for transactions
  80. self.execution_date = False # The posted date of the action
  81. self.value_date = False # The value date of the action
  82. self.remote_account = False # The account of the other party
  83. self.remote_currency = False # The currency used by the other party
  84. self.exchange_rate = 0.0
  85. # The exchange rate used for conversion of local_currency and
  86. # remote_currency
  87. self.transferred_amount = 0.0 # actual amount transferred
  88. self.message = False # message from the remote party
  89. self.remote_owner = False # name of the other party
  90. self.remote_owner_address = [] # other parties address lines
  91. self.remote_owner_city = False # other parties city name
  92. self.remote_owner_postalcode = False # other parties zip code
  93. self.remote_owner_country_code = False # other parties country code
  94. self.remote_bank_bic = False # bic of other party's bank
  95. self.provision_costs = False # costs charged by bank for transaction
  96. self.provision_costs_currency = False
  97. self.provision_costs_description = False
  98. self.error_message = False # error message for interaction with user
  99. self.storno_retry = False
  100. # If True, make cancelled debit eligible for a next direct debit run
  101. self.data = '' # Raw data from which the transaction has been parsed