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.

346 lines
15 KiB

  1. import re
  2. from collections import defaultdict
  3. from openerp.osv import expression
  4. from openerp.tools.safe_eval import safe_eval
  5. MODE_VARIATION = 'p'
  6. MODE_INITIAL = 'i'
  7. MODE_END = 'e'
  8. class AccountingExpressionProcessor(object):
  9. """ Processor for accounting expressions.
  10. Expressions of the form <field><mode>[accounts][optional move line domain]
  11. are supported, where:
  12. * field is bal, crd, deb
  13. * mode is i (initial balance), e (ending balance),
  14. p (moves over period)
  15. * accounts is a list of accounts, possibly containing % wildcards
  16. * an optional domain on analytic lines allowing filters on eg analytic
  17. accounts or journal
  18. Examples:
  19. * bal[70]: balance of moves on account 70 over the period
  20. (it is the same as balp[70]);
  21. * bali[70,60]: initial balance of accounts 70 and 60;
  22. * bale[1%]: balance of accounts starting with 1 at end of period.
  23. How to use:
  24. * repeatedly invoke parse_expr() for each expression containing
  25. accounting variables as described above; this let the processor
  26. group domains and modes and accounts;
  27. * when all expressions have been parsed, invoke done_parsing()
  28. to notify the processor that it can prepare to query (mainly
  29. search all accounts - children, consolidation - that will need to
  30. be queried;
  31. * for each period, call do_queries(), then call replace_expr() for each
  32. expression to replace accounting variables with their resulting value
  33. for the given period.
  34. How it works:
  35. * by accumulating the expressions before hand, it ensure to do the
  36. strict minimum number of queries to the database (for each period,
  37. one query per domain and mode);
  38. * it queries using the orm read_group which reduces to a query with
  39. sum on debit and credit and group by on account_id (note: it seems
  40. the orm then does one query per account to fetch the account
  41. name...);
  42. * additionally, one query per view/consolidation account is done to
  43. discover the children accounts.
  44. """
  45. ACC_RE = re.compile(r"(?P<field>\bbal|\bcrd|\bdeb)"
  46. r"(?P<mode>[pise])?"
  47. r"(?P<accounts>_[0-9]+|\[.*?\])"
  48. r"(?P<domain>\[.*?\])?")
  49. def __init__(self, env):
  50. self.env = env
  51. # before done_parsing: {(domain, mode): set(account_codes)}
  52. # after done_parsing: {(domain, mode): set(account_ids)}
  53. self._map_account_ids = defaultdict(set)
  54. self._account_ids_by_code = defaultdict(set)
  55. def _load_account_codes(self, account_codes, root_account):
  56. account_model = self.env['account.account']
  57. # TODO: account_obj is necessary because _get_children_and_consol
  58. # does not work in new API?
  59. account_obj = self.env.registry('account.account')
  60. exact_codes = set()
  61. like_codes = set()
  62. for account_code in account_codes:
  63. if account_code in self._account_ids_by_code:
  64. continue
  65. if account_code is None:
  66. # by convention the root account is keyed as
  67. # None in _account_ids_by_code, so it is consistent
  68. # with what _parse_match_object returns for an
  69. # empty list of account codes, ie [None]
  70. exact_codes.add(root_account.code)
  71. elif '%' in account_code:
  72. like_codes.add(account_code)
  73. else:
  74. exact_codes.add(account_code)
  75. for account in account_model.\
  76. search([('code', 'in', list(exact_codes)),
  77. ('parent_id', 'child_of', root_account.id)]):
  78. if account.code == root_account.code:
  79. code = None
  80. else:
  81. code = account.code
  82. if account.type in ('view', 'consolidation'):
  83. self._account_ids_by_code[code].update(
  84. account_obj._get_children_and_consol(
  85. self.env.cr, self.env.uid,
  86. [account.id],
  87. self.env.context))
  88. else:
  89. self._account_ids_by_code[code].add(account.id)
  90. for like_code in like_codes:
  91. for account in account_model.\
  92. search([('code', 'like', like_code),
  93. ('parent_id', 'child_of', root_account.id)]):
  94. if account.type in ('view', 'consolidation'):
  95. self._account_ids_by_code[like_code].update(
  96. account_obj._get_children_and_consol(
  97. self.env.cr, self.env.uid,
  98. [account.id],
  99. self.env.context))
  100. else:
  101. self._account_ids_by_code[like_code].add(account.id)
  102. def _parse_match_object(self, mo):
  103. """Split a match object corresponding to an accounting variable
  104. Returns field, mode, [account codes], [domain expression].
  105. """
  106. field, mode, account_codes, domain = mo.groups()
  107. if not mode:
  108. mode = MODE_VARIATION
  109. elif mode == 's':
  110. mode = MODE_END
  111. if account_codes.startswith('_'):
  112. account_codes = account_codes[1:]
  113. else:
  114. account_codes = account_codes[1:-1]
  115. if account_codes.strip():
  116. account_codes = [a.strip() for a in account_codes.split(',')]
  117. else:
  118. account_codes = [None]
  119. domain = domain or '[]'
  120. domain = tuple(safe_eval(domain))
  121. return field, mode, account_codes, domain
  122. def parse_expr(self, expr):
  123. """Parse an expression, extracting accounting variables.
  124. Domains and accounts are extracted and stored in the map
  125. so when all expressions have been parsed, we know what to query.
  126. """
  127. for mo in self.ACC_RE.finditer(expr):
  128. _, mode, account_codes, domain = self._parse_match_object(mo)
  129. key = (domain, mode)
  130. self._map_account_ids[key].update(account_codes)
  131. def done_parsing(self, root_account):
  132. # load account codes and replace account codes by account ids in _map
  133. for key, account_codes in self._map_account_ids.items():
  134. self._load_account_codes(account_codes, root_account)
  135. account_ids = set()
  136. for account_code in account_codes:
  137. account_ids.update(self._account_ids_by_code[account_code])
  138. self._map_account_ids[key] = list(account_ids)
  139. def has_account_var(self, expr):
  140. return bool(self.ACC_RE.match(expr))
  141. def get_aml_domain_for_expr(self, expr,
  142. date_from, date_to,
  143. period_from, period_to,
  144. target_move):
  145. """ Get a domain on account.move.line for an expression.
  146. Prerequisite: done_parsing() must have been invoked.
  147. Returns a domain that can be used to search on account.move.line.
  148. To be meaningful, this domain must be extended with a domain
  149. obtained from get_aml_domain_for_dates()
  150. """
  151. aml_domains = []
  152. domain_by_mode = {}
  153. for mo in self.ACC_RE.finditer(expr):
  154. field, mode, account_codes, domain = self._parse_match_object(mo)
  155. aml_domain = list(domain)
  156. account_ids = set()
  157. for account_code in account_codes:
  158. account_ids.update(self._account_ids_by_code[account_code])
  159. aml_domain.append(('account_id', 'in', tuple(account_ids)))
  160. if field == 'crd':
  161. aml_domain.append(('credit', '>', 0))
  162. elif field == 'deb':
  163. aml_domain.append(('debit', '>', 0))
  164. aml_domains.append(expression.normalize_domain(aml_domain))
  165. if mode not in domain_by_mode:
  166. domain_by_mode[mode] = \
  167. self.get_aml_domain_for_dates(date_from, date_to,
  168. period_from, period_to,
  169. mode, target_move)
  170. return expression.OR(aml_domains) + \
  171. expression.OR(domain_by_mode.values())
  172. def _period_has_moves(self, period):
  173. move_model = self.env['account.move']
  174. return bool(move_model.search([('period_id', '=', period.id)],
  175. limit=1))
  176. def _get_previous_opening_period(self, period, company_id):
  177. period_model = self.env['account.period']
  178. periods = period_model.search(
  179. [('date_start', '<=', period.date_start),
  180. ('special', '=', True),
  181. ('company_id', '=', company_id)],
  182. order="date_start desc",
  183. limit=1)
  184. return periods and periods[0]
  185. def _get_previous_normal_period(self, period, company_id):
  186. period_model = self.env['account.period']
  187. periods = period_model.search(
  188. [('date_start', '<', period.date_start),
  189. ('special', '=', False),
  190. ('company_id', '=', company_id)],
  191. order="date_start desc",
  192. limit=1)
  193. return periods and periods[0]
  194. def _get_first_normal_period(self, company_id):
  195. period_model = self.env['account.period']
  196. periods = period_model.search(
  197. [('special', '=', False),
  198. ('company_id', '=', company_id)],
  199. order="date_start asc",
  200. limit=1)
  201. return periods and periods[0]
  202. def _get_period_ids_between(self, period_from, period_to, company_id):
  203. period_model = self.env['account.period']
  204. periods = period_model.search(
  205. [('date_start', '>=', period_from.date_start),
  206. ('date_stop', '<=', period_to.date_stop),
  207. ('special', '=', False),
  208. ('company_id', '=', company_id)])
  209. period_ids = [p.id for p in periods]
  210. if period_from.special:
  211. period_ids.append(period_from.id)
  212. return period_ids
  213. def _get_period_company_ids(self, period_from, period_to):
  214. period_model = self.env['account.period']
  215. periods = period_model.search(
  216. [('date_start', '>=', period_from.date_start),
  217. ('date_stop', '<=', period_to.date_stop),
  218. ('special', '=', False)])
  219. return set([p.company_id.id for p in periods])
  220. def _get_period_ids_for_mode(self, period_from, period_to, mode):
  221. assert not period_from.special
  222. assert not period_to.special
  223. assert period_from.company_id == period_to.company_id
  224. assert period_from.date_start <= period_to.date_start
  225. period_ids = []
  226. for company_id in self._get_period_company_ids(period_from, period_to):
  227. if mode == MODE_VARIATION:
  228. period_ids.extend(self._get_period_ids_between(
  229. period_from, period_to, company_id))
  230. else:
  231. if mode == MODE_INITIAL:
  232. period_to = self._get_previous_normal_period(
  233. period_from, company_id)
  234. # look for opening period with moves
  235. opening_period = self._get_previous_opening_period(
  236. period_from, company_id)
  237. if opening_period and \
  238. self._period_has_moves(opening_period[0]):
  239. # found opening period with moves
  240. if opening_period.date_start == period_from.date_start and \
  241. mode == MODE_INITIAL:
  242. # if the opening period has the same start date as
  243. # period_from, then we'll find the initial balance
  244. # in the initial period and that's it
  245. period_ids.append(opening_period[0].id)
  246. continue
  247. period_from = opening_period[0]
  248. else:
  249. # no opening period with moves,
  250. # use very first normal period
  251. period_from = self._get_first_normal_period(company_id)
  252. if period_to:
  253. period_ids.extend(self._get_period_ids_between(
  254. period_from, period_to, company_id))
  255. return period_ids
  256. def get_aml_domain_for_dates(self, date_from, date_to,
  257. period_from, period_to,
  258. mode,
  259. target_move):
  260. if period_from and period_to:
  261. period_ids = self._get_period_ids_for_mode(
  262. period_from, period_to, mode)
  263. domain = [('period_id', 'in', period_ids)]
  264. else:
  265. domain = [('date', '>=', date_from), ('date', '<=', date_to)]
  266. if target_move == 'posted':
  267. domain.append(('move_id.state', '=', 'posted'))
  268. return expression.normalize_domain(domain)
  269. def do_queries(self, date_from, date_to, period_from, period_to,
  270. target_move):
  271. aml_model = self.env['account.move.line']
  272. # {(domain, mode): {account_id: (debit, credit)}}
  273. self._data = defaultdict(dict)
  274. domain_by_mode = {}
  275. for key in self._map_account_ids:
  276. domain, mode = key
  277. if mode not in domain_by_mode:
  278. domain_by_mode[mode] = \
  279. self.get_aml_domain_for_dates(date_from, date_to,
  280. period_from, period_to,
  281. mode, target_move)
  282. domain = list(domain) + domain_by_mode[mode]
  283. domain.append(('account_id', 'in', self._map_account_ids[key]))
  284. # fetch sum of debit/credit, grouped by account_id
  285. accs = aml_model.read_group(domain,
  286. ['debit', 'credit', 'account_id'],
  287. ['account_id'])
  288. for acc in accs:
  289. self._data[key][acc['account_id'][0]] = \
  290. (acc['debit'] or 0.0, acc['credit'] or 0.0)
  291. def replace_expr(self, expr):
  292. """Replace accounting variables in an expression by their amount.
  293. Returns a new expression.
  294. This method must be executed after do_queries().
  295. """
  296. def f(mo):
  297. field, mode, account_codes, domain = self._parse_match_object(mo)
  298. key = (domain, mode)
  299. account_ids_data = self._data[key]
  300. v = 0.0
  301. for account_code in account_codes:
  302. account_ids = self._account_ids_by_code[account_code]
  303. for account_id in account_ids:
  304. debit, credit = \
  305. account_ids_data.get(account_id, (0.0, 0.0))
  306. if field == 'bal':
  307. v += debit - credit
  308. elif field == 'deb':
  309. v += debit
  310. elif field == 'crd':
  311. v += credit
  312. return '(' + repr(v) + ')'
  313. return self.ACC_RE.sub(f, expr)