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.

430 lines
18 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, self.MODE_END)
  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, company, date_from, date_to,
  205. target_move='posted', 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. ends = []
  215. for key in self._map_account_ids:
  216. domain, mode = key
  217. if mode == self.MODE_END and self.smart_end:
  218. # postpone computation of ending balance
  219. ends.append((domain, mode))
  220. continue
  221. if mode not in domain_by_mode:
  222. domain_by_mode[mode] = \
  223. self.get_aml_domain_for_dates(date_from, date_to,
  224. mode, target_move, company)
  225. domain = list(domain) + domain_by_mode[mode]
  226. domain.append(('account_id', 'in', self._map_account_ids[key]))
  227. if additional_move_line_filter:
  228. domain.extend(additional_move_line_filter)
  229. # fetch sum of debit/credit, grouped by account_id
  230. accs = aml_model.read_group(domain,
  231. ['debit', 'credit', 'account_id'],
  232. ['account_id'])
  233. for acc in accs:
  234. debit = acc['debit'] or 0.0
  235. credit = acc['credit'] or 0.0
  236. if mode in (self.MODE_INITIAL, self.MODE_UNALLOCATED) and \
  237. float_is_zero(debit-credit, precision_rounding=2):
  238. # in initial mode, ignore accounts with 0 balance
  239. continue
  240. self._data[key][acc['account_id'][0]] = (debit, credit)
  241. # compute ending balances by summing initial and variation
  242. for key in ends:
  243. domain, mode = key
  244. initial_data = self._data[(domain, self.MODE_INITIAL)]
  245. variation_data = self._data[(domain, self.MODE_VARIATION)]
  246. account_ids = set(initial_data.keys()) | set(variation_data.keys())
  247. for account_id in account_ids:
  248. di, ci = initial_data.get(account_id,
  249. (AccountingNone, AccountingNone))
  250. dv, cv = variation_data.get(account_id,
  251. (AccountingNone, AccountingNone))
  252. self._data[key][account_id] = (di + dv, ci + cv)
  253. def replace_expr(self, expr):
  254. """Replace accounting variables in an expression by their amount.
  255. Returns a new expression string.
  256. This method must be executed after do_queries().
  257. """
  258. def f(mo):
  259. field, mode, account_codes, domain = self._parse_match_object(mo)
  260. key = (domain, mode)
  261. account_ids_data = self._data[key]
  262. v = AccountingNone
  263. for account_code in account_codes:
  264. account_ids = self._account_ids_by_code[account_code]
  265. for account_id in account_ids:
  266. debit, credit = \
  267. account_ids_data.get(account_id,
  268. (AccountingNone, AccountingNone))
  269. if field == 'bal':
  270. v += debit - credit
  271. elif field == 'deb':
  272. v += debit
  273. elif field == 'crd':
  274. v += credit
  275. # in initial balance mode, assume 0 is None
  276. # as it does not make sense to distinguish 0 from "no data"
  277. if v is not AccountingNone and \
  278. mode in (self.MODE_INITIAL, self.MODE_UNALLOCATED) and \
  279. float_is_zero(v, precision_rounding=2):
  280. v = AccountingNone
  281. return '(' + repr(v) + ')'
  282. return self._ACC_RE.sub(f, expr)
  283. def replace_exprs_by_account_id(self, exprs):
  284. """Replace accounting variables in a list of expression
  285. by their amount, iterating by accounts involved in the expression.
  286. yields account_id, replaced_expr
  287. This method must be executed after do_queries().
  288. """
  289. def f(mo):
  290. field, mode, account_codes, domain = self._parse_match_object(mo)
  291. key = (domain, mode)
  292. account_ids_data = self._data[key]
  293. debit, credit = \
  294. account_ids_data.get(account_id,
  295. (AccountingNone, AccountingNone))
  296. if field == 'bal':
  297. v = debit - credit
  298. elif field == 'deb':
  299. v = debit
  300. elif field == 'crd':
  301. v = credit
  302. # in initial balance mode, assume 0 is None
  303. # as it does not make sense to distinguish 0 from "no data"
  304. if v is not AccountingNone and \
  305. mode in (self.MODE_INITIAL, self.MODE_UNALLOCATED) and \
  306. float_is_zero(v, precision_rounding=2):
  307. v = AccountingNone
  308. return '(' + repr(v) + ')'
  309. account_ids = set()
  310. for expr in exprs:
  311. for mo in self._ACC_RE.finditer(expr):
  312. field, mode, account_codes, domain = \
  313. self._parse_match_object(mo)
  314. key = (domain, mode)
  315. account_ids_data = self._data[key]
  316. for account_code in account_codes:
  317. for account_id in self._account_ids_by_code[account_code]:
  318. if account_id in account_ids_data:
  319. account_ids.add(account_id)
  320. for account_id in account_ids:
  321. yield account_id, [self._ACC_RE.sub(f, expr) for expr in exprs]
  322. @classmethod
  323. def _get_balances(cls, mode, company, date_from, date_to,
  324. target_move='posted'):
  325. expr = 'deb{mode}[], crd{mode}[]'.format(mode=mode)
  326. aep = AccountingExpressionProcessor(company.env)
  327. # disable smart_end to have the data at once, instead
  328. # of initial + variation
  329. aep.smart_end = False
  330. aep.parse_expr(expr)
  331. aep.done_parsing(company)
  332. aep.do_queries(company, date_from, date_to, target_move)
  333. return aep._data[((), mode)]
  334. @classmethod
  335. def get_balances_initial(cls, company, date, target_move='posted'):
  336. """ A convenience method to obtain the initial balances of all accounts
  337. at a given date.
  338. It is the same as get_balances_end(date-1).
  339. :param company:
  340. :param date:
  341. :param target_move: if 'posted', consider only posted moves
  342. Returns a dictionary: {account_id, (debit, credit)}
  343. """
  344. return cls._get_balances(cls.MODE_INITIAL, company,
  345. date, date, target_move)
  346. @classmethod
  347. def get_balances_end(cls, company, date, target_move='posted'):
  348. """ A convenience method to obtain the ending balances of all accounts
  349. at a given date.
  350. It is the same as get_balances_init(date+1).
  351. :param company:
  352. :param date:
  353. :param target_move: if 'posted', consider only posted moves
  354. Returns a dictionary: {account_id, (debit, credit)}
  355. """
  356. return cls._get_balances(cls.MODE_END, company,
  357. date, date, target_move)
  358. @classmethod
  359. def get_balances_variation(cls, company, date_from, date_to,
  360. target_move='posted'):
  361. """ A convenience method to obtain the variantion of the
  362. balances of all accounts over a period.
  363. :param company:
  364. :param date:
  365. :param target_move: if 'posted', consider only posted moves
  366. Returns a dictionary: {account_id, (debit, credit)}
  367. """
  368. return cls._get_balances(cls.MODE_VARIATION, company,
  369. date_from, date_to, target_move)
  370. @classmethod
  371. def get_unallocated_pl(cls, company, date, target_move='posted'):
  372. """ A convenience method to obtain the unallocated profit/loss
  373. of the previous fiscal years at a given date.
  374. :param company:
  375. :param date:
  376. :param target_move: if 'posted', consider only posted moves
  377. Returns a tuple (debit, credit)
  378. """
  379. # TODO shoud we include here the accounts of type "unaffected"
  380. # or leave that to the caller?
  381. bals = cls._get_balances(cls.MODE_UNALLOCATED, company,
  382. date, date, target_move)
  383. return tuple(map(sum, izip(*bals.values())))