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.

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