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.

244 lines
10 KiB

9 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2014-2015 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. import re
  5. from collections import defaultdict
  6. from openerp.exceptions import Warning as UserError
  7. from openerp.models import expression
  8. from openerp.tools.safe_eval import safe_eval
  9. MODE_VARIATION = 'p'
  10. MODE_INITIAL = 'i'
  11. MODE_END = 'e'
  12. class AccountingExpressionProcessor(object):
  13. """ Processor for accounting expressions.
  14. Expressions of the form <field><mode>[accounts][optional move line domain]
  15. are supported, where:
  16. * field is bal, crd, deb
  17. * mode is i (initial balance), e (ending balance),
  18. p (moves over period)
  19. * accounts is a list of accounts, possibly containing % wildcards
  20. * an optional domain on move lines allowing filters on eg analytic
  21. accounts or journal
  22. Examples:
  23. * bal[70]: variation of the balance of moves on account 70
  24. over the period (it is the same as balp[70]);
  25. * bali[70,60]: balance of accounts 70 and 60 at the start of period;
  26. * bale[1%]: balance of accounts starting with 1 at end of period.
  27. How to use:
  28. * repeatedly invoke parse_expr() for each expression containing
  29. accounting variables as described above; this lets the processor
  30. group domains and modes and accounts;
  31. * when all expressions have been parsed, invoke done_parsing()
  32. to notify the processor that it can prepare to query (mainly
  33. search all accounts - children, consolidation - that will need to
  34. be queried;
  35. * for each period, call do_queries(), then call replace_expr() for each
  36. expression to replace accounting variables with their resulting value
  37. for the given period.
  38. How it works:
  39. * by accumulating the expressions before hand, it ensures to do the
  40. strict minimum number of queries to the database (for each period,
  41. one query per domain and mode);
  42. * it queries using the orm read_group which reduces to a query with
  43. sum on debit and credit and group by on account_id (note: it seems
  44. the orm then does one query per account to fetch the account
  45. name...);
  46. * additionally, one query per view/consolidation account is done to
  47. discover the children accounts.
  48. """
  49. ACC_RE = re.compile(r"(?P<field>\bbal|\bcrd|\bdeb)"
  50. r"(?P<mode>[pise])?"
  51. r"(?P<accounts>_[a-zA-Z0-9]+|\[.*?\])"
  52. r"(?P<domain>\[.*?\])?")
  53. def __init__(self, env):
  54. self.env = env
  55. # before done_parsing: {(domain, mode): set(account_codes)}
  56. # after done_parsing: {(domain, mode): list(account_ids)}
  57. self._map_account_ids = defaultdict(set)
  58. # {account_code: account_id} where account_code can be
  59. # - None for all accounts
  60. # - NNN% for a like
  61. # - NNN for a code with an exact match
  62. self._account_ids_by_code = defaultdict(set)
  63. def _load_account_codes(self, account_codes):
  64. account_model = self.env['account.account']
  65. exact_codes = set()
  66. for account_code in account_codes:
  67. if account_code in self._account_ids_by_code:
  68. continue
  69. if account_code is None:
  70. # None means we want all accounts
  71. account_ids = account_model.\
  72. search([]).mapped('id')
  73. self._account_ids_by_code[account_code].update(account_ids)
  74. elif '%' in account_code:
  75. account_ids = account_model.\
  76. search([('code', 'like', account_code)]).mapped('id')
  77. self._account_ids_by_code[account_code].update(account_ids)
  78. else:
  79. # search exact codes after the loop to do less queries
  80. exact_codes.add(account_code)
  81. for account in account_model.\
  82. search([('code', 'in', list(exact_codes))]):
  83. self._account_ids_by_code[account.code].add(account.id)
  84. def _parse_match_object(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 = MODE_VARIATION
  91. elif mode == 's':
  92. mode = MODE_END
  93. if account_codes.startswith('_'):
  94. account_codes = account_codes[1:]
  95. else:
  96. account_codes = account_codes[1:-1]
  97. if account_codes.strip():
  98. account_codes = [a.strip() for a in account_codes.split(',')]
  99. else:
  100. account_codes = [None] # None means we want all accounts
  101. domain = domain or '[]'
  102. domain = tuple(safe_eval(domain))
  103. return field, mode, account_codes, domain
  104. def parse_expr(self, expr):
  105. """Parse an expression, extracting accounting variables.
  106. Domains and accounts are extracted and stored in the map
  107. so when all expressions have been parsed, we know which
  108. account codes to query for each domain and mode.
  109. """
  110. for mo in self.ACC_RE.finditer(expr):
  111. _, mode, account_codes, domain = self._parse_match_object(mo)
  112. key = (domain, mode)
  113. self._map_account_ids[key].update(account_codes)
  114. def done_parsing(self):
  115. """Load account codes and replace account codes by
  116. account ids in map."""
  117. for key, account_codes in self._map_account_ids.items():
  118. # TODO _load_account_codes could be done
  119. # for all account_codes at once (also in v8)
  120. self._load_account_codes(account_codes)
  121. account_ids = set()
  122. for account_code in account_codes:
  123. account_ids.update(self._account_ids_by_code[account_code])
  124. self._map_account_ids[key] = list(account_ids)
  125. @classmethod
  126. def has_account_var(cls, expr):
  127. """Test if an string contains an accounting variable."""
  128. return bool(cls.ACC_RE.search(expr))
  129. def get_aml_domain_for_expr(self, expr,
  130. date_from, date_to,
  131. target_move):
  132. """ Get a domain on account.move.line for an expression.
  133. Prerequisite: done_parsing() must have been invoked.
  134. Returns a domain that can be used to search on account.move.line.
  135. """
  136. aml_domains = []
  137. date_domain_by_mode = {}
  138. for mo in self.ACC_RE.finditer(expr):
  139. field, mode, account_codes, domain = self._parse_match_object(mo)
  140. aml_domain = list(domain)
  141. account_ids = set()
  142. for account_code in account_codes:
  143. account_ids.update(self._account_ids_by_code[account_code])
  144. aml_domain.append(('account_id', 'in', tuple(account_ids)))
  145. if field == 'crd':
  146. aml_domain.append(('credit', '>', 0))
  147. elif field == 'deb':
  148. aml_domain.append(('debit', '>', 0))
  149. aml_domains.append(expression.normalize_domain(aml_domain))
  150. if mode not in date_domain_by_mode:
  151. date_domain_by_mode[mode] = \
  152. self.get_aml_domain_for_dates(date_from, date_to,
  153. mode, target_move)
  154. return expression.OR(aml_domains) + \
  155. expression.OR(date_domain_by_mode.values())
  156. def get_aml_domain_for_dates(self, date_from, date_to,
  157. mode,
  158. target_move):
  159. if mode == MODE_VARIATION:
  160. domain = [('date', '>=', date_from), ('date', '<=', date_to)]
  161. elif mode == MODE_INITIAL:
  162. domain = [('date', '<', date_from)]
  163. elif mode == MODE_END:
  164. domain = [('date', '<=', date_to)]
  165. if target_move == 'posted':
  166. domain.append(('move_id.state', '=', 'posted'))
  167. return expression.normalize_domain(domain)
  168. def do_queries(self, date_from, date_to,
  169. target_move, additional_move_line_filter=None):
  170. """Query sums of debit and credit for all accounts and domains
  171. used in expressions.
  172. This method must be executed after done_parsing().
  173. """
  174. aml_model = self.env['account.move.line']
  175. # {(domain, mode): {account_id: (debit, credit)}}
  176. self._data = defaultdict(dict)
  177. domain_by_mode = {}
  178. for key in self._map_account_ids:
  179. domain, mode = key
  180. if mode not in domain_by_mode:
  181. domain_by_mode[mode] = \
  182. self.get_aml_domain_for_dates(date_from, date_to,
  183. mode, target_move)
  184. domain = list(domain) + domain_by_mode[mode]
  185. domain.append(('account_id', 'in', self._map_account_ids[key]))
  186. if additional_move_line_filter:
  187. domain.extend(additional_move_line_filter)
  188. # fetch sum of debit/credit, grouped by account_id
  189. accs = aml_model.read_group(domain,
  190. ['debit', 'credit', 'account_id'],
  191. ['account_id'])
  192. for acc in accs:
  193. self._data[key][acc['account_id'][0]] = \
  194. (acc['debit'] or 0.0, acc['credit'] or 0.0)
  195. def replace_expr(self, expr):
  196. """Replace accounting variables in an expression by their amount.
  197. Returns a new expression string.
  198. This method must be executed after do_queries().
  199. """
  200. def f(mo):
  201. field, mode, account_codes, domain = self._parse_match_object(mo)
  202. key = (domain, mode)
  203. account_ids_data = self._data[key]
  204. v = 0.0
  205. for account_code in account_codes:
  206. account_ids = self._account_ids_by_code[account_code]
  207. for account_id in account_ids:
  208. debit, credit = \
  209. account_ids_data.get(account_id, (0.0, 0.0))
  210. if field == 'bal':
  211. v += debit - credit
  212. elif field == 'deb':
  213. v += debit
  214. elif field == 'crd':
  215. v += credit
  216. return '(' + repr(v) + ')'
  217. return self.ACC_RE.sub(f, expr)