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.

116 lines
4.3 KiB

  1. # Author: Damien Crier
  2. # Author: Julien Coux
  3. # Copyright 2016 Camptocamp SA
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import models, fields, api
  6. from odoo.tools.safe_eval import safe_eval
  7. from odoo.tools import pycompat
  8. class OpenItemsReportWizard(models.TransientModel):
  9. """Open items report wizard."""
  10. _name = "open.items.report.wizard"
  11. _description = "Open Items Report Wizard"
  12. company_id = fields.Many2one(
  13. comodel_name='res.company',
  14. default=lambda self: self.env.user.company_id,
  15. string='Company'
  16. )
  17. date_at = fields.Date(required=True,
  18. default=fields.Date.context_today)
  19. target_move = fields.Selection([('posted', 'All Posted Entries'),
  20. ('all', 'All Entries')],
  21. string='Target Moves',
  22. required=True,
  23. default='all')
  24. account_ids = fields.Many2many(
  25. comodel_name='account.account',
  26. string='Filter accounts',
  27. domain=[('reconcile', '=', True)],
  28. )
  29. hide_account_at_0 = fields.Boolean(
  30. string='Hide account ending balance at 0', default=True,
  31. help='Use this filter to hide an account or a partner '
  32. 'with an ending balance at 0. '
  33. 'If partners are filtered, '
  34. 'debits and credits totals will not match the trial balance.'
  35. )
  36. receivable_accounts_only = fields.Boolean()
  37. payable_accounts_only = fields.Boolean()
  38. partner_ids = fields.Many2many(
  39. comodel_name='res.partner',
  40. string='Filter partners',
  41. )
  42. foreign_currency = fields.Boolean(
  43. string='Show foreign currency',
  44. help='Display foreign currency for move lines, unless '
  45. 'account currency is not setup through chart of accounts '
  46. 'will display initial and final balance in that currency.'
  47. )
  48. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  49. def onchange_type_accounts_only(self):
  50. """Handle receivable/payable accounts only change."""
  51. if self.receivable_accounts_only or self.payable_accounts_only:
  52. domain = []
  53. if self.receivable_accounts_only and self.payable_accounts_only:
  54. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  55. elif self.receivable_accounts_only:
  56. domain += [('internal_type', '=', 'receivable')]
  57. elif self.payable_accounts_only:
  58. domain += [('internal_type', '=', 'payable')]
  59. self.account_ids = self.env['account.account'].search(domain)
  60. else:
  61. self.account_ids = None
  62. @api.multi
  63. def button_export_html(self):
  64. self.ensure_one()
  65. action = self.env.ref(
  66. 'account_financial_report.action_report_open_items')
  67. vals = action.read()[0]
  68. context1 = vals.get('context', {})
  69. if isinstance(context1, pycompat.string_types):
  70. context1 = safe_eval(context1)
  71. model = self.env['report_open_items']
  72. report = model.create(self._prepare_report_open_items())
  73. report.compute_data_for_report()
  74. context1['active_id'] = report.id
  75. context1['active_ids'] = report.ids
  76. vals['context'] = context1
  77. return vals
  78. @api.multi
  79. def button_export_pdf(self):
  80. self.ensure_one()
  81. report_type = 'qweb-pdf'
  82. return self._export(report_type)
  83. @api.multi
  84. def button_export_xlsx(self):
  85. self.ensure_one()
  86. report_type = 'xlsx'
  87. return self._export(report_type)
  88. def _prepare_report_open_items(self):
  89. self.ensure_one()
  90. return {
  91. 'date_at': self.date_at,
  92. 'only_posted_moves': self.target_move == 'posted',
  93. 'hide_account_at_0': self.hide_account_at_0,
  94. 'foreign_currency': self.foreign_currency,
  95. 'company_id': self.company_id.id,
  96. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  97. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  98. }
  99. def _export(self, report_type):
  100. """Default export is PDF."""
  101. model = self.env['report_open_items']
  102. report = model.create(self._prepare_report_open_items())
  103. report.compute_data_for_report()
  104. return report.print_report(report_type)