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.

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