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.

396 lines
17 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 itertools import izip
  7. from openerp import fields
  8. from openerp.models import expression
  9. from openerp.tools.safe_eval import safe_eval
  10. from openerp.tools.float_utils import float_is_zero
  11. from .accounting_none import AccountingNone
  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. * there is also a special u mode (unallocated P&L) which computes
  20. the sum from the beginning until the beginning of the fiscal year
  21. of the period; it is only meaningful for P&L accounts
  22. * accounts is a list of accounts, possibly containing % wildcards
  23. * an optional domain on move lines allowing filters on eg analytic
  24. accounts or journal
  25. Examples:
  26. * bal[70]: variation of the balance of moves on account 70
  27. over the period (it is the same as balp[70]);
  28. * bali[70,60]: balance of accounts 70 and 60 at the start of period;
  29. * bale[1%]: balance of accounts starting with 1 at end of period.
  30. How to use:
  31. * repeatedly invoke parse_expr() for each expression containing
  32. accounting variables as described above; this lets the processor
  33. group domains and modes and accounts;
  34. * when all expressions have been parsed, invoke done_parsing()
  35. to notify the processor that it can prepare to query (mainly
  36. search all accounts - children, consolidation - that will need to
  37. be queried;
  38. * for each period, call do_queries(), then call replace_expr() for each
  39. expression to replace accounting variables with their resulting value
  40. for the given period.
  41. How it works:
  42. * by accumulating the expressions before hand, it ensures to do the
  43. strict minimum number of queries to the database (for each period,
  44. one query per domain and mode);
  45. * it queries using the orm read_group which reduces to a query with
  46. sum on debit and credit and group by on account_id (note: it seems
  47. the orm then does one query per account to fetch the account
  48. name...);
  49. * additionally, one query per view/consolidation account is done to
  50. discover the children accounts.
  51. """
  52. MODE_VARIATION = 'p'
  53. MODE_INITIAL = 'i'
  54. MODE_END = 'e'
  55. MODE_UNALLOCATED = 'u'
  56. _ACC_RE = re.compile(r"(?P<field>\bbal|\bcrd|\bdeb)"
  57. r"(?P<mode>[piseu])?"
  58. r"(?P<accounts>_[a-zA-Z0-9]+|\[.*?\])"
  59. r"(?P<domain>\[.*?\])?")
  60. def __init__(self, env):
  61. self.env = env
  62. # before done_parsing: {(domain, mode): set(account_codes)}
  63. # after done_parsing: {(domain, mode): list(account_ids)}
  64. self._map_account_ids = defaultdict(set)
  65. # {account_code: account_id} where account_code can be
  66. # - None for all accounts
  67. # - NNN% for a like
  68. # - NNN for a code with an exact match
  69. self._account_ids_by_code = defaultdict(set)
  70. # smart ending balance (returns AccountingNone if there
  71. # are no moves in period and 0 initial balance), implies
  72. # a first query to get the initial balance and another
  73. # to get the variation, so it's a bit slower
  74. self.smart_end = True
  75. def _load_account_codes(self, account_codes, company):
  76. account_model = self.env['account.account']
  77. exact_codes = set()
  78. for account_code in account_codes:
  79. if account_code in self._account_ids_by_code:
  80. continue
  81. if account_code is None:
  82. # None means we want all accounts
  83. account_ids = account_model.\
  84. search([('company_id', '=', company.id)]).ids
  85. self._account_ids_by_code[account_code].update(account_ids)
  86. elif '%' in account_code:
  87. account_ids = account_model.\
  88. search([('code', '=like', account_code),
  89. ('company_id', '=', company.id)]).ids
  90. self._account_ids_by_code[account_code].update(account_ids)
  91. else:
  92. # search exact codes after the loop to do less queries
  93. exact_codes.add(account_code)
  94. for account in account_model.\
  95. search([('code', 'in', list(exact_codes)),
  96. ('company_id', '=', company.id)]):
  97. self._account_ids_by_code[account.code].add(account.id)
  98. def _parse_match_object(self, mo):
  99. """Split a match object corresponding to an accounting variable
  100. Returns field, mode, [account codes], (domain expression).
  101. """
  102. field, mode, account_codes, domain = mo.groups()
  103. if not mode:
  104. mode = self.MODE_VARIATION
  105. elif mode == 's':
  106. mode = self.MODE_END
  107. if account_codes.startswith('_'):
  108. account_codes = account_codes[1:]
  109. else:
  110. account_codes = account_codes[1:-1]
  111. if account_codes.strip():
  112. account_codes = [a.strip() for a in account_codes.split(',')]
  113. else:
  114. account_codes = [None] # None means we want all accounts
  115. domain = domain or '[]'
  116. domain = tuple(safe_eval(domain))
  117. return field, mode, account_codes, domain
  118. def parse_expr(self, expr):
  119. """Parse an expression, extracting accounting variables.
  120. Domains and accounts are extracted and stored in the map
  121. so when all expressions have been parsed, we know which
  122. account codes to query for each domain and mode.
  123. """
  124. for mo in self._ACC_RE.finditer(expr):
  125. _, mode, account_codes, domain = self._parse_match_object(mo)
  126. if mode == self.MODE_END and self.smart_end:
  127. modes = (self.MODE_INITIAL, self.MODE_VARIATION)
  128. else:
  129. modes = (mode, )
  130. for mode in modes:
  131. key = (domain, mode)
  132. self._map_account_ids[key].update(account_codes)
  133. def done_parsing(self, company):
  134. """Load account codes and replace account codes by
  135. account ids in map."""
  136. for key, account_codes in self._map_account_ids.items():
  137. # TODO _load_account_codes could be done
  138. # for all account_codes at once (also in v8)
  139. self._load_account_codes(account_codes, company)
  140. account_ids = set()
  141. for account_code in account_codes:
  142. account_ids.update(self._account_ids_by_code[account_code])
  143. self._map_account_ids[key] = list(account_ids)
  144. @classmethod
  145. def has_account_var(cls, expr):
  146. """Test if an string contains an accounting variable."""
  147. return bool(cls._ACC_RE.search(expr))
  148. def get_aml_domain_for_expr(self, expr,
  149. date_from, date_to,
  150. target_move, company):
  151. """ Get a domain on account.move.line for an expression.
  152. Prerequisite: done_parsing() must have been invoked.
  153. Returns a domain that can be used to search on account.move.line.
  154. """
  155. aml_domains = []
  156. date_domain_by_mode = {}
  157. for mo in self._ACC_RE.finditer(expr):
  158. field, mode, account_codes, domain = self._parse_match_object(mo)
  159. aml_domain = list(domain)
  160. account_ids = set()
  161. for account_code in account_codes:
  162. account_ids.update(self._account_ids_by_code[account_code])
  163. aml_domain.append(('account_id', 'in', tuple(account_ids)))
  164. if field == 'crd':
  165. aml_domain.append(('credit', '>', 0))
  166. elif field == 'deb':
  167. aml_domain.append(('debit', '>', 0))
  168. aml_domains.append(expression.normalize_domain(aml_domain))
  169. if mode not in date_domain_by_mode:
  170. date_domain_by_mode[mode] = \
  171. self.get_aml_domain_for_dates(date_from, date_to,
  172. mode, target_move,
  173. company)
  174. return expression.OR(aml_domains) + \
  175. expression.OR(date_domain_by_mode.values())
  176. def get_aml_domain_for_dates(self, date_from, date_to,
  177. mode,
  178. target_move, company):
  179. if mode == self.MODE_VARIATION:
  180. domain = [('date', '>=', date_from), ('date', '<=', date_to)]
  181. elif mode in (self.MODE_INITIAL, self.MODE_END):
  182. # for income and expense account, sum from the beginning
  183. # of the current fiscal year only, for balance sheet accounts
  184. # sum from the beginning of time
  185. date_from_date = fields.Date.from_string(date_from)
  186. fy_date_from = \
  187. company.compute_fiscalyear_dates(date_from_date)['date_from']
  188. domain = ['|',
  189. ('date', '>=', fields.Date.to_string(fy_date_from)),
  190. ('user_type_id.include_initial_balance', '=', True)]
  191. if mode == self.MODE_INITIAL:
  192. domain.append(('date', '<', date_from))
  193. elif mode == self.MODE_END:
  194. domain.append(('date', '<=', date_to))
  195. elif mode == self.MODE_UNALLOCATED:
  196. date_from_date = fields.Date.from_string(date_from)
  197. fy_date_from = \
  198. company.compute_fiscalyear_dates(date_from_date)['date_from']
  199. domain = [('date', '<', fields.Date.to_string(fy_date_from)),
  200. ('user_type_id.include_initial_balance', '=', False)]
  201. if target_move == 'posted':
  202. domain.append(('move_id.state', '=', 'posted'))
  203. return expression.normalize_domain(domain)
  204. def do_queries(self, date_from, date_to,
  205. target_move, company, additional_move_line_filter=None):
  206. """Query sums of debit and credit for all accounts and domains
  207. used in expressions.
  208. This method must be executed after done_parsing().
  209. """
  210. aml_model = self.env['account.move.line']
  211. # {(domain, mode): {account_id: (debit, credit)}}
  212. self._data = defaultdict(dict)
  213. domain_by_mode = {}
  214. for key in self._map_account_ids:
  215. domain, mode = key
  216. if mode not in domain_by_mode:
  217. domain_by_mode[mode] = \
  218. self.get_aml_domain_for_dates(date_from, date_to,
  219. mode, target_move, company)
  220. domain = list(domain) + domain_by_mode[mode]
  221. domain.append(('account_id', 'in', self._map_account_ids[key]))
  222. if additional_move_line_filter:
  223. domain.extend(additional_move_line_filter)
  224. # fetch sum of debit/credit, grouped by account_id
  225. accs = aml_model.read_group(domain,
  226. ['debit', 'credit', 'account_id'],
  227. ['account_id'])
  228. for acc in accs:
  229. debit = acc['debit'] or 0.0
  230. credit = acc['credit'] or 0.0
  231. if mode in (self.MODE_INITIAL, self.MODE_UNALLOCATED) and \
  232. float_is_zero(debit-credit, precision_rounding=2):
  233. # in initial mode, ignore accounts with 0 balance
  234. continue
  235. self._data[key][acc['account_id'][0]] = (debit, credit)
  236. def replace_expr(self, expr):
  237. """Replace accounting variables in an expression by their amount.
  238. Returns a new expression string.
  239. This method must be executed after do_queries().
  240. """
  241. def s(field, mode, account_codes, domain):
  242. key = (domain, mode)
  243. account_ids_data = self._data[key]
  244. v = AccountingNone
  245. for account_code in account_codes:
  246. account_ids = self._account_ids_by_code[account_code]
  247. for account_id in account_ids:
  248. debit, credit = \
  249. account_ids_data.get(account_id,
  250. (AccountingNone, AccountingNone))
  251. if field == 'bal':
  252. v += debit - credit
  253. elif field == 'deb':
  254. v += debit
  255. elif field == 'crd':
  256. v += credit
  257. # in initial balance mode, assume 0 is None
  258. # as it does not make sense to distinguish 0 from "no data"
  259. if v is not AccountingNone and \
  260. mode in (self.MODE_INITIAL, self.MODE_UNALLOCATED) and \
  261. float_is_zero(v, precision_rounding=2):
  262. v = AccountingNone
  263. return v
  264. def f(mo):
  265. field, mode, account_codes, domain = self._parse_match_object(mo)
  266. if mode == self.MODE_END and self.smart_end:
  267. # split ending balance in initial+variation, so
  268. # if there is no move in period, we end up with AccountingNone
  269. v = s(field, self.MODE_INITIAL, account_codes, domain) + \
  270. s(field, self.MODE_VARIATION, account_codes, domain)
  271. else:
  272. v = s(field, mode, account_codes, domain)
  273. return '(' + repr(v) + ')'
  274. return self._ACC_RE.sub(f, expr)
  275. def replace_expr_by_account_id(self, expr):
  276. """Replace accounting variables in an expression by their amount,
  277. iterating by accounts involved in the expression.
  278. yields account_id, replaced_expr
  279. This method must be executed after do_queries().
  280. """
  281. def s(field, mode, account_codes, domain):
  282. key = (domain, mode)
  283. account_ids_data = self._data[key]
  284. debit, credit = \
  285. account_ids_data.get(account_id,
  286. (AccountingNone, AccountingNone))
  287. if field == 'bal':
  288. v = debit - credit
  289. elif field == 'deb':
  290. v = debit
  291. elif field == 'crd':
  292. v = credit
  293. # in initial balance mode, assume 0 is None
  294. # as it does not make sense to distinguish 0 from "no data"
  295. if v is not AccountingNone and \
  296. mode in (self.MODE_INITIAL, self.MODE_UNALLOCATED) and \
  297. float_is_zero(v, precision_rounding=2):
  298. v = AccountingNone
  299. return v
  300. def f(mo):
  301. field, mode, account_codes, domain = self._parse_match_object(mo)
  302. if mode == self.MODE_END and self.smart_end:
  303. # split ending balance in initial+variation, so
  304. # if there is no move in period, we end up with AccountingNone
  305. v = s(field, self.MODE_INITIAL, account_codes, domain) + \
  306. s(field, self.MODE_VARIATION, account_codes, domain)
  307. else:
  308. v = s(field, mode, account_codes, domain)
  309. return '(' + repr(v) + ')'
  310. account_ids = set()
  311. for mo in self._ACC_RE.finditer(expr):
  312. field, mode, account_codes, domain = self._parse_match_object(mo)
  313. key = (domain, mode)
  314. account_ids_data = self._data[key]
  315. for account_code in account_codes:
  316. for account_id in self._account_ids_by_code[account_code]:
  317. if account_id in account_ids_data:
  318. account_ids.add(account_id)
  319. for account_id in account_ids:
  320. yield account_id, self._ACC_RE.sub(f, expr)
  321. @classmethod
  322. def get_balances(cls, mode, date_from, date_to, target_move, company):
  323. """ A convenience method to obtain the balances of all accounts
  324. :param mode: MODE_INITIAL|MODE_END|MODE_VARIATION
  325. :param date_from:
  326. :param date_to:
  327. :param target_move: if 'posted', consider only posted moves
  328. :param company:
  329. Returns a dictionary: {account_id, (debit, credit)}
  330. """
  331. assert mode in (cls.MODE_INITIAL, cls.MODE_END, cls.MODE_VARIATION)
  332. expr = 'deb{mode}[], crd{mode}[]'.format(mode=mode)
  333. aep = AccountingExpressionProcessor(company.env)
  334. # disable smart_end to have the data at once, instead
  335. # of initial + variation
  336. aep.smart_end = False
  337. aep.parse_expr(expr)
  338. aep.done_parsing(company)
  339. aep.do_queries(date_from, date_to, target_move, company)
  340. return aep._data[((), mode)]
  341. @classmethod
  342. def get_unallocated_pl(cls, date, target_move, company):
  343. """ A convenience method to obtain the unallocated profit/loss
  344. of the previous fiscal years
  345. :param date:
  346. :param target_move: if 'posted', consider only posted moves
  347. :param company:
  348. Returns a tuple (debit, credit)
  349. """
  350. aep = AccountingExpressionProcessor(company.env)
  351. expr = 'deb{mode}[], crd{mode}[]'.format(mode=cls.MODE_UNALLOCATED)
  352. aep.parse_expr(expr)
  353. aep.done_parsing(company)
  354. aep.do_queries(date, date, target_move, company)
  355. values = aep._data[((), cls.MODE_UNALLOCATED)].values()
  356. return tuple(map(sum, izip(*values)))