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.

204 lines
8.6 KiB

  1. import re
  2. from collections import defaultdict
  3. from openerp.osv import expression
  4. class AccountingExpressionProcessor(object):
  5. """ Processor for accounting expressions.
  6. Expressions of the form <field><mode>[accounts][optional move line domain]
  7. are supported, where:
  8. * field is bal, crd, deb
  9. * mode is i (initial balance), e (ending balance),
  10. p (moves over period)
  11. * accounts is a list of accounts, possibly containing % wildcards
  12. * an optional domain on analytic lines allowing filters on eg analytic
  13. accounts or journal
  14. Examples:
  15. * bal[70]: balance of moves on account 70 over the period
  16. (it is the same as balp[70]);
  17. * bali[70,60]: initial balance of accounts 70 and 60;
  18. * bale[1%]: balance of accounts starting with 1 at end of period.
  19. How to use:
  20. * repeatedly invoke parse_expr() for each expression containing
  21. accounting variables as described above; this let the processor
  22. group domains and modes and accounts;
  23. * when all expressions have been parsed, invoke done_parsing()
  24. to notify the processor that it can prepare to query (mainly
  25. search all accounts - children, consolidation - that will need to
  26. be queried;
  27. * for each period, call do_queries(), then call replace_expr() for each
  28. expression to replace accounting variables with their resulting value
  29. for the given period.
  30. How it works:
  31. * by accumulating the expressions before hand, it ensure to do the
  32. strict minimum number of queries to the database (for each period,
  33. one query per domain and mode);
  34. * it queries using the orm read_group which reduces to a query with
  35. sum on debit and credit and group by on account_id (note: it seems
  36. the orm then does one query per account to fetch the account
  37. name...);
  38. * additionally, one query per view/consolidation account is done to
  39. discover the children accounts.
  40. """
  41. ACC_RE = re.compile(r"(?P<field>\bbal|\bcrd|\bdeb)"
  42. r"(?P<mode>[pise])?"
  43. r"(?P<accounts>_[0-9]+|\[.*?\])"
  44. r"(?P<domain>\[.*?\])?")
  45. def __init__(self, env):
  46. self.env = env
  47. self._map = defaultdict(set) # {(domain, mode): set(account_ids)}
  48. self._account_ids_by_code = defaultdict(set)
  49. def _load_account_codes(self, account_codes, account_domain):
  50. account_model = self.env['account.account']
  51. # TODO: account_obj is necessary because _get_children_and_consol
  52. # does not work in new API?
  53. account_obj = self.env.registry('account.account')
  54. exact_codes = set()
  55. like_codes = set()
  56. for account_code in account_codes:
  57. if account_code in self._account_ids_by_code:
  58. continue
  59. if '%' in account_code:
  60. like_codes.add(account_code)
  61. else:
  62. exact_codes.add(account_code)
  63. for account in account_model.\
  64. search([('code', 'in', list(exact_codes))] + account_domain):
  65. if account.type in ('view', 'consolidation'):
  66. self._account_ids_by_code[account.code].update(
  67. account_obj._get_children_and_consol(
  68. self.env.cr, self.env.uid,
  69. [account.id],
  70. self.env.context))
  71. else:
  72. self._account_ids_by_code[account.code].add(account.id)
  73. for like_code in like_codes:
  74. for account in account_model.\
  75. search([('code', 'like', like_code)] + account_domain):
  76. if account.type in ('view', 'consolidation'):
  77. self._account_ids_by_code[like_code].update(
  78. account_obj._get_children_and_consol(
  79. self.env.cr, self.env.uid,
  80. [account.id],
  81. self.env.context))
  82. else:
  83. self._account_ids_by_code[like_code].add(account.id)
  84. def _parse_mo(self, mo):
  85. """Split a match object corresponding to an accounting variable
  86. Returns field, mode, [account codes], [domain expression].
  87. """
  88. field, mode, account_codes, domain = mo.groups()
  89. if not mode:
  90. mode = 'p'
  91. elif mode == 's':
  92. mode = 'e'
  93. if account_codes.startswith('_'):
  94. account_codes = account_codes[1:]
  95. else:
  96. account_codes = account_codes[1:-1]
  97. account_codes = [a.strip() for a in account_codes.split(',')]
  98. domain = domain or '[]'
  99. domain = tuple(eval(domain)) # TODO: safe_eval
  100. return field, mode, account_codes, domain
  101. def parse_expr(self, expr):
  102. """Parse an expression, extracting accounting variables.
  103. Domains and accounts are extracted and stored in the map
  104. so when all expressions have been parsed, we know what to query.
  105. """
  106. for mo in self.ACC_RE.finditer(expr):
  107. field, mode, account_codes, domain = self._parse_mo(mo)
  108. key = (domain, mode)
  109. self._map[key].update(account_codes)
  110. def done_parsing(self, account_domain):
  111. # load account codes and replace account codes by account ids in _map
  112. for key, account_codes in self._map.items():
  113. self._load_account_codes(account_codes, account_domain)
  114. account_ids = set()
  115. for account_code in account_codes:
  116. account_ids.update(self._account_ids_by_code[account_code])
  117. self._map[key] = list(account_ids)
  118. def get_aml_domain_for_expr(self, expr):
  119. """ Get a domain on account.move.line for an expression.
  120. Only accounting expression in mode 'p' and 'e' are considered.
  121. Prerequisite: done_parsing() must have been invoked.
  122. Returns a domain that can be used to search on account.move.line.
  123. """
  124. domains = []
  125. for mo in self.ACC_RE.finditer(expr):
  126. field, mode, account_codes, domain = self._parse_mo(mo)
  127. if mode == 'i':
  128. continue
  129. account_ids = set()
  130. for account_code in account_codes:
  131. account_ids.update(self._account_ids_by_code[account_code])
  132. domain = [('account_id', 'in', tuple(account_ids))]
  133. if field == 'crd':
  134. domain.append(('credit', '>', 0))
  135. elif field == 'deb':
  136. domain.append(('debit', '>', 0))
  137. domain.extend(domain)
  138. domains.append(expression.normalize_domain(domain))
  139. return expression.OR(domains)
  140. def do_queries(self, period_domain, period_domain_i, period_domain_e):
  141. aml_model = self.env['account.move.line']
  142. self._data = {} # {(domain, mode): {account_id: (debit, credit)}}
  143. for key in self._map:
  144. self._data[key] = {}
  145. domain, mode = key
  146. if mode == 'p':
  147. domain = list(domain) + period_domain
  148. elif mode == 'i':
  149. domain = list(domain) + period_domain_i
  150. elif mode == 'e':
  151. domain = list(domain) + period_domain_e
  152. else:
  153. raise RuntimeError("unexpected mode %s" % (mode,))
  154. domain = [('account_id', 'in', self._map[key])] + domain
  155. accs = aml_model.read_group(domain,
  156. ['debit', 'credit', 'account_id'],
  157. ['account_id'])
  158. for acc in accs:
  159. self._data[key][acc['account_id'][0]] = \
  160. (acc['debit'], acc['credit'])
  161. def replace_expr(self, expr):
  162. """Replace accounting variables in an expression by their amount.
  163. Returns a new expression.
  164. This method must be executed after do_queries().
  165. """
  166. def f(mo):
  167. field, mode, account_codes, domain = self._parse_mo(mo)
  168. key = (domain, mode)
  169. account_ids_data = self._data[key]
  170. v = 0.0
  171. for account_code in account_codes:
  172. for account_id in self._account_ids_by_code[account_code]:
  173. debit, credit = \
  174. account_ids_data.get(account_id, (0.0, 0.0))
  175. if field == 'deb':
  176. v += debit
  177. elif field == 'crd':
  178. v += credit
  179. elif field == 'bal':
  180. v += debit - credit
  181. return '(' + repr(v) + ')'
  182. return self.ACC_RE.sub(f, expr)