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.

133 lines
4.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Yannick Vaucher (Camptocamp)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import models, fields, api
  5. class FinancialReportLine(models.Model):
  6. _inherit = 'financial.report.line'
  7. _name = 'general.ledger.line'
  8. _description = "General Ledger report line"
  9. _auto = False
  10. _order = 'account_id, date'
  11. @api.depends('invoice_number', 'name')
  12. def _get_label(self):
  13. for rec in self:
  14. label = rec.name
  15. if rec.invoice_number:
  16. label += u' ({})'.format(rec.invoice_number)
  17. rec.label = label
  18. label = fields.Char(compute='_get_label', readonly=True, store=False)
  19. class GeneralLedgerReport(models.TransientModel):
  20. _name = 'report.account.report_generalledger_qweb'
  21. _inherit = 'account.report.common'
  22. @api.multi
  23. def _get_account_ids(self):
  24. res = False
  25. context = self.env.context
  26. if (context.get('active_model') == 'account.account' and
  27. context.get('active_ids')):
  28. res = context['active_ids']
  29. return res
  30. name = fields.Char()
  31. initial_balance = fields.Integer()
  32. account_ids = fields.Many2many(
  33. 'account.account',
  34. string='Filter on accounts',
  35. default=_get_account_ids,
  36. help="Only selected accounts will be printed. Leave empty to "
  37. "print all accounts.")
  38. journal_ids = fields.Many2many(
  39. 'account.journal',
  40. string='Filter on jourvals',
  41. help="Only selected journals will be printed. Leave empty to "
  42. "print all journals.")
  43. balance_mode = fields.Selection(
  44. [('initial_balance', 'Initial balance'),
  45. ('opening_balance', 'Opening balance')]
  46. )
  47. display_account = fields.Char()
  48. display_ledger_lines = fields.Boolean()
  49. display_initial_balance = fields.Boolean()
  50. MAPPING = {
  51. 'date_from': 'start_date',
  52. 'date_to': 'end_date',
  53. }
  54. @api.model
  55. def _get_values_from_wizard(self, data):
  56. """ Get values from wizard """
  57. values = {}
  58. for key, val in data.iteritems():
  59. if key in self.MAPPING:
  60. values[self.MAPPING[key]] = val
  61. elif key == 'fiscalyear':
  62. if val:
  63. values[key] = val[0]
  64. elif key == 'journal_ids':
  65. if val:
  66. values[key] = [(6, 0, val)]
  67. else:
  68. values[key] = val
  69. return values
  70. @api.multi
  71. def _get_centralized_move_ids(self, domain):
  72. """ Get last line of each selected centralized accounts """
  73. # inverse search on centralized boolean to finish the search to get the
  74. # ids of last lines of centralized accounts
  75. # XXX USE DISTINCT to speed up ?
  76. domain = domain[:]
  77. centralize_index = domain.index(('centralized', '=', False))
  78. domain[centralize_index] = ('centralized', '=', True)
  79. gl_lines = self.env['general.ledger.line'].search(domain)
  80. accounts = gl_lines.mapped('account_id')
  81. line_ids = []
  82. for acc in accounts:
  83. acc_lines = gl_lines.filtered(lambda rec: rec.account_id == acc)
  84. line_ids.append(acc_lines[-1].id)
  85. return line_ids
  86. @api.multi
  87. def _get_moves_from_dates(self):
  88. domain = self._get_moves_from_dates_domain()
  89. if self.centralize:
  90. centralized_ids = self._get_centralized_move_ids(domain)
  91. if centralized_ids:
  92. domain.insert(0, '|')
  93. domain.append(('id', 'in', centralized_ids))
  94. return self.env['general.ledger.line'].search(domain)
  95. @api.multi
  96. def render_html(self, data=None):
  97. report_name = 'account.report_generalledger_qweb'
  98. if data is None:
  99. return
  100. values = self._get_values_from_wizard(data['form'])
  101. report = self.create(values)
  102. report_lines = report._get_moves_from_dates()
  103. # TODO warning if no report_lines
  104. self.env['report']._get_report_from_name(report_name)
  105. docargs = {
  106. 'doc_ids': report.ids,
  107. 'doc_model': self._name,
  108. 'report_lines': report_lines,
  109. 'docs': report,
  110. # XXX
  111. 'has_currency': True
  112. }
  113. return self.env['report'].render(report_name, docargs)