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.

260 lines
10 KiB

  1. # -*- coding: utf-8 -*-
  2. """Class to parse camt files."""
  3. # © 2013-2016 Therp BV <http://therp.nl>
  4. # Copyright 2017 Open Net Sàrl
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. import re
  7. from lxml import etree
  8. from openerp import models
  9. class CamtParser(models.AbstractModel):
  10. _name = 'account.bank.statement.import.camt.parser'
  11. """Parser for camt bank statement import files."""
  12. def parse_amount(self, ns, node):
  13. """Parse element that contains Amount and CreditDebitIndicator."""
  14. if node is None:
  15. return 0.0
  16. sign = 1
  17. amount = 0.0
  18. sign_node = node.xpath('ns:CdtDbtInd', namespaces={'ns': ns})
  19. if sign_node and sign_node[0].text == 'DBIT':
  20. sign = -1
  21. amount_node = node.xpath('ns:Amt', namespaces={'ns': ns})
  22. if amount_node:
  23. amount = sign * float(amount_node[0].text)
  24. return amount
  25. def add_value_from_node(
  26. self, ns, node, xpath_str, obj, attr_name, join_str=None):
  27. """Add value to object from first or all nodes found with xpath.
  28. If xpath_str is a list (or iterable), it will be seen as a series
  29. of search path's in order of preference. The first item that results
  30. in a found node will be used to set a value."""
  31. if not isinstance(xpath_str, (list, tuple)):
  32. xpath_str = [xpath_str]
  33. for search_str in xpath_str:
  34. found_node = node.xpath(search_str, namespaces={'ns': ns})
  35. if found_node:
  36. if join_str is None:
  37. attr_value = found_node[0].text
  38. else:
  39. attr_value = join_str.join([x.text for x in found_node])
  40. obj[attr_name] = attr_value
  41. break
  42. def parse_transaction_details(self, ns, node, transaction):
  43. """Parse TxDtls node."""
  44. # message
  45. self.add_value_from_node(
  46. ns, node, [
  47. './ns:RmtInf/ns:Ustrd',
  48. './ns:AddtlNtryInf',
  49. './ns:Refs/ns:InstrId',
  50. ], transaction, 'name', join_str='\n')
  51. # name
  52. self.add_value_from_node(
  53. ns, node, [
  54. './ns:AddtlTxInf',
  55. ], transaction, 'note', join_str='\n')
  56. # eref
  57. self.add_value_from_node(
  58. ns, node, [
  59. './ns:RmtInf/ns:Strd/ns:CdtrRefInf/ns:Ref',
  60. './ns:Refs/ns:EndToEndId',
  61. ],
  62. transaction, 'ref'
  63. )
  64. amount = self.parse_amount(ns, node)
  65. if amount != 0.0:
  66. transaction['amount'] = amount
  67. # remote party values
  68. party_type = 'Dbtr'
  69. party_type_node = node.xpath(
  70. '../../ns:CdtDbtInd', namespaces={'ns': ns})
  71. if party_type_node and party_type_node[0].text != 'CRDT':
  72. party_type = 'Cdtr'
  73. party_node = node.xpath(
  74. './ns:RltdPties/ns:%s' % party_type, namespaces={'ns': ns})
  75. if party_node:
  76. self.add_value_from_node(
  77. ns, party_node[0], './ns:Nm', transaction, 'partner_name')
  78. self.add_value_from_node(
  79. ns, party_node[0], './ns:PstlAdr/ns:Ctry', transaction,
  80. 'partner_country'
  81. )
  82. address_node = party_node[0].xpath(
  83. './ns:PstlAdr/ns:AdrLine', namespaces={'ns': ns})
  84. if address_node:
  85. transaction['partner_address'] = [address_node[0].text]
  86. # Get remote_account from iban or from domestic account:
  87. account_node = node.xpath(
  88. './ns:RltdPties/ns:%sAcct/ns:Id' % party_type,
  89. namespaces={'ns': ns}
  90. )
  91. if account_node:
  92. iban_node = account_node[0].xpath(
  93. './ns:IBAN', namespaces={'ns': ns})
  94. if iban_node:
  95. transaction['account_number'] = iban_node[0].text
  96. bic_node = node.xpath(
  97. './ns:RltdAgts/ns:%sAgt/ns:FinInstnId/ns:BIC' % party_type,
  98. namespaces={'ns': ns}
  99. )
  100. if bic_node:
  101. transaction['account_bic'] = bic_node[0].text
  102. else:
  103. self.add_value_from_node(
  104. ns, account_node[0], './ns:Othr/ns:Id', transaction,
  105. 'account_number'
  106. )
  107. transaction['data'] = etree.tostring(node)
  108. def parse_entry(self, ns, node):
  109. """Parse an Ntry node and yield transactions"""
  110. transaction = {'name': '/', 'amount': 0} # fallback defaults
  111. self.add_value_from_node(
  112. ns, node, './ns:BkTxCd/ns:Prtry/ns:Cd', transaction,
  113. 'transfer_type'
  114. )
  115. self.add_value_from_node(
  116. ns, node, './ns:BookgDt/ns:Dt', transaction, 'date')
  117. self.add_value_from_node(
  118. ns, node, './ns:BookgDt/ns:Dt', transaction, 'execution_date')
  119. self.add_value_from_node(
  120. ns, node, './ns:ValDt/ns:Dt', transaction, 'value_date')
  121. amount = self.parse_amount(ns, node)
  122. if amount != 0.0:
  123. transaction['amount'] = amount
  124. self.add_value_from_node(
  125. ns, node, './ns:AddtlNtryInf', transaction, 'name')
  126. self.add_value_from_node(
  127. ns, node, [
  128. './ns:NtryDtls/ns:RmtInf/ns:Strd/ns:CdtrRefInf/ns:Ref',
  129. './ns:NtryDtls/ns:Btch/ns:PmtInfId',
  130. ],
  131. transaction, 'ref'
  132. )
  133. details_nodes = node.xpath(
  134. './ns:NtryDtls/ns:TxDtls', namespaces={'ns': ns})
  135. if len(details_nodes) == 0:
  136. yield transaction
  137. return
  138. transaction_base = transaction
  139. for node in details_nodes:
  140. transaction = transaction_base.copy()
  141. self.parse_transaction_details(ns, node, transaction)
  142. yield transaction
  143. def get_balance_amounts(self, ns, node):
  144. """Return opening and closing balance.
  145. Depending on kind of balance and statement, the balance might be in a
  146. different kind of node:
  147. OPBD = OpeningBalance
  148. PRCD = PreviousClosingBalance
  149. ITBD = InterimBalance (first ITBD is start-, second is end-balance)
  150. CLBD = ClosingBalance
  151. """
  152. start_balance_node = None
  153. end_balance_node = None
  154. for node_name in ['OPBD', 'PRCD', 'CLBD', 'ITBD']:
  155. code_expr = (
  156. './ns:Bal/ns:Tp/ns:CdOrPrtry/ns:Cd[text()="%s"]/../../..' %
  157. node_name
  158. )
  159. balance_node = node.xpath(code_expr, namespaces={'ns': ns})
  160. if balance_node:
  161. if node_name in ['OPBD', 'PRCD']:
  162. start_balance_node = balance_node[0]
  163. elif node_name == 'CLBD':
  164. end_balance_node = balance_node[0]
  165. else:
  166. if not start_balance_node:
  167. start_balance_node = balance_node[0]
  168. if not end_balance_node:
  169. end_balance_node = balance_node[-1]
  170. return (
  171. self.parse_amount(ns, start_balance_node),
  172. self.parse_amount(ns, end_balance_node)
  173. )
  174. def parse_statement(self, ns, node):
  175. """Parse a single Stmt node."""
  176. result = {}
  177. self.add_value_from_node(
  178. ns, node, [
  179. './ns:Acct/ns:Id/ns:IBAN',
  180. './ns:Acct/ns:Id/ns:Othr/ns:Id',
  181. ], result, 'account_number'
  182. )
  183. self.add_value_from_node(
  184. ns, node, './ns:Id', result, 'name')
  185. self.add_value_from_node(
  186. ns, node, './ns:Dt', result, 'date')
  187. self.add_value_from_node(
  188. ns, node, './ns:Acct/ns:Ccy', result, 'currency')
  189. result['balance_start'], result['balance_end_real'] = (
  190. self.get_balance_amounts(ns, node))
  191. entry_nodes = node.xpath('./ns:Ntry', namespaces={'ns': ns})
  192. transactions = []
  193. for entry_node in entry_nodes:
  194. transactions.extend(self.parse_entry(ns, entry_node))
  195. result['transactions'] = transactions
  196. return result
  197. def check_version(self, ns, root):
  198. """Validate validity of camt file."""
  199. # Check wether it is camt at all:
  200. re_camt = re.compile(
  201. r'(^urn:iso:std:iso:20022:tech:xsd:camt.'
  202. r'|^ISO:camt.)'
  203. )
  204. if not re_camt.search(ns):
  205. raise ValueError('no camt: ' + ns)
  206. # Check wether version 052 ,053 or 054:
  207. re_camt_version = re.compile(
  208. r'(^urn:iso:std:iso:20022:tech:xsd:camt.054.'
  209. r'|^urn:iso:std:iso:20022:tech:xsd:camt.053.'
  210. r'|^urn:iso:std:iso:20022:tech:xsd:camt.052.'
  211. r'|^ISO:camt.054.'
  212. r'|^ISO:camt.053.'
  213. r'|^ISO:camt.052.)'
  214. )
  215. if not re_camt_version.search(ns):
  216. raise ValueError('no camt 052 or 053: ' + ns)
  217. # Check GrpHdr element:
  218. root_0_0 = root[0][0].tag[len(ns) + 2:] # strip namespace
  219. if root_0_0 != 'GrpHdr':
  220. raise ValueError('expected GrpHdr, got: ' + root_0_0)
  221. def parse(self, data):
  222. """Parse a camt.052 or camt.053 file."""
  223. try:
  224. root = etree.fromstring(
  225. data, parser=etree.XMLParser(recover=True))
  226. except etree.XMLSyntaxError:
  227. # ABNAmro is known to mix up encodings
  228. root = etree.fromstring(
  229. data.decode('iso-8859-15').encode('utf-8'))
  230. if root is None:
  231. raise ValueError(
  232. 'Not a valid xml file, or not an xml file at all.')
  233. ns = root.tag[1:root.tag.index("}")]
  234. self.check_version(ns, root)
  235. statements = []
  236. currency = None
  237. account_number = None
  238. for node in root[0][1:]:
  239. statement = self.parse_statement(ns, node)
  240. if len(statement['transactions']):
  241. if 'currency' in statement:
  242. currency = statement.pop('currency')
  243. if 'account_number' in statement:
  244. account_number = statement.pop('account_number')
  245. statements.append(statement)
  246. return currency, account_number, statements