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.

992 lines
38 KiB

9 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2014-2016 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from collections import defaultdict, OrderedDict
  5. import datetime
  6. import dateutil
  7. from itertools import izip
  8. import logging
  9. import re
  10. import time
  11. import pytz
  12. from openerp import api, fields, models, _
  13. from openerp.exceptions import UserError
  14. from openerp.tools.safe_eval import safe_eval
  15. from .aep import AccountingExpressionProcessor as AEP
  16. from .aggregate import _sum, _avg, _min, _max
  17. from .accounting_none import AccountingNone
  18. from .simple_array import SimpleArray
  19. from .mis_safe_eval import mis_safe_eval, DataError
  20. from .mis_report_style import (
  21. TYPE_NUM, TYPE_PCT, TYPE_STR, CMP_DIFF, CMP_PCT, CMP_NONE
  22. )
  23. _logger = logging.getLogger(__name__)
  24. class AutoStruct(object):
  25. def __init__(self, **kwargs):
  26. for k, v in kwargs.items():
  27. setattr(self, k, v)
  28. class KpiMatrixRow(object):
  29. # TODO: ultimately, the kpi matrix will become ignorant of KPI's and
  30. # accounts and know about rows, columns, sub columns and styles only.
  31. # It is already ignorant of period and only knowns about columns.
  32. # This will require a correct abstraction for expanding row details.
  33. def __init__(self, matrix, kpi, account_id=None, parent_row=None):
  34. self._matrix = matrix
  35. self.kpi = kpi
  36. self.account_id = account_id
  37. self.description = ''
  38. self.parent_row = parent_row
  39. if not self.account_id:
  40. self.style_props = self._matrix._style_model.merge([
  41. self.kpi.report_id.style_id,
  42. self.kpi.style_id])
  43. else:
  44. self.style_props = self._matrix._style_model.merge([
  45. self.kpi.report_id.style_id,
  46. self.kpi.auto_expand_accounts_style_id])
  47. @property
  48. def label(self):
  49. if not self.account_id:
  50. return self.kpi.description
  51. else:
  52. return self._matrix.get_account_name(self.account_id)
  53. @property
  54. def row_id(self):
  55. if not self.account_id:
  56. return self.kpi.name
  57. else:
  58. return '{}:{}'.format(self.kpi.name, self.account_id)
  59. def iter_cell_tuples(self, cols=None):
  60. if cols is None:
  61. cols = self._matrix.iter_cols()
  62. for col in cols:
  63. yield col.get_cell_tuple_for_row(self)
  64. def iter_cells(self, subcols=None):
  65. if subcols is None:
  66. subcols = self._matrix.iter_subcols()
  67. for subcol in subcols:
  68. yield subcol.get_cell_for_row(self)
  69. class KpiMatrixCol(object):
  70. def __init__(self, label, description, locals_dict, subkpis):
  71. self.label = label
  72. self.description = description
  73. self.locals_dict = locals_dict
  74. self.colspan = subkpis and len(subkpis) or 1
  75. self._subcols = []
  76. self.subkpis = subkpis
  77. if not subkpis:
  78. subcol = KpiMatrixSubCol(self, '', '', 0)
  79. self._subcols.append(subcol)
  80. else:
  81. for i, subkpi in enumerate(subkpis):
  82. subcol = KpiMatrixSubCol(self, subkpi.description, '', i)
  83. self._subcols.append(subcol)
  84. self._cell_tuples_by_row = {} # {row: (cells tuple)}
  85. def _set_cell_tuple(self, row, cell_tuple):
  86. self._cell_tuples_by_row[row] = cell_tuple
  87. def iter_subcols(self):
  88. return self._subcols
  89. def iter_cell_tuples(self):
  90. return self._cells_by_row.values()
  91. def get_cell_tuple_for_row(self, row):
  92. return self._cell_tuples_by_row.get(row)
  93. class KpiMatrixSubCol(object):
  94. def __init__(self, col, label, description, index=0):
  95. self.col = col
  96. self.label = label
  97. self.description = description
  98. self.index = index
  99. @property
  100. def subkpi(self):
  101. if self.col.subkpis:
  102. return self.col.subkpis[self.index]
  103. def iter_cells(self):
  104. for cell_tuple in self.col.iter_cell_tuples():
  105. yield cell_tuple[self.index]
  106. def get_cell_for_row(self, row):
  107. cell_tuple = self.col.get_cell_tuple_for_row(row)
  108. if cell_tuple is None:
  109. return None
  110. return cell_tuple[self.index]
  111. class KpiMatrixCell(object):
  112. def __init__(self, row, subcol,
  113. val, val_rendered, val_comment,
  114. style_props,
  115. drilldown_arg):
  116. self.row = row
  117. self.subcol = subcol
  118. self.val = val
  119. self.val_rendered = val_rendered
  120. self.val_comment = val_comment
  121. self.style_props = style_props
  122. self.drilldown_arg = drilldown_arg
  123. class KpiMatrix(object):
  124. def __init__(self, env):
  125. # cache language id for faster rendering
  126. lang_model = env['res.lang']
  127. lang_id = lang_model._lang_get(env.user.lang)
  128. self.lang = lang_model.browse(lang_id)
  129. self._style_model = env['mis.report.style']
  130. self._account_model = env['account.account']
  131. # data structures
  132. # { kpi: KpiMatrixRow }
  133. self._kpi_rows = OrderedDict()
  134. # { kpi: {account_id: KpiMatrixRow} }
  135. self._detail_rows = {}
  136. # { col_key: KpiMatrixCol }
  137. self._cols = OrderedDict()
  138. # { col_key (left of comparison): [(col_key, base_col_key)] }
  139. self._comparison_todo = defaultdict(list)
  140. self._comparison_cols = defaultdict(list)
  141. # { account_id: account_name }
  142. self._account_names = {}
  143. def declare_kpi(self, kpi):
  144. """ Declare a new kpi (row) in the matrix.
  145. Invoke this first for all kpi, in display order.
  146. """
  147. self._kpi_rows[kpi] = KpiMatrixRow(self, kpi)
  148. self._detail_rows[kpi] = {}
  149. def declare_col(self, col_key, label, description,
  150. locals_dict, subkpis):
  151. """ Declare a new column, giving it an identifier (key).
  152. Invoke this and declare_comparison in display order.
  153. """
  154. self._cols[col_key] = KpiMatrixCol(label, description,
  155. locals_dict, subkpis)
  156. def declare_comparison(self, col_key, base_col_key):
  157. """ Declare a new comparison column.
  158. Invoke this and declare_col in display order.
  159. """
  160. last_col_key = list(self._cols.keys())[-1]
  161. self._comparison_todo[last_col_key].append(
  162. (col_key, base_col_key))
  163. def set_values(self, kpi, col_key, vals,
  164. drilldown_args):
  165. """ Set values for a kpi and a colum.
  166. Invoke this after declaring the kpi and the column.
  167. """
  168. self.set_values_detail_account(kpi, col_key, None, vals,
  169. drilldown_args)
  170. def set_values_detail_account(self, kpi, col_key, account_id, vals,
  171. drilldown_args):
  172. """ Set values for a kpi and a column and a detail account.
  173. Invoke this after declaring the kpi and the column.
  174. """
  175. if not account_id:
  176. row = self._kpi_rows[kpi]
  177. else:
  178. kpi_row = self._kpi_rows[kpi]
  179. if account_id in self._detail_rows[kpi]:
  180. row = self._detail_rows[kpi][account_id]
  181. else:
  182. row = KpiMatrixRow(self, kpi, account_id, parent_row=kpi_row)
  183. self._detail_rows[kpi][account_id] = row
  184. col = self._cols[col_key]
  185. cell_tuple = []
  186. assert len(vals) == col.colspan
  187. for val, drilldown_arg, subcol in \
  188. izip(vals, drilldown_args, col.iter_subcols()):
  189. if isinstance(val, DataError):
  190. val_rendered = val.name
  191. val_comment = val.msg
  192. else:
  193. val_rendered = self._style_model.render(
  194. self.lang, row.style_props, kpi.type, val)
  195. if subcol.subkpi:
  196. val_comment = u'{}.{} = {}'.format(
  197. row.kpi.name,
  198. subcol.subkpi.name,
  199. row.kpi.get_expression_for_subkpi(subcol.subkpi))
  200. else:
  201. val_comment = u'{} = {}'.format(
  202. row.kpi.name,
  203. row.kpi.expression)
  204. cell_style_props = row.style_props
  205. if row.kpi.style_expression:
  206. # evaluate style expression
  207. try:
  208. style_name = mis_safe_eval(row.kpi.style_expression,
  209. col.locals_dict)
  210. except:
  211. _logger.error("Error evaluating style expression <%s>",
  212. row.kpi.style_expression, exc_info=True)
  213. if style_name:
  214. style = self._style_model.search(
  215. [('name', '=', style_name)])
  216. if style:
  217. cell_style_props = self._style_model.merge(
  218. [row.style_props, style[0]])
  219. else:
  220. _logger.error("Style '%s' not found.", style_name)
  221. cell = KpiMatrixCell(row, subcol, val, val_rendered, val_comment,
  222. cell_style_props, drilldown_arg)
  223. cell_tuple.append(cell)
  224. col._set_cell_tuple(row, cell_tuple)
  225. def compute_comparisons(self):
  226. """ Compute comparisons.
  227. Invoke this after setting all values.
  228. """
  229. for pos_col_key, comparisons in self._comparison_todo.items():
  230. for col_key, base_col_key in comparisons:
  231. col = self._cols[col_key]
  232. base_col = self._cols[base_col_key]
  233. common_subkpis = set(col.subkpis) & set(base_col.subkpis)
  234. if not common_subkpis:
  235. raise UserError('Columns {} and {} are not comparable'.
  236. format(col.description,
  237. base_col.description))
  238. label = u'{} vs {}'.\
  239. format(col.label, base_col.label)
  240. comparison_col = KpiMatrixCol(label, None, {},
  241. sorted(common_subkpis,
  242. key=lambda s: s.sequence))
  243. for row in self.iter_rows():
  244. cell_tuple = col.get_cell_tuple_for_row(row)
  245. base_cell_tuple = base_col.get_cell_tuple_for_row(row)
  246. if cell_tuple is None and base_cell_tuple is None:
  247. continue
  248. if cell_tuple is None:
  249. vals = [AccountingNone] * len(common_subkpis)
  250. else:
  251. vals = [cell.val for cell in cell_tuple
  252. if cell.subcol.subkpi in common_subkpis]
  253. if base_cell_tuple is None:
  254. base_vals = [AccountingNone] * len(common_subkpis)
  255. else:
  256. base_vals = [cell.val for cell in base_cell_tuple
  257. if cell.subcol.subkpi in common_subkpis]
  258. comparison_cell_tuple = []
  259. for val, base_val, comparison_subcol in \
  260. izip(vals,
  261. base_vals,
  262. comparison_col.iter_subcols()):
  263. # TODO FIXME average factors
  264. delta, delta_r, style_r = \
  265. self._style_model.compare_and_render(
  266. self.lang, row.style_props,
  267. row.kpi.type, row.kpi.compare_method,
  268. val, base_val, 1, 1)
  269. comparison_cell_tuple.append(KpiMatrixCell(
  270. row, comparison_subcol, delta, delta_r, None,
  271. style_r, None))
  272. comparison_col._set_cell_tuple(row, comparison_cell_tuple)
  273. self._comparison_cols[pos_col_key].append(comparison_col)
  274. def iter_rows(self):
  275. """ Iterate rows in display order.
  276. yields KpiMatrixRow.
  277. """
  278. for kpi_row in self._kpi_rows.values():
  279. yield kpi_row
  280. detail_rows = self._detail_rows[kpi_row.kpi].values()
  281. detail_rows = sorted(detail_rows, key=lambda r: r.description)
  282. for detail_row in detail_rows:
  283. yield detail_row
  284. def iter_cols(self):
  285. """ Iterate columns in display order.
  286. yields KpiMatrixCol: one for each column or comparison.
  287. """
  288. for col_key, col in self._cols.items():
  289. yield col
  290. for comparison_col in self._comparison_cols[col_key]:
  291. yield comparison_col
  292. def iter_subcols(self):
  293. """ Iterate sub columns in display order.
  294. yields KpiMatrixSubCol: one for each subkpi in each column
  295. and comparison.
  296. """
  297. for col in self.iter_cols():
  298. for subcol in col.iter_subcols():
  299. yield subcol
  300. def _load_account_names(self):
  301. account_ids = set()
  302. for detail_rows in self._detail_rows.values():
  303. account_ids.update(detail_rows.keys())
  304. account_ids = list(account_ids)
  305. accounts = self._account_model.search([('id', 'in', account_ids)])
  306. self._account_names = {a.id: u'{} {}'.format(a.code, a.name)
  307. for a in accounts}
  308. def get_account_name(self, account_id):
  309. if account_id not in self._account_names:
  310. self._load_account_names()
  311. return self._account_names[account_id]
  312. def as_dict(self):
  313. header = [{'cols': []}, {'cols': []}]
  314. for col in self.iter_cols():
  315. header[0]['cols'].append({
  316. 'label': col.label,
  317. 'description': col.description,
  318. 'colspan': col.colspan,
  319. })
  320. for subcol in col.iter_subcols():
  321. header[1]['cols'].append({
  322. 'label': subcol.label,
  323. 'description': subcol.description,
  324. 'colspan': 1,
  325. })
  326. body = []
  327. for row in self.iter_rows():
  328. row_data = {
  329. 'row_id': row.row_id,
  330. 'parent_row_id': (row.parent_row and
  331. row.parent_row.row_id or None),
  332. 'label': row.label,
  333. 'description': row.description,
  334. 'style': self._style_model.to_css_style(
  335. row.style_props),
  336. 'cells': []
  337. }
  338. for cell in row.iter_cells():
  339. if cell is None:
  340. # TODO use subcol style here
  341. row_data['cells'].append({})
  342. else:
  343. col_data = {
  344. 'val': (cell.val
  345. if cell.val is not AccountingNone else None),
  346. 'val_r': cell.val_rendered,
  347. 'val_c': cell.val_comment,
  348. 'style': self._style_model.to_css_style(
  349. cell.style_props),
  350. }
  351. if cell.drilldown_arg:
  352. col_data['drilldown_arg'] = cell.drilldown_arg
  353. row_data['cells'].append(col_data)
  354. body.append(row_data)
  355. return {
  356. 'header': header,
  357. 'body': body,
  358. }
  359. def _utc_midnight(d, tz_name, add_day=0):
  360. d = fields.Datetime.from_string(d) + datetime.timedelta(days=add_day)
  361. utc_tz = pytz.timezone('UTC')
  362. context_tz = pytz.timezone(tz_name)
  363. local_timestamp = context_tz.localize(d, is_dst=False)
  364. return fields.Datetime.to_string(local_timestamp.astimezone(utc_tz))
  365. def _python_var(var_str):
  366. return re.sub(r'\W|^(?=\d)', '_', var_str).lower()
  367. def _is_valid_python_var(name):
  368. return re.match("[_A-Za-z][_a-zA-Z0-9]*$", name)
  369. class MisReportKpi(models.Model):
  370. """ A KPI is an element (ie a line) of a MIS report.
  371. In addition to a name and description, it has an expression
  372. to compute it based on queries defined in the MIS report.
  373. It also has various informations defining how to render it
  374. (numeric or percentage or a string, a prefix, a suffix, divider) and
  375. how to render comparison of two values of the KPI.
  376. KPI's have a sequence and are ordered inside the MIS report.
  377. """
  378. _name = 'mis.report.kpi'
  379. name = fields.Char(size=32, required=True,
  380. string='Name')
  381. description = fields.Char(required=True,
  382. string='Description',
  383. translate=True)
  384. multi = fields.Boolean()
  385. expression = fields.Char(
  386. compute='_compute_expression',
  387. inverse='_inverse_expression')
  388. expression_ids = fields.One2many('mis.report.kpi.expression', 'kpi_id')
  389. auto_expand_accounts = fields.Boolean(string='Display details by account')
  390. auto_expand_accounts_style_id = fields.Many2one(
  391. string="Style for account detail rows",
  392. comodel_name="mis.report.style",
  393. required=False
  394. )
  395. style_id = fields.Many2one(
  396. string="Style",
  397. comodel_name="mis.report.style",
  398. required=False
  399. )
  400. style_expression = fields.Char(
  401. string='Style expression',
  402. help='An expression that returns a style depending on the KPI value. '
  403. 'Such style is applied on top of the row style.')
  404. type = fields.Selection([(TYPE_NUM, _('Numeric')),
  405. (TYPE_PCT, _('Percentage')),
  406. (TYPE_STR, _('String'))],
  407. required=True,
  408. string='Value type',
  409. default=TYPE_NUM)
  410. compare_method = fields.Selection([(CMP_DIFF, _('Difference')),
  411. (CMP_PCT, _('Percentage')),
  412. (CMP_NONE, _('None'))],
  413. required=True,
  414. string='Comparison Method',
  415. default=CMP_PCT)
  416. sequence = fields.Integer(string='Sequence', default=100)
  417. report_id = fields.Many2one('mis.report',
  418. string='Report',
  419. ondelete='cascade')
  420. _order = 'sequence, id'
  421. @api.one
  422. @api.constrains('name')
  423. def _check_name(self):
  424. if not _is_valid_python_var(self.name):
  425. raise UserError(_('The name must be a valid python identifier'))
  426. @api.onchange('name')
  427. def _onchange_name(self):
  428. if self.name and not _is_valid_python_var(self.name):
  429. return {
  430. 'warning': {
  431. 'title': 'Invalid name %s' % self.name,
  432. 'message': 'The name must be a valid python identifier'
  433. }
  434. }
  435. @api.multi
  436. def _compute_expression(self):
  437. for kpi in self:
  438. l = []
  439. for expression in kpi.expression_ids:
  440. if expression.subkpi_id:
  441. l.append(u'{}\xa0=\xa0{}'.format(
  442. expression.subkpi_id.name, expression.name))
  443. else:
  444. l.append(
  445. expression.name or 'AccountingNone')
  446. kpi.expression = ',\n'.join(l)
  447. @api.multi
  448. def _inverse_expression(self):
  449. for kpi in self:
  450. if kpi.multi:
  451. raise UserError('Can not update a multi kpi from the kpi line')
  452. if kpi.expression_ids:
  453. kpi.expression_ids[0].write({
  454. 'name': kpi.expression,
  455. 'subkpi_id': None})
  456. for expression in kpi.expression_ids[1:]:
  457. expression.unlink()
  458. else:
  459. kpi.write({
  460. 'expression_ids': [(0, 0, {
  461. 'name': kpi.expression
  462. })]
  463. })
  464. @api.onchange('multi')
  465. def _onchange_multi(self):
  466. for kpi in self:
  467. if not kpi.multi:
  468. if kpi.expression_ids:
  469. kpi.expression = kpi.expression_ids[0].name
  470. else:
  471. kpi.expression = None
  472. else:
  473. expressions = []
  474. for subkpi in kpi.report_id.subkpi_ids:
  475. expressions.append((0, 0, {
  476. 'name': kpi.expression,
  477. 'subkpi_id': subkpi.id,
  478. }))
  479. kpi.expression_ids = expressions
  480. @api.onchange('description')
  481. def _onchange_description(self):
  482. """ construct name from description """
  483. if self.description and not self.name:
  484. self.name = _python_var(self.description)
  485. @api.onchange('type')
  486. def _onchange_type(self):
  487. if self.type == TYPE_NUM:
  488. self.compare_method = CMP_PCT
  489. elif self.type == TYPE_PCT:
  490. self.compare_method = CMP_DIFF
  491. elif self.type == TYPE_STR:
  492. self.compare_method = CMP_NONE
  493. def get_expression_for_subkpi(self, subkpi):
  494. for expression in self.expression_ids:
  495. if expression.subkpi_id == subkpi:
  496. return expression.name
  497. class MisReportSubkpi(models.Model):
  498. _name = 'mis.report.subkpi'
  499. _order = 'sequence'
  500. sequence = fields.Integer()
  501. report_id = fields.Many2one('mis.report')
  502. name = fields.Char(size=32, required=True,
  503. string='Name')
  504. description = fields.Char(required=True,
  505. string='Description',
  506. translate=True)
  507. expression_ids = fields.One2many('mis.report.kpi.expression', 'subkpi_id')
  508. @api.one
  509. @api.constrains('name')
  510. def _check_name(self):
  511. if not _is_valid_python_var(self.name):
  512. raise UserError(_('The name must be a valid python identifier'))
  513. @api.onchange('name')
  514. def _onchange_name(self):
  515. if self.name and not _is_valid_python_var(self.name):
  516. return {
  517. 'warning': {
  518. 'title': 'Invalid name %s' % self.name,
  519. 'message': 'The name must be a valid python identifier'
  520. }
  521. }
  522. @api.onchange('description')
  523. def _onchange_description(self):
  524. """ construct name from description """
  525. if self.description and not self.name:
  526. self.name = _python_var(self.description)
  527. @api.multi
  528. def unlink(self):
  529. for subkpi in self:
  530. subkpi.expression_ids.unlink()
  531. return super(MisReportSubkpi, self).unlink()
  532. class MisReportKpiExpression(models.Model):
  533. """ A KPI Expression is an expression of a line of a MIS report Kpi.
  534. It's used to compute the kpi value.
  535. """
  536. _name = 'mis.report.kpi.expression'
  537. _order = 'sequence, name'
  538. sequence = fields.Integer(
  539. related='subkpi_id.sequence',
  540. store=True,
  541. readonly=True)
  542. name = fields.Char(string='Expression')
  543. kpi_id = fields.Many2one('mis.report.kpi')
  544. # TODO FIXME set readonly=True when onchange('subkpi_ids') below works
  545. subkpi_id = fields.Many2one(
  546. 'mis.report.subkpi',
  547. readonly=False)
  548. _sql_constraints = [
  549. ('subkpi_kpi_unique', 'unique(subkpi_id, kpi_id)',
  550. 'Sub KPI must be used once and only once for each KPI'),
  551. ]
  552. class MisReportQuery(models.Model):
  553. """ A query to fetch arbitrary data for a MIS report.
  554. A query works on a model and has a domain and list of fields to fetch.
  555. At runtime, the domain is expanded with a "and" on the date/datetime field.
  556. """
  557. _name = 'mis.report.query'
  558. @api.one
  559. @api.depends('field_ids')
  560. def _compute_field_names(self):
  561. field_names = [field.name for field in self.field_ids]
  562. self.field_names = ', '.join(field_names)
  563. name = fields.Char(size=32, required=True,
  564. string='Name')
  565. model_id = fields.Many2one('ir.model', required=True,
  566. string='Model')
  567. field_ids = fields.Many2many('ir.model.fields', required=True,
  568. string='Fields to fetch')
  569. field_names = fields.Char(compute='_compute_field_names',
  570. string='Fetched fields name')
  571. aggregate = fields.Selection([('sum', _('Sum')),
  572. ('avg', _('Average')),
  573. ('min', _('Min')),
  574. ('max', _('Max'))],
  575. string='Aggregate')
  576. date_field = fields.Many2one('ir.model.fields', required=True,
  577. string='Date field',
  578. domain=[('ttype', 'in',
  579. ('date', 'datetime'))])
  580. domain = fields.Char(string='Domain')
  581. report_id = fields.Many2one('mis.report', string='Report',
  582. ondelete='cascade')
  583. _order = 'name'
  584. @api.one
  585. @api.constrains('name')
  586. def _check_name(self):
  587. if not _is_valid_python_var(self.name):
  588. raise UserError(_('The name must be a valid python identifier'))
  589. class MisReport(models.Model):
  590. """ A MIS report template (without period information)
  591. The MIS report holds:
  592. * a list of explicit queries; the result of each query is
  593. stored in a variable with same name as a query, containing as list
  594. of data structures populated with attributes for each fields to fetch;
  595. when queries have an aggregate method and no fields to group, it returns
  596. a data structure with the aggregated fields
  597. * a list of KPI to be evaluated based on the variables resulting
  598. from the accounting data and queries (KPI expressions can references
  599. queries and accounting expression - see AccoutingExpressionProcessor)
  600. """
  601. _name = 'mis.report'
  602. name = fields.Char(required=True,
  603. string='Name', translate=True)
  604. description = fields.Char(required=False,
  605. string='Description', translate=True)
  606. style_id = fields.Many2one(string="Style",
  607. comodel_name="mis.report.style")
  608. query_ids = fields.One2many('mis.report.query', 'report_id',
  609. string='Queries',
  610. copy=True)
  611. kpi_ids = fields.One2many('mis.report.kpi', 'report_id',
  612. string='KPI\'s',
  613. copy=True)
  614. subkpi_ids = fields.One2many('mis.report.subkpi', 'report_id',
  615. string="Sub KPI",
  616. copy=True)
  617. @api.onchange('subkpi_ids')
  618. def _on_change_subkpi_ids(self):
  619. """ Update kpi expressions when subkpis change on the report,
  620. so the list of kpi expressions is always up-to-date """
  621. for kpi in self.kpi_ids:
  622. if not kpi.multi:
  623. continue
  624. new_subkpis = set([subkpi for subkpi in self.subkpi_ids])
  625. expressions = []
  626. for expression in kpi.expression_ids:
  627. assert expression.subkpi_id # must be true if kpi is multi
  628. if expression.subkpi_id not in self.subkpi_ids:
  629. expressions.append((2, expression.id, None)) # remove
  630. else:
  631. new_subkpis.remove(expression.subkpi_id) # no change
  632. for subkpi in new_subkpis:
  633. # TODO FIXME this does not work, while the remove above works
  634. expressions.append((0, None, {
  635. 'name': False,
  636. 'subkpi_id': subkpi.id,
  637. })) # add empty expressions for new subkpis
  638. if expressions:
  639. kpi.expressions_ids = expressions
  640. @api.multi
  641. def get_wizard_report_action(self):
  642. action = self.env.ref('mis_builder.mis_report_instance_view_action')
  643. res = action.read()[0]
  644. view = self.env.ref('mis_builder.wizard_mis_report_instance_view_form')
  645. res.update({
  646. 'view_id': view.id,
  647. 'views': [(view.id, 'form')],
  648. 'target': 'new',
  649. 'context': {
  650. 'default_report_id': self.id,
  651. 'default_name': self.name,
  652. 'default_temporary': True,
  653. }
  654. })
  655. return res
  656. @api.one
  657. def copy(self, default=None):
  658. default = dict(default or {})
  659. default['name'] = _('%s (copy)') % self.name
  660. return super(MisReport, self).copy(default)
  661. # TODO: kpi name cannot be start with query name
  662. @api.multi
  663. def prepare_kpi_matrix(self):
  664. self.ensure_one()
  665. kpi_matrix = KpiMatrix(self.env)
  666. for kpi in self.kpi_ids:
  667. kpi_matrix.declare_kpi(kpi)
  668. return kpi_matrix
  669. @api.multi
  670. def prepare_aep(self, company):
  671. self.ensure_one()
  672. aep = AEP(self.env)
  673. for kpi in self.kpi_ids:
  674. for expression in kpi.expression_ids:
  675. aep.parse_expr(expression.name)
  676. aep.done_parsing(company)
  677. return aep
  678. @api.multi
  679. def _fetch_queries(self, date_from, date_to,
  680. get_additional_query_filter=None):
  681. self.ensure_one()
  682. res = {}
  683. for query in self.query_ids:
  684. model = self.env[query.model_id.model]
  685. eval_context = {
  686. 'env': self.env,
  687. 'time': time,
  688. 'datetime': datetime,
  689. 'dateutil': dateutil,
  690. # deprecated
  691. 'uid': self.env.uid,
  692. 'context': self.env.context,
  693. }
  694. domain = query.domain and \
  695. safe_eval(query.domain, eval_context) or []
  696. if get_additional_query_filter:
  697. domain.extend(get_additional_query_filter(query))
  698. if query.date_field.ttype == 'date':
  699. domain.extend([(query.date_field.name, '>=', date_from),
  700. (query.date_field.name, '<=', date_to)])
  701. else:
  702. datetime_from = _utc_midnight(
  703. date_from, self._context.get('tz', 'UTC'))
  704. datetime_to = _utc_midnight(
  705. date_to, self._context.get('tz', 'UTC'), add_day=1)
  706. domain.extend([(query.date_field.name, '>=', datetime_from),
  707. (query.date_field.name, '<', datetime_to)])
  708. field_names = [f.name for f in query.field_ids]
  709. all_stored = all([model._fields[f].store for f in field_names])
  710. if not query.aggregate:
  711. data = model.search_read(domain, field_names)
  712. res[query.name] = [AutoStruct(**d) for d in data]
  713. elif query.aggregate == 'sum' and all_stored:
  714. # use read_group to sum stored fields
  715. data = model.read_group(
  716. domain, field_names, [])
  717. s = AutoStruct(count=data[0]['__count'])
  718. for field_name in field_names:
  719. try:
  720. v = data[0][field_name]
  721. except KeyError:
  722. _logger.error('field %s not found in read_group '
  723. 'for %s; not summable?',
  724. field_name, model._name)
  725. v = AccountingNone
  726. setattr(s, field_name, v)
  727. res[query.name] = s
  728. else:
  729. data = model.search_read(domain, field_names)
  730. s = AutoStruct(count=len(data))
  731. if query.aggregate == 'min':
  732. agg = _min
  733. elif query.aggregate == 'max':
  734. agg = _max
  735. elif query.aggregate == 'avg':
  736. agg = _avg
  737. elif query.aggregate == 'sum':
  738. agg = _sum
  739. for field_name in field_names:
  740. setattr(s, field_name,
  741. agg([d[field_name] for d in data]))
  742. res[query.name] = s
  743. return res
  744. @api.multi
  745. def declare_and_compute_period(self, kpi_matrix,
  746. col_key,
  747. col_label,
  748. col_description,
  749. aep,
  750. date_from, date_to,
  751. target_move,
  752. company,
  753. subkpis_filter=None,
  754. get_additional_move_line_filter=None,
  755. get_additional_query_filter=None):
  756. """ Evaluate a report for a given period, populating a KpiMatrix.
  757. :param kpi_matrix: the KpiMatrix object to be populated created
  758. with prepare_kpi_matrix()
  759. :param col_key: the period key to use when populating the KpiMatrix
  760. :param aep: an AccountingExpressionProcessor instance created
  761. using _prepare_aep()
  762. :param date_from, date_to: the starting and ending date
  763. :param target_move: all|posted
  764. :param company:
  765. :param get_additional_move_line_filter: a bound method that takes
  766. no arguments and returns
  767. a domain compatible with
  768. account.move.line
  769. :param get_additional_query_filter: a bound method that takes a single
  770. query argument and returns a
  771. domain compatible with the query
  772. underlying model
  773. """
  774. self.ensure_one()
  775. locals_dict = {
  776. 'sum': _sum,
  777. 'min': _min,
  778. 'max': _max,
  779. 'len': len,
  780. 'avg': _avg,
  781. 'AccountingNone': AccountingNone,
  782. 'SimpleArray': SimpleArray,
  783. }
  784. # fetch non-accounting queries
  785. locals_dict.update(self._fetch_queries(
  786. date_from, date_to, get_additional_query_filter))
  787. # use AEP to do the accounting queries
  788. additional_move_line_filter = None
  789. if get_additional_move_line_filter:
  790. additional_move_line_filter = get_additional_move_line_filter()
  791. aep.do_queries(company,
  792. date_from, date_to,
  793. target_move,
  794. additional_move_line_filter)
  795. if subkpis_filter:
  796. subkpis = [subkpi for subkpi in self.subkpi_ids
  797. if subkpi in subkpis_filter]
  798. else:
  799. subkpis = self.subkpi_ids
  800. kpi_matrix.declare_col(col_key,
  801. col_label, col_description,
  802. locals_dict, subkpis)
  803. compute_queue = self.kpi_ids
  804. recompute_queue = []
  805. while True:
  806. for kpi in compute_queue:
  807. # build the list of expressions for this kpi
  808. expressions = []
  809. for expression in kpi.expression_ids:
  810. if expression.subkpi_id and \
  811. subkpis_filter and \
  812. expression.subkpi_id not in subkpis_filter:
  813. continue
  814. expressions.append(expression.name)
  815. vals = []
  816. drilldown_args = []
  817. try:
  818. for expression in expressions:
  819. replaced_expr = aep.replace_expr(expression)
  820. vals.append(
  821. mis_safe_eval(replaced_expr, locals_dict))
  822. if replaced_expr != expression:
  823. drilldown_args.append({
  824. 'period_id': col_key,
  825. 'expr': expression,
  826. })
  827. else:
  828. drilldown_args.append(None)
  829. except NameError:
  830. recompute_queue.append(kpi)
  831. break
  832. else:
  833. # no error, set it in locals_dict so it can be used
  834. # in computing other kpis
  835. if len(expressions) == 1:
  836. locals_dict[kpi.name] = vals[0]
  837. else:
  838. locals_dict[kpi.name] = SimpleArray(vals)
  839. kpi_matrix.set_values(
  840. kpi, col_key, vals, drilldown_args)
  841. if not kpi.auto_expand_accounts:
  842. continue
  843. for account_id, replaced_exprs in \
  844. aep.replace_exprs_by_account_id(expressions):
  845. vals = []
  846. drilldown_args = []
  847. for expression, replaced_expr in \
  848. izip(expressions, replaced_exprs):
  849. vals.append(mis_safe_eval(replaced_expr, locals_dict))
  850. if replaced_expr != expression:
  851. drilldown_args.append({
  852. 'period_id': col_key,
  853. 'expr': expression,
  854. 'account_id': account_id
  855. })
  856. else:
  857. drilldown_args.append(None)
  858. kpi_matrix.set_values_detail_account(
  859. kpi, col_key, account_id, vals, drilldown_args)
  860. if len(recompute_queue) == 0:
  861. # nothing to recompute, we are done
  862. break
  863. if len(recompute_queue) == len(compute_queue):
  864. # could not compute anything in this iteration
  865. # (ie real Name errors or cyclic dependency)
  866. # so we stop trying
  867. break
  868. # try again
  869. compute_queue = recompute_queue
  870. recompute_queue = []