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.

128 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. 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. '|', ('company_id', '=', self.company_id.id),
  58. ('company_id', '=', False)]
  59. return res
  60. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  61. def onchange_type_accounts_only(self):
  62. """Handle receivable/payable accounts only change."""
  63. if self.receivable_accounts_only or self.payable_accounts_only:
  64. domain = [('company_id', '=', self.company_id.id)]
  65. if self.receivable_accounts_only and self.payable_accounts_only:
  66. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  67. elif self.receivable_accounts_only:
  68. domain += [('internal_type', '=', 'receivable')]
  69. elif self.payable_accounts_only:
  70. domain += [('internal_type', '=', 'payable')]
  71. self.account_ids = self.env['account.account'].search(domain)
  72. else:
  73. self.account_ids = None
  74. @api.multi
  75. def button_export_html(self):
  76. self.ensure_one()
  77. action = self.env.ref(
  78. 'account_financial_report.action_report_aged_partner_balance')
  79. vals = action.read()[0]
  80. context1 = vals.get('context', {})
  81. if isinstance(context1, pycompat.string_types):
  82. context1 = safe_eval(context1)
  83. model = self.env['report_aged_partner_balance']
  84. report = model.create(self._prepare_report_aged_partner_balance())
  85. report.compute_data_for_report()
  86. context1['active_id'] = report.id
  87. context1['active_ids'] = report.ids
  88. vals['context'] = context1
  89. return vals
  90. @api.multi
  91. def button_export_pdf(self):
  92. self.ensure_one()
  93. report_type = 'qweb-pdf'
  94. return self._export(report_type)
  95. @api.multi
  96. def button_export_xlsx(self):
  97. self.ensure_one()
  98. report_type = 'xlsx'
  99. return self._export(report_type)
  100. def _prepare_report_aged_partner_balance(self):
  101. self.ensure_one()
  102. return {
  103. 'date_at': self.date_at,
  104. 'only_posted_moves': self.target_move == 'posted',
  105. 'company_id': self.company_id.id,
  106. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  107. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  108. 'show_move_line_details': self.show_move_line_details,
  109. }
  110. def _export(self, report_type):
  111. """Default export is PDF."""
  112. model = self.env['report_aged_partner_balance']
  113. report = model.create(self._prepare_report_aged_partner_balance())
  114. report.compute_data_for_report()
  115. return report.print_report(report_type)