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.

920 lines
37 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # mis_builder module for Odoo, Management Information System Builder
  5. # Copyright (C) 2014-2015 ACSONE SA/NV (<http://acsone.eu>)
  6. #
  7. # This file is a part of mis_builder
  8. #
  9. # mis_builder is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License v3 or later
  11. # as published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # mis_builder is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License v3 or later for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # v3 or later along with this program.
  21. # If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. import datetime
  25. import dateutil
  26. from dateutil import parser
  27. import logging
  28. import re
  29. import time
  30. import traceback
  31. import pytz
  32. from openerp.osv import orm, fields
  33. from openerp import tools
  34. from openerp.tools.safe_eval import safe_eval
  35. from openerp.tools.translate import _
  36. from .aep import AccountingExpressionProcessor as AEP
  37. from .aggregate import _sum, _avg, _min, _max
  38. _logger = logging.getLogger(__name__)
  39. DATE_LENGTH = len(datetime.date.today().strftime(
  40. tools.DEFAULT_SERVER_DATE_FORMAT))
  41. DATETIME_LENGTH = len(datetime.datetime.now().strftime(
  42. tools.DEFAULT_SERVER_DATETIME_FORMAT))
  43. class AutoStruct(object):
  44. def __init__(self, **kwargs):
  45. for k, v in kwargs.items():
  46. setattr(self, k, v)
  47. def _get_selection_label(selection, value):
  48. for v, l in selection:
  49. if v == value:
  50. return l
  51. return ''
  52. def _utc_midnight(d, tz_name, add_day=0):
  53. d = d[:DATETIME_LENGTH]
  54. if len(d) == DATE_LENGTH:
  55. d += " 00:00:00"
  56. d = datetime.datetime.strptime(d, tools.DEFAULT_SERVER_DATETIME_FORMAT)
  57. utc_tz = pytz.timezone('UTC')
  58. if add_day:
  59. d = d + datetime.timedelta(days=add_day)
  60. context_tz = pytz.timezone(tz_name)
  61. local_timestamp = context_tz.localize(d, is_dst=False)
  62. return local_timestamp.astimezone(utc_tz).strftime(
  63. tools.DEFAULT_SERVER_DATETIME_FORMAT)
  64. def _python_var(var_str):
  65. return re.sub(r'\W|^(?=\d)', '_', var_str).lower()
  66. def _is_valid_python_var(name):
  67. return re.match("[_A-Za-z][_a-zA-Z0-9]*$", name)
  68. class MisReportKpi(orm.Model):
  69. """ A KPI is an element (ie a line) of a MIS report.
  70. In addition to a name and description, it has an expression
  71. to compute it based on queries defined in the MIS report.
  72. It also has various informations defining how to render it
  73. (numeric or percentage or a string, a suffix, divider) and
  74. how to render comparison of two values of the KPI.
  75. KPI's have a sequence and are ordered inside the MIS report.
  76. """
  77. _name = 'mis.report.kpi'
  78. _columns = {
  79. 'name': fields.char(size=32, required=True,
  80. string='Name'),
  81. 'description': fields.char(required=True,
  82. string='Description',
  83. translate=True),
  84. 'expression': fields.char(required=True,
  85. string='Expression'),
  86. 'default_css_style': fields.char(
  87. string='Default CSS style'),
  88. 'css_style': fields.char(string='CSS style expression'),
  89. 'type': fields.selection([('num', _('Numeric')),
  90. ('pct', _('Percentage')),
  91. ('str', _('String'))],
  92. required=True,
  93. string='Type'),
  94. 'divider': fields.selection([('1e-6', _('µ')),
  95. ('1e-3', _('m')),
  96. ('1', _('1')),
  97. ('1e3', _('k')),
  98. ('1e6', _('M'))],
  99. string='Factor'),
  100. 'dp': fields.integer(string='Rounding'),
  101. 'suffix': fields.char(size=16, string='Suffix'),
  102. 'compare_method': fields.selection([('diff', _('Difference')),
  103. ('pct', _('Percentage')),
  104. ('none', _('None'))],
  105. required=True,
  106. string='Comparison Method'),
  107. 'sequence': fields.integer(string='Sequence'),
  108. 'report_id': fields.many2one('mis.report', string='Report'),
  109. }
  110. _defaults = {
  111. 'type': 'num',
  112. 'divider': '1',
  113. 'dp': 0,
  114. 'compare_method': 'pct',
  115. 'sequence': 100,
  116. }
  117. _order = 'sequence, id'
  118. def _check_name(self, cr, uid, ids, context=None):
  119. for record_name in self.read(cr, uid, ids, ['name']):
  120. if not _is_valid_python_var(record_name['name']):
  121. return False
  122. return True
  123. _constraints = [
  124. (_check_name, 'The name must be a valid python identifier', ['name']),
  125. ]
  126. def onchange_name(self, cr, uid, ids, name, context=None):
  127. res = {}
  128. if name and not _is_valid_python_var(name):
  129. res['warning'] = {
  130. 'title': 'Invalid name %s' % name,
  131. 'message': 'The name must be a valid python identifier'}
  132. return res
  133. def onchange_description(self, cr, uid, ids, description, name,
  134. context=None):
  135. """ construct name from description """
  136. res = {}
  137. if description and not name:
  138. res = {'value': {'name': _python_var(description)}}
  139. return res
  140. def onchange_type(self, cr, uid, ids, kpi_type, context=None):
  141. res = {}
  142. if kpi_type == 'num':
  143. res['value'] = {
  144. 'compare_method': 'pct',
  145. 'divider': '1',
  146. 'dp': 0
  147. }
  148. elif kpi_type == 'pct':
  149. res['value'] = {
  150. 'compare_method': 'diff',
  151. 'divider': '1',
  152. 'dp': 0
  153. }
  154. elif kpi_type == 'str':
  155. res['value'] = {
  156. 'compare_method': 'none',
  157. 'divider': '',
  158. 'dp': 0
  159. }
  160. return res
  161. def render(self, cr, uid, lang_id, kpi, value, context=None):
  162. if value is None:
  163. return '#N/A'
  164. if kpi.type == 'num':
  165. return self._render_num(cr, uid, lang_id, value, kpi.divider,
  166. kpi.dp, kpi.suffix, context=context)
  167. elif kpi.type == 'pct':
  168. return self._render_num(cr, uid, lang_id, value, 0.01,
  169. kpi.dp, '%', context=context)
  170. else:
  171. return unicode(value)
  172. def _render_comparison(self, cr, uid, lang_id, kpi, value, base_value,
  173. average_value, average_base_value, context=None):
  174. """ render the comparison of two KPI values, ready for display """
  175. if value is None or base_value is None:
  176. return ''
  177. if kpi.type == 'pct':
  178. return self._render_num(cr, uid, lang_id, value - base_value, 0.01,
  179. kpi.dp, _('pp'), sign='+', context=context)
  180. elif kpi.type == 'num':
  181. if average_value:
  182. value = value / float(average_value)
  183. if average_base_value:
  184. base_value = base_value / float(average_base_value)
  185. if kpi.compare_method == 'diff':
  186. return self._render_num(cr, uid, lang_id, value - base_value,
  187. kpi.divider,
  188. kpi.dp, kpi.suffix, sign='+',
  189. context=context)
  190. elif kpi.compare_method == 'pct':
  191. if round(base_value, kpi.dp) != 0:
  192. return self._render_num(
  193. cr, uid, lang_id,
  194. (value - base_value) / abs(base_value),
  195. 0.01, kpi.dp, '%', sign='+', context=context)
  196. return ''
  197. def _render_num(self, cr, uid, lang_id, value, divider,
  198. dp, suffix, sign='-', context=None):
  199. divider_label = _get_selection_label(
  200. self._columns['divider'].selection, divider)
  201. if divider_label == '1':
  202. divider_label = ''
  203. # format number following user language
  204. value = round(value / float(divider or 1), dp) or 0
  205. value = self.pool['res.lang'].format(
  206. cr, uid, lang_id,
  207. '%%%s.%df' % (sign, dp),
  208. value,
  209. grouping=True,
  210. context=context)
  211. value = u'%s\N{NO-BREAK SPACE}%s%s' % \
  212. (value, divider_label, suffix or '')
  213. value = value.replace('-', u'\N{NON-BREAKING HYPHEN}')
  214. return value
  215. class MisReportQuery(orm.Model):
  216. """ A query to fetch arbitrary data for a MIS report.
  217. A query works on a model and has a domain and list of fields to fetch.
  218. At runtime, the domain is expanded with a "and" on the date/datetime field.
  219. """
  220. _name = 'mis.report.query'
  221. def _get_field_names(self, cr, uid, ids, name, args, context=None):
  222. res = {}
  223. for query in self.browse(cr, uid, ids, context=context):
  224. field_names = []
  225. for field in query.field_ids:
  226. field_names.append(field.name)
  227. res[query.id] = ', '.join(field_names)
  228. return res
  229. def onchange_field_ids(self, cr, uid, ids, field_ids, context=None):
  230. # compute field_names
  231. field_names = []
  232. for field in self.pool.get('ir.model.fields').read(
  233. cr, uid,
  234. field_ids[0][2],
  235. ['name'],
  236. context=context):
  237. field_names.append(field['name'])
  238. return {'value': {'field_names': ', '.join(field_names)}}
  239. _columns = {
  240. 'name': fields.char(size=32, required=True,
  241. string='Name'),
  242. 'model_id': fields.many2one('ir.model', required=True,
  243. string='Model'),
  244. 'field_ids': fields.many2many('ir.model.fields', required=True,
  245. string='Fields to fetch'),
  246. 'field_names': fields.function(_get_field_names, type='char',
  247. string='Fetched fields name',
  248. store={'mis.report.query':
  249. (lambda self, cr, uid, ids, c={}:
  250. ids, ['field_ids'], 20), }),
  251. 'aggregate': fields.selection([('sum', _('Sum')),
  252. ('avg', _('Average')),
  253. ('min', _('Min')),
  254. ('max', _('Max'))],
  255. string='Aggregate'),
  256. 'date_field': fields.many2one('ir.model.fields', required=True,
  257. string='Date field',
  258. domain=[('ttype', 'in',
  259. ('date', 'datetime'))]),
  260. 'domain': fields.char(string='Domain'),
  261. 'report_id': fields.many2one('mis.report', string='Report',
  262. ondelete='cascade'),
  263. }
  264. _order = 'name'
  265. def _check_name(self, cr, uid, ids, context=None):
  266. for record_name in self.read(cr, uid, ids, ['name']):
  267. if not _is_valid_python_var(record_name['name']):
  268. return False
  269. return True
  270. _constraints = [
  271. (_check_name, 'The name must be a valid python identifier', ['name']),
  272. ]
  273. class MisReport(orm.Model):
  274. """ A MIS report template (without period information)
  275. The MIS report holds:
  276. * a list of explicit queries; the result of each query is
  277. stored in a variable with same name as a query, containing as list
  278. of data structures populated with attributes for each fields to fetch;
  279. when queries have an aggregate method and no fields to group, it returns
  280. a data structure with the aggregated fields
  281. * a list of KPI to be evaluated based on the variables resulting
  282. from the accounting data and queries (KPI expressions can references
  283. queries and accounting expression - see AccoutingExpressionProcessor)
  284. """
  285. _name = 'mis.report'
  286. _columns = {
  287. 'name': fields.char(size=32, required=True,
  288. string='Name', translate=True),
  289. 'description': fields.char(required=False,
  290. string='Description', translate=True),
  291. 'query_ids': fields.one2many('mis.report.query', 'report_id',
  292. string='Queries'),
  293. 'kpi_ids': fields.one2many('mis.report.kpi', 'report_id',
  294. string='KPI\'s'),
  295. }
  296. # TODO: kpi name cannot be start with query name
  297. def create(self, cr, uid, vals, context=None):
  298. # TODO: explain this
  299. if 'kpi_ids' in vals:
  300. mis_report_kpi_obj = self.pool.get('mis.report.kpi')
  301. for idx, line in enumerate(vals['kpi_ids']):
  302. if line[0] == 0:
  303. line[2]['sequence'] = idx + 1
  304. else:
  305. mis_report_kpi_obj.write(
  306. cr, uid, [line[1]], {'sequence': idx + 1},
  307. context=context)
  308. return super(MisReport, self).create(cr, uid, vals, context=context)
  309. def write(self, cr, uid, ids, vals, context=None):
  310. # TODO: explain this
  311. res = super(MisReport, self).write(
  312. cr, uid, ids, vals, context=context)
  313. mis_report_kpi_obj = self.pool.get('mis.report.kpi')
  314. for report in self.browse(cr, uid, ids, context):
  315. for idx, kpi in enumerate(report.kpi_ids):
  316. mis_report_kpi_obj.write(
  317. cr, uid, [kpi.id], {'sequence': idx + 1}, context=context)
  318. return res
  319. class MisReportInstancePeriod(orm.Model):
  320. """ A MIS report instance has the logic to compute
  321. a report template for a given date period.
  322. Periods have a duration (day, week, fiscal period) and
  323. are defined as an offset relative to a pivot date.
  324. """
  325. def _get_dates(self, cr, uid, ids, field_names, arg, context=None):
  326. if isinstance(ids, (int, long)):
  327. ids = [ids]
  328. res = {}
  329. for c in self.browse(cr, uid, ids, context=context):
  330. period_ids = None
  331. valid = True
  332. date_from = False
  333. date_to = False
  334. d = parser.parse(c.report_instance_id.pivot_date)
  335. if c.type == 'd':
  336. date_from = d + datetime.timedelta(days=c.offset)
  337. date_to = date_from + datetime.timedelta(days=c.duration - 1)
  338. date_from = date_from.strftime(
  339. tools.DEFAULT_SERVER_DATE_FORMAT)
  340. date_to = date_to.strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
  341. elif c.type == 'w':
  342. date_from = d - datetime.timedelta(d.weekday())
  343. date_from = date_from + datetime.timedelta(days=c.offset * 7)
  344. date_to = date_from + datetime.timedelta(
  345. days=(7 * c.duration) - 1)
  346. date_from = date_from.strftime(
  347. tools.DEFAULT_SERVER_DATE_FORMAT)
  348. date_to = date_to.strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
  349. elif c.type == 'fp':
  350. period_obj = self.pool['account.period']
  351. current_period_ids = period_obj.search(
  352. cr, uid,
  353. [('special', '=', False),
  354. ('date_start', '<=', d),
  355. ('date_stop', '>=', d),
  356. ('company_id', '=', c.company_id.id)],
  357. context=context)
  358. if current_period_ids:
  359. all_period_ids = period_obj.search(
  360. cr, uid,
  361. [('special', '=', False),
  362. ('company_id', '=', c.company_id.id)],
  363. order='date_start',
  364. context=context)
  365. p = all_period_ids.index(current_period_ids[0]) + \
  366. c.offset
  367. if p >= 0 and p + c.duration <= len(all_period_ids):
  368. period_ids = all_period_ids[p:p + c.duration]
  369. periods = period_obj.browse(cr, uid, period_ids,
  370. context=context)
  371. date_from = periods[0].date_start
  372. date_to = periods[-1].date_stop
  373. res[c.id] = {
  374. 'date_from': date_from,
  375. 'date_to': date_to,
  376. 'period_from': period_ids and period_ids[0] or False,
  377. 'period_to': period_ids and period_ids[-1] or False,
  378. 'valid': valid,
  379. }
  380. return res
  381. _name = 'mis.report.instance.period'
  382. _columns = {
  383. 'name': fields.char(size=32, required=True,
  384. string='Description', translate=True),
  385. 'type': fields.selection([('d', _('Day')),
  386. ('w', _('Week')),
  387. ('fp', _('Fiscal Period')),
  388. # ('fy', _('Fiscal Year'))
  389. ],
  390. required=True,
  391. string='Period type'),
  392. 'offset': fields.integer(string='Offset',
  393. help='Offset from current period'),
  394. 'duration': fields.integer(string='Duration',
  395. help='Number of periods'),
  396. 'date_from': fields.function(_get_dates,
  397. type='date',
  398. multi="dates",
  399. string="From"),
  400. 'date_to': fields.function(_get_dates,
  401. type='date',
  402. multi="dates",
  403. string="To"),
  404. 'period_from': fields.function(_get_dates,
  405. type='many2one', obj='account.period',
  406. multi="dates", string="From period"),
  407. 'period_to': fields.function(_get_dates,
  408. type='many2one', obj='account.period',
  409. multi="dates", string="To period"),
  410. 'valid': fields.function(_get_dates,
  411. type='boolean',
  412. multi="dates",
  413. string='Valid'),
  414. 'sequence': fields.integer(string='Sequence'),
  415. 'report_instance_id': fields.many2one('mis.report.instance',
  416. string='Report Instance',
  417. ondelete='cascade'),
  418. 'comparison_column_ids': fields.many2many(
  419. 'mis.report.instance.period',
  420. 'mis_report_instance_period_rel',
  421. 'period_id',
  422. 'compare_period_id',
  423. string='Compare with'),
  424. 'company_id': fields.related('report_instance_id', 'company_id',
  425. type="many2one", relation="res.company",
  426. string="Company", readonly=True),
  427. 'normalize_factor': fields.integer(
  428. string='Factor',
  429. help='Factor to use to normalize the period (used in comparison'),
  430. }
  431. _defaults = {
  432. 'offset': -1,
  433. 'duration': 1,
  434. 'sequence': 100,
  435. 'normalize_factor': 1,
  436. }
  437. _order = 'sequence, id'
  438. _sql_constraints = [
  439. ('duration', 'CHECK (duration>0)',
  440. 'Wrong duration, it must be positive!'),
  441. ('normalize_factor', 'CHECK (normalize_factor>0)',
  442. 'Wrong normalize factor, it must be positive!'),
  443. ('name_unique', 'unique(name, report_instance_id)',
  444. 'Period name should be unique by report'),
  445. ]
  446. def _get_additional_move_line_filter(self, cr, uid, _id, context=None):
  447. """ Prepare a filter to apply on all move lines
  448. This filter is applied with a AND operator on all
  449. accounting expression domains. This hook is intended
  450. to be inherited, and is useful to implement filtering
  451. on analytic dimensions or operational units.
  452. Returns an Odoo domain expression (a python list)
  453. compatible with account.move.line."""
  454. return []
  455. def _get_additional_query_filter(self, cr, uid, _id, query, context=None):
  456. """ Prepare an additional filter to apply on the query
  457. This filter is combined to the query domain with a AND
  458. operator. This hook is intended
  459. to be inherited, and is useful to implement filtering
  460. on analytic dimensions or operational units.
  461. Returns an Odoo domain expression (a python list)
  462. compatible with the model of the query."""
  463. return []
  464. def drilldown(self, cr, uid, _id, expr, context=None):
  465. this = self.browse(cr, uid, _id, context=context)
  466. if AEP.has_account_var(expr):
  467. aep = AEP(cr)
  468. aep.parse_expr(expr)
  469. aep.done_parsing(cr, uid, this.report_instance_id.root_account,
  470. context=context)
  471. domain = aep.get_aml_domain_for_expr(
  472. cr, uid, expr,
  473. this.date_from, this.date_to,
  474. this.period_from, this.period_to,
  475. this.report_instance_id.target_move,
  476. context=context)
  477. domain.extend(self._get_additional_move_line_filter(
  478. cr, uid, _id, context=context))
  479. return {
  480. 'name': expr + ' - ' + this.name,
  481. 'domain': domain,
  482. 'type': 'ir.actions.act_window',
  483. 'res_model': 'account.move.line',
  484. 'views': [[False, 'list'], [False, 'form']],
  485. 'view_type': 'list',
  486. 'view_mode': 'list',
  487. 'target': 'current',
  488. }
  489. else:
  490. return False
  491. def _fetch_queries(self, cr, uid, c, context):
  492. res = {}
  493. report = c.report_instance_id.report_id
  494. for query in report.query_ids:
  495. obj = self.pool[query.model_id.model]
  496. eval_context = {
  497. 'time': time,
  498. 'datetime': datetime,
  499. 'dateutil': dateutil,
  500. # deprecated
  501. 'uid': uid,
  502. 'context': context,
  503. }
  504. if not c.date_from or not c.date_to:
  505. raise orm.except_orm(_('Error!'),
  506. _('Please define From and To dates for '
  507. 'period %s.') % c.name)
  508. domain = query.domain and \
  509. safe_eval(query.domain, eval_context) or []
  510. domain.extend(self._get_additional_query_filter(
  511. cr, uid, c.id, query, context=context))
  512. if query.date_field.ttype == 'date':
  513. domain.extend([(query.date_field.name, '>=', c.date_from),
  514. (query.date_field.name, '<=', c.date_to)])
  515. else:
  516. tz = context.get('tz', False) or 'UTC'
  517. datetime_from = _utc_midnight(
  518. c.date_from, tz)
  519. datetime_to = _utc_midnight(
  520. c.date_to, tz, add_day=1)
  521. domain.extend([(query.date_field.name, '>=', datetime_from),
  522. (query.date_field.name, '<', datetime_to)])
  523. if obj._columns.get('company_id', False):
  524. domain.extend(['|', ('company_id', '=', False),
  525. ('company_id', '=', c.company_id.id)])
  526. field_names = [f.name for f in query.field_ids]
  527. if not query.aggregate:
  528. obj_ids = obj.search(cr, uid, domain, context=context)
  529. data = obj.read(
  530. cr, uid, obj_ids, field_names, context=context)
  531. res[query.name] = [AutoStruct(**d) for d in data]
  532. elif query.aggregate == 'sum':
  533. data = obj.read_group(
  534. cr, uid, domain, field_names, '', context=context)
  535. s = AutoStruct(count=data[0]['_count'])
  536. for field_name in field_names:
  537. v = data[0][field_name]
  538. setattr(s, field_name, v)
  539. res[query.name] = s
  540. else:
  541. obj_ids = obj.search(cr, uid, domain, context=context)
  542. data = obj.read(
  543. cr, uid, obj_ids, field_names, context=context)
  544. s = AutoStruct(count=len(data))
  545. if query.aggregate == 'min':
  546. agg = _min
  547. elif query.aggregate == 'max':
  548. agg = _max
  549. elif query.aggregate == 'avg':
  550. agg = _avg
  551. for field_name in field_names:
  552. setattr(s, field_name,
  553. agg([d[field_name] for d in data]))
  554. res[query.name] = s
  555. return res
  556. def _compute(self, cr, uid, lang_id, c, aep, context=None):
  557. if context is None:
  558. context = {}
  559. kpi_obj = self.pool['mis.report.kpi']
  560. res = {}
  561. localdict = {
  562. 'registry': self.pool,
  563. 'sum': _sum,
  564. 'min': _min,
  565. 'max': _max,
  566. 'len': len,
  567. 'avg': _avg,
  568. }
  569. localdict.update(self._fetch_queries(cr, uid, c, context=context))
  570. aep.do_queries(cr, uid, c.date_from, c.date_to,
  571. c.period_from, c.period_to,
  572. c.report_instance_id.target_move,
  573. self._get_additional_move_line_filter(cr, uid, c.id,
  574. context=context),
  575. context=context)
  576. compute_queue = c.report_instance_id.report_id.kpi_ids
  577. recompute_queue = []
  578. while True:
  579. for kpi in compute_queue:
  580. try:
  581. kpi_val_comment = kpi.name + " = " + kpi.expression
  582. kpi_eval_expression = aep.replace_expr(kpi.expression)
  583. kpi_val = safe_eval(kpi_eval_expression, localdict)
  584. except ZeroDivisionError:
  585. kpi_val = None
  586. kpi_val_rendered = '#DIV/0'
  587. kpi_val_comment += '\n\n%s' % (traceback.format_exc(),)
  588. except (NameError, ValueError):
  589. recompute_queue.append(kpi)
  590. kpi_val = None
  591. kpi_val_rendered = '#ERR'
  592. kpi_val_comment += '\n\n%s' % (traceback.format_exc(),)
  593. except:
  594. kpi_val = None
  595. kpi_val_rendered = '#ERR'
  596. kpi_val_comment += '\n\n%s' % (traceback.format_exc(),)
  597. else:
  598. kpi_val_rendered = kpi_obj.render(
  599. cr, uid, lang_id, kpi, kpi_val, context=context)
  600. localdict[kpi.name] = kpi_val
  601. try:
  602. kpi_style = None
  603. if kpi.css_style:
  604. kpi_style = safe_eval(kpi.css_style, localdict)
  605. except:
  606. _logger.warning("error evaluating css stype expression %s",
  607. kpi.css_style, exc_info=True)
  608. kpi_style = None
  609. drilldown = (kpi_val is not None and
  610. AEP.has_account_var(kpi.expression))
  611. res[kpi.name] = {
  612. 'val': kpi_val,
  613. 'val_r': kpi_val_rendered,
  614. 'val_c': kpi_val_comment,
  615. 'style': kpi_style,
  616. 'default_style': kpi.default_css_style or None,
  617. 'suffix': kpi.suffix,
  618. 'dp': kpi.dp,
  619. 'is_percentage': kpi.type == 'pct',
  620. 'period_id': c.id,
  621. 'expr': kpi.expression,
  622. 'drilldown': drilldown,
  623. }
  624. if len(recompute_queue) == 0:
  625. # nothing to recompute, we are done
  626. break
  627. if len(recompute_queue) == len(compute_queue):
  628. # could not compute anything in this iteration
  629. # (ie real Value errors or cyclic dependency)
  630. # so we stop trying
  631. break
  632. # try again
  633. compute_queue = recompute_queue
  634. recompute_queue = []
  635. return res
  636. class MisReportInstance(orm.Model):
  637. """The MIS report instance combines everything to compute
  638. a MIS report template for a set of periods."""
  639. def _compute_pivot_date(self, cr, uid, ids, field_name, arg, context=None):
  640. res = {}
  641. for r in self.browse(cr, uid, ids, context=context):
  642. if r.date:
  643. res[r.id] = r.date
  644. else:
  645. res[r.id] = fields.date.context_today(self, cr, uid,
  646. context=context)
  647. return res
  648. _name = 'mis.report.instance'
  649. _columns = {
  650. 'name': fields.char(size=32, required=True,
  651. string='Name', translate=True),
  652. 'description': fields.char(required=False,
  653. string='Description', translate=True),
  654. 'date': fields.date(string='Base date',
  655. help='Report base date '
  656. '(leave empty to use current date)'),
  657. 'pivot_date': fields.function(_compute_pivot_date,
  658. type='date',
  659. string="Pivot date"),
  660. 'report_id': fields.many2one('mis.report',
  661. required=True,
  662. string='Report'),
  663. 'period_ids': fields.one2many('mis.report.instance.period',
  664. 'report_instance_id',
  665. required=True,
  666. string='Periods'),
  667. 'target_move': fields.selection([('posted', 'All Posted Entries'),
  668. ('all', 'All Entries'),
  669. ], 'Target Moves', required=True),
  670. 'company_id': fields.related('root_account', 'company_id',
  671. type='many2one', relation='res.company',
  672. string='Company', readonly=True,
  673. store=True),
  674. 'root_account': fields.many2one('account.account',
  675. domain='[("parent_id", "=", False)]',
  676. string="Account chart",
  677. required=True)
  678. }
  679. _defaults = {
  680. 'target_move': 'posted',
  681. }
  682. def create(self, cr, uid, vals, context=None):
  683. if not vals:
  684. return context.get('active_id', None)
  685. # TODO: explain this
  686. if 'period_ids' in vals:
  687. mis_report_instance_period_obj = self.pool.get(
  688. 'mis.report.instance.period')
  689. for idx, line in enumerate(vals['period_ids']):
  690. if line[0] == 0:
  691. line[2]['sequence'] = idx + 1
  692. else:
  693. mis_report_instance_period_obj.write(
  694. cr, uid, [line[1]], {'sequence': idx + 1},
  695. context=context)
  696. return super(MisReportInstance, self).create(cr, uid, vals,
  697. context=context)
  698. def write(self, cr, uid, ids, vals, context=None):
  699. # TODO: explain this
  700. res = super(MisReportInstance, self).write(
  701. cr, uid, ids, vals, context=context)
  702. mis_report_instance_period_obj = self.pool.get(
  703. 'mis.report.instance.period')
  704. for instance in self.browse(cr, uid, ids, context):
  705. for idx, period in enumerate(instance.period_ids):
  706. mis_report_instance_period_obj.write(
  707. cr, uid, [period.id], {'sequence': idx + 1},
  708. context=context)
  709. return res
  710. def preview(self, cr, uid, ids, context=None):
  711. assert len(ids) == 1
  712. view_id = self.pool['ir.model.data'].get_object_reference(
  713. cr, uid, 'mis_builder',
  714. 'mis_report_instance_result_view_form')[1]
  715. return {
  716. 'type': 'ir.actions.act_window',
  717. 'res_model': 'mis.report.instance',
  718. 'res_id': ids[0],
  719. 'view_mode': 'form',
  720. 'view_type': 'form',
  721. 'view_id': view_id,
  722. 'target': 'new',
  723. }
  724. def _format_date(self, cr, uid, lang_id, date, context=None):
  725. # format date following user language
  726. tformat = self.pool['res.lang'].read(
  727. cr, uid, lang_id, ['date_format'])[0]['date_format']
  728. date = datetime.datetime.strptime(date,
  729. tools.DEFAULT_SERVER_DATE_FORMAT)
  730. return date.strftime(tformat)
  731. def compute(self, cr, uid, _id, context=None):
  732. assert isinstance(_id, (int, long))
  733. if context is None:
  734. context = {}
  735. r = self.browse(cr, uid, _id, context=context)
  736. # prepare AccountingExpressionProcessor
  737. aep = AEP(cr)
  738. for kpi in r.report_id.kpi_ids:
  739. aep.parse_expr(kpi.expression)
  740. aep.done_parsing(cr, uid, r.root_account, context=context)
  741. report_instance_period_obj = self.pool['mis.report.instance.period']
  742. kpi_obj = self.pool.get('mis.report.kpi')
  743. # fetch user language only once
  744. # TODO: is this necessary?
  745. lang = self.pool['res.users'].read(
  746. cr, uid, uid, ['lang'], context=context)['lang']
  747. if not lang:
  748. lang = 'en_US'
  749. lang_id = self.pool['res.lang'].search(
  750. cr, uid, [('code', '=', lang)], context=context)
  751. # compute kpi values for each period
  752. kpi_values_by_period_ids = {}
  753. for period in r.period_ids:
  754. if not period.valid:
  755. continue
  756. kpi_values = report_instance_period_obj._compute(
  757. cr, uid, lang_id, period, aep, context=context)
  758. kpi_values_by_period_ids[period.id] = kpi_values
  759. # prepare header and content
  760. header = []
  761. header.append({
  762. 'kpi_name': '',
  763. 'cols': []
  764. })
  765. content = []
  766. rows_by_kpi_name = {}
  767. for kpi in r.report_id.kpi_ids:
  768. rows_by_kpi_name[kpi.name] = {
  769. 'kpi_name': kpi.description,
  770. 'cols': [],
  771. 'default_style': kpi.default_css_style
  772. }
  773. content.append(rows_by_kpi_name[kpi.name])
  774. # populate header and content
  775. for period in r.period_ids:
  776. if not period.valid:
  777. continue
  778. # add the column header
  779. if period.duration > 1 or period.type == 'w':
  780. # from, to
  781. if period.period_from and period.period_to:
  782. date_from = period.period_from.name
  783. date_to = period.period_to.name
  784. else:
  785. date_from = self._format_date(
  786. cr, uid, lang_id, period.date_from)
  787. date_to = self._format_date(
  788. cr, uid, lang_id, period.date_to)
  789. header_date = _('from %s to %s') % (date_from, date_to)
  790. else:
  791. # one period or one day
  792. if period.period_from and period.period_to:
  793. header_date = period.period_from.name
  794. else:
  795. header_date = self._format_date(
  796. cr, uid, lang_id, period.date_from)
  797. header[0]['cols'].append(dict(name=period.name, date=header_date))
  798. # add kpi values
  799. kpi_values = kpi_values_by_period_ids[period.id]
  800. for kpi_name in kpi_values:
  801. rows_by_kpi_name[kpi_name]['cols'].append(kpi_values[kpi_name])
  802. # add comparison columns
  803. for compare_col in period.comparison_column_ids:
  804. compare_kpi_values = \
  805. kpi_values_by_period_ids.get(compare_col.id)
  806. if compare_kpi_values:
  807. # add the comparison column header
  808. header[0]['cols'].append(
  809. dict(name=_('%s vs %s') % (period.name,
  810. compare_col.name),
  811. date=''))
  812. # add comparison values
  813. for kpi in r.report_id.kpi_ids:
  814. rows_by_kpi_name[kpi.name]['cols'].append({
  815. 'val_r': kpi_obj._render_comparison(
  816. cr,
  817. uid,
  818. lang_id,
  819. kpi,
  820. kpi_values[kpi.name]['val'],
  821. compare_kpi_values[kpi.name]['val'],
  822. period.normalize_factor,
  823. compare_col.normalize_factor,
  824. context=context)
  825. })
  826. return {'header': header,
  827. 'content': content}