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.

160 lines
4.9 KiB

  1. # Copyright 2017 ACSONE SA/NV
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models, _
  4. from odoo.tools.safe_eval import safe_eval
  5. from odoo.tools import pycompat
  6. class JournalLedgerReportWizard(models.TransientModel):
  7. """Journal Ledger report wizard."""
  8. _name = 'journal.ledger.report.wizard'
  9. _description = "Journal Ledger Report Wizard"
  10. company_id = fields.Many2one(
  11. comodel_name='res.company',
  12. default=lambda self: self.env.user.company_id,
  13. string='Company',
  14. required=False,
  15. ondelete='cascade',
  16. )
  17. date_range_id = fields.Many2one(
  18. comodel_name='date.range',
  19. string='Date range',
  20. )
  21. date_from = fields.Date(
  22. string="Start date",
  23. required=True
  24. )
  25. date_to = fields.Date(
  26. string="End date",
  27. required=True
  28. )
  29. journal_ids = fields.Many2many(
  30. comodel_name='account.journal',
  31. string="Journals",
  32. required=False,
  33. )
  34. move_target = fields.Selection(
  35. selection='_get_move_targets',
  36. default='all',
  37. required=True,
  38. )
  39. foreign_currency = fields.Boolean()
  40. sort_option = fields.Selection(
  41. selection='_get_sort_options',
  42. string="Sort entries by",
  43. default='move_name',
  44. required=True,
  45. )
  46. group_option = fields.Selection(
  47. selection='_get_group_options',
  48. string="Group entries by",
  49. default='journal',
  50. required=True,
  51. )
  52. with_account_name = fields.Boolean(
  53. default=False,
  54. )
  55. @api.model
  56. def _get_move_targets(self):
  57. return [
  58. ('all', _("All")),
  59. ('posted', _("Posted")),
  60. ('draft', _("Not Posted"))
  61. ]
  62. @api.model
  63. def _get_sort_options(self):
  64. return [
  65. ('move_name', _("Entry number")),
  66. ('date', _("Date")),
  67. ]
  68. @api.model
  69. def _get_group_options(self):
  70. return [
  71. ('journal', _("Journal")),
  72. ('none', _("No group")),
  73. ]
  74. @api.onchange('date_range_id')
  75. def onchange_date_range_id(self):
  76. self.date_from = self.date_range_id.date_start
  77. self.date_to = self.date_range_id.date_end
  78. @api.onchange('company_id')
  79. def onchange_company_id(self):
  80. """Handle company change."""
  81. if self.company_id and self.date_range_id.company_id and \
  82. self.date_range_id.company_id != self.company_id:
  83. self.date_range_id = False
  84. if self.company_id and self.journal_ids:
  85. self.journal_ids = self.journal_ids.filtered(
  86. lambda p: p.company_id == self.company_id or not p.company_id)
  87. res = {'domain': {'journal_ids': []}}
  88. if not self.company_id:
  89. return res
  90. else:
  91. res['domain']['journal_ids'] += [
  92. ('company_id', '=', self.company_id.id)]
  93. return res
  94. @api.multi
  95. def button_export_html(self):
  96. self.ensure_one()
  97. action = self.env.ref(
  98. 'account_financial_report.action_report_journal_ledger')
  99. vals = action.read()[0]
  100. context1 = vals.get('context', {})
  101. if isinstance(context1, pycompat.string_types):
  102. context1 = safe_eval(context1)
  103. model = self.env['report_journal_ledger']
  104. report = model.create(self._prepare_report_journal_ledger())
  105. report.compute_data_for_report()
  106. context1['active_id'] = report.id
  107. context1['active_ids'] = report.ids
  108. vals['context'] = context1
  109. return vals
  110. @api.multi
  111. def button_export_pdf(self):
  112. self.ensure_one()
  113. report_type = 'qweb-pdf'
  114. return self._export(report_type)
  115. @api.multi
  116. def button_export_xlsx(self):
  117. self.ensure_one()
  118. report_type = 'xlsx'
  119. return self._export(report_type)
  120. @api.multi
  121. def _prepare_report_journal_ledger(self):
  122. self.ensure_one()
  123. journals = self.journal_ids
  124. if not journals:
  125. # Not selecting a journal means that we'll display all journals
  126. journals = self.env['account.journal'].search(
  127. [('company_id', '=', self.company_id.id)])
  128. return {
  129. 'date_from': self.date_from,
  130. 'date_to': self.date_to,
  131. 'move_target': self.move_target,
  132. 'foreign_currency': self.foreign_currency,
  133. 'company_id': self.company_id.id,
  134. 'journal_ids': [(6, 0, journals.ids)],
  135. 'sort_option': self.sort_option,
  136. 'group_option': self.group_option,
  137. 'with_account_name': self.with_account_name,
  138. }
  139. def _export(self, report_type):
  140. """Default export is PDF."""
  141. self.ensure_one()
  142. model = self.env['report_journal_ledger']
  143. report = model.create(self._prepare_report_journal_ledger())
  144. report.compute_data_for_report()
  145. return report.print_report(report_type)