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.

254 lines
9.8 KiB

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