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.

1295 lines
49 KiB

9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 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. from collections import defaultdict, OrderedDict
  5. import datetime
  6. import dateutil
  7. import logging
  8. import re
  9. import time
  10. import traceback
  11. import pytz
  12. from openerp import api, exceptions, fields, models, _
  13. from openerp.tools.safe_eval import safe_eval
  14. from .aep import AccountingExpressionProcessor as AEP
  15. from .aggregate import _sum, _avg, _min, _max
  16. from .accounting_none import AccountingNone
  17. from openerp.exceptions import UserError
  18. from .simple_array import SimpleArray
  19. _logger = logging.getLogger(__name__)
  20. class DataError(Exception):
  21. def __init__(self, name, msg):
  22. self.name = name
  23. self.msg = msg
  24. class AutoStruct(object):
  25. def __init__(self, **kwargs):
  26. for k, v in kwargs.items():
  27. setattr(self, k, v)
  28. class KpiMatrix(object):
  29. """ This object holds the computation results in a way
  30. that can be browsed easily for rendering """
  31. def __init__(self):
  32. # { period: {kpi: vals}
  33. self._kpi_vals = defaultdict(dict)
  34. # { period: {kpi: {account_id: vals}}}
  35. self._kpi_exploded_vals = defaultdict(dict)
  36. # { period: localdict }
  37. self._localdict = {}
  38. # { kpi: set(account_ids) }
  39. self._kpis = OrderedDict()
  40. # { account_id: account name }
  41. self._account_names_by_id = {}
  42. def set_kpi_vals(self, period, kpi, vals):
  43. """ Set the values for a kpi in a period
  44. vals is a list of sub-kpi values.
  45. """
  46. self._kpi_vals[period][kpi] = vals
  47. if kpi not in self._kpis:
  48. self._kpis[kpi] = set()
  49. def set_kpi_exploded_vals(self, period, kpi, account_id, vals):
  50. """ Set the detail values for a kpi in a period for a GL account
  51. This is used by the automatic details mechanism.
  52. vals is a list of sub-kpi values.
  53. """
  54. exploded_vals = self._kpi_exploded_vals[period]
  55. if kpi not in exploded_vals:
  56. exploded_vals[kpi] = {}
  57. exploded_vals[kpi][account_id] = vals
  58. self._kpis[kpi].add(account_id)
  59. def set_localdict(self, period, localdict):
  60. # TODO FIXME to be removed when we have styles
  61. self._localdict[period] = localdict
  62. def get_localdict(self, period):
  63. # TODO FIXME to be removed when we have styles
  64. return self._localdict[period]
  65. def iter_kpi_vals(self, period):
  66. """ Iterate kpi values, including auto-expanded details by account
  67. It yields, in no specific order:
  68. * kpi technical name
  69. * kpi object
  70. * subkpi values tuple
  71. """
  72. for kpi, vals in self._kpi_vals[period].iteritems():
  73. yield kpi.name, kpi, vals
  74. kpi_exploded_vals = self._kpi_exploded_vals[period]
  75. if kpi not in kpi_exploded_vals:
  76. continue
  77. for account_id, account_id_vals in \
  78. kpi_exploded_vals[kpi].iteritems():
  79. yield "%s:%s" % (kpi.name, account_id), kpi, account_id_vals
  80. def iter_kpis(self):
  81. """ Iterate kpis, including auto-expanded details by accounts
  82. It yields, in display order:
  83. * kpi technical name
  84. * kpi display name
  85. * kpi object
  86. """
  87. for kpi, account_ids in self._kpis.iteritems():
  88. yield kpi.name, kpi.description, kpi
  89. for account_id in sorted(account_ids, key=self.get_account_name):
  90. yield "%s:%s" % (kpi.name, account_id), \
  91. self.get_account_name(account_id), kpi
  92. def get_exploded_account_ids(self):
  93. """ Get the list of auto-expanded account ids
  94. It returns the complete list, across all periods and kpis.
  95. This method must be called after setting all kpi values
  96. using set_kpi_vals and set_exploded_kpi_vals.
  97. """
  98. res = set()
  99. for kpi, account_ids in self._kpis.iteritems():
  100. res.update(account_ids)
  101. return list(res)
  102. def load_account_names(self, account_obj):
  103. """ Load account names for all exploded account ids
  104. This method must be called after setting all kpi values
  105. using set_kpi_vals and set_exploded_kpi_vals, and before
  106. calling get_account_name().
  107. """
  108. account_data = account_obj.browse(self.get_exploded_account_ids())
  109. self._account_names_by_id = {a.id: u"{} {}".format(a.code, a.name)
  110. for a in account_data}
  111. def get_account_name(self, account_id):
  112. """ Get account display name from it's id
  113. This method must be called after loading account names with
  114. load_account_names().
  115. """
  116. return self._account_names_by_id.get(account_id, account_id)
  117. def _get_selection_label(selection, value):
  118. for v, l in selection:
  119. if v == value:
  120. return l
  121. return ''
  122. def _utc_midnight(d, tz_name, add_day=0):
  123. d = fields.Datetime.from_string(d) + datetime.timedelta(days=add_day)
  124. utc_tz = pytz.timezone('UTC')
  125. context_tz = pytz.timezone(tz_name)
  126. local_timestamp = context_tz.localize(d, is_dst=False)
  127. return fields.Datetime.to_string(local_timestamp.astimezone(utc_tz))
  128. def _python_var(var_str):
  129. return re.sub(r'\W|^(?=\d)', '_', var_str).lower()
  130. def _is_valid_python_var(name):
  131. return re.match("[_A-Za-z][_a-zA-Z0-9]*$", name)
  132. class MisReportKpi(models.Model):
  133. """ A KPI is an element (ie a line) of a MIS report.
  134. In addition to a name and description, it has an expression
  135. to compute it based on queries defined in the MIS report.
  136. It also has various informations defining how to render it
  137. (numeric or percentage or a string, a prefix, a suffix, divider) and
  138. how to render comparison of two values of the KPI.
  139. KPI's have a sequence and are ordered inside the MIS report.
  140. """
  141. _name = 'mis.report.kpi'
  142. name = fields.Char(size=32, required=True,
  143. string='Name')
  144. description = fields.Char(required=True,
  145. string='Description',
  146. translate=True)
  147. multi = fields.Boolean()
  148. expression = fields.Char(
  149. compute='_compute_expression',
  150. inverse='_inverse_expression')
  151. expression_ids = fields.One2many('mis.report.kpi.expression', 'kpi_id')
  152. auto_expand_accounts = fields.Boolean(string='Display details by account')
  153. style = fields.Many2one(
  154. string="Default style for KPI",
  155. comodel_name="mis.report.kpi.style",
  156. required=False
  157. )
  158. style_expression = fields.Char(
  159. string='Style expression',
  160. help='An expression that returns a style name for the kpi style')
  161. type = fields.Selection([('num', _('Numeric')),
  162. ('pct', _('Percentage')),
  163. ('str', _('String'))],
  164. required=True,
  165. string='Type',
  166. default='num')
  167. divider = fields.Selection([('1e-6', _('µ')),
  168. ('1e-3', _('m')),
  169. ('1', _('1')),
  170. ('1e3', _('k')),
  171. ('1e6', _('M'))],
  172. string='Factor',
  173. default='1')
  174. dp = fields.Integer(string='Rounding', default=0)
  175. prefix = fields.Char(size=16, string='Prefix')
  176. suffix = fields.Char(size=16, string='Suffix')
  177. compare_method = fields.Selection([('diff', _('Difference')),
  178. ('pct', _('Percentage')),
  179. ('none', _('None'))],
  180. required=True,
  181. string='Comparison Method',
  182. default='pct')
  183. sequence = fields.Integer(string='Sequence', default=100)
  184. report_id = fields.Many2one('mis.report',
  185. string='Report',
  186. ondelete='cascade')
  187. _order = 'sequence, id'
  188. @api.one
  189. @api.constrains('name')
  190. def _check_name(self):
  191. if not _is_valid_python_var(self.name):
  192. raise exceptions.Warning(_('The name must be a valid '
  193. 'python identifier'))
  194. @api.onchange('name')
  195. def _onchange_name(self):
  196. if self.name and not _is_valid_python_var(self.name):
  197. return {
  198. 'warning': {
  199. 'title': 'Invalid name %s' % self.name,
  200. 'message': 'The name must be a valid python identifier'
  201. }
  202. }
  203. @api.multi
  204. def _compute_expression(self):
  205. for kpi in self:
  206. l = []
  207. for expression in kpi.expression_ids:
  208. if expression.subkpi_id:
  209. l.append('{}={}'.format(
  210. expression.subkpi_id.name, expression.name))
  211. else:
  212. l.append(
  213. expression.name)
  214. kpi.expression = ',\n'.join(l)
  215. @api.multi
  216. def _inverse_expression(self):
  217. for kpi in self:
  218. if kpi.multi:
  219. raise UserError('Can not update a multi kpi from the kpi line')
  220. if kpi.expression_ids:
  221. kpi.expression_ids[0].write({
  222. 'name': kpi.expression,
  223. 'subkpi_id': None})
  224. for expression in kpi.expression_ids[1:]:
  225. expression.unlink()
  226. else:
  227. kpi.write({
  228. 'expression_ids': [(0, 0, {
  229. 'name': kpi.expression
  230. })]
  231. })
  232. @api.onchange('multi')
  233. def _onchange_multi(self):
  234. for kpi in self:
  235. if not kpi.multi:
  236. if kpi.expression_ids:
  237. kpi.expression = kpi.expression_ids[0].name
  238. else:
  239. kpi.expression = None
  240. else:
  241. expressions = []
  242. for subkpi in kpi.report_id.subkpi_ids:
  243. expressions.append((0, 0, {
  244. 'name': kpi.expression,
  245. 'subkpi_id': subkpi.id,
  246. }))
  247. kpi.expression_ids = expressions
  248. @api.onchange('description')
  249. def _onchange_description(self):
  250. """ construct name from description """
  251. if self.description and not self.name:
  252. self.name = _python_var(self.description)
  253. @api.onchange('type')
  254. def _onchange_type(self):
  255. if self.type == 'num':
  256. self.compare_method = 'pct'
  257. self.divider = '1'
  258. self.dp = 0
  259. elif self.type == 'pct':
  260. self.compare_method = 'diff'
  261. self.divider = '1'
  262. self.dp = 0
  263. elif self.type == 'str':
  264. self.compare_method = 'none'
  265. self.divider = ''
  266. self.dp = 0
  267. def render(self, lang_id, value):
  268. """ render a KPI value as a unicode string, ready for display """
  269. assert len(self) == 1
  270. if value is None or value is AccountingNone:
  271. return ''
  272. elif self.type == 'num':
  273. return self._render_num(lang_id, value, self.divider,
  274. self.dp, self.prefix, self.suffix)
  275. elif self.type == 'pct':
  276. return self._render_num(lang_id, value, 0.01,
  277. self.dp, '', '%')
  278. else:
  279. return unicode(value)
  280. def render_comparison(self, lang_id, value, base_value,
  281. average_value, average_base_value):
  282. """ render the comparison of two KPI values, ready for display
  283. If the difference is 0, an empty string is returned.
  284. """
  285. assert len(self) == 1
  286. if value is None:
  287. value = AccountingNone
  288. if base_value is None:
  289. base_value = AccountingNone
  290. if self.type == 'pct':
  291. delta = value - base_value
  292. if delta and round(delta, self.dp) != 0:
  293. return self._render_num(
  294. lang_id,
  295. delta,
  296. 0.01, self.dp, '', _('pp'),
  297. sign='+')
  298. elif self.type == 'num':
  299. if value and average_value:
  300. value = value / float(average_value)
  301. if base_value and average_base_value:
  302. base_value = base_value / float(average_base_value)
  303. if self.compare_method == 'diff':
  304. delta = value - base_value
  305. if delta and round(delta, self.dp) != 0:
  306. return self._render_num(
  307. lang_id,
  308. delta,
  309. self.divider, self.dp, self.prefix, self.suffix,
  310. sign='+')
  311. elif self.compare_method == 'pct':
  312. if base_value and round(base_value, self.dp) != 0:
  313. delta = (value - base_value) / abs(base_value)
  314. if delta and round(delta, self.dp) != 0:
  315. return self._render_num(
  316. lang_id,
  317. delta,
  318. 0.01, self.dp, '', '%',
  319. sign='+')
  320. return ''
  321. def _render_num(self, lang_id, value, divider,
  322. dp, prefix, suffix, sign='-'):
  323. divider_label = _get_selection_label(
  324. self._columns['divider'].selection, divider)
  325. if divider_label == '1':
  326. divider_label = ''
  327. # format number following user language
  328. value = round(value / float(divider or 1), dp) or 0
  329. value = self.env['res.lang'].browse(lang_id).format(
  330. '%%%s.%df' % (sign, dp),
  331. value,
  332. grouping=True)
  333. value = u'%s\N{NARROW NO-BREAK SPACE}%s\N{NO-BREAK SPACE}%s%s' % \
  334. (prefix or '', value, divider_label, suffix or '')
  335. value = value.replace('-', u'\N{NON-BREAKING HYPHEN}')
  336. return value
  337. class MisReportSubkpi(models.Model):
  338. _name = 'mis.report.subkpi'
  339. _order = 'sequence'
  340. sequence = fields.Integer()
  341. report_id = fields.Many2one('mis.report')
  342. name = fields.Char(required=True)
  343. expression_ids = fields.One2many('mis.report.kpi.expression', 'subkpi_id')
  344. @api.multi
  345. def unlink(self):
  346. for subkpi in self:
  347. subkpi.expression_ids.unlink()
  348. return super(MisReportSubkpi, self).unlink()
  349. class MisReportKpiExpression(models.Model):
  350. """ A KPI Expression is an expression of a line of a MIS report Kpi.
  351. It's used to compute the kpi value.
  352. """
  353. _name = 'mis.report.kpi.expression'
  354. _order = 'sequence, name'
  355. sequence = fields.Integer(
  356. related='subkpi_id.sequence',
  357. store=True,
  358. readonly=True)
  359. name = fields.Char(string='Expression')
  360. kpi_id = fields.Many2one('mis.report.kpi')
  361. subkpi_id = fields.Many2one(
  362. 'mis.report.subkpi',
  363. readonly=True)
  364. class MisReportQuery(models.Model):
  365. """ A query to fetch arbitrary data for a MIS report.
  366. A query works on a model and has a domain and list of fields to fetch.
  367. At runtime, the domain is expanded with a "and" on the date/datetime field.
  368. """
  369. _name = 'mis.report.query'
  370. @api.one
  371. @api.depends('field_ids')
  372. def _compute_field_names(self):
  373. field_names = [field.name for field in self.field_ids]
  374. self.field_names = ', '.join(field_names)
  375. name = fields.Char(size=32, required=True,
  376. string='Name')
  377. model_id = fields.Many2one('ir.model', required=True,
  378. string='Model')
  379. field_ids = fields.Many2many('ir.model.fields', required=True,
  380. string='Fields to fetch')
  381. field_names = fields.Char(compute='_compute_field_names',
  382. string='Fetched fields name')
  383. aggregate = fields.Selection([('sum', _('Sum')),
  384. ('avg', _('Average')),
  385. ('min', _('Min')),
  386. ('max', _('Max'))],
  387. string='Aggregate')
  388. date_field = fields.Many2one('ir.model.fields', required=True,
  389. string='Date field',
  390. domain=[('ttype', 'in',
  391. ('date', 'datetime'))])
  392. domain = fields.Char(string='Domain')
  393. report_id = fields.Many2one('mis.report', string='Report',
  394. ondelete='cascade')
  395. _order = 'name'
  396. @api.one
  397. @api.constrains('name')
  398. def _check_name(self):
  399. if not _is_valid_python_var(self.name):
  400. raise exceptions.Warning(_('The name must be a valid '
  401. 'python identifier'))
  402. class MisReport(models.Model):
  403. """ A MIS report template (without period information)
  404. The MIS report holds:
  405. * a list of explicit queries; the result of each query is
  406. stored in a variable with same name as a query, containing as list
  407. of data structures populated with attributes for each fields to fetch;
  408. when queries have an aggregate method and no fields to group, it returns
  409. a data structure with the aggregated fields
  410. * a list of KPI to be evaluated based on the variables resulting
  411. from the accounting data and queries (KPI expressions can references
  412. queries and accounting expression - see AccoutingExpressionProcessor)
  413. """
  414. _name = 'mis.report'
  415. name = fields.Char(required=True,
  416. string='Name', translate=True)
  417. description = fields.Char(required=False,
  418. string='Description', translate=True)
  419. query_ids = fields.One2many('mis.report.query', 'report_id',
  420. string='Queries',
  421. copy=True)
  422. kpi_ids = fields.One2many('mis.report.kpi', 'report_id',
  423. string='KPI\'s',
  424. copy=True)
  425. subkpi_ids = fields.One2many(
  426. 'mis.report.subkpi',
  427. 'report_id',
  428. string="Sub KPI")
  429. @api.multi
  430. def get_wizard_report_action(self):
  431. action = self.env.ref('mis_builder.mis_report_instance_view_action')
  432. res = action.read()[0]
  433. view = self.env.ref('mis_builder.wizard_mis_report_instance_view_form')
  434. res.update({
  435. 'view_id': view.id,
  436. 'views': [(view.id, 'form')],
  437. 'target': 'new',
  438. 'context': {
  439. 'default_report_id': self.id,
  440. 'default_name': self.name,
  441. 'default_temporary': True,
  442. }
  443. })
  444. return res
  445. @api.one
  446. def copy(self, default=None):
  447. default = dict(default or {})
  448. default['name'] = _('%s (copy)') % self.name
  449. return super(MisReport, self).copy(default)
  450. # TODO: kpi name cannot be start with query name
  451. @api.multi
  452. def _prepare_aep(self, company):
  453. self.ensure_one()
  454. aep = AEP(self.env)
  455. for kpi in self.kpi_ids:
  456. aep.parse_expr(kpi.expression)
  457. aep.done_parsing(company)
  458. return aep
  459. @api.multi
  460. def _fetch_queries(self, date_from, date_to,
  461. get_additional_query_filter=None):
  462. self.ensure_one()
  463. res = {}
  464. for query in self.query_ids:
  465. model = self.env[query.model_id.model]
  466. eval_context = {
  467. 'env': self.env,
  468. 'time': time,
  469. 'datetime': datetime,
  470. 'dateutil': dateutil,
  471. # deprecated
  472. 'uid': self.env.uid,
  473. 'context': self.env.context,
  474. }
  475. domain = query.domain and \
  476. safe_eval(query.domain, eval_context) or []
  477. if get_additional_query_filter:
  478. domain.extend(get_additional_query_filter(query))
  479. if query.date_field.ttype == 'date':
  480. domain.extend([(query.date_field.name, '>=', date_from),
  481. (query.date_field.name, '<=', date_to)])
  482. else:
  483. datetime_from = _utc_midnight(
  484. date_from, self._context.get('tz', 'UTC'))
  485. datetime_to = _utc_midnight(
  486. date_to, self._context.get('tz', 'UTC'), add_day=1)
  487. domain.extend([(query.date_field.name, '>=', datetime_from),
  488. (query.date_field.name, '<', datetime_to)])
  489. field_names = [f.name for f in query.field_ids]
  490. if not query.aggregate:
  491. data = model.search_read(domain, field_names)
  492. res[query.name] = [AutoStruct(**d) for d in data]
  493. elif query.aggregate == 'sum':
  494. data = model.read_group(
  495. domain, field_names, [])
  496. s = AutoStruct(count=data[0]['__count'])
  497. for field_name in field_names:
  498. v = data[0][field_name]
  499. setattr(s, field_name, v)
  500. res[query.name] = s
  501. else:
  502. data = model.search_read(domain, field_names)
  503. s = AutoStruct(count=len(data))
  504. if query.aggregate == 'min':
  505. agg = _min
  506. elif query.aggregate == 'max':
  507. agg = _max
  508. elif query.aggregate == 'avg':
  509. agg = _avg
  510. for field_name in field_names:
  511. setattr(s, field_name,
  512. agg([d[field_name] for d in data]))
  513. res[query.name] = s
  514. return res
  515. @api.multi
  516. def _compute(self, kpi_matrix, kpi_matrix_period,
  517. lang_id, aep,
  518. date_from, date_to,
  519. target_move,
  520. company,
  521. subkpis_filter,
  522. get_additional_move_line_filter=None,
  523. get_additional_query_filter=None):
  524. """ Evaluate a report for a given period, populating a KpiMatrix.
  525. :param kpi_matrix: the KpiMatrix object to be populated
  526. :param kpi_matrix_period: the period key to use when populating
  527. the KpiMatrix
  528. :param lang_id: id of a res.lang object
  529. :param aep: an AccountingExpressionProcessor instance created
  530. using _prepare_aep()
  531. :param date_from, date_to: the starting and ending date
  532. :param target_move: all|posted
  533. :param company:
  534. :param get_additional_move_line_filter: a bound method that takes
  535. no arguments and returns
  536. a domain compatible with
  537. account.move.line
  538. :param get_additional_query_filter: a bound method that takes a single
  539. query argument and returns a
  540. domain compatible with the query
  541. underlying model
  542. For each kpi, it calls set_kpi_vals and set_kpi_exploded_vals
  543. with vals being a tuple with the evaluation
  544. result for sub-kpis, or a DataError object if the evaluation failed.
  545. When done, it also calls set_localdict to store the local values
  546. that served for the computation of the period.
  547. """
  548. self.ensure_one()
  549. localdict = {
  550. 'registry': self.pool,
  551. 'sum': _sum,
  552. 'min': _min,
  553. 'max': _max,
  554. 'len': len,
  555. 'avg': _avg,
  556. 'AccountingNone': AccountingNone,
  557. }
  558. localdict.update(self._fetch_queries(
  559. date_from, date_to, get_additional_query_filter))
  560. additional_move_line_filter = None
  561. if get_additional_move_line_filter:
  562. additional_move_line_filter = get_additional_move_line_filter()
  563. aep.do_queries(date_from, date_to,
  564. target_move,
  565. company,
  566. additional_move_line_filter)
  567. compute_queue = self.kpi_ids
  568. recompute_queue = []
  569. while True:
  570. for kpi in compute_queue:
  571. vals = []
  572. has_error = False
  573. for expression in kpi.expression_ids:
  574. if expression.subkpi_id and \
  575. subkpis_filter and \
  576. expression.subkpi_id not in subkpis_filter:
  577. continue
  578. try:
  579. kpi_eval_expression = aep.replace_expr(expression.name)
  580. vals.append(safe_eval(kpi_eval_expression, localdict))
  581. except ZeroDivisionError:
  582. has_error = True
  583. vals.append(DataError(
  584. '#DIV/0',
  585. '\n\n%s' % (traceback.format_exc(),)))
  586. except (NameError, ValueError):
  587. has_error = True
  588. recompute_queue.append(kpi)
  589. vals.append(DataError(
  590. '#ERR',
  591. '\n\n%s' % (traceback.format_exc(),)))
  592. except:
  593. has_error = True
  594. vals.append(DataError(
  595. '#ERR',
  596. '\n\n%s' % (traceback.format_exc(),)))
  597. if len(vals) == 1 and isinstance(vals[0], SimpleArray):
  598. vals = vals[0]
  599. else:
  600. vals = SimpleArray(vals)
  601. kpi_matrix.set_kpi_vals(kpi_matrix_period, kpi, vals)
  602. if has_error:
  603. continue
  604. # no error, set it in localdict so it can be used
  605. # in computing other kpis
  606. localdict[kpi.name] = vals
  607. # let's compute the exploded values by account
  608. # we assume there will be no errors, because it is a
  609. # the same as the kpi, just filtered on one account;
  610. # I'd say if we have an exception in this part, it's bug...
  611. if not kpi.auto_expand_accounts:
  612. continue
  613. for account_id in aep.get_accounts_in_expr(kpi.expression):
  614. account_id_vals = []
  615. for expression in kpi.expression_ids:
  616. if expression.subkpi_id and \
  617. subkpis_filter and \
  618. expression.subkpi_id not in subkpis_filter:
  619. continue
  620. kpi_eval_expression = \
  621. aep.replace_expr(expression.name,
  622. account_ids_filter=[account_id])
  623. account_id_vals.\
  624. append(safe_eval(kpi_eval_expression, localdict))
  625. kpi_matrix.set_kpi_exploded_vals(kpi_matrix_period, kpi,
  626. account_id,
  627. account_id_vals)
  628. if len(recompute_queue) == 0:
  629. # nothing to recompute, we are done
  630. break
  631. if len(recompute_queue) == len(compute_queue):
  632. # could not compute anything in this iteration
  633. # (ie real Value errors or cyclic dependency)
  634. # so we stop trying
  635. break
  636. # try again
  637. compute_queue = recompute_queue
  638. recompute_queue = []
  639. kpi_matrix.set_localdict(kpi_matrix_period, localdict)
  640. class MisReportInstancePeriod(models.Model):
  641. """ A MIS report instance has the logic to compute
  642. a report template for a given date period.
  643. Periods have a duration (day, week, fiscal period) and
  644. are defined as an offset relative to a pivot date.
  645. """
  646. @api.one
  647. @api.depends('report_instance_id.pivot_date', 'type', 'offset',
  648. 'duration', 'report_instance_id.comparison_mode')
  649. def _compute_dates(self):
  650. self.date_from = False
  651. self.date_to = False
  652. self.valid = False
  653. report = self.report_instance_id
  654. d = fields.Date.from_string(report.pivot_date)
  655. if not report.comparison_mode:
  656. self.date_from = report.date_from
  657. self.date_to = report.date_to
  658. self.valid = True
  659. elif self.mode == 'fix':
  660. self.date_from = self.manual_date_from
  661. self.date_to = self.manual_date_to
  662. self.valid = True
  663. elif self.type == 'd':
  664. date_from = d + datetime.timedelta(days=self.offset)
  665. date_to = date_from + \
  666. datetime.timedelta(days=self.duration - 1)
  667. self.date_from = fields.Date.to_string(date_from)
  668. self.date_to = fields.Date.to_string(date_to)
  669. self.valid = True
  670. elif self.type == 'w':
  671. date_from = d - datetime.timedelta(d.weekday())
  672. date_from = date_from + datetime.timedelta(days=self.offset * 7)
  673. date_to = date_from + \
  674. datetime.timedelta(days=(7 * self.duration) - 1)
  675. self.date_from = fields.Date.to_string(date_from)
  676. self.date_to = fields.Date.to_string(date_to)
  677. self.valid = True
  678. elif self.type == 'date_range':
  679. date_range_obj = self.env['date.range']
  680. current_periods = date_range_obj.search(
  681. [('type_id', '=', self.date_range_type_id.id),
  682. ('date_start', '<=', d),
  683. ('date_end', '>=', d),
  684. ('company_id', '=', self.report_instance_id.company_id.id)])
  685. if current_periods:
  686. all_periods = date_range_obj.search(
  687. [('type_id', '=', self.date_range_type_id.id),
  688. ('company_id', '=',
  689. self.report_instance_id.company_id.id)],
  690. order='date_start')
  691. all_period_ids = [p.id for p in all_periods]
  692. p = all_period_ids.index(current_periods[0].id) + self.offset
  693. if p >= 0 and p + self.duration <= len(all_period_ids):
  694. periods = all_periods[p:p + self.duration]
  695. self.date_from = periods[0].date_start
  696. self.date_to = periods[-1].date_end
  697. self.valid = True
  698. _name = 'mis.report.instance.period'
  699. name = fields.Char(size=32, required=True,
  700. string='Description', translate=True)
  701. mode = fields.Selection([('fix', 'Fix'),
  702. ('relative', 'Relative'),
  703. ], required=True,
  704. default='fix')
  705. type = fields.Selection([('d', _('Day')),
  706. ('w', _('Week')),
  707. ('date_range', _('Date Range'))
  708. ],
  709. required=True,
  710. string='Period type')
  711. date_range_type_id = fields.Many2one(
  712. comodel_name='date.range.type', string='Date Range Type')
  713. offset = fields.Integer(string='Offset',
  714. help='Offset from current period',
  715. default=-1)
  716. duration = fields.Integer(string='Duration',
  717. help='Number of periods',
  718. default=1)
  719. date_from = fields.Date(compute='_compute_dates', string="From")
  720. date_to = fields.Date(compute='_compute_dates', string="To")
  721. manual_date_from = fields.Date(string="From")
  722. manual_date_to = fields.Date(string="To")
  723. date_range_id = fields.Many2one(
  724. comodel_name='date.range',
  725. string='Date Range')
  726. valid = fields.Boolean(compute='_compute_dates',
  727. type='boolean',
  728. string='Valid')
  729. sequence = fields.Integer(string='Sequence', default=100)
  730. report_instance_id = fields.Many2one('mis.report.instance',
  731. string='Report Instance',
  732. ondelete='cascade')
  733. comparison_column_ids = fields.Many2many(
  734. comodel_name='mis.report.instance.period',
  735. relation='mis_report_instance_period_rel',
  736. column1='period_id',
  737. column2='compare_period_id',
  738. string='Compare with')
  739. normalize_factor = fields.Integer(
  740. string='Factor',
  741. help='Factor to use to normalize the period (used in comparison',
  742. default=1)
  743. subkpi_ids = fields.Many2many(
  744. 'mis.report.subkpi',
  745. string="Sub KPI Filter")
  746. _order = 'sequence, id'
  747. _sql_constraints = [
  748. ('duration', 'CHECK (duration>0)',
  749. 'Wrong duration, it must be positive!'),
  750. ('normalize_factor', 'CHECK (normalize_factor>0)',
  751. 'Wrong normalize factor, it must be positive!'),
  752. ('name_unique', 'unique(name, report_instance_id)',
  753. 'Period name should be unique by report'),
  754. ]
  755. @api.onchange('date_range_id')
  756. def onchange_date_range(self):
  757. for record in self:
  758. record.manual_date_from = record.date_range_id.date_start
  759. record.manual_date_to = record.date_range_id.date_end
  760. @api.multi
  761. def _get_additional_move_line_filter(self):
  762. """ Prepare a filter to apply on all move lines
  763. This filter is applied with a AND operator on all
  764. accounting expression domains. This hook is intended
  765. to be inherited, and is useful to implement filtering
  766. on analytic dimensions or operational units.
  767. Returns an Odoo domain expression (a python list)
  768. compatible with account.move.line."""
  769. self.ensure_one()
  770. return []
  771. @api.multi
  772. def _get_additional_query_filter(self, query):
  773. """ Prepare an additional filter to apply on the query
  774. This filter is combined to the query domain with a AND
  775. operator. This hook is intended
  776. to be inherited, and is useful to implement filtering
  777. on analytic dimensions or operational units.
  778. Returns an Odoo domain expression (a python list)
  779. compatible with the model of the query."""
  780. self.ensure_one()
  781. return []
  782. @api.multi
  783. def drilldown(self, expr):
  784. self.ensure_one()
  785. # TODO FIXME: drilldown by account
  786. if AEP.has_account_var(expr):
  787. aep = AEP(self.env)
  788. aep.parse_expr(expr)
  789. aep.done_parsing(self.report_instance_id.company_id)
  790. domain = aep.get_aml_domain_for_expr(
  791. expr,
  792. self.date_from, self.date_to,
  793. self.report_instance_id.target_move,
  794. self.report_instance_id.company_id)
  795. domain.extend(self._get_additional_move_line_filter())
  796. return {
  797. 'name': expr + ' - ' + self.name,
  798. 'domain': domain,
  799. 'type': 'ir.actions.act_window',
  800. 'res_model': 'account.move.line',
  801. 'views': [[False, 'list'], [False, 'form']],
  802. 'view_type': 'list',
  803. 'view_mode': 'list',
  804. 'target': 'current',
  805. }
  806. else:
  807. return False
  808. @api.multi
  809. def _compute(self, kpi_matrix, lang_id, aep):
  810. """ Compute and render a mis report instance period
  811. It returns a dictionary keyed on kpi.name with a list of dictionaries
  812. with the following values (one item in the list for each subkpi):
  813. * val: the evaluated kpi, or None if there is no data or an error
  814. * val_r: the rendered kpi as a string, or #ERR, #DIV
  815. * val_c: a comment (explaining the error, typically)
  816. * style: the css style of the kpi
  817. (may change in the future!)
  818. * prefix: a prefix to display in front of the rendered value
  819. * suffix: a prefix to display after rendered value
  820. * dp: the decimal precision of the kpi
  821. * is_percentage: true if the kpi is of percentage type
  822. (may change in the future!)
  823. * expr: the kpi expression
  824. * drilldown: true if the drilldown method of
  825. mis.report.instance.period is going to do something
  826. useful in this kpi
  827. """
  828. self.ensure_one()
  829. # first invoke the compute method on the mis report template
  830. # passing it all the information regarding period and filters
  831. self.report_instance_id.report_id._compute(
  832. kpi_matrix, self,
  833. lang_id, aep,
  834. self.date_from, self.date_to,
  835. self.report_instance_id.target_move,
  836. self.report_instance_id.company_id,
  837. self.subkpi_ids,
  838. self._get_additional_move_line_filter,
  839. self._get_additional_query_filter,
  840. )
  841. # second, render it to something that can be used by the widget
  842. res = {}
  843. mis_report_kpi_style = self.env['mis.report.kpi.style']
  844. for kpi_name, kpi, vals in kpi_matrix.iter_kpi_vals(self):
  845. res[kpi_name] = []
  846. try:
  847. # TODO FIXME check style_expression evaluation wrt subkpis
  848. kpi_style = None
  849. if kpi.style_expression:
  850. style_name = safe_eval(kpi.style_expression,
  851. kpi_matrix.get_localdict(self))
  852. styles = mis_report_kpi_style.search(
  853. [('name', '=', style_name)])
  854. kpi_style = styles and styles[0]
  855. except:
  856. _logger.warning("error evaluating css stype expression %s",
  857. kpi.style, exc_info=True)
  858. default_vals = {
  859. 'prefix': kpi.prefix,
  860. 'suffix': kpi.suffix,
  861. 'dp': kpi.dp,
  862. 'is_percentage': kpi.type == 'pct',
  863. 'period_id': self.id,
  864. 'expr': kpi.expression, # TODO FIXME
  865. 'style': '',
  866. 'xlsx_style': {},
  867. }
  868. if kpi_style:
  869. default_vals.update({
  870. 'style': kpi_style.to_css_style(),
  871. 'xlsx_style': kpi_style.to_xlsx_forma_properties(),
  872. })
  873. for idx, subkpi_val in enumerate(vals):
  874. vals = default_vals.copy()
  875. if isinstance(subkpi_val, DataError):
  876. vals.update({
  877. 'val': subkpi_val.name,
  878. 'val_r': subkpi_val.name,
  879. 'val_c': subkpi_val.msg,
  880. 'drilldown': False,
  881. })
  882. else:
  883. # TODO FIXME: has_account_var on each subkpi expression?
  884. drilldown = (subkpi_val is not AccountingNone and
  885. AEP.has_account_var(kpi.expression))
  886. if kpi.multi:
  887. expression = kpi.expression_ids[idx].name
  888. else:
  889. expression = kpi.expression
  890. # TODO FIXME: check we have meaningfulname for exploded
  891. # kpis
  892. comment = kpi.name + " = " + expression
  893. vals.update({
  894. 'val': (None
  895. if subkpi_val is AccountingNone
  896. else subkpi_val),
  897. 'val_r': kpi.render(lang_id, subkpi_val),
  898. 'val_c': comment,
  899. 'drilldown': drilldown,
  900. })
  901. res[kpi_name].append(vals)
  902. return res
  903. class MisReportInstance(models.Model):
  904. """The MIS report instance combines everything to compute
  905. a MIS report template for a set of periods."""
  906. @api.one
  907. @api.depends('date')
  908. def _compute_pivot_date(self):
  909. if self.date:
  910. self.pivot_date = self.date
  911. else:
  912. self.pivot_date = fields.Date.context_today(self)
  913. @api.model
  914. def _default_company(self):
  915. return self.env['res.company'].\
  916. _company_default_get('mis.report.instance')
  917. _name = 'mis.report.instance'
  918. name = fields.Char(required=True,
  919. string='Name', translate=True)
  920. description = fields.Char(related='report_id.description',
  921. readonly=True)
  922. date = fields.Date(string='Base date',
  923. help='Report base date '
  924. '(leave empty to use current date)')
  925. pivot_date = fields.Date(compute='_compute_pivot_date',
  926. string="Pivot date")
  927. report_id = fields.Many2one('mis.report',
  928. required=True,
  929. string='Report')
  930. period_ids = fields.One2many('mis.report.instance.period',
  931. 'report_instance_id',
  932. required=True,
  933. string='Periods',
  934. copy=True)
  935. target_move = fields.Selection([('posted', 'All Posted Entries'),
  936. ('all', 'All Entries')],
  937. string='Target Moves',
  938. required=True,
  939. default='posted')
  940. company_id = fields.Many2one(comodel_name='res.company',
  941. string='Company',
  942. default=_default_company,
  943. required=True)
  944. landscape_pdf = fields.Boolean(string='Landscape PDF')
  945. comparison_mode = fields.Boolean(
  946. compute="_compute_comparison_mode",
  947. inverse="_inverse_comparison_mode")
  948. date_range_id = fields.Many2one(
  949. comodel_name='date.range',
  950. string='Date Range')
  951. date_from = fields.Date(string="From")
  952. date_to = fields.Date(string="To")
  953. temporary = fields.Boolean(default=False)
  954. @api.multi
  955. def save_report(self):
  956. self.ensure_one()
  957. self.write({'temporary': False})
  958. action = self.env.ref('mis_builder.mis_report_instance_view_action')
  959. res = action.read()[0]
  960. view = self.env.ref('mis_builder.mis_report_instance_view_form')
  961. res.update({
  962. 'views': [(view.id, 'form')],
  963. 'res_id': self.id,
  964. })
  965. return res
  966. @api.one
  967. def copy(self, default=None):
  968. default = dict(default or {})
  969. default['name'] = _('%s (copy)') % self.name
  970. return super(MisReportInstance, self).copy(default)
  971. def _format_date(self, lang_id, date):
  972. # format date following user language
  973. date_format = self.env['res.lang'].browse(lang_id).date_format
  974. return datetime.datetime.strftime(
  975. fields.Date.from_string(date), date_format)
  976. @api.multi
  977. @api.depends('date_from')
  978. def _compute_comparison_mode(self):
  979. for instance in self:
  980. instance.comparison_mode = not bool(instance.date_from)
  981. @api.multi
  982. def _inverse_comparison_mode(self):
  983. for record in self:
  984. if not record.comparison_mode:
  985. if not record.date_from:
  986. record.date_from = datetime.now()
  987. if not record.date_to:
  988. record.date_to = datetime.now()
  989. record.period_ids.unlink()
  990. record.write({'period_ids': [
  991. (0, 0, {
  992. 'name': 'Default',
  993. 'type': 'd',
  994. })
  995. ]})
  996. else:
  997. record.date_from = None
  998. record.date_to = None
  999. @api.onchange('date_range_id')
  1000. def onchange_date_range(self):
  1001. for record in self:
  1002. record.date_from = record.date_range_id.date_start
  1003. record.date_to = record.date_range_id.date_end
  1004. @api.multi
  1005. def preview(self):
  1006. assert len(self) == 1
  1007. view_id = self.env.ref('mis_builder.'
  1008. 'mis_report_instance_result_view_form')
  1009. return {
  1010. 'type': 'ir.actions.act_window',
  1011. 'res_model': 'mis.report.instance',
  1012. 'res_id': self.id,
  1013. 'view_mode': 'form',
  1014. 'view_type': 'form',
  1015. 'view_id': view_id.id,
  1016. 'target': 'current',
  1017. }
  1018. @api.multi
  1019. def print_pdf(self):
  1020. self.ensure_one()
  1021. return {
  1022. 'name': 'MIS report instance QWEB PDF report',
  1023. 'model': 'mis.report.instance',
  1024. 'type': 'ir.actions.report.xml',
  1025. 'report_name': 'mis_builder.report_mis_report_instance',
  1026. 'report_type': 'qweb-pdf',
  1027. 'context': self.env.context,
  1028. }
  1029. @api.multi
  1030. def export_xls(self):
  1031. self.ensure_one()
  1032. return {
  1033. 'name': 'MIS report instance XLSX report',
  1034. 'model': 'mis.report.instance',
  1035. 'type': 'ir.actions.report.xml',
  1036. 'report_name': 'mis.report.instance.xlsx',
  1037. 'report_type': 'xlsx',
  1038. 'context': self.env.context,
  1039. }
  1040. @api.multi
  1041. def display_settings(self):
  1042. assert len(self.ids) <= 1
  1043. view_id = self.env.ref('mis_builder.mis_report_instance_view_form')
  1044. return {
  1045. 'type': 'ir.actions.act_window',
  1046. 'res_model': 'mis.report.instance',
  1047. 'res_id': self.id if self.id else False,
  1048. 'view_mode': 'form',
  1049. 'view_type': 'form',
  1050. 'views': [(view_id.id, 'form')],
  1051. 'view_id': view_id.id,
  1052. 'target': 'current',
  1053. }
  1054. @api.multi
  1055. def compute(self):
  1056. self.ensure_one()
  1057. aep = self.report_id._prepare_aep(self.company_id)
  1058. # fetch user language only once
  1059. # TODO: is this necessary?
  1060. lang = self.env.user.lang
  1061. if not lang:
  1062. lang = 'en_US'
  1063. lang_id = self.env['res.lang'].search([('code', '=', lang)]).id
  1064. # compute kpi values for each period
  1065. kpi_values_by_period_ids = {}
  1066. kpi_matrix = KpiMatrix()
  1067. for period in self.period_ids:
  1068. if not period.valid:
  1069. continue
  1070. kpi_values = period._compute(kpi_matrix, lang_id, aep)
  1071. kpi_values_by_period_ids[period.id] = kpi_values
  1072. kpi_matrix.load_account_names(self.env['account.account'])
  1073. # prepare header and content
  1074. header = [{
  1075. 'kpi_name': '',
  1076. 'cols': []
  1077. }, {
  1078. 'kpi_name': '',
  1079. 'cols': []
  1080. }]
  1081. content = []
  1082. rows_by_kpi_name = {}
  1083. for kpi_name, kpi_description, kpi in kpi_matrix.iter_kpis():
  1084. props = {
  1085. 'kpi_name': kpi_description,
  1086. 'cols': [],
  1087. 'default_style': '',
  1088. 'default_xlsx_style': {},
  1089. }
  1090. rows_by_kpi_name[kpi_name] = props
  1091. if kpi.style:
  1092. props.update({
  1093. 'default_style': kpi.style.to_css_style(),
  1094. 'default_xlsx_style': kpi.style.to_xlsx_format_properties()
  1095. })
  1096. content.append(rows_by_kpi_name[kpi_name])
  1097. # populate header and content
  1098. for period in self.period_ids:
  1099. if not period.valid:
  1100. continue
  1101. # add the column header
  1102. if period.duration > 1 or period.type == 'w':
  1103. # from, to
  1104. date_from = self._format_date(lang_id, period.date_from)
  1105. date_to = self._format_date(lang_id, period.date_to)
  1106. header_date = _('from %s to %s') % (date_from, date_to)
  1107. else:
  1108. header_date = self._format_date(lang_id, period.date_from)
  1109. subkpis = period.subkpi_ids or \
  1110. period.report_instance_id.report_id.subkpi_ids
  1111. header[0]['cols'].append(dict(
  1112. name=period.name,
  1113. date=header_date,
  1114. colspan=len(subkpis) or 1,
  1115. ))
  1116. if subkpis:
  1117. for subkpi in subkpis:
  1118. header[1]['cols'].append(dict(
  1119. name=subkpi.name,
  1120. colspan=1,
  1121. ))
  1122. else:
  1123. header[1]['cols'].append(dict(
  1124. name="",
  1125. colspan=1,
  1126. ))
  1127. # add kpi values
  1128. kpi_values = kpi_values_by_period_ids[period.id]
  1129. for kpi_name in kpi_values:
  1130. rows_by_kpi_name[kpi_name]['cols'] += kpi_values[kpi_name]
  1131. # add comparison columns
  1132. for compare_col in period.comparison_column_ids:
  1133. compare_kpi_values = \
  1134. kpi_values_by_period_ids.get(compare_col.id)
  1135. if compare_kpi_values:
  1136. # add the comparison column header
  1137. header[0]['cols'].append(
  1138. dict(name=_('%s vs %s') % (period.name,
  1139. compare_col.name),
  1140. date=''))
  1141. # add comparison values
  1142. for kpi in self.report_id.kpi_ids:
  1143. rows_by_kpi_name[kpi.name]['cols'].append({
  1144. 'val_r': kpi.render_comparison(
  1145. lang_id,
  1146. kpi_values[kpi.name]['val'],
  1147. compare_kpi_values[kpi.name]['val'],
  1148. period.normalize_factor,
  1149. compare_col.normalize_factor)
  1150. })
  1151. return {
  1152. 'header': header,
  1153. 'content': content,
  1154. }