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. # -*- coding: utf-8 -*-
  2. # Author: Damien Crier, Andrea Stirpe, Kevin Graveman, Dennis Sluijk
  3. # Author: Julien Coux
  4. # Copyright 2016 Camptocamp SA, Onestein B.V.
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from odoo import api, fields, models
  7. from odoo.tools.safe_eval import safe_eval
  8. class AgedPartnerBalance(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. ]
  62. return res
  63. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  64. def onchange_type_accounts_only(self):
  65. """Handle receivable/payable accounts only change."""
  66. if self.receivable_accounts_only or self.payable_accounts_only:
  67. domain = [('company_id', '=', self.company_id.id)]
  68. if self.receivable_accounts_only and self.payable_accounts_only:
  69. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  70. elif self.receivable_accounts_only:
  71. domain += [('internal_type', '=', 'receivable')]
  72. elif self.payable_accounts_only:
  73. domain += [('internal_type', '=', 'payable')]
  74. self.account_ids = self.env['account.account'].search(domain)
  75. else:
  76. self.account_ids = None
  77. @api.multi
  78. def button_export_html(self):
  79. self.ensure_one()
  80. action = self.env.ref(
  81. 'account_financial_report_qweb.action_report_aged_partner_balance')
  82. vals = action.read()[0]
  83. context1 = vals.get('context', {})
  84. if isinstance(context1, basestring):
  85. context1 = safe_eval(context1)
  86. model = self.env['report_aged_partner_balance_qweb']
  87. report = model.create(self._prepare_report_aged_partner_balance())
  88. report.compute_data_for_report()
  89. context1['active_id'] = report.id
  90. context1['active_ids'] = report.ids
  91. vals['context'] = context1
  92. return vals
  93. @api.multi
  94. def button_export_pdf(self):
  95. self.ensure_one()
  96. report_type = 'qweb-pdf'
  97. return self._export(report_type)
  98. @api.multi
  99. def button_export_xlsx(self):
  100. self.ensure_one()
  101. report_type = 'xlsx'
  102. return self._export(report_type)
  103. def _prepare_report_aged_partner_balance(self):
  104. self.ensure_one()
  105. return {
  106. 'date_at': self.date_at,
  107. 'only_posted_moves': self.target_move == 'posted',
  108. 'company_id': self.company_id.id,
  109. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  110. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  111. 'show_move_line_details': self.show_move_line_details,
  112. }
  113. def _export(self, report_type):
  114. """Default export is PDF."""
  115. model = self.env['report_aged_partner_balance_qweb']
  116. report = model.create(self._prepare_report_aged_partner_balance())
  117. report.compute_data_for_report()
  118. return report.print_report(report_type)