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.

127 lines
4.9 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. _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. )
  30. receivable_accounts_only = fields.Boolean()
  31. payable_accounts_only = fields.Boolean()
  32. partner_ids = fields.Many2many(
  33. comodel_name='res.partner',
  34. string='Filter partners',
  35. )
  36. show_move_line_details = fields.Boolean()
  37. @api.onchange('company_id')
  38. def onchange_company_id(self):
  39. """Handle company change."""
  40. if self.company_id and self.partner_ids:
  41. self.partner_ids = self.partner_ids.filtered(
  42. lambda p: p.company_id == self.company_id or
  43. not p.company_id)
  44. if self.company_id and self.account_ids:
  45. if self.receivable_accounts_only or self.payable_accounts_only:
  46. self.onchange_type_accounts_only()
  47. else:
  48. self.account_ids = self.account_ids.filtered(
  49. lambda a: a.company_id == self.company_id)
  50. res = {'domain': {'account_ids': [],
  51. 'partner_ids': []}}
  52. if not self.company_id:
  53. return res
  54. else:
  55. res['domain']['account_ids'] += [
  56. ('company_id', '=', self.company_id.id)]
  57. res['domain']['partner_ids'] += self._get_partner_ids_domain()
  58. return res
  59. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  60. def onchange_type_accounts_only(self):
  61. """Handle receivable/payable accounts only change."""
  62. if self.receivable_accounts_only or self.payable_accounts_only:
  63. domain = [('company_id', '=', self.company_id.id)]
  64. if self.receivable_accounts_only and self.payable_accounts_only:
  65. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  66. elif self.receivable_accounts_only:
  67. domain += [('internal_type', '=', 'receivable')]
  68. elif self.payable_accounts_only:
  69. domain += [('internal_type', '=', 'payable')]
  70. self.account_ids = self.env['account.account'].search(domain)
  71. else:
  72. self.account_ids = None
  73. @api.multi
  74. def button_export_html(self):
  75. self.ensure_one()
  76. action = self.env.ref(
  77. 'account_financial_report.action_report_aged_partner_balance')
  78. vals = action.read()[0]
  79. context1 = vals.get('context', {})
  80. if isinstance(context1, pycompat.string_types):
  81. context1 = safe_eval(context1)
  82. model = self.env['report_aged_partner_balance']
  83. report = model.create(self._prepare_report_aged_partner_balance())
  84. report.compute_data_for_report()
  85. context1['active_id'] = report.id
  86. context1['active_ids'] = report.ids
  87. vals['context'] = context1
  88. return vals
  89. @api.multi
  90. def button_export_pdf(self):
  91. self.ensure_one()
  92. report_type = 'qweb-pdf'
  93. return self._export(report_type)
  94. @api.multi
  95. def button_export_xlsx(self):
  96. self.ensure_one()
  97. report_type = 'xlsx'
  98. return self._export(report_type)
  99. def _prepare_report_aged_partner_balance(self):
  100. self.ensure_one()
  101. return {
  102. 'date_at': self.date_at,
  103. 'only_posted_moves': self.target_move == 'posted',
  104. 'company_id': self.company_id.id,
  105. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  106. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  107. 'show_move_line_details': self.show_move_line_details,
  108. }
  109. def _export(self, report_type):
  110. """Default export is PDF."""
  111. model = self.env['report_aged_partner_balance']
  112. report = model.create(self._prepare_report_aged_partner_balance())
  113. report.compute_data_for_report()
  114. return report.print_report(report_type)