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.

126 lines
4.7 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. class AgedPartnerBalanceWizard(models.TransientModel):
  7. """Aged partner balance report wizard."""
  8. _name = "aged.partner.balance.report.wizard"
  9. _description = "Aged Partner Balance Wizard"
  10. _inherit = "account_financial_report_abstract_wizard"
  11. company_id = fields.Many2one(
  12. comodel_name="res.company",
  13. default=lambda self: self.env.user.company_id,
  14. required=False,
  15. string="Company",
  16. )
  17. date_at = fields.Date(required=True, default=fields.Date.context_today)
  18. target_move = fields.Selection(
  19. [("posted", "All Posted Entries"), ("all", "All Entries")],
  20. string="Target Moves",
  21. required=True,
  22. default="posted",
  23. )
  24. account_ids = fields.Many2many(
  25. comodel_name='account.account',
  26. string='Filter accounts',
  27. domain=[('reconcile', '=', True)],
  28. required=True,
  29. )
  30. receivable_accounts_only = fields.Boolean()
  31. payable_accounts_only = fields.Boolean()
  32. partner_ids = fields.Many2many(comodel_name="res.partner", string="Filter partners")
  33. show_move_line_details = fields.Boolean()
  34. @api.onchange("company_id")
  35. def onchange_company_id(self):
  36. """Handle company change."""
  37. if self.company_id and self.partner_ids:
  38. self.partner_ids = self.partner_ids.filtered(
  39. lambda p: p.company_id == self.company_id or not p.company_id
  40. )
  41. if self.company_id and self.account_ids:
  42. if self.receivable_accounts_only or self.payable_accounts_only:
  43. self.onchange_type_accounts_only()
  44. else:
  45. self.account_ids = self.account_ids.filtered(
  46. lambda a: a.company_id == self.company_id
  47. )
  48. res = {"domain": {"account_ids": [], "partner_ids": []}}
  49. if not self.company_id:
  50. return res
  51. else:
  52. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  53. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  54. return res
  55. @api.onchange('account_ids')
  56. def onchange_account_ids(self):
  57. return {'domain': {'account_ids': [('reconcile', '=', True)]}}
  58. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  59. def onchange_type_accounts_only(self):
  60. """Handle receivable/payable accounts only change."""
  61. domain = [("company_id", "=", self.company_id.id)]
  62. if self.receivable_accounts_only or self.payable_accounts_only:
  63. if self.receivable_accounts_only and self.payable_accounts_only:
  64. domain += [("internal_type", "in", ("receivable", "payable"))]
  65. elif self.receivable_accounts_only:
  66. domain += [("internal_type", "=", "receivable")]
  67. elif self.payable_accounts_only:
  68. domain += [('internal_type', '=', 'payable')]
  69. self.account_ids = self.env['account.account'].search(domain)
  70. else:
  71. self.account_ids = None
  72. def _print_report(self, report_type):
  73. self.ensure_one()
  74. data = self._prepare_report_aged_partner_balance()
  75. if report_type == "xlsx":
  76. report_name = "a_f_r.report_aged_partner_balance_xlsx"
  77. else:
  78. report_name = "account_financial_report.aged_partner_balance"
  79. return (
  80. self.env["ir.actions.report"]
  81. .search(
  82. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  83. limit=1,
  84. )
  85. .report_action(self, data=data)
  86. )
  87. def button_export_html(self):
  88. self.ensure_one()
  89. report_type = "qweb-html"
  90. return self._export(report_type)
  91. def button_export_pdf(self):
  92. self.ensure_one()
  93. report_type = "qweb-pdf"
  94. return self._export(report_type)
  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. "wizard_id": self.id,
  103. "date_at": self.date_at,
  104. "only_posted_moves": self.target_move == "posted",
  105. "company_id": self.company_id.id,
  106. "account_ids": self.account_ids.ids,
  107. "partner_ids": 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. return self._print_report(report_type)