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.

125 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="all",
  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(
  30. comodel_name="res.partner", string="Filter partners",
  31. )
  32. show_move_line_details = fields.Boolean()
  33. @api.onchange("company_id")
  34. def onchange_company_id(self):
  35. """Handle company change."""
  36. if self.company_id and self.partner_ids:
  37. self.partner_ids = self.partner_ids.filtered(
  38. lambda p: p.company_id == self.company_id or not p.company_id
  39. )
  40. if self.company_id and self.account_ids:
  41. if self.receivable_accounts_only or self.payable_accounts_only:
  42. self.onchange_type_accounts_only()
  43. else:
  44. self.account_ids = self.account_ids.filtered(
  45. lambda a: a.company_id == self.company_id
  46. )
  47. res = {"domain": {"account_ids": [], "partner_ids": []}}
  48. if not self.company_id:
  49. return res
  50. else:
  51. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  52. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  53. return res
  54. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  55. def onchange_type_accounts_only(self):
  56. """Handle receivable/payable accounts only change."""
  57. domain = [("company_id", "=", self.company_id.id)]
  58. if self.receivable_accounts_only or self.payable_accounts_only:
  59. if self.receivable_accounts_only and self.payable_accounts_only:
  60. domain += [("internal_type", "in", ("receivable", "payable"))]
  61. elif self.receivable_accounts_only:
  62. domain += [("internal_type", "=", "receivable")]
  63. elif self.payable_accounts_only:
  64. domain += [("internal_type", "=", "payable")]
  65. elif not self.receivable_accounts_only and not self.payable_accounts_only:
  66. domain += [("reconcile", "=", True)]
  67. self.account_ids = self.env["account.account"].search(domain)
  68. @api.multi
  69. def _print_report(self, report_type):
  70. self.ensure_one()
  71. data = self._prepare_report_aged_partner_balance()
  72. if report_type == "xlsx":
  73. report_name = "a_f_r.report_aged_partner_balance_xlsx"
  74. else:
  75. report_name = "account_financial_report.aged_partner_balance"
  76. return (
  77. self.env["ir.actions.report"]
  78. .search(
  79. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  80. limit=1,
  81. )
  82. .report_action(self, data=data)
  83. )
  84. @api.multi
  85. def button_export_html(self):
  86. self.ensure_one()
  87. report_type = "qweb-html"
  88. return self._export(report_type)
  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. "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)