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.

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