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.

418 lines
16 KiB

  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 openerp import api, fields, models, _
  5. import datetime
  6. import logging
  7. from .aep import AccountingExpressionProcessor as AEP
  8. _logger = logging.getLogger(__name__)
  9. class MisReportInstancePeriod(models.Model):
  10. """ A MIS report instance has the logic to compute
  11. a report template for a given date period.
  12. Periods have a duration (day, week, fiscal period) and
  13. are defined as an offset relative to a pivot date.
  14. """
  15. @api.one
  16. @api.depends('report_instance_id.pivot_date',
  17. 'report_instance_id.comparison_mode',
  18. 'type', 'offset', 'duration', 'mode')
  19. def _compute_dates(self):
  20. self.date_from = False
  21. self.date_to = False
  22. self.valid = False
  23. report = self.report_instance_id
  24. d = fields.Date.from_string(report.pivot_date)
  25. if not report.comparison_mode:
  26. self.date_from = report.date_from
  27. self.date_to = report.date_to
  28. self.valid = True
  29. elif self.mode == 'fix':
  30. self.date_from = self.manual_date_from
  31. self.date_to = self.manual_date_to
  32. self.valid = True
  33. elif self.type == 'd':
  34. date_from = d + datetime.timedelta(days=self.offset)
  35. date_to = date_from + \
  36. datetime.timedelta(days=self.duration - 1)
  37. self.date_from = fields.Date.to_string(date_from)
  38. self.date_to = fields.Date.to_string(date_to)
  39. self.valid = True
  40. elif self.type == 'w':
  41. date_from = d - datetime.timedelta(d.weekday())
  42. date_from = date_from + datetime.timedelta(days=self.offset * 7)
  43. date_to = date_from + \
  44. datetime.timedelta(days=(7 * self.duration) - 1)
  45. self.date_from = fields.Date.to_string(date_from)
  46. self.date_to = fields.Date.to_string(date_to)
  47. self.valid = True
  48. elif self.type == 'date_range':
  49. date_range_obj = self.env['date.range']
  50. current_periods = date_range_obj.search(
  51. [('type_id', '=', self.date_range_type_id.id),
  52. ('date_start', '<=', d),
  53. ('date_end', '>=', d),
  54. ('company_id', '=', self.report_instance_id.company_id.id)])
  55. if current_periods:
  56. all_periods = date_range_obj.search(
  57. [('type_id', '=', self.date_range_type_id.id),
  58. ('company_id', '=',
  59. self.report_instance_id.company_id.id)],
  60. order='date_start')
  61. all_period_ids = [p.id for p in all_periods]
  62. p = all_period_ids.index(current_periods[0].id) + self.offset
  63. if p >= 0 and p + self.duration <= len(all_period_ids):
  64. periods = all_periods[p:p + self.duration]
  65. self.date_from = periods[0].date_start
  66. self.date_to = periods[-1].date_end
  67. self.valid = True
  68. _name = 'mis.report.instance.period'
  69. name = fields.Char(size=32, required=True,
  70. string='Description', translate=True)
  71. mode = fields.Selection([('fix', 'Fix'),
  72. ('relative', 'Relative'),
  73. ], required=True,
  74. default='fix')
  75. type = fields.Selection([('d', _('Day')),
  76. ('w', _('Week')),
  77. ('date_range', _('Date Range'))
  78. ],
  79. string='Period type')
  80. date_range_type_id = fields.Many2one(
  81. comodel_name='date.range.type', string='Date Range Type')
  82. offset = fields.Integer(string='Offset',
  83. help='Offset from current period',
  84. default=-1)
  85. duration = fields.Integer(string='Duration',
  86. help='Number of periods',
  87. default=1)
  88. date_from = fields.Date(compute='_compute_dates', string="From")
  89. date_to = fields.Date(compute='_compute_dates', string="To")
  90. manual_date_from = fields.Date(string="From")
  91. manual_date_to = fields.Date(string="To")
  92. date_range_id = fields.Many2one(
  93. comodel_name='date.range',
  94. string='Date Range')
  95. valid = fields.Boolean(compute='_compute_dates',
  96. type='boolean',
  97. string='Valid')
  98. sequence = fields.Integer(string='Sequence', default=100)
  99. report_instance_id = fields.Many2one('mis.report.instance',
  100. string='Report Instance',
  101. ondelete='cascade')
  102. comparison_column_ids = fields.Many2many(
  103. comodel_name='mis.report.instance.period',
  104. relation='mis_report_instance_period_rel',
  105. column1='period_id',
  106. column2='compare_period_id',
  107. string='Compare with')
  108. normalize_factor = fields.Integer(
  109. string='Factor',
  110. help='Factor to use to normalize the period (used in comparison',
  111. default=1)
  112. subkpi_ids = fields.Many2many(
  113. 'mis.report.subkpi',
  114. string="Sub KPI Filter")
  115. _order = 'sequence, id'
  116. _sql_constraints = [
  117. ('duration', 'CHECK (duration>0)',
  118. 'Wrong duration, it must be positive!'),
  119. ('normalize_factor', 'CHECK (normalize_factor>0)',
  120. 'Wrong normalize factor, it must be positive!'),
  121. ('name_unique', 'unique(name, report_instance_id)',
  122. 'Period name should be unique by report'),
  123. ]
  124. @api.onchange('date_range_id')
  125. def onchange_date_range(self):
  126. for record in self:
  127. record.manual_date_from = record.date_range_id.date_start
  128. record.manual_date_to = record.date_range_id.date_end
  129. record.name = record.date_range_id.name
  130. @api.multi
  131. def _get_additional_move_line_filter(self):
  132. """ Prepare a filter to apply on all move lines
  133. This filter is applied with a AND operator on all
  134. accounting expression domains. This hook is intended
  135. to be inherited, and is useful to implement filtering
  136. on analytic dimensions or operational units.
  137. Returns an Odoo domain expression (a python list)
  138. compatible with account.move.line."""
  139. self.ensure_one()
  140. return []
  141. @api.multi
  142. def _get_additional_query_filter(self, query):
  143. """ Prepare an additional filter to apply on the query
  144. This filter is combined to the query domain with a AND
  145. operator. This hook is intended
  146. to be inherited, and is useful to implement filtering
  147. on analytic dimensions or operational units.
  148. Returns an Odoo domain expression (a python list)
  149. compatible with the model of the query."""
  150. self.ensure_one()
  151. return []
  152. class MisReportInstance(models.Model):
  153. """The MIS report instance combines everything to compute
  154. a MIS report template for a set of periods."""
  155. @api.one
  156. @api.depends('date')
  157. def _compute_pivot_date(self):
  158. if self.date:
  159. self.pivot_date = self.date
  160. else:
  161. self.pivot_date = fields.Date.context_today(self)
  162. @api.model
  163. def _default_company(self):
  164. return self.env['res.company'].\
  165. _company_default_get('mis.report.instance')
  166. _name = 'mis.report.instance'
  167. name = fields.Char(required=True,
  168. string='Name', translate=True)
  169. description = fields.Char(related='report_id.description',
  170. readonly=True)
  171. date = fields.Date(string='Base date',
  172. help='Report base date '
  173. '(leave empty to use current date)')
  174. pivot_date = fields.Date(compute='_compute_pivot_date',
  175. string="Pivot date")
  176. report_id = fields.Many2one('mis.report',
  177. required=True,
  178. string='Report')
  179. period_ids = fields.One2many('mis.report.instance.period',
  180. 'report_instance_id',
  181. required=True,
  182. string='Periods',
  183. copy=True)
  184. target_move = fields.Selection([('posted', 'All Posted Entries'),
  185. ('all', 'All Entries')],
  186. string='Target Moves',
  187. required=True,
  188. default='posted')
  189. company_id = fields.Many2one(comodel_name='res.company',
  190. string='Company',
  191. default=_default_company,
  192. required=True)
  193. landscape_pdf = fields.Boolean(string='Landscape PDF')
  194. comparison_mode = fields.Boolean(
  195. compute="_compute_comparison_mode",
  196. inverse="_inverse_comparison_mode")
  197. date_range_id = fields.Many2one(
  198. comodel_name='date.range',
  199. string='Date Range')
  200. date_from = fields.Date(string="From")
  201. date_to = fields.Date(string="To")
  202. temporary = fields.Boolean(default=False)
  203. @api.multi
  204. def save_report(self):
  205. self.ensure_one()
  206. self.write({'temporary': False})
  207. action = self.env.ref('mis_builder.mis_report_instance_view_action')
  208. res = action.read()[0]
  209. view = self.env.ref('mis_builder.mis_report_instance_view_form')
  210. res.update({
  211. 'views': [(view.id, 'form')],
  212. 'res_id': self.id,
  213. })
  214. return res
  215. @api.model
  216. def _vacuum_report(self, hours=24):
  217. clear_date = fields.Datetime.to_string(
  218. datetime.datetime.now() - datetime.timedelta(hours=hours))
  219. reports = self.search([
  220. ('write_date', '<', clear_date),
  221. ('temporary', '=', True),
  222. ])
  223. _logger.debug('Vacuum %s Temporary MIS Builder Report', len(reports))
  224. return reports.unlink()
  225. @api.one
  226. def copy(self, default=None):
  227. default = dict(default or {})
  228. default['name'] = _('%s (copy)') % self.name
  229. return super(MisReportInstance, self).copy(default)
  230. def _format_date(self, date):
  231. # format date following user language
  232. lang_model = self.env['res.lang']
  233. lang_id = lang_model._lang_get(self.env.user.lang)
  234. date_format = lang_model.browse(lang_id).date_format
  235. return datetime.datetime.strftime(
  236. fields.Date.from_string(date), date_format)
  237. @api.multi
  238. @api.depends('date_from')
  239. def _compute_comparison_mode(self):
  240. for instance in self:
  241. instance.comparison_mode = bool(instance.period_ids) and\
  242. not bool(instance.date_from)
  243. @api.multi
  244. def _inverse_comparison_mode(self):
  245. for record in self:
  246. if not record.comparison_mode:
  247. if not record.date_from:
  248. record.date_from = datetime.now()
  249. if not record.date_to:
  250. record.date_to = datetime.now()
  251. record.period_ids.unlink()
  252. record.write({'period_ids': [
  253. (0, 0, {
  254. 'name': 'Default',
  255. 'type': 'd',
  256. })
  257. ]})
  258. else:
  259. record.date_from = None
  260. record.date_to = None
  261. @api.onchange('date_range_id')
  262. def onchange_date_range(self):
  263. for record in self:
  264. record.date_from = record.date_range_id.date_start
  265. record.date_to = record.date_range_id.date_end
  266. @api.multi
  267. def preview(self):
  268. assert len(self) == 1
  269. view_id = self.env.ref('mis_builder.'
  270. 'mis_report_instance_result_view_form')
  271. return {
  272. 'type': 'ir.actions.act_window',
  273. 'res_model': 'mis.report.instance',
  274. 'res_id': self.id,
  275. 'view_mode': 'form',
  276. 'view_type': 'form',
  277. 'view_id': view_id.id,
  278. 'target': 'current',
  279. }
  280. @api.multi
  281. def print_pdf(self):
  282. self.ensure_one()
  283. return {
  284. 'name': 'MIS report instance QWEB PDF report',
  285. 'model': 'mis.report.instance',
  286. 'type': 'ir.actions.report.xml',
  287. 'report_name': 'mis_builder.report_mis_report_instance',
  288. 'report_type': 'qweb-pdf',
  289. 'context': self.env.context,
  290. }
  291. @api.multi
  292. def export_xls(self):
  293. self.ensure_one()
  294. return {
  295. 'name': 'MIS report instance XLSX report',
  296. 'model': 'mis.report.instance',
  297. 'type': 'ir.actions.report.xml',
  298. 'report_name': 'mis.report.instance.xlsx',
  299. 'report_type': 'xlsx',
  300. 'context': self.env.context,
  301. }
  302. @api.multi
  303. def display_settings(self):
  304. assert len(self.ids) <= 1
  305. view_id = self.env.ref('mis_builder.mis_report_instance_view_form')
  306. return {
  307. 'type': 'ir.actions.act_window',
  308. 'res_model': 'mis.report.instance',
  309. 'res_id': self.id if self.id else False,
  310. 'view_mode': 'form',
  311. 'view_type': 'form',
  312. 'views': [(view_id.id, 'form')],
  313. 'view_id': view_id.id,
  314. 'target': 'current',
  315. }
  316. @api.multi
  317. def _compute_matrix(self):
  318. self.ensure_one()
  319. aep = self.report_id._prepare_aep(self.company_id)
  320. kpi_matrix = self.report_id._prepare_kpi_matrix()
  321. for period in self.period_ids:
  322. if period.date_from == period.date_to:
  323. comment = self._format_date(period.date_from)
  324. else:
  325. date_from = self._format_date(period.date_from)
  326. date_to = self._format_date(period.date_to)
  327. comment = _('from %s to %s') % (date_from, date_to)
  328. self.report_id._declare_and_compute_period(
  329. kpi_matrix,
  330. period.id,
  331. period.name,
  332. comment,
  333. aep,
  334. period.date_from,
  335. period.date_to,
  336. self.target_move,
  337. self.company_id,
  338. period.subkpi_ids,
  339. period._get_additional_move_line_filter,
  340. period._get_additional_query_filter)
  341. for comparison_column in period.comparison_column_ids:
  342. kpi_matrix.declare_comparison(period.id, comparison_column.id)
  343. kpi_matrix.compute_comparisons()
  344. return kpi_matrix
  345. @api.multi
  346. def compute(self):
  347. self.ensure_one()
  348. kpi_matrix = self._compute_matrix()
  349. return kpi_matrix.as_dict()
  350. @api.multi
  351. def drilldown(self, arg):
  352. self.ensure_one()
  353. period_id = arg.get('period_id')
  354. expr = arg.get('expr')
  355. account_id = arg.get('account_id')
  356. if period_id and expr and AEP.has_account_var(expr):
  357. period = self.env['mis.report.instance.period'].browse(period_id)
  358. aep = AEP(self.env)
  359. aep.parse_expr(expr)
  360. aep.done_parsing(self.company_id)
  361. domain = aep.get_aml_domain_for_expr(
  362. expr,
  363. period.date_from, period.date_to,
  364. self.target_move,
  365. self.company_id,
  366. account_id)
  367. domain.extend(period._get_additional_move_line_filter())
  368. return {
  369. 'name': u'{} - {}'.format(expr, period.name),
  370. 'domain': domain,
  371. 'type': 'ir.actions.act_window',
  372. 'res_model': 'account.move.line',
  373. 'views': [[False, 'list'], [False, 'form']],
  374. 'view_type': 'list',
  375. 'view_mode': 'list',
  376. 'target': 'current',
  377. }
  378. else:
  379. return False