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.

146 lines
5.6 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. _inherit = 'account_financial_report_abstract_wizard'
  13. company_id = fields.Many2one(
  14. comodel_name='res.company',
  15. default=lambda self: self.env.user.company_id,
  16. required=False,
  17. string='Company'
  18. )
  19. date_at = fields.Date(required=True,
  20. default=fields.Date.context_today)
  21. target_move = fields.Selection([('posted', 'All Posted Entries'),
  22. ('all', 'All Entries')],
  23. string='Target Moves',
  24. required=True,
  25. default='all')
  26. account_ids = fields.Many2many(
  27. comodel_name='account.account',
  28. string='Filter accounts',
  29. domain=[('reconcile', '=', True)],
  30. )
  31. hide_account_at_0 = fields.Boolean(
  32. string='Hide account ending balance at 0', default=True,
  33. help='Use this filter to hide an account or a partner '
  34. 'with an ending balance at 0. '
  35. 'If partners are filtered, '
  36. 'debits and credits totals will not match the trial balance.'
  37. )
  38. receivable_accounts_only = fields.Boolean()
  39. payable_accounts_only = fields.Boolean()
  40. partner_ids = fields.Many2many(
  41. comodel_name='res.partner',
  42. string='Filter partners',
  43. default=lambda self: self._default_partners(),
  44. )
  45. foreign_currency = fields.Boolean(
  46. string='Show foreign currency',
  47. help='Display foreign currency for move lines, unless '
  48. 'account currency is not setup through chart of accounts '
  49. 'will display initial and final balance in that currency.',
  50. default=lambda self: self._default_foreign_currency(),
  51. )
  52. def _default_foreign_currency(self):
  53. return self.env.user.has_group('base.group_multi_currency')
  54. @api.onchange('company_id')
  55. def onchange_company_id(self):
  56. """Handle company change."""
  57. if self.company_id and self.partner_ids:
  58. self.partner_ids = self.partner_ids.filtered(
  59. lambda p: p.company_id == self.company_id or
  60. not p.company_id)
  61. if self.company_id and self.account_ids:
  62. if self.receivable_accounts_only or self.payable_accounts_only:
  63. self.onchange_type_accounts_only()
  64. else:
  65. self.account_ids = self.account_ids.filtered(
  66. lambda a: a.company_id == self.company_id)
  67. res = {'domain': {'account_ids': [],
  68. 'partner_ids': []}}
  69. if not self.company_id:
  70. return res
  71. else:
  72. res['domain']['account_ids'] += [
  73. ('company_id', '=', self.company_id.id)]
  74. res['domain']['partner_ids'] += self._get_partner_ids_domain()
  75. return res
  76. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  77. def onchange_type_accounts_only(self):
  78. """Handle receivable/payable accounts only change."""
  79. if self.receivable_accounts_only or self.payable_accounts_only:
  80. domain = [('company_id', '=', self.company_id.id)]
  81. if self.receivable_accounts_only and self.payable_accounts_only:
  82. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  83. elif self.receivable_accounts_only:
  84. domain += [('internal_type', '=', 'receivable')]
  85. elif self.payable_accounts_only:
  86. domain += [('internal_type', '=', 'payable')]
  87. self.account_ids = self.env['account.account'].search(domain)
  88. else:
  89. self.account_ids = None
  90. @api.multi
  91. def button_export_html(self):
  92. self.ensure_one()
  93. action = self.env.ref(
  94. 'account_financial_report.action_report_open_items')
  95. vals = action.read()[0]
  96. context1 = vals.get('context', {})
  97. if isinstance(context1, pycompat.string_types):
  98. context1 = safe_eval(context1)
  99. model = self.env['report_open_items']
  100. report = model.create(self._prepare_report_open_items())
  101. report.compute_data_for_report()
  102. context1['active_id'] = report.id
  103. context1['active_ids'] = report.ids
  104. vals['context'] = context1
  105. return vals
  106. @api.multi
  107. def button_export_pdf(self):
  108. self.ensure_one()
  109. report_type = 'qweb-pdf'
  110. return self._export(report_type)
  111. @api.multi
  112. def button_export_xlsx(self):
  113. self.ensure_one()
  114. report_type = 'xlsx'
  115. return self._export(report_type)
  116. def _prepare_report_open_items(self):
  117. self.ensure_one()
  118. return {
  119. 'date_at': self.date_at,
  120. 'only_posted_moves': self.target_move == 'posted',
  121. 'hide_account_at_0': self.hide_account_at_0,
  122. 'foreign_currency': self.foreign_currency,
  123. 'company_id': self.company_id.id,
  124. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  125. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  126. }
  127. def _export(self, report_type):
  128. """Default export is PDF."""
  129. model = self.env['report_open_items']
  130. report = model.create(self._prepare_report_open_items())
  131. report.compute_data_for_report()
  132. return report.print_report(report_type)