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.

119 lines
4.6 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", string="Filter accounts"
  26. )
  27. receivable_accounts_only = fields.Boolean()
  28. payable_accounts_only = fields.Boolean()
  29. partner_ids = fields.Many2many(comodel_name="res.partner", string="Filter partners")
  30. show_move_line_details = fields.Boolean()
  31. @api.onchange("company_id")
  32. def onchange_company_id(self):
  33. """Handle company change."""
  34. if self.company_id and self.partner_ids:
  35. self.partner_ids = self.partner_ids.filtered(
  36. lambda p: p.company_id == self.company_id or not p.company_id
  37. )
  38. if self.company_id and self.account_ids:
  39. if self.receivable_accounts_only or self.payable_accounts_only:
  40. self.onchange_type_accounts_only()
  41. else:
  42. self.account_ids = self.account_ids.filtered(
  43. lambda a: a.company_id == self.company_id
  44. )
  45. res = {"domain": {"account_ids": [], "partner_ids": []}}
  46. if not self.company_id:
  47. return res
  48. else:
  49. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  50. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  51. return res
  52. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  53. def onchange_type_accounts_only(self):
  54. """Handle receivable/payable accounts only change."""
  55. domain = [("company_id", "=", self.company_id.id)]
  56. if self.receivable_accounts_only or self.payable_accounts_only:
  57. if self.receivable_accounts_only and self.payable_accounts_only:
  58. domain += [("internal_type", "in", ("receivable", "payable"))]
  59. elif self.receivable_accounts_only:
  60. domain += [("internal_type", "=", "receivable")]
  61. elif self.payable_accounts_only:
  62. domain += [("internal_type", "=", "payable")]
  63. elif not self.receivable_accounts_only and not self.payable_accounts_only:
  64. domain += [("reconcile", "=", True)]
  65. self.account_ids = self.env["account.account"].search(domain)
  66. def _print_report(self, report_type):
  67. self.ensure_one()
  68. data = self._prepare_report_aged_partner_balance()
  69. if report_type == "xlsx":
  70. report_name = "a_f_r.report_aged_partner_balance_xlsx"
  71. else:
  72. report_name = "account_financial_report.aged_partner_balance"
  73. return (
  74. self.env["ir.actions.report"]
  75. .search(
  76. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  77. limit=1,
  78. )
  79. .report_action(self, data=data)
  80. )
  81. def button_export_html(self):
  82. self.ensure_one()
  83. report_type = "qweb-html"
  84. return self._export(report_type)
  85. def button_export_pdf(self):
  86. self.ensure_one()
  87. report_type = "qweb-pdf"
  88. return self._export(report_type)
  89. def button_export_xlsx(self):
  90. self.ensure_one()
  91. report_type = "xlsx"
  92. return self._export(report_type)
  93. def _prepare_report_aged_partner_balance(self):
  94. self.ensure_one()
  95. return {
  96. "wizard_id": self.id,
  97. "date_at": self.date_at,
  98. "only_posted_moves": self.target_move == "posted",
  99. "company_id": self.company_id.id,
  100. "account_ids": self.account_ids.ids,
  101. "partner_ids": self.partner_ids.ids,
  102. "show_move_line_details": self.show_move_line_details,
  103. }
  104. def _export(self, report_type):
  105. """Default export is PDF."""
  106. return self._print_report(report_type)