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.

130 lines
5.0 KiB

  1. # Author: Damien Crier, Andrea Stirpe, Kevin Graveman, Dennis Sluijk
  2. # Author: Julien Coux
  3. # Copyright 2016 Camptocamp SA, Onestein B.V.
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import api, fields, models
  6. from odoo.tools.safe_eval import safe_eval
  7. from odoo.tools import pycompat
  8. class AgedPartnerBalanceWizard(models.TransientModel):
  9. """Aged partner balance report wizard."""
  10. _name = 'aged.partner.balance.wizard'
  11. _description = 'Aged Partner Balance Wizard'
  12. company_id = fields.Many2one(
  13. comodel_name='res.company',
  14. default=lambda self: self.env.user.company_id,
  15. required=False,
  16. string='Company'
  17. )
  18. date_at = fields.Date(required=True,
  19. default=fields.Date.context_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. )
  29. receivable_accounts_only = fields.Boolean()
  30. payable_accounts_only = fields.Boolean()
  31. partner_ids = fields.Many2many(
  32. comodel_name='res.partner',
  33. string='Filter partners',
  34. )
  35. show_move_line_details = fields.Boolean()
  36. @api.onchange('company_id')
  37. def onchange_company_id(self):
  38. """Handle company change."""
  39. if self.company_id and self.partner_ids:
  40. self.partner_ids = self.partner_ids.filtered(
  41. lambda p: p.company_id == self.company_id or
  42. not p.company_id)
  43. if self.company_id and self.account_ids:
  44. if self.receivable_accounts_only or self.payable_accounts_only:
  45. self.onchange_type_accounts_only()
  46. else:
  47. self.account_ids = self.account_ids.filtered(
  48. lambda a: a.company_id == self.company_id)
  49. res = {'domain': {'account_ids': [],
  50. 'partner_ids': []}}
  51. if not self.company_id:
  52. return res
  53. else:
  54. res['domain']['account_ids'] += [
  55. ('company_id', '=', self.company_id.id)]
  56. res['domain']['partner_ids'] += [
  57. '&',
  58. '|', ('company_id', '=', self.company_id.id),
  59. ('company_id', '=', False),
  60. ('parent_id', '=', False)]
  61. return res
  62. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  63. def onchange_type_accounts_only(self):
  64. """Handle receivable/payable accounts only change."""
  65. if self.receivable_accounts_only or self.payable_accounts_only:
  66. domain = [('company_id', '=', self.company_id.id)]
  67. if self.receivable_accounts_only and self.payable_accounts_only:
  68. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  69. elif self.receivable_accounts_only:
  70. domain += [('internal_type', '=', 'receivable')]
  71. elif self.payable_accounts_only:
  72. domain += [('internal_type', '=', 'payable')]
  73. self.account_ids = self.env['account.account'].search(domain)
  74. else:
  75. self.account_ids = None
  76. @api.multi
  77. def button_export_html(self):
  78. self.ensure_one()
  79. action = self.env.ref(
  80. 'account_financial_report.action_report_aged_partner_balance')
  81. vals = action.read()[0]
  82. context1 = vals.get('context', {})
  83. if isinstance(context1, pycompat.string_types):
  84. context1 = safe_eval(context1)
  85. model = self.env['report_aged_partner_balance']
  86. report = model.create(self._prepare_report_aged_partner_balance())
  87. report.compute_data_for_report()
  88. context1['active_id'] = report.id
  89. context1['active_ids'] = report.ids
  90. vals['context'] = context1
  91. return vals
  92. @api.multi
  93. def button_export_pdf(self):
  94. self.ensure_one()
  95. report_type = 'qweb-pdf'
  96. return self._export(report_type)
  97. @api.multi
  98. def button_export_xlsx(self):
  99. self.ensure_one()
  100. report_type = 'xlsx'
  101. return self._export(report_type)
  102. def _prepare_report_aged_partner_balance(self):
  103. self.ensure_one()
  104. return {
  105. 'date_at': self.date_at,
  106. 'only_posted_moves': self.target_move == 'posted',
  107. 'company_id': self.company_id.id,
  108. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  109. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  110. 'show_move_line_details': self.show_move_line_details,
  111. }
  112. def _export(self, report_type):
  113. """Default export is PDF."""
  114. model = self.env['report_aged_partner_balance']
  115. report = model.create(self._prepare_report_aged_partner_balance())
  116. report.compute_data_for_report()
  117. return report.print_report(report_type)